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