19 #include <linux/kernel.h>
20 #include <linux/pci.h>
21 #include <linux/string.h>
25 #include <linux/list.h>
29 #include <linux/slab.h>
33 #include <linux/export.h>
35 #include <asm/processor.h>
37 #include <asm/pci-bridge.h>
38 #include <asm/byteorder.h>
44 static int global_phb_number;
53 static int pci_bus_count;
74 spin_lock(&hose_spinlock);
75 phb->global_number = global_phb_number++;
77 spin_unlock(&hose_spinlock);
85 spin_lock(&hose_spinlock);
87 spin_unlock(&hose_spinlock);
95 return resource_size(&hose->io_resource);
104 spin_lock(&hose_spinlock);
106 size = pcibios_io_size(hose);
107 if (address >= hose->io_base_virt &&
108 address < (hose->io_base_virt + size)) {
113 spin_unlock(&hose_spinlock);
121 unsigned long ret = ~0;
123 spin_lock(&hose_spinlock);
125 size = pcibios_io_size(hose);
126 if (address >= hose->io_base_phys &&
127 address < (hose->io_base_phys + size)) {
130 ret = base + (address - hose->io_base_phys);
134 spin_unlock(&hose_spinlock);
147 return hose->global_number;
163 if (hose->dn == node)
177 np = pci_device_to_OF_node(pdev);
214 pr_debug(
"PCI: Try to map irq for %s...\n", pci_name(pci_dev));
217 memset(&oirq, 0xff,
sizeof(oirq));
235 line == 0xff || line == 0) {
238 pr_debug(
" No map ! Using line %d (pin %d) from PCI config\n",
245 pr_debug(
" Got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
246 oirq.size, oirq.specifier[0], oirq.specifier[1],
247 of_node_full_name(oirq.controller));
257 pr_debug(
" Mapped to linux irq %d\n", virq);
287 unsigned long io_offset = 0;
296 *offset += hose->pci_mem_offset;
300 io_offset = (
unsigned long)hose->io_base_virt -
_IO_BASE;
301 *offset += io_offset;
318 if ((flags & res_bit) == 0)
327 *offset += hose->io_base_phys - io_offset;
353 else if (write_combine == 0) {
383 int flags = rp->
flags;
404 pr_debug(
"PCI: Non-PCI map for %llx, prot: %lx\n",
405 (
unsigned long long)offset,
pgprot_val(prot));
428 rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
435 mmap_state, write_combine);
448 struct resource *rp = &hose->io_resource;
463 addr = hose->io_base_virt +
port;
467 *((
u8 *)val) =
in_8(addr);
488 struct resource *rp = &hose->io_resource;
503 addr = hose->io_base_virt +
port;
512 out_8(addr, val >> 24);
539 pr_debug(
"pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
542 (
unsigned long long)offset,
543 (
unsigned long long)(offset + size - 1));
553 if ((offset + size) > hose->isa_mem_size) {
556 "Process %s (pid:%d) mapped non-existing PCI"
557 "legacy memory for 0%04x:%02x\n",
565 offset += hose->isa_mem_phys;
567 unsigned long io_offset = (
unsigned long)hose->io_base_virt - \
569 unsigned long roffset = offset + io_offset;
570 rp = &hose->io_resource;
573 if (roffset < rp->
start || (roffset + size) > rp->
end)
575 offset += hose->io_base_phys;
577 pr_debug(
" -> mapping phys %llx\n", (
unsigned long long)offset);
618 offset = hose->pci_mem_offset;
666 int memno = 0, isa_hole = -1;
669 unsigned long long isa_mb = 0;
673 dev->
full_name, primary ?
"(primary)" :
"");
681 pr_debug(
"Parsing ranges property...\n");
682 while ((rlen -= np * 4) >= 0) {
684 pci_space = ranges[0];
685 pci_addr = of_read_number(ranges + 1, 2);
687 size = of_read_number(ranges + pna + 3, 2);
689 pr_debug(
"pci_space: 0x%08x pci_addr:0x%016llx "
690 "cpu_addr:0x%016llx size:0x%016llx\n",
691 pci_space, pci_addr, cpu_addr, size);
700 if (cpu_addr == OF_BAD_ADDR || size == 0)
704 for (; rlen >= np *
sizeof(
u32);
705 ranges += np, rlen -= np * 4) {
706 if (ranges[0] != pci_space)
708 pci_next = of_read_number(ranges + 1, 2);
710 if (pci_next != pci_addr + size ||
711 cpu_next != cpu_addr + size)
713 size += of_read_number(ranges + pna + 3, 2);
718 switch ((pci_space >> 24) & 0x3) {
721 " IO 0x%016llx..0x%016llx -> 0x%016llx\n",
722 cpu_addr, cpu_addr + size - 1, pci_addr);
725 if (hose->pci_io_size) {
727 " \\--> Skipped (too many) !\n");
731 if (size > 0x01000000)
735 hose->io_base_virt =
ioremap(cpu_addr, size);
740 (
unsigned long)hose->io_base_virt;
744 hose->pci_io_size = pci_addr + size;
745 hose->io_base_phys = cpu_addr - pci_addr;
748 res = &hose->io_resource;
750 res->
start = pci_addr;
755 " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
756 cpu_addr, cpu_addr + size - 1, pci_addr,
757 (pci_space & 0x40000000) ?
"Prefetch" :
"");
762 " \\--> Skipped (too many) !\n");
772 hose->isa_mem_size =
size;
780 (isa_hole >= 0 && pci_addr != 0 &&
781 hose->pci_mem_offset == isa_mb))
782 hose->pci_mem_offset = cpu_addr -
pci_addr;
783 else if (pci_addr != 0 &&
784 hose->pci_mem_offset != cpu_addr - pci_addr) {
786 " \\--> Skipped (offset mismatch) !\n");
793 if (pci_space & 0x40000000)
811 if (isa_hole >= 0 && hose->pci_mem_offset != isa_mb) {
812 unsigned int next = isa_hole + 1;
817 sizeof(
struct resource) * (memno - next));
847 if (res->
start == 0) {
848 pr_debug(
"PCI:%s Resource %d %016llx-%016llx [%x]" \
851 (
unsigned long long)res->
start,
852 (
unsigned long long)res->
end,
853 (
unsigned int)res->
flags);
860 pr_debug(
"PCI:%s Resource %d %016llx-%016llx [%x]\n",
862 (
unsigned long long)res->
start,\
863 (
unsigned long long)res->
end,
864 (
unsigned int)res->
flags);
888 if (res->
start != hose->pci_mem_offset)
902 for (i = 0; i < 3; i++) {
915 if (((res->
start - offset) & 0xfffffffful) != 0)
943 pci_bus_for_each_resource(bus, res, i) {
948 if (i >= 3 && bus->
self->transparent)
951 pr_debug(
"PCI:%s Bus rsrc %d %016llx-%016llx [%x] fixup...\n",
953 (
unsigned long long)res->
start,\
954 (
unsigned long long)res->
end,
955 (
unsigned int)res->
flags);
960 if (pcibios_uninitialized_bridge_resource(bus, res)) {
965 pr_debug(
"PCI:%s %016llx-%016llx\n",
967 (
unsigned long long)res->
start,
968 (
unsigned long long)res->
end);
977 pcibios_fixup_bridge(bus);
984 pr_debug(
"PCI: Fixup bus devices %d (%s)\n",
989 dev->
dev.of_node = pci_device_to_OF_node(dev);
997 set_dma_ops(&dev->
dev, pci_dma_ops);
1022 static int skip_isa_ioresource_align(
struct pci_dev *dev)
1047 if (skip_isa_ioresource_align(dev))
1050 start = (start + 0x3ff) & ~0x3ff;
1074 if (firstpp ==
NULL)
1077 if (firstpp ==
NULL)
1080 res->
child = *firstpp;
1086 pr_debug(
"PCI: Reparented %s [%llx..%llx] under %s\n",
1088 (
unsigned long long)p->
start,
1089 (
unsigned long long)p->
end, res->
name);
1133 pr_debug(
"PCI: Allocating bus resources for %04x:%02x...\n",
1136 pci_bus_for_each_resource(bus, res, i) {
1137 if (!res || !res->
flags
1160 pr_debug(
"PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx "
1161 "[0x%x], parent %p (%s)\n",
1162 bus->
self ? pci_name(bus->
self) :
"PHB",
1164 (
unsigned long long)res->
start,
1165 (
unsigned long long)res->
end,
1166 (
unsigned int)res->
flags,
1167 pr, (pr && pr->
name) ? pr->
name :
"nil");
1177 if (reparent_resources(pr, res) == 0)
1181 "%d of PCI bridge %d, will remap\n", i, bus->
number);
1195 pr_debug(
"PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n",
1197 (
unsigned long long)r->
start,
1198 (
unsigned long long)r->
end,
1199 (
unsigned int)r->
flags);
1205 " of device %s, will remap\n", idx, pci_name(dev));
1207 pr_debug(
"PCI: parent is %p: %016llx-%016llx [%x]\n",
1209 (
unsigned long long)pr->
start,
1210 (
unsigned long long)pr->
end,
1211 (
unsigned int)pr->
flags);
1219 static void __init pcibios_allocate_resources(
int pass)
1243 if (pass == disabled)
1256 pr_debug(
"PCI: Switching off ROM of %s\n",
1260 reg & ~PCI_ROM_ADDRESS_ENABLE);
1266 static void __init pcibios_reserve_legacy_regions(
struct pci_bus *bus)
1273 pr_debug(
"Reserving legacy ranges for domain %04x\n",
1282 res->
name =
"Legacy IO";
1285 res->
end = (offset + 0xfff) & 0xfffffffful;
1286 pr_debug(
"Candidate legacy IO: %pR\n", res);
1289 "PCI %04x:%02x Cannot reserve Legacy IO %pR\n",
1296 offset = hose->pci_mem_offset;
1297 pr_debug(
"hose mem offset: %016llx\n", (
unsigned long long)offset);
1298 for (i = 0; i < 3; i++) {
1302 pr_debug(
"hose mem res: %pR\n", pres);
1303 if ((pres->
start - offset) <= 0xa0000 &&
1304 (pres->
end - offset) >= 0xbffff)
1311 res->
name =
"Legacy VGA memory";
1315 pr_debug(
"Candidate VGA memory: %pR\n", res);
1318 "PCI %04x:%02x Cannot reserve VGA memory %pR\n",
1334 pcibios_allocate_resources(0);
1335 pcibios_allocate_resources(1);
1342 pcibios_reserve_legacy_regions(b);
1345 pr_debug(
"PCI: Assigning unassigned resources...\n");
1349 #ifdef CONFIG_HOTPLUG
1371 "Resource %d: %016llx..%016llx [%x]\n",
1373 (
unsigned long long)r->
start,
1374 (
unsigned long long)r->
end,
1375 (
unsigned int)r->
flags);
1395 pr_debug(
"PCI: Finishing adding to hotplug bus %04x:%02x\n",
1419 unsigned long io_offset;
1424 res = &hose->io_resource;
1428 res->
start = (res->
start + io_offset) & 0xffffffff
u;
1429 res->
end = (res->
end + io_offset) & 0xffffffffu;
1433 " bridge %s (domain %d)\n",
1434 hose->dn->full_name, hose->global_number);
1442 pr_debug(
"PCI: PHB IO resource = %016llx-%016llx [%lx]\n",
1443 (
unsigned long long)res->
start,
1444 (
unsigned long long)res->
end,
1445 (
unsigned long)res->
flags);
1448 for (i = 0; i < 3; ++
i) {
1454 "host bridge %s (domain %d)\n",
1455 hose->dn->full_name, hose->global_number);
1458 res->
start = hose->pci_mem_offset;
1465 pr_debug(
"PCI: PHB MEM resource %d = %016llx-%016llx [%lx]\n",
1466 i, (
unsigned long long)res->
start,
1467 (
unsigned long long)res->
end,
1468 (
unsigned long)res->
flags);
1471 pr_debug(
"PCI: PHB MEM offset = %016llx\n",
1472 (
unsigned long long)hose->pci_mem_offset);
1473 pr_debug(
"PCI: PHB IO offset = %08lx\n",
1474 (
unsigned long)hose->io_base_virt -
_IO_BASE);
1481 return of_node_get(hose->dn);
1490 pr_debug(
"PCI: Scanning PHB %s\n", of_node_full_name(node));
1492 pcibios_setup_phb_resources(hose, &resources);
1495 hose->
ops, hose, &resources);
1498 hose->global_number);
1522 pci_bus_count = next_busno;
1553 hose = pci_bus_to_hose(bus);
1561 return (
long)hose->pci_mem_offset;
1563 return (
long)hose->io_base_phys;
1577 #define NULL_PCI_OP(rw, size, type) \
1579 null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
1581 return PCIBIOS_DEVICE_NOT_FOUND; \
1585 null_read_config(
struct pci_bus *bus,
unsigned int devfn,
int offset,
1592 null_write_config(
struct pci_bus *bus,
unsigned int devfn,
int offset,
1598 static struct pci_ops null_pci_ops = {
1599 .read = null_read_config,
1600 .write = null_write_config,
1617 bus.
ops = hose ? hose->
ops : &null_pci_ops;
1621 #define EARLY_PCI_OP(rw, size, type) \
1622 int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
1623 int devfn, int offset, type value) \
1625 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
1626 devfn, offset, value); \