blob: c0fca2c1c858372493675695f9a5341200c139ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * pci.c - Low-Level PCI Access in IA-64
3 *
4 * Derived from bios32.c of i386 tree.
5 *
6 * (c) Copyright 2002, 2005 Hewlett-Packard Development Company, L.P.
7 * David Mosberger-Tang <davidm@hpl.hp.com>
8 * Bjorn Helgaas <bjorn.helgaas@hp.com>
9 * Copyright (C) 2004 Silicon Graphics, Inc.
10 *
11 * Note: Above list of copyright holders is incomplete...
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/spinlock.h>
John Keller175add12008-11-24 16:47:17 -060022#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/machvec.h>
25#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/system.h>
27#include <asm/io.h>
28#include <asm/sal.h>
29#include <asm/smp.h>
30#include <asm/irq.h>
31#include <asm/hw_irq.h>
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * Low-level SAL-based PCI configuration access functions. Note that SAL
35 * calls are already serialized (via sal_lock), so we don't need another
36 * synchronization mechanism here.
37 */
38
39#define PCI_SAL_ADDRESS(seg, bus, devfn, reg) \
40 (((u64) seg << 24) | (bus << 16) | (devfn << 8) | (reg))
41
42/* SAL 3.2 adds support for extended config space. */
43
44#define PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg) \
45 (((u64) seg << 28) | (bus << 20) | (devfn << 12) | (reg))
46
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -050047int raw_pci_read(unsigned int seg, unsigned int bus, unsigned int devfn,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 int reg, int len, u32 *value)
49{
50 u64 addr, data = 0;
51 int mode, result;
52
53 if (!value || (seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
54 return -EINVAL;
55
56 if ((seg | reg) <= 255) {
57 addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
58 mode = 0;
Matthew Wilcoxadcd7402009-10-12 08:24:30 -060059 } else if (sal_revision >= SAL_VERSION_CODE(3,2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
61 mode = 1;
Matthew Wilcoxadcd7402009-10-12 08:24:30 -060062 } else {
63 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
Matthew Wilcoxadcd7402009-10-12 08:24:30 -060065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 result = ia64_sal_pci_config_read(addr, mode, len, &data);
67 if (result != 0)
68 return -EINVAL;
69
70 *value = (u32) data;
71 return 0;
72}
73
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -050074int raw_pci_write(unsigned int seg, unsigned int bus, unsigned int devfn,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int reg, int len, u32 value)
76{
77 u64 addr;
78 int mode, result;
79
80 if ((seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
81 return -EINVAL;
82
83 if ((seg | reg) <= 255) {
84 addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
85 mode = 0;
Matthew Wilcoxadcd7402009-10-12 08:24:30 -060086 } else if (sal_revision >= SAL_VERSION_CODE(3,2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
88 mode = 1;
Matthew Wilcoxadcd7402009-10-12 08:24:30 -060089 } else {
90 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92 result = ia64_sal_pci_config_write(addr, mode, len, value);
93 if (result != 0)
94 return -EINVAL;
95 return 0;
96}
97
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -050098static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
99 int size, u32 *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500101 return raw_pci_read(pci_domain_nr(bus), bus->number,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 devfn, where, size, value);
103}
104
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500105static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
106 int size, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500108 return raw_pci_write(pci_domain_nr(bus), bus->number,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 devfn, where, size, value);
110}
111
112struct pci_ops pci_root_ops = {
113 .read = pci_read,
114 .write = pci_write,
115};
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* Called by ACPI when it finds a new root bus. */
118
119static struct pci_controller * __devinit
120alloc_pci_controller (int seg)
121{
122 struct pci_controller *controller;
123
Yan Burman52fd9102006-12-04 14:58:35 -0800124 controller = kzalloc(sizeof(*controller), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (!controller)
126 return NULL;
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 controller->segment = seg;
Christoph Lameter514604c2005-07-07 16:59:00 -0700129 controller->node = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return controller;
131}
132
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700133struct pci_root_info {
134 struct pci_controller *controller;
135 char *name;
136};
137
138static unsigned int
139new_space (u64 phys_base, int sparse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700141 u64 mmio_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int i;
143
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700144 if (phys_base == 0)
145 return 0; /* legacy I/O port space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700147 mmio_base = (u64) ioremap(phys_base, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 for (i = 0; i < num_io_spaces; i++)
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700149 if (io_space[i].mmio_base == mmio_base &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 io_space[i].sparse == sparse)
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700151 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if (num_io_spaces == MAX_IO_SPACES) {
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700154 printk(KERN_ERR "PCI: Too many IO port spaces "
155 "(MAX_IO_SPACES=%lu)\n", MAX_IO_SPACES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return ~0;
157 }
158
159 i = num_io_spaces++;
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700160 io_space[i].mmio_base = mmio_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 io_space[i].sparse = sparse;
162
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700163 return i;
164}
165
166static u64 __devinit
167add_io_space (struct pci_root_info *info, struct acpi_resource_address64 *addr)
168{
169 struct resource *resource;
170 char *name;
Matthew Wilcoxe088a4a2009-05-22 13:49:49 -0700171 unsigned long base, min, max, base_port;
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700172 unsigned int sparse = 0, space_nr, len;
173
174 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
175 if (!resource) {
176 printk(KERN_ERR "PCI: No memory for %s I/O port space\n",
177 info->name);
178 goto out;
179 }
180
181 len = strlen(info->name) + 32;
182 name = kzalloc(len, GFP_KERNEL);
183 if (!name) {
184 printk(KERN_ERR "PCI: No memory for %s I/O port space name\n",
185 info->name);
186 goto free_resource;
187 }
188
Bob Moore50eca3e2005-09-30 19:03:00 -0400189 min = addr->minimum;
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700190 max = min + addr->address_length - 1;
Bob Moore08978312005-10-21 00:00:00 -0400191 if (addr->info.io.translation_type == ACPI_SPARSE_TRANSLATION)
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700192 sparse = 1;
193
Bob Moore50eca3e2005-09-30 19:03:00 -0400194 space_nr = new_space(addr->translation_offset, sparse);
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700195 if (space_nr == ~0)
196 goto free_name;
197
198 base = __pa(io_space[space_nr].mmio_base);
199 base_port = IO_SPACE_BASE(space_nr);
200 snprintf(name, len, "%s I/O Ports %08lx-%08lx", info->name,
201 base_port + min, base_port + max);
202
203 /*
204 * The SDM guarantees the legacy 0-64K space is sparse, but if the
205 * mapping is done by the processor (not the bridge), ACPI may not
206 * mark it as sparse.
207 */
208 if (space_nr == 0)
209 sparse = 1;
210
211 resource->name = name;
212 resource->flags = IORESOURCE_MEM;
213 resource->start = base + (sparse ? IO_SPACE_SPARSE_ENCODING(min) : min);
214 resource->end = base + (sparse ? IO_SPACE_SPARSE_ENCODING(max) : max);
215 insert_resource(&iomem_resource, resource);
216
217 return base_port;
218
219free_name:
220 kfree(name);
221free_resource:
222 kfree(resource);
223out:
224 return ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Bjorn Helgaas463eb292005-09-23 11:39:07 -0600227static acpi_status __devinit resource_to_window(struct acpi_resource *resource,
228 struct acpi_resource_address64 *addr)
229{
230 acpi_status status;
231
232 /*
233 * We're only interested in _CRS descriptors that are
234 * - address space descriptors for memory or I/O space
235 * - non-zero size
236 * - producers, i.e., the address space is routed downstream,
237 * not consumed by the bridge itself
238 */
239 status = acpi_resource_to_address64(resource, addr);
240 if (ACPI_SUCCESS(status) &&
241 (addr->resource_type == ACPI_MEMORY_RANGE ||
242 addr->resource_type == ACPI_IO_RANGE) &&
243 addr->address_length &&
244 addr->producer_consumer == ACPI_PRODUCER)
245 return AE_OK;
246
247 return AE_ERROR;
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static acpi_status __devinit
251count_window (struct acpi_resource *resource, void *data)
252{
253 unsigned int *windows = (unsigned int *) data;
254 struct acpi_resource_address64 addr;
255 acpi_status status;
256
Bjorn Helgaas463eb292005-09-23 11:39:07 -0600257 status = resource_to_window(resource, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (ACPI_SUCCESS(status))
Bjorn Helgaas463eb292005-09-23 11:39:07 -0600259 (*windows)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 return AE_OK;
262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264static __devinit acpi_status add_window(struct acpi_resource *res, void *data)
265{
266 struct pci_root_info *info = data;
267 struct pci_window *window;
268 struct acpi_resource_address64 addr;
269 acpi_status status;
270 unsigned long flags, offset = 0;
271 struct resource *root;
272
Bjorn Helgaas463eb292005-09-23 11:39:07 -0600273 /* Return AE_OK for non-window resources to keep scanning for more */
274 status = resource_to_window(res, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (!ACPI_SUCCESS(status))
276 return AE_OK;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (addr.resource_type == ACPI_MEMORY_RANGE) {
279 flags = IORESOURCE_MEM;
280 root = &iomem_resource;
Bob Moore50eca3e2005-09-30 19:03:00 -0400281 offset = addr.translation_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 } else if (addr.resource_type == ACPI_IO_RANGE) {
283 flags = IORESOURCE_IO;
284 root = &ioport_resource;
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700285 offset = add_io_space(info, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (offset == ~0)
287 return AE_OK;
288 } else
289 return AE_OK;
290
291 window = &info->controller->window[info->controller->windows++];
292 window->resource.name = info->name;
293 window->resource.flags = flags;
Bob Moore50eca3e2005-09-30 19:03:00 -0400294 window->resource.start = addr.minimum + offset;
Bjorn Helgaas4f41d5a2005-11-07 15:13:59 -0700295 window->resource.end = window->resource.start + addr.address_length - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 window->resource.child = NULL;
297 window->offset = offset;
298
299 if (insert_resource(root, &window->resource)) {
Matthew Wilcoxe088a4a2009-05-22 13:49:49 -0700300 printk(KERN_ERR "alloc 0x%llx-0x%llx from %s for %s failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 window->resource.start, window->resource.end,
302 root->name, info->name);
303 }
304
305 return AE_OK;
306}
307
308static void __devinit
309pcibios_setup_root_windows(struct pci_bus *bus, struct pci_controller *ctrl)
310{
311 int i, j;
312
313 j = 0;
314 for (i = 0; i < ctrl->windows; i++) {
315 struct resource *res = &ctrl->window[i].resource;
316 /* HP's firmware has a hack to work around a Windows bug.
317 * Ignore these tiny memory ranges */
318 if ((res->flags & IORESOURCE_MEM) &&
319 (res->end - res->start < 16))
320 continue;
321 if (j >= PCI_BUS_NUM_RESOURCES) {
Matthew Wilcoxe088a4a2009-05-22 13:49:49 -0700322 printk("Ignoring range [%#llx-%#llx] (%lx)\n",
323 res->start, res->end, res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 continue;
325 }
326 bus->resource[j++] = res;
327 }
328}
329
330struct pci_bus * __devinit
331pci_acpi_scan_root(struct acpi_device *device, int domain, int bus)
332{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct pci_controller *controller;
334 unsigned int windows = 0;
335 struct pci_bus *pbus;
336 char *name;
Christoph Lameter514604c2005-07-07 16:59:00 -0700337 int pxm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 controller = alloc_pci_controller(domain);
340 if (!controller)
341 goto out1;
342
343 controller->acpi_handle = device->handle;
344
Christoph Lameter514604c2005-07-07 16:59:00 -0700345 pxm = acpi_get_pxm(controller->acpi_handle);
346#ifdef CONFIG_NUMA
347 if (pxm >= 0)
Yasunori Goto762834e2006-06-23 02:03:19 -0700348 controller->node = pxm_to_node(pxm);
Christoph Lameter514604c2005-07-07 16:59:00 -0700349#endif
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_window,
352 &windows);
Kenji Kaneshigea66aa702007-05-22 10:20:36 -0700353 if (windows) {
Luck, Tony8a20fd52008-08-15 15:37:48 -0700354 struct pci_root_info info;
355
Kenji Kaneshigea66aa702007-05-22 10:20:36 -0700356 controller->window =
357 kmalloc_node(sizeof(*controller->window) * windows,
358 GFP_KERNEL, controller->node);
359 if (!controller->window)
360 goto out2;
Luck, Tony8a20fd52008-08-15 15:37:48 -0700361
362 name = kmalloc(16, GFP_KERNEL);
363 if (!name)
364 goto out3;
365
366 sprintf(name, "PCI Bus %04x:%02x", domain, bus);
367 info.controller = controller;
368 info.name = name;
369 acpi_walk_resources(device->handle, METHOD_NAME__CRS,
370 add_window, &info);
Kenji Kaneshigea66aa702007-05-22 10:20:36 -0700371 }
yakui.zhao@intel.comb87e81e2008-04-15 14:34:49 -0700372 /*
373 * See arch/x86/pci/acpi.c.
374 * The desired pci bus might already be scanned in a quirk. We
375 * should handle the case here, but it appears that IA64 hasn't
376 * such quirk. So we just ignore the case now.
377 */
Rajesh Shahc431ada2005-04-28 00:25:45 -0700378 pbus = pci_scan_bus_parented(NULL, bus, &pci_root_ops, controller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 return pbus;
381
382out3:
383 kfree(controller->window);
384out2:
385 kfree(controller);
386out1:
387 return NULL;
388}
389
390void pcibios_resource_to_bus(struct pci_dev *dev,
391 struct pci_bus_region *region, struct resource *res)
392{
393 struct pci_controller *controller = PCI_CONTROLLER(dev);
394 unsigned long offset = 0;
395 int i;
396
397 for (i = 0; i < controller->windows; i++) {
398 struct pci_window *window = &controller->window[i];
399 if (!(window->resource.flags & res->flags))
400 continue;
401 if (window->resource.start > res->start)
402 continue;
403 if (window->resource.end < res->end)
404 continue;
405 offset = window->offset;
406 break;
407 }
408
409 region->start = res->start - offset;
410 region->end = res->end - offset;
411}
412EXPORT_SYMBOL(pcibios_resource_to_bus);
413
414void pcibios_bus_to_resource(struct pci_dev *dev,
415 struct resource *res, struct pci_bus_region *region)
416{
417 struct pci_controller *controller = PCI_CONTROLLER(dev);
418 unsigned long offset = 0;
419 int i;
420
421 for (i = 0; i < controller->windows; i++) {
422 struct pci_window *window = &controller->window[i];
423 if (!(window->resource.flags & res->flags))
424 continue;
425 if (window->resource.start - window->offset > region->start)
426 continue;
427 if (window->resource.end - window->offset < region->end)
428 continue;
429 offset = window->offset;
430 break;
431 }
432
433 res->start = region->start + offset;
434 res->end = region->end + offset;
435}
Keith Owens41290c12005-08-24 16:06:25 +1000436EXPORT_SYMBOL(pcibios_bus_to_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Rajesh Shah71c35112005-04-28 00:25:46 -0700438static int __devinit is_valid_resource(struct pci_dev *dev, int idx)
439{
440 unsigned int i, type_mask = IORESOURCE_IO | IORESOURCE_MEM;
441 struct resource *devr = &dev->resource[idx];
442
443 if (!dev->bus)
444 return 0;
445 for (i=0; i<PCI_BUS_NUM_RESOURCES; i++) {
446 struct resource *busr = dev->bus->resource[i];
447
448 if (!busr || ((busr->flags ^ devr->flags) & type_mask))
449 continue;
450 if ((devr->start) && (devr->start >= busr->start) &&
451 (devr->end <= busr->end))
452 return 1;
453 }
454 return 0;
455}
456
Kenji Kaneshige7b9c8ba2006-01-16 13:45:23 +0900457static void __devinit
458pcibios_fixup_resources(struct pci_dev *dev, int start, int limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct pci_bus_region region;
461 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Kenji Kaneshige7b9c8ba2006-01-16 13:45:23 +0900463 for (i = start; i < limit; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (!dev->resource[i].flags)
465 continue;
466 region.start = dev->resource[i].start;
467 region.end = dev->resource[i].end;
468 pcibios_bus_to_resource(dev, &dev->resource[i], &region);
Rajesh Shah71c35112005-04-28 00:25:46 -0700469 if ((is_valid_resource(dev, i)))
470 pci_claim_resource(dev, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472}
473
John Keller8ea60912006-10-04 16:49:25 -0500474void __devinit pcibios_fixup_device_resources(struct pci_dev *dev)
Kenji Kaneshige7b9c8ba2006-01-16 13:45:23 +0900475{
476 pcibios_fixup_resources(dev, 0, PCI_BRIDGE_RESOURCES);
477}
John Keller8ea60912006-10-04 16:49:25 -0500478EXPORT_SYMBOL_GPL(pcibios_fixup_device_resources);
Kenji Kaneshige7b9c8ba2006-01-16 13:45:23 +0900479
480static void __devinit pcibios_fixup_bridge_resources(struct pci_dev *dev)
481{
482 pcibios_fixup_resources(dev, PCI_BRIDGE_RESOURCES, PCI_NUM_RESOURCES);
483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/*
486 * Called after each bus is probed, but before its children are examined.
487 */
488void __devinit
489pcibios_fixup_bus (struct pci_bus *b)
490{
491 struct pci_dev *dev;
492
Rajesh Shahf7d473d2005-04-28 00:25:51 -0700493 if (b->self) {
494 pci_read_bridge_bases(b);
Kenji Kaneshige7b9c8ba2006-01-16 13:45:23 +0900495 pcibios_fixup_bridge_resources(b->self);
Matthew Wilcox1d89b302009-06-17 16:33:36 -0400496 } else {
497 pcibios_setup_root_windows(b, b->sysdata);
Rajesh Shahf7d473d2005-04-28 00:25:51 -0700498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 list_for_each_entry(dev, &b->devices, bus_list)
500 pcibios_fixup_device_resources(dev);
John Keller8ea60912006-10-04 16:49:25 -0500501 platform_pci_fixup_bus(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 return;
504}
505
506void __devinit
507pcibios_update_irq (struct pci_dev *dev, int irq)
508{
509 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
510
511 /* ??? FIXME -- record old value for shutdown. */
512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514int
515pcibios_enable_device (struct pci_dev *dev, int mask)
516{
517 int ret;
518
Bjorn Helgaasd981f162008-03-04 11:56:52 -0700519 ret = pci_enable_resources(dev, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (ret < 0)
521 return ret;
522
Eric W. Biedermanbba6f6f2007-03-28 15:36:09 +0200523 if (!dev->msi_enabled)
524 return acpi_pci_irq_enable(dev);
525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528void
529pcibios_disable_device (struct pci_dev *dev)
530{
Peter Chubbc7f570a2006-12-05 12:25:31 +1100531 BUG_ON(atomic_read(&dev->enable_cnt));
Eric W. Biedermanbba6f6f2007-03-28 15:36:09 +0200532 if (!dev->msi_enabled)
533 acpi_pci_irq_disable(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536void
537pcibios_align_resource (void *data, struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700538 resource_size_t size, resource_size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540}
541
542/*
543 * PCI BIOS setup, always defaults to SAL interface
544 */
Ingo Molnar944c54e2009-07-01 00:10:16 +0200545char * __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546pcibios_setup (char *str)
547{
Matthew Wilcoxac311ac2006-02-24 12:46:23 -0700548 return str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551int
552pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
553 enum pci_mmap_state mmap_state, int write_combine)
554{
Alex Chiang012b7102007-07-11 11:02:15 -0600555 unsigned long size = vma->vm_end - vma->vm_start;
556 pgprot_t prot;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /*
559 * I/O space cannot be accessed via normal processor loads and
560 * stores on this platform.
561 */
562 if (mmap_state == pci_mmap_io)
563 /*
564 * XXX we could relax this for I/O spaces for which ACPI
565 * indicates that the space is 1-to-1 mapped. But at the
566 * moment, we don't support multiple PCI address spaces and
567 * the legacy I/O space is not 1-to-1 mapped, so this is moot.
568 */
569 return -EINVAL;
570
Alex Chiang012b7102007-07-11 11:02:15 -0600571 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
572 return -EINVAL;
573
574 prot = phys_mem_access_prot(NULL, vma->vm_pgoff, size,
575 vma->vm_page_prot);
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /*
Alex Chiang012b7102007-07-11 11:02:15 -0600578 * If the user requested WC, the kernel uses UC or WC for this region,
579 * and the chipset supports WC, we can use WC. Otherwise, we have to
580 * use the same attribute the kernel uses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
Alex Chiang012b7102007-07-11 11:02:15 -0600582 if (write_combine &&
583 ((pgprot_val(prot) & _PAGE_MA_MASK) == _PAGE_MA_UC ||
584 (pgprot_val(prot) & _PAGE_MA_MASK) == _PAGE_MA_WC) &&
585 efi_range_is_wc(vma->vm_start, vma->vm_end - vma->vm_start))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
587 else
Alex Chiang012b7102007-07-11 11:02:15 -0600588 vma->vm_page_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
591 vma->vm_end - vma->vm_start, vma->vm_page_prot))
592 return -EAGAIN;
593
594 return 0;
595}
596
597/**
598 * ia64_pci_get_legacy_mem - generic legacy mem routine
599 * @bus: bus to get legacy memory base address for
600 *
601 * Find the base of legacy memory for @bus. This is typically the first
602 * megabyte of bus address space for @bus or is simply 0 on platforms whose
603 * chipsets support legacy I/O and memory routing. Returns the base address
604 * or an error pointer if an error occurred.
605 *
606 * This is the ia64 generic version of this routine. Other platforms
607 * are free to override it with a machine vector.
608 */
609char *ia64_pci_get_legacy_mem(struct pci_bus *bus)
610{
611 return (char *)__IA64_UNCACHED_OFFSET;
612}
613
614/**
615 * pci_mmap_legacy_page_range - map legacy memory space to userland
616 * @bus: bus whose legacy space we're mapping
617 * @vma: vma passed in by mmap
618 *
619 * Map legacy memory space for this device back to userspace using a machine
620 * vector to get the base address.
621 */
622int
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000623pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma,
624 enum pci_mmap_state mmap_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600626 unsigned long size = vma->vm_end - vma->vm_start;
627 pgprot_t prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 char *addr;
629
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000630 /* We only support mmap'ing of legacy memory space */
631 if (mmap_state != pci_mmap_mem)
632 return -ENOSYS;
633
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600634 /*
635 * Avoid attribute aliasing. See Documentation/ia64/aliasing.txt
636 * for more details.
637 */
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700638 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600639 return -EINVAL;
640 prot = phys_mem_access_prot(NULL, vma->vm_pgoff, size,
641 vma->vm_page_prot);
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 addr = pci_get_legacy_mem(bus);
644 if (IS_ERR(addr))
645 return PTR_ERR(addr);
646
647 vma->vm_pgoff += (unsigned long)addr >> PAGE_SHIFT;
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600648 vma->vm_page_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
Bjorn Helgaas32e62c62006-05-05 17:19:50 -0600651 size, vma->vm_page_prot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return -EAGAIN;
653
654 return 0;
655}
656
657/**
658 * ia64_pci_legacy_read - read from legacy I/O space
659 * @bus: bus to read
660 * @port: legacy port value
661 * @val: caller allocated storage for returned value
662 * @size: number of bytes to read
663 *
664 * Simply reads @size bytes from @port and puts the result in @val.
665 *
666 * Again, this (and the write routine) are generic versions that can be
667 * overridden by the platform. This is necessary on platforms that don't
668 * support legacy I/O routing or that hard fail on legacy I/O timeouts.
669 */
670int ia64_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
671{
672 int ret = size;
673
674 switch (size) {
675 case 1:
676 *val = inb(port);
677 break;
678 case 2:
679 *val = inw(port);
680 break;
681 case 4:
682 *val = inl(port);
683 break;
684 default:
685 ret = -EINVAL;
686 break;
687 }
688
689 return ret;
690}
691
692/**
693 * ia64_pci_legacy_write - perform a legacy I/O write
694 * @bus: bus pointer
695 * @port: port to write
696 * @val: value to write
697 * @size: number of bytes to write from @val
698 *
699 * Simply writes @size bytes of @val to @port.
700 */
Satoru Takeuchia72391e2006-04-20 18:49:48 +0900701int ia64_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Alex Williamson408045a2005-12-21 15:21:36 -0700703 int ret = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 switch (size) {
706 case 1:
707 outb(val, port);
708 break;
709 case 2:
710 outw(val, port);
711 break;
712 case 4:
713 outl(val, port);
714 break;
715 default:
716 ret = -EINVAL;
717 break;
718 }
719
720 return ret;
721}
722
Matthew Wilcox3efe2d82006-10-10 08:01:19 -0600723/* It's defined in drivers/pci/pci.c */
724extern u8 pci_cache_line_size;
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726/**
Matthew Wilcox3efe2d82006-10-10 08:01:19 -0600727 * set_pci_cacheline_size - determine cacheline size for PCI devices
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 *
729 * We want to use the line-size of the outer-most cache. We assume
730 * that this line-size is the same for all CPUs.
731 *
732 * Code mostly taken from arch/ia64/kernel/palinfo.c:cache_info().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 */
Matthew Wilcox3efe2d82006-10-10 08:01:19 -0600734static void __init set_pci_cacheline_size(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Matthew Wilcoxe088a4a2009-05-22 13:49:49 -0700736 unsigned long levels, unique_caches;
737 long status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 pal_cache_config_info_t cci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 status = ia64_pal_cache_summary(&levels, &unique_caches);
741 if (status != 0) {
Matthew Wilcox3efe2d82006-10-10 08:01:19 -0600742 printk(KERN_ERR "%s: ia64_pal_cache_summary() failed "
Harvey Harrisond4ed8082008-03-04 15:15:00 -0800743 "(status=%ld)\n", __func__, status);
Matthew Wilcox3efe2d82006-10-10 08:01:19 -0600744 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
Matthew Wilcox3efe2d82006-10-10 08:01:19 -0600747 status = ia64_pal_cache_config_info(levels - 1,
748 /* cache_type (data_or_unified)= */ 2, &cci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (status != 0) {
Matthew Wilcox3efe2d82006-10-10 08:01:19 -0600750 printk(KERN_ERR "%s: ia64_pal_cache_config_info() failed "
Harvey Harrisond4ed8082008-03-04 15:15:00 -0800751 "(status=%ld)\n", __func__, status);
Matthew Wilcox3efe2d82006-10-10 08:01:19 -0600752 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Matthew Wilcox3efe2d82006-10-10 08:01:19 -0600754 pci_cache_line_size = (1 << cci.pcci_line_size) / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
John Keller175add12008-11-24 16:47:17 -0600757u64 ia64_dma_get_required_mask(struct device *dev)
758{
759 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
760 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
761 u64 mask;
762
763 if (!high_totalram) {
764 /* convert to mask just covering totalram */
765 low_totalram = (1 << (fls(low_totalram) - 1));
766 low_totalram += low_totalram - 1;
767 mask = low_totalram;
768 } else {
769 high_totalram = (1 << (fls(high_totalram) - 1));
770 high_totalram += high_totalram - 1;
771 mask = (((u64)high_totalram) << 32) + 0xffffffff;
772 }
773 return mask;
774}
775EXPORT_SYMBOL_GPL(ia64_dma_get_required_mask);
776
777u64 dma_get_required_mask(struct device *dev)
778{
779 return platform_dma_get_required_mask(dev);
780}
781EXPORT_SYMBOL_GPL(dma_get_required_mask);
782
Matthew Wilcox3efe2d82006-10-10 08:01:19 -0600783static int __init pcibios_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Matthew Wilcox3efe2d82006-10-10 08:01:19 -0600785 set_pci_cacheline_size();
786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
Matthew Wilcox3efe2d82006-10-10 08:01:19 -0600788
789subsys_initcall(pcibios_init);