blob: ec0d12a48c2bf9a70c1a39c7a0de68d486dfa7ef [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>
17#include <linux/smp_lock.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>
21
22#include <asm/uaccess.h>
23#include <asm/pbm.h>
24#include <asm/pgtable.h>
25#include <asm/irq.h>
26#include <asm/ebus.h>
27#include <asm/isa.h>
David S. Millere87dc352006-06-21 18:18:47 -070028#include <asm/prom.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 -070032unsigned long pci_memspace_mask = 0xffffffffUL;
33
34#ifndef CONFIG_PCI
35/* A "nop" PCI implementation. */
36asmlinkage int sys_pciconfig_read(unsigned long bus, unsigned long dfn,
37 unsigned long off, unsigned long len,
38 unsigned char *buf)
39{
40 return 0;
41}
42asmlinkage int sys_pciconfig_write(unsigned long bus, unsigned long dfn,
43 unsigned long off, unsigned long len,
44 unsigned char *buf)
45{
46 return 0;
47}
48#else
49
50/* List of all PCI controllers found in the system. */
51struct pci_controller_info *pci_controller_root = NULL;
52
53/* Each PCI controller found gets a unique index. */
54int pci_num_controllers = 0;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056volatile int pci_poke_in_progress;
57volatile int pci_poke_cpu = -1;
58volatile int pci_poke_faulted;
59
60static DEFINE_SPINLOCK(pci_poke_lock);
61
62void pci_config_read8(u8 *addr, u8 *ret)
63{
64 unsigned long flags;
65 u8 byte;
66
67 spin_lock_irqsave(&pci_poke_lock, flags);
68 pci_poke_cpu = smp_processor_id();
69 pci_poke_in_progress = 1;
70 pci_poke_faulted = 0;
71 __asm__ __volatile__("membar #Sync\n\t"
72 "lduba [%1] %2, %0\n\t"
73 "membar #Sync"
74 : "=r" (byte)
75 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
76 : "memory");
77 pci_poke_in_progress = 0;
78 pci_poke_cpu = -1;
79 if (!pci_poke_faulted)
80 *ret = byte;
81 spin_unlock_irqrestore(&pci_poke_lock, flags);
82}
83
84void pci_config_read16(u16 *addr, u16 *ret)
85{
86 unsigned long flags;
87 u16 word;
88
89 spin_lock_irqsave(&pci_poke_lock, flags);
90 pci_poke_cpu = smp_processor_id();
91 pci_poke_in_progress = 1;
92 pci_poke_faulted = 0;
93 __asm__ __volatile__("membar #Sync\n\t"
94 "lduha [%1] %2, %0\n\t"
95 "membar #Sync"
96 : "=r" (word)
97 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
98 : "memory");
99 pci_poke_in_progress = 0;
100 pci_poke_cpu = -1;
101 if (!pci_poke_faulted)
102 *ret = word;
103 spin_unlock_irqrestore(&pci_poke_lock, flags);
104}
105
106void pci_config_read32(u32 *addr, u32 *ret)
107{
108 unsigned long flags;
109 u32 dword;
110
111 spin_lock_irqsave(&pci_poke_lock, flags);
112 pci_poke_cpu = smp_processor_id();
113 pci_poke_in_progress = 1;
114 pci_poke_faulted = 0;
115 __asm__ __volatile__("membar #Sync\n\t"
116 "lduwa [%1] %2, %0\n\t"
117 "membar #Sync"
118 : "=r" (dword)
119 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
120 : "memory");
121 pci_poke_in_progress = 0;
122 pci_poke_cpu = -1;
123 if (!pci_poke_faulted)
124 *ret = dword;
125 spin_unlock_irqrestore(&pci_poke_lock, flags);
126}
127
128void pci_config_write8(u8 *addr, u8 val)
129{
130 unsigned long flags;
131
132 spin_lock_irqsave(&pci_poke_lock, flags);
133 pci_poke_cpu = smp_processor_id();
134 pci_poke_in_progress = 1;
135 pci_poke_faulted = 0;
136 __asm__ __volatile__("membar #Sync\n\t"
137 "stba %0, [%1] %2\n\t"
138 "membar #Sync"
139 : /* no outputs */
140 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
141 : "memory");
142 pci_poke_in_progress = 0;
143 pci_poke_cpu = -1;
144 spin_unlock_irqrestore(&pci_poke_lock, flags);
145}
146
147void pci_config_write16(u16 *addr, u16 val)
148{
149 unsigned long flags;
150
151 spin_lock_irqsave(&pci_poke_lock, flags);
152 pci_poke_cpu = smp_processor_id();
153 pci_poke_in_progress = 1;
154 pci_poke_faulted = 0;
155 __asm__ __volatile__("membar #Sync\n\t"
156 "stha %0, [%1] %2\n\t"
157 "membar #Sync"
158 : /* no outputs */
159 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
160 : "memory");
161 pci_poke_in_progress = 0;
162 pci_poke_cpu = -1;
163 spin_unlock_irqrestore(&pci_poke_lock, flags);
164}
165
166void pci_config_write32(u32 *addr, u32 val)
167{
168 unsigned long flags;
169
170 spin_lock_irqsave(&pci_poke_lock, flags);
171 pci_poke_cpu = smp_processor_id();
172 pci_poke_in_progress = 1;
173 pci_poke_faulted = 0;
174 __asm__ __volatile__("membar #Sync\n\t"
175 "stwa %0, [%1] %2\n\t"
176 "membar #Sync"
177 : /* no outputs */
178 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
179 : "memory");
180 pci_poke_in_progress = 0;
181 pci_poke_cpu = -1;
182 spin_unlock_irqrestore(&pci_poke_lock, flags);
183}
184
185/* Probe for all PCI controllers in the system. */
David S. Millere87dc352006-06-21 18:18:47 -0700186extern void sabre_init(struct device_node *, const char *);
187extern void psycho_init(struct device_node *, const char *);
188extern void schizo_init(struct device_node *, const char *);
189extern void schizo_plus_init(struct device_node *, const char *);
190extern void tomatillo_init(struct device_node *, const char *);
191extern void sun4v_pci_init(struct device_node *, const char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193static struct {
194 char *model_name;
David S. Millere87dc352006-06-21 18:18:47 -0700195 void (*init)(struct device_node *, const char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196} pci_controller_table[] __initdata = {
197 { "SUNW,sabre", sabre_init },
198 { "pci108e,a000", sabre_init },
199 { "pci108e,a001", sabre_init },
200 { "SUNW,psycho", psycho_init },
201 { "pci108e,8000", psycho_init },
202 { "SUNW,schizo", schizo_init },
203 { "pci108e,8001", schizo_init },
204 { "SUNW,schizo+", schizo_plus_init },
205 { "pci108e,8002", schizo_plus_init },
206 { "SUNW,tomatillo", tomatillo_init },
207 { "pci108e,a801", tomatillo_init },
David S. Miller8f6a93a2006-02-09 21:32:07 -0800208 { "SUNW,sun4v-pci", sun4v_pci_init },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210#define PCI_NUM_CONTROLLER_TYPES (sizeof(pci_controller_table) / \
211 sizeof(pci_controller_table[0]))
212
David S. Millere87dc352006-06-21 18:18:47 -0700213static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 int i;
216
217 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
218 if (!strncmp(model_name,
219 pci_controller_table[i].model_name,
220 namelen)) {
David S. Millere87dc352006-06-21 18:18:47 -0700221 pci_controller_table[i].init(dp, model_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return 1;
223 }
224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 return 0;
227}
228
David S. Millere87dc352006-06-21 18:18:47 -0700229static int __init pci_is_controller(const char *model_name, int namelen, struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 int i;
232
233 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
234 if (!strncmp(model_name,
235 pci_controller_table[i].model_name,
236 namelen)) {
237 return 1;
238 }
239 }
240 return 0;
241}
242
David S. Millere87dc352006-06-21 18:18:47 -0700243static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
David S. Millere87dc352006-06-21 18:18:47 -0700245 struct device_node *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 int count = 0;
247
David S. Millere87dc352006-06-21 18:18:47 -0700248 for_each_node_by_name(dp, "pci") {
249 struct property *prop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 int len;
251
David S. Millere87dc352006-06-21 18:18:47 -0700252 prop = of_find_property(dp, "model", &len);
253 if (!prop)
254 prop = of_find_property(dp, "compatible", &len);
255
256 if (prop) {
257 const char *model = prop->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 int item_len = 0;
259
260 /* Our value may be a multi-valued string in the
261 * case of some compatible properties. For sanity,
David S. Millere87dc352006-06-21 18:18:47 -0700262 * only try the first one.
263 */
264 while (model[item_len] && len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 len--;
266 item_len++;
267 }
268
David S. Millere87dc352006-06-21 18:18:47 -0700269 if (handler(model, item_len, dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 count++;
271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
274 return count;
275}
276
277
278/* Is there some PCI controller in the system? */
279int __init pcic_present(void)
280{
281 return pci_controller_scan(pci_is_controller);
282}
283
David S. Miller8f6a93a2006-02-09 21:32:07 -0800284struct pci_iommu_ops *pci_iommu_ops;
285EXPORT_SYMBOL(pci_iommu_ops);
286
287extern struct pci_iommu_ops pci_sun4u_iommu_ops,
288 pci_sun4v_iommu_ops;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/* Find each controller in the system, attach and initialize
291 * software state structure for each and link into the
292 * pci_controller_root. Setup the controller enough such
293 * that bus scanning can be done.
294 */
295static void __init pci_controller_probe(void)
296{
David S. Miller8f6a93a2006-02-09 21:32:07 -0800297 if (tlb_type == hypervisor)
298 pci_iommu_ops = &pci_sun4v_iommu_ops;
299 else
300 pci_iommu_ops = &pci_sun4u_iommu_ops;
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 printk("PCI: Probing for controllers.\n");
303
304 pci_controller_scan(pci_controller_init);
305}
306
David S. Millera2fb23a2007-02-28 23:35:04 -0800307static unsigned long pci_parse_of_flags(u32 addr0)
308{
309 unsigned long flags = 0;
310
311 if (addr0 & 0x02000000) {
312 flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
313 flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
314 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
315 if (addr0 & 0x40000000)
316 flags |= IORESOURCE_PREFETCH
317 | PCI_BASE_ADDRESS_MEM_PREFETCH;
318 } else if (addr0 & 0x01000000)
319 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
320 return flags;
321}
322
323/* The of_device layer has translated all of the assigned-address properties
324 * into physical address resources, we only have to figure out the register
325 * mapping.
326 */
327static void pci_parse_of_addrs(struct of_device *op,
328 struct device_node *node,
329 struct pci_dev *dev)
330{
331 struct resource *op_res;
332 const u32 *addrs;
333 int proplen;
334
335 addrs = of_get_property(node, "assigned-addresses", &proplen);
336 if (!addrs)
337 return;
338 printk(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
339 op_res = &op->resource[0];
340 for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
341 struct resource *res;
342 unsigned long flags;
343 int i;
344
345 flags = pci_parse_of_flags(addrs[0]);
346 if (!flags)
347 continue;
348 i = addrs[0] & 0xff;
349 printk(" start: %lx, end: %lx, i: %x\n",
350 op_res->start, op_res->end, i);
351
352 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
353 res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
354 } else if (i == dev->rom_base_reg) {
355 res = &dev->resource[PCI_ROM_RESOURCE];
356 flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
357 } else {
358 printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
359 continue;
360 }
361 res->start = op_res->start;
362 res->end = op_res->end;
363 res->flags = flags;
364 res->name = pci_name(dev);
365 }
366}
367
368struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
369 struct device_node *node,
370 struct pci_bus *bus, int devfn)
371{
372 struct dev_archdata *sd;
373 struct pci_dev *dev;
374 const char *type;
375
376 dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
377 if (!dev)
378 return NULL;
379
380 sd = &dev->dev.archdata;
381 sd->iommu = pbm->iommu;
382 sd->stc = &pbm->stc;
383 sd->host_controller = pbm;
384 sd->prom_node = node;
385 sd->op = of_find_device_by_node(node);
386 sd->msi_num = 0xffffffff;
387
388 type = of_get_property(node, "device_type", NULL);
389 if (type == NULL)
390 type = "";
391
392 printk(" create device, devfn: %x, type: %s\n", devfn, type);
393
394 dev->bus = bus;
395 dev->sysdata = node;
396 dev->dev.parent = bus->bridge;
397 dev->dev.bus = &pci_bus_type;
398 dev->devfn = devfn;
399 dev->multifunction = 0; /* maybe a lie? */
400
401 dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
402 dev->device = of_getintprop_default(node, "device-id", 0xffff);
403 dev->subsystem_vendor =
404 of_getintprop_default(node, "subsystem-vendor-id", 0);
405 dev->subsystem_device =
406 of_getintprop_default(node, "subsystem-id", 0);
407
408 dev->cfg_size = pci_cfg_space_size(dev);
409
410 sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
411 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
412 dev->class = of_getintprop_default(node, "class-code", 0);
413
414 printk(" class: 0x%x\n", dev->class);
415
416 dev->current_state = 4; /* unknown power state */
417 dev->error_state = pci_channel_io_normal;
418
419 if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
420 /* a PCI-PCI bridge */
421 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
422 dev->rom_base_reg = PCI_ROM_ADDRESS1;
423 } else if (!strcmp(type, "cardbus")) {
424 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
425 } else {
426 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
427 dev->rom_base_reg = PCI_ROM_ADDRESS;
428
429 dev->irq = sd->op->irqs[0];
430 if (dev->irq == 0xffffffff)
431 dev->irq = PCI_IRQ_NONE;
432 }
433
434 pci_parse_of_addrs(sd->op, node, dev);
435
436 printk(" adding to system ...\n");
437
438 pci_device_add(dev, bus);
439
440 return dev;
441}
442
443static void __init pci_of_scan_bus(struct pci_pbm_info *pbm,
444 struct device_node *node,
445 struct pci_bus *bus);
446
447#define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
448
449void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
450 struct device_node *node,
451 struct pci_dev *dev)
452{
453 struct pci_bus *bus;
454 const u32 *busrange, *ranges;
455 int len, i;
456 struct resource *res;
457 unsigned int flags;
458 u64 size;
459
460 printk("of_scan_pci_bridge(%s)\n", node->full_name);
461
462 /* parse bus-range property */
463 busrange = of_get_property(node, "bus-range", &len);
464 if (busrange == NULL || len != 8) {
465 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
466 node->full_name);
467 return;
468 }
469 ranges = of_get_property(node, "ranges", &len);
470 if (ranges == NULL) {
471 printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
472 node->full_name);
473 return;
474 }
475
476 bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
477 if (!bus) {
478 printk(KERN_ERR "Failed to create pci bus for %s\n",
479 node->full_name);
480 return;
481 }
482
483 bus->primary = dev->bus->number;
484 bus->subordinate = busrange[1];
485 bus->bridge_ctl = 0;
486
487 /* parse ranges property */
488 /* PCI #address-cells == 3 and #size-cells == 2 always */
489 res = &dev->resource[PCI_BRIDGE_RESOURCES];
490 for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
491 res->flags = 0;
492 bus->resource[i] = res;
493 ++res;
494 }
495 i = 1;
496 for (; len >= 32; len -= 32, ranges += 8) {
497 struct resource *root;
498
499 flags = pci_parse_of_flags(ranges[0]);
500 size = GET_64BIT(ranges, 6);
501 if (flags == 0 || size == 0)
502 continue;
503 if (flags & IORESOURCE_IO) {
504 res = bus->resource[0];
505 if (res->flags) {
506 printk(KERN_ERR "PCI: ignoring extra I/O range"
507 " for bridge %s\n", node->full_name);
508 continue;
509 }
510 root = &pbm->io_space;
511 } else {
512 if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
513 printk(KERN_ERR "PCI: too many memory ranges"
514 " for bridge %s\n", node->full_name);
515 continue;
516 }
517 res = bus->resource[i];
518 ++i;
519 root = &pbm->mem_space;
520 }
521
522 res->start = GET_64BIT(ranges, 1);
523 res->end = res->start + size - 1;
524 res->flags = flags;
525
526 /* Another way to implement this would be to add an of_device
527 * layer routine that can calculate a resource for a given
528 * range property value in a PCI device.
529 */
530 pbm->parent->resource_adjust(dev, res, root);
531 }
532 sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
533 bus->number);
534 printk(" bus name: %s\n", bus->name);
535
536 pci_of_scan_bus(pbm, node, bus);
537}
538
539static void __init pci_of_scan_bus(struct pci_pbm_info *pbm,
540 struct device_node *node,
541 struct pci_bus *bus)
542{
543 struct device_node *child;
544 const u32 *reg;
545 int reglen, devfn;
546 struct pci_dev *dev;
547
548 printk("PCI: scan_bus[%s] bus no %d\n",
549 node->full_name, bus->number);
550
551 child = NULL;
552 while ((child = of_get_next_child(node, child)) != NULL) {
553 printk(" * %s\n", child->full_name);
554 reg = of_get_property(child, "reg", &reglen);
555 if (reg == NULL || reglen < 20)
556 continue;
557 devfn = (reg[0] >> 8) & 0xff;
558
559 /* create a new pci_dev for this device */
560 dev = of_create_pci_dev(pbm, child, bus, devfn);
561 if (!dev)
562 continue;
563 printk("PCI: dev header type: %x\n", dev->hdr_type);
564
565 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
566 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
567 of_scan_pci_bridge(pbm, child, dev);
568 }
569}
570
571static ssize_t
572show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
573{
574 struct pci_dev *pdev;
575 struct device_node *dp;
576
577 pdev = to_pci_dev(dev);
578 dp = pdev->dev.archdata.prom_node;
579
580 return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
581}
582
583static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
584
585static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
586{
587 struct pci_dev *dev;
588 int err;
589
590 list_for_each_entry(dev, &bus->devices, bus_list) {
591 /* we don't really care if we can create this file or
592 * not, but we need to assign the result of the call
593 * or the world will fall under alien invasion and
594 * everybody will be frozen on a spaceship ready to be
595 * eaten on alpha centauri by some green and jelly
596 * humanoid.
597 */
598 err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
599 }
600}
601
602struct pci_bus * __init pci_scan_one_pbm(struct pci_pbm_info *pbm)
603{
604 struct pci_controller_info *p = pbm->parent;
605 struct device_node *node = pbm->prom_node;
606 struct pci_bus *bus;
607
608 printk("PCI: Scanning PBM %s\n", node->full_name);
609
610 /* XXX parent device? XXX */
611 bus = pci_create_bus(NULL, pbm->pci_first_busno, p->pci_ops, pbm);
612 if (!bus) {
613 printk(KERN_ERR "Failed to create bus for %s\n",
614 node->full_name);
615 return NULL;
616 }
617 bus->secondary = pbm->pci_first_busno;
618 bus->subordinate = pbm->pci_last_busno;
619
620 bus->resource[0] = &pbm->io_space;
621 bus->resource[1] = &pbm->mem_space;
622
623 pci_of_scan_bus(pbm, node, bus);
624 pci_bus_add_devices(bus);
625 pci_bus_register_of_sysfs(bus);
626
627 return bus;
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630static void __init pci_scan_each_controller_bus(void)
631{
632 struct pci_controller_info *p;
633
634 for (p = pci_controller_root; p; p = p->next)
635 p->scan_bus(p);
636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638extern void power_init(void);
639
640static int __init pcibios_init(void)
641{
642 pci_controller_probe();
643 if (pci_controller_root == NULL)
644 return 0;
645
646 pci_scan_each_controller_bus();
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 isa_init();
649 ebus_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 power_init();
651
652 return 0;
653}
654
655subsys_initcall(pcibios_init);
656
Robert Reiff6b45da2007-04-12 13:47:37 -0700657void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct pci_pbm_info *pbm = pbus->sysdata;
660
661 /* Generic PCI bus probing sets these to point at
662 * &io{port,mem}_resouce which is wrong for us.
663 */
664 pbus->resource[0] = &pbm->io_space;
665 pbus->resource[1] = &pbm->mem_space;
666}
667
David S. Miller085ae412005-08-08 13:19:08 -0700668struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct pci_pbm_info *pbm = pdev->bus->sysdata;
David S. Miller085ae412005-08-08 13:19:08 -0700671 struct resource *root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
David S. Miller085ae412005-08-08 13:19:08 -0700673 if (r->flags & IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 root = &pbm->io_space;
David S. Miller085ae412005-08-08 13:19:08 -0700675 if (r->flags & IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 root = &pbm->mem_space;
677
David S. Miller085ae412005-08-08 13:19:08 -0700678 return root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681void pcibios_update_irq(struct pci_dev *pdev, int irq)
682{
683}
684
685void pcibios_align_resource(void *data, struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700686 resource_size_t size, resource_size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688}
689
David S. Millera2fb23a2007-02-28 23:35:04 -0800690int pcibios_enable_device(struct pci_dev *dev, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
David S. Millera2fb23a2007-02-28 23:35:04 -0800692 u16 cmd, oldcmd;
693 int i;
694
695 pci_read_config_word(dev, PCI_COMMAND, &cmd);
696 oldcmd = cmd;
697
698 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
699 struct resource *res = &dev->resource[i];
700
701 /* Only set up the requested stuff */
702 if (!(mask & (1<<i)))
703 continue;
704
705 if (res->flags & IORESOURCE_IO)
706 cmd |= PCI_COMMAND_IO;
707 if (res->flags & IORESOURCE_MEM)
708 cmd |= PCI_COMMAND_MEMORY;
709 }
710
711 if (cmd != oldcmd) {
712 printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
713 pci_name(dev), cmd);
714 /* Enable the appropriate bits in the PCI command register. */
715 pci_write_config_word(dev, PCI_COMMAND, cmd);
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return 0;
718}
719
720void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
721 struct resource *res)
722{
723 struct pci_pbm_info *pbm = pdev->bus->sysdata;
724 struct resource zero_res, *root;
725
726 zero_res.start = 0;
727 zero_res.end = 0;
728 zero_res.flags = res->flags;
729
730 if (res->flags & IORESOURCE_IO)
731 root = &pbm->io_space;
732 else
733 root = &pbm->mem_space;
734
735 pbm->parent->resource_adjust(pdev, &zero_res, root);
736
737 region->start = res->start - zero_res.start;
738 region->end = res->end - zero_res.start;
739}
David S. Miller5fdfd422006-04-17 13:34:44 -0700740EXPORT_SYMBOL(pcibios_resource_to_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
743 struct pci_bus_region *region)
744{
745 struct pci_pbm_info *pbm = pdev->bus->sysdata;
746 struct resource *root;
747
748 res->start = region->start;
749 res->end = region->end;
750
751 if (res->flags & IORESOURCE_IO)
752 root = &pbm->io_space;
753 else
754 root = &pbm->mem_space;
755
756 pbm->parent->resource_adjust(pdev, res, root);
757}
Keith Owens41290c12005-08-24 16:06:25 +1000758EXPORT_SYMBOL(pcibios_bus_to_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Robert Reiff6b45da2007-04-12 13:47:37 -0700760char * __devinit pcibios_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return str;
763}
764
765/* Platform support for /proc/bus/pci/X/Y mmap()s. */
766
767/* If the user uses a host-bridge as the PCI device, he may use
768 * this to perform a raw mmap() of the I/O or MEM space behind
769 * that controller.
770 *
771 * This can be useful for execution of x86 PCI bios initialization code
772 * on a PCI card, like the xfree86 int10 stuff does.
773 */
774static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
775 enum pci_mmap_state mmap_state)
776{
David S. Millera2fb23a2007-02-28 23:35:04 -0800777 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct pci_controller_info *p;
779 unsigned long space_size, user_offset, user_size;
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 p = pbm->parent;
782 if (p->pbms_same_domain) {
783 unsigned long lowest, highest;
784
785 lowest = ~0UL; highest = 0UL;
786 if (mmap_state == pci_mmap_io) {
787 if (p->pbm_A.io_space.flags) {
788 lowest = p->pbm_A.io_space.start;
789 highest = p->pbm_A.io_space.end + 1;
790 }
791 if (p->pbm_B.io_space.flags) {
792 if (lowest > p->pbm_B.io_space.start)
793 lowest = p->pbm_B.io_space.start;
794 if (highest < p->pbm_B.io_space.end + 1)
795 highest = p->pbm_B.io_space.end + 1;
796 }
797 space_size = highest - lowest;
798 } else {
799 if (p->pbm_A.mem_space.flags) {
800 lowest = p->pbm_A.mem_space.start;
801 highest = p->pbm_A.mem_space.end + 1;
802 }
803 if (p->pbm_B.mem_space.flags) {
804 if (lowest > p->pbm_B.mem_space.start)
805 lowest = p->pbm_B.mem_space.start;
806 if (highest < p->pbm_B.mem_space.end + 1)
807 highest = p->pbm_B.mem_space.end + 1;
808 }
809 space_size = highest - lowest;
810 }
811 } else {
812 if (mmap_state == pci_mmap_io) {
813 space_size = (pbm->io_space.end -
814 pbm->io_space.start) + 1;
815 } else {
816 space_size = (pbm->mem_space.end -
817 pbm->mem_space.start) + 1;
818 }
819 }
820
821 /* Make sure the request is in range. */
822 user_offset = vma->vm_pgoff << PAGE_SHIFT;
823 user_size = vma->vm_end - vma->vm_start;
824
825 if (user_offset >= space_size ||
826 (user_offset + user_size) > space_size)
827 return -EINVAL;
828
829 if (p->pbms_same_domain) {
830 unsigned long lowest = ~0UL;
831
832 if (mmap_state == pci_mmap_io) {
833 if (p->pbm_A.io_space.flags)
834 lowest = p->pbm_A.io_space.start;
835 if (p->pbm_B.io_space.flags &&
836 lowest > p->pbm_B.io_space.start)
837 lowest = p->pbm_B.io_space.start;
838 } else {
839 if (p->pbm_A.mem_space.flags)
840 lowest = p->pbm_A.mem_space.start;
841 if (p->pbm_B.mem_space.flags &&
842 lowest > p->pbm_B.mem_space.start)
843 lowest = p->pbm_B.mem_space.start;
844 }
845 vma->vm_pgoff = (lowest + user_offset) >> PAGE_SHIFT;
846 } else {
847 if (mmap_state == pci_mmap_io) {
848 vma->vm_pgoff = (pbm->io_space.start +
849 user_offset) >> PAGE_SHIFT;
850 } else {
851 vma->vm_pgoff = (pbm->mem_space.start +
852 user_offset) >> PAGE_SHIFT;
853 }
854 }
855
856 return 0;
857}
858
859/* Adjust vm_pgoff of VMA such that it is the physical page offset corresponding
860 * to the 32-bit pci bus offset for DEV requested by the user.
861 *
862 * Basically, the user finds the base address for his device which he wishes
863 * to mmap. They read the 32-bit value from the config space base register,
864 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
865 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
866 *
867 * Returns negative error code on failure, zero on success.
868 */
869static int __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
870 enum pci_mmap_state mmap_state)
871{
872 unsigned long user_offset = vma->vm_pgoff << PAGE_SHIFT;
873 unsigned long user32 = user_offset & pci_memspace_mask;
874 unsigned long largest_base, this_base, addr32;
875 int i;
876
877 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
878 return __pci_mmap_make_offset_bus(dev, vma, mmap_state);
879
880 /* Figure out which base address this is for. */
881 largest_base = 0UL;
882 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
883 struct resource *rp = &dev->resource[i];
884
885 /* Active? */
886 if (!rp->flags)
887 continue;
888
889 /* Same type? */
890 if (i == PCI_ROM_RESOURCE) {
891 if (mmap_state != pci_mmap_mem)
892 continue;
893 } else {
894 if ((mmap_state == pci_mmap_io &&
895 (rp->flags & IORESOURCE_IO) == 0) ||
896 (mmap_state == pci_mmap_mem &&
897 (rp->flags & IORESOURCE_MEM) == 0))
898 continue;
899 }
900
901 this_base = rp->start;
902
903 addr32 = (this_base & PAGE_MASK) & pci_memspace_mask;
904
905 if (mmap_state == pci_mmap_io)
906 addr32 &= 0xffffff;
907
908 if (addr32 <= user32 && this_base > largest_base)
909 largest_base = this_base;
910 }
911
912 if (largest_base == 0UL)
913 return -EINVAL;
914
915 /* Now construct the final physical address. */
916 if (mmap_state == pci_mmap_io)
917 vma->vm_pgoff = (((largest_base & ~0xffffffUL) | user32) >> PAGE_SHIFT);
918 else
919 vma->vm_pgoff = (((largest_base & ~(pci_memspace_mask)) | user32) >> PAGE_SHIFT);
920
921 return 0;
922}
923
924/* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
925 * mapping.
926 */
927static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
928 enum pci_mmap_state mmap_state)
929{
930 vma->vm_flags |= (VM_IO | VM_RESERVED);
931}
932
933/* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
934 * device mapping.
935 */
936static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
937 enum pci_mmap_state mmap_state)
938{
David S. Millera7a6cac2005-09-01 21:51:26 -0700939 /* Our io_remap_pfn_range takes care of this, do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
942/* Perform the actual remap of the pages for a PCI device mapping, as appropriate
943 * for this architecture. The region in the process to map is described by vm_start
944 * and vm_end members of VMA, the base physical address is found in vm_pgoff.
945 * The pci device structure is provided so that architectures may make mapping
946 * decisions on a per-device or per-bus basis.
947 *
948 * Returns a negative error code on failure, zero on success.
949 */
950int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
951 enum pci_mmap_state mmap_state,
952 int write_combine)
953{
954 int ret;
955
956 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
957 if (ret < 0)
958 return ret;
959
960 __pci_mmap_set_flags(dev, vma, mmap_state);
961 __pci_mmap_set_pgprot(dev, vma, mmap_state);
962
David S. Miller14778d92006-03-21 02:29:39 -0800963 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 ret = io_remap_pfn_range(vma, vma->vm_start,
965 vma->vm_pgoff,
966 vma->vm_end - vma->vm_start,
967 vma->vm_page_prot);
968 if (ret)
969 return ret;
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return 0;
972}
973
974/* Return the domain nuber for this pci bus */
975
976int pci_domain_nr(struct pci_bus *pbus)
977{
978 struct pci_pbm_info *pbm = pbus->sysdata;
979 int ret;
980
981 if (pbm == NULL || pbm->parent == NULL) {
982 ret = -ENXIO;
983 } else {
984 struct pci_controller_info *p = pbm->parent;
985
986 ret = p->index;
987 if (p->pbms_same_domain == 0)
988 ret = ((ret << 1) +
989 ((pbm == &pbm->parent->pbm_B) ? 1 : 0));
990 }
991
992 return ret;
993}
994EXPORT_SYMBOL(pci_domain_nr);
995
David S. Miller35a17eb2007-02-10 17:41:02 -0800996#ifdef CONFIG_PCI_MSI
997int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
998{
David S. Millera2fb23a2007-02-28 23:35:04 -0800999 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
David S. Miller35a17eb2007-02-10 17:41:02 -08001000 struct pci_controller_info *p = pbm->parent;
1001 int virt_irq, err;
1002
1003 if (!pbm->msi_num || !p->setup_msi_irq)
1004 return -EINVAL;
1005
1006 err = p->setup_msi_irq(&virt_irq, pdev, desc);
1007 if (err < 0)
1008 return err;
1009
1010 return virt_irq;
1011}
1012
1013void arch_teardown_msi_irq(unsigned int virt_irq)
1014{
David S. Millerabfd3362007-02-26 09:40:34 -08001015 struct msi_desc *entry = get_irq_msi(virt_irq);
David S. Miller35a17eb2007-02-10 17:41:02 -08001016 struct pci_dev *pdev = entry->dev;
David S. Millera2fb23a2007-02-28 23:35:04 -08001017 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
David S. Miller35a17eb2007-02-10 17:41:02 -08001018 struct pci_controller_info *p = pbm->parent;
1019
1020 if (!pbm->msi_num || !p->setup_msi_irq)
1021 return;
1022
1023 return p->teardown_msi_irq(virt_irq, pdev);
1024}
1025#endif /* !(CONFIG_PCI_MSI) */
1026
David S. Millerf6d0f9e2007-03-01 18:09:18 -08001027struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
1028{
David S. Millera2fb23a2007-02-28 23:35:04 -08001029 return pdev->dev.archdata.prom_node;
David S. Millerf6d0f9e2007-03-01 18:09:18 -08001030}
1031EXPORT_SYMBOL(pci_device_to_OF_node);
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033#endif /* !(CONFIG_PCI) */