blob: 026d49f28de3a5cbbb2120c1cf15783c94f7e92a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/alpha/kernel/pci.c
3 *
4 * Extruded from code written by
5 * Dave Rusling (david.rusling@reo.mts.dec.com)
6 * David Mosberger (davidm@cs.arizona.edu)
7 */
8
9/* 2.3.x PCI/resources, 1999 Andrea Arcangeli <andrea@suse.de> */
10
11/*
12 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
13 * PCI-PCI bridges cleanup
14 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/string.h>
16#include <linux/pci.h>
17#include <linux/init.h>
18#include <linux/ioport.h>
19#include <linux/kernel.h>
20#include <linux/bootmem.h>
21#include <linux/module.h>
22#include <linux/cache.h>
23#include <linux/slab.h>
24#include <asm/machvec.h>
25
26#include "proto.h"
27#include "pci_impl.h"
28
29
30/*
31 * Some string constants used by the various core logics.
32 */
33
34const char *const pci_io_names[] = {
35 "PCI IO bus 0", "PCI IO bus 1", "PCI IO bus 2", "PCI IO bus 3",
36 "PCI IO bus 4", "PCI IO bus 5", "PCI IO bus 6", "PCI IO bus 7"
37};
38
39const char *const pci_mem_names[] = {
40 "PCI mem bus 0", "PCI mem bus 1", "PCI mem bus 2", "PCI mem bus 3",
41 "PCI mem bus 4", "PCI mem bus 5", "PCI mem bus 6", "PCI mem bus 7"
42};
43
44const char pci_hae0_name[] = "HAE0";
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
Bjorn Helgaas151d16d2012-02-23 20:18:56 -070047 * If PCI_PROBE_ONLY in pci_flags is set, we don't change any PCI resource
48 * assignments.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*
52 * The PCI controller list.
53 */
54
55struct pci_controller *hose_head, **hose_tail = &hose_head;
56struct pci_controller *pci_isa_hose;
57
58/*
59 * Quirks.
60 */
61
Sebastian Andrzej Siewior8ff255a2012-06-03 20:48:20 +020062static void __devinit quirk_isa_bridge(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 dev->class = PCI_CLASS_BRIDGE_ISA << 8;
65}
66DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82378, quirk_isa_bridge);
67
Sebastian Andrzej Siewior8ff255a2012-06-03 20:48:20 +020068static void __devinit quirk_cypress(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Ivan Kokshayskya744e012008-06-21 03:28:54 +040070 /* The Notorious Cy82C693 chip. */
71
72 /* The generic legacy mode IDE fixup in drivers/pci/probe.c
73 doesn't work correctly with the Cypress IDE controller as
74 it has non-standard register layout. Fix that. */
75 if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE) {
76 dev->resource[2].start = dev->resource[3].start = 0;
77 dev->resource[2].end = dev->resource[3].end = 0;
78 dev->resource[2].flags = dev->resource[3].flags = 0;
79 if (PCI_FUNC(dev->devfn) == 2) {
80 dev->resource[0].start = 0x170;
81 dev->resource[0].end = 0x177;
82 dev->resource[1].start = 0x376;
83 dev->resource[1].end = 0x376;
84 }
85 }
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 /* The Cypress bridge responds on the PCI bus in the address range
88 0xffff0000-0xffffffff (conventional x86 BIOS ROM). There is no
89 way to turn this off. The bridge also supports several extended
90 BIOS ranges (disabled after power-up), and some consoles do turn
91 them on. So if we use a large direct-map window, or a large SG
92 window, we must avoid the entire 0xfff00000-0xffffffff region. */
Ivan Kokshaysky72cff122008-04-24 16:51:55 +040093 if (dev->class >> 8 == PCI_CLASS_BRIDGE_ISA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (__direct_map_base + __direct_map_size >= 0xfff00000UL)
95 __direct_map_size = 0xfff00000UL - __direct_map_base;
96 else {
97 struct pci_controller *hose = dev->sysdata;
98 struct pci_iommu_arena *pci = hose->sg_pci;
99 if (pci && pci->dma_base + pci->size >= 0xfff00000UL)
100 pci->size = 0xfff00000UL - pci->dma_base;
101 }
102 }
103}
104DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693, quirk_cypress);
105
106/* Called for each device after PCI setup is done. */
Sebastian Andrzej Siewior8ff255a2012-06-03 20:48:20 +0200107static void __devinit pcibios_fixup_final(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 unsigned int class = dev->class >> 8;
110
111 if (class == PCI_CLASS_BRIDGE_ISA || class == PCI_CLASS_BRIDGE_EISA) {
112 dev->dma_mask = MAX_ISA_DMA_ADDRESS - 1;
113 isa_bridge = dev;
114 }
115}
116DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_final);
117
118/* Just declaring that the power-of-ten prefixes are actually the
119 power-of-two ones doesn't make it true :) */
120#define KB 1024
121#define MB (1024*KB)
122#define GB (1024*MB)
123
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100124resource_size_t
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +0100125pcibios_align_resource(void *data, const struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700126 resource_size_t size, resource_size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 struct pci_dev *dev = data;
129 struct pci_controller *hose = dev->sysdata;
130 unsigned long alignto;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700131 resource_size_t start = res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 if (res->flags & IORESOURCE_IO) {
134 /* Make sure we start at our min on all hoses */
135 if (start - hose->io_space->start < PCIBIOS_MIN_IO)
136 start = PCIBIOS_MIN_IO + hose->io_space->start;
137
138 /*
139 * Put everything into 0x00-0xff region modulo 0x400
140 */
141 if (start & 0x300)
142 start = (start + 0x3ff) & ~0x3ff;
143 }
144 else if (res->flags & IORESOURCE_MEM) {
145 /* Make sure we start at our min on all hoses */
146 if (start - hose->mem_space->start < PCIBIOS_MIN_MEM)
147 start = PCIBIOS_MIN_MEM + hose->mem_space->start;
148
149 /*
150 * The following holds at least for the Low Cost
151 * Alpha implementation of the PCI interface:
152 *
153 * In sparse memory address space, the first
154 * octant (16MB) of every 128MB segment is
155 * aliased to the very first 16 MB of the
156 * address space (i.e., it aliases the ISA
157 * memory address space). Thus, we try to
158 * avoid allocating PCI devices in that range.
159 * Can be allocated in 2nd-7th octant only.
160 * Devices that need more than 112MB of
161 * address space must be accessed through
162 * dense memory space only!
163 */
164
165 /* Align to multiple of size of minimum base. */
Randy Dunlap5f0e3da2009-03-31 15:23:36 -0700166 alignto = max_t(resource_size_t, 0x1000, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 start = ALIGN(start, alignto);
168 if (hose->sparse_mem_base && size <= 7 * 16*MB) {
169 if (((start / (16*MB)) & 0x7) == 0) {
170 start &= ~(128*MB - 1);
171 start += 16*MB;
172 start = ALIGN(start, alignto);
173 }
174 if (start/(128*MB) != (start + size - 1)/(128*MB)) {
175 start &= ~(128*MB - 1);
176 start += (128 + 16)*MB;
177 start = ALIGN(start, alignto);
178 }
179 }
180 }
181
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100182 return start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184#undef KB
185#undef MB
186#undef GB
187
188static int __init
189pcibios_init(void)
190{
191 if (alpha_mv.init_pci)
192 alpha_mv.init_pci();
193 return 0;
194}
195
196subsys_initcall(pcibios_init);
197
Al Viroed5f6562007-07-26 17:34:19 +0100198char * __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199pcibios_setup(char *str)
200{
201 return str;
202}
203
204#ifdef ALPHA_RESTORE_SRM_SETUP
205static struct pdev_srm_saved_conf *srm_saved_configs;
206
Al Viroed5f6562007-07-26 17:34:19 +0100207void __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208pdev_save_srm_config(struct pci_dev *dev)
209{
210 struct pdev_srm_saved_conf *tmp;
211 static int printed = 0;
212
Bjorn Helgaas151d16d2012-02-23 20:18:56 -0700213 if (!alpha_using_srm || pci_has_flag(PCI_PROBE_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return;
215
216 if (!printed) {
217 printk(KERN_INFO "pci: enabling save/restore of SRM state\n");
218 printed = 1;
219 }
220
221 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
222 if (!tmp) {
Harvey Harrisonbbb8d342008-04-28 02:13:46 -0700223 printk(KERN_ERR "%s: kmalloc() failed!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return;
225 }
226 tmp->next = srm_saved_configs;
227 tmp->dev = dev;
228
229 pci_save_state(dev);
230
231 srm_saved_configs = tmp;
232}
233
234void
235pci_restore_srm_config(void)
236{
237 struct pdev_srm_saved_conf *tmp;
238
239 /* No need to restore if probed only. */
Bjorn Helgaas151d16d2012-02-23 20:18:56 -0700240 if (pci_has_flag(PCI_PROBE_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return;
242
243 /* Restore SRM config. */
244 for (tmp = srm_saved_configs; tmp; tmp = tmp->next) {
245 pci_restore_state(tmp->dev);
246 }
247}
248#endif
249
Al Viroed5f6562007-07-26 17:34:19 +0100250void __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251pcibios_fixup_bus(struct pci_bus *bus)
252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct pci_dev *dev = bus->self;
254
Bjorn Helgaas151d16d2012-02-23 20:18:56 -0700255 if (pci_has_flag(PCI_PROBE_ONLY) && dev &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
257 pci_read_bridge_bases(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
260 list_for_each_entry(dev, &bus->devices, bus_list) {
261 pdev_save_srm_config(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263}
264
265void __init
266pcibios_update_irq(struct pci_dev *dev, int irq)
267{
268 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271int
272pcibios_enable_device(struct pci_dev *dev, int mask)
273{
Bjorn Helgaas10f000a2008-03-04 11:56:48 -0700274 return pci_enable_resources(dev, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277/*
278 * If we set up a device for bus mastering, we need to check the latency
279 * timer as certain firmware forgets to set it properly, as seen
280 * on SX164 and LX164 with SRM.
281 */
282void
283pcibios_set_master(struct pci_dev *dev)
284{
285 u8 lat;
286 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
287 if (lat >= 16) return;
288 printk("PCI: Setting latency timer of device %s to 64\n",
289 pci_name(dev));
290 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
291}
292
Ivan Kokshaysky72cff122008-04-24 16:51:55 +0400293void __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294pcibios_claim_one_bus(struct pci_bus *b)
295{
296 struct pci_dev *dev;
297 struct pci_bus *child_bus;
298
299 list_for_each_entry(dev, &b->devices, bus_list) {
300 int i;
301
302 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
303 struct resource *r = &dev->resource[i];
304
305 if (r->parent || !r->start || !r->flags)
306 continue;
Bjorn Helgaas151d16d2012-02-23 20:18:56 -0700307 if (pci_has_flag(PCI_PROBE_ONLY) ||
308 (r->flags & IORESOURCE_PCI_FIXED))
Ivan Kokshaysky72cff122008-04-24 16:51:55 +0400309 pci_claim_resource(dev, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311 }
312
313 list_for_each_entry(child_bus, &b->children, node)
314 pcibios_claim_one_bus(child_bus);
315}
316
317static void __init
318pcibios_claim_console_setup(void)
319{
320 struct pci_bus *b;
321
322 list_for_each_entry(b, &pci_root_buses, node)
323 pcibios_claim_one_bus(b);
324}
325
326void __init
327common_init_pci(void)
328{
329 struct pci_controller *hose;
Bjorn Helgaasa2f33da2011-10-28 16:26:11 -0600330 struct list_head resources;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct pci_bus *bus;
332 int next_busno;
333 int need_domain_info = 0;
Bjorn Helgaasa2f33da2011-10-28 16:26:11 -0600334 u32 pci_mem_end;
335 u32 sg_base;
336 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* Scan all of the recorded PCI controllers. */
339 for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
Bjorn Helgaasa2f33da2011-10-28 16:26:11 -0600340 sg_base = hose->sg_pci ? hose->sg_pci->dma_base : ~0;
341
342 /* Adjust hose mem_space limit to prevent PCI allocations
343 in the iommu windows. */
344 pci_mem_end = min((u32)__direct_map_base, sg_base) - 1;
345 end = hose->mem_space->start + pci_mem_end;
346 if (hose->mem_space->end > end)
347 hose->mem_space->end = end;
348
349 INIT_LIST_HEAD(&resources);
Bjorn Helgaasc04d9e32012-02-23 20:19:01 -0700350 pci_add_resource_offset(&resources, hose->io_space,
351 hose->io_space->start);
352 pci_add_resource_offset(&resources, hose->mem_space,
353 hose->mem_space->start);
Bjorn Helgaasa2f33da2011-10-28 16:26:11 -0600354
355 bus = pci_scan_root_bus(NULL, next_busno, alpha_mv.pci_ops,
356 hose, &resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 hose->bus = bus;
358 hose->need_domain_info = need_domain_info;
359 next_busno = bus->subordinate + 1;
360 /* Don't allow 8-bit bus number overflow inside the hose -
361 reserve some space for bridges. */
362 if (next_busno > 224) {
363 next_busno = 0;
364 need_domain_info = 1;
365 }
366 }
367
Ivan Kokshaysky72cff122008-04-24 16:51:55 +0400368 pcibios_claim_console_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 pci_assign_unassigned_resources();
371 pci_fixup_irqs(alpha_mv.pci_swizzle, alpha_mv.pci_map_irq);
372}
373
374
375struct pci_controller * __init
376alloc_pci_controller(void)
377{
378 struct pci_controller *hose;
379
380 hose = alloc_bootmem(sizeof(*hose));
381
382 *hose_tail = hose;
383 hose_tail = &hose->next;
384
385 return hose;
386}
387
388struct resource * __init
389alloc_resource(void)
390{
391 struct resource *res;
392
393 res = alloc_bootmem(sizeof(*res));
394
395 return res;
396}
397
398
399/* Provide information on locations of various I/O regions in physical
400 memory. Do this on a per-card basis so that we choose the right hose. */
401
402asmlinkage long
403sys_pciconfig_iobase(long which, unsigned long bus, unsigned long dfn)
404{
405 struct pci_controller *hose;
406 struct pci_dev *dev;
407
408 /* from hose or from bus.devfn */
409 if (which & IOBASE_FROM_HOSE) {
410 for(hose = hose_head; hose; hose = hose->next)
411 if (hose->index == bus) break;
412 if (!hose) return -ENODEV;
413 } else {
414 /* Special hook for ISA access. */
415 if (bus == 0 && dfn == 0) {
416 hose = pci_isa_hose;
417 } else {
Alan Cox074cec52006-12-06 20:33:59 -0800418 dev = pci_get_bus_and_slot(bus, dfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (!dev)
420 return -ENODEV;
421 hose = dev->sysdata;
Alan Cox074cec52006-12-06 20:33:59 -0800422 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 }
425
426 switch (which & ~IOBASE_FROM_HOSE) {
427 case IOBASE_HOSE:
428 return hose->index;
429 case IOBASE_SPARSE_MEM:
430 return hose->sparse_mem_base;
431 case IOBASE_DENSE_MEM:
432 return hose->dense_mem_base;
433 case IOBASE_SPARSE_IO:
434 return hose->sparse_io_base;
435 case IOBASE_DENSE_IO:
436 return hose->dense_io_base;
437 case IOBASE_ROOT_BUS:
438 return hose->bus->number;
439 }
440
441 return -EOPNOTSUPP;
442}
443
Michael S. Tsirkinf2971c42011-11-24 20:48:56 +0200444/* Destroy an __iomem token. Not copied from lib/iomap.c. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
447{
448 if (__is_mmio(addr))
449 iounmap(addr);
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452EXPORT_SYMBOL(pci_iounmap);
Adrian Bunk8255cf32007-01-06 21:48:41 +0100453
454/* FIXME: Some boxes have multiple ISA bridges! */
455struct pci_dev *isa_bridge;
456EXPORT_SYMBOL(isa_bridge);