blob: 8447dcc2c2b3f7db34086a997030c7d92742dd0a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Port for PPC64 David Engebretsen, IBM Corp.
3 * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
4 *
5 * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
6 * Rework, based on alpha PCI code.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14#undef DEBUG
15
16#include <linux/config.h>
17#include <linux/kernel.h>
18#include <linux/pci.h>
19#include <linux/string.h>
20#include <linux/init.h>
21#include <linux/bootmem.h>
22#include <linux/mm.h>
23#include <linux/list.h>
Paul Mackerrasb2ad7b52005-09-09 23:02:36 +100024#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/processor.h>
27#include <asm/io.h>
28#include <asm/prom.h>
29#include <asm/pci-bridge.h>
30#include <asm/byteorder.h>
31#include <asm/irq.h>
32#include <asm/machdep.h>
33#include <asm/udbg.h>
34
35#include "pci.h"
36
37#ifdef DEBUG
38#define DBG(fmt...) udbg_printf(fmt)
39#else
40#define DBG(fmt...)
41#endif
42
43unsigned long pci_probe_only = 1;
44unsigned long pci_assign_all_buses = 0;
45
46/*
47 * legal IO pages under MAX_ISA_PORT. This is to ensure we don't touch
48 * devices we don't have access to.
49 */
50unsigned long io_page_mask;
51
52EXPORT_SYMBOL(io_page_mask);
53
54
55unsigned int pcibios_assign_all_busses(void)
56{
57 return pci_assign_all_buses;
58}
59
60/* pci_io_base -- the base address from which io bars are offsets.
61 * This is the lowest I/O base address (so bar values are always positive),
62 * and it *must* be the start of ISA space if an ISA bus exists because
63 * ISA drivers use hard coded offsets. If no ISA bus exists a dummy
64 * page is mapped and isa_io_limit prevents access to it.
65 */
66unsigned long isa_io_base; /* NULL if no ISA bus */
67EXPORT_SYMBOL(isa_io_base);
68unsigned long pci_io_base;
69EXPORT_SYMBOL(pci_io_base);
70
71void iSeries_pcibios_init(void);
72
73LIST_HEAD(hose_list);
74
75struct dma_mapping_ops pci_dma_ops;
76EXPORT_SYMBOL(pci_dma_ops);
77
78int global_phb_number; /* Global phb counter */
79
80/* Cached ISA bridge dev. */
81struct pci_dev *ppc64_isabridge_dev = NULL;
82
83static void fixup_broken_pcnet32(struct pci_dev* dev)
84{
85 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
86 dev->vendor = PCI_VENDOR_ID_AMD;
87 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89}
90DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
91
92void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
93 struct resource *res)
94{
95 unsigned long offset = 0;
96 struct pci_controller *hose = pci_bus_to_host(dev->bus);
97
98 if (!hose)
99 return;
100
101 if (res->flags & IORESOURCE_IO)
102 offset = (unsigned long)hose->io_base_virt - pci_io_base;
103
104 if (res->flags & IORESOURCE_MEM)
105 offset = hose->pci_mem_offset;
106
107 region->start = res->start - offset;
108 region->end = res->end - offset;
109}
110
Dominik Brodowski43c34732005-08-04 18:06:21 -0700111void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
112 struct pci_bus_region *region)
113{
114 unsigned long offset = 0;
115 struct pci_controller *hose = pci_bus_to_host(dev->bus);
116
117 if (!hose)
118 return;
119
120 if (res->flags & IORESOURCE_IO)
121 offset = (unsigned long)hose->io_base_virt - pci_io_base;
122
123 if (res->flags & IORESOURCE_MEM)
124 offset = hose->pci_mem_offset;
125
126 res->start = region->start + offset;
127 res->end = region->end + offset;
128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#ifdef CONFIG_HOTPLUG
131EXPORT_SYMBOL(pcibios_resource_to_bus);
Dominik Brodowski43c34732005-08-04 18:06:21 -0700132EXPORT_SYMBOL(pcibios_bus_to_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#endif
134
135/*
136 * We need to avoid collisions with `mirrored' VGA ports
137 * and other strange ISA hardware, so we always want the
138 * addresses to be allocated in the 0x000-0x0ff region
139 * modulo 0x400.
140 *
141 * Why? Because some silly external IO cards only decode
142 * the low 10 bits of the IO address. The 0x00-0xff region
143 * is reserved for motherboard devices that decode all 16
144 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
145 * but we want to try to avoid allocating at 0x2900-0x2bff
146 * which might have be mirrored at 0x0100-0x03ff..
147 */
148void pcibios_align_resource(void *data, struct resource *res,
149 unsigned long size, unsigned long align)
150{
151 struct pci_dev *dev = data;
152 struct pci_controller *hose = pci_bus_to_host(dev->bus);
153 unsigned long start = res->start;
154 unsigned long alignto;
155
156 if (res->flags & IORESOURCE_IO) {
157 unsigned long offset = (unsigned long)hose->io_base_virt -
158 pci_io_base;
159 /* Make sure we start at our min on all hoses */
160 if (start - offset < PCIBIOS_MIN_IO)
161 start = PCIBIOS_MIN_IO + offset;
162
163 /*
164 * Put everything into 0x00-0xff region modulo 0x400
165 */
166 if (start & 0x300)
167 start = (start + 0x3ff) & ~0x3ff;
168
169 } else if (res->flags & IORESOURCE_MEM) {
170 /* Make sure we start at our min on all hoses */
171 if (start - hose->pci_mem_offset < PCIBIOS_MIN_MEM)
172 start = PCIBIOS_MIN_MEM + hose->pci_mem_offset;
173
174 /* Align to multiple of size of minimum base. */
175 alignto = max(0x1000UL, align);
176 start = ALIGN(start, alignto);
177 }
178
179 res->start = start;
180}
181
182static DEFINE_SPINLOCK(hose_spinlock);
183
184/*
185 * pci_controller(phb) initialized common variables.
186 */
187void __devinit pci_setup_pci_controller(struct pci_controller *hose)
188{
189 memset(hose, 0, sizeof(struct pci_controller));
190
191 spin_lock(&hose_spinlock);
192 hose->global_number = global_phb_number++;
193 list_add_tail(&hose->list_node, &hose_list);
194 spin_unlock(&hose_spinlock);
195}
196
197static void __init pcibios_claim_one_bus(struct pci_bus *b)
198{
199 struct pci_dev *dev;
200 struct pci_bus *child_bus;
201
202 list_for_each_entry(dev, &b->devices, bus_list) {
203 int i;
204
205 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
206 struct resource *r = &dev->resource[i];
207
208 if (r->parent || !r->start || !r->flags)
209 continue;
210 pci_claim_resource(dev, i);
211 }
212 }
213
214 list_for_each_entry(child_bus, &b->children, node)
215 pcibios_claim_one_bus(child_bus);
216}
217
218#ifndef CONFIG_PPC_ISERIES
219static void __init pcibios_claim_of_setup(void)
220{
221 struct pci_bus *b;
222
223 list_for_each_entry(b, &pci_root_buses, node)
224 pcibios_claim_one_bus(b);
225}
226#endif
227
228static int __init pcibios_init(void)
229{
230 struct pci_controller *hose, *tmp;
231 struct pci_bus *bus;
232
233 /* For now, override phys_mem_access_prot. If we need it,
234 * later, we may move that initialization to each ppc_md
235 */
236 ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
237
238#ifdef CONFIG_PPC_ISERIES
239 iSeries_pcibios_init();
240#endif
241
242 printk("PCI: Probing PCI hardware\n");
243
244 /* Scan all of the recorded PCI controllers. */
245 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
246 hose->last_busno = 0xff;
247 bus = pci_scan_bus(hose->first_busno, hose->ops,
248 hose->arch_data);
249 hose->bus = bus;
250 hose->last_busno = bus->subordinate;
251 }
252
253#ifndef CONFIG_PPC_ISERIES
254 if (pci_probe_only)
255 pcibios_claim_of_setup();
256 else
257 /* FIXME: `else' will be removed when
258 pci_assign_unassigned_resources() is able to work
259 correctly with [partially] allocated PCI tree. */
260 pci_assign_unassigned_resources();
261#endif /* !CONFIG_PPC_ISERIES */
262
263 /* Call machine dependent final fixup */
264 if (ppc_md.pcibios_fixup)
265 ppc_md.pcibios_fixup();
266
267 /* Cache the location of the ISA bridge (if we have one) */
268 ppc64_isabridge_dev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
269 if (ppc64_isabridge_dev != NULL)
270 printk("ISA bridge at %s\n", pci_name(ppc64_isabridge_dev));
271
272 printk("PCI: Probing PCI hardware done\n");
273
274 return 0;
275}
276
277subsys_initcall(pcibios_init);
278
279char __init *pcibios_setup(char *str)
280{
281 return str;
282}
283
284int pcibios_enable_device(struct pci_dev *dev, int mask)
285{
286 u16 cmd, oldcmd;
287 int i;
288
289 pci_read_config_word(dev, PCI_COMMAND, &cmd);
290 oldcmd = cmd;
291
292 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
293 struct resource *res = &dev->resource[i];
294
295 /* Only set up the requested stuff */
296 if (!(mask & (1<<i)))
297 continue;
298
299 if (res->flags & IORESOURCE_IO)
300 cmd |= PCI_COMMAND_IO;
301 if (res->flags & IORESOURCE_MEM)
302 cmd |= PCI_COMMAND_MEMORY;
303 }
304
305 if (cmd != oldcmd) {
306 printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
307 pci_name(dev), cmd);
308 /* Enable the appropriate bits in the PCI command register. */
309 pci_write_config_word(dev, PCI_COMMAND, cmd);
310 }
311 return 0;
312}
313
314/*
315 * Return the domain number for this bus.
316 */
317int pci_domain_nr(struct pci_bus *bus)
318{
319#ifdef CONFIG_PPC_ISERIES
320 return 0;
321#else
322 struct pci_controller *hose = pci_bus_to_host(bus);
323
324 return hose->global_number;
325#endif
326}
327
328EXPORT_SYMBOL(pci_domain_nr);
329
330/* Decide whether to display the domain number in /proc */
331int pci_proc_domain(struct pci_bus *bus)
332{
333#ifdef CONFIG_PPC_ISERIES
334 return 0;
335#else
336 struct pci_controller *hose = pci_bus_to_host(bus);
337 return hose->buid;
338#endif
339}
340
341/*
342 * Platform support for /proc/bus/pci/X/Y mmap()s,
343 * modelled on the sparc64 implementation by Dave Miller.
344 * -- paulus.
345 */
346
347/*
348 * Adjust vm_pgoff of VMA such that it is the physical page offset
349 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
350 *
351 * Basically, the user finds the base address for his device which he wishes
352 * to mmap. They read the 32-bit value from the config space base register,
353 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
354 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
355 *
356 * Returns negative error code on failure, zero on success.
357 */
358static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
359 unsigned long *offset,
360 enum pci_mmap_state mmap_state)
361{
362 struct pci_controller *hose = pci_bus_to_host(dev->bus);
363 unsigned long io_offset = 0;
364 int i, res_bit;
365
366 if (hose == 0)
367 return NULL; /* should never happen */
368
369 /* If memory, add on the PCI bridge address offset */
370 if (mmap_state == pci_mmap_mem) {
371 *offset += hose->pci_mem_offset;
372 res_bit = IORESOURCE_MEM;
373 } else {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000374 io_offset = (unsigned long)hose->io_base_virt - pci_io_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 *offset += io_offset;
376 res_bit = IORESOURCE_IO;
377 }
378
379 /*
380 * Check that the offset requested corresponds to one of the
381 * resources of the device.
382 */
383 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
384 struct resource *rp = &dev->resource[i];
385 int flags = rp->flags;
386
387 /* treat ROM as memory (should be already) */
388 if (i == PCI_ROM_RESOURCE)
389 flags |= IORESOURCE_MEM;
390
391 /* Active and same type? */
392 if ((flags & res_bit) == 0)
393 continue;
394
395 /* In the range of this resource? */
396 if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
397 continue;
398
399 /* found it! construct the final physical address */
400 if (mmap_state == pci_mmap_io)
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000401 *offset += hose->io_base_phys - io_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return rp;
403 }
404
405 return NULL;
406}
407
408/*
409 * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
410 * device mapping.
411 */
412static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
413 pgprot_t protection,
414 enum pci_mmap_state mmap_state,
415 int write_combine)
416{
417 unsigned long prot = pgprot_val(protection);
418
419 /* Write combine is always 0 on non-memory space mappings. On
420 * memory space, if the user didn't pass 1, we check for a
421 * "prefetchable" resource. This is a bit hackish, but we use
422 * this to workaround the inability of /sysfs to provide a write
423 * combine bit
424 */
425 if (mmap_state != pci_mmap_mem)
426 write_combine = 0;
427 else if (write_combine == 0) {
428 if (rp->flags & IORESOURCE_PREFETCH)
429 write_combine = 1;
430 }
431
432 /* XXX would be nice to have a way to ask for write-through */
433 prot |= _PAGE_NO_CACHE;
434 if (write_combine)
435 prot &= ~_PAGE_GUARDED;
436 else
437 prot |= _PAGE_GUARDED;
438
439 printk("PCI map for %s:%lx, prot: %lx\n", pci_name(dev), rp->start,
440 prot);
441
442 return __pgprot(prot);
443}
444
445/*
446 * This one is used by /dev/mem and fbdev who have no clue about the
447 * PCI device, it tries to find the PCI device first and calls the
448 * above routine
449 */
450pgprot_t pci_phys_mem_access_prot(struct file *file,
451 unsigned long offset,
452 unsigned long size,
453 pgprot_t protection)
454{
455 struct pci_dev *pdev = NULL;
456 struct resource *found = NULL;
457 unsigned long prot = pgprot_val(protection);
458 int i;
459
460 if (page_is_ram(offset >> PAGE_SHIFT))
David Gibson1f8d4192005-05-05 16:15:13 -0700461 return __pgprot(prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
464
465 for_each_pci_dev(pdev) {
466 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
467 struct resource *rp = &pdev->resource[i];
468 int flags = rp->flags;
469
470 /* Active and same type? */
471 if ((flags & IORESOURCE_MEM) == 0)
472 continue;
473 /* In the range of this resource? */
474 if (offset < (rp->start & PAGE_MASK) ||
475 offset > rp->end)
476 continue;
477 found = rp;
478 break;
479 }
480 if (found)
481 break;
482 }
483 if (found) {
484 if (found->flags & IORESOURCE_PREFETCH)
485 prot &= ~_PAGE_GUARDED;
486 pci_dev_put(pdev);
487 }
488
489 DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
490
491 return __pgprot(prot);
492}
493
494
495/*
496 * Perform the actual remap of the pages for a PCI device mapping, as
497 * appropriate for this architecture. The region in the process to map
498 * is described by vm_start and vm_end members of VMA, the base physical
499 * address is found in vm_pgoff.
500 * The pci device structure is provided so that architectures may make mapping
501 * decisions on a per-device or per-bus basis.
502 *
503 * Returns a negative error code on failure, zero on success.
504 */
505int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
506 enum pci_mmap_state mmap_state,
507 int write_combine)
508{
509 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
510 struct resource *rp;
511 int ret;
512
513 rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
514 if (rp == NULL)
515 return -EINVAL;
516
517 vma->vm_pgoff = offset >> PAGE_SHIFT;
518 vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
519 vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
520 vma->vm_page_prot,
521 mmap_state, write_combine);
522
523 ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
524 vma->vm_end - vma->vm_start, vma->vm_page_prot);
525
526 return ret;
527}
528
529#ifdef CONFIG_PPC_MULTIPLATFORM
Yani Ioannouff381d22005-05-17 06:40:51 -0400530static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 struct pci_dev *pdev;
533 struct device_node *np;
534
535 pdev = to_pci_dev (dev);
536 np = pci_device_to_OF_node(pdev);
537 if (np == NULL || np->full_name == NULL)
538 return 0;
539 return sprintf(buf, "%s", np->full_name);
540}
541static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
542#endif /* CONFIG_PPC_MULTIPLATFORM */
543
544void pcibios_add_platform_entries(struct pci_dev *pdev)
545{
546#ifdef CONFIG_PPC_MULTIPLATFORM
547 device_create_file(&pdev->dev, &dev_attr_devspec);
548#endif /* CONFIG_PPC_MULTIPLATFORM */
549}
550
551#ifdef CONFIG_PPC_MULTIPLATFORM
552
553#define ISA_SPACE_MASK 0x1
554#define ISA_SPACE_IO 0x1
555
556static void __devinit pci_process_ISA_OF_ranges(struct device_node *isa_node,
557 unsigned long phb_io_base_phys,
558 void __iomem * phb_io_base_virt)
559{
560 struct isa_range *range;
561 unsigned long pci_addr;
562 unsigned int isa_addr;
563 unsigned int size;
564 int rlen = 0;
565
566 range = (struct isa_range *) get_property(isa_node, "ranges", &rlen);
567 if (range == NULL || (rlen < sizeof(struct isa_range))) {
568 printk(KERN_ERR "no ISA ranges or unexpected isa range size,"
569 "mapping 64k\n");
Benjamin Herrenschmidtdfbacdc2005-04-16 15:24:33 -0700570 __ioremap_explicit(phb_io_base_phys,
571 (unsigned long)phb_io_base_virt,
572 0x10000, _PAGE_NO_CACHE | _PAGE_GUARDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return;
574 }
575
576 /* From "ISA Binding to 1275"
577 * The ranges property is laid out as an array of elements,
578 * each of which comprises:
579 * cells 0 - 1: an ISA address
580 * cells 2 - 4: a PCI address
581 * (size depending on dev->n_addr_cells)
582 * cell 5: the size of the range
583 */
584 if ((range->isa_addr.a_hi && ISA_SPACE_MASK) == ISA_SPACE_IO) {
585 isa_addr = range->isa_addr.a_lo;
586 pci_addr = (unsigned long) range->pci_addr.a_mid << 32 |
587 range->pci_addr.a_lo;
588
589 /* Assume these are both zero */
590 if ((pci_addr != 0) || (isa_addr != 0)) {
591 printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
592 __FUNCTION__);
593 return;
594 }
595
596 size = PAGE_ALIGN(range->size);
597
598 __ioremap_explicit(phb_io_base_phys,
599 (unsigned long) phb_io_base_virt,
Benjamin Herrenschmidtdfbacdc2005-04-16 15:24:33 -0700600 size, _PAGE_NO_CACHE | _PAGE_GUARDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602}
603
604void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
605 struct device_node *dev)
606{
607 unsigned int *ranges;
608 unsigned long size;
609 int rlen = 0;
610 int memno = 0;
611 struct resource *res;
612 int np, na = prom_n_addr_cells(dev);
613 unsigned long pci_addr, cpu_phys_addr;
614
615 np = na + 5;
616
617 /* From "PCI Binding to 1275"
618 * The ranges property is laid out as an array of elements,
619 * each of which comprises:
620 * cells 0 - 2: a PCI address
621 * cells 3 or 3+4: a CPU physical address
622 * (size depending on dev->n_addr_cells)
623 * cells 4+5 or 5+6: the size of the range
624 */
625 rlen = 0;
626 hose->io_base_phys = 0;
627 ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
628 while ((rlen -= np * sizeof(unsigned int)) >= 0) {
629 res = NULL;
630 pci_addr = (unsigned long)ranges[1] << 32 | ranges[2];
631
632 cpu_phys_addr = ranges[3];
633 if (na == 2)
634 cpu_phys_addr = cpu_phys_addr << 32 | ranges[4];
635
636 size = (unsigned long)ranges[na+3] << 32 | ranges[na+4];
637 if (size == 0)
638 continue;
639 switch ((ranges[0] >> 24) & 0x3) {
640 case 1: /* I/O space */
641 hose->io_base_phys = cpu_phys_addr;
642 hose->pci_io_size = size;
643
644 res = &hose->io_resource;
645 res->flags = IORESOURCE_IO;
646 res->start = pci_addr;
647 DBG("phb%d: IO 0x%lx -> 0x%lx\n", hose->global_number,
648 res->start, res->start + size - 1);
649 break;
650 case 2: /* memory space */
651 memno = 0;
652 while (memno < 3 && hose->mem_resources[memno].flags)
653 ++memno;
654
655 if (memno == 0)
656 hose->pci_mem_offset = cpu_phys_addr - pci_addr;
657 if (memno < 3) {
658 res = &hose->mem_resources[memno];
659 res->flags = IORESOURCE_MEM;
660 res->start = cpu_phys_addr;
661 DBG("phb%d: MEM 0x%lx -> 0x%lx\n", hose->global_number,
662 res->start, res->start + size - 1);
663 }
664 break;
665 }
666 if (res != NULL) {
667 res->name = dev->full_name;
668 res->end = res->start + size - 1;
669 res->parent = NULL;
670 res->sibling = NULL;
671 res->child = NULL;
672 }
673 ranges += np;
674 }
675}
676
677void __init pci_setup_phb_io(struct pci_controller *hose, int primary)
678{
679 unsigned long size = hose->pci_io_size;
680 unsigned long io_virt_offset;
681 struct resource *res;
682 struct device_node *isa_dn;
683
684 hose->io_base_virt = reserve_phb_iospace(size);
685 DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
686 hose->global_number, hose->io_base_phys,
687 (unsigned long) hose->io_base_virt);
688
689 if (primary) {
690 pci_io_base = (unsigned long)hose->io_base_virt;
691 isa_dn = of_find_node_by_type(NULL, "isa");
692 if (isa_dn) {
693 isa_io_base = pci_io_base;
694 pci_process_ISA_OF_ranges(isa_dn, hose->io_base_phys,
695 hose->io_base_virt);
696 of_node_put(isa_dn);
697 /* Allow all IO */
698 io_page_mask = -1;
699 }
700 }
701
702 io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
703 res = &hose->io_resource;
704 res->start += io_virt_offset;
705 res->end += io_virt_offset;
706}
707
708void __devinit pci_setup_phb_io_dynamic(struct pci_controller *hose,
709 int primary)
710{
711 unsigned long size = hose->pci_io_size;
712 unsigned long io_virt_offset;
713 struct resource *res;
714
715 hose->io_base_virt = __ioremap(hose->io_base_phys, size,
Benjamin Herrenschmidtdfbacdc2005-04-16 15:24:33 -0700716 _PAGE_NO_CACHE | _PAGE_GUARDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
718 hose->global_number, hose->io_base_phys,
719 (unsigned long) hose->io_base_virt);
720
721 if (primary)
722 pci_io_base = (unsigned long)hose->io_base_virt;
723
724 io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
725 res = &hose->io_resource;
726 res->start += io_virt_offset;
727 res->end += io_virt_offset;
728}
729
730
731static int get_bus_io_range(struct pci_bus *bus, unsigned long *start_phys,
732 unsigned long *start_virt, unsigned long *size)
733{
734 struct pci_controller *hose = pci_bus_to_host(bus);
735 struct pci_bus_region region;
736 struct resource *res;
737
738 if (bus->self) {
739 res = bus->resource[0];
740 pcibios_resource_to_bus(bus->self, &region, res);
741 *start_phys = hose->io_base_phys + region.start;
742 *start_virt = (unsigned long) hose->io_base_virt +
743 region.start;
744 if (region.end > region.start)
745 *size = region.end - region.start + 1;
746 else {
747 printk("%s(): unexpected region 0x%lx->0x%lx\n",
748 __FUNCTION__, region.start, region.end);
749 return 1;
750 }
751
752 } else {
753 /* Root Bus */
754 res = &hose->io_resource;
755 *start_phys = hose->io_base_phys;
756 *start_virt = (unsigned long) hose->io_base_virt;
757 if (res->end > res->start)
758 *size = res->end - res->start + 1;
759 else {
760 printk("%s(): unexpected region 0x%lx->0x%lx\n",
761 __FUNCTION__, res->start, res->end);
762 return 1;
763 }
764 }
765
766 return 0;
767}
768
769int unmap_bus_range(struct pci_bus *bus)
770{
771 unsigned long start_phys;
772 unsigned long start_virt;
773 unsigned long size;
774
775 if (!bus) {
776 printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
777 return 1;
778 }
779
780 if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
781 return 1;
782 if (iounmap_explicit((void __iomem *) start_virt, size))
783 return 1;
784
785 return 0;
786}
787EXPORT_SYMBOL(unmap_bus_range);
788
789int remap_bus_range(struct pci_bus *bus)
790{
791 unsigned long start_phys;
792 unsigned long start_virt;
793 unsigned long size;
794
795 if (!bus) {
796 printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
797 return 1;
798 }
799
800
801 if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
802 return 1;
803 printk("mapping IO %lx -> %lx, size: %lx\n", start_phys, start_virt, size);
Benjamin Herrenschmidtdfbacdc2005-04-16 15:24:33 -0700804 if (__ioremap_explicit(start_phys, start_virt, size,
805 _PAGE_NO_CACHE | _PAGE_GUARDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return 1;
807
808 return 0;
809}
810EXPORT_SYMBOL(remap_bus_range);
811
812void phbs_remap_io(void)
813{
814 struct pci_controller *hose, *tmp;
815
816 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
817 remap_bus_range(hose->bus);
818}
819
820/*
821 * ppc64 can have multifunction devices that do not respond to function 0.
822 * In this case we must scan all functions.
823 */
824int pcibios_scan_all_fns(struct pci_bus *bus, int devfn)
825{
826 struct device_node *busdn, *dn;
827
828 if (bus->self)
829 busdn = pci_device_to_OF_node(bus->self);
830 else
831 busdn = bus->sysdata; /* must be a phb */
832
833 if (busdn == NULL)
834 return 0;
835
836 /*
837 * Check to see if there is any of the 8 functions are in the
838 * device tree. If they are then we need to scan all the
839 * functions of this slot.
840 */
Paul Mackerras16353172005-09-06 13:17:54 +1000841 for (dn = busdn->child; dn; dn = dn->sibling) {
842 struct pci_dn *pdn = dn->data;
843 if (pdn && (pdn->devfn >> 3) == (devfn >> 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return 1;
Paul Mackerras16353172005-09-06 13:17:54 +1000845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 return 0;
848}
849
850
851void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
852 struct pci_bus *bus)
853{
854 /* Update device resources. */
855 struct pci_controller *hose = pci_bus_to_host(bus);
856 int i;
857
858 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
859 if (dev->resource[i].flags & IORESOURCE_IO) {
860 unsigned long offset = (unsigned long)hose->io_base_virt
861 - pci_io_base;
862 unsigned long start, end, mask;
863
864 start = dev->resource[i].start += offset;
865 end = dev->resource[i].end += offset;
866
867 /* Need to allow IO access to pages that are in the
868 ISA range */
869 if (start < MAX_ISA_PORT) {
870 if (end > MAX_ISA_PORT)
871 end = MAX_ISA_PORT;
872
873 start >>= PAGE_SHIFT;
874 end >>= PAGE_SHIFT;
875
876 /* get the range of pages for the map */
877 mask = ((1 << (end+1))-1) ^ ((1 << start)-1);
878 io_page_mask |= mask;
879 }
880 }
881 else if (dev->resource[i].flags & IORESOURCE_MEM) {
882 dev->resource[i].start += hose->pci_mem_offset;
883 dev->resource[i].end += hose->pci_mem_offset;
884 }
885 }
886}
887EXPORT_SYMBOL(pcibios_fixup_device_resources);
888
889void __devinit pcibios_fixup_bus(struct pci_bus *bus)
890{
891 struct pci_controller *hose = pci_bus_to_host(bus);
892 struct pci_dev *dev = bus->self;
893 struct resource *res;
894 int i;
895
896 if (!dev) {
897 /* Root bus. */
898
899 hose->bus = bus;
900 bus->resource[0] = res = &hose->io_resource;
901
902 if (res->flags && request_resource(&ioport_resource, res))
903 printk(KERN_ERR "Failed to request IO on "
904 "PCI domain %d\n", pci_domain_nr(bus));
905
906 for (i = 0; i < 3; ++i) {
907 res = &hose->mem_resources[i];
908 bus->resource[i+1] = res;
909 if (res->flags && request_resource(&iomem_resource, res))
910 printk(KERN_ERR "Failed to request MEM on "
911 "PCI domain %d\n",
912 pci_domain_nr(bus));
913 }
914 } else if (pci_probe_only &&
915 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
916 /* This is a subordinate bridge */
917
918 pci_read_bridge_bases(bus);
919 pcibios_fixup_device_resources(dev, bus);
920 }
921
922 ppc_md.iommu_bus_setup(bus);
923
924 list_for_each_entry(dev, &bus->devices, bus_list)
925 ppc_md.iommu_dev_setup(dev);
926
John Rosedad32bb2005-06-23 17:09:54 +1000927 if (ppc_md.irq_bus_setup)
928 ppc_md.irq_bus_setup(bus);
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (!pci_probe_only)
931 return;
932
933 list_for_each_entry(dev, &bus->devices, bus_list) {
934 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
935 pcibios_fixup_device_resources(dev, bus);
936 }
937}
938EXPORT_SYMBOL(pcibios_fixup_bus);
939
940/*
941 * Reads the interrupt pin to determine if interrupt is use by card.
942 * If the interrupt is used, then gets the interrupt line from the
943 * openfirmware and sets it in the pci_dev and pci_config line.
944 */
945int pci_read_irq_line(struct pci_dev *pci_dev)
946{
947 u8 intpin;
948 struct device_node *node;
949
950 pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &intpin);
951 if (intpin == 0)
952 return 0;
953
954 node = pci_device_to_OF_node(pci_dev);
955 if (node == NULL)
956 return -1;
957
958 if (node->n_intrs == 0)
959 return -1;
960
961 pci_dev->irq = node->intrs[0].line;
962
963 pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, pci_dev->irq);
964
965 return 0;
966}
967EXPORT_SYMBOL(pci_read_irq_line);
968
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000969void pci_resource_to_user(const struct pci_dev *dev, int bar,
970 const struct resource *rsrc,
971 u64 *start, u64 *end)
972{
973 struct pci_controller *hose = pci_bus_to_host(dev->bus);
974 unsigned long offset = 0;
975
976 if (hose == NULL)
977 return;
978
979 if (rsrc->flags & IORESOURCE_IO)
980 offset = pci_io_base - (unsigned long)hose->io_base_virt +
981 hose->io_base_phys;
982
983 *start = rsrc->start + offset;
984 *end = rsrc->end + offset;
985}
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987#endif /* CONFIG_PPC_MULTIPLATFORM */
Paul Mackerrasb2ad7b52005-09-09 23:02:36 +1000988
989
990#define IOBASE_BRIDGE_NUMBER 0
991#define IOBASE_MEMORY 1
992#define IOBASE_IO 2
993#define IOBASE_ISA_IO 3
994#define IOBASE_ISA_MEM 4
995
996long sys_pciconfig_iobase(long which, unsigned long in_bus,
997 unsigned long in_devfn)
998{
999 struct pci_controller* hose;
1000 struct list_head *ln;
1001 struct pci_bus *bus = NULL;
1002 struct device_node *hose_node;
1003
1004 /* Argh ! Please forgive me for that hack, but that's the
1005 * simplest way to get existing XFree to not lockup on some
1006 * G5 machines... So when something asks for bus 0 io base
1007 * (bus 0 is HT root), we return the AGP one instead.
1008 */
1009#ifdef CONFIG_PPC_PMAC
1010 if (systemcfg->platform == PLATFORM_POWERMAC &&
1011 machine_is_compatible("MacRISC4"))
1012 if (in_bus == 0)
1013 in_bus = 0xf0;
1014#endif /* CONFIG_PPC_PMAC */
1015
1016 /* That syscall isn't quite compatible with PCI domains, but it's
1017 * used on pre-domains setup. We return the first match
1018 */
1019
1020 for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
1021 bus = pci_bus_b(ln);
1022 if (in_bus >= bus->number && in_bus < (bus->number + bus->subordinate))
1023 break;
1024 bus = NULL;
1025 }
1026 if (bus == NULL || bus->sysdata == NULL)
1027 return -ENODEV;
1028
1029 hose_node = (struct device_node *)bus->sysdata;
1030 hose = PCI_DN(hose_node)->phb;
1031
1032 switch (which) {
1033 case IOBASE_BRIDGE_NUMBER:
1034 return (long)hose->first_busno;
1035 case IOBASE_MEMORY:
1036 return (long)hose->pci_mem_offset;
1037 case IOBASE_IO:
1038 return (long)hose->io_base_phys;
1039 case IOBASE_ISA_IO:
1040 return (long)isa_io_base;
1041 case IOBASE_ISA_MEM:
1042 return -EINVAL;
1043 }
1044
1045 return -EOPNOTSUPP;
1046}