blob: d14e9d9d77972e9991c7eaad11a12140472e2c51 [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>
35#include <asm/ppcdebug.h>
36#include <asm/iommu.h>
Stephen Rothwell426c1a12005-10-14 14:51:42 +100037#include <asm/abs_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/iSeries/HvCallPci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/iSeries/HvCallXm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/iSeries/mf.h>
42
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 Rothwellb08567cb2005-09-28 23:37:01 +100047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048extern unsigned long io_page_mask;
49
50/*
Stephen Rothwelld3878992005-09-28 02:50:25 +100051 * Forward declares of prototypes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +100053static struct device_node *find_Device_Node(int bus, int devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static void scan_PHB_slots(struct pci_controller *Phb);
55static void scan_EADS_bridge(HvBusNumber Bus, HvSubBusNumber SubBus, int IdSel);
56static int scan_bridge_slot(HvBusNumber Bus, struct HvCallPci_BridgeInfo *Info);
57
58LIST_HEAD(iSeries_Global_Device_List);
59
60static int DeviceCount;
61
62/* Counters and control flags. */
63static long Pci_Io_Read_Count;
64static long Pci_Io_Write_Count;
65#if 0
66static long Pci_Cfg_Read_Count;
67static long Pci_Cfg_Write_Count;
68#endif
69static long Pci_Error_Count;
70
Stephen Rothwelld3878992005-09-28 02:50:25 +100071static int Pci_Retry_Max = 3; /* Only retry 3 times */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int Pci_Error_Flag = 1; /* Set Retry Error on. */
73
74static struct pci_ops iSeries_pci_ops;
75
76/*
77 * Table defines
78 * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
79 */
80#define IOMM_TABLE_MAX_ENTRIES 1024
81#define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
82#define BASE_IO_MEMORY 0xE000000000000000UL
83
84static unsigned long max_io_memory = 0xE000000000000000UL;
85static long current_iomm_table_entry;
86
87/*
88 * Lookup Tables.
89 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +100090static struct device_node **iomm_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static u8 *iobar_table;
92
93/*
94 * Static and Global variables
95 */
96static char *pci_io_text = "iSeries PCI I/O";
97static DEFINE_SPINLOCK(iomm_table_lock);
98
99/*
100 * iomm_table_initialize
101 *
102 * Allocates and initalizes the Address Translation Table and Bar
103 * Tables to get them ready for use. Must be called before any
104 * I/O space is handed out to the device BARs.
105 */
106static void iomm_table_initialize(void)
107{
108 spin_lock(&iomm_table_lock);
109 iomm_table = kmalloc(sizeof(*iomm_table) * IOMM_TABLE_MAX_ENTRIES,
110 GFP_KERNEL);
111 iobar_table = kmalloc(sizeof(*iobar_table) * IOMM_TABLE_MAX_ENTRIES,
112 GFP_KERNEL);
113 spin_unlock(&iomm_table_lock);
114 if ((iomm_table == NULL) || (iobar_table == NULL))
115 panic("PCI: I/O tables allocation failed.\n");
116}
117
118/*
119 * iomm_table_allocate_entry
120 *
121 * Adds pci_dev entry in address translation table
122 *
123 * - Allocates the number of entries required in table base on BAR
124 * size.
125 * - Allocates starting at BASE_IO_MEMORY and increases.
126 * - The size is round up to be a multiple of entry size.
127 * - CurrentIndex is incremented to keep track of the last entry.
128 * - Builds the resource entry for allocated BARs.
129 */
130static void iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
131{
132 struct resource *bar_res = &dev->resource[bar_num];
133 long bar_size = pci_resource_len(dev, bar_num);
134
135 /*
136 * No space to allocate, quick exit, skip Allocation.
137 */
138 if (bar_size == 0)
139 return;
140 /*
141 * Set Resource values.
142 */
143 spin_lock(&iomm_table_lock);
144 bar_res->name = pci_io_text;
145 bar_res->start =
146 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
147 bar_res->start += BASE_IO_MEMORY;
148 bar_res->end = bar_res->start + bar_size - 1;
149 /*
150 * Allocate the number of table entries needed for BAR.
151 */
152 while (bar_size > 0 ) {
153 iomm_table[current_iomm_table_entry] = dev->sysdata;
154 iobar_table[current_iomm_table_entry] = bar_num;
155 bar_size -= IOMM_TABLE_ENTRY_SIZE;
156 ++current_iomm_table_entry;
157 }
158 max_io_memory = BASE_IO_MEMORY +
159 (IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry);
160 spin_unlock(&iomm_table_lock);
161}
162
163/*
164 * allocate_device_bars
165 *
166 * - Allocates ALL pci_dev BAR's and updates the resources with the
167 * BAR value. BARS with zero length will have the resources
168 * The HvCallPci_getBarParms is used to get the size of the BAR
169 * space. It calls iomm_table_allocate_entry to allocate
170 * each entry.
171 * - Loops through The Bar resources(0 - 5) including the ROM
172 * is resource(6).
173 */
174static void allocate_device_bars(struct pci_dev *dev)
175{
176 struct resource *bar_res;
177 int bar_num;
178
179 for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num) {
180 bar_res = &dev->resource[bar_num];
181 iomm_table_allocate_entry(dev, bar_num);
Stephen Rothwelld3878992005-09-28 02:50:25 +1000182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185/*
186 * Log error information to system console.
187 * Filter out the device not there errors.
188 * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
189 * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
190 * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
191 */
192static void pci_Log_Error(char *Error_Text, int Bus, int SubBus,
193 int AgentId, int HvRc)
194{
195 if (HvRc == 0x0302)
196 return;
197 printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
198 Error_Text, Bus, SubBus, AgentId, HvRc);
199}
200
201/*
202 * build_device_node(u16 Bus, int SubBus, u8 DevFn)
203 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000204static struct device_node *build_device_node(HvBusNumber Bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 HvSubBusNumber SubBus, int AgentId, int Function)
206{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000207 struct device_node *node;
208 struct pci_dn *pdn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 PPCDBG(PPCDBG_BUSWALK,
211 "-build_device_node 0x%02X.%02X.%02X Function: %02X\n",
212 Bus, SubBus, AgentId, Function);
213
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000214 node = kmalloc(sizeof(struct device_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (node == NULL)
216 return NULL;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000217 memset(node, 0, sizeof(struct device_node));
218 pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
219 if (pdn == NULL) {
220 kfree(node);
221 return NULL;
222 }
223 node->data = pdn;
Stephen Rothwellf255f0d2005-10-10 17:10:54 +1000224 pdn->node = node;
Paul Mackerras76f9f872005-10-10 22:52:26 +1000225 list_add_tail(&pdn->Device_List, &iSeries_Global_Device_List);
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000226 pdn->busno = Bus;
227 pdn->bussubno = SubBus;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000228 pdn->devfn = PCI_DEVFN(ISERIES_ENCODE_DEVICE(AgentId), Function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return node;
230}
231
232/*
233 * unsigned long __init find_and_init_phbs(void)
234 *
235 * Description:
236 * This function checks for all possible system PCI host bridges that connect
237 * PCI buses. The system hypervisor is queried as to the guest partition
238 * ownership status. A pci_controller is built for any bus which is partially
239 * owned or fully owned by this guest partition.
240 */
241unsigned long __init find_and_init_phbs(void)
242{
243 struct pci_controller *phb;
244 HvBusNumber bus;
245
246 PPCDBG(PPCDBG_BUSWALK, "find_and_init_phbs Entry\n");
247
248 /* Check all possible buses. */
249 for (bus = 0; bus < 256; bus++) {
250 int ret = HvCallXm_testBus(bus);
251 if (ret == 0) {
252 printk("bus %d appears to exist\n", bus);
253
254 phb = (struct pci_controller *)kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
255 if (phb == NULL)
256 return -ENOMEM;
Anton Blanchard3238e9c2005-09-12 13:14:26 +1000257 pci_setup_pci_controller(phb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 phb->pci_mem_offset = phb->local_number = bus;
260 phb->first_busno = bus;
261 phb->last_busno = bus;
262 phb->ops = &iSeries_pci_ops;
263
264 PPCDBG(PPCDBG_BUSWALK, "PCI:Create iSeries pci_controller(%p), Bus: %04X\n",
265 phb, bus);
266
267 /* Find and connect the devices. */
268 scan_PHB_slots(phb);
269 }
270 /*
271 * Check for Unexpected Return code, a clue that something
272 * has gone wrong.
273 */
274 else if (ret != 0x0301)
275 printk(KERN_ERR "Unexpected Return on Probe(0x%04X): 0x%04X",
276 bus, ret);
277 }
278 return 0;
279}
280
281/*
282 * iSeries_pcibios_init
Stephen Rothwelld3878992005-09-28 02:50:25 +1000283 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * Chance to initialize and structures or variable before PCI Bus walk.
285 */
286void iSeries_pcibios_init(void)
287{
Stephen Rothwelld3878992005-09-28 02:50:25 +1000288 PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_init Entry.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 iomm_table_initialize();
290 find_and_init_phbs();
291 io_page_mask = -1;
Stephen Rothwelld3878992005-09-28 02:50:25 +1000292 PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_init Exit.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295/*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000296 * iSeries_pci_final_fixup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
298void __init iSeries_pci_final_fixup(void)
299{
300 struct pci_dev *pdev = NULL;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000301 struct device_node *node;
Stephen Rothwelld3878992005-09-28 02:50:25 +1000302 int DeviceCount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Stephen Rothwelld3878992005-09-28 02:50:25 +1000304 PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_fixup Entry.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* Fix up at the device node and pci_dev relationship */
307 mf_display_src(0xC9000100);
308
309 printk("pcibios_final_fixup\n");
310 for_each_pci_dev(pdev) {
311 node = find_Device_Node(pdev->bus->number, pdev->devfn);
312 printk("pci dev %p (%x.%x), node %p\n", pdev,
313 pdev->bus->number, pdev->devfn, node);
314
315 if (node != NULL) {
316 ++DeviceCount;
317 pdev->sysdata = (void *)node;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000318 PCI_DN(node)->pcidev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 PPCDBG(PPCDBG_BUSWALK,
320 "pdev 0x%p <==> DevNode 0x%p\n",
321 pdev, node);
322 allocate_device_bars(pdev);
Stephen Rothwell061c0632005-06-21 17:15:48 -0700323 iSeries_Device_Information(pdev, DeviceCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 iommu_devnode_init_iSeries(node);
325 } else
326 printk("PCI: Device Tree not found for 0x%016lX\n",
327 (unsigned long)pdev);
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000328 pdev->irq = PCI_DN(node)->Irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330 iSeries_activate_IRQs();
331 mf_display_src(0xC9000200);
332}
333
334void pcibios_fixup_bus(struct pci_bus *PciBus)
335{
336 PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_fixup_bus(0x%04X) Entry.\n",
Stephen Rothwelld3878992005-09-28 02:50:25 +1000337 PciBus->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340void pcibios_fixup_resources(struct pci_dev *pdev)
341{
342 PPCDBG(PPCDBG_BUSWALK, "fixup_resources pdev %p\n", pdev);
Stephen Rothwelld3878992005-09-28 02:50:25 +1000343}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345/*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000346 * Loop through each node function to find usable EADs bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
348static void scan_PHB_slots(struct pci_controller *Phb)
349{
350 struct HvCallPci_DeviceInfo *DevInfo;
Stephen Rothwelld3878992005-09-28 02:50:25 +1000351 HvBusNumber bus = Phb->local_number; /* System Bus */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 const HvSubBusNumber SubBus = 0; /* EADs is always 0. */
353 int HvRc = 0;
Stephen Rothwelld3878992005-09-28 02:50:25 +1000354 int IdSel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 const int MaxAgents = 8;
356
357 DevInfo = (struct HvCallPci_DeviceInfo*)
358 kmalloc(sizeof(struct HvCallPci_DeviceInfo), GFP_KERNEL);
359 if (DevInfo == NULL)
360 return;
361
362 /*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000363 * Probe for EADs Bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365 for (IdSel = 1; IdSel < MaxAgents; ++IdSel) {
Stephen Rothwelld3878992005-09-28 02:50:25 +1000366 HvRc = HvCallPci_getDeviceInfo(bus, SubBus, IdSel,
Stephen Rothwell426c1a12005-10-14 14:51:42 +1000367 iseries_hv_addr(DevInfo),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 sizeof(struct HvCallPci_DeviceInfo));
369 if (HvRc == 0) {
370 if (DevInfo->deviceType == HvCallPci_NodeDevice)
371 scan_EADS_bridge(bus, SubBus, IdSel);
372 else
373 printk("PCI: Invalid System Configuration(0x%02X)"
374 " for bus 0x%02x id 0x%02x.\n",
375 DevInfo->deviceType, bus, IdSel);
376 }
377 else
378 pci_Log_Error("getDeviceInfo", bus, SubBus, IdSel, HvRc);
379 }
380 kfree(DevInfo);
381}
382
383static void scan_EADS_bridge(HvBusNumber bus, HvSubBusNumber SubBus,
384 int IdSel)
385{
386 struct HvCallPci_BridgeInfo *BridgeInfo;
387 HvAgentId AgentId;
388 int Function;
389 int HvRc;
390
391 BridgeInfo = (struct HvCallPci_BridgeInfo *)
392 kmalloc(sizeof(struct HvCallPci_BridgeInfo), GFP_KERNEL);
393 if (BridgeInfo == NULL)
394 return;
395
396 /* Note: hvSubBus and irq is always be 0 at this level! */
397 for (Function = 0; Function < 8; ++Function) {
Stephen Rothwelld3878992005-09-28 02:50:25 +1000398 AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 HvRc = HvCallXm_connectBusUnit(bus, SubBus, AgentId, 0);
Stephen Rothwelld3878992005-09-28 02:50:25 +1000400 if (HvRc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 printk("found device at bus %d idsel %d func %d (AgentId %x)\n",
402 bus, IdSel, Function, AgentId);
Stephen Rothwelld3878992005-09-28 02:50:25 +1000403 /* Connect EADs: 0x18.00.12 = 0x00 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 PPCDBG(PPCDBG_BUSWALK,
405 "PCI:Connect EADs: 0x%02X.%02X.%02X\n",
406 bus, SubBus, AgentId);
Stephen Rothwelld3878992005-09-28 02:50:25 +1000407 HvRc = HvCallPci_getBusUnitInfo(bus, SubBus, AgentId,
Stephen Rothwell426c1a12005-10-14 14:51:42 +1000408 iseries_hv_addr(BridgeInfo),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 sizeof(struct HvCallPci_BridgeInfo));
Stephen Rothwelld3878992005-09-28 02:50:25 +1000410 if (HvRc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 printk("bridge info: type %x subbus %x maxAgents %x maxsubbus %x logslot %x\n",
412 BridgeInfo->busUnitInfo.deviceType,
413 BridgeInfo->subBusNumber,
414 BridgeInfo->maxAgents,
415 BridgeInfo->maxSubBusNumber,
416 BridgeInfo->logicalSlotNumber);
417 PPCDBG(PPCDBG_BUSWALK,
418 "PCI: BridgeInfo, Type:0x%02X, SubBus:0x%02X, MaxAgents:0x%02X, MaxSubBus: 0x%02X, LSlot: 0x%02X\n",
419 BridgeInfo->busUnitInfo.deviceType,
420 BridgeInfo->subBusNumber,
421 BridgeInfo->maxAgents,
422 BridgeInfo->maxSubBusNumber,
423 BridgeInfo->logicalSlotNumber);
424
425 if (BridgeInfo->busUnitInfo.deviceType ==
426 HvCallPci_BridgeDevice) {
427 /* Scan_Bridge_Slot...: 0x18.00.12 */
428 scan_bridge_slot(bus, BridgeInfo);
429 } else
430 printk("PCI: Invalid Bridge Configuration(0x%02X)",
431 BridgeInfo->busUnitInfo.deviceType);
432 }
Stephen Rothwelld3878992005-09-28 02:50:25 +1000433 } else if (HvRc != 0x000B)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 pci_Log_Error("EADs Connect",
435 bus, SubBus, AgentId, HvRc);
436 }
437 kfree(BridgeInfo);
438}
439
440/*
441 * This assumes that the node slot is always on the primary bus!
442 */
443static int scan_bridge_slot(HvBusNumber Bus,
444 struct HvCallPci_BridgeInfo *BridgeInfo)
445{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000446 struct device_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 HvSubBusNumber SubBus = BridgeInfo->subBusNumber;
448 u16 VendorId = 0;
449 int HvRc = 0;
450 u8 Irq = 0;
451 int IdSel = ISERIES_GET_DEVICE_FROM_SUBBUS(SubBus);
452 int Function = ISERIES_GET_FUNCTION_FROM_SUBBUS(SubBus);
453 HvAgentId EADsIdSel = ISERIES_PCI_AGENTID(IdSel, Function);
454
455 /* iSeries_allocate_IRQ.: 0x18.00.12(0xA3) */
Stephen Rothwelld3878992005-09-28 02:50:25 +1000456 Irq = iSeries_allocate_IRQ(Bus, 0, EADsIdSel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 PPCDBG(PPCDBG_BUSWALK,
458 "PCI:- allocate and assign IRQ 0x%02X.%02X.%02X = 0x%02X\n",
459 Bus, 0, EADsIdSel, Irq);
460
461 /*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000462 * Connect all functions of any device found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 */
Stephen Rothwelld3878992005-09-28 02:50:25 +1000464 for (IdSel = 1; IdSel <= BridgeInfo->maxAgents; ++IdSel) {
465 for (Function = 0; Function < 8; ++Function) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 HvAgentId AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
467 HvRc = HvCallXm_connectBusUnit(Bus, SubBus,
468 AgentId, Irq);
469 if (HvRc != 0) {
470 pci_Log_Error("Connect Bus Unit",
471 Bus, SubBus, AgentId, HvRc);
472 continue;
473 }
474
475 HvRc = HvCallPci_configLoad16(Bus, SubBus, AgentId,
476 PCI_VENDOR_ID, &VendorId);
477 if (HvRc != 0) {
478 pci_Log_Error("Read Vendor",
479 Bus, SubBus, AgentId, HvRc);
480 continue;
481 }
482 printk("read vendor ID: %x\n", VendorId);
483
484 /* FoundDevice: 0x18.28.10 = 0x12AE */
485 PPCDBG(PPCDBG_BUSWALK,
486 "PCI:- FoundDevice: 0x%02X.%02X.%02X = 0x%04X, irq %d\n",
487 Bus, SubBus, AgentId, VendorId, Irq);
488 HvRc = HvCallPci_configStore8(Bus, SubBus, AgentId,
Stephen Rothwelld3878992005-09-28 02:50:25 +1000489 PCI_INTERRUPT_LINE, Irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (HvRc != 0)
491 pci_Log_Error("PciCfgStore Irq Failed!",
492 Bus, SubBus, AgentId, HvRc);
493
494 ++DeviceCount;
495 node = build_device_node(Bus, SubBus, EADsIdSel, Function);
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000496 PCI_DN(node)->Irq = Irq;
497 PCI_DN(node)->LogicalSlot = BridgeInfo->logicalSlotNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 } /* for (Function = 0; Function < 8; ++Function) */
500 } /* for (IdSel = 1; IdSel <= MaxAgents; ++IdSel) */
501 return HvRc;
502}
503
504/*
505 * I/0 Memory copy MUST use mmio commands on iSeries
506 * To do; For performance, include the hv call directly
507 */
508void iSeries_memset_io(volatile void __iomem *dest, char c, size_t Count)
509{
510 u8 ByteValue = c;
511 long NumberOfBytes = Count;
512
513 while (NumberOfBytes > 0) {
514 iSeries_Write_Byte(ByteValue, dest++);
515 -- NumberOfBytes;
516 }
517}
518EXPORT_SYMBOL(iSeries_memset_io);
519
520void iSeries_memcpy_toio(volatile void __iomem *dest, void *source, size_t count)
521{
522 char *src = source;
523 long NumberOfBytes = count;
524
525 while (NumberOfBytes > 0) {
526 iSeries_Write_Byte(*src++, dest++);
527 -- NumberOfBytes;
528 }
529}
530EXPORT_SYMBOL(iSeries_memcpy_toio);
531
532void iSeries_memcpy_fromio(void *dest, const volatile void __iomem *src, size_t count)
533{
534 char *dst = dest;
535 long NumberOfBytes = count;
536
537 while (NumberOfBytes > 0) {
538 *dst++ = iSeries_Read_Byte(src++);
539 -- NumberOfBytes;
540 }
541}
542EXPORT_SYMBOL(iSeries_memcpy_fromio);
543
544/*
545 * Look down the chain to find the matching Device Device
546 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000547static struct device_node *find_Device_Node(int bus, int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Paul Mackerras76f9f872005-10-10 22:52:26 +1000549 struct pci_dn *pdn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Paul Mackerras76f9f872005-10-10 22:52:26 +1000551 list_for_each_entry(pdn, &iSeries_Global_Device_List, Device_List) {
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000552 if ((bus == pdn->busno) && (devfn == pdn->devfn))
Paul Mackerras76f9f872005-10-10 22:52:26 +1000553 return pdn->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555 return NULL;
556}
557
558#if 0
559/*
560 * Returns the device node for the passed pci_dev
561 * Sanity Check Node PciDev to passed pci_dev
562 * If none is found, returns a NULL which the client must handle.
563 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000564static struct device_node *get_Device_Node(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000566 struct device_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 node = pdev->sysdata;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000569 if (node == NULL || PCI_DN(node)->pcidev != pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 node = find_Device_Node(pdev->bus->number, pdev->devfn);
571 return node;
572}
573#endif
574
575/*
576 * Config space read and write functions.
577 * For now at least, we look for the device node for the bus and devfn
578 * that we are asked to access. It may be possible to translate the devfn
579 * to a subbus and deviceid more directly.
580 */
581static u64 hv_cfg_read_func[4] = {
582 HvCallPciConfigLoad8, HvCallPciConfigLoad16,
583 HvCallPciConfigLoad32, HvCallPciConfigLoad32
584};
585
586static u64 hv_cfg_write_func[4] = {
587 HvCallPciConfigStore8, HvCallPciConfigStore16,
588 HvCallPciConfigStore32, HvCallPciConfigStore32
589};
590
591/*
592 * Read PCI config space
593 */
594static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
595 int offset, int size, u32 *val)
596{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000597 struct device_node *node = find_Device_Node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 u64 fn;
599 struct HvCallPci_LoadReturn ret;
600
601 if (node == NULL)
602 return PCIBIOS_DEVICE_NOT_FOUND;
603 if (offset > 255) {
604 *val = ~0;
605 return PCIBIOS_BAD_REGISTER_NUMBER;
606 }
607
608 fn = hv_cfg_read_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000609 HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 if (ret.rc != 0) {
612 *val = ~0;
613 return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
614 }
615
616 *val = ret.value;
617 return 0;
618}
619
620/*
621 * Write PCI config space
622 */
623
624static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
625 int offset, int size, u32 val)
626{
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000627 struct device_node *node = find_Device_Node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 u64 fn;
629 u64 ret;
630
631 if (node == NULL)
632 return PCIBIOS_DEVICE_NOT_FOUND;
633 if (offset > 255)
634 return PCIBIOS_BAD_REGISTER_NUMBER;
635
636 fn = hv_cfg_write_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000637 ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (ret != 0)
640 return PCIBIOS_DEVICE_NOT_FOUND;
641
642 return 0;
643}
644
645static struct pci_ops iSeries_pci_ops = {
646 .read = iSeries_pci_read_config,
647 .write = iSeries_pci_write_config
648};
649
650/*
651 * Check Return Code
652 * -> On Failure, print and log information.
653 * Increment Retry Count, if exceeds max, panic partition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 *
655 * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
656 * PCI: Device 23.90 ReadL Retry( 1)
657 * PCI: Device 23.90 ReadL Retry Successful(1)
658 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000659static int CheckReturnCode(char *TextHdr, struct device_node *DevNode,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700660 int *retry, u64 ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 if (ret != 0) {
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000663 struct pci_dn *pdn = PCI_DN(DevNode);
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 ++Pci_Error_Count;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700666 (*retry)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000668 TextHdr, pdn->busno, pdn->devfn,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700669 *retry, (int)ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /*
671 * Bump the retry and check for retry count exceeded.
672 * If, Exceeded, panic the system.
673 */
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700674 if (((*retry) > Pci_Retry_Max) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 (Pci_Error_Flag > 0)) {
676 mf_display_src(0xB6000103);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700677 panic_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 panic("PCI: Hardware I/O Error, SRC B6000103, "
679 "Automatic Reboot Disabled.\n");
680 }
681 return -1; /* Retry Try */
682 }
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
686/*
687 * Translate the I/O Address into a device node, bar, and bar offset.
688 * Note: Make sure the passed variable end up on the stack to avoid
689 * the exposure of being device global.
690 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000691static inline struct device_node *xlate_iomm_address(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 const volatile void __iomem *IoAddress,
693 u64 *dsaptr, u64 *BarOffsetPtr)
694{
695 unsigned long OrigIoAddr;
696 unsigned long BaseIoAddr;
697 unsigned long TableIndex;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000698 struct device_node *DevNode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 OrigIoAddr = (unsigned long __force)IoAddress;
701 if ((OrigIoAddr < BASE_IO_MEMORY) || (OrigIoAddr >= max_io_memory))
702 return NULL;
703 BaseIoAddr = OrigIoAddr - BASE_IO_MEMORY;
704 TableIndex = BaseIoAddr / IOMM_TABLE_ENTRY_SIZE;
705 DevNode = iomm_table[TableIndex];
706
707 if (DevNode != NULL) {
708 int barnum = iobar_table[TableIndex];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000709 *dsaptr = iseries_ds_addr(DevNode) | (barnum << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 *BarOffsetPtr = BaseIoAddr % IOMM_TABLE_ENTRY_SIZE;
711 } else
712 panic("PCI: Invalid PCI IoAddress detected!\n");
713 return DevNode;
714}
715
716/*
717 * Read MM I/O Instructions for the iSeries
718 * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
719 * else, data is returned in big Endian format.
720 *
721 * iSeries_Read_Byte = Read Byte ( 8 bit)
722 * iSeries_Read_Word = Read Word (16 bit)
723 * iSeries_Read_Long = Read Long (32 bit)
724 */
725u8 iSeries_Read_Byte(const volatile void __iomem *IoAddress)
726{
727 u64 BarOffset;
728 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700729 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000731 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
733
734 if (DevNode == NULL) {
735 static unsigned long last_jiffies;
736 static int num_printed;
737
738 if ((jiffies - last_jiffies) > 60 * HZ) {
739 last_jiffies = jiffies;
740 num_printed = 0;
741 }
742 if (num_printed++ < 10)
743 printk(KERN_ERR "iSeries_Read_Byte: invalid access at IO address %p\n", IoAddress);
744 return 0xff;
745 }
746 do {
747 ++Pci_Io_Read_Count;
748 HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700749 } while (CheckReturnCode("RDB", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 return (u8)ret.value;
752}
753EXPORT_SYMBOL(iSeries_Read_Byte);
754
755u16 iSeries_Read_Word(const volatile void __iomem *IoAddress)
756{
757 u64 BarOffset;
758 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700759 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct HvCallPci_LoadReturn ret;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000761 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
763
764 if (DevNode == NULL) {
765 static unsigned long last_jiffies;
766 static int num_printed;
767
768 if ((jiffies - last_jiffies) > 60 * HZ) {
769 last_jiffies = jiffies;
770 num_printed = 0;
771 }
772 if (num_printed++ < 10)
773 printk(KERN_ERR "iSeries_Read_Word: invalid access at IO address %p\n", IoAddress);
774 return 0xffff;
775 }
776 do {
777 ++Pci_Io_Read_Count;
778 HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
779 BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700780 } while (CheckReturnCode("RDW", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 return swab16((u16)ret.value);
783}
784EXPORT_SYMBOL(iSeries_Read_Word);
785
786u32 iSeries_Read_Long(const 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 struct HvCallPci_LoadReturn ret;
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_Read_Long: invalid access at IO address %p\n", IoAddress);
805 return 0xffffffff;
806 }
807 do {
808 ++Pci_Io_Read_Count;
809 HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
810 BarOffset, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700811 } while (CheckReturnCode("RDL", DevNode, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 return swab32((u32)ret.value);
814}
815EXPORT_SYMBOL(iSeries_Read_Long);
816
817/*
818 * Write MM I/O Instructions for the iSeries
819 *
820 * iSeries_Write_Byte = Write Byte (8 bit)
821 * iSeries_Write_Word = Write Word(16 bit)
822 * iSeries_Write_Long = Write Long(32 bit)
823 */
824void iSeries_Write_Byte(u8 data, volatile void __iomem *IoAddress)
825{
826 u64 BarOffset;
827 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700828 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000830 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
832
833 if (DevNode == NULL) {
834 static unsigned long last_jiffies;
835 static int num_printed;
836
837 if ((jiffies - last_jiffies) > 60 * HZ) {
838 last_jiffies = jiffies;
839 num_printed = 0;
840 }
841 if (num_printed++ < 10)
842 printk(KERN_ERR "iSeries_Write_Byte: invalid access at IO address %p\n", IoAddress);
843 return;
844 }
845 do {
846 ++Pci_Io_Write_Count;
847 rc = HvCall4(HvCallPciBarStore8, dsa, BarOffset, data, 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700848 } while (CheckReturnCode("WWB", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850EXPORT_SYMBOL(iSeries_Write_Byte);
851
852void iSeries_Write_Word(u16 data, volatile void __iomem *IoAddress)
853{
854 u64 BarOffset;
855 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700856 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000858 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
860
861 if (DevNode == NULL) {
862 static unsigned long last_jiffies;
863 static int num_printed;
864
865 if ((jiffies - last_jiffies) > 60 * HZ) {
866 last_jiffies = jiffies;
867 num_printed = 0;
868 }
869 if (num_printed++ < 10)
870 printk(KERN_ERR "iSeries_Write_Word: invalid access at IO address %p\n", IoAddress);
871 return;
872 }
873 do {
874 ++Pci_Io_Write_Count;
875 rc = HvCall4(HvCallPciBarStore16, dsa, BarOffset, swab16(data), 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700876 } while (CheckReturnCode("WWW", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878EXPORT_SYMBOL(iSeries_Write_Word);
879
880void iSeries_Write_Long(u32 data, volatile void __iomem *IoAddress)
881{
882 u64 BarOffset;
883 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700884 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 u64 rc;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000886 struct device_node *DevNode =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
888
889 if (DevNode == NULL) {
890 static unsigned long last_jiffies;
891 static int num_printed;
892
893 if ((jiffies - last_jiffies) > 60 * HZ) {
894 last_jiffies = jiffies;
895 num_printed = 0;
896 }
897 if (num_printed++ < 10)
898 printk(KERN_ERR "iSeries_Write_Long: invalid access at IO address %p\n", IoAddress);
899 return;
900 }
901 do {
902 ++Pci_Io_Write_Count;
903 rc = HvCall4(HvCallPciBarStore32, dsa, BarOffset, swab32(data), 0);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700904 } while (CheckReturnCode("WWL", DevNode, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906EXPORT_SYMBOL(iSeries_Write_Long);