upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / arch / ia64 / pci / pci.c
1 /*
2  * pci.c - Low-Level PCI Access in IA-64
3  *
4  * Derived from bios32.c of i386 tree.
5  *
6  * Copyright (C) 2002 Hewlett-Packard Co
7  *      David Mosberger-Tang <davidm@hpl.hp.com>
8  *      Bjorn Helgaas <bjorn_helgaas@hp.com>
9  *
10  * Note: Above list of copyright holders is incomplete...
11  */
12 #include <linux/config.h>
13
14 #include <linux/acpi.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/pci.h>
18 #include <linux/init.h>
19 #include <linux/ioport.h>
20 #include <linux/slab.h>
21 #include <linux/smp_lock.h>
22 #include <linux/spinlock.h>
23
24 #include <asm/machvec.h>
25 #include <asm/page.h>
26 #include <asm/segment.h>
27 #include <asm/system.h>
28 #include <asm/io.h>
29
30 #include <asm/sal.h>
31
32
33 #ifdef CONFIG_SMP
34 # include <asm/smp.h>
35 #endif
36 #include <asm/irq.h>
37 #include <asm/hw_irq.h>
38
39
40 #undef DEBUG
41 #define DEBUG
42
43 #ifdef DEBUG
44 #define DBG(x...) printk(x)
45 #else
46 #define DBG(x...)
47 #endif
48
49 static int pci_routeirq;
50
51 /*
52  * Low-level SAL-based PCI configuration access functions. Note that SAL
53  * calls are already serialized (via sal_lock), so we don't need another
54  * synchronization mechanism here.
55  */
56
57 #define PCI_SAL_ADDRESS(seg, bus, devfn, reg)   \
58         ((u64)(seg << 24) | (u64)(bus << 16) |  \
59          (u64)(devfn << 8) | (u64)(reg))
60
61 /* SAL 3.2 adds support for extended config space. */
62
63 #define PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg)       \
64         ((u64)(seg << 28) | (u64)(bus << 20) |          \
65          (u64)(devfn << 12) | (u64)(reg))
66
67 static int
68 pci_sal_read (int seg, int bus, int devfn, int reg, int len, u32 *value)
69 {
70         u64 addr, mode, data = 0;
71         int result = 0;
72
73         if ((seg > 255) || (bus > 255) || (devfn > 255) || (reg > 4095))
74                 return -EINVAL;
75
76         if ((seg | reg) <= 255) {
77                 addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
78                 mode = 0;
79         } else {
80                 addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
81                 mode = 1;
82         }
83         result = ia64_sal_pci_config_read(addr, mode, len, &data);
84
85         *value = (u32) data;
86
87         return result;
88 }
89
90 static int
91 pci_sal_write (int seg, int bus, int devfn, int reg, int len, u32 value)
92 {
93         u64 addr, mode;
94
95         if ((seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
96                 return -EINVAL;
97
98         if ((seg | reg) <= 255) {
99                 addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
100                 mode = 0;
101         } else {
102                 addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
103                 mode = 1;
104         }
105         return ia64_sal_pci_config_write(addr, mode, len, value);
106 }
107
108 static struct pci_raw_ops pci_sal_ops = {
109         .read =         pci_sal_read,
110         .write =        pci_sal_write
111 };
112
113 struct pci_raw_ops *raw_pci_ops = &pci_sal_ops;
114
115 static int
116 pci_read (struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
117 {
118         return raw_pci_ops->read(pci_domain_nr(bus), bus->number,
119                                  devfn, where, size, value);
120 }
121
122 static int
123 pci_write (struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
124 {
125         return raw_pci_ops->write(pci_domain_nr(bus), bus->number,
126                                   devfn, where, size, value);
127 }
128
129 struct pci_ops pci_root_ops = {
130         .read = pci_read,
131         .write = pci_write,
132 };
133
134 static int __init
135 pci_acpi_init (void)
136 {
137         struct pci_dev *dev = NULL;
138
139         printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
140
141 #ifdef CONFIG_NUMA
142 extern acpi_status acpi_map_iosapic (acpi_handle, u32, void*, void**);
143
144         acpi_get_devices(NULL, acpi_map_iosapic, NULL, NULL);
145 #endif
146
147         if (pci_routeirq) {
148                 /*
149                  * PCI IRQ routing is set up by pci_enable_device(), but we
150                  * also do it here in case there are still broken drivers that
151                  * don't use pci_enable_device().
152                  */
153                 printk(KERN_INFO "** Routing PCI interrupts for all devices because \"pci=routeirq\"\n");
154                 printk(KERN_INFO "** was specified.  If this was required to make a driver work,\n");
155                 printk(KERN_INFO "** please email the output of \"lspci\" to bjorn.helgaas@hp.com\n");
156                 printk(KERN_INFO "** so I can fix the driver.\n");
157                 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL)
158                         acpi_pci_irq_enable(dev);
159         } else {
160                 printk(KERN_INFO "** PCI interrupts are no longer routed automatically.  If this\n");
161                 printk(KERN_INFO "** causes a device to stop working, it is probably because the\n");
162                 printk(KERN_INFO "** driver failed to call pci_enable_device().  As a temporary\n");
163                 printk(KERN_INFO "** workaround, the \"pci=routeirq\" argument restores the old\n");
164                 printk(KERN_INFO "** behavior.  If this argument makes the device work again,\n");
165                 printk(KERN_INFO "** please email the output of \"lspci\" to bjorn.helgaas@hp.com\n");
166                 printk(KERN_INFO "** so I can fix the driver.\n");
167         }
168
169         return 0;
170 }
171
172 subsys_initcall(pci_acpi_init);
173
174 /* Called by ACPI when it finds a new root bus.  */
175
176 static struct pci_controller * __devinit
177 alloc_pci_controller (int seg)
178 {
179         struct pci_controller *controller;
180
181         controller = kmalloc(sizeof(*controller), GFP_KERNEL);
182         if (!controller)
183                 return NULL;
184
185         memset(controller, 0, sizeof(*controller));
186         controller->segment = seg;
187         return controller;
188 }
189
190 static int __devinit
191 alloc_resource (char *name, struct resource *root, unsigned long start, unsigned long end,
192                 unsigned long flags)
193 {
194         struct resource *res;
195
196         res = kmalloc(sizeof(*res), GFP_KERNEL);
197         if (!res)
198                 return -ENOMEM;
199
200         memset(res, 0, sizeof(*res));
201         res->name = name;
202         res->start = start;
203         res->end = end;
204         res->flags = flags;
205
206         if (insert_resource(root, res)) {
207                 kfree(res);
208                 return -EBUSY;
209         }
210
211         return 0;
212 }
213
214 static u64 __devinit
215 add_io_space (struct acpi_resource_address64 *addr)
216 {
217         u64 offset;
218         int sparse = 0;
219         int i;
220
221         if (addr->address_translation_offset == 0)
222                 return IO_SPACE_BASE(0);        /* part of legacy IO space */
223
224         if (addr->attribute.io.translation_attribute == ACPI_SPARSE_TRANSLATION)
225                 sparse = 1;
226
227         offset = (u64) ioremap(addr->address_translation_offset, 0);
228         for (i = 0; i < num_io_spaces; i++)
229                 if (io_space[i].mmio_base == offset &&
230                     io_space[i].sparse == sparse)
231                         return IO_SPACE_BASE(i);
232
233         if (num_io_spaces == MAX_IO_SPACES) {
234                 printk("Too many IO port spaces\n");
235                 return ~0;
236         }
237
238         i = num_io_spaces++;
239         io_space[i].mmio_base = offset;
240         io_space[i].sparse = sparse;
241
242         return IO_SPACE_BASE(i);
243 }
244
245 static acpi_status __devinit
246 count_window (struct acpi_resource *resource, void *data)
247 {
248         unsigned int *windows = (unsigned int *) data;
249         struct acpi_resource_address64 addr;
250         acpi_status status;
251
252         status = acpi_resource_to_address64(resource, &addr);
253         if (ACPI_SUCCESS(status))
254                 if (addr.resource_type == ACPI_MEMORY_RANGE ||
255                     addr.resource_type == ACPI_IO_RANGE)
256                         (*windows)++;
257
258         return AE_OK;
259 }
260
261 struct pci_root_info {
262         struct pci_controller *controller;
263         char *name;
264 };
265
266 static acpi_status __devinit
267 add_window (struct acpi_resource *res, void *data)
268 {
269         struct pci_root_info *info = (struct pci_root_info *) data;
270         struct pci_window *window;
271         struct acpi_resource_address64 addr;
272         acpi_status status;
273         unsigned long flags, offset = 0;
274         struct resource *root;
275
276         status = acpi_resource_to_address64(res, &addr);
277         if (ACPI_SUCCESS(status)) {
278                 if (!addr.address_length)
279                         return AE_OK;
280
281                 if (addr.resource_type == ACPI_MEMORY_RANGE) {
282                         flags = IORESOURCE_MEM;
283                         root = &iomem_resource;
284                         offset = addr.address_translation_offset;
285                 } else if (addr.resource_type == ACPI_IO_RANGE) {
286                         flags = IORESOURCE_IO;
287                         root = &ioport_resource;
288                         offset = add_io_space(&addr);
289                         if (offset == ~0)
290                                 return AE_OK;
291                 } else
292                         return AE_OK;
293
294                 window = &info->controller->window[info->controller->windows++];
295                 window->resource.flags  = flags;
296                 window->resource.start  = addr.min_address_range;
297                 window->resource.end    = addr.max_address_range;
298                 window->offset          = offset;
299
300                 if (alloc_resource(info->name, root, addr.min_address_range + offset,
301                         addr.max_address_range + offset, flags))
302                         printk(KERN_ERR "alloc 0x%lx-0x%lx from %s for %s failed\n",
303                                 addr.min_address_range + offset, addr.max_address_range + offset,
304                                 root->name, info->name);
305         }
306
307         return AE_OK;
308 }
309
310 struct pci_bus * __devinit
311 pci_acpi_scan_root (struct acpi_device *device, int domain, int bus)
312 {
313         struct pci_root_info info;
314         struct pci_controller *controller;
315         unsigned int windows = 0;
316         char *name;
317
318         controller = alloc_pci_controller(domain);
319         if (!controller)
320                 goto out1;
321
322         controller->acpi_handle = device->handle;
323
324         acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_window, &windows);
325         controller->window = kmalloc(sizeof(*controller->window) * windows, GFP_KERNEL);
326         if (!controller->window)
327                 goto out2;
328
329         name = kmalloc(16, GFP_KERNEL);
330         if (!name)
331                 goto out3;
332
333         sprintf(name, "PCI Bus %04x:%02x", domain, bus);
334         info.controller = controller;
335         info.name = name;
336         acpi_walk_resources(device->handle, METHOD_NAME__CRS, add_window, &info);
337
338         return pci_scan_bus(bus, &pci_root_ops, controller);
339
340 out3:
341         kfree(controller->window);
342 out2:
343         kfree(controller);
344 out1:
345         return NULL;
346 }
347
348 void pcibios_resource_to_bus(struct pci_dev *dev,
349                 struct pci_bus_region *region, struct resource *res)
350 {
351         struct pci_controller *controller = PCI_CONTROLLER(dev);
352         unsigned long offset = 0;
353         int i;
354
355         for (i = 0; i < controller->windows; i++) {
356                 struct pci_window *window = &controller->window[i];
357                 if (!(window->resource.flags & res->flags))
358                         continue;
359                 if (window->resource.start > res->start - window->offset)
360                         continue;
361                 if (window->resource.end < res->end - window->offset)
362                         continue;
363                 offset = window->offset;
364                 break;
365         }
366
367         region->start = res->start - offset;
368         region->end = res->end - offset;
369 }
370
371 void pcibios_bus_to_resource(struct pci_dev *dev,
372                 struct resource *res, struct pci_bus_region *region)
373 {
374         struct pci_controller *controller = PCI_CONTROLLER(dev);
375         unsigned long offset = 0;
376         int i;
377
378         for (i = 0; i < controller->windows; i++) {
379                 struct pci_window *window = &controller->window[i];
380                 if (!(window->resource.flags & res->flags))
381                         continue;
382                 if (window->resource.start > region->start)
383                         continue;
384                 if (window->resource.end < region->end)
385                         continue;
386                 offset = window->offset;
387                 break;
388         }
389
390         res->start = region->start + offset;
391         res->end = region->end + offset;
392 }
393
394 static void __devinit pcibios_fixup_device_resources(struct pci_dev *dev)
395 {
396         struct pci_bus_region region;
397         int i;
398         int limit = (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) ? \
399                 PCI_BRIDGE_RESOURCES : PCI_NUM_RESOURCES;
400
401         for (i = 0; i < limit; i++) {
402                 if (!dev->resource[i].flags)
403                         continue;
404                 region.start = dev->resource[i].start;
405                 region.end = dev->resource[i].end;
406                 pcibios_bus_to_resource(dev, &dev->resource[i], &region);
407                 pci_claim_resource(dev, i);
408         }
409 }
410
411 /*
412  *  Called after each bus is probed, but before its children are examined.
413  */
414 void __devinit
415 pcibios_fixup_bus (struct pci_bus *b)
416 {
417         struct pci_dev *dev;
418
419         list_for_each_entry(dev, &b->devices, bus_list)
420                 pcibios_fixup_device_resources(dev);
421
422         return;
423 }
424
425 void __devinit
426 pcibios_update_irq (struct pci_dev *dev, int irq)
427 {
428         pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
429
430         /* ??? FIXME -- record old value for shutdown.  */
431 }
432
433 static inline int
434 pcibios_enable_resources (struct pci_dev *dev, int mask)
435 {
436         u16 cmd, old_cmd;
437         int idx;
438         struct resource *r;
439
440         if (!dev)
441                 return -EINVAL;
442
443         pci_read_config_word(dev, PCI_COMMAND, &cmd);
444         old_cmd = cmd;
445         for (idx=0; idx<6; idx++) {
446                 /* Only set up the desired resources.  */
447                 if (!(mask & (1 << idx)))
448                         continue;
449
450                 r = &dev->resource[idx];
451                 if (!r->start && r->end) {
452                         printk(KERN_ERR
453                                "PCI: Device %s not available because of resource collisions\n",
454                                pci_name(dev));
455                         return -EINVAL;
456                 }
457                 if (r->flags & IORESOURCE_IO)
458                         cmd |= PCI_COMMAND_IO;
459                 if (r->flags & IORESOURCE_MEM)
460                         cmd |= PCI_COMMAND_MEMORY;
461         }
462         if (dev->resource[PCI_ROM_RESOURCE].start)
463                 cmd |= PCI_COMMAND_MEMORY;
464         if (cmd != old_cmd) {
465                 printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
466                 pci_write_config_word(dev, PCI_COMMAND, cmd);
467         }
468         return 0;
469 }
470
471 int
472 pcibios_enable_device (struct pci_dev *dev, int mask)
473 {
474         int ret;
475
476         ret = pcibios_enable_resources(dev, mask);
477         if (ret < 0)
478                 return ret;
479
480         return acpi_pci_irq_enable(dev);
481 }
482
483 void
484 pcibios_align_resource (void *data, struct resource *res,
485                         unsigned long size, unsigned long align)
486 {
487 }
488
489 /*
490  * PCI BIOS setup, always defaults to SAL interface
491  */
492 char * __init
493 pcibios_setup (char *str)
494 {
495         if (!strcmp(str, "routeirq"))
496                 pci_routeirq = 1;
497         return NULL;
498 }
499
500 int
501 pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
502                      enum pci_mmap_state mmap_state, int write_combine)
503 {
504         /*
505          * I/O space cannot be accessed via normal processor loads and
506          * stores on this platform.
507          */
508         if (mmap_state == pci_mmap_io)
509                 /*
510                  * XXX we could relax this for I/O spaces for which ACPI
511                  * indicates that the space is 1-to-1 mapped.  But at the
512                  * moment, we don't support multiple PCI address spaces and
513                  * the legacy I/O space is not 1-to-1 mapped, so this is moot.
514                  */
515                 return -EINVAL;
516
517         /*
518          * Leave vm_pgoff as-is, the PCI space address is the physical
519          * address on this platform.
520          */
521         vma->vm_flags |= (VM_SHM | VM_LOCKED | VM_IO);
522
523         if (write_combine)
524                 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
525         else
526                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
527
528         if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
529                              vma->vm_end - vma->vm_start, vma->vm_page_prot))
530                 return -EAGAIN;
531
532         return 0;
533 }
534
535 /**
536  * pci_cacheline_size - determine cacheline size for PCI devices
537  * @dev: void
538  *
539  * We want to use the line-size of the outer-most cache.  We assume
540  * that this line-size is the same for all CPUs.
541  *
542  * Code mostly taken from arch/ia64/kernel/palinfo.c:cache_info().
543  *
544  * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
545  */
546 static unsigned long
547 pci_cacheline_size (void)
548 {
549         u64 levels, unique_caches;
550         s64 status;
551         pal_cache_config_info_t cci;
552         static u8 cacheline_size;
553
554         if (cacheline_size)
555                 return cacheline_size;
556
557         status = ia64_pal_cache_summary(&levels, &unique_caches);
558         if (status != 0) {
559                 printk(KERN_ERR "%s: ia64_pal_cache_summary() failed (status=%ld)\n",
560                        __FUNCTION__, status);
561                 return SMP_CACHE_BYTES;
562         }
563
564         status = ia64_pal_cache_config_info(levels - 1, /* cache_type (data_or_unified)= */ 2,
565                                             &cci);
566         if (status != 0) {
567                 printk(KERN_ERR "%s: ia64_pal_cache_config_info() failed (status=%ld)\n",
568                        __FUNCTION__, status);
569                 return SMP_CACHE_BYTES;
570         }
571         cacheline_size = 1 << cci.pcci_line_size;
572         return cacheline_size;
573 }
574
575 /**
576  * pcibios_prep_mwi - helper function for drivers/pci/pci.c:pci_set_mwi()
577  * @dev: the PCI device for which MWI is enabled
578  *
579  * For ia64, we can get the cacheline sizes from PAL.
580  *
581  * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
582  */
583 int
584 pcibios_prep_mwi (struct pci_dev *dev)
585 {
586         unsigned long desired_linesize, current_linesize;
587         int rc = 0;
588         u8 pci_linesize;
589
590         desired_linesize = pci_cacheline_size();
591
592         pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &pci_linesize);
593         current_linesize = 4 * pci_linesize;
594         if (desired_linesize != current_linesize) {
595                 printk(KERN_WARNING "PCI: slot %s has incorrect PCI cache line size of %lu bytes,",
596                        pci_name(dev), current_linesize);
597                 if (current_linesize > desired_linesize) {
598                         printk(" expected %lu bytes instead\n", desired_linesize);
599                         rc = -EINVAL;
600                 } else {
601                         printk(" correcting to %lu\n", desired_linesize);
602                         pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, desired_linesize / 4);
603                 }
604         }
605         return rc;
606 }
607
608 int pci_vector_resources(int last, int nr_released)
609 {
610         int count = nr_released;
611
612         count += (IA64_LAST_DEVICE_VECTOR - last);
613
614         return count;
615 }