blob: dafc518fbb8334ec5db75eb388e34e1c09ca6fcf [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Stephen Rothwelld3878992005-09-28 02:50:25 +100041#include <asm/ppc-pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Stephen Rothwellb08567cb2005-09-28 23:37:01 +100043#include "irq.h"
Stephen Rothwell426c1a12005-10-14 14:51:42 +100044#include "pci.h"
Stephen Rothwellc6d2ea92005-10-14 17:16:17 +100045#include "call_pci.h"
Stephen Rothwellb08567cb2005-09-28 23:37:01 +100046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047extern unsigned long io_page_mask;
48
49/*
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 -070053static void scan_PHB_slots(struct pci_controller *Phb);
54static void scan_EADS_bridge(HvBusNumber Bus, HvSubBusNumber SubBus, int IdSel);
55static int scan_bridge_slot(HvBusNumber Bus, struct HvCallPci_BridgeInfo *Info);
56
57LIST_HEAD(iSeries_Global_Device_List);
58
59static int DeviceCount;
60
61/* Counters and control flags. */
62static long Pci_Io_Read_Count;
63static long Pci_Io_Write_Count;
64#if 0
65static long Pci_Cfg_Read_Count;
66static long Pci_Cfg_Write_Count;
67#endif
68static long Pci_Error_Count;
69
Stephen Rothwelld3878992005-09-28 02:50:25 +100070static int Pci_Retry_Max = 3; /* Only retry 3 times */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static int Pci_Error_Flag = 1; /* Set Retry Error on. */
72
73static struct pci_ops iSeries_pci_ops;
74
75/*
76 * Table defines
77 * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
78 */
79#define IOMM_TABLE_MAX_ENTRIES 1024
80#define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
81#define BASE_IO_MEMORY 0xE000000000000000UL
82
83static unsigned long max_io_memory = 0xE000000000000000UL;
84static long current_iomm_table_entry;
85
86/*
87 * Lookup Tables.
88 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +100089static struct device_node **iomm_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static u8 *iobar_table;
91
92/*
93 * Static and Global variables
94 */
95static char *pci_io_text = "iSeries PCI I/O";
96static DEFINE_SPINLOCK(iomm_table_lock);
97
98/*
99 * iomm_table_initialize
100 *
101 * Allocates and initalizes the Address Translation Table and Bar
102 * Tables to get them ready for use. Must be called before any
103 * I/O space is handed out to the device BARs.
104 */
105static void iomm_table_initialize(void)
106{
107 spin_lock(&iomm_table_lock);
108 iomm_table = kmalloc(sizeof(*iomm_table) * IOMM_TABLE_MAX_ENTRIES,
109 GFP_KERNEL);
110 iobar_table = kmalloc(sizeof(*iobar_table) * IOMM_TABLE_MAX_ENTRIES,
111 GFP_KERNEL);
112 spin_unlock(&iomm_table_lock);
113 if ((iomm_table == NULL) || (iobar_table == NULL))
114 panic("PCI: I/O tables allocation failed.\n");
115}
116
117/*
118 * iomm_table_allocate_entry
119 *
120 * Adds pci_dev entry in address translation table
121 *
122 * - Allocates the number of entries required in table base on BAR
123 * size.
124 * - Allocates starting at BASE_IO_MEMORY and increases.
125 * - The size is round up to be a multiple of entry size.
126 * - CurrentIndex is incremented to keep track of the last entry.
127 * - Builds the resource entry for allocated BARs.
128 */
129static void iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
130{
131 struct resource *bar_res = &dev->resource[bar_num];
132 long bar_size = pci_resource_len(dev, bar_num);
133
134 /*
135 * No space to allocate, quick exit, skip Allocation.
136 */
137 if (bar_size == 0)
138 return;
139 /*
140 * Set Resource values.
141 */
142 spin_lock(&iomm_table_lock);
143 bar_res->name = pci_io_text;
144 bar_res->start =
145 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
146 bar_res->start += BASE_IO_MEMORY;
147 bar_res->end = bar_res->start + bar_size - 1;
148 /*
149 * Allocate the number of table entries needed for BAR.
150 */
151 while (bar_size > 0 ) {
152 iomm_table[current_iomm_table_entry] = dev->sysdata;
153 iobar_table[current_iomm_table_entry] = bar_num;
154 bar_size -= IOMM_TABLE_ENTRY_SIZE;
155 ++current_iomm_table_entry;
156 }
157 max_io_memory = BASE_IO_MEMORY +
158 (IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry);
159 spin_unlock(&iomm_table_lock);
160}
161
162/*
163 * allocate_device_bars
164 *
165 * - Allocates ALL pci_dev BAR's and updates the resources with the
166 * BAR value. BARS with zero length will have the resources
167 * The HvCallPci_getBarParms is used to get the size of the BAR
168 * space. It calls iomm_table_allocate_entry to allocate
169 * each entry.
170 * - Loops through The Bar resources(0 - 5) including the ROM
171 * is resource(6).
172 */
173static void allocate_device_bars(struct pci_dev *dev)
174{
175 struct resource *bar_res;
176 int bar_num;
177
178 for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num) {
179 bar_res = &dev->resource[bar_num];
180 iomm_table_allocate_entry(dev, bar_num);
Stephen Rothwelld3878992005-09-28 02:50:25 +1000181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184/*
185 * Log error information to system console.
186 * Filter out the device not there errors.
187 * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
188 * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
189 * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
190 */
191static void pci_Log_Error(char *Error_Text, int Bus, int SubBus,
192 int AgentId, int HvRc)
193{
194 if (HvRc == 0x0302)
195 return;
196 printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
197 Error_Text, Bus, SubBus, AgentId, HvRc);
198}
199
200/*
201 * build_device_node(u16 Bus, int SubBus, u8 DevFn)
202 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000203static struct device_node *build_device_node(HvBusNumber Bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 HvSubBusNumber SubBus, int AgentId, int Function)
205{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000206 struct device_node *node;
207 struct pci_dn *pdn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000209 node = kmalloc(sizeof(struct device_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (node == NULL)
211 return NULL;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000212 memset(node, 0, sizeof(struct device_node));
213 pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
214 if (pdn == NULL) {
215 kfree(node);
216 return NULL;
217 }
218 node->data = pdn;
Stephen Rothwellf255f0d2005-10-10 17:10:54 +1000219 pdn->node = node;
Paul Mackerras76f9f872005-10-10 22:52:26 +1000220 list_add_tail(&pdn->Device_List, &iSeries_Global_Device_List);
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000221 pdn->busno = Bus;
222 pdn->bussubno = SubBus;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000223 pdn->devfn = PCI_DEVFN(ISERIES_ENCODE_DEVICE(AgentId), Function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return node;
225}
226
227/*
228 * unsigned long __init find_and_init_phbs(void)
229 *
230 * Description:
231 * This function checks for all possible system PCI host bridges that connect
232 * PCI buses. The system hypervisor is queried as to the guest partition
233 * ownership status. A pci_controller is built for any bus which is partially
234 * owned or fully owned by this guest partition.
235 */
236unsigned long __init find_and_init_phbs(void)
237{
238 struct pci_controller *phb;
239 HvBusNumber bus;
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* Check all possible buses. */
242 for (bus = 0; bus < 256; bus++) {
243 int ret = HvCallXm_testBus(bus);
244 if (ret == 0) {
245 printk("bus %d appears to exist\n", bus);
246
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100247 phb = pcibios_alloc_controller(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (phb == NULL)
249 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 phb->pci_mem_offset = phb->local_number = bus;
252 phb->first_busno = bus;
253 phb->last_busno = bus;
254 phb->ops = &iSeries_pci_ops;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* Find and connect the devices. */
257 scan_PHB_slots(phb);
258 }
259 /*
260 * Check for Unexpected Return code, a clue that something
261 * has gone wrong.
262 */
263 else if (ret != 0x0301)
264 printk(KERN_ERR "Unexpected Return on Probe(0x%04X): 0x%04X",
265 bus, ret);
266 }
267 return 0;
268}
269
270/*
271 * iSeries_pcibios_init
Stephen Rothwelld3878992005-09-28 02:50:25 +1000272 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * Chance to initialize and structures or variable before PCI Bus walk.
274 */
275void iSeries_pcibios_init(void)
276{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 iomm_table_initialize();
278 find_and_init_phbs();
279 io_page_mask = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000283 * iSeries_pci_final_fixup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
285void __init iSeries_pci_final_fixup(void)
286{
287 struct pci_dev *pdev = NULL;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000288 struct device_node *node;
Stephen Rothwelld3878992005-09-28 02:50:25 +1000289 int DeviceCount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* Fix up at the device node and pci_dev relationship */
292 mf_display_src(0xC9000100);
293
294 printk("pcibios_final_fixup\n");
295 for_each_pci_dev(pdev) {
296 node = find_Device_Node(pdev->bus->number, pdev->devfn);
297 printk("pci dev %p (%x.%x), node %p\n", pdev,
298 pdev->bus->number, pdev->devfn, node);
299
300 if (node != NULL) {
301 ++DeviceCount;
302 pdev->sysdata = (void *)node;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000303 PCI_DN(node)->pcidev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 allocate_device_bars(pdev);
Stephen Rothwell061c0632005-06-21 17:15:48 -0700305 iSeries_Device_Information(pdev, DeviceCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 iommu_devnode_init_iSeries(node);
307 } else
308 printk("PCI: Device Tree not found for 0x%016lX\n",
309 (unsigned long)pdev);
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000310 pdev->irq = PCI_DN(node)->Irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312 iSeries_activate_IRQs();
313 mf_display_src(0xC9000200);
314}
315
316void pcibios_fixup_bus(struct pci_bus *PciBus)
317{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320void pcibios_fixup_resources(struct pci_dev *pdev)
321{
Stephen Rothwelld3878992005-09-28 02:50:25 +1000322}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324/*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000325 * Loop through each node function to find usable EADs bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
327static void scan_PHB_slots(struct pci_controller *Phb)
328{
329 struct HvCallPci_DeviceInfo *DevInfo;
Stephen Rothwelld3878992005-09-28 02:50:25 +1000330 HvBusNumber bus = Phb->local_number; /* System Bus */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 const HvSubBusNumber SubBus = 0; /* EADs is always 0. */
332 int HvRc = 0;
Stephen Rothwelld3878992005-09-28 02:50:25 +1000333 int IdSel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 const int MaxAgents = 8;
335
336 DevInfo = (struct HvCallPci_DeviceInfo*)
337 kmalloc(sizeof(struct HvCallPci_DeviceInfo), GFP_KERNEL);
338 if (DevInfo == NULL)
339 return;
340
341 /*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000342 * Probe for EADs Bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
344 for (IdSel = 1; IdSel < MaxAgents; ++IdSel) {
Stephen Rothwelld3878992005-09-28 02:50:25 +1000345 HvRc = HvCallPci_getDeviceInfo(bus, SubBus, IdSel,
Stephen Rothwell426c1a12005-10-14 14:51:42 +1000346 iseries_hv_addr(DevInfo),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 sizeof(struct HvCallPci_DeviceInfo));
348 if (HvRc == 0) {
349 if (DevInfo->deviceType == HvCallPci_NodeDevice)
350 scan_EADS_bridge(bus, SubBus, IdSel);
351 else
352 printk("PCI: Invalid System Configuration(0x%02X)"
353 " for bus 0x%02x id 0x%02x.\n",
354 DevInfo->deviceType, bus, IdSel);
355 }
356 else
357 pci_Log_Error("getDeviceInfo", bus, SubBus, IdSel, HvRc);
358 }
359 kfree(DevInfo);
360}
361
362static void scan_EADS_bridge(HvBusNumber bus, HvSubBusNumber SubBus,
363 int IdSel)
364{
365 struct HvCallPci_BridgeInfo *BridgeInfo;
366 HvAgentId AgentId;
367 int Function;
368 int HvRc;
369
370 BridgeInfo = (struct HvCallPci_BridgeInfo *)
371 kmalloc(sizeof(struct HvCallPci_BridgeInfo), GFP_KERNEL);
372 if (BridgeInfo == NULL)
373 return;
374
375 /* Note: hvSubBus and irq is always be 0 at this level! */
376 for (Function = 0; Function < 8; ++Function) {
Stephen Rothwelld3878992005-09-28 02:50:25 +1000377 AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 HvRc = HvCallXm_connectBusUnit(bus, SubBus, AgentId, 0);
Stephen Rothwelld3878992005-09-28 02:50:25 +1000379 if (HvRc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 printk("found device at bus %d idsel %d func %d (AgentId %x)\n",
381 bus, IdSel, Function, AgentId);
Stephen Rothwelld3878992005-09-28 02:50:25 +1000382 /* Connect EADs: 0x18.00.12 = 0x00 */
Stephen Rothwelld3878992005-09-28 02:50:25 +1000383 HvRc = HvCallPci_getBusUnitInfo(bus, SubBus, AgentId,
Stephen Rothwell426c1a12005-10-14 14:51:42 +1000384 iseries_hv_addr(BridgeInfo),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 sizeof(struct HvCallPci_BridgeInfo));
Stephen Rothwelld3878992005-09-28 02:50:25 +1000386 if (HvRc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 printk("bridge info: type %x subbus %x maxAgents %x maxsubbus %x logslot %x\n",
388 BridgeInfo->busUnitInfo.deviceType,
389 BridgeInfo->subBusNumber,
390 BridgeInfo->maxAgents,
391 BridgeInfo->maxSubBusNumber,
392 BridgeInfo->logicalSlotNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (BridgeInfo->busUnitInfo.deviceType ==
394 HvCallPci_BridgeDevice) {
395 /* Scan_Bridge_Slot...: 0x18.00.12 */
396 scan_bridge_slot(bus, BridgeInfo);
397 } else
398 printk("PCI: Invalid Bridge Configuration(0x%02X)",
399 BridgeInfo->busUnitInfo.deviceType);
400 }
Stephen Rothwelld3878992005-09-28 02:50:25 +1000401 } else if (HvRc != 0x000B)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 pci_Log_Error("EADs Connect",
403 bus, SubBus, AgentId, HvRc);
404 }
405 kfree(BridgeInfo);
406}
407
408/*
409 * This assumes that the node slot is always on the primary bus!
410 */
411static int scan_bridge_slot(HvBusNumber Bus,
412 struct HvCallPci_BridgeInfo *BridgeInfo)
413{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000414 struct device_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 HvSubBusNumber SubBus = BridgeInfo->subBusNumber;
416 u16 VendorId = 0;
417 int HvRc = 0;
418 u8 Irq = 0;
419 int IdSel = ISERIES_GET_DEVICE_FROM_SUBBUS(SubBus);
420 int Function = ISERIES_GET_FUNCTION_FROM_SUBBUS(SubBus);
421 HvAgentId EADsIdSel = ISERIES_PCI_AGENTID(IdSel, Function);
422
423 /* iSeries_allocate_IRQ.: 0x18.00.12(0xA3) */
Stephen Rothwelld3878992005-09-28 02:50:25 +1000424 Irq = iSeries_allocate_IRQ(Bus, 0, EADsIdSel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000427 * Connect all functions of any device found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
Stephen Rothwelld3878992005-09-28 02:50:25 +1000429 for (IdSel = 1; IdSel <= BridgeInfo->maxAgents; ++IdSel) {
430 for (Function = 0; Function < 8; ++Function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 HvAgentId AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
432 HvRc = HvCallXm_connectBusUnit(Bus, SubBus,
433 AgentId, Irq);
434 if (HvRc != 0) {
435 pci_Log_Error("Connect Bus Unit",
436 Bus, SubBus, AgentId, HvRc);
437 continue;
438 }
439
440 HvRc = HvCallPci_configLoad16(Bus, SubBus, AgentId,
441 PCI_VENDOR_ID, &VendorId);
442 if (HvRc != 0) {
443 pci_Log_Error("Read Vendor",
444 Bus, SubBus, AgentId, HvRc);
445 continue;
446 }
447 printk("read vendor ID: %x\n", VendorId);
448
449 /* FoundDevice: 0x18.28.10 = 0x12AE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 HvRc = HvCallPci_configStore8(Bus, SubBus, AgentId,
Stephen Rothwelld3878992005-09-28 02:50:25 +1000451 PCI_INTERRUPT_LINE, Irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (HvRc != 0)
453 pci_Log_Error("PciCfgStore Irq Failed!",
454 Bus, SubBus, AgentId, HvRc);
455
456 ++DeviceCount;
457 node = build_device_node(Bus, SubBus, EADsIdSel, Function);
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000458 PCI_DN(node)->Irq = Irq;
459 PCI_DN(node)->LogicalSlot = BridgeInfo->logicalSlotNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 } /* for (Function = 0; Function < 8; ++Function) */
462 } /* for (IdSel = 1; IdSel <= MaxAgents; ++IdSel) */
463 return HvRc;
464}
465
466/*
467 * I/0 Memory copy MUST use mmio commands on iSeries
468 * To do; For performance, include the hv call directly
469 */
470void iSeries_memset_io(volatile void __iomem *dest, char c, size_t Count)
471{
472 u8 ByteValue = c;
473 long NumberOfBytes = Count;
474
475 while (NumberOfBytes > 0) {
476 iSeries_Write_Byte(ByteValue, dest++);
477 -- NumberOfBytes;
478 }
479}
480EXPORT_SYMBOL(iSeries_memset_io);
481
482void iSeries_memcpy_toio(volatile void __iomem *dest, void *source, size_t count)
483{
484 char *src = source;
485 long NumberOfBytes = count;
486
487 while (NumberOfBytes > 0) {
488 iSeries_Write_Byte(*src++, dest++);
489 -- NumberOfBytes;
490 }
491}
492EXPORT_SYMBOL(iSeries_memcpy_toio);
493
494void iSeries_memcpy_fromio(void *dest, const volatile void __iomem *src, size_t count)
495{
496 char *dst = dest;
497 long NumberOfBytes = count;
498
499 while (NumberOfBytes > 0) {
500 *dst++ = iSeries_Read_Byte(src++);
501 -- NumberOfBytes;
502 }
503}
504EXPORT_SYMBOL(iSeries_memcpy_fromio);
505
506/*
507 * Look down the chain to find the matching Device Device
508 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000509static struct device_node *find_Device_Node(int bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Paul Mackerras76f9f872005-10-10 22:52:26 +1000511 struct pci_dn *pdn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Paul Mackerras76f9f872005-10-10 22:52:26 +1000513 list_for_each_entry(pdn, &iSeries_Global_Device_List, Device_List) {
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000514 if ((bus == pdn->busno) && (devfn == pdn->devfn))
Paul Mackerras76f9f872005-10-10 22:52:26 +1000515 return pdn->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 return NULL;
518}
519
520#if 0
521/*
522 * Returns the device node for the passed pci_dev
523 * Sanity Check Node PciDev to passed pci_dev
524 * If none is found, returns a NULL which the client must handle.
525 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000526static struct device_node *get_Device_Node(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000528 struct device_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 node = pdev->sysdata;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000531 if (node == NULL || PCI_DN(node)->pcidev != pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 node = find_Device_Node(pdev->bus->number, pdev->devfn);
533 return node;
534}
535#endif
536
537/*
538 * Config space read and write functions.
539 * For now at least, we look for the device node for the bus and devfn
540 * that we are asked to access. It may be possible to translate the devfn
541 * to a subbus and deviceid more directly.
542 */
543static u64 hv_cfg_read_func[4] = {
544 HvCallPciConfigLoad8, HvCallPciConfigLoad16,
545 HvCallPciConfigLoad32, HvCallPciConfigLoad32
546};
547
548static u64 hv_cfg_write_func[4] = {
549 HvCallPciConfigStore8, HvCallPciConfigStore16,
550 HvCallPciConfigStore32, HvCallPciConfigStore32
551};
552
553/*
554 * Read PCI config space
555 */
556static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
557 int offset, int size, u32 *val)
558{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000559 struct device_node *node = find_Device_Node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 u64 fn;
561 struct HvCallPci_LoadReturn ret;
562
563 if (node == NULL)
564 return PCIBIOS_DEVICE_NOT_FOUND;
565 if (offset > 255) {
566 *val = ~0;
567 return PCIBIOS_BAD_REGISTER_NUMBER;
568 }
569
570 fn = hv_cfg_read_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000571 HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (ret.rc != 0) {
574 *val = ~0;
575 return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
576 }
577
578 *val = ret.value;
579 return 0;
580}
581
582/*
583 * Write PCI config space
584 */
585
586static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
587 int offset, int size, u32 val)
588{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000589 struct device_node *node = find_Device_Node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 u64 fn;
591 u64 ret;
592
593 if (node == NULL)
594 return PCIBIOS_DEVICE_NOT_FOUND;
595 if (offset > 255)
596 return PCIBIOS_BAD_REGISTER_NUMBER;
597
598 fn = hv_cfg_write_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000599 ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 if (ret != 0)
602 return PCIBIOS_DEVICE_NOT_FOUND;
603
604 return 0;
605}
606
607static struct pci_ops iSeries_pci_ops = {
608 .read = iSeries_pci_read_config,
609 .write = iSeries_pci_write_config
610};
611
612/*
613 * Check Return Code
614 * -> On Failure, print and log information.
615 * Increment Retry Count, if exceeds max, panic partition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *
617 * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
618 * PCI: Device 23.90 ReadL Retry( 1)
619 * PCI: Device 23.90 ReadL Retry Successful(1)
620 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000621static int CheckReturnCode(char *TextHdr, struct device_node *DevNode,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700622 int *retry, u64 ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 if (ret != 0) {
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000625 struct pci_dn *pdn = PCI_DN(DevNode);
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 ++Pci_Error_Count;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700628 (*retry)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000630 TextHdr, pdn->busno, pdn->devfn,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700631 *retry, (int)ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * Bump the retry and check for retry count exceeded.
634 * If, Exceeded, panic the system.
635 */
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700636 if (((*retry) > Pci_Retry_Max) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 (Pci_Error_Flag > 0)) {
638 mf_display_src(0xB6000103);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700639 panic_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 panic("PCI: Hardware I/O Error, SRC B6000103, "
641 "Automatic Reboot Disabled.\n");
642 }
643 return -1; /* Retry Try */
644 }
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648/*
649 * Translate the I/O Address into a device node, bar, and bar offset.
650 * Note: Make sure the passed variable end up on the stack to avoid
651 * the exposure of being device global.
652 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000653static inline struct device_node *xlate_iomm_address(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 const volatile void __iomem *IoAddress,
655 u64 *dsaptr, u64 *BarOffsetPtr)
656{
657 unsigned long OrigIoAddr;
658 unsigned long BaseIoAddr;
659 unsigned long TableIndex;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000660 struct device_node *DevNode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 OrigIoAddr = (unsigned long __force)IoAddress;
663 if ((OrigIoAddr < BASE_IO_MEMORY) || (OrigIoAddr >= max_io_memory))
664 return NULL;
665 BaseIoAddr = OrigIoAddr - BASE_IO_MEMORY;
666 TableIndex = BaseIoAddr / IOMM_TABLE_ENTRY_SIZE;
667 DevNode = iomm_table[TableIndex];
668
669 if (DevNode != NULL) {
670 int barnum = iobar_table[TableIndex];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000671 *dsaptr = iseries_ds_addr(DevNode) | (barnum << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 *BarOffsetPtr = BaseIoAddr % IOMM_TABLE_ENTRY_SIZE;
673 } else
674 panic("PCI: Invalid PCI IoAddress detected!\n");
675 return DevNode;
676}
677
678/*
679 * Read MM I/O Instructions for the iSeries
680 * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
681 * else, data is returned in big Endian format.
682 *
683 * iSeries_Read_Byte = Read Byte ( 8 bit)
684 * iSeries_Read_Word = Read Word (16 bit)
685 * iSeries_Read_Long = Read Long (32 bit)
686 */
687u8 iSeries_Read_Byte(const volatile void __iomem *IoAddress)
688{
689 u64 BarOffset;
690 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700691 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000693 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
695
696 if (DevNode == NULL) {
697 static unsigned long last_jiffies;
698 static int num_printed;
699
700 if ((jiffies - last_jiffies) > 60 * HZ) {
701 last_jiffies = jiffies;
702 num_printed = 0;
703 }
704 if (num_printed++ < 10)
705 printk(KERN_ERR "iSeries_Read_Byte: invalid access at IO address %p\n", IoAddress);
706 return 0xff;
707 }
708 do {
709 ++Pci_Io_Read_Count;
710 HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700711 } while (CheckReturnCode("RDB", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 return (u8)ret.value;
714}
715EXPORT_SYMBOL(iSeries_Read_Byte);
716
717u16 iSeries_Read_Word(const volatile void __iomem *IoAddress)
718{
719 u64 BarOffset;
720 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700721 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000723 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
725
726 if (DevNode == NULL) {
727 static unsigned long last_jiffies;
728 static int num_printed;
729
730 if ((jiffies - last_jiffies) > 60 * HZ) {
731 last_jiffies = jiffies;
732 num_printed = 0;
733 }
734 if (num_printed++ < 10)
735 printk(KERN_ERR "iSeries_Read_Word: invalid access at IO address %p\n", IoAddress);
736 return 0xffff;
737 }
738 do {
739 ++Pci_Io_Read_Count;
740 HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
741 BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700742 } while (CheckReturnCode("RDW", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 return swab16((u16)ret.value);
745}
746EXPORT_SYMBOL(iSeries_Read_Word);
747
748u32 iSeries_Read_Long(const volatile void __iomem *IoAddress)
749{
750 u64 BarOffset;
751 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700752 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000754 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
756
757 if (DevNode == NULL) {
758 static unsigned long last_jiffies;
759 static int num_printed;
760
761 if ((jiffies - last_jiffies) > 60 * HZ) {
762 last_jiffies = jiffies;
763 num_printed = 0;
764 }
765 if (num_printed++ < 10)
766 printk(KERN_ERR "iSeries_Read_Long: invalid access at IO address %p\n", IoAddress);
767 return 0xffffffff;
768 }
769 do {
770 ++Pci_Io_Read_Count;
771 HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
772 BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700773 } while (CheckReturnCode("RDL", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 return swab32((u32)ret.value);
776}
777EXPORT_SYMBOL(iSeries_Read_Long);
778
779/*
780 * Write MM I/O Instructions for the iSeries
781 *
782 * iSeries_Write_Byte = Write Byte (8 bit)
783 * iSeries_Write_Word = Write Word(16 bit)
784 * iSeries_Write_Long = Write Long(32 bit)
785 */
786void iSeries_Write_Byte(u8 data, volatile void __iomem *IoAddress)
787{
788 u64 BarOffset;
789 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700790 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000792 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
794
795 if (DevNode == NULL) {
796 static unsigned long last_jiffies;
797 static int num_printed;
798
799 if ((jiffies - last_jiffies) > 60 * HZ) {
800 last_jiffies = jiffies;
801 num_printed = 0;
802 }
803 if (num_printed++ < 10)
804 printk(KERN_ERR "iSeries_Write_Byte: invalid access at IO address %p\n", IoAddress);
805 return;
806 }
807 do {
808 ++Pci_Io_Write_Count;
809 rc = HvCall4(HvCallPciBarStore8, dsa, BarOffset, data, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700810 } while (CheckReturnCode("WWB", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812EXPORT_SYMBOL(iSeries_Write_Byte);
813
814void iSeries_Write_Word(u16 data, volatile void __iomem *IoAddress)
815{
816 u64 BarOffset;
817 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700818 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000820 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
822
823 if (DevNode == NULL) {
824 static unsigned long last_jiffies;
825 static int num_printed;
826
827 if ((jiffies - last_jiffies) > 60 * HZ) {
828 last_jiffies = jiffies;
829 num_printed = 0;
830 }
831 if (num_printed++ < 10)
832 printk(KERN_ERR "iSeries_Write_Word: invalid access at IO address %p\n", IoAddress);
833 return;
834 }
835 do {
836 ++Pci_Io_Write_Count;
837 rc = HvCall4(HvCallPciBarStore16, dsa, BarOffset, swab16(data), 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700838 } while (CheckReturnCode("WWW", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840EXPORT_SYMBOL(iSeries_Write_Word);
841
842void iSeries_Write_Long(u32 data, volatile void __iomem *IoAddress)
843{
844 u64 BarOffset;
845 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700846 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000848 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
850
851 if (DevNode == NULL) {
852 static unsigned long last_jiffies;
853 static int num_printed;
854
855 if ((jiffies - last_jiffies) > 60 * HZ) {
856 last_jiffies = jiffies;
857 num_printed = 0;
858 }
859 if (num_printed++ < 10)
860 printk(KERN_ERR "iSeries_Write_Long: invalid access at IO address %p\n", IoAddress);
861 return;
862 }
863 do {
864 ++Pci_Io_Write_Count;
865 rc = HvCall4(HvCallPciBarStore32, dsa, BarOffset, swab32(data), 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700866 } while (CheckReturnCode("WWL", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868EXPORT_SYMBOL(iSeries_Write_Long);