1e05b3b4aSPaul Mackerras /* 2e05b3b4aSPaul Mackerras * Common pmac/prep/chrp pci routines. -- Cort 3e05b3b4aSPaul Mackerras */ 4e05b3b4aSPaul Mackerras 5e05b3b4aSPaul Mackerras #include <linux/kernel.h> 6e05b3b4aSPaul Mackerras #include <linux/pci.h> 7e05b3b4aSPaul Mackerras #include <linux/delay.h> 8e05b3b4aSPaul Mackerras #include <linux/string.h> 9e05b3b4aSPaul Mackerras #include <linux/init.h> 10e05b3b4aSPaul Mackerras #include <linux/capability.h> 11e05b3b4aSPaul Mackerras #include <linux/sched.h> 12e05b3b4aSPaul Mackerras #include <linux/errno.h> 13e05b3b4aSPaul Mackerras #include <linux/bootmem.h> 146e99e458SBenjamin Herrenschmidt #include <linux/irq.h> 15f90bb153SBenjamin Herrenschmidt #include <linux/list.h> 1666524b22SStephen Rothwell #include <linux/of.h> 175a0e3ad6STejun Heo #include <linux/slab.h> 1893087948SPaul Gortmaker #include <linux/export.h> 19e05b3b4aSPaul Mackerras 20e05b3b4aSPaul Mackerras #include <asm/processor.h> 21e05b3b4aSPaul Mackerras #include <asm/io.h> 22e05b3b4aSPaul Mackerras #include <asm/prom.h> 23e05b3b4aSPaul Mackerras #include <asm/sections.h> 24e05b3b4aSPaul Mackerras #include <asm/pci-bridge.h> 25c3bd517dSMilton Miller #include <asm/ppc-pci.h> 26e05b3b4aSPaul Mackerras #include <asm/byteorder.h> 27e05b3b4aSPaul Mackerras #include <asm/uaccess.h> 28e05b3b4aSPaul Mackerras #include <asm/machdep.h> 29e05b3b4aSPaul Mackerras 30e05b3b4aSPaul Mackerras #undef DEBUG 31e05b3b4aSPaul Mackerras 32e05b3b4aSPaul Mackerras unsigned long isa_io_base = 0; 33e05b3b4aSPaul Mackerras unsigned long pci_dram_offset = 0; 34e05b3b4aSPaul Mackerras int pcibios_assign_bus_offset = 1; 35e05b3b4aSPaul Mackerras 36e05b3b4aSPaul Mackerras void pcibios_make_OF_bus_map(void); 37e05b3b4aSPaul Mackerras 38e05b3b4aSPaul Mackerras static void fixup_cpc710_pci64(struct pci_dev* dev); 39e05b3b4aSPaul Mackerras static u8* pci_to_OF_bus_map; 40e05b3b4aSPaul Mackerras 41e05b3b4aSPaul Mackerras /* By default, we don't re-assign bus numbers. We do this only on 42e05b3b4aSPaul Mackerras * some pmacs 43e05b3b4aSPaul Mackerras */ 44fc3fb71cSBenjamin Herrenschmidt static int pci_assign_all_buses; 45e05b3b4aSPaul Mackerras 46e05b3b4aSPaul Mackerras static int pci_bus_count; 47e05b3b4aSPaul Mackerras 487b6b574cSBenjamin Herrenschmidt /* This will remain NULL for now, until isa-bridge.c is made common 497b6b574cSBenjamin Herrenschmidt * to both 32-bit and 64-bit. 507b6b574cSBenjamin Herrenschmidt */ 517b6b574cSBenjamin Herrenschmidt struct pci_dev *isa_bridge_pcidev; 527b6b574cSBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(isa_bridge_pcidev); 537b6b574cSBenjamin Herrenschmidt 54e05b3b4aSPaul Mackerras static void 55e05b3b4aSPaul Mackerras fixup_cpc710_pci64(struct pci_dev* dev) 56e05b3b4aSPaul Mackerras { 57e05b3b4aSPaul Mackerras /* Hide the PCI64 BARs from the kernel as their content doesn't 58e05b3b4aSPaul Mackerras * fit well in the resource management 59e05b3b4aSPaul Mackerras */ 60e05b3b4aSPaul Mackerras dev->resource[0].start = dev->resource[0].end = 0; 61e05b3b4aSPaul Mackerras dev->resource[0].flags = 0; 62e05b3b4aSPaul Mackerras dev->resource[1].start = dev->resource[1].end = 0; 63e05b3b4aSPaul Mackerras dev->resource[1].flags = 0; 64e05b3b4aSPaul Mackerras } 65e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64); 66e05b3b4aSPaul Mackerras 67e05b3b4aSPaul Mackerras /* 68e05b3b4aSPaul Mackerras * Functions below are used on OpenFirmware machines. 69e05b3b4aSPaul Mackerras */ 70e05b3b4aSPaul Mackerras static void 71e05b3b4aSPaul Mackerras make_one_node_map(struct device_node* node, u8 pci_bus) 72e05b3b4aSPaul Mackerras { 73a7f67bdfSJeremy Kerr const int *bus_range; 74e05b3b4aSPaul Mackerras int len; 75e05b3b4aSPaul Mackerras 76e05b3b4aSPaul Mackerras if (pci_bus >= pci_bus_count) 77e05b3b4aSPaul Mackerras return; 78e2eb6392SStephen Rothwell bus_range = of_get_property(node, "bus-range", &len); 79e05b3b4aSPaul Mackerras if (bus_range == NULL || len < 2 * sizeof(int)) { 80e05b3b4aSPaul Mackerras printk(KERN_WARNING "Can't get bus-range for %s, " 81e05b3b4aSPaul Mackerras "assuming it starts at 0\n", node->full_name); 82e05b3b4aSPaul Mackerras pci_to_OF_bus_map[pci_bus] = 0; 83e05b3b4aSPaul Mackerras } else 84e05b3b4aSPaul Mackerras pci_to_OF_bus_map[pci_bus] = bus_range[0]; 85e05b3b4aSPaul Mackerras 8666524b22SStephen Rothwell for_each_child_of_node(node, node) { 87e05b3b4aSPaul Mackerras struct pci_dev* dev; 88a7f67bdfSJeremy Kerr const unsigned int *class_code, *reg; 89e05b3b4aSPaul Mackerras 90e2eb6392SStephen Rothwell class_code = of_get_property(node, "class-code", NULL); 91e05b3b4aSPaul Mackerras if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI && 92e05b3b4aSPaul Mackerras (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) 93e05b3b4aSPaul Mackerras continue; 94e2eb6392SStephen Rothwell reg = of_get_property(node, "reg", NULL); 95e05b3b4aSPaul Mackerras if (!reg) 96e05b3b4aSPaul Mackerras continue; 97ab462768SAlan Cox dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff)); 98ab462768SAlan Cox if (!dev || !dev->subordinate) { 99ab462768SAlan Cox pci_dev_put(dev); 100e05b3b4aSPaul Mackerras continue; 101ab462768SAlan Cox } 102e05b3b4aSPaul Mackerras make_one_node_map(node, dev->subordinate->number); 103ab462768SAlan Cox pci_dev_put(dev); 104e05b3b4aSPaul Mackerras } 105e05b3b4aSPaul Mackerras } 106e05b3b4aSPaul Mackerras 107e05b3b4aSPaul Mackerras void 108e05b3b4aSPaul Mackerras pcibios_make_OF_bus_map(void) 109e05b3b4aSPaul Mackerras { 110e05b3b4aSPaul Mackerras int i; 111a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 112a7f67bdfSJeremy Kerr struct property *map_prop; 1138c8dc322SStephen Rothwell struct device_node *dn; 114e05b3b4aSPaul Mackerras 1155cbded58SRobert P. J. Day pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL); 116e05b3b4aSPaul Mackerras if (!pci_to_OF_bus_map) { 117e05b3b4aSPaul Mackerras printk(KERN_ERR "Can't allocate OF bus map !\n"); 118e05b3b4aSPaul Mackerras return; 119e05b3b4aSPaul Mackerras } 120e05b3b4aSPaul Mackerras 121e05b3b4aSPaul Mackerras /* We fill the bus map with invalid values, that helps 122e05b3b4aSPaul Mackerras * debugging. 123e05b3b4aSPaul Mackerras */ 124e05b3b4aSPaul Mackerras for (i=0; i<pci_bus_count; i++) 125e05b3b4aSPaul Mackerras pci_to_OF_bus_map[i] = 0xff; 126e05b3b4aSPaul Mackerras 127e05b3b4aSPaul Mackerras /* For each hose, we begin searching bridges */ 128a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 12944ef3390SStephen Rothwell struct device_node* node = hose->dn; 13044ef3390SStephen Rothwell 131e05b3b4aSPaul Mackerras if (!node) 132e05b3b4aSPaul Mackerras continue; 133e05b3b4aSPaul Mackerras make_one_node_map(node, hose->first_busno); 134e05b3b4aSPaul Mackerras } 1358c8dc322SStephen Rothwell dn = of_find_node_by_path("/"); 1368c8dc322SStephen Rothwell map_prop = of_find_property(dn, "pci-OF-bus-map", NULL); 137a7f67bdfSJeremy Kerr if (map_prop) { 138a7f67bdfSJeremy Kerr BUG_ON(pci_bus_count > map_prop->length); 139a7f67bdfSJeremy Kerr memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count); 140a7f67bdfSJeremy Kerr } 1418c8dc322SStephen Rothwell of_node_put(dn); 142e05b3b4aSPaul Mackerras #ifdef DEBUG 143e05b3b4aSPaul Mackerras printk("PCI->OF bus map:\n"); 144e05b3b4aSPaul Mackerras for (i=0; i<pci_bus_count; i++) { 145e05b3b4aSPaul Mackerras if (pci_to_OF_bus_map[i] == 0xff) 146e05b3b4aSPaul Mackerras continue; 147e05b3b4aSPaul Mackerras printk("%d -> %d\n", i, pci_to_OF_bus_map[i]); 148e05b3b4aSPaul Mackerras } 149e05b3b4aSPaul Mackerras #endif 150e05b3b4aSPaul Mackerras } 151e05b3b4aSPaul Mackerras 152e05b3b4aSPaul Mackerras 153e05b3b4aSPaul Mackerras /* 154e05b3b4aSPaul Mackerras * Returns the PCI device matching a given OF node 155e05b3b4aSPaul Mackerras */ 15698d9f30cSBenjamin Herrenschmidt int pci_device_from_OF_node(struct device_node *node, u8 *bus, u8 *devfn) 157e05b3b4aSPaul Mackerras { 158e05b3b4aSPaul Mackerras struct pci_dev *dev = NULL; 15998d9f30cSBenjamin Herrenschmidt const __be32 *reg; 16098d9f30cSBenjamin Herrenschmidt int size; 161e05b3b4aSPaul Mackerras 16298d9f30cSBenjamin Herrenschmidt /* Check if it might have a chance to be a PCI device */ 16398d9f30cSBenjamin Herrenschmidt if (!pci_find_hose_for_OF_device(node)) 164e05b3b4aSPaul Mackerras return -ENODEV; 16598d9f30cSBenjamin Herrenschmidt 16698d9f30cSBenjamin Herrenschmidt reg = of_get_property(node, "reg", &size); 16798d9f30cSBenjamin Herrenschmidt if (!reg || size < 5 * sizeof(u32)) 168e05b3b4aSPaul Mackerras return -ENODEV; 16998d9f30cSBenjamin Herrenschmidt 17098d9f30cSBenjamin Herrenschmidt *bus = (be32_to_cpup(®[0]) >> 16) & 0xff; 17198d9f30cSBenjamin Herrenschmidt *devfn = (be32_to_cpup(®[0]) >> 8) & 0xff; 172e05b3b4aSPaul Mackerras 173e05b3b4aSPaul Mackerras /* Ok, here we need some tweak. If we have already renumbered 174e05b3b4aSPaul Mackerras * all busses, we can't rely on the OF bus number any more. 175e05b3b4aSPaul Mackerras * the pci_to_OF_bus_map is not enough as several PCI busses 176e05b3b4aSPaul Mackerras * may match the same OF bus number. 177e05b3b4aSPaul Mackerras */ 178e05b3b4aSPaul Mackerras if (!pci_to_OF_bus_map) 179e05b3b4aSPaul Mackerras return 0; 180e05b3b4aSPaul Mackerras 181e05b3b4aSPaul Mackerras for_each_pci_dev(dev) 182e05b3b4aSPaul Mackerras if (pci_to_OF_bus_map[dev->bus->number] == *bus && 183e05b3b4aSPaul Mackerras dev->devfn == *devfn) { 184e05b3b4aSPaul Mackerras *bus = dev->bus->number; 185e05b3b4aSPaul Mackerras pci_dev_put(dev); 186e05b3b4aSPaul Mackerras return 0; 187e05b3b4aSPaul Mackerras } 188e05b3b4aSPaul Mackerras 189e05b3b4aSPaul Mackerras return -ENODEV; 190e05b3b4aSPaul Mackerras } 191e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_from_OF_node); 192e05b3b4aSPaul Mackerras 193e05b3b4aSPaul Mackerras /* We create the "pci-OF-bus-map" property now so it appears in the 194e05b3b4aSPaul Mackerras * /proc device tree 195e05b3b4aSPaul Mackerras */ 196e05b3b4aSPaul Mackerras void __init 197e05b3b4aSPaul Mackerras pci_create_OF_bus_map(void) 198e05b3b4aSPaul Mackerras { 199e05b3b4aSPaul Mackerras struct property* of_prop; 2008c8dc322SStephen Rothwell struct device_node *dn; 201e05b3b4aSPaul Mackerras 202e05b3b4aSPaul Mackerras of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256); 2038c8dc322SStephen Rothwell if (!of_prop) 2048c8dc322SStephen Rothwell return; 2058c8dc322SStephen Rothwell dn = of_find_node_by_path("/"); 2068c8dc322SStephen Rothwell if (dn) { 207e05b3b4aSPaul Mackerras memset(of_prop, -1, sizeof(struct property) + 256); 208e05b3b4aSPaul Mackerras of_prop->name = "pci-OF-bus-map"; 209e05b3b4aSPaul Mackerras of_prop->length = 256; 2101a38147eSStephen Rothwell of_prop->value = &of_prop[1]; 21179d1c712SNathan Fontenot of_add_property(dn, of_prop); 2128c8dc322SStephen Rothwell of_node_put(dn); 213e05b3b4aSPaul Mackerras } 214e05b3b4aSPaul Mackerras } 215e05b3b4aSPaul Mackerras 216cad5cef6SGreg Kroah-Hartman void pcibios_setup_phb_io_space(struct pci_controller *hose) 21753280323SBenjamin Herrenschmidt { 21853280323SBenjamin Herrenschmidt unsigned long io_offset; 21953280323SBenjamin Herrenschmidt struct resource *res = &hose->io_resource; 22053280323SBenjamin Herrenschmidt 22153280323SBenjamin Herrenschmidt /* Fixup IO space offset */ 22238973ba7SBjorn Helgaas io_offset = pcibios_io_space_offset(hose); 22338973ba7SBjorn Helgaas res->start += io_offset; 22438973ba7SBjorn Helgaas res->end += io_offset; 22553280323SBenjamin Herrenschmidt } 22653280323SBenjamin Herrenschmidt 2273fd94c6bSBenjamin Herrenschmidt static int __init pcibios_init(void) 228e05b3b4aSPaul Mackerras { 229a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 230a4c9e328SKumar Gala int next_busno = 0; 231e05b3b4aSPaul Mackerras 232e05b3b4aSPaul Mackerras printk(KERN_INFO "PCI: Probing PCI hardware\n"); 233e05b3b4aSPaul Mackerras 2340e47ff1cSRob Herring if (pci_has_flag(PCI_REASSIGN_ALL_BUS)) 235fc3fb71cSBenjamin Herrenschmidt pci_assign_all_buses = 1; 236fc3fb71cSBenjamin Herrenschmidt 237e05b3b4aSPaul Mackerras /* Scan all of the recorded PCI controllers. */ 238a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 239e05b3b4aSPaul Mackerras if (pci_assign_all_buses) 240e05b3b4aSPaul Mackerras hose->first_busno = next_busno; 241e05b3b4aSPaul Mackerras hose->last_busno = 0xff; 242b5d937deSGrant Likely pcibios_scan_phb(hose); 24353280323SBenjamin Herrenschmidt pci_bus_add_devices(hose->bus); 244e05b3b4aSPaul Mackerras if (pci_assign_all_buses || next_busno <= hose->last_busno) 245e05b3b4aSPaul Mackerras next_busno = hose->last_busno + pcibios_assign_bus_offset; 246e05b3b4aSPaul Mackerras } 247e05b3b4aSPaul Mackerras pci_bus_count = next_busno; 248e05b3b4aSPaul Mackerras 249e05b3b4aSPaul Mackerras /* OpenFirmware based machines need a map of OF bus 250e05b3b4aSPaul Mackerras * numbers vs. kernel bus numbers since we may have to 251e05b3b4aSPaul Mackerras * remap them. 252e05b3b4aSPaul Mackerras */ 2536b82b3e4SAnton Vorontsov if (pci_assign_all_buses) 254e05b3b4aSPaul Mackerras pcibios_make_OF_bus_map(); 255e05b3b4aSPaul Mackerras 2563fd94c6bSBenjamin Herrenschmidt /* Call common code to handle resource allocation */ 2573fd94c6bSBenjamin Herrenschmidt pcibios_resource_survey(); 258e05b3b4aSPaul Mackerras 259e05b3b4aSPaul Mackerras /* Call machine dependent post-init code */ 260e05b3b4aSPaul Mackerras if (ppc_md.pcibios_after_init) 261e05b3b4aSPaul Mackerras ppc_md.pcibios_after_init(); 262e05b3b4aSPaul Mackerras 263e05b3b4aSPaul Mackerras return 0; 264e05b3b4aSPaul Mackerras } 265e05b3b4aSPaul Mackerras 266e05b3b4aSPaul Mackerras subsys_initcall(pcibios_init); 267e05b3b4aSPaul Mackerras 2680b1d40c4SKumar Gala static struct pci_controller* 269e05b3b4aSPaul Mackerras pci_bus_to_hose(int bus) 270e05b3b4aSPaul Mackerras { 271a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 272e05b3b4aSPaul Mackerras 273a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) 274e05b3b4aSPaul Mackerras if (bus >= hose->first_busno && bus <= hose->last_busno) 275e05b3b4aSPaul Mackerras return hose; 276e05b3b4aSPaul Mackerras return NULL; 277e05b3b4aSPaul Mackerras } 278e05b3b4aSPaul Mackerras 279e05b3b4aSPaul Mackerras /* Provide information on locations of various I/O regions in physical 280e05b3b4aSPaul Mackerras * memory. Do this on a per-card basis so that we choose the right 281e05b3b4aSPaul Mackerras * root bridge. 282e05b3b4aSPaul Mackerras * Note that the returned IO or memory base is a physical address 283e05b3b4aSPaul Mackerras */ 284e05b3b4aSPaul Mackerras 285e05b3b4aSPaul Mackerras long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn) 286e05b3b4aSPaul Mackerras { 287e05b3b4aSPaul Mackerras struct pci_controller* hose; 288e05b3b4aSPaul Mackerras long result = -EOPNOTSUPP; 289e05b3b4aSPaul Mackerras 290e05b3b4aSPaul Mackerras hose = pci_bus_to_hose(bus); 291e05b3b4aSPaul Mackerras if (!hose) 292e05b3b4aSPaul Mackerras return -ENODEV; 293e05b3b4aSPaul Mackerras 294e05b3b4aSPaul Mackerras switch (which) { 295e05b3b4aSPaul Mackerras case IOBASE_BRIDGE_NUMBER: 296e05b3b4aSPaul Mackerras return (long)hose->first_busno; 297e05b3b4aSPaul Mackerras case IOBASE_MEMORY: 298e05b3b4aSPaul Mackerras return (long)hose->pci_mem_offset; 299e05b3b4aSPaul Mackerras case IOBASE_IO: 300e05b3b4aSPaul Mackerras return (long)hose->io_base_phys; 301e05b3b4aSPaul Mackerras case IOBASE_ISA_IO: 302e05b3b4aSPaul Mackerras return (long)isa_io_base; 303e05b3b4aSPaul Mackerras case IOBASE_ISA_MEM: 304e05b3b4aSPaul Mackerras return (long)isa_mem_base; 305e05b3b4aSPaul Mackerras } 306e05b3b4aSPaul Mackerras 307e05b3b4aSPaul Mackerras return result; 308e05b3b4aSPaul Mackerras } 309e05b3b4aSPaul Mackerras 310e05b3b4aSPaul Mackerras 311