vfio_pci_config.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. /*
  2. * VFIO PCI config space virtualization
  3. *
  4. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  5. * Author: Alex Williamson <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Derived from original vfio:
  12. * Copyright 2010 Cisco Systems, Inc. All rights reserved.
  13. * Author: Tom Lyon, [email protected]
  14. */
  15. /*
  16. * This code handles reading and writing of PCI configuration registers.
  17. * This is hairy because we want to allow a lot of flexibility to the
  18. * user driver, but cannot trust it with all of the config fields.
  19. * Tables determine which fields can be read and written, as well as
  20. * which fields are 'virtualized' - special actions and translations to
  21. * make it appear to the user that he has control, when in fact things
  22. * must be negotiated with the underlying OS.
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/pci.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/vfio.h>
  28. #include <linux/slab.h>
  29. #include "vfio_pci_private.h"
  30. #define PCI_CFG_SPACE_SIZE 256
  31. /* Fake capability ID for standard config space */
  32. #define PCI_CAP_ID_BASIC 0
  33. #define is_bar(offset) \
  34. ((offset >= PCI_BASE_ADDRESS_0 && offset < PCI_BASE_ADDRESS_5 + 4) || \
  35. (offset >= PCI_ROM_ADDRESS && offset < PCI_ROM_ADDRESS + 4))
  36. /*
  37. * Lengths of PCI Config Capabilities
  38. * 0: Removed from the user visible capability list
  39. * FF: Variable length
  40. */
  41. static const u8 pci_cap_length[PCI_CAP_ID_MAX + 1] = {
  42. [PCI_CAP_ID_BASIC] = PCI_STD_HEADER_SIZEOF, /* pci config header */
  43. [PCI_CAP_ID_PM] = PCI_PM_SIZEOF,
  44. [PCI_CAP_ID_AGP] = PCI_AGP_SIZEOF,
  45. [PCI_CAP_ID_VPD] = PCI_CAP_VPD_SIZEOF,
  46. [PCI_CAP_ID_SLOTID] = 0, /* bridge - don't care */
  47. [PCI_CAP_ID_MSI] = 0xFF, /* 10, 14, 20, or 24 */
  48. [PCI_CAP_ID_CHSWP] = 0, /* cpci - not yet */
  49. [PCI_CAP_ID_PCIX] = 0xFF, /* 8 or 24 */
  50. [PCI_CAP_ID_HT] = 0xFF, /* hypertransport */
  51. [PCI_CAP_ID_VNDR] = 0xFF, /* variable */
  52. [PCI_CAP_ID_DBG] = 0, /* debug - don't care */
  53. [PCI_CAP_ID_CCRC] = 0, /* cpci - not yet */
  54. [PCI_CAP_ID_SHPC] = 0, /* hotswap - not yet */
  55. [PCI_CAP_ID_SSVID] = 0, /* bridge - don't care */
  56. [PCI_CAP_ID_AGP3] = 0, /* AGP8x - not yet */
  57. [PCI_CAP_ID_SECDEV] = 0, /* secure device not yet */
  58. [PCI_CAP_ID_EXP] = 0xFF, /* 20 or 44 */
  59. [PCI_CAP_ID_MSIX] = PCI_CAP_MSIX_SIZEOF,
  60. [PCI_CAP_ID_SATA] = 0xFF,
  61. [PCI_CAP_ID_AF] = PCI_CAP_AF_SIZEOF,
  62. };
  63. /*
  64. * Lengths of PCIe/PCI-X Extended Config Capabilities
  65. * 0: Removed or masked from the user visible capability list
  66. * FF: Variable length
  67. */
  68. static const u16 pci_ext_cap_length[PCI_EXT_CAP_ID_MAX + 1] = {
  69. [PCI_EXT_CAP_ID_ERR] = PCI_ERR_ROOT_COMMAND,
  70. [PCI_EXT_CAP_ID_VC] = 0xFF,
  71. [PCI_EXT_CAP_ID_DSN] = PCI_EXT_CAP_DSN_SIZEOF,
  72. [PCI_EXT_CAP_ID_PWR] = PCI_EXT_CAP_PWR_SIZEOF,
  73. [PCI_EXT_CAP_ID_RCLD] = 0, /* root only - don't care */
  74. [PCI_EXT_CAP_ID_RCILC] = 0, /* root only - don't care */
  75. [PCI_EXT_CAP_ID_RCEC] = 0, /* root only - don't care */
  76. [PCI_EXT_CAP_ID_MFVC] = 0xFF,
  77. [PCI_EXT_CAP_ID_VC9] = 0xFF, /* same as CAP_ID_VC */
  78. [PCI_EXT_CAP_ID_RCRB] = 0, /* root only - don't care */
  79. [PCI_EXT_CAP_ID_VNDR] = 0xFF,
  80. [PCI_EXT_CAP_ID_CAC] = 0, /* obsolete */
  81. [PCI_EXT_CAP_ID_ACS] = 0xFF,
  82. [PCI_EXT_CAP_ID_ARI] = PCI_EXT_CAP_ARI_SIZEOF,
  83. [PCI_EXT_CAP_ID_ATS] = PCI_EXT_CAP_ATS_SIZEOF,
  84. [PCI_EXT_CAP_ID_SRIOV] = PCI_EXT_CAP_SRIOV_SIZEOF,
  85. [PCI_EXT_CAP_ID_MRIOV] = 0, /* not yet */
  86. [PCI_EXT_CAP_ID_MCAST] = PCI_EXT_CAP_MCAST_ENDPOINT_SIZEOF,
  87. [PCI_EXT_CAP_ID_PRI] = PCI_EXT_CAP_PRI_SIZEOF,
  88. [PCI_EXT_CAP_ID_AMD_XXX] = 0, /* not yet */
  89. [PCI_EXT_CAP_ID_REBAR] = 0xFF,
  90. [PCI_EXT_CAP_ID_DPA] = 0xFF,
  91. [PCI_EXT_CAP_ID_TPH] = 0xFF,
  92. [PCI_EXT_CAP_ID_LTR] = PCI_EXT_CAP_LTR_SIZEOF,
  93. [PCI_EXT_CAP_ID_SECPCI] = 0, /* not yet */
  94. [PCI_EXT_CAP_ID_PMUX] = 0, /* not yet */
  95. [PCI_EXT_CAP_ID_PASID] = 0, /* not yet */
  96. };
  97. /*
  98. * Read/Write Permission Bits - one bit for each bit in capability
  99. * Any field can be read if it exists, but what is read depends on
  100. * whether the field is 'virtualized', or just pass thru to the
  101. * hardware. Any virtualized field is also virtualized for writes.
  102. * Writes are only permitted if they have a 1 bit here.
  103. */
  104. struct perm_bits {
  105. u8 *virt; /* read/write virtual data, not hw */
  106. u8 *write; /* writeable bits */
  107. int (*readfn)(struct vfio_pci_device *vdev, int pos, int count,
  108. struct perm_bits *perm, int offset, __le32 *val);
  109. int (*writefn)(struct vfio_pci_device *vdev, int pos, int count,
  110. struct perm_bits *perm, int offset, __le32 val);
  111. };
  112. #define NO_VIRT 0
  113. #define ALL_VIRT 0xFFFFFFFFU
  114. #define NO_WRITE 0
  115. #define ALL_WRITE 0xFFFFFFFFU
  116. static int vfio_user_config_read(struct pci_dev *pdev, int offset,
  117. __le32 *val, int count)
  118. {
  119. int ret = -EINVAL;
  120. u32 tmp_val = 0;
  121. switch (count) {
  122. case 1:
  123. {
  124. u8 tmp;
  125. ret = pci_user_read_config_byte(pdev, offset, &tmp);
  126. tmp_val = tmp;
  127. break;
  128. }
  129. case 2:
  130. {
  131. u16 tmp;
  132. ret = pci_user_read_config_word(pdev, offset, &tmp);
  133. tmp_val = tmp;
  134. break;
  135. }
  136. case 4:
  137. ret = pci_user_read_config_dword(pdev, offset, &tmp_val);
  138. break;
  139. }
  140. *val = cpu_to_le32(tmp_val);
  141. return pcibios_err_to_errno(ret);
  142. }
  143. static int vfio_user_config_write(struct pci_dev *pdev, int offset,
  144. __le32 val, int count)
  145. {
  146. int ret = -EINVAL;
  147. u32 tmp_val = le32_to_cpu(val);
  148. switch (count) {
  149. case 1:
  150. ret = pci_user_write_config_byte(pdev, offset, tmp_val);
  151. break;
  152. case 2:
  153. ret = pci_user_write_config_word(pdev, offset, tmp_val);
  154. break;
  155. case 4:
  156. ret = pci_user_write_config_dword(pdev, offset, tmp_val);
  157. break;
  158. }
  159. return pcibios_err_to_errno(ret);
  160. }
  161. static int vfio_default_config_read(struct vfio_pci_device *vdev, int pos,
  162. int count, struct perm_bits *perm,
  163. int offset, __le32 *val)
  164. {
  165. __le32 virt = 0;
  166. memcpy(val, vdev->vconfig + pos, count);
  167. memcpy(&virt, perm->virt + offset, count);
  168. /* Any non-virtualized bits? */
  169. if (cpu_to_le32(~0U >> (32 - (count * 8))) != virt) {
  170. struct pci_dev *pdev = vdev->pdev;
  171. __le32 phys_val = 0;
  172. int ret;
  173. ret = vfio_user_config_read(pdev, pos, &phys_val, count);
  174. if (ret)
  175. return ret;
  176. *val = (phys_val & ~virt) | (*val & virt);
  177. }
  178. return count;
  179. }
  180. static int vfio_default_config_write(struct vfio_pci_device *vdev, int pos,
  181. int count, struct perm_bits *perm,
  182. int offset, __le32 val)
  183. {
  184. __le32 virt = 0, write = 0;
  185. memcpy(&write, perm->write + offset, count);
  186. if (!write)
  187. return count; /* drop, no writable bits */
  188. memcpy(&virt, perm->virt + offset, count);
  189. /* Virtualized and writable bits go to vconfig */
  190. if (write & virt) {
  191. __le32 virt_val = 0;
  192. memcpy(&virt_val, vdev->vconfig + pos, count);
  193. virt_val &= ~(write & virt);
  194. virt_val |= (val & (write & virt));
  195. memcpy(vdev->vconfig + pos, &virt_val, count);
  196. }
  197. /* Non-virtualzed and writable bits go to hardware */
  198. if (write & ~virt) {
  199. struct pci_dev *pdev = vdev->pdev;
  200. __le32 phys_val = 0;
  201. int ret;
  202. ret = vfio_user_config_read(pdev, pos, &phys_val, count);
  203. if (ret)
  204. return ret;
  205. phys_val &= ~(write & ~virt);
  206. phys_val |= (val & (write & ~virt));
  207. ret = vfio_user_config_write(pdev, pos, phys_val, count);
  208. if (ret)
  209. return ret;
  210. }
  211. return count;
  212. }
  213. /* Allow direct read from hardware, except for capability next pointer */
  214. static int vfio_direct_config_read(struct vfio_pci_device *vdev, int pos,
  215. int count, struct perm_bits *perm,
  216. int offset, __le32 *val)
  217. {
  218. int ret;
  219. ret = vfio_user_config_read(vdev->pdev, pos, val, count);
  220. if (ret)
  221. return pcibios_err_to_errno(ret);
  222. if (pos >= PCI_CFG_SPACE_SIZE) { /* Extended cap header mangling */
  223. if (offset < 4)
  224. memcpy(val, vdev->vconfig + pos, count);
  225. } else if (pos >= PCI_STD_HEADER_SIZEOF) { /* Std cap mangling */
  226. if (offset == PCI_CAP_LIST_ID && count > 1)
  227. memcpy(val, vdev->vconfig + pos,
  228. min(PCI_CAP_FLAGS, count));
  229. else if (offset == PCI_CAP_LIST_NEXT)
  230. memcpy(val, vdev->vconfig + pos, 1);
  231. }
  232. return count;
  233. }
  234. /* Raw access skips any kind of virtualization */
  235. static int vfio_raw_config_write(struct vfio_pci_device *vdev, int pos,
  236. int count, struct perm_bits *perm,
  237. int offset, __le32 val)
  238. {
  239. int ret;
  240. ret = vfio_user_config_write(vdev->pdev, pos, val, count);
  241. if (ret)
  242. return ret;
  243. return count;
  244. }
  245. static int vfio_raw_config_read(struct vfio_pci_device *vdev, int pos,
  246. int count, struct perm_bits *perm,
  247. int offset, __le32 *val)
  248. {
  249. int ret;
  250. ret = vfio_user_config_read(vdev->pdev, pos, val, count);
  251. if (ret)
  252. return pcibios_err_to_errno(ret);
  253. return count;
  254. }
  255. /* Virt access uses only virtualization */
  256. static int vfio_virt_config_write(struct vfio_pci_device *vdev, int pos,
  257. int count, struct perm_bits *perm,
  258. int offset, __le32 val)
  259. {
  260. memcpy(vdev->vconfig + pos, &val, count);
  261. return count;
  262. }
  263. static int vfio_virt_config_read(struct vfio_pci_device *vdev, int pos,
  264. int count, struct perm_bits *perm,
  265. int offset, __le32 *val)
  266. {
  267. memcpy(val, vdev->vconfig + pos, count);
  268. return count;
  269. }
  270. /* Default capability regions to read-only, no-virtualization */
  271. static struct perm_bits cap_perms[PCI_CAP_ID_MAX + 1] = {
  272. [0 ... PCI_CAP_ID_MAX] = { .readfn = vfio_direct_config_read }
  273. };
  274. static struct perm_bits ecap_perms[PCI_EXT_CAP_ID_MAX + 1] = {
  275. [0 ... PCI_EXT_CAP_ID_MAX] = { .readfn = vfio_direct_config_read }
  276. };
  277. /*
  278. * Default unassigned regions to raw read-write access. Some devices
  279. * require this to function as they hide registers between the gaps in
  280. * config space (be2net). Like MMIO and I/O port registers, we have
  281. * to trust the hardware isolation.
  282. */
  283. static struct perm_bits unassigned_perms = {
  284. .readfn = vfio_raw_config_read,
  285. .writefn = vfio_raw_config_write
  286. };
  287. static struct perm_bits virt_perms = {
  288. .readfn = vfio_virt_config_read,
  289. .writefn = vfio_virt_config_write
  290. };
  291. static void free_perm_bits(struct perm_bits *perm)
  292. {
  293. kfree(perm->virt);
  294. kfree(perm->write);
  295. perm->virt = NULL;
  296. perm->write = NULL;
  297. }
  298. static int alloc_perm_bits(struct perm_bits *perm, int size)
  299. {
  300. /*
  301. * Round up all permission bits to the next dword, this lets us
  302. * ignore whether a read/write exceeds the defined capability
  303. * structure. We can do this because:
  304. * - Standard config space is already dword aligned
  305. * - Capabilities are all dword aligned (bits 0:1 of next reserved)
  306. * - Express capabilities defined as dword aligned
  307. */
  308. size = round_up(size, 4);
  309. /*
  310. * Zero state is
  311. * - All Readable, None Writeable, None Virtualized
  312. */
  313. perm->virt = kzalloc(size, GFP_KERNEL);
  314. perm->write = kzalloc(size, GFP_KERNEL);
  315. if (!perm->virt || !perm->write) {
  316. free_perm_bits(perm);
  317. return -ENOMEM;
  318. }
  319. perm->readfn = vfio_default_config_read;
  320. perm->writefn = vfio_default_config_write;
  321. return 0;
  322. }
  323. /*
  324. * Helper functions for filling in permission tables
  325. */
  326. static inline void p_setb(struct perm_bits *p, int off, u8 virt, u8 write)
  327. {
  328. p->virt[off] = virt;
  329. p->write[off] = write;
  330. }
  331. /* Handle endian-ness - pci and tables are little-endian */
  332. static inline void p_setw(struct perm_bits *p, int off, u16 virt, u16 write)
  333. {
  334. *(__le16 *)(&p->virt[off]) = cpu_to_le16(virt);
  335. *(__le16 *)(&p->write[off]) = cpu_to_le16(write);
  336. }
  337. /* Handle endian-ness - pci and tables are little-endian */
  338. static inline void p_setd(struct perm_bits *p, int off, u32 virt, u32 write)
  339. {
  340. *(__le32 *)(&p->virt[off]) = cpu_to_le32(virt);
  341. *(__le32 *)(&p->write[off]) = cpu_to_le32(write);
  342. }
  343. /*
  344. * Restore the *real* BARs after we detect a FLR or backdoor reset.
  345. * (backdoor = some device specific technique that we didn't catch)
  346. */
  347. static void vfio_bar_restore(struct vfio_pci_device *vdev)
  348. {
  349. struct pci_dev *pdev = vdev->pdev;
  350. u32 *rbar = vdev->rbar;
  351. u16 cmd;
  352. int i;
  353. if (pdev->is_virtfn)
  354. return;
  355. pr_info("%s: %s reset recovery - restoring bars\n",
  356. __func__, dev_name(&pdev->dev));
  357. for (i = PCI_BASE_ADDRESS_0; i <= PCI_BASE_ADDRESS_5; i += 4, rbar++)
  358. pci_user_write_config_dword(pdev, i, *rbar);
  359. pci_user_write_config_dword(pdev, PCI_ROM_ADDRESS, *rbar);
  360. if (vdev->nointx) {
  361. pci_user_read_config_word(pdev, PCI_COMMAND, &cmd);
  362. cmd |= PCI_COMMAND_INTX_DISABLE;
  363. pci_user_write_config_word(pdev, PCI_COMMAND, cmd);
  364. }
  365. }
  366. static __le32 vfio_generate_bar_flags(struct pci_dev *pdev, int bar)
  367. {
  368. unsigned long flags = pci_resource_flags(pdev, bar);
  369. u32 val;
  370. if (flags & IORESOURCE_IO)
  371. return cpu_to_le32(PCI_BASE_ADDRESS_SPACE_IO);
  372. val = PCI_BASE_ADDRESS_SPACE_MEMORY;
  373. if (flags & IORESOURCE_PREFETCH)
  374. val |= PCI_BASE_ADDRESS_MEM_PREFETCH;
  375. if (flags & IORESOURCE_MEM_64)
  376. val |= PCI_BASE_ADDRESS_MEM_TYPE_64;
  377. return cpu_to_le32(val);
  378. }
  379. /*
  380. * Pretend we're hardware and tweak the values of the *virtual* PCI BARs
  381. * to reflect the hardware capabilities. This implements BAR sizing.
  382. */
  383. static void vfio_bar_fixup(struct vfio_pci_device *vdev)
  384. {
  385. struct pci_dev *pdev = vdev->pdev;
  386. int i;
  387. __le32 *bar;
  388. u64 mask;
  389. bar = (__le32 *)&vdev->vconfig[PCI_BASE_ADDRESS_0];
  390. for (i = PCI_STD_RESOURCES; i <= PCI_STD_RESOURCE_END; i++, bar++) {
  391. if (!pci_resource_start(pdev, i)) {
  392. *bar = 0; /* Unmapped by host = unimplemented to user */
  393. continue;
  394. }
  395. mask = ~(pci_resource_len(pdev, i) - 1);
  396. *bar &= cpu_to_le32((u32)mask);
  397. *bar |= vfio_generate_bar_flags(pdev, i);
  398. if (*bar & cpu_to_le32(PCI_BASE_ADDRESS_MEM_TYPE_64)) {
  399. bar++;
  400. *bar &= cpu_to_le32((u32)(mask >> 32));
  401. i++;
  402. }
  403. }
  404. bar = (__le32 *)&vdev->vconfig[PCI_ROM_ADDRESS];
  405. /*
  406. * NB. REGION_INFO will have reported zero size if we weren't able
  407. * to read the ROM, but we still return the actual BAR size here if
  408. * it exists (or the shadow ROM space).
  409. */
  410. if (pci_resource_start(pdev, PCI_ROM_RESOURCE)) {
  411. mask = ~(pci_resource_len(pdev, PCI_ROM_RESOURCE) - 1);
  412. mask |= PCI_ROM_ADDRESS_ENABLE;
  413. *bar &= cpu_to_le32((u32)mask);
  414. } else if (pdev->resource[PCI_ROM_RESOURCE].flags &
  415. IORESOURCE_ROM_SHADOW) {
  416. mask = ~(0x20000 - 1);
  417. mask |= PCI_ROM_ADDRESS_ENABLE;
  418. *bar &= cpu_to_le32((u32)mask);
  419. } else
  420. *bar = 0;
  421. vdev->bardirty = false;
  422. }
  423. static int vfio_basic_config_read(struct vfio_pci_device *vdev, int pos,
  424. int count, struct perm_bits *perm,
  425. int offset, __le32 *val)
  426. {
  427. if (is_bar(offset)) /* pos == offset for basic config */
  428. vfio_bar_fixup(vdev);
  429. count = vfio_default_config_read(vdev, pos, count, perm, offset, val);
  430. /* Mask in virtual memory enable for SR-IOV devices */
  431. if (offset == PCI_COMMAND && vdev->pdev->is_virtfn) {
  432. u16 cmd = le16_to_cpu(*(__le16 *)&vdev->vconfig[PCI_COMMAND]);
  433. u32 tmp_val = le32_to_cpu(*val);
  434. tmp_val |= cmd & PCI_COMMAND_MEMORY;
  435. *val = cpu_to_le32(tmp_val);
  436. }
  437. return count;
  438. }
  439. /* Test whether BARs match the value we think they should contain */
  440. static bool vfio_need_bar_restore(struct vfio_pci_device *vdev)
  441. {
  442. int i = 0, pos = PCI_BASE_ADDRESS_0, ret;
  443. u32 bar;
  444. for (; pos <= PCI_BASE_ADDRESS_5; i++, pos += 4) {
  445. if (vdev->rbar[i]) {
  446. ret = pci_user_read_config_dword(vdev->pdev, pos, &bar);
  447. if (ret || vdev->rbar[i] != bar)
  448. return true;
  449. }
  450. }
  451. return false;
  452. }
  453. static int vfio_basic_config_write(struct vfio_pci_device *vdev, int pos,
  454. int count, struct perm_bits *perm,
  455. int offset, __le32 val)
  456. {
  457. struct pci_dev *pdev = vdev->pdev;
  458. __le16 *virt_cmd;
  459. u16 new_cmd = 0;
  460. int ret;
  461. virt_cmd = (__le16 *)&vdev->vconfig[PCI_COMMAND];
  462. if (offset == PCI_COMMAND) {
  463. bool phys_mem, virt_mem, new_mem, phys_io, virt_io, new_io;
  464. u16 phys_cmd;
  465. ret = pci_user_read_config_word(pdev, PCI_COMMAND, &phys_cmd);
  466. if (ret)
  467. return ret;
  468. new_cmd = le32_to_cpu(val);
  469. phys_mem = !!(phys_cmd & PCI_COMMAND_MEMORY);
  470. virt_mem = !!(le16_to_cpu(*virt_cmd) & PCI_COMMAND_MEMORY);
  471. new_mem = !!(new_cmd & PCI_COMMAND_MEMORY);
  472. phys_io = !!(phys_cmd & PCI_COMMAND_IO);
  473. virt_io = !!(le16_to_cpu(*virt_cmd) & PCI_COMMAND_IO);
  474. new_io = !!(new_cmd & PCI_COMMAND_IO);
  475. /*
  476. * If the user is writing mem/io enable (new_mem/io) and we
  477. * think it's already enabled (virt_mem/io), but the hardware
  478. * shows it disabled (phys_mem/io, then the device has
  479. * undergone some kind of backdoor reset and needs to be
  480. * restored before we allow it to enable the bars.
  481. * SR-IOV devices will trigger this, but we catch them later
  482. */
  483. if ((new_mem && virt_mem && !phys_mem) ||
  484. (new_io && virt_io && !phys_io) ||
  485. vfio_need_bar_restore(vdev))
  486. vfio_bar_restore(vdev);
  487. }
  488. count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
  489. if (count < 0)
  490. return count;
  491. /*
  492. * Save current memory/io enable bits in vconfig to allow for
  493. * the test above next time.
  494. */
  495. if (offset == PCI_COMMAND) {
  496. u16 mask = PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
  497. *virt_cmd &= cpu_to_le16(~mask);
  498. *virt_cmd |= cpu_to_le16(new_cmd & mask);
  499. }
  500. /* Emulate INTx disable */
  501. if (offset >= PCI_COMMAND && offset <= PCI_COMMAND + 1) {
  502. bool virt_intx_disable;
  503. virt_intx_disable = !!(le16_to_cpu(*virt_cmd) &
  504. PCI_COMMAND_INTX_DISABLE);
  505. if (virt_intx_disable && !vdev->virq_disabled) {
  506. vdev->virq_disabled = true;
  507. vfio_pci_intx_mask(vdev);
  508. } else if (!virt_intx_disable && vdev->virq_disabled) {
  509. vdev->virq_disabled = false;
  510. vfio_pci_intx_unmask(vdev);
  511. }
  512. }
  513. if (is_bar(offset))
  514. vdev->bardirty = true;
  515. return count;
  516. }
  517. /* Permissions for the Basic PCI Header */
  518. static int __init init_pci_cap_basic_perm(struct perm_bits *perm)
  519. {
  520. if (alloc_perm_bits(perm, PCI_STD_HEADER_SIZEOF))
  521. return -ENOMEM;
  522. perm->readfn = vfio_basic_config_read;
  523. perm->writefn = vfio_basic_config_write;
  524. /* Virtualized for SR-IOV functions, which just have FFFF */
  525. p_setw(perm, PCI_VENDOR_ID, (u16)ALL_VIRT, NO_WRITE);
  526. p_setw(perm, PCI_DEVICE_ID, (u16)ALL_VIRT, NO_WRITE);
  527. /*
  528. * Virtualize INTx disable, we use it internally for interrupt
  529. * control and can emulate it for non-PCI 2.3 devices.
  530. */
  531. p_setw(perm, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE, (u16)ALL_WRITE);
  532. /* Virtualize capability list, we might want to skip/disable */
  533. p_setw(perm, PCI_STATUS, PCI_STATUS_CAP_LIST, NO_WRITE);
  534. /* No harm to write */
  535. p_setb(perm, PCI_CACHE_LINE_SIZE, NO_VIRT, (u8)ALL_WRITE);
  536. p_setb(perm, PCI_LATENCY_TIMER, NO_VIRT, (u8)ALL_WRITE);
  537. p_setb(perm, PCI_BIST, NO_VIRT, (u8)ALL_WRITE);
  538. /* Virtualize all bars, can't touch the real ones */
  539. p_setd(perm, PCI_BASE_ADDRESS_0, ALL_VIRT, ALL_WRITE);
  540. p_setd(perm, PCI_BASE_ADDRESS_1, ALL_VIRT, ALL_WRITE);
  541. p_setd(perm, PCI_BASE_ADDRESS_2, ALL_VIRT, ALL_WRITE);
  542. p_setd(perm, PCI_BASE_ADDRESS_3, ALL_VIRT, ALL_WRITE);
  543. p_setd(perm, PCI_BASE_ADDRESS_4, ALL_VIRT, ALL_WRITE);
  544. p_setd(perm, PCI_BASE_ADDRESS_5, ALL_VIRT, ALL_WRITE);
  545. p_setd(perm, PCI_ROM_ADDRESS, ALL_VIRT, ALL_WRITE);
  546. /* Allow us to adjust capability chain */
  547. p_setb(perm, PCI_CAPABILITY_LIST, (u8)ALL_VIRT, NO_WRITE);
  548. /* Sometimes used by sw, just virtualize */
  549. p_setb(perm, PCI_INTERRUPT_LINE, (u8)ALL_VIRT, (u8)ALL_WRITE);
  550. /* Virtualize interrupt pin to allow hiding INTx */
  551. p_setb(perm, PCI_INTERRUPT_PIN, (u8)ALL_VIRT, (u8)NO_WRITE);
  552. return 0;
  553. }
  554. static int vfio_pm_config_write(struct vfio_pci_device *vdev, int pos,
  555. int count, struct perm_bits *perm,
  556. int offset, __le32 val)
  557. {
  558. count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
  559. if (count < 0)
  560. return count;
  561. if (offset == PCI_PM_CTRL) {
  562. pci_power_t state;
  563. switch (le32_to_cpu(val) & PCI_PM_CTRL_STATE_MASK) {
  564. case 0:
  565. state = PCI_D0;
  566. break;
  567. case 1:
  568. state = PCI_D1;
  569. break;
  570. case 2:
  571. state = PCI_D2;
  572. break;
  573. case 3:
  574. state = PCI_D3hot;
  575. break;
  576. }
  577. pci_set_power_state(vdev->pdev, state);
  578. }
  579. return count;
  580. }
  581. /* Permissions for the Power Management capability */
  582. static int __init init_pci_cap_pm_perm(struct perm_bits *perm)
  583. {
  584. if (alloc_perm_bits(perm, pci_cap_length[PCI_CAP_ID_PM]))
  585. return -ENOMEM;
  586. perm->writefn = vfio_pm_config_write;
  587. /*
  588. * We always virtualize the next field so we can remove
  589. * capabilities from the chain if we want to.
  590. */
  591. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  592. /*
  593. * Power management is defined *per function*, so we can let
  594. * the user change power state, but we trap and initiate the
  595. * change ourselves, so the state bits are read-only.
  596. */
  597. p_setd(perm, PCI_PM_CTRL, NO_VIRT, ~PCI_PM_CTRL_STATE_MASK);
  598. return 0;
  599. }
  600. static int vfio_vpd_config_write(struct vfio_pci_device *vdev, int pos,
  601. int count, struct perm_bits *perm,
  602. int offset, __le32 val)
  603. {
  604. struct pci_dev *pdev = vdev->pdev;
  605. __le16 *paddr = (__le16 *)(vdev->vconfig + pos - offset + PCI_VPD_ADDR);
  606. __le32 *pdata = (__le32 *)(vdev->vconfig + pos - offset + PCI_VPD_DATA);
  607. u16 addr;
  608. u32 data;
  609. /*
  610. * Write through to emulation. If the write includes the upper byte
  611. * of PCI_VPD_ADDR, then the PCI_VPD_ADDR_F bit is written and we
  612. * have work to do.
  613. */
  614. count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
  615. if (count < 0 || offset > PCI_VPD_ADDR + 1 ||
  616. offset + count <= PCI_VPD_ADDR + 1)
  617. return count;
  618. addr = le16_to_cpu(*paddr);
  619. if (addr & PCI_VPD_ADDR_F) {
  620. data = le32_to_cpu(*pdata);
  621. if (pci_write_vpd(pdev, addr & ~PCI_VPD_ADDR_F, 4, &data) != 4)
  622. return count;
  623. } else {
  624. data = 0;
  625. if (pci_read_vpd(pdev, addr, 4, &data) < 0)
  626. return count;
  627. *pdata = cpu_to_le32(data);
  628. }
  629. /*
  630. * Toggle PCI_VPD_ADDR_F in the emulated PCI_VPD_ADDR register to
  631. * signal completion. If an error occurs above, we assume that not
  632. * toggling this bit will induce a driver timeout.
  633. */
  634. addr ^= PCI_VPD_ADDR_F;
  635. *paddr = cpu_to_le16(addr);
  636. return count;
  637. }
  638. /* Permissions for Vital Product Data capability */
  639. static int __init init_pci_cap_vpd_perm(struct perm_bits *perm)
  640. {
  641. if (alloc_perm_bits(perm, pci_cap_length[PCI_CAP_ID_VPD]))
  642. return -ENOMEM;
  643. perm->writefn = vfio_vpd_config_write;
  644. /*
  645. * We always virtualize the next field so we can remove
  646. * capabilities from the chain if we want to.
  647. */
  648. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  649. /*
  650. * Both the address and data registers are virtualized to
  651. * enable access through the pci_vpd_read/write functions
  652. */
  653. p_setw(perm, PCI_VPD_ADDR, (u16)ALL_VIRT, (u16)ALL_WRITE);
  654. p_setd(perm, PCI_VPD_DATA, ALL_VIRT, ALL_WRITE);
  655. return 0;
  656. }
  657. /* Permissions for PCI-X capability */
  658. static int __init init_pci_cap_pcix_perm(struct perm_bits *perm)
  659. {
  660. /* Alloc 24, but only 8 are used in v0 */
  661. if (alloc_perm_bits(perm, PCI_CAP_PCIX_SIZEOF_V2))
  662. return -ENOMEM;
  663. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  664. p_setw(perm, PCI_X_CMD, NO_VIRT, (u16)ALL_WRITE);
  665. p_setd(perm, PCI_X_ECC_CSR, NO_VIRT, ALL_WRITE);
  666. return 0;
  667. }
  668. static int vfio_exp_config_write(struct vfio_pci_device *vdev, int pos,
  669. int count, struct perm_bits *perm,
  670. int offset, __le32 val)
  671. {
  672. __le16 *ctrl = (__le16 *)(vdev->vconfig + pos -
  673. offset + PCI_EXP_DEVCTL);
  674. int readrq = le16_to_cpu(*ctrl) & PCI_EXP_DEVCTL_READRQ;
  675. count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
  676. if (count < 0)
  677. return count;
  678. /*
  679. * The FLR bit is virtualized, if set and the device supports PCIe
  680. * FLR, issue a reset_function. Regardless, clear the bit, the spec
  681. * requires it to be always read as zero. NB, reset_function might
  682. * not use a PCIe FLR, we don't have that level of granularity.
  683. */
  684. if (*ctrl & cpu_to_le16(PCI_EXP_DEVCTL_BCR_FLR)) {
  685. u32 cap;
  686. int ret;
  687. *ctrl &= ~cpu_to_le16(PCI_EXP_DEVCTL_BCR_FLR);
  688. ret = pci_user_read_config_dword(vdev->pdev,
  689. pos - offset + PCI_EXP_DEVCAP,
  690. &cap);
  691. if (!ret && (cap & PCI_EXP_DEVCAP_FLR))
  692. pci_try_reset_function(vdev->pdev);
  693. }
  694. /*
  695. * MPS is virtualized to the user, writes do not change the physical
  696. * register since determining a proper MPS value requires a system wide
  697. * device view. The MRRS is largely independent of MPS, but since the
  698. * user does not have that system-wide view, they might set a safe, but
  699. * inefficiently low value. Here we allow writes through to hardware,
  700. * but we set the floor to the physical device MPS setting, so that
  701. * we can at least use full TLPs, as defined by the MPS value.
  702. *
  703. * NB, if any devices actually depend on an artificially low MRRS
  704. * setting, this will need to be revisited, perhaps with a quirk
  705. * though pcie_set_readrq().
  706. */
  707. if (readrq != (le16_to_cpu(*ctrl) & PCI_EXP_DEVCTL_READRQ)) {
  708. readrq = 128 <<
  709. ((le16_to_cpu(*ctrl) & PCI_EXP_DEVCTL_READRQ) >> 12);
  710. readrq = max(readrq, pcie_get_mps(vdev->pdev));
  711. pcie_set_readrq(vdev->pdev, readrq);
  712. }
  713. return count;
  714. }
  715. /* Permissions for PCI Express capability */
  716. static int __init init_pci_cap_exp_perm(struct perm_bits *perm)
  717. {
  718. /* Alloc larger of two possible sizes */
  719. if (alloc_perm_bits(perm, PCI_CAP_EXP_ENDPOINT_SIZEOF_V2))
  720. return -ENOMEM;
  721. perm->writefn = vfio_exp_config_write;
  722. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  723. /*
  724. * Allow writes to device control fields, except devctl_phantom,
  725. * which could confuse IOMMU, MPS, which can break communication
  726. * with other physical devices, and the ARI bit in devctl2, which
  727. * is set at probe time. FLR and MRRS get virtualized via our
  728. * writefn.
  729. */
  730. p_setw(perm, PCI_EXP_DEVCTL,
  731. PCI_EXP_DEVCTL_BCR_FLR | PCI_EXP_DEVCTL_PAYLOAD |
  732. PCI_EXP_DEVCTL_READRQ, ~PCI_EXP_DEVCTL_PHANTOM);
  733. p_setw(perm, PCI_EXP_DEVCTL2, NO_VIRT, ~PCI_EXP_DEVCTL2_ARI);
  734. return 0;
  735. }
  736. static int vfio_af_config_write(struct vfio_pci_device *vdev, int pos,
  737. int count, struct perm_bits *perm,
  738. int offset, __le32 val)
  739. {
  740. u8 *ctrl = vdev->vconfig + pos - offset + PCI_AF_CTRL;
  741. count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
  742. if (count < 0)
  743. return count;
  744. /*
  745. * The FLR bit is virtualized, if set and the device supports AF
  746. * FLR, issue a reset_function. Regardless, clear the bit, the spec
  747. * requires it to be always read as zero. NB, reset_function might
  748. * not use an AF FLR, we don't have that level of granularity.
  749. */
  750. if (*ctrl & PCI_AF_CTRL_FLR) {
  751. u8 cap;
  752. int ret;
  753. *ctrl &= ~PCI_AF_CTRL_FLR;
  754. ret = pci_user_read_config_byte(vdev->pdev,
  755. pos - offset + PCI_AF_CAP,
  756. &cap);
  757. if (!ret && (cap & PCI_AF_CAP_FLR) && (cap & PCI_AF_CAP_TP))
  758. pci_try_reset_function(vdev->pdev);
  759. }
  760. return count;
  761. }
  762. /* Permissions for Advanced Function capability */
  763. static int __init init_pci_cap_af_perm(struct perm_bits *perm)
  764. {
  765. if (alloc_perm_bits(perm, pci_cap_length[PCI_CAP_ID_AF]))
  766. return -ENOMEM;
  767. perm->writefn = vfio_af_config_write;
  768. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  769. p_setb(perm, PCI_AF_CTRL, PCI_AF_CTRL_FLR, PCI_AF_CTRL_FLR);
  770. return 0;
  771. }
  772. /* Permissions for Advanced Error Reporting extended capability */
  773. static int __init init_pci_ext_cap_err_perm(struct perm_bits *perm)
  774. {
  775. u32 mask;
  776. if (alloc_perm_bits(perm, pci_ext_cap_length[PCI_EXT_CAP_ID_ERR]))
  777. return -ENOMEM;
  778. /*
  779. * Virtualize the first dword of all express capabilities
  780. * because it includes the next pointer. This lets us later
  781. * remove capabilities from the chain if we need to.
  782. */
  783. p_setd(perm, 0, ALL_VIRT, NO_WRITE);
  784. /* Writable bits mask */
  785. mask = PCI_ERR_UNC_UND | /* Undefined */
  786. PCI_ERR_UNC_DLP | /* Data Link Protocol */
  787. PCI_ERR_UNC_SURPDN | /* Surprise Down */
  788. PCI_ERR_UNC_POISON_TLP | /* Poisoned TLP */
  789. PCI_ERR_UNC_FCP | /* Flow Control Protocol */
  790. PCI_ERR_UNC_COMP_TIME | /* Completion Timeout */
  791. PCI_ERR_UNC_COMP_ABORT | /* Completer Abort */
  792. PCI_ERR_UNC_UNX_COMP | /* Unexpected Completion */
  793. PCI_ERR_UNC_RX_OVER | /* Receiver Overflow */
  794. PCI_ERR_UNC_MALF_TLP | /* Malformed TLP */
  795. PCI_ERR_UNC_ECRC | /* ECRC Error Status */
  796. PCI_ERR_UNC_UNSUP | /* Unsupported Request */
  797. PCI_ERR_UNC_ACSV | /* ACS Violation */
  798. PCI_ERR_UNC_INTN | /* internal error */
  799. PCI_ERR_UNC_MCBTLP | /* MC blocked TLP */
  800. PCI_ERR_UNC_ATOMEG | /* Atomic egress blocked */
  801. PCI_ERR_UNC_TLPPRE; /* TLP prefix blocked */
  802. p_setd(perm, PCI_ERR_UNCOR_STATUS, NO_VIRT, mask);
  803. p_setd(perm, PCI_ERR_UNCOR_MASK, NO_VIRT, mask);
  804. p_setd(perm, PCI_ERR_UNCOR_SEVER, NO_VIRT, mask);
  805. mask = PCI_ERR_COR_RCVR | /* Receiver Error Status */
  806. PCI_ERR_COR_BAD_TLP | /* Bad TLP Status */
  807. PCI_ERR_COR_BAD_DLLP | /* Bad DLLP Status */
  808. PCI_ERR_COR_REP_ROLL | /* REPLAY_NUM Rollover */
  809. PCI_ERR_COR_REP_TIMER | /* Replay Timer Timeout */
  810. PCI_ERR_COR_ADV_NFAT | /* Advisory Non-Fatal */
  811. PCI_ERR_COR_INTERNAL | /* Corrected Internal */
  812. PCI_ERR_COR_LOG_OVER; /* Header Log Overflow */
  813. p_setd(perm, PCI_ERR_COR_STATUS, NO_VIRT, mask);
  814. p_setd(perm, PCI_ERR_COR_MASK, NO_VIRT, mask);
  815. mask = PCI_ERR_CAP_ECRC_GENE | /* ECRC Generation Enable */
  816. PCI_ERR_CAP_ECRC_CHKE; /* ECRC Check Enable */
  817. p_setd(perm, PCI_ERR_CAP, NO_VIRT, mask);
  818. return 0;
  819. }
  820. /* Permissions for Power Budgeting extended capability */
  821. static int __init init_pci_ext_cap_pwr_perm(struct perm_bits *perm)
  822. {
  823. if (alloc_perm_bits(perm, pci_ext_cap_length[PCI_EXT_CAP_ID_PWR]))
  824. return -ENOMEM;
  825. p_setd(perm, 0, ALL_VIRT, NO_WRITE);
  826. /* Writing the data selector is OK, the info is still read-only */
  827. p_setb(perm, PCI_PWR_DATA, NO_VIRT, (u8)ALL_WRITE);
  828. return 0;
  829. }
  830. /*
  831. * Initialize the shared permission tables
  832. */
  833. void vfio_pci_uninit_perm_bits(void)
  834. {
  835. free_perm_bits(&cap_perms[PCI_CAP_ID_BASIC]);
  836. free_perm_bits(&cap_perms[PCI_CAP_ID_PM]);
  837. free_perm_bits(&cap_perms[PCI_CAP_ID_VPD]);
  838. free_perm_bits(&cap_perms[PCI_CAP_ID_PCIX]);
  839. free_perm_bits(&cap_perms[PCI_CAP_ID_EXP]);
  840. free_perm_bits(&cap_perms[PCI_CAP_ID_AF]);
  841. free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_ERR]);
  842. free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
  843. }
  844. int __init vfio_pci_init_perm_bits(void)
  845. {
  846. int ret;
  847. /* Basic config space */
  848. ret = init_pci_cap_basic_perm(&cap_perms[PCI_CAP_ID_BASIC]);
  849. /* Capabilities */
  850. ret |= init_pci_cap_pm_perm(&cap_perms[PCI_CAP_ID_PM]);
  851. ret |= init_pci_cap_vpd_perm(&cap_perms[PCI_CAP_ID_VPD]);
  852. ret |= init_pci_cap_pcix_perm(&cap_perms[PCI_CAP_ID_PCIX]);
  853. cap_perms[PCI_CAP_ID_VNDR].writefn = vfio_raw_config_write;
  854. ret |= init_pci_cap_exp_perm(&cap_perms[PCI_CAP_ID_EXP]);
  855. ret |= init_pci_cap_af_perm(&cap_perms[PCI_CAP_ID_AF]);
  856. /* Extended capabilities */
  857. ret |= init_pci_ext_cap_err_perm(&ecap_perms[PCI_EXT_CAP_ID_ERR]);
  858. ret |= init_pci_ext_cap_pwr_perm(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
  859. ecap_perms[PCI_EXT_CAP_ID_VNDR].writefn = vfio_raw_config_write;
  860. if (ret)
  861. vfio_pci_uninit_perm_bits();
  862. return ret;
  863. }
  864. static int vfio_find_cap_start(struct vfio_pci_device *vdev, int pos)
  865. {
  866. u8 cap;
  867. int base = (pos >= PCI_CFG_SPACE_SIZE) ? PCI_CFG_SPACE_SIZE :
  868. PCI_STD_HEADER_SIZEOF;
  869. cap = vdev->pci_config_map[pos];
  870. if (cap == PCI_CAP_ID_BASIC)
  871. return 0;
  872. /* XXX Can we have to abutting capabilities of the same type? */
  873. while (pos - 1 >= base && vdev->pci_config_map[pos - 1] == cap)
  874. pos--;
  875. return pos;
  876. }
  877. static int vfio_msi_config_read(struct vfio_pci_device *vdev, int pos,
  878. int count, struct perm_bits *perm,
  879. int offset, __le32 *val)
  880. {
  881. /* Update max available queue size from msi_qmax */
  882. if (offset <= PCI_MSI_FLAGS && offset + count >= PCI_MSI_FLAGS) {
  883. __le16 *flags;
  884. int start;
  885. start = vfio_find_cap_start(vdev, pos);
  886. flags = (__le16 *)&vdev->vconfig[start];
  887. *flags &= cpu_to_le16(~PCI_MSI_FLAGS_QMASK);
  888. *flags |= cpu_to_le16(vdev->msi_qmax << 1);
  889. }
  890. return vfio_default_config_read(vdev, pos, count, perm, offset, val);
  891. }
  892. static int vfio_msi_config_write(struct vfio_pci_device *vdev, int pos,
  893. int count, struct perm_bits *perm,
  894. int offset, __le32 val)
  895. {
  896. count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
  897. if (count < 0)
  898. return count;
  899. /* Fixup and write configured queue size and enable to hardware */
  900. if (offset <= PCI_MSI_FLAGS && offset + count >= PCI_MSI_FLAGS) {
  901. __le16 *pflags;
  902. u16 flags;
  903. int start, ret;
  904. start = vfio_find_cap_start(vdev, pos);
  905. pflags = (__le16 *)&vdev->vconfig[start + PCI_MSI_FLAGS];
  906. flags = le16_to_cpu(*pflags);
  907. /* MSI is enabled via ioctl */
  908. if (!is_msi(vdev))
  909. flags &= ~PCI_MSI_FLAGS_ENABLE;
  910. /* Check queue size */
  911. if ((flags & PCI_MSI_FLAGS_QSIZE) >> 4 > vdev->msi_qmax) {
  912. flags &= ~PCI_MSI_FLAGS_QSIZE;
  913. flags |= vdev->msi_qmax << 4;
  914. }
  915. /* Write back to virt and to hardware */
  916. *pflags = cpu_to_le16(flags);
  917. ret = pci_user_write_config_word(vdev->pdev,
  918. start + PCI_MSI_FLAGS,
  919. flags);
  920. if (ret)
  921. return pcibios_err_to_errno(ret);
  922. }
  923. return count;
  924. }
  925. /*
  926. * MSI determination is per-device, so this routine gets used beyond
  927. * initialization time. Don't add __init
  928. */
  929. static int init_pci_cap_msi_perm(struct perm_bits *perm, int len, u16 flags)
  930. {
  931. if (alloc_perm_bits(perm, len))
  932. return -ENOMEM;
  933. perm->readfn = vfio_msi_config_read;
  934. perm->writefn = vfio_msi_config_write;
  935. p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
  936. /*
  937. * The upper byte of the control register is reserved,
  938. * just setup the lower byte.
  939. */
  940. p_setb(perm, PCI_MSI_FLAGS, (u8)ALL_VIRT, (u8)ALL_WRITE);
  941. p_setd(perm, PCI_MSI_ADDRESS_LO, ALL_VIRT, ALL_WRITE);
  942. if (flags & PCI_MSI_FLAGS_64BIT) {
  943. p_setd(perm, PCI_MSI_ADDRESS_HI, ALL_VIRT, ALL_WRITE);
  944. p_setw(perm, PCI_MSI_DATA_64, (u16)ALL_VIRT, (u16)ALL_WRITE);
  945. if (flags & PCI_MSI_FLAGS_MASKBIT) {
  946. p_setd(perm, PCI_MSI_MASK_64, NO_VIRT, ALL_WRITE);
  947. p_setd(perm, PCI_MSI_PENDING_64, NO_VIRT, ALL_WRITE);
  948. }
  949. } else {
  950. p_setw(perm, PCI_MSI_DATA_32, (u16)ALL_VIRT, (u16)ALL_WRITE);
  951. if (flags & PCI_MSI_FLAGS_MASKBIT) {
  952. p_setd(perm, PCI_MSI_MASK_32, NO_VIRT, ALL_WRITE);
  953. p_setd(perm, PCI_MSI_PENDING_32, NO_VIRT, ALL_WRITE);
  954. }
  955. }
  956. return 0;
  957. }
  958. /* Determine MSI CAP field length; initialize msi_perms on 1st call per vdev */
  959. static int vfio_msi_cap_len(struct vfio_pci_device *vdev, u8 pos)
  960. {
  961. struct pci_dev *pdev = vdev->pdev;
  962. int len, ret;
  963. u16 flags;
  964. ret = pci_read_config_word(pdev, pos + PCI_MSI_FLAGS, &flags);
  965. if (ret)
  966. return pcibios_err_to_errno(ret);
  967. len = 10; /* Minimum size */
  968. if (flags & PCI_MSI_FLAGS_64BIT)
  969. len += 4;
  970. if (flags & PCI_MSI_FLAGS_MASKBIT)
  971. len += 10;
  972. if (vdev->msi_perm)
  973. return len;
  974. vdev->msi_perm = kmalloc(sizeof(struct perm_bits), GFP_KERNEL);
  975. if (!vdev->msi_perm)
  976. return -ENOMEM;
  977. ret = init_pci_cap_msi_perm(vdev->msi_perm, len, flags);
  978. if (ret)
  979. return ret;
  980. return len;
  981. }
  982. /* Determine extended capability length for VC (2 & 9) and MFVC */
  983. static int vfio_vc_cap_len(struct vfio_pci_device *vdev, u16 pos)
  984. {
  985. struct pci_dev *pdev = vdev->pdev;
  986. u32 tmp;
  987. int ret, evcc, phases, vc_arb;
  988. int len = PCI_CAP_VC_BASE_SIZEOF;
  989. ret = pci_read_config_dword(pdev, pos + PCI_VC_PORT_CAP1, &tmp);
  990. if (ret)
  991. return pcibios_err_to_errno(ret);
  992. evcc = tmp & PCI_VC_CAP1_EVCC; /* extended vc count */
  993. ret = pci_read_config_dword(pdev, pos + PCI_VC_PORT_CAP2, &tmp);
  994. if (ret)
  995. return pcibios_err_to_errno(ret);
  996. if (tmp & PCI_VC_CAP2_128_PHASE)
  997. phases = 128;
  998. else if (tmp & PCI_VC_CAP2_64_PHASE)
  999. phases = 64;
  1000. else if (tmp & PCI_VC_CAP2_32_PHASE)
  1001. phases = 32;
  1002. else
  1003. phases = 0;
  1004. vc_arb = phases * 4;
  1005. /*
  1006. * Port arbitration tables are root & switch only;
  1007. * function arbitration tables are function 0 only.
  1008. * In either case, we'll never let user write them so
  1009. * we don't care how big they are
  1010. */
  1011. len += (1 + evcc) * PCI_CAP_VC_PER_VC_SIZEOF;
  1012. if (vc_arb) {
  1013. len = round_up(len, 16);
  1014. len += vc_arb / 8;
  1015. }
  1016. return len;
  1017. }
  1018. static int vfio_cap_len(struct vfio_pci_device *vdev, u8 cap, u8 pos)
  1019. {
  1020. struct pci_dev *pdev = vdev->pdev;
  1021. u32 dword;
  1022. u16 word;
  1023. u8 byte;
  1024. int ret;
  1025. switch (cap) {
  1026. case PCI_CAP_ID_MSI:
  1027. return vfio_msi_cap_len(vdev, pos);
  1028. case PCI_CAP_ID_PCIX:
  1029. ret = pci_read_config_word(pdev, pos + PCI_X_CMD, &word);
  1030. if (ret)
  1031. return pcibios_err_to_errno(ret);
  1032. if (PCI_X_CMD_VERSION(word)) {
  1033. if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) {
  1034. /* Test for extended capabilities */
  1035. pci_read_config_dword(pdev, PCI_CFG_SPACE_SIZE,
  1036. &dword);
  1037. vdev->extended_caps = (dword != 0);
  1038. }
  1039. return PCI_CAP_PCIX_SIZEOF_V2;
  1040. } else
  1041. return PCI_CAP_PCIX_SIZEOF_V0;
  1042. case PCI_CAP_ID_VNDR:
  1043. /* length follows next field */
  1044. ret = pci_read_config_byte(pdev, pos + PCI_CAP_FLAGS, &byte);
  1045. if (ret)
  1046. return pcibios_err_to_errno(ret);
  1047. return byte;
  1048. case PCI_CAP_ID_EXP:
  1049. if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) {
  1050. /* Test for extended capabilities */
  1051. pci_read_config_dword(pdev, PCI_CFG_SPACE_SIZE, &dword);
  1052. vdev->extended_caps = (dword != 0);
  1053. }
  1054. /* length based on version */
  1055. if ((pcie_caps_reg(pdev) & PCI_EXP_FLAGS_VERS) == 1)
  1056. return PCI_CAP_EXP_ENDPOINT_SIZEOF_V1;
  1057. else
  1058. return PCI_CAP_EXP_ENDPOINT_SIZEOF_V2;
  1059. case PCI_CAP_ID_HT:
  1060. ret = pci_read_config_byte(pdev, pos + 3, &byte);
  1061. if (ret)
  1062. return pcibios_err_to_errno(ret);
  1063. return (byte & HT_3BIT_CAP_MASK) ?
  1064. HT_CAP_SIZEOF_SHORT : HT_CAP_SIZEOF_LONG;
  1065. case PCI_CAP_ID_SATA:
  1066. ret = pci_read_config_byte(pdev, pos + PCI_SATA_REGS, &byte);
  1067. if (ret)
  1068. return pcibios_err_to_errno(ret);
  1069. byte &= PCI_SATA_REGS_MASK;
  1070. if (byte == PCI_SATA_REGS_INLINE)
  1071. return PCI_SATA_SIZEOF_LONG;
  1072. else
  1073. return PCI_SATA_SIZEOF_SHORT;
  1074. default:
  1075. pr_warn("%s: %s unknown length for pci cap 0x%x@0x%x\n",
  1076. dev_name(&pdev->dev), __func__, cap, pos);
  1077. }
  1078. return 0;
  1079. }
  1080. static int vfio_ext_cap_len(struct vfio_pci_device *vdev, u16 ecap, u16 epos)
  1081. {
  1082. struct pci_dev *pdev = vdev->pdev;
  1083. u8 byte;
  1084. u32 dword;
  1085. int ret;
  1086. switch (ecap) {
  1087. case PCI_EXT_CAP_ID_VNDR:
  1088. ret = pci_read_config_dword(pdev, epos + PCI_VSEC_HDR, &dword);
  1089. if (ret)
  1090. return pcibios_err_to_errno(ret);
  1091. return dword >> PCI_VSEC_HDR_LEN_SHIFT;
  1092. case PCI_EXT_CAP_ID_VC:
  1093. case PCI_EXT_CAP_ID_VC9:
  1094. case PCI_EXT_CAP_ID_MFVC:
  1095. return vfio_vc_cap_len(vdev, epos);
  1096. case PCI_EXT_CAP_ID_ACS:
  1097. ret = pci_read_config_byte(pdev, epos + PCI_ACS_CAP, &byte);
  1098. if (ret)
  1099. return pcibios_err_to_errno(ret);
  1100. if (byte & PCI_ACS_EC) {
  1101. int bits;
  1102. ret = pci_read_config_byte(pdev,
  1103. epos + PCI_ACS_EGRESS_BITS,
  1104. &byte);
  1105. if (ret)
  1106. return pcibios_err_to_errno(ret);
  1107. bits = byte ? round_up(byte, 32) : 256;
  1108. return 8 + (bits / 8);
  1109. }
  1110. return 8;
  1111. case PCI_EXT_CAP_ID_REBAR:
  1112. ret = pci_read_config_byte(pdev, epos + PCI_REBAR_CTRL, &byte);
  1113. if (ret)
  1114. return pcibios_err_to_errno(ret);
  1115. byte &= PCI_REBAR_CTRL_NBAR_MASK;
  1116. byte >>= PCI_REBAR_CTRL_NBAR_SHIFT;
  1117. return 4 + (byte * 8);
  1118. case PCI_EXT_CAP_ID_DPA:
  1119. ret = pci_read_config_byte(pdev, epos + PCI_DPA_CAP, &byte);
  1120. if (ret)
  1121. return pcibios_err_to_errno(ret);
  1122. byte &= PCI_DPA_CAP_SUBSTATE_MASK;
  1123. return PCI_DPA_BASE_SIZEOF + byte + 1;
  1124. case PCI_EXT_CAP_ID_TPH:
  1125. ret = pci_read_config_dword(pdev, epos + PCI_TPH_CAP, &dword);
  1126. if (ret)
  1127. return pcibios_err_to_errno(ret);
  1128. if ((dword & PCI_TPH_CAP_LOC_MASK) == PCI_TPH_LOC_CAP) {
  1129. int sts;
  1130. sts = dword & PCI_TPH_CAP_ST_MASK;
  1131. sts >>= PCI_TPH_CAP_ST_SHIFT;
  1132. return PCI_TPH_BASE_SIZEOF + (sts * 2) + 2;
  1133. }
  1134. return PCI_TPH_BASE_SIZEOF;
  1135. default:
  1136. pr_warn("%s: %s unknown length for pci ecap 0x%x@0x%x\n",
  1137. dev_name(&pdev->dev), __func__, ecap, epos);
  1138. }
  1139. return 0;
  1140. }
  1141. static int vfio_fill_vconfig_bytes(struct vfio_pci_device *vdev,
  1142. int offset, int size)
  1143. {
  1144. struct pci_dev *pdev = vdev->pdev;
  1145. int ret = 0;
  1146. /*
  1147. * We try to read physical config space in the largest chunks
  1148. * we can, assuming that all of the fields support dword access.
  1149. * pci_save_state() makes this same assumption and seems to do ok.
  1150. */
  1151. while (size) {
  1152. int filled;
  1153. if (size >= 4 && !(offset % 4)) {
  1154. __le32 *dwordp = (__le32 *)&vdev->vconfig[offset];
  1155. u32 dword;
  1156. ret = pci_read_config_dword(pdev, offset, &dword);
  1157. if (ret)
  1158. return ret;
  1159. *dwordp = cpu_to_le32(dword);
  1160. filled = 4;
  1161. } else if (size >= 2 && !(offset % 2)) {
  1162. __le16 *wordp = (__le16 *)&vdev->vconfig[offset];
  1163. u16 word;
  1164. ret = pci_read_config_word(pdev, offset, &word);
  1165. if (ret)
  1166. return ret;
  1167. *wordp = cpu_to_le16(word);
  1168. filled = 2;
  1169. } else {
  1170. u8 *byte = &vdev->vconfig[offset];
  1171. ret = pci_read_config_byte(pdev, offset, byte);
  1172. if (ret)
  1173. return ret;
  1174. filled = 1;
  1175. }
  1176. offset += filled;
  1177. size -= filled;
  1178. }
  1179. return ret;
  1180. }
  1181. static int vfio_cap_init(struct vfio_pci_device *vdev)
  1182. {
  1183. struct pci_dev *pdev = vdev->pdev;
  1184. u8 *map = vdev->pci_config_map;
  1185. u16 status;
  1186. u8 pos, *prev, cap;
  1187. int loops, ret, caps = 0;
  1188. /* Any capabilities? */
  1189. ret = pci_read_config_word(pdev, PCI_STATUS, &status);
  1190. if (ret)
  1191. return ret;
  1192. if (!(status & PCI_STATUS_CAP_LIST))
  1193. return 0; /* Done */
  1194. ret = pci_read_config_byte(pdev, PCI_CAPABILITY_LIST, &pos);
  1195. if (ret)
  1196. return ret;
  1197. /* Mark the previous position in case we want to skip a capability */
  1198. prev = &vdev->vconfig[PCI_CAPABILITY_LIST];
  1199. /* We can bound our loop, capabilities are dword aligned */
  1200. loops = (PCI_CFG_SPACE_SIZE - PCI_STD_HEADER_SIZEOF) / PCI_CAP_SIZEOF;
  1201. while (pos && loops--) {
  1202. u8 next;
  1203. int i, len = 0;
  1204. ret = pci_read_config_byte(pdev, pos, &cap);
  1205. if (ret)
  1206. return ret;
  1207. ret = pci_read_config_byte(pdev,
  1208. pos + PCI_CAP_LIST_NEXT, &next);
  1209. if (ret)
  1210. return ret;
  1211. if (cap <= PCI_CAP_ID_MAX) {
  1212. len = pci_cap_length[cap];
  1213. if (len == 0xFF) { /* Variable length */
  1214. len = vfio_cap_len(vdev, cap, pos);
  1215. if (len < 0)
  1216. return len;
  1217. }
  1218. }
  1219. if (!len) {
  1220. pr_info("%s: %s hiding cap 0x%x\n",
  1221. __func__, dev_name(&pdev->dev), cap);
  1222. *prev = next;
  1223. pos = next;
  1224. continue;
  1225. }
  1226. /* Sanity check, do we overlap other capabilities? */
  1227. for (i = 0; i < len; i++) {
  1228. if (likely(map[pos + i] == PCI_CAP_ID_INVALID))
  1229. continue;
  1230. pr_warn("%s: %s pci config conflict @0x%x, was cap 0x%x now cap 0x%x\n",
  1231. __func__, dev_name(&pdev->dev),
  1232. pos + i, map[pos + i], cap);
  1233. }
  1234. BUILD_BUG_ON(PCI_CAP_ID_MAX >= PCI_CAP_ID_INVALID_VIRT);
  1235. memset(map + pos, cap, len);
  1236. ret = vfio_fill_vconfig_bytes(vdev, pos, len);
  1237. if (ret)
  1238. return ret;
  1239. prev = &vdev->vconfig[pos + PCI_CAP_LIST_NEXT];
  1240. pos = next;
  1241. caps++;
  1242. }
  1243. /* If we didn't fill any capabilities, clear the status flag */
  1244. if (!caps) {
  1245. __le16 *vstatus = (__le16 *)&vdev->vconfig[PCI_STATUS];
  1246. *vstatus &= ~cpu_to_le16(PCI_STATUS_CAP_LIST);
  1247. }
  1248. return 0;
  1249. }
  1250. static int vfio_ecap_init(struct vfio_pci_device *vdev)
  1251. {
  1252. struct pci_dev *pdev = vdev->pdev;
  1253. u8 *map = vdev->pci_config_map;
  1254. u16 epos;
  1255. __le32 *prev = NULL;
  1256. int loops, ret, ecaps = 0;
  1257. if (!vdev->extended_caps)
  1258. return 0;
  1259. epos = PCI_CFG_SPACE_SIZE;
  1260. loops = (pdev->cfg_size - PCI_CFG_SPACE_SIZE) / PCI_CAP_SIZEOF;
  1261. while (loops-- && epos >= PCI_CFG_SPACE_SIZE) {
  1262. u32 header;
  1263. u16 ecap;
  1264. int i, len = 0;
  1265. bool hidden = false;
  1266. ret = pci_read_config_dword(pdev, epos, &header);
  1267. if (ret)
  1268. return ret;
  1269. ecap = PCI_EXT_CAP_ID(header);
  1270. if (ecap <= PCI_EXT_CAP_ID_MAX) {
  1271. len = pci_ext_cap_length[ecap];
  1272. if (len == 0xFF) {
  1273. len = vfio_ext_cap_len(vdev, ecap, epos);
  1274. if (len < 0)
  1275. return ret;
  1276. }
  1277. }
  1278. if (!len) {
  1279. pr_info("%s: %s hiding ecap 0x%x@0x%x\n",
  1280. __func__, dev_name(&pdev->dev), ecap, epos);
  1281. /* If not the first in the chain, we can skip over it */
  1282. if (prev) {
  1283. u32 val = epos = PCI_EXT_CAP_NEXT(header);
  1284. *prev &= cpu_to_le32(~(0xffcU << 20));
  1285. *prev |= cpu_to_le32(val << 20);
  1286. continue;
  1287. }
  1288. /*
  1289. * Otherwise, fill in a placeholder, the direct
  1290. * readfn will virtualize this automatically
  1291. */
  1292. len = PCI_CAP_SIZEOF;
  1293. hidden = true;
  1294. }
  1295. for (i = 0; i < len; i++) {
  1296. if (likely(map[epos + i] == PCI_CAP_ID_INVALID))
  1297. continue;
  1298. pr_warn("%s: %s pci config conflict @0x%x, was ecap 0x%x now ecap 0x%x\n",
  1299. __func__, dev_name(&pdev->dev),
  1300. epos + i, map[epos + i], ecap);
  1301. }
  1302. /*
  1303. * Even though ecap is 2 bytes, we're currently a long way
  1304. * from exceeding 1 byte capabilities. If we ever make it
  1305. * up to 0xFE we'll need to up this to a two-byte, byte map.
  1306. */
  1307. BUILD_BUG_ON(PCI_EXT_CAP_ID_MAX >= PCI_CAP_ID_INVALID_VIRT);
  1308. memset(map + epos, ecap, len);
  1309. ret = vfio_fill_vconfig_bytes(vdev, epos, len);
  1310. if (ret)
  1311. return ret;
  1312. /*
  1313. * If we're just using this capability to anchor the list,
  1314. * hide the real ID. Only count real ecaps. XXX PCI spec
  1315. * indicates to use cap id = 0, version = 0, next = 0 if
  1316. * ecaps are absent, hope users check all the way to next.
  1317. */
  1318. if (hidden)
  1319. *(__le32 *)&vdev->vconfig[epos] &=
  1320. cpu_to_le32((0xffcU << 20));
  1321. else
  1322. ecaps++;
  1323. prev = (__le32 *)&vdev->vconfig[epos];
  1324. epos = PCI_EXT_CAP_NEXT(header);
  1325. }
  1326. if (!ecaps)
  1327. *(u32 *)&vdev->vconfig[PCI_CFG_SPACE_SIZE] = 0;
  1328. return 0;
  1329. }
  1330. /*
  1331. * For each device we allocate a pci_config_map that indicates the
  1332. * capability occupying each dword and thus the struct perm_bits we
  1333. * use for read and write. We also allocate a virtualized config
  1334. * space which tracks reads and writes to bits that we emulate for
  1335. * the user. Initial values filled from device.
  1336. *
  1337. * Using shared struct perm_bits between all vfio-pci devices saves
  1338. * us from allocating cfg_size buffers for virt and write for every
  1339. * device. We could remove vconfig and allocate individual buffers
  1340. * for each area requiring emulated bits, but the array of pointers
  1341. * would be comparable in size (at least for standard config space).
  1342. */
  1343. int vfio_config_init(struct vfio_pci_device *vdev)
  1344. {
  1345. struct pci_dev *pdev = vdev->pdev;
  1346. u8 *map, *vconfig;
  1347. int ret;
  1348. /*
  1349. * Config space, caps and ecaps are all dword aligned, so we could
  1350. * use one byte per dword to record the type. However, there are
  1351. * no requiremenst on the length of a capability, so the gap between
  1352. * capabilities needs byte granularity.
  1353. */
  1354. map = kmalloc(pdev->cfg_size, GFP_KERNEL);
  1355. if (!map)
  1356. return -ENOMEM;
  1357. vconfig = kmalloc(pdev->cfg_size, GFP_KERNEL);
  1358. if (!vconfig) {
  1359. kfree(map);
  1360. return -ENOMEM;
  1361. }
  1362. vdev->pci_config_map = map;
  1363. vdev->vconfig = vconfig;
  1364. memset(map, PCI_CAP_ID_BASIC, PCI_STD_HEADER_SIZEOF);
  1365. memset(map + PCI_STD_HEADER_SIZEOF, PCI_CAP_ID_INVALID,
  1366. pdev->cfg_size - PCI_STD_HEADER_SIZEOF);
  1367. ret = vfio_fill_vconfig_bytes(vdev, 0, PCI_STD_HEADER_SIZEOF);
  1368. if (ret)
  1369. goto out;
  1370. vdev->bardirty = true;
  1371. /*
  1372. * XXX can we just pci_load_saved_state/pci_restore_state?
  1373. * may need to rebuild vconfig after that
  1374. */
  1375. /* For restore after reset */
  1376. vdev->rbar[0] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_0]);
  1377. vdev->rbar[1] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_1]);
  1378. vdev->rbar[2] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_2]);
  1379. vdev->rbar[3] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_3]);
  1380. vdev->rbar[4] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_4]);
  1381. vdev->rbar[5] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_5]);
  1382. vdev->rbar[6] = le32_to_cpu(*(__le32 *)&vconfig[PCI_ROM_ADDRESS]);
  1383. if (pdev->is_virtfn) {
  1384. *(__le16 *)&vconfig[PCI_VENDOR_ID] = cpu_to_le16(pdev->vendor);
  1385. *(__le16 *)&vconfig[PCI_DEVICE_ID] = cpu_to_le16(pdev->device);
  1386. }
  1387. if (!IS_ENABLED(CONFIG_VFIO_PCI_INTX) || vdev->nointx)
  1388. vconfig[PCI_INTERRUPT_PIN] = 0;
  1389. ret = vfio_cap_init(vdev);
  1390. if (ret)
  1391. goto out;
  1392. ret = vfio_ecap_init(vdev);
  1393. if (ret)
  1394. goto out;
  1395. return 0;
  1396. out:
  1397. kfree(map);
  1398. vdev->pci_config_map = NULL;
  1399. kfree(vconfig);
  1400. vdev->vconfig = NULL;
  1401. return pcibios_err_to_errno(ret);
  1402. }
  1403. void vfio_config_free(struct vfio_pci_device *vdev)
  1404. {
  1405. kfree(vdev->vconfig);
  1406. vdev->vconfig = NULL;
  1407. kfree(vdev->pci_config_map);
  1408. vdev->pci_config_map = NULL;
  1409. kfree(vdev->msi_perm);
  1410. vdev->msi_perm = NULL;
  1411. }
  1412. /*
  1413. * Find the remaining number of bytes in a dword that match the given
  1414. * position. Stop at either the end of the capability or the dword boundary.
  1415. */
  1416. static size_t vfio_pci_cap_remaining_dword(struct vfio_pci_device *vdev,
  1417. loff_t pos)
  1418. {
  1419. u8 cap = vdev->pci_config_map[pos];
  1420. size_t i;
  1421. for (i = 1; (pos + i) % 4 && vdev->pci_config_map[pos + i] == cap; i++)
  1422. /* nop */;
  1423. return i;
  1424. }
  1425. static ssize_t vfio_config_do_rw(struct vfio_pci_device *vdev, char __user *buf,
  1426. size_t count, loff_t *ppos, bool iswrite)
  1427. {
  1428. struct pci_dev *pdev = vdev->pdev;
  1429. struct perm_bits *perm;
  1430. __le32 val = 0;
  1431. int cap_start = 0, offset;
  1432. u8 cap_id;
  1433. ssize_t ret;
  1434. if (*ppos < 0 || *ppos >= pdev->cfg_size ||
  1435. *ppos + count > pdev->cfg_size)
  1436. return -EFAULT;
  1437. /*
  1438. * Chop accesses into aligned chunks containing no more than a
  1439. * single capability. Caller increments to the next chunk.
  1440. */
  1441. count = min(count, vfio_pci_cap_remaining_dword(vdev, *ppos));
  1442. if (count >= 4 && !(*ppos % 4))
  1443. count = 4;
  1444. else if (count >= 2 && !(*ppos % 2))
  1445. count = 2;
  1446. else
  1447. count = 1;
  1448. ret = count;
  1449. cap_id = vdev->pci_config_map[*ppos];
  1450. if (cap_id == PCI_CAP_ID_INVALID) {
  1451. perm = &unassigned_perms;
  1452. cap_start = *ppos;
  1453. } else if (cap_id == PCI_CAP_ID_INVALID_VIRT) {
  1454. perm = &virt_perms;
  1455. cap_start = *ppos;
  1456. } else {
  1457. if (*ppos >= PCI_CFG_SPACE_SIZE) {
  1458. WARN_ON(cap_id > PCI_EXT_CAP_ID_MAX);
  1459. perm = &ecap_perms[cap_id];
  1460. cap_start = vfio_find_cap_start(vdev, *ppos);
  1461. } else {
  1462. WARN_ON(cap_id > PCI_CAP_ID_MAX);
  1463. perm = &cap_perms[cap_id];
  1464. if (cap_id == PCI_CAP_ID_MSI)
  1465. perm = vdev->msi_perm;
  1466. if (cap_id > PCI_CAP_ID_BASIC)
  1467. cap_start = vfio_find_cap_start(vdev, *ppos);
  1468. }
  1469. }
  1470. WARN_ON(!cap_start && cap_id != PCI_CAP_ID_BASIC);
  1471. WARN_ON(cap_start > *ppos);
  1472. offset = *ppos - cap_start;
  1473. if (iswrite) {
  1474. if (!perm->writefn)
  1475. return ret;
  1476. if (copy_from_user(&val, buf, count))
  1477. return -EFAULT;
  1478. ret = perm->writefn(vdev, *ppos, count, perm, offset, val);
  1479. } else {
  1480. if (perm->readfn) {
  1481. ret = perm->readfn(vdev, *ppos, count,
  1482. perm, offset, &val);
  1483. if (ret < 0)
  1484. return ret;
  1485. }
  1486. if (copy_to_user(buf, &val, count))
  1487. return -EFAULT;
  1488. }
  1489. return ret;
  1490. }
  1491. ssize_t vfio_pci_config_rw(struct vfio_pci_device *vdev, char __user *buf,
  1492. size_t count, loff_t *ppos, bool iswrite)
  1493. {
  1494. size_t done = 0;
  1495. int ret = 0;
  1496. loff_t pos = *ppos;
  1497. pos &= VFIO_PCI_OFFSET_MASK;
  1498. while (count) {
  1499. ret = vfio_config_do_rw(vdev, buf, count, &pos, iswrite);
  1500. if (ret < 0)
  1501. return ret;
  1502. count -= ret;
  1503. done += ret;
  1504. buf += ret;
  1505. pos += ret;
  1506. }
  1507. *ppos += done;
  1508. return done;
  1509. }