1f8091a88SPaul Burton /* 2f8091a88SPaul Burton * This program is free software; you can redistribute it and/or modify it 3f8091a88SPaul Burton * under the terms of the GNU General Public License as published by the 4f8091a88SPaul Burton * Free Software Foundation; either version 2 of the License, or (at your 5f8091a88SPaul Burton * option) any later version. 6f8091a88SPaul Burton * 7f8091a88SPaul Burton * Copyright (C) 2003, 04, 11 Ralf Baechle (ralf@linux-mips.org) 8f8091a88SPaul Burton * Copyright (C) 2011 Wind River Systems, 9f8091a88SPaul Burton * written by Ralf Baechle (ralf@linux-mips.org) 10f8091a88SPaul Burton */ 11f8091a88SPaul Burton #include <linux/bug.h> 12f8091a88SPaul Burton #include <linux/kernel.h> 13f8091a88SPaul Burton #include <linux/mm.h> 14f8091a88SPaul Burton #include <linux/bootmem.h> 15f8091a88SPaul Burton #include <linux/export.h> 16f8091a88SPaul Burton #include <linux/init.h> 17f8091a88SPaul Burton #include <linux/types.h> 18f8091a88SPaul Burton #include <linux/pci.h> 19f8091a88SPaul Burton #include <linux/of_address.h> 20f8091a88SPaul Burton 21f8091a88SPaul Burton #include <asm/cpu-info.h> 22f8091a88SPaul Burton 23f8091a88SPaul Burton /* 24f8091a88SPaul Burton * If PCI_PROBE_ONLY in pci_flags is set, we don't change any PCI resource 25f8091a88SPaul Burton * assignments. 26f8091a88SPaul Burton */ 27f8091a88SPaul Burton 28f8091a88SPaul Burton /* 29f8091a88SPaul Burton * The PCI controller list. 30f8091a88SPaul Burton */ 31f8091a88SPaul Burton static LIST_HEAD(controllers); 32f8091a88SPaul Burton 33f8091a88SPaul Burton static int pci_initialized; 34f8091a88SPaul Burton 35f8091a88SPaul Burton /* 36f8091a88SPaul Burton * We need to avoid collisions with `mirrored' VGA ports 37f8091a88SPaul Burton * and other strange ISA hardware, so we always want the 38f8091a88SPaul Burton * addresses to be allocated in the 0x000-0x0ff region 39f8091a88SPaul Burton * modulo 0x400. 40f8091a88SPaul Burton * 41f8091a88SPaul Burton * Why? Because some silly external IO cards only decode 42f8091a88SPaul Burton * the low 10 bits of the IO address. The 0x00-0xff region 43f8091a88SPaul Burton * is reserved for motherboard devices that decode all 16 44f8091a88SPaul Burton * bits, so it's ok to allocate at, say, 0x2800-0x28ff, 45f8091a88SPaul Burton * but we want to try to avoid allocating at 0x2900-0x2bff 46f8091a88SPaul Burton * which might have be mirrored at 0x0100-0x03ff.. 47f8091a88SPaul Burton */ 48f8091a88SPaul Burton resource_size_t 49f8091a88SPaul Burton pcibios_align_resource(void *data, const struct resource *res, 50f8091a88SPaul Burton resource_size_t size, resource_size_t align) 51f8091a88SPaul Burton { 52f8091a88SPaul Burton struct pci_dev *dev = data; 53f8091a88SPaul Burton struct pci_controller *hose = dev->sysdata; 54f8091a88SPaul Burton resource_size_t start = res->start; 55f8091a88SPaul Burton 56f8091a88SPaul Burton if (res->flags & IORESOURCE_IO) { 57f8091a88SPaul Burton /* Make sure we start at our min on all hoses */ 58f8091a88SPaul Burton if (start < PCIBIOS_MIN_IO + hose->io_resource->start) 59f8091a88SPaul Burton start = PCIBIOS_MIN_IO + hose->io_resource->start; 60f8091a88SPaul Burton 61f8091a88SPaul Burton /* 62f8091a88SPaul Burton * Put everything into 0x00-0xff region modulo 0x400 63f8091a88SPaul Burton */ 64f8091a88SPaul Burton if (start & 0x300) 65f8091a88SPaul Burton start = (start + 0x3ff) & ~0x3ff; 66f8091a88SPaul Burton } else if (res->flags & IORESOURCE_MEM) { 67f8091a88SPaul Burton /* Make sure we start at our min on all hoses */ 68f8091a88SPaul Burton if (start < PCIBIOS_MIN_MEM + hose->mem_resource->start) 69f8091a88SPaul Burton start = PCIBIOS_MIN_MEM + hose->mem_resource->start; 70f8091a88SPaul Burton } 71f8091a88SPaul Burton 72f8091a88SPaul Burton return start; 73f8091a88SPaul Burton } 74f8091a88SPaul Burton 75f8091a88SPaul Burton static void pcibios_scanbus(struct pci_controller *hose) 76f8091a88SPaul Burton { 77f8091a88SPaul Burton static int next_busno; 78f8091a88SPaul Burton static int need_domain_info; 79f8091a88SPaul Burton LIST_HEAD(resources); 80f8091a88SPaul Burton struct pci_bus *bus; 8104c81c72SLorenzo Pieralisi struct pci_host_bridge *bridge; 8204c81c72SLorenzo Pieralisi int ret; 8304c81c72SLorenzo Pieralisi 8404c81c72SLorenzo Pieralisi bridge = pci_alloc_host_bridge(0); 8504c81c72SLorenzo Pieralisi if (!bridge) 8604c81c72SLorenzo Pieralisi return; 87f8091a88SPaul Burton 88f8091a88SPaul Burton if (hose->get_busno && pci_has_flag(PCI_PROBE_ONLY)) 89f8091a88SPaul Burton next_busno = (*hose->get_busno)(); 90f8091a88SPaul Burton 91f8091a88SPaul Burton pci_add_resource_offset(&resources, 92f8091a88SPaul Burton hose->mem_resource, hose->mem_offset); 93f8091a88SPaul Burton pci_add_resource_offset(&resources, 94f8091a88SPaul Burton hose->io_resource, hose->io_offset); 957ee214b5SBjorn Helgaas pci_add_resource(&resources, hose->busn_resource); 9604c81c72SLorenzo Pieralisi list_splice_init(&resources, &bridge->windows); 9704c81c72SLorenzo Pieralisi bridge->dev.parent = NULL; 9804c81c72SLorenzo Pieralisi bridge->sysdata = hose; 9904c81c72SLorenzo Pieralisi bridge->busnr = next_busno; 10004c81c72SLorenzo Pieralisi bridge->ops = hose->pci_ops; 10104c81c72SLorenzo Pieralisi bridge->swizzle_irq = pci_common_swizzle; 10204c81c72SLorenzo Pieralisi bridge->map_irq = pcibios_map_irq; 10304c81c72SLorenzo Pieralisi ret = pci_scan_root_bus_bridge(bridge); 10404c81c72SLorenzo Pieralisi if (ret) { 10504c81c72SLorenzo Pieralisi pci_free_host_bridge(bridge); 106f8091a88SPaul Burton return; 107f8091a88SPaul Burton } 108f8091a88SPaul Burton 10904c81c72SLorenzo Pieralisi hose->bus = bus = bridge->bus; 110902d886dSLorenzo Pieralisi 111902d886dSLorenzo Pieralisi need_domain_info = need_domain_info || pci_domain_nr(bus); 112902d886dSLorenzo Pieralisi set_pci_need_domain_info(hose, need_domain_info); 113902d886dSLorenzo Pieralisi 114f8091a88SPaul Burton next_busno = bus->busn_res.end + 1; 115f8091a88SPaul Burton /* Don't allow 8-bit bus number overflow inside the hose - 116f8091a88SPaul Burton reserve some space for bridges. */ 117f8091a88SPaul Burton if (next_busno > 224) { 118f8091a88SPaul Burton next_busno = 0; 119f8091a88SPaul Burton need_domain_info = 1; 120f8091a88SPaul Burton } 121f8091a88SPaul Burton 122f8091a88SPaul Burton /* 123f8091a88SPaul Burton * We insert PCI resources into the iomem_resource and 124f8091a88SPaul Burton * ioport_resource trees in either pci_bus_claim_resources() 125f8091a88SPaul Burton * or pci_bus_assign_resources(). 126f8091a88SPaul Burton */ 127f8091a88SPaul Burton if (pci_has_flag(PCI_PROBE_ONLY)) { 128f8091a88SPaul Burton pci_bus_claim_resources(bus); 129f8091a88SPaul Burton } else { 130*2794f688SHuacai Chen struct pci_bus *child; 131*2794f688SHuacai Chen 132f8091a88SPaul Burton pci_bus_size_bridges(bus); 133f8091a88SPaul Burton pci_bus_assign_resources(bus); 134*2794f688SHuacai Chen list_for_each_entry(child, &bus->children, node) 135*2794f688SHuacai Chen pcie_bus_configure_settings(child); 136f8091a88SPaul Burton } 137f8091a88SPaul Burton pci_bus_add_devices(bus); 138f8091a88SPaul Burton } 139f8091a88SPaul Burton 140f8091a88SPaul Burton #ifdef CONFIG_OF 141f8091a88SPaul Burton void pci_load_of_ranges(struct pci_controller *hose, struct device_node *node) 142f8091a88SPaul Burton { 143f8091a88SPaul Burton struct of_pci_range range; 144f8091a88SPaul Burton struct of_pci_range_parser parser; 145f8091a88SPaul Burton 1467f27b5b8SRob Herring pr_info("PCI host bridge %pOF ranges:\n", node); 147f8091a88SPaul Burton hose->of_node = node; 148f8091a88SPaul Burton 149f8091a88SPaul Burton if (of_pci_range_parser_init(&parser, node)) 150f8091a88SPaul Burton return; 151f8091a88SPaul Burton 152f8091a88SPaul Burton for_each_of_pci_range(&parser, &range) { 153f8091a88SPaul Burton struct resource *res = NULL; 154f8091a88SPaul Burton 155f8091a88SPaul Burton switch (range.flags & IORESOURCE_TYPE_BITS) { 156f8091a88SPaul Burton case IORESOURCE_IO: 157f8091a88SPaul Burton pr_info(" IO 0x%016llx..0x%016llx\n", 158f8091a88SPaul Burton range.cpu_addr, 159f8091a88SPaul Burton range.cpu_addr + range.size - 1); 160f8091a88SPaul Burton hose->io_map_base = 161f8091a88SPaul Burton (unsigned long)ioremap(range.cpu_addr, 162f8091a88SPaul Burton range.size); 163f8091a88SPaul Burton res = hose->io_resource; 164f8091a88SPaul Burton break; 165f8091a88SPaul Burton case IORESOURCE_MEM: 166f8091a88SPaul Burton pr_info(" MEM 0x%016llx..0x%016llx\n", 167f8091a88SPaul Burton range.cpu_addr, 168f8091a88SPaul Burton range.cpu_addr + range.size - 1); 169f8091a88SPaul Burton res = hose->mem_resource; 170f8091a88SPaul Burton break; 171f8091a88SPaul Burton } 172f8091a88SPaul Burton if (res != NULL) 173f8091a88SPaul Burton of_pci_range_to_resource(&range, node, res); 174f8091a88SPaul Burton } 175f8091a88SPaul Burton } 176f8091a88SPaul Burton 177f8091a88SPaul Burton struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus) 178f8091a88SPaul Burton { 179f8091a88SPaul Burton struct pci_controller *hose = bus->sysdata; 180f8091a88SPaul Burton 181f8091a88SPaul Burton return of_node_get(hose->of_node); 182f8091a88SPaul Burton } 183f8091a88SPaul Burton #endif 184f8091a88SPaul Burton 185f8091a88SPaul Burton static DEFINE_MUTEX(pci_scan_mutex); 186f8091a88SPaul Burton 187f8091a88SPaul Burton void register_pci_controller(struct pci_controller *hose) 188f8091a88SPaul Burton { 189f8091a88SPaul Burton struct resource *parent; 190f8091a88SPaul Burton 191f8091a88SPaul Burton parent = hose->mem_resource->parent; 192f8091a88SPaul Burton if (!parent) 193f8091a88SPaul Burton parent = &iomem_resource; 194f8091a88SPaul Burton 195f8091a88SPaul Burton if (request_resource(parent, hose->mem_resource) < 0) 196f8091a88SPaul Burton goto out; 197f8091a88SPaul Burton 198f8091a88SPaul Burton parent = hose->io_resource->parent; 199f8091a88SPaul Burton if (!parent) 200f8091a88SPaul Burton parent = &ioport_resource; 201f8091a88SPaul Burton 202f8091a88SPaul Burton if (request_resource(parent, hose->io_resource) < 0) { 203f8091a88SPaul Burton release_resource(hose->mem_resource); 204f8091a88SPaul Burton goto out; 205f8091a88SPaul Burton } 206f8091a88SPaul Burton 207f8091a88SPaul Burton INIT_LIST_HEAD(&hose->list); 208edb0b6a0SMathias Kresin list_add_tail(&hose->list, &controllers); 209f8091a88SPaul Burton 210f8091a88SPaul Burton /* 211f8091a88SPaul Burton * Do not panic here but later - this might happen before console init. 212f8091a88SPaul Burton */ 213f8091a88SPaul Burton if (!hose->io_map_base) { 214f8091a88SPaul Burton printk(KERN_WARNING 215f8091a88SPaul Burton "registering PCI controller with io_map_base unset\n"); 216f8091a88SPaul Burton } 217f8091a88SPaul Burton 218f8091a88SPaul Burton /* 219f8091a88SPaul Burton * Scan the bus if it is register after the PCI subsystem 220f8091a88SPaul Burton * initialization. 221f8091a88SPaul Burton */ 222f8091a88SPaul Burton if (pci_initialized) { 223f8091a88SPaul Burton mutex_lock(&pci_scan_mutex); 224f8091a88SPaul Burton pcibios_scanbus(hose); 225f8091a88SPaul Burton mutex_unlock(&pci_scan_mutex); 226f8091a88SPaul Burton } 227f8091a88SPaul Burton 228f8091a88SPaul Burton return; 229f8091a88SPaul Burton 230f8091a88SPaul Burton out: 231f8091a88SPaul Burton printk(KERN_WARNING 232f8091a88SPaul Burton "Skipping PCI bus scan due to resource conflict\n"); 233f8091a88SPaul Burton } 234f8091a88SPaul Burton 235f8091a88SPaul Burton static int __init pcibios_init(void) 236f8091a88SPaul Burton { 237f8091a88SPaul Burton struct pci_controller *hose; 238f8091a88SPaul Burton 239f8091a88SPaul Burton /* Scan all of the recorded PCI controllers. */ 240f8091a88SPaul Burton list_for_each_entry(hose, &controllers, list) 241f8091a88SPaul Burton pcibios_scanbus(hose); 242f8091a88SPaul Burton 243f8091a88SPaul Burton pci_initialized = 1; 244f8091a88SPaul Burton 245f8091a88SPaul Burton return 0; 246f8091a88SPaul Burton } 247f8091a88SPaul Burton 248f8091a88SPaul Burton subsys_initcall(pcibios_init); 249f8091a88SPaul Burton 250f8091a88SPaul Burton static int pcibios_enable_resources(struct pci_dev *dev, int mask) 251f8091a88SPaul Burton { 252f8091a88SPaul Burton u16 cmd, old_cmd; 253f8091a88SPaul Burton int idx; 254f8091a88SPaul Burton struct resource *r; 255f8091a88SPaul Burton 256f8091a88SPaul Burton pci_read_config_word(dev, PCI_COMMAND, &cmd); 257f8091a88SPaul Burton old_cmd = cmd; 258f8091a88SPaul Burton for (idx=0; idx < PCI_NUM_RESOURCES; idx++) { 259f8091a88SPaul Burton /* Only set up the requested stuff */ 260f8091a88SPaul Burton if (!(mask & (1<<idx))) 261f8091a88SPaul Burton continue; 262f8091a88SPaul Burton 263f8091a88SPaul Burton r = &dev->resource[idx]; 264f8091a88SPaul Burton if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM))) 265f8091a88SPaul Burton continue; 266f8091a88SPaul Burton if ((idx == PCI_ROM_RESOURCE) && 267f8091a88SPaul Burton (!(r->flags & IORESOURCE_ROM_ENABLE))) 268f8091a88SPaul Burton continue; 269f8091a88SPaul Burton if (!r->start && r->end) { 27077edfac4SBjorn Helgaas pci_err(dev, 27177edfac4SBjorn Helgaas "can't enable device: resource collisions\n"); 272f8091a88SPaul Burton return -EINVAL; 273f8091a88SPaul Burton } 274f8091a88SPaul Burton if (r->flags & IORESOURCE_IO) 275f8091a88SPaul Burton cmd |= PCI_COMMAND_IO; 276f8091a88SPaul Burton if (r->flags & IORESOURCE_MEM) 277f8091a88SPaul Burton cmd |= PCI_COMMAND_MEMORY; 278f8091a88SPaul Burton } 279f8091a88SPaul Burton if (cmd != old_cmd) { 28077edfac4SBjorn Helgaas pci_info(dev, "enabling device (%04x -> %04x)\n", old_cmd, cmd); 281f8091a88SPaul Burton pci_write_config_word(dev, PCI_COMMAND, cmd); 282f8091a88SPaul Burton } 283f8091a88SPaul Burton return 0; 284f8091a88SPaul Burton } 285f8091a88SPaul Burton 286f8091a88SPaul Burton int pcibios_enable_device(struct pci_dev *dev, int mask) 287f8091a88SPaul Burton { 288f8091a88SPaul Burton int err; 289f8091a88SPaul Burton 290f8091a88SPaul Burton if ((err = pcibios_enable_resources(dev, mask)) < 0) 291f8091a88SPaul Burton return err; 292f8091a88SPaul Burton 293f8091a88SPaul Burton return pcibios_plat_dev_init(dev); 294f8091a88SPaul Burton } 295f8091a88SPaul Burton 296f8091a88SPaul Burton void pcibios_fixup_bus(struct pci_bus *bus) 297f8091a88SPaul Burton { 298f8091a88SPaul Burton struct pci_dev *dev = bus->self; 299f8091a88SPaul Burton 300f8091a88SPaul Burton if (pci_has_flag(PCI_PROBE_ONLY) && dev && 301f8091a88SPaul Burton (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) { 302f8091a88SPaul Burton pci_read_bridge_bases(bus); 303f8091a88SPaul Burton } 304f8091a88SPaul Burton } 305f8091a88SPaul Burton 306f8091a88SPaul Burton char * (*pcibios_plat_setup)(char *str) __initdata; 307f8091a88SPaul Burton 308f8091a88SPaul Burton char *__init pcibios_setup(char *str) 309f8091a88SPaul Burton { 310f8091a88SPaul Burton if (pcibios_plat_setup) 311f8091a88SPaul Burton return pcibios_plat_setup(str); 312f8091a88SPaul Burton return str; 313f8091a88SPaul Burton } 314