2 * USB xHCI controller with PCI bus emulation
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
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.
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.
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/>.
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"
33 #include "qapi/error.h"
35 #define OFF_MSIX_TABLE 0x3000
36 #define OFF_MSIX_PBA 0x3800
38 static void xhci_pci_intr_update(XHCIState
*xhci
, int n
, bool enable
)
40 XHCIPciState
*s
= container_of(xhci
, XHCIPciState
, xhci
);
41 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
43 if (!msix_enabled(pci_dev
)) {
46 if (enable
== !!xhci
->intr
[n
].msix_used
) {
50 trace_usb_xhci_irq_msix_use(n
);
51 msix_vector_use(pci_dev
, n
);
52 xhci
->intr
[n
].msix_used
= true;
54 trace_usb_xhci_irq_msix_unuse(n
);
55 msix_vector_unuse(pci_dev
, n
);
56 xhci
->intr
[n
].msix_used
= false;
60 static void xhci_pci_intr_raise(XHCIState
*xhci
, int n
, bool level
)
62 XHCIPciState
*s
= container_of(xhci
, XHCIPciState
, xhci
);
63 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
66 !(msix_enabled(pci_dev
) ||
67 msi_enabled(pci_dev
))) {
68 pci_set_irq(pci_dev
, level
);
70 if (msix_enabled(pci_dev
)) {
71 msix_notify(pci_dev
, n
);
75 if (msi_enabled(pci_dev
)) {
76 msi_notify(pci_dev
, n
);
81 static void xhci_pci_reset(DeviceState
*dev
)
83 XHCIPciState
*s
= XHCI_PCI(dev
);
85 device_legacy_reset(DEVICE(&s
->xhci
));
88 static int xhci_pci_vmstate_post_load(void *opaque
, int version_id
)
90 XHCIPciState
*s
= XHCI_PCI(opaque
);
91 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
94 for (intr
= 0; intr
< s
->xhci
.numintrs
; intr
++) {
95 if (s
->xhci
.intr
[intr
].msix_used
) {
96 msix_vector_use(pci_dev
, intr
);
98 msix_vector_unuse(pci_dev
, intr
);
104 static void usb_xhci_pci_realize(struct PCIDevice
*dev
, Error
**errp
)
108 XHCIPciState
*s
= XHCI_PCI(dev
);
110 dev
->config
[PCI_CLASS_PROG
] = 0x30; /* xHCI */
111 dev
->config
[PCI_INTERRUPT_PIN
] = 0x01; /* interrupt pin 1 */
112 dev
->config
[PCI_CACHE_LINE_SIZE
] = 0x10;
113 dev
->config
[0x60] = 0x30; /* release number */
115 object_property_set_link(OBJECT(&s
->xhci
), "host", OBJECT(s
), NULL
);
116 s
->xhci
.intr_update
= xhci_pci_intr_update
;
117 s
->xhci
.intr_raise
= xhci_pci_intr_raise
;
118 if (!qdev_realize(DEVICE(&s
->xhci
), NULL
, errp
)) {
121 if (strcmp(object_get_typename(OBJECT(dev
)), TYPE_NEC_XHCI
) == 0) {
122 s
->xhci
.nec_quirks
= true;
125 if (s
->msi
!= ON_OFF_AUTO_OFF
) {
126 ret
= msi_init(dev
, 0x70, s
->xhci
.numintrs
, true, false, &err
);
128 * Any error other than -ENOTSUP(board's MSI support is broken)
129 * is a programming error
131 assert(!ret
|| ret
== -ENOTSUP
);
132 if (ret
&& s
->msi
== ON_OFF_AUTO_ON
) {
133 /* Can't satisfy user's explicit msi=on request, fail */
134 error_append_hint(&err
, "You have to use msi=auto (default) or "
135 "msi=off with this machine type.\n");
136 error_propagate(errp
, err
);
139 assert(!err
|| s
->msi
== ON_OFF_AUTO_AUTO
);
140 /* With msi=auto, we fall back to MSI off silently */
143 pci_register_bar(dev
, 0,
144 PCI_BASE_ADDRESS_SPACE_MEMORY
|
145 PCI_BASE_ADDRESS_MEM_TYPE_64
,
148 if (pci_bus_is_express(pci_get_bus(dev
)) ||
149 xhci_get_flag(&s
->xhci
, XHCI_FLAG_FORCE_PCIE_ENDCAP
)) {
150 ret
= pcie_endpoint_cap_init(dev
, 0xa0);
154 if (s
->msix
!= ON_OFF_AUTO_OFF
) {
155 /* TODO check for errors, and should fail when msix=on */
156 msix_init(dev
, s
->xhci
.numintrs
,
157 &s
->xhci
.mem
, 0, OFF_MSIX_TABLE
,
158 &s
->xhci
.mem
, 0, OFF_MSIX_PBA
,
161 s
->xhci
.as
= pci_get_address_space(dev
);
164 static void usb_xhci_pci_exit(PCIDevice
*dev
)
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
);
174 static const VMStateDescription vmstate_xhci_pci
= {
177 .post_load
= xhci_pci_vmstate_post_load
,
178 .fields
= (VMStateField
[]) {
179 VMSTATE_PCI_DEVICE(parent_obj
, XHCIPciState
),
180 VMSTATE_MSIX(parent_obj
, XHCIPciState
),
181 VMSTATE_STRUCT(xhci
, XHCIPciState
, 1, vmstate_xhci
, XHCIState
),
182 VMSTATE_END_OF_LIST()
186 static void xhci_instance_init(Object
*obj
)
188 XHCIPciState
*s
= XHCI_PCI(obj
);
190 * QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command
191 * line, therefore, no need to wait to realize like other devices
193 PCI_DEVICE(obj
)->cap_present
|= QEMU_PCI_CAP_EXPRESS
;
194 object_initialize_child(obj
, "xhci-core", &s
->xhci
, TYPE_XHCI
);
195 qdev_alias_all_properties(DEVICE(&s
->xhci
), obj
);
198 static void xhci_class_init(ObjectClass
*klass
, void *data
)
200 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
201 DeviceClass
*dc
= DEVICE_CLASS(klass
);
203 dc
->reset
= xhci_pci_reset
;
204 dc
->vmsd
= &vmstate_xhci_pci
;
205 set_bit(DEVICE_CATEGORY_USB
, dc
->categories
);
206 k
->realize
= usb_xhci_pci_realize
;
207 k
->exit
= usb_xhci_pci_exit
;
208 k
->class_id
= PCI_CLASS_SERIAL_USB
;
211 static const TypeInfo xhci_pci_info
= {
212 .name
= TYPE_XHCI_PCI
,
213 .parent
= TYPE_PCI_DEVICE
,
214 .instance_size
= sizeof(XHCIPciState
),
215 .class_init
= xhci_class_init
,
216 .instance_init
= xhci_instance_init
,
218 .interfaces
= (InterfaceInfo
[]) {
219 { INTERFACE_PCIE_DEVICE
},
220 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
225 static void qemu_xhci_class_init(ObjectClass
*klass
, void *data
)
227 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
229 k
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
230 k
->device_id
= PCI_DEVICE_ID_REDHAT_XHCI
;
234 static void qemu_xhci_instance_init(Object
*obj
)
236 XHCIPciState
*s
= XHCI_PCI(obj
);
237 XHCIState
*xhci
= &s
->xhci
;
239 s
->msi
= ON_OFF_AUTO_OFF
;
240 s
->msix
= ON_OFF_AUTO_AUTO
;
241 xhci
->numintrs
= XHCI_MAXINTRS
;
242 xhci
->numslots
= XHCI_MAXSLOTS
;
243 xhci_set_flag(xhci
, XHCI_FLAG_SS_FIRST
);
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
,
253 static void xhci_register_types(void)
255 type_register_static(&xhci_pci_info
);
256 type_register_static(&qemu_xhci_info
);
259 type_init(xhci_register_types
)