hcd-xhci-pci.c 7.8 KB

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