proxy.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright © 2018, 2021 Oracle and/or its affiliates.
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  5. * See the COPYING file in the top-level directory.
  6. *
  7. */
  8. #include "qemu/osdep.h"
  9. #include "qemu-common.h"
  10. #include "hw/remote/proxy.h"
  11. #include "hw/pci/pci.h"
  12. #include "qapi/error.h"
  13. #include "io/channel-util.h"
  14. #include "hw/qdev-properties.h"
  15. #include "monitor/monitor.h"
  16. #include "migration/blocker.h"
  17. #include "qemu/sockets.h"
  18. #include "hw/remote/mpqemu-link.h"
  19. #include "qemu/error-report.h"
  20. static void pci_proxy_dev_realize(PCIDevice *device, Error **errp)
  21. {
  22. ERRP_GUARD();
  23. PCIProxyDev *dev = PCI_PROXY_DEV(device);
  24. int fd;
  25. if (!dev->fd) {
  26. error_setg(errp, "fd parameter not specified for %s",
  27. DEVICE(device)->id);
  28. return;
  29. }
  30. fd = monitor_fd_param(monitor_cur(), dev->fd, errp);
  31. if (fd == -1) {
  32. error_prepend(errp, "proxy: unable to parse fd %s: ", dev->fd);
  33. return;
  34. }
  35. if (!fd_is_socket(fd)) {
  36. error_setg(errp, "proxy: fd %d is not a socket", fd);
  37. close(fd);
  38. return;
  39. }
  40. dev->ioc = qio_channel_new_fd(fd, errp);
  41. error_setg(&dev->migration_blocker, "%s does not support migration",
  42. TYPE_PCI_PROXY_DEV);
  43. migrate_add_blocker(dev->migration_blocker, errp);
  44. qemu_mutex_init(&dev->io_mutex);
  45. qio_channel_set_blocking(dev->ioc, true, NULL);
  46. }
  47. static void pci_proxy_dev_exit(PCIDevice *pdev)
  48. {
  49. PCIProxyDev *dev = PCI_PROXY_DEV(pdev);
  50. if (dev->ioc) {
  51. qio_channel_close(dev->ioc, NULL);
  52. }
  53. migrate_del_blocker(dev->migration_blocker);
  54. error_free(dev->migration_blocker);
  55. }
  56. static void config_op_send(PCIProxyDev *pdev, uint32_t addr, uint32_t *val,
  57. int len, unsigned int op)
  58. {
  59. MPQemuMsg msg = { 0 };
  60. uint64_t ret = -EINVAL;
  61. Error *local_err = NULL;
  62. msg.cmd = op;
  63. msg.data.pci_conf_data.addr = addr;
  64. msg.data.pci_conf_data.val = (op == MPQEMU_CMD_PCI_CFGWRITE) ? *val : 0;
  65. msg.data.pci_conf_data.len = len;
  66. msg.size = sizeof(PciConfDataMsg);
  67. ret = mpqemu_msg_send_and_await_reply(&msg, pdev, &local_err);
  68. if (local_err) {
  69. error_report_err(local_err);
  70. }
  71. if (ret == UINT64_MAX) {
  72. error_report("Failed to perform PCI config %s operation",
  73. (op == MPQEMU_CMD_PCI_CFGREAD) ? "READ" : "WRITE");
  74. }
  75. if (op == MPQEMU_CMD_PCI_CFGREAD) {
  76. *val = (uint32_t)ret;
  77. }
  78. }
  79. static uint32_t pci_proxy_read_config(PCIDevice *d, uint32_t addr, int len)
  80. {
  81. uint32_t val;
  82. config_op_send(PCI_PROXY_DEV(d), addr, &val, len, MPQEMU_CMD_PCI_CFGREAD);
  83. return val;
  84. }
  85. static void pci_proxy_write_config(PCIDevice *d, uint32_t addr, uint32_t val,
  86. int len)
  87. {
  88. /*
  89. * Some of the functions access the copy of remote device's PCI config
  90. * space which is cached in the proxy device. Therefore, maintain
  91. * it updated.
  92. */
  93. pci_default_write_config(d, addr, val, len);
  94. config_op_send(PCI_PROXY_DEV(d), addr, &val, len, MPQEMU_CMD_PCI_CFGWRITE);
  95. }
  96. static Property proxy_properties[] = {
  97. DEFINE_PROP_STRING("fd", PCIProxyDev, fd),
  98. DEFINE_PROP_END_OF_LIST(),
  99. };
  100. static void pci_proxy_dev_class_init(ObjectClass *klass, void *data)
  101. {
  102. DeviceClass *dc = DEVICE_CLASS(klass);
  103. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  104. k->realize = pci_proxy_dev_realize;
  105. k->exit = pci_proxy_dev_exit;
  106. k->config_read = pci_proxy_read_config;
  107. k->config_write = pci_proxy_write_config;
  108. device_class_set_props(dc, proxy_properties);
  109. }
  110. static const TypeInfo pci_proxy_dev_type_info = {
  111. .name = TYPE_PCI_PROXY_DEV,
  112. .parent = TYPE_PCI_DEVICE,
  113. .instance_size = sizeof(PCIProxyDev),
  114. .class_init = pci_proxy_dev_class_init,
  115. .interfaces = (InterfaceInfo[]) {
  116. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  117. { },
  118. },
  119. };
  120. static void pci_proxy_dev_register_types(void)
  121. {
  122. type_register_static(&pci_proxy_dev_type_info);
  123. }
  124. type_init(pci_proxy_dev_register_types)