[POWERPC] iSeries: Make pcibios_final_fixup not depend on pci_dn
[linux-2.6/libata-dev.git] / arch / powerpc / platforms / iseries / pci.c
blob4bcf446cbebffed073d216bb97471f7903c56ba3
1 /*
2 * Copyright (C) 2001 Allan Trautman, IBM Corporation
4 * iSeries specific routines for PCI.
6 * Based on code from pci.c and iSeries_pci.c 32bit
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/string.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
29 #include <asm/io.h>
30 #include <asm/irq.h>
31 #include <asm/prom.h>
32 #include <asm/machdep.h>
33 #include <asm/pci-bridge.h>
34 #include <asm/iommu.h>
35 #include <asm/abs_addr.h>
36 #include <asm/firmware.h>
38 #include <asm/iseries/hv_call_xm.h>
39 #include <asm/iseries/mf.h>
40 #include <asm/iseries/iommu.h>
42 #include <asm/ppc-pci.h>
44 #include "irq.h"
45 #include "pci.h"
46 #include "call_pci.h"
48 #define PCI_RETRY_MAX 3
49 static int limit_pci_retries = 1; /* Set Retry Error on. */
52 * Table defines
53 * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
55 #define IOMM_TABLE_MAX_ENTRIES 1024
56 #define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
57 #define BASE_IO_MEMORY 0xE000000000000000UL
59 static unsigned long max_io_memory = BASE_IO_MEMORY;
60 static long current_iomm_table_entry;
63 * Lookup Tables.
65 static struct device_node *iomm_table[IOMM_TABLE_MAX_ENTRIES];
66 static u8 iobar_table[IOMM_TABLE_MAX_ENTRIES];
68 static const char pci_io_text[] = "iSeries PCI I/O";
69 static DEFINE_SPINLOCK(iomm_table_lock);
72 * Generate a Direct Select Address for the Hypervisor
74 static inline u64 iseries_ds_addr(struct device_node *node)
76 struct pci_dn *pdn = PCI_DN(node);
78 return ((u64)pdn->busno << 48) + ((u64)pdn->bussubno << 40)
79 + ((u64)0x10 << 32);
83 * iomm_table_allocate_entry
85 * Adds pci_dev entry in address translation table
87 * - Allocates the number of entries required in table base on BAR
88 * size.
89 * - Allocates starting at BASE_IO_MEMORY and increases.
90 * - The size is round up to be a multiple of entry size.
91 * - CurrentIndex is incremented to keep track of the last entry.
92 * - Builds the resource entry for allocated BARs.
94 static void __init iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
96 struct resource *bar_res = &dev->resource[bar_num];
97 long bar_size = pci_resource_len(dev, bar_num);
100 * No space to allocate, quick exit, skip Allocation.
102 if (bar_size == 0)
103 return;
105 * Set Resource values.
107 spin_lock(&iomm_table_lock);
108 bar_res->name = pci_io_text;
109 bar_res->start = BASE_IO_MEMORY +
110 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
111 bar_res->end = bar_res->start + bar_size - 1;
113 * Allocate the number of table entries needed for BAR.
115 while (bar_size > 0 ) {
116 iomm_table[current_iomm_table_entry] = dev->sysdata;
117 iobar_table[current_iomm_table_entry] = bar_num;
118 bar_size -= IOMM_TABLE_ENTRY_SIZE;
119 ++current_iomm_table_entry;
121 max_io_memory = BASE_IO_MEMORY +
122 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
123 spin_unlock(&iomm_table_lock);
127 * allocate_device_bars
129 * - Allocates ALL pci_dev BAR's and updates the resources with the
130 * BAR value. BARS with zero length will have the resources
131 * The HvCallPci_getBarParms is used to get the size of the BAR
132 * space. It calls iomm_table_allocate_entry to allocate
133 * each entry.
134 * - Loops through The Bar resources(0 - 5) including the ROM
135 * is resource(6).
137 static void __init allocate_device_bars(struct pci_dev *dev)
139 int bar_num;
141 for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num)
142 iomm_table_allocate_entry(dev, bar_num);
146 * Log error information to system console.
147 * Filter out the device not there errors.
148 * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
149 * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
150 * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
152 static void pci_log_error(char *error, int bus, int subbus,
153 int agent, int hv_res)
155 if (hv_res == 0x0302)
156 return;
157 printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
158 error, bus, subbus, agent, hv_res);
162 * Look down the chain to find the matching Device Device
164 static struct device_node *find_device_node(int bus, int devfn)
166 struct device_node *node;
168 for (node = NULL; (node = of_find_all_nodes(node)); ) {
169 struct pci_dn *pdn = PCI_DN(node);
171 if (pdn && (bus == pdn->busno) && (devfn == pdn->devfn))
172 return node;
174 return NULL;
178 * iSeries_pci_final_fixup(void)
180 void __init iSeries_pci_final_fixup(void)
182 struct pci_dev *pdev = NULL;
183 struct device_node *node;
184 int num_dev = 0;
186 /* Fix up at the device node and pci_dev relationship */
187 mf_display_src(0xC9000100);
189 printk("pcibios_final_fixup\n");
190 for_each_pci_dev(pdev) {
191 const u32 *agent;
192 const u32 *sub_bus;
193 unsigned char bus = pdev->bus->number;
195 node = find_device_node(bus, pdev->devfn);
196 printk("pci dev %p (%x.%x), node %p\n", pdev, bus,
197 pdev->devfn, node);
198 if (!node) {
199 printk("PCI: Device Tree not found for 0x%016lX\n",
200 (unsigned long)pdev);
201 continue;
204 agent = of_get_property(node, "linux,agent-id", NULL);
205 sub_bus = of_get_property(node, "linux,subbus", NULL);
206 if (agent && sub_bus) {
207 u8 irq = iSeries_allocate_IRQ(bus, 0, *sub_bus);
208 int err;
210 err = HvCallXm_connectBusUnit(bus, *sub_bus,
211 *agent, irq);
212 if (err)
213 pci_log_error("Connect Bus Unit",
214 bus, *sub_bus, *agent, err);
215 else {
216 err = HvCallPci_configStore8(bus, *sub_bus,
217 *agent, PCI_INTERRUPT_LINE, irq);
218 if (err)
219 pci_log_error("PciCfgStore Irq Failed!",
220 bus, *sub_bus, *agent, err);
221 else
222 pdev->irq = irq;
226 num_dev++;
227 pdev->sysdata = node;
228 PCI_DN(node)->pcidev = pdev;
229 allocate_device_bars(pdev);
230 iSeries_Device_Information(pdev, num_dev, bus, *sub_bus);
231 iommu_devnode_init_iSeries(pdev, node);
233 iSeries_activate_IRQs();
234 mf_display_src(0xC9000200);
238 * Config space read and write functions.
239 * For now at least, we look for the device node for the bus and devfn
240 * that we are asked to access. It may be possible to translate the devfn
241 * to a subbus and deviceid more directly.
243 static u64 hv_cfg_read_func[4] = {
244 HvCallPciConfigLoad8, HvCallPciConfigLoad16,
245 HvCallPciConfigLoad32, HvCallPciConfigLoad32
248 static u64 hv_cfg_write_func[4] = {
249 HvCallPciConfigStore8, HvCallPciConfigStore16,
250 HvCallPciConfigStore32, HvCallPciConfigStore32
254 * Read PCI config space
256 static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
257 int offset, int size, u32 *val)
259 struct device_node *node = find_device_node(bus->number, devfn);
260 u64 fn;
261 struct HvCallPci_LoadReturn ret;
263 if (node == NULL)
264 return PCIBIOS_DEVICE_NOT_FOUND;
265 if (offset > 255) {
266 *val = ~0;
267 return PCIBIOS_BAD_REGISTER_NUMBER;
270 fn = hv_cfg_read_func[(size - 1) & 3];
271 HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
273 if (ret.rc != 0) {
274 *val = ~0;
275 return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
278 *val = ret.value;
279 return 0;
283 * Write PCI config space
286 static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
287 int offset, int size, u32 val)
289 struct device_node *node = find_device_node(bus->number, devfn);
290 u64 fn;
291 u64 ret;
293 if (node == NULL)
294 return PCIBIOS_DEVICE_NOT_FOUND;
295 if (offset > 255)
296 return PCIBIOS_BAD_REGISTER_NUMBER;
298 fn = hv_cfg_write_func[(size - 1) & 3];
299 ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
301 if (ret != 0)
302 return PCIBIOS_DEVICE_NOT_FOUND;
304 return 0;
307 static struct pci_ops iSeries_pci_ops = {
308 .read = iSeries_pci_read_config,
309 .write = iSeries_pci_write_config
313 * Check Return Code
314 * -> On Failure, print and log information.
315 * Increment Retry Count, if exceeds max, panic partition.
317 * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
318 * PCI: Device 23.90 ReadL Retry( 1)
319 * PCI: Device 23.90 ReadL Retry Successful(1)
321 static int check_return_code(char *type, struct device_node *dn,
322 int *retry, u64 ret)
324 if (ret != 0) {
325 struct pci_dn *pdn = PCI_DN(dn);
327 (*retry)++;
328 printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
329 type, pdn->busno, pdn->devfn,
330 *retry, (int)ret);
332 * Bump the retry and check for retry count exceeded.
333 * If, Exceeded, panic the system.
335 if (((*retry) > PCI_RETRY_MAX) &&
336 (limit_pci_retries > 0)) {
337 mf_display_src(0xB6000103);
338 panic_timeout = 0;
339 panic("PCI: Hardware I/O Error, SRC B6000103, "
340 "Automatic Reboot Disabled.\n");
342 return -1; /* Retry Try */
344 return 0;
348 * Translate the I/O Address into a device node, bar, and bar offset.
349 * Note: Make sure the passed variable end up on the stack to avoid
350 * the exposure of being device global.
352 static inline struct device_node *xlate_iomm_address(
353 const volatile void __iomem *addr,
354 u64 *dsaptr, u64 *bar_offset, const char *func)
356 unsigned long orig_addr;
357 unsigned long base_addr;
358 unsigned long ind;
359 struct device_node *dn;
361 orig_addr = (unsigned long __force)addr;
362 if ((orig_addr < BASE_IO_MEMORY) || (orig_addr >= max_io_memory)) {
363 static unsigned long last_jiffies;
364 static int num_printed;
366 if ((jiffies - last_jiffies) > 60 * HZ) {
367 last_jiffies = jiffies;
368 num_printed = 0;
370 if (num_printed++ < 10)
371 printk(KERN_ERR
372 "iSeries_%s: invalid access at IO address %p\n",
373 func, addr);
374 return NULL;
376 base_addr = orig_addr - BASE_IO_MEMORY;
377 ind = base_addr / IOMM_TABLE_ENTRY_SIZE;
378 dn = iomm_table[ind];
380 if (dn != NULL) {
381 int barnum = iobar_table[ind];
382 *dsaptr = iseries_ds_addr(dn) | (barnum << 24);
383 *bar_offset = base_addr % IOMM_TABLE_ENTRY_SIZE;
384 } else
385 panic("PCI: Invalid PCI IO address detected!\n");
386 return dn;
390 * Read MM I/O Instructions for the iSeries
391 * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
392 * else, data is returned in Big Endian format.
394 static u8 iseries_readb(const volatile void __iomem *addr)
396 u64 bar_offset;
397 u64 dsa;
398 int retry = 0;
399 struct HvCallPci_LoadReturn ret;
400 struct device_node *dn =
401 xlate_iomm_address(addr, &dsa, &bar_offset, "read_byte");
403 if (dn == NULL)
404 return 0xff;
405 do {
406 HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, bar_offset, 0);
407 } while (check_return_code("RDB", dn, &retry, ret.rc) != 0);
409 return ret.value;
412 static u16 iseries_readw_be(const volatile void __iomem *addr)
414 u64 bar_offset;
415 u64 dsa;
416 int retry = 0;
417 struct HvCallPci_LoadReturn ret;
418 struct device_node *dn =
419 xlate_iomm_address(addr, &dsa, &bar_offset, "read_word");
421 if (dn == NULL)
422 return 0xffff;
423 do {
424 HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
425 bar_offset, 0);
426 } while (check_return_code("RDW", dn, &retry, ret.rc) != 0);
428 return ret.value;
431 static u32 iseries_readl_be(const volatile void __iomem *addr)
433 u64 bar_offset;
434 u64 dsa;
435 int retry = 0;
436 struct HvCallPci_LoadReturn ret;
437 struct device_node *dn =
438 xlate_iomm_address(addr, &dsa, &bar_offset, "read_long");
440 if (dn == NULL)
441 return 0xffffffff;
442 do {
443 HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
444 bar_offset, 0);
445 } while (check_return_code("RDL", dn, &retry, ret.rc) != 0);
447 return ret.value;
451 * Write MM I/O Instructions for the iSeries
454 static void iseries_writeb(u8 data, volatile void __iomem *addr)
456 u64 bar_offset;
457 u64 dsa;
458 int retry = 0;
459 u64 rc;
460 struct device_node *dn =
461 xlate_iomm_address(addr, &dsa, &bar_offset, "write_byte");
463 if (dn == NULL)
464 return;
465 do {
466 rc = HvCall4(HvCallPciBarStore8, dsa, bar_offset, data, 0);
467 } while (check_return_code("WWB", dn, &retry, rc) != 0);
470 static void iseries_writew_be(u16 data, volatile void __iomem *addr)
472 u64 bar_offset;
473 u64 dsa;
474 int retry = 0;
475 u64 rc;
476 struct device_node *dn =
477 xlate_iomm_address(addr, &dsa, &bar_offset, "write_word");
479 if (dn == NULL)
480 return;
481 do {
482 rc = HvCall4(HvCallPciBarStore16, dsa, bar_offset, data, 0);
483 } while (check_return_code("WWW", dn, &retry, rc) != 0);
486 static void iseries_writel_be(u32 data, volatile void __iomem *addr)
488 u64 bar_offset;
489 u64 dsa;
490 int retry = 0;
491 u64 rc;
492 struct device_node *dn =
493 xlate_iomm_address(addr, &dsa, &bar_offset, "write_long");
495 if (dn == NULL)
496 return;
497 do {
498 rc = HvCall4(HvCallPciBarStore32, dsa, bar_offset, data, 0);
499 } while (check_return_code("WWL", dn, &retry, rc) != 0);
502 static u16 iseries_readw(const volatile void __iomem *addr)
504 return le16_to_cpu(iseries_readw_be(addr));
507 static u32 iseries_readl(const volatile void __iomem *addr)
509 return le32_to_cpu(iseries_readl_be(addr));
512 static void iseries_writew(u16 data, volatile void __iomem *addr)
514 iseries_writew_be(cpu_to_le16(data), addr);
517 static void iseries_writel(u32 data, volatile void __iomem *addr)
519 iseries_writel(cpu_to_le32(data), addr);
522 static void iseries_readsb(const volatile void __iomem *addr, void *buf,
523 unsigned long count)
525 u8 *dst = buf;
526 while(count-- > 0)
527 *(dst++) = iseries_readb(addr);
530 static void iseries_readsw(const volatile void __iomem *addr, void *buf,
531 unsigned long count)
533 u16 *dst = buf;
534 while(count-- > 0)
535 *(dst++) = iseries_readw_be(addr);
538 static void iseries_readsl(const volatile void __iomem *addr, void *buf,
539 unsigned long count)
541 u32 *dst = buf;
542 while(count-- > 0)
543 *(dst++) = iseries_readl_be(addr);
546 static void iseries_writesb(volatile void __iomem *addr, const void *buf,
547 unsigned long count)
549 const u8 *src = buf;
550 while(count-- > 0)
551 iseries_writeb(*(src++), addr);
554 static void iseries_writesw(volatile void __iomem *addr, const void *buf,
555 unsigned long count)
557 const u16 *src = buf;
558 while(count-- > 0)
559 iseries_writew_be(*(src++), addr);
562 static void iseries_writesl(volatile void __iomem *addr, const void *buf,
563 unsigned long count)
565 const u32 *src = buf;
566 while(count-- > 0)
567 iseries_writel_be(*(src++), addr);
570 static void iseries_memset_io(volatile void __iomem *addr, int c,
571 unsigned long n)
573 volatile char __iomem *d = addr;
575 while (n-- > 0)
576 iseries_writeb(c, d++);
579 static void iseries_memcpy_fromio(void *dest, const volatile void __iomem *src,
580 unsigned long n)
582 char *d = dest;
583 const volatile char __iomem *s = src;
585 while (n-- > 0)
586 *d++ = iseries_readb(s++);
589 static void iseries_memcpy_toio(volatile void __iomem *dest, const void *src,
590 unsigned long n)
592 const char *s = src;
593 volatile char __iomem *d = dest;
595 while (n-- > 0)
596 iseries_writeb(*s++, d++);
599 /* We only set MMIO ops. The default PIO ops will be default
600 * to the MMIO ops + pci_io_base which is 0 on iSeries as
601 * expected so both should work.
603 * Note that we don't implement the readq/writeq versions as
604 * I don't know of an HV call for doing so. Thus, the default
605 * operation will be used instead, which will fault a the value
606 * return by iSeries for MMIO addresses always hits a non mapped
607 * area. This is as good as the BUG() we used to have there.
609 static struct ppc_pci_io __initdata iseries_pci_io = {
610 .readb = iseries_readb,
611 .readw = iseries_readw,
612 .readl = iseries_readl,
613 .readw_be = iseries_readw_be,
614 .readl_be = iseries_readl_be,
615 .writeb = iseries_writeb,
616 .writew = iseries_writew,
617 .writel = iseries_writel,
618 .writew_be = iseries_writew_be,
619 .writel_be = iseries_writel_be,
620 .readsb = iseries_readsb,
621 .readsw = iseries_readsw,
622 .readsl = iseries_readsl,
623 .writesb = iseries_writesb,
624 .writesw = iseries_writesw,
625 .writesl = iseries_writesl,
626 .memset_io = iseries_memset_io,
627 .memcpy_fromio = iseries_memcpy_fromio,
628 .memcpy_toio = iseries_memcpy_toio,
632 * iSeries_pcibios_init
634 * Description:
635 * This function checks for all possible system PCI host bridges that connect
636 * PCI buses. The system hypervisor is queried as to the guest partition
637 * ownership status. A pci_controller is built for any bus which is partially
638 * owned or fully owned by this guest partition.
640 void __init iSeries_pcibios_init(void)
642 struct pci_controller *phb;
643 struct device_node *root = of_find_node_by_path("/");
644 struct device_node *node = NULL;
646 /* Install IO hooks */
647 ppc_pci_io = iseries_pci_io;
649 /* iSeries has no IO space in the common sense, it needs to set
650 * the IO base to 0
652 pci_io_base = 0;
654 if (root == NULL) {
655 printk(KERN_CRIT "iSeries_pcibios_init: can't find root "
656 "of device tree\n");
657 return;
659 while ((node = of_get_next_child(root, node)) != NULL) {
660 HvBusNumber bus;
661 const u32 *busp;
663 if ((node->type == NULL) || (strcmp(node->type, "pci") != 0))
664 continue;
666 busp = of_get_property(node, "bus-range", NULL);
667 if (busp == NULL)
668 continue;
669 bus = *busp;
670 printk("bus %d appears to exist\n", bus);
671 phb = pcibios_alloc_controller(node);
672 if (phb == NULL)
673 continue;
675 phb->pci_mem_offset = bus;
676 phb->first_busno = bus;
677 phb->last_busno = bus;
678 phb->ops = &iSeries_pci_ops;
681 of_node_put(root);
683 pci_devs_phb_init();