This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / arch / ppc64 / kernel / pSeries_pci.c
1 /*
2  * pSeries_pci.c
3  *
4  * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
5  * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
6  *
7  * pSeries specific routines for PCI.
8  * 
9  * Based on code from pci.c and chrp_pci.c
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *    
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
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 /* RTAS tokens */
49 static int read_pci_config;
50 static int write_pci_config;
51 static int ibm_read_pci_config;
52 static int ibm_write_pci_config;
53
54 static int s7a_workaround;
55
56 extern unsigned long pci_probe_only;
57
58 static int rtas_read_config(struct device_node *dn, int where, int size, u32 *val)
59 {
60         int returnval = -1;
61         unsigned long buid, addr;
62         int ret;
63
64         if (!dn)
65                 return PCIBIOS_DEVICE_NOT_FOUND;
66         if (where & (size - 1))
67                 return PCIBIOS_BAD_REGISTER_NUMBER;
68
69         addr = (dn->busno << 16) | (dn->devfn << 8) | where;
70         buid = dn->phb->buid;
71         if (buid) {
72                 ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval,
73                                 addr, buid >> 32, buid & 0xffffffff, size);
74         } else {
75                 ret = rtas_call(read_pci_config, 2, 2, &returnval, addr, size);
76         }
77         *val = returnval;
78
79         if (ret)
80                 return PCIBIOS_DEVICE_NOT_FOUND;
81
82         if (returnval == EEH_IO_ERROR_VALUE(size)
83             && eeh_dn_check_failure (dn, NULL))
84                 return PCIBIOS_DEVICE_NOT_FOUND;
85
86         return PCIBIOS_SUCCESSFUL;
87 }
88
89 static int rtas_pci_read_config(struct pci_bus *bus,
90                                 unsigned int devfn,
91                                 int where, int size, u32 *val)
92 {
93         struct device_node *busdn, *dn;
94
95         if (bus->self)
96                 busdn = pci_device_to_OF_node(bus->self);
97         else
98                 busdn = bus->sysdata;   /* must be a phb */
99
100         /* Search only direct children of the bus */
101         for (dn = busdn->child; dn; dn = dn->sibling)
102                 if (dn->devfn == devfn)
103                         return rtas_read_config(dn, where, size, val);
104         return PCIBIOS_DEVICE_NOT_FOUND;
105 }
106
107 static int rtas_write_config(struct device_node *dn, int where, int size, u32 val)
108 {
109         unsigned long buid, addr;
110         int ret;
111
112         if (!dn)
113                 return PCIBIOS_DEVICE_NOT_FOUND;
114         if (where & (size - 1))
115                 return PCIBIOS_BAD_REGISTER_NUMBER;
116
117         addr = (dn->busno << 16) | (dn->devfn << 8) | where;
118         buid = dn->phb->buid;
119         if (buid) {
120                 ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr, buid >> 32, buid & 0xffffffff, size, (ulong) val);
121         } else {
122                 ret = rtas_call(write_pci_config, 3, 1, NULL, addr, size, (ulong)val);
123         }
124
125         if (ret)
126                 return PCIBIOS_DEVICE_NOT_FOUND;
127
128         return PCIBIOS_SUCCESSFUL;
129 }
130
131 static int rtas_pci_write_config(struct pci_bus *bus,
132                                  unsigned int devfn,
133                                  int where, int size, u32 val)
134 {
135         struct device_node *busdn, *dn;
136
137         if (bus->self)
138                 busdn = pci_device_to_OF_node(bus->self);
139         else
140                 busdn = bus->sysdata;   /* must be a phb */
141
142         /* Search only direct children of the bus */
143         for (dn = busdn->child; dn; dn = dn->sibling)
144                 if (dn->devfn == devfn)
145                         return rtas_write_config(dn, where, size, val);
146         return PCIBIOS_DEVICE_NOT_FOUND;
147 }
148
149 struct pci_ops rtas_pci_ops = {
150         rtas_pci_read_config,
151         rtas_pci_write_config
152 };
153
154 static void python_countermeasures(unsigned long addr)
155 {
156         void *chip_regs;
157         volatile u32 *tmp, i;
158
159         /* Python's register file is 1 MB in size. */
160         chip_regs = ioremap(addr & ~(0xfffffUL), 0x100000); 
161
162         /* 
163          * Firmware doesn't always clear this bit which is critical
164          * for good performance - Anton
165          */
166
167 #define PRG_CL_RESET_VALID 0x00010000
168
169         tmp = (u32 *)((unsigned long)chip_regs + 0xf6030);
170
171         if (*tmp & PRG_CL_RESET_VALID) {
172                 printk(KERN_INFO "Python workaround: ");
173                 *tmp &= ~PRG_CL_RESET_VALID;
174                 /*
175                  * We must read it back for changes to
176                  * take effect
177                  */
178                 i = *tmp;
179                 printk("reg0: %x\n", i);
180         }
181
182         iounmap(chip_regs);
183 }
184
185 void __init init_pci_config_tokens (void)
186 {
187         read_pci_config = rtas_token("read-pci-config");
188         write_pci_config = rtas_token("write-pci-config");
189         ibm_read_pci_config = rtas_token("ibm,read-pci-config");
190         ibm_write_pci_config = rtas_token("ibm,write-pci-config");
191 }
192
193 unsigned long __devinit get_phb_buid (struct device_node *phb)
194 {
195         int addr_cells;
196         unsigned int *buid_vals;
197         unsigned int len;
198         unsigned long buid;
199
200         if (ibm_read_pci_config == -1) return 0;
201
202         /* PHB's will always be children of the root node,
203          * or so it is promised by the current firmware. */
204         if (phb->parent == NULL)
205                 return 0;
206         if (phb->parent->parent)
207                 return 0;
208
209         buid_vals = (unsigned int *) get_property(phb, "reg", &len);
210         if (buid_vals == NULL)
211                 return 0;
212
213         addr_cells = prom_n_addr_cells(phb);
214         if (addr_cells == 1) {
215                 buid = (unsigned long) buid_vals[0];
216         } else {
217                 buid = (((unsigned long)buid_vals[0]) << 32UL) |
218                         (((unsigned long)buid_vals[1]) & 0xffffffff);
219         }
220         return buid;
221 }
222
223 static enum phb_types get_phb_type(struct device_node *dev)
224 {
225         enum phb_types type;
226         char *model;
227
228         model = (char *)get_property(dev, "model", NULL);
229
230         if (!model) {
231                 printk(KERN_ERR "%s: phb has no model property\n",
232                                 __FUNCTION__);
233                 model = "<empty>";
234         }
235
236         if (strstr(model, "Python")) {
237                 type = phb_type_python;
238         } else if (strstr(model, "Speedwagon")) {
239                 type = phb_type_speedwagon;
240         } else if (strstr(model, "Winnipeg")) {
241                 type = phb_type_winnipeg;
242         } else {
243                 printk(KERN_ERR "%s: unknown PHB %s\n", __FUNCTION__, model);
244                 type = phb_type_unknown;
245         }
246
247         return type;
248 }
249
250 int get_phb_reg_prop(struct device_node *dev, unsigned int addr_size_words,
251                 struct reg_property64 *reg)
252 {
253         unsigned int *ui_ptr = NULL, len;
254
255         /* Found a PHB, now figure out where his registers are mapped. */
256         ui_ptr = (unsigned int *) get_property(dev, "reg", &len);
257         if (ui_ptr == NULL) {
258                 PPCDBG(PPCDBG_PHBINIT, "\tget reg failed.\n"); 
259                 return 1;
260         }
261
262         if (addr_size_words == 1) {
263                 reg->address = ((struct reg_property32 *)ui_ptr)->address;
264                 reg->size    = ((struct reg_property32 *)ui_ptr)->size;
265         } else {
266                 *reg = *((struct reg_property64 *)ui_ptr);
267         }
268
269         return 0;
270 }
271
272 int phb_set_bus_ranges(struct device_node *dev, struct pci_controller *phb)
273 {
274         int *bus_range;
275         unsigned int len;
276
277         bus_range = (int *) get_property(dev, "bus-range", &len);
278         if (bus_range == NULL || len < 2 * sizeof(int)) {
279                 return 1;
280         }
281
282         phb->first_busno =  bus_range[0];
283         phb->last_busno  =  bus_range[1];
284
285         return 0;
286 }
287
288 static struct pci_controller *alloc_phb(struct device_node *dev,
289                                  unsigned int addr_size_words)
290 {
291         struct pci_controller *phb;
292         struct reg_property64 reg_struct;
293         enum phb_types phb_type;
294         struct property *of_prop;
295         int rc;
296
297         phb_type = get_phb_type(dev);
298
299         rc = get_phb_reg_prop(dev, addr_size_words, &reg_struct);
300         if (rc)
301                 return NULL;
302
303         phb = pci_alloc_pci_controller(phb_type);
304         if (phb == NULL)
305                 return NULL;
306
307         if (phb_type == phb_type_python)
308                 python_countermeasures(reg_struct.address);
309
310         rc = phb_set_bus_ranges(dev, phb);
311         if (rc)
312                 return NULL;
313
314         of_prop = (struct property *)alloc_bootmem(sizeof(struct property) +
315                         sizeof(phb->global_number));        
316
317         if (!of_prop) {
318                 kfree(phb);
319                 return NULL;
320         }
321
322         memset(of_prop, 0, sizeof(struct property));
323         of_prop->name = "linux,pci-domain";
324         of_prop->length = sizeof(phb->global_number);
325         of_prop->value = (unsigned char *)&of_prop[1];
326         memcpy(of_prop->value, &phb->global_number, sizeof(phb->global_number));
327         prom_add_property(dev, of_prop);
328
329         phb->arch_data   = dev;
330         phb->ops = &rtas_pci_ops;
331
332         phb->buid = get_phb_buid(dev);
333
334         return phb;
335 }
336
337 static struct pci_controller * __devinit alloc_phb_dynamic(struct device_node *dev, unsigned int addr_size_words)
338 {
339         struct pci_controller *phb;
340         struct reg_property64 reg_struct;
341         enum phb_types phb_type;
342         int rc;
343
344         phb_type = get_phb_type(dev);
345
346         rc = get_phb_reg_prop(dev, addr_size_words, &reg_struct);
347         if (rc)
348                 return NULL;
349
350         phb = pci_alloc_phb_dynamic(phb_type);
351         if (phb == NULL)
352                 return NULL;
353
354         if (phb_type == phb_type_python)
355                 python_countermeasures(reg_struct.address);
356
357         rc = phb_set_bus_ranges(dev, phb);
358         if (rc)
359                 return NULL;
360
361         /* TODO: linux,pci-domain? */
362
363         phb->arch_data   = dev;
364         phb->ops = &rtas_pci_ops;
365
366         phb->buid = get_phb_buid(dev);
367
368         return phb;
369 }
370
371 unsigned long __init find_and_init_phbs(void)
372 {
373         struct device_node *node;
374         struct pci_controller *phb;
375         unsigned int root_size_cells = 0;
376         unsigned int index;
377         unsigned int *opprop = NULL;
378         struct device_node *root = of_find_node_by_path("/");
379
380         if (naca->interrupt_controller == IC_OPEN_PIC) {
381                 opprop = (unsigned int *)get_property(root,
382                                 "platform-open-pic", NULL);
383         }
384
385         root_size_cells = prom_n_size_cells(root);
386
387         index = 0;
388
389         for (node = of_get_next_child(root, NULL);
390              node != NULL;
391              node = of_get_next_child(root, node)) {
392                 if (node->type == NULL || strcmp(node->type, "pci") != 0)
393                         continue;
394
395                 phb = alloc_phb(node, root_size_cells);
396                 if (!phb)
397                         continue;
398
399                 pci_process_bridge_OF_ranges(phb, node);
400                 pci_setup_phb_io(phb, index == 0);
401
402                 if (naca->interrupt_controller == IC_OPEN_PIC) {
403                         int addr = root_size_cells * (index + 2) - 1;
404                         openpic_setup_ISU(index, opprop[addr]); 
405                 }
406
407                 index++;
408         }
409
410         of_node_put(root);
411         pci_devs_phb_init();
412
413         return 0;
414 }
415
416 struct pci_controller * __devinit init_phb_dynamic(struct device_node *dn)
417 {
418         struct device_node *root = of_find_node_by_path("/");
419         unsigned int root_size_cells = 0;
420         struct pci_controller *phb;
421         struct pci_bus *bus;
422
423         root_size_cells = prom_n_size_cells(root);
424
425         phb = alloc_phb_dynamic(dn, root_size_cells);
426         if (!phb)
427                 return NULL;
428
429         pci_process_bridge_OF_ranges(phb, dn);
430
431         pci_setup_phb_io_dynamic(phb);
432         of_node_put(root);
433
434         pci_devs_phb_init_dynamic(phb);
435         phb->last_busno = 0xff;
436         bus = pci_scan_bus(phb->first_busno, phb->ops, phb->arch_data);
437         phb->bus = bus;
438         phb->last_busno = bus->subordinate;
439
440         return phb;
441 }
442 EXPORT_SYMBOL(init_phb_dynamic);
443
444 #if 0
445 void pcibios_name_device(struct pci_dev *dev)
446 {
447         struct device_node *dn;
448
449         /*
450          * Add IBM loc code (slot) as a prefix to the device names for service
451          */
452         dn = pci_device_to_OF_node(dev);
453         if (dn) {
454                 char *loc_code = get_property(dn, "ibm,loc-code", 0);
455                 if (loc_code) {
456                         int loc_len = strlen(loc_code);
457                         if (loc_len < sizeof(dev->dev.name)) {
458                                 memmove(dev->dev.name+loc_len+1, dev->dev.name,
459                                         sizeof(dev->dev.name)-loc_len-1);
460                                 memcpy(dev->dev.name, loc_code, loc_len);
461                                 dev->dev.name[loc_len] = ' ';
462                                 dev->dev.name[sizeof(dev->dev.name)-1] = '\0';
463                         }
464                 }
465         }
466 }   
467 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_name_device);
468 #endif
469
470 static void check_s7a(void)
471 {
472         struct device_node *root;
473         char *model;
474
475         root = of_find_node_by_path("/");
476         if (root) {
477                 model = get_property(root, "model", NULL);
478                 if (model && !strcmp(model, "IBM,7013-S7A"))
479                         s7a_workaround = 1;
480                 of_node_put(root);
481         }
482 }
483
484 static int get_bus_io_range(struct pci_bus *bus, unsigned long *start_phys,
485                                 unsigned long *start_virt, unsigned long *size)
486 {
487         struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
488         struct pci_bus_region region;
489         struct resource *res;
490
491         if (bus->self) {
492                 res = bus->resource[0];
493                 pcibios_resource_to_bus(bus->self, &region, res);
494                 *start_phys = hose->io_base_phys + region.start;
495                 *start_virt = (unsigned long) hose->io_base_virt + 
496                                 region.start;
497                 if (region.end > region.start) 
498                         *size = region.end - region.start + 1;
499                 else {
500                         printk("%s(): unexpected region 0x%lx->0x%lx\n", 
501                                         __FUNCTION__, region.start, region.end);
502                         return 1;
503                 }
504                 
505         } else {
506                 /* Root Bus */
507                 res = &hose->io_resource;
508                 *start_phys = hose->io_base_phys;
509                 *start_virt = (unsigned long) hose->io_base_virt;
510                 if (res->end > res->start)
511                         *size = res->end - res->start + 1;
512                 else {
513                         printk("%s(): unexpected region 0x%lx->0x%lx\n", 
514                                         __FUNCTION__, res->start, res->end);
515                         return 1;
516                 }
517         }
518
519         return 0;
520 }
521
522 int unmap_bus_range(struct pci_bus *bus)
523 {
524         unsigned long start_phys;
525         unsigned long start_virt;
526         unsigned long size;
527
528         if (!bus) {
529                 printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
530                 return 1;
531         }
532         
533         if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
534                 return 1;
535         if (iounmap_explicit((void *) start_virt, size))
536                 return 1;
537
538         return 0;
539 }
540 EXPORT_SYMBOL(unmap_bus_range);
541
542 int remap_bus_range(struct pci_bus *bus)
543 {
544         unsigned long start_phys;
545         unsigned long start_virt;
546         unsigned long size;
547
548         if (!bus) {
549                 printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
550                 return 1;
551         }
552         
553         if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
554                 return 1;
555         if (__ioremap_explicit(start_phys, start_virt, size, _PAGE_NO_CACHE))
556                 return 1;
557
558         return 0;
559 }
560 EXPORT_SYMBOL(remap_bus_range);
561
562 static void phbs_remap_io(void)
563 {
564         struct pci_controller *hose, *tmp;
565
566         list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
567                 remap_bus_range(hose->bus);
568 }
569
570 static void __init pSeries_request_regions(void)
571 {
572         struct device_node *i8042;
573
574         request_region(0x20,0x20,"pic1");
575         request_region(0xa0,0x20,"pic2");
576         request_region(0x00,0x20,"dma1");
577         request_region(0x40,0x20,"timer");
578         request_region(0x80,0x10,"dma page reg");
579         request_region(0xc0,0x20,"dma2");
580
581 #define I8042_DATA_REG 0x60
582
583         /*
584          * Some machines have an unterminated i8042 so check the device
585          * tree and reserve the region if it does not appear. Later on
586          * the i8042 code will try and reserve this region and fail.
587          */
588         if (!(i8042 = of_find_node_by_type(NULL, "8042")))
589                 request_region(I8042_DATA_REG, 16, "reserved (no i8042)");
590         of_node_put(i8042);
591 }
592
593 /* RPA-specific bits for removing PHBs */
594 int pcibios_remove_root_bus(struct pci_controller *phb)
595 {
596         struct pci_bus *b = phb->bus;
597         struct resource *res;
598         int rc, i;
599
600         res = b->resource[0];
601         if (!res->flags) {
602                 printk(KERN_ERR "%s: no IO resource for PHB %s\n", __FUNCTION__,
603                                 b->name);
604                 return 1;
605         }
606
607         rc = unmap_bus_range(b);
608         if (rc) {
609                 printk(KERN_ERR "%s: failed to unmap IO on bus %s\n",
610                         __FUNCTION__, b->name);
611                 return 1;
612         }
613
614         if (release_resource(res)) {
615                 printk(KERN_ERR "%s: failed to release IO on bus %s\n",
616                                 __FUNCTION__, b->name);
617                 return 1;
618         }
619
620         for (i = 1; i < 3; ++i) {
621                 res = b->resource[i];
622                 if (!res->flags && i == 0) {
623                         printk(KERN_ERR "%s: no MEM resource for PHB %s\n",
624                                 __FUNCTION__, b->name);
625                         return 1;
626                 }
627                 if (res->flags && release_resource(res)) {
628                         printk(KERN_ERR
629                                "%s: failed to release IO %d on bus %s\n",
630                                 __FUNCTION__, i, b->name);
631                         return 1;
632                 }
633         }
634
635         list_del(&phb->list_node);
636         if (phb->is_dynamic)
637                 kfree(phb);
638
639         return 0;
640 }
641 EXPORT_SYMBOL(pcibios_remove_root_bus);
642
643 void __init pSeries_final_fixup(void)
644 {
645         struct pci_dev *dev = NULL;
646
647         check_s7a();
648
649         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
650                 pci_read_irq_line(dev);
651                 if (s7a_workaround) {
652                         if (dev->irq > 16) {
653                                 dev->irq -= 3;
654                                 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
655                         }
656                 }
657         }
658
659         phbs_remap_io();
660         pSeries_request_regions();
661         pci_fix_bus_sysdata();
662
663         if (!of_chosen || !get_property(of_chosen, "linux,iommu-off", NULL))
664                 iommu_setup_pSeries();
665
666         pci_addr_cache_build();
667 }