usb: usb host adapter hotplug
v3 cover usb: usb host adapter hotplug
From: Gonglei <[email protected]>
add support for usb host adapter hotplug, as the same as other pci devices.
change since v2:
* add ehci unrealize function suggested by Gerd.
* add usb host adapters exit trace.
* rebase on latest qemu master tree.
changes since v1:
* rework usb_bus_release function suggested by Gerd.
* add more completely resource cleanup for every usb host adapter.
* fix memory leak.
Gonglei (10):
usb: add usb_bus_release function
usb-ohci: Fix memory leak for ohci timer
usb-ohci: add exit function
usb-uhci: clean up uhci resource when pci-uhci exit
usb-ehci: add vmstate properity for EHCIState
usb-ehci: add ehci unrealize funciton
usb-ehci: add ehci-pci device exit function
usb-xhci: add exit function
usb: tag usb host controller as hotpluggable
usb: add usb host adapters exit trace
hw/usb/bus.c | 7 +++++++ hw/usb/hcd-ehci-pci.c | 15 ++++++++++++++- hw/usb/hcd-ehci.c | 33 ++++++++++++++++++++++++++++++++- hw/usb/hcd-ehci.h | 2 ++ hw/usb/hcd-ohci.c | 26 ++++++++++++++++++++++++-- hw/usb/hcd-uhci.c | 19 ++++++++++++++++++- hw/usb/hcd-xhci.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- include/hw/usb.h | 1 + trace-events | 3 +++ 9 files changed, 149 insertions(+), 6 deletions(-)
Signature · 1 line
1.7.12.4
PATCH v3 1/10 usb: add usb_bus_release function
From: Gonglei <[email protected]>
add global variables releasing logic when the usb buses were removed or hot-unpluged.
--- hw/usb/bus.c | 7 +++++++ include/hw/usb.h | 1 + 2 files changed, 8 insertions(+)
hw/usb/bus.c +7 -0
@@ -87,6 +87,13 @@ void usb_bus_new(USBBus *bus, size_t bus_size,QTAILQ_INSERT_TAIL (&busses, bus, next); } +void usb_bus_release (USBBus *bus) +{ +assert (next_usb_bus >0 ); + +QTAILQ_REMOVE (&busses, bus, next); +} +USBBus *usb_bus_find (int busnr) {USBBus *bus;
include/hw/usb.h +1 -0
@@ -524,6 +524,7 @@ struct USBBusOps {void usb_bus_new (USBBus *bus,size_t bus_size,USBBusOps *ops,DeviceState *host); +void usb_bus_release (USBBus *bus);USBBus *usb_bus_find (int busnr); void usb_legacy_register(const char *typename, const char *usbdevice_name, USBDevice *(*usbdevice_init)(USBBus *bus,
Signature · 1 line
1.7.12.4
PATCH v3 2/10 usb-ohci: Fix memory leak for ohci timer
In reply to [email protected]From: Gonglei <[email protected]>
--- hw/usb/hcd-ohci.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
hw/usb/hcd-ohci.c +3 -1
@@ -1371,8 +1371,10 @@ static int ohci_bus_start(OHCIState *ohci)/* Stop sending SOF tokens on the bus */ static void ohci_bus_stop (OHCIState *ohci) { -if (ohci->eof_timer) +if (ohci->eof_timer) {timer_del (ohci->eof_timer); +timer_free (ohci->eof_timer); + } ohci->eof_timer =NULL ; }
Signature · 1 line
1.7.12.4
PATCH v3 3/10 usb-ohci: add exit function
In reply to [email protected]From: Gonglei <[email protected]>
clean up ohci resource when ohci pci device exit.
--- hw/usb/hcd-ohci.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
hw/usb/hcd-ohci.c +21 -0
@@ -1954,6 +1954,26 @@ static int usb_ohci_initfn_pci(PCIDevice *dev)return 0 ; } +static void usb_ohci_exit (PCIDevice *dev) +{ +OHCIPCIState *ohci =PCI_OHCI (dev); +OHCIState *s = &ohci->state; + +ohci_bus_stop (s); + +if (s->async_td) { +usb_cancel_packet (&s->usb_packet); + s->async_td =0 ; + } +ohci_stop_endpoints (s); + +memory_region_destroy (&s->mem); + +if (!ohci->masterbus) { +usb_bus_release (&s->bus); + } +} +#define TYPE_SYSBUS_OHCI "sysbus-ohci"#define SYSBUS_OHCI (obj) OBJECT_CHECK(OHCISysBusState, (obj), TYPE_SYSBUS_OHCI)
@@ -2094,6 +2114,7 @@ static void ohci_pci_class_init(ObjectClass *klass, void *data)PCIDeviceClass *k =PCI_DEVICE_CLASS (klass); k->init = usb_ohci_initfn_pci; + k->exit = usb_ohci_exit; k->vendor_id =PCI_VENDOR_ID_APPLE ; k->device_id =PCI_DEVICE_ID_APPLE_IPID_USB ; k->class_id =PCI_CLASS_SERIAL_USB ;
Signature · 1 line
1.7.12.4
PATCH v3 4/10 usb-uhci: clean up uhci resource when pci-uhci exit
In reply to [email protected]From: Gonglei <[email protected]>
clean up uhci resource when uhci pci device exit.
--- hw/usb/hcd-uhci.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
hw/usb/hcd-uhci.c +16 -0
@@ -1260,7 +1260,23 @@ static void usb_uhci_exit(PCIDevice *dev) {UHCIState *s =DO_UPCAST (UHCIState, dev, dev); +if (s->frame_timer) { +timer_del (s->frame_timer); +timer_free (s->frame_timer); + s->frame_timer =NULL ; + } + +if (s->bh) { +qemu_bh_delete (s->bh); + } + +uhci_async_cancel_all (s); +memory_region_destroy (&s->io_bar); + +if (!s->masterbus) { +usb_bus_release (&s->bus); + } }static Property uhci_properties[] = {
Signature · 1 line
1.7.12.4
PATCH v3 5/10 usb-ehci: add vmstate properity for EHCIState
In reply to [email protected]From: Gonglei <[email protected]>
since hotunplug the ehci host adapter, we should
delete vm_change_state_handler also, so the
VMChangeStateEntry should be saved in EHCIState.
--- hw/usb/hcd-ehci.c | 2 +- hw/usb/hcd-ehci.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
hw/usb/hcd-ehci.c +1 -1
@@ -2468,7 +2468,7 @@ void usb_ehci_realize(EHCIState *s, DeviceState *dev, Error **errp) s->device = dev;qemu_register_reset (ehci_reset, s); -qemu_add_vm_change_state_handler (usb_ehci_vm_state_change, s); + s->vmstate =qemu_add_vm_change_state_handler (usb_ehci_vm_state_change, s); }void usb_ehci_init (EHCIState *s,DeviceState *dev)
hw/usb/hcd-ehci.h +1 -0
@@ -316,6 +316,7 @@ struct EHCIState {uint32_t async_stepdown;uint32_t periodic_sched_active;bool int_req_by_async; +VMChangeStateEntry *vmstate; };extern const VMStateDescription vmstate_ehci;
Signature · 1 line
1.7.12.4
PATCH v3 6/10 usb-ehci: add ehci unrealize funciton
In reply to [email protected]From: Gonglei <[email protected]>
cleanup ehci controller resource, both pci and sysbus if they're necessary.
--- hw/usb/hcd-ehci.c | 29 +++++++++++++++++++++++++++++ hw/usb/hcd-ehci.h | 1 + 2 files changed, 30 insertions(+)
hw/usb/hcd-ehci.c +29 -0
@@ -2471,6 +2471,35 @@ void usb_ehci_realize(EHCIState *s, DeviceState *dev, Error **errp) s->vmstate =qemu_add_vm_change_state_handler (usb_ehci_vm_state_change, s); } +void usb_ehci_unrealize (EHCIState *s,DeviceState *dev,Error **errp) +{ +if (s->frame_timer) { +timer_del (s->frame_timer); +timer_free (s->frame_timer); + s->frame_timer =NULL ; + } +if (s->async_bh) { +qemu_bh_delete (s->async_bh); + } + +ehci_queues_rip_all (s,0 ); +ehci_queues_rip_all (s,1 ); + +memory_region_del_subregion (&s->mem, &s->mem_caps); +memory_region_del_subregion (&s->mem, &s->mem_opreg); +memory_region_del_subregion (&s->mem, &s->mem_ports); +memory_region_destroy (&s->mem_caps); +memory_region_destroy (&s->mem_opreg); +memory_region_destroy (&s->mem_ports); +memory_region_destroy (&s->mem); + +usb_bus_release (&s->bus); + +if (s->vmstate) { +qemu_del_vm_change_state_handler (s->vmstate); + } +} +void usb_ehci_init (EHCIState *s,DeviceState *dev) {/* 2.2 host controller interface version */
hw/usb/hcd-ehci.h +1 -0
@@ -323,6 +323,7 @@ extern const VMStateDescription vmstate_ehci;void usb_ehci_init (EHCIState *s,DeviceState *dev);void usb_ehci_realize (EHCIState *s,DeviceState *dev,Error **errp); +void usb_ehci_unrealize (EHCIState *s,DeviceState *dev,Error **errp);#define TYPE_PCI_EHCI "pci-ehci-usb"#define PCI_EHCI (obj) OBJECT_CHECK(EHCIPCIState, (obj), TYPE_PCI_EHCI)
Signature · 1 line
1.7.12.4
PATCH v3 7/10 usb-ehci: add ehci-pci device exit function
In reply to [email protected]From: Gonglei <[email protected]>
clean up ehci resource when ehci pci device exit.
--- hw/usb/hcd-ehci-pci.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
hw/usb/hcd-ehci-pci.c +14 -0
@@ -84,6 +84,19 @@ static void usb_ehci_pci_init(Object *obj)usb_ehci_init (s,DEVICE (obj)); } +static void usb_ehci_pci_exit (PCIDevice *dev) +{ +EHCIPCIState *i =PCI_EHCI (dev); +EHCIState *s = &i->ehci; + +usb_ehci_unrealize (s,DEVICE (dev),NULL ); + +if (s->irq) { +g_free (s->irq); + s->irq =NULL ; + } +} +static void usb_ehci_pci_write_config (PCIDevice *dev,uint32_t addr,uint32_t val,int l) {
@@ -121,6 +134,7 @@ static void ehci_class_init(ObjectClass *klass, void *data)PCIDeviceClass *k =PCI_DEVICE_CLASS (klass); k->init = usb_ehci_pci_initfn; + k->exit = usb_ehci_pci_exit; k->class_id =PCI_CLASS_SERIAL_USB ; k->config_write = usb_ehci_pci_write_config; dc->hotpluggable = false;
Signature · 1 line
1.7.12.4
PATCH v3 8/10 usb-xhci: add exit function
In reply to [email protected]From: Gonglei <[email protected]>
clean up xhci resource when xhci pci device exit.
--- hw/usb/hcd-xhci.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)
hw/usb/hcd-xhci.c +46 -0
@@ -3644,6 +3644,51 @@ static int usb_xhci_initfn(struct PCIDevice *dev)return 0 ; } +static void usb_xhci_exit (PCIDevice *dev) +{ +int i; +XHCIState *xhci =XHCI (dev); + +for (i =0 ; i < xhci->numslots; i++) { +xhci_disable_slot (xhci, i +1 ); + } + +if (xhci->mfwrap_timer) { +timer_del (xhci->mfwrap_timer); +timer_free (xhci->mfwrap_timer); + xhci->mfwrap_timer =NULL ; + } + +memory_region_del_subregion (&xhci->mem, &xhci->mem_cap); +memory_region_del_subregion (&xhci->mem, &xhci->mem_oper); +memory_region_del_subregion (&xhci->mem, &xhci->mem_runtime); +memory_region_del_subregion (&xhci->mem, &xhci->mem_doorbell); + +memory_region_destroy (&xhci->mem_cap); +memory_region_destroy (&xhci->mem_oper); +memory_region_destroy (&xhci->mem_runtime); +memory_region_destroy (&xhci->mem_doorbell); + +for (i =0 ; i < xhci->numports; i++) { +XHCIPort *port = &xhci->ports[i]; +memory_region_del_subregion (&xhci->mem, &port->mem); +memory_region_destroy (&port->mem); + } + +/* destroy msix memory region */ +if (dev->msix_table && dev->msix_pba + && dev->msix_entry_used) { +memory_region_del_subregion (&xhci->mem, &dev->msix_table_mmio); +memory_region_destroy (&dev->msix_table_mmio); +memory_region_del_subregion (&xhci->mem, &dev->msix_pba_mmio); +memory_region_destroy (&dev->msix_pba_mmio); + } + +memory_region_destroy (&xhci->mem); + +usb_bus_release (&xhci->bus); +} +static int usb_xhci_post_load (void *opaque,int version_id) {XHCIState *xhci = opaque;
@@ -3835,6 +3880,7 @@ static void xhci_class_init(ObjectClass *klass, void *data) dc->hotpluggable = false;set_bit (DEVICE_CATEGORY_USB , dc->categories); k->init = usb_xhci_initfn; + k->exit = usb_xhci_exit; k->vendor_id =PCI_VENDOR_ID_NEC ; k->device_id =PCI_DEVICE_ID_NEC_UPD720200 ; k->class_id =PCI_CLASS_SERIAL_USB ;
Signature · 1 line
1.7.12.4
PATCH v3 9/10 usb: tag usb host controller as hotpluggable
In reply to [email protected]From: Gonglei <[email protected]>
usb host controller should be able to support hotplug/unplug, as the same as the other pci devices, which not enable multifunction capability.
BTW, the qemu have not the capability to support
hotplug mulitfuncition pci devices at present.
--- hw/usb/hcd-ehci-pci.c | 1 - hw/usb/hcd-ohci.c | 1 - hw/usb/hcd-uhci.c | 1 - hw/usb/hcd-xhci.c | 1 - 4 files changed, 4 deletions(-)
hw/usb/hcd-ehci-pci.c +0 -1
@@ -137,7 +137,6 @@ static void ehci_class_init(ObjectClass *klass, void *data) k->exit = usb_ehci_pci_exit; k->class_id =PCI_CLASS_SERIAL_USB ; k->config_write = usb_ehci_pci_write_config; - dc->hotpluggable = false; dc->vmsd = &vmstate_ehci_pci; dc->props = ehci_pci_properties; }
hw/usb/hcd-ohci.c +0 -1
@@ -2121,7 +2121,6 @@ static void ohci_pci_class_init(ObjectClass *klass, void *data)set_bit (DEVICE_CATEGORY_USB , dc->categories); dc->desc ="Apple USB Controller" ; dc->props = ohci_pci_properties; - dc->hotpluggable = false; dc->vmsd = &vmstate_ohci; }
hw/usb/hcd-uhci.c +0 -1
@@ -1300,7 +1300,6 @@ static void uhci_class_init(ObjectClass *klass, void *data) k->device_id = info->device_id; k->revision = info->revision; k->class_id =PCI_CLASS_SERIAL_USB ; - dc->hotpluggable = false; dc->vmsd = &vmstate_uhci; dc->props = uhci_properties;set_bit (DEVICE_CATEGORY_USB , dc->categories);
hw/usb/hcd-xhci.c +0 -1
@@ -3877,7 +3877,6 @@ static void xhci_class_init(ObjectClass *klass, void *data) dc->vmsd = &vmstate_xhci; dc->props = xhci_properties; dc->reset = xhci_reset; - dc->hotpluggable = false;set_bit (DEVICE_CATEGORY_USB , dc->categories); k->init = usb_xhci_initfn; k->exit = usb_xhci_exit;
Signature · 1 line
1.7.12.4
PATCH v3 10/10 usb: add usb host adapters exit trace
In reply to [email protected]From: Gonglei <[email protected]>
--- hw/usb/hcd-ehci.c | 2 ++ hw/usb/hcd-uhci.c | 2 ++ hw/usb/hcd-xhci.c | 2 ++ trace-events | 3 +++ 4 files changed, 9 insertions(+)
hw/usb/hcd-ehci.c +2 -0
@@ -2473,6 +2473,8 @@ void usb_ehci_realize(EHCIState *s, DeviceState *dev, Error **errp)void usb_ehci_unrealize (EHCIState *s,DeviceState *dev,Error **errp) { +trace_usb_ehci_unrealize (); +if (s->frame_timer) {timer_del (s->frame_timer);timer_free (s->frame_timer);
hw/usb/hcd-uhci.c +2 -0
@@ -1260,6 +1260,8 @@ static void usb_uhci_exit(PCIDevice *dev) {UHCIState *s =DO_UPCAST (UHCIState, dev, dev); +trace_usb_uhci_exit (); +if (s->frame_timer) {timer_del (s->frame_timer);timer_free (s->frame_timer);
hw/usb/hcd-xhci.c +2 -0
@@ -3649,6 +3649,8 @@ static void usb_xhci_exit(PCIDevice *dev)int i;XHCIState *xhci =XHCI (dev); +trace_usb_xhci_exit (); +for (i =0 ; i < xhci->numslots; i++) {xhci_disable_slot (xhci, i +1 ); }
trace-events +3 -0
@@ -292,6 +292,7 @@ usb_port_release(int bus, const char *port) "bus %d, port %s" # hw/usb/hcd-ehci.c usb_ehci_reset(void) "=== RESET ===" +usb_ehci_unrealize(void) "=== UNREALIZE ===" usb_ehci_opreg_read(uint32_t addr, const char *str, uint32_t val) "rd mmio %04x [%s] = %x" usb_ehci_opreg_write(uint32_t addr, const char *str, uint32_t val) "wr mmio %04x [%s] = %x" usb_ehci_opreg_change(uint32_t addr, const char *str, uint32_t new, uint32_t old) "ch mmio %04x [%s] = %x (old: %x)"
@@ -324,6 +325,7 @@ usb_ehci_dma_error(void) "" # hw/usb/hcd-uhci.c usb_uhci_reset(void) "=== RESET ===" +usb_uhci_exit(void) "=== EXIT ===" usb_uhci_schedule_start(void) "" usb_uhci_schedule_stop(void) "" usb_uhci_frame_start(uint32_t num) "nr %d"
@@ -353,6 +355,7 @@ usb_uhci_td_complete(uint32_t qh, uint32_t td) "qh 0x%x, td 0x%x" # hw/usb/hcd-xhci.c usb_xhci_reset(void) "=== RESET ===" +usb_xhci_exit(void) "=== EXIT ===" usb_xhci_run(void) "" usb_xhci_stop(void) "" usb_xhci_cap_read(uint32_t off, uint32_t val) "off 0x%04x, ret 0x%08x"
Signature · 1 line
1.7.12.4
Hi,
Gerd, do you plan to apply and pull the patch serials? Thanks!
I have finished some qtest cases for usb host adapters hotpug/unplug.
Best regards,
-Gonglei
Quoted 50 lines
-----Original Message-----
From: Gonglei (Arei)
Sent: Wednesday, June 04, 2014 4:32 PM
To: [email protected]
Cc: [email protected]; Luonengjun; Huangweidong (C); Huangpeng (Peter); Gonglei (Arei)
Subject: [PATCH v3 00/10] usb: usb host adapter hotplugFrom: Gonglei <[email protected]>
add support for usb host adapter hotplug, as the same as other pci devices.
change since v2:
* add ehci unrealize function suggested by Gerd.
* add usb host adapters exit trace.
* rebase on latest qemu master tree.changes since v1:
* reworkusb_bus_releasefunction suggested by Gerd.
* add more completely resource cleanup for every usb host adapter.
* fix memory leak.Gonglei (10):
usb: addusb_bus_releasefunction
usb-ohci: Fix memory leak for ohci timer
usb-ohci: add exit function
usb-uhci: clean up uhci resource when pci-uhci exit
usb-ehci: add vmstate properity for EHCIState
usb-ehci: add ehci unrealize funciton
usb-ehci: add ehci-pci device exit function
usb-xhci: add exit function
usb: tag usb host controller as hotpluggable
usb: add usb host adapters exit tracehw/usb/bus.c | 7 +++++++ hw/usb/hcd-ehci-pci.c | 15 ++++++++++++++- hw/usb/hcd-ehci.c | 33 ++++++++++++++++++++++++++++++++- hw/usb/hcd-ehci.h | 2 ++ hw/usb/hcd-ohci.c | 26 ++++++++++++++++++++++++-- hw/usb/hcd-uhci.c | 19 ++++++++++++++++++- hw/usb/hcd-xhci.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- include/hw/usb.h | 1 + trace-events | 3 +++ 9 files changed, 149 insertions(+), 6 deletions(-)--
1.7.12.4
Quoted 3 lines · Gonglei (Arei)
Hi,
Gerd, do you plan to apply and pull the patch serials? Thanks!
Series looks sane, I want run some stress tests on them before committing though. Havn't found yet the time to do that.
Quoted 1 line
I have finished some qtest cases for usb host adapters hotpug/unplug.
Send them over, no need to wait for this series to be committed, just mention the dependency in the cover letter and it'll be fine.
cheers,
Gerd
Quoted 8 lines
-----Original Message-----
From: Gerd Hoffmann [mailto:[email protected]]
Sent: Thursday, June 12, 2014 7:33 PM
To: Gonglei (Arei)
Cc: [email protected]; Luonengjun; Huangweidong (C); Huangpeng (Peter)
Subject: Re: [PATCH v3 00/10] usb: usb host adapter hotplug
Quoted 8 lines
Quoted 3 lines · Gonglei (Arei)
Hi,
Gerd, do you plan to apply and pull the patch serials? Thanks!
Series looks sane, I want run some stress tests on them before committing though. Havn't found yet the time to do that.
OK, thanks for your work and look forward to your results.
Quoted 5 lines
Quoted 1 line
I have finished some qtest cases for usb host adapters hotpug/unplug.
Send them over, no need to wait for this series to be committed, just mention the dependency in the cover letter and it'll be fine.
OK, I' will do this. Thanks.
Best regards,
-Gonglei