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 3297d5408fSPeter Maydell #include "qemu/osdep.h" 332dc48da2SPhilippe Mathieu-Daudé #include "qemu/units.h" 34c759b24fSMichael S. Tsirkin #include "hw/pci/pci_bridge.h" 3506aac7bdSMichael S. Tsirkin #include "hw/pci/pci_bus.h" 360b8fa32fSMarkus Armbruster #include "qemu/module.h" 371de7afc9SPaolo Bonzini #include "qemu/range.h" 389a7c2a59SMao Zhongyi #include "qapi/error.h" 39d78644c7SIgor Mammedov #include "hw/acpi/acpi_aml_interface.h" 406c36ec46SIgor Mammedov #include "hw/acpi/pci.h" 414565917bSMichael S. Tsirkin #include "hw/qdev-properties.h" 42783753fdSIsaku Yamahata 43f4c817e0SIsaku Yamahata /* PCI bridge subsystem vendor ID helper functions */ 44f4c817e0SIsaku Yamahata #define PCI_SSVID_SIZEOF 8 45f4c817e0SIsaku Yamahata #define PCI_SSVID_SVID 4 46f4c817e0SIsaku Yamahata #define PCI_SSVID_SSID 6 47f4c817e0SIsaku Yamahata 48f4c817e0SIsaku Yamahata int pci_bridge_ssvid_init(PCIDevice *dev, uint8_t offset, 49f8cd1b02SMao Zhongyi uint16_t svid, uint16_t ssid, 50f8cd1b02SMao Zhongyi Error **errp) 51f4c817e0SIsaku Yamahata { 52f4c817e0SIsaku Yamahata int pos; 539a7c2a59SMao Zhongyi 549a7c2a59SMao Zhongyi pos = pci_add_capability(dev, PCI_CAP_ID_SSVID, offset, 55f8cd1b02SMao Zhongyi PCI_SSVID_SIZEOF, errp); 56f4c817e0SIsaku Yamahata if (pos < 0) { 57f4c817e0SIsaku Yamahata return pos; 58f4c817e0SIsaku Yamahata } 59f4c817e0SIsaku Yamahata 60f4c817e0SIsaku Yamahata pci_set_word(dev->config + pos + PCI_SSVID_SVID, svid); 61f4c817e0SIsaku Yamahata pci_set_word(dev->config + pos + PCI_SSVID_SSID, ssid); 62f4c817e0SIsaku Yamahata return pos; 63f4c817e0SIsaku Yamahata } 64f4c817e0SIsaku Yamahata 6568f79994SIsaku Yamahata /* Accessor function to get parent bridge device from pci bus. */ 66783753fdSIsaku Yamahata PCIDevice *pci_bridge_get_device(PCIBus *bus) 67783753fdSIsaku Yamahata { 68783753fdSIsaku Yamahata return bus->parent_dev; 69783753fdSIsaku Yamahata } 70783753fdSIsaku Yamahata 7168f79994SIsaku Yamahata /* Accessor function to get secondary bus from pci-to-pci bridge device */ 7268f79994SIsaku Yamahata PCIBus *pci_bridge_get_sec_bus(PCIBridge *br) 7368f79994SIsaku Yamahata { 7468f79994SIsaku Yamahata return &br->sec_bus; 7568f79994SIsaku Yamahata } 7668f79994SIsaku Yamahata 7768f79994SIsaku Yamahata static uint32_t pci_config_get_io_base(const PCIDevice *d, 78783753fdSIsaku Yamahata uint32_t base, uint32_t base_upper16) 79783753fdSIsaku Yamahata { 80783753fdSIsaku Yamahata uint32_t val; 81783753fdSIsaku Yamahata 82783753fdSIsaku Yamahata val = ((uint32_t)d->config[base] & PCI_IO_RANGE_MASK) << 8; 83783753fdSIsaku Yamahata if (d->config[base] & PCI_IO_RANGE_TYPE_32) { 84783753fdSIsaku Yamahata val |= (uint32_t)pci_get_word(d->config + base_upper16) << 16; 85783753fdSIsaku Yamahata } 86783753fdSIsaku Yamahata return val; 87783753fdSIsaku Yamahata } 88783753fdSIsaku Yamahata 8968f79994SIsaku Yamahata static pcibus_t pci_config_get_memory_base(const PCIDevice *d, uint32_t base) 90783753fdSIsaku Yamahata { 91783753fdSIsaku Yamahata return ((pcibus_t)pci_get_word(d->config + base) & PCI_MEMORY_RANGE_MASK) 92783753fdSIsaku Yamahata << 16; 93783753fdSIsaku Yamahata } 94783753fdSIsaku Yamahata 9568f79994SIsaku Yamahata static pcibus_t pci_config_get_pref_base(const PCIDevice *d, 96783753fdSIsaku Yamahata uint32_t base, uint32_t upper) 97783753fdSIsaku Yamahata { 98783753fdSIsaku Yamahata pcibus_t tmp; 99783753fdSIsaku Yamahata pcibus_t val; 100783753fdSIsaku Yamahata 101783753fdSIsaku Yamahata tmp = (pcibus_t)pci_get_word(d->config + base); 102783753fdSIsaku Yamahata val = (tmp & PCI_PREF_RANGE_MASK) << 16; 103783753fdSIsaku Yamahata if (tmp & PCI_PREF_RANGE_TYPE_64) { 104783753fdSIsaku Yamahata val |= (pcibus_t)pci_get_long(d->config + upper) << 32; 105783753fdSIsaku Yamahata } 106783753fdSIsaku Yamahata return val; 107783753fdSIsaku Yamahata } 108783753fdSIsaku Yamahata 10968f79994SIsaku Yamahata /* accessor function to get bridge filtering base address */ 11068f79994SIsaku Yamahata pcibus_t pci_bridge_get_base(const PCIDevice *bridge, uint8_t type) 111783753fdSIsaku Yamahata { 112783753fdSIsaku Yamahata pcibus_t base; 113783753fdSIsaku Yamahata if (type & PCI_BASE_ADDRESS_SPACE_IO) { 114783753fdSIsaku Yamahata base = pci_config_get_io_base(bridge, 115783753fdSIsaku Yamahata PCI_IO_BASE, PCI_IO_BASE_UPPER16); 116783753fdSIsaku Yamahata } else { 117783753fdSIsaku Yamahata if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) { 118783753fdSIsaku Yamahata base = pci_config_get_pref_base( 119783753fdSIsaku Yamahata bridge, PCI_PREF_MEMORY_BASE, PCI_PREF_BASE_UPPER32); 120783753fdSIsaku Yamahata } else { 121783753fdSIsaku Yamahata base = pci_config_get_memory_base(bridge, PCI_MEMORY_BASE); 122783753fdSIsaku Yamahata } 123783753fdSIsaku Yamahata } 124783753fdSIsaku Yamahata 125783753fdSIsaku Yamahata return base; 126783753fdSIsaku Yamahata } 127783753fdSIsaku Yamahata 128cb8d4c8fSStefan Weil /* accessor function to get bridge filtering limit */ 12968f79994SIsaku Yamahata pcibus_t pci_bridge_get_limit(const PCIDevice *bridge, uint8_t type) 130783753fdSIsaku Yamahata { 131783753fdSIsaku Yamahata pcibus_t limit; 132783753fdSIsaku Yamahata if (type & PCI_BASE_ADDRESS_SPACE_IO) { 133783753fdSIsaku Yamahata limit = pci_config_get_io_base(bridge, 134783753fdSIsaku Yamahata PCI_IO_LIMIT, PCI_IO_LIMIT_UPPER16); 135783753fdSIsaku Yamahata limit |= 0xfff; /* PCI bridge spec 3.2.5.6. */ 136783753fdSIsaku Yamahata } else { 137783753fdSIsaku Yamahata if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) { 138783753fdSIsaku Yamahata limit = pci_config_get_pref_base( 139783753fdSIsaku Yamahata bridge, PCI_PREF_MEMORY_LIMIT, PCI_PREF_LIMIT_UPPER32); 140783753fdSIsaku Yamahata } else { 141783753fdSIsaku Yamahata limit = pci_config_get_memory_base(bridge, PCI_MEMORY_LIMIT); 142783753fdSIsaku Yamahata } 143783753fdSIsaku Yamahata limit |= 0xfffff; /* PCI bridge spec 3.2.5.{1, 8}. */ 144783753fdSIsaku Yamahata } 145783753fdSIsaku Yamahata return limit; 146783753fdSIsaku Yamahata } 147783753fdSIsaku Yamahata 1487df32ca0SMichael S. Tsirkin static void pci_bridge_init_alias(PCIBridge *bridge, MemoryRegion *alias, 1497df32ca0SMichael S. Tsirkin uint8_t type, const char *name, 1507df32ca0SMichael S. Tsirkin MemoryRegion *space, 1517df32ca0SMichael S. Tsirkin MemoryRegion *parent_space, 1527df32ca0SMichael S. Tsirkin bool enabled) 1537df32ca0SMichael S. Tsirkin { 154f055e96bSAndreas Färber PCIDevice *bridge_dev = PCI_DEVICE(bridge); 155f055e96bSAndreas Färber pcibus_t base = pci_bridge_get_base(bridge_dev, type); 156f055e96bSAndreas Färber pcibus_t limit = pci_bridge_get_limit(bridge_dev, type); 1577df32ca0SMichael S. Tsirkin /* TODO: this doesn't handle base = 0 limit = 2^64 - 1 correctly. 1587df32ca0SMichael S. Tsirkin * Apparently no way to do this with existing memory APIs. */ 1597df32ca0SMichael S. Tsirkin pcibus_t size = enabled && limit >= base ? limit + 1 - base : 0; 1607df32ca0SMichael S. Tsirkin 16140c5dce9SPaolo Bonzini memory_region_init_alias(alias, OBJECT(bridge), name, space, base, size); 1627df32ca0SMichael S. Tsirkin memory_region_add_subregion_overlap(parent_space, base, alias, 1); 1637df32ca0SMichael S. Tsirkin } 1647df32ca0SMichael S. Tsirkin 165ba7d8515SAlex Williamson static void pci_bridge_init_vga_aliases(PCIBridge *br, PCIBus *parent, 166ba7d8515SAlex Williamson MemoryRegion *alias_vga) 167ba7d8515SAlex Williamson { 168f055e96bSAndreas Färber PCIDevice *pd = PCI_DEVICE(br); 169f055e96bSAndreas Färber uint16_t brctl = pci_get_word(pd->config + PCI_BRIDGE_CONTROL); 170ba7d8515SAlex Williamson 17140c5dce9SPaolo Bonzini memory_region_init_alias(&alias_vga[QEMU_PCI_VGA_IO_LO], OBJECT(br), 172ba7d8515SAlex Williamson "pci_bridge_vga_io_lo", &br->address_space_io, 173ba7d8515SAlex Williamson QEMU_PCI_VGA_IO_LO_BASE, QEMU_PCI_VGA_IO_LO_SIZE); 17440c5dce9SPaolo Bonzini memory_region_init_alias(&alias_vga[QEMU_PCI_VGA_IO_HI], OBJECT(br), 175ba7d8515SAlex Williamson "pci_bridge_vga_io_hi", &br->address_space_io, 176ba7d8515SAlex Williamson QEMU_PCI_VGA_IO_HI_BASE, QEMU_PCI_VGA_IO_HI_SIZE); 17740c5dce9SPaolo Bonzini memory_region_init_alias(&alias_vga[QEMU_PCI_VGA_MEM], OBJECT(br), 178ba7d8515SAlex Williamson "pci_bridge_vga_mem", &br->address_space_mem, 179ba7d8515SAlex Williamson QEMU_PCI_VGA_MEM_BASE, QEMU_PCI_VGA_MEM_SIZE); 180ba7d8515SAlex Williamson 181ba7d8515SAlex Williamson if (brctl & PCI_BRIDGE_CTL_VGA) { 182f055e96bSAndreas Färber pci_register_vga(pd, &alias_vga[QEMU_PCI_VGA_MEM], 183ba7d8515SAlex Williamson &alias_vga[QEMU_PCI_VGA_IO_LO], 184ba7d8515SAlex Williamson &alias_vga[QEMU_PCI_VGA_IO_HI]); 185ba7d8515SAlex Williamson } 186ba7d8515SAlex Williamson } 187ba7d8515SAlex Williamson 188b2999ed8SJonathan Cameron static void pci_bridge_region_init(PCIBridge *br) 1897df32ca0SMichael S. Tsirkin { 190f055e96bSAndreas Färber PCIDevice *pd = PCI_DEVICE(br); 191fd56e061SDavid Gibson PCIBus *parent = pci_get_bus(pd); 192b2999ed8SJonathan Cameron PCIBridgeWindows *w = &br->windows; 193f055e96bSAndreas Färber uint16_t cmd = pci_get_word(pd->config + PCI_COMMAND); 1947df32ca0SMichael S. Tsirkin 195b308c82cSAvi Kivity pci_bridge_init_alias(br, &w->alias_pref_mem, 1967df32ca0SMichael S. Tsirkin PCI_BASE_ADDRESS_MEM_PREFETCH, 1977df32ca0SMichael S. Tsirkin "pci_bridge_pref_mem", 198336411caSMichael S. Tsirkin &br->address_space_mem, 1997df32ca0SMichael S. Tsirkin parent->address_space_mem, 2007df32ca0SMichael S. Tsirkin cmd & PCI_COMMAND_MEMORY); 201b308c82cSAvi Kivity pci_bridge_init_alias(br, &w->alias_mem, 2027df32ca0SMichael S. Tsirkin PCI_BASE_ADDRESS_SPACE_MEMORY, 2037df32ca0SMichael S. Tsirkin "pci_bridge_mem", 204336411caSMichael S. Tsirkin &br->address_space_mem, 2057df32ca0SMichael S. Tsirkin parent->address_space_mem, 2067df32ca0SMichael S. Tsirkin cmd & PCI_COMMAND_MEMORY); 207b308c82cSAvi Kivity pci_bridge_init_alias(br, &w->alias_io, 2087df32ca0SMichael S. Tsirkin PCI_BASE_ADDRESS_SPACE_IO, 2097df32ca0SMichael S. Tsirkin "pci_bridge_io", 210336411caSMichael S. Tsirkin &br->address_space_io, 2117df32ca0SMichael S. Tsirkin parent->address_space_io, 2127df32ca0SMichael S. Tsirkin cmd & PCI_COMMAND_IO); 213ba7d8515SAlex Williamson 214ba7d8515SAlex Williamson pci_bridge_init_vga_aliases(br, parent, w->alias_vga); 2157df32ca0SMichael S. Tsirkin } 2167df32ca0SMichael S. Tsirkin 217b308c82cSAvi Kivity static void pci_bridge_region_del(PCIBridge *br, PCIBridgeWindows *w) 2187df32ca0SMichael S. Tsirkin { 219f055e96bSAndreas Färber PCIDevice *pd = PCI_DEVICE(br); 220fd56e061SDavid Gibson PCIBus *parent = pci_get_bus(pd); 221b308c82cSAvi Kivity 222b308c82cSAvi Kivity memory_region_del_subregion(parent->address_space_io, &w->alias_io); 223b308c82cSAvi Kivity memory_region_del_subregion(parent->address_space_mem, &w->alias_mem); 224b308c82cSAvi Kivity memory_region_del_subregion(parent->address_space_mem, &w->alias_pref_mem); 225f055e96bSAndreas Färber pci_unregister_vga(pd); 226b308c82cSAvi Kivity } 227b308c82cSAvi Kivity 228b308c82cSAvi Kivity static void pci_bridge_region_cleanup(PCIBridge *br, PCIBridgeWindows *w) 229b308c82cSAvi Kivity { 2309f6b2f1cSPaolo Bonzini object_unparent(OBJECT(&w->alias_io)); 2319f6b2f1cSPaolo Bonzini object_unparent(OBJECT(&w->alias_mem)); 2329f6b2f1cSPaolo Bonzini object_unparent(OBJECT(&w->alias_pref_mem)); 2339f6b2f1cSPaolo Bonzini object_unparent(OBJECT(&w->alias_vga[QEMU_PCI_VGA_IO_LO])); 2349f6b2f1cSPaolo Bonzini object_unparent(OBJECT(&w->alias_vga[QEMU_PCI_VGA_IO_HI])); 2359f6b2f1cSPaolo Bonzini object_unparent(OBJECT(&w->alias_vga[QEMU_PCI_VGA_MEM])); 2367df32ca0SMichael S. Tsirkin } 2377df32ca0SMichael S. Tsirkin 238e78e9ae4SDon Koch void pci_bridge_update_mappings(PCIBridge *br) 2397df32ca0SMichael S. Tsirkin { 240b2999ed8SJonathan Cameron PCIBridgeWindows *w = &br->windows; 241b308c82cSAvi Kivity 2427df32ca0SMichael S. Tsirkin /* Make updates atomic to: handle the case of one VCPU updating the bridge 2437df32ca0SMichael S. Tsirkin * while another accesses an unaffected region. */ 2447df32ca0SMichael S. Tsirkin memory_region_transaction_begin(); 245b2999ed8SJonathan Cameron pci_bridge_region_del(br, w); 246e7176cdbSMatthias Weckbecker pci_bridge_region_cleanup(br, w); 247b2999ed8SJonathan Cameron pci_bridge_region_init(br); 2487df32ca0SMichael S. Tsirkin memory_region_transaction_commit(); 2497df32ca0SMichael S. Tsirkin } 2507df32ca0SMichael S. Tsirkin 25168f79994SIsaku Yamahata /* default write_config function for PCI-to-PCI bridge */ 25268f79994SIsaku Yamahata void pci_bridge_write_config(PCIDevice *d, 253783753fdSIsaku Yamahata uint32_t address, uint32_t val, int len) 254783753fdSIsaku Yamahata { 255f055e96bSAndreas Färber PCIBridge *s = PCI_BRIDGE(d); 256a5fce077SIsaku Yamahata uint16_t oldctl = pci_get_word(d->config + PCI_BRIDGE_CONTROL); 257a5fce077SIsaku Yamahata uint16_t newctl; 258a5fce077SIsaku Yamahata 259783753fdSIsaku Yamahata pci_default_write_config(d, address, val, len); 260783753fdSIsaku Yamahata 2617df32ca0SMichael S. Tsirkin if (ranges_overlap(address, len, PCI_COMMAND, 2) || 2627df32ca0SMichael S. Tsirkin 2637df32ca0SMichael S. Tsirkin /* io base/limit */ 264783753fdSIsaku Yamahata ranges_overlap(address, len, PCI_IO_BASE, 2) || 265783753fdSIsaku Yamahata 266783753fdSIsaku Yamahata /* memory base/limit, prefetchable base/limit and 267783753fdSIsaku Yamahata io base/limit upper 16 */ 268ba7d8515SAlex Williamson ranges_overlap(address, len, PCI_MEMORY_BASE, 20) || 269ba7d8515SAlex Williamson 270ba7d8515SAlex Williamson /* vga enable */ 271ba7d8515SAlex Williamson ranges_overlap(address, len, PCI_BRIDGE_CONTROL, 2)) { 2727df32ca0SMichael S. Tsirkin pci_bridge_update_mappings(s); 273783753fdSIsaku Yamahata } 274a5fce077SIsaku Yamahata 275a5fce077SIsaku Yamahata newctl = pci_get_word(d->config + PCI_BRIDGE_CONTROL); 276a5fce077SIsaku Yamahata if (~oldctl & newctl & PCI_BRIDGE_CTL_BUS_RESET) { 277a5fce077SIsaku Yamahata /* Trigger hot reset on 0->1 transition. */ 27878e4d5cbSPeter Maydell bus_cold_reset(BUS(&s->sec_bus)); 279a5fce077SIsaku Yamahata } 280783753fdSIsaku Yamahata } 281783753fdSIsaku Yamahata 2820208def1SIsaku Yamahata void pci_bridge_disable_base_limit(PCIDevice *dev) 2830208def1SIsaku Yamahata { 2840208def1SIsaku Yamahata uint8_t *conf = dev->config; 2850208def1SIsaku Yamahata 2860208def1SIsaku Yamahata pci_byte_test_and_set_mask(conf + PCI_IO_BASE, 2870208def1SIsaku Yamahata PCI_IO_RANGE_MASK & 0xff); 2880208def1SIsaku Yamahata pci_byte_test_and_clear_mask(conf + PCI_IO_LIMIT, 2890208def1SIsaku Yamahata PCI_IO_RANGE_MASK & 0xff); 2900208def1SIsaku Yamahata pci_word_test_and_set_mask(conf + PCI_MEMORY_BASE, 2910208def1SIsaku Yamahata PCI_MEMORY_RANGE_MASK & 0xffff); 2920208def1SIsaku Yamahata pci_word_test_and_clear_mask(conf + PCI_MEMORY_LIMIT, 2930208def1SIsaku Yamahata PCI_MEMORY_RANGE_MASK & 0xffff); 2940208def1SIsaku Yamahata pci_word_test_and_set_mask(conf + PCI_PREF_MEMORY_BASE, 2950208def1SIsaku Yamahata PCI_PREF_RANGE_MASK & 0xffff); 2960208def1SIsaku Yamahata pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_LIMIT, 2970208def1SIsaku Yamahata PCI_PREF_RANGE_MASK & 0xffff); 298cd7898f7SMichael S. Tsirkin pci_set_long(conf + PCI_PREF_BASE_UPPER32, 0); 299cd7898f7SMichael S. Tsirkin pci_set_long(conf + PCI_PREF_LIMIT_UPPER32, 0); 3000208def1SIsaku Yamahata } 3010208def1SIsaku Yamahata 30268f79994SIsaku Yamahata /* reset bridge specific configuration registers */ 303cbd2d434SJan Kiszka void pci_bridge_reset(DeviceState *qdev) 304783753fdSIsaku Yamahata { 305cbd2d434SJan Kiszka PCIDevice *dev = PCI_DEVICE(qdev); 30668f79994SIsaku Yamahata uint8_t *conf = dev->config; 307783753fdSIsaku Yamahata 30868f79994SIsaku Yamahata conf[PCI_PRIMARY_BUS] = 0; 30968f79994SIsaku Yamahata conf[PCI_SECONDARY_BUS] = 0; 31068f79994SIsaku Yamahata conf[PCI_SUBORDINATE_BUS] = 0; 31168f79994SIsaku Yamahata conf[PCI_SEC_LATENCY_TIMER] = 0; 31268f79994SIsaku Yamahata 3130208def1SIsaku Yamahata /* 3140208def1SIsaku Yamahata * the default values for base/limit registers aren't specified 3155892cfc7SMao Zhongyi * in the PCI-to-PCI-bridge spec. So we don't touch them here. 3160208def1SIsaku Yamahata * Each implementation can override it. 3170208def1SIsaku Yamahata * typical implementation does 3180208def1SIsaku Yamahata * zero base/limit registers or 3190208def1SIsaku Yamahata * disable forwarding: pci_bridge_disable_base_limit() 3200208def1SIsaku Yamahata * If disable forwarding is wanted, call pci_bridge_disable_base_limit() 3210208def1SIsaku Yamahata * after this function. 3220208def1SIsaku Yamahata */ 3230208def1SIsaku Yamahata pci_byte_test_and_clear_mask(conf + PCI_IO_BASE, 3240208def1SIsaku Yamahata PCI_IO_RANGE_MASK & 0xff); 3250208def1SIsaku Yamahata pci_byte_test_and_clear_mask(conf + PCI_IO_LIMIT, 3260208def1SIsaku Yamahata PCI_IO_RANGE_MASK & 0xff); 3270208def1SIsaku Yamahata pci_word_test_and_clear_mask(conf + PCI_MEMORY_BASE, 3280208def1SIsaku Yamahata PCI_MEMORY_RANGE_MASK & 0xffff); 3290208def1SIsaku Yamahata pci_word_test_and_clear_mask(conf + PCI_MEMORY_LIMIT, 3300208def1SIsaku Yamahata PCI_MEMORY_RANGE_MASK & 0xffff); 3310208def1SIsaku Yamahata pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_BASE, 3320208def1SIsaku Yamahata PCI_PREF_RANGE_MASK & 0xffff); 3330208def1SIsaku Yamahata pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_LIMIT, 3340208def1SIsaku Yamahata PCI_PREF_RANGE_MASK & 0xffff); 335cd7898f7SMichael S. Tsirkin pci_set_long(conf + PCI_PREF_BASE_UPPER32, 0); 336cd7898f7SMichael S. Tsirkin pci_set_long(conf + PCI_PREF_LIMIT_UPPER32, 0); 33768f79994SIsaku Yamahata 33868f79994SIsaku Yamahata pci_set_word(conf + PCI_BRIDGE_CONTROL, 0); 33968f79994SIsaku Yamahata } 34068f79994SIsaku Yamahata 34168f79994SIsaku Yamahata /* default qdev initialization function for PCI-to-PCI bridge */ 3429cfaa007SCao jin void pci_bridge_initfn(PCIDevice *dev, const char *typename) 34368f79994SIsaku Yamahata { 344fd56e061SDavid Gibson PCIBus *parent = pci_get_bus(dev); 345f055e96bSAndreas Färber PCIBridge *br = PCI_BRIDGE(dev); 34668f79994SIsaku Yamahata PCIBus *sec_bus = &br->sec_bus; 347783753fdSIsaku Yamahata 34895be1196SMichael S. Tsirkin pci_word_test_and_set_mask(dev->config + PCI_STATUS, 349783753fdSIsaku Yamahata PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); 350ba7d8515SAlex Williamson 351ba7d8515SAlex Williamson /* 352ba7d8515SAlex Williamson * TODO: We implement VGA Enable in the Bridge Control Register 353ba7d8515SAlex Williamson * therefore per the PCI to PCI bridge spec we must also implement 354ba7d8515SAlex Williamson * VGA Palette Snooping. When done, set this bit writable: 355ba7d8515SAlex Williamson * 356ba7d8515SAlex Williamson * pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND, 357ba7d8515SAlex Williamson * PCI_COMMAND_VGA_PALETTE); 358ba7d8515SAlex Williamson */ 359ba7d8515SAlex Williamson 360783753fdSIsaku Yamahata pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI); 361783753fdSIsaku Yamahata dev->config[PCI_HEADER_TYPE] = 362783753fdSIsaku Yamahata (dev->config[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MULTI_FUNCTION) | 363783753fdSIsaku Yamahata PCI_HEADER_TYPE_BRIDGE; 364783753fdSIsaku Yamahata pci_set_word(dev->config + PCI_SEC_STATUS, 365783753fdSIsaku Yamahata PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); 36668f79994SIsaku Yamahata 3678a3d80faSMichael S. Tsirkin /* 3688a3d80faSMichael S. Tsirkin * If we don't specify the name, the bus will be addressed as <id>.0, where 3698a3d80faSMichael S. Tsirkin * id is the device id. 3708a3d80faSMichael S. Tsirkin * Since PCI Bridge devices have a single bus each, we don't need the index: 3718a3d80faSMichael S. Tsirkin * let users address the bus using the device name. 3728a3d80faSMichael S. Tsirkin */ 3738a3d80faSMichael S. Tsirkin if (!br->bus_name && dev->qdev.id && *dev->qdev.id) { 3748a3d80faSMichael S. Tsirkin br->bus_name = dev->qdev.id; 3758a3d80faSMichael S. Tsirkin } 3768a3d80faSMichael S. Tsirkin 377d637e1dcSPeter Maydell qbus_init(sec_bus, sizeof(br->sec_bus), typename, DEVICE(dev), 378fb17dfe0SAndreas Färber br->bus_name); 37968f79994SIsaku Yamahata sec_bus->parent_dev = dev; 380659fefeeSAlex Williamson sec_bus->map_irq = br->map_irq ? br->map_irq : pci_swizzle_map_irq_fn; 381336411caSMichael S. Tsirkin sec_bus->address_space_mem = &br->address_space_mem; 382cf252e51SMichael S. Tsirkin memory_region_init(&br->address_space_mem, OBJECT(br), "pci_bridge_pci", UINT64_MAX); 383*55fa4be6SGao Shiyuan address_space_init(&br->as_mem, &br->address_space_mem, 384*55fa4be6SGao Shiyuan "pci_bridge_pci_mem"); 385336411caSMichael S. Tsirkin sec_bus->address_space_io = &br->address_space_io; 3869cd1e97aSMark Cave-Ayland memory_region_init(&br->address_space_io, OBJECT(br), "pci_bridge_io", 3872dc48da2SPhilippe Mathieu-Daudé 4 * GiB); 388*55fa4be6SGao Shiyuan address_space_init(&br->as_io, &br->address_space_io, "pci_bridge_pci_io"); 389b2999ed8SJonathan Cameron pci_bridge_region_init(br); 39068f79994SIsaku Yamahata QLIST_INIT(&sec_bus->child); 39168f79994SIsaku Yamahata QLIST_INSERT_HEAD(&parent->child, sec_bus, sibling); 3924565917bSMichael S. Tsirkin 3934565917bSMichael S. Tsirkin /* For express secondary buses, secondary latency timer is RO 0 */ 3944565917bSMichael S. Tsirkin if (pci_bus_is_express(sec_bus) && !br->pcie_writeable_slt_bug) { 3954565917bSMichael S. Tsirkin dev->wmask[PCI_SEC_LATENCY_TIMER] = 0; 3964565917bSMichael S. Tsirkin } 397783753fdSIsaku Yamahata } 398783753fdSIsaku Yamahata 39968f79994SIsaku Yamahata /* default qdev clean up function for PCI-to-PCI bridge */ 400f90c2bcdSAlex Williamson void pci_bridge_exitfn(PCIDevice *pci_dev) 401783753fdSIsaku Yamahata { 402f055e96bSAndreas Färber PCIBridge *s = PCI_BRIDGE(pci_dev); 40351a92333SIsaku Yamahata assert(QLIST_EMPTY(&s->sec_bus.child)); 40451a92333SIsaku Yamahata QLIST_REMOVE(&s->sec_bus, sibling); 405*55fa4be6SGao Shiyuan address_space_destroy(&s->as_mem); 406*55fa4be6SGao Shiyuan address_space_destroy(&s->as_io); 407b2999ed8SJonathan Cameron pci_bridge_region_del(s, &s->windows); 408b2999ed8SJonathan Cameron pci_bridge_region_cleanup(s, &s->windows); 4096780a22cSStefan Hajnoczi /* object_unparent() is called automatically during device deletion */ 410783753fdSIsaku Yamahata } 411783753fdSIsaku Yamahata 41268f79994SIsaku Yamahata /* 41368f79994SIsaku Yamahata * before qdev initialization(qdev_init()), this function sets bus_name and 414d05eec73SMao Zhongyi * map_irq callback which are necessary for pci_bridge_initfn() to 41568f79994SIsaku Yamahata * initialize bus. 41668f79994SIsaku Yamahata */ 41768f79994SIsaku Yamahata void pci_bridge_map_irq(PCIBridge *br, const char* bus_name, 41868f79994SIsaku Yamahata pci_map_irq_fn map_irq) 419783753fdSIsaku Yamahata { 42068f79994SIsaku Yamahata br->map_irq = map_irq; 42168f79994SIsaku Yamahata br->bus_name = bus_name; 422783753fdSIsaku Yamahata } 423f055e96bSAndreas Färber 42470e1ee59SAleksandr Bezzubikov 42570e1ee59SAleksandr Bezzubikov int pci_bridge_qemu_reserve_cap_init(PCIDevice *dev, int cap_offset, 4269e899399SJing Liu PCIResReserve res_reserve, Error **errp) 42770e1ee59SAleksandr Bezzubikov { 4289e899399SJing Liu if (res_reserve.mem_pref_32 != (uint64_t)-1 && 4299e899399SJing Liu res_reserve.mem_pref_64 != (uint64_t)-1) { 43070e1ee59SAleksandr Bezzubikov error_setg(errp, 43170e1ee59SAleksandr Bezzubikov "PCI resource reserve cap: PREF32 and PREF64 conflict"); 43270e1ee59SAleksandr Bezzubikov return -EINVAL; 43370e1ee59SAleksandr Bezzubikov } 43470e1ee59SAleksandr Bezzubikov 4359e899399SJing Liu if (res_reserve.mem_non_pref != (uint64_t)-1 && 43637e7211cSPhilippe Mathieu-Daudé res_reserve.mem_non_pref >= 4 * GiB) { 437fc67208fSMarcel Apfelbaum error_setg(errp, 438fc67208fSMarcel Apfelbaum "PCI resource reserve cap: mem-reserve must be less than 4G"); 439fc67208fSMarcel Apfelbaum return -EINVAL; 440fc67208fSMarcel Apfelbaum } 441fc67208fSMarcel Apfelbaum 4429e899399SJing Liu if (res_reserve.mem_pref_32 != (uint64_t)-1 && 44337e7211cSPhilippe Mathieu-Daudé res_reserve.mem_pref_32 >= 4 * GiB) { 444fc67208fSMarcel Apfelbaum error_setg(errp, 445fc67208fSMarcel Apfelbaum "PCI resource reserve cap: pref32-reserve must be less than 4G"); 446fc67208fSMarcel Apfelbaum return -EINVAL; 447fc67208fSMarcel Apfelbaum } 448fc67208fSMarcel Apfelbaum 4499e899399SJing Liu if (res_reserve.bus == (uint32_t)-1 && 4509e899399SJing Liu res_reserve.io == (uint64_t)-1 && 4519e899399SJing Liu res_reserve.mem_non_pref == (uint64_t)-1 && 4529e899399SJing Liu res_reserve.mem_pref_32 == (uint64_t)-1 && 4539e899399SJing Liu res_reserve.mem_pref_64 == (uint64_t)-1) { 45470e1ee59SAleksandr Bezzubikov return 0; 45570e1ee59SAleksandr Bezzubikov } 45670e1ee59SAleksandr Bezzubikov 45770e1ee59SAleksandr Bezzubikov size_t cap_len = sizeof(PCIBridgeQemuCap); 45870e1ee59SAleksandr Bezzubikov PCIBridgeQemuCap cap = { 45970e1ee59SAleksandr Bezzubikov .len = cap_len, 46070e1ee59SAleksandr Bezzubikov .type = REDHAT_PCI_CAP_RESOURCE_RESERVE, 4610e464f7dSMichael S. Tsirkin .bus_res = cpu_to_le32(res_reserve.bus), 4620e464f7dSMichael S. Tsirkin .io = cpu_to_le64(res_reserve.io), 4630e464f7dSMichael S. Tsirkin .mem = cpu_to_le32(res_reserve.mem_non_pref), 4640e464f7dSMichael S. Tsirkin .mem_pref_32 = cpu_to_le32(res_reserve.mem_pref_32), 4650e464f7dSMichael S. Tsirkin .mem_pref_64 = cpu_to_le64(res_reserve.mem_pref_64) 46670e1ee59SAleksandr Bezzubikov }; 46770e1ee59SAleksandr Bezzubikov 46870e1ee59SAleksandr Bezzubikov int offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 46970e1ee59SAleksandr Bezzubikov cap_offset, cap_len, errp); 47070e1ee59SAleksandr Bezzubikov if (offset < 0) { 47170e1ee59SAleksandr Bezzubikov return offset; 47270e1ee59SAleksandr Bezzubikov } 47370e1ee59SAleksandr Bezzubikov 47470e1ee59SAleksandr Bezzubikov memcpy(dev->config + offset + PCI_CAP_FLAGS, 47570e1ee59SAleksandr Bezzubikov (char *)&cap + PCI_CAP_FLAGS, 47670e1ee59SAleksandr Bezzubikov cap_len - PCI_CAP_FLAGS); 47770e1ee59SAleksandr Bezzubikov return 0; 47870e1ee59SAleksandr Bezzubikov } 47970e1ee59SAleksandr Bezzubikov 4804565917bSMichael S. Tsirkin static Property pci_bridge_properties[] = { 4814565917bSMichael S. Tsirkin DEFINE_PROP_BOOL("x-pci-express-writeable-slt-bug", PCIBridge, 4824565917bSMichael S. Tsirkin pcie_writeable_slt_bug, false), 4834565917bSMichael S. Tsirkin DEFINE_PROP_END_OF_LIST(), 4844565917bSMichael S. Tsirkin }; 4854565917bSMichael S. Tsirkin 4866c36ec46SIgor Mammedov static void pci_bridge_class_init(ObjectClass *klass, void *data) 4876c36ec46SIgor Mammedov { 4886c36ec46SIgor Mammedov AcpiDevAmlIfClass *adevc = ACPI_DEV_AML_IF_CLASS(klass); 4894565917bSMichael S. Tsirkin DeviceClass *k = DEVICE_CLASS(klass); 4906c36ec46SIgor Mammedov 4914565917bSMichael S. Tsirkin device_class_set_props(k, pci_bridge_properties); 4926c36ec46SIgor Mammedov adevc->build_dev_aml = build_pci_bridge_aml; 4936c36ec46SIgor Mammedov } 4946c36ec46SIgor Mammedov 495f055e96bSAndreas Färber static const TypeInfo pci_bridge_type_info = { 496f055e96bSAndreas Färber .name = TYPE_PCI_BRIDGE, 497f055e96bSAndreas Färber .parent = TYPE_PCI_DEVICE, 498f055e96bSAndreas Färber .instance_size = sizeof(PCIBridge), 4996c36ec46SIgor Mammedov .class_init = pci_bridge_class_init, 500f055e96bSAndreas Färber .abstract = true, 501d78644c7SIgor Mammedov .interfaces = (InterfaceInfo[]) { 502d78644c7SIgor Mammedov { TYPE_ACPI_DEV_AML_IF }, 503d78644c7SIgor Mammedov { }, 504d78644c7SIgor Mammedov }, 505f055e96bSAndreas Färber }; 506f055e96bSAndreas Färber 507f055e96bSAndreas Färber static void pci_bridge_register_types(void) 508f055e96bSAndreas Färber { 509f055e96bSAndreas Färber type_register_static(&pci_bridge_type_info); 510f055e96bSAndreas Färber } 511f055e96bSAndreas Färber 512f055e96bSAndreas Färber type_init(pci_bridge_register_types) 513