vfio_pci_config.c 41 KB

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