1783753fdSIsaku Yamahata /* 2783753fdSIsaku Yamahata * QEMU PCI bus manager 3783753fdSIsaku Yamahata * 4783753fdSIsaku Yamahata * Copyright (c) 2004 Fabrice Bellard 5783753fdSIsaku Yamahata * 6783753fdSIsaku Yamahata * Permission is hereby granted, free of charge, to any person obtaining a copy 7783753fdSIsaku Yamahata * of this software and associated documentation files (the "Software"), to dea 8783753fdSIsaku Yamahata 9783753fdSIsaku Yamahata * in the Software without restriction, including without limitation the rights 10783753fdSIsaku Yamahata * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11783753fdSIsaku Yamahata * copies of the Software, and to permit persons to whom the Software is 12783753fdSIsaku Yamahata * furnished to do so, subject to the following conditions: 13783753fdSIsaku Yamahata * 14783753fdSIsaku Yamahata * The above copyright notice and this permission notice shall be included in 15783753fdSIsaku Yamahata * all copies or substantial portions of the Software. 16783753fdSIsaku Yamahata * 17783753fdSIsaku Yamahata * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18783753fdSIsaku Yamahata * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19783753fdSIsaku Yamahata * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20783753fdSIsaku Yamahata * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21783753fdSIsaku Yamahata * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM 22783753fdSIsaku Yamahata 23783753fdSIsaku Yamahata * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24783753fdSIsaku Yamahata * THE SOFTWARE. 25783753fdSIsaku Yamahata */ 26783753fdSIsaku Yamahata /* 27783753fdSIsaku Yamahata * split out from pci.c 28783753fdSIsaku Yamahata * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp> 29783753fdSIsaku Yamahata * VA Linux Systems Japan K.K. 30783753fdSIsaku Yamahata */ 31783753fdSIsaku Yamahata 32c759b24fSMichael S. Tsirkin #include "hw/pci/pci_bridge.h" 3306aac7bdSMichael S. Tsirkin #include "hw/pci/pci_bus.h" 34*1de7afc9SPaolo Bonzini #include "qemu/range.h" 35783753fdSIsaku Yamahata 36f4c817e0SIsaku Yamahata /* PCI bridge subsystem vendor ID helper functions */ 37f4c817e0SIsaku Yamahata #define PCI_SSVID_SIZEOF 8 38f4c817e0SIsaku Yamahata #define PCI_SSVID_SVID 4 39f4c817e0SIsaku Yamahata #define PCI_SSVID_SSID 6 40f4c817e0SIsaku Yamahata 41f4c817e0SIsaku Yamahata int pci_bridge_ssvid_init(PCIDevice *dev, uint8_t offset, 42f4c817e0SIsaku Yamahata uint16_t svid, uint16_t ssid) 43f4c817e0SIsaku Yamahata { 44f4c817e0SIsaku Yamahata int pos; 45f4c817e0SIsaku Yamahata pos = pci_add_capability(dev, PCI_CAP_ID_SSVID, offset, PCI_SSVID_SIZEOF); 46f4c817e0SIsaku Yamahata if (pos < 0) { 47f4c817e0SIsaku Yamahata return pos; 48f4c817e0SIsaku Yamahata } 49f4c817e0SIsaku Yamahata 50f4c817e0SIsaku Yamahata pci_set_word(dev->config + pos + PCI_SSVID_SVID, svid); 51f4c817e0SIsaku Yamahata pci_set_word(dev->config + pos + PCI_SSVID_SSID, ssid); 52f4c817e0SIsaku Yamahata return pos; 53f4c817e0SIsaku Yamahata } 54f4c817e0SIsaku Yamahata 5568f79994SIsaku Yamahata /* Accessor function to get parent bridge device from pci bus. */ 56783753fdSIsaku Yamahata PCIDevice *pci_bridge_get_device(PCIBus *bus) 57783753fdSIsaku Yamahata { 58783753fdSIsaku Yamahata return bus->parent_dev; 59783753fdSIsaku Yamahata } 60783753fdSIsaku Yamahata 6168f79994SIsaku Yamahata /* Accessor function to get secondary bus from pci-to-pci bridge device */ 6268f79994SIsaku Yamahata PCIBus *pci_bridge_get_sec_bus(PCIBridge *br) 6368f79994SIsaku Yamahata { 6468f79994SIsaku Yamahata return &br->sec_bus; 6568f79994SIsaku Yamahata } 6668f79994SIsaku Yamahata 6768f79994SIsaku Yamahata static uint32_t pci_config_get_io_base(const PCIDevice *d, 68783753fdSIsaku Yamahata uint32_t base, uint32_t base_upper16) 69783753fdSIsaku Yamahata { 70783753fdSIsaku Yamahata uint32_t val; 71783753fdSIsaku Yamahata 72783753fdSIsaku Yamahata val = ((uint32_t)d->config[base] & PCI_IO_RANGE_MASK) << 8; 73783753fdSIsaku Yamahata if (d->config[base] & PCI_IO_RANGE_TYPE_32) { 74783753fdSIsaku Yamahata val |= (uint32_t)pci_get_word(d->config + base_upper16) << 16; 75783753fdSIsaku Yamahata } 76783753fdSIsaku Yamahata return val; 77783753fdSIsaku Yamahata } 78783753fdSIsaku Yamahata 7968f79994SIsaku Yamahata static pcibus_t pci_config_get_memory_base(const PCIDevice *d, uint32_t base) 80783753fdSIsaku Yamahata { 81783753fdSIsaku Yamahata return ((pcibus_t)pci_get_word(d->config + base) & PCI_MEMORY_RANGE_MASK) 82783753fdSIsaku Yamahata << 16; 83783753fdSIsaku Yamahata } 84783753fdSIsaku Yamahata 8568f79994SIsaku Yamahata static pcibus_t pci_config_get_pref_base(const PCIDevice *d, 86783753fdSIsaku Yamahata uint32_t base, uint32_t upper) 87783753fdSIsaku Yamahata { 88783753fdSIsaku Yamahata pcibus_t tmp; 89783753fdSIsaku Yamahata pcibus_t val; 90783753fdSIsaku Yamahata 91783753fdSIsaku Yamahata tmp = (pcibus_t)pci_get_word(d->config + base); 92783753fdSIsaku Yamahata val = (tmp & PCI_PREF_RANGE_MASK) << 16; 93783753fdSIsaku Yamahata if (tmp & PCI_PREF_RANGE_TYPE_64) { 94783753fdSIsaku Yamahata val |= (pcibus_t)pci_get_long(d->config + upper) << 32; 95783753fdSIsaku Yamahata } 96783753fdSIsaku Yamahata return val; 97783753fdSIsaku Yamahata } 98783753fdSIsaku Yamahata 9968f79994SIsaku Yamahata /* accessor function to get bridge filtering base address */ 10068f79994SIsaku Yamahata pcibus_t pci_bridge_get_base(const PCIDevice *bridge, uint8_t type) 101783753fdSIsaku Yamahata { 102783753fdSIsaku Yamahata pcibus_t base; 103783753fdSIsaku Yamahata if (type & PCI_BASE_ADDRESS_SPACE_IO) { 104783753fdSIsaku Yamahata base = pci_config_get_io_base(bridge, 105783753fdSIsaku Yamahata PCI_IO_BASE, PCI_IO_BASE_UPPER16); 106783753fdSIsaku Yamahata } else { 107783753fdSIsaku Yamahata if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) { 108783753fdSIsaku Yamahata base = pci_config_get_pref_base( 109783753fdSIsaku Yamahata bridge, PCI_PREF_MEMORY_BASE, PCI_PREF_BASE_UPPER32); 110783753fdSIsaku Yamahata } else { 111783753fdSIsaku Yamahata base = pci_config_get_memory_base(bridge, PCI_MEMORY_BASE); 112783753fdSIsaku Yamahata } 113783753fdSIsaku Yamahata } 114783753fdSIsaku Yamahata 115783753fdSIsaku Yamahata return base; 116783753fdSIsaku Yamahata } 117783753fdSIsaku Yamahata 11868f79994SIsaku Yamahata /* accessor funciton to get bridge filtering limit */ 11968f79994SIsaku Yamahata pcibus_t pci_bridge_get_limit(const PCIDevice *bridge, uint8_t type) 120783753fdSIsaku Yamahata { 121783753fdSIsaku Yamahata pcibus_t limit; 122783753fdSIsaku Yamahata if (type & PCI_BASE_ADDRESS_SPACE_IO) { 123783753fdSIsaku Yamahata limit = pci_config_get_io_base(bridge, 124783753fdSIsaku Yamahata PCI_IO_LIMIT, PCI_IO_LIMIT_UPPER16); 125783753fdSIsaku Yamahata limit |= 0xfff; /* PCI bridge spec 3.2.5.6. */ 126783753fdSIsaku Yamahata } else { 127783753fdSIsaku Yamahata if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) { 128783753fdSIsaku Yamahata limit = pci_config_get_pref_base( 129783753fdSIsaku Yamahata bridge, PCI_PREF_MEMORY_LIMIT, PCI_PREF_LIMIT_UPPER32); 130783753fdSIsaku Yamahata } else { 131783753fdSIsaku Yamahata limit = pci_config_get_memory_base(bridge, PCI_MEMORY_LIMIT); 132783753fdSIsaku Yamahata } 133783753fdSIsaku Yamahata limit |= 0xfffff; /* PCI bridge spec 3.2.5.{1, 8}. */ 134783753fdSIsaku Yamahata } 135783753fdSIsaku Yamahata return limit; 136783753fdSIsaku Yamahata } 137783753fdSIsaku Yamahata 1387df32ca0SMichael S. Tsirkin static void pci_bridge_init_alias(PCIBridge *bridge, MemoryRegion *alias, 1397df32ca0SMichael S. Tsirkin uint8_t type, const char *name, 1407df32ca0SMichael S. Tsirkin MemoryRegion *space, 1417df32ca0SMichael S. Tsirkin MemoryRegion *parent_space, 1427df32ca0SMichael S. Tsirkin bool enabled) 1437df32ca0SMichael S. Tsirkin { 1447df32ca0SMichael S. Tsirkin pcibus_t base = pci_bridge_get_base(&bridge->dev, type); 1457df32ca0SMichael S. Tsirkin pcibus_t limit = pci_bridge_get_limit(&bridge->dev, type); 1467df32ca0SMichael S. Tsirkin /* TODO: this doesn't handle base = 0 limit = 2^64 - 1 correctly. 1477df32ca0SMichael S. Tsirkin * Apparently no way to do this with existing memory APIs. */ 1487df32ca0SMichael S. Tsirkin pcibus_t size = enabled && limit >= base ? limit + 1 - base : 0; 1497df32ca0SMichael S. Tsirkin 1507df32ca0SMichael S. Tsirkin memory_region_init_alias(alias, name, space, base, size); 1517df32ca0SMichael S. Tsirkin memory_region_add_subregion_overlap(parent_space, base, alias, 1); 1527df32ca0SMichael S. Tsirkin } 1537df32ca0SMichael S. Tsirkin 154b308c82cSAvi Kivity static PCIBridgeWindows *pci_bridge_region_init(PCIBridge *br) 1557df32ca0SMichael S. Tsirkin { 1567df32ca0SMichael S. Tsirkin PCIBus *parent = br->dev.bus; 157b308c82cSAvi Kivity PCIBridgeWindows *w = g_new(PCIBridgeWindows, 1); 1587df32ca0SMichael S. Tsirkin uint16_t cmd = pci_get_word(br->dev.config + PCI_COMMAND); 1597df32ca0SMichael S. Tsirkin 160b308c82cSAvi Kivity pci_bridge_init_alias(br, &w->alias_pref_mem, 1617df32ca0SMichael S. Tsirkin PCI_BASE_ADDRESS_MEM_PREFETCH, 1627df32ca0SMichael S. Tsirkin "pci_bridge_pref_mem", 163336411caSMichael S. Tsirkin &br->address_space_mem, 1647df32ca0SMichael S. Tsirkin parent->address_space_mem, 1657df32ca0SMichael S. Tsirkin cmd & PCI_COMMAND_MEMORY); 166b308c82cSAvi Kivity pci_bridge_init_alias(br, &w->alias_mem, 1677df32ca0SMichael S. Tsirkin PCI_BASE_ADDRESS_SPACE_MEMORY, 1687df32ca0SMichael S. Tsirkin "pci_bridge_mem", 169336411caSMichael S. Tsirkin &br->address_space_mem, 1707df32ca0SMichael S. Tsirkin parent->address_space_mem, 1717df32ca0SMichael S. Tsirkin cmd & PCI_COMMAND_MEMORY); 172b308c82cSAvi Kivity pci_bridge_init_alias(br, &w->alias_io, 1737df32ca0SMichael S. Tsirkin PCI_BASE_ADDRESS_SPACE_IO, 1747df32ca0SMichael S. Tsirkin "pci_bridge_io", 175336411caSMichael S. Tsirkin &br->address_space_io, 1767df32ca0SMichael S. Tsirkin parent->address_space_io, 1777df32ca0SMichael S. Tsirkin cmd & PCI_COMMAND_IO); 1787df32ca0SMichael S. Tsirkin /* TODO: optinal VGA and VGA palette snooping support. */ 179b308c82cSAvi Kivity 180b308c82cSAvi Kivity return w; 1817df32ca0SMichael S. Tsirkin } 1827df32ca0SMichael S. Tsirkin 183b308c82cSAvi Kivity static void pci_bridge_region_del(PCIBridge *br, PCIBridgeWindows *w) 1847df32ca0SMichael S. Tsirkin { 1857df32ca0SMichael S. Tsirkin PCIBus *parent = br->dev.bus; 186b308c82cSAvi Kivity 187b308c82cSAvi Kivity memory_region_del_subregion(parent->address_space_io, &w->alias_io); 188b308c82cSAvi Kivity memory_region_del_subregion(parent->address_space_mem, &w->alias_mem); 189b308c82cSAvi Kivity memory_region_del_subregion(parent->address_space_mem, &w->alias_pref_mem); 190b308c82cSAvi Kivity } 191b308c82cSAvi Kivity 192b308c82cSAvi Kivity static void pci_bridge_region_cleanup(PCIBridge *br, PCIBridgeWindows *w) 193b308c82cSAvi Kivity { 194b308c82cSAvi Kivity memory_region_destroy(&w->alias_io); 195b308c82cSAvi Kivity memory_region_destroy(&w->alias_mem); 196b308c82cSAvi Kivity memory_region_destroy(&w->alias_pref_mem); 197b308c82cSAvi Kivity g_free(w); 1987df32ca0SMichael S. Tsirkin } 1997df32ca0SMichael S. Tsirkin 2007df32ca0SMichael S. Tsirkin static void pci_bridge_update_mappings(PCIBridge *br) 2017df32ca0SMichael S. Tsirkin { 202b308c82cSAvi Kivity PCIBridgeWindows *w = br->windows; 203b308c82cSAvi Kivity 2047df32ca0SMichael S. Tsirkin /* Make updates atomic to: handle the case of one VCPU updating the bridge 2057df32ca0SMichael S. Tsirkin * while another accesses an unaffected region. */ 2067df32ca0SMichael S. Tsirkin memory_region_transaction_begin(); 207b308c82cSAvi Kivity pci_bridge_region_del(br, br->windows); 208b308c82cSAvi Kivity br->windows = pci_bridge_region_init(br); 2097df32ca0SMichael S. Tsirkin memory_region_transaction_commit(); 210b308c82cSAvi Kivity pci_bridge_region_cleanup(br, w); 2117df32ca0SMichael S. Tsirkin } 2127df32ca0SMichael S. Tsirkin 21368f79994SIsaku Yamahata /* default write_config function for PCI-to-PCI bridge */ 21468f79994SIsaku Yamahata void pci_bridge_write_config(PCIDevice *d, 215783753fdSIsaku Yamahata uint32_t address, uint32_t val, int len) 216783753fdSIsaku Yamahata { 217a5fce077SIsaku Yamahata PCIBridge *s = container_of(d, PCIBridge, dev); 218a5fce077SIsaku Yamahata uint16_t oldctl = pci_get_word(d->config + PCI_BRIDGE_CONTROL); 219a5fce077SIsaku Yamahata uint16_t newctl; 220a5fce077SIsaku Yamahata 221783753fdSIsaku Yamahata pci_default_write_config(d, address, val, len); 222783753fdSIsaku Yamahata 2237df32ca0SMichael S. Tsirkin if (ranges_overlap(address, len, PCI_COMMAND, 2) || 2247df32ca0SMichael S. Tsirkin 2257df32ca0SMichael S. Tsirkin /* io base/limit */ 226783753fdSIsaku Yamahata ranges_overlap(address, len, PCI_IO_BASE, 2) || 227783753fdSIsaku Yamahata 228783753fdSIsaku Yamahata /* memory base/limit, prefetchable base/limit and 229783753fdSIsaku Yamahata io base/limit upper 16 */ 230783753fdSIsaku Yamahata ranges_overlap(address, len, PCI_MEMORY_BASE, 20)) { 2317df32ca0SMichael S. Tsirkin pci_bridge_update_mappings(s); 232783753fdSIsaku Yamahata } 233a5fce077SIsaku Yamahata 234a5fce077SIsaku Yamahata newctl = pci_get_word(d->config + PCI_BRIDGE_CONTROL); 235a5fce077SIsaku Yamahata if (~oldctl & newctl & PCI_BRIDGE_CTL_BUS_RESET) { 236a5fce077SIsaku Yamahata /* Trigger hot reset on 0->1 transition. */ 237a5fce077SIsaku Yamahata pci_bus_reset(&s->sec_bus); 238a5fce077SIsaku Yamahata } 239783753fdSIsaku Yamahata } 240783753fdSIsaku Yamahata 2410208def1SIsaku Yamahata void pci_bridge_disable_base_limit(PCIDevice *dev) 2420208def1SIsaku Yamahata { 2430208def1SIsaku Yamahata uint8_t *conf = dev->config; 2440208def1SIsaku Yamahata 2450208def1SIsaku Yamahata pci_byte_test_and_set_mask(conf + PCI_IO_BASE, 2460208def1SIsaku Yamahata PCI_IO_RANGE_MASK & 0xff); 2470208def1SIsaku Yamahata pci_byte_test_and_clear_mask(conf + PCI_IO_LIMIT, 2480208def1SIsaku Yamahata PCI_IO_RANGE_MASK & 0xff); 2490208def1SIsaku Yamahata pci_word_test_and_set_mask(conf + PCI_MEMORY_BASE, 2500208def1SIsaku Yamahata PCI_MEMORY_RANGE_MASK & 0xffff); 2510208def1SIsaku Yamahata pci_word_test_and_clear_mask(conf + PCI_MEMORY_LIMIT, 2520208def1SIsaku Yamahata PCI_MEMORY_RANGE_MASK & 0xffff); 2530208def1SIsaku Yamahata pci_word_test_and_set_mask(conf + PCI_PREF_MEMORY_BASE, 2540208def1SIsaku Yamahata PCI_PREF_RANGE_MASK & 0xffff); 2550208def1SIsaku Yamahata pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_LIMIT, 2560208def1SIsaku Yamahata PCI_PREF_RANGE_MASK & 0xffff); 257cd7898f7SMichael S. Tsirkin pci_set_long(conf + PCI_PREF_BASE_UPPER32, 0); 258cd7898f7SMichael S. Tsirkin pci_set_long(conf + PCI_PREF_LIMIT_UPPER32, 0); 2590208def1SIsaku Yamahata } 2600208def1SIsaku Yamahata 26168f79994SIsaku Yamahata /* reset bridge specific configuration registers */ 262cbd2d434SJan Kiszka void pci_bridge_reset(DeviceState *qdev) 263783753fdSIsaku Yamahata { 264cbd2d434SJan Kiszka PCIDevice *dev = PCI_DEVICE(qdev); 26568f79994SIsaku Yamahata uint8_t *conf = dev->config; 266783753fdSIsaku Yamahata 26768f79994SIsaku Yamahata conf[PCI_PRIMARY_BUS] = 0; 26868f79994SIsaku Yamahata conf[PCI_SECONDARY_BUS] = 0; 26968f79994SIsaku Yamahata conf[PCI_SUBORDINATE_BUS] = 0; 27068f79994SIsaku Yamahata conf[PCI_SEC_LATENCY_TIMER] = 0; 27168f79994SIsaku Yamahata 2720208def1SIsaku Yamahata /* 2730208def1SIsaku Yamahata * the default values for base/limit registers aren't specified 2740208def1SIsaku Yamahata * in the PCI-to-PCI-bridge spec. So we don't thouch them here. 2750208def1SIsaku Yamahata * Each implementation can override it. 2760208def1SIsaku Yamahata * typical implementation does 2770208def1SIsaku Yamahata * zero base/limit registers or 2780208def1SIsaku Yamahata * disable forwarding: pci_bridge_disable_base_limit() 2790208def1SIsaku Yamahata * If disable forwarding is wanted, call pci_bridge_disable_base_limit() 2800208def1SIsaku Yamahata * after this function. 2810208def1SIsaku Yamahata */ 2820208def1SIsaku Yamahata pci_byte_test_and_clear_mask(conf + PCI_IO_BASE, 2830208def1SIsaku Yamahata PCI_IO_RANGE_MASK & 0xff); 2840208def1SIsaku Yamahata pci_byte_test_and_clear_mask(conf + PCI_IO_LIMIT, 2850208def1SIsaku Yamahata PCI_IO_RANGE_MASK & 0xff); 2860208def1SIsaku Yamahata pci_word_test_and_clear_mask(conf + PCI_MEMORY_BASE, 2870208def1SIsaku Yamahata PCI_MEMORY_RANGE_MASK & 0xffff); 2880208def1SIsaku Yamahata pci_word_test_and_clear_mask(conf + PCI_MEMORY_LIMIT, 2890208def1SIsaku Yamahata PCI_MEMORY_RANGE_MASK & 0xffff); 2900208def1SIsaku Yamahata pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_BASE, 2910208def1SIsaku Yamahata PCI_PREF_RANGE_MASK & 0xffff); 2920208def1SIsaku Yamahata pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_LIMIT, 2930208def1SIsaku Yamahata PCI_PREF_RANGE_MASK & 0xffff); 294cd7898f7SMichael S. Tsirkin pci_set_long(conf + PCI_PREF_BASE_UPPER32, 0); 295cd7898f7SMichael S. Tsirkin pci_set_long(conf + PCI_PREF_LIMIT_UPPER32, 0); 29668f79994SIsaku Yamahata 29768f79994SIsaku Yamahata pci_set_word(conf + PCI_BRIDGE_CONTROL, 0); 29868f79994SIsaku Yamahata } 29968f79994SIsaku Yamahata 30068f79994SIsaku Yamahata /* default qdev initialization function for PCI-to-PCI bridge */ 30168f79994SIsaku Yamahata int pci_bridge_initfn(PCIDevice *dev) 30268f79994SIsaku Yamahata { 30368f79994SIsaku Yamahata PCIBus *parent = dev->bus; 30468f79994SIsaku Yamahata PCIBridge *br = DO_UPCAST(PCIBridge, dev, dev); 30568f79994SIsaku Yamahata PCIBus *sec_bus = &br->sec_bus; 306783753fdSIsaku Yamahata 30795be1196SMichael S. Tsirkin pci_word_test_and_set_mask(dev->config + PCI_STATUS, 308783753fdSIsaku Yamahata PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); 309783753fdSIsaku Yamahata pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI); 310783753fdSIsaku Yamahata dev->config[PCI_HEADER_TYPE] = 311783753fdSIsaku Yamahata (dev->config[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MULTI_FUNCTION) | 312783753fdSIsaku Yamahata PCI_HEADER_TYPE_BRIDGE; 313783753fdSIsaku Yamahata pci_set_word(dev->config + PCI_SEC_STATUS, 314783753fdSIsaku Yamahata PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); 31568f79994SIsaku Yamahata 3168a3d80faSMichael S. Tsirkin /* 3178a3d80faSMichael S. Tsirkin * If we don't specify the name, the bus will be addressed as <id>.0, where 3188a3d80faSMichael S. Tsirkin * id is the device id. 3198a3d80faSMichael S. Tsirkin * Since PCI Bridge devices have a single bus each, we don't need the index: 3208a3d80faSMichael S. Tsirkin * let users address the bus using the device name. 3218a3d80faSMichael S. Tsirkin */ 3228a3d80faSMichael S. Tsirkin if (!br->bus_name && dev->qdev.id && *dev->qdev.id) { 3238a3d80faSMichael S. Tsirkin br->bus_name = dev->qdev.id; 3248a3d80faSMichael S. Tsirkin } 3258a3d80faSMichael S. Tsirkin 3260d936928SAnthony Liguori qbus_create_inplace(&sec_bus->qbus, TYPE_PCI_BUS, &dev->qdev, 32768f79994SIsaku Yamahata br->bus_name); 32868f79994SIsaku Yamahata sec_bus->parent_dev = dev; 32968f79994SIsaku Yamahata sec_bus->map_irq = br->map_irq; 330336411caSMichael S. Tsirkin sec_bus->address_space_mem = &br->address_space_mem; 33152ce6f05SBlue Swirl memory_region_init(&br->address_space_mem, "pci_bridge_pci", INT64_MAX); 332336411caSMichael S. Tsirkin sec_bus->address_space_io = &br->address_space_io; 333336411caSMichael S. Tsirkin memory_region_init(&br->address_space_io, "pci_bridge_io", 65536); 334b308c82cSAvi Kivity br->windows = pci_bridge_region_init(br); 33568f79994SIsaku Yamahata QLIST_INIT(&sec_bus->child); 33668f79994SIsaku Yamahata QLIST_INSERT_HEAD(&parent->child, sec_bus, sibling); 337783753fdSIsaku Yamahata return 0; 338783753fdSIsaku Yamahata } 339783753fdSIsaku Yamahata 34068f79994SIsaku Yamahata /* default qdev clean up function for PCI-to-PCI bridge */ 341f90c2bcdSAlex Williamson void pci_bridge_exitfn(PCIDevice *pci_dev) 342783753fdSIsaku Yamahata { 343783753fdSIsaku Yamahata PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev); 34451a92333SIsaku Yamahata assert(QLIST_EMPTY(&s->sec_bus.child)); 34551a92333SIsaku Yamahata QLIST_REMOVE(&s->sec_bus, sibling); 346b308c82cSAvi Kivity pci_bridge_region_del(s, s->windows); 347b308c82cSAvi Kivity pci_bridge_region_cleanup(s, s->windows); 348336411caSMichael S. Tsirkin memory_region_destroy(&s->address_space_mem); 349336411caSMichael S. Tsirkin memory_region_destroy(&s->address_space_io); 35068f79994SIsaku Yamahata /* qbus_free() is called automatically by qdev_free() */ 351783753fdSIsaku Yamahata } 352783753fdSIsaku Yamahata 35368f79994SIsaku Yamahata /* 35468f79994SIsaku Yamahata * before qdev initialization(qdev_init()), this function sets bus_name and 35568f79994SIsaku Yamahata * map_irq callback which are necessry for pci_bridge_initfn() to 35668f79994SIsaku Yamahata * initialize bus. 35768f79994SIsaku Yamahata */ 35868f79994SIsaku Yamahata void pci_bridge_map_irq(PCIBridge *br, const char* bus_name, 35968f79994SIsaku Yamahata pci_map_irq_fn map_irq) 360783753fdSIsaku Yamahata { 36168f79994SIsaku Yamahata br->map_irq = map_irq; 36268f79994SIsaku Yamahata br->bus_name = bus_name; 363783753fdSIsaku Yamahata } 364