patch-2_6_7-vs1_9_1_12
[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 struct pci_fixup pcibios_fixups[1];
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 static 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         if (!acpi_pci_irq_init())
138                 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
139         else
140                 printk(KERN_WARNING "PCI: Invalid ACPI-PCI IRQ routing table\n");
141         return 0;
142 }
143
144 subsys_initcall(pci_acpi_init);
145
146 /* Called by ACPI when it finds a new root bus.  */
147
148 static struct pci_controller * __devinit
149 alloc_pci_controller (int seg)
150 {
151         struct pci_controller *controller;
152
153         controller = kmalloc(sizeof(*controller), GFP_KERNEL);
154         if (!controller)
155                 return NULL;
156
157         memset(controller, 0, sizeof(*controller));
158         controller->segment = seg;
159         return controller;
160 }
161
162 static int __devinit
163 alloc_resource (char *name, struct resource *root, unsigned long start, unsigned long end,
164                 unsigned long flags)
165 {
166         struct resource *res;
167
168         res = kmalloc(sizeof(*res), GFP_KERNEL);
169         if (!res)
170                 return -ENOMEM;
171
172         memset(res, 0, sizeof(*res));
173         res->name = name;
174         res->start = start;
175         res->end = end;
176         res->flags = flags;
177
178         if (insert_resource(root, res)) {
179                 kfree(res);
180                 return -EBUSY;
181         }
182
183         return 0;
184 }
185
186 static u64 __devinit
187 add_io_space (struct acpi_resource_address64 *addr)
188 {
189         u64 offset;
190         int sparse = 0;
191         int i;
192
193         if (addr->address_translation_offset == 0)
194                 return IO_SPACE_BASE(0);        /* part of legacy IO space */
195
196         if (addr->attribute.io.translation_attribute == ACPI_SPARSE_TRANSLATION)
197                 sparse = 1;
198
199         offset = (u64) ioremap(addr->address_translation_offset, 0);
200         for (i = 0; i < num_io_spaces; i++)
201                 if (io_space[i].mmio_base == offset &&
202                     io_space[i].sparse == sparse)
203                         return IO_SPACE_BASE(i);
204
205         if (num_io_spaces == MAX_IO_SPACES) {
206                 printk("Too many IO port spaces\n");
207                 return ~0;
208         }
209
210         i = num_io_spaces++;
211         io_space[i].mmio_base = offset;
212         io_space[i].sparse = sparse;
213
214         return IO_SPACE_BASE(i);
215 }
216
217 static acpi_status __devinit
218 count_window (struct acpi_resource *resource, void *data)
219 {
220         unsigned int *windows = (unsigned int *) data;
221         struct acpi_resource_address64 addr;
222         acpi_status status;
223
224         status = acpi_resource_to_address64(resource, &addr);
225         if (ACPI_SUCCESS(status))
226                 if (addr.resource_type == ACPI_MEMORY_RANGE ||
227                     addr.resource_type == ACPI_IO_RANGE)
228                         (*windows)++;
229
230         return AE_OK;
231 }
232
233 struct pci_root_info {
234         struct pci_controller *controller;
235         char *name;
236 };
237
238 static acpi_status __devinit
239 add_window (struct acpi_resource *res, void *data)
240 {
241         struct pci_root_info *info = (struct pci_root_info *) data;
242         struct pci_window *window;
243         struct acpi_resource_address64 addr;
244         acpi_status status;
245         unsigned long flags, offset = 0;
246         struct resource *root;
247
248         status = acpi_resource_to_address64(res, &addr);
249         if (ACPI_SUCCESS(status)) {
250                 if (!addr.address_length)
251                         return AE_OK;
252
253                 if (addr.resource_type == ACPI_MEMORY_RANGE) {
254                         flags = IORESOURCE_MEM;
255                         root = &iomem_resource;
256                         offset = addr.address_translation_offset;
257                 } else if (addr.resource_type == ACPI_IO_RANGE) {
258                         flags = IORESOURCE_IO;
259                         root = &ioport_resource;
260                         offset = add_io_space(&addr);
261                         if (offset == ~0)
262                                 return AE_OK;
263                 } else
264                         return AE_OK;
265
266                 window = &info->controller->window[info->controller->windows++];
267                 window->resource.flags |= flags;
268                 window->resource.start  = addr.min_address_range;
269                 window->resource.end    = addr.max_address_range;
270                 window->offset          = offset;
271
272                 if (alloc_resource(info->name, root, addr.min_address_range + offset,
273                         addr.max_address_range + offset, flags))
274                         printk(KERN_ERR "alloc 0x%lx-0x%lx from %s for %s failed\n",
275                                 addr.min_address_range + offset, addr.max_address_range + offset,
276                                 root->name, info->name);
277         }
278
279         return AE_OK;
280 }
281
282 struct pci_bus * __devinit
283 pci_acpi_scan_root (struct acpi_device *device, int domain, int bus)
284 {
285         struct pci_root_info info;
286         struct pci_controller *controller;
287         unsigned int windows = 0;
288         char *name;
289
290         controller = alloc_pci_controller(domain);
291         if (!controller)
292                 goto out1;
293
294         controller->acpi_handle = device->handle;
295
296         acpi_walk_resources(device->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 %04x:%02x", domain, bus);
306         info.controller = controller;
307         info.name = name;
308         acpi_walk_resources(device->handle, METHOD_NAME__CRS, add_window, &info);
309
310         return pci_scan_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         int limit = (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) ? \
327                 PCI_ROM_RESOURCE : PCI_NUM_RESOURCES;
328
329         for (i = 0; i < limit; i++) {
330                 if (!dev->resource[i].start)
331                         continue;
332
333 #define contains(win, res)      ((res)->start >= (win)->start && \
334                                  (res)->end   <= (win)->end)
335
336                 for (j = 0; j < controller->windows; j++) {
337                         window = &controller->window[j];
338                         if (((dev->resource[i].flags & IORESOURCE_MEM &&
339                               window->resource.flags & IORESOURCE_MEM) ||
340                              (dev->resource[i].flags & IORESOURCE_IO &&
341                               window->resource.flags & IORESOURCE_IO)) &&
342                             contains(&window->resource, &dev->resource[i])) {
343                                 dev->resource[i].start += window->offset;
344                                 dev->resource[i].end   += window->offset;
345                         }
346                 }
347                 pci_claim_resource(dev, i);
348         }
349 }
350
351 /*
352  *  Called after each bus is probed, but before its children are examined.
353  */
354 void __devinit
355 pcibios_fixup_bus (struct pci_bus *b)
356 {
357         struct list_head *ln;
358
359         for (ln = b->devices.next; ln != &b->devices; ln = ln->next)
360                 pcibios_fixup_device_resources(pci_dev_b(ln), b);
361
362         return;
363 }
364
365 void __devinit
366 pcibios_update_irq (struct pci_dev *dev, int irq)
367 {
368         pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
369
370         /* ??? FIXME -- record old value for shutdown.  */
371 }
372
373 static inline int
374 pcibios_enable_resources (struct pci_dev *dev, int mask)
375 {
376         u16 cmd, old_cmd;
377         int idx;
378         struct resource *r;
379
380         if (!dev)
381                 return -EINVAL;
382
383         pci_read_config_word(dev, PCI_COMMAND, &cmd);
384         old_cmd = cmd;
385         for (idx=0; idx<6; idx++) {
386                 /* Only set up the desired resources.  */
387                 if (!(mask & (1 << idx)))
388                         continue;
389
390                 r = &dev->resource[idx];
391                 if (!r->start && r->end) {
392                         printk(KERN_ERR
393                                "PCI: Device %s not available because of resource collisions\n",
394                                pci_name(dev));
395                         return -EINVAL;
396                 }
397                 if (r->flags & IORESOURCE_IO)
398                         cmd |= PCI_COMMAND_IO;
399                 if (r->flags & IORESOURCE_MEM)
400                         cmd |= PCI_COMMAND_MEMORY;
401         }
402         if (dev->resource[PCI_ROM_RESOURCE].start)
403                 cmd |= PCI_COMMAND_MEMORY;
404         if (cmd != old_cmd) {
405                 printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
406                 pci_write_config_word(dev, PCI_COMMAND, cmd);
407         }
408         return 0;
409 }
410
411 int
412 pcibios_enable_device (struct pci_dev *dev, int mask)
413 {
414         int ret;
415
416         ret = pcibios_enable_resources(dev, mask);
417         if (ret < 0)
418                 return ret;
419
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                        pci_name(dev), 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 }
545
546 int pci_vector_resources(int last, int nr_released)
547 {
548         int count = nr_released;
549
550         count += (IA64_LAST_DEVICE_VECTOR - last);
551
552         return count;
553 }