- Separate out show_stack changes into own patch.
[linux-flexiantxendom0-3.2.10.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
38
39 #undef DEBUG
40 #define DEBUG
41
42 #ifdef DEBUG
43 #define DBG(x...) printk(x)
44 #else
45 #define DBG(x...)
46 #endif
47
48 struct pci_fixup pcibios_fixups[1];
49
50 /*
51  * Low-level SAL-based PCI configuration access functions. Note that SAL
52  * calls are already serialized (via sal_lock), so we don't need another
53  * synchronization mechanism here.
54  */
55
56 #define PCI_SAL_ADDRESS(seg, bus, dev, fn, reg) \
57         ((u64)(seg << 24) | (u64)(bus << 16) | \
58          (u64)(dev << 11) | (u64)(fn << 8) | (u64)(reg))
59
60
61 static int
62 __pci_sal_read (int seg, int bus, int dev, int fn, int reg, int len, u32 *value)
63 {
64         int result = 0;
65         u64 data = 0;
66
67         if (!value || (seg > 255) || (bus > 255) || (dev > 31) || (fn > 7) || (reg > 255))
68                 return -EINVAL;
69
70         result = ia64_sal_pci_config_read(PCI_SAL_ADDRESS(seg, bus, dev, fn, reg), len, &data);
71
72         *value = (u32) data;
73
74         return result;
75 }
76
77 static int
78 __pci_sal_write (int seg, int bus, int dev, int fn, int reg, int len, u32 value)
79 {
80         if ((seg > 255) || (bus > 255) || (dev > 31) || (fn > 7) || (reg > 255))
81                 return -EINVAL;
82
83         return ia64_sal_pci_config_write(PCI_SAL_ADDRESS(seg, bus, dev, fn, reg), len, value);
84 }
85
86
87 static int
88 pci_sal_read (struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
89 {
90         return __pci_sal_read(PCI_SEGMENT(bus), bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn),
91                               where, size, value);
92 }
93
94 static int
95 pci_sal_write (struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
96 {
97         return __pci_sal_write(PCI_SEGMENT(bus), bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn),
98                                where, size, value);
99 }
100
101 struct pci_ops pci_sal_ops = {
102         .read =         pci_sal_read,
103         .write =        pci_sal_write
104 };
105
106 struct pci_ops *pci_root_ops = &pci_sal_ops;    /* default to SAL */
107
108 static int __init
109 pci_acpi_init (void)
110 {
111         if (!acpi_pci_irq_init())
112                 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
113         else
114                 printk(KERN_WARNING "PCI: Invalid ACPI-PCI IRQ routing table\n");
115         return 0;
116 }
117
118 subsys_initcall(pci_acpi_init);
119
120 /* Called by ACPI when it finds a new root bus.  */
121
122 static struct pci_controller *
123 alloc_pci_controller (int seg)
124 {
125         struct pci_controller *controller;
126
127         controller = kmalloc(sizeof(*controller), GFP_KERNEL);
128         if (!controller)
129                 return NULL;
130
131         memset(controller, 0, sizeof(*controller));
132         controller->segment = seg;
133         return controller;
134 }
135
136 static struct pci_bus *
137 scan_root_bus (int bus, struct pci_ops *ops, void *sysdata)
138 {
139         struct pci_bus *b;
140
141         /*
142          * We know this is a new root bus we haven't seen before, so
143          * scan it, even if we've seen the same bus number in a different
144          * segment.
145          */
146         b = kmalloc(sizeof(*b), GFP_KERNEL);
147         if (!b)
148                 return NULL;
149
150         memset(b, 0, sizeof(*b));
151         INIT_LIST_HEAD(&b->children);
152         INIT_LIST_HEAD(&b->devices);
153
154         list_add_tail(&b->node, &pci_root_buses);
155
156         b->number = b->secondary = bus;
157         b->resource[0] = &ioport_resource;
158         b->resource[1] = &iomem_resource;
159
160         b->sysdata = sysdata;
161         b->ops = ops;
162         b->subordinate = pci_do_scan_bus(b);
163
164         return b;
165 }
166
167 static int
168 alloc_resource (char *name, struct resource *root, unsigned long start, unsigned long end, unsigned long flags)
169 {
170         struct resource *res;
171
172         res = kmalloc(sizeof(*res), GFP_KERNEL);
173         if (!res)
174                 return -ENOMEM;
175
176         memset(res, 0, sizeof(*res));
177         res->name = name;
178         res->start = start;
179         res->end = end;
180         res->flags = flags;
181
182         if (request_resource(root, res))
183                 return -EBUSY;
184
185         return 0;
186 }
187
188 static u64
189 add_io_space (struct acpi_resource_address64 *addr)
190 {
191         u64 offset;
192         int sparse = 0;
193         int i;
194
195         if (addr->address_translation_offset == 0)
196                 return IO_SPACE_BASE(0);        /* part of legacy IO space */
197
198         if (addr->attribute.io.translation_attribute == ACPI_SPARSE_TRANSLATION)
199                 sparse = 1;
200
201         offset = (u64) ioremap(addr->address_translation_offset, 0);
202         for (i = 0; i < num_io_spaces; i++)
203                 if (io_space[i].mmio_base == offset &&
204                     io_space[i].sparse == sparse)
205                         return IO_SPACE_BASE(i);
206
207         if (num_io_spaces == MAX_IO_SPACES) {
208                 printk("Too many IO port spaces\n");
209                 return ~0;
210         }
211
212         i = num_io_spaces++;
213         io_space[i].mmio_base = offset;
214         io_space[i].sparse = sparse;
215
216         return IO_SPACE_BASE(i);
217 }
218
219 static acpi_status
220 count_window (struct acpi_resource *resource, void *data)
221 {
222         unsigned int *windows = (unsigned int *) data;
223         struct acpi_resource_address64 addr;
224         acpi_status status;
225
226         status = acpi_resource_to_address64(resource, &addr);
227         if (ACPI_SUCCESS(status))
228                 if (addr.resource_type == ACPI_MEMORY_RANGE ||
229                     addr.resource_type == ACPI_IO_RANGE)
230                         (*windows)++;
231
232         return AE_OK;
233 }
234
235 struct pci_root_info {
236         struct pci_controller *controller;
237         char *name;
238 };
239
240 static acpi_status
241 add_window (struct acpi_resource *res, void *data)
242 {
243         struct pci_root_info *info = (struct pci_root_info *) data;
244         struct pci_window *window;
245         struct acpi_resource_address64 addr;
246         acpi_status status;
247         unsigned long flags, offset = 0;
248         struct resource *root;
249
250         status = acpi_resource_to_address64(res, &addr);
251         if (ACPI_SUCCESS(status)) {
252                 if (addr.resource_type == ACPI_MEMORY_RANGE) {
253                         flags = IORESOURCE_MEM;
254                         root = &iomem_resource;
255                         offset = addr.address_translation_offset;
256                 } else if (addr.resource_type == ACPI_IO_RANGE) {
257                         flags = IORESOURCE_IO;
258                         root = &ioport_resource;
259                         offset = add_io_space(&addr);
260                         if (offset == ~0)
261                                 return AE_OK;
262                 } else
263                         return AE_OK;
264
265                 window = &info->controller->window[info->controller->windows++];
266                 window->resource.flags |= flags;
267                 window->resource.start  = addr.min_address_range;
268                 window->resource.end    = addr.max_address_range;
269                 window->offset          = offset;
270
271                 if (alloc_resource(info->name, root, addr.min_address_range + offset,
272                         addr.max_address_range + offset, flags))
273                         printk(KERN_ERR "alloc 0x%lx-0x%lx from %s for %s failed\n",
274                                 addr.min_address_range + offset, addr.max_address_range + offset,
275                                 root->name, info->name);
276         }
277
278         return AE_OK;
279 }
280
281 struct pci_bus *
282 pcibios_scan_root (void *handle, int seg, int bus)
283 {
284         struct pci_root_info info;
285         struct pci_controller *controller;
286         unsigned int windows = 0;
287         char *name;
288
289         printk("PCI: Probing PCI hardware on bus (%02x:%02x)\n", seg, bus);
290         controller = alloc_pci_controller(seg);
291         if (!controller)
292                 goto out1;
293
294         controller->acpi_handle = handle;
295
296         acpi_walk_resources(handle, METHOD_NAME__CRS, count_window, &windows);
297         controller->window = kmalloc(sizeof(*controller->window) * windows, GFP_KERNEL);
298         if (!controller->window)
299                 goto out2;
300
301         name = kmalloc(16, GFP_KERNEL);
302         if (!name)
303                 goto out3;
304
305         sprintf(name, "PCI Bus %02x:%02x", seg, bus);
306         info.controller = controller;
307         info.name = name;
308         acpi_walk_resources(handle, METHOD_NAME__CRS, add_window, &info);
309
310         return scan_root_bus(bus, pci_root_ops, controller);
311
312 out3:
313         kfree(controller->window);
314 out2:
315         kfree(controller);
316 out1:
317         return NULL;
318 }
319
320 void __init
321 pcibios_fixup_device_resources (struct pci_dev *dev, struct pci_bus *bus)
322 {
323         struct pci_controller *controller = PCI_CONTROLLER(dev);
324         struct pci_window *window;
325         int i, j;
326
327         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
328                 if (!dev->resource[i].start)
329                         continue;
330
331 #define contains(win, res)      ((res)->start >= (win)->start && \
332                                  (res)->end   <= (win)->end)
333
334                 for (j = 0; j < controller->windows; j++) {
335                         window = &controller->window[j];
336                         if (((dev->resource[i].flags & IORESOURCE_MEM &&
337                               window->resource.flags & IORESOURCE_MEM) ||
338                              (dev->resource[i].flags & IORESOURCE_IO &&
339                               window->resource.flags & IORESOURCE_IO)) &&
340                             contains(&window->resource, &dev->resource[i])) {
341                                 dev->resource[i].start += window->offset;
342                                 dev->resource[i].end   += window->offset;
343                         }
344                 }
345         }
346 }
347
348 /*
349  *  Called after each bus is probed, but before its children are examined.
350  */
351 void __devinit
352 pcibios_fixup_bus (struct pci_bus *b)
353 {
354         struct list_head *ln;
355
356         for (ln = b->devices.next; ln != &b->devices; ln = ln->next)
357                 pcibios_fixup_device_resources(pci_dev_b(ln), b);
358
359         return;
360 }
361
362 #warning pcibios_update_resource() is now a generic implementation - please check
363
364 void __devinit
365 pcibios_update_irq (struct pci_dev *dev, int irq)
366 {
367         pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
368
369         /* ??? FIXME -- record old value for shutdown.  */
370 }
371
372 static inline int
373 pcibios_enable_resources (struct pci_dev *dev, int mask)
374 {
375         u16 cmd, old_cmd;
376         int idx;
377         struct resource *r;
378
379         if (!dev)
380                 return -EINVAL;
381
382         pci_read_config_word(dev, PCI_COMMAND, &cmd);
383         old_cmd = cmd;
384         for (idx=0; idx<6; idx++) {
385                 /* Only set up the desired resources.  */
386                 if (!(mask & (1 << idx)))
387                         continue;
388
389                 r = &dev->resource[idx];
390                 if (!r->start && r->end) {
391                         printk(KERN_ERR
392                                "PCI: Device %s not available because of resource collisions\n",
393                                dev->slot_name);
394                         return -EINVAL;
395                 }
396                 if (r->flags & IORESOURCE_IO)
397                         cmd |= PCI_COMMAND_IO;
398                 if (r->flags & IORESOURCE_MEM)
399                         cmd |= PCI_COMMAND_MEMORY;
400         }
401         if (dev->resource[PCI_ROM_RESOURCE].start)
402                 cmd |= PCI_COMMAND_MEMORY;
403         if (cmd != old_cmd) {
404                 printk("PCI: Enabling device %s (%04x -> %04x)\n", dev->slot_name, old_cmd, cmd);
405                 pci_write_config_word(dev, PCI_COMMAND, cmd);
406         }
407         return 0;
408 }
409
410 int
411 pcibios_enable_device (struct pci_dev *dev, int mask)
412 {
413         int ret;
414
415         ret = pcibios_enable_resources(dev, mask);
416         if (ret < 0)
417                 return ret;
418
419         printk(KERN_INFO "PCI: Found IRQ %d for device %s\n", dev->irq, dev->slot_name);
420         return acpi_pci_irq_enable(dev);
421 }
422
423 void
424 pcibios_align_resource (void *data, struct resource *res,
425                         unsigned long size, unsigned long align)
426 {
427 }
428
429 /*
430  * PCI BIOS setup, always defaults to SAL interface
431  */
432 char * __init
433 pcibios_setup (char *str)
434 {
435         return NULL;
436 }
437
438 int
439 pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
440                      enum pci_mmap_state mmap_state, int write_combine)
441 {
442         /*
443          * I/O space cannot be accessed via normal processor loads and stores on this
444          * platform.
445          */
446         if (mmap_state == pci_mmap_io)
447                 /*
448                  * XXX we could relax this for I/O spaces for which ACPI indicates that
449                  * the space is 1-to-1 mapped.  But at the moment, we don't support
450                  * multiple PCI address spaces and the legacy I/O space is not 1-to-1
451                  * mapped, so this is moot.
452                  */
453                 return -EINVAL;
454
455         /*
456          * Leave vm_pgoff as-is, the PCI space address is the physical address on this
457          * platform.
458          */
459         vma->vm_flags |= (VM_SHM | VM_LOCKED | VM_IO);
460
461         if (write_combine)
462                 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
463         else
464                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
465
466         if (remap_page_range(vma, vma->vm_start, vma->vm_pgoff << PAGE_SHIFT,
467                              vma->vm_end - vma->vm_start, vma->vm_page_prot))
468                 return -EAGAIN;
469
470         return 0;
471 }
472
473 /**
474  * pci_cacheline_size - determine cacheline size for PCI devices
475  * @dev: void
476  *
477  * We want to use the line-size of the outer-most cache.  We assume
478  * that this line-size is the same for all CPUs.
479  *
480  * Code mostly taken from arch/ia64/kernel/palinfo.c:cache_info().
481  *
482  * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
483  */
484 static unsigned long
485 pci_cacheline_size (void)
486 {
487         u64 levels, unique_caches;
488         s64 status;
489         pal_cache_config_info_t cci;
490         static u8 cacheline_size;
491
492         if (cacheline_size)
493                 return cacheline_size;
494
495         status = ia64_pal_cache_summary(&levels, &unique_caches);
496         if (status != 0) {
497                 printk(KERN_ERR "%s: ia64_pal_cache_summary() failed (status=%ld)\n",
498                        __FUNCTION__, status);
499                 return SMP_CACHE_BYTES;
500         }
501
502         status = ia64_pal_cache_config_info(levels - 1, /* cache_type (data_or_unified)= */ 2,
503                                             &cci);
504         if (status != 0) {
505                 printk(KERN_ERR "%s: ia64_pal_cache_config_info() failed (status=%ld)\n",
506                        __FUNCTION__, status);
507                 return SMP_CACHE_BYTES;
508         }
509         cacheline_size = 1 << cci.pcci_line_size;
510         return cacheline_size;
511 }
512
513 /**
514  * pcibios_prep_mwi - helper function for drivers/pci/pci.c:pci_set_mwi()
515  * @dev: the PCI device for which MWI is enabled
516  *
517  * For ia64, we can get the cacheline sizes from PAL.
518  *
519  * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
520  */
521 int
522 pcibios_prep_mwi (struct pci_dev *dev)
523 {
524         unsigned long desired_linesize, current_linesize;
525         int rc = 0;
526         u8 pci_linesize;
527
528         desired_linesize = pci_cacheline_size();
529
530         pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &pci_linesize);
531         current_linesize = 4 * pci_linesize;
532         if (desired_linesize != current_linesize) {
533                 printk(KERN_WARNING "PCI: slot %s has incorrect PCI cache line size of %lu bytes,",
534                        dev->slot_name, current_linesize);
535                 if (current_linesize > desired_linesize) {
536                         printk(" expected %lu bytes instead\n", desired_linesize);
537                         rc = -EINVAL;
538                 } else {
539                         printk(" correcting to %lu\n", desired_linesize);
540                         pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, desired_linesize / 4);
541                 }
542         }
543         return rc;
544 }