hcd-xhci-pci.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * USB xHCI controller with PCI bus emulation
  3. *
  4. * SPDX-FileCopyrightText: 2011 Securiforest
  5. * SPDX-FileContributor: Hector Martin <hector@marcansoft.com>
  6. * SPDX-sourceInfo: Based on usb-ohci.c, emulates Renesas NEC USB 3.0
  7. * SPDX-FileCopyrightText: 2020 Xilinx
  8. * SPDX-FileContributor: Sai Pavan Boddu <sai.pavan.boddu@xilinx.com>
  9. * SPDX-sourceInfo: Moved the pci specific content for hcd-xhci.c to
  10. * hcd-xhci-pci.c
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "hw/pci/pci.h"
  27. #include "hw/qdev-properties.h"
  28. #include "migration/vmstate.h"
  29. #include "hw/pci/msi.h"
  30. #include "hw/pci/msix.h"
  31. #include "hcd-xhci-pci.h"
  32. #include "trace.h"
  33. #include "qapi/error.h"
  34. #define OFF_MSIX_TABLE 0x3000
  35. #define OFF_MSIX_PBA 0x3800
  36. static void xhci_pci_intr_update(XHCIState *xhci, int n, bool enable)
  37. {
  38. XHCIPciState *s = container_of(xhci, XHCIPciState, xhci);
  39. PCIDevice *pci_dev = PCI_DEVICE(s);
  40. if (!msix_enabled(pci_dev)) {
  41. return;
  42. }
  43. if (enable == !!xhci->intr[n].msix_used) {
  44. return;
  45. }
  46. if (enable) {
  47. trace_usb_xhci_irq_msix_use(n);
  48. msix_vector_use(pci_dev, n);
  49. xhci->intr[n].msix_used = true;
  50. } else {
  51. trace_usb_xhci_irq_msix_unuse(n);
  52. msix_vector_unuse(pci_dev, n);
  53. xhci->intr[n].msix_used = false;
  54. }
  55. }
  56. static bool xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
  57. {
  58. XHCIPciState *s = container_of(xhci, XHCIPciState, xhci);
  59. PCIDevice *pci_dev = PCI_DEVICE(s);
  60. if (n == 0 &&
  61. !(msix_enabled(pci_dev) ||
  62. msi_enabled(pci_dev))) {
  63. pci_set_irq(pci_dev, level);
  64. }
  65. if (msix_enabled(pci_dev) && level) {
  66. msix_notify(pci_dev, n);
  67. return true;
  68. }
  69. if (msi_enabled(pci_dev) && level) {
  70. n %= msi_nr_vectors_allocated(pci_dev);
  71. msi_notify(pci_dev, n);
  72. return true;
  73. }
  74. return false;
  75. }
  76. static bool xhci_pci_intr_mapping_conditional(XHCIState *xhci)
  77. {
  78. XHCIPciState *s = container_of(xhci, XHCIPciState, xhci);
  79. PCIDevice *pci_dev = PCI_DEVICE(s);
  80. /*
  81. * Implementation of the "conditional-intr-mapping" property, which only
  82. * enables interrupter mapping if MSI or MSI-X is available and active.
  83. * Forces all events onto interrupter/event ring 0 in pin-based IRQ mode.
  84. * Provides compatibility with macOS guests on machine types where MSI(-X)
  85. * is not available.
  86. */
  87. return msix_enabled(pci_dev) || msi_enabled(pci_dev);
  88. }
  89. static void xhci_pci_reset(DeviceState *dev)
  90. {
  91. XHCIPciState *s = XHCI_PCI(dev);
  92. device_cold_reset(DEVICE(&s->xhci));
  93. }
  94. static int xhci_pci_vmstate_post_load(void *opaque, int version_id)
  95. {
  96. XHCIPciState *s = XHCI_PCI(opaque);
  97. PCIDevice *pci_dev = PCI_DEVICE(s);
  98. int intr;
  99. for (intr = 0; intr < s->xhci.numintrs; intr++) {
  100. if (s->xhci.intr[intr].msix_used) {
  101. msix_vector_use(pci_dev, intr);
  102. } else {
  103. msix_vector_unuse(pci_dev, intr);
  104. }
  105. }
  106. return 0;
  107. }
  108. static void usb_xhci_pci_realize(struct PCIDevice *dev, Error **errp)
  109. {
  110. int ret;
  111. Error *err = NULL;
  112. XHCIPciState *s = XHCI_PCI(dev);
  113. dev->config[PCI_CLASS_PROG] = 0x30; /* xHCI */
  114. dev->config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin 1 */
  115. dev->config[PCI_CACHE_LINE_SIZE] = 0x10;
  116. dev->config[0x60] = 0x30; /* release number */
  117. object_property_set_link(OBJECT(&s->xhci), "host", OBJECT(s), NULL);
  118. s->xhci.intr_update = xhci_pci_intr_update;
  119. s->xhci.intr_raise = xhci_pci_intr_raise;
  120. if (s->conditional_intr_mapping) {
  121. s->xhci.intr_mapping_supported = xhci_pci_intr_mapping_conditional;
  122. }
  123. if (!qdev_realize(DEVICE(&s->xhci), NULL, errp)) {
  124. return;
  125. }
  126. if (strcmp(object_get_typename(OBJECT(dev)), TYPE_NEC_XHCI) == 0) {
  127. s->xhci.nec_quirks = true;
  128. }
  129. if (s->msi != ON_OFF_AUTO_OFF) {
  130. ret = msi_init(dev, 0x70, s->xhci.numintrs, true, false, &err);
  131. /*
  132. * Any error other than -ENOTSUP(board's MSI support is broken)
  133. * is a programming error
  134. */
  135. assert(!ret || ret == -ENOTSUP);
  136. if (ret && s->msi == ON_OFF_AUTO_ON) {
  137. /* Can't satisfy user's explicit msi=on request, fail */
  138. error_append_hint(&err, "You have to use msi=auto (default) or "
  139. "msi=off with this machine type.\n");
  140. error_propagate(errp, err);
  141. return;
  142. }
  143. assert(!err || s->msi == ON_OFF_AUTO_AUTO);
  144. /* With msi=auto, we fall back to MSI off silently */
  145. error_free(err);
  146. }
  147. pci_register_bar(dev, 0,
  148. PCI_BASE_ADDRESS_SPACE_MEMORY |
  149. PCI_BASE_ADDRESS_MEM_TYPE_64,
  150. &s->xhci.mem);
  151. if (pci_bus_is_express(pci_get_bus(dev))) {
  152. ret = pcie_endpoint_cap_init(dev, 0xa0);
  153. assert(ret > 0);
  154. }
  155. if (s->msix != ON_OFF_AUTO_OFF) {
  156. /* TODO check for errors, and should fail when msix=on */
  157. msix_init(dev, s->xhci.numintrs,
  158. &s->xhci.mem, 0, OFF_MSIX_TABLE,
  159. &s->xhci.mem, 0, OFF_MSIX_PBA,
  160. 0x90, NULL);
  161. }
  162. s->xhci.as = pci_get_address_space(dev);
  163. }
  164. static void usb_xhci_pci_exit(PCIDevice *dev)
  165. {
  166. XHCIPciState *s = XHCI_PCI(dev);
  167. /* destroy msix memory region */
  168. if (dev->msix_table && dev->msix_pba
  169. && dev->msix_entry_used) {
  170. msix_uninit(dev, &s->xhci.mem, &s->xhci.mem);
  171. }
  172. }
  173. static const VMStateDescription vmstate_xhci_pci = {
  174. .name = "xhci",
  175. .version_id = 1,
  176. .post_load = xhci_pci_vmstate_post_load,
  177. .fields = (const VMStateField[]) {
  178. VMSTATE_PCI_DEVICE(parent_obj, XHCIPciState),
  179. VMSTATE_MSIX(parent_obj, XHCIPciState),
  180. VMSTATE_STRUCT(xhci, XHCIPciState, 1, vmstate_xhci, XHCIState),
  181. VMSTATE_END_OF_LIST()
  182. }
  183. };
  184. static void xhci_instance_init(Object *obj)
  185. {
  186. XHCIPciState *s = XHCI_PCI(obj);
  187. /*
  188. * QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command
  189. * line, therefore, no need to wait to realize like other devices
  190. */
  191. PCI_DEVICE(obj)->cap_present |= QEMU_PCI_CAP_EXPRESS;
  192. object_initialize_child(obj, "xhci-core", &s->xhci, TYPE_XHCI);
  193. qdev_alias_all_properties(DEVICE(&s->xhci), obj);
  194. }
  195. static const Property xhci_pci_properties[] = {
  196. DEFINE_PROP_ON_OFF_AUTO("msi", XHCIPciState, msi, ON_OFF_AUTO_AUTO),
  197. DEFINE_PROP_ON_OFF_AUTO("msix", XHCIPciState, msix, ON_OFF_AUTO_AUTO),
  198. DEFINE_PROP_BOOL("conditional-intr-mapping", XHCIPciState,
  199. conditional_intr_mapping, false),
  200. };
  201. static void xhci_class_init(ObjectClass *klass, void *data)
  202. {
  203. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  204. DeviceClass *dc = DEVICE_CLASS(klass);
  205. device_class_set_legacy_reset(dc, xhci_pci_reset);
  206. dc->vmsd = &vmstate_xhci_pci;
  207. set_bit(DEVICE_CATEGORY_USB, dc->categories);
  208. k->realize = usb_xhci_pci_realize;
  209. k->exit = usb_xhci_pci_exit;
  210. k->class_id = PCI_CLASS_SERIAL_USB;
  211. device_class_set_props(dc, xhci_pci_properties);
  212. object_class_property_set_description(klass, "conditional-intr-mapping",
  213. "When true, disables interrupter mapping for pin-based IRQ mode. "
  214. "Intended to be used with guest drivers with questionable behaviour, "
  215. "such as macOS's.");
  216. }
  217. static const TypeInfo xhci_pci_info = {
  218. .name = TYPE_XHCI_PCI,
  219. .parent = TYPE_PCI_DEVICE,
  220. .instance_size = sizeof(XHCIPciState),
  221. .class_init = xhci_class_init,
  222. .instance_init = xhci_instance_init,
  223. .abstract = true,
  224. .interfaces = (InterfaceInfo[]) {
  225. { INTERFACE_PCIE_DEVICE },
  226. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  227. { }
  228. },
  229. };
  230. static void qemu_xhci_class_init(ObjectClass *klass, void *data)
  231. {
  232. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  233. k->vendor_id = PCI_VENDOR_ID_REDHAT;
  234. k->device_id = PCI_DEVICE_ID_REDHAT_XHCI;
  235. k->revision = 0x01;
  236. }
  237. static void qemu_xhci_instance_init(Object *obj)
  238. {
  239. XHCIPciState *s = XHCI_PCI(obj);
  240. XHCIState *xhci = &s->xhci;
  241. s->msi = ON_OFF_AUTO_OFF;
  242. s->msix = ON_OFF_AUTO_AUTO;
  243. xhci->numintrs = XHCI_MAXINTRS;
  244. xhci->numslots = XHCI_MAXSLOTS;
  245. }
  246. static const TypeInfo qemu_xhci_info = {
  247. .name = TYPE_QEMU_XHCI,
  248. .parent = TYPE_XHCI_PCI,
  249. .class_init = qemu_xhci_class_init,
  250. .instance_init = qemu_xhci_instance_init,
  251. };
  252. static void xhci_register_types(void)
  253. {
  254. type_register_static(&xhci_pci_info);
  255. type_register_static(&qemu_xhci_info);
  256. }
  257. type_init(xhci_register_types)