[POWERPC] Disable PCI IO/Mem on a device when resources can't be allocated
[deliverable/linux.git] / arch / powerpc / kernel / pci-common.c
1 /*
2 * Contains common pci routines for ALL ppc platform
3 * (based on pci_32.c and pci_64.c)
4 *
5 * Port for PPC64 David Engebretsen, IBM Corp.
6 * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
7 *
8 * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
9 * Rework, based on alpha PCI code.
10 *
11 * Common pmac/prep/chrp pci routines. -- Cort
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
17 */
18
19 #undef DEBUG
20
21 #include <linux/kernel.h>
22 #include <linux/pci.h>
23 #include <linux/string.h>
24 #include <linux/init.h>
25 #include <linux/bootmem.h>
26 #include <linux/mm.h>
27 #include <linux/list.h>
28 #include <linux/syscalls.h>
29 #include <linux/irq.h>
30 #include <linux/vmalloc.h>
31
32 #include <asm/processor.h>
33 #include <asm/io.h>
34 #include <asm/prom.h>
35 #include <asm/pci-bridge.h>
36 #include <asm/byteorder.h>
37 #include <asm/machdep.h>
38 #include <asm/ppc-pci.h>
39 #include <asm/firmware.h>
40
41 #ifdef DEBUG
42 #include <asm/udbg.h>
43 #define DBG(fmt...) printk(fmt)
44 #else
45 #define DBG(fmt...)
46 #endif
47
48 static DEFINE_SPINLOCK(hose_spinlock);
49
50 /* XXX kill that some day ... */
51 static int global_phb_number; /* Global phb counter */
52
53 /* ISA Memory physical address */
54 resource_size_t isa_mem_base;
55
56 /* Default PCI flags is 0 */
57 unsigned int ppc_pci_flags;
58
59 struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
60 {
61 struct pci_controller *phb;
62
63 phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
64 if (phb == NULL)
65 return NULL;
66 spin_lock(&hose_spinlock);
67 phb->global_number = global_phb_number++;
68 list_add_tail(&phb->list_node, &hose_list);
69 spin_unlock(&hose_spinlock);
70 phb->dn = dev;
71 phb->is_dynamic = mem_init_done;
72 #ifdef CONFIG_PPC64
73 if (dev) {
74 int nid = of_node_to_nid(dev);
75
76 if (nid < 0 || !node_online(nid))
77 nid = -1;
78
79 PHB_SET_NODE(phb, nid);
80 }
81 #endif
82 return phb;
83 }
84
85 void pcibios_free_controller(struct pci_controller *phb)
86 {
87 spin_lock(&hose_spinlock);
88 list_del(&phb->list_node);
89 spin_unlock(&hose_spinlock);
90
91 if (phb->is_dynamic)
92 kfree(phb);
93 }
94
95 int pcibios_vaddr_is_ioport(void __iomem *address)
96 {
97 int ret = 0;
98 struct pci_controller *hose;
99 unsigned long size;
100
101 spin_lock(&hose_spinlock);
102 list_for_each_entry(hose, &hose_list, list_node) {
103 #ifdef CONFIG_PPC64
104 size = hose->pci_io_size;
105 #else
106 size = hose->io_resource.end - hose->io_resource.start + 1;
107 #endif
108 if (address >= hose->io_base_virt &&
109 address < (hose->io_base_virt + size)) {
110 ret = 1;
111 break;
112 }
113 }
114 spin_unlock(&hose_spinlock);
115 return ret;
116 }
117
118 /*
119 * Return the domain number for this bus.
120 */
121 int pci_domain_nr(struct pci_bus *bus)
122 {
123 struct pci_controller *hose = pci_bus_to_host(bus);
124
125 return hose->global_number;
126 }
127 EXPORT_SYMBOL(pci_domain_nr);
128
129 #ifdef CONFIG_PPC_OF
130
131 /* This routine is meant to be used early during boot, when the
132 * PCI bus numbers have not yet been assigned, and you need to
133 * issue PCI config cycles to an OF device.
134 * It could also be used to "fix" RTAS config cycles if you want
135 * to set pci_assign_all_buses to 1 and still use RTAS for PCI
136 * config cycles.
137 */
138 struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
139 {
140 if (!have_of)
141 return NULL;
142 while(node) {
143 struct pci_controller *hose, *tmp;
144 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
145 if (hose->dn == node)
146 return hose;
147 node = node->parent;
148 }
149 return NULL;
150 }
151
152 static ssize_t pci_show_devspec(struct device *dev,
153 struct device_attribute *attr, char *buf)
154 {
155 struct pci_dev *pdev;
156 struct device_node *np;
157
158 pdev = to_pci_dev (dev);
159 np = pci_device_to_OF_node(pdev);
160 if (np == NULL || np->full_name == NULL)
161 return 0;
162 return sprintf(buf, "%s", np->full_name);
163 }
164 static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
165 #endif /* CONFIG_PPC_OF */
166
167 /* Add sysfs properties */
168 int pcibios_add_platform_entries(struct pci_dev *pdev)
169 {
170 #ifdef CONFIG_PPC_OF
171 return device_create_file(&pdev->dev, &dev_attr_devspec);
172 #else
173 return 0;
174 #endif /* CONFIG_PPC_OF */
175
176 }
177
178 char __devinit *pcibios_setup(char *str)
179 {
180 return str;
181 }
182
183 /*
184 * Reads the interrupt pin to determine if interrupt is use by card.
185 * If the interrupt is used, then gets the interrupt line from the
186 * openfirmware and sets it in the pci_dev and pci_config line.
187 */
188 int pci_read_irq_line(struct pci_dev *pci_dev)
189 {
190 struct of_irq oirq;
191 unsigned int virq;
192
193 /* The current device-tree that iSeries generates from the HV
194 * PCI informations doesn't contain proper interrupt routing,
195 * and all the fallback would do is print out crap, so we
196 * don't attempt to resolve the interrupts here at all, some
197 * iSeries specific fixup does it.
198 *
199 * In the long run, we will hopefully fix the generated device-tree
200 * instead.
201 */
202 #ifdef CONFIG_PPC_ISERIES
203 if (firmware_has_feature(FW_FEATURE_ISERIES))
204 return -1;
205 #endif
206
207 DBG("Try to map irq for %s...\n", pci_name(pci_dev));
208
209 #ifdef DEBUG
210 memset(&oirq, 0xff, sizeof(oirq));
211 #endif
212 /* Try to get a mapping from the device-tree */
213 if (of_irq_map_pci(pci_dev, &oirq)) {
214 u8 line, pin;
215
216 /* If that fails, lets fallback to what is in the config
217 * space and map that through the default controller. We
218 * also set the type to level low since that's what PCI
219 * interrupts are. If your platform does differently, then
220 * either provide a proper interrupt tree or don't use this
221 * function.
222 */
223 if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
224 return -1;
225 if (pin == 0)
226 return -1;
227 if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
228 line == 0xff) {
229 return -1;
230 }
231 DBG(" -> no map ! Using irq line %d from PCI config\n", line);
232
233 virq = irq_create_mapping(NULL, line);
234 if (virq != NO_IRQ)
235 set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
236 } else {
237 DBG(" -> got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
238 oirq.size, oirq.specifier[0], oirq.specifier[1],
239 oirq.controller->full_name);
240
241 virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
242 oirq.size);
243 }
244 if(virq == NO_IRQ) {
245 DBG(" -> failed to map !\n");
246 return -1;
247 }
248
249 DBG(" -> mapped to linux irq %d\n", virq);
250
251 pci_dev->irq = virq;
252
253 return 0;
254 }
255 EXPORT_SYMBOL(pci_read_irq_line);
256
257 /*
258 * Platform support for /proc/bus/pci/X/Y mmap()s,
259 * modelled on the sparc64 implementation by Dave Miller.
260 * -- paulus.
261 */
262
263 /*
264 * Adjust vm_pgoff of VMA such that it is the physical page offset
265 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
266 *
267 * Basically, the user finds the base address for his device which he wishes
268 * to mmap. They read the 32-bit value from the config space base register,
269 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
270 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
271 *
272 * Returns negative error code on failure, zero on success.
273 */
274 static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
275 resource_size_t *offset,
276 enum pci_mmap_state mmap_state)
277 {
278 struct pci_controller *hose = pci_bus_to_host(dev->bus);
279 unsigned long io_offset = 0;
280 int i, res_bit;
281
282 if (hose == 0)
283 return NULL; /* should never happen */
284
285 /* If memory, add on the PCI bridge address offset */
286 if (mmap_state == pci_mmap_mem) {
287 #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
288 *offset += hose->pci_mem_offset;
289 #endif
290 res_bit = IORESOURCE_MEM;
291 } else {
292 io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
293 *offset += io_offset;
294 res_bit = IORESOURCE_IO;
295 }
296
297 /*
298 * Check that the offset requested corresponds to one of the
299 * resources of the device.
300 */
301 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
302 struct resource *rp = &dev->resource[i];
303 int flags = rp->flags;
304
305 /* treat ROM as memory (should be already) */
306 if (i == PCI_ROM_RESOURCE)
307 flags |= IORESOURCE_MEM;
308
309 /* Active and same type? */
310 if ((flags & res_bit) == 0)
311 continue;
312
313 /* In the range of this resource? */
314 if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
315 continue;
316
317 /* found it! construct the final physical address */
318 if (mmap_state == pci_mmap_io)
319 *offset += hose->io_base_phys - io_offset;
320 return rp;
321 }
322
323 return NULL;
324 }
325
326 /*
327 * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
328 * device mapping.
329 */
330 static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
331 pgprot_t protection,
332 enum pci_mmap_state mmap_state,
333 int write_combine)
334 {
335 unsigned long prot = pgprot_val(protection);
336
337 /* Write combine is always 0 on non-memory space mappings. On
338 * memory space, if the user didn't pass 1, we check for a
339 * "prefetchable" resource. This is a bit hackish, but we use
340 * this to workaround the inability of /sysfs to provide a write
341 * combine bit
342 */
343 if (mmap_state != pci_mmap_mem)
344 write_combine = 0;
345 else if (write_combine == 0) {
346 if (rp->flags & IORESOURCE_PREFETCH)
347 write_combine = 1;
348 }
349
350 /* XXX would be nice to have a way to ask for write-through */
351 prot |= _PAGE_NO_CACHE;
352 if (write_combine)
353 prot &= ~_PAGE_GUARDED;
354 else
355 prot |= _PAGE_GUARDED;
356
357 return __pgprot(prot);
358 }
359
360 /*
361 * This one is used by /dev/mem and fbdev who have no clue about the
362 * PCI device, it tries to find the PCI device first and calls the
363 * above routine
364 */
365 pgprot_t pci_phys_mem_access_prot(struct file *file,
366 unsigned long pfn,
367 unsigned long size,
368 pgprot_t protection)
369 {
370 struct pci_dev *pdev = NULL;
371 struct resource *found = NULL;
372 unsigned long prot = pgprot_val(protection);
373 unsigned long offset = pfn << PAGE_SHIFT;
374 int i;
375
376 if (page_is_ram(pfn))
377 return __pgprot(prot);
378
379 prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
380
381 for_each_pci_dev(pdev) {
382 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
383 struct resource *rp = &pdev->resource[i];
384 int flags = rp->flags;
385
386 /* Active and same type? */
387 if ((flags & IORESOURCE_MEM) == 0)
388 continue;
389 /* In the range of this resource? */
390 if (offset < (rp->start & PAGE_MASK) ||
391 offset > rp->end)
392 continue;
393 found = rp;
394 break;
395 }
396 if (found)
397 break;
398 }
399 if (found) {
400 if (found->flags & IORESOURCE_PREFETCH)
401 prot &= ~_PAGE_GUARDED;
402 pci_dev_put(pdev);
403 }
404
405 DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
406
407 return __pgprot(prot);
408 }
409
410
411 /*
412 * Perform the actual remap of the pages for a PCI device mapping, as
413 * appropriate for this architecture. The region in the process to map
414 * is described by vm_start and vm_end members of VMA, the base physical
415 * address is found in vm_pgoff.
416 * The pci device structure is provided so that architectures may make mapping
417 * decisions on a per-device or per-bus basis.
418 *
419 * Returns a negative error code on failure, zero on success.
420 */
421 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
422 enum pci_mmap_state mmap_state, int write_combine)
423 {
424 resource_size_t offset = vma->vm_pgoff << PAGE_SHIFT;
425 struct resource *rp;
426 int ret;
427
428 rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
429 if (rp == NULL)
430 return -EINVAL;
431
432 vma->vm_pgoff = offset >> PAGE_SHIFT;
433 vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
434 vma->vm_page_prot,
435 mmap_state, write_combine);
436
437 ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
438 vma->vm_end - vma->vm_start, vma->vm_page_prot);
439
440 return ret;
441 }
442
443 void pci_resource_to_user(const struct pci_dev *dev, int bar,
444 const struct resource *rsrc,
445 resource_size_t *start, resource_size_t *end)
446 {
447 struct pci_controller *hose = pci_bus_to_host(dev->bus);
448 resource_size_t offset = 0;
449
450 if (hose == NULL)
451 return;
452
453 if (rsrc->flags & IORESOURCE_IO)
454 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
455
456 /* We pass a fully fixed up address to userland for MMIO instead of
457 * a BAR value because X is lame and expects to be able to use that
458 * to pass to /dev/mem !
459 *
460 * That means that we'll have potentially 64 bits values where some
461 * userland apps only expect 32 (like X itself since it thinks only
462 * Sparc has 64 bits MMIO) but if we don't do that, we break it on
463 * 32 bits CHRPs :-(
464 *
465 * Hopefully, the sysfs insterface is immune to that gunk. Once X
466 * has been fixed (and the fix spread enough), we can re-enable the
467 * 2 lines below and pass down a BAR value to userland. In that case
468 * we'll also have to re-enable the matching code in
469 * __pci_mmap_make_offset().
470 *
471 * BenH.
472 */
473 #if 0
474 else if (rsrc->flags & IORESOURCE_MEM)
475 offset = hose->pci_mem_offset;
476 #endif
477
478 *start = rsrc->start - offset;
479 *end = rsrc->end - offset;
480 }
481
482 /**
483 * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
484 * @hose: newly allocated pci_controller to be setup
485 * @dev: device node of the host bridge
486 * @primary: set if primary bus (32 bits only, soon to be deprecated)
487 *
488 * This function will parse the "ranges" property of a PCI host bridge device
489 * node and setup the resource mapping of a pci controller based on its
490 * content.
491 *
492 * Life would be boring if it wasn't for a few issues that we have to deal
493 * with here:
494 *
495 * - We can only cope with one IO space range and up to 3 Memory space
496 * ranges. However, some machines (thanks Apple !) tend to split their
497 * space into lots of small contiguous ranges. So we have to coalesce.
498 *
499 * - We can only cope with all memory ranges having the same offset
500 * between CPU addresses and PCI addresses. Unfortunately, some bridges
501 * are setup for a large 1:1 mapping along with a small "window" which
502 * maps PCI address 0 to some arbitrary high address of the CPU space in
503 * order to give access to the ISA memory hole.
504 * The way out of here that I've chosen for now is to always set the
505 * offset based on the first resource found, then override it if we
506 * have a different offset and the previous was set by an ISA hole.
507 *
508 * - Some busses have IO space not starting at 0, which causes trouble with
509 * the way we do our IO resource renumbering. The code somewhat deals with
510 * it for 64 bits but I would expect problems on 32 bits.
511 *
512 * - Some 32 bits platforms such as 4xx can have physical space larger than
513 * 32 bits so we need to use 64 bits values for the parsing
514 */
515 void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
516 struct device_node *dev,
517 int primary)
518 {
519 const u32 *ranges;
520 int rlen;
521 int pna = of_n_addr_cells(dev);
522 int np = pna + 5;
523 int memno = 0, isa_hole = -1;
524 u32 pci_space;
525 unsigned long long pci_addr, cpu_addr, pci_next, cpu_next, size;
526 unsigned long long isa_mb = 0;
527 struct resource *res;
528
529 printk(KERN_INFO "PCI host bridge %s %s ranges:\n",
530 dev->full_name, primary ? "(primary)" : "");
531
532 /* Get ranges property */
533 ranges = of_get_property(dev, "ranges", &rlen);
534 if (ranges == NULL)
535 return;
536
537 /* Parse it */
538 while ((rlen -= np * 4) >= 0) {
539 /* Read next ranges element */
540 pci_space = ranges[0];
541 pci_addr = of_read_number(ranges + 1, 2);
542 cpu_addr = of_translate_address(dev, ranges + 3);
543 size = of_read_number(ranges + pna + 3, 2);
544 ranges += np;
545 if (cpu_addr == OF_BAD_ADDR || size == 0)
546 continue;
547
548 /* Now consume following elements while they are contiguous */
549 for (; rlen >= np * sizeof(u32);
550 ranges += np, rlen -= np * 4) {
551 if (ranges[0] != pci_space)
552 break;
553 pci_next = of_read_number(ranges + 1, 2);
554 cpu_next = of_translate_address(dev, ranges + 3);
555 if (pci_next != pci_addr + size ||
556 cpu_next != cpu_addr + size)
557 break;
558 size += of_read_number(ranges + pna + 3, 2);
559 }
560
561 /* Act based on address space type */
562 res = NULL;
563 switch ((pci_space >> 24) & 0x3) {
564 case 1: /* PCI IO space */
565 printk(KERN_INFO
566 " IO 0x%016llx..0x%016llx -> 0x%016llx\n",
567 cpu_addr, cpu_addr + size - 1, pci_addr);
568
569 /* We support only one IO range */
570 if (hose->pci_io_size) {
571 printk(KERN_INFO
572 " \\--> Skipped (too many) !\n");
573 continue;
574 }
575 #ifdef CONFIG_PPC32
576 /* On 32 bits, limit I/O space to 16MB */
577 if (size > 0x01000000)
578 size = 0x01000000;
579
580 /* 32 bits needs to map IOs here */
581 hose->io_base_virt = ioremap(cpu_addr, size);
582
583 /* Expect trouble if pci_addr is not 0 */
584 if (primary)
585 isa_io_base =
586 (unsigned long)hose->io_base_virt;
587 #endif /* CONFIG_PPC32 */
588 /* pci_io_size and io_base_phys always represent IO
589 * space starting at 0 so we factor in pci_addr
590 */
591 hose->pci_io_size = pci_addr + size;
592 hose->io_base_phys = cpu_addr - pci_addr;
593
594 /* Build resource */
595 res = &hose->io_resource;
596 res->flags = IORESOURCE_IO;
597 res->start = pci_addr;
598 break;
599 case 2: /* PCI Memory space */
600 printk(KERN_INFO
601 " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
602 cpu_addr, cpu_addr + size - 1, pci_addr,
603 (pci_space & 0x40000000) ? "Prefetch" : "");
604
605 /* We support only 3 memory ranges */
606 if (memno >= 3) {
607 printk(KERN_INFO
608 " \\--> Skipped (too many) !\n");
609 continue;
610 }
611 /* Handles ISA memory hole space here */
612 if (pci_addr == 0) {
613 isa_mb = cpu_addr;
614 isa_hole = memno;
615 if (primary || isa_mem_base == 0)
616 isa_mem_base = cpu_addr;
617 }
618
619 /* We get the PCI/Mem offset from the first range or
620 * the, current one if the offset came from an ISA
621 * hole. If they don't match, bugger.
622 */
623 if (memno == 0 ||
624 (isa_hole >= 0 && pci_addr != 0 &&
625 hose->pci_mem_offset == isa_mb))
626 hose->pci_mem_offset = cpu_addr - pci_addr;
627 else if (pci_addr != 0 &&
628 hose->pci_mem_offset != cpu_addr - pci_addr) {
629 printk(KERN_INFO
630 " \\--> Skipped (offset mismatch) !\n");
631 continue;
632 }
633
634 /* Build resource */
635 res = &hose->mem_resources[memno++];
636 res->flags = IORESOURCE_MEM;
637 if (pci_space & 0x40000000)
638 res->flags |= IORESOURCE_PREFETCH;
639 res->start = cpu_addr;
640 break;
641 }
642 if (res != NULL) {
643 res->name = dev->full_name;
644 res->end = res->start + size - 1;
645 res->parent = NULL;
646 res->sibling = NULL;
647 res->child = NULL;
648 }
649 }
650
651 /* Out of paranoia, let's put the ISA hole last if any */
652 if (isa_hole >= 0 && memno > 0 && isa_hole != (memno-1)) {
653 struct resource tmp = hose->mem_resources[isa_hole];
654 hose->mem_resources[isa_hole] = hose->mem_resources[memno-1];
655 hose->mem_resources[memno-1] = tmp;
656 }
657 }
658
659 /* Decide whether to display the domain number in /proc */
660 int pci_proc_domain(struct pci_bus *bus)
661 {
662 struct pci_controller *hose = pci_bus_to_host(bus);
663 #ifdef CONFIG_PPC64
664 return hose->buid != 0;
665 #else
666 if (!(ppc_pci_flags & PPC_PCI_ENABLE_PROC_DOMAINS))
667 return 0;
668 if (ppc_pci_flags & PPC_PCI_COMPAT_DOMAIN_0)
669 return hose->global_number != 0;
670 return 1;
671 #endif
672 }
673
674 void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
675 struct resource *res)
676 {
677 resource_size_t offset = 0, mask = (resource_size_t)-1;
678 struct pci_controller *hose = pci_bus_to_host(dev->bus);
679
680 if (!hose)
681 return;
682 if (res->flags & IORESOURCE_IO) {
683 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
684 mask = 0xffffffffu;
685 } else if (res->flags & IORESOURCE_MEM)
686 offset = hose->pci_mem_offset;
687
688 region->start = (res->start - offset) & mask;
689 region->end = (res->end - offset) & mask;
690 }
691 EXPORT_SYMBOL(pcibios_resource_to_bus);
692
693 void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
694 struct pci_bus_region *region)
695 {
696 resource_size_t offset = 0, mask = (resource_size_t)-1;
697 struct pci_controller *hose = pci_bus_to_host(dev->bus);
698
699 if (!hose)
700 return;
701 if (res->flags & IORESOURCE_IO) {
702 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
703 mask = 0xffffffffu;
704 } else if (res->flags & IORESOURCE_MEM)
705 offset = hose->pci_mem_offset;
706 res->start = (region->start + offset) & mask;
707 res->end = (region->end + offset) & mask;
708 }
709 EXPORT_SYMBOL(pcibios_bus_to_resource);
710
711 /* Fixup a bus resource into a linux resource */
712 static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
713 {
714 struct pci_controller *hose = pci_bus_to_host(dev->bus);
715 resource_size_t offset = 0, mask = (resource_size_t)-1;
716
717 if (res->flags & IORESOURCE_IO) {
718 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
719 mask = 0xffffffffu;
720 } else if (res->flags & IORESOURCE_MEM)
721 offset = hose->pci_mem_offset;
722
723 res->start = (res->start + offset) & mask;
724 res->end = (res->end + offset) & mask;
725
726 pr_debug("PCI:%s %016llx-%016llx\n",
727 pci_name(dev),
728 (unsigned long long)res->start,
729 (unsigned long long)res->end);
730 }
731
732
733 /* This header fixup will do the resource fixup for all devices as they are
734 * probed, but not for bridge ranges
735 */
736 static void __devinit pcibios_fixup_resources(struct pci_dev *dev)
737 {
738 struct pci_controller *hose = pci_bus_to_host(dev->bus);
739 int i;
740
741 if (!hose) {
742 printk(KERN_ERR "No host bridge for PCI dev %s !\n",
743 pci_name(dev));
744 return;
745 }
746 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
747 struct resource *res = dev->resource + i;
748 if (!res->flags)
749 continue;
750 if (res->end == 0xffffffff) {
751 pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] is unassigned\n",
752 pci_name(dev), i,
753 (unsigned long long)res->start,
754 (unsigned long long)res->end,
755 (unsigned int)res->flags);
756 res->end -= res->start;
757 res->start = 0;
758 res->flags |= IORESOURCE_UNSET;
759 continue;
760 }
761
762 pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] fixup...\n",
763 pci_name(dev), i,
764 (unsigned long long)res->start,\
765 (unsigned long long)res->end,
766 (unsigned int)res->flags);
767
768 fixup_resource(res, dev);
769 }
770
771 /* Call machine specific resource fixup */
772 if (ppc_md.pcibios_fixup_resources)
773 ppc_md.pcibios_fixup_resources(dev);
774 }
775 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
776
777 static void __devinit __pcibios_fixup_bus(struct pci_bus *bus)
778 {
779 struct pci_controller *hose = pci_bus_to_host(bus);
780 struct pci_dev *dev = bus->self;
781
782 pr_debug("PCI: Fixup bus %d (%s)\n", bus->number, dev ? pci_name(dev) : "PHB");
783
784 /* Fixup PCI<->PCI bridges. Host bridges are handled separately, for
785 * now differently between 32 and 64 bits.
786 */
787 if (dev != NULL) {
788 struct resource *res;
789 int i;
790
791 for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
792 if ((res = bus->resource[i]) == NULL)
793 continue;
794 if (!res->flags || bus->self->transparent)
795 continue;
796
797 /* On PowerMac, Apple leaves bridge windows open over
798 * an inaccessible region of memory space (0...fffff)
799 * which is somewhat bogus, but that's what they think
800 * means disabled...
801 *
802 * We clear those to force them to be reallocated later
803 *
804 * We detect such regions by the fact that the base is
805 * equal to the pci_mem_offset of the host bridge and
806 * their size is smaller than 1M.
807 */
808 if (res->start == hose->pci_mem_offset &&
809 res->end < 0x100000) {
810 printk(KERN_INFO
811 "PCI: Closing bogus Apple Firmware"
812 " region %d on bus 0x%02x\n",
813 i, bus->number);
814 res->flags = 0;
815 continue;
816 }
817
818 pr_debug("PCI:%s Bus rsrc %d %016llx-%016llx [%x] fixup...\n",
819 pci_name(dev), i,
820 (unsigned long long)res->start,\
821 (unsigned long long)res->end,
822 (unsigned int)res->flags);
823
824 fixup_resource(res, dev);
825 }
826 }
827
828 /* Additional setup that is different between 32 and 64 bits for now */
829 pcibios_do_bus_setup(bus);
830
831 /* Platform specific bus fixups */
832 if (ppc_md.pcibios_fixup_bus)
833 ppc_md.pcibios_fixup_bus(bus);
834
835 /* Read default IRQs and fixup if necessary */
836 list_for_each_entry(dev, &bus->devices, bus_list) {
837 pci_read_irq_line(dev);
838 if (ppc_md.pci_irq_fixup)
839 ppc_md.pci_irq_fixup(dev);
840 }
841 }
842
843 void __devinit pcibios_fixup_bus(struct pci_bus *bus)
844 {
845 /* When called from the generic PCI probe, read PCI<->PCI bridge
846 * bases before proceeding
847 */
848 if (bus->self != NULL)
849 pci_read_bridge_bases(bus);
850 __pcibios_fixup_bus(bus);
851 }
852 EXPORT_SYMBOL(pcibios_fixup_bus);
853
854 /* When building a bus from the OF tree rather than probing, we need a
855 * slightly different version of the fixup which doesn't read the
856 * bridge bases using config space accesses
857 */
858 void __devinit pcibios_fixup_of_probed_bus(struct pci_bus *bus)
859 {
860 __pcibios_fixup_bus(bus);
861 }
862
863 static int skip_isa_ioresource_align(struct pci_dev *dev)
864 {
865 if ((ppc_pci_flags & PPC_PCI_CAN_SKIP_ISA_ALIGN) &&
866 !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
867 return 1;
868 return 0;
869 }
870
871 /*
872 * We need to avoid collisions with `mirrored' VGA ports
873 * and other strange ISA hardware, so we always want the
874 * addresses to be allocated in the 0x000-0x0ff region
875 * modulo 0x400.
876 *
877 * Why? Because some silly external IO cards only decode
878 * the low 10 bits of the IO address. The 0x00-0xff region
879 * is reserved for motherboard devices that decode all 16
880 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
881 * but we want to try to avoid allocating at 0x2900-0x2bff
882 * which might have be mirrored at 0x0100-0x03ff..
883 */
884 void pcibios_align_resource(void *data, struct resource *res,
885 resource_size_t size, resource_size_t align)
886 {
887 struct pci_dev *dev = data;
888
889 if (res->flags & IORESOURCE_IO) {
890 resource_size_t start = res->start;
891
892 if (skip_isa_ioresource_align(dev))
893 return;
894 if (start & 0x300) {
895 start = (start + 0x3ff) & ~0x3ff;
896 res->start = start;
897 }
898 }
899 }
900 EXPORT_SYMBOL(pcibios_align_resource);
901
902 /*
903 * Reparent resource children of pr that conflict with res
904 * under res, and make res replace those children.
905 */
906 static int __init reparent_resources(struct resource *parent,
907 struct resource *res)
908 {
909 struct resource *p, **pp;
910 struct resource **firstpp = NULL;
911
912 for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
913 if (p->end < res->start)
914 continue;
915 if (res->end < p->start)
916 break;
917 if (p->start < res->start || p->end > res->end)
918 return -1; /* not completely contained */
919 if (firstpp == NULL)
920 firstpp = pp;
921 }
922 if (firstpp == NULL)
923 return -1; /* didn't find any conflicting entries? */
924 res->parent = parent;
925 res->child = *firstpp;
926 res->sibling = *pp;
927 *firstpp = res;
928 *pp = NULL;
929 for (p = res->child; p != NULL; p = p->sibling) {
930 p->parent = res;
931 DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
932 p->name,
933 (unsigned long long)p->start,
934 (unsigned long long)p->end, res->name);
935 }
936 return 0;
937 }
938
939 /*
940 * Handle resources of PCI devices. If the world were perfect, we could
941 * just allocate all the resource regions and do nothing more. It isn't.
942 * On the other hand, we cannot just re-allocate all devices, as it would
943 * require us to know lots of host bridge internals. So we attempt to
944 * keep as much of the original configuration as possible, but tweak it
945 * when it's found to be wrong.
946 *
947 * Known BIOS problems we have to work around:
948 * - I/O or memory regions not configured
949 * - regions configured, but not enabled in the command register
950 * - bogus I/O addresses above 64K used
951 * - expansion ROMs left enabled (this may sound harmless, but given
952 * the fact the PCI specs explicitly allow address decoders to be
953 * shared between expansion ROMs and other resource regions, it's
954 * at least dangerous)
955 *
956 * Our solution:
957 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
958 * This gives us fixed barriers on where we can allocate.
959 * (2) Allocate resources for all enabled devices. If there is
960 * a collision, just mark the resource as unallocated. Also
961 * disable expansion ROMs during this step.
962 * (3) Try to allocate resources for disabled devices. If the
963 * resources were assigned correctly, everything goes well,
964 * if they weren't, they won't disturb allocation of other
965 * resources.
966 * (4) Assign new addresses to resources which were either
967 * not configured at all or misconfigured. If explicitly
968 * requested by the user, configure expansion ROM address
969 * as well.
970 */
971
972 static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
973 {
974 struct pci_bus *bus;
975 int i;
976 struct resource *res, *pr;
977
978 /* Depth-First Search on bus tree */
979 list_for_each_entry(bus, bus_list, node) {
980 for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
981 if ((res = bus->resource[i]) == NULL || !res->flags
982 || res->start > res->end)
983 continue;
984 if (bus->parent == NULL)
985 pr = (res->flags & IORESOURCE_IO) ?
986 &ioport_resource : &iomem_resource;
987 else {
988 /* Don't bother with non-root busses when
989 * re-assigning all resources. We clear the
990 * resource flags as if they were colliding
991 * and as such ensure proper re-allocation
992 * later.
993 */
994 if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)
995 goto clear_resource;
996 pr = pci_find_parent_resource(bus->self, res);
997 if (pr == res) {
998 /* this happens when the generic PCI
999 * code (wrongly) decides that this
1000 * bridge is transparent -- paulus
1001 */
1002 continue;
1003 }
1004 }
1005
1006 DBG("PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx "
1007 "[0x%x], parent %p (%s)\n",
1008 bus->self ? pci_name(bus->self) : "PHB",
1009 bus->number, i,
1010 (unsigned long long)res->start,
1011 (unsigned long long)res->end,
1012 (unsigned int)res->flags,
1013 pr, (pr && pr->name) ? pr->name : "nil");
1014
1015 if (pr && !(pr->flags & IORESOURCE_UNSET)) {
1016 if (request_resource(pr, res) == 0)
1017 continue;
1018 /*
1019 * Must be a conflict with an existing entry.
1020 * Move that entry (or entries) under the
1021 * bridge resource and try again.
1022 */
1023 if (reparent_resources(pr, res) == 0)
1024 continue;
1025 }
1026 printk(KERN_WARNING
1027 "PCI: Cannot allocate resource region "
1028 "%d of PCI bridge %d, will remap\n",
1029 i, bus->number);
1030 clear_resource:
1031 res->flags = 0;
1032 }
1033 pcibios_allocate_bus_resources(&bus->children);
1034 }
1035 }
1036
1037 static inline int __devinit alloc_resource(struct pci_dev *dev, int idx)
1038 {
1039 struct resource *pr, *r = &dev->resource[idx];
1040
1041 DBG("PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n",
1042 pci_name(dev), idx,
1043 (unsigned long long)r->start,
1044 (unsigned long long)r->end,
1045 (unsigned int)r->flags);
1046
1047 pr = pci_find_parent_resource(dev, r);
1048 if (!pr || (pr->flags & IORESOURCE_UNSET) ||
1049 request_resource(pr, r) < 0) {
1050 printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
1051 " of device %s, will remap\n", idx, pci_name(dev));
1052 if (pr)
1053 DBG("PCI: parent is %p: %016llx-%016llx [%x]\n", pr,
1054 (unsigned long long)pr->start,
1055 (unsigned long long)pr->end,
1056 (unsigned int)pr->flags);
1057 /* We'll assign a new address later */
1058 r->flags |= IORESOURCE_UNSET;
1059 r->end -= r->start;
1060 r->start = 0;
1061
1062 return -EBUSY;
1063 }
1064 return 0;
1065 }
1066
1067 static void __init pcibios_allocate_resources(int pass)
1068 {
1069 struct pci_dev *dev = NULL;
1070 int idx, disabled;
1071 u16 command;
1072 struct resource *r;
1073
1074 for_each_pci_dev(dev) {
1075 pci_read_config_word(dev, PCI_COMMAND, &command);
1076 for (idx = 0; idx < 6; idx++) {
1077 r = &dev->resource[idx];
1078 if (r->parent) /* Already allocated */
1079 continue;
1080 if (!r->flags || (r->flags & IORESOURCE_UNSET))
1081 continue; /* Not assigned at all */
1082 if (r->flags & IORESOURCE_IO)
1083 disabled = !(command & PCI_COMMAND_IO);
1084 else
1085 disabled = !(command & PCI_COMMAND_MEMORY);
1086 if (pass == disabled && alloc_resource(dev, idx)) {
1087 command &= ~(r->flags & (IORESOURCE_IO |
1088 IORESOURCE_MEM));
1089 pci_write_config_word(dev,
1090 PCI_COMMAND, command);
1091 }
1092 }
1093 if (pass)
1094 continue;
1095 r = &dev->resource[PCI_ROM_RESOURCE];
1096 if (r->flags & IORESOURCE_ROM_ENABLE) {
1097 /* Turn the ROM off, leave the resource region,
1098 * but keep it unregistered.
1099 */
1100 u32 reg;
1101 DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
1102 r->flags &= ~IORESOURCE_ROM_ENABLE;
1103 pci_read_config_dword(dev, dev->rom_base_reg, &reg);
1104 pci_write_config_dword(dev, dev->rom_base_reg,
1105 reg & ~PCI_ROM_ADDRESS_ENABLE);
1106 }
1107 }
1108 }
1109
1110 void __init pcibios_resource_survey(void)
1111 {
1112 /* Allocate and assign resources. If we re-assign everything, then
1113 * we skip the allocate phase
1114 */
1115 pcibios_allocate_bus_resources(&pci_root_buses);
1116
1117 if (!(ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)) {
1118 pcibios_allocate_resources(0);
1119 pcibios_allocate_resources(1);
1120 }
1121
1122 if (!(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) {
1123 DBG("PCI: Assigning unassigned resouces...\n");
1124 pci_assign_unassigned_resources();
1125 }
1126
1127 /* Call machine dependent fixup */
1128 if (ppc_md.pcibios_fixup)
1129 ppc_md.pcibios_fixup();
1130 }
1131
1132 #ifdef CONFIG_HOTPLUG
1133 /* This is used by the pSeries hotplug driver to allocate resource
1134 * of newly plugged busses. We can try to consolidate with the
1135 * rest of the code later, for now, keep it as-is
1136 */
1137 void __devinit pcibios_claim_one_bus(struct pci_bus *bus)
1138 {
1139 struct pci_dev *dev;
1140 struct pci_bus *child_bus;
1141
1142 list_for_each_entry(dev, &bus->devices, bus_list) {
1143 int i;
1144
1145 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1146 struct resource *r = &dev->resource[i];
1147
1148 if (r->parent || !r->start || !r->flags)
1149 continue;
1150 pci_claim_resource(dev, i);
1151 }
1152 }
1153
1154 list_for_each_entry(child_bus, &bus->children, node)
1155 pcibios_claim_one_bus(child_bus);
1156 }
1157 EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
1158 #endif /* CONFIG_HOTPLUG */
1159
1160 int pcibios_enable_device(struct pci_dev *dev, int mask)
1161 {
1162 u16 cmd, old_cmd;
1163 int idx;
1164 struct resource *r;
1165
1166 if (ppc_md.pcibios_enable_device_hook)
1167 if (ppc_md.pcibios_enable_device_hook(dev))
1168 return -EINVAL;
1169
1170 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1171 old_cmd = cmd;
1172 for (idx = 0; idx < PCI_NUM_RESOURCES; idx++) {
1173 /* Only set up the requested stuff */
1174 if (!(mask & (1 << idx)))
1175 continue;
1176 r = &dev->resource[idx];
1177 if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
1178 continue;
1179 if ((idx == PCI_ROM_RESOURCE) &&
1180 (!(r->flags & IORESOURCE_ROM_ENABLE)))
1181 continue;
1182 if (r->parent == NULL) {
1183 printk(KERN_ERR "PCI: Device %s not available because"
1184 " of resource collisions\n", pci_name(dev));
1185 return -EINVAL;
1186 }
1187 if (r->flags & IORESOURCE_IO)
1188 cmd |= PCI_COMMAND_IO;
1189 if (r->flags & IORESOURCE_MEM)
1190 cmd |= PCI_COMMAND_MEMORY;
1191 }
1192 if (cmd != old_cmd) {
1193 printk("PCI: Enabling device %s (%04x -> %04x)\n",
1194 pci_name(dev), old_cmd, cmd);
1195 pci_write_config_word(dev, PCI_COMMAND, cmd);
1196 }
1197 return 0;
1198 }
1199
This page took 0.056041 seconds and 6 git commands to generate.