blob: 86a869839d34b743adf33f5bfa77ef6bca503ca2 [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>
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
Stephen Rothwelld3878992005-09-28 02:50:25 +100049 * Forward declares of prototypes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +100051static struct device_node *find_Device_Node(int bus, int devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Stephen Rothwelld3878992005-09-28 02:50:25 +100053static int Pci_Retry_Max = 3; /* Only retry 3 times */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static int Pci_Error_Flag = 1; /* Set Retry Error on. */
55
56static struct pci_ops iSeries_pci_ops;
57
58/*
59 * Table defines
60 * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
61 */
62#define IOMM_TABLE_MAX_ENTRIES 1024
63#define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
64#define BASE_IO_MEMORY 0xE000000000000000UL
65
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100066static unsigned long max_io_memory = BASE_IO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static long current_iomm_table_entry;
68
69/*
70 * Lookup Tables.
71 */
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100072static struct device_node *iomm_table[IOMM_TABLE_MAX_ENTRIES];
73static u8 iobar_table[IOMM_TABLE_MAX_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100075static const char pci_io_text[] = "iSeries PCI I/O";
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static DEFINE_SPINLOCK(iomm_table_lock);
77
78/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * iomm_table_allocate_entry
80 *
81 * Adds pci_dev entry in address translation table
82 *
83 * - Allocates the number of entries required in table base on BAR
84 * size.
85 * - Allocates starting at BASE_IO_MEMORY and increases.
86 * - The size is round up to be a multiple of entry size.
87 * - CurrentIndex is incremented to keep track of the last entry.
88 * - Builds the resource entry for allocated BARs.
89 */
90static void iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
91{
92 struct resource *bar_res = &dev->resource[bar_num];
93 long bar_size = pci_resource_len(dev, bar_num);
94
95 /*
96 * No space to allocate, quick exit, skip Allocation.
97 */
98 if (bar_size == 0)
99 return;
100 /*
101 * Set Resource values.
102 */
103 spin_lock(&iomm_table_lock);
104 bar_res->name = pci_io_text;
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000105 bar_res->start = BASE_IO_MEMORY +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 bar_res->end = bar_res->start + bar_size - 1;
108 /*
109 * Allocate the number of table entries needed for BAR.
110 */
111 while (bar_size > 0 ) {
112 iomm_table[current_iomm_table_entry] = dev->sysdata;
113 iobar_table[current_iomm_table_entry] = bar_num;
114 bar_size -= IOMM_TABLE_ENTRY_SIZE;
115 ++current_iomm_table_entry;
116 }
117 max_io_memory = BASE_IO_MEMORY +
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000118 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 spin_unlock(&iomm_table_lock);
120}
121
122/*
123 * allocate_device_bars
124 *
125 * - Allocates ALL pci_dev BAR's and updates the resources with the
126 * BAR value. BARS with zero length will have the resources
127 * The HvCallPci_getBarParms is used to get the size of the BAR
128 * space. It calls iomm_table_allocate_entry to allocate
129 * each entry.
130 * - Loops through The Bar resources(0 - 5) including the ROM
131 * is resource(6).
132 */
133static void allocate_device_bars(struct pci_dev *dev)
134{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int bar_num;
136
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000137 for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 iomm_table_allocate_entry(dev, bar_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141/*
142 * Log error information to system console.
143 * Filter out the device not there errors.
144 * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
145 * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
146 * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
147 */
148static void pci_Log_Error(char *Error_Text, int Bus, int SubBus,
149 int AgentId, int HvRc)
150{
151 if (HvRc == 0x0302)
152 return;
153 printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
154 Error_Text, Bus, SubBus, AgentId, HvRc);
155}
156
157/*
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000158 * iSeries_pcibios_init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
160 * Description:
161 * This function checks for all possible system PCI host bridges that connect
162 * PCI buses. The system hypervisor is queried as to the guest partition
163 * ownership status. A pci_controller is built for any bus which is partially
164 * owned or fully owned by this guest partition.
165 */
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000166void iSeries_pcibios_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 struct pci_controller *phb;
Stephen Rothwell0d177df2006-05-19 16:46:28 +1000169 struct device_node *node;
170 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Stephen Rothwell0d177df2006-05-19 16:46:28 +1000172 for_each_node_by_type(node, "pci") {
173 HvBusNumber bus;
174 u32 *busp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Stephen Rothwell0d177df2006-05-19 16:46:28 +1000176 busp = (u32 *)get_property(node, "bus-range", NULL);
177 if (busp == NULL)
178 continue;
179 bus = *busp;
180 printk("bus %d appears to exist\n", bus);
181 phb = pcibios_alloc_controller(node);
182 if (phb == NULL)
183 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Stephen Rothwell0d177df2006-05-19 16:46:28 +1000185 phb->pci_mem_offset = phb->local_number = bus;
186 phb->first_busno = bus;
187 phb->last_busno = bus;
188 phb->ops = &iSeries_pci_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Stephen Rothwell0d177df2006-05-19 16:46:28 +1000190 /* Find and connect the devices. */
191 for (dn = NULL; (dn = of_get_next_child(node, dn)) != NULL;) {
192 struct pci_dn *pdn;
Stephen Rothwell0d177df2006-05-19 16:46:28 +1000193 u32 *reg;
Stephen Rothwell0d177df2006-05-19 16:46:28 +1000194
195 reg = (u32 *)get_property(dn, "reg", NULL);
196 if (reg == NULL) {
197 printk(KERN_DEBUG "no reg property!\n");
198 continue;
199 }
200 busp = (u32 *)get_property(dn, "linux,subbus", NULL);
201 if (busp == NULL) {
202 printk(KERN_DEBUG "no subbus property!\n");
203 continue;
204 }
Stephen Rothwell0d177df2006-05-19 16:46:28 +1000205
Stephen Rothwell0d177df2006-05-19 16:46:28 +1000206 pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
207 if (pdn == NULL)
208 return;
209 dn->data = pdn;
210 pdn->node = dn;
211 pdn->busno = bus;
212 pdn->devfn = (reg[0] >> 8) & 0xff;
213 pdn->bussubno = *busp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218/*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000219 * iSeries_pci_final_fixup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
221void __init iSeries_pci_final_fixup(void)
222{
223 struct pci_dev *pdev = NULL;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000224 struct device_node *node;
Stephen Rothwelld3878992005-09-28 02:50:25 +1000225 int DeviceCount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* Fix up at the device node and pci_dev relationship */
228 mf_display_src(0xC9000100);
229
230 printk("pcibios_final_fixup\n");
231 for_each_pci_dev(pdev) {
232 node = find_Device_Node(pdev->bus->number, pdev->devfn);
233 printk("pci dev %p (%x.%x), node %p\n", pdev,
234 pdev->bus->number, pdev->devfn, node);
235
236 if (node != NULL) {
Stephen Rothwellb0252792006-05-19 16:50:39 +1000237 struct pci_dn *pdn = PCI_DN(node);
238 u32 *agent;
239
240 agent = (u32 *)get_property(node, "linux,agent-id",
241 NULL);
242 if ((pdn != NULL) && (agent != NULL)) {
243 u8 irq = iSeries_allocate_IRQ(pdn->busno, 0,
244 pdn->bussubno);
245 int err;
246
247 err = HvCallXm_connectBusUnit(pdn->busno, pdn->bussubno,
248 *agent, irq);
249 if (err)
250 pci_Log_Error("Connect Bus Unit",
251 pdn->busno, pdn->bussubno, *agent, err);
252 else {
253 err = HvCallPci_configStore8(pdn->busno, pdn->bussubno,
254 *agent,
255 PCI_INTERRUPT_LINE,
256 irq);
257 if (err)
258 pci_Log_Error("PciCfgStore Irq Failed!",
259 pdn->busno, pdn->bussubno, *agent, err);
260 }
261 if (!err)
262 pdev->irq = irq;
263 }
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 ++DeviceCount;
266 pdev->sysdata = (void *)node;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000267 PCI_DN(node)->pcidev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 allocate_device_bars(pdev);
Stephen Rothwell061c0632005-06-21 17:15:48 -0700269 iSeries_Device_Information(pdev, DeviceCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 iommu_devnode_init_iSeries(node);
271 } else
272 printk("PCI: Device Tree not found for 0x%016lX\n",
273 (unsigned long)pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 iSeries_activate_IRQs();
276 mf_display_src(0xC9000200);
277}
278
279void pcibios_fixup_bus(struct pci_bus *PciBus)
280{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283void pcibios_fixup_resources(struct pci_dev *pdev)
284{
Stephen Rothwelld3878992005-09-28 02:50:25 +1000285}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * I/0 Memory copy MUST use mmio commands on iSeries
289 * To do; For performance, include the hv call directly
290 */
291void iSeries_memset_io(volatile void __iomem *dest, char c, size_t Count)
292{
293 u8 ByteValue = c;
294 long NumberOfBytes = Count;
295
296 while (NumberOfBytes > 0) {
297 iSeries_Write_Byte(ByteValue, dest++);
298 -- NumberOfBytes;
299 }
300}
301EXPORT_SYMBOL(iSeries_memset_io);
302
303void iSeries_memcpy_toio(volatile void __iomem *dest, void *source, size_t count)
304{
305 char *src = source;
306 long NumberOfBytes = count;
307
308 while (NumberOfBytes > 0) {
309 iSeries_Write_Byte(*src++, dest++);
310 -- NumberOfBytes;
311 }
312}
313EXPORT_SYMBOL(iSeries_memcpy_toio);
314
315void iSeries_memcpy_fromio(void *dest, const volatile void __iomem *src, size_t count)
316{
317 char *dst = dest;
318 long NumberOfBytes = count;
319
320 while (NumberOfBytes > 0) {
321 *dst++ = iSeries_Read_Byte(src++);
322 -- NumberOfBytes;
323 }
324}
325EXPORT_SYMBOL(iSeries_memcpy_fromio);
326
327/*
328 * Look down the chain to find the matching Device Device
329 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000330static struct device_node *find_Device_Node(int bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Stephen Rothwell96ff6af2006-05-19 16:48:47 +1000332 struct device_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Stephen Rothwell96ff6af2006-05-19 16:48:47 +1000334 for (node = NULL; (node = of_find_all_nodes(node)); ) {
335 struct pci_dn *pdn = PCI_DN(node);
336
337 if (pdn && (bus == pdn->busno) && (devfn == pdn->devfn))
338 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 return NULL;
341}
342
343#if 0
344/*
345 * Returns the device node for the passed pci_dev
346 * Sanity Check Node PciDev to passed pci_dev
347 * If none is found, returns a NULL which the client must handle.
348 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000349static struct device_node *get_Device_Node(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000351 struct device_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 node = pdev->sysdata;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000354 if (node == NULL || PCI_DN(node)->pcidev != pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 node = find_Device_Node(pdev->bus->number, pdev->devfn);
356 return node;
357}
358#endif
359
360/*
361 * Config space read and write functions.
362 * For now at least, we look for the device node for the bus and devfn
363 * that we are asked to access. It may be possible to translate the devfn
364 * to a subbus and deviceid more directly.
365 */
366static u64 hv_cfg_read_func[4] = {
367 HvCallPciConfigLoad8, HvCallPciConfigLoad16,
368 HvCallPciConfigLoad32, HvCallPciConfigLoad32
369};
370
371static u64 hv_cfg_write_func[4] = {
372 HvCallPciConfigStore8, HvCallPciConfigStore16,
373 HvCallPciConfigStore32, HvCallPciConfigStore32
374};
375
376/*
377 * Read PCI config space
378 */
379static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
380 int offset, int size, u32 *val)
381{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000382 struct device_node *node = find_Device_Node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 u64 fn;
384 struct HvCallPci_LoadReturn ret;
385
386 if (node == NULL)
387 return PCIBIOS_DEVICE_NOT_FOUND;
388 if (offset > 255) {
389 *val = ~0;
390 return PCIBIOS_BAD_REGISTER_NUMBER;
391 }
392
393 fn = hv_cfg_read_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000394 HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (ret.rc != 0) {
397 *val = ~0;
398 return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
399 }
400
401 *val = ret.value;
402 return 0;
403}
404
405/*
406 * Write PCI config space
407 */
408
409static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
410 int offset, int size, u32 val)
411{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000412 struct device_node *node = find_Device_Node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 u64 fn;
414 u64 ret;
415
416 if (node == NULL)
417 return PCIBIOS_DEVICE_NOT_FOUND;
418 if (offset > 255)
419 return PCIBIOS_BAD_REGISTER_NUMBER;
420
421 fn = hv_cfg_write_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000422 ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 if (ret != 0)
425 return PCIBIOS_DEVICE_NOT_FOUND;
426
427 return 0;
428}
429
430static struct pci_ops iSeries_pci_ops = {
431 .read = iSeries_pci_read_config,
432 .write = iSeries_pci_write_config
433};
434
435/*
436 * Check Return Code
437 * -> On Failure, print and log information.
438 * Increment Retry Count, if exceeds max, panic partition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 *
440 * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
441 * PCI: Device 23.90 ReadL Retry( 1)
442 * PCI: Device 23.90 ReadL Retry Successful(1)
443 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000444static int CheckReturnCode(char *TextHdr, struct device_node *DevNode,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700445 int *retry, u64 ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 if (ret != 0) {
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000448 struct pci_dn *pdn = PCI_DN(DevNode);
449
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700450 (*retry)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000452 TextHdr, pdn->busno, pdn->devfn,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700453 *retry, (int)ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /*
455 * Bump the retry and check for retry count exceeded.
456 * If, Exceeded, panic the system.
457 */
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700458 if (((*retry) > Pci_Retry_Max) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 (Pci_Error_Flag > 0)) {
460 mf_display_src(0xB6000103);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700461 panic_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 panic("PCI: Hardware I/O Error, SRC B6000103, "
463 "Automatic Reboot Disabled.\n");
464 }
465 return -1; /* Retry Try */
466 }
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470/*
471 * Translate the I/O Address into a device node, bar, and bar offset.
472 * Note: Make sure the passed variable end up on the stack to avoid
473 * the exposure of being device global.
474 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000475static inline struct device_node *xlate_iomm_address(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 const volatile void __iomem *IoAddress,
477 u64 *dsaptr, u64 *BarOffsetPtr)
478{
479 unsigned long OrigIoAddr;
480 unsigned long BaseIoAddr;
481 unsigned long TableIndex;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000482 struct device_node *DevNode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 OrigIoAddr = (unsigned long __force)IoAddress;
485 if ((OrigIoAddr < BASE_IO_MEMORY) || (OrigIoAddr >= max_io_memory))
486 return NULL;
487 BaseIoAddr = OrigIoAddr - BASE_IO_MEMORY;
488 TableIndex = BaseIoAddr / IOMM_TABLE_ENTRY_SIZE;
489 DevNode = iomm_table[TableIndex];
490
491 if (DevNode != NULL) {
492 int barnum = iobar_table[TableIndex];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000493 *dsaptr = iseries_ds_addr(DevNode) | (barnum << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 *BarOffsetPtr = BaseIoAddr % IOMM_TABLE_ENTRY_SIZE;
495 } else
496 panic("PCI: Invalid PCI IoAddress detected!\n");
497 return DevNode;
498}
499
500/*
501 * Read MM I/O Instructions for the iSeries
502 * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
503 * else, data is returned in big Endian format.
504 *
505 * iSeries_Read_Byte = Read Byte ( 8 bit)
506 * iSeries_Read_Word = Read Word (16 bit)
507 * iSeries_Read_Long = Read Long (32 bit)
508 */
509u8 iSeries_Read_Byte(const volatile void __iomem *IoAddress)
510{
511 u64 BarOffset;
512 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700513 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000515 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
517
518 if (DevNode == NULL) {
519 static unsigned long last_jiffies;
520 static int num_printed;
521
522 if ((jiffies - last_jiffies) > 60 * HZ) {
523 last_jiffies = jiffies;
524 num_printed = 0;
525 }
526 if (num_printed++ < 10)
527 printk(KERN_ERR "iSeries_Read_Byte: invalid access at IO address %p\n", IoAddress);
528 return 0xff;
529 }
530 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700532 } while (CheckReturnCode("RDB", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 return (u8)ret.value;
535}
536EXPORT_SYMBOL(iSeries_Read_Byte);
537
538u16 iSeries_Read_Word(const volatile void __iomem *IoAddress)
539{
540 u64 BarOffset;
541 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700542 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000544 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
546
547 if (DevNode == NULL) {
548 static unsigned long last_jiffies;
549 static int num_printed;
550
551 if ((jiffies - last_jiffies) > 60 * HZ) {
552 last_jiffies = jiffies;
553 num_printed = 0;
554 }
555 if (num_printed++ < 10)
556 printk(KERN_ERR "iSeries_Read_Word: invalid access at IO address %p\n", IoAddress);
557 return 0xffff;
558 }
559 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
561 BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700562 } while (CheckReturnCode("RDW", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 return swab16((u16)ret.value);
565}
566EXPORT_SYMBOL(iSeries_Read_Word);
567
568u32 iSeries_Read_Long(const volatile void __iomem *IoAddress)
569{
570 u64 BarOffset;
571 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700572 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000574 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
576
577 if (DevNode == NULL) {
578 static unsigned long last_jiffies;
579 static int num_printed;
580
581 if ((jiffies - last_jiffies) > 60 * HZ) {
582 last_jiffies = jiffies;
583 num_printed = 0;
584 }
585 if (num_printed++ < 10)
586 printk(KERN_ERR "iSeries_Read_Long: invalid access at IO address %p\n", IoAddress);
587 return 0xffffffff;
588 }
589 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
591 BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700592 } while (CheckReturnCode("RDL", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 return swab32((u32)ret.value);
595}
596EXPORT_SYMBOL(iSeries_Read_Long);
597
598/*
599 * Write MM I/O Instructions for the iSeries
600 *
601 * iSeries_Write_Byte = Write Byte (8 bit)
602 * iSeries_Write_Word = Write Word(16 bit)
603 * iSeries_Write_Long = Write Long(32 bit)
604 */
605void iSeries_Write_Byte(u8 data, volatile void __iomem *IoAddress)
606{
607 u64 BarOffset;
608 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700609 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000611 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
613
614 if (DevNode == NULL) {
615 static unsigned long last_jiffies;
616 static int num_printed;
617
618 if ((jiffies - last_jiffies) > 60 * HZ) {
619 last_jiffies = jiffies;
620 num_printed = 0;
621 }
622 if (num_printed++ < 10)
623 printk(KERN_ERR "iSeries_Write_Byte: invalid access at IO address %p\n", IoAddress);
624 return;
625 }
626 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 rc = HvCall4(HvCallPciBarStore8, dsa, BarOffset, data, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700628 } while (CheckReturnCode("WWB", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630EXPORT_SYMBOL(iSeries_Write_Byte);
631
632void iSeries_Write_Word(u16 data, volatile void __iomem *IoAddress)
633{
634 u64 BarOffset;
635 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700636 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000638 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
640
641 if (DevNode == NULL) {
642 static unsigned long last_jiffies;
643 static int num_printed;
644
645 if ((jiffies - last_jiffies) > 60 * HZ) {
646 last_jiffies = jiffies;
647 num_printed = 0;
648 }
649 if (num_printed++ < 10)
650 printk(KERN_ERR "iSeries_Write_Word: invalid access at IO address %p\n", IoAddress);
651 return;
652 }
653 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 rc = HvCall4(HvCallPciBarStore16, dsa, BarOffset, swab16(data), 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700655 } while (CheckReturnCode("WWW", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657EXPORT_SYMBOL(iSeries_Write_Word);
658
659void iSeries_Write_Long(u32 data, volatile void __iomem *IoAddress)
660{
661 u64 BarOffset;
662 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700663 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000665 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
667
668 if (DevNode == NULL) {
669 static unsigned long last_jiffies;
670 static int num_printed;
671
672 if ((jiffies - last_jiffies) > 60 * HZ) {
673 last_jiffies = jiffies;
674 num_printed = 0;
675 }
676 if (num_printed++ < 10)
677 printk(KERN_ERR "iSeries_Write_Long: invalid access at IO address %p\n", IoAddress);
678 return;
679 }
680 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 rc = HvCall4(HvCallPciBarStore32, dsa, BarOffset, swab32(data), 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700682 } while (CheckReturnCode("WWL", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684EXPORT_SYMBOL(iSeries_Write_Long);