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/ide.h>
28 #include <linux/pci.h>
33 #include <asm/machdep.h>
34 #include <asm/pci-bridge.h>
35 #include <asm/iommu.h>
36 #include <asm/abs_addr.h>
37 #include <asm/firmware.h>
39 #include <asm/iseries/hv_call_xm.h>
40 #include <asm/iseries/mf.h>
41 #include <asm/iseries/iommu.h>
43 #include <asm/ppc-pci.h>
50 * Forward declares of prototypes.
52 static struct device_node
*find_Device_Node(int bus
, int devfn
);
54 static int Pci_Retry_Max
= 3; /* Only retry 3 times */
55 static int Pci_Error_Flag
= 1; /* Set Retry Error on. */
57 static struct pci_ops iSeries_pci_ops
;
61 * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
63 #define IOMM_TABLE_MAX_ENTRIES 1024
64 #define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
65 #define BASE_IO_MEMORY 0xE000000000000000UL
67 static unsigned long max_io_memory
= BASE_IO_MEMORY
;
68 static long current_iomm_table_entry
;
73 static struct device_node
*iomm_table
[IOMM_TABLE_MAX_ENTRIES
];
74 static u8 iobar_table
[IOMM_TABLE_MAX_ENTRIES
];
76 static const char pci_io_text
[] = "iSeries PCI I/O";
77 static DEFINE_SPINLOCK(iomm_table_lock
);
80 * iomm_table_allocate_entry
82 * Adds pci_dev entry in address translation table
84 * - Allocates the number of entries required in table base on BAR
86 * - Allocates starting at BASE_IO_MEMORY and increases.
87 * - The size is round up to be a multiple of entry size.
88 * - CurrentIndex is incremented to keep track of the last entry.
89 * - Builds the resource entry for allocated BARs.
91 static void iomm_table_allocate_entry(struct pci_dev
*dev
, int bar_num
)
93 struct resource
*bar_res
= &dev
->resource
[bar_num
];
94 long bar_size
= pci_resource_len(dev
, bar_num
);
97 * No space to allocate, quick exit, skip Allocation.
102 * Set Resource values.
104 spin_lock(&iomm_table_lock
);
105 bar_res
->name
= pci_io_text
;
106 bar_res
->start
= BASE_IO_MEMORY
+
107 IOMM_TABLE_ENTRY_SIZE
* current_iomm_table_entry
;
108 bar_res
->end
= bar_res
->start
+ bar_size
- 1;
110 * Allocate the number of table entries needed for BAR.
112 while (bar_size
> 0 ) {
113 iomm_table
[current_iomm_table_entry
] = dev
->sysdata
;
114 iobar_table
[current_iomm_table_entry
] = bar_num
;
115 bar_size
-= IOMM_TABLE_ENTRY_SIZE
;
116 ++current_iomm_table_entry
;
118 max_io_memory
= BASE_IO_MEMORY
+
119 IOMM_TABLE_ENTRY_SIZE
* current_iomm_table_entry
;
120 spin_unlock(&iomm_table_lock
);
124 * allocate_device_bars
126 * - Allocates ALL pci_dev BAR's and updates the resources with the
127 * BAR value. BARS with zero length will have the resources
128 * The HvCallPci_getBarParms is used to get the size of the BAR
129 * space. It calls iomm_table_allocate_entry to allocate
131 * - Loops through The Bar resources(0 - 5) including the ROM
134 static void allocate_device_bars(struct pci_dev
*dev
)
138 for (bar_num
= 0; bar_num
<= PCI_ROM_RESOURCE
; ++bar_num
)
139 iomm_table_allocate_entry(dev
, bar_num
);
143 * Log error information to system console.
144 * Filter out the device not there errors.
145 * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
146 * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
147 * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
149 static void pci_Log_Error(char *Error_Text
, int Bus
, int SubBus
,
150 int AgentId
, int HvRc
)
154 printk(KERN_ERR
"PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
155 Error_Text
, Bus
, SubBus
, AgentId
, HvRc
);
159 * iSeries_pci_final_fixup(void)
161 void __init
iSeries_pci_final_fixup(void)
163 struct pci_dev
*pdev
= NULL
;
164 struct device_node
*node
;
167 /* Fix up at the device node and pci_dev relationship */
168 mf_display_src(0xC9000100);
170 printk("pcibios_final_fixup\n");
171 for_each_pci_dev(pdev
) {
172 node
= find_Device_Node(pdev
->bus
->number
, pdev
->devfn
);
173 printk("pci dev %p (%x.%x), node %p\n", pdev
,
174 pdev
->bus
->number
, pdev
->devfn
, node
);
177 struct pci_dn
*pdn
= PCI_DN(node
);
180 agent
= get_property(node
, "linux,agent-id", NULL
);
181 if ((pdn
!= NULL
) && (agent
!= NULL
)) {
182 u8 irq
= iSeries_allocate_IRQ(pdn
->busno
, 0,
186 err
= HvCallXm_connectBusUnit(pdn
->busno
, pdn
->bussubno
,
189 pci_Log_Error("Connect Bus Unit",
190 pdn
->busno
, pdn
->bussubno
, *agent
, err
);
192 err
= HvCallPci_configStore8(pdn
->busno
, pdn
->bussubno
,
197 pci_Log_Error("PciCfgStore Irq Failed!",
198 pdn
->busno
, pdn
->bussubno
, *agent
, err
);
205 pdev
->sysdata
= (void *)node
;
206 PCI_DN(node
)->pcidev
= pdev
;
207 allocate_device_bars(pdev
);
208 iSeries_Device_Information(pdev
, DeviceCount
);
209 iommu_devnode_init_iSeries(pdev
, node
);
211 printk("PCI: Device Tree not found for 0x%016lX\n",
212 (unsigned long)pdev
);
214 iSeries_activate_IRQs();
215 mf_display_src(0xC9000200);
219 * Look down the chain to find the matching Device Device
221 static struct device_node
*find_Device_Node(int bus
, int devfn
)
223 struct device_node
*node
;
225 for (node
= NULL
; (node
= of_find_all_nodes(node
)); ) {
226 struct pci_dn
*pdn
= PCI_DN(node
);
228 if (pdn
&& (bus
== pdn
->busno
) && (devfn
== pdn
->devfn
))
236 * Returns the device node for the passed pci_dev
237 * Sanity Check Node PciDev to passed pci_dev
238 * If none is found, returns a NULL which the client must handle.
240 static struct device_node
*get_Device_Node(struct pci_dev
*pdev
)
242 struct device_node
*node
;
244 node
= pdev
->sysdata
;
245 if (node
== NULL
|| PCI_DN(node
)->pcidev
!= pdev
)
246 node
= find_Device_Node(pdev
->bus
->number
, pdev
->devfn
);
252 * Config space read and write functions.
253 * For now at least, we look for the device node for the bus and devfn
254 * that we are asked to access. It may be possible to translate the devfn
255 * to a subbus and deviceid more directly.
257 static u64 hv_cfg_read_func
[4] = {
258 HvCallPciConfigLoad8
, HvCallPciConfigLoad16
,
259 HvCallPciConfigLoad32
, HvCallPciConfigLoad32
262 static u64 hv_cfg_write_func
[4] = {
263 HvCallPciConfigStore8
, HvCallPciConfigStore16
,
264 HvCallPciConfigStore32
, HvCallPciConfigStore32
268 * Read PCI config space
270 static int iSeries_pci_read_config(struct pci_bus
*bus
, unsigned int devfn
,
271 int offset
, int size
, u32
*val
)
273 struct device_node
*node
= find_Device_Node(bus
->number
, devfn
);
275 struct HvCallPci_LoadReturn ret
;
278 return PCIBIOS_DEVICE_NOT_FOUND
;
281 return PCIBIOS_BAD_REGISTER_NUMBER
;
284 fn
= hv_cfg_read_func
[(size
- 1) & 3];
285 HvCall3Ret16(fn
, &ret
, iseries_ds_addr(node
), offset
, 0);
289 return PCIBIOS_DEVICE_NOT_FOUND
; /* or something */
297 * Write PCI config space
300 static int iSeries_pci_write_config(struct pci_bus
*bus
, unsigned int devfn
,
301 int offset
, int size
, u32 val
)
303 struct device_node
*node
= find_Device_Node(bus
->number
, devfn
);
308 return PCIBIOS_DEVICE_NOT_FOUND
;
310 return PCIBIOS_BAD_REGISTER_NUMBER
;
312 fn
= hv_cfg_write_func
[(size
- 1) & 3];
313 ret
= HvCall4(fn
, iseries_ds_addr(node
), offset
, val
, 0);
316 return PCIBIOS_DEVICE_NOT_FOUND
;
321 static struct pci_ops iSeries_pci_ops
= {
322 .read
= iSeries_pci_read_config
,
323 .write
= iSeries_pci_write_config
328 * -> On Failure, print and log information.
329 * Increment Retry Count, if exceeds max, panic partition.
331 * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
332 * PCI: Device 23.90 ReadL Retry( 1)
333 * PCI: Device 23.90 ReadL Retry Successful(1)
335 static int CheckReturnCode(char *TextHdr
, struct device_node
*DevNode
,
339 struct pci_dn
*pdn
= PCI_DN(DevNode
);
342 printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
343 TextHdr
, pdn
->busno
, pdn
->devfn
,
346 * Bump the retry and check for retry count exceeded.
347 * If, Exceeded, panic the system.
349 if (((*retry
) > Pci_Retry_Max
) &&
350 (Pci_Error_Flag
> 0)) {
351 mf_display_src(0xB6000103);
353 panic("PCI: Hardware I/O Error, SRC B6000103, "
354 "Automatic Reboot Disabled.\n");
356 return -1; /* Retry Try */
362 * Translate the I/O Address into a device node, bar, and bar offset.
363 * Note: Make sure the passed variable end up on the stack to avoid
364 * the exposure of being device global.
366 static inline struct device_node
*xlate_iomm_address(
367 const volatile void __iomem
*IoAddress
,
368 u64
*dsaptr
, u64
*BarOffsetPtr
)
370 unsigned long OrigIoAddr
;
371 unsigned long BaseIoAddr
;
372 unsigned long TableIndex
;
373 struct device_node
*DevNode
;
375 OrigIoAddr
= (unsigned long __force
)IoAddress
;
376 if ((OrigIoAddr
< BASE_IO_MEMORY
) || (OrigIoAddr
>= max_io_memory
))
378 BaseIoAddr
= OrigIoAddr
- BASE_IO_MEMORY
;
379 TableIndex
= BaseIoAddr
/ IOMM_TABLE_ENTRY_SIZE
;
380 DevNode
= iomm_table
[TableIndex
];
382 if (DevNode
!= NULL
) {
383 int barnum
= iobar_table
[TableIndex
];
384 *dsaptr
= iseries_ds_addr(DevNode
) | (barnum
<< 24);
385 *BarOffsetPtr
= BaseIoAddr
% IOMM_TABLE_ENTRY_SIZE
;
387 panic("PCI: Invalid PCI IoAddress detected!\n");
392 * Read MM I/O Instructions for the iSeries
393 * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
394 * else, data is returned in Big Endian format.
396 static u8
iSeries_Read_Byte(const volatile void __iomem
*IoAddress
)
401 struct HvCallPci_LoadReturn ret
;
402 struct device_node
*DevNode
=
403 xlate_iomm_address(IoAddress
, &dsa
, &BarOffset
);
405 if (DevNode
== NULL
) {
406 static unsigned long last_jiffies
;
407 static int num_printed
;
409 if ((jiffies
- last_jiffies
) > 60 * HZ
) {
410 last_jiffies
= jiffies
;
413 if (num_printed
++ < 10)
414 printk(KERN_ERR
"iSeries_Read_Byte: invalid access at IO address %p\n",
419 HvCall3Ret16(HvCallPciBarLoad8
, &ret
, dsa
, BarOffset
, 0);
420 } while (CheckReturnCode("RDB", DevNode
, &retry
, ret
.rc
) != 0);
425 static u16
iSeries_Read_Word(const volatile void __iomem
*IoAddress
)
430 struct HvCallPci_LoadReturn ret
;
431 struct device_node
*DevNode
=
432 xlate_iomm_address(IoAddress
, &dsa
, &BarOffset
);
434 if (DevNode
== NULL
) {
435 static unsigned long last_jiffies
;
436 static int num_printed
;
438 if ((jiffies
- last_jiffies
) > 60 * HZ
) {
439 last_jiffies
= jiffies
;
442 if (num_printed
++ < 10)
443 printk(KERN_ERR
"iSeries_Read_Word: invalid access at IO address %p\n",
448 HvCall3Ret16(HvCallPciBarLoad16
, &ret
, dsa
,
450 } while (CheckReturnCode("RDW", DevNode
, &retry
, ret
.rc
) != 0);
455 static u32
iSeries_Read_Long(const volatile void __iomem
*IoAddress
)
460 struct HvCallPci_LoadReturn ret
;
461 struct device_node
*DevNode
=
462 xlate_iomm_address(IoAddress
, &dsa
, &BarOffset
);
464 if (DevNode
== NULL
) {
465 static unsigned long last_jiffies
;
466 static int num_printed
;
468 if ((jiffies
- last_jiffies
) > 60 * HZ
) {
469 last_jiffies
= jiffies
;
472 if (num_printed
++ < 10)
473 printk(KERN_ERR
"iSeries_Read_Long: invalid access at IO address %p\n",
478 HvCall3Ret16(HvCallPciBarLoad32
, &ret
, dsa
,
480 } while (CheckReturnCode("RDL", DevNode
, &retry
, ret
.rc
) != 0);
486 * Write MM I/O Instructions for the iSeries
489 static void iSeries_Write_Byte(u8 data
, volatile void __iomem
*IoAddress
)
495 struct device_node
*DevNode
=
496 xlate_iomm_address(IoAddress
, &dsa
, &BarOffset
);
498 if (DevNode
== NULL
) {
499 static unsigned long last_jiffies
;
500 static int num_printed
;
502 if ((jiffies
- last_jiffies
) > 60 * HZ
) {
503 last_jiffies
= jiffies
;
506 if (num_printed
++ < 10)
507 printk(KERN_ERR
"iSeries_Write_Byte: invalid access at IO address %p\n", IoAddress
);
511 rc
= HvCall4(HvCallPciBarStore8
, dsa
, BarOffset
, data
, 0);
512 } while (CheckReturnCode("WWB", DevNode
, &retry
, rc
) != 0);
515 static void iSeries_Write_Word(u16 data
, volatile void __iomem
*IoAddress
)
521 struct device_node
*DevNode
=
522 xlate_iomm_address(IoAddress
, &dsa
, &BarOffset
);
524 if (DevNode
== NULL
) {
525 static unsigned long last_jiffies
;
526 static int num_printed
;
528 if ((jiffies
- last_jiffies
) > 60 * HZ
) {
529 last_jiffies
= jiffies
;
532 if (num_printed
++ < 10)
533 printk(KERN_ERR
"iSeries_Write_Word: invalid access at IO address %p\n",
538 rc
= HvCall4(HvCallPciBarStore16
, dsa
, BarOffset
, data
, 0);
539 } while (CheckReturnCode("WWW", DevNode
, &retry
, rc
) != 0);
542 static void iSeries_Write_Long(u32 data
, volatile void __iomem
*IoAddress
)
548 struct device_node
*DevNode
=
549 xlate_iomm_address(IoAddress
, &dsa
, &BarOffset
);
551 if (DevNode
== NULL
) {
552 static unsigned long last_jiffies
;
553 static int num_printed
;
555 if ((jiffies
- last_jiffies
) > 60 * HZ
) {
556 last_jiffies
= jiffies
;
559 if (num_printed
++ < 10)
560 printk(KERN_ERR
"iSeries_Write_Long: invalid access at IO address %p\n",
565 rc
= HvCall4(HvCallPciBarStore32
, dsa
, BarOffset
, data
, 0);
566 } while (CheckReturnCode("WWL", DevNode
, &retry
, rc
) != 0);
569 static u8
iseries_readb(const volatile void __iomem
*addr
)
571 return iSeries_Read_Byte(addr
);
574 static u16
iseries_readw(const volatile void __iomem
*addr
)
576 return le16_to_cpu(iSeries_Read_Word(addr
));
579 static u32
iseries_readl(const volatile void __iomem
*addr
)
581 return le32_to_cpu(iSeries_Read_Long(addr
));
584 static u16
iseries_readw_be(const volatile void __iomem
*addr
)
586 return iSeries_Read_Word(addr
);
589 static u32
iseries_readl_be(const volatile void __iomem
*addr
)
591 return iSeries_Read_Long(addr
);
594 static void iseries_writeb(u8 data
, volatile void __iomem
*addr
)
596 iSeries_Write_Byte(data
, addr
);
599 static void iseries_writew(u16 data
, volatile void __iomem
*addr
)
601 iSeries_Write_Word(cpu_to_le16(data
), addr
);
604 static void iseries_writel(u32 data
, volatile void __iomem
*addr
)
606 iSeries_Write_Long(cpu_to_le32(data
), addr
);
609 static void iseries_writew_be(u16 data
, volatile void __iomem
*addr
)
611 iSeries_Write_Word(data
, addr
);
614 static void iseries_writel_be(u32 data
, volatile void __iomem
*addr
)
616 iSeries_Write_Long(data
, addr
);
619 static void iseries_readsb(const volatile void __iomem
*addr
, void *buf
,
624 *(dst
++) = iSeries_Read_Byte(addr
);
627 static void iseries_readsw(const volatile void __iomem
*addr
, void *buf
,
632 *(dst
++) = iSeries_Read_Word(addr
);
635 static void iseries_readsl(const volatile void __iomem
*addr
, void *buf
,
640 *(dst
++) = iSeries_Read_Long(addr
);
643 static void iseries_writesb(volatile void __iomem
*addr
, const void *buf
,
648 iSeries_Write_Byte(*(src
++), addr
);
651 static void iseries_writesw(volatile void __iomem
*addr
, const void *buf
,
654 const u16
*src
= buf
;
656 iSeries_Write_Word(*(src
++), addr
);
659 static void iseries_writesl(volatile void __iomem
*addr
, const void *buf
,
662 const u32
*src
= buf
;
664 iSeries_Write_Long(*(src
++), addr
);
667 static void iseries_memset_io(volatile void __iomem
*addr
, int c
,
670 volatile char __iomem
*d
= addr
;
673 iSeries_Write_Byte(c
, d
++);
676 static void iseries_memcpy_fromio(void *dest
, const volatile void __iomem
*src
,
680 const volatile char __iomem
*s
= src
;
683 *d
++ = iSeries_Read_Byte(s
++);
686 static void iseries_memcpy_toio(volatile void __iomem
*dest
, const void *src
,
690 volatile char __iomem
*d
= dest
;
693 iSeries_Write_Byte(*s
++, d
++);
696 /* We only set MMIO ops. The default PIO ops will be default
697 * to the MMIO ops + pci_io_base which is 0 on iSeries as
698 * expected so both should work.
700 * Note that we don't implement the readq/writeq versions as
701 * I don't know of an HV call for doing so. Thus, the default
702 * operation will be used instead, which will fault a the value
703 * return by iSeries for MMIO addresses always hits a non mapped
704 * area. This is as good as the BUG() we used to have there.
706 static struct ppc_pci_io __initdata iseries_pci_io
= {
707 .readb
= iseries_readb
,
708 .readw
= iseries_readw
,
709 .readl
= iseries_readl
,
710 .readw_be
= iseries_readw_be
,
711 .readl_be
= iseries_readl_be
,
712 .writeb
= iseries_writeb
,
713 .writew
= iseries_writew
,
714 .writel
= iseries_writel
,
715 .writew_be
= iseries_writew_be
,
716 .writel_be
= iseries_writel_be
,
717 .readsb
= iseries_readsb
,
718 .readsw
= iseries_readsw
,
719 .readsl
= iseries_readsl
,
720 .writesb
= iseries_writesb
,
721 .writesw
= iseries_writesw
,
722 .writesl
= iseries_writesl
,
723 .memset_io
= iseries_memset_io
,
724 .memcpy_fromio
= iseries_memcpy_fromio
,
725 .memcpy_toio
= iseries_memcpy_toio
,
729 * iSeries_pcibios_init
732 * This function checks for all possible system PCI host bridges that connect
733 * PCI buses. The system hypervisor is queried as to the guest partition
734 * ownership status. A pci_controller is built for any bus which is partially
735 * owned or fully owned by this guest partition.
737 void __init
iSeries_pcibios_init(void)
739 struct pci_controller
*phb
;
740 struct device_node
*root
= of_find_node_by_path("/");
741 struct device_node
*node
= NULL
;
743 /* Install IO hooks */
744 ppc_pci_io
= iseries_pci_io
;
747 printk(KERN_CRIT
"iSeries_pcibios_init: can't find root "
751 while ((node
= of_get_next_child(root
, node
)) != NULL
) {
755 if ((node
->type
== NULL
) || (strcmp(node
->type
, "pci") != 0))
758 busp
= get_property(node
, "bus-range", NULL
);
762 printk("bus %d appears to exist\n", bus
);
763 phb
= pcibios_alloc_controller(node
);
767 phb
->pci_mem_offset
= phb
->local_number
= bus
;
768 phb
->first_busno
= bus
;
769 phb
->last_busno
= bus
;
770 phb
->ops
= &iSeries_pci_ops
;