2 * linux/arch/alpha/kernel/pci.c
4 * Extruded from code written by
5 * Dave Rusling (david.rusling@reo.mts.dec.com)
6 * David Mosberger (davidm@cs.arizona.edu)
9 /* 2.3.x PCI/resources, 1999 Andrea Arcangeli <andrea@suse.de> */
12 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
13 * PCI-PCI bridges cleanup
15 #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>
31 * Some string constants used by the various core logics.
34 const 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"
39 const 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"
44 const char pci_hae0_name[] = "HAE0";
46 /* Indicate whether we respect the PCI setup left by console. */
48 * Make this long-lived so that we know when shutting down
49 * whether we probed only or not.
54 * The PCI controller list.
57 struct pci_controller *hose_head, **hose_tail = &hose_head;
58 struct pci_controller *pci_isa_hose;
65 quirk_isa_bridge(struct pci_dev *dev)
67 dev->class = PCI_CLASS_BRIDGE_ISA << 8;
69 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82378, quirk_isa_bridge);
72 quirk_cypress(struct pci_dev *dev)
74 /* The Notorious Cy82C693 chip. */
76 /* The Cypress IDE controller doesn't support native mode, but it
77 has programmable addresses of IDE command/control registers.
78 This violates PCI specifications, confuses the IDE subsystem and
79 causes resource conflicts between the primary HD_CMD register and
80 the floppy controller. Ugh. Fix that. */
81 if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE) {
82 dev->resource[0].flags = 0;
83 dev->resource[1].flags = 0;
86 /* The Cypress bridge responds on the PCI bus in the address range
87 0xffff0000-0xffffffff (conventional x86 BIOS ROM). There is no
88 way to turn this off. The bridge also supports several extended
89 BIOS ranges (disabled after power-up), and some consoles do turn
90 them on. So if we use a large direct-map window, or a large SG
91 window, we must avoid the entire 0xfff00000-0xffffffff region. */
92 else if (dev->class >> 8 == PCI_CLASS_BRIDGE_ISA) {
93 if (__direct_map_base + __direct_map_size >= 0xfff00000UL)
94 __direct_map_size = 0xfff00000UL - __direct_map_base;
96 struct pci_controller *hose = dev->sysdata;
97 struct pci_iommu_arena *pci = hose->sg_pci;
98 if (pci && pci->dma_base + pci->size >= 0xfff00000UL)
99 pci->size = 0xfff00000UL - pci->dma_base;
103 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693, quirk_cypress);
105 /* Called for each device after PCI setup is done. */
107 pcibios_fixup_final(struct pci_dev *dev)
109 unsigned int class = dev->class >> 8;
111 if (class == PCI_CLASS_BRIDGE_ISA || class == PCI_CLASS_BRIDGE_EISA) {
112 dev->dma_mask = MAX_ISA_DMA_ADDRESS - 1;
116 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_final);
118 /* Just declaring that the power-of-ten prefixes are actually the
119 power-of-two ones doesn't make it true :) */
125 pcibios_align_resource(void *data, struct resource *res,
126 resource_size_t size, resource_size_t align)
128 struct pci_dev *dev = data;
129 struct pci_controller *hose = dev->sysdata;
130 unsigned long alignto;
131 resource_size_t start = res->start;
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;
139 * Put everything into 0x00-0xff region modulo 0x400
142 start = (start + 0x3ff) & ~0x3ff;
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;
150 * The following holds at least for the Low Cost
151 * Alpha implementation of the PCI interface:
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!
165 /* Align to multiple of size of minimum base. */
166 alignto = max(0x1000UL, align);
167 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);
172 start = ALIGN(start, alignto);
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);
191 if (alpha_mv.init_pci)
196 subsys_initcall(pcibios_init);
199 pcibios_setup(char *str)
204 #ifdef ALPHA_RESTORE_SRM_SETUP
205 static struct pdev_srm_saved_conf *srm_saved_configs;
208 pdev_save_srm_config(struct pci_dev *dev)
210 struct pdev_srm_saved_conf *tmp;
211 static int printed = 0;
213 if (!alpha_using_srm || pci_probe_only)
217 printk(KERN_INFO "pci: enabling save/restore of SRM state\n");
221 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
223 printk(KERN_ERR "%s: kmalloc() failed!\n", __FUNCTION__);
226 tmp->next = srm_saved_configs;
231 srm_saved_configs = tmp;
235 pci_restore_srm_config(void)
237 struct pdev_srm_saved_conf *tmp;
239 /* No need to restore if probed only. */
243 /* Restore SRM config. */
244 for (tmp = srm_saved_configs; tmp; tmp = tmp->next) {
245 pci_restore_state(tmp->dev);
251 pcibios_fixup_resource(struct resource *res, struct resource *root)
253 res->start += root->start;
254 res->end += root->start;
258 pcibios_fixup_device_resources(struct pci_dev *dev, struct pci_bus *bus)
260 /* Update device resources. */
261 struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
264 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
265 if (!dev->resource[i].start)
267 if (dev->resource[i].flags & IORESOURCE_IO)
268 pcibios_fixup_resource(&dev->resource[i],
270 else if (dev->resource[i].flags & IORESOURCE_MEM)
271 pcibios_fixup_resource(&dev->resource[i],
277 pcibios_fixup_bus(struct pci_bus *bus)
279 /* Propagate hose info into the subordinate devices. */
281 struct pci_controller *hose = bus->sysdata;
282 struct pci_dev *dev = bus->self;
287 u32 sg_base = hose->sg_pci ? hose->sg_pci->dma_base : ~0;
290 bus->resource[0] = hose->io_space;
291 bus->resource[1] = hose->mem_space;
293 /* Adjust hose mem_space limit to prevent PCI allocations
294 in the iommu windows. */
295 pci_mem_end = min((u32)__direct_map_base, sg_base) - 1;
296 end = hose->mem_space->start + pci_mem_end;
297 if (hose->mem_space->end > end)
298 hose->mem_space->end = end;
299 } else if (pci_probe_only &&
300 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
301 pci_read_bridge_bases(bus);
302 pcibios_fixup_device_resources(dev, bus);
305 list_for_each_entry(dev, &bus->devices, bus_list) {
306 pdev_save_srm_config(dev);
307 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
308 pcibios_fixup_device_resources(dev, bus);
313 pcibios_update_irq(struct pci_dev *dev, int irq)
315 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
318 /* Most Alphas have straight-forward swizzling needs. */
321 common_swizzle(struct pci_dev *dev, u8 *pinp)
325 while (dev->bus->parent) {
326 pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
327 /* Move up the chain of bridges. */
328 dev = dev->bus->self;
332 /* The slot is the slot of the last bridge. */
333 return PCI_SLOT(dev->devfn);
337 pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
338 struct resource *res)
340 struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
341 unsigned long offset = 0;
343 if (res->flags & IORESOURCE_IO)
344 offset = hose->io_space->start;
345 else if (res->flags & IORESOURCE_MEM)
346 offset = hose->mem_space->start;
348 region->start = res->start - offset;
349 region->end = res->end - offset;
352 void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
353 struct pci_bus_region *region)
355 struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
356 unsigned long offset = 0;
358 if (res->flags & IORESOURCE_IO)
359 offset = hose->io_space->start;
360 else if (res->flags & IORESOURCE_MEM)
361 offset = hose->mem_space->start;
363 res->start = region->start + offset;
364 res->end = region->end + offset;
367 #ifdef CONFIG_HOTPLUG
368 EXPORT_SYMBOL(pcibios_resource_to_bus);
369 EXPORT_SYMBOL(pcibios_bus_to_resource);
373 pcibios_enable_device(struct pci_dev *dev, int mask)
375 return pci_enable_resources(dev, mask);
379 * If we set up a device for bus mastering, we need to check the latency
380 * timer as certain firmware forgets to set it properly, as seen
381 * on SX164 and LX164 with SRM.
384 pcibios_set_master(struct pci_dev *dev)
387 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
388 if (lat >= 16) return;
389 printk("PCI: Setting latency timer of device %s to 64\n",
391 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
395 pcibios_claim_one_bus(struct pci_bus *b)
398 struct pci_bus *child_bus;
400 list_for_each_entry(dev, &b->devices, bus_list) {
403 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
404 struct resource *r = &dev->resource[i];
406 if (r->parent || !r->start || !r->flags)
408 pci_claim_resource(dev, i);
412 list_for_each_entry(child_bus, &b->children, node)
413 pcibios_claim_one_bus(child_bus);
417 pcibios_claim_console_setup(void)
421 list_for_each_entry(b, &pci_root_buses, node)
422 pcibios_claim_one_bus(b);
426 common_init_pci(void)
428 struct pci_controller *hose;
431 int need_domain_info = 0;
433 /* Scan all of the recorded PCI controllers. */
434 for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
435 bus = pci_scan_bus(next_busno, alpha_mv.pci_ops, hose);
437 hose->need_domain_info = need_domain_info;
438 next_busno = bus->subordinate + 1;
439 /* Don't allow 8-bit bus number overflow inside the hose -
440 reserve some space for bridges. */
441 if (next_busno > 224) {
443 need_domain_info = 1;
448 pcibios_claim_console_setup();
450 pci_assign_unassigned_resources();
451 pci_fixup_irqs(alpha_mv.pci_swizzle, alpha_mv.pci_map_irq);
455 struct pci_controller * __init
456 alloc_pci_controller(void)
458 struct pci_controller *hose;
460 hose = alloc_bootmem(sizeof(*hose));
463 hose_tail = &hose->next;
468 struct resource * __init
471 struct resource *res;
473 res = alloc_bootmem(sizeof(*res));
479 /* Provide information on locations of various I/O regions in physical
480 memory. Do this on a per-card basis so that we choose the right hose. */
483 sys_pciconfig_iobase(long which, unsigned long bus, unsigned long dfn)
485 struct pci_controller *hose;
488 /* from hose or from bus.devfn */
489 if (which & IOBASE_FROM_HOSE) {
490 for(hose = hose_head; hose; hose = hose->next)
491 if (hose->index == bus) break;
492 if (!hose) return -ENODEV;
494 /* Special hook for ISA access. */
495 if (bus == 0 && dfn == 0) {
498 dev = pci_get_bus_and_slot(bus, dfn);
506 switch (which & ~IOBASE_FROM_HOSE) {
509 case IOBASE_SPARSE_MEM:
510 return hose->sparse_mem_base;
511 case IOBASE_DENSE_MEM:
512 return hose->dense_mem_base;
513 case IOBASE_SPARSE_IO:
514 return hose->sparse_io_base;
515 case IOBASE_DENSE_IO:
516 return hose->dense_io_base;
517 case IOBASE_ROOT_BUS:
518 return hose->bus->number;
524 /* Create an __iomem token from a PCI BAR. Copied from lib/iomap.c with
525 no changes, since we don't want the other things in that object file. */
527 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
529 unsigned long start = pci_resource_start(dev, bar);
530 unsigned long len = pci_resource_len(dev, bar);
531 unsigned long flags = pci_resource_flags(dev, bar);
535 if (maxlen && len > maxlen)
537 if (flags & IORESOURCE_IO)
538 return ioport_map(start, len);
539 if (flags & IORESOURCE_MEM) {
540 /* Not checking IORESOURCE_CACHEABLE because alpha does
541 not distinguish between ioremap and ioremap_nocache. */
542 return ioremap(start, len);
547 /* Destroy that token. Not copied from lib/iomap.c. */
549 void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
555 EXPORT_SYMBOL(pci_iomap);
556 EXPORT_SYMBOL(pci_iounmap);
558 /* FIXME: Some boxes have multiple ISA bridges! */
559 struct pci_dev *isa_bridge;
560 EXPORT_SYMBOL(isa_bridge);