vfio_pci_config.c 53 KB

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