blob: fa8e378413b1640ed1e2efaf17894efde23925da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License as published by the
4 * Free Software Foundation; either version 2 of the License, or (at your
5 * option) any later version.
6 *
7 * Copyright (C) 2003, 04 Ralf Baechle (ralf@linux-mips.org)
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/kernel.h>
10#include <linux/mm.h>
11#include <linux/bootmem.h>
Paul Gortmakercae39d12011-07-28 18:46:31 -040012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
14#include <linux/types.h>
15#include <linux/pci.h>
16
17/*
18 * Indicate whether we respect the PCI setup left by the firmware.
19 *
20 * Make this long-lived so that we know when shutting down
21 * whether we probed only or not.
22 */
23int pci_probe_only;
24
25#define PCI_ASSIGN_ALL_BUSSES 1
26
27unsigned int pci_probe = PCI_ASSIGN_ALL_BUSSES;
28
29/*
30 * The PCI controller list.
31 */
32
Dmitri Vorobievd58eaab2008-06-18 10:18:20 +030033static struct pci_controller *hose_head, **hose_tail = &hose_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Ralf Baechle982f6ff2009-09-17 02:25:07 +020035unsigned long PCIBIOS_MIN_IO;
36unsigned long PCIBIOS_MIN_MEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Aurelien Jarno540799e2008-10-14 11:45:09 +020038static int pci_initialized;
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
41 * We need to avoid collisions with `mirrored' VGA ports
42 * and other strange ISA hardware, so we always want the
43 * addresses to be allocated in the 0x000-0x0ff region
44 * modulo 0x400.
45 *
46 * Why? Because some silly external IO cards only decode
47 * the low 10 bits of the IO address. The 0x00-0xff region
48 * is reserved for motherboard devices that decode all 16
49 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
50 * but we want to try to avoid allocating at 0x2900-0x2bff
51 * which might have be mirrored at 0x0100-0x03ff..
52 */
Dominik Brodowskib26b2d42010-01-01 17:40:49 +010053resource_size_t
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +010054pcibios_align_resource(void *data, const struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070055 resource_size_t size, resource_size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 struct pci_dev *dev = data;
58 struct pci_controller *hose = dev->sysdata;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070059 resource_size_t start = res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 if (res->flags & IORESOURCE_IO) {
62 /* Make sure we start at our min on all hoses */
63 if (start < PCIBIOS_MIN_IO + hose->io_resource->start)
64 start = PCIBIOS_MIN_IO + hose->io_resource->start;
65
66 /*
67 * Put everything into 0x00-0xff region modulo 0x400
68 */
69 if (start & 0x300)
70 start = (start + 0x3ff) & ~0x3ff;
71 } else if (res->flags & IORESOURCE_MEM) {
72 /* Make sure we start at our min on all hoses */
73 if (start < PCIBIOS_MIN_MEM + hose->mem_resource->start)
74 start = PCIBIOS_MIN_MEM + hose->mem_resource->start;
75 }
76
Dominik Brodowskib26b2d42010-01-01 17:40:49 +010077 return start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Aurelien Jarno540799e2008-10-14 11:45:09 +020080static void __devinit pcibios_scanbus(struct pci_controller *hose)
81{
82 static int next_busno;
83 static int need_domain_info;
Bjorn Helgaas7c090e52011-10-28 16:26:57 -060084 LIST_HEAD(resources);
Aurelien Jarno540799e2008-10-14 11:45:09 +020085 struct pci_bus *bus;
86
87 if (!hose->iommu)
88 PCI_DMA_BUS_IS_PHYS = 1;
89
90 if (hose->get_busno && pci_probe_only)
91 next_busno = (*hose->get_busno)();
92
Bjorn Helgaas7c090e52011-10-28 16:26:57 -060093 pci_add_resource(&resources, hose->mem_resource);
94 pci_add_resource(&resources, hose->io_resource);
95 bus = pci_scan_root_bus(NULL, next_busno, hose->pci_ops, hose,
96 &resources);
97 if (!bus)
98 pci_free_resource_list(&resources);
99
Aurelien Jarno540799e2008-10-14 11:45:09 +0200100 hose->bus = bus;
101
102 need_domain_info = need_domain_info || hose->index;
103 hose->need_domain_info = need_domain_info;
104 if (bus) {
105 next_busno = bus->subordinate + 1;
106 /* Don't allow 8-bit bus number overflow inside the hose -
107 reserve some space for bridges. */
108 if (next_busno > 224) {
109 next_busno = 0;
110 need_domain_info = 1;
111 }
112
113 if (!pci_probe_only) {
114 pci_bus_size_bridges(bus);
115 pci_bus_assign_resources(bus);
116 pci_enable_bridges(bus);
117 }
118 }
119}
120
121static DEFINE_MUTEX(pci_scan_mutex);
122
Ralf Baechle606bf782007-08-24 02:13:33 +0100123void __devinit register_pci_controller(struct pci_controller *hose)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Thomas Bogendoerfer639702b2007-04-08 13:28:44 +0200125 if (request_resource(&iomem_resource, hose->mem_resource) < 0)
126 goto out;
127 if (request_resource(&ioport_resource, hose->io_resource) < 0) {
128 release_resource(hose->mem_resource);
129 goto out;
130 }
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 *hose_tail = hose;
133 hose_tail = &hose->next;
Ralf Baechle140c1722006-12-07 15:35:43 +0100134
135 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300136 * Do not panic here but later - this might happen before console init.
Ralf Baechle140c1722006-12-07 15:35:43 +0100137 */
138 if (!hose->io_map_base) {
139 printk(KERN_WARNING
140 "registering PCI controller with io_map_base unset\n");
141 }
Aurelien Jarno540799e2008-10-14 11:45:09 +0200142
143 /*
144 * Scan the bus if it is register after the PCI subsystem
145 * initialization.
146 */
147 if (pci_initialized) {
148 mutex_lock(&pci_scan_mutex);
149 pcibios_scanbus(hose);
150 mutex_unlock(&pci_scan_mutex);
151 }
152
Thomas Bogendoerfer639702b2007-04-08 13:28:44 +0200153 return;
154
155out:
156 printk(KERN_WARNING
157 "Skipping PCI bus scan due to resource conflict\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static int __init pcibios_init(void)
161{
162 struct pci_controller *hose;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 /* Scan all of the recorded PCI controllers. */
Aurelien Jarno540799e2008-10-14 11:45:09 +0200165 for (hose = hose_head; hose; hose = hose->next)
166 pcibios_scanbus(hose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Bjorn Helgaas67eed582008-12-16 21:37:10 -0700168 pci_fixup_irqs(pci_common_swizzle, pcibios_map_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Aurelien Jarno540799e2008-10-14 11:45:09 +0200170 pci_initialized = 1;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return 0;
173}
174
175subsys_initcall(pcibios_init);
176
177static int pcibios_enable_resources(struct pci_dev *dev, int mask)
178{
179 u16 cmd, old_cmd;
180 int idx;
181 struct resource *r;
182
183 pci_read_config_word(dev, PCI_COMMAND, &cmd);
184 old_cmd = cmd;
Ralf Baechlee5de3b42005-07-12 09:18:53 +0000185 for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* Only set up the requested stuff */
187 if (!(mask & (1<<idx)))
188 continue;
189
190 r = &dev->resource[idx];
Ralf Baechle986c9482008-02-19 15:59:33 +0000191 if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
192 continue;
193 if ((idx == PCI_ROM_RESOURCE) &&
194 (!(r->flags & IORESOURCE_ROM_ENABLE)))
195 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (!r->start && r->end) {
Ralf Baechle40d7c1a2008-02-19 16:01:20 +0000197 printk(KERN_ERR "PCI: Device %s not available "
198 "because of resource collisions\n",
199 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return -EINVAL;
201 }
202 if (r->flags & IORESOURCE_IO)
203 cmd |= PCI_COMMAND_IO;
204 if (r->flags & IORESOURCE_MEM)
205 cmd |= PCI_COMMAND_MEMORY;
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (cmd != old_cmd) {
Ralf Baechle40d7c1a2008-02-19 16:01:20 +0000208 printk("PCI: Enabling device %s (%04x -> %04x)\n",
209 pci_name(dev), old_cmd, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 pci_write_config_word(dev, PCI_COMMAND, cmd);
211 }
212 return 0;
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215unsigned int pcibios_assign_all_busses(void)
216{
217 return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
218}
219
220int pcibios_enable_device(struct pci_dev *dev, int mask)
221{
222 int err;
223
224 if ((err = pcibios_enable_resources(dev, mask)) < 0)
225 return err;
226
227 return pcibios_plat_dev_init(dev);
228}
229
Ralf Baechlec4aa2562007-08-23 14:17:14 +0100230static void pcibios_fixup_device_resources(struct pci_dev *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct pci_bus *bus)
232{
233 /* Update device resources. */
234 struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
235 unsigned long offset = 0;
236 int i;
237
238 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
239 if (!dev->resource[i].start)
240 continue;
241 if (dev->resource[i].flags & IORESOURCE_IO)
242 offset = hose->io_offset;
243 else if (dev->resource[i].flags & IORESOURCE_MEM)
244 offset = hose->mem_offset;
245
246 dev->resource[i].start += offset;
247 dev->resource[i].end += offset;
248 }
249}
250
Ralf Baechle234fcd12008-03-08 09:56:28 +0000251void __devinit pcibios_fixup_bus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 /* Propagate hose info into the subordinate devices. */
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct list_head *ln;
256 struct pci_dev *dev = bus->self;
257
Bjorn Helgaas7c090e52011-10-28 16:26:57 -0600258 if (pci_probe_only && dev &&
259 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 pci_read_bridge_bases(bus);
261 pcibios_fixup_device_resources(dev, bus);
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
Atsushi Nemoto8ed07a12007-07-13 01:26:52 +0900265 dev = pci_dev_b(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
268 pcibios_fixup_device_resources(dev, bus);
269 }
270}
271
272void __init
273pcibios_update_irq(struct pci_dev *dev, int irq)
274{
275 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
276}
277
Ralf Baechlec4aa2562007-08-23 14:17:14 +0100278void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 struct resource *res)
280{
281 struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
282 unsigned long offset = 0;
283
284 if (res->flags & IORESOURCE_IO)
285 offset = hose->io_offset;
286 else if (res->flags & IORESOURCE_MEM)
287 offset = hose->mem_offset;
288
289 region->start = res->start - offset;
290 region->end = res->end - offset;
291}
292
Yoichi Yuasae63ea562005-09-03 15:56:20 -0700293void __devinit
294pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
295 struct pci_bus_region *region)
296{
297 struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
298 unsigned long offset = 0;
299
300 if (res->flags & IORESOURCE_IO)
301 offset = hose->io_offset;
302 else if (res->flags & IORESOURCE_MEM)
303 offset = hose->mem_offset;
304
305 res->start = region->start + offset;
306 res->end = region->end + offset;
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#ifdef CONFIG_HOTPLUG
310EXPORT_SYMBOL(pcibios_resource_to_bus);
Yoichi Yuasae63ea562005-09-03 15:56:20 -0700311EXPORT_SYMBOL(pcibios_bus_to_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312EXPORT_SYMBOL(PCIBIOS_MIN_IO);
313EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
314#endif
315
Ralf Baechle98873f52008-12-09 17:58:46 +0000316int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
317 enum pci_mmap_state mmap_state, int write_combine)
318{
319 unsigned long prot;
320
321 /*
322 * I/O space can be accessed via normal processor loads and stores on
323 * this platform but for now we elect not to do this and portable
324 * drivers should not do this anyway.
325 */
326 if (mmap_state == pci_mmap_io)
327 return -EINVAL;
328
329 /*
330 * Ignore write-combine; for now only return uncached mappings.
331 */
332 prot = pgprot_val(vma->vm_page_prot);
333 prot = (prot & ~_CACHE_MASK) | _CACHE_UNCACHED;
334 vma->vm_page_prot = __pgprot(prot);
335
336 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
337 vma->vm_end - vma->vm_start, vma->vm_page_prot);
338}
339
Atsushi Nemoto47a5c972008-07-24 00:25:14 +0900340char * (*pcibios_plat_setup)(char *str) __devinitdata;
341
342char *__devinit pcibios_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Atsushi Nemoto47a5c972008-07-24 00:25:14 +0900344 if (pcibios_plat_setup)
345 return pcibios_plat_setup(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return str;
347}