ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / alpha / kernel / pci.c
1 /*
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  */
15 #include <linux/config.h>
16 #include <linux/string.h>
17 #include <linux/pci.h>
18 #include <linux/init.h>
19 #include <linux/ioport.h>
20 #include <linux/kernel.h>
21 #include <linux/bootmem.h>
22 #include <linux/module.h>
23 #include <linux/cache.h>
24 #include <linux/slab.h>
25 #include <asm/machvec.h>
26
27 #include "proto.h"
28 #include "pci_impl.h"
29
30
31 /*
32  * Some string constants used by the various core logics. 
33  */
34
35 const char *const pci_io_names[] = {
36   "PCI IO bus 0", "PCI IO bus 1", "PCI IO bus 2", "PCI IO bus 3",
37   "PCI IO bus 4", "PCI IO bus 5", "PCI IO bus 6", "PCI IO bus 7"
38 };
39
40 const char *const pci_mem_names[] = {
41   "PCI mem bus 0", "PCI mem bus 1", "PCI mem bus 2", "PCI mem bus 3",
42   "PCI mem bus 4", "PCI mem bus 5", "PCI mem bus 6", "PCI mem bus 7"
43 };
44
45 const char pci_hae0_name[] = "HAE0";
46
47 /* Indicate whether we respect the PCI setup left by console. */
48 /*
49  * Make this long-lived  so that we know when shutting down
50  * whether we probed only or not.
51  */
52 int pci_probe_only;
53
54 /*
55  * The PCI controller list.
56  */
57
58 struct pci_controller *hose_head, **hose_tail = &hose_head;
59 struct pci_controller *pci_isa_hose;
60
61 /*
62  * Quirks.
63  */
64
65 static void __init
66 quirk_isa_bridge(struct pci_dev *dev)
67 {
68         dev->class = PCI_CLASS_BRIDGE_ISA << 8;
69 }
70
71 static void __init
72 quirk_cypress(struct pci_dev *dev)
73 {
74         /* The Notorious Cy82C693 chip.  */
75
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;
84         }
85
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;
95                 else {
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;
100                 }
101         }
102 }
103
104 /* Called for each device after PCI setup is done. */
105 static void __init
106 pcibios_fixup_final(struct pci_dev *dev)
107 {
108         unsigned int class = dev->class >> 8;
109
110         if (class == PCI_CLASS_BRIDGE_ISA || class == PCI_CLASS_BRIDGE_EISA) {
111                 dev->dma_mask = MAX_ISA_DMA_ADDRESS - 1;
112                 isa_bridge = dev;
113         }
114 }
115
116 struct pci_fixup pcibios_fixups[] __initdata = {
117         { PCI_FIXUP_HEADER, PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82378,
118           quirk_isa_bridge },
119         { PCI_FIXUP_HEADER, PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693,
120           quirk_cypress },
121         { PCI_FIXUP_FINAL,  PCI_ANY_ID, PCI_ANY_ID,
122           pcibios_fixup_final },
123         { 0 }
124 };
125
126 #define KB                      1024
127 #define MB                      (1024*KB)
128 #define GB                      (1024*MB)
129
130 void
131 pcibios_align_resource(void *data, struct resource *res,
132                        unsigned long size, unsigned long align)
133 {
134         struct pci_dev *dev = data;
135         struct pci_controller *hose = dev->sysdata;
136         unsigned long alignto;
137         unsigned long start = res->start;
138
139         if (res->flags & IORESOURCE_IO) {
140                 /* Make sure we start at our min on all hoses */
141                 if (start - hose->io_space->start < PCIBIOS_MIN_IO)
142                         start = PCIBIOS_MIN_IO + hose->io_space->start;
143
144                 /*
145                  * Put everything into 0x00-0xff region modulo 0x400
146                  */
147                 if (start & 0x300)
148                         start = (start + 0x3ff) & ~0x3ff;
149         }
150         else if (res->flags & IORESOURCE_MEM) {
151                 /* Make sure we start at our min on all hoses */
152                 if (start - hose->mem_space->start < PCIBIOS_MIN_MEM)
153                         start = PCIBIOS_MIN_MEM + hose->mem_space->start;
154
155                 /*
156                  * The following holds at least for the Low Cost
157                  * Alpha implementation of the PCI interface:
158                  *
159                  * In sparse memory address space, the first
160                  * octant (16MB) of every 128MB segment is
161                  * aliased to the very first 16 MB of the
162                  * address space (i.e., it aliases the ISA
163                  * memory address space).  Thus, we try to
164                  * avoid allocating PCI devices in that range.
165                  * Can be allocated in 2nd-7th octant only.
166                  * Devices that need more than 112MB of
167                  * address space must be accessed through
168                  * dense memory space only!
169                  */
170
171                 /* Align to multiple of size of minimum base.  */
172                 alignto = max(0x1000UL, align);
173                 start = ALIGN(start, alignto);
174                 if (hose->sparse_mem_base && size <= 7 * 16*MB) {
175                         if (((start / (16*MB)) & 0x7) == 0) {
176                                 start &= ~(128*MB - 1);
177                                 start += 16*MB;
178                                 start  = ALIGN(start, alignto);
179                         }
180                         if (start/(128*MB) != (start + size - 1)/(128*MB)) {
181                                 start &= ~(128*MB - 1);
182                                 start += (128 + 16)*MB;
183                                 start  = ALIGN(start, alignto);
184                         }
185                 }
186         }
187
188         res->start = start;
189 }
190 #undef KB
191 #undef MB
192 #undef GB
193
194 static int __init
195 pcibios_init(void)
196 {
197         if (alpha_mv.init_pci)
198                 alpha_mv.init_pci();
199         return 0;
200 }
201
202 subsys_initcall(pcibios_init);
203
204 char * __init
205 pcibios_setup(char *str)
206 {
207         return str;
208 }
209
210 #ifdef ALPHA_RESTORE_SRM_SETUP
211 static struct pdev_srm_saved_conf *srm_saved_configs;
212
213 void __init
214 pdev_save_srm_config(struct pci_dev *dev)
215 {
216         struct pdev_srm_saved_conf *tmp;
217         static int printed = 0;
218
219         if (!alpha_using_srm || pci_probe_only)
220                 return;
221
222         if (!printed) {
223                 printk(KERN_INFO "pci: enabling save/restore of SRM state\n");
224                 printed = 1;
225         }
226
227         tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
228         if (!tmp) {
229                 printk(KERN_ERR "%s: kmalloc() failed!\n", __FUNCTION__);
230                 return;
231         }
232         tmp->next = srm_saved_configs;
233         tmp->dev = dev;
234
235         pci_save_state(dev, tmp->regs);
236
237         srm_saved_configs = tmp;
238 }
239
240 void
241 pci_restore_srm_config(void)
242 {
243         struct pdev_srm_saved_conf *tmp;
244
245         /* No need to restore if probed only. */
246         if (pci_probe_only)
247                 return;
248
249         /* Restore SRM config. */
250         for (tmp = srm_saved_configs; tmp; tmp = tmp->next) {
251                 pci_restore_state(tmp->dev, tmp->regs);
252         }
253 }
254 #endif
255
256 void __init
257 pcibios_fixup_resource(struct resource *res, struct resource *root)
258 {
259         res->start += root->start;
260         res->end += root->start;
261 }
262
263 void __init
264 pcibios_fixup_device_resources(struct pci_dev *dev, struct pci_bus *bus)
265 {
266         /* Update device resources.  */
267         struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
268         int i;
269
270         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
271                 if (!dev->resource[i].start)
272                         continue;
273                 if (dev->resource[i].flags & IORESOURCE_IO)
274                         pcibios_fixup_resource(&dev->resource[i],
275                                                hose->io_space);
276                 else if (dev->resource[i].flags & IORESOURCE_MEM)
277                         pcibios_fixup_resource(&dev->resource[i],
278                                                hose->mem_space);
279         }
280 }
281
282 void __init
283 pcibios_fixup_bus(struct pci_bus *bus)
284 {
285         /* Propagate hose info into the subordinate devices.  */
286
287         struct pci_controller *hose = bus->sysdata;
288         struct list_head *ln;
289         struct pci_dev *dev = bus->self;
290
291         if (!dev) {
292                 /* Root bus. */
293                 u32 pci_mem_end;
294                 u32 sg_base = hose->sg_pci ? hose->sg_pci->dma_base : ~0;
295                 unsigned long end;
296
297                 bus->resource[0] = hose->io_space;
298                 bus->resource[1] = hose->mem_space;
299
300                 /* Adjust hose mem_space limit to prevent PCI allocations
301                    in the iommu windows. */
302                 pci_mem_end = min((u32)__direct_map_base, sg_base) - 1;
303                 end = hose->mem_space->start + pci_mem_end;
304                 if (hose->mem_space->end > end)
305                         hose->mem_space->end = end;
306         } else if (pci_probe_only &&
307                    (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
308                 pci_read_bridge_bases(bus);
309                 pcibios_fixup_device_resources(dev, bus);
310         } 
311
312         for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
313                 struct pci_dev *dev = pci_dev_b(ln);
314
315                 pdev_save_srm_config(dev);
316                 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
317                         pcibios_fixup_device_resources(dev, bus);
318         }
319 }
320
321 void __init
322 pcibios_update_irq(struct pci_dev *dev, int irq)
323 {
324         pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
325 }
326
327 /* Most Alphas have straight-forward swizzling needs.  */
328
329 u8 __init
330 common_swizzle(struct pci_dev *dev, u8 *pinp)
331 {
332         u8 pin = *pinp;
333
334         while (dev->bus->parent) {
335                 pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
336                 /* Move up the chain of bridges. */
337                 dev = dev->bus->self;
338         }
339         *pinp = pin;
340
341         /* The slot is the slot of the last bridge. */
342         return PCI_SLOT(dev->devfn);
343 }
344
345 void __devinit
346 pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
347                          struct resource *res)
348 {
349         struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
350         unsigned long offset = 0;
351
352         if (res->flags & IORESOURCE_IO)
353                 offset = hose->io_space->start;
354         else if (res->flags & IORESOURCE_MEM)
355                 offset = hose->mem_space->start;
356
357         region->start = res->start - offset;
358         region->end = res->end - offset;
359 }
360
361 #ifdef CONFIG_HOTPLUG
362 EXPORT_SYMBOL(pcibios_resource_to_bus);
363 #endif
364
365 int
366 pcibios_enable_device(struct pci_dev *dev, int mask)
367 {
368         u16 cmd, oldcmd;
369         int i;
370
371         pci_read_config_word(dev, PCI_COMMAND, &cmd);
372         oldcmd = cmd;
373
374         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
375                 struct resource *res = &dev->resource[i];
376
377                 if (res->flags & IORESOURCE_IO)
378                         cmd |= PCI_COMMAND_IO;
379                 else if (res->flags & IORESOURCE_MEM)
380                         cmd |= PCI_COMMAND_MEMORY;
381         }
382
383         if (cmd != oldcmd) {
384                 printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
385                        pci_name(dev), cmd);
386                 /* Enable the appropriate bits in the PCI command register.  */
387                 pci_write_config_word(dev, PCI_COMMAND, cmd);
388         }
389         return 0;
390 }
391
392 /*
393  *  If we set up a device for bus mastering, we need to check the latency
394  *  timer as certain firmware forgets to set it properly, as seen
395  *  on SX164 and LX164 with SRM.
396  */
397 void
398 pcibios_set_master(struct pci_dev *dev)
399 {
400         u8 lat;
401         pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
402         if (lat >= 16) return;
403         printk("PCI: Setting latency timer of device %s to 64\n",
404                                                         pci_name(dev));
405         pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
406 }
407
408 static void __init
409 pcibios_claim_one_bus(struct pci_bus *b)
410 {
411         struct list_head *ld;
412         struct pci_bus *child_bus;
413
414         for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
415                 struct pci_dev *dev = pci_dev_b(ld);
416                 int i;
417
418                 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
419                         struct resource *r = &dev->resource[i];
420
421                         if (r->parent || !r->start || !r->flags)
422                                 continue;
423                         pci_claim_resource(dev, i);
424                 }
425         }
426
427         list_for_each_entry(child_bus, &b->children, node)
428                 pcibios_claim_one_bus(child_bus);
429 }
430
431 static void __init
432 pcibios_claim_console_setup(void)
433 {
434         struct list_head *lb;
435
436         for(lb = pci_root_buses.next; lb != &pci_root_buses; lb = lb->next) {
437                 struct pci_bus *b = pci_bus_b(lb);
438                 pcibios_claim_one_bus(b);
439         }
440 }
441
442 void __init
443 common_init_pci(void)
444 {
445         struct pci_controller *hose;
446         struct pci_bus *bus;
447         int next_busno;
448         int need_domain_info = 0;
449
450         /* Scan all of the recorded PCI controllers.  */
451         for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
452                 bus = pci_scan_bus(next_busno, alpha_mv.pci_ops, hose);
453                 hose->bus = bus;
454                 hose->need_domain_info = need_domain_info;
455                 next_busno = bus->subordinate + 1;
456                 /* Don't allow 8-bit bus number overflow inside the hose -
457                    reserve some space for bridges. */ 
458                 if (next_busno > 224) {
459                         next_busno = 0;
460                         need_domain_info = 1;
461                 }
462         }
463
464         if (pci_probe_only)
465                 pcibios_claim_console_setup();
466
467         pci_assign_unassigned_resources();
468         pci_fixup_irqs(alpha_mv.pci_swizzle, alpha_mv.pci_map_irq);
469 }
470
471
472 struct pci_controller * __init
473 alloc_pci_controller(void)
474 {
475         struct pci_controller *hose;
476
477         hose = alloc_bootmem(sizeof(*hose));
478
479         *hose_tail = hose;
480         hose_tail = &hose->next;
481
482         return hose;
483 }
484
485 struct resource * __init
486 alloc_resource(void)
487 {
488         struct resource *res;
489
490         res = alloc_bootmem(sizeof(*res));
491
492         return res;
493 }
494
495
496 /* Provide information on locations of various I/O regions in physical
497    memory.  Do this on a per-card basis so that we choose the right hose.  */
498
499 asmlinkage long
500 sys_pciconfig_iobase(long which, unsigned long bus, unsigned long dfn)
501 {
502         struct pci_controller *hose;
503         struct pci_dev *dev;
504
505         /* from hose or from bus.devfn */
506         if (which & IOBASE_FROM_HOSE) {
507                 for(hose = hose_head; hose; hose = hose->next) 
508                         if (hose->index == bus) break;
509                 if (!hose) return -ENODEV;
510         } else {
511                 /* Special hook for ISA access.  */
512                 if (bus == 0 && dfn == 0) {
513                         hose = pci_isa_hose;
514                 } else {
515                         dev = pci_find_slot(bus, dfn);
516                         if (!dev)
517                                 return -ENODEV;
518                         hose = dev->sysdata;
519                 }
520         }
521
522         switch (which & ~IOBASE_FROM_HOSE) {
523         case IOBASE_HOSE:
524                 return hose->index;
525         case IOBASE_SPARSE_MEM:
526                 return hose->sparse_mem_base;
527         case IOBASE_DENSE_MEM:
528                 return hose->dense_mem_base;
529         case IOBASE_SPARSE_IO:
530                 return hose->sparse_io_base;
531         case IOBASE_DENSE_IO:
532                 return hose->dense_io_base;
533         case IOBASE_ROOT_BUS:
534                 return hose->bus->number;
535         }
536
537         return -EOPNOTSUPP;
538 }