RFC: qbus reset callback and implement pci bus reset
cover RFC: qbus reset callback and implement pci bus reset
This patch isn't for 0.13 release. and for MST pci branch.
(git://git.kernel.org/pub/scm/linux/kernel/git/mst/qemu.git pci)
Patch description:
Introduce bus reset notion at qbus layer and implement pci bus reset with it.
At first related codes are cleaned up and then introduce bus reset callback. And then implement pci bus reset.
The main motivation is to implement pci bus reset. But I suppose scsi bus and ide bus also can take advantage of this patch series.
Isaku Yamahata (8):
apb: fix typo.
qdev: export qdev_reset() for later use.
pci: export pci_bus_reset() and pci_device_reset() for later use.
pci: make pci_device_reset() aware of qdev.
qdev: introduce bus reset callback and helper functions.
pci: use qbus bus reset callback.
pci: eliminate work around in pci_device_reset().
pci bridge: implement secondary bus reset.
hw/apb_pci.c | 8 ++++-- hw/e1000.c | 1 + hw/lsi53c895a.c | 2 + hw/pci.c | 31 ++++++++++++++++++++++--- hw/pci.h | 5 ++++ hw/pci_bridge.c | 15 +++++++++++- hw/pcnet.c | 1 + hw/qdev.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++---- hw/qdev.h | 8 ++++++ hw/rtl8139.c | 2 + hw/virtio-pci.c | 1 + 11 files changed, 127 insertions(+), 13 deletions(-)
PATCH 1/8 apb: fix typo.
fix typo.
--- hw/apb_pci.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
hw/apb_pci.c +3 -3
@@ -362,7 +362,7 @@ PCIBus *pci_apb_init(target_phys_addr_t special_base,/* APB secondary busses */ pci_dev =pci_create_multifunction (d->bus,PCI_DEVFN (1 ,0 ), true,"pbm-bridge" ); - br =DO_UPCAST (PCIBridge, dev, dev); + br =DO_UPCAST (PCIBridge, dev, pci_dev);pci_bridge_map_irq (br,"Advanced PCI Bus secondary bridge 1" , pci_apb_map_irq);qdev_init_nofail (&pci_dev->qdev);
@@ -370,7 +370,7 @@ PCIBus *pci_apb_init(target_phys_addr_t special_base, pci_dev =pci_create_multifunction (d->bus,PCI_DEVFN (1 ,1 ), true,"pbm-bridge" ); - br =DO_UPCAST (PCIBridge, dev, dev); + br =DO_UPCAST (PCIBridge, dev, pci_dev);pci_bridge_map_irq (br,"Advanced PCI Bus secondary bridge 2" , pci_apb_map_irq);qdev_init_nofail (&pci_dev->qdev);
@@ -462,7 +462,7 @@ static PCIDeviceInfo pbm_pci_bridge_info = { .qdev.name = "pbm-bridge", .qdev.size = sizeof(PCIBridge), .qdev.vmsd = &vmstate_pci_device, - .qdev.reset = pci_brdige_reset, + .qdev.reset = pci_bridge_reset, .init = apb_pci_bridge_initfn, .exit = pci_bridge_exitfn, .config_write = pci_bridge_write_config,
Signature · 1 line
1.7.1.1
PATCH 2/8 qdev: export qdev_reset() for later use.
In reply to Isaku Yamahataexport qdev_reset() for later use.
--- hw/qdev.c | 29 +++++++++++++++++++++++++---- hw/qdev.h | 1 + 2 files changed, 26 insertions(+), 4 deletions(-)
hw/qdev.c +25 -4
@@ -256,13 +256,34 @@ DeviceState *qdev_device_add(QemuOpts *opts)return qdev; } -static void qdev_reset (void *opaque) +/* +* reset the device. +* Bring the device into initial known state (to some extent) +* on warm reset(system reset). +* Typically on system reset(or power-on reset), bus reset occurs on +* each bus which causes devices to reset. +* This reset doesn't include software reset which is triggered by +* issuing reset command. Those device reset would be implemented in a bus +* specific way. +* +* For example +* PCI: reset with RST# signal asserted. Not FLR of advanced feature capability +* PCIe: conventional reset. Not FLR. +* ATA: hardware reset with RESET- signal asserted. Not DEVICE RESET command. +* SCSI: hard reset with SCSI RST signal asserted. +* Not bus device reset message. +*/ +void qdev_reset (DeviceState *dev) { -DeviceState *dev = opaque;if (dev->info->reset) dev->info->reset (dev); } +static void qdev_reset_fn (void *opaque) +{ +qdev_reset (opaque); +} +/* Initialize a device. Device properties should be set before calling this function. IRQs and MMIO regions should be connected/mapped after calling this function.
@@ -278,7 +299,7 @@ int qdev_init(DeviceState *dev)qdev_free (dev);return rc; } -qemu_register_reset (qdev_reset, dev); +qemu_register_reset (qdev_reset_fn, dev);if (dev->info->vmsd) { vmstate_register_with_alias_id(dev, -1, dev->info->vmsd, dev, dev->instance_id_alias,
@@ -350,7 +371,7 @@ void qdev_free(DeviceState *dev)if (dev->opts)qemu_opts_del (dev->opts); } -qemu_unregister_reset (qdev_reset, dev); +qemu_unregister_reset (qdev_reset_fn, dev);QLIST_REMOVE (dev, sibling);for (prop = dev->info->props; prop && prop->name; prop++) {if (prop->info->free) {
hw/qdev.h +1 -0
@@ -162,6 +162,7 @@ struct DeviceInfo {extern DeviceInfo *device_info_list;void qdev_register (DeviceInfo *info); +void qdev_reset (DeviceState *dev);/* Register device properties. */ /* GPIO inputs also double as IRQ sinks. */
Signature · 1 line
1.7.1.1
PATCH 3/8 pci: export pci_bus_reset() and pci_device_reset() for later use.
In reply to Isaku Yamahataexport pci_bus_reset() and pci_device_reset() for later use with slight function signature adjustment.
--- hw/pci.c | 17 +++++++++++++---- hw/pci.h | 4 ++++ 2 files changed, 17 insertions(+), 4 deletions(-)
hw/pci.c +13 -4
@@ -130,7 +130,8 @@ static void pci_update_irq_status(PCIDevice *dev) } } -static void pci_device_reset (PCIDevice *dev) +/* Reset the device in response to RST# signal. */ +void pci_device_reset (PCIDevice *dev) {int r;
@@ -158,9 +159,12 @@ static void pci_device_reset(PCIDevice *dev)pci_update_mappings (dev); } -static void pci_bus_reset (void *opaque) +/* +* Trigger pci bus reset under a given bus. +* This functions emulates RST#. +*/ +void pci_bus_reset (PCIBus *bus) { -PCIBus *bus = opaque;int i;for (i =0 ; i < bus->nirq; i++) {
@@ -173,6 +177,11 @@ static void pci_bus_reset(void *opaque) } } +static void pci_bus_reset_fn (void *opaque) +{ +pci_bus_reset (opaque); +} +static void pci_host_bus_register (int domain,PCIBus *bus) {struct PCIHostBus *host;
@@ -227,7 +236,7 @@ void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,pci_host_bus_register (0 , bus);/* for now only pci domain 0 is supported */ vmstate_register (NULL ,-1 , &vmstate_pcibus, bus); -qemu_register_reset (pci_bus_reset, bus); +qemu_register_reset (pci_bus_reset_fn, bus); }PCIBus *pci_bus_new (DeviceState *parent,const char *name,int devfn_min)
hw/pci.h +4 -0
@@ -207,6 +207,10 @@ typedef int (*pci_hotplug_fn)(DeviceState *qdev, PCIDevice *pci_dev, int state);void pci_bus_new_inplace (PCIBus *bus,DeviceState *parent,const char *name,int devfn_min);PCIBus *pci_bus_new (DeviceState *parent,const char *name,int devfn_min); + +void pci_bus_reset (PCIBus *bus); +void pci_device_reset (PCIDevice *dev); +void pci_bus_irqs (PCIBus *bus,pci_set_irq_fn set_irq,pci_map_irq_fn map_irq,void *irq_opaque,int nirq);void pci_bus_hotplug (PCIBus *bus,pci_hotplug_fn hotplug,DeviceState *dev);
Signature · 1 line
1.7.1.1
PATCH 4/8 pci: make pci_device_reset() aware of qdev.
In reply to Isaku YamahataMake pci_device_reset handle qdevfied device and non-converted device differently.
Later they will be handled differently.
--- hw/pci.c | 35 +++++++++++++++++++++++++++++++++-- hw/pci.h | 1 + 2 files changed, 34 insertions(+), 2 deletions(-)
hw/pci.c +33 -2
@@ -130,8 +130,7 @@ static void pci_update_irq_status(PCIDevice *dev) } } -/* Reset the device in response to RST# signal. */ -void pci_device_reset (PCIDevice *dev) +void pci_device_reset_default (PCIDevice *dev) {int r;
@@ -159,6 +158,38 @@ void pci_device_reset(PCIDevice *dev)pci_update_mappings (dev); } +/* Reset the device in response to RST# signal. */ +void pci_device_reset (PCIDevice *dev) +{ +if (!dev->qdev.info || !dev->qdev.info->reset) { +/* for not qdevified device or reset isn't implemented property. +* So take care of them in PCI generic layer. +*/ +pci_device_reset_default (dev); +return ; + } + +/* +* There are two paths to reset pci device. Each resets does partially. +* qemu_system_reset() +* -> pci_device_reset() with bus +* -> pci_device_reset_default() which resets pci common part. +* -> DeviceState::reset: each device specific reset hanlder +* which resets device specific part. +* +* TODO: +* It requires two execution paths to reset the device fully. +* It is confusing and prone to error. Each device should know all +* its states. +* So move this part to each device specific callback. +*/ + +/* For now qdev_reset() is called directly by qemu_system_reset() */ +/* qdev_reset(&dev->qdev); */ + +pci_device_reset_default (dev); +} +/* * Trigger pci bus reset under a given bus. * This functions emulates RST#.
hw/pci.h +1 -0
@@ -210,6 +210,7 @@ PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min);void pci_bus_reset (PCIBus *bus);void pci_device_reset (PCIDevice *dev); +void pci_device_reset_default (PCIDevice *dev);void pci_bus_irqs (PCIBus *bus,pci_set_irq_fn set_irq,pci_map_irq_fn map_irq,void *irq_opaque,int nirq);
Signature · 1 line
1.7.1.1
PATCH 5/8 qdev: introduce bus reset callback and helper functions.
In reply to Isaku YamahataIntroduce bus reset callback to support bus reset at qbus layer and a function to trigger bus reset.
--- hw/qdev.c | 39 +++++++++++++++++++++++++++++++++++++-- hw/qdev.h | 7 +++++++ 2 files changed, 44 insertions(+), 2 deletions(-)
hw/qdev.c +37 -2
@@ -256,6 +256,14 @@ DeviceState *qdev_device_add(QemuOpts *opts)return qdev; } +void qdev_reset_default (DeviceState *dev) +{ +BusState *bus; + QLIST_FOREACH(bus, &dev->child_bus, sibling) { +qbus_reset (bus); + } +} +/* * reset the device. * Bring the device into initial known state (to some extent)
@@ -299,7 +307,11 @@ int qdev_init(DeviceState *dev)qdev_free (dev);return rc; } -qemu_register_reset (qdev_reset_fn, dev); +if (!dev->parent_bus->info->reset) { +/* If bus defines reset callback, reset will be propagated +via bus reset callback. So don't register device reset callback */ +qemu_register_reset (qdev_reset_fn, dev); + }if (dev->info->vmsd) { vmstate_register_with_alias_id(dev, -1, dev->info->vmsd, dev, dev->instance_id_alias,
@@ -671,6 +683,26 @@ static BusState *qbus_find(const char *path) } } +void qbus_reset_default (BusState *bus) +{ +DeviceState *dev; + QLIST_FOREACH(dev, &bus->children, sibling) { +qdev_reset (dev); + } +} + +/* trigger bus reset */ +void qbus_reset (BusState *bus) +{ +if (bus->info->reset) + bus->info->reset (bus); +} + +static void qbus_reset_fn (void *opaque) +{ +qbus_reset (opaque); +} +void qbus_create_inplace (BusState *bus,BusInfo *info,DeviceState *parent,const char *name) {
@@ -705,7 +737,10 @@ void qbus_create_inplace(BusState *bus, BusInfo *info,QLIST_INSERT_HEAD (&parent->child_bus, bus, sibling); parent->num_child_bus++; } - +if (!parent || !parent->info || !parent->info->reset) { +/* parent device should take care of child bus reset */ +qemu_register_reset (qbus_reset_fn, bus); + } }BusState *qbus_create (BusInfo *info,DeviceState *parent,const char *name)
hw/qdev.h +7 -0
@@ -50,6 +50,7 @@ struct DeviceState {typedef void (*bus_dev_printfn )(Monitor *mon,DeviceState *dev,int indent);typedef char *(*bus_get_dev_path )(DeviceState *dev); +typedef void (*bus_resetfn )(BusState *bus);struct BusInfo {const char *name;
@@ -57,6 +58,9 @@ struct BusInfo {bus_dev_printfn print_dev;bus_get_dev_path get_dev_path;Property *props; + +/* bus reset callbacks */ +bus_resetfn reset; };struct BusState {
@@ -163,6 +167,7 @@ extern DeviceInfo *device_info_list;void qdev_register (DeviceInfo *info);void qdev_reset (DeviceState *dev); +void qdev_reset_default (DeviceState *dev);/* Register device properties. */ /* GPIO inputs also double as IRQ sinks. */
@@ -179,6 +184,8 @@ void qbus_create_inplace(BusState *bus, BusInfo *info, DeviceState *parent, const char *name);BusState *qbus_create (BusInfo *info,DeviceState *parent,const char *name);void qbus_free (BusState *bus); +void qbus_reset (BusState *bus); +void qbus_reset_default (BusState *bus);#define FROM_QBUS (type, dev) DO_UPCAST(type, qbus, dev)
Signature · 1 line
1.7.1.1
PATCH 6/8 pci: use qbus bus reset callback.
In reply to Isaku Yamahatause qbus bus reset callback.
--- hw/apb_pci.c | 2 ++ hw/pci.c | 23 ++++++----------------- hw/pci_bridge.c | 2 ++ 3 files changed, 10 insertions(+), 17 deletions(-)
hw/apb_pci.c +2 -0
@@ -384,6 +384,8 @@ static void pci_pbm_reset(DeviceState *d)unsigned int i;APBState *s =container_of (d, APBState, busdev.qdev); +qdev_reset_default (d); +for (i =0 ; i <8 ; i++) { s->pci_irq_map[i] &=PBM_PCI_IMR_MASK ; }
hw/pci.c +6 -17
@@ -40,12 +40,14 @@static void pcibus_dev_print (Monitor *mon,DeviceState *dev,int indent);static char *pcibus_get_dev_path (DeviceState *dev); +static void pci_bus_reset_fn (BusState *qbus);struct BusInfo pci_bus_info = { .name ="PCI" , .size =sizeof (PCIBus), .print_dev = pcibus_dev_print, .get_dev_path = pcibus_get_dev_path, + .reset = pci_bus_reset_fn, .props = (Property []) {DEFINE_PROP_PCI_DEVFN ("addr" , PCIDevice, devfn,-1 ),DEFINE_PROP_STRING ("romfile" , PCIDevice, romfile),
@@ -170,23 +172,11 @@ void pci_device_reset(PCIDevice *dev) }/* -* There are two paths to reset pci device. Each resets does partially. -* qemu_system_reset() -* -> pci_device_reset() with bus -* -> pci_device_reset_default() which resets pci common part. -* -> DeviceState::reset: each device specific reset hanlder -* which resets device specific part. -* * TODO: -* It requires two execution paths to reset the device fully. -* It is confusing and prone to error. Each device should know all -* its states. +* Each device should know all its states. * So move this part to each device specific callback. */ - -/* For now qdev_reset() is called directly by qemu_system_reset() */ -/* qdev_reset(&dev->qdev); */ - +qdev_reset (&dev->qdev);pci_device_reset_default (dev); }
@@ -208,9 +198,9 @@ void pci_bus_reset(PCIBus *bus) } } -static void pci_bus_reset_fn (void *opaque) +static void pci_bus_reset_fn (BusState *qbus) { -pci_bus_reset (opaque); +pci_bus_reset (DO_UPCAST (PCIBus, qbus, qbus)); }static void pci_host_bus_register (int domain,PCIBus *bus)
@@ -267,7 +257,6 @@ void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,pci_host_bus_register (0 , bus);/* for now only pci domain 0 is supported */ vmstate_register (NULL ,-1 , &vmstate_pcibus, bus); -qemu_register_reset (pci_bus_reset_fn, bus); }PCIBus *pci_bus_new (DeviceState *parent,const char *name,int devfn_min)
hw/pci_bridge.c +2 -0
@@ -158,6 +158,8 @@ void pci_bridge_reset_reg(PCIDevice *dev)void pci_bridge_reset (DeviceState *qdev) {PCIDevice *dev =DO_UPCAST (PCIDevice, qdev, qdev); +PCIBridge *br =DO_UPCAST (PCIBridge, dev, dev); +pci_bus_reset (&br->sec_bus);pci_bridge_reset_reg (dev); }
Signature · 1 line
1.7.1.1
PATCH 7/8 pci: eliminate work around in pci_device_reset().
In reply to Isaku YamahataEliminate work around in pci_device_reset() by
making each pci reset function to call pci_device_reset_default(). Each device should know reset itself. It shouldn't be done pci generic layer automatically. PCI layer should just signal reset and let each device respond to reset.
--- hw/e1000.c | 1 + hw/lsi53c895a.c | 2 ++ hw/pci.c | 6 ------ hw/pcnet.c | 1 + hw/rtl8139.c | 2 ++ hw/virtio-pci.c | 1 + 6 files changed, 7 insertions(+), 6 deletions(-)
hw/e1000.c +1 -0
@@ -1077,6 +1077,7 @@ static void e1000_reset(void *opaque)memmove (d->mac_reg, mac_reg_init,sizeof mac_reg_init); d->rxbuf_min_shift =1 ;memset (&d->tx,0 ,sizeof d->tx); +pci_device_reset_default (&d->dev); }static NetClientInfo net_e1000_info = {
hw/lsi53c895a.c +2 -0
@@ -358,6 +358,8 @@ static void lsi_soft_reset(LSIState *s)qemu_free (s->current); s->current =NULL ; } + +pci_device_reset_default (&s->dev); }static int lsi_dma_40bit (LSIState *s)
hw/pci.c +0 -6
@@ -171,13 +171,7 @@ void pci_device_reset(PCIDevice *dev)return ; } -/* -* TODO: -* Each device should know all its states. -* So move this part to each device specific callback. -*/ qdev_reset (&dev->qdev); -pci_device_reset_default (dev); }/*
hw/pcnet.c +1 -0
@@ -2023,6 +2023,7 @@ static void pci_reset(DeviceState *dev)PCIPCNetState *d =DO_UPCAST (PCIPCNetState, pci_dev.qdev, dev);pcnet_h_reset (&d->state); +pci_device_reset_default (&d->pci_dev); }static PCIDeviceInfo pcnet_info = {
hw/rtl8139.c +2 -0
@@ -1260,6 +1260,8 @@ static void rtl8139_reset(DeviceState *d)/* reset tally counters */ RTL8139TallyCounters_clear (&s->tally_counters); + +pci_device_reset_default (&s->dev); }static void RTL8139TallyCounters_clear (RTL8139TallyCounters * counters)
hw/virtio-pci.c +1 -0
@@ -184,6 +184,7 @@ static void virtio_pci_reset(DeviceState *d)virtio_reset (proxy->vdev);msix_reset (&proxy->pci_dev); proxy->bugs =0 ; +pci_device_reset_default (&proxy->pci_dev); }static void virtio_ioport_write (void *opaque,uint32_t addr,uint32_t val)
Signature · 1 line
1.7.1.1
PATCH 8/8 pci bridge: implement secondary bus reset.
In reply to Isaku Yamahataimplement secondary bus reset.
--- hw/pci_bridge.c | 13 ++++++++++++- 1 files changed, 12 insertions(+), 1 deletions(-)
hw/pci_bridge.c +12 -1
@@ -119,6 +119,9 @@ pcibus_t pci_bridge_get_limit(const PCIDevice *bridge, uint8_t type)void pci_bridge_write_config (PCIDevice *d,uint32_t address,uint32_t val,int len) { +PCIBridge *s =container_of (d, PCIBridge, dev); +uint16_t bridge_control =pci_get_word (d->config +PCI_BRIDGE_CONTROL ); +pci_default_write_config (d, address, val, len); if (/* io base/limit */
@@ -127,9 +130,17 @@ void pci_bridge_write_config(PCIDevice *d,/* memory base/limit, prefetchable base/limit and io base/limit upper 16 */ ranges_overlap(address, len, PCI_MEMORY_BASE, 20)) { -PCIBridge *s =container_of (d, PCIBridge, dev);pci_bridge_update_mappings (&s->sec_bus); } + +if (ranges_overlap (address, len,PCI_BRIDGE_CONTROL ,2 )) { +uint16_t new =pci_get_word (d->config +PCI_BRIDGE_CONTROL ); +if (!(bridge_control &PCI_BRIDGE_CTL_BUS_RESET ) && + (new &PCI_BRIDGE_CTL_BUS_RESET )) { +/* 0 -> 1 */ +pci_bus_reset (&s->sec_bus); + } + } }/* reset bridge specific configuration registers */
Signature · 1 line
1.7.1.1