2 * pci.c - Low-Level PCI Access in IA-64
4 * Derived from bios32.c of i386 tree.
6 * Copyright (C) 2002 Hewlett-Packard Co
7 * David Mosberger-Tang <davidm@hpl.hp.com>
8 * Bjorn Helgaas <bjorn_helgaas@hp.com>
10 * Note: Above list of copyright holders is incomplete...
12 #include <linux/config.h>
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>
24 #include <asm/machvec.h>
26 #include <asm/segment.h>
27 #include <asm/system.h>
37 #include <asm/hw_irq.h>
44 #define DBG(x...) printk(x)
49 struct pci_fixup pcibios_fixups[1];
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.
57 #define PCI_SAL_ADDRESS(seg, bus, devfn, reg) \
58 ((u64)(seg << 24) | (u64)(bus << 16) | \
59 (u64)(devfn << 8) | (u64)(reg))
61 /* SAL 3.2 adds support for extended config space. */
63 #define PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg) \
64 ((u64)(seg << 28) | (u64)(bus << 20) | \
65 (u64)(devfn << 12) | (u64)(reg))
68 pci_sal_read (int seg, int bus, int devfn, int reg, int len, u32 *value)
70 u64 addr, mode, data = 0;
73 if ((seg > 255) || (bus > 255) || (devfn > 255) || (reg > 4095))
76 if ((seg | reg) <= 255) {
77 addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
80 addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
83 result = ia64_sal_pci_config_read(addr, mode, len, &data);
91 pci_sal_write (int seg, int bus, int devfn, int reg, int len, u32 value)
95 if ((seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
98 if ((seg | reg) <= 255) {
99 addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
102 addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
105 return ia64_sal_pci_config_write(addr, mode, len, value);
108 static struct pci_raw_ops pci_sal_ops = {
109 .read = pci_sal_read,
110 .write = pci_sal_write
113 struct pci_raw_ops *raw_pci_ops = &pci_sal_ops;
116 pci_read (struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
118 return raw_pci_ops->read(pci_domain_nr(bus), bus->number,
119 devfn, where, size, value);
123 pci_write (struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
125 return raw_pci_ops->write(pci_domain_nr(bus), bus->number,
126 devfn, where, size, value);
129 static struct pci_ops pci_root_ops = {
137 struct pci_dev *dev = NULL;
139 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
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().
146 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL)
147 acpi_pci_irq_enable(dev);
152 subsys_initcall(pci_acpi_init);
154 /* Called by ACPI when it finds a new root bus. */
156 static struct pci_controller * __devinit
157 alloc_pci_controller (int seg)
159 struct pci_controller *controller;
161 controller = kmalloc(sizeof(*controller), GFP_KERNEL);
165 memset(controller, 0, sizeof(*controller));
166 controller->segment = seg;
171 alloc_resource (char *name, struct resource *root, unsigned long start, unsigned long end,
174 struct resource *res;
176 res = kmalloc(sizeof(*res), GFP_KERNEL);
180 memset(res, 0, sizeof(*res));
186 if (insert_resource(root, res)) {
195 add_io_space (struct acpi_resource_address64 *addr)
201 if (addr->address_translation_offset == 0)
202 return IO_SPACE_BASE(0); /* part of legacy IO space */
204 if (addr->attribute.io.translation_attribute == ACPI_SPARSE_TRANSLATION)
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);
213 if (num_io_spaces == MAX_IO_SPACES) {
214 printk("Too many IO port spaces\n");
219 io_space[i].mmio_base = offset;
220 io_space[i].sparse = sparse;
222 return IO_SPACE_BASE(i);
225 static acpi_status __devinit
226 count_window (struct acpi_resource *resource, void *data)
228 unsigned int *windows = (unsigned int *) data;
229 struct acpi_resource_address64 addr;
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)
241 struct pci_root_info {
242 struct pci_controller *controller;
246 static acpi_status __devinit
247 add_window (struct acpi_resource *res, void *data)
249 struct pci_root_info *info = (struct pci_root_info *) data;
250 struct pci_window *window;
251 struct acpi_resource_address64 addr;
253 unsigned long flags, offset = 0;
254 struct resource *root;
256 status = acpi_resource_to_address64(res, &addr);
257 if (ACPI_SUCCESS(status)) {
258 if (!addr.address_length)
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);
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;
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);
290 struct pci_bus * __devinit
291 pci_acpi_scan_root (struct acpi_device *device, int domain, int bus)
293 struct pci_root_info info;
294 struct pci_controller *controller;
295 unsigned int windows = 0;
298 controller = alloc_pci_controller(domain);
302 controller->acpi_handle = device->handle;
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)
309 name = kmalloc(16, GFP_KERNEL);
313 sprintf(name, "PCI Bus %04x:%02x", domain, bus);
314 info.controller = controller;
316 acpi_walk_resources(device->handle, METHOD_NAME__CRS, add_window, &info);
318 return pci_scan_bus(bus, &pci_root_ops, controller);
321 kfree(controller->window);
329 pcibios_fixup_device_resources (struct pci_dev *dev, struct pci_bus *bus)
331 struct pci_controller *controller = PCI_CONTROLLER(dev);
332 struct pci_window *window;
334 int limit = (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) ? \
335 PCI_ROM_RESOURCE : PCI_NUM_RESOURCES;
337 for (i = 0; i < limit; i++) {
338 if (!dev->resource[i].start)
341 #define contains(win, res) ((res)->start >= (win)->start && \
342 (res)->end <= (win)->end)
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;
355 pci_claim_resource(dev, i);
360 * Called after each bus is probed, but before its children are examined.
363 pcibios_fixup_bus (struct pci_bus *b)
365 struct list_head *ln;
367 for (ln = b->devices.next; ln != &b->devices; ln = ln->next)
368 pcibios_fixup_device_resources(pci_dev_b(ln), b);
374 pcibios_update_irq (struct pci_dev *dev, int irq)
376 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
378 /* ??? FIXME -- record old value for shutdown. */
382 pcibios_enable_resources (struct pci_dev *dev, int mask)
391 pci_read_config_word(dev, PCI_COMMAND, &cmd);
393 for (idx=0; idx<6; idx++) {
394 /* Only set up the desired resources. */
395 if (!(mask & (1 << idx)))
398 r = &dev->resource[idx];
399 if (!r->start && r->end) {
401 "PCI: Device %s not available because of resource collisions\n",
405 if (r->flags & IORESOURCE_IO)
406 cmd |= PCI_COMMAND_IO;
407 if (r->flags & IORESOURCE_MEM)
408 cmd |= PCI_COMMAND_MEMORY;
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);
420 pcibios_enable_device (struct pci_dev *dev, int mask)
424 ret = pcibios_enable_resources(dev, mask);
428 return acpi_pci_irq_enable(dev);
432 pcibios_align_resource (void *data, struct resource *res,
433 unsigned long size, unsigned long align)
438 * PCI BIOS setup, always defaults to SAL interface
441 pcibios_setup (char *str)
447 pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
448 enum pci_mmap_state mmap_state, int write_combine)
451 * I/O space cannot be accessed via normal processor loads and stores on this
454 if (mmap_state == pci_mmap_io)
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.
464 * Leave vm_pgoff as-is, the PCI space address is the physical address on this
467 vma->vm_flags |= (VM_SHM | VM_LOCKED | VM_IO);
470 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
472 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
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))
482 * pci_cacheline_size - determine cacheline size for PCI devices
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.
488 * Code mostly taken from arch/ia64/kernel/palinfo.c:cache_info().
490 * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
493 pci_cacheline_size (void)
495 u64 levels, unique_caches;
497 pal_cache_config_info_t cci;
498 static u8 cacheline_size;
501 return cacheline_size;
503 status = ia64_pal_cache_summary(&levels, &unique_caches);
505 printk(KERN_ERR "%s: ia64_pal_cache_summary() failed (status=%ld)\n",
506 __FUNCTION__, status);
507 return SMP_CACHE_BYTES;
510 status = ia64_pal_cache_config_info(levels - 1, /* cache_type (data_or_unified)= */ 2,
513 printk(KERN_ERR "%s: ia64_pal_cache_config_info() failed (status=%ld)\n",
514 __FUNCTION__, status);
515 return SMP_CACHE_BYTES;
517 cacheline_size = 1 << cci.pcci_line_size;
518 return cacheline_size;
522 * pcibios_prep_mwi - helper function for drivers/pci/pci.c:pci_set_mwi()
523 * @dev: the PCI device for which MWI is enabled
525 * For ia64, we can get the cacheline sizes from PAL.
527 * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
530 pcibios_prep_mwi (struct pci_dev *dev)
532 unsigned long desired_linesize, current_linesize;
536 desired_linesize = pci_cacheline_size();
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);
547 printk(" correcting to %lu\n", desired_linesize);
548 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, desired_linesize / 4);
554 int pci_vector_resources(int last, int nr_released)
556 int count = nr_released;
558 count += (IA64_LAST_DEVICE_VECTOR - last);