pci-assign.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905
  1. /*
  2. * Copyright (c) 2007, Neocleus Corporation.
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2. See
  5. * the COPYING file in the top-level directory.
  6. *
  7. *
  8. * Assign a PCI device from the host to a guest VM.
  9. *
  10. * This implementation uses the classic device assignment interface of KVM
  11. * and is only available on x86 hosts. It is expected to be obsoleted by VFIO
  12. * based device assignment.
  13. *
  14. * Adapted for KVM (qemu-kvm) by Qumranet. QEMU version was based on qemu-kvm
  15. * revision 4144fe9d48. See its repository for the history.
  16. *
  17. * Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)
  18. * Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)
  19. * Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)
  20. * Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)
  21. * Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)
  22. */
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <sys/io.h>
  26. #include <sys/mman.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include "hw/hw.h"
  30. #include "hw/pc.h"
  31. #include "qemu-error.h"
  32. #include "ui/console.h"
  33. #include "hw/loader.h"
  34. #include "monitor/monitor.h"
  35. #include "range.h"
  36. #include "sysemu.h"
  37. #include "hw/pci/pci.h"
  38. #include "hw/pci/msi.h"
  39. #include "kvm_i386.h"
  40. #define MSIX_PAGE_SIZE 0x1000
  41. /* From linux/ioport.h */
  42. #define IORESOURCE_IO 0x00000100 /* Resource type */
  43. #define IORESOURCE_MEM 0x00000200
  44. #define IORESOURCE_IRQ 0x00000400
  45. #define IORESOURCE_DMA 0x00000800
  46. #define IORESOURCE_PREFETCH 0x00002000 /* No side effects */
  47. //#define DEVICE_ASSIGNMENT_DEBUG
  48. #ifdef DEVICE_ASSIGNMENT_DEBUG
  49. #define DEBUG(fmt, ...) \
  50. do { \
  51. fprintf(stderr, "%s: " fmt, __func__ , __VA_ARGS__); \
  52. } while (0)
  53. #else
  54. #define DEBUG(fmt, ...)
  55. #endif
  56. typedef struct PCIRegion {
  57. int type; /* Memory or port I/O */
  58. int valid;
  59. uint64_t base_addr;
  60. uint64_t size; /* size of the region */
  61. int resource_fd;
  62. } PCIRegion;
  63. typedef struct PCIDevRegions {
  64. uint8_t bus, dev, func; /* Bus inside domain, device and function */
  65. int irq; /* IRQ number */
  66. uint16_t region_number; /* number of active regions */
  67. /* Port I/O or MMIO Regions */
  68. PCIRegion regions[PCI_NUM_REGIONS - 1];
  69. int config_fd;
  70. } PCIDevRegions;
  71. typedef struct AssignedDevRegion {
  72. MemoryRegion container;
  73. MemoryRegion real_iomem;
  74. union {
  75. uint8_t *r_virtbase; /* mmapped access address for memory regions */
  76. uint32_t r_baseport; /* the base guest port for I/O regions */
  77. } u;
  78. pcibus_t e_size; /* emulated size of region in bytes */
  79. pcibus_t r_size; /* real size of region in bytes */
  80. PCIRegion *region;
  81. } AssignedDevRegion;
  82. #define ASSIGNED_DEVICE_PREFER_MSI_BIT 0
  83. #define ASSIGNED_DEVICE_SHARE_INTX_BIT 1
  84. #define ASSIGNED_DEVICE_PREFER_MSI_MASK (1 << ASSIGNED_DEVICE_PREFER_MSI_BIT)
  85. #define ASSIGNED_DEVICE_SHARE_INTX_MASK (1 << ASSIGNED_DEVICE_SHARE_INTX_BIT)
  86. typedef struct MSIXTableEntry {
  87. uint32_t addr_lo;
  88. uint32_t addr_hi;
  89. uint32_t data;
  90. uint32_t ctrl;
  91. } MSIXTableEntry;
  92. typedef enum AssignedIRQType {
  93. ASSIGNED_IRQ_NONE = 0,
  94. ASSIGNED_IRQ_INTX_HOST_INTX,
  95. ASSIGNED_IRQ_INTX_HOST_MSI,
  96. ASSIGNED_IRQ_MSI,
  97. ASSIGNED_IRQ_MSIX
  98. } AssignedIRQType;
  99. typedef struct AssignedDevice {
  100. PCIDevice dev;
  101. PCIHostDeviceAddress host;
  102. uint32_t dev_id;
  103. uint32_t features;
  104. int intpin;
  105. AssignedDevRegion v_addrs[PCI_NUM_REGIONS - 1];
  106. PCIDevRegions real_device;
  107. PCIINTxRoute intx_route;
  108. AssignedIRQType assigned_irq_type;
  109. struct {
  110. #define ASSIGNED_DEVICE_CAP_MSI (1 << 0)
  111. #define ASSIGNED_DEVICE_CAP_MSIX (1 << 1)
  112. uint32_t available;
  113. #define ASSIGNED_DEVICE_MSI_ENABLED (1 << 0)
  114. #define ASSIGNED_DEVICE_MSIX_ENABLED (1 << 1)
  115. #define ASSIGNED_DEVICE_MSIX_MASKED (1 << 2)
  116. uint32_t state;
  117. } cap;
  118. uint8_t emulate_config_read[PCI_CONFIG_SPACE_SIZE];
  119. uint8_t emulate_config_write[PCI_CONFIG_SPACE_SIZE];
  120. int msi_virq_nr;
  121. int *msi_virq;
  122. MSIXTableEntry *msix_table;
  123. hwaddr msix_table_addr;
  124. uint16_t msix_max;
  125. MemoryRegion mmio;
  126. char *configfd_name;
  127. int32_t bootindex;
  128. } AssignedDevice;
  129. static void assigned_dev_update_irq_routing(PCIDevice *dev);
  130. static void assigned_dev_load_option_rom(AssignedDevice *dev);
  131. static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev);
  132. static uint64_t assigned_dev_ioport_rw(AssignedDevRegion *dev_region,
  133. hwaddr addr, int size,
  134. uint64_t *data)
  135. {
  136. uint64_t val = 0;
  137. int fd = dev_region->region->resource_fd;
  138. if (fd >= 0) {
  139. if (data) {
  140. DEBUG("pwrite data=%" PRIx64 ", size=%d, e_phys=" TARGET_FMT_plx
  141. ", addr="TARGET_FMT_plx"\n", *data, size, addr, addr);
  142. if (pwrite(fd, data, size, addr) != size) {
  143. error_report("%s - pwrite failed %s",
  144. __func__, strerror(errno));
  145. }
  146. } else {
  147. if (pread(fd, &val, size, addr) != size) {
  148. error_report("%s - pread failed %s",
  149. __func__, strerror(errno));
  150. val = (1UL << (size * 8)) - 1;
  151. }
  152. DEBUG("pread val=%" PRIx64 ", size=%d, e_phys=" TARGET_FMT_plx
  153. ", addr=" TARGET_FMT_plx "\n", val, size, addr, addr);
  154. }
  155. } else {
  156. uint32_t port = addr + dev_region->u.r_baseport;
  157. if (data) {
  158. DEBUG("out data=%" PRIx64 ", size=%d, e_phys=" TARGET_FMT_plx
  159. ", host=%x\n", *data, size, addr, port);
  160. switch (size) {
  161. case 1:
  162. outb(*data, port);
  163. break;
  164. case 2:
  165. outw(*data, port);
  166. break;
  167. case 4:
  168. outl(*data, port);
  169. break;
  170. }
  171. } else {
  172. switch (size) {
  173. case 1:
  174. val = inb(port);
  175. break;
  176. case 2:
  177. val = inw(port);
  178. break;
  179. case 4:
  180. val = inl(port);
  181. break;
  182. }
  183. DEBUG("in data=%" PRIx64 ", size=%d, e_phys=" TARGET_FMT_plx
  184. ", host=%x\n", val, size, addr, port);
  185. }
  186. }
  187. return val;
  188. }
  189. static void assigned_dev_ioport_write(void *opaque, hwaddr addr,
  190. uint64_t data, unsigned size)
  191. {
  192. assigned_dev_ioport_rw(opaque, addr, size, &data);
  193. }
  194. static uint64_t assigned_dev_ioport_read(void *opaque,
  195. hwaddr addr, unsigned size)
  196. {
  197. return assigned_dev_ioport_rw(opaque, addr, size, NULL);
  198. }
  199. static uint32_t slow_bar_readb(void *opaque, hwaddr addr)
  200. {
  201. AssignedDevRegion *d = opaque;
  202. uint8_t *in = d->u.r_virtbase + addr;
  203. uint32_t r;
  204. r = *in;
  205. DEBUG("slow_bar_readl addr=0x" TARGET_FMT_plx " val=0x%08x\n", addr, r);
  206. return r;
  207. }
  208. static uint32_t slow_bar_readw(void *opaque, hwaddr addr)
  209. {
  210. AssignedDevRegion *d = opaque;
  211. uint16_t *in = (uint16_t *)(d->u.r_virtbase + addr);
  212. uint32_t r;
  213. r = *in;
  214. DEBUG("slow_bar_readl addr=0x" TARGET_FMT_plx " val=0x%08x\n", addr, r);
  215. return r;
  216. }
  217. static uint32_t slow_bar_readl(void *opaque, hwaddr addr)
  218. {
  219. AssignedDevRegion *d = opaque;
  220. uint32_t *in = (uint32_t *)(d->u.r_virtbase + addr);
  221. uint32_t r;
  222. r = *in;
  223. DEBUG("slow_bar_readl addr=0x" TARGET_FMT_plx " val=0x%08x\n", addr, r);
  224. return r;
  225. }
  226. static void slow_bar_writeb(void *opaque, hwaddr addr, uint32_t val)
  227. {
  228. AssignedDevRegion *d = opaque;
  229. uint8_t *out = d->u.r_virtbase + addr;
  230. DEBUG("slow_bar_writeb addr=0x" TARGET_FMT_plx " val=0x%02x\n", addr, val);
  231. *out = val;
  232. }
  233. static void slow_bar_writew(void *opaque, hwaddr addr, uint32_t val)
  234. {
  235. AssignedDevRegion *d = opaque;
  236. uint16_t *out = (uint16_t *)(d->u.r_virtbase + addr);
  237. DEBUG("slow_bar_writew addr=0x" TARGET_FMT_plx " val=0x%04x\n", addr, val);
  238. *out = val;
  239. }
  240. static void slow_bar_writel(void *opaque, hwaddr addr, uint32_t val)
  241. {
  242. AssignedDevRegion *d = opaque;
  243. uint32_t *out = (uint32_t *)(d->u.r_virtbase + addr);
  244. DEBUG("slow_bar_writel addr=0x" TARGET_FMT_plx " val=0x%08x\n", addr, val);
  245. *out = val;
  246. }
  247. static const MemoryRegionOps slow_bar_ops = {
  248. .old_mmio = {
  249. .read = { slow_bar_readb, slow_bar_readw, slow_bar_readl, },
  250. .write = { slow_bar_writeb, slow_bar_writew, slow_bar_writel, },
  251. },
  252. .endianness = DEVICE_NATIVE_ENDIAN,
  253. };
  254. static void assigned_dev_iomem_setup(PCIDevice *pci_dev, int region_num,
  255. pcibus_t e_size)
  256. {
  257. AssignedDevice *r_dev = DO_UPCAST(AssignedDevice, dev, pci_dev);
  258. AssignedDevRegion *region = &r_dev->v_addrs[region_num];
  259. PCIRegion *real_region = &r_dev->real_device.regions[region_num];
  260. if (e_size > 0) {
  261. memory_region_init(&region->container, "assigned-dev-container",
  262. e_size);
  263. memory_region_add_subregion(&region->container, 0, &region->real_iomem);
  264. /* deal with MSI-X MMIO page */
  265. if (real_region->base_addr <= r_dev->msix_table_addr &&
  266. real_region->base_addr + real_region->size >
  267. r_dev->msix_table_addr) {
  268. uint64_t offset = r_dev->msix_table_addr - real_region->base_addr;
  269. memory_region_add_subregion_overlap(&region->container,
  270. offset,
  271. &r_dev->mmio,
  272. 1);
  273. }
  274. }
  275. }
  276. static const MemoryRegionOps assigned_dev_ioport_ops = {
  277. .read = assigned_dev_ioport_read,
  278. .write = assigned_dev_ioport_write,
  279. .endianness = DEVICE_NATIVE_ENDIAN,
  280. };
  281. static void assigned_dev_ioport_setup(PCIDevice *pci_dev, int region_num,
  282. pcibus_t size)
  283. {
  284. AssignedDevice *r_dev = DO_UPCAST(AssignedDevice, dev, pci_dev);
  285. AssignedDevRegion *region = &r_dev->v_addrs[region_num];
  286. region->e_size = size;
  287. memory_region_init(&region->container, "assigned-dev-container", size);
  288. memory_region_init_io(&region->real_iomem, &assigned_dev_ioport_ops,
  289. r_dev->v_addrs + region_num,
  290. "assigned-dev-iomem", size);
  291. memory_region_add_subregion(&region->container, 0, &region->real_iomem);
  292. }
  293. static uint32_t assigned_dev_pci_read(PCIDevice *d, int pos, int len)
  294. {
  295. AssignedDevice *pci_dev = DO_UPCAST(AssignedDevice, dev, d);
  296. uint32_t val;
  297. ssize_t ret;
  298. int fd = pci_dev->real_device.config_fd;
  299. again:
  300. ret = pread(fd, &val, len, pos);
  301. if (ret != len) {
  302. if ((ret < 0) && (errno == EINTR || errno == EAGAIN)) {
  303. goto again;
  304. }
  305. hw_error("pci read failed, ret = %zd errno = %d\n", ret, errno);
  306. }
  307. return val;
  308. }
  309. static uint8_t assigned_dev_pci_read_byte(PCIDevice *d, int pos)
  310. {
  311. return (uint8_t)assigned_dev_pci_read(d, pos, 1);
  312. }
  313. static void assigned_dev_pci_write(PCIDevice *d, int pos, uint32_t val, int len)
  314. {
  315. AssignedDevice *pci_dev = DO_UPCAST(AssignedDevice, dev, d);
  316. ssize_t ret;
  317. int fd = pci_dev->real_device.config_fd;
  318. again:
  319. ret = pwrite(fd, &val, len, pos);
  320. if (ret != len) {
  321. if ((ret < 0) && (errno == EINTR || errno == EAGAIN)) {
  322. goto again;
  323. }
  324. hw_error("pci write failed, ret = %zd errno = %d\n", ret, errno);
  325. }
  326. }
  327. static void assigned_dev_emulate_config_read(AssignedDevice *dev,
  328. uint32_t offset, uint32_t len)
  329. {
  330. memset(dev->emulate_config_read + offset, 0xff, len);
  331. }
  332. static void assigned_dev_direct_config_read(AssignedDevice *dev,
  333. uint32_t offset, uint32_t len)
  334. {
  335. memset(dev->emulate_config_read + offset, 0, len);
  336. }
  337. static void assigned_dev_direct_config_write(AssignedDevice *dev,
  338. uint32_t offset, uint32_t len)
  339. {
  340. memset(dev->emulate_config_write + offset, 0, len);
  341. }
  342. static uint8_t pci_find_cap_offset(PCIDevice *d, uint8_t cap, uint8_t start)
  343. {
  344. int id;
  345. int max_cap = 48;
  346. int pos = start ? start : PCI_CAPABILITY_LIST;
  347. int status;
  348. status = assigned_dev_pci_read_byte(d, PCI_STATUS);
  349. if ((status & PCI_STATUS_CAP_LIST) == 0) {
  350. return 0;
  351. }
  352. while (max_cap--) {
  353. pos = assigned_dev_pci_read_byte(d, pos);
  354. if (pos < 0x40) {
  355. break;
  356. }
  357. pos &= ~3;
  358. id = assigned_dev_pci_read_byte(d, pos + PCI_CAP_LIST_ID);
  359. if (id == 0xff) {
  360. break;
  361. }
  362. if (id == cap) {
  363. return pos;
  364. }
  365. pos += PCI_CAP_LIST_NEXT;
  366. }
  367. return 0;
  368. }
  369. static int assigned_dev_register_regions(PCIRegion *io_regions,
  370. unsigned long regions_num,
  371. AssignedDevice *pci_dev)
  372. {
  373. uint32_t i;
  374. PCIRegion *cur_region = io_regions;
  375. for (i = 0; i < regions_num; i++, cur_region++) {
  376. if (!cur_region->valid) {
  377. continue;
  378. }
  379. /* handle memory io regions */
  380. if (cur_region->type & IORESOURCE_MEM) {
  381. int t = cur_region->type & IORESOURCE_PREFETCH
  382. ? PCI_BASE_ADDRESS_MEM_PREFETCH
  383. : PCI_BASE_ADDRESS_SPACE_MEMORY;
  384. /* map physical memory */
  385. pci_dev->v_addrs[i].u.r_virtbase = mmap(NULL, cur_region->size,
  386. PROT_WRITE | PROT_READ,
  387. MAP_SHARED,
  388. cur_region->resource_fd,
  389. (off_t)0);
  390. if (pci_dev->v_addrs[i].u.r_virtbase == MAP_FAILED) {
  391. pci_dev->v_addrs[i].u.r_virtbase = NULL;
  392. error_report("%s: Error: Couldn't mmap 0x%" PRIx64 "!",
  393. __func__, cur_region->base_addr);
  394. return -1;
  395. }
  396. pci_dev->v_addrs[i].r_size = cur_region->size;
  397. pci_dev->v_addrs[i].e_size = 0;
  398. /* add offset */
  399. pci_dev->v_addrs[i].u.r_virtbase +=
  400. (cur_region->base_addr & 0xFFF);
  401. if (cur_region->size & 0xFFF) {
  402. error_report("PCI region %d at address 0x%" PRIx64 " has "
  403. "size 0x%" PRIx64 ", which is not a multiple of "
  404. "4K. You might experience some performance hit "
  405. "due to that.",
  406. i, cur_region->base_addr, cur_region->size);
  407. memory_region_init_io(&pci_dev->v_addrs[i].real_iomem,
  408. &slow_bar_ops, &pci_dev->v_addrs[i],
  409. "assigned-dev-slow-bar",
  410. cur_region->size);
  411. } else {
  412. void *virtbase = pci_dev->v_addrs[i].u.r_virtbase;
  413. char name[32];
  414. snprintf(name, sizeof(name), "%s.bar%d",
  415. object_get_typename(OBJECT(pci_dev)), i);
  416. memory_region_init_ram_ptr(&pci_dev->v_addrs[i].real_iomem,
  417. name, cur_region->size,
  418. virtbase);
  419. vmstate_register_ram(&pci_dev->v_addrs[i].real_iomem,
  420. &pci_dev->dev.qdev);
  421. }
  422. assigned_dev_iomem_setup(&pci_dev->dev, i, cur_region->size);
  423. pci_register_bar((PCIDevice *) pci_dev, i, t,
  424. &pci_dev->v_addrs[i].container);
  425. continue;
  426. } else {
  427. /* handle port io regions */
  428. uint32_t val;
  429. int ret;
  430. /* Test kernel support for ioport resource read/write. Old
  431. * kernels return EIO. New kernels only allow 1/2/4 byte reads
  432. * so should return EINVAL for a 3 byte read */
  433. ret = pread(pci_dev->v_addrs[i].region->resource_fd, &val, 3, 0);
  434. if (ret >= 0) {
  435. error_report("Unexpected return from I/O port read: %d", ret);
  436. abort();
  437. } else if (errno != EINVAL) {
  438. error_report("Kernel doesn't support ioport resource "
  439. "access, hiding this region.");
  440. close(pci_dev->v_addrs[i].region->resource_fd);
  441. cur_region->valid = 0;
  442. continue;
  443. }
  444. pci_dev->v_addrs[i].u.r_baseport = cur_region->base_addr;
  445. pci_dev->v_addrs[i].r_size = cur_region->size;
  446. pci_dev->v_addrs[i].e_size = 0;
  447. assigned_dev_ioport_setup(&pci_dev->dev, i, cur_region->size);
  448. pci_register_bar((PCIDevice *) pci_dev, i,
  449. PCI_BASE_ADDRESS_SPACE_IO,
  450. &pci_dev->v_addrs[i].container);
  451. }
  452. }
  453. /* success */
  454. return 0;
  455. }
  456. static int get_real_id(const char *devpath, const char *idname, uint16_t *val)
  457. {
  458. FILE *f;
  459. char name[128];
  460. long id;
  461. snprintf(name, sizeof(name), "%s%s", devpath, idname);
  462. f = fopen(name, "r");
  463. if (f == NULL) {
  464. error_report("%s: %s: %m", __func__, name);
  465. return -1;
  466. }
  467. if (fscanf(f, "%li\n", &id) == 1) {
  468. *val = id;
  469. } else {
  470. return -1;
  471. }
  472. fclose(f);
  473. return 0;
  474. }
  475. static int get_real_vendor_id(const char *devpath, uint16_t *val)
  476. {
  477. return get_real_id(devpath, "vendor", val);
  478. }
  479. static int get_real_device_id(const char *devpath, uint16_t *val)
  480. {
  481. return get_real_id(devpath, "device", val);
  482. }
  483. static int get_real_device(AssignedDevice *pci_dev, uint16_t r_seg,
  484. uint8_t r_bus, uint8_t r_dev, uint8_t r_func)
  485. {
  486. char dir[128], name[128];
  487. int fd, r = 0, v;
  488. FILE *f;
  489. uint64_t start, end, size, flags;
  490. uint16_t id;
  491. PCIRegion *rp;
  492. PCIDevRegions *dev = &pci_dev->real_device;
  493. dev->region_number = 0;
  494. snprintf(dir, sizeof(dir), "/sys/bus/pci/devices/%04x:%02x:%02x.%x/",
  495. r_seg, r_bus, r_dev, r_func);
  496. snprintf(name, sizeof(name), "%sconfig", dir);
  497. if (pci_dev->configfd_name && *pci_dev->configfd_name) {
  498. dev->config_fd = monitor_handle_fd_param(cur_mon, pci_dev->configfd_name);
  499. if (dev->config_fd < 0) {
  500. return 1;
  501. }
  502. } else {
  503. dev->config_fd = open(name, O_RDWR);
  504. if (dev->config_fd == -1) {
  505. error_report("%s: %s: %m", __func__, name);
  506. return 1;
  507. }
  508. }
  509. again:
  510. r = read(dev->config_fd, pci_dev->dev.config,
  511. pci_config_size(&pci_dev->dev));
  512. if (r < 0) {
  513. if (errno == EINTR || errno == EAGAIN) {
  514. goto again;
  515. }
  516. error_report("%s: read failed, errno = %d", __func__, errno);
  517. }
  518. /* Restore or clear multifunction, this is always controlled by qemu */
  519. if (pci_dev->dev.cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
  520. pci_dev->dev.config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
  521. } else {
  522. pci_dev->dev.config[PCI_HEADER_TYPE] &= ~PCI_HEADER_TYPE_MULTI_FUNCTION;
  523. }
  524. /* Clear host resource mapping info. If we choose not to register a
  525. * BAR, such as might be the case with the option ROM, we can get
  526. * confusing, unwritable, residual addresses from the host here. */
  527. memset(&pci_dev->dev.config[PCI_BASE_ADDRESS_0], 0, 24);
  528. memset(&pci_dev->dev.config[PCI_ROM_ADDRESS], 0, 4);
  529. snprintf(name, sizeof(name), "%sresource", dir);
  530. f = fopen(name, "r");
  531. if (f == NULL) {
  532. error_report("%s: %s: %m", __func__, name);
  533. return 1;
  534. }
  535. for (r = 0; r < PCI_ROM_SLOT; r++) {
  536. if (fscanf(f, "%" SCNi64 " %" SCNi64 " %" SCNi64 "\n",
  537. &start, &end, &flags) != 3) {
  538. break;
  539. }
  540. rp = dev->regions + r;
  541. rp->valid = 0;
  542. rp->resource_fd = -1;
  543. size = end - start + 1;
  544. flags &= IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
  545. if (size == 0 || (flags & ~IORESOURCE_PREFETCH) == 0) {
  546. continue;
  547. }
  548. if (flags & IORESOURCE_MEM) {
  549. flags &= ~IORESOURCE_IO;
  550. } else {
  551. flags &= ~IORESOURCE_PREFETCH;
  552. }
  553. snprintf(name, sizeof(name), "%sresource%d", dir, r);
  554. fd = open(name, O_RDWR);
  555. if (fd == -1) {
  556. continue;
  557. }
  558. rp->resource_fd = fd;
  559. rp->type = flags;
  560. rp->valid = 1;
  561. rp->base_addr = start;
  562. rp->size = size;
  563. pci_dev->v_addrs[r].region = rp;
  564. DEBUG("region %d size %" PRIu64 " start 0x%" PRIx64
  565. " type %d resource_fd %d\n",
  566. r, rp->size, start, rp->type, rp->resource_fd);
  567. }
  568. fclose(f);
  569. /* read and fill vendor ID */
  570. v = get_real_vendor_id(dir, &id);
  571. if (v) {
  572. return 1;
  573. }
  574. pci_dev->dev.config[0] = id & 0xff;
  575. pci_dev->dev.config[1] = (id & 0xff00) >> 8;
  576. /* read and fill device ID */
  577. v = get_real_device_id(dir, &id);
  578. if (v) {
  579. return 1;
  580. }
  581. pci_dev->dev.config[2] = id & 0xff;
  582. pci_dev->dev.config[3] = (id & 0xff00) >> 8;
  583. pci_word_test_and_clear_mask(pci_dev->emulate_config_write + PCI_COMMAND,
  584. PCI_COMMAND_MASTER | PCI_COMMAND_INTX_DISABLE);
  585. dev->region_number = r;
  586. return 0;
  587. }
  588. static void free_msi_virqs(AssignedDevice *dev)
  589. {
  590. int i;
  591. for (i = 0; i < dev->msi_virq_nr; i++) {
  592. if (dev->msi_virq[i] >= 0) {
  593. kvm_irqchip_release_virq(kvm_state, dev->msi_virq[i]);
  594. dev->msi_virq[i] = -1;
  595. }
  596. }
  597. g_free(dev->msi_virq);
  598. dev->msi_virq = NULL;
  599. dev->msi_virq_nr = 0;
  600. }
  601. static void free_assigned_device(AssignedDevice *dev)
  602. {
  603. int i;
  604. if (dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX) {
  605. assigned_dev_unregister_msix_mmio(dev);
  606. }
  607. for (i = 0; i < dev->real_device.region_number; i++) {
  608. PCIRegion *pci_region = &dev->real_device.regions[i];
  609. AssignedDevRegion *region = &dev->v_addrs[i];
  610. if (!pci_region->valid) {
  611. continue;
  612. }
  613. if (pci_region->type & IORESOURCE_IO) {
  614. if (region->u.r_baseport) {
  615. memory_region_del_subregion(&region->container,
  616. &region->real_iomem);
  617. memory_region_destroy(&region->real_iomem);
  618. memory_region_destroy(&region->container);
  619. }
  620. } else if (pci_region->type & IORESOURCE_MEM) {
  621. if (region->u.r_virtbase) {
  622. memory_region_del_subregion(&region->container,
  623. &region->real_iomem);
  624. /* Remove MSI-X table subregion */
  625. if (pci_region->base_addr <= dev->msix_table_addr &&
  626. pci_region->base_addr + pci_region->size >
  627. dev->msix_table_addr) {
  628. memory_region_del_subregion(&region->container,
  629. &dev->mmio);
  630. }
  631. memory_region_destroy(&region->real_iomem);
  632. memory_region_destroy(&region->container);
  633. if (munmap(region->u.r_virtbase,
  634. (pci_region->size + 0xFFF) & 0xFFFFF000)) {
  635. error_report("Failed to unmap assigned device region: %s",
  636. strerror(errno));
  637. }
  638. }
  639. }
  640. if (pci_region->resource_fd >= 0) {
  641. close(pci_region->resource_fd);
  642. }
  643. }
  644. if (dev->real_device.config_fd >= 0) {
  645. close(dev->real_device.config_fd);
  646. }
  647. free_msi_virqs(dev);
  648. }
  649. static void assign_failed_examine(AssignedDevice *dev)
  650. {
  651. char name[PATH_MAX], dir[PATH_MAX], driver[PATH_MAX] = {}, *ns;
  652. uint16_t vendor_id, device_id;
  653. int r;
  654. snprintf(dir, sizeof(dir), "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/",
  655. dev->host.domain, dev->host.bus, dev->host.slot,
  656. dev->host.function);
  657. snprintf(name, sizeof(name), "%sdriver", dir);
  658. r = readlink(name, driver, sizeof(driver));
  659. if ((r <= 0) || r >= sizeof(driver)) {
  660. goto fail;
  661. }
  662. ns = strrchr(driver, '/');
  663. if (!ns) {
  664. goto fail;
  665. }
  666. ns++;
  667. if (get_real_vendor_id(dir, &vendor_id) ||
  668. get_real_device_id(dir, &device_id)) {
  669. goto fail;
  670. }
  671. error_report("*** The driver '%s' is occupying your device "
  672. "%04x:%02x:%02x.%x.",
  673. ns, dev->host.domain, dev->host.bus, dev->host.slot,
  674. dev->host.function);
  675. error_report("***");
  676. error_report("*** You can try the following commands to free it:");
  677. error_report("***");
  678. error_report("*** $ echo \"%04x %04x\" > /sys/bus/pci/drivers/pci-stub/"
  679. "new_id", vendor_id, device_id);
  680. error_report("*** $ echo \"%04x:%02x:%02x.%x\" > /sys/bus/pci/drivers/"
  681. "%s/unbind",
  682. dev->host.domain, dev->host.bus, dev->host.slot,
  683. dev->host.function, ns);
  684. error_report("*** $ echo \"%04x:%02x:%02x.%x\" > /sys/bus/pci/drivers/"
  685. "pci-stub/bind",
  686. dev->host.domain, dev->host.bus, dev->host.slot,
  687. dev->host.function);
  688. error_report("*** $ echo \"%04x %04x\" > /sys/bus/pci/drivers/pci-stub"
  689. "/remove_id", vendor_id, device_id);
  690. error_report("***");
  691. return;
  692. fail:
  693. error_report("Couldn't find out why.");
  694. }
  695. static int assign_device(AssignedDevice *dev)
  696. {
  697. uint32_t flags = KVM_DEV_ASSIGN_ENABLE_IOMMU;
  698. int r;
  699. /* Only pass non-zero PCI segment to capable module */
  700. if (!kvm_check_extension(kvm_state, KVM_CAP_PCI_SEGMENT) &&
  701. dev->host.domain) {
  702. error_report("Can't assign device inside non-zero PCI segment "
  703. "as this KVM module doesn't support it.");
  704. return -ENODEV;
  705. }
  706. if (!kvm_check_extension(kvm_state, KVM_CAP_IOMMU)) {
  707. error_report("No IOMMU found. Unable to assign device \"%s\"",
  708. dev->dev.qdev.id);
  709. return -ENODEV;
  710. }
  711. if (dev->features & ASSIGNED_DEVICE_SHARE_INTX_MASK &&
  712. kvm_has_intx_set_mask()) {
  713. flags |= KVM_DEV_ASSIGN_PCI_2_3;
  714. }
  715. r = kvm_device_pci_assign(kvm_state, &dev->host, flags, &dev->dev_id);
  716. if (r < 0) {
  717. error_report("Failed to assign device \"%s\" : %s",
  718. dev->dev.qdev.id, strerror(-r));
  719. switch (r) {
  720. case -EBUSY:
  721. assign_failed_examine(dev);
  722. break;
  723. default:
  724. break;
  725. }
  726. }
  727. return r;
  728. }
  729. static bool check_irqchip_in_kernel(void)
  730. {
  731. if (kvm_irqchip_in_kernel()) {
  732. return true;
  733. }
  734. error_report("pci-assign: error: requires KVM with in-kernel irqchip "
  735. "enabled");
  736. return false;
  737. }
  738. static int assign_intx(AssignedDevice *dev)
  739. {
  740. AssignedIRQType new_type;
  741. PCIINTxRoute intx_route;
  742. bool intx_host_msi;
  743. int r;
  744. /* Interrupt PIN 0 means don't use INTx */
  745. if (assigned_dev_pci_read_byte(&dev->dev, PCI_INTERRUPT_PIN) == 0) {
  746. pci_device_set_intx_routing_notifier(&dev->dev, NULL);
  747. return 0;
  748. }
  749. if (!check_irqchip_in_kernel()) {
  750. return -ENOTSUP;
  751. }
  752. pci_device_set_intx_routing_notifier(&dev->dev,
  753. assigned_dev_update_irq_routing);
  754. intx_route = pci_device_route_intx_to_irq(&dev->dev, dev->intpin);
  755. assert(intx_route.mode != PCI_INTX_INVERTED);
  756. if (!pci_intx_route_changed(&dev->intx_route, &intx_route)) {
  757. return 0;
  758. }
  759. switch (dev->assigned_irq_type) {
  760. case ASSIGNED_IRQ_INTX_HOST_INTX:
  761. case ASSIGNED_IRQ_INTX_HOST_MSI:
  762. intx_host_msi = dev->assigned_irq_type == ASSIGNED_IRQ_INTX_HOST_MSI;
  763. r = kvm_device_intx_deassign(kvm_state, dev->dev_id, intx_host_msi);
  764. break;
  765. case ASSIGNED_IRQ_MSI:
  766. r = kvm_device_msi_deassign(kvm_state, dev->dev_id);
  767. break;
  768. case ASSIGNED_IRQ_MSIX:
  769. r = kvm_device_msix_deassign(kvm_state, dev->dev_id);
  770. break;
  771. default:
  772. r = 0;
  773. break;
  774. }
  775. if (r) {
  776. perror("assign_intx: deassignment of previous interrupt failed");
  777. }
  778. dev->assigned_irq_type = ASSIGNED_IRQ_NONE;
  779. if (intx_route.mode == PCI_INTX_DISABLED) {
  780. dev->intx_route = intx_route;
  781. return 0;
  782. }
  783. retry:
  784. if (dev->features & ASSIGNED_DEVICE_PREFER_MSI_MASK &&
  785. dev->cap.available & ASSIGNED_DEVICE_CAP_MSI) {
  786. intx_host_msi = true;
  787. new_type = ASSIGNED_IRQ_INTX_HOST_MSI;
  788. } else {
  789. intx_host_msi = false;
  790. new_type = ASSIGNED_IRQ_INTX_HOST_INTX;
  791. }
  792. r = kvm_device_intx_assign(kvm_state, dev->dev_id, intx_host_msi,
  793. intx_route.irq);
  794. if (r < 0) {
  795. if (r == -EIO && !(dev->features & ASSIGNED_DEVICE_PREFER_MSI_MASK) &&
  796. dev->cap.available & ASSIGNED_DEVICE_CAP_MSI) {
  797. /* Retry with host-side MSI. There might be an IRQ conflict and
  798. * either the kernel or the device doesn't support sharing. */
  799. error_report("Host-side INTx sharing not supported, "
  800. "using MSI instead.\n"
  801. "Some devices do not to work properly in this mode.");
  802. dev->features |= ASSIGNED_DEVICE_PREFER_MSI_MASK;
  803. goto retry;
  804. }
  805. error_report("Failed to assign irq for \"%s\": %s",
  806. dev->dev.qdev.id, strerror(-r));
  807. error_report("Perhaps you are assigning a device "
  808. "that shares an IRQ with another device?");
  809. return r;
  810. }
  811. dev->intx_route = intx_route;
  812. dev->assigned_irq_type = new_type;
  813. return r;
  814. }
  815. static void deassign_device(AssignedDevice *dev)
  816. {
  817. int r;
  818. r = kvm_device_pci_deassign(kvm_state, dev->dev_id);
  819. assert(r == 0);
  820. }
  821. /* The pci config space got updated. Check if irq numbers have changed
  822. * for our devices
  823. */
  824. static void assigned_dev_update_irq_routing(PCIDevice *dev)
  825. {
  826. AssignedDevice *assigned_dev = DO_UPCAST(AssignedDevice, dev, dev);
  827. Error *err = NULL;
  828. int r;
  829. r = assign_intx(assigned_dev);
  830. if (r < 0) {
  831. qdev_unplug(&dev->qdev, &err);
  832. assert(!err);
  833. }
  834. }
  835. static void assigned_dev_update_msi(PCIDevice *pci_dev)
  836. {
  837. AssignedDevice *assigned_dev = DO_UPCAST(AssignedDevice, dev, pci_dev);
  838. uint8_t ctrl_byte = pci_get_byte(pci_dev->config + pci_dev->msi_cap +
  839. PCI_MSI_FLAGS);
  840. int r;
  841. /* Some guests gratuitously disable MSI even if they're not using it,
  842. * try to catch this by only deassigning irqs if the guest is using
  843. * MSI or intends to start. */
  844. if (assigned_dev->assigned_irq_type == ASSIGNED_IRQ_MSI ||
  845. (ctrl_byte & PCI_MSI_FLAGS_ENABLE)) {
  846. r = kvm_device_msi_deassign(kvm_state, assigned_dev->dev_id);
  847. /* -ENXIO means no assigned irq */
  848. if (r && r != -ENXIO) {
  849. perror("assigned_dev_update_msi: deassign irq");
  850. }
  851. free_msi_virqs(assigned_dev);
  852. assigned_dev->assigned_irq_type = ASSIGNED_IRQ_NONE;
  853. pci_device_set_intx_routing_notifier(pci_dev, NULL);
  854. }
  855. if (ctrl_byte & PCI_MSI_FLAGS_ENABLE) {
  856. MSIMessage msg = msi_get_message(pci_dev, 0);
  857. int virq;
  858. virq = kvm_irqchip_add_msi_route(kvm_state, msg);
  859. if (virq < 0) {
  860. perror("assigned_dev_update_msi: kvm_irqchip_add_msi_route");
  861. return;
  862. }
  863. assigned_dev->msi_virq = g_malloc(sizeof(*assigned_dev->msi_virq));
  864. assigned_dev->msi_virq_nr = 1;
  865. assigned_dev->msi_virq[0] = virq;
  866. if (kvm_device_msi_assign(kvm_state, assigned_dev->dev_id, virq) < 0) {
  867. perror("assigned_dev_update_msi: kvm_device_msi_assign");
  868. }
  869. assigned_dev->intx_route.mode = PCI_INTX_DISABLED;
  870. assigned_dev->intx_route.irq = -1;
  871. assigned_dev->assigned_irq_type = ASSIGNED_IRQ_MSI;
  872. } else {
  873. assign_intx(assigned_dev);
  874. }
  875. }
  876. static bool assigned_dev_msix_masked(MSIXTableEntry *entry)
  877. {
  878. return (entry->ctrl & cpu_to_le32(0x1)) != 0;
  879. }
  880. static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
  881. {
  882. AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
  883. uint16_t entries_nr = 0;
  884. int i, r = 0;
  885. MSIXTableEntry *entry = adev->msix_table;
  886. MSIMessage msg;
  887. /* Get the usable entry number for allocating */
  888. for (i = 0; i < adev->msix_max; i++, entry++) {
  889. if (assigned_dev_msix_masked(entry)) {
  890. continue;
  891. }
  892. entries_nr++;
  893. }
  894. DEBUG("MSI-X entries: %d\n", entries_nr);
  895. /* It's valid to enable MSI-X with all entries masked */
  896. if (!entries_nr) {
  897. return 0;
  898. }
  899. r = kvm_device_msix_init_vectors(kvm_state, adev->dev_id, entries_nr);
  900. if (r != 0) {
  901. error_report("fail to set MSI-X entry number for MSIX! %s",
  902. strerror(-r));
  903. return r;
  904. }
  905. free_msi_virqs(adev);
  906. adev->msi_virq_nr = adev->msix_max;
  907. adev->msi_virq = g_malloc(adev->msix_max * sizeof(*adev->msi_virq));
  908. entry = adev->msix_table;
  909. for (i = 0; i < adev->msix_max; i++, entry++) {
  910. adev->msi_virq[i] = -1;
  911. if (assigned_dev_msix_masked(entry)) {
  912. continue;
  913. }
  914. msg.address = entry->addr_lo | ((uint64_t)entry->addr_hi << 32);
  915. msg.data = entry->data;
  916. r = kvm_irqchip_add_msi_route(kvm_state, msg);
  917. if (r < 0) {
  918. return r;
  919. }
  920. adev->msi_virq[i] = r;
  921. DEBUG("MSI-X vector %d, gsi %d, addr %08x_%08x, data %08x\n", i,
  922. r, entry->addr_hi, entry->addr_lo, entry->data);
  923. r = kvm_device_msix_set_vector(kvm_state, adev->dev_id, i,
  924. adev->msi_virq[i]);
  925. if (r) {
  926. error_report("fail to set MSI-X entry! %s", strerror(-r));
  927. break;
  928. }
  929. }
  930. return r;
  931. }
  932. static void assigned_dev_update_msix(PCIDevice *pci_dev)
  933. {
  934. AssignedDevice *assigned_dev = DO_UPCAST(AssignedDevice, dev, pci_dev);
  935. uint16_t ctrl_word = pci_get_word(pci_dev->config + pci_dev->msix_cap +
  936. PCI_MSIX_FLAGS);
  937. int r;
  938. /* Some guests gratuitously disable MSIX even if they're not using it,
  939. * try to catch this by only deassigning irqs if the guest is using
  940. * MSIX or intends to start. */
  941. if ((assigned_dev->assigned_irq_type == ASSIGNED_IRQ_MSIX) ||
  942. (ctrl_word & PCI_MSIX_FLAGS_ENABLE)) {
  943. r = kvm_device_msix_deassign(kvm_state, assigned_dev->dev_id);
  944. /* -ENXIO means no assigned irq */
  945. if (r && r != -ENXIO) {
  946. perror("assigned_dev_update_msix: deassign irq");
  947. }
  948. free_msi_virqs(assigned_dev);
  949. assigned_dev->assigned_irq_type = ASSIGNED_IRQ_NONE;
  950. pci_device_set_intx_routing_notifier(pci_dev, NULL);
  951. }
  952. if (ctrl_word & PCI_MSIX_FLAGS_ENABLE) {
  953. if (assigned_dev_update_msix_mmio(pci_dev) < 0) {
  954. perror("assigned_dev_update_msix_mmio");
  955. return;
  956. }
  957. if (assigned_dev->msi_virq_nr > 0) {
  958. if (kvm_device_msix_assign(kvm_state, assigned_dev->dev_id) < 0) {
  959. perror("assigned_dev_enable_msix: assign irq");
  960. return;
  961. }
  962. }
  963. assigned_dev->intx_route.mode = PCI_INTX_DISABLED;
  964. assigned_dev->intx_route.irq = -1;
  965. assigned_dev->assigned_irq_type = ASSIGNED_IRQ_MSIX;
  966. } else {
  967. assign_intx(assigned_dev);
  968. }
  969. }
  970. static uint32_t assigned_dev_pci_read_config(PCIDevice *pci_dev,
  971. uint32_t address, int len)
  972. {
  973. AssignedDevice *assigned_dev = DO_UPCAST(AssignedDevice, dev, pci_dev);
  974. uint32_t virt_val = pci_default_read_config(pci_dev, address, len);
  975. uint32_t real_val, emulate_mask, full_emulation_mask;
  976. emulate_mask = 0;
  977. memcpy(&emulate_mask, assigned_dev->emulate_config_read + address, len);
  978. emulate_mask = le32_to_cpu(emulate_mask);
  979. full_emulation_mask = 0xffffffff >> (32 - len * 8);
  980. if (emulate_mask != full_emulation_mask) {
  981. real_val = assigned_dev_pci_read(pci_dev, address, len);
  982. return (virt_val & emulate_mask) | (real_val & ~emulate_mask);
  983. } else {
  984. return virt_val;
  985. }
  986. }
  987. static void assigned_dev_pci_write_config(PCIDevice *pci_dev, uint32_t address,
  988. uint32_t val, int len)
  989. {
  990. AssignedDevice *assigned_dev = DO_UPCAST(AssignedDevice, dev, pci_dev);
  991. uint16_t old_cmd = pci_get_word(pci_dev->config + PCI_COMMAND);
  992. uint32_t emulate_mask, full_emulation_mask;
  993. int ret;
  994. pci_default_write_config(pci_dev, address, val, len);
  995. if (kvm_has_intx_set_mask() &&
  996. range_covers_byte(address, len, PCI_COMMAND + 1)) {
  997. bool intx_masked = (pci_get_word(pci_dev->config + PCI_COMMAND) &
  998. PCI_COMMAND_INTX_DISABLE);
  999. if (intx_masked != !!(old_cmd & PCI_COMMAND_INTX_DISABLE)) {
  1000. ret = kvm_device_intx_set_mask(kvm_state, assigned_dev->dev_id,
  1001. intx_masked);
  1002. if (ret) {
  1003. perror("assigned_dev_pci_write_config: set intx mask");
  1004. }
  1005. }
  1006. }
  1007. if (assigned_dev->cap.available & ASSIGNED_DEVICE_CAP_MSI) {
  1008. if (range_covers_byte(address, len,
  1009. pci_dev->msi_cap + PCI_MSI_FLAGS)) {
  1010. assigned_dev_update_msi(pci_dev);
  1011. }
  1012. }
  1013. if (assigned_dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX) {
  1014. if (range_covers_byte(address, len,
  1015. pci_dev->msix_cap + PCI_MSIX_FLAGS + 1)) {
  1016. assigned_dev_update_msix(pci_dev);
  1017. }
  1018. }
  1019. emulate_mask = 0;
  1020. memcpy(&emulate_mask, assigned_dev->emulate_config_write + address, len);
  1021. emulate_mask = le32_to_cpu(emulate_mask);
  1022. full_emulation_mask = 0xffffffff >> (32 - len * 8);
  1023. if (emulate_mask != full_emulation_mask) {
  1024. if (emulate_mask) {
  1025. val &= ~emulate_mask;
  1026. val |= assigned_dev_pci_read(pci_dev, address, len) & emulate_mask;
  1027. }
  1028. assigned_dev_pci_write(pci_dev, address, val, len);
  1029. }
  1030. }
  1031. static void assigned_dev_setup_cap_read(AssignedDevice *dev, uint32_t offset,
  1032. uint32_t len)
  1033. {
  1034. assigned_dev_direct_config_read(dev, offset, len);
  1035. assigned_dev_emulate_config_read(dev, offset + PCI_CAP_LIST_NEXT, 1);
  1036. }
  1037. static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
  1038. {
  1039. AssignedDevice *dev = DO_UPCAST(AssignedDevice, dev, pci_dev);
  1040. PCIRegion *pci_region = dev->real_device.regions;
  1041. int ret, pos;
  1042. /* Clear initial capabilities pointer and status copied from hw */
  1043. pci_set_byte(pci_dev->config + PCI_CAPABILITY_LIST, 0);
  1044. pci_set_word(pci_dev->config + PCI_STATUS,
  1045. pci_get_word(pci_dev->config + PCI_STATUS) &
  1046. ~PCI_STATUS_CAP_LIST);
  1047. /* Expose MSI capability
  1048. * MSI capability is the 1st capability in capability config */
  1049. pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_MSI, 0);
  1050. if (pos != 0 && kvm_check_extension(kvm_state, KVM_CAP_ASSIGN_DEV_IRQ)) {
  1051. if (!check_irqchip_in_kernel()) {
  1052. return -ENOTSUP;
  1053. }
  1054. dev->cap.available |= ASSIGNED_DEVICE_CAP_MSI;
  1055. /* Only 32-bit/no-mask currently supported */
  1056. ret = pci_add_capability(pci_dev, PCI_CAP_ID_MSI, pos, 10);
  1057. if (ret < 0) {
  1058. return ret;
  1059. }
  1060. pci_dev->msi_cap = pos;
  1061. pci_set_word(pci_dev->config + pos + PCI_MSI_FLAGS,
  1062. pci_get_word(pci_dev->config + pos + PCI_MSI_FLAGS) &
  1063. PCI_MSI_FLAGS_QMASK);
  1064. pci_set_long(pci_dev->config + pos + PCI_MSI_ADDRESS_LO, 0);
  1065. pci_set_word(pci_dev->config + pos + PCI_MSI_DATA_32, 0);
  1066. /* Set writable fields */
  1067. pci_set_word(pci_dev->wmask + pos + PCI_MSI_FLAGS,
  1068. PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
  1069. pci_set_long(pci_dev->wmask + pos + PCI_MSI_ADDRESS_LO, 0xfffffffc);
  1070. pci_set_word(pci_dev->wmask + pos + PCI_MSI_DATA_32, 0xffff);
  1071. }
  1072. /* Expose MSI-X capability */
  1073. pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_MSIX, 0);
  1074. if (pos != 0 && kvm_device_msix_supported(kvm_state)) {
  1075. int bar_nr;
  1076. uint32_t msix_table_entry;
  1077. if (!check_irqchip_in_kernel()) {
  1078. return -ENOTSUP;
  1079. }
  1080. dev->cap.available |= ASSIGNED_DEVICE_CAP_MSIX;
  1081. ret = pci_add_capability(pci_dev, PCI_CAP_ID_MSIX, pos, 12);
  1082. if (ret < 0) {
  1083. return ret;
  1084. }
  1085. pci_dev->msix_cap = pos;
  1086. pci_set_word(pci_dev->config + pos + PCI_MSIX_FLAGS,
  1087. pci_get_word(pci_dev->config + pos + PCI_MSIX_FLAGS) &
  1088. PCI_MSIX_FLAGS_QSIZE);
  1089. /* Only enable and function mask bits are writable */
  1090. pci_set_word(pci_dev->wmask + pos + PCI_MSIX_FLAGS,
  1091. PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
  1092. msix_table_entry = pci_get_long(pci_dev->config + pos + PCI_MSIX_TABLE);
  1093. bar_nr = msix_table_entry & PCI_MSIX_FLAGS_BIRMASK;
  1094. msix_table_entry &= ~PCI_MSIX_FLAGS_BIRMASK;
  1095. dev->msix_table_addr = pci_region[bar_nr].base_addr + msix_table_entry;
  1096. dev->msix_max = pci_get_word(pci_dev->config + pos + PCI_MSIX_FLAGS);
  1097. dev->msix_max &= PCI_MSIX_FLAGS_QSIZE;
  1098. dev->msix_max += 1;
  1099. }
  1100. /* Minimal PM support, nothing writable, device appears to NAK changes */
  1101. pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_PM, 0);
  1102. if (pos) {
  1103. uint16_t pmc;
  1104. ret = pci_add_capability(pci_dev, PCI_CAP_ID_PM, pos, PCI_PM_SIZEOF);
  1105. if (ret < 0) {
  1106. return ret;
  1107. }
  1108. assigned_dev_setup_cap_read(dev, pos, PCI_PM_SIZEOF);
  1109. pmc = pci_get_word(pci_dev->config + pos + PCI_CAP_FLAGS);
  1110. pmc &= (PCI_PM_CAP_VER_MASK | PCI_PM_CAP_DSI);
  1111. pci_set_word(pci_dev->config + pos + PCI_CAP_FLAGS, pmc);
  1112. /* assign_device will bring the device up to D0, so we don't need
  1113. * to worry about doing that ourselves here. */
  1114. pci_set_word(pci_dev->config + pos + PCI_PM_CTRL,
  1115. PCI_PM_CTRL_NO_SOFT_RESET);
  1116. pci_set_byte(pci_dev->config + pos + PCI_PM_PPB_EXTENSIONS, 0);
  1117. pci_set_byte(pci_dev->config + pos + PCI_PM_DATA_REGISTER, 0);
  1118. }
  1119. pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0);
  1120. if (pos) {
  1121. uint8_t version, size = 0;
  1122. uint16_t type, devctl, lnksta;
  1123. uint32_t devcap, lnkcap;
  1124. version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS);
  1125. version &= PCI_EXP_FLAGS_VERS;
  1126. if (version == 1) {
  1127. size = 0x14;
  1128. } else if (version == 2) {
  1129. /*
  1130. * Check for non-std size, accept reduced size to 0x34,
  1131. * which is what bcm5761 implemented, violating the
  1132. * PCIe v3.0 spec that regs should exist and be read as 0,
  1133. * not optionally provided and shorten the struct size.
  1134. */
  1135. size = MIN(0x3c, PCI_CONFIG_SPACE_SIZE - pos);
  1136. if (size < 0x34) {
  1137. error_report("%s: Invalid size PCIe cap-id 0x%x",
  1138. __func__, PCI_CAP_ID_EXP);
  1139. return -EINVAL;
  1140. } else if (size != 0x3c) {
  1141. error_report("WARNING, %s: PCIe cap-id 0x%x has "
  1142. "non-standard size 0x%x; std size should be 0x3c",
  1143. __func__, PCI_CAP_ID_EXP, size);
  1144. }
  1145. } else if (version == 0) {
  1146. uint16_t vid, did;
  1147. vid = pci_get_word(pci_dev->config + PCI_VENDOR_ID);
  1148. did = pci_get_word(pci_dev->config + PCI_DEVICE_ID);
  1149. if (vid == PCI_VENDOR_ID_INTEL && did == 0x10ed) {
  1150. /*
  1151. * quirk for Intel 82599 VF with invalid PCIe capability
  1152. * version, should really be version 2 (same as PF)
  1153. */
  1154. size = 0x3c;
  1155. }
  1156. }
  1157. if (size == 0) {
  1158. error_report("%s: Unsupported PCI express capability version %d",
  1159. __func__, version);
  1160. return -EINVAL;
  1161. }
  1162. ret = pci_add_capability(pci_dev, PCI_CAP_ID_EXP, pos, size);
  1163. if (ret < 0) {
  1164. return ret;
  1165. }
  1166. assigned_dev_setup_cap_read(dev, pos, size);
  1167. type = pci_get_word(pci_dev->config + pos + PCI_EXP_FLAGS);
  1168. type = (type & PCI_EXP_FLAGS_TYPE) >> 4;
  1169. if (type != PCI_EXP_TYPE_ENDPOINT &&
  1170. type != PCI_EXP_TYPE_LEG_END && type != PCI_EXP_TYPE_RC_END) {
  1171. error_report("Device assignment only supports endpoint assignment,"
  1172. " device type %d", type);
  1173. return -EINVAL;
  1174. }
  1175. /* capabilities, pass existing read-only copy
  1176. * PCI_EXP_FLAGS_IRQ: updated by hardware, should be direct read */
  1177. /* device capabilities: hide FLR */
  1178. devcap = pci_get_long(pci_dev->config + pos + PCI_EXP_DEVCAP);
  1179. devcap &= ~PCI_EXP_DEVCAP_FLR;
  1180. pci_set_long(pci_dev->config + pos + PCI_EXP_DEVCAP, devcap);
  1181. /* device control: clear all error reporting enable bits, leaving
  1182. * only a few host values. Note, these are
  1183. * all writable, but not passed to hw.
  1184. */
  1185. devctl = pci_get_word(pci_dev->config + pos + PCI_EXP_DEVCTL);
  1186. devctl = (devctl & (PCI_EXP_DEVCTL_READRQ | PCI_EXP_DEVCTL_PAYLOAD)) |
  1187. PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
  1188. pci_set_word(pci_dev->config + pos + PCI_EXP_DEVCTL, devctl);
  1189. devctl = PCI_EXP_DEVCTL_BCR_FLR | PCI_EXP_DEVCTL_AUX_PME;
  1190. pci_set_word(pci_dev->wmask + pos + PCI_EXP_DEVCTL, ~devctl);
  1191. /* Clear device status */
  1192. pci_set_word(pci_dev->config + pos + PCI_EXP_DEVSTA, 0);
  1193. /* Link capabilities, expose links and latencues, clear reporting */
  1194. lnkcap = pci_get_long(pci_dev->config + pos + PCI_EXP_LNKCAP);
  1195. lnkcap &= (PCI_EXP_LNKCAP_SLS | PCI_EXP_LNKCAP_MLW |
  1196. PCI_EXP_LNKCAP_ASPMS | PCI_EXP_LNKCAP_L0SEL |
  1197. PCI_EXP_LNKCAP_L1EL);
  1198. pci_set_long(pci_dev->config + pos + PCI_EXP_LNKCAP, lnkcap);
  1199. /* Link control, pass existing read-only copy. Should be writable? */
  1200. /* Link status, only expose current speed and width */
  1201. lnksta = pci_get_word(pci_dev->config + pos + PCI_EXP_LNKSTA);
  1202. lnksta &= (PCI_EXP_LNKSTA_CLS | PCI_EXP_LNKSTA_NLW);
  1203. pci_set_word(pci_dev->config + pos + PCI_EXP_LNKSTA, lnksta);
  1204. if (version >= 2) {
  1205. /* Slot capabilities, control, status - not needed for endpoints */
  1206. pci_set_long(pci_dev->config + pos + PCI_EXP_SLTCAP, 0);
  1207. pci_set_word(pci_dev->config + pos + PCI_EXP_SLTCTL, 0);
  1208. pci_set_word(pci_dev->config + pos + PCI_EXP_SLTSTA, 0);
  1209. /* Root control, capabilities, status - not needed for endpoints */
  1210. pci_set_word(pci_dev->config + pos + PCI_EXP_RTCTL, 0);
  1211. pci_set_word(pci_dev->config + pos + PCI_EXP_RTCAP, 0);
  1212. pci_set_long(pci_dev->config + pos + PCI_EXP_RTSTA, 0);
  1213. /* Device capabilities/control 2, pass existing read-only copy */
  1214. /* Link control 2, pass existing read-only copy */
  1215. }
  1216. }
  1217. pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_PCIX, 0);
  1218. if (pos) {
  1219. uint16_t cmd;
  1220. uint32_t status;
  1221. /* Only expose the minimum, 8 byte capability */
  1222. ret = pci_add_capability(pci_dev, PCI_CAP_ID_PCIX, pos, 8);
  1223. if (ret < 0) {
  1224. return ret;
  1225. }
  1226. assigned_dev_setup_cap_read(dev, pos, 8);
  1227. /* Command register, clear upper bits, including extended modes */
  1228. cmd = pci_get_word(pci_dev->config + pos + PCI_X_CMD);
  1229. cmd &= (PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO | PCI_X_CMD_MAX_READ |
  1230. PCI_X_CMD_MAX_SPLIT);
  1231. pci_set_word(pci_dev->config + pos + PCI_X_CMD, cmd);
  1232. /* Status register, update with emulated PCI bus location, clear
  1233. * error bits, leave the rest. */
  1234. status = pci_get_long(pci_dev->config + pos + PCI_X_STATUS);
  1235. status &= ~(PCI_X_STATUS_BUS | PCI_X_STATUS_DEVFN);
  1236. status |= (pci_bus_num(pci_dev->bus) << 8) | pci_dev->devfn;
  1237. status &= ~(PCI_X_STATUS_SPL_DISC | PCI_X_STATUS_UNX_SPL |
  1238. PCI_X_STATUS_SPL_ERR);
  1239. pci_set_long(pci_dev->config + pos + PCI_X_STATUS, status);
  1240. }
  1241. pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_VPD, 0);
  1242. if (pos) {
  1243. /* Direct R/W passthrough */
  1244. ret = pci_add_capability(pci_dev, PCI_CAP_ID_VPD, pos, 8);
  1245. if (ret < 0) {
  1246. return ret;
  1247. }
  1248. assigned_dev_setup_cap_read(dev, pos, 8);
  1249. /* direct write for cap content */
  1250. assigned_dev_direct_config_write(dev, pos + 2, 6);
  1251. }
  1252. /* Devices can have multiple vendor capabilities, get them all */
  1253. for (pos = 0; (pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_VNDR, pos));
  1254. pos += PCI_CAP_LIST_NEXT) {
  1255. uint8_t len = pci_get_byte(pci_dev->config + pos + PCI_CAP_FLAGS);
  1256. /* Direct R/W passthrough */
  1257. ret = pci_add_capability(pci_dev, PCI_CAP_ID_VNDR, pos, len);
  1258. if (ret < 0) {
  1259. return ret;
  1260. }
  1261. assigned_dev_setup_cap_read(dev, pos, len);
  1262. /* direct write for cap content */
  1263. assigned_dev_direct_config_write(dev, pos + 2, len - 2);
  1264. }
  1265. /* If real and virtual capability list status bits differ, virtualize the
  1266. * access. */
  1267. if ((pci_get_word(pci_dev->config + PCI_STATUS) & PCI_STATUS_CAP_LIST) !=
  1268. (assigned_dev_pci_read_byte(pci_dev, PCI_STATUS) &
  1269. PCI_STATUS_CAP_LIST)) {
  1270. dev->emulate_config_read[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
  1271. }
  1272. return 0;
  1273. }
  1274. static uint64_t
  1275. assigned_dev_msix_mmio_read(void *opaque, hwaddr addr,
  1276. unsigned size)
  1277. {
  1278. AssignedDevice *adev = opaque;
  1279. uint64_t val;
  1280. memcpy(&val, (void *)((uint8_t *)adev->msix_table + addr), size);
  1281. return val;
  1282. }
  1283. static void assigned_dev_msix_mmio_write(void *opaque, hwaddr addr,
  1284. uint64_t val, unsigned size)
  1285. {
  1286. AssignedDevice *adev = opaque;
  1287. PCIDevice *pdev = &adev->dev;
  1288. uint16_t ctrl;
  1289. MSIXTableEntry orig;
  1290. int i = addr >> 4;
  1291. if (i >= adev->msix_max) {
  1292. return; /* Drop write */
  1293. }
  1294. ctrl = pci_get_word(pdev->config + pdev->msix_cap + PCI_MSIX_FLAGS);
  1295. DEBUG("write to MSI-X table offset 0x%lx, val 0x%lx\n", addr, val);
  1296. if (ctrl & PCI_MSIX_FLAGS_ENABLE) {
  1297. orig = adev->msix_table[i];
  1298. }
  1299. memcpy((uint8_t *)adev->msix_table + addr, &val, size);
  1300. if (ctrl & PCI_MSIX_FLAGS_ENABLE) {
  1301. MSIXTableEntry *entry = &adev->msix_table[i];
  1302. if (!assigned_dev_msix_masked(&orig) &&
  1303. assigned_dev_msix_masked(entry)) {
  1304. /*
  1305. * Vector masked, disable it
  1306. *
  1307. * XXX It's not clear if we can or should actually attempt
  1308. * to mask or disable the interrupt. KVM doesn't have
  1309. * support for pending bits and kvm_assign_set_msix_entry
  1310. * doesn't modify the device hardware mask. Interrupts
  1311. * while masked are simply not injected to the guest, so
  1312. * are lost. Can we get away with always injecting an
  1313. * interrupt on unmask?
  1314. */
  1315. } else if (assigned_dev_msix_masked(&orig) &&
  1316. !assigned_dev_msix_masked(entry)) {
  1317. /* Vector unmasked */
  1318. if (i >= adev->msi_virq_nr || adev->msi_virq[i] < 0) {
  1319. /* Previously unassigned vector, start from scratch */
  1320. assigned_dev_update_msix(pdev);
  1321. return;
  1322. } else {
  1323. /* Update an existing, previously masked vector */
  1324. MSIMessage msg;
  1325. int ret;
  1326. msg.address = entry->addr_lo |
  1327. ((uint64_t)entry->addr_hi << 32);
  1328. msg.data = entry->data;
  1329. ret = kvm_irqchip_update_msi_route(kvm_state,
  1330. adev->msi_virq[i], msg);
  1331. if (ret) {
  1332. error_report("Error updating irq routing entry (%d)", ret);
  1333. }
  1334. }
  1335. }
  1336. }
  1337. }
  1338. static const MemoryRegionOps assigned_dev_msix_mmio_ops = {
  1339. .read = assigned_dev_msix_mmio_read,
  1340. .write = assigned_dev_msix_mmio_write,
  1341. .endianness = DEVICE_NATIVE_ENDIAN,
  1342. .valid = {
  1343. .min_access_size = 4,
  1344. .max_access_size = 8,
  1345. },
  1346. .impl = {
  1347. .min_access_size = 4,
  1348. .max_access_size = 8,
  1349. },
  1350. };
  1351. static void assigned_dev_msix_reset(AssignedDevice *dev)
  1352. {
  1353. MSIXTableEntry *entry;
  1354. int i;
  1355. if (!dev->msix_table) {
  1356. return;
  1357. }
  1358. memset(dev->msix_table, 0, MSIX_PAGE_SIZE);
  1359. for (i = 0, entry = dev->msix_table; i < dev->msix_max; i++, entry++) {
  1360. entry->ctrl = cpu_to_le32(0x1); /* Masked */
  1361. }
  1362. }
  1363. static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
  1364. {
  1365. dev->msix_table = mmap(NULL, MSIX_PAGE_SIZE, PROT_READ|PROT_WRITE,
  1366. MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
  1367. if (dev->msix_table == MAP_FAILED) {
  1368. error_report("fail allocate msix_table! %s", strerror(errno));
  1369. return -EFAULT;
  1370. }
  1371. assigned_dev_msix_reset(dev);
  1372. memory_region_init_io(&dev->mmio, &assigned_dev_msix_mmio_ops, dev,
  1373. "assigned-dev-msix", MSIX_PAGE_SIZE);
  1374. return 0;
  1375. }
  1376. static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev)
  1377. {
  1378. if (!dev->msix_table) {
  1379. return;
  1380. }
  1381. memory_region_destroy(&dev->mmio);
  1382. if (munmap(dev->msix_table, MSIX_PAGE_SIZE) == -1) {
  1383. error_report("error unmapping msix_table! %s", strerror(errno));
  1384. }
  1385. dev->msix_table = NULL;
  1386. }
  1387. static const VMStateDescription vmstate_assigned_device = {
  1388. .name = "pci-assign",
  1389. .unmigratable = 1,
  1390. };
  1391. static void reset_assigned_device(DeviceState *dev)
  1392. {
  1393. PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
  1394. AssignedDevice *adev = DO_UPCAST(AssignedDevice, dev, pci_dev);
  1395. char reset_file[64];
  1396. const char reset[] = "1";
  1397. int fd, ret;
  1398. /*
  1399. * If a guest is reset without being shutdown, MSI/MSI-X can still
  1400. * be running. We want to return the device to a known state on
  1401. * reset, so disable those here. We especially do not want MSI-X
  1402. * enabled since it lives in MMIO space, which is about to get
  1403. * disabled.
  1404. */
  1405. if (adev->assigned_irq_type == ASSIGNED_IRQ_MSIX) {
  1406. uint16_t ctrl = pci_get_word(pci_dev->config +
  1407. pci_dev->msix_cap + PCI_MSIX_FLAGS);
  1408. pci_set_word(pci_dev->config + pci_dev->msix_cap + PCI_MSIX_FLAGS,
  1409. ctrl & ~PCI_MSIX_FLAGS_ENABLE);
  1410. assigned_dev_update_msix(pci_dev);
  1411. } else if (adev->assigned_irq_type == ASSIGNED_IRQ_MSI) {
  1412. uint8_t ctrl = pci_get_byte(pci_dev->config +
  1413. pci_dev->msi_cap + PCI_MSI_FLAGS);
  1414. pci_set_byte(pci_dev->config + pci_dev->msi_cap + PCI_MSI_FLAGS,
  1415. ctrl & ~PCI_MSI_FLAGS_ENABLE);
  1416. assigned_dev_update_msi(pci_dev);
  1417. }
  1418. snprintf(reset_file, sizeof(reset_file),
  1419. "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/reset",
  1420. adev->host.domain, adev->host.bus, adev->host.slot,
  1421. adev->host.function);
  1422. /*
  1423. * Issue a device reset via pci-sysfs. Note that we use write(2) here
  1424. * and ignore the return value because some kernels have a bug that
  1425. * returns 0 rather than bytes written on success, sending us into an
  1426. * infinite retry loop using other write mechanisms.
  1427. */
  1428. fd = open(reset_file, O_WRONLY);
  1429. if (fd != -1) {
  1430. ret = write(fd, reset, strlen(reset));
  1431. (void)ret;
  1432. close(fd);
  1433. }
  1434. /*
  1435. * When a 0 is written to the bus master register, the device is logically
  1436. * disconnected from the PCI bus. This avoids further DMA transfers.
  1437. */
  1438. assigned_dev_pci_write_config(pci_dev, PCI_COMMAND, 0, 1);
  1439. }
  1440. static int assigned_initfn(struct PCIDevice *pci_dev)
  1441. {
  1442. AssignedDevice *dev = DO_UPCAST(AssignedDevice, dev, pci_dev);
  1443. uint8_t e_intx;
  1444. int r;
  1445. if (!kvm_enabled()) {
  1446. error_report("pci-assign: error: requires KVM support");
  1447. return -1;
  1448. }
  1449. if (!dev->host.domain && !dev->host.bus && !dev->host.slot &&
  1450. !dev->host.function) {
  1451. error_report("pci-assign: error: no host device specified");
  1452. return -1;
  1453. }
  1454. /*
  1455. * Set up basic config space access control. Will be further refined during
  1456. * device initialization.
  1457. */
  1458. assigned_dev_emulate_config_read(dev, 0, PCI_CONFIG_SPACE_SIZE);
  1459. assigned_dev_direct_config_read(dev, PCI_STATUS, 2);
  1460. assigned_dev_direct_config_read(dev, PCI_REVISION_ID, 1);
  1461. assigned_dev_direct_config_read(dev, PCI_CLASS_PROG, 3);
  1462. assigned_dev_direct_config_read(dev, PCI_CACHE_LINE_SIZE, 1);
  1463. assigned_dev_direct_config_read(dev, PCI_LATENCY_TIMER, 1);
  1464. assigned_dev_direct_config_read(dev, PCI_BIST, 1);
  1465. assigned_dev_direct_config_read(dev, PCI_CARDBUS_CIS, 4);
  1466. assigned_dev_direct_config_read(dev, PCI_SUBSYSTEM_VENDOR_ID, 2);
  1467. assigned_dev_direct_config_read(dev, PCI_SUBSYSTEM_ID, 2);
  1468. assigned_dev_direct_config_read(dev, PCI_CAPABILITY_LIST + 1, 7);
  1469. assigned_dev_direct_config_read(dev, PCI_MIN_GNT, 1);
  1470. assigned_dev_direct_config_read(dev, PCI_MAX_LAT, 1);
  1471. memcpy(dev->emulate_config_write, dev->emulate_config_read,
  1472. sizeof(dev->emulate_config_read));
  1473. if (get_real_device(dev, dev->host.domain, dev->host.bus,
  1474. dev->host.slot, dev->host.function)) {
  1475. error_report("pci-assign: Error: Couldn't get real device (%s)!",
  1476. dev->dev.qdev.id);
  1477. goto out;
  1478. }
  1479. if (assigned_device_pci_cap_init(pci_dev) < 0) {
  1480. goto out;
  1481. }
  1482. /* intercept MSI-X entry page in the MMIO */
  1483. if (dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX) {
  1484. if (assigned_dev_register_msix_mmio(dev)) {
  1485. goto out;
  1486. }
  1487. }
  1488. /* handle real device's MMIO/PIO BARs */
  1489. if (assigned_dev_register_regions(dev->real_device.regions,
  1490. dev->real_device.region_number,
  1491. dev)) {
  1492. goto out;
  1493. }
  1494. /* handle interrupt routing */
  1495. e_intx = dev->dev.config[PCI_INTERRUPT_PIN] - 1;
  1496. dev->intpin = e_intx;
  1497. dev->intx_route.mode = PCI_INTX_DISABLED;
  1498. dev->intx_route.irq = -1;
  1499. /* assign device to guest */
  1500. r = assign_device(dev);
  1501. if (r < 0) {
  1502. goto out;
  1503. }
  1504. /* assign legacy INTx to the device */
  1505. r = assign_intx(dev);
  1506. if (r < 0) {
  1507. goto assigned_out;
  1508. }
  1509. assigned_dev_load_option_rom(dev);
  1510. add_boot_device_path(dev->bootindex, &pci_dev->qdev, NULL);
  1511. return 0;
  1512. assigned_out:
  1513. deassign_device(dev);
  1514. out:
  1515. free_assigned_device(dev);
  1516. return -1;
  1517. }
  1518. static void assigned_exitfn(struct PCIDevice *pci_dev)
  1519. {
  1520. AssignedDevice *dev = DO_UPCAST(AssignedDevice, dev, pci_dev);
  1521. deassign_device(dev);
  1522. free_assigned_device(dev);
  1523. }
  1524. static Property assigned_dev_properties[] = {
  1525. DEFINE_PROP_PCI_HOST_DEVADDR("host", AssignedDevice, host),
  1526. DEFINE_PROP_BIT("prefer_msi", AssignedDevice, features,
  1527. ASSIGNED_DEVICE_PREFER_MSI_BIT, false),
  1528. DEFINE_PROP_BIT("share_intx", AssignedDevice, features,
  1529. ASSIGNED_DEVICE_SHARE_INTX_BIT, true),
  1530. DEFINE_PROP_INT32("bootindex", AssignedDevice, bootindex, -1),
  1531. DEFINE_PROP_STRING("configfd", AssignedDevice, configfd_name),
  1532. DEFINE_PROP_END_OF_LIST(),
  1533. };
  1534. static void assign_class_init(ObjectClass *klass, void *data)
  1535. {
  1536. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  1537. DeviceClass *dc = DEVICE_CLASS(klass);
  1538. k->init = assigned_initfn;
  1539. k->exit = assigned_exitfn;
  1540. k->config_read = assigned_dev_pci_read_config;
  1541. k->config_write = assigned_dev_pci_write_config;
  1542. dc->props = assigned_dev_properties;
  1543. dc->vmsd = &vmstate_assigned_device;
  1544. dc->reset = reset_assigned_device;
  1545. dc->desc = "KVM-based PCI passthrough";
  1546. }
  1547. static const TypeInfo assign_info = {
  1548. .name = "kvm-pci-assign",
  1549. .parent = TYPE_PCI_DEVICE,
  1550. .instance_size = sizeof(AssignedDevice),
  1551. .class_init = assign_class_init,
  1552. };
  1553. static void assign_register_types(void)
  1554. {
  1555. type_register_static(&assign_info);
  1556. }
  1557. type_init(assign_register_types)
  1558. /*
  1559. * Scan the assigned devices for the devices that have an option ROM, and then
  1560. * load the corresponding ROM data to RAM. If an error occurs while loading an
  1561. * option ROM, we just ignore that option ROM and continue with the next one.
  1562. */
  1563. static void assigned_dev_load_option_rom(AssignedDevice *dev)
  1564. {
  1565. char name[32], rom_file[64];
  1566. FILE *fp;
  1567. uint8_t val;
  1568. struct stat st;
  1569. void *ptr;
  1570. /* If loading ROM from file, pci handles it */
  1571. if (dev->dev.romfile || !dev->dev.rom_bar) {
  1572. return;
  1573. }
  1574. snprintf(rom_file, sizeof(rom_file),
  1575. "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/rom",
  1576. dev->host.domain, dev->host.bus, dev->host.slot,
  1577. dev->host.function);
  1578. if (stat(rom_file, &st)) {
  1579. return;
  1580. }
  1581. if (access(rom_file, F_OK)) {
  1582. error_report("pci-assign: Insufficient privileges for %s", rom_file);
  1583. return;
  1584. }
  1585. /* Write "1" to the ROM file to enable it */
  1586. fp = fopen(rom_file, "r+");
  1587. if (fp == NULL) {
  1588. return;
  1589. }
  1590. val = 1;
  1591. if (fwrite(&val, 1, 1, fp) != 1) {
  1592. goto close_rom;
  1593. }
  1594. fseek(fp, 0, SEEK_SET);
  1595. snprintf(name, sizeof(name), "%s.rom",
  1596. object_get_typename(OBJECT(dev)));
  1597. memory_region_init_ram(&dev->dev.rom, name, st.st_size);
  1598. vmstate_register_ram(&dev->dev.rom, &dev->dev.qdev);
  1599. ptr = memory_region_get_ram_ptr(&dev->dev.rom);
  1600. memset(ptr, 0xff, st.st_size);
  1601. if (!fread(ptr, 1, st.st_size, fp)) {
  1602. error_report("pci-assign: Cannot read from host %s\n"
  1603. "\tDevice option ROM contents are probably invalid "
  1604. "(check dmesg).\n\tSkip option ROM probe with rombar=0, "
  1605. "or load from file with romfile=", rom_file);
  1606. memory_region_destroy(&dev->dev.rom);
  1607. goto close_rom;
  1608. }
  1609. pci_register_bar(&dev->dev, PCI_ROM_SLOT, 0, &dev->dev.rom);
  1610. dev->dev.has_rom = true;
  1611. close_rom:
  1612. /* Write "0" to disable ROM */
  1613. fseek(fp, 0, SEEK_SET);
  1614. val = 0;
  1615. if (!fwrite(&val, 1, 1, fp)) {
  1616. DEBUG("%s\n", "Failed to disable pci-sysfs rom file");
  1617. }
  1618. fclose(fp);
  1619. }