vfio_pci_config.c 42 KB

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