1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/kernel.h>
27#include <linux/threads.h>
28#include <linux/pci.h>
29#include <linux/delay.h>
30#include <linux/string.h>
31#include <linux/init.h>
32#include <linux/bootmem.h>
33
34#include <asm/io.h>
35#include <asm/pgtable.h>
36#include <asm/irq.h>
37#include <asm/prom.h>
38#include <asm/machdep.h>
39#include <asm/pci-bridge.h>
40#include <asm/ppcdebug.h>
41#include <asm/naca.h>
42#include <asm/iommu.h>
43#include <asm/rtas.h>
44
45#include "open_pic.h"
46#include "pci.h"
47
48
49
50unsigned long io_page_mask;
51
52EXPORT_SYMBOL(io_page_mask);
53
54
55static int read_pci_config;
56static int write_pci_config;
57static int ibm_read_pci_config;
58static int ibm_write_pci_config;
59
60static int s7a_workaround;
61
62extern unsigned long pci_probe_only;
63
64static int rtas_read_config(struct device_node *dn, int where, int size, u32 *val)
65{
66 int returnval = -1;
67 unsigned long buid, addr;
68 int ret;
69
70 if (!dn)
71 return -2;
72
73 addr = (dn->busno << 16) | (dn->devfn << 8) | where;
74 buid = dn->phb->buid;
75 if (buid) {
76 ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval,
77 addr, buid >> 32, buid & 0xffffffff, size);
78 } else {
79 ret = rtas_call(read_pci_config, 2, 2, &returnval, addr, size);
80 }
81 *val = returnval;
82 return ret;
83}
84
85static int rtas_pci_read_config(struct pci_bus *bus,
86 unsigned int devfn,
87 int where, int size, u32 *val)
88{
89 struct device_node *busdn, *dn;
90
91 if (bus->self)
92 busdn = pci_device_to_OF_node(bus->self);
93 else
94 busdn = bus->sysdata;
95
96
97 for (dn = busdn->child; dn; dn = dn->sibling)
98 if (dn->devfn == devfn)
99 return rtas_read_config(dn, where, size, val);
100 return PCIBIOS_DEVICE_NOT_FOUND;
101}
102
103static int rtas_write_config(struct device_node *dn, int where, int size, u32 val)
104{
105 unsigned long buid, addr;
106 int ret;
107
108 if (!dn)
109 return -2;
110
111 addr = (dn->busno << 16) | (dn->devfn << 8) | where;
112 buid = dn->phb->buid;
113 if (buid) {
114 ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr, buid >> 32, buid & 0xffffffff, size, (ulong) val);
115 } else {
116 ret = rtas_call(write_pci_config, 3, 1, NULL, addr, size, (ulong)val);
117 }
118 return ret;
119}
120
121static int rtas_pci_write_config(struct pci_bus *bus,
122 unsigned int devfn,
123 int where, int size, u32 val)
124{
125 struct device_node *busdn, *dn;
126
127 if (bus->self)
128 busdn = pci_device_to_OF_node(bus->self);
129 else
130 busdn = bus->sysdata;
131
132
133 for (dn = busdn->child; dn; dn = dn->sibling)
134 if (dn->devfn == devfn)
135 return rtas_write_config(dn, where, size, val);
136 return PCIBIOS_DEVICE_NOT_FOUND;
137}
138
139struct pci_ops rtas_pci_ops = {
140 rtas_pci_read_config,
141 rtas_pci_write_config
142};
143
144
145
146
147
148
149
150
151
152int pci_read_irq_line(struct pci_dev *pci_dev)
153{
154 u8 intpin;
155 struct device_node *node;
156
157 pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &intpin);
158
159 if (intpin == 0) {
160 PPCDBG(PPCDBG_BUSWALK,"\tDevice: %s No Interrupt used by device.\n", pci_name(pci_dev));
161 return 0;
162 }
163
164 node = pci_device_to_OF_node(pci_dev);
165 if (node == NULL) {
166 PPCDBG(PPCDBG_BUSWALK,"\tDevice: %s Device Node not found.\n",
167 pci_name(pci_dev));
168 return -1;
169 }
170 if (node->n_intrs == 0) {
171 PPCDBG(PPCDBG_BUSWALK,"\tDevice: %s No Device OF interrupts defined.\n", pci_name(pci_dev));
172 return -1;
173 }
174 pci_dev->irq = node->intrs[0].line;
175
176 if (s7a_workaround) {
177 if (pci_dev->irq > 16)
178 pci_dev->irq -= 3;
179 }
180
181 pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, pci_dev->irq);
182
183 PPCDBG(PPCDBG_BUSWALK,"\tDevice: %s pci_dev->irq = 0x%02X\n",
184 pci_name(pci_dev), pci_dev->irq);
185 return 0;
186}
187EXPORT_SYMBOL(pci_read_irq_line);
188
189#define ISA_SPACE_MASK 0x1
190#define ISA_SPACE_IO 0x1
191
192static void pci_process_ISA_OF_ranges(struct device_node *isa_node,
193 unsigned long phb_io_base_phys,
194 void * phb_io_base_virt)
195{
196 struct isa_range *range;
197 unsigned long pci_addr;
198 unsigned int isa_addr;
199 unsigned int size;
200 int rlen = 0;
201
202 range = (struct isa_range *) get_property(isa_node, "ranges", &rlen);
203 if (rlen < sizeof(struct isa_range)) {
204 printk(KERN_ERR "unexpected isa range size: %s\n",
205 __FUNCTION__);
206 return;
207 }
208
209
210
211
212
213
214
215
216
217 if ((range->isa_addr.a_hi && ISA_SPACE_MASK) == ISA_SPACE_IO) {
218 isa_addr = range->isa_addr.a_lo;
219 pci_addr = (unsigned long) range->pci_addr.a_mid << 32 |
220 range->pci_addr.a_lo;
221
222
223 if ((pci_addr != 0) || (isa_addr != 0)) {
224 printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
225 __FUNCTION__);
226 return;
227 }
228
229 size = PAGE_ALIGN(range->size);
230
231 __ioremap_explicit(phb_io_base_phys,
232 (unsigned long) phb_io_base_virt,
233 size, _PAGE_NO_CACHE);
234 }
235}
236
237static void __init pci_process_bridge_OF_ranges(struct pci_controller *hose,
238 struct device_node *dev,
239 int primary)
240{
241 unsigned int *ranges;
242 unsigned long size;
243 int rlen = 0;
244 int memno = 0;
245 struct resource *res;
246 int np, na = prom_n_addr_cells(dev);
247 unsigned long pci_addr, cpu_phys_addr;
248 struct device_node *isa_dn;
249
250 np = na + 5;
251
252
253
254
255
256
257
258
259
260 rlen = 0;
261 hose->io_base_phys = 0;
262 ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
263 while ((rlen -= np * sizeof(unsigned int)) >= 0) {
264 res = NULL;
265 pci_addr = (unsigned long)ranges[1] << 32 | ranges[2];
266
267 cpu_phys_addr = ranges[3];
268 if (na == 2)
269 cpu_phys_addr = cpu_phys_addr << 32 | ranges[4];
270
271 size = (unsigned long)ranges[na+3] << 32 | ranges[na+4];
272
273 switch (ranges[0] >> 24) {
274 case 1:
275 hose->io_base_phys = cpu_phys_addr;
276 hose->io_base_virt = reserve_phb_iospace(size);
277 PPCDBG(PPCDBG_PHBINIT,
278 "phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
279 hose->global_number, hose->io_base_phys,
280 (unsigned long) hose->io_base_virt);
281
282 if (primary) {
283 pci_io_base = (unsigned long)hose->io_base_virt;
284 isa_dn = of_find_node_by_type(NULL, "isa");
285 if (isa_dn) {
286 isa_io_base = pci_io_base;
287 pci_process_ISA_OF_ranges(isa_dn,
288 hose->io_base_phys,
289 hose->io_base_virt);
290 of_node_put(isa_dn);
291
292 io_page_mask = -1;
293 }
294 }
295
296 res = &hose->io_resource;
297 res->flags = IORESOURCE_IO;
298 res->start = pci_addr;
299 res->start += (unsigned long)hose->io_base_virt -
300 pci_io_base;
301 break;
302 case 2:
303 memno = 0;
304 while (memno < 3 && hose->mem_resources[memno].flags)
305 ++memno;
306
307 if (memno == 0)
308 hose->pci_mem_offset = cpu_phys_addr - pci_addr;
309 if (memno < 3) {
310 res = &hose->mem_resources[memno];
311 res->flags = IORESOURCE_MEM;
312 res->start = cpu_phys_addr;
313 }
314 break;
315 }
316 if (res != NULL) {
317 res->name = dev->full_name;
318 res->end = res->start + size - 1;
319 res->parent = NULL;
320 res->sibling = NULL;
321 res->child = NULL;
322 }
323 ranges += np;
324 }
325}
326
327static void python_countermeasures(unsigned long addr)
328{
329 void *chip_regs;
330 volatile u32 *tmp, i;
331
332
333 chip_regs = ioremap(addr & ~(0xfffffUL), 0x100000);
334
335
336
337
338
339
340#define PRG_CL_RESET_VALID 0x00010000
341
342 tmp = (u32 *)((unsigned long)chip_regs + 0xf6030);
343
344 if (*tmp & PRG_CL_RESET_VALID) {
345 printk(KERN_INFO "Python workaround: ");
346 *tmp &= ~PRG_CL_RESET_VALID;
347
348
349
350
351 i = *tmp;
352 printk("reg0: %x\n", i);
353 }
354
355 iounmap(chip_regs);
356}
357
358void __init init_pci_config_tokens (void)
359{
360 read_pci_config = rtas_token("read-pci-config");
361 write_pci_config = rtas_token("write-pci-config");
362 ibm_read_pci_config = rtas_token("ibm,read-pci-config");
363 ibm_write_pci_config = rtas_token("ibm,write-pci-config");
364}
365
366unsigned long __init get_phb_buid (struct device_node *phb)
367{
368 int addr_cells;
369 unsigned int *buid_vals;
370 unsigned int len;
371 unsigned long buid;
372
373 if (ibm_read_pci_config == -1) return 0;
374
375
376
377 if (phb->parent == NULL)
378 return 0;
379 if (phb->parent->parent)
380 return 0;
381
382 buid_vals = (unsigned int *) get_property(phb, "reg", &len);
383 if (buid_vals == NULL)
384 return 0;
385
386 addr_cells = prom_n_addr_cells(phb);
387 if (addr_cells == 1) {
388 buid = (unsigned long) buid_vals[0];
389 } else {
390 buid = (((unsigned long)buid_vals[0]) << 32UL) |
391 (((unsigned long)buid_vals[1]) & 0xffffffff);
392 }
393 return buid;
394}
395
396static struct pci_controller * __init alloc_phb(struct device_node *dev,
397 unsigned int addr_size_words)
398{
399 struct pci_controller *phb;
400 unsigned int *ui_ptr = NULL, len;
401 struct reg_property64 reg_struct;
402 int *bus_range;
403 char *model;
404 enum phb_types phb_type;
405 struct property *of_prop;
406
407 model = (char *)get_property(dev, "model", NULL);
408
409 if (!model) {
410 printk(KERN_ERR "alloc_phb: phb has no model property\n");
411 model = "<empty>";
412 }
413
414
415 ui_ptr = (unsigned int *) get_property(dev, "reg", &len);
416 if (ui_ptr == NULL) {
417 PPCDBG(PPCDBG_PHBINIT, "\tget reg failed.\n");
418 return NULL;
419 }
420
421 if (addr_size_words == 1) {
422 reg_struct.address = ((struct reg_property32 *)ui_ptr)->address;
423 reg_struct.size = ((struct reg_property32 *)ui_ptr)->size;
424 } else {
425 reg_struct = *((struct reg_property64 *)ui_ptr);
426 }
427
428 if (strstr(model, "Python")) {
429 phb_type = phb_type_python;
430 } else if (strstr(model, "Speedwagon")) {
431 phb_type = phb_type_speedwagon;
432 } else if (strstr(model, "Winnipeg")) {
433 phb_type = phb_type_winnipeg;
434 } else {
435 printk(KERN_ERR "alloc_phb: unknown PHB %s\n", model);
436 phb_type = phb_type_unknown;
437 }
438
439 phb = pci_alloc_pci_controller(phb_type);
440 if (phb == NULL)
441 return NULL;
442
443 if (phb_type == phb_type_python)
444 python_countermeasures(reg_struct.address);
445
446 bus_range = (int *) get_property(dev, "bus-range", &len);
447 if (bus_range == NULL || len < 2 * sizeof(int)) {
448 kfree(phb);
449 return NULL;
450 }
451
452 of_prop = (struct property *)alloc_bootmem(sizeof(struct property) +
453 sizeof(phb->global_number));
454
455 if (!of_prop) {
456 kfree(phb);
457 return NULL;
458 }
459
460 memset(of_prop, 0, sizeof(struct property));
461 of_prop->name = "linux,pci-domain";
462 of_prop->length = sizeof(phb->global_number);
463 of_prop->value = (unsigned char *)&of_prop[1];
464 memcpy(of_prop->value, &phb->global_number, sizeof(phb->global_number));
465 prom_add_property(dev, of_prop);
466
467 phb->first_busno = bus_range[0];
468 phb->last_busno = bus_range[1];
469
470 phb->arch_data = dev;
471 phb->ops = &rtas_pci_ops;
472
473 phb->buid = get_phb_buid(dev);
474
475 return phb;
476}
477
478unsigned long __init find_and_init_phbs(void)
479{
480 struct device_node *node;
481 struct pci_controller *phb;
482 unsigned int root_size_cells = 0;
483 unsigned int index;
484 unsigned int *opprop;
485 struct device_node *root = of_find_node_by_path("/");
486
487 if (naca->interrupt_controller == IC_OPEN_PIC) {
488 opprop = (unsigned int *)get_property(root,
489 "platform-open-pic", NULL);
490 }
491
492 root_size_cells = prom_n_size_cells(root);
493
494 index = 0;
495
496 for (node = of_get_next_child(root, NULL);
497 node != NULL;
498 node = of_get_next_child(root, node)) {
499 if (node->type == NULL || strcmp(node->type, "pci") != 0)
500 continue;
501
502 phb = alloc_phb(node, root_size_cells);
503 if (!phb)
504 continue;
505
506 pci_process_bridge_OF_ranges(phb, node, index == 0);
507
508 if (naca->interrupt_controller == IC_OPEN_PIC) {
509 int addr = root_size_cells * (index + 2) - 1;
510 openpic_setup_ISU(index, opprop[addr]);
511 }
512
513 index++;
514 }
515
516 of_node_put(root);
517 pci_devs_phb_init();
518
519 return 0;
520}
521
522void pcibios_name_device(struct pci_dev *dev)
523{
524#if 0
525 struct device_node *dn;
526
527
528
529
530 dn = pci_device_to_OF_node(dev);
531 if (dn) {
532 char *loc_code = get_property(dn, "ibm,loc-code", 0);
533 if (loc_code) {
534 int loc_len = strlen(loc_code);
535 if (loc_len < sizeof(dev->dev.name)) {
536 memmove(dev->dev.name+loc_len+1, dev->dev.name,
537 sizeof(dev->dev.name)-loc_len-1);
538 memcpy(dev->dev.name, loc_code, loc_len);
539 dev->dev.name[loc_len] = ' ';
540 dev->dev.name[sizeof(dev->dev.name)-1] = '\0';
541 }
542 }
543 }
544#endif
545}
546
547void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
548 struct pci_bus *bus)
549{
550
551 struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
552 int i;
553
554 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
555 if (dev->resource[i].flags & IORESOURCE_IO) {
556 unsigned long offset = (unsigned long)hose->io_base_virt - pci_io_base;
557 unsigned long start, end, mask;
558
559 start = dev->resource[i].start += offset;
560 end = dev->resource[i].end += offset;
561
562
563
564 if (start < MAX_ISA_PORT) {
565 if (end > MAX_ISA_PORT)
566 end = MAX_ISA_PORT;
567
568 start >>= PAGE_SHIFT;
569 end >>= PAGE_SHIFT;
570
571
572 mask = ((1 << (end+1))-1) ^ ((1 << start)-1);
573 io_page_mask |= mask;
574 }
575 }
576 else if (dev->resource[i].flags & IORESOURCE_MEM) {
577 dev->resource[i].start += hose->pci_mem_offset;
578 dev->resource[i].end += hose->pci_mem_offset;
579 }
580 }
581}
582EXPORT_SYMBOL(pcibios_fixup_device_resources);
583
584void __devinit pcibios_fixup_bus(struct pci_bus *bus)
585{
586 struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
587 struct list_head *ln;
588
589
590 struct pci_dev *dev = bus->self;
591 struct resource *res;
592 int i;
593
594 if (!dev) {
595
596
597 hose->bus = bus;
598 bus->resource[0] = res = &hose->io_resource;
599 if (!res->flags)
600 BUG();
601
602 if (request_resource(&ioport_resource, res))
603 printk(KERN_ERR "Failed to request IO on "
604 "PCI domain %d\n", pci_domain_nr(bus));
605
606
607 for (i = 0; i < 3; ++i) {
608 res = &hose->mem_resources[i];
609 if (!res->flags && i == 0)
610 BUG();
611 bus->resource[i+1] = res;
612 if (res->flags && request_resource(&iomem_resource, res))
613 printk(KERN_ERR "Failed to request MEM on "
614 "PCI domain %d\n",
615 pci_domain_nr(bus));
616 }
617 } else if (pci_probe_only &&
618 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
619
620
621 pci_read_bridge_bases(bus);
622 pcibios_fixup_device_resources(dev, bus);
623 }
624
625
626 if (!pci_probe_only)
627 return;
628
629 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
630 struct pci_dev *dev = pci_dev_b(ln);
631 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
632 pcibios_fixup_device_resources(dev, bus);
633 }
634}
635EXPORT_SYMBOL(pcibios_fixup_bus);
636
637static void check_s7a(void)
638{
639 struct device_node *root;
640 char *model;
641
642 root = of_find_node_by_path("/");
643 if (root) {
644 model = get_property(root, "model", NULL);
645 if (model && !strcmp(model, "IBM,7013-S7A"))
646 s7a_workaround = 1;
647 of_node_put(root);
648 }
649}
650
651static int get_bus_io_range(struct pci_bus *bus, unsigned long *start_phys,
652 unsigned long *start_virt, unsigned long *size)
653{
654 struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
655 struct pci_bus_region region;
656 struct resource *res;
657
658 if (bus->self) {
659 res = bus->resource[0];
660 pcibios_resource_to_bus(bus->self, ®ion, res);
661 *start_phys = hose->io_base_phys + region.start;
662 *start_virt = (unsigned long) hose->io_base_virt +
663 region.start;
664 if (region.end > region.start)
665 *size = region.end - region.start + 1;
666 else {
667 printk("%s(): unexpected region 0x%lx->0x%lx\n",
668 __FUNCTION__, region.start, region.end);
669 return 1;
670 }
671
672 } else {
673
674 res = &hose->io_resource;
675 *start_phys = hose->io_base_phys;
676 *start_virt = (unsigned long) hose->io_base_virt;
677 if (res->end > res->start)
678 *size = res->end - res->start + 1;
679 else {
680 printk("%s(): unexpected region 0x%lx->0x%lx\n",
681 __FUNCTION__, res->start, res->end);
682 return 1;
683 }
684 }
685
686 return 0;
687}
688
689int unmap_bus_range(struct pci_bus *bus)
690{
691 unsigned long start_phys;
692 unsigned long start_virt;
693 unsigned long size;
694
695 if (!bus) {
696 printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
697 return 1;
698 }
699
700 if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
701 return 1;
702 if (iounmap_explicit((void *) start_virt, size))
703 return 1;
704
705 return 0;
706}
707EXPORT_SYMBOL(unmap_bus_range);
708
709int remap_bus_range(struct pci_bus *bus)
710{
711 unsigned long start_phys;
712 unsigned long start_virt;
713 unsigned long size;
714
715 if (!bus) {
716 printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
717 return 1;
718 }
719
720 if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
721 return 1;
722 if (__ioremap_explicit(start_phys, start_virt, size, _PAGE_NO_CACHE))
723 return 1;
724
725 return 0;
726}
727EXPORT_SYMBOL(remap_bus_range);
728
729static void phbs_fixup_io(void)
730{
731 struct pci_controller *hose;
732
733 for (hose=hose_head;hose;hose=hose->next)
734 remap_bus_range(hose->bus);
735}
736
737extern void chrp_request_regions(void);
738
739void __init pSeries_final_fixup(void)
740{
741 struct pci_dev *dev = NULL;
742
743 check_s7a();
744
745 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL)
746 pci_read_irq_line(dev);
747
748 phbs_fixup_io();
749 chrp_request_regions();
750 pci_fix_bus_sysdata();
751 if (!ppc64_iommu_off)
752 iommu_setup_pSeries();
753}
754
755
756
757
758
759
760
761
762struct pci_controller*
763pci_find_hose_for_OF_device(struct device_node *node)
764{
765 while (node) {
766 struct pci_controller *hose;
767 for (hose=hose_head;hose;hose=hose->next)
768 if (hose->arch_data == node)
769 return hose;
770 node=node->parent;
771 }
772 return NULL;
773}
774
775
776
777
778
779int
780pcibios_scan_all_fns(struct pci_bus *bus, int devfn)
781{
782 struct device_node *busdn, *dn;
783
784 if (bus->self)
785 busdn = pci_device_to_OF_node(bus->self);
786 else
787 busdn = bus->sysdata;
788
789
790
791
792
793
794 for (dn = busdn->child; dn; dn = dn->sibling)
795 if ((dn->devfn >> 3) == (devfn >> 3))
796 return 1;
797
798 return 0;
799}
800