0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #undef DEBUG
0011
0012 #include <linux/kernel.h>
0013 #include <linux/pci.h>
0014 #include <linux/string.h>
0015 #include <linux/init.h>
0016 #include <linux/export.h>
0017 #include <linux/mm.h>
0018 #include <linux/list.h>
0019 #include <linux/syscalls.h>
0020 #include <linux/irq.h>
0021 #include <linux/vmalloc.h>
0022 #include <linux/of.h>
0023
0024 #include <asm/processor.h>
0025 #include <asm/io.h>
0026 #include <asm/pci-bridge.h>
0027 #include <asm/byteorder.h>
0028 #include <asm/machdep.h>
0029 #include <asm/ppc-pci.h>
0030
0031
0032
0033
0034
0035
0036
0037 unsigned long pci_io_base;
0038 EXPORT_SYMBOL(pci_io_base);
0039
0040 static int __init pcibios_init(void)
0041 {
0042 struct pci_controller *hose, *tmp;
0043
0044 printk(KERN_INFO "PCI: Probing PCI hardware\n");
0045
0046
0047
0048
0049 ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
0050
0051
0052
0053
0054 pci_add_flags(PCI_ENABLE_PROC_DOMAINS | PCI_COMPAT_DOMAIN_0);
0055
0056
0057 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
0058 pcibios_scan_phb(hose);
0059
0060
0061 pcibios_resource_survey();
0062
0063
0064 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
0065 pci_bus_add_devices(hose->bus);
0066
0067
0068 if (ppc_md.pcibios_fixup)
0069 ppc_md.pcibios_fixup();
0070
0071 printk(KERN_DEBUG "PCI: Probing PCI hardware done\n");
0072
0073 return 0;
0074 }
0075
0076 subsys_initcall(pcibios_init);
0077
0078 int pcibios_unmap_io_space(struct pci_bus *bus)
0079 {
0080 struct pci_controller *hose;
0081
0082 WARN_ON(bus == NULL);
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094 if (bus->self) {
0095 #ifdef CONFIG_PPC_BOOK3S_64
0096 struct resource *res = bus->resource[0];
0097 #endif
0098
0099 pr_debug("IO unmapping for PCI-PCI bridge %s\n",
0100 pci_name(bus->self));
0101
0102 #ifdef CONFIG_PPC_BOOK3S_64
0103 __flush_hash_table_range(res->start + _IO_BASE,
0104 res->end + _IO_BASE + 1);
0105 #endif
0106 return 0;
0107 }
0108
0109
0110 hose = pci_bus_to_host(bus);
0111
0112 pr_debug("IO unmapping for PHB %pOF\n", hose->dn);
0113 pr_debug(" alloc=0x%p\n", hose->io_base_alloc);
0114
0115 iounmap(hose->io_base_alloc);
0116 return 0;
0117 }
0118 EXPORT_SYMBOL_GPL(pcibios_unmap_io_space);
0119
0120 void __iomem *ioremap_phb(phys_addr_t paddr, unsigned long size)
0121 {
0122 struct vm_struct *area;
0123 unsigned long addr;
0124
0125 WARN_ON_ONCE(paddr & ~PAGE_MASK);
0126 WARN_ON_ONCE(size & ~PAGE_MASK);
0127
0128
0129
0130
0131
0132
0133
0134
0135 area = __get_vm_area_caller(size, 0, PHB_IO_BASE, PHB_IO_END,
0136 __builtin_return_address(0));
0137 if (!area)
0138 return NULL;
0139
0140 addr = (unsigned long)area->addr;
0141 if (ioremap_page_range(addr, addr + size, paddr,
0142 pgprot_noncached(PAGE_KERNEL))) {
0143 vunmap_range(addr, addr + size);
0144 return NULL;
0145 }
0146
0147 return (void __iomem *)addr;
0148 }
0149 EXPORT_SYMBOL_GPL(ioremap_phb);
0150
0151 static int pcibios_map_phb_io_space(struct pci_controller *hose)
0152 {
0153 unsigned long phys_page;
0154 unsigned long size_page;
0155 unsigned long io_virt_offset;
0156
0157 phys_page = ALIGN_DOWN(hose->io_base_phys, PAGE_SIZE);
0158 size_page = ALIGN(hose->pci_io_size, PAGE_SIZE);
0159
0160
0161 hose->io_base_alloc = NULL;
0162
0163
0164 if (hose->pci_io_size == 0 || hose->io_base_phys == 0)
0165 return 0;
0166
0167
0168
0169
0170
0171
0172
0173 hose->io_base_alloc = ioremap_phb(phys_page, size_page);
0174 if (!hose->io_base_alloc)
0175 return -ENOMEM;
0176 hose->io_base_virt = hose->io_base_alloc +
0177 hose->io_base_phys - phys_page;
0178
0179 pr_debug("IO mapping for PHB %pOF\n", hose->dn);
0180 pr_debug(" phys=0x%016llx, virt=0x%p (alloc=0x%p)\n",
0181 hose->io_base_phys, hose->io_base_virt, hose->io_base_alloc);
0182 pr_debug(" size=0x%016llx (alloc=0x%016lx)\n",
0183 hose->pci_io_size, size_page);
0184
0185
0186 io_virt_offset = pcibios_io_space_offset(hose);
0187 hose->io_resource.start += io_virt_offset;
0188 hose->io_resource.end += io_virt_offset;
0189
0190 pr_debug(" hose->io_resource=%pR\n", &hose->io_resource);
0191
0192 return 0;
0193 }
0194
0195 int pcibios_map_io_space(struct pci_bus *bus)
0196 {
0197 WARN_ON(bus == NULL);
0198
0199
0200
0201
0202 if (bus->self) {
0203 pr_debug("IO mapping for PCI-PCI bridge %s\n",
0204 pci_name(bus->self));
0205 pr_debug(" virt=0x%016llx...0x%016llx\n",
0206 bus->resource[0]->start + _IO_BASE,
0207 bus->resource[0]->end + _IO_BASE);
0208 return 0;
0209 }
0210
0211 return pcibios_map_phb_io_space(pci_bus_to_host(bus));
0212 }
0213 EXPORT_SYMBOL_GPL(pcibios_map_io_space);
0214
0215 void pcibios_setup_phb_io_space(struct pci_controller *hose)
0216 {
0217 pcibios_map_phb_io_space(hose);
0218 }
0219
0220 #define IOBASE_BRIDGE_NUMBER 0
0221 #define IOBASE_MEMORY 1
0222 #define IOBASE_IO 2
0223 #define IOBASE_ISA_IO 3
0224 #define IOBASE_ISA_MEM 4
0225
0226 SYSCALL_DEFINE3(pciconfig_iobase, long, which, unsigned long, in_bus,
0227 unsigned long, in_devfn)
0228 {
0229 struct pci_controller* hose;
0230 struct pci_bus *tmp_bus, *bus = NULL;
0231 struct device_node *hose_node;
0232
0233
0234
0235
0236
0237
0238 if (in_bus == 0 && of_machine_is_compatible("MacRISC4")) {
0239 struct device_node *agp;
0240
0241 agp = of_find_compatible_node(NULL, NULL, "u3-agp");
0242 if (agp)
0243 in_bus = 0xf0;
0244 of_node_put(agp);
0245 }
0246
0247
0248
0249
0250
0251 list_for_each_entry(tmp_bus, &pci_root_buses, node) {
0252 if (in_bus >= tmp_bus->number &&
0253 in_bus <= tmp_bus->busn_res.end) {
0254 bus = tmp_bus;
0255 break;
0256 }
0257 }
0258 if (bus == NULL || bus->dev.of_node == NULL)
0259 return -ENODEV;
0260
0261 hose_node = bus->dev.of_node;
0262 hose = PCI_DN(hose_node)->phb;
0263
0264 switch (which) {
0265 case IOBASE_BRIDGE_NUMBER:
0266 return (long)hose->first_busno;
0267 case IOBASE_MEMORY:
0268 return (long)hose->mem_offset[0];
0269 case IOBASE_IO:
0270 return (long)hose->io_base_phys;
0271 case IOBASE_ISA_IO:
0272 return (long)isa_io_base;
0273 case IOBASE_ISA_MEM:
0274 return -EINVAL;
0275 }
0276
0277 return -EOPNOTSUPP;
0278 }
0279
0280 #ifdef CONFIG_NUMA
0281 int pcibus_to_node(struct pci_bus *bus)
0282 {
0283 struct pci_controller *phb = pci_bus_to_host(bus);
0284 return phb->node;
0285 }
0286 EXPORT_SYMBOL(pcibus_to_node);
0287 #endif
0288
0289 #ifdef CONFIG_PPC_PMAC
0290 int pci_device_from_OF_node(struct device_node *np, u8 *bus, u8 *devfn)
0291 {
0292 if (!PCI_DN(np))
0293 return -ENODEV;
0294 *bus = PCI_DN(np)->busno;
0295 *devfn = PCI_DN(np)->devfn;
0296 return 0;
0297 }
0298 #endif