VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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         struct pci_dev *dev = NULL;
138
139         printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
140
141         /*
142          * PCI IRQ routing is set up by pci_enable_device(), but we
143          * also do it here in case there are still broken drivers that
144          * don't use pci_enable_device().
145          */
146         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL)
147                 acpi_pci_irq_enable(dev);
148
149         return 0;
150 }
151
152 subsys_initcall(pci_acpi_init);
153
154 /* Called by ACPI when it finds a new root bus.  */
155
156 static struct pci_controller * __devinit
157 alloc_pci_controller (int seg)
158 {
159         struct pci_controller *controller;
160
161         controller = kmalloc(sizeof(*controller), GFP_KERNEL);
162         if (!controller)
163                 return NULL;
164
165         memset(controller, 0, sizeof(*controller));
166         controller->segment = seg;
167         return controller;
168 }
169
170 static int __devinit
171 alloc_resource (char *name, struct resource *root, unsigned long start, unsigned long end,
172                 unsigned long flags)
173 {
174         struct resource *res;
175
176         res = kmalloc(sizeof(*res), GFP_KERNEL);
177         if (!res)
178                 return -ENOMEM;
179
180         memset(res, 0, sizeof(*res));
181         res->name = name;
182         res->start = start;
183         res->end = end;
184         res->flags = flags;
185
186         if (insert_resource(root, res)) {
187                 kfree(res);
188                 return -EBUSY;
189         }
190
191         return 0;
192 }
193
194 static u64 __devinit
195 add_io_space (struct acpi_resource_address64 *addr)
196 {
197         u64 offset;
198         int sparse = 0;
199         int i;
200
201         if (addr->address_translation_offset == 0)
202                 return IO_SPACE_BASE(0);        /* part of legacy IO space */
203
204         if (addr->attribute.io.translation_attribute == ACPI_SPARSE_TRANSLATION)
205                 sparse = 1;
206
207         offset = (u64) ioremap(addr->address_translation_offset, 0);
208         for (i = 0; i < num_io_spaces; i++)
209                 if (io_space[i].mmio_base == offset &&
210                     io_space[i].sparse == sparse)
211                         return IO_SPACE_BASE(i);
212
213         if (num_io_spaces == MAX_IO_SPACES) {
214                 printk("Too many IO port spaces\n");
215                 return ~0;
216         }
217
218         i = num_io_spaces++;
219         io_space[i].mmio_base = offset;
220         io_space[i].sparse = sparse;
221
222         return IO_SPACE_BASE(i);
223 }
224
225 static acpi_status __devinit
226 count_window (struct acpi_resource *resource, void *data)
227 {
228         unsigned int *windows = (unsigned int *) data;
229         struct acpi_resource_address64 addr;
230         acpi_status status;
231
232         status = acpi_resource_to_address64(resource, &addr);
233         if (ACPI_SUCCESS(status))
234                 if (addr.resource_type == ACPI_MEMORY_RANGE ||
235                     addr.resource_type == ACPI_IO_RANGE)
236                         (*windows)++;
237
238         return AE_OK;
239 }
240
241 struct pci_root_info {
242         struct pci_controller *controller;
243         char *name;
244 };
245
246 static acpi_status __devinit
247 add_window (struct acpi_resource *res, void *data)
248 {
249         struct pci_root_info *info = (struct pci_root_info *) data;
250         struct pci_window *window;
251         struct acpi_resource_address64 addr;
252         acpi_status status;
253         unsigned long flags, offset = 0;
254         struct resource *root;
255
256         status = acpi_resource_to_address64(res, &addr);
257         if (ACPI_SUCCESS(status)) {
258                 if (!addr.address_length)
259                         return AE_OK;
260
261                 if (addr.resource_type == ACPI_MEMORY_RANGE) {
262                         flags = IORESOURCE_MEM;
263                         root = &iomem_resource;
264                         offset = addr.address_translation_offset;
265                 } else if (addr.resource_type == ACPI_IO_RANGE) {
266                         flags = IORESOURCE_IO;
267                         root = &ioport_resource;
268                         offset = add_io_space(&addr);
269                         if (offset == ~0)
270                                 return AE_OK;
271                 } else
272                         return AE_OK;
273
274                 window = &info->controller->window[info->controller->windows++];
275                 window->resource.flags |= flags;
276                 window->resource.start  = addr.min_address_range;
277                 window->resource.end    = addr.max_address_range;
278                 window->offset          = offset;
279
280                 if (alloc_resource(info->name, root, addr.min_address_range + offset,
281                         addr.max_address_range + offset, flags))
282                         printk(KERN_ERR "alloc 0x%lx-0x%lx from %s for %s failed\n",
283                                 addr.min_address_range + offset, addr.max_address_range + offset,
284                                 root->name, info->name);
285         }
286
287         return AE_OK;
288 }
289
290 struct pci_bus * __devinit
291 pci_acpi_scan_root (struct acpi_device *device, int domain, int bus)
292 {
293         struct pci_root_info info;
294         struct pci_controller *controller;
295         unsigned int windows = 0;
296         char *name;
297
298         controller = alloc_pci_controller(domain);
299         if (!controller)
300                 goto out1;
301
302         controller->acpi_handle = device->handle;
303
304         acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_window, &windows);
305         controller->window = kmalloc(sizeof(*controller->window) * windows, GFP_KERNEL);
306         if (!controller->window)
307                 goto out2;
308
309         name = kmalloc(16, GFP_KERNEL);
310         if (!name)
311                 goto out3;
312
313         sprintf(name, "PCI Bus %04x:%02x", domain, bus);
314         info.controller = controller;
315         info.name = name;
316         acpi_walk_resources(device->handle, METHOD_NAME__CRS, add_window, &info);
317
318         return pci_scan_bus(bus, &pci_root_ops, controller);
319
320 out3:
321         kfree(controller->window);
322 out2:
323         kfree(controller);
324 out1:
325         return NULL;
326 }
327
328 void __init
329 pcibios_fixup_device_resources (struct pci_dev *dev, struct pci_bus *bus)
330 {
331         struct pci_controller *controller = PCI_CONTROLLER(dev);
332         struct pci_window *window;
333         int i, j;
334         int limit = (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) ? \
335                 PCI_ROM_RESOURCE : PCI_NUM_RESOURCES;
336
337         for (i = 0; i < limit; i++) {
338                 if (!dev->resource[i].start)
339                         continue;
340
341 #define contains(win, res)      ((res)->start >= (win)->start && \
342                                  (res)->end   <= (win)->end)
343
344                 for (j = 0; j < controller->windows; j++) {
345                         window = &controller->window[j];
346                         if (((dev->resource[i].flags & IORESOURCE_MEM &&
347                               window->resource.flags & IORESOURCE_MEM) ||
348                              (dev->resource[i].flags & IORESOURCE_IO &&
349                               window->resource.flags & IORESOURCE_IO)) &&
350                             contains(&window->resource, &dev->resource[i])) {
351                                 dev->resource[i].start += window->offset;
352                                 dev->resource[i].end   += window->offset;
353                         }
354                 }
355                 pci_claim_resource(dev, i);
356         }
357 }
358
359 /*
360  *  Called after each bus is probed, but before its children are examined.
361  */
362 void __devinit
363 pcibios_fixup_bus (struct pci_bus *b)
364 {
365         struct list_head *ln;
366
367         for (ln = b->devices.next; ln != &b->devices; ln = ln->next)
368                 pcibios_fixup_device_resources(pci_dev_b(ln), b);
369
370         return;
371 }
372
373 void __devinit
374 pcibios_update_irq (struct pci_dev *dev, int irq)
375 {
376         pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
377
378         /* ??? FIXME -- record old value for shutdown.  */
379 }
380
381 static inline int
382 pcibios_enable_resources (struct pci_dev *dev, int mask)
383 {
384         u16 cmd, old_cmd;
385         int idx;
386         struct resource *r;
387
388         if (!dev)
389                 return -EINVAL;
390
391         pci_read_config_word(dev, PCI_COMMAND, &cmd);
392         old_cmd = cmd;
393         for (idx=0; idx<6; idx++) {
394                 /* Only set up the desired resources.  */
395                 if (!(mask & (1 << idx)))
396                         continue;
397
398                 r = &dev->resource[idx];
399                 if (!r->start && r->end) {
400                         printk(KERN_ERR
401                                "PCI: Device %s not available because of resource collisions\n",
402                                pci_name(dev));
403                         return -EINVAL;
404                 }
405                 if (r->flags & IORESOURCE_IO)
406                         cmd |= PCI_COMMAND_IO;
407                 if (r->flags & IORESOURCE_MEM)
408                         cmd |= PCI_COMMAND_MEMORY;
409         }
410         if (dev->resource[PCI_ROM_RESOURCE].start)
411                 cmd |= PCI_COMMAND_MEMORY;
412         if (cmd != old_cmd) {
413                 printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
414                 pci_write_config_word(dev, PCI_COMMAND, cmd);
415         }
416         return 0;
417 }
418
419 int
420 pcibios_enable_device (struct pci_dev *dev, int mask)
421 {
422         int ret;
423
424         ret = pcibios_enable_resources(dev, mask);
425         if (ret < 0)
426                 return ret;
427
428         return acpi_pci_irq_enable(dev);
429 }
430
431 void
432 pcibios_align_resource (void *data, struct resource *res,
433                         unsigned long size, unsigned long align)
434 {
435 }
436
437 /*
438  * PCI BIOS setup, always defaults to SAL interface
439  */
440 char * __init
441 pcibios_setup (char *str)
442 {
443         return NULL;
444 }
445
446 int
447 pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
448                      enum pci_mmap_state mmap_state, int write_combine)
449 {
450         /*
451          * I/O space cannot be accessed via normal processor loads and stores on this
452          * platform.
453          */
454         if (mmap_state == pci_mmap_io)
455                 /*
456                  * XXX we could relax this for I/O spaces for which ACPI indicates that
457                  * the space is 1-to-1 mapped.  But at the moment, we don't support
458                  * multiple PCI address spaces and the legacy I/O space is not 1-to-1
459                  * mapped, so this is moot.
460                  */
461                 return -EINVAL;
462
463         /*
464          * Leave vm_pgoff as-is, the PCI space address is the physical address on this
465          * platform.
466          */
467         vma->vm_flags |= (VM_SHM | VM_LOCKED | VM_IO);
468
469         if (write_combine)
470                 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
471         else
472                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
473
474         if (remap_page_range(vma, vma->vm_start, vma->vm_pgoff << PAGE_SHIFT,
475                              vma->vm_end - vma->vm_start, vma->vm_page_prot))
476                 return -EAGAIN;
477
478         return 0;
479 }
480
481 /**
482  * pci_cacheline_size - determine cacheline size for PCI devices
483  * @dev: void
484  *
485  * We want to use the line-size of the outer-most cache.  We assume
486  * that this line-size is the same for all CPUs.
487  *
488  * Code mostly taken from arch/ia64/kernel/palinfo.c:cache_info().
489  *
490  * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
491  */
492 static unsigned long
493 pci_cacheline_size (void)
494 {
495         u64 levels, unique_caches;
496         s64 status;
497         pal_cache_config_info_t cci;
498         static u8 cacheline_size;
499
500         if (cacheline_size)
501                 return cacheline_size;
502
503         status = ia64_pal_cache_summary(&levels, &unique_caches);
504         if (status != 0) {
505                 printk(KERN_ERR "%s: ia64_pal_cache_summary() failed (status=%ld)\n",
506                        __FUNCTION__, status);
507                 return SMP_CACHE_BYTES;
508         }
509
510         status = ia64_pal_cache_config_info(levels - 1, /* cache_type (data_or_unified)= */ 2,
511                                             &cci);
512         if (status != 0) {
513                 printk(KERN_ERR "%s: ia64_pal_cache_config_info() failed (status=%ld)\n",
514                        __FUNCTION__, status);
515                 return SMP_CACHE_BYTES;
516         }
517         cacheline_size = 1 << cci.pcci_line_size;
518         return cacheline_size;
519 }
520
521 /**
522  * pcibios_prep_mwi - helper function for drivers/pci/pci.c:pci_set_mwi()
523  * @dev: the PCI device for which MWI is enabled
524  *
525  * For ia64, we can get the cacheline sizes from PAL.
526  *
527  * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
528  */
529 int
530 pcibios_prep_mwi (struct pci_dev *dev)
531 {
532         unsigned long desired_linesize, current_linesize;
533         int rc = 0;
534         u8 pci_linesize;
535
536         desired_linesize = pci_cacheline_size();
537
538         pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &pci_linesize);
539         current_linesize = 4 * pci_linesize;
540         if (desired_linesize != current_linesize) {
541                 printk(KERN_WARNING "PCI: slot %s has incorrect PCI cache line size of %lu bytes,",
542                        pci_name(dev), current_linesize);
543                 if (current_linesize > desired_linesize) {
544                         printk(" expected %lu bytes instead\n", desired_linesize);
545                         rc = -EINVAL;
546                 } else {
547                         printk(" correcting to %lu\n", desired_linesize);
548                         pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, desired_linesize / 4);
549                 }
550         }
551         return rc;
552 }
553
554 int pci_vector_resources(int last, int nr_released)
555 {
556         int count = nr_released;
557
558         count += (IA64_LAST_DEVICE_VECTOR - last);
559
560         return count;
561 }