blob: 4a06d9c349869cdcb1fda45c516034b8cf992867 [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>
27#include <linux/ide.h>
28#include <linux/pci.h>
29
30#include <asm/io.h>
31#include <asm/irq.h>
32#include <asm/prom.h>
33#include <asm/machdep.h>
34#include <asm/pci-bridge.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/iommu.h>
Stephen Rothwell426c1a12005-10-14 14:51:42 +100036#include <asm/abs_addr.h>
Stephen Rothwellcaf81322006-09-21 18:00:00 +100037#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Kelly Daly8021b8a2005-11-02 11:41:12 +110039#include <asm/iseries/hv_call_xm.h>
Kelly Dalybbc8b622005-11-02 15:10:38 +110040#include <asm/iseries/mf.h>
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100041#include <asm/iseries/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Stephen Rothwelld3878992005-09-28 02:50:25 +100043#include <asm/ppc-pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Stephen Rothwellb08567cb2005-09-28 23:37:01 +100045#include "irq.h"
Stephen Rothwell426c1a12005-10-14 14:51:42 +100046#include "pci.h"
Stephen Rothwellc6d2ea92005-10-14 17:16:17 +100047#include "call_pci.h"
Stephen Rothwellb08567cb2005-09-28 23:37:01 +100048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
Stephen Rothwelld3878992005-09-28 02:50:25 +100050 * Forward declares of prototypes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +100052static struct device_node *find_Device_Node(int bus, int devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Stephen Rothwelld3878992005-09-28 02:50:25 +100054static int Pci_Retry_Max = 3; /* Only retry 3 times */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static int Pci_Error_Flag = 1; /* Set Retry Error on. */
56
57static struct pci_ops iSeries_pci_ops;
58
59/*
60 * Table defines
61 * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
62 */
63#define IOMM_TABLE_MAX_ENTRIES 1024
64#define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
65#define BASE_IO_MEMORY 0xE000000000000000UL
66
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100067static unsigned long max_io_memory = BASE_IO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static long current_iomm_table_entry;
69
70/*
71 * Lookup Tables.
72 */
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100073static struct device_node *iomm_table[IOMM_TABLE_MAX_ENTRIES];
74static u8 iobar_table[IOMM_TABLE_MAX_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100076static const char pci_io_text[] = "iSeries PCI I/O";
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static DEFINE_SPINLOCK(iomm_table_lock);
78
79/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * iomm_table_allocate_entry
81 *
82 * Adds pci_dev entry in address translation table
83 *
84 * - Allocates the number of entries required in table base on BAR
85 * size.
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.
90 */
91static void iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
92{
93 struct resource *bar_res = &dev->resource[bar_num];
94 long bar_size = pci_resource_len(dev, bar_num);
95
96 /*
97 * No space to allocate, quick exit, skip Allocation.
98 */
99 if (bar_size == 0)
100 return;
101 /*
102 * Set Resource values.
103 */
104 spin_lock(&iomm_table_lock);
105 bar_res->name = pci_io_text;
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000106 bar_res->start = BASE_IO_MEMORY +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 bar_res->end = bar_res->start + bar_size - 1;
109 /*
110 * Allocate the number of table entries needed for BAR.
111 */
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;
117 }
118 max_io_memory = BASE_IO_MEMORY +
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000119 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 spin_unlock(&iomm_table_lock);
121}
122
123/*
124 * allocate_device_bars
125 *
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
130 * each entry.
131 * - Loops through The Bar resources(0 - 5) including the ROM
132 * is resource(6).
133 */
134static void allocate_device_bars(struct pci_dev *dev)
135{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int bar_num;
137
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000138 for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 iomm_table_allocate_entry(dev, bar_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142/*
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
148 */
149static void pci_Log_Error(char *Error_Text, int Bus, int SubBus,
150 int AgentId, int HvRc)
151{
152 if (HvRc == 0x0302)
153 return;
154 printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
155 Error_Text, Bus, SubBus, AgentId, HvRc);
156}
157
158/*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000159 * iSeries_pci_final_fixup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
161void __init iSeries_pci_final_fixup(void)
162{
163 struct pci_dev *pdev = NULL;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000164 struct device_node *node;
Stephen Rothwelld3878992005-09-28 02:50:25 +1000165 int DeviceCount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /* Fix up at the device node and pci_dev relationship */
168 mf_display_src(0xC9000100);
169
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);
175
176 if (node != NULL) {
Stephen Rothwellb0252792006-05-19 16:50:39 +1000177 struct pci_dn *pdn = PCI_DN(node);
Jeremy Kerrc4c7cba2006-07-12 15:39:42 +1000178 const u32 *agent;
Stephen Rothwellb0252792006-05-19 16:50:39 +1000179
Jeremy Kerrc4c7cba2006-07-12 15:39:42 +1000180 agent = get_property(node, "linux,agent-id", NULL);
Stephen Rothwellb0252792006-05-19 16:50:39 +1000181 if ((pdn != NULL) && (agent != NULL)) {
182 u8 irq = iSeries_allocate_IRQ(pdn->busno, 0,
183 pdn->bussubno);
184 int err;
185
186 err = HvCallXm_connectBusUnit(pdn->busno, pdn->bussubno,
187 *agent, irq);
188 if (err)
189 pci_Log_Error("Connect Bus Unit",
190 pdn->busno, pdn->bussubno, *agent, err);
191 else {
192 err = HvCallPci_configStore8(pdn->busno, pdn->bussubno,
193 *agent,
194 PCI_INTERRUPT_LINE,
195 irq);
196 if (err)
197 pci_Log_Error("PciCfgStore Irq Failed!",
198 pdn->busno, pdn->bussubno, *agent, err);
199 }
200 if (!err)
201 pdev->irq = irq;
202 }
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 ++DeviceCount;
205 pdev->sysdata = (void *)node;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000206 PCI_DN(node)->pcidev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 allocate_device_bars(pdev);
Stephen Rothwell061c0632005-06-21 17:15:48 -0700208 iSeries_Device_Information(pdev, DeviceCount);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100209 iommu_devnode_init_iSeries(pdev, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 } else
211 printk("PCI: Device Tree not found for 0x%016lX\n",
212 (unsigned long)pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 iSeries_activate_IRQs();
215 mf_display_src(0xC9000200);
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * Look down the chain to find the matching Device Device
220 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000221static struct device_node *find_Device_Node(int bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Stephen Rothwell96ff6af2006-05-19 16:48:47 +1000223 struct device_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Stephen Rothwell96ff6af2006-05-19 16:48:47 +1000225 for (node = NULL; (node = of_find_all_nodes(node)); ) {
226 struct pci_dn *pdn = PCI_DN(node);
227
228 if (pdn && (bus == pdn->busno) && (devfn == pdn->devfn))
229 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 return NULL;
232}
233
234#if 0
235/*
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.
239 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000240static struct device_node *get_Device_Node(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000242 struct device_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 node = pdev->sysdata;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000245 if (node == NULL || PCI_DN(node)->pcidev != pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 node = find_Device_Node(pdev->bus->number, pdev->devfn);
247 return node;
248}
249#endif
250
251/*
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.
256 */
257static u64 hv_cfg_read_func[4] = {
258 HvCallPciConfigLoad8, HvCallPciConfigLoad16,
259 HvCallPciConfigLoad32, HvCallPciConfigLoad32
260};
261
262static u64 hv_cfg_write_func[4] = {
263 HvCallPciConfigStore8, HvCallPciConfigStore16,
264 HvCallPciConfigStore32, HvCallPciConfigStore32
265};
266
267/*
268 * Read PCI config space
269 */
270static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
271 int offset, int size, u32 *val)
272{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000273 struct device_node *node = find_Device_Node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 u64 fn;
275 struct HvCallPci_LoadReturn ret;
276
277 if (node == NULL)
278 return PCIBIOS_DEVICE_NOT_FOUND;
279 if (offset > 255) {
280 *val = ~0;
281 return PCIBIOS_BAD_REGISTER_NUMBER;
282 }
283
284 fn = hv_cfg_read_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000285 HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (ret.rc != 0) {
288 *val = ~0;
289 return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
290 }
291
292 *val = ret.value;
293 return 0;
294}
295
296/*
297 * Write PCI config space
298 */
299
300static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
301 int offset, int size, u32 val)
302{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000303 struct device_node *node = find_Device_Node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 u64 fn;
305 u64 ret;
306
307 if (node == NULL)
308 return PCIBIOS_DEVICE_NOT_FOUND;
309 if (offset > 255)
310 return PCIBIOS_BAD_REGISTER_NUMBER;
311
312 fn = hv_cfg_write_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000313 ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 if (ret != 0)
316 return PCIBIOS_DEVICE_NOT_FOUND;
317
318 return 0;
319}
320
321static struct pci_ops iSeries_pci_ops = {
322 .read = iSeries_pci_read_config,
323 .write = iSeries_pci_write_config
324};
325
326/*
327 * Check Return Code
328 * -> On Failure, print and log information.
329 * Increment Retry Count, if exceeds max, panic partition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 *
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)
334 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000335static int CheckReturnCode(char *TextHdr, struct device_node *DevNode,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700336 int *retry, u64 ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 if (ret != 0) {
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000339 struct pci_dn *pdn = PCI_DN(DevNode);
340
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700341 (*retry)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000343 TextHdr, pdn->busno, pdn->devfn,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700344 *retry, (int)ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /*
346 * Bump the retry and check for retry count exceeded.
347 * If, Exceeded, panic the system.
348 */
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700349 if (((*retry) > Pci_Retry_Max) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 (Pci_Error_Flag > 0)) {
351 mf_display_src(0xB6000103);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700352 panic_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 panic("PCI: Hardware I/O Error, SRC B6000103, "
354 "Automatic Reboot Disabled.\n");
355 }
356 return -1; /* Retry Try */
357 }
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361/*
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.
365 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000366static inline struct device_node *xlate_iomm_address(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 const volatile void __iomem *IoAddress,
368 u64 *dsaptr, u64 *BarOffsetPtr)
369{
370 unsigned long OrigIoAddr;
371 unsigned long BaseIoAddr;
372 unsigned long TableIndex;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000373 struct device_node *DevNode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 OrigIoAddr = (unsigned long __force)IoAddress;
376 if ((OrigIoAddr < BASE_IO_MEMORY) || (OrigIoAddr >= max_io_memory))
377 return NULL;
378 BaseIoAddr = OrigIoAddr - BASE_IO_MEMORY;
379 TableIndex = BaseIoAddr / IOMM_TABLE_ENTRY_SIZE;
380 DevNode = iomm_table[TableIndex];
381
382 if (DevNode != NULL) {
383 int barnum = iobar_table[TableIndex];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000384 *dsaptr = iseries_ds_addr(DevNode) | (barnum << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 *BarOffsetPtr = BaseIoAddr % IOMM_TABLE_ENTRY_SIZE;
386 } else
387 panic("PCI: Invalid PCI IoAddress detected!\n");
388 return DevNode;
389}
390
391/*
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
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100394 * else, data is returned in Big Endian format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000396static u8 iSeries_Read_Byte(const volatile void __iomem *IoAddress)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 u64 BarOffset;
399 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700400 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000402 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
404
405 if (DevNode == NULL) {
406 static unsigned long last_jiffies;
407 static int num_printed;
408
409 if ((jiffies - last_jiffies) > 60 * HZ) {
410 last_jiffies = jiffies;
411 num_printed = 0;
412 }
413 if (num_printed++ < 10)
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100414 printk(KERN_ERR "iSeries_Read_Byte: invalid access at IO address %p\n",
415 IoAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return 0xff;
417 }
418 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700420 } while (CheckReturnCode("RDB", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100422 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000425static u16 iSeries_Read_Word(const volatile void __iomem *IoAddress)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 u64 BarOffset;
428 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700429 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000431 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
433
434 if (DevNode == NULL) {
435 static unsigned long last_jiffies;
436 static int num_printed;
437
438 if ((jiffies - last_jiffies) > 60 * HZ) {
439 last_jiffies = jiffies;
440 num_printed = 0;
441 }
442 if (num_printed++ < 10)
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100443 printk(KERN_ERR "iSeries_Read_Word: invalid access at IO address %p\n",
444 IoAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 0xffff;
446 }
447 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
449 BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700450 } while (CheckReturnCode("RDW", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100452 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000455static u32 iSeries_Read_Long(const volatile void __iomem *IoAddress)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 u64 BarOffset;
458 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700459 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000461 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
463
464 if (DevNode == NULL) {
465 static unsigned long last_jiffies;
466 static int num_printed;
467
468 if ((jiffies - last_jiffies) > 60 * HZ) {
469 last_jiffies = jiffies;
470 num_printed = 0;
471 }
472 if (num_printed++ < 10)
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100473 printk(KERN_ERR "iSeries_Read_Long: invalid access at IO address %p\n",
474 IoAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return 0xffffffff;
476 }
477 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
479 BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700480 } while (CheckReturnCode("RDL", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100482 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485/*
486 * Write MM I/O Instructions for the iSeries
487 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 */
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000489static void iSeries_Write_Byte(u8 data, volatile void __iomem *IoAddress)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 u64 BarOffset;
492 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700493 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000495 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
497
498 if (DevNode == NULL) {
499 static unsigned long last_jiffies;
500 static int num_printed;
501
502 if ((jiffies - last_jiffies) > 60 * HZ) {
503 last_jiffies = jiffies;
504 num_printed = 0;
505 }
506 if (num_printed++ < 10)
507 printk(KERN_ERR "iSeries_Write_Byte: invalid access at IO address %p\n", IoAddress);
508 return;
509 }
510 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 rc = HvCall4(HvCallPciBarStore8, dsa, BarOffset, data, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700512 } while (CheckReturnCode("WWB", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000515static void iSeries_Write_Word(u16 data, volatile void __iomem *IoAddress)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 u64 BarOffset;
518 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700519 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000521 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
523
524 if (DevNode == NULL) {
525 static unsigned long last_jiffies;
526 static int num_printed;
527
528 if ((jiffies - last_jiffies) > 60 * HZ) {
529 last_jiffies = jiffies;
530 num_printed = 0;
531 }
532 if (num_printed++ < 10)
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100533 printk(KERN_ERR "iSeries_Write_Word: invalid access at IO address %p\n",
534 IoAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return;
536 }
537 do {
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100538 rc = HvCall4(HvCallPciBarStore16, dsa, BarOffset, data, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700539 } while (CheckReturnCode("WWW", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000542static void iSeries_Write_Long(u32 data, volatile void __iomem *IoAddress)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 u64 BarOffset;
545 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700546 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000548 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
550
551 if (DevNode == NULL) {
552 static unsigned long last_jiffies;
553 static int num_printed;
554
555 if ((jiffies - last_jiffies) > 60 * HZ) {
556 last_jiffies = jiffies;
557 num_printed = 0;
558 }
559 if (num_printed++ < 10)
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100560 printk(KERN_ERR "iSeries_Write_Long: invalid access at IO address %p\n",
561 IoAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return;
563 }
564 do {
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100565 rc = HvCall4(HvCallPciBarStore32, dsa, BarOffset, data, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700566 } while (CheckReturnCode("WWL", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000568
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100569static u8 iseries_readb(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000570{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100571 return iSeries_Read_Byte(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000572}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000573
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100574static u16 iseries_readw(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000575{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100576 return le16_to_cpu(iSeries_Read_Word(addr));
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000577}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000578
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100579static u32 iseries_readl(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000580{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100581 return le32_to_cpu(iSeries_Read_Long(addr));
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000582}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000583
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100584static u16 iseries_readw_be(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000585{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100586 return iSeries_Read_Word(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000587}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000588
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100589static u32 iseries_readl_be(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000590{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100591 return iSeries_Read_Long(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000592}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000593
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100594static void iseries_writeb(u8 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000595{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100596 iSeries_Write_Byte(data, addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000597}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000598
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100599static void iseries_writew(u16 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000600{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100601 iSeries_Write_Word(cpu_to_le16(data), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000602}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000603
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100604static void iseries_writel(u32 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000605{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100606 iSeries_Write_Long(cpu_to_le32(data), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000607}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000608
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100609static void iseries_writew_be(u16 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000610{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100611 iSeries_Write_Word(data, addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000612}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000613
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100614static void iseries_writel_be(u32 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000615{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100616 iSeries_Write_Long(data, 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_readsb(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 u8 *dst = buf;
623 while(count-- > 0)
624 *(dst++) = iSeries_Read_Byte(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_readsw(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 u16 *dst = buf;
631 while(count-- > 0)
632 *(dst++) = iSeries_Read_Word(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_readsl(const volatile void __iomem *addr, void *buf,
636 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000637{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100638 u32 *dst = buf;
639 while(count-- > 0)
640 *(dst++) = iSeries_Read_Long(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_writesb(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 u8 *src = buf;
647 while(count-- > 0)
648 iSeries_Write_Byte(*(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_writesw(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 u16 *src = buf;
655 while(count-- > 0)
656 iSeries_Write_Word(*(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_writesl(volatile void __iomem *addr, const void *buf,
660 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000661{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100662 const u32 *src = buf;
663 while(count-- > 0)
664 iSeries_Write_Long(*(src++), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000665}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000666
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100667static void iseries_memset_io(volatile void __iomem *addr, int c,
668 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000669{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100670 volatile char __iomem *d = addr;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000671
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100672 while (n-- > 0)
673 iSeries_Write_Byte(c, d++);
674}
675
676static void iseries_memcpy_fromio(void *dest, const volatile void __iomem *src,
677 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000678{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100679 char *d = dest;
680 const volatile char __iomem *s = src;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000681
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100682 while (n-- > 0)
683 *d++ = iSeries_Read_Byte(s++);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000684}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000685
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100686static void iseries_memcpy_toio(volatile void __iomem *dest, const void *src,
687 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000688{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100689 const char *s = src;
690 volatile char __iomem *d = dest;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000691
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100692 while (n-- > 0)
693 iSeries_Write_Byte(*s++, d++);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000694}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000695
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100696/* 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.
699 *
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.
705 */
706static 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,
726};
727
728/*
729 * iSeries_pcibios_init
730 *
731 * Description:
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.
736 */
737void __init iSeries_pcibios_init(void)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000738{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100739 struct pci_controller *phb;
740 struct device_node *root = of_find_node_by_path("/");
741 struct device_node *node = NULL;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000742
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100743 /* Install IO hooks */
744 ppc_pci_io = iseries_pci_io;
745
746 if (root == NULL) {
747 printk(KERN_CRIT "iSeries_pcibios_init: can't find root "
748 "of device tree\n");
749 return;
750 }
751 while ((node = of_get_next_child(root, node)) != NULL) {
752 HvBusNumber bus;
753 const u32 *busp;
754
755 if ((node->type == NULL) || (strcmp(node->type, "pci") != 0))
756 continue;
757
758 busp = get_property(node, "bus-range", NULL);
759 if (busp == NULL)
760 continue;
761 bus = *busp;
762 printk("bus %d appears to exist\n", bus);
763 phb = pcibios_alloc_controller(node);
764 if (phb == NULL)
765 continue;
766
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;
771 }
772
773 of_node_put(root);
774
775 pci_devs_phb_init();
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000776}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000777