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>
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>
48 #define PCI_RETRY_MAX 3
49 static int limit_pci_retries
= 1; /* Set Retry Error on. */
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
;
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 * iomm_table_allocate_entry
74 * Adds pci_dev entry in address translation table
76 * - Allocates the number of entries required in table base on BAR
78 * - Allocates starting at BASE_IO_MEMORY and increases.
79 * - The size is round up to be a multiple of entry size.
80 * - CurrentIndex is incremented to keep track of the last entry.
81 * - Builds the resource entry for allocated BARs.
83 static void __init
iomm_table_allocate_entry(struct pci_dev
*dev
, int bar_num
)
85 struct resource
*bar_res
= &dev
->resource
[bar_num
];
86 long bar_size
= pci_resource_len(dev
, bar_num
);
89 * No space to allocate, quick exit, skip Allocation.
94 * Set Resource values.
96 spin_lock(&iomm_table_lock
);
97 bar_res
->name
= pci_io_text
;
98 bar_res
->start
= BASE_IO_MEMORY
+
99 IOMM_TABLE_ENTRY_SIZE
* current_iomm_table_entry
;
100 bar_res
->end
= bar_res
->start
+ bar_size
- 1;
102 * Allocate the number of table entries needed for BAR.
104 while (bar_size
> 0 ) {
105 iomm_table
[current_iomm_table_entry
] = dev
->sysdata
;
106 iobar_table
[current_iomm_table_entry
] = bar_num
;
107 bar_size
-= IOMM_TABLE_ENTRY_SIZE
;
108 ++current_iomm_table_entry
;
110 max_io_memory
= BASE_IO_MEMORY
+
111 IOMM_TABLE_ENTRY_SIZE
* current_iomm_table_entry
;
112 spin_unlock(&iomm_table_lock
);
116 * allocate_device_bars
118 * - Allocates ALL pci_dev BAR's and updates the resources with the
119 * BAR value. BARS with zero length will have the resources
120 * The HvCallPci_getBarParms is used to get the size of the BAR
121 * space. It calls iomm_table_allocate_entry to allocate
123 * - Loops through The Bar resources(0 - 5) including the ROM
126 static void __init
allocate_device_bars(struct pci_dev
*dev
)
130 for (bar_num
= 0; bar_num
<= PCI_ROM_RESOURCE
; ++bar_num
)
131 iomm_table_allocate_entry(dev
, bar_num
);
135 * Log error information to system console.
136 * Filter out the device not there errors.
137 * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
138 * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
139 * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
141 static void pci_log_error(char *error
, int bus
, int subbus
,
142 int agent
, int hv_res
)
144 if (hv_res
== 0x0302)
146 printk(KERN_ERR
"PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
147 error
, bus
, subbus
, agent
, hv_res
);
151 * Look down the chain to find the matching Device Device
153 static struct device_node
*find_device_node(int bus
, int devfn
)
155 struct device_node
*node
;
157 for (node
= NULL
; (node
= of_find_all_nodes(node
)); ) {
158 struct pci_dn
*pdn
= PCI_DN(node
);
160 if (pdn
&& (bus
== pdn
->busno
) && (devfn
== pdn
->devfn
))
167 * iSeries_pci_final_fixup(void)
169 void __init
iSeries_pci_final_fixup(void)
171 struct pci_dev
*pdev
= NULL
;
172 struct device_node
*node
;
175 /* Fix up at the device node and pci_dev relationship */
176 mf_display_src(0xC9000100);
178 printk("pcibios_final_fixup\n");
179 for_each_pci_dev(pdev
) {
180 node
= find_device_node(pdev
->bus
->number
, pdev
->devfn
);
181 printk("pci dev %p (%x.%x), node %p\n", pdev
,
182 pdev
->bus
->number
, pdev
->devfn
, node
);
185 struct pci_dn
*pdn
= PCI_DN(node
);
188 agent
= of_get_property(node
, "linux,agent-id", NULL
);
189 if ((pdn
!= NULL
) && (agent
!= NULL
)) {
190 u8 irq
= iSeries_allocate_IRQ(pdn
->busno
, 0,
194 err
= HvCallXm_connectBusUnit(pdn
->busno
, pdn
->bussubno
,
197 pci_log_error("Connect Bus Unit",
198 pdn
->busno
, pdn
->bussubno
, *agent
, err
);
200 err
= HvCallPci_configStore8(pdn
->busno
, pdn
->bussubno
,
205 pci_log_error("PciCfgStore Irq Failed!",
206 pdn
->busno
, pdn
->bussubno
, *agent
, err
);
213 pdev
->sysdata
= (void *)node
;
214 PCI_DN(node
)->pcidev
= pdev
;
215 allocate_device_bars(pdev
);
216 iSeries_Device_Information(pdev
, num_dev
);
217 iommu_devnode_init_iSeries(pdev
, node
);
219 printk("PCI: Device Tree not found for 0x%016lX\n",
220 (unsigned long)pdev
);
222 iSeries_activate_IRQs();
223 mf_display_src(0xC9000200);
228 * Returns the device node for the passed pci_dev
229 * Sanity Check Node PciDev to passed pci_dev
230 * If none is found, returns a NULL which the client must handle.
232 static struct device_node
*get_device_node(struct pci_dev
*pdev
)
234 struct device_node
*node
;
236 node
= pdev
->sysdata
;
237 if (node
== NULL
|| PCI_DN(node
)->pcidev
!= pdev
)
238 node
= find_device_node(pdev
->bus
->number
, pdev
->devfn
);
244 * Config space read and write functions.
245 * For now at least, we look for the device node for the bus and devfn
246 * that we are asked to access. It may be possible to translate the devfn
247 * to a subbus and deviceid more directly.
249 static u64 hv_cfg_read_func
[4] = {
250 HvCallPciConfigLoad8
, HvCallPciConfigLoad16
,
251 HvCallPciConfigLoad32
, HvCallPciConfigLoad32
254 static u64 hv_cfg_write_func
[4] = {
255 HvCallPciConfigStore8
, HvCallPciConfigStore16
,
256 HvCallPciConfigStore32
, HvCallPciConfigStore32
260 * Read PCI config space
262 static int iSeries_pci_read_config(struct pci_bus
*bus
, unsigned int devfn
,
263 int offset
, int size
, u32
*val
)
265 struct device_node
*node
= find_device_node(bus
->number
, devfn
);
267 struct HvCallPci_LoadReturn ret
;
270 return PCIBIOS_DEVICE_NOT_FOUND
;
273 return PCIBIOS_BAD_REGISTER_NUMBER
;
276 fn
= hv_cfg_read_func
[(size
- 1) & 3];
277 HvCall3Ret16(fn
, &ret
, iseries_ds_addr(node
), offset
, 0);
281 return PCIBIOS_DEVICE_NOT_FOUND
; /* or something */
289 * Write PCI config space
292 static int iSeries_pci_write_config(struct pci_bus
*bus
, unsigned int devfn
,
293 int offset
, int size
, u32 val
)
295 struct device_node
*node
= find_device_node(bus
->number
, devfn
);
300 return PCIBIOS_DEVICE_NOT_FOUND
;
302 return PCIBIOS_BAD_REGISTER_NUMBER
;
304 fn
= hv_cfg_write_func
[(size
- 1) & 3];
305 ret
= HvCall4(fn
, iseries_ds_addr(node
), offset
, val
, 0);
308 return PCIBIOS_DEVICE_NOT_FOUND
;
313 static struct pci_ops iSeries_pci_ops
= {
314 .read
= iSeries_pci_read_config
,
315 .write
= iSeries_pci_write_config
320 * -> On Failure, print and log information.
321 * Increment Retry Count, if exceeds max, panic partition.
323 * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
324 * PCI: Device 23.90 ReadL Retry( 1)
325 * PCI: Device 23.90 ReadL Retry Successful(1)
327 static int check_return_code(char *type
, struct device_node
*dn
,
331 struct pci_dn
*pdn
= PCI_DN(dn
);
334 printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
335 type
, pdn
->busno
, pdn
->devfn
,
338 * Bump the retry and check for retry count exceeded.
339 * If, Exceeded, panic the system.
341 if (((*retry
) > PCI_RETRY_MAX
) &&
342 (limit_pci_retries
> 0)) {
343 mf_display_src(0xB6000103);
345 panic("PCI: Hardware I/O Error, SRC B6000103, "
346 "Automatic Reboot Disabled.\n");
348 return -1; /* Retry Try */
354 * Translate the I/O Address into a device node, bar, and bar offset.
355 * Note: Make sure the passed variable end up on the stack to avoid
356 * the exposure of being device global.
358 static inline struct device_node
*xlate_iomm_address(
359 const volatile void __iomem
*addr
,
360 u64
*dsaptr
, u64
*bar_offset
)
362 unsigned long orig_addr
;
363 unsigned long base_addr
;
365 struct device_node
*dn
;
367 orig_addr
= (unsigned long __force
)addr
;
368 if ((orig_addr
< BASE_IO_MEMORY
) || (orig_addr
>= max_io_memory
))
370 base_addr
= orig_addr
- BASE_IO_MEMORY
;
371 ind
= base_addr
/ IOMM_TABLE_ENTRY_SIZE
;
372 dn
= iomm_table
[ind
];
375 int barnum
= iobar_table
[ind
];
376 *dsaptr
= iseries_ds_addr(dn
) | (barnum
<< 24);
377 *bar_offset
= base_addr
% IOMM_TABLE_ENTRY_SIZE
;
379 panic("PCI: Invalid PCI IO address detected!\n");
384 * Read MM I/O Instructions for the iSeries
385 * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
386 * else, data is returned in Big Endian format.
388 static u8
iSeries_read_byte(const volatile void __iomem
*addr
)
393 struct HvCallPci_LoadReturn ret
;
394 struct device_node
*dn
=
395 xlate_iomm_address(addr
, &dsa
, &bar_offset
);
398 static unsigned long last_jiffies
;
399 static int num_printed
;
401 if ((jiffies
- last_jiffies
) > 60 * HZ
) {
402 last_jiffies
= jiffies
;
405 if (num_printed
++ < 10)
406 printk(KERN_ERR
"iSeries_read_byte: invalid access at IO address %p\n",
411 HvCall3Ret16(HvCallPciBarLoad8
, &ret
, dsa
, bar_offset
, 0);
412 } while (check_return_code("RDB", dn
, &retry
, ret
.rc
) != 0);
417 static u16
iSeries_read_word(const volatile void __iomem
*addr
)
422 struct HvCallPci_LoadReturn ret
;
423 struct device_node
*dn
=
424 xlate_iomm_address(addr
, &dsa
, &bar_offset
);
427 static unsigned long last_jiffies
;
428 static int num_printed
;
430 if ((jiffies
- last_jiffies
) > 60 * HZ
) {
431 last_jiffies
= jiffies
;
434 if (num_printed
++ < 10)
435 printk(KERN_ERR
"iSeries_read_word: invalid access at IO address %p\n",
440 HvCall3Ret16(HvCallPciBarLoad16
, &ret
, dsa
,
442 } while (check_return_code("RDW", dn
, &retry
, ret
.rc
) != 0);
447 static u32
iSeries_read_long(const volatile void __iomem
*addr
)
452 struct HvCallPci_LoadReturn ret
;
453 struct device_node
*dn
=
454 xlate_iomm_address(addr
, &dsa
, &bar_offset
);
457 static unsigned long last_jiffies
;
458 static int num_printed
;
460 if ((jiffies
- last_jiffies
) > 60 * HZ
) {
461 last_jiffies
= jiffies
;
464 if (num_printed
++ < 10)
465 printk(KERN_ERR
"iSeries_read_long: invalid access at IO address %p\n",
470 HvCall3Ret16(HvCallPciBarLoad32
, &ret
, dsa
,
472 } while (check_return_code("RDL", dn
, &retry
, ret
.rc
) != 0);
478 * Write MM I/O Instructions for the iSeries
481 static void iSeries_write_byte(u8 data
, volatile void __iomem
*addr
)
487 struct device_node
*dn
=
488 xlate_iomm_address(addr
, &dsa
, &bar_offset
);
491 static unsigned long last_jiffies
;
492 static int num_printed
;
494 if ((jiffies
- last_jiffies
) > 60 * HZ
) {
495 last_jiffies
= jiffies
;
498 if (num_printed
++ < 10)
499 printk(KERN_ERR
"iSeries_write_byte: invalid access at IO address %p\n", addr
);
503 rc
= HvCall4(HvCallPciBarStore8
, dsa
, bar_offset
, data
, 0);
504 } while (check_return_code("WWB", dn
, &retry
, rc
) != 0);
507 static void iSeries_write_word(u16 data
, volatile void __iomem
*addr
)
513 struct device_node
*dn
=
514 xlate_iomm_address(addr
, &dsa
, &bar_offset
);
517 static unsigned long last_jiffies
;
518 static int num_printed
;
520 if ((jiffies
- last_jiffies
) > 60 * HZ
) {
521 last_jiffies
= jiffies
;
524 if (num_printed
++ < 10)
525 printk(KERN_ERR
"iSeries_write_word: invalid access at IO address %p\n",
530 rc
= HvCall4(HvCallPciBarStore16
, dsa
, bar_offset
, data
, 0);
531 } while (check_return_code("WWW", dn
, &retry
, rc
) != 0);
534 static void iSeries_write_long(u32 data
, volatile void __iomem
*addr
)
540 struct device_node
*dn
=
541 xlate_iomm_address(addr
, &dsa
, &bar_offset
);
544 static unsigned long last_jiffies
;
545 static int num_printed
;
547 if ((jiffies
- last_jiffies
) > 60 * HZ
) {
548 last_jiffies
= jiffies
;
551 if (num_printed
++ < 10)
552 printk(KERN_ERR
"iSeries_write_long: invalid access at IO address %p\n",
557 rc
= HvCall4(HvCallPciBarStore32
, dsa
, bar_offset
, data
, 0);
558 } while (check_return_code("WWL", dn
, &retry
, rc
) != 0);
561 static u8
iseries_readb(const volatile void __iomem
*addr
)
563 return iSeries_read_byte(addr
);
566 static u16
iseries_readw(const volatile void __iomem
*addr
)
568 return le16_to_cpu(iSeries_read_word(addr
));
571 static u32
iseries_readl(const volatile void __iomem
*addr
)
573 return le32_to_cpu(iSeries_read_long(addr
));
576 static u16
iseries_readw_be(const volatile void __iomem
*addr
)
578 return iSeries_read_word(addr
);
581 static u32
iseries_readl_be(const volatile void __iomem
*addr
)
583 return iSeries_read_long(addr
);
586 static void iseries_writeb(u8 data
, volatile void __iomem
*addr
)
588 iSeries_write_byte(data
, addr
);
591 static void iseries_writew(u16 data
, volatile void __iomem
*addr
)
593 iSeries_write_word(cpu_to_le16(data
), addr
);
596 static void iseries_writel(u32 data
, volatile void __iomem
*addr
)
598 iSeries_write_long(cpu_to_le32(data
), addr
);
601 static void iseries_writew_be(u16 data
, volatile void __iomem
*addr
)
603 iSeries_write_word(data
, addr
);
606 static void iseries_writel_be(u32 data
, volatile void __iomem
*addr
)
608 iSeries_write_long(data
, addr
);
611 static void iseries_readsb(const volatile void __iomem
*addr
, void *buf
,
616 *(dst
++) = iSeries_read_byte(addr
);
619 static void iseries_readsw(const volatile void __iomem
*addr
, void *buf
,
624 *(dst
++) = iSeries_read_word(addr
);
627 static void iseries_readsl(const volatile void __iomem
*addr
, void *buf
,
632 *(dst
++) = iSeries_read_long(addr
);
635 static void iseries_writesb(volatile void __iomem
*addr
, const void *buf
,
640 iSeries_write_byte(*(src
++), addr
);
643 static void iseries_writesw(volatile void __iomem
*addr
, const void *buf
,
646 const u16
*src
= buf
;
648 iSeries_write_word(*(src
++), addr
);
651 static void iseries_writesl(volatile void __iomem
*addr
, const void *buf
,
654 const u32
*src
= buf
;
656 iSeries_write_long(*(src
++), addr
);
659 static void iseries_memset_io(volatile void __iomem
*addr
, int c
,
662 volatile char __iomem
*d
= addr
;
665 iSeries_write_byte(c
, d
++);
668 static void iseries_memcpy_fromio(void *dest
, const volatile void __iomem
*src
,
672 const volatile char __iomem
*s
= src
;
675 *d
++ = iSeries_read_byte(s
++);
678 static void iseries_memcpy_toio(volatile void __iomem
*dest
, const void *src
,
682 volatile char __iomem
*d
= dest
;
685 iSeries_write_byte(*s
++, d
++);
688 /* We only set MMIO ops. The default PIO ops will be default
689 * to the MMIO ops + pci_io_base which is 0 on iSeries as
690 * expected so both should work.
692 * Note that we don't implement the readq/writeq versions as
693 * I don't know of an HV call for doing so. Thus, the default
694 * operation will be used instead, which will fault a the value
695 * return by iSeries for MMIO addresses always hits a non mapped
696 * area. This is as good as the BUG() we used to have there.
698 static struct ppc_pci_io __initdata iseries_pci_io
= {
699 .readb
= iseries_readb
,
700 .readw
= iseries_readw
,
701 .readl
= iseries_readl
,
702 .readw_be
= iseries_readw_be
,
703 .readl_be
= iseries_readl_be
,
704 .writeb
= iseries_writeb
,
705 .writew
= iseries_writew
,
706 .writel
= iseries_writel
,
707 .writew_be
= iseries_writew_be
,
708 .writel_be
= iseries_writel_be
,
709 .readsb
= iseries_readsb
,
710 .readsw
= iseries_readsw
,
711 .readsl
= iseries_readsl
,
712 .writesb
= iseries_writesb
,
713 .writesw
= iseries_writesw
,
714 .writesl
= iseries_writesl
,
715 .memset_io
= iseries_memset_io
,
716 .memcpy_fromio
= iseries_memcpy_fromio
,
717 .memcpy_toio
= iseries_memcpy_toio
,
721 * iSeries_pcibios_init
724 * This function checks for all possible system PCI host bridges that connect
725 * PCI buses. The system hypervisor is queried as to the guest partition
726 * ownership status. A pci_controller is built for any bus which is partially
727 * owned or fully owned by this guest partition.
729 void __init
iSeries_pcibios_init(void)
731 struct pci_controller
*phb
;
732 struct device_node
*root
= of_find_node_by_path("/");
733 struct device_node
*node
= NULL
;
735 /* Install IO hooks */
736 ppc_pci_io
= iseries_pci_io
;
738 /* iSeries has no IO space in the common sense, it needs to set
744 printk(KERN_CRIT
"iSeries_pcibios_init: can't find root "
748 while ((node
= of_get_next_child(root
, node
)) != NULL
) {
752 if ((node
->type
== NULL
) || (strcmp(node
->type
, "pci") != 0))
755 busp
= of_get_property(node
, "bus-range", NULL
);
759 printk("bus %d appears to exist\n", bus
);
760 phb
= pcibios_alloc_controller(node
);
764 phb
->pci_mem_offset
= bus
;
765 phb
->first_busno
= bus
;
766 phb
->last_busno
= bus
;
767 phb
->ops
= &iSeries_pci_ops
;