vserver 1.9.3
[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 __init 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 struct pci_controller * __init alloc_phb(struct device_node *dev,
224                                  unsigned int addr_size_words)
225 {
226         struct pci_controller *phb;
227         unsigned int *ui_ptr = NULL, len;
228         struct reg_property64 reg_struct;
229         int *bus_range;
230         char *model;
231         enum phb_types phb_type;
232         struct property *of_prop;
233
234         model = (char *)get_property(dev, "model", NULL);
235
236         if (!model) {
237                 printk(KERN_ERR "alloc_phb: phb has no model property\n");
238                 model = "<empty>";
239         }
240
241         /* Found a PHB, now figure out where his registers are mapped. */
242         ui_ptr = (unsigned int *) get_property(dev, "reg", &len);
243         if (ui_ptr == NULL) {
244                 PPCDBG(PPCDBG_PHBINIT, "\tget reg failed.\n"); 
245                 return NULL;
246         }
247
248         if (addr_size_words == 1) {
249                 reg_struct.address = ((struct reg_property32 *)ui_ptr)->address;
250                 reg_struct.size    = ((struct reg_property32 *)ui_ptr)->size;
251         } else {
252                 reg_struct = *((struct reg_property64 *)ui_ptr);
253         }
254
255         if (strstr(model, "Python")) {
256                 phb_type = phb_type_python;
257         } else if (strstr(model, "Speedwagon")) {
258                 phb_type = phb_type_speedwagon;
259         } else if (strstr(model, "Winnipeg")) {
260                 phb_type = phb_type_winnipeg;
261         } else {
262                 printk(KERN_ERR "alloc_phb: unknown PHB %s\n", model);
263                 phb_type = phb_type_unknown;
264         }
265
266         phb = pci_alloc_pci_controller(phb_type);
267         if (phb == NULL)
268                 return NULL;
269
270         if (phb_type == phb_type_python)
271                 python_countermeasures(reg_struct.address);
272
273         bus_range = (int *) get_property(dev, "bus-range", &len);
274         if (bus_range == NULL || len < 2 * sizeof(int)) {
275                 kfree(phb);
276                 return NULL;
277         }
278
279         of_prop = (struct property *)alloc_bootmem(sizeof(struct property) +
280                         sizeof(phb->global_number));        
281
282         if (!of_prop) {
283                 kfree(phb);
284                 return NULL;
285         }
286
287         memset(of_prop, 0, sizeof(struct property));
288         of_prop->name = "linux,pci-domain";
289         of_prop->length = sizeof(phb->global_number);
290         of_prop->value = (unsigned char *)&of_prop[1];
291         memcpy(of_prop->value, &phb->global_number, sizeof(phb->global_number));
292         prom_add_property(dev, of_prop);
293
294         phb->first_busno =  bus_range[0];
295         phb->last_busno  =  bus_range[1];
296
297         phb->arch_data   = dev;
298         phb->ops = &rtas_pci_ops;
299
300         phb->buid = get_phb_buid(dev);
301
302         return phb;
303 }
304
305 unsigned long __init find_and_init_phbs(void)
306 {
307         struct device_node *node;
308         struct pci_controller *phb;
309         unsigned int root_size_cells = 0;
310         unsigned int index;
311         unsigned int *opprop = NULL;
312         struct device_node *root = of_find_node_by_path("/");
313
314         if (naca->interrupt_controller == IC_OPEN_PIC) {
315                 opprop = (unsigned int *)get_property(root,
316                                 "platform-open-pic", NULL);
317         }
318
319         root_size_cells = prom_n_size_cells(root);
320
321         index = 0;
322
323         for (node = of_get_next_child(root, NULL);
324              node != NULL;
325              node = of_get_next_child(root, node)) {
326                 if (node->type == NULL || strcmp(node->type, "pci") != 0)
327                         continue;
328
329                 phb = alloc_phb(node, root_size_cells);
330                 if (!phb)
331                         continue;
332
333                 pci_process_bridge_OF_ranges(phb, node, index == 0);
334
335                 if (naca->interrupt_controller == IC_OPEN_PIC) {
336                         int addr = root_size_cells * (index + 2) - 1;
337                         openpic_setup_ISU(index, opprop[addr]); 
338                 }
339
340                 index++;
341         }
342
343         of_node_put(root);
344         pci_devs_phb_init();
345
346         return 0;
347 }
348
349 #if 0
350 void pcibios_name_device(struct pci_dev *dev)
351 {
352         struct device_node *dn;
353
354         /*
355          * Add IBM loc code (slot) as a prefix to the device names for service
356          */
357         dn = pci_device_to_OF_node(dev);
358         if (dn) {
359                 char *loc_code = get_property(dn, "ibm,loc-code", 0);
360                 if (loc_code) {
361                         int loc_len = strlen(loc_code);
362                         if (loc_len < sizeof(dev->dev.name)) {
363                                 memmove(dev->dev.name+loc_len+1, dev->dev.name,
364                                         sizeof(dev->dev.name)-loc_len-1);
365                                 memcpy(dev->dev.name, loc_code, loc_len);
366                                 dev->dev.name[loc_len] = ' ';
367                                 dev->dev.name[sizeof(dev->dev.name)-1] = '\0';
368                         }
369                 }
370         }
371 }   
372 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_name_device);
373 #endif
374
375 static void check_s7a(void)
376 {
377         struct device_node *root;
378         char *model;
379
380         root = of_find_node_by_path("/");
381         if (root) {
382                 model = get_property(root, "model", NULL);
383                 if (model && !strcmp(model, "IBM,7013-S7A"))
384                         s7a_workaround = 1;
385                 of_node_put(root);
386         }
387 }
388
389 static int get_bus_io_range(struct pci_bus *bus, unsigned long *start_phys,
390                                 unsigned long *start_virt, unsigned long *size)
391 {
392         struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
393         struct pci_bus_region region;
394         struct resource *res;
395
396         if (bus->self) {
397                 res = bus->resource[0];
398                 pcibios_resource_to_bus(bus->self, &region, res);
399                 *start_phys = hose->io_base_phys + region.start;
400                 *start_virt = (unsigned long) hose->io_base_virt + 
401                                 region.start;
402                 if (region.end > region.start) 
403                         *size = region.end - region.start + 1;
404                 else {
405                         printk("%s(): unexpected region 0x%lx->0x%lx\n", 
406                                         __FUNCTION__, region.start, region.end);
407                         return 1;
408                 }
409                 
410         } else {
411                 /* Root Bus */
412                 res = &hose->io_resource;
413                 *start_phys = hose->io_base_phys;
414                 *start_virt = (unsigned long) hose->io_base_virt;
415                 if (res->end > res->start)
416                         *size = res->end - res->start + 1;
417                 else {
418                         printk("%s(): unexpected region 0x%lx->0x%lx\n", 
419                                         __FUNCTION__, res->start, res->end);
420                         return 1;
421                 }
422         }
423
424         return 0;
425 }
426
427 int unmap_bus_range(struct pci_bus *bus)
428 {
429         unsigned long start_phys;
430         unsigned long start_virt;
431         unsigned long size;
432
433         if (!bus) {
434                 printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
435                 return 1;
436         }
437         
438         if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
439                 return 1;
440         if (iounmap_explicit((void *) start_virt, size))
441                 return 1;
442
443         return 0;
444 }
445 EXPORT_SYMBOL(unmap_bus_range);
446
447 int remap_bus_range(struct pci_bus *bus)
448 {
449         unsigned long start_phys;
450         unsigned long start_virt;
451         unsigned long size;
452
453         if (!bus) {
454                 printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
455                 return 1;
456         }
457         
458         if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
459                 return 1;
460         if (__ioremap_explicit(start_phys, start_virt, size, _PAGE_NO_CACHE))
461                 return 1;
462
463         return 0;
464 }
465 EXPORT_SYMBOL(remap_bus_range);
466
467 static void phbs_fixup_io(void)
468 {
469         struct pci_controller *hose, *tmp;
470
471         list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
472                 remap_bus_range(hose->bus);
473 }
474
475 static void __init pSeries_request_regions(void)
476 {
477         struct device_node *i8042;
478
479         request_region(0x20,0x20,"pic1");
480         request_region(0xa0,0x20,"pic2");
481         request_region(0x00,0x20,"dma1");
482         request_region(0x40,0x20,"timer");
483         request_region(0x80,0x10,"dma page reg");
484         request_region(0xc0,0x20,"dma2");
485
486 #define I8042_DATA_REG 0x60
487
488         /*
489          * Some machines have an unterminated i8042 so check the device
490          * tree and reserve the region if it does not appear. Later on
491          * the i8042 code will try and reserve this region and fail.
492          */
493         if (!(i8042 = of_find_node_by_type(NULL, "8042")))
494                 request_region(I8042_DATA_REG, 16, "reserved (no i8042)");
495         of_node_put(i8042);
496 }
497
498 void __init pSeries_final_fixup(void)
499 {
500         struct pci_dev *dev = NULL;
501
502         check_s7a();
503
504         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
505                 pci_read_irq_line(dev);
506                 if (s7a_workaround) {
507                         if (dev->irq > 16) {
508                                 dev->irq -= 3;
509                                 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
510                         }
511                 }
512         }
513
514         phbs_fixup_io();
515         pSeries_request_regions();
516         pci_fix_bus_sysdata();
517
518         if (!of_chosen || !get_property(of_chosen, "linux,iommu-off", NULL))
519                 iommu_setup_pSeries();
520
521         pci_addr_cache_build();
522 }
523