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