vfio_pci_config.c 50 KB

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