blob: 35bcc98111f5b7077faa9a1f612861cfa678b983 [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 Rothwell095eed42006-05-19 16:54:42 +1000169 struct device_node *root = of_find_node_by_path("/");
170 struct device_node *node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Stephen Rothwell095eed42006-05-19 16:54:42 +1000172 if (root == NULL) {
173 printk(KERN_CRIT "iSeries_pcibios_init: can't find root "
174 "of device tree\n");
175 return;
176 }
177 while ((node = of_get_next_child(root, node)) != NULL) {
Stephen Rothwell0d177df2006-05-19 16:46:28 +1000178 HvBusNumber bus;
179 u32 *busp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Stephen Rothwell095eed42006-05-19 16:54:42 +1000181 if ((node->type == NULL) || (strcmp(node->type, "pci") != 0))
182 continue;
183
Stephen Rothwell0d177df2006-05-19 16:46:28 +1000184 busp = (u32 *)get_property(node, "bus-range", NULL);
185 if (busp == NULL)
186 continue;
187 bus = *busp;
188 printk("bus %d appears to exist\n", bus);
189 phb = pcibios_alloc_controller(node);
190 if (phb == NULL)
191 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Stephen Rothwell0d177df2006-05-19 16:46:28 +1000193 phb->pci_mem_offset = phb->local_number = bus;
194 phb->first_busno = bus;
195 phb->last_busno = bus;
196 phb->ops = &iSeries_pci_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Stephen Rothwell095eed42006-05-19 16:54:42 +1000198
199 of_node_put(root);
200
201 pci_devs_phb_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204/*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000205 * iSeries_pci_final_fixup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
207void __init iSeries_pci_final_fixup(void)
208{
209 struct pci_dev *pdev = NULL;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000210 struct device_node *node;
Stephen Rothwelld3878992005-09-28 02:50:25 +1000211 int DeviceCount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Fix up at the device node and pci_dev relationship */
214 mf_display_src(0xC9000100);
215
216 printk("pcibios_final_fixup\n");
217 for_each_pci_dev(pdev) {
218 node = find_Device_Node(pdev->bus->number, pdev->devfn);
219 printk("pci dev %p (%x.%x), node %p\n", pdev,
220 pdev->bus->number, pdev->devfn, node);
221
222 if (node != NULL) {
Stephen Rothwellb0252792006-05-19 16:50:39 +1000223 struct pci_dn *pdn = PCI_DN(node);
224 u32 *agent;
225
226 agent = (u32 *)get_property(node, "linux,agent-id",
227 NULL);
228 if ((pdn != NULL) && (agent != NULL)) {
229 u8 irq = iSeries_allocate_IRQ(pdn->busno, 0,
230 pdn->bussubno);
231 int err;
232
233 err = HvCallXm_connectBusUnit(pdn->busno, pdn->bussubno,
234 *agent, irq);
235 if (err)
236 pci_Log_Error("Connect Bus Unit",
237 pdn->busno, pdn->bussubno, *agent, err);
238 else {
239 err = HvCallPci_configStore8(pdn->busno, pdn->bussubno,
240 *agent,
241 PCI_INTERRUPT_LINE,
242 irq);
243 if (err)
244 pci_Log_Error("PciCfgStore Irq Failed!",
245 pdn->busno, pdn->bussubno, *agent, err);
246 }
247 if (!err)
248 pdev->irq = irq;
249 }
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 ++DeviceCount;
252 pdev->sysdata = (void *)node;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000253 PCI_DN(node)->pcidev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 allocate_device_bars(pdev);
Stephen Rothwell061c0632005-06-21 17:15:48 -0700255 iSeries_Device_Information(pdev, DeviceCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 iommu_devnode_init_iSeries(node);
257 } else
258 printk("PCI: Device Tree not found for 0x%016lX\n",
259 (unsigned long)pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 iSeries_activate_IRQs();
262 mf_display_src(0xC9000200);
263}
264
265void pcibios_fixup_bus(struct pci_bus *PciBus)
266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269void pcibios_fixup_resources(struct pci_dev *pdev)
270{
Stephen Rothwelld3878992005-09-28 02:50:25 +1000271}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * I/0 Memory copy MUST use mmio commands on iSeries
275 * To do; For performance, include the hv call directly
276 */
277void iSeries_memset_io(volatile void __iomem *dest, char c, size_t Count)
278{
279 u8 ByteValue = c;
280 long NumberOfBytes = Count;
281
282 while (NumberOfBytes > 0) {
283 iSeries_Write_Byte(ByteValue, dest++);
284 -- NumberOfBytes;
285 }
286}
287EXPORT_SYMBOL(iSeries_memset_io);
288
289void iSeries_memcpy_toio(volatile void __iomem *dest, void *source, size_t count)
290{
291 char *src = source;
292 long NumberOfBytes = count;
293
294 while (NumberOfBytes > 0) {
295 iSeries_Write_Byte(*src++, dest++);
296 -- NumberOfBytes;
297 }
298}
299EXPORT_SYMBOL(iSeries_memcpy_toio);
300
301void iSeries_memcpy_fromio(void *dest, const volatile void __iomem *src, size_t count)
302{
303 char *dst = dest;
304 long NumberOfBytes = count;
305
306 while (NumberOfBytes > 0) {
307 *dst++ = iSeries_Read_Byte(src++);
308 -- NumberOfBytes;
309 }
310}
311EXPORT_SYMBOL(iSeries_memcpy_fromio);
312
313/*
314 * Look down the chain to find the matching Device Device
315 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000316static struct device_node *find_Device_Node(int bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Stephen Rothwell96ff6af2006-05-19 16:48:47 +1000318 struct device_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Stephen Rothwell96ff6af2006-05-19 16:48:47 +1000320 for (node = NULL; (node = of_find_all_nodes(node)); ) {
321 struct pci_dn *pdn = PCI_DN(node);
322
323 if (pdn && (bus == pdn->busno) && (devfn == pdn->devfn))
324 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326 return NULL;
327}
328
329#if 0
330/*
331 * Returns the device node for the passed pci_dev
332 * Sanity Check Node PciDev to passed pci_dev
333 * If none is found, returns a NULL which the client must handle.
334 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000335static struct device_node *get_Device_Node(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000337 struct device_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 node = pdev->sysdata;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000340 if (node == NULL || PCI_DN(node)->pcidev != pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 node = find_Device_Node(pdev->bus->number, pdev->devfn);
342 return node;
343}
344#endif
345
346/*
347 * Config space read and write functions.
348 * For now at least, we look for the device node for the bus and devfn
349 * that we are asked to access. It may be possible to translate the devfn
350 * to a subbus and deviceid more directly.
351 */
352static u64 hv_cfg_read_func[4] = {
353 HvCallPciConfigLoad8, HvCallPciConfigLoad16,
354 HvCallPciConfigLoad32, HvCallPciConfigLoad32
355};
356
357static u64 hv_cfg_write_func[4] = {
358 HvCallPciConfigStore8, HvCallPciConfigStore16,
359 HvCallPciConfigStore32, HvCallPciConfigStore32
360};
361
362/*
363 * Read PCI config space
364 */
365static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
366 int offset, int size, u32 *val)
367{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000368 struct device_node *node = find_Device_Node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 u64 fn;
370 struct HvCallPci_LoadReturn ret;
371
372 if (node == NULL)
373 return PCIBIOS_DEVICE_NOT_FOUND;
374 if (offset > 255) {
375 *val = ~0;
376 return PCIBIOS_BAD_REGISTER_NUMBER;
377 }
378
379 fn = hv_cfg_read_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000380 HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 if (ret.rc != 0) {
383 *val = ~0;
384 return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
385 }
386
387 *val = ret.value;
388 return 0;
389}
390
391/*
392 * Write PCI config space
393 */
394
395static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
396 int offset, int size, u32 val)
397{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000398 struct device_node *node = find_Device_Node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 u64 fn;
400 u64 ret;
401
402 if (node == NULL)
403 return PCIBIOS_DEVICE_NOT_FOUND;
404 if (offset > 255)
405 return PCIBIOS_BAD_REGISTER_NUMBER;
406
407 fn = hv_cfg_write_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000408 ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 if (ret != 0)
411 return PCIBIOS_DEVICE_NOT_FOUND;
412
413 return 0;
414}
415
416static struct pci_ops iSeries_pci_ops = {
417 .read = iSeries_pci_read_config,
418 .write = iSeries_pci_write_config
419};
420
421/*
422 * Check Return Code
423 * -> On Failure, print and log information.
424 * Increment Retry Count, if exceeds max, panic partition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 *
426 * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
427 * PCI: Device 23.90 ReadL Retry( 1)
428 * PCI: Device 23.90 ReadL Retry Successful(1)
429 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000430static int CheckReturnCode(char *TextHdr, struct device_node *DevNode,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700431 int *retry, u64 ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 if (ret != 0) {
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000434 struct pci_dn *pdn = PCI_DN(DevNode);
435
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700436 (*retry)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000438 TextHdr, pdn->busno, pdn->devfn,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700439 *retry, (int)ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /*
441 * Bump the retry and check for retry count exceeded.
442 * If, Exceeded, panic the system.
443 */
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700444 if (((*retry) > Pci_Retry_Max) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 (Pci_Error_Flag > 0)) {
446 mf_display_src(0xB6000103);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700447 panic_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 panic("PCI: Hardware I/O Error, SRC B6000103, "
449 "Automatic Reboot Disabled.\n");
450 }
451 return -1; /* Retry Try */
452 }
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456/*
457 * Translate the I/O Address into a device node, bar, and bar offset.
458 * Note: Make sure the passed variable end up on the stack to avoid
459 * the exposure of being device global.
460 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000461static inline struct device_node *xlate_iomm_address(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 const volatile void __iomem *IoAddress,
463 u64 *dsaptr, u64 *BarOffsetPtr)
464{
465 unsigned long OrigIoAddr;
466 unsigned long BaseIoAddr;
467 unsigned long TableIndex;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000468 struct device_node *DevNode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 OrigIoAddr = (unsigned long __force)IoAddress;
471 if ((OrigIoAddr < BASE_IO_MEMORY) || (OrigIoAddr >= max_io_memory))
472 return NULL;
473 BaseIoAddr = OrigIoAddr - BASE_IO_MEMORY;
474 TableIndex = BaseIoAddr / IOMM_TABLE_ENTRY_SIZE;
475 DevNode = iomm_table[TableIndex];
476
477 if (DevNode != NULL) {
478 int barnum = iobar_table[TableIndex];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000479 *dsaptr = iseries_ds_addr(DevNode) | (barnum << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 *BarOffsetPtr = BaseIoAddr % IOMM_TABLE_ENTRY_SIZE;
481 } else
482 panic("PCI: Invalid PCI IoAddress detected!\n");
483 return DevNode;
484}
485
486/*
487 * Read MM I/O Instructions for the iSeries
488 * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
489 * else, data is returned in big Endian format.
490 *
491 * iSeries_Read_Byte = Read Byte ( 8 bit)
492 * iSeries_Read_Word = Read Word (16 bit)
493 * iSeries_Read_Long = Read Long (32 bit)
494 */
495u8 iSeries_Read_Byte(const volatile void __iomem *IoAddress)
496{
497 u64 BarOffset;
498 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700499 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000501 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
503
504 if (DevNode == NULL) {
505 static unsigned long last_jiffies;
506 static int num_printed;
507
508 if ((jiffies - last_jiffies) > 60 * HZ) {
509 last_jiffies = jiffies;
510 num_printed = 0;
511 }
512 if (num_printed++ < 10)
513 printk(KERN_ERR "iSeries_Read_Byte: invalid access at IO address %p\n", IoAddress);
514 return 0xff;
515 }
516 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700518 } while (CheckReturnCode("RDB", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 return (u8)ret.value;
521}
522EXPORT_SYMBOL(iSeries_Read_Byte);
523
524u16 iSeries_Read_Word(const volatile void __iomem *IoAddress)
525{
526 u64 BarOffset;
527 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700528 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000530 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
532
533 if (DevNode == NULL) {
534 static unsigned long last_jiffies;
535 static int num_printed;
536
537 if ((jiffies - last_jiffies) > 60 * HZ) {
538 last_jiffies = jiffies;
539 num_printed = 0;
540 }
541 if (num_printed++ < 10)
542 printk(KERN_ERR "iSeries_Read_Word: invalid access at IO address %p\n", IoAddress);
543 return 0xffff;
544 }
545 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
547 BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700548 } while (CheckReturnCode("RDW", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 return swab16((u16)ret.value);
551}
552EXPORT_SYMBOL(iSeries_Read_Word);
553
554u32 iSeries_Read_Long(const volatile void __iomem *IoAddress)
555{
556 u64 BarOffset;
557 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700558 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000560 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
562
563 if (DevNode == NULL) {
564 static unsigned long last_jiffies;
565 static int num_printed;
566
567 if ((jiffies - last_jiffies) > 60 * HZ) {
568 last_jiffies = jiffies;
569 num_printed = 0;
570 }
571 if (num_printed++ < 10)
572 printk(KERN_ERR "iSeries_Read_Long: invalid access at IO address %p\n", IoAddress);
573 return 0xffffffff;
574 }
575 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
577 BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700578 } while (CheckReturnCode("RDL", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 return swab32((u32)ret.value);
581}
582EXPORT_SYMBOL(iSeries_Read_Long);
583
584/*
585 * Write MM I/O Instructions for the iSeries
586 *
587 * iSeries_Write_Byte = Write Byte (8 bit)
588 * iSeries_Write_Word = Write Word(16 bit)
589 * iSeries_Write_Long = Write Long(32 bit)
590 */
591void iSeries_Write_Byte(u8 data, volatile void __iomem *IoAddress)
592{
593 u64 BarOffset;
594 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700595 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000597 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
599
600 if (DevNode == NULL) {
601 static unsigned long last_jiffies;
602 static int num_printed;
603
604 if ((jiffies - last_jiffies) > 60 * HZ) {
605 last_jiffies = jiffies;
606 num_printed = 0;
607 }
608 if (num_printed++ < 10)
609 printk(KERN_ERR "iSeries_Write_Byte: invalid access at IO address %p\n", IoAddress);
610 return;
611 }
612 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 rc = HvCall4(HvCallPciBarStore8, dsa, BarOffset, data, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700614 } while (CheckReturnCode("WWB", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616EXPORT_SYMBOL(iSeries_Write_Byte);
617
618void iSeries_Write_Word(u16 data, volatile void __iomem *IoAddress)
619{
620 u64 BarOffset;
621 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700622 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000624 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
626
627 if (DevNode == NULL) {
628 static unsigned long last_jiffies;
629 static int num_printed;
630
631 if ((jiffies - last_jiffies) > 60 * HZ) {
632 last_jiffies = jiffies;
633 num_printed = 0;
634 }
635 if (num_printed++ < 10)
636 printk(KERN_ERR "iSeries_Write_Word: invalid access at IO address %p\n", IoAddress);
637 return;
638 }
639 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 rc = HvCall4(HvCallPciBarStore16, dsa, BarOffset, swab16(data), 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700641 } while (CheckReturnCode("WWW", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643EXPORT_SYMBOL(iSeries_Write_Word);
644
645void iSeries_Write_Long(u32 data, volatile void __iomem *IoAddress)
646{
647 u64 BarOffset;
648 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700649 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000651 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
653
654 if (DevNode == NULL) {
655 static unsigned long last_jiffies;
656 static int num_printed;
657
658 if ((jiffies - last_jiffies) > 60 * HZ) {
659 last_jiffies = jiffies;
660 num_printed = 0;
661 }
662 if (num_printed++ < 10)
663 printk(KERN_ERR "iSeries_Write_Long: invalid access at IO address %p\n", IoAddress);
664 return;
665 }
666 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 rc = HvCall4(HvCallPciBarStore32, dsa, BarOffset, swab32(data), 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700668 } while (CheckReturnCode("WWL", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670EXPORT_SYMBOL(iSeries_Write_Long);