blob: e4e6ae0acd4fd119756ded21d663ca6d55276823 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2001 Allan Trautman, IBM Corporation
3 *
4 * iSeries specific routines for PCI.
Stephen Rothwelld3878992005-09-28 02:50:25 +10005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based on code from pci.c and iSeries_pci.c 32bit
7 *
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.
Stephen Rothwelld3878992005-09-28 02:50:25 +100012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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.
Stephen Rothwelld3878992005-09-28 02:50:25 +100017 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * 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
21 */
22#include <linux/kernel.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100023#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/string.h>
25#include <linux/init.h>
26#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/pci.h>
28
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/iommu.h>
Stephen Rothwell426c1a12005-10-14 14:51:42 +100035#include <asm/abs_addr.h>
Stephen Rothwellcaf81322006-09-21 18:00:00 +100036#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Kelly Daly8021b8a2005-11-02 11:41:12 +110038#include <asm/iseries/hv_call_xm.h>
Kelly Dalybbc8b622005-11-02 15:10:38 +110039#include <asm/iseries/mf.h>
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100040#include <asm/iseries/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Stephen Rothwelld3878992005-09-28 02:50:25 +100042#include <asm/ppc-pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Stephen Rothwellb08567cb2005-09-28 23:37:01 +100044#include "irq.h"
Stephen Rothwell426c1a12005-10-14 14:51:42 +100045#include "pci.h"
Stephen Rothwellc6d2ea92005-10-14 17:16:17 +100046#include "call_pci.h"
Stephen Rothwellb08567cb2005-09-28 23:37:01 +100047
Stephen Rothwelld3878992005-09-28 02:50:25 +100048static int Pci_Retry_Max = 3; /* Only retry 3 times */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static int Pci_Error_Flag = 1; /* Set Retry Error on. */
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * Table defines
53 * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
54 */
55#define IOMM_TABLE_MAX_ENTRIES 1024
56#define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
57#define BASE_IO_MEMORY 0xE000000000000000UL
58
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100059static unsigned long max_io_memory = BASE_IO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static long current_iomm_table_entry;
61
62/*
63 * Lookup Tables.
64 */
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100065static struct device_node *iomm_table[IOMM_TABLE_MAX_ENTRIES];
66static u8 iobar_table[IOMM_TABLE_MAX_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100068static const char pci_io_text[] = "iSeries PCI I/O";
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static DEFINE_SPINLOCK(iomm_table_lock);
70
71/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * iomm_table_allocate_entry
73 *
74 * Adds pci_dev entry in address translation table
75 *
76 * - Allocates the number of entries required in table base on BAR
77 * size.
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.
82 */
Stephen Rothwell1e105902007-11-19 17:03:59 +110083static void __init iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 struct resource *bar_res = &dev->resource[bar_num];
86 long bar_size = pci_resource_len(dev, bar_num);
87
88 /*
89 * No space to allocate, quick exit, skip Allocation.
90 */
91 if (bar_size == 0)
92 return;
93 /*
94 * Set Resource values.
95 */
96 spin_lock(&iomm_table_lock);
97 bar_res->name = pci_io_text;
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100098 bar_res->start = BASE_IO_MEMORY +
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 bar_res->end = bar_res->start + bar_size - 1;
101 /*
102 * Allocate the number of table entries needed for BAR.
103 */
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;
109 }
110 max_io_memory = BASE_IO_MEMORY +
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000111 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 spin_unlock(&iomm_table_lock);
113}
114
115/*
116 * allocate_device_bars
117 *
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
122 * each entry.
123 * - Loops through The Bar resources(0 - 5) including the ROM
124 * is resource(6).
125 */
Stephen Rothwell1e105902007-11-19 17:03:59 +1100126static void __init allocate_device_bars(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int bar_num;
129
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000130 for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 iomm_table_allocate_entry(dev, bar_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134/*
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
140 */
141static void pci_Log_Error(char *Error_Text, int Bus, int SubBus,
142 int AgentId, int HvRc)
143{
144 if (HvRc == 0x0302)
145 return;
146 printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
147 Error_Text, Bus, SubBus, AgentId, HvRc);
148}
149
150/*
Stephen Rothwell9103eb72007-12-07 01:46:40 +1100151 * Look down the chain to find the matching Device Device
152 */
153static struct device_node *find_Device_Node(int bus, int devfn)
154{
155 struct device_node *node;
156
157 for (node = NULL; (node = of_find_all_nodes(node)); ) {
158 struct pci_dn *pdn = PCI_DN(node);
159
160 if (pdn && (bus == pdn->busno) && (devfn == pdn->devfn))
161 return node;
162 }
163 return NULL;
164}
165
166/*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000167 * iSeries_pci_final_fixup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
169void __init iSeries_pci_final_fixup(void)
170{
171 struct pci_dev *pdev = NULL;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000172 struct device_node *node;
Stephen Rothwelld3878992005-09-28 02:50:25 +1000173 int DeviceCount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* Fix up at the device node and pci_dev relationship */
176 mf_display_src(0xC9000100);
177
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);
183
184 if (node != NULL) {
Stephen Rothwellb0252792006-05-19 16:50:39 +1000185 struct pci_dn *pdn = PCI_DN(node);
Jeremy Kerrc4c7cba2006-07-12 15:39:42 +1000186 const u32 *agent;
Stephen Rothwellb0252792006-05-19 16:50:39 +1000187
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000188 agent = of_get_property(node, "linux,agent-id", NULL);
Stephen Rothwellb0252792006-05-19 16:50:39 +1000189 if ((pdn != NULL) && (agent != NULL)) {
190 u8 irq = iSeries_allocate_IRQ(pdn->busno, 0,
191 pdn->bussubno);
192 int err;
193
194 err = HvCallXm_connectBusUnit(pdn->busno, pdn->bussubno,
195 *agent, irq);
196 if (err)
197 pci_Log_Error("Connect Bus Unit",
198 pdn->busno, pdn->bussubno, *agent, err);
199 else {
200 err = HvCallPci_configStore8(pdn->busno, pdn->bussubno,
201 *agent,
202 PCI_INTERRUPT_LINE,
203 irq);
204 if (err)
205 pci_Log_Error("PciCfgStore Irq Failed!",
206 pdn->busno, pdn->bussubno, *agent, err);
207 }
208 if (!err)
209 pdev->irq = irq;
210 }
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 ++DeviceCount;
213 pdev->sysdata = (void *)node;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000214 PCI_DN(node)->pcidev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 allocate_device_bars(pdev);
Stephen Rothwell061c0632005-06-21 17:15:48 -0700216 iSeries_Device_Information(pdev, DeviceCount);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100217 iommu_devnode_init_iSeries(pdev, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 } else
219 printk("PCI: Device Tree not found for 0x%016lX\n",
220 (unsigned long)pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222 iSeries_activate_IRQs();
223 mf_display_src(0xC9000200);
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#if 0
227/*
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.
231 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000232static struct device_node *get_Device_Node(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000234 struct device_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 node = pdev->sysdata;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000237 if (node == NULL || PCI_DN(node)->pcidev != pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 node = find_Device_Node(pdev->bus->number, pdev->devfn);
239 return node;
240}
241#endif
242
243/*
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.
248 */
249static u64 hv_cfg_read_func[4] = {
250 HvCallPciConfigLoad8, HvCallPciConfigLoad16,
251 HvCallPciConfigLoad32, HvCallPciConfigLoad32
252};
253
254static u64 hv_cfg_write_func[4] = {
255 HvCallPciConfigStore8, HvCallPciConfigStore16,
256 HvCallPciConfigStore32, HvCallPciConfigStore32
257};
258
259/*
260 * Read PCI config space
261 */
262static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
263 int offset, int size, u32 *val)
264{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000265 struct device_node *node = find_Device_Node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 u64 fn;
267 struct HvCallPci_LoadReturn ret;
268
269 if (node == NULL)
270 return PCIBIOS_DEVICE_NOT_FOUND;
271 if (offset > 255) {
272 *val = ~0;
273 return PCIBIOS_BAD_REGISTER_NUMBER;
274 }
275
276 fn = hv_cfg_read_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000277 HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (ret.rc != 0) {
280 *val = ~0;
281 return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
282 }
283
284 *val = ret.value;
285 return 0;
286}
287
288/*
289 * Write PCI config space
290 */
291
292static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
293 int offset, int size, u32 val)
294{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000295 struct device_node *node = find_Device_Node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 u64 fn;
297 u64 ret;
298
299 if (node == NULL)
300 return PCIBIOS_DEVICE_NOT_FOUND;
301 if (offset > 255)
302 return PCIBIOS_BAD_REGISTER_NUMBER;
303
304 fn = hv_cfg_write_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000305 ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 if (ret != 0)
308 return PCIBIOS_DEVICE_NOT_FOUND;
309
310 return 0;
311}
312
313static struct pci_ops iSeries_pci_ops = {
314 .read = iSeries_pci_read_config,
315 .write = iSeries_pci_write_config
316};
317
318/*
319 * Check Return Code
320 * -> On Failure, print and log information.
321 * Increment Retry Count, if exceeds max, panic partition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 *
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)
326 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000327static int CheckReturnCode(char *TextHdr, struct device_node *DevNode,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700328 int *retry, u64 ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 if (ret != 0) {
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000331 struct pci_dn *pdn = PCI_DN(DevNode);
332
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700333 (*retry)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000335 TextHdr, pdn->busno, pdn->devfn,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700336 *retry, (int)ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /*
338 * Bump the retry and check for retry count exceeded.
339 * If, Exceeded, panic the system.
340 */
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700341 if (((*retry) > Pci_Retry_Max) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 (Pci_Error_Flag > 0)) {
343 mf_display_src(0xB6000103);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700344 panic_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 panic("PCI: Hardware I/O Error, SRC B6000103, "
346 "Automatic Reboot Disabled.\n");
347 }
348 return -1; /* Retry Try */
349 }
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353/*
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.
357 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000358static inline struct device_node *xlate_iomm_address(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 const volatile void __iomem *IoAddress,
360 u64 *dsaptr, u64 *BarOffsetPtr)
361{
362 unsigned long OrigIoAddr;
363 unsigned long BaseIoAddr;
364 unsigned long TableIndex;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000365 struct device_node *DevNode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 OrigIoAddr = (unsigned long __force)IoAddress;
368 if ((OrigIoAddr < BASE_IO_MEMORY) || (OrigIoAddr >= max_io_memory))
369 return NULL;
370 BaseIoAddr = OrigIoAddr - BASE_IO_MEMORY;
371 TableIndex = BaseIoAddr / IOMM_TABLE_ENTRY_SIZE;
372 DevNode = iomm_table[TableIndex];
373
374 if (DevNode != NULL) {
375 int barnum = iobar_table[TableIndex];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000376 *dsaptr = iseries_ds_addr(DevNode) | (barnum << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 *BarOffsetPtr = BaseIoAddr % IOMM_TABLE_ENTRY_SIZE;
378 } else
379 panic("PCI: Invalid PCI IoAddress detected!\n");
380 return DevNode;
381}
382
383/*
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
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100386 * else, data is returned in Big Endian format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000388static u8 iSeries_Read_Byte(const volatile void __iomem *IoAddress)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 u64 BarOffset;
391 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700392 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000394 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
396
397 if (DevNode == NULL) {
398 static unsigned long last_jiffies;
399 static int num_printed;
400
401 if ((jiffies - last_jiffies) > 60 * HZ) {
402 last_jiffies = jiffies;
403 num_printed = 0;
404 }
405 if (num_printed++ < 10)
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100406 printk(KERN_ERR "iSeries_Read_Byte: invalid access at IO address %p\n",
407 IoAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0xff;
409 }
410 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700412 } while (CheckReturnCode("RDB", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100414 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000417static u16 iSeries_Read_Word(const volatile void __iomem *IoAddress)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 u64 BarOffset;
420 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700421 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000423 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
425
426 if (DevNode == NULL) {
427 static unsigned long last_jiffies;
428 static int num_printed;
429
430 if ((jiffies - last_jiffies) > 60 * HZ) {
431 last_jiffies = jiffies;
432 num_printed = 0;
433 }
434 if (num_printed++ < 10)
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100435 printk(KERN_ERR "iSeries_Read_Word: invalid access at IO address %p\n",
436 IoAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return 0xffff;
438 }
439 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
441 BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700442 } while (CheckReturnCode("RDW", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100444 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000447static u32 iSeries_Read_Long(const volatile void __iomem *IoAddress)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 u64 BarOffset;
450 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700451 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000453 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
455
456 if (DevNode == NULL) {
457 static unsigned long last_jiffies;
458 static int num_printed;
459
460 if ((jiffies - last_jiffies) > 60 * HZ) {
461 last_jiffies = jiffies;
462 num_printed = 0;
463 }
464 if (num_printed++ < 10)
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100465 printk(KERN_ERR "iSeries_Read_Long: invalid access at IO address %p\n",
466 IoAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return 0xffffffff;
468 }
469 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
471 BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700472 } while (CheckReturnCode("RDL", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100474 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477/*
478 * Write MM I/O Instructions for the iSeries
479 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 */
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000481static void iSeries_Write_Byte(u8 data, volatile void __iomem *IoAddress)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 u64 BarOffset;
484 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700485 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000487 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
489
490 if (DevNode == NULL) {
491 static unsigned long last_jiffies;
492 static int num_printed;
493
494 if ((jiffies - last_jiffies) > 60 * HZ) {
495 last_jiffies = jiffies;
496 num_printed = 0;
497 }
498 if (num_printed++ < 10)
499 printk(KERN_ERR "iSeries_Write_Byte: invalid access at IO address %p\n", IoAddress);
500 return;
501 }
502 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 rc = HvCall4(HvCallPciBarStore8, dsa, BarOffset, data, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700504 } while (CheckReturnCode("WWB", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000507static void iSeries_Write_Word(u16 data, volatile void __iomem *IoAddress)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 u64 BarOffset;
510 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700511 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000513 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
515
516 if (DevNode == NULL) {
517 static unsigned long last_jiffies;
518 static int num_printed;
519
520 if ((jiffies - last_jiffies) > 60 * HZ) {
521 last_jiffies = jiffies;
522 num_printed = 0;
523 }
524 if (num_printed++ < 10)
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100525 printk(KERN_ERR "iSeries_Write_Word: invalid access at IO address %p\n",
526 IoAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return;
528 }
529 do {
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100530 rc = HvCall4(HvCallPciBarStore16, dsa, BarOffset, data, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700531 } while (CheckReturnCode("WWW", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000534static void iSeries_Write_Long(u32 data, volatile void __iomem *IoAddress)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 u64 BarOffset;
537 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700538 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000540 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
542
543 if (DevNode == NULL) {
544 static unsigned long last_jiffies;
545 static int num_printed;
546
547 if ((jiffies - last_jiffies) > 60 * HZ) {
548 last_jiffies = jiffies;
549 num_printed = 0;
550 }
551 if (num_printed++ < 10)
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100552 printk(KERN_ERR "iSeries_Write_Long: invalid access at IO address %p\n",
553 IoAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return;
555 }
556 do {
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100557 rc = HvCall4(HvCallPciBarStore32, dsa, BarOffset, data, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700558 } while (CheckReturnCode("WWL", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000560
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100561static u8 iseries_readb(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000562{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100563 return iSeries_Read_Byte(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000564}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000565
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100566static u16 iseries_readw(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000567{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100568 return le16_to_cpu(iSeries_Read_Word(addr));
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000569}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000570
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100571static u32 iseries_readl(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000572{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100573 return le32_to_cpu(iSeries_Read_Long(addr));
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000574}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000575
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100576static u16 iseries_readw_be(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000577{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100578 return iSeries_Read_Word(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000579}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000580
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100581static u32 iseries_readl_be(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000582{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100583 return iSeries_Read_Long(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000584}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000585
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100586static void iseries_writeb(u8 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000587{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100588 iSeries_Write_Byte(data, addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000589}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000590
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100591static void iseries_writew(u16 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000592{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100593 iSeries_Write_Word(cpu_to_le16(data), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000594}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000595
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100596static void iseries_writel(u32 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000597{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100598 iSeries_Write_Long(cpu_to_le32(data), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000599}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000600
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100601static void iseries_writew_be(u16 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000602{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100603 iSeries_Write_Word(data, addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000604}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000605
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100606static void iseries_writel_be(u32 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000607{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100608 iSeries_Write_Long(data, addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000609}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000610
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100611static void iseries_readsb(const volatile void __iomem *addr, void *buf,
612 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000613{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100614 u8 *dst = buf;
615 while(count-- > 0)
616 *(dst++) = iSeries_Read_Byte(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000617}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000618
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100619static void iseries_readsw(const volatile void __iomem *addr, void *buf,
620 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000621{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100622 u16 *dst = buf;
623 while(count-- > 0)
624 *(dst++) = iSeries_Read_Word(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000625}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000626
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100627static void iseries_readsl(const volatile void __iomem *addr, void *buf,
628 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000629{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100630 u32 *dst = buf;
631 while(count-- > 0)
632 *(dst++) = iSeries_Read_Long(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000633}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000634
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100635static void iseries_writesb(volatile void __iomem *addr, const void *buf,
636 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000637{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100638 const u8 *src = buf;
639 while(count-- > 0)
640 iSeries_Write_Byte(*(src++), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000641}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000642
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100643static void iseries_writesw(volatile void __iomem *addr, const void *buf,
644 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000645{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100646 const u16 *src = buf;
647 while(count-- > 0)
648 iSeries_Write_Word(*(src++), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000649}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000650
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100651static void iseries_writesl(volatile void __iomem *addr, const void *buf,
652 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000653{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100654 const u32 *src = buf;
655 while(count-- > 0)
656 iSeries_Write_Long(*(src++), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000657}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000658
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100659static void iseries_memset_io(volatile void __iomem *addr, int c,
660 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000661{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100662 volatile char __iomem *d = addr;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000663
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100664 while (n-- > 0)
665 iSeries_Write_Byte(c, d++);
666}
667
668static void iseries_memcpy_fromio(void *dest, const volatile void __iomem *src,
669 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000670{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100671 char *d = dest;
672 const volatile char __iomem *s = src;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000673
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100674 while (n-- > 0)
675 *d++ = iSeries_Read_Byte(s++);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000676}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000677
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100678static void iseries_memcpy_toio(volatile void __iomem *dest, const void *src,
679 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000680{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100681 const char *s = src;
682 volatile char __iomem *d = dest;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000683
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100684 while (n-- > 0)
685 iSeries_Write_Byte(*s++, d++);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000686}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000687
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100688/* 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.
691 *
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.
697 */
698static 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,
718};
719
720/*
721 * iSeries_pcibios_init
722 *
723 * Description:
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.
728 */
729void __init iSeries_pcibios_init(void)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000730{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100731 struct pci_controller *phb;
732 struct device_node *root = of_find_node_by_path("/");
733 struct device_node *node = NULL;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000734
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100735 /* Install IO hooks */
736 ppc_pci_io = iseries_pci_io;
737
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000738 /* iSeries has no IO space in the common sense, it needs to set
739 * the IO base to 0
740 */
741 pci_io_base = 0;
742
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100743 if (root == NULL) {
744 printk(KERN_CRIT "iSeries_pcibios_init: can't find root "
745 "of device tree\n");
746 return;
747 }
748 while ((node = of_get_next_child(root, node)) != NULL) {
749 HvBusNumber bus;
750 const u32 *busp;
751
752 if ((node->type == NULL) || (strcmp(node->type, "pci") != 0))
753 continue;
754
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000755 busp = of_get_property(node, "bus-range", NULL);
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100756 if (busp == NULL)
757 continue;
758 bus = *busp;
759 printk("bus %d appears to exist\n", bus);
760 phb = pcibios_alloc_controller(node);
761 if (phb == NULL)
762 continue;
763
Kumar Galabf440b712007-06-27 00:19:08 -0500764 phb->pci_mem_offset = bus;
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100765 phb->first_busno = bus;
766 phb->last_busno = bus;
767 phb->ops = &iSeries_pci_ops;
768 }
769
770 of_node_put(root);
771
772 pci_devs_phb_init();
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000773}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000774