blob: f3fd7eb90a7b4e949258738188f83665c407e32a [file] [log] [blame]
Kumar Gala5516b542007-06-27 01:17:57 -05001/*
2 * Contains common pci routines for ALL ppc platform
Kumar Galacf1d8a82007-06-28 22:56:24 -05003 * (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
Kumar Gala5516b542007-06-27 01:17:57 -050012 *
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>
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +000040#include <asm/eeh.h>
Kumar Gala5516b542007-06-27 01:17:57 -050041
Kumar Galaa4c9e322007-06-27 13:09:43 -050042static DEFINE_SPINLOCK(hose_spinlock);
43
44/* XXX kill that some day ... */
Stephen Rothwellebfc00f2007-11-19 16:56:15 +110045static int global_phb_number; /* Global phb counter */
Kumar Galaa4c9e322007-06-27 13:09:43 -050046
Benjamin Herrenschmidt25e81f92007-12-11 14:48:17 +110047/* ISA Memory physical address */
48resource_size_t isa_mem_base;
49
Benjamin Herrenschmidt1fd0f522008-10-02 14:12:51 +000050/* Default PCI flags is 0 on ppc32, modified at boot on ppc64 */
51unsigned int ppc_pci_flags = 0;
52
Kumar Galaa4c9e322007-06-27 13:09:43 -050053
Becky Bruce4fc665b2008-09-12 10:34:46 +000054static struct dma_mapping_ops *pci_dma_ops;
55
56void set_pci_dma_ops(struct dma_mapping_ops *dma_ops)
57{
58 pci_dma_ops = dma_ops;
59}
60
61struct dma_mapping_ops *get_pci_dma_ops(void)
62{
63 return pci_dma_ops;
64}
65EXPORT_SYMBOL(get_pci_dma_ops);
66
67int pci_set_dma_mask(struct pci_dev *dev, u64 mask)
68{
69 return dma_set_mask(&dev->dev, mask);
70}
71
72int pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
73{
74 int rc;
75
76 rc = dma_set_mask(&dev->dev, mask);
77 dev->dev.coherent_dma_mask = dev->dma_mask;
78
79 return rc;
80}
81
Stephen Rothwelle60516e2007-12-11 11:02:07 +110082struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
Kumar Galaa4c9e322007-06-27 13:09:43 -050083{
84 struct pci_controller *phb;
85
Stephen Rothwelle60516e2007-12-11 11:02:07 +110086 phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
Kumar Galaa4c9e322007-06-27 13:09:43 -050087 if (phb == NULL)
88 return NULL;
Stephen Rothwelle60516e2007-12-11 11:02:07 +110089 spin_lock(&hose_spinlock);
90 phb->global_number = global_phb_number++;
91 list_add_tail(&phb->list_node, &hose_list);
92 spin_unlock(&hose_spinlock);
Stephen Rothwell44ef3392007-12-10 14:33:21 +110093 phb->dn = dev;
Kumar Galaa4c9e322007-06-27 13:09:43 -050094 phb->is_dynamic = mem_init_done;
95#ifdef CONFIG_PPC64
96 if (dev) {
97 int nid = of_node_to_nid(dev);
98
99 if (nid < 0 || !node_online(nid))
100 nid = -1;
101
102 PHB_SET_NODE(phb, nid);
103 }
104#endif
105 return phb;
106}
107
108void pcibios_free_controller(struct pci_controller *phb)
109{
110 spin_lock(&hose_spinlock);
111 list_del(&phb->list_node);
112 spin_unlock(&hose_spinlock);
113
114 if (phb->is_dynamic)
115 kfree(phb);
116}
117
Benjamin Herrenschmidt6dfbde22007-07-26 14:07:13 +1000118int pcibios_vaddr_is_ioport(void __iomem *address)
119{
120 int ret = 0;
121 struct pci_controller *hose;
122 unsigned long size;
123
124 spin_lock(&hose_spinlock);
125 list_for_each_entry(hose, &hose_list, list_node) {
126#ifdef CONFIG_PPC64
127 size = hose->pci_io_size;
128#else
129 size = hose->io_resource.end - hose->io_resource.start + 1;
130#endif
131 if (address >= hose->io_base_virt &&
132 address < (hose->io_base_virt + size)) {
133 ret = 1;
134 break;
135 }
136 }
137 spin_unlock(&hose_spinlock);
138 return ret;
139}
140
Kumar Gala5516b542007-06-27 01:17:57 -0500141/*
142 * Return the domain number for this bus.
143 */
144int pci_domain_nr(struct pci_bus *bus)
145{
Stephen Rothwell6207e812007-12-07 02:04:33 +1100146 struct pci_controller *hose = pci_bus_to_host(bus);
Kumar Gala5516b542007-06-27 01:17:57 -0500147
Stephen Rothwell6207e812007-12-07 02:04:33 +1100148 return hose->global_number;
Kumar Gala5516b542007-06-27 01:17:57 -0500149}
Kumar Gala5516b542007-06-27 01:17:57 -0500150EXPORT_SYMBOL(pci_domain_nr);
Kumar Gala58083da2007-06-27 11:07:51 -0500151
152#ifdef CONFIG_PPC_OF
Kumar Galaa4c9e322007-06-27 13:09:43 -0500153
154/* This routine is meant to be used early during boot, when the
155 * PCI bus numbers have not yet been assigned, and you need to
156 * issue PCI config cycles to an OF device.
157 * It could also be used to "fix" RTAS config cycles if you want
158 * to set pci_assign_all_buses to 1 and still use RTAS for PCI
159 * config cycles.
160 */
161struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
162{
163 if (!have_of)
164 return NULL;
165 while(node) {
166 struct pci_controller *hose, *tmp;
167 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100168 if (hose->dn == node)
Kumar Galaa4c9e322007-06-27 13:09:43 -0500169 return hose;
170 node = node->parent;
171 }
172 return NULL;
173}
174
Kumar Gala58083da2007-06-27 11:07:51 -0500175static ssize_t pci_show_devspec(struct device *dev,
176 struct device_attribute *attr, char *buf)
177{
178 struct pci_dev *pdev;
179 struct device_node *np;
180
181 pdev = to_pci_dev (dev);
182 np = pci_device_to_OF_node(pdev);
183 if (np == NULL || np->full_name == NULL)
184 return 0;
185 return sprintf(buf, "%s", np->full_name);
186}
187static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
188#endif /* CONFIG_PPC_OF */
189
190/* Add sysfs properties */
Tony Breeds4f3731d2007-07-18 11:03:55 +1000191int pcibios_add_platform_entries(struct pci_dev *pdev)
Kumar Gala58083da2007-06-27 11:07:51 -0500192{
193#ifdef CONFIG_PPC_OF
Tony Breeds4f3731d2007-07-18 11:03:55 +1000194 return device_create_file(&pdev->dev, &dev_attr_devspec);
195#else
196 return 0;
Kumar Gala58083da2007-06-27 11:07:51 -0500197#endif /* CONFIG_PPC_OF */
Tony Breeds4f3731d2007-07-18 11:03:55 +1000198
Kumar Gala58083da2007-06-27 11:07:51 -0500199}
200
Stephen Rothwella2b73902007-07-22 00:37:38 +1000201char __devinit *pcibios_setup(char *str)
Kumar Gala58083da2007-06-27 11:07:51 -0500202{
203 return str;
204}
205
Benjamin Herrenschmidtfd6852c2008-10-27 19:48:52 +0000206static void __devinit pcibios_setup_new_device(struct pci_dev *dev)
Becky Bruce4fc665b2008-09-12 10:34:46 +0000207{
208 struct dev_archdata *sd = &dev->dev.archdata;
209
210 sd->of_node = pci_device_to_OF_node(dev);
211
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000212 pr_debug("PCI: device %s OF node: %s\n", pci_name(dev),
213 sd->of_node ? sd->of_node->full_name : "<none>");
Becky Bruce4fc665b2008-09-12 10:34:46 +0000214
215 sd->dma_ops = pci_dma_ops;
216#ifdef CONFIG_PPC32
217 sd->dma_data = (void *)PCI_DRAM_OFFSET;
218#endif
219 set_dev_node(&dev->dev, pcibus_to_node(dev->bus));
220
221 if (ppc_md.pci_dma_dev_setup)
222 ppc_md.pci_dma_dev_setup(dev);
223}
Becky Bruce4fc665b2008-09-12 10:34:46 +0000224
Kumar Gala58083da2007-06-27 11:07:51 -0500225/*
226 * Reads the interrupt pin to determine if interrupt is use by card.
227 * If the interrupt is used, then gets the interrupt line from the
228 * openfirmware and sets it in the pci_dev and pci_config line.
229 */
230int pci_read_irq_line(struct pci_dev *pci_dev)
231{
232 struct of_irq oirq;
233 unsigned int virq;
234
Benjamin Herrenschmidt50c9bc22007-12-20 14:54:55 +1100235 /* The current device-tree that iSeries generates from the HV
236 * PCI informations doesn't contain proper interrupt routing,
237 * and all the fallback would do is print out crap, so we
238 * don't attempt to resolve the interrupts here at all, some
239 * iSeries specific fixup does it.
240 *
241 * In the long run, we will hopefully fix the generated device-tree
242 * instead.
243 */
244#ifdef CONFIG_PPC_ISERIES
245 if (firmware_has_feature(FW_FEATURE_ISERIES))
246 return -1;
247#endif
248
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000249 pr_debug("PCI: Try to map irq for %s...\n", pci_name(pci_dev));
Kumar Gala58083da2007-06-27 11:07:51 -0500250
251#ifdef DEBUG
252 memset(&oirq, 0xff, sizeof(oirq));
253#endif
254 /* Try to get a mapping from the device-tree */
255 if (of_irq_map_pci(pci_dev, &oirq)) {
256 u8 line, pin;
257
258 /* If that fails, lets fallback to what is in the config
259 * space and map that through the default controller. We
260 * also set the type to level low since that's what PCI
261 * interrupts are. If your platform does differently, then
262 * either provide a proper interrupt tree or don't use this
263 * function.
264 */
265 if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
266 return -1;
267 if (pin == 0)
268 return -1;
269 if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
Benjamin Herrenschmidt54a24cb2007-12-20 15:10:02 +1100270 line == 0xff || line == 0) {
Kumar Gala58083da2007-06-27 11:07:51 -0500271 return -1;
272 }
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000273 pr_debug(" No map ! Using line %d (pin %d) from PCI config\n",
274 line, pin);
Kumar Gala58083da2007-06-27 11:07:51 -0500275
276 virq = irq_create_mapping(NULL, line);
277 if (virq != NO_IRQ)
278 set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
279 } else {
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000280 pr_debug(" Got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
281 oirq.size, oirq.specifier[0], oirq.specifier[1],
Kumar Gala58083da2007-06-27 11:07:51 -0500282 oirq.controller->full_name);
283
284 virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
285 oirq.size);
286 }
287 if(virq == NO_IRQ) {
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000288 pr_debug(" Failed to map !\n");
Kumar Gala58083da2007-06-27 11:07:51 -0500289 return -1;
290 }
291
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000292 pr_debug(" Mapped to linux irq %d\n", virq);
Kumar Gala58083da2007-06-27 11:07:51 -0500293
294 pci_dev->irq = virq;
295
296 return 0;
297}
298EXPORT_SYMBOL(pci_read_irq_line);
299
300/*
301 * Platform support for /proc/bus/pci/X/Y mmap()s,
302 * modelled on the sparc64 implementation by Dave Miller.
303 * -- paulus.
304 */
305
306/*
307 * Adjust vm_pgoff of VMA such that it is the physical page offset
308 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
309 *
310 * Basically, the user finds the base address for his device which he wishes
311 * to mmap. They read the 32-bit value from the config space base register,
312 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
313 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
314 *
315 * Returns negative error code on failure, zero on success.
316 */
317static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
318 resource_size_t *offset,
319 enum pci_mmap_state mmap_state)
320{
321 struct pci_controller *hose = pci_bus_to_host(dev->bus);
322 unsigned long io_offset = 0;
323 int i, res_bit;
324
325 if (hose == 0)
326 return NULL; /* should never happen */
327
328 /* If memory, add on the PCI bridge address offset */
329 if (mmap_state == pci_mmap_mem) {
330#if 0 /* See comment in pci_resource_to_user() for why this is disabled */
331 *offset += hose->pci_mem_offset;
332#endif
333 res_bit = IORESOURCE_MEM;
334 } else {
335 io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
336 *offset += io_offset;
337 res_bit = IORESOURCE_IO;
338 }
339
340 /*
341 * Check that the offset requested corresponds to one of the
342 * resources of the device.
343 */
344 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
345 struct resource *rp = &dev->resource[i];
346 int flags = rp->flags;
347
348 /* treat ROM as memory (should be already) */
349 if (i == PCI_ROM_RESOURCE)
350 flags |= IORESOURCE_MEM;
351
352 /* Active and same type? */
353 if ((flags & res_bit) == 0)
354 continue;
355
356 /* In the range of this resource? */
357 if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
358 continue;
359
360 /* found it! construct the final physical address */
361 if (mmap_state == pci_mmap_io)
362 *offset += hose->io_base_phys - io_offset;
363 return rp;
364 }
365
366 return NULL;
367}
368
369/*
370 * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
371 * device mapping.
372 */
373static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
374 pgprot_t protection,
375 enum pci_mmap_state mmap_state,
376 int write_combine)
377{
378 unsigned long prot = pgprot_val(protection);
379
380 /* Write combine is always 0 on non-memory space mappings. On
381 * memory space, if the user didn't pass 1, we check for a
382 * "prefetchable" resource. This is a bit hackish, but we use
383 * this to workaround the inability of /sysfs to provide a write
384 * combine bit
385 */
386 if (mmap_state != pci_mmap_mem)
387 write_combine = 0;
388 else if (write_combine == 0) {
389 if (rp->flags & IORESOURCE_PREFETCH)
390 write_combine = 1;
391 }
392
393 /* XXX would be nice to have a way to ask for write-through */
394 prot |= _PAGE_NO_CACHE;
395 if (write_combine)
396 prot &= ~_PAGE_GUARDED;
397 else
398 prot |= _PAGE_GUARDED;
399
400 return __pgprot(prot);
401}
402
403/*
404 * This one is used by /dev/mem and fbdev who have no clue about the
405 * PCI device, it tries to find the PCI device first and calls the
406 * above routine
407 */
408pgprot_t pci_phys_mem_access_prot(struct file *file,
409 unsigned long pfn,
410 unsigned long size,
411 pgprot_t protection)
412{
413 struct pci_dev *pdev = NULL;
414 struct resource *found = NULL;
415 unsigned long prot = pgprot_val(protection);
Benjamin Herrenschmidt7c12d902008-10-01 15:30:04 +0000416 resource_size_t offset = ((resource_size_t)pfn) << PAGE_SHIFT;
Kumar Gala58083da2007-06-27 11:07:51 -0500417 int i;
418
419 if (page_is_ram(pfn))
420 return __pgprot(prot);
421
422 prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
423
424 for_each_pci_dev(pdev) {
425 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
426 struct resource *rp = &pdev->resource[i];
427 int flags = rp->flags;
428
429 /* Active and same type? */
430 if ((flags & IORESOURCE_MEM) == 0)
431 continue;
432 /* In the range of this resource? */
433 if (offset < (rp->start & PAGE_MASK) ||
434 offset > rp->end)
435 continue;
436 found = rp;
437 break;
438 }
439 if (found)
440 break;
441 }
442 if (found) {
443 if (found->flags & IORESOURCE_PREFETCH)
444 prot &= ~_PAGE_GUARDED;
445 pci_dev_put(pdev);
446 }
447
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000448 pr_debug("PCI: Non-PCI map for %llx, prot: %lx\n",
449 (unsigned long long)offset, prot);
Kumar Gala58083da2007-06-27 11:07:51 -0500450
451 return __pgprot(prot);
452}
453
454
455/*
456 * Perform the actual remap of the pages for a PCI device mapping, as
457 * appropriate for this architecture. The region in the process to map
458 * is described by vm_start and vm_end members of VMA, the base physical
459 * address is found in vm_pgoff.
460 * The pci device structure is provided so that architectures may make mapping
461 * decisions on a per-device or per-bus basis.
462 *
463 * Returns a negative error code on failure, zero on success.
464 */
465int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
466 enum pci_mmap_state mmap_state, int write_combine)
467{
Benjamin Herrenschmidt7c12d902008-10-01 15:30:04 +0000468 resource_size_t offset =
469 ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
Kumar Gala58083da2007-06-27 11:07:51 -0500470 struct resource *rp;
471 int ret;
472
473 rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
474 if (rp == NULL)
475 return -EINVAL;
476
477 vma->vm_pgoff = offset >> PAGE_SHIFT;
478 vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
479 vma->vm_page_prot,
480 mmap_state, write_combine);
481
482 ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
483 vma->vm_end - vma->vm_start, vma->vm_page_prot);
484
485 return ret;
486}
487
Benjamin Herrenschmidte9f82cb2008-10-14 11:55:31 +1100488/* This provides legacy IO read access on a bus */
489int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
490{
491 unsigned long offset;
492 struct pci_controller *hose = pci_bus_to_host(bus);
493 struct resource *rp = &hose->io_resource;
494 void __iomem *addr;
495
496 /* Check if port can be supported by that bus. We only check
497 * the ranges of the PHB though, not the bus itself as the rules
498 * for forwarding legacy cycles down bridges are not our problem
499 * here. So if the host bridge supports it, we do it.
500 */
501 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
502 offset += port;
503
504 if (!(rp->flags & IORESOURCE_IO))
505 return -ENXIO;
506 if (offset < rp->start || (offset + size) > rp->end)
507 return -ENXIO;
508 addr = hose->io_base_virt + port;
509
510 switch(size) {
511 case 1:
512 *((u8 *)val) = in_8(addr);
513 return 1;
514 case 2:
515 if (port & 1)
516 return -EINVAL;
517 *((u16 *)val) = in_le16(addr);
518 return 2;
519 case 4:
520 if (port & 3)
521 return -EINVAL;
522 *((u32 *)val) = in_le32(addr);
523 return 4;
524 }
525 return -EINVAL;
526}
527
528/* This provides legacy IO write access on a bus */
529int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
530{
531 unsigned long offset;
532 struct pci_controller *hose = pci_bus_to_host(bus);
533 struct resource *rp = &hose->io_resource;
534 void __iomem *addr;
535
536 /* Check if port can be supported by that bus. We only check
537 * the ranges of the PHB though, not the bus itself as the rules
538 * for forwarding legacy cycles down bridges are not our problem
539 * here. So if the host bridge supports it, we do it.
540 */
541 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
542 offset += port;
543
544 if (!(rp->flags & IORESOURCE_IO))
545 return -ENXIO;
546 if (offset < rp->start || (offset + size) > rp->end)
547 return -ENXIO;
548 addr = hose->io_base_virt + port;
549
550 /* WARNING: The generic code is idiotic. It gets passed a pointer
551 * to what can be a 1, 2 or 4 byte quantity and always reads that
552 * as a u32, which means that we have to correct the location of
553 * the data read within those 32 bits for size 1 and 2
554 */
555 switch(size) {
556 case 1:
557 out_8(addr, val >> 24);
558 return 1;
559 case 2:
560 if (port & 1)
561 return -EINVAL;
562 out_le16(addr, val >> 16);
563 return 2;
564 case 4:
565 if (port & 3)
566 return -EINVAL;
567 out_le32(addr, val);
568 return 4;
569 }
570 return -EINVAL;
571}
572
573/* This provides legacy IO or memory mmap access on a bus */
574int pci_mmap_legacy_page_range(struct pci_bus *bus,
575 struct vm_area_struct *vma,
576 enum pci_mmap_state mmap_state)
577{
578 struct pci_controller *hose = pci_bus_to_host(bus);
579 resource_size_t offset =
580 ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
581 resource_size_t size = vma->vm_end - vma->vm_start;
582 struct resource *rp;
583
584 pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
585 pci_domain_nr(bus), bus->number,
586 mmap_state == pci_mmap_mem ? "MEM" : "IO",
587 (unsigned long long)offset,
588 (unsigned long long)(offset + size - 1));
589
590 if (mmap_state == pci_mmap_mem) {
591 if ((offset + size) > hose->isa_mem_size)
592 return -ENXIO;
593 offset += hose->isa_mem_phys;
594 } else {
595 unsigned long io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
596 unsigned long roffset = offset + io_offset;
597 rp = &hose->io_resource;
598 if (!(rp->flags & IORESOURCE_IO))
599 return -ENXIO;
600 if (roffset < rp->start || (roffset + size) > rp->end)
601 return -ENXIO;
602 offset += hose->io_base_phys;
603 }
604 pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset);
605
606 vma->vm_pgoff = offset >> PAGE_SHIFT;
David Gibson201bdc82008-10-20 17:55:29 +0000607 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
608 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Benjamin Herrenschmidte9f82cb2008-10-14 11:55:31 +1100609 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
610 vma->vm_end - vma->vm_start,
611 vma->vm_page_prot);
612}
613
Kumar Gala58083da2007-06-27 11:07:51 -0500614void pci_resource_to_user(const struct pci_dev *dev, int bar,
615 const struct resource *rsrc,
616 resource_size_t *start, resource_size_t *end)
617{
618 struct pci_controller *hose = pci_bus_to_host(dev->bus);
619 resource_size_t offset = 0;
620
621 if (hose == NULL)
622 return;
623
624 if (rsrc->flags & IORESOURCE_IO)
625 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
626
627 /* We pass a fully fixed up address to userland for MMIO instead of
628 * a BAR value because X is lame and expects to be able to use that
629 * to pass to /dev/mem !
630 *
631 * That means that we'll have potentially 64 bits values where some
632 * userland apps only expect 32 (like X itself since it thinks only
633 * Sparc has 64 bits MMIO) but if we don't do that, we break it on
634 * 32 bits CHRPs :-(
635 *
636 * Hopefully, the sysfs insterface is immune to that gunk. Once X
637 * has been fixed (and the fix spread enough), we can re-enable the
638 * 2 lines below and pass down a BAR value to userland. In that case
639 * we'll also have to re-enable the matching code in
640 * __pci_mmap_make_offset().
641 *
642 * BenH.
643 */
644#if 0
645 else if (rsrc->flags & IORESOURCE_MEM)
646 offset = hose->pci_mem_offset;
647#endif
648
649 *start = rsrc->start - offset;
650 *end = rsrc->end - offset;
651}
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100652
653/**
654 * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
655 * @hose: newly allocated pci_controller to be setup
656 * @dev: device node of the host bridge
657 * @primary: set if primary bus (32 bits only, soon to be deprecated)
658 *
659 * This function will parse the "ranges" property of a PCI host bridge device
660 * node and setup the resource mapping of a pci controller based on its
661 * content.
662 *
663 * Life would be boring if it wasn't for a few issues that we have to deal
664 * with here:
665 *
666 * - We can only cope with one IO space range and up to 3 Memory space
667 * ranges. However, some machines (thanks Apple !) tend to split their
668 * space into lots of small contiguous ranges. So we have to coalesce.
669 *
670 * - We can only cope with all memory ranges having the same offset
671 * between CPU addresses and PCI addresses. Unfortunately, some bridges
672 * are setup for a large 1:1 mapping along with a small "window" which
673 * maps PCI address 0 to some arbitrary high address of the CPU space in
674 * order to give access to the ISA memory hole.
675 * The way out of here that I've chosen for now is to always set the
676 * offset based on the first resource found, then override it if we
677 * have a different offset and the previous was set by an ISA hole.
678 *
679 * - Some busses have IO space not starting at 0, which causes trouble with
680 * the way we do our IO resource renumbering. The code somewhat deals with
681 * it for 64 bits but I would expect problems on 32 bits.
682 *
683 * - Some 32 bits platforms such as 4xx can have physical space larger than
684 * 32 bits so we need to use 64 bits values for the parsing
685 */
686void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
687 struct device_node *dev,
688 int primary)
689{
690 const u32 *ranges;
691 int rlen;
692 int pna = of_n_addr_cells(dev);
693 int np = pna + 5;
694 int memno = 0, isa_hole = -1;
695 u32 pci_space;
696 unsigned long long pci_addr, cpu_addr, pci_next, cpu_next, size;
697 unsigned long long isa_mb = 0;
698 struct resource *res;
699
700 printk(KERN_INFO "PCI host bridge %s %s ranges:\n",
701 dev->full_name, primary ? "(primary)" : "");
702
703 /* Get ranges property */
704 ranges = of_get_property(dev, "ranges", &rlen);
705 if (ranges == NULL)
706 return;
707
708 /* Parse it */
709 while ((rlen -= np * 4) >= 0) {
710 /* Read next ranges element */
711 pci_space = ranges[0];
712 pci_addr = of_read_number(ranges + 1, 2);
713 cpu_addr = of_translate_address(dev, ranges + 3);
714 size = of_read_number(ranges + pna + 3, 2);
715 ranges += np;
Benjamin Herrenschmidte9f82cb2008-10-14 11:55:31 +1100716
717 /* If we failed translation or got a zero-sized region
718 * (some FW try to feed us with non sensical zero sized regions
719 * such as power3 which look like some kind of attempt at exposing
720 * the VGA memory hole)
721 */
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100722 if (cpu_addr == OF_BAD_ADDR || size == 0)
723 continue;
724
725 /* Now consume following elements while they are contiguous */
726 for (; rlen >= np * sizeof(u32);
727 ranges += np, rlen -= np * 4) {
728 if (ranges[0] != pci_space)
729 break;
730 pci_next = of_read_number(ranges + 1, 2);
731 cpu_next = of_translate_address(dev, ranges + 3);
732 if (pci_next != pci_addr + size ||
733 cpu_next != cpu_addr + size)
734 break;
735 size += of_read_number(ranges + pna + 3, 2);
736 }
737
738 /* Act based on address space type */
739 res = NULL;
740 switch ((pci_space >> 24) & 0x3) {
741 case 1: /* PCI IO space */
742 printk(KERN_INFO
743 " IO 0x%016llx..0x%016llx -> 0x%016llx\n",
744 cpu_addr, cpu_addr + size - 1, pci_addr);
745
746 /* We support only one IO range */
747 if (hose->pci_io_size) {
748 printk(KERN_INFO
749 " \\--> Skipped (too many) !\n");
750 continue;
751 }
752#ifdef CONFIG_PPC32
753 /* On 32 bits, limit I/O space to 16MB */
754 if (size > 0x01000000)
755 size = 0x01000000;
756
757 /* 32 bits needs to map IOs here */
758 hose->io_base_virt = ioremap(cpu_addr, size);
759
760 /* Expect trouble if pci_addr is not 0 */
761 if (primary)
762 isa_io_base =
763 (unsigned long)hose->io_base_virt;
764#endif /* CONFIG_PPC32 */
765 /* pci_io_size and io_base_phys always represent IO
766 * space starting at 0 so we factor in pci_addr
767 */
768 hose->pci_io_size = pci_addr + size;
769 hose->io_base_phys = cpu_addr - pci_addr;
770
771 /* Build resource */
772 res = &hose->io_resource;
773 res->flags = IORESOURCE_IO;
774 res->start = pci_addr;
775 break;
776 case 2: /* PCI Memory space */
Benjamin Herrenschmidt67260ac2008-07-17 15:53:31 +1000777 case 3: /* PCI 64 bits Memory space */
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100778 printk(KERN_INFO
779 " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
780 cpu_addr, cpu_addr + size - 1, pci_addr,
781 (pci_space & 0x40000000) ? "Prefetch" : "");
782
783 /* We support only 3 memory ranges */
784 if (memno >= 3) {
785 printk(KERN_INFO
786 " \\--> Skipped (too many) !\n");
787 continue;
788 }
789 /* Handles ISA memory hole space here */
790 if (pci_addr == 0) {
791 isa_mb = cpu_addr;
792 isa_hole = memno;
793 if (primary || isa_mem_base == 0)
794 isa_mem_base = cpu_addr;
Benjamin Herrenschmidte9f82cb2008-10-14 11:55:31 +1100795 hose->isa_mem_phys = cpu_addr;
796 hose->isa_mem_size = size;
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100797 }
798
799 /* We get the PCI/Mem offset from the first range or
800 * the, current one if the offset came from an ISA
801 * hole. If they don't match, bugger.
802 */
803 if (memno == 0 ||
804 (isa_hole >= 0 && pci_addr != 0 &&
805 hose->pci_mem_offset == isa_mb))
806 hose->pci_mem_offset = cpu_addr - pci_addr;
807 else if (pci_addr != 0 &&
808 hose->pci_mem_offset != cpu_addr - pci_addr) {
809 printk(KERN_INFO
810 " \\--> Skipped (offset mismatch) !\n");
811 continue;
812 }
813
814 /* Build resource */
815 res = &hose->mem_resources[memno++];
816 res->flags = IORESOURCE_MEM;
817 if (pci_space & 0x40000000)
818 res->flags |= IORESOURCE_PREFETCH;
819 res->start = cpu_addr;
820 break;
821 }
822 if (res != NULL) {
823 res->name = dev->full_name;
824 res->end = res->start + size - 1;
825 res->parent = NULL;
826 res->sibling = NULL;
827 res->child = NULL;
828 }
829 }
830
Benjamin Herrenschmidt8db13a02008-07-31 15:24:13 +1000831 /* If there's an ISA hole and the pci_mem_offset is -not- matching
832 * the ISA hole offset, then we need to remove the ISA hole from
833 * the resource list for that brige
834 */
835 if (isa_hole >= 0 && hose->pci_mem_offset != isa_mb) {
836 unsigned int next = isa_hole + 1;
837 printk(KERN_INFO " Removing ISA hole at 0x%016llx\n", isa_mb);
838 if (next < memno)
839 memmove(&hose->mem_resources[isa_hole],
840 &hose->mem_resources[next],
841 sizeof(struct resource) * (memno - next));
842 hose->mem_resources[--memno].flags = 0;
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100843 }
844}
Benjamin Herrenschmidtfa462f22007-12-20 14:54:49 +1100845
846/* Decide whether to display the domain number in /proc */
847int pci_proc_domain(struct pci_bus *bus)
848{
849 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt1fd0f522008-10-02 14:12:51 +0000850
Benjamin Herrenschmidtfa462f22007-12-20 14:54:49 +1100851 if (!(ppc_pci_flags & PPC_PCI_ENABLE_PROC_DOMAINS))
852 return 0;
853 if (ppc_pci_flags & PPC_PCI_COMPAT_DOMAIN_0)
854 return hose->global_number != 0;
855 return 1;
Benjamin Herrenschmidtfa462f22007-12-20 14:54:49 +1100856}
857
Benjamin Herrenschmidtfe2d3382007-12-20 14:54:50 +1100858void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
859 struct resource *res)
860{
861 resource_size_t offset = 0, mask = (resource_size_t)-1;
862 struct pci_controller *hose = pci_bus_to_host(dev->bus);
863
864 if (!hose)
865 return;
866 if (res->flags & IORESOURCE_IO) {
867 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
868 mask = 0xffffffffu;
869 } else if (res->flags & IORESOURCE_MEM)
870 offset = hose->pci_mem_offset;
871
872 region->start = (res->start - offset) & mask;
873 region->end = (res->end - offset) & mask;
874}
875EXPORT_SYMBOL(pcibios_resource_to_bus);
876
877void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
878 struct pci_bus_region *region)
879{
880 resource_size_t offset = 0, mask = (resource_size_t)-1;
881 struct pci_controller *hose = pci_bus_to_host(dev->bus);
882
883 if (!hose)
884 return;
885 if (res->flags & IORESOURCE_IO) {
886 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
887 mask = 0xffffffffu;
888 } else if (res->flags & IORESOURCE_MEM)
889 offset = hose->pci_mem_offset;
890 res->start = (region->start + offset) & mask;
891 res->end = (region->end + offset) & mask;
892}
893EXPORT_SYMBOL(pcibios_bus_to_resource);
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100894
895/* Fixup a bus resource into a linux resource */
896static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
897{
898 struct pci_controller *hose = pci_bus_to_host(dev->bus);
899 resource_size_t offset = 0, mask = (resource_size_t)-1;
900
901 if (res->flags & IORESOURCE_IO) {
902 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
903 mask = 0xffffffffu;
904 } else if (res->flags & IORESOURCE_MEM)
905 offset = hose->pci_mem_offset;
906
907 res->start = (res->start + offset) & mask;
908 res->end = (res->end + offset) & mask;
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100909}
910
911
912/* This header fixup will do the resource fixup for all devices as they are
913 * probed, but not for bridge ranges
914 */
915static void __devinit pcibios_fixup_resources(struct pci_dev *dev)
916{
917 struct pci_controller *hose = pci_bus_to_host(dev->bus);
918 int i;
919
920 if (!hose) {
921 printk(KERN_ERR "No host bridge for PCI dev %s !\n",
922 pci_name(dev));
923 return;
924 }
925 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
926 struct resource *res = dev->resource + i;
927 if (!res->flags)
928 continue;
Benjamin Herrenschmidt7f172892008-02-29 14:58:03 +1100929 /* On platforms that have PPC_PCI_PROBE_ONLY set, we don't
930 * consider 0 as an unassigned BAR value. It's technically
931 * a valid value, but linux doesn't like it... so when we can
932 * re-assign things, we do so, but if we can't, we keep it
933 * around and hope for the best...
934 */
935 if (res->start == 0 && !(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) {
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100936 pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] is unassigned\n",
937 pci_name(dev), i,
938 (unsigned long long)res->start,
939 (unsigned long long)res->end,
940 (unsigned int)res->flags);
941 res->end -= res->start;
942 res->start = 0;
943 res->flags |= IORESOURCE_UNSET;
944 continue;
945 }
946
947 pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] fixup...\n",
948 pci_name(dev), i,
949 (unsigned long long)res->start,\
950 (unsigned long long)res->end,
951 (unsigned int)res->flags);
952
953 fixup_resource(res, dev);
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000954
955 pr_debug("PCI:%s %016llx-%016llx\n",
956 pci_name(dev),
957 (unsigned long long)res->start,
958 (unsigned long long)res->end);
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100959 }
960
961 /* Call machine specific resource fixup */
962 if (ppc_md.pcibios_fixup_resources)
963 ppc_md.pcibios_fixup_resources(dev);
964}
965DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
966
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000967/* This function tries to figure out if a bridge resource has been initialized
968 * by the firmware or not. It doesn't have to be absolutely bullet proof, but
969 * things go more smoothly when it gets it right. It should covers cases such
970 * as Apple "closed" bridge resources and bare-metal pSeries unassigned bridges
971 */
972static int __devinit pcibios_uninitialized_bridge_resource(struct pci_bus *bus,
973 struct resource *res)
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100974{
Benjamin Herrenschmidtbe8cbcd2007-12-20 14:55:04 +1100975 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100976 struct pci_dev *dev = bus->self;
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000977 resource_size_t offset;
978 u16 command;
979 int i;
980
981 /* We don't do anything if PCI_PROBE_ONLY is set */
982 if (ppc_pci_flags & PPC_PCI_PROBE_ONLY)
983 return 0;
984
985 /* Job is a bit different between memory and IO */
986 if (res->flags & IORESOURCE_MEM) {
987 /* If the BAR is non-0 (res != pci_mem_offset) then it's probably been
988 * initialized by somebody
989 */
990 if (res->start != hose->pci_mem_offset)
991 return 0;
992
993 /* The BAR is 0, let's check if memory decoding is enabled on
994 * the bridge. If not, we consider it unassigned
995 */
996 pci_read_config_word(dev, PCI_COMMAND, &command);
997 if ((command & PCI_COMMAND_MEMORY) == 0)
998 return 1;
999
1000 /* Memory decoding is enabled and the BAR is 0. If any of the bridge
1001 * resources covers that starting address (0 then it's good enough for
1002 * us for memory
1003 */
1004 for (i = 0; i < 3; i++) {
1005 if ((hose->mem_resources[i].flags & IORESOURCE_MEM) &&
1006 hose->mem_resources[i].start == hose->pci_mem_offset)
1007 return 0;
1008 }
1009
1010 /* Well, it starts at 0 and we know it will collide so we may as
1011 * well consider it as unassigned. That covers the Apple case.
1012 */
1013 return 1;
1014 } else {
1015 /* If the BAR is non-0, then we consider it assigned */
1016 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
1017 if (((res->start - offset) & 0xfffffffful) != 0)
1018 return 0;
1019
1020 /* Here, we are a bit different than memory as typically IO space
1021 * starting at low addresses -is- valid. What we do instead if that
1022 * we consider as unassigned anything that doesn't have IO enabled
1023 * in the PCI command register, and that's it.
1024 */
1025 pci_read_config_word(dev, PCI_COMMAND, &command);
1026 if (command & PCI_COMMAND_IO)
1027 return 0;
1028
1029 /* It's starting at 0 and IO is disabled in the bridge, consider
1030 * it unassigned
1031 */
1032 return 1;
1033 }
1034}
1035
1036/* Fixup resources of a PCI<->PCI bridge */
1037static void __devinit pcibios_fixup_bridge(struct pci_bus *bus)
1038{
1039 struct resource *res;
1040 int i;
1041
1042 struct pci_dev *dev = bus->self;
1043
1044 for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
1045 if ((res = bus->resource[i]) == NULL)
1046 continue;
1047 if (!res->flags)
1048 continue;
1049 if (i >= 3 && bus->self->transparent)
1050 continue;
1051
1052 pr_debug("PCI:%s Bus rsrc %d %016llx-%016llx [%x] fixup...\n",
1053 pci_name(dev), i,
1054 (unsigned long long)res->start,\
1055 (unsigned long long)res->end,
1056 (unsigned int)res->flags);
1057
1058 /* Perform fixup */
1059 fixup_resource(res, dev);
1060
1061 /* Try to detect uninitialized P2P bridge resources,
1062 * and clear them out so they get re-assigned later
1063 */
1064 if (pcibios_uninitialized_bridge_resource(bus, res)) {
1065 res->flags = 0;
1066 pr_debug("PCI:%s (unassigned)\n", pci_name(dev));
1067 } else {
1068
1069 pr_debug("PCI:%s %016llx-%016llx\n",
1070 pci_name(dev),
1071 (unsigned long long)res->start,
1072 (unsigned long long)res->end);
1073 }
1074 }
1075}
1076
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +00001077void __devinit pcibios_setup_bus_devices(struct pci_bus *bus)
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +00001078{
Benjamin Herrenschmidtab56ced2008-10-27 19:48:33 +00001079 struct pci_dev *dev;
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +11001080
Benjamin Herrenschmidtab56ced2008-10-27 19:48:33 +00001081 pr_debug("PCI: Fixup bus %d (%s)\n",
1082 bus->number, bus->self ? pci_name(bus->self) : "PHB");
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +11001083
Benjamin Herrenschmidtab56ced2008-10-27 19:48:33 +00001084 /* Setup DMA for all PCI devices on that bus */
1085 list_for_each_entry(dev, &bus->devices, bus_list)
1086 pcibios_setup_new_device(dev);
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +11001087
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +11001088 /* Read default IRQs and fixup if necessary */
1089 list_for_each_entry(dev, &bus->devices, bus_list) {
1090 pci_read_irq_line(dev);
1091 if (ppc_md.pci_irq_fixup)
1092 ppc_md.pci_irq_fixup(dev);
1093 }
1094}
1095
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +00001096void __devinit pcibios_setup_bus_self(struct pci_bus *bus)
1097{
1098 /* Fix up the bus resources */
1099 if (bus->self != NULL)
1100 pcibios_fixup_bridge(bus);
1101
1102 /* Platform specific bus fixups. This is currently only used
1103 * by fsl_pci and I'm hoping getting rid of it at some point
1104 */
1105 if (ppc_md.pcibios_fixup_bus)
1106 ppc_md.pcibios_fixup_bus(bus);
1107
1108 /* Setup bus DMA mappings */
1109 if (ppc_md.pci_dma_bus_setup)
1110 ppc_md.pci_dma_bus_setup(bus);
1111}
1112
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +11001113void __devinit pcibios_fixup_bus(struct pci_bus *bus)
1114{
1115 /* When called from the generic PCI probe, read PCI<->PCI bridge
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +00001116 * bases. This isn't called when generating the PCI tree from
1117 * the OF device-tree.
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +11001118 */
1119 if (bus->self != NULL)
1120 pci_read_bridge_bases(bus);
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +00001121
1122 /* Now fixup the bus bus */
1123 pcibios_setup_bus_self(bus);
1124
1125 /* Now fixup devices on that bus */
1126 pcibios_setup_bus_devices(bus);
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +11001127}
1128EXPORT_SYMBOL(pcibios_fixup_bus);
1129
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001130static int skip_isa_ioresource_align(struct pci_dev *dev)
1131{
1132 if ((ppc_pci_flags & PPC_PCI_CAN_SKIP_ISA_ALIGN) &&
1133 !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
1134 return 1;
1135 return 0;
1136}
1137
1138/*
1139 * We need to avoid collisions with `mirrored' VGA ports
1140 * and other strange ISA hardware, so we always want the
1141 * addresses to be allocated in the 0x000-0x0ff region
1142 * modulo 0x400.
1143 *
1144 * Why? Because some silly external IO cards only decode
1145 * the low 10 bits of the IO address. The 0x00-0xff region
1146 * is reserved for motherboard devices that decode all 16
1147 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
1148 * but we want to try to avoid allocating at 0x2900-0x2bff
1149 * which might have be mirrored at 0x0100-0x03ff..
1150 */
1151void pcibios_align_resource(void *data, struct resource *res,
1152 resource_size_t size, resource_size_t align)
1153{
1154 struct pci_dev *dev = data;
1155
1156 if (res->flags & IORESOURCE_IO) {
1157 resource_size_t start = res->start;
1158
1159 if (skip_isa_ioresource_align(dev))
1160 return;
1161 if (start & 0x300) {
1162 start = (start + 0x3ff) & ~0x3ff;
1163 res->start = start;
1164 }
1165 }
1166}
1167EXPORT_SYMBOL(pcibios_align_resource);
1168
1169/*
1170 * Reparent resource children of pr that conflict with res
1171 * under res, and make res replace those children.
1172 */
1173static int __init reparent_resources(struct resource *parent,
1174 struct resource *res)
1175{
1176 struct resource *p, **pp;
1177 struct resource **firstpp = NULL;
1178
1179 for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
1180 if (p->end < res->start)
1181 continue;
1182 if (res->end < p->start)
1183 break;
1184 if (p->start < res->start || p->end > res->end)
1185 return -1; /* not completely contained */
1186 if (firstpp == NULL)
1187 firstpp = pp;
1188 }
1189 if (firstpp == NULL)
1190 return -1; /* didn't find any conflicting entries? */
1191 res->parent = parent;
1192 res->child = *firstpp;
1193 res->sibling = *pp;
1194 *firstpp = res;
1195 *pp = NULL;
1196 for (p = res->child; p != NULL; p = p->sibling) {
1197 p->parent = res;
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +00001198 pr_debug("PCI: Reparented %s [%llx..%llx] under %s\n",
1199 p->name,
1200 (unsigned long long)p->start,
1201 (unsigned long long)p->end, res->name);
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001202 }
1203 return 0;
1204}
1205
1206/*
1207 * Handle resources of PCI devices. If the world were perfect, we could
1208 * just allocate all the resource regions and do nothing more. It isn't.
1209 * On the other hand, we cannot just re-allocate all devices, as it would
1210 * require us to know lots of host bridge internals. So we attempt to
1211 * keep as much of the original configuration as possible, but tweak it
1212 * when it's found to be wrong.
1213 *
1214 * Known BIOS problems we have to work around:
1215 * - I/O or memory regions not configured
1216 * - regions configured, but not enabled in the command register
1217 * - bogus I/O addresses above 64K used
1218 * - expansion ROMs left enabled (this may sound harmless, but given
1219 * the fact the PCI specs explicitly allow address decoders to be
1220 * shared between expansion ROMs and other resource regions, it's
1221 * at least dangerous)
1222 *
1223 * Our solution:
1224 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
1225 * This gives us fixed barriers on where we can allocate.
1226 * (2) Allocate resources for all enabled devices. If there is
1227 * a collision, just mark the resource as unallocated. Also
1228 * disable expansion ROMs during this step.
1229 * (3) Try to allocate resources for disabled devices. If the
1230 * resources were assigned correctly, everything goes well,
1231 * if they weren't, they won't disturb allocation of other
1232 * resources.
1233 * (4) Assign new addresses to resources which were either
1234 * not configured at all or misconfigured. If explicitly
1235 * requested by the user, configure expansion ROM address
1236 * as well.
1237 */
1238
Nathan Fontenote90a1312008-10-27 19:48:17 +00001239void pcibios_allocate_bus_resources(struct pci_bus *bus)
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001240{
Nathan Fontenote90a1312008-10-27 19:48:17 +00001241 struct pci_bus *b;
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001242 int i;
1243 struct resource *res, *pr;
1244
Benjamin Herrenschmidtb5ae5f92008-10-27 19:48:44 +00001245 pr_debug("PCI: Allocating bus resources for %04x:%02x...\n",
1246 pci_domain_nr(bus), bus->number);
1247
Nathan Fontenote90a1312008-10-27 19:48:17 +00001248 for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
1249 if ((res = bus->resource[i]) == NULL || !res->flags
Benjamin Herrenschmidtb5ae5f92008-10-27 19:48:44 +00001250 || res->start > res->end || res->parent)
Nathan Fontenote90a1312008-10-27 19:48:17 +00001251 continue;
1252 if (bus->parent == NULL)
1253 pr = (res->flags & IORESOURCE_IO) ?
1254 &ioport_resource : &iomem_resource;
1255 else {
1256 /* Don't bother with non-root busses when
1257 * re-assigning all resources. We clear the
1258 * resource flags as if they were colliding
1259 * and as such ensure proper re-allocation
1260 * later.
1261 */
1262 if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)
1263 goto clear_resource;
1264 pr = pci_find_parent_resource(bus->self, res);
1265 if (pr == res) {
1266 /* this happens when the generic PCI
1267 * code (wrongly) decides that this
1268 * bridge is transparent -- paulus
1269 */
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001270 continue;
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001271 }
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001272 }
Nathan Fontenote90a1312008-10-27 19:48:17 +00001273
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +00001274 pr_debug("PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx "
1275 "[0x%x], parent %p (%s)\n",
1276 bus->self ? pci_name(bus->self) : "PHB",
1277 bus->number, i,
1278 (unsigned long long)res->start,
1279 (unsigned long long)res->end,
1280 (unsigned int)res->flags,
1281 pr, (pr && pr->name) ? pr->name : "nil");
Nathan Fontenote90a1312008-10-27 19:48:17 +00001282
1283 if (pr && !(pr->flags & IORESOURCE_UNSET)) {
1284 if (request_resource(pr, res) == 0)
1285 continue;
1286 /*
1287 * Must be a conflict with an existing entry.
1288 * Move that entry (or entries) under the
1289 * bridge resource and try again.
1290 */
1291 if (reparent_resources(pr, res) == 0)
1292 continue;
1293 }
1294 printk(KERN_WARNING "PCI: Cannot allocate resource region "
1295 "%d of PCI bridge %d, will remap\n", i, bus->number);
1296clear_resource:
1297 res->flags = 0;
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001298 }
Nathan Fontenote90a1312008-10-27 19:48:17 +00001299
1300 list_for_each_entry(b, &bus->children, node)
1301 pcibios_allocate_bus_resources(b);
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001302}
1303
Paul Mackerras533b1922007-12-31 10:04:15 +11001304static inline void __devinit alloc_resource(struct pci_dev *dev, int idx)
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001305{
1306 struct resource *pr, *r = &dev->resource[idx];
1307
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +00001308 pr_debug("PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n",
1309 pci_name(dev), idx,
1310 (unsigned long long)r->start,
1311 (unsigned long long)r->end,
1312 (unsigned int)r->flags);
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001313
1314 pr = pci_find_parent_resource(dev, r);
1315 if (!pr || (pr->flags & IORESOURCE_UNSET) ||
1316 request_resource(pr, r) < 0) {
1317 printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
1318 " of device %s, will remap\n", idx, pci_name(dev));
1319 if (pr)
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +00001320 pr_debug("PCI: parent is %p: %016llx-%016llx [%x]\n",
1321 pr,
1322 (unsigned long long)pr->start,
1323 (unsigned long long)pr->end,
1324 (unsigned int)pr->flags);
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001325 /* We'll assign a new address later */
1326 r->flags |= IORESOURCE_UNSET;
1327 r->end -= r->start;
1328 r->start = 0;
1329 }
1330}
1331
1332static void __init pcibios_allocate_resources(int pass)
1333{
1334 struct pci_dev *dev = NULL;
1335 int idx, disabled;
1336 u16 command;
1337 struct resource *r;
1338
1339 for_each_pci_dev(dev) {
1340 pci_read_config_word(dev, PCI_COMMAND, &command);
1341 for (idx = 0; idx < 6; idx++) {
1342 r = &dev->resource[idx];
1343 if (r->parent) /* Already allocated */
1344 continue;
1345 if (!r->flags || (r->flags & IORESOURCE_UNSET))
1346 continue; /* Not assigned at all */
1347 if (r->flags & IORESOURCE_IO)
1348 disabled = !(command & PCI_COMMAND_IO);
1349 else
1350 disabled = !(command & PCI_COMMAND_MEMORY);
Paul Mackerras533b1922007-12-31 10:04:15 +11001351 if (pass == disabled)
1352 alloc_resource(dev, idx);
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001353 }
1354 if (pass)
1355 continue;
1356 r = &dev->resource[PCI_ROM_RESOURCE];
1357 if (r->flags & IORESOURCE_ROM_ENABLE) {
1358 /* Turn the ROM off, leave the resource region,
1359 * but keep it unregistered.
1360 */
1361 u32 reg;
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +00001362 pr_debug("PCI: Switching off ROM of %s\n",
1363 pci_name(dev));
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001364 r->flags &= ~IORESOURCE_ROM_ENABLE;
1365 pci_read_config_dword(dev, dev->rom_base_reg, &reg);
1366 pci_write_config_dword(dev, dev->rom_base_reg,
1367 reg & ~PCI_ROM_ADDRESS_ENABLE);
1368 }
1369 }
1370}
1371
1372void __init pcibios_resource_survey(void)
1373{
Nathan Fontenote90a1312008-10-27 19:48:17 +00001374 struct pci_bus *b;
1375
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001376 /* Allocate and assign resources. If we re-assign everything, then
1377 * we skip the allocate phase
1378 */
Nathan Fontenote90a1312008-10-27 19:48:17 +00001379 list_for_each_entry(b, &pci_root_buses, node)
1380 pcibios_allocate_bus_resources(b);
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001381
1382 if (!(ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)) {
1383 pcibios_allocate_resources(0);
1384 pcibios_allocate_resources(1);
1385 }
1386
1387 if (!(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) {
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +00001388 pr_debug("PCI: Assigning unassigned resouces...\n");
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001389 pci_assign_unassigned_resources();
1390 }
1391
1392 /* Call machine dependent fixup */
1393 if (ppc_md.pcibios_fixup)
1394 ppc_md.pcibios_fixup();
1395}
1396
1397#ifdef CONFIG_HOTPLUG
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +00001398
Benjamin Herrenschmidtfd6852c2008-10-27 19:48:52 +00001399/* This is used by the PCI hotplug driver to allocate resource
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001400 * of newly plugged busses. We can try to consolidate with the
Benjamin Herrenschmidtfd6852c2008-10-27 19:48:52 +00001401 * rest of the code later, for now, keep it as-is as our main
1402 * resource allocation function doesn't deal with sub-trees yet.
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001403 */
1404void __devinit pcibios_claim_one_bus(struct pci_bus *bus)
1405{
1406 struct pci_dev *dev;
1407 struct pci_bus *child_bus;
1408
1409 list_for_each_entry(dev, &bus->devices, bus_list) {
1410 int i;
1411
1412 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1413 struct resource *r = &dev->resource[i];
1414
1415 if (r->parent || !r->start || !r->flags)
1416 continue;
Benjamin Herrenschmidtfd6852c2008-10-27 19:48:52 +00001417
1418 pr_debug("PCI: Claiming %s: "
1419 "Resource %d: %016llx..%016llx [%x]\n",
1420 pci_name(dev), i,
1421 (unsigned long long)r->start,
1422 (unsigned long long)r->end,
1423 (unsigned int)r->flags);
1424
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001425 pci_claim_resource(dev, i);
1426 }
1427 }
1428
1429 list_for_each_entry(child_bus, &bus->children, node)
1430 pcibios_claim_one_bus(child_bus);
1431}
1432EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
Benjamin Herrenschmidtfd6852c2008-10-27 19:48:52 +00001433
1434
1435/* pcibios_finish_adding_to_bus
1436 *
1437 * This is to be called by the hotplug code after devices have been
1438 * added to a bus, this include calling it for a PHB that is just
1439 * being added
1440 */
1441void pcibios_finish_adding_to_bus(struct pci_bus *bus)
1442{
1443 pr_debug("PCI: Finishing adding to hotplug bus %04x:%02x\n",
1444 pci_domain_nr(bus), bus->number);
1445
1446 /* Allocate bus and devices resources */
1447 pcibios_allocate_bus_resources(bus);
1448 pcibios_claim_one_bus(bus);
1449
1450 /* Add new devices to global lists. Register in proc, sysfs. */
1451 pci_bus_add_devices(bus);
1452
1453 /* Fixup EEH */
1454 eeh_add_device_tree_late(bus);
1455}
1456EXPORT_SYMBOL_GPL(pcibios_finish_adding_to_bus);
1457
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001458#endif /* CONFIG_HOTPLUG */
Benjamin Herrenschmidt549beb92007-12-20 14:54:57 +11001459
1460int pcibios_enable_device(struct pci_dev *dev, int mask)
1461{
Benjamin Herrenschmidt549beb92007-12-20 14:54:57 +11001462 if (ppc_md.pcibios_enable_device_hook)
1463 if (ppc_md.pcibios_enable_device_hook(dev))
1464 return -EINVAL;
1465
Bjorn Helgaas7cfb5f92008-03-04 11:56:56 -07001466 return pci_enable_resources(dev, mask);
Benjamin Herrenschmidt549beb92007-12-20 14:54:57 +11001467}
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +00001468
1469void __devinit pcibios_setup_phb_resources(struct pci_controller *hose)
1470{
1471 struct pci_bus *bus = hose->bus;
1472 struct resource *res;
1473 int i;
1474
1475 /* Hookup PHB IO resource */
1476 bus->resource[0] = res = &hose->io_resource;
1477
1478 if (!res->flags) {
1479 printk(KERN_WARNING "PCI: I/O resource not set for host"
1480 " bridge %s (domain %d)\n",
1481 hose->dn->full_name, hose->global_number);
1482#ifdef CONFIG_PPC32
1483 /* Workaround for lack of IO resource only on 32-bit */
1484 res->start = (unsigned long)hose->io_base_virt - isa_io_base;
1485 res->end = res->start + IO_SPACE_LIMIT;
1486 res->flags = IORESOURCE_IO;
1487#endif /* CONFIG_PPC32 */
1488 }
1489
1490 pr_debug("PCI: PHB IO resource = %016llx-%016llx [%lx]\n",
1491 (unsigned long long)res->start,
1492 (unsigned long long)res->end,
1493 (unsigned long)res->flags);
1494
1495 /* Hookup PHB Memory resources */
1496 for (i = 0; i < 3; ++i) {
1497 res = &hose->mem_resources[i];
1498 if (!res->flags) {
1499 if (i > 0)
1500 continue;
1501 printk(KERN_ERR "PCI: Memory resource 0 not set for "
1502 "host bridge %s (domain %d)\n",
1503 hose->dn->full_name, hose->global_number);
1504#ifdef CONFIG_PPC32
1505 /* Workaround for lack of MEM resource only on 32-bit */
1506 res->start = hose->pci_mem_offset;
1507 res->end = (resource_size_t)-1LL;
1508 res->flags = IORESOURCE_MEM;
1509#endif /* CONFIG_PPC32 */
1510 }
1511 bus->resource[i+1] = res;
1512
1513 pr_debug("PCI: PHB MEM resource %d = %016llx-%016llx [%lx]\n", i,
1514 (unsigned long long)res->start,
1515 (unsigned long long)res->end,
1516 (unsigned long)res->flags);
1517 }
1518
1519 pr_debug("PCI: PHB MEM offset = %016llx\n",
1520 (unsigned long long)hose->pci_mem_offset);
1521 pr_debug("PCI: PHB IO offset = %08lx\n",
1522 (unsigned long)hose->io_base_virt - _IO_BASE);
1523
1524}
1525