blob: 63b11fddffecdda25145d749043cc23773122c07 [file] [log] [blame]
Paul Mundt4c5107e2009-04-20 15:43:36 +09001/*
2 * New-style PCI core.
3 *
Paul Mundt4c5107e2009-04-20 15:43:36 +09004 * Copyright (c) 2004 - 2009 Paul Mundt
Paul Mundt35bcfff2009-04-20 21:51:19 +09005 * Copyright (c) 2002 M. R. Brown
6 *
7 * Modelled after arch/mips/pci/pci.c:
8 * Copyright (C) 2003, 04 Ralf Baechle (ralf@linux-mips.org)
Paul Mundt4c5107e2009-04-20 15:43:36 +09009 *
10 * This file is subject to the terms and conditions of the GNU General Public
11 * License. See the file "COPYING" in the main directory of this archive
12 * for more details.
13 */
14#include <linux/kernel.h>
Paul Mundt35bcfff2009-04-20 21:51:19 +090015#include <linux/mm.h>
Paul Mundt4c5107e2009-04-20 15:43:36 +090016#include <linux/pci.h>
17#include <linux/init.h>
Paul Mundt35bcfff2009-04-20 21:51:19 +090018#include <linux/types.h>
Paul Mundt4c5107e2009-04-20 15:43:36 +090019#include <linux/dma-debug.h>
20#include <linux/io.h>
Paul Mundte79066a2009-04-20 18:29:22 +090021#include <linux/mutex.h>
22
Paul Mundt35bcfff2009-04-20 21:51:19 +090023unsigned long PCIBIOS_MIN_IO = 0x0000;
24unsigned long PCIBIOS_MIN_MEM = 0;
25
Paul Mundte79066a2009-04-20 18:29:22 +090026/*
27 * The PCI controller list.
28 */
29static struct pci_channel *hose_head, **hose_tail = &hose_head;
30
31static int pci_initialized;
32
33static void __devinit pcibios_scanbus(struct pci_channel *hose)
34{
35 static int next_busno;
Paul Mundt320e68d2010-01-29 22:38:13 +090036 static int need_domain_info;
Paul Mundte79066a2009-04-20 18:29:22 +090037 struct pci_bus *bus;
38
Paul Mundte79066a2009-04-20 18:29:22 +090039 bus = pci_scan_bus(next_busno, hose->pci_ops, hose);
Paul Mundt320e68d2010-01-29 22:38:13 +090040 hose->bus = bus;
41
42 need_domain_info = need_domain_info || hose->index;
43 hose->need_domain_info = need_domain_info;
Paul Mundte79066a2009-04-20 18:29:22 +090044 if (bus) {
45 next_busno = bus->subordinate + 1;
46 /* Don't allow 8-bit bus number overflow inside the hose -
47 reserve some space for bridges. */
Paul Mundt320e68d2010-01-29 22:38:13 +090048 if (next_busno > 224) {
Paul Mundte79066a2009-04-20 18:29:22 +090049 next_busno = 0;
Paul Mundt320e68d2010-01-29 22:38:13 +090050 need_domain_info = 1;
51 }
Paul Mundte79066a2009-04-20 18:29:22 +090052
53 pci_bus_size_bridges(bus);
54 pci_bus_assign_resources(bus);
55 pci_enable_bridges(bus);
56 }
57}
58
59static DEFINE_MUTEX(pci_scan_mutex);
60
61void __devinit register_pci_controller(struct pci_channel *hose)
62{
Paul Mundtac8ab542010-01-29 22:22:27 +090063 if (request_resource(&iomem_resource, hose->mem_resource) < 0)
64 goto out;
65 if (request_resource(&ioport_resource, hose->io_resource) < 0) {
66 release_resource(hose->mem_resource);
67 goto out;
68 }
Paul Mundte79066a2009-04-20 18:29:22 +090069
70 *hose_tail = hose;
71 hose_tail = &hose->next;
72
73 /*
74 * Do not panic here but later - this might hapen before console init.
75 */
76 if (!hose->io_map_base) {
77 printk(KERN_WARNING
78 "registering PCI controller with io_map_base unset\n");
79 }
80
81 /*
82 * Scan the bus if it is register after the PCI subsystem
83 * initialization.
84 */
85 if (pci_initialized) {
86 mutex_lock(&pci_scan_mutex);
87 pcibios_scanbus(hose);
88 mutex_unlock(&pci_scan_mutex);
89 }
Paul Mundtac8ab542010-01-29 22:22:27 +090090
Paul Mundt85b59f52010-02-01 13:01:42 +090091 return;
92
Paul Mundtac8ab542010-01-29 22:22:27 +090093out:
94 printk(KERN_WARNING "Skipping PCI bus scan due to resource conflict\n");
Paul Mundte79066a2009-04-20 18:29:22 +090095}
Paul Mundt4c5107e2009-04-20 15:43:36 +090096
97static int __init pcibios_init(void)
98{
Paul Mundte79066a2009-04-20 18:29:22 +090099 struct pci_channel *hose;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900100
Paul Mundte79066a2009-04-20 18:29:22 +0900101 /* Scan all of the recorded PCI controllers. */
102 for (hose = hose_head; hose; hose = hose->next)
103 pcibios_scanbus(hose);
Paul Mundt4c5107e2009-04-20 15:43:36 +0900104
105 pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq);
106
107 dma_debug_add_bus(&pci_bus_type);
108
Paul Mundte79066a2009-04-20 18:29:22 +0900109 pci_initialized = 1;
110
Paul Mundt4c5107e2009-04-20 15:43:36 +0900111 return 0;
112}
113subsys_initcall(pcibios_init);
114
115static void pcibios_fixup_device_resources(struct pci_dev *dev,
116 struct pci_bus *bus)
117{
118 /* Update device resources. */
Paul Mundt09cfeb12009-04-20 18:42:00 +0900119 struct pci_channel *hose = bus->sysdata;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900120 unsigned long offset = 0;
121 int i;
122
123 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
124 if (!dev->resource[i].start)
125 continue;
126 if (dev->resource[i].flags & IORESOURCE_PCI_FIXED)
127 continue;
128 if (dev->resource[i].flags & IORESOURCE_IO)
Paul Mundt09cfeb12009-04-20 18:42:00 +0900129 offset = hose->io_offset;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900130 else if (dev->resource[i].flags & IORESOURCE_MEM)
Paul Mundt09cfeb12009-04-20 18:42:00 +0900131 offset = hose->mem_offset;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900132
133 dev->resource[i].start += offset;
134 dev->resource[i].end += offset;
135 }
136}
137
Paul Mundt4c5107e2009-04-20 15:43:36 +0900138/*
139 * Called after each bus is probed, but before its children
140 * are examined.
141 */
Paul Mundt35bcfff2009-04-20 21:51:19 +0900142void __devinit pcibios_fixup_bus(struct pci_bus *bus)
Paul Mundt4c5107e2009-04-20 15:43:36 +0900143{
144 struct pci_dev *dev = bus->self;
145 struct list_head *ln;
146 struct pci_channel *chan = bus->sysdata;
147
148 if (!dev) {
149 bus->resource[0] = chan->io_resource;
150 bus->resource[1] = chan->mem_resource;
151 }
152
153 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
154 dev = pci_dev_b(ln);
155
156 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
157 pcibios_fixup_device_resources(dev, bus);
158 }
159}
Paul Mundt35bcfff2009-04-20 21:51:19 +0900160
161/*
162 * We need to avoid collisions with `mirrored' VGA ports
163 * and other strange ISA hardware, so we always want the
164 * addresses to be allocated in the 0x000-0x0ff region
165 * modulo 0x400.
166 */
167void pcibios_align_resource(void *data, struct resource *res,
168 resource_size_t size, resource_size_t align)
169{
170 struct pci_dev *dev = data;
171 struct pci_channel *chan = dev->sysdata;
172 resource_size_t start = res->start;
173
174 if (res->flags & IORESOURCE_IO) {
175 if (start < PCIBIOS_MIN_IO + chan->io_resource->start)
176 start = PCIBIOS_MIN_IO + chan->io_resource->start;
177
178 /*
179 * Put everything into 0x00-0xff region modulo 0x400.
180 */
Paul Mundt84959352010-01-28 18:15:05 +0900181 if (start & 0x300)
Paul Mundt35bcfff2009-04-20 21:51:19 +0900182 start = (start + 0x3ff) & ~0x3ff;
Paul Mundt35bcfff2009-04-20 21:51:19 +0900183 } else if (res->flags & IORESOURCE_MEM) {
184 if (start < PCIBIOS_MIN_MEM + chan->mem_resource->start)
185 start = PCIBIOS_MIN_MEM + chan->mem_resource->start;
186 }
187
188 res->start = start;
189}
190
191void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
192 struct resource *res)
193{
194 struct pci_channel *hose = dev->sysdata;
195 unsigned long offset = 0;
196
197 if (res->flags & IORESOURCE_IO)
198 offset = hose->io_offset;
199 else if (res->flags & IORESOURCE_MEM)
200 offset = hose->mem_offset;
201
202 region->start = res->start - offset;
203 region->end = res->end - offset;
204}
205
206void __devinit
207pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
208 struct pci_bus_region *region)
209{
210 struct pci_channel *hose = dev->sysdata;
211 unsigned long offset = 0;
212
213 if (res->flags & IORESOURCE_IO)
214 offset = hose->io_offset;
215 else if (res->flags & IORESOURCE_MEM)
216 offset = hose->mem_offset;
217
218 res->start = region->start + offset;
219 res->end = region->end + offset;
220}
221
222int pcibios_enable_device(struct pci_dev *dev, int mask)
223{
224 u16 cmd, old_cmd;
225 int idx;
226 struct resource *r;
227
228 pci_read_config_word(dev, PCI_COMMAND, &cmd);
229 old_cmd = cmd;
230 for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
231 /* Only set up the requested stuff */
232 if (!(mask & (1<<idx)))
233 continue;
234
235 r = &dev->resource[idx];
236 if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
237 continue;
238 if ((idx == PCI_ROM_RESOURCE) &&
239 (!(r->flags & IORESOURCE_ROM_ENABLE)))
240 continue;
241 if (!r->start && r->end) {
242 printk(KERN_ERR "PCI: Device %s not available "
243 "because of resource collisions\n",
244 pci_name(dev));
245 return -EINVAL;
246 }
247 if (r->flags & IORESOURCE_IO)
248 cmd |= PCI_COMMAND_IO;
249 if (r->flags & IORESOURCE_MEM)
250 cmd |= PCI_COMMAND_MEMORY;
251 }
252 if (cmd != old_cmd) {
253 printk("PCI: Enabling device %s (%04x -> %04x)\n",
254 pci_name(dev), old_cmd, cmd);
255 pci_write_config_word(dev, PCI_COMMAND, cmd);
256 }
257 return 0;
258}
259
260/*
261 * If we set up a device for bus mastering, we need to check and set
262 * the latency timer as it may not be properly set.
263 */
264static unsigned int pcibios_max_latency = 255;
265
266void pcibios_set_master(struct pci_dev *dev)
267{
268 u8 lat;
269 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
270 if (lat < 16)
271 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
272 else if (lat > pcibios_max_latency)
273 lat = pcibios_max_latency;
274 else
275 return;
276 printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
277 pci_name(dev), lat);
278 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
279}
280
281void __init pcibios_update_irq(struct pci_dev *dev, int irq)
282{
283 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
284}
285
286char * __devinit pcibios_setup(char *str)
287{
288 return str;
289}
290
291int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
292 enum pci_mmap_state mmap_state, int write_combine)
293{
294 /*
295 * I/O space can be accessed via normal processor loads and stores on
296 * this platform but for now we elect not to do this and portable
297 * drivers should not do this anyway.
298 */
299 if (mmap_state == pci_mmap_io)
300 return -EINVAL;
301
302 /*
303 * Ignore write-combine; for now only return uncached mappings.
304 */
305 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
306
307 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
308 vma->vm_end - vma->vm_start,
309 vma->vm_page_prot);
310}
311
David McKay15444a82009-08-24 16:10:40 +0900312#ifndef CONFIG_GENERIC_IOMAP
313
Paul Mundt35bcfff2009-04-20 21:51:19 +0900314static void __iomem *ioport_map_pci(struct pci_dev *dev,
315 unsigned long port, unsigned int nr)
316{
317 struct pci_channel *chan = dev->sysdata;
318
Paul Mundt320e68d2010-01-29 22:38:13 +0900319 if (unlikely(!chan->io_map_base)) {
Paul Mundt35bcfff2009-04-20 21:51:19 +0900320 chan->io_map_base = generic_io_base;
321
Paul Mundt320e68d2010-01-29 22:38:13 +0900322 if (pci_domains_supported)
323 panic("To avoid data corruption io_map_base MUST be "
324 "set with multiple PCI domains.");
325 }
326
327
Paul Mundt35bcfff2009-04-20 21:51:19 +0900328 return (void __iomem *)(chan->io_map_base + port);
329}
330
331void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
332{
333 resource_size_t start = pci_resource_start(dev, bar);
334 resource_size_t len = pci_resource_len(dev, bar);
335 unsigned long flags = pci_resource_flags(dev, bar);
336
337 if (unlikely(!len || !start))
338 return NULL;
339 if (maxlen && len > maxlen)
340 len = maxlen;
341
342 if (flags & IORESOURCE_IO)
343 return ioport_map_pci(dev, start, len);
Paul Mundt35bcfff2009-04-20 21:51:19 +0900344 if (flags & IORESOURCE_MEM) {
345 if (flags & IORESOURCE_CACHEABLE)
346 return ioremap(start, len);
Paul Mundt35bcfff2009-04-20 21:51:19 +0900347 return ioremap_nocache(start, len);
348 }
349
350 return NULL;
351}
352EXPORT_SYMBOL(pci_iomap);
353
354void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
355{
356 iounmap(addr);
357}
358EXPORT_SYMBOL(pci_iounmap);
359
David McKay15444a82009-08-24 16:10:40 +0900360#endif /* CONFIG_GENERIC_IOMAP */
361
Paul Mundt35bcfff2009-04-20 21:51:19 +0900362#ifdef CONFIG_HOTPLUG
363EXPORT_SYMBOL(pcibios_resource_to_bus);
364EXPORT_SYMBOL(pcibios_bus_to_resource);
365EXPORT_SYMBOL(PCIBIOS_MIN_IO);
366EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
367#endif