sparc: Kill EBUS driver layer.
[deliverable/linux.git] / arch / sparc64 / kernel / pci.c
1 /* pci.c: UltraSparc PCI controller support.
2 *
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)
6 *
7 * OF tree based PCI bus probing taken from the PowerPC port
8 * with minor modifications, see there for credits.
9 */
10
11 #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/pci.h>
18 #include <linux/msi.h>
19 #include <linux/irq.h>
20 #include <linux/init.h>
21 #include <linux/of.h>
22 #include <linux/of_device.h>
23
24 #include <asm/uaccess.h>
25 #include <asm/pgtable.h>
26 #include <asm/irq.h>
27 #include <asm/prom.h>
28 #include <asm/apb.h>
29
30 #include "pci_impl.h"
31
32 /* List of all PCI controllers found in the system. */
33 struct pci_pbm_info *pci_pbm_root = NULL;
34
35 /* Each PBM found gets a unique index. */
36 int pci_num_pbms = 0;
37
38 volatile int pci_poke_in_progress;
39 volatile int pci_poke_cpu = -1;
40 volatile int pci_poke_faulted;
41
42 static DEFINE_SPINLOCK(pci_poke_lock);
43
44 void 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
66 void 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
88 void 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
110 void 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
129 void 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
148 void 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. */
168 extern void sabre_init(struct device_node *, const char *);
169 extern void psycho_init(struct device_node *, const char *);
170 extern void schizo_init(struct device_node *, const char *);
171 extern void schizo_plus_init(struct device_node *, const char *);
172 extern void tomatillo_init(struct device_node *, const char *);
173 extern void sun4v_pci_init(struct device_node *, const char *);
174 extern void fire_pci_init(struct device_node *, const char *);
175
176 static struct {
177 char *model_name;
178 void (*init)(struct device_node *, const char *);
179 } pci_controller_table[] __initdata = {
180 { "SUNW,sabre", sabre_init },
181 { "pci108e,a000", sabre_init },
182 { "pci108e,a001", sabre_init },
183 { "SUNW,psycho", psycho_init },
184 { "pci108e,8000", psycho_init },
185 { "SUNW,schizo", schizo_init },
186 { "pci108e,8001", schizo_init },
187 { "SUNW,schizo+", schizo_plus_init },
188 { "pci108e,8002", schizo_plus_init },
189 { "SUNW,tomatillo", tomatillo_init },
190 { "pci108e,a801", tomatillo_init },
191 { "SUNW,sun4v-pci", sun4v_pci_init },
192 { "pciex108e,80f0", fire_pci_init },
193 };
194 #define PCI_NUM_CONTROLLER_TYPES ARRAY_SIZE(pci_controller_table)
195
196 static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
197 {
198 int i;
199
200 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
201 if (!strncmp(model_name,
202 pci_controller_table[i].model_name,
203 namelen)) {
204 pci_controller_table[i].init(dp, model_name);
205 return 1;
206 }
207 }
208
209 return 0;
210 }
211
212 static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
213 {
214 struct device_node *dp;
215 int count = 0;
216
217 for_each_node_by_name(dp, "pci") {
218 struct property *prop;
219 int len;
220
221 prop = of_find_property(dp, "model", &len);
222 if (!prop)
223 prop = of_find_property(dp, "compatible", &len);
224
225 if (prop) {
226 const char *model = prop->value;
227 int item_len = 0;
228
229 /* Our value may be a multi-valued string in the
230 * case of some compatible properties. For sanity,
231 * only try the first one.
232 */
233 while (model[item_len] && len) {
234 len--;
235 item_len++;
236 }
237
238 if (handler(model, item_len, dp))
239 count++;
240 }
241 }
242
243 return count;
244 }
245
246 /* Find each controller in the system, attach and initialize
247 * software state structure for each and link into the
248 * pci_pbm_root. Setup the controller enough such
249 * that bus scanning can be done.
250 */
251 static void __init pci_controller_probe(void)
252 {
253 printk("PCI: Probing for controllers.\n");
254
255 pci_controller_scan(pci_controller_init);
256 }
257
258 static int ofpci_verbose;
259
260 static int __init ofpci_debug(char *str)
261 {
262 int val = 0;
263
264 get_option(&str, &val);
265 if (val)
266 ofpci_verbose = 1;
267 return 1;
268 }
269
270 __setup("ofpci_debug=", ofpci_debug);
271
272 static unsigned long pci_parse_of_flags(u32 addr0)
273 {
274 unsigned long flags = 0;
275
276 if (addr0 & 0x02000000) {
277 flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
278 flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
279 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
280 if (addr0 & 0x40000000)
281 flags |= IORESOURCE_PREFETCH
282 | PCI_BASE_ADDRESS_MEM_PREFETCH;
283 } else if (addr0 & 0x01000000)
284 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
285 return flags;
286 }
287
288 /* The of_device layer has translated all of the assigned-address properties
289 * into physical address resources, we only have to figure out the register
290 * mapping.
291 */
292 static void pci_parse_of_addrs(struct of_device *op,
293 struct device_node *node,
294 struct pci_dev *dev)
295 {
296 struct resource *op_res;
297 const u32 *addrs;
298 int proplen;
299
300 addrs = of_get_property(node, "assigned-addresses", &proplen);
301 if (!addrs)
302 return;
303 if (ofpci_verbose)
304 printk(" parse addresses (%d bytes) @ %p\n",
305 proplen, addrs);
306 op_res = &op->resource[0];
307 for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
308 struct resource *res;
309 unsigned long flags;
310 int i;
311
312 flags = pci_parse_of_flags(addrs[0]);
313 if (!flags)
314 continue;
315 i = addrs[0] & 0xff;
316 if (ofpci_verbose)
317 printk(" start: %lx, end: %lx, i: %x\n",
318 op_res->start, op_res->end, i);
319
320 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
321 res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
322 } else if (i == dev->rom_base_reg) {
323 res = &dev->resource[PCI_ROM_RESOURCE];
324 flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
325 } else {
326 printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
327 continue;
328 }
329 res->start = op_res->start;
330 res->end = op_res->end;
331 res->flags = flags;
332 res->name = pci_name(dev);
333 }
334 }
335
336 struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
337 struct device_node *node,
338 struct pci_bus *bus, int devfn)
339 {
340 struct dev_archdata *sd;
341 struct of_device *op;
342 struct pci_dev *dev;
343 const char *type;
344 u32 class;
345
346 dev = alloc_pci_dev();
347 if (!dev)
348 return NULL;
349
350 sd = &dev->dev.archdata;
351 sd->iommu = pbm->iommu;
352 sd->stc = &pbm->stc;
353 sd->host_controller = pbm;
354 sd->prom_node = node;
355 sd->op = op = of_find_device_by_node(node);
356 sd->numa_node = pbm->numa_node;
357
358 sd = &op->dev.archdata;
359 sd->iommu = pbm->iommu;
360 sd->stc = &pbm->stc;
361 sd->numa_node = pbm->numa_node;
362
363 if (!strcmp(node->name, "ebus"))
364 of_propagate_archdata(op);
365
366 type = of_get_property(node, "device_type", NULL);
367 if (type == NULL)
368 type = "";
369
370 if (ofpci_verbose)
371 printk(" create device, devfn: %x, type: %s\n",
372 devfn, type);
373
374 dev->bus = bus;
375 dev->sysdata = node;
376 dev->dev.parent = bus->bridge;
377 dev->dev.bus = &pci_bus_type;
378 dev->devfn = devfn;
379 dev->multifunction = 0; /* maybe a lie? */
380
381 dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
382 dev->device = of_getintprop_default(node, "device-id", 0xffff);
383 dev->subsystem_vendor =
384 of_getintprop_default(node, "subsystem-vendor-id", 0);
385 dev->subsystem_device =
386 of_getintprop_default(node, "subsystem-id", 0);
387
388 dev->cfg_size = pci_cfg_space_size(dev);
389
390 /* We can't actually use the firmware value, we have
391 * to read what is in the register right now. One
392 * reason is that in the case of IDE interfaces the
393 * firmware can sample the value before the the IDE
394 * interface is programmed into native mode.
395 */
396 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
397 dev->class = class >> 8;
398 dev->revision = class & 0xff;
399
400 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
401 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
402
403 if (ofpci_verbose)
404 printk(" class: 0x%x device name: %s\n",
405 dev->class, pci_name(dev));
406
407 /* I have seen IDE devices which will not respond to
408 * the bmdma simplex check reads if bus mastering is
409 * disabled.
410 */
411 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
412 pci_set_master(dev);
413
414 dev->current_state = 4; /* unknown power state */
415 dev->error_state = pci_channel_io_normal;
416
417 if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
418 /* a PCI-PCI bridge */
419 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
420 dev->rom_base_reg = PCI_ROM_ADDRESS1;
421 } else if (!strcmp(type, "cardbus")) {
422 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
423 } else {
424 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
425 dev->rom_base_reg = PCI_ROM_ADDRESS;
426
427 dev->irq = sd->op->irqs[0];
428 if (dev->irq == 0xffffffff)
429 dev->irq = PCI_IRQ_NONE;
430 }
431
432 pci_parse_of_addrs(sd->op, node, dev);
433
434 if (ofpci_verbose)
435 printk(" adding to system ...\n");
436
437 pci_device_add(dev, bus);
438
439 return dev;
440 }
441
442 static void __devinit apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
443 {
444 u32 idx, first, last;
445
446 first = 8;
447 last = 0;
448 for (idx = 0; idx < 8; idx++) {
449 if ((map & (1 << idx)) != 0) {
450 if (first > idx)
451 first = idx;
452 if (last < idx)
453 last = idx;
454 }
455 }
456
457 *first_p = first;
458 *last_p = last;
459 }
460
461 static void pci_resource_adjust(struct resource *res,
462 struct resource *root)
463 {
464 res->start += root->start;
465 res->end += root->start;
466 }
467
468 /* For PCI bus devices which lack a 'ranges' property we interrogate
469 * the config space values to set the resources, just like the generic
470 * Linux PCI probing code does.
471 */
472 static void __devinit pci_cfg_fake_ranges(struct pci_dev *dev,
473 struct pci_bus *bus,
474 struct pci_pbm_info *pbm)
475 {
476 struct resource *res;
477 u8 io_base_lo, io_limit_lo;
478 u16 mem_base_lo, mem_limit_lo;
479 unsigned long base, limit;
480
481 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
482 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
483 base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
484 limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
485
486 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
487 u16 io_base_hi, io_limit_hi;
488
489 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
490 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
491 base |= (io_base_hi << 16);
492 limit |= (io_limit_hi << 16);
493 }
494
495 res = bus->resource[0];
496 if (base <= limit) {
497 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
498 if (!res->start)
499 res->start = base;
500 if (!res->end)
501 res->end = limit + 0xfff;
502 pci_resource_adjust(res, &pbm->io_space);
503 }
504
505 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
506 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
507 base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
508 limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
509
510 res = bus->resource[1];
511 if (base <= limit) {
512 res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
513 IORESOURCE_MEM);
514 res->start = base;
515 res->end = limit + 0xfffff;
516 pci_resource_adjust(res, &pbm->mem_space);
517 }
518
519 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
520 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
521 base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
522 limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
523
524 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
525 u32 mem_base_hi, mem_limit_hi;
526
527 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
528 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
529
530 /*
531 * Some bridges set the base > limit by default, and some
532 * (broken) BIOSes do not initialize them. If we find
533 * this, just assume they are not being used.
534 */
535 if (mem_base_hi <= mem_limit_hi) {
536 base |= ((long) mem_base_hi) << 32;
537 limit |= ((long) mem_limit_hi) << 32;
538 }
539 }
540
541 res = bus->resource[2];
542 if (base <= limit) {
543 res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
544 IORESOURCE_MEM | IORESOURCE_PREFETCH);
545 res->start = base;
546 res->end = limit + 0xfffff;
547 pci_resource_adjust(res, &pbm->mem_space);
548 }
549 }
550
551 /* Cook up fake bus resources for SUNW,simba PCI bridges which lack
552 * a proper 'ranges' property.
553 */
554 static void __devinit apb_fake_ranges(struct pci_dev *dev,
555 struct pci_bus *bus,
556 struct pci_pbm_info *pbm)
557 {
558 struct resource *res;
559 u32 first, last;
560 u8 map;
561
562 pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
563 apb_calc_first_last(map, &first, &last);
564 res = bus->resource[0];
565 res->start = (first << 21);
566 res->end = (last << 21) + ((1 << 21) - 1);
567 res->flags = IORESOURCE_IO;
568 pci_resource_adjust(res, &pbm->io_space);
569
570 pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
571 apb_calc_first_last(map, &first, &last);
572 res = bus->resource[1];
573 res->start = (first << 21);
574 res->end = (last << 21) + ((1 << 21) - 1);
575 res->flags = IORESOURCE_MEM;
576 pci_resource_adjust(res, &pbm->mem_space);
577 }
578
579 static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
580 struct device_node *node,
581 struct pci_bus *bus);
582
583 #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
584
585 static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
586 struct device_node *node,
587 struct pci_dev *dev)
588 {
589 struct pci_bus *bus;
590 const u32 *busrange, *ranges;
591 int len, i, simba;
592 struct resource *res;
593 unsigned int flags;
594 u64 size;
595
596 if (ofpci_verbose)
597 printk("of_scan_pci_bridge(%s)\n", node->full_name);
598
599 /* parse bus-range property */
600 busrange = of_get_property(node, "bus-range", &len);
601 if (busrange == NULL || len != 8) {
602 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
603 node->full_name);
604 return;
605 }
606 ranges = of_get_property(node, "ranges", &len);
607 simba = 0;
608 if (ranges == NULL) {
609 const char *model = of_get_property(node, "model", NULL);
610 if (model && !strcmp(model, "SUNW,simba"))
611 simba = 1;
612 }
613
614 bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
615 if (!bus) {
616 printk(KERN_ERR "Failed to create pci bus for %s\n",
617 node->full_name);
618 return;
619 }
620
621 bus->primary = dev->bus->number;
622 bus->subordinate = busrange[1];
623 bus->bridge_ctl = 0;
624
625 /* parse ranges property, or cook one up by hand for Simba */
626 /* PCI #address-cells == 3 and #size-cells == 2 always */
627 res = &dev->resource[PCI_BRIDGE_RESOURCES];
628 for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
629 res->flags = 0;
630 bus->resource[i] = res;
631 ++res;
632 }
633 if (simba) {
634 apb_fake_ranges(dev, bus, pbm);
635 goto after_ranges;
636 } else if (ranges == NULL) {
637 pci_cfg_fake_ranges(dev, bus, pbm);
638 goto after_ranges;
639 }
640 i = 1;
641 for (; len >= 32; len -= 32, ranges += 8) {
642 struct resource *root;
643
644 flags = pci_parse_of_flags(ranges[0]);
645 size = GET_64BIT(ranges, 6);
646 if (flags == 0 || size == 0)
647 continue;
648 if (flags & IORESOURCE_IO) {
649 res = bus->resource[0];
650 if (res->flags) {
651 printk(KERN_ERR "PCI: ignoring extra I/O range"
652 " for bridge %s\n", node->full_name);
653 continue;
654 }
655 root = &pbm->io_space;
656 } else {
657 if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
658 printk(KERN_ERR "PCI: too many memory ranges"
659 " for bridge %s\n", node->full_name);
660 continue;
661 }
662 res = bus->resource[i];
663 ++i;
664 root = &pbm->mem_space;
665 }
666
667 res->start = GET_64BIT(ranges, 1);
668 res->end = res->start + size - 1;
669 res->flags = flags;
670
671 /* Another way to implement this would be to add an of_device
672 * layer routine that can calculate a resource for a given
673 * range property value in a PCI device.
674 */
675 pci_resource_adjust(res, root);
676 }
677 after_ranges:
678 sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
679 bus->number);
680 if (ofpci_verbose)
681 printk(" bus name: %s\n", bus->name);
682
683 pci_of_scan_bus(pbm, node, bus);
684 }
685
686 static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
687 struct device_node *node,
688 struct pci_bus *bus)
689 {
690 struct device_node *child;
691 const u32 *reg;
692 int reglen, devfn, prev_devfn;
693 struct pci_dev *dev;
694
695 if (ofpci_verbose)
696 printk("PCI: scan_bus[%s] bus no %d\n",
697 node->full_name, bus->number);
698
699 child = NULL;
700 prev_devfn = -1;
701 while ((child = of_get_next_child(node, child)) != NULL) {
702 if (ofpci_verbose)
703 printk(" * %s\n", child->full_name);
704 reg = of_get_property(child, "reg", &reglen);
705 if (reg == NULL || reglen < 20)
706 continue;
707
708 devfn = (reg[0] >> 8) & 0xff;
709
710 /* This is a workaround for some device trees
711 * which list PCI devices twice. On the V100
712 * for example, device number 3 is listed twice.
713 * Once as "pm" and once again as "lomp".
714 */
715 if (devfn == prev_devfn)
716 continue;
717 prev_devfn = devfn;
718
719 /* create a new pci_dev for this device */
720 dev = of_create_pci_dev(pbm, child, bus, devfn);
721 if (!dev)
722 continue;
723 if (ofpci_verbose)
724 printk("PCI: dev header type: %x\n",
725 dev->hdr_type);
726
727 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
728 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
729 of_scan_pci_bridge(pbm, child, dev);
730 }
731 }
732
733 static ssize_t
734 show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
735 {
736 struct pci_dev *pdev;
737 struct device_node *dp;
738
739 pdev = to_pci_dev(dev);
740 dp = pdev->dev.archdata.prom_node;
741
742 return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
743 }
744
745 static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
746
747 static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
748 {
749 struct pci_dev *dev;
750 struct pci_bus *child_bus;
751 int err;
752
753 list_for_each_entry(dev, &bus->devices, bus_list) {
754 /* we don't really care if we can create this file or
755 * not, but we need to assign the result of the call
756 * or the world will fall under alien invasion and
757 * everybody will be frozen on a spaceship ready to be
758 * eaten on alpha centauri by some green and jelly
759 * humanoid.
760 */
761 err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
762 }
763 list_for_each_entry(child_bus, &bus->children, node)
764 pci_bus_register_of_sysfs(child_bus);
765 }
766
767 struct pci_bus * __devinit pci_scan_one_pbm(struct pci_pbm_info *pbm)
768 {
769 struct device_node *node = pbm->prom_node;
770 struct pci_bus *bus;
771
772 printk("PCI: Scanning PBM %s\n", node->full_name);
773
774 /* XXX parent device? XXX */
775 bus = pci_create_bus(NULL, pbm->pci_first_busno, pbm->pci_ops, pbm);
776 if (!bus) {
777 printk(KERN_ERR "Failed to create bus for %s\n",
778 node->full_name);
779 return NULL;
780 }
781 bus->secondary = pbm->pci_first_busno;
782 bus->subordinate = pbm->pci_last_busno;
783
784 bus->resource[0] = &pbm->io_space;
785 bus->resource[1] = &pbm->mem_space;
786
787 pci_of_scan_bus(pbm, node, bus);
788 pci_bus_add_devices(bus);
789 pci_bus_register_of_sysfs(bus);
790
791 return bus;
792 }
793
794 static void __init pci_scan_each_controller_bus(void)
795 {
796 struct pci_pbm_info *pbm;
797
798 for (pbm = pci_pbm_root; pbm; pbm = pbm->next)
799 pbm->scan_bus(pbm);
800 }
801
802 extern void power_init(void);
803
804 static int __init pcibios_init(void)
805 {
806 pci_controller_probe();
807 if (pci_pbm_root == NULL)
808 return 0;
809
810 pci_scan_each_controller_bus();
811
812 power_init();
813
814 return 0;
815 }
816
817 subsys_initcall(pcibios_init);
818
819 void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
820 {
821 struct pci_pbm_info *pbm = pbus->sysdata;
822
823 /* Generic PCI bus probing sets these to point at
824 * &io{port,mem}_resouce which is wrong for us.
825 */
826 pbus->resource[0] = &pbm->io_space;
827 pbus->resource[1] = &pbm->mem_space;
828 }
829
830 struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
831 {
832 struct pci_pbm_info *pbm = pdev->bus->sysdata;
833 struct resource *root = NULL;
834
835 if (r->flags & IORESOURCE_IO)
836 root = &pbm->io_space;
837 if (r->flags & IORESOURCE_MEM)
838 root = &pbm->mem_space;
839
840 return root;
841 }
842
843 void pcibios_update_irq(struct pci_dev *pdev, int irq)
844 {
845 }
846
847 void pcibios_align_resource(void *data, struct resource *res,
848 resource_size_t size, resource_size_t align)
849 {
850 }
851
852 int pcibios_enable_device(struct pci_dev *dev, int mask)
853 {
854 u16 cmd, oldcmd;
855 int i;
856
857 pci_read_config_word(dev, PCI_COMMAND, &cmd);
858 oldcmd = cmd;
859
860 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
861 struct resource *res = &dev->resource[i];
862
863 /* Only set up the requested stuff */
864 if (!(mask & (1<<i)))
865 continue;
866
867 if (res->flags & IORESOURCE_IO)
868 cmd |= PCI_COMMAND_IO;
869 if (res->flags & IORESOURCE_MEM)
870 cmd |= PCI_COMMAND_MEMORY;
871 }
872
873 if (cmd != oldcmd) {
874 printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
875 pci_name(dev), cmd);
876 /* Enable the appropriate bits in the PCI command register. */
877 pci_write_config_word(dev, PCI_COMMAND, cmd);
878 }
879 return 0;
880 }
881
882 void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
883 struct resource *res)
884 {
885 struct pci_pbm_info *pbm = pdev->bus->sysdata;
886 struct resource zero_res, *root;
887
888 zero_res.start = 0;
889 zero_res.end = 0;
890 zero_res.flags = res->flags;
891
892 if (res->flags & IORESOURCE_IO)
893 root = &pbm->io_space;
894 else
895 root = &pbm->mem_space;
896
897 pci_resource_adjust(&zero_res, root);
898
899 region->start = res->start - zero_res.start;
900 region->end = res->end - zero_res.start;
901 }
902 EXPORT_SYMBOL(pcibios_resource_to_bus);
903
904 void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
905 struct pci_bus_region *region)
906 {
907 struct pci_pbm_info *pbm = pdev->bus->sysdata;
908 struct resource *root;
909
910 res->start = region->start;
911 res->end = region->end;
912
913 if (res->flags & IORESOURCE_IO)
914 root = &pbm->io_space;
915 else
916 root = &pbm->mem_space;
917
918 pci_resource_adjust(res, root);
919 }
920 EXPORT_SYMBOL(pcibios_bus_to_resource);
921
922 char * __devinit pcibios_setup(char *str)
923 {
924 return str;
925 }
926
927 /* Platform support for /proc/bus/pci/X/Y mmap()s. */
928
929 /* If the user uses a host-bridge as the PCI device, he may use
930 * this to perform a raw mmap() of the I/O or MEM space behind
931 * that controller.
932 *
933 * This can be useful for execution of x86 PCI bios initialization code
934 * on a PCI card, like the xfree86 int10 stuff does.
935 */
936 static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
937 enum pci_mmap_state mmap_state)
938 {
939 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
940 unsigned long space_size, user_offset, user_size;
941
942 if (mmap_state == pci_mmap_io) {
943 space_size = (pbm->io_space.end -
944 pbm->io_space.start) + 1;
945 } else {
946 space_size = (pbm->mem_space.end -
947 pbm->mem_space.start) + 1;
948 }
949
950 /* Make sure the request is in range. */
951 user_offset = vma->vm_pgoff << PAGE_SHIFT;
952 user_size = vma->vm_end - vma->vm_start;
953
954 if (user_offset >= space_size ||
955 (user_offset + user_size) > space_size)
956 return -EINVAL;
957
958 if (mmap_state == pci_mmap_io) {
959 vma->vm_pgoff = (pbm->io_space.start +
960 user_offset) >> PAGE_SHIFT;
961 } else {
962 vma->vm_pgoff = (pbm->mem_space.start +
963 user_offset) >> PAGE_SHIFT;
964 }
965
966 return 0;
967 }
968
969 /* Adjust vm_pgoff of VMA such that it is the physical page offset
970 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
971 *
972 * Basically, the user finds the base address for his device which he wishes
973 * to mmap. They read the 32-bit value from the config space base register,
974 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
975 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
976 *
977 * Returns negative error code on failure, zero on success.
978 */
979 static int __pci_mmap_make_offset(struct pci_dev *pdev,
980 struct vm_area_struct *vma,
981 enum pci_mmap_state mmap_state)
982 {
983 unsigned long user_paddr, user_size;
984 int i, err;
985
986 /* First compute the physical address in vma->vm_pgoff,
987 * making sure the user offset is within range in the
988 * appropriate PCI space.
989 */
990 err = __pci_mmap_make_offset_bus(pdev, vma, mmap_state);
991 if (err)
992 return err;
993
994 /* If this is a mapping on a host bridge, any address
995 * is OK.
996 */
997 if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
998 return err;
999
1000 /* Otherwise make sure it's in the range for one of the
1001 * device's resources.
1002 */
1003 user_paddr = vma->vm_pgoff << PAGE_SHIFT;
1004 user_size = vma->vm_end - vma->vm_start;
1005
1006 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
1007 struct resource *rp = &pdev->resource[i];
1008
1009 /* Active? */
1010 if (!rp->flags)
1011 continue;
1012
1013 /* Same type? */
1014 if (i == PCI_ROM_RESOURCE) {
1015 if (mmap_state != pci_mmap_mem)
1016 continue;
1017 } else {
1018 if ((mmap_state == pci_mmap_io &&
1019 (rp->flags & IORESOURCE_IO) == 0) ||
1020 (mmap_state == pci_mmap_mem &&
1021 (rp->flags & IORESOURCE_MEM) == 0))
1022 continue;
1023 }
1024
1025 if ((rp->start <= user_paddr) &&
1026 (user_paddr + user_size) <= (rp->end + 1UL))
1027 break;
1028 }
1029
1030 if (i > PCI_ROM_RESOURCE)
1031 return -EINVAL;
1032
1033 return 0;
1034 }
1035
1036 /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
1037 * mapping.
1038 */
1039 static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
1040 enum pci_mmap_state mmap_state)
1041 {
1042 vma->vm_flags |= (VM_IO | VM_RESERVED);
1043 }
1044
1045 /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
1046 * device mapping.
1047 */
1048 static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
1049 enum pci_mmap_state mmap_state)
1050 {
1051 /* Our io_remap_pfn_range takes care of this, do nothing. */
1052 }
1053
1054 /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
1055 * for this architecture. The region in the process to map is described by vm_start
1056 * and vm_end members of VMA, the base physical address is found in vm_pgoff.
1057 * The pci device structure is provided so that architectures may make mapping
1058 * decisions on a per-device or per-bus basis.
1059 *
1060 * Returns a negative error code on failure, zero on success.
1061 */
1062 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
1063 enum pci_mmap_state mmap_state,
1064 int write_combine)
1065 {
1066 int ret;
1067
1068 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
1069 if (ret < 0)
1070 return ret;
1071
1072 __pci_mmap_set_flags(dev, vma, mmap_state);
1073 __pci_mmap_set_pgprot(dev, vma, mmap_state);
1074
1075 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1076 ret = io_remap_pfn_range(vma, vma->vm_start,
1077 vma->vm_pgoff,
1078 vma->vm_end - vma->vm_start,
1079 vma->vm_page_prot);
1080 if (ret)
1081 return ret;
1082
1083 return 0;
1084 }
1085
1086 #ifdef CONFIG_NUMA
1087 int pcibus_to_node(struct pci_bus *pbus)
1088 {
1089 struct pci_pbm_info *pbm = pbus->sysdata;
1090
1091 return pbm->numa_node;
1092 }
1093 EXPORT_SYMBOL(pcibus_to_node);
1094 #endif
1095
1096 /* Return the domain nuber for this pci bus */
1097
1098 int pci_domain_nr(struct pci_bus *pbus)
1099 {
1100 struct pci_pbm_info *pbm = pbus->sysdata;
1101 int ret;
1102
1103 if (pbm == NULL || pbm->parent == NULL) {
1104 ret = -ENXIO;
1105 } else {
1106 ret = pbm->index;
1107 }
1108
1109 return ret;
1110 }
1111 EXPORT_SYMBOL(pci_domain_nr);
1112
1113 #ifdef CONFIG_PCI_MSI
1114 int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
1115 {
1116 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1117 int virt_irq;
1118
1119 if (!pbm->setup_msi_irq)
1120 return -EINVAL;
1121
1122 return pbm->setup_msi_irq(&virt_irq, pdev, desc);
1123 }
1124
1125 void arch_teardown_msi_irq(unsigned int virt_irq)
1126 {
1127 struct msi_desc *entry = get_irq_msi(virt_irq);
1128 struct pci_dev *pdev = entry->dev;
1129 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1130
1131 if (!pbm->teardown_msi_irq)
1132 return;
1133
1134 return pbm->teardown_msi_irq(virt_irq, pdev);
1135 }
1136 #endif /* !(CONFIG_PCI_MSI) */
1137
1138 struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
1139 {
1140 return pdev->dev.archdata.prom_node;
1141 }
1142 EXPORT_SYMBOL(pci_device_to_OF_node);
1143
1144 static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
1145 {
1146 struct pci_dev *ali_isa_bridge;
1147 u8 val;
1148
1149 /* ALI sound chips generate 31-bits of DMA, a special register
1150 * determines what bit 31 is emitted as.
1151 */
1152 ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
1153 PCI_DEVICE_ID_AL_M1533,
1154 NULL);
1155
1156 pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
1157 if (set_bit)
1158 val |= 0x01;
1159 else
1160 val &= ~0x01;
1161 pci_write_config_byte(ali_isa_bridge, 0x7e, val);
1162 pci_dev_put(ali_isa_bridge);
1163 }
1164
1165 int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)
1166 {
1167 u64 dma_addr_mask;
1168
1169 if (pdev == NULL) {
1170 dma_addr_mask = 0xffffffff;
1171 } else {
1172 struct iommu *iommu = pdev->dev.archdata.iommu;
1173
1174 dma_addr_mask = iommu->dma_addr_mask;
1175
1176 if (pdev->vendor == PCI_VENDOR_ID_AL &&
1177 pdev->device == PCI_DEVICE_ID_AL_M5451 &&
1178 device_mask == 0x7fffffff) {
1179 ali_sound_dma_hack(pdev,
1180 (dma_addr_mask & 0x80000000) != 0);
1181 return 1;
1182 }
1183 }
1184
1185 if (device_mask >= (1UL << 32UL))
1186 return 0;
1187
1188 return (device_mask & dma_addr_mask) == dma_addr_mask;
1189 }
1190
1191 void pci_resource_to_user(const struct pci_dev *pdev, int bar,
1192 const struct resource *rp, resource_size_t *start,
1193 resource_size_t *end)
1194 {
1195 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1196 unsigned long offset;
1197
1198 if (rp->flags & IORESOURCE_IO)
1199 offset = pbm->io_space.start;
1200 else
1201 offset = pbm->mem_space.start;
1202
1203 *start = rp->start - offset;
1204 *end = rp->end - offset;
1205 }
This page took 0.06826 seconds and 5 git commands to generate.