Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Routines for tracking a legacy ISA bridge
0004  *
0005  * Copyrigh 2007 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
0006  *
0007  * Some bits and pieces moved over from pci_64.c
0008  *
0009  * Copyrigh 2003 Anton Blanchard <anton@au.ibm.com>, IBM Corp.
0010  */
0011 
0012 #define DEBUG
0013 
0014 #include <linux/kernel.h>
0015 #include <linux/pci.h>
0016 #include <linux/string.h>
0017 #include <linux/export.h>
0018 #include <linux/init.h>
0019 #include <linux/mm.h>
0020 #include <linux/notifier.h>
0021 #include <linux/of_address.h>
0022 #include <linux/vmalloc.h>
0023 
0024 #include <asm/processor.h>
0025 #include <asm/io.h>
0026 #include <asm/pci-bridge.h>
0027 #include <asm/machdep.h>
0028 #include <asm/ppc-pci.h>
0029 #include <asm/isa-bridge.h>
0030 
0031 unsigned long isa_io_base;  /* NULL if no ISA bus */
0032 EXPORT_SYMBOL(isa_io_base);
0033 
0034 /* Cached ISA bridge dev. */
0035 static struct device_node *isa_bridge_devnode;
0036 struct pci_dev *isa_bridge_pcidev;
0037 EXPORT_SYMBOL_GPL(isa_bridge_pcidev);
0038 
0039 #define ISA_SPACE_MASK 0x1
0040 #define ISA_SPACE_IO 0x1
0041 
0042 static void remap_isa_base(phys_addr_t pa, unsigned long size)
0043 {
0044     WARN_ON_ONCE(ISA_IO_BASE & ~PAGE_MASK);
0045     WARN_ON_ONCE(pa & ~PAGE_MASK);
0046     WARN_ON_ONCE(size & ~PAGE_MASK);
0047 
0048     if (slab_is_available()) {
0049         if (ioremap_page_range(ISA_IO_BASE, ISA_IO_BASE + size, pa,
0050                 pgprot_noncached(PAGE_KERNEL)))
0051             vunmap_range(ISA_IO_BASE, ISA_IO_BASE + size);
0052     } else {
0053         early_ioremap_range(ISA_IO_BASE, pa, size,
0054                 pgprot_noncached(PAGE_KERNEL));
0055     }
0056 }
0057 
0058 static void pci_process_ISA_OF_ranges(struct device_node *isa_node,
0059                       unsigned long phb_io_base_phys)
0060 {
0061     /* We should get some saner parsing here and remove these structs */
0062     struct pci_address {
0063         u32 a_hi;
0064         u32 a_mid;
0065         u32 a_lo;
0066     };
0067 
0068     struct isa_address {
0069         u32 a_hi;
0070         u32 a_lo;
0071     };
0072 
0073     struct isa_range {
0074         struct isa_address isa_addr;
0075         struct pci_address pci_addr;
0076         unsigned int size;
0077     };
0078 
0079     const struct isa_range *range;
0080     unsigned long pci_addr;
0081     unsigned int isa_addr;
0082     unsigned int size;
0083     int rlen = 0;
0084 
0085     range = of_get_property(isa_node, "ranges", &rlen);
0086     if (range == NULL || (rlen < sizeof(struct isa_range)))
0087         goto inval_range;
0088 
0089     /* From "ISA Binding to 1275"
0090      * The ranges property is laid out as an array of elements,
0091      * each of which comprises:
0092      *   cells 0 - 1:   an ISA address
0093      *   cells 2 - 4:   a PCI address
0094      *          (size depending on dev->n_addr_cells)
0095      *   cell 5:        the size of the range
0096      */
0097     if ((range->isa_addr.a_hi & ISA_SPACE_MASK) != ISA_SPACE_IO) {
0098         range++;
0099         rlen -= sizeof(struct isa_range);
0100         if (rlen < sizeof(struct isa_range))
0101             goto inval_range;
0102     }
0103     if ((range->isa_addr.a_hi & ISA_SPACE_MASK) != ISA_SPACE_IO)
0104         goto inval_range;
0105 
0106     isa_addr = range->isa_addr.a_lo;
0107     pci_addr = (unsigned long) range->pci_addr.a_mid << 32 |
0108         range->pci_addr.a_lo;
0109 
0110     /* Assume these are both zero. Note: We could fix that and
0111      * do a proper parsing instead ... oh well, that will do for
0112      * now as nobody uses fancy mappings for ISA bridges
0113      */
0114     if ((pci_addr != 0) || (isa_addr != 0)) {
0115         printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
0116                __func__);
0117         return;
0118     }
0119 
0120     /* Align size and make sure it's cropped to 64K */
0121     size = PAGE_ALIGN(range->size);
0122     if (size > 0x10000)
0123         size = 0x10000;
0124 
0125     remap_isa_base(phb_io_base_phys, size);
0126     return;
0127 
0128 inval_range:
0129     printk(KERN_ERR "no ISA IO ranges or unexpected isa range, "
0130            "mapping 64k\n");
0131     remap_isa_base(phb_io_base_phys, 0x10000);
0132 }
0133 
0134 
0135 /**
0136  * isa_bridge_find_early - Find and map the ISA IO space early before
0137  *                         main PCI discovery. This is optionally called by
0138  *                         the arch code when adding PCI PHBs to get early
0139  *                         access to ISA IO ports
0140  */
0141 void __init isa_bridge_find_early(struct pci_controller *hose)
0142 {
0143     struct device_node *np, *parent = NULL, *tmp;
0144 
0145     /* If we already have an ISA bridge, bail off */
0146     if (isa_bridge_devnode != NULL)
0147         return;
0148 
0149     /* For each "isa" node in the system. Note : we do a search by
0150      * type and not by name. It might be better to do by name but that's
0151      * what the code used to do and I don't want to break too much at
0152      * once. We can look into changing that separately
0153      */
0154     for_each_node_by_type(np, "isa") {
0155         /* Look for our hose being a parent */
0156         for (parent = of_get_parent(np); parent;) {
0157             if (parent == hose->dn) {
0158                 of_node_put(parent);
0159                 break;
0160             }
0161             tmp = parent;
0162             parent = of_get_parent(parent);
0163             of_node_put(tmp);
0164         }
0165         if (parent != NULL)
0166             break;
0167     }
0168     if (np == NULL)
0169         return;
0170     isa_bridge_devnode = np;
0171 
0172     /* Now parse the "ranges" property and setup the ISA mapping */
0173     pci_process_ISA_OF_ranges(np, hose->io_base_phys);
0174 
0175     /* Set the global ISA io base to indicate we have an ISA bridge */
0176     isa_io_base = ISA_IO_BASE;
0177 
0178     pr_debug("ISA bridge (early) is %pOF\n", np);
0179 }
0180 
0181 /**
0182  * isa_bridge_find_early - Find and map the ISA IO space early before
0183  *                         main PCI discovery. This is optionally called by
0184  *                         the arch code when adding PCI PHBs to get early
0185  *                         access to ISA IO ports
0186  */
0187 void __init isa_bridge_init_non_pci(struct device_node *np)
0188 {
0189     const __be32 *ranges, *pbasep = NULL;
0190     int rlen, i, rs;
0191     u32 na, ns, pna;
0192     u64 cbase, pbase, size = 0;
0193 
0194     /* If we already have an ISA bridge, bail off */
0195     if (isa_bridge_devnode != NULL)
0196         return;
0197 
0198     pna = of_n_addr_cells(np);
0199     if (of_property_read_u32(np, "#address-cells", &na) ||
0200         of_property_read_u32(np, "#size-cells", &ns)) {
0201         pr_warn("ISA: Non-PCI bridge %pOF is missing address format\n",
0202             np);
0203         return;
0204     }
0205 
0206     /* Check it's a supported address format */
0207     if (na != 2 || ns != 1) {
0208         pr_warn("ISA: Non-PCI bridge %pOF has unsupported address format\n",
0209             np);
0210         return;
0211     }
0212     rs = na + ns + pna;
0213 
0214     /* Grab the ranges property */
0215     ranges = of_get_property(np, "ranges", &rlen);
0216     if (ranges == NULL || rlen < rs) {
0217         pr_warn("ISA: Non-PCI bridge %pOF has absent or invalid ranges\n",
0218             np);
0219         return;
0220     }
0221 
0222     /* Parse it. We are only looking for IO space */
0223     for (i = 0; (i + rs - 1) < rlen; i += rs) {
0224         if (be32_to_cpup(ranges + i) != 1)
0225             continue;
0226         cbase = be32_to_cpup(ranges + i + 1);
0227         size = of_read_number(ranges + i + na + pna, ns);
0228         pbasep = ranges + i + na;
0229         break;
0230     }
0231 
0232     /* Got something ? */
0233     if (!size || !pbasep) {
0234         pr_warn("ISA: Non-PCI bridge %pOF has no usable IO range\n",
0235             np);
0236         return;
0237     }
0238 
0239     /* Align size and make sure it's cropped to 64K */
0240     size = PAGE_ALIGN(size);
0241     if (size > 0x10000)
0242         size = 0x10000;
0243 
0244     /* Map pbase */
0245     pbase = of_translate_address(np, pbasep);
0246     if (pbase == OF_BAD_ADDR) {
0247         pr_warn("ISA: Non-PCI bridge %pOF failed to translate IO base\n",
0248             np);
0249         return;
0250     }
0251 
0252     /* We need page alignment */
0253     if ((cbase & ~PAGE_MASK) || (pbase & ~PAGE_MASK)) {
0254         pr_warn("ISA: Non-PCI bridge %pOF has non aligned IO range\n",
0255             np);
0256         return;
0257     }
0258 
0259     /* Got it */
0260     isa_bridge_devnode = np;
0261 
0262     /* Set the global ISA io base to indicate we have an ISA bridge
0263      * and map it
0264      */
0265     isa_io_base = ISA_IO_BASE;
0266     remap_isa_base(pbase, size);
0267 
0268     pr_debug("ISA: Non-PCI bridge is %pOF\n", np);
0269 }
0270 
0271 /**
0272  * isa_bridge_find_late - Find and map the ISA IO space upon discovery of
0273  *                        a new ISA bridge
0274  */
0275 static void isa_bridge_find_late(struct pci_dev *pdev,
0276                  struct device_node *devnode)
0277 {
0278     struct pci_controller *hose = pci_bus_to_host(pdev->bus);
0279 
0280     /* Store ISA device node and PCI device */
0281     isa_bridge_devnode = of_node_get(devnode);
0282     isa_bridge_pcidev = pdev;
0283 
0284     /* Now parse the "ranges" property and setup the ISA mapping */
0285     pci_process_ISA_OF_ranges(devnode, hose->io_base_phys);
0286 
0287     /* Set the global ISA io base to indicate we have an ISA bridge */
0288     isa_io_base = ISA_IO_BASE;
0289 
0290     pr_debug("ISA bridge (late) is %pOF on %s\n",
0291          devnode, pci_name(pdev));
0292 }
0293 
0294 /**
0295  * isa_bridge_remove - Remove/unmap an ISA bridge
0296  */
0297 static void isa_bridge_remove(void)
0298 {
0299     pr_debug("ISA bridge removed !\n");
0300 
0301     /* Clear the global ISA io base to indicate that we have no more
0302      * ISA bridge. Note that drivers don't quite handle that, though
0303      * we should probably do something about it. But do we ever really
0304      * have ISA bridges being removed on machines using legacy devices ?
0305      */
0306     isa_io_base = ISA_IO_BASE;
0307 
0308     /* Clear references to the bridge */
0309     of_node_put(isa_bridge_devnode);
0310     isa_bridge_devnode = NULL;
0311     isa_bridge_pcidev = NULL;
0312 
0313     /* Unmap the ISA area */
0314     vunmap_range(ISA_IO_BASE, ISA_IO_BASE + 0x10000);
0315 }
0316 
0317 /**
0318  * isa_bridge_notify - Get notified of PCI devices addition/removal
0319  */
0320 static int isa_bridge_notify(struct notifier_block *nb, unsigned long action,
0321                  void *data)
0322 {
0323     struct device *dev = data;
0324     struct pci_dev *pdev = to_pci_dev(dev);
0325     struct device_node *devnode = pci_device_to_OF_node(pdev);
0326 
0327     switch(action) {
0328     case BUS_NOTIFY_ADD_DEVICE:
0329         /* Check if we have an early ISA device, without PCI dev */
0330         if (isa_bridge_devnode && isa_bridge_devnode == devnode &&
0331             !isa_bridge_pcidev) {
0332             pr_debug("ISA bridge PCI attached: %s\n",
0333                  pci_name(pdev));
0334             isa_bridge_pcidev = pdev;
0335         }
0336 
0337         /* Check if we have no ISA device, and this happens to be one,
0338          * register it as such if it has an OF device
0339          */
0340         if (!isa_bridge_devnode && of_node_is_type(devnode, "isa"))
0341             isa_bridge_find_late(pdev, devnode);
0342 
0343         return 0;
0344     case BUS_NOTIFY_DEL_DEVICE:
0345         /* Check if this our existing ISA device */
0346         if (pdev == isa_bridge_pcidev ||
0347             (devnode && devnode == isa_bridge_devnode))
0348             isa_bridge_remove();
0349         return 0;
0350     }
0351     return 0;
0352 }
0353 
0354 static struct notifier_block isa_bridge_notifier = {
0355     .notifier_call = isa_bridge_notify
0356 };
0357 
0358 /**
0359  * isa_bridge_init - register to be notified of ISA bridge addition/removal
0360  *
0361  */
0362 static int __init isa_bridge_init(void)
0363 {
0364     bus_register_notifier(&pci_bus_type, &isa_bridge_notifier);
0365     return 0;
0366 }
0367 arch_initcall(isa_bridge_init);