blob: 7c63811fcdab635b7881fbcc1718bfe6b4f67cac [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
Kumar Gala5516b542007-06-27 01:17:57 -050019#include <linux/kernel.h>
20#include <linux/pci.h>
21#include <linux/string.h>
22#include <linux/init.h>
Gavin Shand92a2082014-04-24 18:00:24 +100023#include <linux/delay.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040024#include <linux/export.h>
Grant Likely22ae7822010-07-29 11:49:01 -060025#include <linux/of_address.h>
Sebastian Andrzej Siewior04bea682011-01-24 09:58:55 +053026#include <linux/of_pci.h>
Kumar Gala5516b542007-06-27 01:17:57 -050027#include <linux/mm.h>
28#include <linux/list.h>
29#include <linux/syscalls.h>
30#include <linux/irq.h>
31#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Brian Kingc2e1d842013-04-08 03:05:10 +000033#include <linux/vgaarb.h>
Kumar Gala5516b542007-06-27 01:17:57 -050034
35#include <asm/processor.h>
36#include <asm/io.h>
37#include <asm/prom.h>
38#include <asm/pci-bridge.h>
39#include <asm/byteorder.h>
40#include <asm/machdep.h>
41#include <asm/ppc-pci.h>
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +000042#include <asm/eeh.h>
Kumar Gala5516b542007-06-27 01:17:57 -050043
Guilherme G. Piccoli63a72282016-06-29 15:14:22 -030044/* hose_spinlock protects accesses to the the phb_bitmap. */
Kumar Galaa4c9e322007-06-27 13:09:43 -050045static DEFINE_SPINLOCK(hose_spinlock);
Milton Millerc3bd5172009-01-08 02:19:46 +000046LIST_HEAD(hose_list);
Kumar Galaa4c9e322007-06-27 13:09:43 -050047
Guilherme G. Piccoli63a72282016-06-29 15:14:22 -030048/* For dynamic PHB numbering on get_phb_number(): max number of PHBs. */
49#define MAX_PHBS 0x10000
50
51/*
52 * For dynamic PHB numbering: used/free PHBs tracking bitmap.
53 * Accesses to this bitmap should be protected by hose_spinlock.
54 */
55static DECLARE_BITMAP(phb_bitmap, MAX_PHBS);
Kumar Galaa4c9e322007-06-27 13:09:43 -050056
Benjamin Herrenschmidt25e81f92007-12-11 14:48:17 +110057/* ISA Memory physical address */
58resource_size_t isa_mem_base;
Al Viro9445aa12016-01-13 23:33:46 -050059EXPORT_SYMBOL(isa_mem_base);
Benjamin Herrenschmidt25e81f92007-12-11 14:48:17 +110060
Kumar Galaa4c9e322007-06-27 13:09:43 -050061
FUJITA Tomonori45223c52009-08-04 19:08:25 +000062static struct dma_map_ops *pci_dma_ops = &dma_direct_ops;
Becky Bruce4fc665b2008-09-12 10:34:46 +000063
FUJITA Tomonori45223c52009-08-04 19:08:25 +000064void set_pci_dma_ops(struct dma_map_ops *dma_ops)
Becky Bruce4fc665b2008-09-12 10:34:46 +000065{
66 pci_dma_ops = dma_ops;
67}
68
FUJITA Tomonori45223c52009-08-04 19:08:25 +000069struct dma_map_ops *get_pci_dma_ops(void)
Becky Bruce4fc665b2008-09-12 10:34:46 +000070{
71 return pci_dma_ops;
72}
73EXPORT_SYMBOL(get_pci_dma_ops);
74
Guilherme G. Piccoli63a72282016-06-29 15:14:22 -030075/*
76 * This function should run under locking protection, specifically
77 * hose_spinlock.
78 */
79static int get_phb_number(struct device_node *dn)
80{
81 int ret, phb_id = -1;
82 u64 prop;
83
84 /*
85 * Try fixed PHB numbering first, by checking archs and reading
86 * the respective device-tree properties. Firstly, try powernv by
87 * reading "ibm,opal-phbid", only present in OPAL environment.
88 */
89 ret = of_property_read_u64(dn, "ibm,opal-phbid", &prop);
90 if (ret)
91 ret = of_property_read_u32_index(dn, "reg", 1, (u32 *)&prop);
92
93 if (!ret)
94 phb_id = (int)(prop & (MAX_PHBS - 1));
95
96 /* We need to be sure to not use the same PHB number twice. */
97 if ((phb_id >= 0) && !test_and_set_bit(phb_id, phb_bitmap))
98 return phb_id;
99
100 /*
101 * If not pseries nor powernv, or if fixed PHB numbering tried to add
102 * the same PHB number twice, then fallback to dynamic PHB numbering.
103 */
104 phb_id = find_first_zero_bit(phb_bitmap, MAX_PHBS);
105 BUG_ON(phb_id >= MAX_PHBS);
106 set_bit(phb_id, phb_bitmap);
107
108 return phb_id;
109}
110
Stephen Rothwelle60516e2007-12-11 11:02:07 +1100111struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
Kumar Galaa4c9e322007-06-27 13:09:43 -0500112{
113 struct pci_controller *phb;
114
Stephen Rothwelle60516e2007-12-11 11:02:07 +1100115 phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
Kumar Galaa4c9e322007-06-27 13:09:43 -0500116 if (phb == NULL)
117 return NULL;
Stephen Rothwelle60516e2007-12-11 11:02:07 +1100118 spin_lock(&hose_spinlock);
Guilherme G. Piccoli63a72282016-06-29 15:14:22 -0300119 phb->global_number = get_phb_number(dev);
Stephen Rothwelle60516e2007-12-11 11:02:07 +1100120 list_add_tail(&phb->list_node, &hose_list);
121 spin_unlock(&hose_spinlock);
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100122 phb->dn = dev;
Michael Ellermanf691fa12015-03-30 14:10:37 +1100123 phb->is_dynamic = slab_is_available();
Kumar Galaa4c9e322007-06-27 13:09:43 -0500124#ifdef CONFIG_PPC64
125 if (dev) {
126 int nid = of_node_to_nid(dev);
127
128 if (nid < 0 || !node_online(nid))
129 nid = -1;
130
131 PHB_SET_NODE(phb, nid);
132 }
133#endif
134 return phb;
135}
Daniel Axtens5b64d2c2015-05-27 16:06:56 +1000136EXPORT_SYMBOL_GPL(pcibios_alloc_controller);
Kumar Galaa4c9e322007-06-27 13:09:43 -0500137
138void pcibios_free_controller(struct pci_controller *phb)
139{
140 spin_lock(&hose_spinlock);
Guilherme G. Piccoli63a72282016-06-29 15:14:22 -0300141
142 /* Clear bit of phb_bitmap to allow reuse of this PHB number. */
143 if (phb->global_number < MAX_PHBS)
144 clear_bit(phb->global_number, phb_bitmap);
145
Kumar Galaa4c9e322007-06-27 13:09:43 -0500146 list_del(&phb->list_node);
147 spin_unlock(&hose_spinlock);
148
149 if (phb->is_dynamic)
150 kfree(phb);
151}
Andrew Donnellan6b8b2522015-09-10 16:28:34 +1000152EXPORT_SYMBOL_GPL(pcibios_free_controller);
Kumar Galaa4c9e322007-06-27 13:09:43 -0500153
Gavin Shan4c2245b2012-09-11 16:59:46 -0600154/*
155 * The function is used to return the minimal alignment
156 * for memory or I/O windows of the associated P2P bridge.
157 * By default, 4KiB alignment for I/O windows and 1MiB for
158 * memory windows.
159 */
160resource_size_t pcibios_window_alignment(struct pci_bus *bus,
161 unsigned long type)
162{
Daniel Axtens467efc22015-03-31 16:00:56 +1100163 struct pci_controller *phb = pci_bus_to_host(bus);
164
165 if (phb->controller_ops.window_alignment)
166 return phb->controller_ops.window_alignment(bus, type);
167
168 /*
169 * PCI core will figure out the default
170 * alignment: 4KiB for I/O and 1MiB for
171 * memory window.
172 */
173 return 1;
Gavin Shan4c2245b2012-09-11 16:59:46 -0600174}
175
Gavin Shanc5fcb292016-05-20 16:41:26 +1000176void pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
177{
178 struct pci_controller *hose = pci_bus_to_host(bus);
179
180 if (hose->controller_ops.setup_bridge)
181 hose->controller_ops.setup_bridge(bus, type);
182}
183
Gavin Shand92a2082014-04-24 18:00:24 +1000184void pcibios_reset_secondary_bus(struct pci_dev *dev)
185{
Daniel Axtens467efc22015-03-31 16:00:56 +1100186 struct pci_controller *phb = pci_bus_to_host(dev->bus);
187
188 if (phb->controller_ops.reset_secondary_bus) {
189 phb->controller_ops.reset_secondary_bus(dev);
190 return;
191 }
192
193 pci_reset_secondary_bus(dev);
Gavin Shand92a2082014-04-24 18:00:24 +1000194}
195
Wei Yang5350ab32015-03-25 16:23:56 +0800196#ifdef CONFIG_PCI_IOV
197resource_size_t pcibios_iov_resource_alignment(struct pci_dev *pdev, int resno)
198{
199 if (ppc_md.pcibios_iov_resource_alignment)
200 return ppc_md.pcibios_iov_resource_alignment(pdev, resno);
201
202 return pci_iov_resource_size(pdev, resno);
203}
204#endif /* CONFIG_PCI_IOV */
205
Milton Millerc3bd5172009-01-08 02:19:46 +0000206static resource_size_t pcibios_io_size(const struct pci_controller *hose)
207{
208#ifdef CONFIG_PPC64
209 return hose->pci_io_size;
210#else
Joe Perches28f65c112011-06-09 09:13:32 -0700211 return resource_size(&hose->io_resource);
Milton Millerc3bd5172009-01-08 02:19:46 +0000212#endif
213}
214
Benjamin Herrenschmidt6dfbde22007-07-26 14:07:13 +1000215int pcibios_vaddr_is_ioport(void __iomem *address)
216{
217 int ret = 0;
218 struct pci_controller *hose;
Milton Millerc3bd5172009-01-08 02:19:46 +0000219 resource_size_t size;
Benjamin Herrenschmidt6dfbde22007-07-26 14:07:13 +1000220
221 spin_lock(&hose_spinlock);
222 list_for_each_entry(hose, &hose_list, list_node) {
Milton Millerc3bd5172009-01-08 02:19:46 +0000223 size = pcibios_io_size(hose);
Benjamin Herrenschmidt6dfbde22007-07-26 14:07:13 +1000224 if (address >= hose->io_base_virt &&
225 address < (hose->io_base_virt + size)) {
226 ret = 1;
227 break;
228 }
229 }
230 spin_unlock(&hose_spinlock);
231 return ret;
232}
233
Milton Millerc3bd5172009-01-08 02:19:46 +0000234unsigned long pci_address_to_pio(phys_addr_t address)
235{
236 struct pci_controller *hose;
237 resource_size_t size;
238 unsigned long ret = ~0;
239
240 spin_lock(&hose_spinlock);
241 list_for_each_entry(hose, &hose_list, list_node) {
242 size = pcibios_io_size(hose);
243 if (address >= hose->io_base_phys &&
244 address < (hose->io_base_phys + size)) {
245 unsigned long base =
246 (unsigned long)hose->io_base_virt - _IO_BASE;
247 ret = base + (address - hose->io_base_phys);
248 break;
249 }
250 }
251 spin_unlock(&hose_spinlock);
252
253 return ret;
254}
255EXPORT_SYMBOL_GPL(pci_address_to_pio);
256
Kumar Gala5516b542007-06-27 01:17:57 -0500257/*
258 * Return the domain number for this bus.
259 */
260int pci_domain_nr(struct pci_bus *bus)
261{
Stephen Rothwell6207e812007-12-07 02:04:33 +1100262 struct pci_controller *hose = pci_bus_to_host(bus);
Kumar Gala5516b542007-06-27 01:17:57 -0500263
Stephen Rothwell6207e812007-12-07 02:04:33 +1100264 return hose->global_number;
Kumar Gala5516b542007-06-27 01:17:57 -0500265}
Kumar Gala5516b542007-06-27 01:17:57 -0500266EXPORT_SYMBOL(pci_domain_nr);
Kumar Gala58083da2007-06-27 11:07:51 -0500267
Kumar Galaa4c9e322007-06-27 13:09:43 -0500268/* This routine is meant to be used early during boot, when the
269 * PCI bus numbers have not yet been assigned, and you need to
270 * issue PCI config cycles to an OF device.
271 * It could also be used to "fix" RTAS config cycles if you want
272 * to set pci_assign_all_buses to 1 and still use RTAS for PCI
273 * config cycles.
274 */
275struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
276{
Kumar Galaa4c9e322007-06-27 13:09:43 -0500277 while(node) {
278 struct pci_controller *hose, *tmp;
279 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100280 if (hose->dn == node)
Kumar Galaa4c9e322007-06-27 13:09:43 -0500281 return hose;
282 node = node->parent;
283 }
284 return NULL;
285}
286
Kumar Gala58083da2007-06-27 11:07:51 -0500287/*
288 * Reads the interrupt pin to determine if interrupt is use by card.
289 * If the interrupt is used, then gets the interrupt line from the
290 * openfirmware and sets it in the pci_dev and pci_config line.
291 */
Benjamin Herrenschmidt4666ca22011-11-29 20:16:25 +0000292static int pci_read_irq_line(struct pci_dev *pci_dev)
Kumar Gala58083da2007-06-27 11:07:51 -0500293{
Grant Likely530210c2013-09-15 16:39:11 +0100294 struct of_phandle_args oirq;
Kumar Gala58083da2007-06-27 11:07:51 -0500295 unsigned int virq;
296
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000297 pr_debug("PCI: Try to map irq for %s...\n", pci_name(pci_dev));
Kumar Gala58083da2007-06-27 11:07:51 -0500298
299#ifdef DEBUG
300 memset(&oirq, 0xff, sizeof(oirq));
301#endif
302 /* Try to get a mapping from the device-tree */
Grant Likely0c02c802013-09-19 11:22:36 -0500303 if (of_irq_parse_pci(pci_dev, &oirq)) {
Kumar Gala58083da2007-06-27 11:07:51 -0500304 u8 line, pin;
305
306 /* If that fails, lets fallback to what is in the config
307 * space and map that through the default controller. We
308 * also set the type to level low since that's what PCI
309 * interrupts are. If your platform does differently, then
310 * either provide a proper interrupt tree or don't use this
311 * function.
312 */
313 if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
314 return -1;
315 if (pin == 0)
316 return -1;
317 if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
Benjamin Herrenschmidt54a24cb2007-12-20 15:10:02 +1100318 line == 0xff || line == 0) {
Kumar Gala58083da2007-06-27 11:07:51 -0500319 return -1;
320 }
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000321 pr_debug(" No map ! Using line %d (pin %d) from PCI config\n",
322 line, pin);
Kumar Gala58083da2007-06-27 11:07:51 -0500323
324 virq = irq_create_mapping(NULL, line);
325 if (virq != NO_IRQ)
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100326 irq_set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
Kumar Gala58083da2007-06-27 11:07:51 -0500327 } else {
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000328 pr_debug(" Got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
Grant Likely530210c2013-09-15 16:39:11 +0100329 oirq.args_count, oirq.args[0], oirq.args[1],
330 of_node_full_name(oirq.np));
Kumar Gala58083da2007-06-27 11:07:51 -0500331
Grant Likelye6d30ab2013-09-15 16:55:53 +0100332 virq = irq_create_of_mapping(&oirq);
Kumar Gala58083da2007-06-27 11:07:51 -0500333 }
334 if(virq == NO_IRQ) {
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000335 pr_debug(" Failed to map !\n");
Kumar Gala58083da2007-06-27 11:07:51 -0500336 return -1;
337 }
338
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000339 pr_debug(" Mapped to linux irq %d\n", virq);
Kumar Gala58083da2007-06-27 11:07:51 -0500340
341 pci_dev->irq = virq;
342
343 return 0;
344}
Kumar Gala58083da2007-06-27 11:07:51 -0500345
346/*
347 * Platform support for /proc/bus/pci/X/Y mmap()s,
348 * modelled on the sparc64 implementation by Dave Miller.
349 * -- paulus.
350 */
351
352/*
353 * Adjust vm_pgoff of VMA such that it is the physical page offset
354 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
355 *
356 * Basically, the user finds the base address for his device which he wishes
357 * to mmap. They read the 32-bit value from the config space base register,
358 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
359 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
360 *
361 * Returns negative error code on failure, zero on success.
362 */
363static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
364 resource_size_t *offset,
365 enum pci_mmap_state mmap_state)
366{
367 struct pci_controller *hose = pci_bus_to_host(dev->bus);
368 unsigned long io_offset = 0;
369 int i, res_bit;
370
Anton Blanchardb0d436c2013-08-07 02:01:24 +1000371 if (hose == NULL)
Kumar Gala58083da2007-06-27 11:07:51 -0500372 return NULL; /* should never happen */
373
374 /* If memory, add on the PCI bridge address offset */
375 if (mmap_state == pci_mmap_mem) {
376#if 0 /* See comment in pci_resource_to_user() for why this is disabled */
377 *offset += hose->pci_mem_offset;
378#endif
379 res_bit = IORESOURCE_MEM;
380 } else {
381 io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
382 *offset += io_offset;
383 res_bit = IORESOURCE_IO;
384 }
385
386 /*
387 * Check that the offset requested corresponds to one of the
388 * resources of the device.
389 */
390 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
391 struct resource *rp = &dev->resource[i];
392 int flags = rp->flags;
393
394 /* treat ROM as memory (should be already) */
395 if (i == PCI_ROM_RESOURCE)
396 flags |= IORESOURCE_MEM;
397
398 /* Active and same type? */
399 if ((flags & res_bit) == 0)
400 continue;
401
402 /* In the range of this resource? */
403 if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
404 continue;
405
406 /* found it! construct the final physical address */
407 if (mmap_state == pci_mmap_io)
408 *offset += hose->io_base_phys - io_offset;
409 return rp;
410 }
411
412 return NULL;
413}
414
415/*
Kumar Gala58083da2007-06-27 11:07:51 -0500416 * This one is used by /dev/mem and fbdev who have no clue about the
417 * PCI device, it tries to find the PCI device first and calls the
418 * above routine
419 */
420pgprot_t pci_phys_mem_access_prot(struct file *file,
421 unsigned long pfn,
422 unsigned long size,
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000423 pgprot_t prot)
Kumar Gala58083da2007-06-27 11:07:51 -0500424{
425 struct pci_dev *pdev = NULL;
426 struct resource *found = NULL;
Benjamin Herrenschmidt7c12d902008-10-01 15:30:04 +0000427 resource_size_t offset = ((resource_size_t)pfn) << PAGE_SHIFT;
Kumar Gala58083da2007-06-27 11:07:51 -0500428 int i;
429
430 if (page_is_ram(pfn))
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000431 return prot;
Kumar Gala58083da2007-06-27 11:07:51 -0500432
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000433 prot = pgprot_noncached(prot);
Kumar Gala58083da2007-06-27 11:07:51 -0500434 for_each_pci_dev(pdev) {
435 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
436 struct resource *rp = &pdev->resource[i];
437 int flags = rp->flags;
438
439 /* Active and same type? */
440 if ((flags & IORESOURCE_MEM) == 0)
441 continue;
442 /* In the range of this resource? */
443 if (offset < (rp->start & PAGE_MASK) ||
444 offset > rp->end)
445 continue;
446 found = rp;
447 break;
448 }
449 if (found)
450 break;
451 }
452 if (found) {
453 if (found->flags & IORESOURCE_PREFETCH)
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000454 prot = pgprot_noncached_wc(prot);
Kumar Gala58083da2007-06-27 11:07:51 -0500455 pci_dev_put(pdev);
456 }
457
Benjamin Herrenschmidtb0494bc2008-10-27 19:48:22 +0000458 pr_debug("PCI: Non-PCI map for %llx, prot: %lx\n",
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000459 (unsigned long long)offset, pgprot_val(prot));
Kumar Gala58083da2007-06-27 11:07:51 -0500460
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000461 return prot;
Kumar Gala58083da2007-06-27 11:07:51 -0500462}
463
464
465/*
466 * Perform the actual remap of the pages for a PCI device mapping, as
467 * appropriate for this architecture. The region in the process to map
468 * is described by vm_start and vm_end members of VMA, the base physical
469 * address is found in vm_pgoff.
470 * The pci device structure is provided so that architectures may make mapping
471 * decisions on a per-device or per-bus basis.
472 *
473 * Returns a negative error code on failure, zero on success.
474 */
475int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
476 enum pci_mmap_state mmap_state, int write_combine)
477{
Benjamin Herrenschmidt7c12d902008-10-01 15:30:04 +0000478 resource_size_t offset =
479 ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
Kumar Gala58083da2007-06-27 11:07:51 -0500480 struct resource *rp;
481 int ret;
482
483 rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
484 if (rp == NULL)
485 return -EINVAL;
486
487 vma->vm_pgoff = offset >> PAGE_SHIFT;
Yinghai Lu1e70cdd2016-06-17 14:43:33 -0500488 if (write_combine)
489 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
490 else
491 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Kumar Gala58083da2007-06-27 11:07:51 -0500492
493 ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
494 vma->vm_end - vma->vm_start, vma->vm_page_prot);
495
496 return ret;
497}
498
Benjamin Herrenschmidte9f82cb2008-10-14 11:55:31 +1100499/* This provides legacy IO read access on a bus */
500int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
501{
502 unsigned long offset;
503 struct pci_controller *hose = pci_bus_to_host(bus);
504 struct resource *rp = &hose->io_resource;
505 void __iomem *addr;
506
507 /* Check if port can be supported by that bus. We only check
508 * the ranges of the PHB though, not the bus itself as the rules
509 * for forwarding legacy cycles down bridges are not our problem
510 * here. So if the host bridge supports it, we do it.
511 */
512 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
513 offset += port;
514
515 if (!(rp->flags & IORESOURCE_IO))
516 return -ENXIO;
517 if (offset < rp->start || (offset + size) > rp->end)
518 return -ENXIO;
519 addr = hose->io_base_virt + port;
520
521 switch(size) {
522 case 1:
523 *((u8 *)val) = in_8(addr);
524 return 1;
525 case 2:
526 if (port & 1)
527 return -EINVAL;
528 *((u16 *)val) = in_le16(addr);
529 return 2;
530 case 4:
531 if (port & 3)
532 return -EINVAL;
533 *((u32 *)val) = in_le32(addr);
534 return 4;
535 }
536 return -EINVAL;
537}
538
539/* This provides legacy IO write access on a bus */
540int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
541{
542 unsigned long offset;
543 struct pci_controller *hose = pci_bus_to_host(bus);
544 struct resource *rp = &hose->io_resource;
545 void __iomem *addr;
546
547 /* Check if port can be supported by that bus. We only check
548 * the ranges of the PHB though, not the bus itself as the rules
549 * for forwarding legacy cycles down bridges are not our problem
550 * here. So if the host bridge supports it, we do it.
551 */
552 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
553 offset += port;
554
555 if (!(rp->flags & IORESOURCE_IO))
556 return -ENXIO;
557 if (offset < rp->start || (offset + size) > rp->end)
558 return -ENXIO;
559 addr = hose->io_base_virt + port;
560
561 /* WARNING: The generic code is idiotic. It gets passed a pointer
562 * to what can be a 1, 2 or 4 byte quantity and always reads that
563 * as a u32, which means that we have to correct the location of
564 * the data read within those 32 bits for size 1 and 2
565 */
566 switch(size) {
567 case 1:
568 out_8(addr, val >> 24);
569 return 1;
570 case 2:
571 if (port & 1)
572 return -EINVAL;
573 out_le16(addr, val >> 16);
574 return 2;
575 case 4:
576 if (port & 3)
577 return -EINVAL;
578 out_le32(addr, val);
579 return 4;
580 }
581 return -EINVAL;
582}
583
584/* This provides legacy IO or memory mmap access on a bus */
585int pci_mmap_legacy_page_range(struct pci_bus *bus,
586 struct vm_area_struct *vma,
587 enum pci_mmap_state mmap_state)
588{
589 struct pci_controller *hose = pci_bus_to_host(bus);
590 resource_size_t offset =
591 ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
592 resource_size_t size = vma->vm_end - vma->vm_start;
593 struct resource *rp;
594
595 pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
596 pci_domain_nr(bus), bus->number,
597 mmap_state == pci_mmap_mem ? "MEM" : "IO",
598 (unsigned long long)offset,
599 (unsigned long long)(offset + size - 1));
600
601 if (mmap_state == pci_mmap_mem) {
Benjamin Herrenschmidt5b11abf2009-02-08 14:27:21 +0000602 /* Hack alert !
603 *
604 * Because X is lame and can fail starting if it gets an error trying
605 * to mmap legacy_mem (instead of just moving on without legacy memory
606 * access) we fake it here by giving it anonymous memory, effectively
607 * behaving just like /dev/zero
608 */
609 if ((offset + size) > hose->isa_mem_size) {
610 printk(KERN_DEBUG
611 "Process %s (pid:%d) mapped non-existing PCI legacy memory for 0%04x:%02x\n",
612 current->comm, current->pid, pci_domain_nr(bus), bus->number);
613 if (vma->vm_flags & VM_SHARED)
614 return shmem_zero_setup(vma);
615 return 0;
616 }
Benjamin Herrenschmidte9f82cb2008-10-14 11:55:31 +1100617 offset += hose->isa_mem_phys;
618 } else {
619 unsigned long io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
620 unsigned long roffset = offset + io_offset;
621 rp = &hose->io_resource;
622 if (!(rp->flags & IORESOURCE_IO))
623 return -ENXIO;
624 if (roffset < rp->start || (roffset + size) > rp->end)
625 return -ENXIO;
626 offset += hose->io_base_phys;
627 }
628 pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset);
629
630 vma->vm_pgoff = offset >> PAGE_SHIFT;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000631 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Benjamin Herrenschmidte9f82cb2008-10-14 11:55:31 +1100632 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
633 vma->vm_end - vma->vm_start,
634 vma->vm_page_prot);
635}
636
Kumar Gala58083da2007-06-27 11:07:51 -0500637void pci_resource_to_user(const struct pci_dev *dev, int bar,
638 const struct resource *rsrc,
639 resource_size_t *start, resource_size_t *end)
640{
Bjorn Helgaas38301352016-06-17 14:43:34 -0500641 struct pci_bus_region region;
Kumar Gala58083da2007-06-27 11:07:51 -0500642
Bjorn Helgaas38301352016-06-17 14:43:34 -0500643 if (rsrc->flags & IORESOURCE_IO) {
644 pcibios_resource_to_bus(dev->bus, &region,
645 (struct resource *) rsrc);
646 *start = region.start;
647 *end = region.end;
Kumar Gala58083da2007-06-27 11:07:51 -0500648 return;
Bjorn Helgaas38301352016-06-17 14:43:34 -0500649 }
Kumar Gala58083da2007-06-27 11:07:51 -0500650
Bjorn Helgaas38301352016-06-17 14:43:34 -0500651 /* We pass a CPU physical address to userland for MMIO instead of a
652 * BAR value because X is lame and expects to be able to use that
653 * to pass to /dev/mem!
Kumar Gala58083da2007-06-27 11:07:51 -0500654 *
Bjorn Helgaas38301352016-06-17 14:43:34 -0500655 * That means we may have 64-bit values where some apps only expect
656 * 32 (like X itself since it thinks only Sparc has 64-bit MMIO).
Kumar Gala58083da2007-06-27 11:07:51 -0500657 */
Bjorn Helgaas38301352016-06-17 14:43:34 -0500658 *start = rsrc->start;
659 *end = rsrc->end;
Kumar Gala58083da2007-06-27 11:07:51 -0500660}
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100661
662/**
663 * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
664 * @hose: newly allocated pci_controller to be setup
665 * @dev: device node of the host bridge
666 * @primary: set if primary bus (32 bits only, soon to be deprecated)
667 *
668 * This function will parse the "ranges" property of a PCI host bridge device
669 * node and setup the resource mapping of a pci controller based on its
670 * content.
671 *
672 * Life would be boring if it wasn't for a few issues that we have to deal
673 * with here:
674 *
675 * - We can only cope with one IO space range and up to 3 Memory space
676 * ranges. However, some machines (thanks Apple !) tend to split their
677 * space into lots of small contiguous ranges. So we have to coalesce.
678 *
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100679 * - 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 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800686void pci_process_bridge_OF_ranges(struct pci_controller *hose,
687 struct device_node *dev, int primary)
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100688{
Kevin Hao858957a2013-05-16 20:58:42 +0000689 int memno = 0;
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100690 struct resource *res;
Andrew Murray654837e2014-02-25 06:32:11 +0000691 struct of_pci_range range;
692 struct of_pci_range_parser parser;
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100693
694 printk(KERN_INFO "PCI host bridge %s %s ranges:\n",
695 dev->full_name, primary ? "(primary)" : "");
696
Andrew Murray654837e2014-02-25 06:32:11 +0000697 /* Check for ranges property */
698 if (of_pci_range_parser_init(&parser, dev))
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100699 return;
700
701 /* Parse it */
Andrew Murray654837e2014-02-25 06:32:11 +0000702 for_each_of_pci_range(&parser, &range) {
Benjamin Herrenschmidte9f82cb2008-10-14 11:55:31 +1100703 /* If we failed translation or got a zero-sized region
704 * (some FW try to feed us with non sensical zero sized regions
705 * such as power3 which look like some kind of attempt at exposing
706 * the VGA memory hole)
707 */
Andrew Murray654837e2014-02-25 06:32:11 +0000708 if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100709 continue;
710
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100711 /* Act based on address space type */
712 res = NULL;
Andrew Murray654837e2014-02-25 06:32:11 +0000713 switch (range.flags & IORESOURCE_TYPE_BITS) {
714 case IORESOURCE_IO:
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100715 printk(KERN_INFO
716 " IO 0x%016llx..0x%016llx -> 0x%016llx\n",
Andrew Murray654837e2014-02-25 06:32:11 +0000717 range.cpu_addr, range.cpu_addr + range.size - 1,
718 range.pci_addr);
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100719
720 /* We support only one IO range */
721 if (hose->pci_io_size) {
722 printk(KERN_INFO
723 " \\--> Skipped (too many) !\n");
724 continue;
725 }
726#ifdef CONFIG_PPC32
727 /* On 32 bits, limit I/O space to 16MB */
Andrew Murray654837e2014-02-25 06:32:11 +0000728 if (range.size > 0x01000000)
729 range.size = 0x01000000;
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100730
731 /* 32 bits needs to map IOs here */
Andrew Murray654837e2014-02-25 06:32:11 +0000732 hose->io_base_virt = ioremap(range.cpu_addr,
733 range.size);
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100734
735 /* Expect trouble if pci_addr is not 0 */
736 if (primary)
737 isa_io_base =
738 (unsigned long)hose->io_base_virt;
739#endif /* CONFIG_PPC32 */
740 /* pci_io_size and io_base_phys always represent IO
741 * space starting at 0 so we factor in pci_addr
742 */
Andrew Murray654837e2014-02-25 06:32:11 +0000743 hose->pci_io_size = range.pci_addr + range.size;
744 hose->io_base_phys = range.cpu_addr - range.pci_addr;
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100745
746 /* Build resource */
747 res = &hose->io_resource;
Andrew Murray654837e2014-02-25 06:32:11 +0000748 range.cpu_addr = range.pci_addr;
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100749 break;
Andrew Murray654837e2014-02-25 06:32:11 +0000750 case IORESOURCE_MEM:
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100751 printk(KERN_INFO
752 " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
Andrew Murray654837e2014-02-25 06:32:11 +0000753 range.cpu_addr, range.cpu_addr + range.size - 1,
754 range.pci_addr,
755 (range.pci_space & 0x40000000) ?
756 "Prefetch" : "");
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100757
758 /* We support only 3 memory ranges */
759 if (memno >= 3) {
760 printk(KERN_INFO
761 " \\--> Skipped (too many) !\n");
762 continue;
763 }
764 /* Handles ISA memory hole space here */
Andrew Murray654837e2014-02-25 06:32:11 +0000765 if (range.pci_addr == 0) {
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100766 if (primary || isa_mem_base == 0)
Andrew Murray654837e2014-02-25 06:32:11 +0000767 isa_mem_base = range.cpu_addr;
768 hose->isa_mem_phys = range.cpu_addr;
769 hose->isa_mem_size = range.size;
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100770 }
771
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100772 /* Build resource */
Andrew Murray654837e2014-02-25 06:32:11 +0000773 hose->mem_offset[memno] = range.cpu_addr -
774 range.pci_addr;
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100775 res = &hose->mem_resources[memno++];
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100776 break;
777 }
778 if (res != NULL) {
Michael Ellermanaeba3732014-10-16 12:29:46 +1100779 res->name = dev->full_name;
780 res->flags = range.flags;
781 res->start = range.cpu_addr;
782 res->end = range.cpu_addr + range.size - 1;
783 res->parent = res->child = res->sibling = NULL;
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100784 }
785 }
Benjamin Herrenschmidt13dccb92007-12-11 14:48:18 +1100786}
Benjamin Herrenschmidtfa462f22007-12-20 14:54:49 +1100787
788/* Decide whether to display the domain number in /proc */
789int pci_proc_domain(struct pci_bus *bus)
790{
791 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt1fd0f522008-10-02 14:12:51 +0000792
Rob Herring0e47ff12011-07-12 09:25:51 -0500793 if (!pci_has_flag(PCI_ENABLE_PROC_DOMAINS))
Benjamin Herrenschmidtfa462f22007-12-20 14:54:49 +1100794 return 0;
Rob Herring0e47ff12011-07-12 09:25:51 -0500795 if (pci_has_flag(PCI_COMPAT_DOMAIN_0))
Benjamin Herrenschmidtfa462f22007-12-20 14:54:49 +1100796 return hose->global_number != 0;
797 return 1;
Benjamin Herrenschmidtfa462f22007-12-20 14:54:49 +1100798}
799
Kleber Sacilotto de Souzad82fb312013-05-03 12:43:12 +0000800int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
801{
802 if (ppc_md.pcibios_root_bridge_prepare)
803 return ppc_md.pcibios_root_bridge_prepare(bridge);
804
805 return 0;
806}
807
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100808/* This header fixup will do the resource fixup for all devices as they are
809 * probed, but not for bridge ranges
810 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800811static void pcibios_fixup_resources(struct pci_dev *dev)
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100812{
813 struct pci_controller *hose = pci_bus_to_host(dev->bus);
814 int i;
815
816 if (!hose) {
817 printk(KERN_ERR "No host bridge for PCI dev %s !\n",
818 pci_name(dev));
819 return;
820 }
Wei Yangc3b80fb2015-03-25 16:23:53 +0800821
822 if (dev->is_virtfn)
823 return;
824
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100825 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
826 struct resource *res = dev->resource + i;
Kevin Haoc5df4572013-06-05 02:26:51 +0000827 struct pci_bus_region reg;
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100828 if (!res->flags)
829 continue;
Benjamin Herrenschmidt48c2ce92011-11-06 18:55:58 +0000830
831 /* If we're going to re-assign everything, we mark all resources
832 * as unset (and 0-base them). In addition, we mark BARs starting
833 * at 0 as unset as well, except if PCI_PROBE_ONLY is also set
834 * since in that case, we don't want to re-assign anything
Benjamin Herrenschmidt7f172892008-02-29 14:58:03 +1100835 */
Yinghai Lufc279852013-12-09 22:54:40 -0800836 pcibios_resource_to_bus(dev->bus, &reg, res);
Benjamin Herrenschmidt48c2ce92011-11-06 18:55:58 +0000837 if (pci_has_flag(PCI_REASSIGN_ALL_RSRC) ||
Kevin Haoc5df4572013-06-05 02:26:51 +0000838 (reg.start == 0 && !pci_has_flag(PCI_PROBE_ONLY))) {
Benjamin Herrenschmidt48c2ce92011-11-06 18:55:58 +0000839 /* Only print message if not re-assigning */
840 if (!pci_has_flag(PCI_REASSIGN_ALL_RSRC))
Kevin Haoae2a84b2015-06-12 10:26:37 +0800841 pr_debug("PCI:%s Resource %d %pR is unassigned\n",
842 pci_name(dev), i, res);
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100843 res->end -= res->start;
844 res->start = 0;
845 res->flags |= IORESOURCE_UNSET;
846 continue;
847 }
848
Kevin Haoae2a84b2015-06-12 10:26:37 +0800849 pr_debug("PCI:%s Resource %d %pR\n", pci_name(dev), i, res);
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100850 }
851
852 /* Call machine specific resource fixup */
853 if (ppc_md.pcibios_fixup_resources)
854 ppc_md.pcibios_fixup_resources(dev);
855}
856DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
857
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000858/* This function tries to figure out if a bridge resource has been initialized
859 * by the firmware or not. It doesn't have to be absolutely bullet proof, but
860 * things go more smoothly when it gets it right. It should covers cases such
861 * as Apple "closed" bridge resources and bare-metal pSeries unassigned bridges
862 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800863static int pcibios_uninitialized_bridge_resource(struct pci_bus *bus,
864 struct resource *res)
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100865{
Benjamin Herrenschmidtbe8cbcd2007-12-20 14:55:04 +1100866 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +1100867 struct pci_dev *dev = bus->self;
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000868 resource_size_t offset;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +1000869 struct pci_bus_region region;
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000870 u16 command;
871 int i;
872
873 /* We don't do anything if PCI_PROBE_ONLY is set */
Rob Herring0e47ff12011-07-12 09:25:51 -0500874 if (pci_has_flag(PCI_PROBE_ONLY))
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000875 return 0;
876
877 /* Job is a bit different between memory and IO */
878 if (res->flags & IORESOURCE_MEM) {
Yinghai Lufc279852013-12-09 22:54:40 -0800879 pcibios_resource_to_bus(dev->bus, &region, res);
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +1000880
881 /* If the BAR is non-0 then it's probably been initialized */
882 if (region.start != 0)
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000883 return 0;
884
885 /* The BAR is 0, let's check if memory decoding is enabled on
886 * the bridge. If not, we consider it unassigned
887 */
888 pci_read_config_word(dev, PCI_COMMAND, &command);
889 if ((command & PCI_COMMAND_MEMORY) == 0)
890 return 1;
891
892 /* Memory decoding is enabled and the BAR is 0. If any of the bridge
893 * resources covers that starting address (0 then it's good enough for
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +1000894 * us for memory space)
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000895 */
896 for (i = 0; i < 3; i++) {
897 if ((hose->mem_resources[i].flags & IORESOURCE_MEM) &&
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +1000898 hose->mem_resources[i].start == hose->mem_offset[i])
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000899 return 0;
900 }
901
902 /* Well, it starts at 0 and we know it will collide so we may as
903 * well consider it as unassigned. That covers the Apple case.
904 */
905 return 1;
906 } else {
907 /* If the BAR is non-0, then we consider it assigned */
908 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
909 if (((res->start - offset) & 0xfffffffful) != 0)
910 return 0;
911
912 /* Here, we are a bit different than memory as typically IO space
913 * starting at low addresses -is- valid. What we do instead if that
914 * we consider as unassigned anything that doesn't have IO enabled
915 * in the PCI command register, and that's it.
916 */
917 pci_read_config_word(dev, PCI_COMMAND, &command);
918 if (command & PCI_COMMAND_IO)
919 return 0;
920
921 /* It's starting at 0 and IO is disabled in the bridge, consider
922 * it unassigned
923 */
924 return 1;
925 }
926}
927
928/* Fixup resources of a PCI<->PCI bridge */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800929static void pcibios_fixup_bridge(struct pci_bus *bus)
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000930{
931 struct resource *res;
932 int i;
933
934 struct pci_dev *dev = bus->self;
935
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700936 pci_bus_for_each_resource(bus, res, i) {
937 if (!res || !res->flags)
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000938 continue;
939 if (i >= 3 && bus->self->transparent)
940 continue;
941
Gavin Shancf1a4cf2012-06-03 22:15:25 +0000942 /* If we're going to reassign everything, we can
943 * shrink the P2P resource to have size as being
944 * of 0 in order to save space.
Benjamin Herrenschmidt48c2ce92011-11-06 18:55:58 +0000945 */
946 if (pci_has_flag(PCI_REASSIGN_ALL_RSRC)) {
947 res->flags |= IORESOURCE_UNSET;
Benjamin Herrenschmidt48c2ce92011-11-06 18:55:58 +0000948 res->start = 0;
Gavin Shancf1a4cf2012-06-03 22:15:25 +0000949 res->end = -1;
Benjamin Herrenschmidt48c2ce92011-11-06 18:55:58 +0000950 continue;
951 }
952
Kevin Haoae2a84b2015-06-12 10:26:37 +0800953 pr_debug("PCI:%s Bus rsrc %d %pR\n", pci_name(dev), i, res);
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000954
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000955 /* Try to detect uninitialized P2P bridge resources,
956 * and clear them out so they get re-assigned later
957 */
958 if (pcibios_uninitialized_bridge_resource(bus, res)) {
959 res->flags = 0;
960 pr_debug("PCI:%s (unassigned)\n", pci_name(dev));
Benjamin Herrenschmidtb5561512008-10-13 13:56:31 +0000961 }
962 }
963}
964
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800965void pcibios_setup_bus_self(struct pci_bus *bus)
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +0000966{
Daniel Axtens467efc22015-03-31 16:00:56 +1100967 struct pci_controller *phb;
968
Benjamin Herrenschmidt7eef4402008-10-27 19:48:56 +0000969 /* Fix up the bus resources for P2P bridges */
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +0000970 if (bus->self != NULL)
971 pcibios_fixup_bridge(bus);
972
973 /* Platform specific bus fixups. This is currently only used
Benjamin Herrenschmidt7eef4402008-10-27 19:48:56 +0000974 * by fsl_pci and I'm hoping to get rid of it at some point
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +0000975 */
976 if (ppc_md.pcibios_fixup_bus)
977 ppc_md.pcibios_fixup_bus(bus);
978
979 /* Setup bus DMA mappings */
Daniel Axtens467efc22015-03-31 16:00:56 +1100980 phb = pci_bus_to_host(bus);
981 if (phb->controller_ops.dma_bus_setup)
982 phb->controller_ops.dma_bus_setup(bus);
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +0000983}
984
Guenter Roeck7846de42013-06-10 10:18:08 -0700985static void pcibios_setup_device(struct pci_dev *dev)
Yuanquan Chen37f02192013-04-02 01:26:54 +0000986{
Daniel Axtens467efc22015-03-31 16:00:56 +1100987 struct pci_controller *phb;
Yuanquan Chen37f02192013-04-02 01:26:54 +0000988 /* Fixup NUMA node as it may not be setup yet by the generic
989 * code and is needed by the DMA init
990 */
991 set_dev_node(&dev->dev, pcibus_to_node(dev->bus));
992
993 /* Hook up default DMA ops */
994 set_dma_ops(&dev->dev, pci_dma_ops);
995 set_dma_offset(&dev->dev, PCI_DRAM_OFFSET);
996
997 /* Additional platform DMA/iommu setup */
Daniel Axtens467efc22015-03-31 16:00:56 +1100998 phb = pci_bus_to_host(dev->bus);
999 if (phb->controller_ops.dma_dev_setup)
1000 phb->controller_ops.dma_dev_setup(dev);
Yuanquan Chen37f02192013-04-02 01:26:54 +00001001
1002 /* Read default IRQs and fixup if necessary */
1003 pci_read_irq_line(dev);
1004 if (ppc_md.pci_irq_fixup)
1005 ppc_md.pci_irq_fixup(dev);
1006}
1007
Guenter Roeck7846de42013-06-10 10:18:08 -07001008int pcibios_add_device(struct pci_dev *dev)
1009{
1010 /*
1011 * We can only call pcibios_setup_device() after bus setup is complete,
1012 * since some of the platform specific DMA setup code depends on it.
1013 */
1014 if (dev->bus->is_added)
1015 pcibios_setup_device(dev);
Wei Yang6e628c72015-03-25 16:23:55 +08001016
1017#ifdef CONFIG_PCI_IOV
1018 if (ppc_md.pcibios_fixup_sriov)
1019 ppc_md.pcibios_fixup_sriov(dev);
1020#endif /* CONFIG_PCI_IOV */
1021
Guenter Roeck7846de42013-06-10 10:18:08 -07001022 return 0;
1023}
1024
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001025void pcibios_setup_bus_devices(struct pci_bus *bus)
Benjamin Herrenschmidt7eef4402008-10-27 19:48:56 +00001026{
1027 struct pci_dev *dev;
1028
1029 pr_debug("PCI: Fixup bus devices %d (%s)\n",
1030 bus->number, bus->self ? pci_name(bus->self) : "PHB");
1031
1032 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidt2d1c8612009-12-09 17:52:13 +11001033 /* Cardbus can call us to add new devices to a bus, so ignore
1034 * those who are already fully discovered
1035 */
1036 if (dev->is_added)
1037 continue;
1038
Yuanquan Chen37f02192013-04-02 01:26:54 +00001039 pcibios_setup_device(dev);
Benjamin Herrenschmidt7eef4402008-10-27 19:48:56 +00001040 }
1041}
1042
Myron Stowe79c8be82011-10-28 15:48:03 -06001043void pcibios_set_master(struct pci_dev *dev)
1044{
1045 /* No special bus mastering setup handling */
1046}
1047
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001048void pcibios_fixup_bus(struct pci_bus *bus)
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +11001049{
Bjorn Helgaas237865f2015-09-15 13:18:04 -05001050 /* When called from the generic PCI probe, read PCI<->PCI bridge
1051 * bases. This is -not- called when generating the PCI tree from
1052 * the OF device-tree.
1053 */
1054 pci_read_bridge_bases(bus);
1055
1056 /* Now fixup the bus bus */
Benjamin Herrenschmidt8b8da352008-10-27 19:48:37 +00001057 pcibios_setup_bus_self(bus);
1058
1059 /* Now fixup devices on that bus */
1060 pcibios_setup_bus_devices(bus);
Benjamin Herrenschmidtbf5e2ba2007-12-20 14:54:51 +11001061}
1062EXPORT_SYMBOL(pcibios_fixup_bus);
1063
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001064void pci_fixup_cardbus(struct pci_bus *bus)
Benjamin Herrenschmidt2d1c8612009-12-09 17:52:13 +11001065{
1066 /* Now fixup devices on that bus */
1067 pcibios_setup_bus_devices(bus);
1068}
1069
1070
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001071static int skip_isa_ioresource_align(struct pci_dev *dev)
1072{
Rob Herring0e47ff12011-07-12 09:25:51 -05001073 if (pci_has_flag(PCI_CAN_SKIP_ISA_ALIGN) &&
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001074 !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
1075 return 1;
1076 return 0;
1077}
1078
1079/*
1080 * We need to avoid collisions with `mirrored' VGA ports
1081 * and other strange ISA hardware, so we always want the
1082 * addresses to be allocated in the 0x000-0x0ff region
1083 * modulo 0x400.
1084 *
1085 * Why? Because some silly external IO cards only decode
1086 * the low 10 bits of the IO address. The 0x00-0xff region
1087 * is reserved for motherboard devices that decode all 16
1088 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
1089 * but we want to try to avoid allocating at 0x2900-0x2bff
1090 * which might have be mirrored at 0x0100-0x03ff..
1091 */
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +01001092resource_size_t pcibios_align_resource(void *data, const struct resource *res,
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001093 resource_size_t size, resource_size_t align)
1094{
1095 struct pci_dev *dev = data;
Dominik Brodowskib26b2d42010-01-01 17:40:49 +01001096 resource_size_t start = res->start;
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001097
1098 if (res->flags & IORESOURCE_IO) {
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001099 if (skip_isa_ioresource_align(dev))
Dominik Brodowskib26b2d42010-01-01 17:40:49 +01001100 return start;
1101 if (start & 0x300)
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001102 start = (start + 0x3ff) & ~0x3ff;
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001103 }
Dominik Brodowskib26b2d42010-01-01 17:40:49 +01001104
1105 return start;
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001106}
1107EXPORT_SYMBOL(pcibios_align_resource);
1108
1109/*
1110 * Reparent resource children of pr that conflict with res
1111 * under res, and make res replace those children.
1112 */
Heiko Schocher0f6023d2009-09-24 02:45:14 +00001113static int reparent_resources(struct resource *parent,
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001114 struct resource *res)
1115{
1116 struct resource *p, **pp;
1117 struct resource **firstpp = NULL;
1118
1119 for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
1120 if (p->end < res->start)
1121 continue;
1122 if (res->end < p->start)
1123 break;
1124 if (p->start < res->start || p->end > res->end)
1125 return -1; /* not completely contained */
1126 if (firstpp == NULL)
1127 firstpp = pp;
1128 }
1129 if (firstpp == NULL)
1130 return -1; /* didn't find any conflicting entries? */
1131 res->parent = parent;
1132 res->child = *firstpp;
1133 res->sibling = *pp;
1134 *firstpp = res;
1135 *pp = NULL;
1136 for (p = res->child; p != NULL; p = p->sibling) {
1137 p->parent = res;
Kevin Haoae2a84b2015-06-12 10:26:37 +08001138 pr_debug("PCI: Reparented %s %pR under %s\n",
1139 p->name, p, res->name);
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001140 }
1141 return 0;
1142}
1143
1144/*
1145 * Handle resources of PCI devices. If the world were perfect, we could
1146 * just allocate all the resource regions and do nothing more. It isn't.
1147 * On the other hand, we cannot just re-allocate all devices, as it would
1148 * require us to know lots of host bridge internals. So we attempt to
1149 * keep as much of the original configuration as possible, but tweak it
1150 * when it's found to be wrong.
1151 *
1152 * Known BIOS problems we have to work around:
1153 * - I/O or memory regions not configured
1154 * - regions configured, but not enabled in the command register
1155 * - bogus I/O addresses above 64K used
1156 * - expansion ROMs left enabled (this may sound harmless, but given
1157 * the fact the PCI specs explicitly allow address decoders to be
1158 * shared between expansion ROMs and other resource regions, it's
1159 * at least dangerous)
1160 *
1161 * Our solution:
1162 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
1163 * This gives us fixed barriers on where we can allocate.
1164 * (2) Allocate resources for all enabled devices. If there is
1165 * a collision, just mark the resource as unallocated. Also
1166 * disable expansion ROMs during this step.
1167 * (3) Try to allocate resources for disabled devices. If the
1168 * resources were assigned correctly, everything goes well,
1169 * if they weren't, they won't disturb allocation of other
1170 * resources.
1171 * (4) Assign new addresses to resources which were either
1172 * not configured at all or misconfigured. If explicitly
1173 * requested by the user, configure expansion ROM address
1174 * as well.
1175 */
1176
Anton Blancharde51df2c2014-08-20 08:55:18 +10001177static void pcibios_allocate_bus_resources(struct pci_bus *bus)
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001178{
Nathan Fontenote90a1312008-10-27 19:48:17 +00001179 struct pci_bus *b;
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001180 int i;
1181 struct resource *res, *pr;
1182
Benjamin Herrenschmidtb5ae5f92008-10-27 19:48:44 +00001183 pr_debug("PCI: Allocating bus resources for %04x:%02x...\n",
1184 pci_domain_nr(bus), bus->number);
1185
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -07001186 pci_bus_for_each_resource(bus, res, i) {
1187 if (!res || !res->flags || res->start > res->end || res->parent)
Nathan Fontenote90a1312008-10-27 19:48:17 +00001188 continue;
Benjamin Herrenschmidt48c2ce92011-11-06 18:55:58 +00001189
1190 /* If the resource was left unset at this point, we clear it */
1191 if (res->flags & IORESOURCE_UNSET)
1192 goto clear_resource;
1193
Nathan Fontenote90a1312008-10-27 19:48:17 +00001194 if (bus->parent == NULL)
1195 pr = (res->flags & IORESOURCE_IO) ?
1196 &ioport_resource : &iomem_resource;
1197 else {
Nathan Fontenote90a1312008-10-27 19:48:17 +00001198 pr = pci_find_parent_resource(bus->self, res);
1199 if (pr == res) {
1200 /* this happens when the generic PCI
1201 * code (wrongly) decides that this
1202 * bridge is transparent -- paulus
1203 */
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001204 continue;
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001205 }
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001206 }
Nathan Fontenote90a1312008-10-27 19:48:17 +00001207
Kevin Haoae2a84b2015-06-12 10:26:37 +08001208 pr_debug("PCI: %s (bus %d) bridge rsrc %d: %pR, parent %p (%s)\n",
1209 bus->self ? pci_name(bus->self) : "PHB", bus->number,
1210 i, res, pr, (pr && pr->name) ? pr->name : "nil");
Nathan Fontenote90a1312008-10-27 19:48:17 +00001211
1212 if (pr && !(pr->flags & IORESOURCE_UNSET)) {
Yinghai Lu3ebfe462015-01-15 16:21:51 -06001213 struct pci_dev *dev = bus->self;
1214
Nathan Fontenote90a1312008-10-27 19:48:17 +00001215 if (request_resource(pr, res) == 0)
1216 continue;
1217 /*
1218 * Must be a conflict with an existing entry.
1219 * Move that entry (or entries) under the
1220 * bridge resource and try again.
1221 */
1222 if (reparent_resources(pr, res) == 0)
1223 continue;
Yinghai Lu3ebfe462015-01-15 16:21:51 -06001224
1225 if (dev && i < PCI_BRIDGE_RESOURCE_NUM &&
1226 pci_claim_bridge_resource(dev,
1227 i + PCI_BRIDGE_RESOURCES) == 0)
1228 continue;
Nathan Fontenote90a1312008-10-27 19:48:17 +00001229 }
Benjamin Herrenschmidt48c2ce92011-11-06 18:55:58 +00001230 pr_warning("PCI: Cannot allocate resource region "
1231 "%d of PCI bridge %d, will remap\n", i, bus->number);
1232 clear_resource:
Gavin Shancf1a4cf2012-06-03 22:15:25 +00001233 /* The resource might be figured out when doing
1234 * reassignment based on the resources required
1235 * by the downstream PCI devices. Here we set
1236 * the size of the resource to be 0 in order to
1237 * save more space.
1238 */
1239 res->start = 0;
1240 res->end = -1;
Nathan Fontenote90a1312008-10-27 19:48:17 +00001241 res->flags = 0;
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001242 }
Nathan Fontenote90a1312008-10-27 19:48:17 +00001243
1244 list_for_each_entry(b, &bus->children, node)
1245 pcibios_allocate_bus_resources(b);
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001246}
1247
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001248static inline void alloc_resource(struct pci_dev *dev, int idx)
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001249{
1250 struct resource *pr, *r = &dev->resource[idx];
1251
Kevin Haoae2a84b2015-06-12 10:26:37 +08001252 pr_debug("PCI: Allocating %s: Resource %d: %pR\n",
1253 pci_name(dev), idx, r);
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001254
1255 pr = pci_find_parent_resource(dev, r);
1256 if (!pr || (pr->flags & IORESOURCE_UNSET) ||
1257 request_resource(pr, r) < 0) {
1258 printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
1259 " of device %s, will remap\n", idx, pci_name(dev));
1260 if (pr)
Kevin Haoae2a84b2015-06-12 10:26:37 +08001261 pr_debug("PCI: parent is %p: %pR\n", pr, pr);
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001262 /* We'll assign a new address later */
1263 r->flags |= IORESOURCE_UNSET;
1264 r->end -= r->start;
1265 r->start = 0;
1266 }
1267}
1268
1269static void __init pcibios_allocate_resources(int pass)
1270{
1271 struct pci_dev *dev = NULL;
1272 int idx, disabled;
1273 u16 command;
1274 struct resource *r;
1275
1276 for_each_pci_dev(dev) {
1277 pci_read_config_word(dev, PCI_COMMAND, &command);
Benjamin Herrenschmidtad892a62009-05-14 20:16:47 +00001278 for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001279 r = &dev->resource[idx];
1280 if (r->parent) /* Already allocated */
1281 continue;
1282 if (!r->flags || (r->flags & IORESOURCE_UNSET))
1283 continue; /* Not assigned at all */
Benjamin Herrenschmidtad892a62009-05-14 20:16:47 +00001284 /* We only allocate ROMs on pass 1 just in case they
1285 * have been screwed up by firmware
1286 */
1287 if (idx == PCI_ROM_RESOURCE )
1288 disabled = 1;
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001289 if (r->flags & IORESOURCE_IO)
1290 disabled = !(command & PCI_COMMAND_IO);
1291 else
1292 disabled = !(command & PCI_COMMAND_MEMORY);
Paul Mackerras533b1922007-12-31 10:04:15 +11001293 if (pass == disabled)
1294 alloc_resource(dev, idx);
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001295 }
1296 if (pass)
1297 continue;
1298 r = &dev->resource[PCI_ROM_RESOURCE];
Benjamin Herrenschmidtad892a62009-05-14 20:16:47 +00001299 if (r->flags) {
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001300 /* Turn the ROM off, leave the resource region,
1301 * but keep it unregistered.
1302 */
1303 u32 reg;
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001304 pci_read_config_dword(dev, dev->rom_base_reg, &reg);
Benjamin Herrenschmidtad892a62009-05-14 20:16:47 +00001305 if (reg & PCI_ROM_ADDRESS_ENABLE) {
1306 pr_debug("PCI: Switching off ROM of %s\n",
1307 pci_name(dev));
1308 r->flags &= ~IORESOURCE_ROM_ENABLE;
1309 pci_write_config_dword(dev, dev->rom_base_reg,
1310 reg & ~PCI_ROM_ADDRESS_ENABLE);
1311 }
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001312 }
1313 }
1314}
1315
Benjamin Herrenschmidtc1f34302008-11-11 17:45:52 +00001316static void __init pcibios_reserve_legacy_regions(struct pci_bus *bus)
1317{
1318 struct pci_controller *hose = pci_bus_to_host(bus);
1319 resource_size_t offset;
1320 struct resource *res, *pres;
1321 int i;
1322
1323 pr_debug("Reserving legacy ranges for domain %04x\n", pci_domain_nr(bus));
1324
1325 /* Check for IO */
1326 if (!(hose->io_resource.flags & IORESOURCE_IO))
1327 goto no_io;
1328 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
1329 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1330 BUG_ON(res == NULL);
1331 res->name = "Legacy IO";
1332 res->flags = IORESOURCE_IO;
1333 res->start = offset;
1334 res->end = (offset + 0xfff) & 0xfffffffful;
1335 pr_debug("Candidate legacy IO: %pR\n", res);
1336 if (request_resource(&hose->io_resource, res)) {
1337 printk(KERN_DEBUG
1338 "PCI %04x:%02x Cannot reserve Legacy IO %pR\n",
1339 pci_domain_nr(bus), bus->number, res);
1340 kfree(res);
1341 }
1342
1343 no_io:
1344 /* Check for memory */
Benjamin Herrenschmidtc1f34302008-11-11 17:45:52 +00001345 for (i = 0; i < 3; i++) {
1346 pres = &hose->mem_resources[i];
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001347 offset = hose->mem_offset[i];
Benjamin Herrenschmidtc1f34302008-11-11 17:45:52 +00001348 if (!(pres->flags & IORESOURCE_MEM))
1349 continue;
1350 pr_debug("hose mem res: %pR\n", pres);
1351 if ((pres->start - offset) <= 0xa0000 &&
1352 (pres->end - offset) >= 0xbffff)
1353 break;
1354 }
1355 if (i >= 3)
1356 return;
1357 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1358 BUG_ON(res == NULL);
1359 res->name = "Legacy VGA memory";
1360 res->flags = IORESOURCE_MEM;
1361 res->start = 0xa0000 + offset;
1362 res->end = 0xbffff + offset;
1363 pr_debug("Candidate VGA memory: %pR\n", res);
1364 if (request_resource(pres, res)) {
1365 printk(KERN_DEBUG
1366 "PCI %04x:%02x Cannot reserve VGA memory %pR\n",
1367 pci_domain_nr(bus), bus->number, res);
1368 kfree(res);
1369 }
1370}
1371
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001372void __init pcibios_resource_survey(void)
1373{
Nathan Fontenote90a1312008-10-27 19:48:17 +00001374 struct pci_bus *b;
1375
Benjamin Herrenschmidt48c2ce92011-11-06 18:55:58 +00001376 /* Allocate and assign resources */
Nathan Fontenote90a1312008-10-27 19:48:17 +00001377 list_for_each_entry(b, &pci_root_buses, node)
1378 pcibios_allocate_bus_resources(b);
Benjamin Herrenschmidt9a1a70a2016-07-08 16:37:18 +10001379 if (!pci_has_flag(PCI_REASSIGN_ALL_RSRC)) {
1380 pcibios_allocate_resources(0);
1381 pcibios_allocate_resources(1);
1382 }
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001383
Benjamin Herrenschmidtc1f34302008-11-11 17:45:52 +00001384 /* Before we start assigning unassigned resource, we try to reserve
1385 * the low IO area and the VGA memory area if they intersect the
1386 * bus available resources to avoid allocating things on top of them
1387 */
Rob Herring0e47ff12011-07-12 09:25:51 -05001388 if (!pci_has_flag(PCI_PROBE_ONLY)) {
Benjamin Herrenschmidtc1f34302008-11-11 17:45:52 +00001389 list_for_each_entry(b, &pci_root_buses, node)
1390 pcibios_reserve_legacy_regions(b);
1391 }
1392
1393 /* Now, if the platform didn't decide to blindly trust the firmware,
1394 * we proceed to assigning things that were left unassigned
1395 */
Rob Herring0e47ff12011-07-12 09:25:51 -05001396 if (!pci_has_flag(PCI_PROBE_ONLY)) {
Wolfram Sanga77acda2009-03-09 06:39:01 +00001397 pr_debug("PCI: Assigning unassigned resources...\n");
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001398 pci_assign_unassigned_resources();
1399 }
1400
1401 /* Call machine dependent fixup */
1402 if (ppc_md.pcibios_fixup)
1403 ppc_md.pcibios_fixup();
1404}
1405
Benjamin Herrenschmidtfd6852c2008-10-27 19:48:52 +00001406/* This is used by the PCI hotplug driver to allocate resource
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001407 * of newly plugged busses. We can try to consolidate with the
Benjamin Herrenschmidtfd6852c2008-10-27 19:48:52 +00001408 * rest of the code later, for now, keep it as-is as our main
1409 * resource allocation function doesn't deal with sub-trees yet.
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001410 */
Stephen Rothwellbaf75b02009-06-01 14:53:53 +00001411void pcibios_claim_one_bus(struct pci_bus *bus)
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001412{
1413 struct pci_dev *dev;
1414 struct pci_bus *child_bus;
1415
1416 list_for_each_entry(dev, &bus->devices, bus_list) {
1417 int i;
1418
1419 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1420 struct resource *r = &dev->resource[i];
1421
1422 if (r->parent || !r->start || !r->flags)
1423 continue;
Benjamin Herrenschmidtfd6852c2008-10-27 19:48:52 +00001424
Kevin Haoae2a84b2015-06-12 10:26:37 +08001425 pr_debug("PCI: Claiming %s: Resource %d: %pR\n",
1426 pci_name(dev), i, r);
Benjamin Herrenschmidtfd6852c2008-10-27 19:48:52 +00001427
Yinghai Lu3ebfe462015-01-15 16:21:51 -06001428 if (pci_claim_resource(dev, i) == 0)
1429 continue;
1430
1431 pci_claim_bridge_resource(dev, i);
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001432 }
1433 }
1434
1435 list_for_each_entry(child_bus, &bus->children, node)
1436 pcibios_claim_one_bus(child_bus);
1437}
Daniel Axtens5b64d2c2015-05-27 16:06:56 +10001438EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
Benjamin Herrenschmidtfd6852c2008-10-27 19:48:52 +00001439
1440
1441/* pcibios_finish_adding_to_bus
1442 *
1443 * This is to be called by the hotplug code after devices have been
1444 * added to a bus, this include calling it for a PHB that is just
1445 * being added
1446 */
1447void pcibios_finish_adding_to_bus(struct pci_bus *bus)
1448{
1449 pr_debug("PCI: Finishing adding to hotplug bus %04x:%02x\n",
1450 pci_domain_nr(bus), bus->number);
1451
1452 /* Allocate bus and devices resources */
1453 pcibios_allocate_bus_resources(bus);
1454 pcibios_claim_one_bus(bus);
Gavin Shan7415c142016-05-20 16:41:36 +10001455 if (!pci_has_flag(PCI_PROBE_ONLY)) {
1456 if (bus->self)
1457 pci_assign_unassigned_bridge_resources(bus->self);
1458 else
1459 pci_assign_unassigned_bus_resources(bus);
1460 }
Benjamin Herrenschmidtfd6852c2008-10-27 19:48:52 +00001461
Thadeu Lima de Souza Cascardo6a040ce2012-12-28 09:13:19 +00001462 /* Fixup EEH */
1463 eeh_add_device_tree_late(bus);
1464
Benjamin Herrenschmidtfd6852c2008-10-27 19:48:52 +00001465 /* Add new devices to global lists. Register in proc, sysfs. */
1466 pci_bus_add_devices(bus);
1467
Thadeu Lima de Souza Cascardo6a040ce2012-12-28 09:13:19 +00001468 /* sysfs files should only be added after devices are added */
1469 eeh_add_sysfs_files(bus);
Benjamin Herrenschmidtfd6852c2008-10-27 19:48:52 +00001470}
1471EXPORT_SYMBOL_GPL(pcibios_finish_adding_to_bus);
1472
Benjamin Herrenschmidt549beb92007-12-20 14:54:57 +11001473int pcibios_enable_device(struct pci_dev *dev, int mask)
1474{
Daniel Axtens467efc22015-03-31 16:00:56 +11001475 struct pci_controller *phb = pci_bus_to_host(dev->bus);
1476
1477 if (phb->controller_ops.enable_device_hook)
1478 if (!phb->controller_ops.enable_device_hook(dev))
1479 return -EINVAL;
Benjamin Herrenschmidt549beb92007-12-20 14:54:57 +11001480
Bjorn Helgaas7cfb5f92008-03-04 11:56:56 -07001481 return pci_enable_resources(dev, mask);
Benjamin Herrenschmidt549beb92007-12-20 14:54:57 +11001482}
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +00001483
Michael Neulingabeeed62015-05-27 16:07:00 +10001484void pcibios_disable_device(struct pci_dev *dev)
1485{
1486 struct pci_controller *phb = pci_bus_to_host(dev->bus);
1487
1488 if (phb->controller_ops.disable_device)
1489 phb->controller_ops.disable_device(dev);
1490}
1491
Bjorn Helgaas38973ba2012-03-16 17:48:09 -06001492resource_size_t pcibios_io_space_offset(struct pci_controller *hose)
1493{
1494 return (unsigned long) hose->io_base_virt - _IO_BASE;
1495}
1496
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001497static void pcibios_setup_phb_resources(struct pci_controller *hose,
1498 struct list_head *resources)
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +00001499{
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +00001500 struct resource *res;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001501 resource_size_t offset;
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +00001502 int i;
1503
1504 /* Hookup PHB IO resource */
Bjorn Helgaas45a709f2011-10-28 16:27:43 -06001505 res = &hose->io_resource;
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +00001506
1507 if (!res->flags) {
Benjamin Herrenschmidtcdb1b342016-06-22 17:23:07 +10001508 pr_debug("PCI: I/O resource not set for host"
1509 " bridge %s (domain %d)\n",
1510 hose->dn->full_name, hose->global_number);
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001511 } else {
1512 offset = pcibios_io_space_offset(hose);
1513
Kevin Haoae2a84b2015-06-12 10:26:37 +08001514 pr_debug("PCI: PHB IO resource = %pR off 0x%08llx\n",
1515 res, (unsigned long long)offset);
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001516 pci_add_resource_offset(resources, res, offset);
Benjamin Herrenschmidta0b8e762013-05-04 14:22:57 +00001517 }
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +00001518
1519 /* Hookup PHB Memory resources */
1520 for (i = 0; i < 3; ++i) {
1521 res = &hose->mem_resources[i];
1522 if (!res->flags) {
Benjamin Herrenschmidtbee7dd92013-05-20 17:24:39 +00001523 if (i == 0)
1524 printk(KERN_ERR "PCI: Memory resource 0 not set for "
1525 "host bridge %s (domain %d)\n",
1526 hose->dn->full_name, hose->global_number);
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001527 continue;
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +00001528 }
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001529 offset = hose->mem_offset[i];
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +00001530
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001531
Kevin Haoae2a84b2015-06-12 10:26:37 +08001532 pr_debug("PCI: PHB MEM resource %d = %pR off 0x%08llx\n", i,
1533 res, (unsigned long long)offset);
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001534
1535 pci_add_resource_offset(resources, res, offset);
1536 }
Benjamin Herrenschmidt53280322008-10-27 19:48:29 +00001537}
Kumar Gala89c2dd62009-08-25 16:20:45 +00001538
1539/*
1540 * Null PCI config access functions, for the case when we can't
1541 * find a hose.
1542 */
1543#define NULL_PCI_OP(rw, size, type) \
1544static int \
1545null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
1546{ \
1547 return PCIBIOS_DEVICE_NOT_FOUND; \
1548}
1549
1550static int
1551null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
1552 int len, u32 *val)
1553{
1554 return PCIBIOS_DEVICE_NOT_FOUND;
1555}
1556
1557static int
1558null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
1559 int len, u32 val)
1560{
1561 return PCIBIOS_DEVICE_NOT_FOUND;
1562}
1563
1564static struct pci_ops null_pci_ops =
1565{
1566 .read = null_read_config,
1567 .write = null_write_config,
1568};
1569
1570/*
1571 * These functions are used early on before PCI scanning is done
1572 * and all of the pci_dev and pci_bus structures have been created.
1573 */
1574static struct pci_bus *
1575fake_pci_bus(struct pci_controller *hose, int busnr)
1576{
1577 static struct pci_bus bus;
1578
Anton Blanchardb0d436c2013-08-07 02:01:24 +10001579 if (hose == NULL) {
Kumar Gala89c2dd62009-08-25 16:20:45 +00001580 printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
1581 }
1582 bus.number = busnr;
1583 bus.sysdata = hose;
1584 bus.ops = hose? hose->ops: &null_pci_ops;
1585 return &bus;
1586}
1587
1588#define EARLY_PCI_OP(rw, size, type) \
1589int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
1590 int devfn, int offset, type value) \
1591{ \
1592 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
1593 devfn, offset, value); \
1594}
1595
1596EARLY_PCI_OP(read, byte, u8 *)
1597EARLY_PCI_OP(read, word, u16 *)
1598EARLY_PCI_OP(read, dword, u32 *)
1599EARLY_PCI_OP(write, byte, u8)
1600EARLY_PCI_OP(write, word, u16)
1601EARLY_PCI_OP(write, dword, u32)
1602
Kumar Gala89c2dd62009-08-25 16:20:45 +00001603int early_find_capability(struct pci_controller *hose, int bus, int devfn,
1604 int cap)
1605{
1606 return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
1607}
Grant Likely0ed2c7222009-08-28 08:58:16 +00001608
Benjamin Herrenschmidt98d9f30c82011-04-11 11:37:07 +10001609struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
1610{
1611 struct pci_controller *hose = bus->sysdata;
1612
1613 return of_node_get(hose->dn);
1614}
1615
Grant Likely0ed2c7222009-08-28 08:58:16 +00001616/**
1617 * pci_scan_phb - Given a pci_controller, setup and scan the PCI bus
1618 * @hose: Pointer to the PCI host controller instance structure
Grant Likely0ed2c7222009-08-28 08:58:16 +00001619 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001620void pcibios_scan_phb(struct pci_controller *hose)
Grant Likely0ed2c7222009-08-28 08:58:16 +00001621{
Bjorn Helgaas45a709f2011-10-28 16:27:43 -06001622 LIST_HEAD(resources);
Grant Likely0ed2c7222009-08-28 08:58:16 +00001623 struct pci_bus *bus;
1624 struct device_node *node = hose->dn;
1625 int mode;
1626
Grant Likely74a7f082012-06-15 11:50:25 -06001627 pr_debug("PCI: Scanning PHB %s\n", of_node_full_name(node));
Grant Likely0ed2c7222009-08-28 08:58:16 +00001628
Grant Likely0ed2c7222009-08-28 08:58:16 +00001629 /* Get some IO space for the new PHB */
1630 pcibios_setup_phb_io_space(hose);
1631
1632 /* Wire up PHB bus resources */
Bjorn Helgaas45a709f2011-10-28 16:27:43 -06001633 pcibios_setup_phb_resources(hose, &resources);
1634
Yinghai Lube8e60d2012-05-17 18:51:12 -07001635 hose->busn.start = hose->first_busno;
1636 hose->busn.end = hose->last_busno;
1637 hose->busn.flags = IORESOURCE_BUS;
1638 pci_add_resource(&resources, &hose->busn);
1639
Bjorn Helgaas45a709f2011-10-28 16:27:43 -06001640 /* Create an empty bus for the toplevel */
1641 bus = pci_create_root_bus(hose->parent, hose->first_busno,
1642 hose->ops, hose, &resources);
1643 if (bus == NULL) {
1644 pr_err("Failed to create bus for PCI domain %04x\n",
1645 hose->global_number);
1646 pci_free_resource_list(&resources);
1647 return;
1648 }
Bjorn Helgaas45a709f2011-10-28 16:27:43 -06001649 hose->bus = bus;
Grant Likely0ed2c7222009-08-28 08:58:16 +00001650
1651 /* Get probe mode and perform scan */
1652 mode = PCI_PROBE_NORMAL;
Daniel Axtens467efc22015-03-31 16:00:56 +11001653 if (node && hose->controller_ops.probe_mode)
1654 mode = hose->controller_ops.probe_mode(bus);
Grant Likely0ed2c7222009-08-28 08:58:16 +00001655 pr_debug(" probe mode: %d\n", mode);
Yinghai Lube8e60d2012-05-17 18:51:12 -07001656 if (mode == PCI_PROBE_DEVTREE)
Grant Likely0ed2c7222009-08-28 08:58:16 +00001657 of_scan_bus(node, bus);
Grant Likely0ed2c7222009-08-28 08:58:16 +00001658
Yinghai Lube8e60d2012-05-17 18:51:12 -07001659 if (mode == PCI_PROBE_NORMAL) {
1660 pci_bus_update_busn_res_end(bus, 255);
1661 hose->last_busno = pci_scan_child_bus(bus);
1662 pci_bus_update_busn_res_end(bus, hose->last_busno);
1663 }
Benjamin Herrenschmidt781fb7a2011-09-19 17:44:50 +00001664
Benjamin Herrenschmidt491b98c2011-11-06 18:55:57 +00001665 /* Platform gets a chance to do some global fixups before
1666 * we proceed to resource allocation
1667 */
1668 if (ppc_md.pcibios_fixup_phb)
1669 ppc_md.pcibios_fixup_phb(hose);
1670
Benjamin Herrenschmidt781fb7a2011-09-19 17:44:50 +00001671 /* Configure PCI Express settings */
Benjamin Herrenschmidtbb36c442011-09-26 14:22:39 +10001672 if (bus && !pci_has_flag(PCI_PROBE_ONLY)) {
Benjamin Herrenschmidt781fb7a2011-09-19 17:44:50 +00001673 struct pci_bus *child;
Bjorn Helgaasa58674f2013-08-22 11:24:44 +08001674 list_for_each_entry(child, &bus->children, node)
1675 pcie_bus_configure_settings(child);
Benjamin Herrenschmidt781fb7a2011-09-19 17:44:50 +00001676 }
Grant Likely0ed2c7222009-08-28 08:58:16 +00001677}
Daniel Axtens5b64d2c2015-05-27 16:06:56 +10001678EXPORT_SYMBOL_GPL(pcibios_scan_phb);
Kumar Galac0654882011-05-19 22:26:18 -05001679
1680static void fixup_hide_host_resource_fsl(struct pci_dev *dev)
1681{
1682 int i, class = dev->class >> 8;
Jason Jin05737c72011-10-28 16:08:00 +08001683 /* When configured as agent, programing interface = 1 */
1684 int prog_if = dev->class & 0xf;
Kumar Galac0654882011-05-19 22:26:18 -05001685
1686 if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
1687 class == PCI_CLASS_BRIDGE_OTHER) &&
1688 (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
Jason Jin05737c72011-10-28 16:08:00 +08001689 (prog_if == 0) &&
Kumar Galac0654882011-05-19 22:26:18 -05001690 (dev->bus->parent == NULL)) {
1691 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1692 dev->resource[i].start = 0;
1693 dev->resource[i].end = 0;
1694 dev->resource[i].flags = 0;
1695 }
1696 }
1697}
1698DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
1699DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
Brian Kingc2e1d842013-04-08 03:05:10 +00001700
1701static void fixup_vga(struct pci_dev *pdev)
1702{
1703 u16 cmd;
1704
1705 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
1706 if ((cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) || !vga_default_device())
1707 vga_set_default_device(pdev);
1708
1709}
1710DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_ANY_ID, PCI_ANY_ID,
1711 PCI_CLASS_DISPLAY_VGA, 8, fixup_vga);