Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Common pmac/prep/chrp pci routines. -- Cort
0004  */
0005 
0006 #include <linux/kernel.h>
0007 #include <linux/pci.h>
0008 #include <linux/delay.h>
0009 #include <linux/string.h>
0010 #include <linux/init.h>
0011 #include <linux/capability.h>
0012 #include <linux/sched.h>
0013 #include <linux/errno.h>
0014 #include <linux/memblock.h>
0015 #include <linux/syscalls.h>
0016 #include <linux/irq.h>
0017 #include <linux/list.h>
0018 #include <linux/of.h>
0019 #include <linux/slab.h>
0020 #include <linux/export.h>
0021 
0022 #include <asm/processor.h>
0023 #include <asm/io.h>
0024 #include <asm/sections.h>
0025 #include <asm/pci-bridge.h>
0026 #include <asm/ppc-pci.h>
0027 #include <asm/byteorder.h>
0028 #include <linux/uaccess.h>
0029 #include <asm/machdep.h>
0030 
0031 #undef DEBUG
0032 
0033 unsigned long isa_io_base     = 0;
0034 unsigned long pci_dram_offset = 0;
0035 int pcibios_assign_bus_offset = 1;
0036 EXPORT_SYMBOL(isa_io_base);
0037 EXPORT_SYMBOL(pci_dram_offset);
0038 
0039 static void fixup_cpc710_pci64(struct pci_dev* dev);
0040 
0041 /* By default, we don't re-assign bus numbers. We do this only on
0042  * some pmacs
0043  */
0044 static int pci_assign_all_buses;
0045 
0046 /* This will remain NULL for now, until isa-bridge.c is made common
0047  * to both 32-bit and 64-bit.
0048  */
0049 struct pci_dev *isa_bridge_pcidev;
0050 EXPORT_SYMBOL_GPL(isa_bridge_pcidev);
0051 
0052 static void
0053 fixup_cpc710_pci64(struct pci_dev* dev)
0054 {
0055     /* Hide the PCI64 BARs from the kernel as their content doesn't
0056      * fit well in the resource management
0057      */
0058     dev->resource[0].start = dev->resource[0].end = 0;
0059     dev->resource[0].flags = 0;
0060     dev->resource[1].start = dev->resource[1].end = 0;
0061     dev->resource[1].flags = 0;
0062 }
0063 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
0064 
0065 #if defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_CHRP)
0066 
0067 static u8* pci_to_OF_bus_map;
0068 static int pci_bus_count;
0069 
0070 /*
0071  * Functions below are used on OpenFirmware machines.
0072  */
0073 static void
0074 make_one_node_map(struct device_node* node, u8 pci_bus)
0075 {
0076     const int *bus_range;
0077     int len;
0078 
0079     if (pci_bus >= pci_bus_count)
0080         return;
0081     bus_range = of_get_property(node, "bus-range", &len);
0082     if (bus_range == NULL || len < 2 * sizeof(int)) {
0083         printk(KERN_WARNING "Can't get bus-range for %pOF, "
0084                "assuming it starts at 0\n", node);
0085         pci_to_OF_bus_map[pci_bus] = 0;
0086     } else
0087         pci_to_OF_bus_map[pci_bus] = bus_range[0];
0088 
0089     for_each_child_of_node(node, node) {
0090         struct pci_dev* dev;
0091         const unsigned int *class_code, *reg;
0092     
0093         class_code = of_get_property(node, "class-code", NULL);
0094         if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
0095             (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
0096             continue;
0097         reg = of_get_property(node, "reg", NULL);
0098         if (!reg)
0099             continue;
0100         dev = pci_get_domain_bus_and_slot(0, pci_bus,
0101                           ((reg[0] >> 8) & 0xff));
0102         if (!dev || !dev->subordinate) {
0103             pci_dev_put(dev);
0104             continue;
0105         }
0106         make_one_node_map(node, dev->subordinate->number);
0107         pci_dev_put(dev);
0108     }
0109 }
0110     
0111 static void __init
0112 pcibios_make_OF_bus_map(void)
0113 {
0114     int i;
0115     struct pci_controller *hose, *tmp;
0116     struct property *map_prop;
0117     struct device_node *dn;
0118 
0119     pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
0120     if (!pci_to_OF_bus_map) {
0121         printk(KERN_ERR "Can't allocate OF bus map !\n");
0122         return;
0123     }
0124 
0125     /* We fill the bus map with invalid values, that helps
0126      * debugging.
0127      */
0128     for (i=0; i<pci_bus_count; i++)
0129         pci_to_OF_bus_map[i] = 0xff;
0130 
0131     /* For each hose, we begin searching bridges */
0132     list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
0133         struct device_node* node = hose->dn;
0134 
0135         if (!node)
0136             continue;
0137         make_one_node_map(node, hose->first_busno);
0138     }
0139     dn = of_find_node_by_path("/");
0140     map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
0141     if (map_prop) {
0142         BUG_ON(pci_bus_count > map_prop->length);
0143         memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
0144     }
0145     of_node_put(dn);
0146 #ifdef DEBUG
0147     printk("PCI->OF bus map:\n");
0148     for (i=0; i<pci_bus_count; i++) {
0149         if (pci_to_OF_bus_map[i] == 0xff)
0150             continue;
0151         printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
0152     }
0153 #endif
0154 }
0155 
0156 
0157 #ifdef CONFIG_PPC_PMAC
0158 /*
0159  * Returns the PCI device matching a given OF node
0160  */
0161 int pci_device_from_OF_node(struct device_node *node, u8 *bus, u8 *devfn)
0162 {
0163     struct pci_dev *dev = NULL;
0164     const __be32 *reg;
0165     int size;
0166 
0167     /* Check if it might have a chance to be a PCI device */
0168     if (!pci_find_hose_for_OF_device(node))
0169         return -ENODEV;
0170 
0171     reg = of_get_property(node, "reg", &size);
0172     if (!reg || size < 5 * sizeof(u32))
0173         return -ENODEV;
0174 
0175     *bus = (be32_to_cpup(&reg[0]) >> 16) & 0xff;
0176     *devfn = (be32_to_cpup(&reg[0]) >> 8) & 0xff;
0177 
0178     /* Ok, here we need some tweak. If we have already renumbered
0179      * all busses, we can't rely on the OF bus number any more.
0180      * the pci_to_OF_bus_map is not enough as several PCI busses
0181      * may match the same OF bus number.
0182      */
0183     if (!pci_to_OF_bus_map)
0184         return 0;
0185 
0186     for_each_pci_dev(dev)
0187         if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
0188                 dev->devfn == *devfn) {
0189             *bus = dev->bus->number;
0190             pci_dev_put(dev);
0191             return 0;
0192         }
0193 
0194     return -ENODEV;
0195 }
0196 EXPORT_SYMBOL(pci_device_from_OF_node);
0197 #endif
0198 
0199 #ifdef CONFIG_PPC_CHRP
0200 /* We create the "pci-OF-bus-map" property now so it appears in the
0201  * /proc device tree
0202  */
0203 void __init
0204 pci_create_OF_bus_map(void)
0205 {
0206     struct property* of_prop;
0207     struct device_node *dn;
0208 
0209     of_prop = memblock_alloc(sizeof(struct property) + 256,
0210                  SMP_CACHE_BYTES);
0211     if (!of_prop)
0212         panic("%s: Failed to allocate %zu bytes\n", __func__,
0213               sizeof(struct property) + 256);
0214     dn = of_find_node_by_path("/");
0215     if (dn) {
0216         memset(of_prop, -1, sizeof(struct property) + 256);
0217         of_prop->name = "pci-OF-bus-map";
0218         of_prop->length = 256;
0219         of_prop->value = &of_prop[1];
0220         of_add_property(dn, of_prop);
0221         of_node_put(dn);
0222     }
0223 }
0224 #endif
0225 
0226 #endif /* defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_CHRP) */
0227 
0228 void pcibios_setup_phb_io_space(struct pci_controller *hose)
0229 {
0230     unsigned long io_offset;
0231     struct resource *res = &hose->io_resource;
0232 
0233     /* Fixup IO space offset */
0234     io_offset = pcibios_io_space_offset(hose);
0235     res->start += io_offset;
0236     res->end += io_offset;
0237 }
0238 
0239 static int __init pcibios_init(void)
0240 {
0241     struct pci_controller *hose, *tmp;
0242 #ifndef CONFIG_PPC_PCI_BUS_NUM_DOMAIN_DEPENDENT
0243     int next_busno = 0;
0244 #endif
0245 
0246     printk(KERN_INFO "PCI: Probing PCI hardware\n");
0247 
0248 #ifdef CONFIG_PPC_PCI_BUS_NUM_DOMAIN_DEPENDENT
0249     /*
0250      * Enable PCI domains in /proc when PCI bus numbers are not unique
0251      * across all PCI domains to prevent conflicts. And keep PCI domain 0
0252      * backward compatible in /proc for video cards.
0253      */
0254     pci_add_flags(PCI_ENABLE_PROC_DOMAINS | PCI_COMPAT_DOMAIN_0);
0255 #endif
0256 
0257     if (pci_has_flag(PCI_REASSIGN_ALL_BUS))
0258         pci_assign_all_buses = 1;
0259 
0260     /* Scan all of the recorded PCI controllers.  */
0261     list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
0262 #ifndef CONFIG_PPC_PCI_BUS_NUM_DOMAIN_DEPENDENT
0263         if (pci_assign_all_buses)
0264             hose->first_busno = next_busno;
0265 #endif
0266         hose->last_busno = 0xff;
0267         pcibios_scan_phb(hose);
0268         pci_bus_add_devices(hose->bus);
0269 #ifndef CONFIG_PPC_PCI_BUS_NUM_DOMAIN_DEPENDENT
0270         if (pci_assign_all_buses || next_busno <= hose->last_busno)
0271             next_busno = hose->last_busno + pcibios_assign_bus_offset;
0272 #endif
0273     }
0274 
0275 #if defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_CHRP)
0276     pci_bus_count = next_busno;
0277 
0278     /* OpenFirmware based machines need a map of OF bus
0279      * numbers vs. kernel bus numbers since we may have to
0280      * remap them.
0281      */
0282     if (pci_assign_all_buses)
0283         pcibios_make_OF_bus_map();
0284 #endif
0285 
0286     /* Call common code to handle resource allocation */
0287     pcibios_resource_survey();
0288 
0289     /* Call machine dependent fixup */
0290     if (ppc_md.pcibios_fixup)
0291         ppc_md.pcibios_fixup();
0292 
0293     /* Call machine dependent post-init code */
0294     if (ppc_md.pcibios_after_init)
0295         ppc_md.pcibios_after_init();
0296 
0297     return 0;
0298 }
0299 
0300 subsys_initcall(pcibios_init);
0301 
0302 static struct pci_controller*
0303 pci_bus_to_hose(int bus)
0304 {
0305     struct pci_controller *hose, *tmp;
0306 
0307     list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
0308         if (bus >= hose->first_busno && bus <= hose->last_busno)
0309             return hose;
0310     return NULL;
0311 }
0312 
0313 /* Provide information on locations of various I/O regions in physical
0314  * memory.  Do this on a per-card basis so that we choose the right
0315  * root bridge.
0316  * Note that the returned IO or memory base is a physical address
0317  */
0318 
0319 SYSCALL_DEFINE3(pciconfig_iobase, long, which,
0320         unsigned long, bus, unsigned long, devfn)
0321 {
0322     struct pci_controller* hose;
0323     long result = -EOPNOTSUPP;
0324 
0325     hose = pci_bus_to_hose(bus);
0326     if (!hose)
0327         return -ENODEV;
0328 
0329     switch (which) {
0330     case IOBASE_BRIDGE_NUMBER:
0331         return (long)hose->first_busno;
0332     case IOBASE_MEMORY:
0333         return (long)hose->mem_offset[0];
0334     case IOBASE_IO:
0335         return (long)hose->io_base_phys;
0336     case IOBASE_ISA_IO:
0337         return (long)isa_io_base;
0338     case IOBASE_ISA_MEM:
0339         return (long)isa_mem_base;
0340     }
0341 
0342     return result;
0343 }