blob: 73f1d42d48668b70ffcfcdb890caa430bd6e2f15 [file] [log] [blame]
David S. Millera2fb23a2007-02-28 23:35:04 -08001/* pci.c: UltraSparc PCI controller support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 1997, 1998, 1999 David S. Miller (davem@redhat.com)
4 * Copyright (C) 1998, 1999 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 1999 Jakub Jelinek (jj@ultra.linux.cz)
David S. Millera2fb23a2007-02-28 23:35:04 -08006 *
7 * OF tree based PCI bus probing taken from the PowerPC port
8 * with minor modifications, see there for credits.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/string.h>
14#include <linux/sched.h>
15#include <linux/capability.h>
16#include <linux/errno.h>
David S. Millerc57c2ff2007-05-08 00:43:56 -070017#include <linux/pci.h>
David S. Miller35a17eb2007-02-10 17:41:02 -080018#include <linux/msi.h>
19#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
David S. Miller356d1642008-08-30 00:36:11 -070021#include <linux/of.h>
22#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/pgtable.h>
26#include <asm/irq.h>
David S. Millere87dc352006-06-21 18:18:47 -070027#include <asm/prom.h>
David S. Miller01f94c42007-03-04 12:53:19 -080028#include <asm/apb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
David S. Miller1e8a8cc2007-02-28 23:38:38 -080030#include "pci_impl.h"
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/* List of all PCI controllers found in the system. */
David S. Miller34768bc2007-05-07 23:06:27 -070033struct pci_pbm_info *pci_pbm_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
David S. Miller6c108f12007-05-07 23:49:01 -070035/* Each PBM found gets a unique index. */
36int pci_num_pbms = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038volatile int pci_poke_in_progress;
39volatile int pci_poke_cpu = -1;
40volatile int pci_poke_faulted;
41
42static DEFINE_SPINLOCK(pci_poke_lock);
43
44void pci_config_read8(u8 *addr, u8 *ret)
45{
46 unsigned long flags;
47 u8 byte;
48
49 spin_lock_irqsave(&pci_poke_lock, flags);
50 pci_poke_cpu = smp_processor_id();
51 pci_poke_in_progress = 1;
52 pci_poke_faulted = 0;
53 __asm__ __volatile__("membar #Sync\n\t"
54 "lduba [%1] %2, %0\n\t"
55 "membar #Sync"
56 : "=r" (byte)
57 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
58 : "memory");
59 pci_poke_in_progress = 0;
60 pci_poke_cpu = -1;
61 if (!pci_poke_faulted)
62 *ret = byte;
63 spin_unlock_irqrestore(&pci_poke_lock, flags);
64}
65
66void pci_config_read16(u16 *addr, u16 *ret)
67{
68 unsigned long flags;
69 u16 word;
70
71 spin_lock_irqsave(&pci_poke_lock, flags);
72 pci_poke_cpu = smp_processor_id();
73 pci_poke_in_progress = 1;
74 pci_poke_faulted = 0;
75 __asm__ __volatile__("membar #Sync\n\t"
76 "lduha [%1] %2, %0\n\t"
77 "membar #Sync"
78 : "=r" (word)
79 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
80 : "memory");
81 pci_poke_in_progress = 0;
82 pci_poke_cpu = -1;
83 if (!pci_poke_faulted)
84 *ret = word;
85 spin_unlock_irqrestore(&pci_poke_lock, flags);
86}
87
88void pci_config_read32(u32 *addr, u32 *ret)
89{
90 unsigned long flags;
91 u32 dword;
92
93 spin_lock_irqsave(&pci_poke_lock, flags);
94 pci_poke_cpu = smp_processor_id();
95 pci_poke_in_progress = 1;
96 pci_poke_faulted = 0;
97 __asm__ __volatile__("membar #Sync\n\t"
98 "lduwa [%1] %2, %0\n\t"
99 "membar #Sync"
100 : "=r" (dword)
101 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
102 : "memory");
103 pci_poke_in_progress = 0;
104 pci_poke_cpu = -1;
105 if (!pci_poke_faulted)
106 *ret = dword;
107 spin_unlock_irqrestore(&pci_poke_lock, flags);
108}
109
110void pci_config_write8(u8 *addr, u8 val)
111{
112 unsigned long flags;
113
114 spin_lock_irqsave(&pci_poke_lock, flags);
115 pci_poke_cpu = smp_processor_id();
116 pci_poke_in_progress = 1;
117 pci_poke_faulted = 0;
118 __asm__ __volatile__("membar #Sync\n\t"
119 "stba %0, [%1] %2\n\t"
120 "membar #Sync"
121 : /* no outputs */
122 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
123 : "memory");
124 pci_poke_in_progress = 0;
125 pci_poke_cpu = -1;
126 spin_unlock_irqrestore(&pci_poke_lock, flags);
127}
128
129void pci_config_write16(u16 *addr, u16 val)
130{
131 unsigned long flags;
132
133 spin_lock_irqsave(&pci_poke_lock, flags);
134 pci_poke_cpu = smp_processor_id();
135 pci_poke_in_progress = 1;
136 pci_poke_faulted = 0;
137 __asm__ __volatile__("membar #Sync\n\t"
138 "stha %0, [%1] %2\n\t"
139 "membar #Sync"
140 : /* no outputs */
141 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
142 : "memory");
143 pci_poke_in_progress = 0;
144 pci_poke_cpu = -1;
145 spin_unlock_irqrestore(&pci_poke_lock, flags);
146}
147
148void pci_config_write32(u32 *addr, u32 val)
149{
150 unsigned long flags;
151
152 spin_lock_irqsave(&pci_poke_lock, flags);
153 pci_poke_cpu = smp_processor_id();
154 pci_poke_in_progress = 1;
155 pci_poke_faulted = 0;
156 __asm__ __volatile__("membar #Sync\n\t"
157 "stwa %0, [%1] %2\n\t"
158 "membar #Sync"
159 : /* no outputs */
160 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
161 : "memory");
162 pci_poke_in_progress = 0;
163 pci_poke_cpu = -1;
164 spin_unlock_irqrestore(&pci_poke_lock, flags);
165}
166
167/* Probe for all PCI controllers in the system. */
David S. Millere87dc352006-06-21 18:18:47 -0700168extern void sabre_init(struct device_node *, const char *);
169extern void psycho_init(struct device_node *, const char *);
David S. Millere87dc352006-06-21 18:18:47 -0700170extern void sun4v_pci_init(struct device_node *, const char *);
David S. Miller861fe902007-05-02 17:31:36 -0700171extern void fire_pci_init(struct device_node *, const char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173static struct {
174 char *model_name;
David S. Millere87dc352006-06-21 18:18:47 -0700175 void (*init)(struct device_node *, const char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176} pci_controller_table[] __initdata = {
177 { "SUNW,sabre", sabre_init },
178 { "pci108e,a000", sabre_init },
179 { "pci108e,a001", sabre_init },
180 { "SUNW,psycho", psycho_init },
181 { "pci108e,8000", psycho_init },
David S. Miller8f6a93a2006-02-09 21:32:07 -0800182 { "SUNW,sun4v-pci", sun4v_pci_init },
David S. Miller861fe902007-05-02 17:31:36 -0700183 { "pciex108e,80f0", fire_pci_init },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
Alejandro Martinez Ruiz29f139c2007-10-22 17:24:19 -0700185#define PCI_NUM_CONTROLLER_TYPES ARRAY_SIZE(pci_controller_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
David S. Millere87dc352006-06-21 18:18:47 -0700187static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 int i;
190
191 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
192 if (!strncmp(model_name,
193 pci_controller_table[i].model_name,
194 namelen)) {
David S. Millere87dc352006-06-21 18:18:47 -0700195 pci_controller_table[i].init(dp, model_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return 1;
197 }
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 return 0;
201}
202
David S. Millere87dc352006-06-21 18:18:47 -0700203static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
David S. Millere87dc352006-06-21 18:18:47 -0700205 struct device_node *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int count = 0;
207
David S. Millere87dc352006-06-21 18:18:47 -0700208 for_each_node_by_name(dp, "pci") {
209 struct property *prop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 int len;
211
David S. Millere87dc352006-06-21 18:18:47 -0700212 prop = of_find_property(dp, "model", &len);
213 if (!prop)
214 prop = of_find_property(dp, "compatible", &len);
215
216 if (prop) {
217 const char *model = prop->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 int item_len = 0;
219
220 /* Our value may be a multi-valued string in the
221 * case of some compatible properties. For sanity,
David S. Millere87dc352006-06-21 18:18:47 -0700222 * only try the first one.
223 */
224 while (model[item_len] && len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 len--;
226 item_len++;
227 }
228
David S. Millere87dc352006-06-21 18:18:47 -0700229 if (handler(model, item_len, dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 count++;
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
234 return count;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/* Find each controller in the system, attach and initialize
238 * software state structure for each and link into the
David S. Miller34768bc2007-05-07 23:06:27 -0700239 * pci_pbm_root. Setup the controller enough such
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * that bus scanning can be done.
241 */
242static void __init pci_controller_probe(void)
243{
244 printk("PCI: Probing for controllers.\n");
245
246 pci_controller_scan(pci_controller_init);
247}
248
David S. Miller5840fc62007-05-22 01:24:14 -0700249static int ofpci_verbose;
250
251static int __init ofpci_debug(char *str)
252{
253 int val = 0;
254
255 get_option(&str, &val);
256 if (val)
257 ofpci_verbose = 1;
258 return 1;
259}
260
261__setup("ofpci_debug=", ofpci_debug);
262
David S. Millera2fb23a2007-02-28 23:35:04 -0800263static unsigned long pci_parse_of_flags(u32 addr0)
264{
265 unsigned long flags = 0;
266
267 if (addr0 & 0x02000000) {
268 flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
269 flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
270 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
271 if (addr0 & 0x40000000)
272 flags |= IORESOURCE_PREFETCH
273 | PCI_BASE_ADDRESS_MEM_PREFETCH;
274 } else if (addr0 & 0x01000000)
275 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
276 return flags;
277}
278
279/* The of_device layer has translated all of the assigned-address properties
280 * into physical address resources, we only have to figure out the register
281 * mapping.
282 */
283static void pci_parse_of_addrs(struct of_device *op,
284 struct device_node *node,
285 struct pci_dev *dev)
286{
287 struct resource *op_res;
288 const u32 *addrs;
289 int proplen;
290
291 addrs = of_get_property(node, "assigned-addresses", &proplen);
292 if (!addrs)
293 return;
David S. Miller5840fc62007-05-22 01:24:14 -0700294 if (ofpci_verbose)
295 printk(" parse addresses (%d bytes) @ %p\n",
296 proplen, addrs);
David S. Millera2fb23a2007-02-28 23:35:04 -0800297 op_res = &op->resource[0];
298 for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
299 struct resource *res;
300 unsigned long flags;
301 int i;
302
303 flags = pci_parse_of_flags(addrs[0]);
304 if (!flags)
305 continue;
306 i = addrs[0] & 0xff;
David S. Miller5840fc62007-05-22 01:24:14 -0700307 if (ofpci_verbose)
308 printk(" start: %lx, end: %lx, i: %x\n",
309 op_res->start, op_res->end, i);
David S. Millera2fb23a2007-02-28 23:35:04 -0800310
311 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
312 res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
313 } else if (i == dev->rom_base_reg) {
314 res = &dev->resource[PCI_ROM_RESOURCE];
315 flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
316 } else {
317 printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
318 continue;
319 }
320 res->start = op_res->start;
321 res->end = op_res->end;
322 res->flags = flags;
323 res->name = pci_name(dev);
324 }
325}
326
327struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
328 struct device_node *node,
David S. Millerc26d3c02008-05-01 01:12:40 -0700329 struct pci_bus *bus, int devfn)
David S. Millera2fb23a2007-02-28 23:35:04 -0800330{
331 struct dev_archdata *sd;
David S. Millerae05f872008-08-29 22:42:34 -0700332 struct of_device *op;
David S. Millera2fb23a2007-02-28 23:35:04 -0800333 struct pci_dev *dev;
334 const char *type;
David S. Miller01f94c42007-03-04 12:53:19 -0800335 u32 class;
David S. Millera2fb23a2007-02-28 23:35:04 -0800336
David S. Miller26e63852007-05-10 02:16:27 -0700337 dev = alloc_pci_dev();
David S. Millera2fb23a2007-02-28 23:35:04 -0800338 if (!dev)
339 return NULL;
340
341 sd = &dev->dev.archdata;
342 sd->iommu = pbm->iommu;
343 sd->stc = &pbm->stc;
344 sd->host_controller = pbm;
345 sd->prom_node = node;
David S. Millerae05f872008-08-29 22:42:34 -0700346 sd->op = op = of_find_device_by_node(node);
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700347 sd->numa_node = pbm->numa_node;
David S. Millera2fb23a2007-02-28 23:35:04 -0800348
David S. Millerae05f872008-08-29 22:42:34 -0700349 sd = &op->dev.archdata;
David S. Millerad7ad572007-07-27 22:39:14 -0700350 sd->iommu = pbm->iommu;
351 sd->stc = &pbm->stc;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700352 sd->numa_node = pbm->numa_node;
David S. Millerad7ad572007-07-27 22:39:14 -0700353
David S. Millerae05f872008-08-29 22:42:34 -0700354 if (!strcmp(node->name, "ebus"))
355 of_propagate_archdata(op);
356
David S. Millera2fb23a2007-02-28 23:35:04 -0800357 type = of_get_property(node, "device_type", NULL);
358 if (type == NULL)
359 type = "";
360
David S. Miller5840fc62007-05-22 01:24:14 -0700361 if (ofpci_verbose)
362 printk(" create device, devfn: %x, type: %s\n",
363 devfn, type);
David S. Millera2fb23a2007-02-28 23:35:04 -0800364
365 dev->bus = bus;
366 dev->sysdata = node;
367 dev->dev.parent = bus->bridge;
368 dev->dev.bus = &pci_bus_type;
369 dev->devfn = devfn;
370 dev->multifunction = 0; /* maybe a lie? */
371
David S. Millerc26d3c02008-05-01 01:12:40 -0700372 dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
373 dev->device = of_getintprop_default(node, "device-id", 0xffff);
374 dev->subsystem_vendor =
375 of_getintprop_default(node, "subsystem-vendor-id", 0);
376 dev->subsystem_device =
377 of_getintprop_default(node, "subsystem-id", 0);
David S. Millera2fb23a2007-02-28 23:35:04 -0800378
David S. Millerc26d3c02008-05-01 01:12:40 -0700379 dev->cfg_size = pci_cfg_space_size(dev);
David S. Miller01f94c42007-03-04 12:53:19 -0800380
David S. Millerc26d3c02008-05-01 01:12:40 -0700381 /* We can't actually use the firmware value, we have
382 * to read what is in the register right now. One
383 * reason is that in the case of IDE interfaces the
384 * firmware can sample the value before the the IDE
385 * interface is programmed into native mode.
386 */
387 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
388 dev->class = class >> 8;
389 dev->revision = class & 0xff;
David S. Miller28f57e72007-03-12 19:40:26 -0700390
Greg Kroah-Hartman2222c312008-05-02 06:02:41 +0200391 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
David S. Millerc26d3c02008-05-01 01:12:40 -0700392 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
393
David S. Miller5840fc62007-05-22 01:24:14 -0700394 if (ofpci_verbose)
395 printk(" class: 0x%x device name: %s\n",
396 dev->class, pci_name(dev));
David S. Millera2fb23a2007-02-28 23:35:04 -0800397
David S. Miller861fe902007-05-02 17:31:36 -0700398 /* I have seen IDE devices which will not respond to
399 * the bmdma simplex check reads if bus mastering is
400 * disabled.
401 */
402 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
403 pci_set_master(dev);
404
David S. Millera2fb23a2007-02-28 23:35:04 -0800405 dev->current_state = 4; /* unknown power state */
406 dev->error_state = pci_channel_io_normal;
407
David S. Millerc26d3c02008-05-01 01:12:40 -0700408 if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
409 /* a PCI-PCI bridge */
David S. Millera2fb23a2007-02-28 23:35:04 -0800410 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
411 dev->rom_base_reg = PCI_ROM_ADDRESS1;
David S. Millerc26d3c02008-05-01 01:12:40 -0700412 } else if (!strcmp(type, "cardbus")) {
413 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
David S. Millera2fb23a2007-02-28 23:35:04 -0800414 } else {
David S. Millerc26d3c02008-05-01 01:12:40 -0700415 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
416 dev->rom_base_reg = PCI_ROM_ADDRESS;
David S. Millera2fb23a2007-02-28 23:35:04 -0800417
David S. Millerc26d3c02008-05-01 01:12:40 -0700418 dev->irq = sd->op->irqs[0];
419 if (dev->irq == 0xffffffff)
420 dev->irq = PCI_IRQ_NONE;
David S. Millera2fb23a2007-02-28 23:35:04 -0800421 }
David S. Millerc26d3c02008-05-01 01:12:40 -0700422
David S. Millera2fb23a2007-02-28 23:35:04 -0800423 pci_parse_of_addrs(sd->op, node, dev);
424
David S. Miller5840fc62007-05-22 01:24:14 -0700425 if (ofpci_verbose)
426 printk(" adding to system ...\n");
David S. Millera2fb23a2007-02-28 23:35:04 -0800427
428 pci_device_add(dev, bus);
429
430 return dev;
431}
432
David S. Millera6009dd2007-05-07 00:01:38 -0700433static void __devinit apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
David S. Miller01f94c42007-03-04 12:53:19 -0800434{
435 u32 idx, first, last;
436
437 first = 8;
438 last = 0;
439 for (idx = 0; idx < 8; idx++) {
440 if ((map & (1 << idx)) != 0) {
441 if (first > idx)
442 first = idx;
443 if (last < idx)
444 last = idx;
445 }
446 }
447
448 *first_p = first;
449 *last_p = last;
450}
451
David S. Millerf16537b2007-05-11 14:29:43 -0700452static void pci_resource_adjust(struct resource *res,
453 struct resource *root)
David S. Miller0bae5f82007-03-08 22:42:19 -0800454{
455 res->start += root->start;
456 res->end += root->start;
457}
458
David S. Miller8c2786c2007-06-07 21:59:44 -0700459/* For PCI bus devices which lack a 'ranges' property we interrogate
460 * the config space values to set the resources, just like the generic
461 * Linux PCI probing code does.
462 */
463static void __devinit pci_cfg_fake_ranges(struct pci_dev *dev,
464 struct pci_bus *bus,
465 struct pci_pbm_info *pbm)
466{
467 struct resource *res;
468 u8 io_base_lo, io_limit_lo;
469 u16 mem_base_lo, mem_limit_lo;
470 unsigned long base, limit;
471
472 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
473 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
474 base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
475 limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
476
477 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
478 u16 io_base_hi, io_limit_hi;
479
480 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
481 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
482 base |= (io_base_hi << 16);
483 limit |= (io_limit_hi << 16);
484 }
485
486 res = bus->resource[0];
487 if (base <= limit) {
488 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
489 if (!res->start)
490 res->start = base;
491 if (!res->end)
492 res->end = limit + 0xfff;
493 pci_resource_adjust(res, &pbm->io_space);
494 }
495
496 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
497 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
498 base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
499 limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
500
501 res = bus->resource[1];
502 if (base <= limit) {
503 res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
504 IORESOURCE_MEM);
505 res->start = base;
506 res->end = limit + 0xfffff;
507 pci_resource_adjust(res, &pbm->mem_space);
508 }
509
510 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
511 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
512 base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
513 limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
514
515 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
516 u32 mem_base_hi, mem_limit_hi;
517
518 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
519 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
520
521 /*
522 * Some bridges set the base > limit by default, and some
523 * (broken) BIOSes do not initialize them. If we find
524 * this, just assume they are not being used.
525 */
526 if (mem_base_hi <= mem_limit_hi) {
527 base |= ((long) mem_base_hi) << 32;
528 limit |= ((long) mem_limit_hi) << 32;
529 }
530 }
531
532 res = bus->resource[2];
533 if (base <= limit) {
534 res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
535 IORESOURCE_MEM | IORESOURCE_PREFETCH);
536 res->start = base;
537 res->end = limit + 0xfffff;
538 pci_resource_adjust(res, &pbm->mem_space);
539 }
540}
541
David S. Miller01f94c42007-03-04 12:53:19 -0800542/* Cook up fake bus resources for SUNW,simba PCI bridges which lack
543 * a proper 'ranges' property.
544 */
David S. Millera6009dd2007-05-07 00:01:38 -0700545static void __devinit apb_fake_ranges(struct pci_dev *dev,
546 struct pci_bus *bus,
547 struct pci_pbm_info *pbm)
David S. Miller01f94c42007-03-04 12:53:19 -0800548{
549 struct resource *res;
550 u32 first, last;
551 u8 map;
552
553 pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
554 apb_calc_first_last(map, &first, &last);
555 res = bus->resource[0];
556 res->start = (first << 21);
557 res->end = (last << 21) + ((1 << 21) - 1);
558 res->flags = IORESOURCE_IO;
David S. Miller0bae5f82007-03-08 22:42:19 -0800559 pci_resource_adjust(res, &pbm->io_space);
David S. Miller01f94c42007-03-04 12:53:19 -0800560
561 pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
562 apb_calc_first_last(map, &first, &last);
563 res = bus->resource[1];
564 res->start = (first << 21);
565 res->end = (last << 21) + ((1 << 21) - 1);
566 res->flags = IORESOURCE_MEM;
David S. Miller0bae5f82007-03-08 22:42:19 -0800567 pci_resource_adjust(res, &pbm->mem_space);
David S. Miller01f94c42007-03-04 12:53:19 -0800568}
569
David S. Millera6009dd2007-05-07 00:01:38 -0700570static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
571 struct device_node *node,
572 struct pci_bus *bus);
David S. Millera2fb23a2007-02-28 23:35:04 -0800573
574#define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
575
David S. Millera6009dd2007-05-07 00:01:38 -0700576static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
577 struct device_node *node,
578 struct pci_dev *dev)
David S. Millera2fb23a2007-02-28 23:35:04 -0800579{
580 struct pci_bus *bus;
581 const u32 *busrange, *ranges;
David S. Miller01f94c42007-03-04 12:53:19 -0800582 int len, i, simba;
David S. Millera2fb23a2007-02-28 23:35:04 -0800583 struct resource *res;
584 unsigned int flags;
585 u64 size;
586
David S. Miller5840fc62007-05-22 01:24:14 -0700587 if (ofpci_verbose)
588 printk("of_scan_pci_bridge(%s)\n", node->full_name);
David S. Millera2fb23a2007-02-28 23:35:04 -0800589
590 /* parse bus-range property */
591 busrange = of_get_property(node, "bus-range", &len);
592 if (busrange == NULL || len != 8) {
593 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
594 node->full_name);
595 return;
596 }
597 ranges = of_get_property(node, "ranges", &len);
David S. Miller01f94c42007-03-04 12:53:19 -0800598 simba = 0;
David S. Millera2fb23a2007-02-28 23:35:04 -0800599 if (ranges == NULL) {
David S. Millera165b422007-03-29 01:50:16 -0700600 const char *model = of_get_property(node, "model", NULL);
David S. Miller8c2786c2007-06-07 21:59:44 -0700601 if (model && !strcmp(model, "SUNW,simba"))
David S. Miller01f94c42007-03-04 12:53:19 -0800602 simba = 1;
David S. Millera2fb23a2007-02-28 23:35:04 -0800603 }
604
605 bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
606 if (!bus) {
607 printk(KERN_ERR "Failed to create pci bus for %s\n",
608 node->full_name);
609 return;
610 }
611
612 bus->primary = dev->bus->number;
613 bus->subordinate = busrange[1];
614 bus->bridge_ctl = 0;
615
David S. Miller01f94c42007-03-04 12:53:19 -0800616 /* parse ranges property, or cook one up by hand for Simba */
David S. Millera2fb23a2007-02-28 23:35:04 -0800617 /* PCI #address-cells == 3 and #size-cells == 2 always */
618 res = &dev->resource[PCI_BRIDGE_RESOURCES];
619 for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
620 res->flags = 0;
621 bus->resource[i] = res;
622 ++res;
623 }
David S. Miller01f94c42007-03-04 12:53:19 -0800624 if (simba) {
625 apb_fake_ranges(dev, bus, pbm);
David S. Miller8c2786c2007-06-07 21:59:44 -0700626 goto after_ranges;
627 } else if (ranges == NULL) {
628 pci_cfg_fake_ranges(dev, bus, pbm);
629 goto after_ranges;
David S. Miller01f94c42007-03-04 12:53:19 -0800630 }
David S. Millera2fb23a2007-02-28 23:35:04 -0800631 i = 1;
632 for (; len >= 32; len -= 32, ranges += 8) {
633 struct resource *root;
634
635 flags = pci_parse_of_flags(ranges[0]);
636 size = GET_64BIT(ranges, 6);
637 if (flags == 0 || size == 0)
638 continue;
639 if (flags & IORESOURCE_IO) {
640 res = bus->resource[0];
641 if (res->flags) {
642 printk(KERN_ERR "PCI: ignoring extra I/O range"
643 " for bridge %s\n", node->full_name);
644 continue;
645 }
646 root = &pbm->io_space;
647 } else {
648 if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
649 printk(KERN_ERR "PCI: too many memory ranges"
650 " for bridge %s\n", node->full_name);
651 continue;
652 }
653 res = bus->resource[i];
654 ++i;
655 root = &pbm->mem_space;
656 }
657
658 res->start = GET_64BIT(ranges, 1);
659 res->end = res->start + size - 1;
660 res->flags = flags;
661
662 /* Another way to implement this would be to add an of_device
663 * layer routine that can calculate a resource for a given
664 * range property value in a PCI device.
665 */
David S. Miller0bae5f82007-03-08 22:42:19 -0800666 pci_resource_adjust(res, root);
David S. Millera2fb23a2007-02-28 23:35:04 -0800667 }
David S. Miller8c2786c2007-06-07 21:59:44 -0700668after_ranges:
David S. Millera2fb23a2007-02-28 23:35:04 -0800669 sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
670 bus->number);
David S. Miller5840fc62007-05-22 01:24:14 -0700671 if (ofpci_verbose)
672 printk(" bus name: %s\n", bus->name);
David S. Millera2fb23a2007-02-28 23:35:04 -0800673
674 pci_of_scan_bus(pbm, node, bus);
675}
676
David S. Millera6009dd2007-05-07 00:01:38 -0700677static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
678 struct device_node *node,
679 struct pci_bus *bus)
David S. Millera2fb23a2007-02-28 23:35:04 -0800680{
681 struct device_node *child;
682 const u32 *reg;
David S. Miller2cc73452007-09-12 10:15:59 +0200683 int reglen, devfn, prev_devfn;
David S. Millera2fb23a2007-02-28 23:35:04 -0800684 struct pci_dev *dev;
685
David S. Miller5840fc62007-05-22 01:24:14 -0700686 if (ofpci_verbose)
687 printk("PCI: scan_bus[%s] bus no %d\n",
688 node->full_name, bus->number);
David S. Millera2fb23a2007-02-28 23:35:04 -0800689
690 child = NULL;
David S. Miller2cc73452007-09-12 10:15:59 +0200691 prev_devfn = -1;
David S. Millera2fb23a2007-02-28 23:35:04 -0800692 while ((child = of_get_next_child(node, child)) != NULL) {
David S. Miller5840fc62007-05-22 01:24:14 -0700693 if (ofpci_verbose)
694 printk(" * %s\n", child->full_name);
David S. Millera2fb23a2007-02-28 23:35:04 -0800695 reg = of_get_property(child, "reg", &reglen);
696 if (reg == NULL || reglen < 20)
697 continue;
David S. Miller2cc73452007-09-12 10:15:59 +0200698
David S. Millera2fb23a2007-02-28 23:35:04 -0800699 devfn = (reg[0] >> 8) & 0xff;
700
David S. Miller2cc73452007-09-12 10:15:59 +0200701 /* This is a workaround for some device trees
702 * which list PCI devices twice. On the V100
703 * for example, device number 3 is listed twice.
704 * Once as "pm" and once again as "lomp".
705 */
706 if (devfn == prev_devfn)
707 continue;
708 prev_devfn = devfn;
709
David S. Millera2fb23a2007-02-28 23:35:04 -0800710 /* create a new pci_dev for this device */
David S. Millerc26d3c02008-05-01 01:12:40 -0700711 dev = of_create_pci_dev(pbm, child, bus, devfn);
David S. Millera2fb23a2007-02-28 23:35:04 -0800712 if (!dev)
713 continue;
David S. Miller5840fc62007-05-22 01:24:14 -0700714 if (ofpci_verbose)
715 printk("PCI: dev header type: %x\n",
716 dev->hdr_type);
David S. Millera2fb23a2007-02-28 23:35:04 -0800717
718 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
719 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
720 of_scan_pci_bridge(pbm, child, dev);
721 }
722}
723
724static ssize_t
725show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
726{
727 struct pci_dev *pdev;
728 struct device_node *dp;
729
730 pdev = to_pci_dev(dev);
731 dp = pdev->dev.archdata.prom_node;
732
733 return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
734}
735
736static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
737
738static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
739{
740 struct pci_dev *dev;
David S. Millera378fd02007-03-01 11:46:13 -0800741 struct pci_bus *child_bus;
David S. Millera2fb23a2007-02-28 23:35:04 -0800742 int err;
743
744 list_for_each_entry(dev, &bus->devices, bus_list) {
745 /* we don't really care if we can create this file or
746 * not, but we need to assign the result of the call
747 * or the world will fall under alien invasion and
748 * everybody will be frozen on a spaceship ready to be
749 * eaten on alpha centauri by some green and jelly
750 * humanoid.
751 */
752 err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
753 }
David S. Millera378fd02007-03-01 11:46:13 -0800754 list_for_each_entry(child_bus, &bus->children, node)
755 pci_bus_register_of_sysfs(child_bus);
David S. Millera2fb23a2007-02-28 23:35:04 -0800756}
757
David S. Millera6009dd2007-05-07 00:01:38 -0700758struct pci_bus * __devinit pci_scan_one_pbm(struct pci_pbm_info *pbm)
David S. Millera2fb23a2007-02-28 23:35:04 -0800759{
David S. Millera2fb23a2007-02-28 23:35:04 -0800760 struct device_node *node = pbm->prom_node;
761 struct pci_bus *bus;
762
763 printk("PCI: Scanning PBM %s\n", node->full_name);
764
765 /* XXX parent device? XXX */
David S. Millerf1cd8de2007-05-07 23:24:05 -0700766 bus = pci_create_bus(NULL, pbm->pci_first_busno, pbm->pci_ops, pbm);
David S. Millera2fb23a2007-02-28 23:35:04 -0800767 if (!bus) {
768 printk(KERN_ERR "Failed to create bus for %s\n",
769 node->full_name);
770 return NULL;
771 }
772 bus->secondary = pbm->pci_first_busno;
773 bus->subordinate = pbm->pci_last_busno;
774
775 bus->resource[0] = &pbm->io_space;
776 bus->resource[1] = &pbm->mem_space;
777
778 pci_of_scan_bus(pbm, node, bus);
779 pci_bus_add_devices(bus);
780 pci_bus_register_of_sysfs(bus);
781
782 return bus;
783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785static void __init pci_scan_each_controller_bus(void)
786{
David S. Miller34768bc2007-05-07 23:06:27 -0700787 struct pci_pbm_info *pbm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
David S. Miller6d19c882008-08-30 02:30:24 -0700789 for (pbm = pci_pbm_root; pbm; pbm = pbm->next) {
790 if (pbm->scan_bus)
791 pbm->scan_bus(pbm);
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795static int __init pcibios_init(void)
796{
797 pci_controller_probe();
David S. Miller34768bc2007-05-07 23:06:27 -0700798 if (pci_pbm_root == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return 0;
800
801 pci_scan_each_controller_bus();
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return 0;
804}
805
806subsys_initcall(pcibios_init);
807
Robert Reiff6b45da2007-04-12 13:47:37 -0700808void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 struct pci_pbm_info *pbm = pbus->sysdata;
811
812 /* Generic PCI bus probing sets these to point at
813 * &io{port,mem}_resouce which is wrong for us.
814 */
815 pbus->resource[0] = &pbm->io_space;
816 pbus->resource[1] = &pbm->mem_space;
817}
818
David S. Miller085ae412005-08-08 13:19:08 -0700819struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 struct pci_pbm_info *pbm = pdev->bus->sysdata;
David S. Miller085ae412005-08-08 13:19:08 -0700822 struct resource *root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
David S. Miller085ae412005-08-08 13:19:08 -0700824 if (r->flags & IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 root = &pbm->io_space;
David S. Miller085ae412005-08-08 13:19:08 -0700826 if (r->flags & IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 root = &pbm->mem_space;
828
David S. Miller085ae412005-08-08 13:19:08 -0700829 return root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
832void pcibios_update_irq(struct pci_dev *pdev, int irq)
833{
834}
835
836void pcibios_align_resource(void *data, struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700837 resource_size_t size, resource_size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839}
840
David S. Millera2fb23a2007-02-28 23:35:04 -0800841int pcibios_enable_device(struct pci_dev *dev, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
David S. Millera2fb23a2007-02-28 23:35:04 -0800843 u16 cmd, oldcmd;
844 int i;
845
846 pci_read_config_word(dev, PCI_COMMAND, &cmd);
847 oldcmd = cmd;
848
849 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
850 struct resource *res = &dev->resource[i];
851
852 /* Only set up the requested stuff */
853 if (!(mask & (1<<i)))
854 continue;
855
856 if (res->flags & IORESOURCE_IO)
857 cmd |= PCI_COMMAND_IO;
858 if (res->flags & IORESOURCE_MEM)
859 cmd |= PCI_COMMAND_MEMORY;
860 }
861
862 if (cmd != oldcmd) {
863 printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
864 pci_name(dev), cmd);
865 /* Enable the appropriate bits in the PCI command register. */
866 pci_write_config_word(dev, PCI_COMMAND, cmd);
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869}
870
871void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
872 struct resource *res)
873{
874 struct pci_pbm_info *pbm = pdev->bus->sysdata;
875 struct resource zero_res, *root;
876
877 zero_res.start = 0;
878 zero_res.end = 0;
879 zero_res.flags = res->flags;
880
881 if (res->flags & IORESOURCE_IO)
882 root = &pbm->io_space;
883 else
884 root = &pbm->mem_space;
885
David S. Miller0bae5f82007-03-08 22:42:19 -0800886 pci_resource_adjust(&zero_res, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 region->start = res->start - zero_res.start;
889 region->end = res->end - zero_res.start;
890}
David S. Miller5fdfd422006-04-17 13:34:44 -0700891EXPORT_SYMBOL(pcibios_resource_to_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
894 struct pci_bus_region *region)
895{
896 struct pci_pbm_info *pbm = pdev->bus->sysdata;
897 struct resource *root;
898
899 res->start = region->start;
900 res->end = region->end;
901
902 if (res->flags & IORESOURCE_IO)
903 root = &pbm->io_space;
904 else
905 root = &pbm->mem_space;
906
David S. Miller0bae5f82007-03-08 22:42:19 -0800907 pci_resource_adjust(res, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
Keith Owens41290c12005-08-24 16:06:25 +1000909EXPORT_SYMBOL(pcibios_bus_to_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Robert Reiff6b45da2007-04-12 13:47:37 -0700911char * __devinit pcibios_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return str;
914}
915
916/* Platform support for /proc/bus/pci/X/Y mmap()s. */
917
918/* If the user uses a host-bridge as the PCI device, he may use
919 * this to perform a raw mmap() of the I/O or MEM space behind
920 * that controller.
921 *
922 * This can be useful for execution of x86 PCI bios initialization code
923 * on a PCI card, like the xfree86 int10 stuff does.
924 */
925static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
926 enum pci_mmap_state mmap_state)
927{
David S. Millera2fb23a2007-02-28 23:35:04 -0800928 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 unsigned long space_size, user_offset, user_size;
930
David S. Miller3875c5c2007-03-08 22:52:11 -0800931 if (mmap_state == pci_mmap_io) {
932 space_size = (pbm->io_space.end -
933 pbm->io_space.start) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 } else {
David S. Miller3875c5c2007-03-08 22:52:11 -0800935 space_size = (pbm->mem_space.end -
936 pbm->mem_space.start) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
939 /* Make sure the request is in range. */
940 user_offset = vma->vm_pgoff << PAGE_SHIFT;
941 user_size = vma->vm_end - vma->vm_start;
942
943 if (user_offset >= space_size ||
944 (user_offset + user_size) > space_size)
945 return -EINVAL;
946
David S. Miller3875c5c2007-03-08 22:52:11 -0800947 if (mmap_state == pci_mmap_io) {
948 vma->vm_pgoff = (pbm->io_space.start +
949 user_offset) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 } else {
David S. Miller3875c5c2007-03-08 22:52:11 -0800951 vma->vm_pgoff = (pbm->mem_space.start +
952 user_offset) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954
955 return 0;
956}
957
David S. Millerbbe0b5e2007-10-11 15:41:01 -0700958/* Adjust vm_pgoff of VMA such that it is the physical page offset
959 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 *
961 * Basically, the user finds the base address for his device which he wishes
962 * to mmap. They read the 32-bit value from the config space base register,
963 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
964 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
965 *
966 * Returns negative error code on failure, zero on success.
967 */
David S. Millerbbe0b5e2007-10-11 15:41:01 -0700968static int __pci_mmap_make_offset(struct pci_dev *pdev,
969 struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 enum pci_mmap_state mmap_state)
971{
David S. Millerbbe0b5e2007-10-11 15:41:01 -0700972 unsigned long user_paddr, user_size;
973 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
David S. Millerbbe0b5e2007-10-11 15:41:01 -0700975 /* First compute the physical address in vma->vm_pgoff,
976 * making sure the user offset is within range in the
977 * appropriate PCI space.
978 */
979 err = __pci_mmap_make_offset_bus(pdev, vma, mmap_state);
980 if (err)
981 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
David S. Millerbbe0b5e2007-10-11 15:41:01 -0700983 /* If this is a mapping on a host bridge, any address
984 * is OK.
985 */
986 if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
987 return err;
988
989 /* Otherwise make sure it's in the range for one of the
990 * device's resources.
991 */
992 user_paddr = vma->vm_pgoff << PAGE_SHIFT;
993 user_size = vma->vm_end - vma->vm_start;
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
David S. Millerbbe0b5e2007-10-11 15:41:01 -0700996 struct resource *rp = &pdev->resource[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 /* Active? */
999 if (!rp->flags)
1000 continue;
1001
1002 /* Same type? */
1003 if (i == PCI_ROM_RESOURCE) {
1004 if (mmap_state != pci_mmap_mem)
1005 continue;
1006 } else {
1007 if ((mmap_state == pci_mmap_io &&
1008 (rp->flags & IORESOURCE_IO) == 0) ||
1009 (mmap_state == pci_mmap_mem &&
1010 (rp->flags & IORESOURCE_MEM) == 0))
1011 continue;
1012 }
1013
David S. Millerbbe0b5e2007-10-11 15:41:01 -07001014 if ((rp->start <= user_paddr) &&
1015 (user_paddr + user_size) <= (rp->end + 1UL))
1016 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018
David S. Millerbbe0b5e2007-10-11 15:41:01 -07001019 if (i > PCI_ROM_RESOURCE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return -EINVAL;
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return 0;
1023}
1024
1025/* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
1026 * mapping.
1027 */
1028static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
1029 enum pci_mmap_state mmap_state)
1030{
1031 vma->vm_flags |= (VM_IO | VM_RESERVED);
1032}
1033
1034/* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
1035 * device mapping.
1036 */
1037static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
1038 enum pci_mmap_state mmap_state)
1039{
David S. Millera7a6cac2005-09-01 21:51:26 -07001040 /* Our io_remap_pfn_range takes care of this, do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043/* Perform the actual remap of the pages for a PCI device mapping, as appropriate
1044 * for this architecture. The region in the process to map is described by vm_start
1045 * and vm_end members of VMA, the base physical address is found in vm_pgoff.
1046 * The pci device structure is provided so that architectures may make mapping
1047 * decisions on a per-device or per-bus basis.
1048 *
1049 * Returns a negative error code on failure, zero on success.
1050 */
1051int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
1052 enum pci_mmap_state mmap_state,
1053 int write_combine)
1054{
1055 int ret;
1056
1057 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
1058 if (ret < 0)
1059 return ret;
1060
1061 __pci_mmap_set_flags(dev, vma, mmap_state);
1062 __pci_mmap_set_pgprot(dev, vma, mmap_state);
1063
David S. Miller14778d92006-03-21 02:29:39 -08001064 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 ret = io_remap_pfn_range(vma, vma->vm_start,
1066 vma->vm_pgoff,
1067 vma->vm_end - vma->vm_start,
1068 vma->vm_page_prot);
1069 if (ret)
1070 return ret;
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return 0;
1073}
1074
David S. Millerc1b1a5f2008-03-19 04:52:48 -07001075#ifdef CONFIG_NUMA
1076int pcibus_to_node(struct pci_bus *pbus)
1077{
1078 struct pci_pbm_info *pbm = pbus->sysdata;
1079
1080 return pbm->numa_node;
1081}
1082EXPORT_SYMBOL(pcibus_to_node);
1083#endif
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085/* Return the domain nuber for this pci bus */
1086
1087int pci_domain_nr(struct pci_bus *pbus)
1088{
1089 struct pci_pbm_info *pbm = pbus->sysdata;
1090 int ret;
1091
1092 if (pbm == NULL || pbm->parent == NULL) {
1093 ret = -ENXIO;
1094 } else {
David S. Miller6c108f12007-05-07 23:49:01 -07001095 ret = pbm->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097
1098 return ret;
1099}
1100EXPORT_SYMBOL(pci_domain_nr);
1101
David S. Miller35a17eb2007-02-10 17:41:02 -08001102#ifdef CONFIG_PCI_MSI
1103int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
1104{
David S. Millera2fb23a2007-02-28 23:35:04 -08001105 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
David S. Millere9870c42007-05-07 23:28:50 -07001106 int virt_irq;
David S. Miller35a17eb2007-02-10 17:41:02 -08001107
David S. Millere9870c42007-05-07 23:28:50 -07001108 if (!pbm->setup_msi_irq)
David S. Miller35a17eb2007-02-10 17:41:02 -08001109 return -EINVAL;
1110
David S. Millere9870c42007-05-07 23:28:50 -07001111 return pbm->setup_msi_irq(&virt_irq, pdev, desc);
David S. Miller35a17eb2007-02-10 17:41:02 -08001112}
1113
1114void arch_teardown_msi_irq(unsigned int virt_irq)
1115{
David S. Millerabfd3362007-02-26 09:40:34 -08001116 struct msi_desc *entry = get_irq_msi(virt_irq);
David S. Miller35a17eb2007-02-10 17:41:02 -08001117 struct pci_dev *pdev = entry->dev;
David S. Millera2fb23a2007-02-28 23:35:04 -08001118 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
David S. Miller35a17eb2007-02-10 17:41:02 -08001119
David S. Millere9870c42007-05-07 23:28:50 -07001120 if (!pbm->teardown_msi_irq)
David S. Miller35a17eb2007-02-10 17:41:02 -08001121 return;
1122
David S. Millere9870c42007-05-07 23:28:50 -07001123 return pbm->teardown_msi_irq(virt_irq, pdev);
David S. Miller35a17eb2007-02-10 17:41:02 -08001124}
1125#endif /* !(CONFIG_PCI_MSI) */
1126
David S. Millerf6d0f9e2007-03-01 18:09:18 -08001127struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
1128{
David S. Millera2fb23a2007-02-28 23:35:04 -08001129 return pdev->dev.archdata.prom_node;
David S. Millerf6d0f9e2007-03-01 18:09:18 -08001130}
1131EXPORT_SYMBOL(pci_device_to_OF_node);
1132
David S. Millerad7ad572007-07-27 22:39:14 -07001133static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
1134{
1135 struct pci_dev *ali_isa_bridge;
1136 u8 val;
1137
1138 /* ALI sound chips generate 31-bits of DMA, a special register
1139 * determines what bit 31 is emitted as.
1140 */
1141 ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
1142 PCI_DEVICE_ID_AL_M1533,
1143 NULL);
1144
1145 pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
1146 if (set_bit)
1147 val |= 0x01;
1148 else
1149 val &= ~0x01;
1150 pci_write_config_byte(ali_isa_bridge, 0x7e, val);
1151 pci_dev_put(ali_isa_bridge);
1152}
1153
1154int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)
1155{
1156 u64 dma_addr_mask;
1157
1158 if (pdev == NULL) {
1159 dma_addr_mask = 0xffffffff;
1160 } else {
1161 struct iommu *iommu = pdev->dev.archdata.iommu;
1162
1163 dma_addr_mask = iommu->dma_addr_mask;
1164
1165 if (pdev->vendor == PCI_VENDOR_ID_AL &&
1166 pdev->device == PCI_DEVICE_ID_AL_M5451 &&
1167 device_mask == 0x7fffffff) {
1168 ali_sound_dma_hack(pdev,
1169 (dma_addr_mask & 0x80000000) != 0);
1170 return 1;
1171 }
1172 }
1173
1174 if (device_mask >= (1UL << 32UL))
1175 return 0;
1176
1177 return (device_mask & dma_addr_mask) == dma_addr_mask;
1178}
1179
David S. Millerbcea1db2007-12-25 02:20:33 -08001180void pci_resource_to_user(const struct pci_dev *pdev, int bar,
1181 const struct resource *rp, resource_size_t *start,
1182 resource_size_t *end)
1183{
1184 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1185 unsigned long offset;
1186
1187 if (rp->flags & IORESOURCE_IO)
1188 offset = pbm->io_space.start;
1189 else
1190 offset = pbm->mem_space.start;
1191
1192 *start = rp->start - offset;
1193 *end = rp->end - offset;
1194}