2
0

virtio-pci.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. * Virtio PCI Bindings
  3. *
  4. * Copyright IBM, Corp. 2007
  5. * Copyright (c) 2009 CodeSourcery
  6. *
  7. * Authors:
  8. * Anthony Liguori <aliguori@us.ibm.com>
  9. * Paul Brook <paul@codesourcery.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2. See
  12. * the COPYING file in the top-level directory.
  13. *
  14. */
  15. #include <inttypes.h>
  16. #include "virtio.h"
  17. #include "virtio-blk.h"
  18. #include "virtio-net.h"
  19. #include "virtio-serial.h"
  20. #include "pci.h"
  21. #include "qemu-error.h"
  22. #include "msix.h"
  23. #include "net.h"
  24. #include "loader.h"
  25. #include "kvm.h"
  26. #include "blockdev.h"
  27. #include "virtio-pci.h"
  28. /* from Linux's linux/virtio_pci.h */
  29. /* A 32-bit r/o bitmask of the features supported by the host */
  30. #define VIRTIO_PCI_HOST_FEATURES 0
  31. /* A 32-bit r/w bitmask of features activated by the guest */
  32. #define VIRTIO_PCI_GUEST_FEATURES 4
  33. /* A 32-bit r/w PFN for the currently selected queue */
  34. #define VIRTIO_PCI_QUEUE_PFN 8
  35. /* A 16-bit r/o queue size for the currently selected queue */
  36. #define VIRTIO_PCI_QUEUE_NUM 12
  37. /* A 16-bit r/w queue selector */
  38. #define VIRTIO_PCI_QUEUE_SEL 14
  39. /* A 16-bit r/w queue notifier */
  40. #define VIRTIO_PCI_QUEUE_NOTIFY 16
  41. /* An 8-bit device status register. */
  42. #define VIRTIO_PCI_STATUS 18
  43. /* An 8-bit r/o interrupt status register. Reading the value will return the
  44. * current contents of the ISR and will also clear it. This is effectively
  45. * a read-and-acknowledge. */
  46. #define VIRTIO_PCI_ISR 19
  47. /* MSI-X registers: only enabled if MSI-X is enabled. */
  48. /* A 16-bit vector for configuration changes. */
  49. #define VIRTIO_MSI_CONFIG_VECTOR 20
  50. /* A 16-bit vector for selected queue notifications. */
  51. #define VIRTIO_MSI_QUEUE_VECTOR 22
  52. /* Config space size */
  53. #define VIRTIO_PCI_CONFIG_NOMSI 20
  54. #define VIRTIO_PCI_CONFIG_MSI 24
  55. #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
  56. VIRTIO_PCI_CONFIG_MSI : \
  57. VIRTIO_PCI_CONFIG_NOMSI)
  58. /* The remaining space is defined by each driver as the per-driver
  59. * configuration space */
  60. #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
  61. VIRTIO_PCI_CONFIG_MSI : \
  62. VIRTIO_PCI_CONFIG_NOMSI)
  63. /* How many bits to shift physical queue address written to QUEUE_PFN.
  64. * 12 is historical, and due to x86 page size. */
  65. #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
  66. /* Flags track per-device state like workarounds for quirks in older guests. */
  67. #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
  68. /* Performance improves when virtqueue kick processing is decoupled from the
  69. * vcpu thread using ioeventfd for some devices. */
  70. #define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
  71. #define VIRTIO_PCI_FLAG_USE_IOEVENTFD (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT)
  72. /* QEMU doesn't strictly need write barriers since everything runs in
  73. * lock-step. We'll leave the calls to wmb() in though to make it obvious for
  74. * KVM or if kqemu gets SMP support.
  75. */
  76. #define wmb() do { } while (0)
  77. /* virtio device */
  78. static void virtio_pci_notify(void *opaque, uint16_t vector)
  79. {
  80. VirtIOPCIProxy *proxy = opaque;
  81. if (msix_enabled(&proxy->pci_dev))
  82. msix_notify(&proxy->pci_dev, vector);
  83. else
  84. qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
  85. }
  86. static void virtio_pci_save_config(void * opaque, QEMUFile *f)
  87. {
  88. VirtIOPCIProxy *proxy = opaque;
  89. pci_device_save(&proxy->pci_dev, f);
  90. msix_save(&proxy->pci_dev, f);
  91. if (msix_present(&proxy->pci_dev))
  92. qemu_put_be16(f, proxy->vdev->config_vector);
  93. }
  94. static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
  95. {
  96. VirtIOPCIProxy *proxy = opaque;
  97. if (msix_present(&proxy->pci_dev))
  98. qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
  99. }
  100. static int virtio_pci_load_config(void * opaque, QEMUFile *f)
  101. {
  102. VirtIOPCIProxy *proxy = opaque;
  103. int ret;
  104. ret = pci_device_load(&proxy->pci_dev, f);
  105. if (ret) {
  106. return ret;
  107. }
  108. msix_load(&proxy->pci_dev, f);
  109. if (msix_present(&proxy->pci_dev)) {
  110. qemu_get_be16s(f, &proxy->vdev->config_vector);
  111. } else {
  112. proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
  113. }
  114. if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
  115. return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
  116. }
  117. return 0;
  118. }
  119. static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
  120. {
  121. VirtIOPCIProxy *proxy = opaque;
  122. uint16_t vector;
  123. if (msix_present(&proxy->pci_dev)) {
  124. qemu_get_be16s(f, &vector);
  125. } else {
  126. vector = VIRTIO_NO_VECTOR;
  127. }
  128. virtio_queue_set_vector(proxy->vdev, n, vector);
  129. if (vector != VIRTIO_NO_VECTOR) {
  130. return msix_vector_use(&proxy->pci_dev, vector);
  131. }
  132. return 0;
  133. }
  134. static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
  135. int n, bool assign)
  136. {
  137. VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
  138. EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
  139. int r;
  140. if (assign) {
  141. r = event_notifier_init(notifier, 1);
  142. if (r < 0) {
  143. error_report("%s: unable to init event notifier: %d",
  144. __func__, r);
  145. return r;
  146. }
  147. r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier),
  148. proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY,
  149. n, assign);
  150. if (r < 0) {
  151. error_report("%s: unable to map ioeventfd: %d",
  152. __func__, r);
  153. event_notifier_cleanup(notifier);
  154. }
  155. } else {
  156. r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier),
  157. proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY,
  158. n, assign);
  159. if (r < 0) {
  160. error_report("%s: unable to unmap ioeventfd: %d",
  161. __func__, r);
  162. return r;
  163. }
  164. /* Handle the race condition where the guest kicked and we deassigned
  165. * before we got around to handling the kick.
  166. */
  167. if (event_notifier_test_and_clear(notifier)) {
  168. virtio_queue_notify_vq(vq);
  169. }
  170. event_notifier_cleanup(notifier);
  171. }
  172. return r;
  173. }
  174. static void virtio_pci_host_notifier_read(void *opaque)
  175. {
  176. VirtQueue *vq = opaque;
  177. EventNotifier *n = virtio_queue_get_host_notifier(vq);
  178. if (event_notifier_test_and_clear(n)) {
  179. virtio_queue_notify_vq(vq);
  180. }
  181. }
  182. static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy *proxy,
  183. int n, bool assign)
  184. {
  185. VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
  186. EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
  187. if (assign) {
  188. qemu_set_fd_handler(event_notifier_get_fd(notifier),
  189. virtio_pci_host_notifier_read, NULL, vq);
  190. } else {
  191. qemu_set_fd_handler(event_notifier_get_fd(notifier),
  192. NULL, NULL, NULL);
  193. }
  194. }
  195. static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
  196. {
  197. int n, r;
  198. if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
  199. proxy->ioeventfd_disabled ||
  200. proxy->ioeventfd_started) {
  201. return;
  202. }
  203. for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
  204. if (!virtio_queue_get_num(proxy->vdev, n)) {
  205. continue;
  206. }
  207. r = virtio_pci_set_host_notifier_internal(proxy, n, true);
  208. if (r < 0) {
  209. goto assign_error;
  210. }
  211. virtio_pci_set_host_notifier_fd_handler(proxy, n, true);
  212. }
  213. proxy->ioeventfd_started = true;
  214. return;
  215. assign_error:
  216. while (--n >= 0) {
  217. if (!virtio_queue_get_num(proxy->vdev, n)) {
  218. continue;
  219. }
  220. virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
  221. r = virtio_pci_set_host_notifier_internal(proxy, n, false);
  222. assert(r >= 0);
  223. }
  224. proxy->ioeventfd_started = false;
  225. error_report("%s: failed. Fallback to a userspace (slower).", __func__);
  226. }
  227. static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
  228. {
  229. int r;
  230. int n;
  231. if (!proxy->ioeventfd_started) {
  232. return;
  233. }
  234. for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
  235. if (!virtio_queue_get_num(proxy->vdev, n)) {
  236. continue;
  237. }
  238. virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
  239. r = virtio_pci_set_host_notifier_internal(proxy, n, false);
  240. assert(r >= 0);
  241. }
  242. proxy->ioeventfd_started = false;
  243. }
  244. static void virtio_pci_reset(DeviceState *d)
  245. {
  246. VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
  247. virtio_pci_stop_ioeventfd(proxy);
  248. virtio_reset(proxy->vdev);
  249. msix_reset(&proxy->pci_dev);
  250. proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
  251. }
  252. static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
  253. {
  254. VirtIOPCIProxy *proxy = opaque;
  255. VirtIODevice *vdev = proxy->vdev;
  256. target_phys_addr_t pa;
  257. switch (addr) {
  258. case VIRTIO_PCI_GUEST_FEATURES:
  259. /* Guest does not negotiate properly? We have to assume nothing. */
  260. if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
  261. if (vdev->bad_features)
  262. val = proxy->host_features & vdev->bad_features(vdev);
  263. else
  264. val = 0;
  265. }
  266. if (vdev->set_features)
  267. vdev->set_features(vdev, val);
  268. vdev->guest_features = val;
  269. break;
  270. case VIRTIO_PCI_QUEUE_PFN:
  271. pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
  272. if (pa == 0) {
  273. virtio_pci_stop_ioeventfd(proxy);
  274. virtio_reset(proxy->vdev);
  275. msix_unuse_all_vectors(&proxy->pci_dev);
  276. }
  277. else
  278. virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
  279. break;
  280. case VIRTIO_PCI_QUEUE_SEL:
  281. if (val < VIRTIO_PCI_QUEUE_MAX)
  282. vdev->queue_sel = val;
  283. break;
  284. case VIRTIO_PCI_QUEUE_NOTIFY:
  285. if (val < VIRTIO_PCI_QUEUE_MAX) {
  286. virtio_queue_notify(vdev, val);
  287. }
  288. break;
  289. case VIRTIO_PCI_STATUS:
  290. if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
  291. virtio_pci_stop_ioeventfd(proxy);
  292. }
  293. virtio_set_status(vdev, val & 0xFF);
  294. if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
  295. virtio_pci_start_ioeventfd(proxy);
  296. }
  297. if (vdev->status == 0) {
  298. virtio_reset(proxy->vdev);
  299. msix_unuse_all_vectors(&proxy->pci_dev);
  300. }
  301. /* Linux before 2.6.34 sets the device as OK without enabling
  302. the PCI device bus master bit. In this case we need to disable
  303. some safety checks. */
  304. if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
  305. !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
  306. proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
  307. }
  308. break;
  309. case VIRTIO_MSI_CONFIG_VECTOR:
  310. msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
  311. /* Make it possible for guest to discover an error took place. */
  312. if (msix_vector_use(&proxy->pci_dev, val) < 0)
  313. val = VIRTIO_NO_VECTOR;
  314. vdev->config_vector = val;
  315. break;
  316. case VIRTIO_MSI_QUEUE_VECTOR:
  317. msix_vector_unuse(&proxy->pci_dev,
  318. virtio_queue_vector(vdev, vdev->queue_sel));
  319. /* Make it possible for guest to discover an error took place. */
  320. if (msix_vector_use(&proxy->pci_dev, val) < 0)
  321. val = VIRTIO_NO_VECTOR;
  322. virtio_queue_set_vector(vdev, vdev->queue_sel, val);
  323. break;
  324. default:
  325. error_report("%s: unexpected address 0x%x value 0x%x",
  326. __func__, addr, val);
  327. break;
  328. }
  329. }
  330. static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
  331. {
  332. VirtIODevice *vdev = proxy->vdev;
  333. uint32_t ret = 0xFFFFFFFF;
  334. switch (addr) {
  335. case VIRTIO_PCI_HOST_FEATURES:
  336. ret = proxy->host_features;
  337. break;
  338. case VIRTIO_PCI_GUEST_FEATURES:
  339. ret = vdev->guest_features;
  340. break;
  341. case VIRTIO_PCI_QUEUE_PFN:
  342. ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
  343. >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
  344. break;
  345. case VIRTIO_PCI_QUEUE_NUM:
  346. ret = virtio_queue_get_num(vdev, vdev->queue_sel);
  347. break;
  348. case VIRTIO_PCI_QUEUE_SEL:
  349. ret = vdev->queue_sel;
  350. break;
  351. case VIRTIO_PCI_STATUS:
  352. ret = vdev->status;
  353. break;
  354. case VIRTIO_PCI_ISR:
  355. /* reading from the ISR also clears it. */
  356. ret = vdev->isr;
  357. vdev->isr = 0;
  358. qemu_set_irq(proxy->pci_dev.irq[0], 0);
  359. break;
  360. case VIRTIO_MSI_CONFIG_VECTOR:
  361. ret = vdev->config_vector;
  362. break;
  363. case VIRTIO_MSI_QUEUE_VECTOR:
  364. ret = virtio_queue_vector(vdev, vdev->queue_sel);
  365. break;
  366. default:
  367. break;
  368. }
  369. return ret;
  370. }
  371. static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
  372. {
  373. VirtIOPCIProxy *proxy = opaque;
  374. uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
  375. addr -= proxy->addr;
  376. if (addr < config)
  377. return virtio_ioport_read(proxy, addr);
  378. addr -= config;
  379. return virtio_config_readb(proxy->vdev, addr);
  380. }
  381. static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
  382. {
  383. VirtIOPCIProxy *proxy = opaque;
  384. uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
  385. addr -= proxy->addr;
  386. if (addr < config)
  387. return virtio_ioport_read(proxy, addr);
  388. addr -= config;
  389. return virtio_config_readw(proxy->vdev, addr);
  390. }
  391. static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
  392. {
  393. VirtIOPCIProxy *proxy = opaque;
  394. uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
  395. addr -= proxy->addr;
  396. if (addr < config)
  397. return virtio_ioport_read(proxy, addr);
  398. addr -= config;
  399. return virtio_config_readl(proxy->vdev, addr);
  400. }
  401. static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
  402. {
  403. VirtIOPCIProxy *proxy = opaque;
  404. uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
  405. addr -= proxy->addr;
  406. if (addr < config) {
  407. virtio_ioport_write(proxy, addr, val);
  408. return;
  409. }
  410. addr -= config;
  411. virtio_config_writeb(proxy->vdev, addr, val);
  412. }
  413. static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
  414. {
  415. VirtIOPCIProxy *proxy = opaque;
  416. uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
  417. addr -= proxy->addr;
  418. if (addr < config) {
  419. virtio_ioport_write(proxy, addr, val);
  420. return;
  421. }
  422. addr -= config;
  423. virtio_config_writew(proxy->vdev, addr, val);
  424. }
  425. static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
  426. {
  427. VirtIOPCIProxy *proxy = opaque;
  428. uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
  429. addr -= proxy->addr;
  430. if (addr < config) {
  431. virtio_ioport_write(proxy, addr, val);
  432. return;
  433. }
  434. addr -= config;
  435. virtio_config_writel(proxy->vdev, addr, val);
  436. }
  437. static void virtio_map(PCIDevice *pci_dev, int region_num,
  438. pcibus_t addr, pcibus_t size, int type)
  439. {
  440. VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev);
  441. VirtIODevice *vdev = proxy->vdev;
  442. unsigned config_len = VIRTIO_PCI_REGION_SIZE(pci_dev) + vdev->config_len;
  443. proxy->addr = addr;
  444. register_ioport_write(addr, config_len, 1, virtio_pci_config_writeb, proxy);
  445. register_ioport_write(addr, config_len, 2, virtio_pci_config_writew, proxy);
  446. register_ioport_write(addr, config_len, 4, virtio_pci_config_writel, proxy);
  447. register_ioport_read(addr, config_len, 1, virtio_pci_config_readb, proxy);
  448. register_ioport_read(addr, config_len, 2, virtio_pci_config_readw, proxy);
  449. register_ioport_read(addr, config_len, 4, virtio_pci_config_readl, proxy);
  450. if (vdev->config_len)
  451. vdev->get_config(vdev, vdev->config);
  452. }
  453. static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
  454. uint32_t val, int len)
  455. {
  456. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  457. if (PCI_COMMAND == address) {
  458. if (!(val & PCI_COMMAND_MASTER)) {
  459. if (!(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
  460. virtio_pci_stop_ioeventfd(proxy);
  461. virtio_set_status(proxy->vdev,
  462. proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
  463. }
  464. }
  465. }
  466. pci_default_write_config(pci_dev, address, val, len);
  467. msix_write_config(pci_dev, address, val, len);
  468. }
  469. static unsigned virtio_pci_get_features(void *opaque)
  470. {
  471. VirtIOPCIProxy *proxy = opaque;
  472. return proxy->host_features;
  473. }
  474. static void virtio_pci_guest_notifier_read(void *opaque)
  475. {
  476. VirtQueue *vq = opaque;
  477. EventNotifier *n = virtio_queue_get_guest_notifier(vq);
  478. if (event_notifier_test_and_clear(n)) {
  479. virtio_irq(vq);
  480. }
  481. }
  482. static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
  483. {
  484. VirtIOPCIProxy *proxy = opaque;
  485. VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
  486. EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
  487. if (assign) {
  488. int r = event_notifier_init(notifier, 0);
  489. if (r < 0) {
  490. return r;
  491. }
  492. qemu_set_fd_handler(event_notifier_get_fd(notifier),
  493. virtio_pci_guest_notifier_read, NULL, vq);
  494. } else {
  495. qemu_set_fd_handler(event_notifier_get_fd(notifier),
  496. NULL, NULL, NULL);
  497. event_notifier_cleanup(notifier);
  498. }
  499. return 0;
  500. }
  501. static bool virtio_pci_query_guest_notifiers(void *opaque)
  502. {
  503. VirtIOPCIProxy *proxy = opaque;
  504. return msix_enabled(&proxy->pci_dev);
  505. }
  506. static int virtio_pci_set_guest_notifiers(void *opaque, bool assign)
  507. {
  508. VirtIOPCIProxy *proxy = opaque;
  509. VirtIODevice *vdev = proxy->vdev;
  510. int r, n;
  511. for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
  512. if (!virtio_queue_get_num(vdev, n)) {
  513. break;
  514. }
  515. r = virtio_pci_set_guest_notifier(opaque, n, assign);
  516. if (r < 0) {
  517. goto assign_error;
  518. }
  519. }
  520. return 0;
  521. assign_error:
  522. /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
  523. while (--n >= 0) {
  524. virtio_pci_set_guest_notifier(opaque, n, !assign);
  525. }
  526. return r;
  527. }
  528. static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
  529. {
  530. VirtIOPCIProxy *proxy = opaque;
  531. /* Stop using ioeventfd for virtqueue kick if the device starts using host
  532. * notifiers. This makes it easy to avoid stepping on each others' toes.
  533. */
  534. proxy->ioeventfd_disabled = assign;
  535. if (assign) {
  536. virtio_pci_stop_ioeventfd(proxy);
  537. }
  538. /* We don't need to start here: it's not needed because backend
  539. * currently only stops on status change away from ok,
  540. * reset, vmstop and such. If we do add code to start here,
  541. * need to check vmstate, device state etc. */
  542. return virtio_pci_set_host_notifier_internal(proxy, n, assign);
  543. }
  544. static void virtio_pci_vmstate_change(void *opaque, bool running)
  545. {
  546. VirtIOPCIProxy *proxy = opaque;
  547. if (running) {
  548. /* Try to find out if the guest has bus master disabled, but is
  549. in ready state. Then we have a buggy guest OS. */
  550. if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
  551. !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
  552. proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
  553. }
  554. virtio_pci_start_ioeventfd(proxy);
  555. } else {
  556. virtio_pci_stop_ioeventfd(proxy);
  557. }
  558. }
  559. static const VirtIOBindings virtio_pci_bindings = {
  560. .notify = virtio_pci_notify,
  561. .save_config = virtio_pci_save_config,
  562. .load_config = virtio_pci_load_config,
  563. .save_queue = virtio_pci_save_queue,
  564. .load_queue = virtio_pci_load_queue,
  565. .get_features = virtio_pci_get_features,
  566. .query_guest_notifiers = virtio_pci_query_guest_notifiers,
  567. .set_host_notifier = virtio_pci_set_host_notifier,
  568. .set_guest_notifiers = virtio_pci_set_guest_notifiers,
  569. .vmstate_change = virtio_pci_vmstate_change,
  570. };
  571. void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
  572. {
  573. uint8_t *config;
  574. uint32_t size;
  575. proxy->vdev = vdev;
  576. config = proxy->pci_dev.config;
  577. if (proxy->class_code) {
  578. pci_config_set_class(config, proxy->class_code);
  579. }
  580. pci_set_word(config + 0x2c, pci_get_word(config + PCI_VENDOR_ID));
  581. pci_set_word(config + 0x2e, vdev->device_id);
  582. config[0x3d] = 1;
  583. if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, 1, 0)) {
  584. pci_register_bar(&proxy->pci_dev, 1,
  585. msix_bar_size(&proxy->pci_dev),
  586. PCI_BASE_ADDRESS_SPACE_MEMORY,
  587. msix_mmio_map);
  588. } else
  589. vdev->nvectors = 0;
  590. proxy->pci_dev.config_write = virtio_write_config;
  591. size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
  592. if (size & (size-1))
  593. size = 1 << qemu_fls(size);
  594. pci_register_bar(&proxy->pci_dev, 0, size, PCI_BASE_ADDRESS_SPACE_IO,
  595. virtio_map);
  596. if (!kvm_has_many_ioeventfds()) {
  597. proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
  598. }
  599. virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
  600. proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
  601. proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
  602. proxy->host_features = vdev->get_features(vdev, proxy->host_features);
  603. }
  604. static int virtio_blk_init_pci(PCIDevice *pci_dev)
  605. {
  606. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  607. VirtIODevice *vdev;
  608. if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
  609. proxy->class_code != PCI_CLASS_STORAGE_OTHER)
  610. proxy->class_code = PCI_CLASS_STORAGE_SCSI;
  611. vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block,
  612. &proxy->block_serial);
  613. if (!vdev) {
  614. return -1;
  615. }
  616. vdev->nvectors = proxy->nvectors;
  617. virtio_init_pci(proxy, vdev);
  618. /* make the actual value visible */
  619. proxy->nvectors = vdev->nvectors;
  620. return 0;
  621. }
  622. static int virtio_exit_pci(PCIDevice *pci_dev)
  623. {
  624. return msix_uninit(pci_dev);
  625. }
  626. static int virtio_blk_exit_pci(PCIDevice *pci_dev)
  627. {
  628. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  629. virtio_pci_stop_ioeventfd(proxy);
  630. virtio_blk_exit(proxy->vdev);
  631. blockdev_mark_auto_del(proxy->block.bs);
  632. return virtio_exit_pci(pci_dev);
  633. }
  634. static int virtio_serial_init_pci(PCIDevice *pci_dev)
  635. {
  636. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  637. VirtIODevice *vdev;
  638. if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
  639. proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
  640. proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
  641. proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
  642. vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
  643. if (!vdev) {
  644. return -1;
  645. }
  646. vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
  647. ? proxy->serial.max_virtserial_ports + 1
  648. : proxy->nvectors;
  649. virtio_init_pci(proxy, vdev);
  650. proxy->nvectors = vdev->nvectors;
  651. return 0;
  652. }
  653. static int virtio_serial_exit_pci(PCIDevice *pci_dev)
  654. {
  655. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  656. virtio_pci_stop_ioeventfd(proxy);
  657. virtio_serial_exit(proxy->vdev);
  658. return virtio_exit_pci(pci_dev);
  659. }
  660. static int virtio_net_init_pci(PCIDevice *pci_dev)
  661. {
  662. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  663. VirtIODevice *vdev;
  664. vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
  665. vdev->nvectors = proxy->nvectors;
  666. virtio_init_pci(proxy, vdev);
  667. /* make the actual value visible */
  668. proxy->nvectors = vdev->nvectors;
  669. return 0;
  670. }
  671. static int virtio_net_exit_pci(PCIDevice *pci_dev)
  672. {
  673. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  674. virtio_pci_stop_ioeventfd(proxy);
  675. virtio_net_exit(proxy->vdev);
  676. return virtio_exit_pci(pci_dev);
  677. }
  678. static int virtio_balloon_init_pci(PCIDevice *pci_dev)
  679. {
  680. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  681. VirtIODevice *vdev;
  682. vdev = virtio_balloon_init(&pci_dev->qdev);
  683. if (!vdev) {
  684. return -1;
  685. }
  686. virtio_init_pci(proxy, vdev);
  687. return 0;
  688. }
  689. static int virtio_balloon_exit_pci(PCIDevice *pci_dev)
  690. {
  691. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  692. virtio_pci_stop_ioeventfd(proxy);
  693. virtio_balloon_exit(proxy->vdev);
  694. return virtio_exit_pci(pci_dev);
  695. }
  696. static PCIDeviceInfo virtio_info[] = {
  697. {
  698. .qdev.name = "virtio-blk-pci",
  699. .qdev.alias = "virtio-blk",
  700. .qdev.size = sizeof(VirtIOPCIProxy),
  701. .init = virtio_blk_init_pci,
  702. .exit = virtio_blk_exit_pci,
  703. .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
  704. .device_id = PCI_DEVICE_ID_VIRTIO_BLOCK,
  705. .revision = VIRTIO_PCI_ABI_VERSION,
  706. .class_id = PCI_CLASS_STORAGE_SCSI,
  707. .qdev.props = (Property[]) {
  708. DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
  709. DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block),
  710. DEFINE_PROP_STRING("serial", VirtIOPCIProxy, block_serial),
  711. DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
  712. VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
  713. DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
  714. DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
  715. DEFINE_PROP_END_OF_LIST(),
  716. },
  717. .qdev.reset = virtio_pci_reset,
  718. },{
  719. .qdev.name = "virtio-net-pci",
  720. .qdev.alias = "virtio-net",
  721. .qdev.size = sizeof(VirtIOPCIProxy),
  722. .init = virtio_net_init_pci,
  723. .exit = virtio_net_exit_pci,
  724. .romfile = "pxe-virtio.rom",
  725. .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
  726. .device_id = PCI_DEVICE_ID_VIRTIO_NET,
  727. .revision = VIRTIO_PCI_ABI_VERSION,
  728. .class_id = PCI_CLASS_NETWORK_ETHERNET,
  729. .qdev.props = (Property[]) {
  730. DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
  731. VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
  732. DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
  733. DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
  734. DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
  735. DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy,
  736. net.txtimer, TX_TIMER_INTERVAL),
  737. DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy,
  738. net.txburst, TX_BURST),
  739. DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
  740. DEFINE_PROP_END_OF_LIST(),
  741. },
  742. .qdev.reset = virtio_pci_reset,
  743. },{
  744. .qdev.name = "virtio-serial-pci",
  745. .qdev.alias = "virtio-serial",
  746. .qdev.size = sizeof(VirtIOPCIProxy),
  747. .init = virtio_serial_init_pci,
  748. .exit = virtio_serial_exit_pci,
  749. .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
  750. .device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE,
  751. .revision = VIRTIO_PCI_ABI_VERSION,
  752. .class_id = PCI_CLASS_COMMUNICATION_OTHER,
  753. .qdev.props = (Property[]) {
  754. DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
  755. VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
  756. DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
  757. DEV_NVECTORS_UNSPECIFIED),
  758. DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
  759. DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
  760. DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy,
  761. serial.max_virtserial_ports, 31),
  762. DEFINE_PROP_END_OF_LIST(),
  763. },
  764. .qdev.reset = virtio_pci_reset,
  765. },{
  766. .qdev.name = "virtio-balloon-pci",
  767. .qdev.alias = "virtio-balloon",
  768. .qdev.size = sizeof(VirtIOPCIProxy),
  769. .init = virtio_balloon_init_pci,
  770. .exit = virtio_balloon_exit_pci,
  771. .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
  772. .device_id = PCI_DEVICE_ID_VIRTIO_BALLOON,
  773. .revision = VIRTIO_PCI_ABI_VERSION,
  774. .class_id = PCI_CLASS_MEMORY_RAM,
  775. .qdev.props = (Property[]) {
  776. DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
  777. DEFINE_PROP_END_OF_LIST(),
  778. },
  779. .qdev.reset = virtio_pci_reset,
  780. },{
  781. /* end of list */
  782. }
  783. };
  784. static void virtio_pci_register_devices(void)
  785. {
  786. pci_qdev_register_many(virtio_info);
  787. }
  788. device_init(virtio_pci_register_devices)