hcd-xhci-pci.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 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 void 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)) {
  66. msix_notify(pci_dev, n);
  67. return;
  68. }
  69. if (msi_enabled(pci_dev)) {
  70. msi_notify(pci_dev, n);
  71. return;
  72. }
  73. }
  74. static void xhci_pci_reset(DeviceState *dev)
  75. {
  76. XHCIPciState *s = XHCI_PCI(dev);
  77. device_legacy_reset(DEVICE(&s->xhci));
  78. }
  79. static int xhci_pci_vmstate_post_load(void *opaque, int version_id)
  80. {
  81. XHCIPciState *s = XHCI_PCI(opaque);
  82. PCIDevice *pci_dev = PCI_DEVICE(s);
  83. int intr;
  84. for (intr = 0; intr < s->xhci.numintrs; intr++) {
  85. if (s->xhci.intr[intr].msix_used) {
  86. msix_vector_use(pci_dev, intr);
  87. } else {
  88. msix_vector_unuse(pci_dev, intr);
  89. }
  90. }
  91. return 0;
  92. }
  93. static void usb_xhci_pci_realize(struct PCIDevice *dev, Error **errp)
  94. {
  95. int ret;
  96. Error *err = NULL;
  97. XHCIPciState *s = XHCI_PCI(dev);
  98. dev->config[PCI_CLASS_PROG] = 0x30; /* xHCI */
  99. dev->config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin 1 */
  100. dev->config[PCI_CACHE_LINE_SIZE] = 0x10;
  101. dev->config[0x60] = 0x30; /* release number */
  102. object_property_set_link(OBJECT(&s->xhci), "host", OBJECT(s), NULL);
  103. s->xhci.intr_update = xhci_pci_intr_update;
  104. s->xhci.intr_raise = xhci_pci_intr_raise;
  105. object_property_set_bool(OBJECT(&s->xhci), "realized", true, &err);
  106. if (err) {
  107. error_propagate(errp, err);
  108. return;
  109. }
  110. if (strcmp(object_get_typename(OBJECT(dev)), TYPE_NEC_XHCI) == 0) {
  111. s->xhci.nec_quirks = true;
  112. }
  113. if (s->msi != ON_OFF_AUTO_OFF) {
  114. ret = msi_init(dev, 0x70, s->xhci.numintrs, true, false, &err);
  115. /*
  116. * Any error other than -ENOTSUP(board's MSI support is broken)
  117. * is a programming error
  118. */
  119. assert(!ret || ret == -ENOTSUP);
  120. if (ret && s->msi == ON_OFF_AUTO_ON) {
  121. /* Can't satisfy user's explicit msi=on request, fail */
  122. error_append_hint(&err, "You have to use msi=auto (default) or "
  123. "msi=off with this machine type.\n");
  124. error_propagate(errp, err);
  125. return;
  126. }
  127. assert(!err || s->msi == ON_OFF_AUTO_AUTO);
  128. /* With msi=auto, we fall back to MSI off silently */
  129. error_free(err);
  130. }
  131. pci_register_bar(dev, 0,
  132. PCI_BASE_ADDRESS_SPACE_MEMORY |
  133. PCI_BASE_ADDRESS_MEM_TYPE_64,
  134. &s->xhci.mem);
  135. if (pci_bus_is_express(pci_get_bus(dev)) ||
  136. xhci_get_flag(&s->xhci, XHCI_FLAG_FORCE_PCIE_ENDCAP)) {
  137. ret = pcie_endpoint_cap_init(dev, 0xa0);
  138. assert(ret > 0);
  139. }
  140. if (s->msix != ON_OFF_AUTO_OFF) {
  141. /* TODO check for errors, and should fail when msix=on */
  142. msix_init(dev, s->xhci.numintrs,
  143. &s->xhci.mem, 0, OFF_MSIX_TABLE,
  144. &s->xhci.mem, 0, OFF_MSIX_PBA,
  145. 0x90, NULL);
  146. }
  147. s->xhci.as = pci_get_address_space(dev);
  148. }
  149. static void usb_xhci_pci_exit(PCIDevice *dev)
  150. {
  151. XHCIPciState *s = XHCI_PCI(dev);
  152. /* destroy msix memory region */
  153. if (dev->msix_table && dev->msix_pba
  154. && dev->msix_entry_used) {
  155. msix_uninit(dev, &s->xhci.mem, &s->xhci.mem);
  156. }
  157. }
  158. static const VMStateDescription vmstate_xhci_pci = {
  159. .name = "xhci",
  160. .version_id = 1,
  161. .post_load = xhci_pci_vmstate_post_load,
  162. .fields = (VMStateField[]) {
  163. VMSTATE_PCI_DEVICE(parent_obj, XHCIPciState),
  164. VMSTATE_MSIX(parent_obj, XHCIPciState),
  165. VMSTATE_STRUCT(xhci, XHCIPciState, 1, vmstate_xhci, XHCIState),
  166. VMSTATE_END_OF_LIST()
  167. }
  168. };
  169. static void xhci_instance_init(Object *obj)
  170. {
  171. XHCIPciState *s = XHCI_PCI(obj);
  172. /*
  173. * QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command
  174. * line, therefore, no need to wait to realize like other devices
  175. */
  176. PCI_DEVICE(obj)->cap_present |= QEMU_PCI_CAP_EXPRESS;
  177. object_initialize_child(obj, "xhci-core", &s->xhci, TYPE_XHCI);
  178. qdev_alias_all_properties(DEVICE(&s->xhci), obj);
  179. }
  180. static void xhci_class_init(ObjectClass *klass, void *data)
  181. {
  182. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  183. DeviceClass *dc = DEVICE_CLASS(klass);
  184. dc->reset = xhci_pci_reset;
  185. dc->vmsd = &vmstate_xhci_pci;
  186. set_bit(DEVICE_CATEGORY_USB, dc->categories);
  187. k->realize = usb_xhci_pci_realize;
  188. k->exit = usb_xhci_pci_exit;
  189. k->class_id = PCI_CLASS_SERIAL_USB;
  190. }
  191. static const TypeInfo xhci_pci_info = {
  192. .name = TYPE_XHCI_PCI,
  193. .parent = TYPE_PCI_DEVICE,
  194. .instance_size = sizeof(XHCIPciState),
  195. .class_init = xhci_class_init,
  196. .instance_init = xhci_instance_init,
  197. .abstract = true,
  198. .interfaces = (InterfaceInfo[]) {
  199. { INTERFACE_PCIE_DEVICE },
  200. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  201. { }
  202. },
  203. };
  204. static void qemu_xhci_class_init(ObjectClass *klass, void *data)
  205. {
  206. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  207. k->vendor_id = PCI_VENDOR_ID_REDHAT;
  208. k->device_id = PCI_DEVICE_ID_REDHAT_XHCI;
  209. k->revision = 0x01;
  210. }
  211. static void qemu_xhci_instance_init(Object *obj)
  212. {
  213. XHCIPciState *s = XHCI_PCI(obj);
  214. XHCIState *xhci = &s->xhci;
  215. s->msi = ON_OFF_AUTO_OFF;
  216. s->msix = ON_OFF_AUTO_AUTO;
  217. xhci->numintrs = MAXINTRS;
  218. xhci->numslots = MAXSLOTS;
  219. xhci_set_flag(xhci, XHCI_FLAG_SS_FIRST);
  220. }
  221. static const TypeInfo qemu_xhci_info = {
  222. .name = TYPE_QEMU_XHCI,
  223. .parent = TYPE_XHCI_PCI,
  224. .class_init = qemu_xhci_class_init,
  225. .instance_init = qemu_xhci_instance_init,
  226. };
  227. static void xhci_register_types(void)
  228. {
  229. type_register_static(&xhci_pci_info);
  230. type_register_static(&qemu_xhci_info);
  231. }
  232. type_init(xhci_register_types)