virtio-mmio.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. * Virtio MMIO bindings
  3. *
  4. * Copyright (c) 2011 Linaro Limited
  5. *
  6. * Author:
  7. * Peter Maydell <peter.maydell@linaro.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "qemu/osdep.h"
  22. #include "standard-headers/linux/virtio_mmio.h"
  23. #include "hw/irq.h"
  24. #include "hw/qdev-properties.h"
  25. #include "hw/sysbus.h"
  26. #include "hw/virtio/virtio.h"
  27. #include "migration/qemu-file-types.h"
  28. #include "qemu/host-utils.h"
  29. #include "qemu/module.h"
  30. #include "sysemu/kvm.h"
  31. #include "hw/virtio/virtio-mmio.h"
  32. #include "qemu/error-report.h"
  33. #include "qemu/log.h"
  34. #include "trace.h"
  35. static bool virtio_mmio_ioeventfd_enabled(DeviceState *d)
  36. {
  37. return kvm_eventfds_enabled();
  38. }
  39. static int virtio_mmio_ioeventfd_assign(DeviceState *d,
  40. EventNotifier *notifier,
  41. int n, bool assign)
  42. {
  43. VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
  44. if (assign) {
  45. memory_region_add_eventfd(&proxy->iomem, VIRTIO_MMIO_QUEUE_NOTIFY, 4,
  46. true, n, notifier);
  47. } else {
  48. memory_region_del_eventfd(&proxy->iomem, VIRTIO_MMIO_QUEUE_NOTIFY, 4,
  49. true, n, notifier);
  50. }
  51. return 0;
  52. }
  53. static void virtio_mmio_start_ioeventfd(VirtIOMMIOProxy *proxy)
  54. {
  55. virtio_bus_start_ioeventfd(&proxy->bus);
  56. }
  57. static void virtio_mmio_stop_ioeventfd(VirtIOMMIOProxy *proxy)
  58. {
  59. virtio_bus_stop_ioeventfd(&proxy->bus);
  60. }
  61. static void virtio_mmio_soft_reset(VirtIOMMIOProxy *proxy)
  62. {
  63. int i;
  64. if (proxy->legacy) {
  65. return;
  66. }
  67. for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
  68. proxy->vqs[i].enabled = 0;
  69. }
  70. }
  71. static uint64_t virtio_mmio_read(void *opaque, hwaddr offset, unsigned size)
  72. {
  73. VirtIOMMIOProxy *proxy = (VirtIOMMIOProxy *)opaque;
  74. VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
  75. trace_virtio_mmio_read(offset);
  76. if (!vdev) {
  77. /* If no backend is present, we treat most registers as
  78. * read-as-zero, except for the magic number, version and
  79. * vendor ID. This is not strictly sanctioned by the virtio
  80. * spec, but it allows us to provide transports with no backend
  81. * plugged in which don't confuse Linux's virtio code: the
  82. * probe won't complain about the bad magic number, but the
  83. * device ID of zero means no backend will claim it.
  84. */
  85. switch (offset) {
  86. case VIRTIO_MMIO_MAGIC_VALUE:
  87. return VIRT_MAGIC;
  88. case VIRTIO_MMIO_VERSION:
  89. if (proxy->legacy) {
  90. return VIRT_VERSION_LEGACY;
  91. } else {
  92. return VIRT_VERSION;
  93. }
  94. case VIRTIO_MMIO_VENDOR_ID:
  95. return VIRT_VENDOR;
  96. default:
  97. return 0;
  98. }
  99. }
  100. if (offset >= VIRTIO_MMIO_CONFIG) {
  101. offset -= VIRTIO_MMIO_CONFIG;
  102. switch (size) {
  103. case 1:
  104. return virtio_config_readb(vdev, offset);
  105. case 2:
  106. return virtio_config_readw(vdev, offset);
  107. case 4:
  108. return virtio_config_readl(vdev, offset);
  109. default:
  110. abort();
  111. }
  112. }
  113. if (size != 4) {
  114. qemu_log_mask(LOG_GUEST_ERROR,
  115. "%s: wrong size access to register!\n",
  116. __func__);
  117. return 0;
  118. }
  119. switch (offset) {
  120. case VIRTIO_MMIO_MAGIC_VALUE:
  121. return VIRT_MAGIC;
  122. case VIRTIO_MMIO_VERSION:
  123. if (proxy->legacy) {
  124. return VIRT_VERSION_LEGACY;
  125. } else {
  126. return VIRT_VERSION;
  127. }
  128. case VIRTIO_MMIO_DEVICE_ID:
  129. return vdev->device_id;
  130. case VIRTIO_MMIO_VENDOR_ID:
  131. return VIRT_VENDOR;
  132. case VIRTIO_MMIO_DEVICE_FEATURES:
  133. if (proxy->legacy) {
  134. if (proxy->host_features_sel) {
  135. return 0;
  136. } else {
  137. return vdev->host_features;
  138. }
  139. } else {
  140. VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
  141. return (vdev->host_features & ~vdc->legacy_features)
  142. >> (32 * proxy->host_features_sel);
  143. }
  144. case VIRTIO_MMIO_QUEUE_NUM_MAX:
  145. if (!virtio_queue_get_num(vdev, vdev->queue_sel)) {
  146. return 0;
  147. }
  148. return VIRTQUEUE_MAX_SIZE;
  149. case VIRTIO_MMIO_QUEUE_PFN:
  150. if (!proxy->legacy) {
  151. qemu_log_mask(LOG_GUEST_ERROR,
  152. "%s: read from legacy register (0x%"
  153. HWADDR_PRIx ") in non-legacy mode\n",
  154. __func__, offset);
  155. return 0;
  156. }
  157. return virtio_queue_get_addr(vdev, vdev->queue_sel)
  158. >> proxy->guest_page_shift;
  159. case VIRTIO_MMIO_QUEUE_READY:
  160. if (proxy->legacy) {
  161. qemu_log_mask(LOG_GUEST_ERROR,
  162. "%s: read from non-legacy register (0x%"
  163. HWADDR_PRIx ") in legacy mode\n",
  164. __func__, offset);
  165. return 0;
  166. }
  167. return proxy->vqs[vdev->queue_sel].enabled;
  168. case VIRTIO_MMIO_INTERRUPT_STATUS:
  169. return atomic_read(&vdev->isr);
  170. case VIRTIO_MMIO_STATUS:
  171. return vdev->status;
  172. case VIRTIO_MMIO_CONFIG_GENERATION:
  173. if (proxy->legacy) {
  174. qemu_log_mask(LOG_GUEST_ERROR,
  175. "%s: read from non-legacy register (0x%"
  176. HWADDR_PRIx ") in legacy mode\n",
  177. __func__, offset);
  178. return 0;
  179. }
  180. return vdev->generation;
  181. case VIRTIO_MMIO_DEVICE_FEATURES_SEL:
  182. case VIRTIO_MMIO_DRIVER_FEATURES:
  183. case VIRTIO_MMIO_DRIVER_FEATURES_SEL:
  184. case VIRTIO_MMIO_GUEST_PAGE_SIZE:
  185. case VIRTIO_MMIO_QUEUE_SEL:
  186. case VIRTIO_MMIO_QUEUE_NUM:
  187. case VIRTIO_MMIO_QUEUE_ALIGN:
  188. case VIRTIO_MMIO_QUEUE_NOTIFY:
  189. case VIRTIO_MMIO_INTERRUPT_ACK:
  190. case VIRTIO_MMIO_QUEUE_DESC_LOW:
  191. case VIRTIO_MMIO_QUEUE_DESC_HIGH:
  192. case VIRTIO_MMIO_QUEUE_AVAIL_LOW:
  193. case VIRTIO_MMIO_QUEUE_AVAIL_HIGH:
  194. case VIRTIO_MMIO_QUEUE_USED_LOW:
  195. case VIRTIO_MMIO_QUEUE_USED_HIGH:
  196. qemu_log_mask(LOG_GUEST_ERROR,
  197. "%s: read of write-only register (0x%" HWADDR_PRIx ")\n",
  198. __func__, offset);
  199. return 0;
  200. default:
  201. qemu_log_mask(LOG_GUEST_ERROR,
  202. "%s: bad register offset (0x%" HWADDR_PRIx ")\n",
  203. __func__, offset);
  204. return 0;
  205. }
  206. return 0;
  207. }
  208. static void virtio_mmio_write(void *opaque, hwaddr offset, uint64_t value,
  209. unsigned size)
  210. {
  211. VirtIOMMIOProxy *proxy = (VirtIOMMIOProxy *)opaque;
  212. VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
  213. trace_virtio_mmio_write_offset(offset, value);
  214. if (!vdev) {
  215. /* If no backend is present, we just make all registers
  216. * write-ignored. This allows us to provide transports with
  217. * no backend plugged in.
  218. */
  219. return;
  220. }
  221. if (offset >= VIRTIO_MMIO_CONFIG) {
  222. offset -= VIRTIO_MMIO_CONFIG;
  223. switch (size) {
  224. case 1:
  225. virtio_config_writeb(vdev, offset, value);
  226. break;
  227. case 2:
  228. virtio_config_writew(vdev, offset, value);
  229. break;
  230. case 4:
  231. virtio_config_writel(vdev, offset, value);
  232. break;
  233. default:
  234. abort();
  235. }
  236. return;
  237. }
  238. if (size != 4) {
  239. qemu_log_mask(LOG_GUEST_ERROR,
  240. "%s: wrong size access to register!\n",
  241. __func__);
  242. return;
  243. }
  244. switch (offset) {
  245. case VIRTIO_MMIO_DEVICE_FEATURES_SEL:
  246. if (value) {
  247. proxy->host_features_sel = 1;
  248. } else {
  249. proxy->host_features_sel = 0;
  250. }
  251. break;
  252. case VIRTIO_MMIO_DRIVER_FEATURES:
  253. if (proxy->legacy) {
  254. if (proxy->guest_features_sel) {
  255. qemu_log_mask(LOG_GUEST_ERROR,
  256. "%s: attempt to write guest features with "
  257. "guest_features_sel > 0 in legacy mode\n",
  258. __func__);
  259. } else {
  260. virtio_set_features(vdev, value);
  261. }
  262. } else {
  263. proxy->guest_features[proxy->guest_features_sel] = value;
  264. }
  265. break;
  266. case VIRTIO_MMIO_DRIVER_FEATURES_SEL:
  267. if (value) {
  268. proxy->guest_features_sel = 1;
  269. } else {
  270. proxy->guest_features_sel = 0;
  271. }
  272. break;
  273. case VIRTIO_MMIO_GUEST_PAGE_SIZE:
  274. if (!proxy->legacy) {
  275. qemu_log_mask(LOG_GUEST_ERROR,
  276. "%s: write to legacy register (0x%"
  277. HWADDR_PRIx ") in non-legacy mode\n",
  278. __func__, offset);
  279. return;
  280. }
  281. proxy->guest_page_shift = ctz32(value);
  282. if (proxy->guest_page_shift > 31) {
  283. proxy->guest_page_shift = 0;
  284. }
  285. trace_virtio_mmio_guest_page(value, proxy->guest_page_shift);
  286. break;
  287. case VIRTIO_MMIO_QUEUE_SEL:
  288. if (value < VIRTIO_QUEUE_MAX) {
  289. vdev->queue_sel = value;
  290. }
  291. break;
  292. case VIRTIO_MMIO_QUEUE_NUM:
  293. trace_virtio_mmio_queue_write(value, VIRTQUEUE_MAX_SIZE);
  294. virtio_queue_set_num(vdev, vdev->queue_sel, value);
  295. if (proxy->legacy) {
  296. virtio_queue_update_rings(vdev, vdev->queue_sel);
  297. } else {
  298. proxy->vqs[vdev->queue_sel].num = value;
  299. }
  300. break;
  301. case VIRTIO_MMIO_QUEUE_ALIGN:
  302. if (!proxy->legacy) {
  303. qemu_log_mask(LOG_GUEST_ERROR,
  304. "%s: write to legacy register (0x%"
  305. HWADDR_PRIx ") in non-legacy mode\n",
  306. __func__, offset);
  307. return;
  308. }
  309. virtio_queue_set_align(vdev, vdev->queue_sel, value);
  310. break;
  311. case VIRTIO_MMIO_QUEUE_PFN:
  312. if (!proxy->legacy) {
  313. qemu_log_mask(LOG_GUEST_ERROR,
  314. "%s: write to legacy register (0x%"
  315. HWADDR_PRIx ") in non-legacy mode\n",
  316. __func__, offset);
  317. return;
  318. }
  319. if (value == 0) {
  320. virtio_reset(vdev);
  321. } else {
  322. virtio_queue_set_addr(vdev, vdev->queue_sel,
  323. value << proxy->guest_page_shift);
  324. }
  325. break;
  326. case VIRTIO_MMIO_QUEUE_READY:
  327. if (proxy->legacy) {
  328. qemu_log_mask(LOG_GUEST_ERROR,
  329. "%s: write to non-legacy register (0x%"
  330. HWADDR_PRIx ") in legacy mode\n",
  331. __func__, offset);
  332. return;
  333. }
  334. if (value) {
  335. virtio_queue_set_num(vdev, vdev->queue_sel,
  336. proxy->vqs[vdev->queue_sel].num);
  337. virtio_queue_set_rings(vdev, vdev->queue_sel,
  338. ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 |
  339. proxy->vqs[vdev->queue_sel].desc[0],
  340. ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 |
  341. proxy->vqs[vdev->queue_sel].avail[0],
  342. ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 |
  343. proxy->vqs[vdev->queue_sel].used[0]);
  344. proxy->vqs[vdev->queue_sel].enabled = 1;
  345. } else {
  346. proxy->vqs[vdev->queue_sel].enabled = 0;
  347. }
  348. break;
  349. case VIRTIO_MMIO_QUEUE_NOTIFY:
  350. if (value < VIRTIO_QUEUE_MAX) {
  351. virtio_queue_notify(vdev, value);
  352. }
  353. break;
  354. case VIRTIO_MMIO_INTERRUPT_ACK:
  355. atomic_and(&vdev->isr, ~value);
  356. virtio_update_irq(vdev);
  357. break;
  358. case VIRTIO_MMIO_STATUS:
  359. if (!(value & VIRTIO_CONFIG_S_DRIVER_OK)) {
  360. virtio_mmio_stop_ioeventfd(proxy);
  361. }
  362. if (!proxy->legacy && (value & VIRTIO_CONFIG_S_FEATURES_OK)) {
  363. virtio_set_features(vdev,
  364. ((uint64_t)proxy->guest_features[1]) << 32 |
  365. proxy->guest_features[0]);
  366. }
  367. virtio_set_status(vdev, value & 0xff);
  368. if (value & VIRTIO_CONFIG_S_DRIVER_OK) {
  369. virtio_mmio_start_ioeventfd(proxy);
  370. }
  371. if (vdev->status == 0) {
  372. virtio_reset(vdev);
  373. virtio_mmio_soft_reset(proxy);
  374. }
  375. break;
  376. case VIRTIO_MMIO_QUEUE_DESC_LOW:
  377. if (proxy->legacy) {
  378. qemu_log_mask(LOG_GUEST_ERROR,
  379. "%s: write to non-legacy register (0x%"
  380. HWADDR_PRIx ") in legacy mode\n",
  381. __func__, offset);
  382. return;
  383. }
  384. proxy->vqs[vdev->queue_sel].desc[0] = value;
  385. break;
  386. case VIRTIO_MMIO_QUEUE_DESC_HIGH:
  387. if (proxy->legacy) {
  388. qemu_log_mask(LOG_GUEST_ERROR,
  389. "%s: write to non-legacy register (0x%"
  390. HWADDR_PRIx ") in legacy mode\n",
  391. __func__, offset);
  392. return;
  393. }
  394. proxy->vqs[vdev->queue_sel].desc[1] = value;
  395. break;
  396. case VIRTIO_MMIO_QUEUE_AVAIL_LOW:
  397. if (proxy->legacy) {
  398. qemu_log_mask(LOG_GUEST_ERROR,
  399. "%s: write to non-legacy register (0x%"
  400. HWADDR_PRIx ") in legacy mode\n",
  401. __func__, offset);
  402. return;
  403. }
  404. proxy->vqs[vdev->queue_sel].avail[0] = value;
  405. break;
  406. case VIRTIO_MMIO_QUEUE_AVAIL_HIGH:
  407. if (proxy->legacy) {
  408. qemu_log_mask(LOG_GUEST_ERROR,
  409. "%s: write to non-legacy register (0x%"
  410. HWADDR_PRIx ") in legacy mode\n",
  411. __func__, offset);
  412. return;
  413. }
  414. proxy->vqs[vdev->queue_sel].avail[1] = value;
  415. break;
  416. case VIRTIO_MMIO_QUEUE_USED_LOW:
  417. if (proxy->legacy) {
  418. qemu_log_mask(LOG_GUEST_ERROR,
  419. "%s: write to non-legacy register (0x%"
  420. HWADDR_PRIx ") in legacy mode\n",
  421. __func__, offset);
  422. return;
  423. }
  424. proxy->vqs[vdev->queue_sel].used[0] = value;
  425. break;
  426. case VIRTIO_MMIO_QUEUE_USED_HIGH:
  427. if (proxy->legacy) {
  428. qemu_log_mask(LOG_GUEST_ERROR,
  429. "%s: write to non-legacy register (0x%"
  430. HWADDR_PRIx ") in legacy mode\n",
  431. __func__, offset);
  432. return;
  433. }
  434. proxy->vqs[vdev->queue_sel].used[1] = value;
  435. break;
  436. case VIRTIO_MMIO_MAGIC_VALUE:
  437. case VIRTIO_MMIO_VERSION:
  438. case VIRTIO_MMIO_DEVICE_ID:
  439. case VIRTIO_MMIO_VENDOR_ID:
  440. case VIRTIO_MMIO_DEVICE_FEATURES:
  441. case VIRTIO_MMIO_QUEUE_NUM_MAX:
  442. case VIRTIO_MMIO_INTERRUPT_STATUS:
  443. case VIRTIO_MMIO_CONFIG_GENERATION:
  444. qemu_log_mask(LOG_GUEST_ERROR,
  445. "%s: write to read-only register (0x%" HWADDR_PRIx ")\n",
  446. __func__, offset);
  447. break;
  448. default:
  449. qemu_log_mask(LOG_GUEST_ERROR,
  450. "%s: bad register offset (0x%" HWADDR_PRIx ")\n",
  451. __func__, offset);
  452. }
  453. }
  454. static const MemoryRegionOps virtio_legacy_mem_ops = {
  455. .read = virtio_mmio_read,
  456. .write = virtio_mmio_write,
  457. .endianness = DEVICE_NATIVE_ENDIAN,
  458. };
  459. static const MemoryRegionOps virtio_mem_ops = {
  460. .read = virtio_mmio_read,
  461. .write = virtio_mmio_write,
  462. .endianness = DEVICE_LITTLE_ENDIAN,
  463. };
  464. static void virtio_mmio_update_irq(DeviceState *opaque, uint16_t vector)
  465. {
  466. VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
  467. VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
  468. int level;
  469. if (!vdev) {
  470. return;
  471. }
  472. level = (atomic_read(&vdev->isr) != 0);
  473. trace_virtio_mmio_setting_irq(level);
  474. qemu_set_irq(proxy->irq, level);
  475. }
  476. static int virtio_mmio_load_config(DeviceState *opaque, QEMUFile *f)
  477. {
  478. VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
  479. proxy->host_features_sel = qemu_get_be32(f);
  480. proxy->guest_features_sel = qemu_get_be32(f);
  481. proxy->guest_page_shift = qemu_get_be32(f);
  482. return 0;
  483. }
  484. static void virtio_mmio_save_config(DeviceState *opaque, QEMUFile *f)
  485. {
  486. VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
  487. qemu_put_be32(f, proxy->host_features_sel);
  488. qemu_put_be32(f, proxy->guest_features_sel);
  489. qemu_put_be32(f, proxy->guest_page_shift);
  490. }
  491. static const VMStateDescription vmstate_virtio_mmio_queue_state = {
  492. .name = "virtio_mmio/queue_state",
  493. .version_id = 1,
  494. .minimum_version_id = 1,
  495. .fields = (VMStateField[]) {
  496. VMSTATE_UINT16(num, VirtIOMMIOQueue),
  497. VMSTATE_BOOL(enabled, VirtIOMMIOQueue),
  498. VMSTATE_UINT32_ARRAY(desc, VirtIOMMIOQueue, 2),
  499. VMSTATE_UINT32_ARRAY(avail, VirtIOMMIOQueue, 2),
  500. VMSTATE_UINT32_ARRAY(used, VirtIOMMIOQueue, 2),
  501. VMSTATE_END_OF_LIST()
  502. }
  503. };
  504. static const VMStateDescription vmstate_virtio_mmio_state_sub = {
  505. .name = "virtio_mmio/state",
  506. .version_id = 1,
  507. .minimum_version_id = 1,
  508. .fields = (VMStateField[]) {
  509. VMSTATE_UINT32_ARRAY(guest_features, VirtIOMMIOProxy, 2),
  510. VMSTATE_STRUCT_ARRAY(vqs, VirtIOMMIOProxy, VIRTIO_QUEUE_MAX, 0,
  511. vmstate_virtio_mmio_queue_state,
  512. VirtIOMMIOQueue),
  513. VMSTATE_END_OF_LIST()
  514. }
  515. };
  516. static const VMStateDescription vmstate_virtio_mmio = {
  517. .name = "virtio_mmio",
  518. .version_id = 1,
  519. .minimum_version_id = 1,
  520. .minimum_version_id_old = 1,
  521. .fields = (VMStateField[]) {
  522. VMSTATE_END_OF_LIST()
  523. },
  524. .subsections = (const VMStateDescription * []) {
  525. &vmstate_virtio_mmio_state_sub,
  526. NULL
  527. }
  528. };
  529. static void virtio_mmio_save_extra_state(DeviceState *opaque, QEMUFile *f)
  530. {
  531. VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
  532. vmstate_save_state(f, &vmstate_virtio_mmio, proxy, NULL);
  533. }
  534. static int virtio_mmio_load_extra_state(DeviceState *opaque, QEMUFile *f)
  535. {
  536. VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
  537. return vmstate_load_state(f, &vmstate_virtio_mmio, proxy, 1);
  538. }
  539. static bool virtio_mmio_has_extra_state(DeviceState *opaque)
  540. {
  541. VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
  542. return !proxy->legacy;
  543. }
  544. static void virtio_mmio_reset(DeviceState *d)
  545. {
  546. VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
  547. int i;
  548. virtio_mmio_stop_ioeventfd(proxy);
  549. virtio_bus_reset(&proxy->bus);
  550. proxy->host_features_sel = 0;
  551. proxy->guest_features_sel = 0;
  552. proxy->guest_page_shift = 0;
  553. if (!proxy->legacy) {
  554. proxy->guest_features[0] = proxy->guest_features[1] = 0;
  555. for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
  556. proxy->vqs[i].enabled = 0;
  557. proxy->vqs[i].num = 0;
  558. proxy->vqs[i].desc[0] = proxy->vqs[i].desc[1] = 0;
  559. proxy->vqs[i].avail[0] = proxy->vqs[i].avail[1] = 0;
  560. proxy->vqs[i].used[0] = proxy->vqs[i].used[1] = 0;
  561. }
  562. }
  563. }
  564. static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign,
  565. bool with_irqfd)
  566. {
  567. VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
  568. VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
  569. VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
  570. VirtQueue *vq = virtio_get_queue(vdev, n);
  571. EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
  572. if (assign) {
  573. int r = event_notifier_init(notifier, 0);
  574. if (r < 0) {
  575. return r;
  576. }
  577. virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
  578. } else {
  579. virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
  580. event_notifier_cleanup(notifier);
  581. }
  582. if (vdc->guest_notifier_mask && vdev->use_guest_notifier_mask) {
  583. vdc->guest_notifier_mask(vdev, n, !assign);
  584. }
  585. return 0;
  586. }
  587. static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs,
  588. bool assign)
  589. {
  590. VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
  591. VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
  592. /* TODO: need to check if kvm-arm supports irqfd */
  593. bool with_irqfd = false;
  594. int r, n;
  595. nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
  596. for (n = 0; n < nvqs; n++) {
  597. if (!virtio_queue_get_num(vdev, n)) {
  598. break;
  599. }
  600. r = virtio_mmio_set_guest_notifier(d, n, assign, with_irqfd);
  601. if (r < 0) {
  602. goto assign_error;
  603. }
  604. }
  605. return 0;
  606. assign_error:
  607. /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
  608. assert(assign);
  609. while (--n >= 0) {
  610. virtio_mmio_set_guest_notifier(d, n, !assign, false);
  611. }
  612. return r;
  613. }
  614. static void virtio_mmio_pre_plugged(DeviceState *d, Error **errp)
  615. {
  616. VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
  617. VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
  618. if (!proxy->legacy) {
  619. virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
  620. }
  621. }
  622. /* virtio-mmio device */
  623. static Property virtio_mmio_properties[] = {
  624. DEFINE_PROP_BOOL("format_transport_address", VirtIOMMIOProxy,
  625. format_transport_address, true),
  626. DEFINE_PROP_BOOL("force-legacy", VirtIOMMIOProxy, legacy, true),
  627. DEFINE_PROP_END_OF_LIST(),
  628. };
  629. static void virtio_mmio_realizefn(DeviceState *d, Error **errp)
  630. {
  631. VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
  632. SysBusDevice *sbd = SYS_BUS_DEVICE(d);
  633. qbus_create_inplace(&proxy->bus, sizeof(proxy->bus), TYPE_VIRTIO_MMIO_BUS,
  634. d, NULL);
  635. sysbus_init_irq(sbd, &proxy->irq);
  636. if (proxy->legacy) {
  637. memory_region_init_io(&proxy->iomem, OBJECT(d),
  638. &virtio_legacy_mem_ops, proxy,
  639. TYPE_VIRTIO_MMIO, 0x200);
  640. } else {
  641. memory_region_init_io(&proxy->iomem, OBJECT(d),
  642. &virtio_mem_ops, proxy,
  643. TYPE_VIRTIO_MMIO, 0x200);
  644. }
  645. sysbus_init_mmio(sbd, &proxy->iomem);
  646. }
  647. static void virtio_mmio_class_init(ObjectClass *klass, void *data)
  648. {
  649. DeviceClass *dc = DEVICE_CLASS(klass);
  650. dc->realize = virtio_mmio_realizefn;
  651. dc->reset = virtio_mmio_reset;
  652. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  653. device_class_set_props(dc, virtio_mmio_properties);
  654. }
  655. static const TypeInfo virtio_mmio_info = {
  656. .name = TYPE_VIRTIO_MMIO,
  657. .parent = TYPE_SYS_BUS_DEVICE,
  658. .instance_size = sizeof(VirtIOMMIOProxy),
  659. .class_init = virtio_mmio_class_init,
  660. };
  661. /* virtio-mmio-bus. */
  662. static char *virtio_mmio_bus_get_dev_path(DeviceState *dev)
  663. {
  664. BusState *virtio_mmio_bus;
  665. VirtIOMMIOProxy *virtio_mmio_proxy;
  666. char *proxy_path;
  667. SysBusDevice *proxy_sbd;
  668. char *path;
  669. virtio_mmio_bus = qdev_get_parent_bus(dev);
  670. virtio_mmio_proxy = VIRTIO_MMIO(virtio_mmio_bus->parent);
  671. proxy_path = qdev_get_dev_path(DEVICE(virtio_mmio_proxy));
  672. /*
  673. * If @format_transport_address is false, then we just perform the same as
  674. * virtio_bus_get_dev_path(): we delegate the address formatting for the
  675. * device on the virtio-mmio bus to the bus that the virtio-mmio proxy
  676. * (i.e., the device that implements the virtio-mmio bus) resides on. In
  677. * this case the base address of the virtio-mmio transport will be
  678. * invisible.
  679. */
  680. if (!virtio_mmio_proxy->format_transport_address) {
  681. return proxy_path;
  682. }
  683. /* Otherwise, we append the base address of the transport. */
  684. proxy_sbd = SYS_BUS_DEVICE(virtio_mmio_proxy);
  685. assert(proxy_sbd->num_mmio == 1);
  686. assert(proxy_sbd->mmio[0].memory == &virtio_mmio_proxy->iomem);
  687. if (proxy_path) {
  688. path = g_strdup_printf("%s/virtio-mmio@" TARGET_FMT_plx, proxy_path,
  689. proxy_sbd->mmio[0].addr);
  690. } else {
  691. path = g_strdup_printf("virtio-mmio@" TARGET_FMT_plx,
  692. proxy_sbd->mmio[0].addr);
  693. }
  694. g_free(proxy_path);
  695. return path;
  696. }
  697. static void virtio_mmio_bus_class_init(ObjectClass *klass, void *data)
  698. {
  699. BusClass *bus_class = BUS_CLASS(klass);
  700. VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
  701. k->notify = virtio_mmio_update_irq;
  702. k->save_config = virtio_mmio_save_config;
  703. k->load_config = virtio_mmio_load_config;
  704. k->save_extra_state = virtio_mmio_save_extra_state;
  705. k->load_extra_state = virtio_mmio_load_extra_state;
  706. k->has_extra_state = virtio_mmio_has_extra_state;
  707. k->set_guest_notifiers = virtio_mmio_set_guest_notifiers;
  708. k->ioeventfd_enabled = virtio_mmio_ioeventfd_enabled;
  709. k->ioeventfd_assign = virtio_mmio_ioeventfd_assign;
  710. k->pre_plugged = virtio_mmio_pre_plugged;
  711. k->has_variable_vring_alignment = true;
  712. bus_class->max_dev = 1;
  713. bus_class->get_dev_path = virtio_mmio_bus_get_dev_path;
  714. }
  715. static const TypeInfo virtio_mmio_bus_info = {
  716. .name = TYPE_VIRTIO_MMIO_BUS,
  717. .parent = TYPE_VIRTIO_BUS,
  718. .instance_size = sizeof(VirtioBusState),
  719. .class_init = virtio_mmio_bus_class_init,
  720. };
  721. static void virtio_mmio_register_types(void)
  722. {
  723. type_register_static(&virtio_mmio_bus_info);
  724. type_register_static(&virtio_mmio_info);
  725. }
  726. type_init(virtio_mmio_register_types)