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>
43 #define DBG(x...) printk(x)
48 struct pci_fixup pcibios_fixups[1];
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.
56 #define PCI_SAL_ADDRESS(seg, bus, devfn, reg) \
57 ((u64)(seg << 24) | (u64)(bus << 16) | \
58 (u64)(devfn << 8) | (u64)(reg))
62 pci_sal_read (int seg, int bus, int devfn, int reg, int len, u32 *value)
67 if (!value || (seg > 255) || (bus > 255) || (devfn > 255) || (reg > 255))
70 result = ia64_sal_pci_config_read(PCI_SAL_ADDRESS(seg, bus, devfn, reg), len, &data);
78 pci_sal_write (int seg, int bus, int devfn, int reg, int len, u32 value)
80 if ((seg > 255) || (bus > 255) || (devfn > 255) || (reg > 255))
83 return ia64_sal_pci_config_write(PCI_SAL_ADDRESS(seg, bus, devfn, reg), len, value);
86 struct pci_raw_ops pci_sal_ops = {
88 .write = pci_sal_write
91 struct pci_raw_ops *raw_pci_ops = &pci_sal_ops; /* default to SAL */
95 pci_read (struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
97 return raw_pci_ops->read(pci_domain_nr(bus), bus->number,
98 devfn, where, size, value);
102 pci_write (struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
104 return raw_pci_ops->write(pci_domain_nr(bus), bus->number,
105 devfn, where, size, value);
108 static struct pci_ops pci_root_ops = {
116 if (!acpi_pci_irq_init())
117 printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
119 printk(KERN_WARNING "PCI: Invalid ACPI-PCI IRQ routing table\n");
123 subsys_initcall(pci_acpi_init);
125 /* Called by ACPI when it finds a new root bus. */
127 static struct pci_controller * __devinit
128 alloc_pci_controller (int seg)
130 struct pci_controller *controller;
132 controller = kmalloc(sizeof(*controller), GFP_KERNEL);
136 memset(controller, 0, sizeof(*controller));
137 controller->segment = seg;
142 alloc_resource (char *name, struct resource *root, unsigned long start, unsigned long end, unsigned long flags)
144 struct resource *res;
146 res = kmalloc(sizeof(*res), GFP_KERNEL);
150 memset(res, 0, sizeof(*res));
156 if (insert_resource(root, res))
163 add_io_space (struct acpi_resource_address64 *addr)
169 if (addr->address_translation_offset == 0)
170 return IO_SPACE_BASE(0); /* part of legacy IO space */
172 if (addr->attribute.io.translation_attribute == ACPI_SPARSE_TRANSLATION)
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);
181 if (num_io_spaces == MAX_IO_SPACES) {
182 printk("Too many IO port spaces\n");
187 io_space[i].mmio_base = offset;
188 io_space[i].sparse = sparse;
190 return IO_SPACE_BASE(i);
193 static acpi_status __devinit
194 count_window (struct acpi_resource *resource, void *data)
196 unsigned int *windows = (unsigned int *) data;
197 struct acpi_resource_address64 addr;
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)
209 struct pci_root_info {
210 struct pci_controller *controller;
214 static acpi_status __devinit
215 add_window (struct acpi_resource *res, void *data)
217 struct pci_root_info *info = (struct pci_root_info *) data;
218 struct pci_window *window;
219 struct acpi_resource_address64 addr;
221 unsigned long flags, offset = 0;
222 struct resource *root;
224 status = acpi_resource_to_address64(res, &addr);
225 if (ACPI_SUCCESS(status)) {
226 if (!addr.address_length)
229 if (addr.resource_type == ACPI_MEMORY_RANGE) {
230 flags = IORESOURCE_MEM;
231 root = &iomem_resource;
232 offset = addr.address_translation_offset;
233 } else if (addr.resource_type == ACPI_IO_RANGE) {
234 flags = IORESOURCE_IO;
235 root = &ioport_resource;
236 offset = add_io_space(&addr);
242 window = &info->controller->window[info->controller->windows++];
243 window->resource.flags |= flags;
244 window->resource.start = addr.min_address_range;
245 window->resource.end = addr.max_address_range;
246 window->offset = offset;
248 if (alloc_resource(info->name, root, addr.min_address_range + offset,
249 addr.max_address_range + offset, flags))
250 printk(KERN_ERR "alloc 0x%lx-0x%lx from %s for %s failed\n",
251 addr.min_address_range + offset, addr.max_address_range + offset,
252 root->name, info->name);
258 struct pci_bus * __devinit
259 pci_acpi_scan_root (struct acpi_device *device, int domain, int bus)
261 struct pci_root_info info;
262 struct pci_controller *controller;
263 unsigned int windows = 0;
266 controller = alloc_pci_controller(domain);
270 controller->acpi_handle = device->handle;
272 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_window, &windows);
273 controller->window = kmalloc(sizeof(*controller->window) * windows, GFP_KERNEL);
274 if (!controller->window)
277 name = kmalloc(16, GFP_KERNEL);
281 sprintf(name, "PCI Bus %04x:%02x", domain, bus);
282 info.controller = controller;
284 acpi_walk_resources(device->handle, METHOD_NAME__CRS, add_window, &info);
286 return pci_scan_bus(bus, &pci_root_ops, controller);
289 kfree(controller->window);
297 pcibios_fixup_device_resources (struct pci_dev *dev, struct pci_bus *bus)
299 struct pci_controller *controller = PCI_CONTROLLER(dev);
300 struct pci_window *window;
303 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
304 if (!dev->resource[i].start)
307 #define contains(win, res) ((res)->start >= (win)->start && \
308 (res)->end <= (win)->end)
310 for (j = 0; j < controller->windows; j++) {
311 window = &controller->window[j];
312 if (((dev->resource[i].flags & IORESOURCE_MEM &&
313 window->resource.flags & IORESOURCE_MEM) ||
314 (dev->resource[i].flags & IORESOURCE_IO &&
315 window->resource.flags & IORESOURCE_IO)) &&
316 contains(&window->resource, &dev->resource[i])) {
317 dev->resource[i].start += window->offset;
318 dev->resource[i].end += window->offset;
325 * Called after each bus is probed, but before its children are examined.
328 pcibios_fixup_bus (struct pci_bus *b)
330 struct list_head *ln;
332 for (ln = b->devices.next; ln != &b->devices; ln = ln->next)
333 pcibios_fixup_device_resources(pci_dev_b(ln), b);
339 pcibios_update_irq (struct pci_dev *dev, int irq)
341 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
343 /* ??? FIXME -- record old value for shutdown. */
347 pcibios_enable_resources (struct pci_dev *dev, int mask)
356 pci_read_config_word(dev, PCI_COMMAND, &cmd);
358 for (idx=0; idx<6; idx++) {
359 /* Only set up the desired resources. */
360 if (!(mask & (1 << idx)))
363 r = &dev->resource[idx];
364 if (!r->start && r->end) {
366 "PCI: Device %s not available because of resource collisions\n",
370 if (r->flags & IORESOURCE_IO)
371 cmd |= PCI_COMMAND_IO;
372 if (r->flags & IORESOURCE_MEM)
373 cmd |= PCI_COMMAND_MEMORY;
375 if (dev->resource[PCI_ROM_RESOURCE].start)
376 cmd |= PCI_COMMAND_MEMORY;
377 if (cmd != old_cmd) {
378 printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
379 pci_write_config_word(dev, PCI_COMMAND, cmd);
385 pcibios_enable_device (struct pci_dev *dev, int mask)
389 ret = pcibios_enable_resources(dev, mask);
393 printk(KERN_INFO "PCI: Found IRQ %d for device %s\n", dev->irq, pci_name(dev));
394 return acpi_pci_irq_enable(dev);
398 pcibios_align_resource (void *data, struct resource *res,
399 unsigned long size, unsigned long align)
404 * PCI BIOS setup, always defaults to SAL interface
407 pcibios_setup (char *str)
413 pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
414 enum pci_mmap_state mmap_state, int write_combine)
417 * I/O space cannot be accessed via normal processor loads and stores on this
420 if (mmap_state == pci_mmap_io)
422 * XXX we could relax this for I/O spaces for which ACPI indicates that
423 * the space is 1-to-1 mapped. But at the moment, we don't support
424 * multiple PCI address spaces and the legacy I/O space is not 1-to-1
425 * mapped, so this is moot.
430 * Leave vm_pgoff as-is, the PCI space address is the physical address on this
433 vma->vm_flags |= (VM_SHM | VM_LOCKED | VM_IO);
436 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
438 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
440 if (remap_page_range(vma, vma->vm_start, vma->vm_pgoff << PAGE_SHIFT,
441 vma->vm_end - vma->vm_start, vma->vm_page_prot))
448 * pci_cacheline_size - determine cacheline size for PCI devices
451 * We want to use the line-size of the outer-most cache. We assume
452 * that this line-size is the same for all CPUs.
454 * Code mostly taken from arch/ia64/kernel/palinfo.c:cache_info().
456 * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
459 pci_cacheline_size (void)
461 u64 levels, unique_caches;
463 pal_cache_config_info_t cci;
464 static u8 cacheline_size;
467 return cacheline_size;
469 status = ia64_pal_cache_summary(&levels, &unique_caches);
471 printk(KERN_ERR "%s: ia64_pal_cache_summary() failed (status=%ld)\n",
472 __FUNCTION__, status);
473 return SMP_CACHE_BYTES;
476 status = ia64_pal_cache_config_info(levels - 1, /* cache_type (data_or_unified)= */ 2,
479 printk(KERN_ERR "%s: ia64_pal_cache_config_info() failed (status=%ld)\n",
480 __FUNCTION__, status);
481 return SMP_CACHE_BYTES;
483 cacheline_size = 1 << cci.pcci_line_size;
484 return cacheline_size;
488 * pcibios_prep_mwi - helper function for drivers/pci/pci.c:pci_set_mwi()
489 * @dev: the PCI device for which MWI is enabled
491 * For ia64, we can get the cacheline sizes from PAL.
493 * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
496 pcibios_prep_mwi (struct pci_dev *dev)
498 unsigned long desired_linesize, current_linesize;
502 desired_linesize = pci_cacheline_size();
504 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &pci_linesize);
505 current_linesize = 4 * pci_linesize;
506 if (desired_linesize != current_linesize) {
507 printk(KERN_WARNING "PCI: slot %s has incorrect PCI cache line size of %lu bytes,",
508 pci_name(dev), current_linesize);
509 if (current_linesize > desired_linesize) {
510 printk(" expected %lu bytes instead\n", desired_linesize);
513 printk(" correcting to %lu\n", desired_linesize);
514 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, desired_linesize / 4);