2
0

virtio-mmio.c 25 KB

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