blob: 68f248b4c6960ca771b610c6c2d9a5e633fe69bf [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
Stephen Rothwell1dee2022007-12-07 16:10:22 +11003 * Copyright (C) 2005,2007 Stephen Rothwell, IBM Corp
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * iSeries specific routines for PCI.
Stephen Rothwelld3878992005-09-28 02:50:25 +10006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Based on code from pci.c and iSeries_pci.c 32bit
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
Stephen Rothwelld3878992005-09-28 02:50:25 +100013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
Stephen Rothwelld3878992005-09-28 02:50:25 +100018 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23#include <linux/kernel.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100024#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
26#include <linux/init.h>
27#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/pci.h>
29
Stephen Rothwell1dee2022007-12-07 16:10:22 +110030#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/io.h>
32#include <asm/irq.h>
33#include <asm/prom.h>
34#include <asm/machdep.h>
35#include <asm/pci-bridge.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/iommu.h>
Stephen Rothwell426c1a12005-10-14 14:51:42 +100037#include <asm/abs_addr.h>
Stephen Rothwellcaf81322006-09-21 18:00:00 +100038#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Stephen Rothwell1dee2022007-12-07 16:10:22 +110040#include <asm/iseries/hv_types.h>
Kelly Daly8021b8a2005-11-02 11:41:12 +110041#include <asm/iseries/hv_call_xm.h>
Kelly Dalybbc8b622005-11-02 15:10:38 +110042#include <asm/iseries/mf.h>
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100043#include <asm/iseries/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Stephen Rothwelld3878992005-09-28 02:50:25 +100045#include <asm/ppc-pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Stephen Rothwellb08567cb2005-09-28 23:37:01 +100047#include "irq.h"
Stephen Rothwell426c1a12005-10-14 14:51:42 +100048#include "pci.h"
Stephen Rothwellc6d2ea92005-10-14 17:16:17 +100049#include "call_pci.h"
Stephen Rothwellb08567cb2005-09-28 23:37:01 +100050
Stephen Rothwellb9b18122007-12-07 01:48:14 +110051#define PCI_RETRY_MAX 3
52static int limit_pci_retries = 1; /* Set Retry Error on. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
55 * Table defines
56 * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
57 */
58#define IOMM_TABLE_MAX_ENTRIES 1024
59#define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
60#define BASE_IO_MEMORY 0xE000000000000000UL
61
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100062static unsigned long max_io_memory = BASE_IO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static long current_iomm_table_entry;
64
65/*
66 * Lookup Tables.
67 */
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100068static struct device_node *iomm_table[IOMM_TABLE_MAX_ENTRIES];
69static u8 iobar_table[IOMM_TABLE_MAX_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100071static const char pci_io_text[] = "iSeries PCI I/O";
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static DEFINE_SPINLOCK(iomm_table_lock);
73
74/*
Stephen Rothwell2cd10082007-12-07 01:55:50 +110075 * Generate a Direct Select Address for the Hypervisor
76 */
77static inline u64 iseries_ds_addr(struct device_node *node)
78{
79 struct pci_dn *pdn = PCI_DN(node);
80
81 return ((u64)pdn->busno << 48) + ((u64)pdn->bussubno << 40)
82 + ((u64)0x10 << 32);
83}
84
85/*
Stephen Rothwell1dee2022007-12-07 16:10:22 +110086 * Size of Bus VPD data
87 */
88#define BUS_VPDSIZE 1024
89
90/*
91 * Bus Vpd Tags
92 */
93#define VPD_END_OF_AREA 0x79
94#define VPD_ID_STRING 0x82
95#define VPD_VENDOR_AREA 0x84
96
97/*
98 * Mfg Area Tags
99 */
100#define VPD_FRU_FRAME_ID 0x4649 /* "FI" */
101#define VPD_SLOT_MAP_FORMAT 0x4D46 /* "MF" */
102#define VPD_SLOT_MAP 0x534D /* "SM" */
103
104/*
105 * Structures of the areas
106 */
107struct mfg_vpd_area {
108 u16 tag;
109 u8 length;
110 u8 data1;
111 u8 data2;
112};
113#define MFG_ENTRY_SIZE 3
114
115struct slot_map {
116 u8 agent;
117 u8 secondary_agent;
118 u8 phb;
119 char card_location[3];
120 char parms[8];
121 char reserved[2];
122};
123#define SLOT_ENTRY_SIZE 16
124
125/*
126 * Parse the Slot Area
127 */
128static void __init iseries_parse_slot_area(struct slot_map *map, int len,
129 HvAgentId agent, u8 *phb, char card[4])
130{
131 /*
132 * Parse Slot label until we find the one requested
133 */
134 while (len > 0) {
135 if (map->agent == agent) {
136 /*
137 * If Phb wasn't found, grab the entry first one found.
138 */
139 if (*phb == 0xff)
140 *phb = map->phb;
141 /* Found it, extract the data. */
142 if (map->phb == *phb) {
143 memcpy(card, &map->card_location, 3);
144 card[3] = 0;
145 break;
146 }
147 }
148 /* Point to the next Slot */
149 map = (struct slot_map *)((char *)map + SLOT_ENTRY_SIZE);
150 len -= SLOT_ENTRY_SIZE;
151 }
152}
153
154/*
155 * Parse the Mfg Area
156 */
157static void __init iseries_parse_mfg_area(struct mfg_vpd_area *area, int len,
158 HvAgentId agent, u8 *phb, u8 *frame, char card[4])
159{
160 u16 slot_map_fmt = 0;
161
162 /* Parse Mfg Data */
163 while (len > 0) {
164 int mfg_tag_len = area->length;
165 /* Frame ID (FI 4649020310 ) */
166 if (area->tag == VPD_FRU_FRAME_ID)
167 *frame = area->data1;
168 /* Slot Map Format (MF 4D46020004 ) */
169 else if (area->tag == VPD_SLOT_MAP_FORMAT)
170 slot_map_fmt = (area->data1 * 256)
171 + area->data2;
172 /* Slot Map (SM 534D90 */
173 else if (area->tag == VPD_SLOT_MAP) {
174 struct slot_map *slot_map;
175
176 if (slot_map_fmt == 0x1004)
177 slot_map = (struct slot_map *)((char *)area
178 + MFG_ENTRY_SIZE + 1);
179 else
180 slot_map = (struct slot_map *)((char *)area
181 + MFG_ENTRY_SIZE);
182 iseries_parse_slot_area(slot_map, mfg_tag_len,
183 agent, phb, card);
184 }
185 /*
186 * Point to the next Mfg Area
187 * Use defined size, sizeof give wrong answer
188 */
189 area = (struct mfg_vpd_area *)((char *)area + mfg_tag_len
190 + MFG_ENTRY_SIZE);
191 len -= (mfg_tag_len + MFG_ENTRY_SIZE);
192 }
193}
194
195/*
196 * Look for "BUS".. Data is not Null terminated.
197 * PHBID of 0xFF indicates PHB was not found in VPD Data.
198 */
199static u8 __init iseries_parse_phbid(u8 *area, int len)
200{
201 while (len > 0) {
202 if ((*area == 'B') && (*(area + 1) == 'U')
203 && (*(area + 2) == 'S')) {
204 area += 3;
205 while (*area == ' ')
206 area++;
207 return *area & 0x0F;
208 }
209 area++;
210 len--;
211 }
212 return 0xff;
213}
214
215/*
216 * Parse out the VPD Areas
217 */
218static void __init iseries_parse_vpd(u8 *data, int data_len,
219 HvAgentId agent, u8 *frame, char card[4])
220{
221 u8 phb = 0xff;
222
223 while (data_len > 0) {
224 int len;
225 u8 tag = *data;
226
227 if (tag == VPD_END_OF_AREA)
228 break;
229 len = *(data + 1) + (*(data + 2) * 256);
230 data += 3;
231 data_len -= 3;
232 if (tag == VPD_ID_STRING)
233 phb = iseries_parse_phbid(data, len);
234 else if (tag == VPD_VENDOR_AREA)
235 iseries_parse_mfg_area((struct mfg_vpd_area *)data, len,
236 agent, &phb, frame, card);
237 /* Point to next Area. */
238 data += len;
239 data_len -= len;
240 }
241}
242
243static int __init iseries_get_location_code(u16 bus, HvAgentId agent,
244 u8 *frame, char card[4])
245{
246 int status = 0;
247 int bus_vpd_len = 0;
248 u8 *bus_vpd = kmalloc(BUS_VPDSIZE, GFP_KERNEL);
249
250 if (bus_vpd == NULL) {
251 printk("PCI: Bus VPD Buffer allocation failure.\n");
252 return 0;
253 }
254 bus_vpd_len = HvCallPci_getBusVpd(bus, iseries_hv_addr(bus_vpd),
255 BUS_VPDSIZE);
256 if (bus_vpd_len == 0) {
257 printk("PCI: Bus VPD Buffer zero length.\n");
258 goto out_free;
259 }
260 /* printk("PCI: bus_vpd: %p, %d\n",bus_vpd, bus_vpd_len); */
261 /* Make sure this is what I think it is */
262 if (*bus_vpd != VPD_ID_STRING) {
263 printk("PCI: Bus VPD Buffer missing starting tag.\n");
264 goto out_free;
265 }
266 iseries_parse_vpd(bus_vpd, bus_vpd_len, agent, frame, card);
267 status = 1;
268out_free:
269 kfree(bus_vpd);
270 return status;
271}
272
273/*
274 * Prints the device information.
275 * - Pass in pci_dev* pointer to the device.
276 * - Pass in the device count
277 *
278 * Format:
279 * PCI: Bus 0, Device 26, Vendor 0x12AE Frame 1, Card C10 Ethernet
280 * controller
281 */
282static void __init iseries_device_information(struct pci_dev *pdev, int count,
283 u16 bus, HvSubBusNumber subbus)
284{
285 u8 frame = 0;
286 char card[4];
287 HvAgentId agent;
288
289 agent = ISERIES_PCI_AGENTID(ISERIES_GET_DEVICE_FROM_SUBBUS(subbus),
290 ISERIES_GET_FUNCTION_FROM_SUBBUS(subbus));
291
292 if (iseries_get_location_code(bus, agent, &frame, card)) {
293 printk("%d. PCI: Bus%3d, Device%3d, Vendor %04X Frame%3d, "
294 "Card %4s 0x%04X\n", count, bus,
295 PCI_SLOT(pdev->devfn), pdev->vendor, frame,
296 card, (int)(pdev->class >> 8));
297 }
298}
299
300/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 * iomm_table_allocate_entry
302 *
303 * Adds pci_dev entry in address translation table
304 *
305 * - Allocates the number of entries required in table base on BAR
306 * size.
307 * - Allocates starting at BASE_IO_MEMORY and increases.
308 * - The size is round up to be a multiple of entry size.
309 * - CurrentIndex is incremented to keep track of the last entry.
310 * - Builds the resource entry for allocated BARs.
311 */
Stephen Rothwell1e105902007-11-19 17:03:59 +1100312static void __init iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct resource *bar_res = &dev->resource[bar_num];
315 long bar_size = pci_resource_len(dev, bar_num);
316
317 /*
318 * No space to allocate, quick exit, skip Allocation.
319 */
320 if (bar_size == 0)
321 return;
322 /*
323 * Set Resource values.
324 */
325 spin_lock(&iomm_table_lock);
326 bar_res->name = pci_io_text;
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000327 bar_res->start = BASE_IO_MEMORY +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 bar_res->end = bar_res->start + bar_size - 1;
330 /*
331 * Allocate the number of table entries needed for BAR.
332 */
333 while (bar_size > 0 ) {
334 iomm_table[current_iomm_table_entry] = dev->sysdata;
335 iobar_table[current_iomm_table_entry] = bar_num;
336 bar_size -= IOMM_TABLE_ENTRY_SIZE;
337 ++current_iomm_table_entry;
338 }
339 max_io_memory = BASE_IO_MEMORY +
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000340 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 spin_unlock(&iomm_table_lock);
342}
343
344/*
345 * allocate_device_bars
346 *
347 * - Allocates ALL pci_dev BAR's and updates the resources with the
348 * BAR value. BARS with zero length will have the resources
349 * The HvCallPci_getBarParms is used to get the size of the BAR
350 * space. It calls iomm_table_allocate_entry to allocate
351 * each entry.
352 * - Loops through The Bar resources(0 - 5) including the ROM
353 * is resource(6).
354 */
Stephen Rothwell1e105902007-11-19 17:03:59 +1100355static void __init allocate_device_bars(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 int bar_num;
358
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000359 for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 iomm_table_allocate_entry(dev, bar_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/*
364 * Log error information to system console.
365 * Filter out the device not there errors.
366 * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
367 * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
368 * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
369 */
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100370static void pci_log_error(char *error, int bus, int subbus,
371 int agent, int hv_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100373 if (hv_res == 0x0302)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return;
375 printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100376 error, bus, subbus, agent, hv_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379/*
Stephen Rothwell9103eb72007-12-07 01:46:40 +1100380 * Look down the chain to find the matching Device Device
381 */
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100382static struct device_node *find_device_node(int bus, int devfn)
Stephen Rothwell9103eb72007-12-07 01:46:40 +1100383{
384 struct device_node *node;
385
386 for (node = NULL; (node = of_find_all_nodes(node)); ) {
387 struct pci_dn *pdn = PCI_DN(node);
388
389 if (pdn && (bus == pdn->busno) && (devfn == pdn->devfn))
390 return node;
391 }
392 return NULL;
393}
394
395/*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000396 * iSeries_pci_final_fixup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
398void __init iSeries_pci_final_fixup(void)
399{
400 struct pci_dev *pdev = NULL;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000401 struct device_node *node;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100402 int num_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* Fix up at the device node and pci_dev relationship */
405 mf_display_src(0xC9000100);
406
407 printk("pcibios_final_fixup\n");
408 for_each_pci_dev(pdev) {
Stephen Rothwell3f178632007-12-07 01:51:48 +1100409 const u32 *agent;
Stephen Rothwell3b32c162007-12-07 01:56:55 +1100410 const u32 *sub_bus;
Stephen Rothwell53761742007-12-07 01:58:01 +1100411 unsigned char bus = pdev->bus->number;
Stephen Rothwell3f178632007-12-07 01:51:48 +1100412
Stephen Rothwell53761742007-12-07 01:58:01 +1100413 node = find_device_node(bus, pdev->devfn);
414 printk("pci dev %p (%x.%x), node %p\n", pdev, bus,
415 pdev->devfn, node);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100416 if (!node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 printk("PCI: Device Tree not found for 0x%016lX\n",
418 (unsigned long)pdev);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100419 continue;
420 }
421
Stephen Rothwell3f178632007-12-07 01:51:48 +1100422 agent = of_get_property(node, "linux,agent-id", NULL);
Stephen Rothwell3b32c162007-12-07 01:56:55 +1100423 sub_bus = of_get_property(node, "linux,subbus", NULL);
Stephen Rothwell53761742007-12-07 01:58:01 +1100424 if (agent && sub_bus) {
425 u8 irq = iSeries_allocate_IRQ(bus, 0, *sub_bus);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100426 int err;
427
Stephen Rothwell53761742007-12-07 01:58:01 +1100428 err = HvCallXm_connectBusUnit(bus, *sub_bus,
Stephen Rothwell3f178632007-12-07 01:51:48 +1100429 *agent, irq);
430 if (err)
431 pci_log_error("Connect Bus Unit",
Stephen Rothwell53761742007-12-07 01:58:01 +1100432 bus, *sub_bus, *agent, err);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100433 else {
Stephen Rothwell53761742007-12-07 01:58:01 +1100434 err = HvCallPci_configStore8(bus, *sub_bus,
435 *agent, PCI_INTERRUPT_LINE, irq);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100436 if (err)
437 pci_log_error("PciCfgStore Irq Failed!",
Stephen Rothwell53761742007-12-07 01:58:01 +1100438 bus, *sub_bus, *agent, err);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100439 else
440 pdev->irq = irq;
441 }
442 }
443
444 num_dev++;
445 pdev->sysdata = node;
446 PCI_DN(node)->pcidev = pdev;
447 allocate_device_bars(pdev);
Stephen Rothwellcd9afb32007-12-07 16:08:33 +1100448 iseries_device_information(pdev, num_dev, bus, *sub_bus);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100449 iommu_devnode_init_iSeries(pdev, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451 iSeries_activate_IRQs();
452 mf_display_src(0xC9000200);
453}
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/*
456 * Config space read and write functions.
457 * For now at least, we look for the device node for the bus and devfn
458 * that we are asked to access. It may be possible to translate the devfn
459 * to a subbus and deviceid more directly.
460 */
461static u64 hv_cfg_read_func[4] = {
462 HvCallPciConfigLoad8, HvCallPciConfigLoad16,
463 HvCallPciConfigLoad32, HvCallPciConfigLoad32
464};
465
466static u64 hv_cfg_write_func[4] = {
467 HvCallPciConfigStore8, HvCallPciConfigStore16,
468 HvCallPciConfigStore32, HvCallPciConfigStore32
469};
470
471/*
472 * Read PCI config space
473 */
474static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
475 int offset, int size, u32 *val)
476{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100477 struct device_node *node = find_device_node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 u64 fn;
479 struct HvCallPci_LoadReturn ret;
480
481 if (node == NULL)
482 return PCIBIOS_DEVICE_NOT_FOUND;
483 if (offset > 255) {
484 *val = ~0;
485 return PCIBIOS_BAD_REGISTER_NUMBER;
486 }
487
488 fn = hv_cfg_read_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000489 HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (ret.rc != 0) {
492 *val = ~0;
493 return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
494 }
495
496 *val = ret.value;
497 return 0;
498}
499
500/*
501 * Write PCI config space
502 */
503
504static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
505 int offset, int size, u32 val)
506{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100507 struct device_node *node = find_device_node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 u64 fn;
509 u64 ret;
510
511 if (node == NULL)
512 return PCIBIOS_DEVICE_NOT_FOUND;
513 if (offset > 255)
514 return PCIBIOS_BAD_REGISTER_NUMBER;
515
516 fn = hv_cfg_write_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000517 ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (ret != 0)
520 return PCIBIOS_DEVICE_NOT_FOUND;
521
522 return 0;
523}
524
525static struct pci_ops iSeries_pci_ops = {
526 .read = iSeries_pci_read_config,
527 .write = iSeries_pci_write_config
528};
529
530/*
531 * Check Return Code
532 * -> On Failure, print and log information.
533 * Increment Retry Count, if exceeds max, panic partition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 *
535 * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
536 * PCI: Device 23.90 ReadL Retry( 1)
537 * PCI: Device 23.90 ReadL Retry Successful(1)
538 */
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100539static int check_return_code(char *type, struct device_node *dn,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700540 int *retry, u64 ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 if (ret != 0) {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100543 struct pci_dn *pdn = PCI_DN(dn);
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000544
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700545 (*retry)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100547 type, pdn->busno, pdn->devfn,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700548 *retry, (int)ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /*
550 * Bump the retry and check for retry count exceeded.
551 * If, Exceeded, panic the system.
552 */
Stephen Rothwellb9b18122007-12-07 01:48:14 +1100553 if (((*retry) > PCI_RETRY_MAX) &&
554 (limit_pci_retries > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 mf_display_src(0xB6000103);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700556 panic_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 panic("PCI: Hardware I/O Error, SRC B6000103, "
558 "Automatic Reboot Disabled.\n");
559 }
560 return -1; /* Retry Try */
561 }
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/*
566 * Translate the I/O Address into a device node, bar, and bar offset.
567 * Note: Make sure the passed variable end up on the stack to avoid
568 * the exposure of being device global.
569 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000570static inline struct device_node *xlate_iomm_address(
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100571 const volatile void __iomem *addr,
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100572 u64 *dsaptr, u64 *bar_offset, const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100574 unsigned long orig_addr;
575 unsigned long base_addr;
576 unsigned long ind;
577 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100579 orig_addr = (unsigned long __force)addr;
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100580 if ((orig_addr < BASE_IO_MEMORY) || (orig_addr >= max_io_memory)) {
581 static unsigned long last_jiffies;
582 static int num_printed;
583
584 if ((jiffies - last_jiffies) > 60 * HZ) {
585 last_jiffies = jiffies;
586 num_printed = 0;
587 }
588 if (num_printed++ < 10)
589 printk(KERN_ERR
590 "iSeries_%s: invalid access at IO address %p\n",
591 func, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return NULL;
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100593 }
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100594 base_addr = orig_addr - BASE_IO_MEMORY;
595 ind = base_addr / IOMM_TABLE_ENTRY_SIZE;
596 dn = iomm_table[ind];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100598 if (dn != NULL) {
599 int barnum = iobar_table[ind];
600 *dsaptr = iseries_ds_addr(dn) | (barnum << 24);
601 *bar_offset = base_addr % IOMM_TABLE_ENTRY_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 } else
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100603 panic("PCI: Invalid PCI IO address detected!\n");
604 return dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607/*
608 * Read MM I/O Instructions for the iSeries
609 * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100610 * else, data is returned in Big Endian format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
Stephen Rothwell34489382007-12-07 01:53:44 +1100612static u8 iseries_readb(const volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100614 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700616 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct HvCallPci_LoadReturn ret;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100618 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100619 xlate_iomm_address(addr, &dsa, &bar_offset, "read_byte");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100621 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 do {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100624 HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, bar_offset, 0);
625 } while (check_return_code("RDB", dn, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100627 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Stephen Rothwell34489382007-12-07 01:53:44 +1100630static u16 iseries_readw_be(const volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100632 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700634 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct HvCallPci_LoadReturn ret;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100636 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100637 xlate_iomm_address(addr, &dsa, &bar_offset, "read_word");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100639 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100643 bar_offset, 0);
644 } while (check_return_code("RDW", dn, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100646 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Stephen Rothwell34489382007-12-07 01:53:44 +1100649static u32 iseries_readl_be(const volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100651 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700653 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct HvCallPci_LoadReturn ret;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100655 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100656 xlate_iomm_address(addr, &dsa, &bar_offset, "read_long");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100658 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100662 bar_offset, 0);
663 } while (check_return_code("RDL", dn, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100665 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668/*
669 * Write MM I/O Instructions for the iSeries
670 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 */
Stephen Rothwell34489382007-12-07 01:53:44 +1100672static void iseries_writeb(u8 data, volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100674 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700676 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 u64 rc;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100678 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100679 xlate_iomm_address(addr, &dsa, &bar_offset, "write_byte");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100681 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 do {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100684 rc = HvCall4(HvCallPciBarStore8, dsa, bar_offset, data, 0);
685 } while (check_return_code("WWB", dn, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Stephen Rothwell34489382007-12-07 01:53:44 +1100688static void iseries_writew_be(u16 data, volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100690 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700692 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 u64 rc;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100694 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100695 xlate_iomm_address(addr, &dsa, &bar_offset, "write_word");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100697 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 do {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100700 rc = HvCall4(HvCallPciBarStore16, dsa, bar_offset, data, 0);
701 } while (check_return_code("WWW", dn, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Stephen Rothwell34489382007-12-07 01:53:44 +1100704static void iseries_writel_be(u32 data, volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100706 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700708 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 u64 rc;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100710 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100711 xlate_iomm_address(addr, &dsa, &bar_offset, "write_long");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100713 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 do {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100716 rc = HvCall4(HvCallPciBarStore32, dsa, bar_offset, data, 0);
717 } while (check_return_code("WWL", dn, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000719
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100720static u16 iseries_readw(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000721{
Stephen Rothwell34489382007-12-07 01:53:44 +1100722 return le16_to_cpu(iseries_readw_be(addr));
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000723}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000724
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100725static u32 iseries_readl(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000726{
Stephen Rothwell34489382007-12-07 01:53:44 +1100727 return le32_to_cpu(iseries_readl_be(addr));
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000728}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000729
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100730static void iseries_writew(u16 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000731{
Stephen Rothwell34489382007-12-07 01:53:44 +1100732 iseries_writew_be(cpu_to_le16(data), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000733}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000734
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100735static void iseries_writel(u32 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000736{
Stephen Rothwell34489382007-12-07 01:53:44 +1100737 iseries_writel(cpu_to_le32(data), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000738}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000739
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100740static void iseries_readsb(const volatile void __iomem *addr, void *buf,
741 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000742{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100743 u8 *dst = buf;
744 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100745 *(dst++) = iseries_readb(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000746}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000747
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100748static void iseries_readsw(const volatile void __iomem *addr, void *buf,
749 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000750{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100751 u16 *dst = buf;
752 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100753 *(dst++) = iseries_readw_be(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000754}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000755
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100756static void iseries_readsl(const volatile void __iomem *addr, void *buf,
757 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000758{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100759 u32 *dst = buf;
760 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100761 *(dst++) = iseries_readl_be(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000762}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000763
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100764static void iseries_writesb(volatile void __iomem *addr, const void *buf,
765 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000766{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100767 const u8 *src = buf;
768 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100769 iseries_writeb(*(src++), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000770}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000771
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100772static void iseries_writesw(volatile void __iomem *addr, const void *buf,
773 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000774{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100775 const u16 *src = buf;
776 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100777 iseries_writew_be(*(src++), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000778}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000779
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100780static void iseries_writesl(volatile void __iomem *addr, const void *buf,
781 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000782{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100783 const u32 *src = buf;
784 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100785 iseries_writel_be(*(src++), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000786}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000787
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100788static void iseries_memset_io(volatile void __iomem *addr, int c,
789 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000790{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100791 volatile char __iomem *d = addr;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000792
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100793 while (n-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100794 iseries_writeb(c, d++);
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100795}
796
797static void iseries_memcpy_fromio(void *dest, const volatile void __iomem *src,
798 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000799{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100800 char *d = dest;
801 const volatile char __iomem *s = src;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000802
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100803 while (n-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100804 *d++ = iseries_readb(s++);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000805}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000806
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100807static void iseries_memcpy_toio(volatile void __iomem *dest, const void *src,
808 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000809{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100810 const char *s = src;
811 volatile char __iomem *d = dest;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000812
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100813 while (n-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100814 iseries_writeb(*s++, d++);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000815}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000816
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100817/* We only set MMIO ops. The default PIO ops will be default
818 * to the MMIO ops + pci_io_base which is 0 on iSeries as
819 * expected so both should work.
820 *
821 * Note that we don't implement the readq/writeq versions as
822 * I don't know of an HV call for doing so. Thus, the default
823 * operation will be used instead, which will fault a the value
824 * return by iSeries for MMIO addresses always hits a non mapped
825 * area. This is as good as the BUG() we used to have there.
826 */
827static struct ppc_pci_io __initdata iseries_pci_io = {
828 .readb = iseries_readb,
829 .readw = iseries_readw,
830 .readl = iseries_readl,
831 .readw_be = iseries_readw_be,
832 .readl_be = iseries_readl_be,
833 .writeb = iseries_writeb,
834 .writew = iseries_writew,
835 .writel = iseries_writel,
836 .writew_be = iseries_writew_be,
837 .writel_be = iseries_writel_be,
838 .readsb = iseries_readsb,
839 .readsw = iseries_readsw,
840 .readsl = iseries_readsl,
841 .writesb = iseries_writesb,
842 .writesw = iseries_writesw,
843 .writesl = iseries_writesl,
844 .memset_io = iseries_memset_io,
845 .memcpy_fromio = iseries_memcpy_fromio,
846 .memcpy_toio = iseries_memcpy_toio,
847};
848
849/*
850 * iSeries_pcibios_init
851 *
852 * Description:
853 * This function checks for all possible system PCI host bridges that connect
854 * PCI buses. The system hypervisor is queried as to the guest partition
855 * ownership status. A pci_controller is built for any bus which is partially
856 * owned or fully owned by this guest partition.
857 */
858void __init iSeries_pcibios_init(void)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000859{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100860 struct pci_controller *phb;
861 struct device_node *root = of_find_node_by_path("/");
862 struct device_node *node = NULL;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000863
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100864 /* Install IO hooks */
865 ppc_pci_io = iseries_pci_io;
866
Stephen Rothwell9ccc4fd2007-12-07 02:03:23 +1100867 pci_probe_only = 1;
868
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000869 /* iSeries has no IO space in the common sense, it needs to set
870 * the IO base to 0
871 */
872 pci_io_base = 0;
873
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100874 if (root == NULL) {
875 printk(KERN_CRIT "iSeries_pcibios_init: can't find root "
876 "of device tree\n");
877 return;
878 }
879 while ((node = of_get_next_child(root, node)) != NULL) {
880 HvBusNumber bus;
881 const u32 *busp;
882
883 if ((node->type == NULL) || (strcmp(node->type, "pci") != 0))
884 continue;
885
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000886 busp = of_get_property(node, "bus-range", NULL);
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100887 if (busp == NULL)
888 continue;
889 bus = *busp;
890 printk("bus %d appears to exist\n", bus);
891 phb = pcibios_alloc_controller(node);
892 if (phb == NULL)
893 continue;
Stephen Rothwell6207e812007-12-07 02:04:33 +1100894 /* All legacy iSeries PHBs are in domain zero */
895 phb->global_number = 0;
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100896
Kumar Galabf440b72007-06-27 00:19:08 -0500897 phb->pci_mem_offset = bus;
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100898 phb->first_busno = bus;
899 phb->last_busno = bus;
900 phb->ops = &iSeries_pci_ops;
901 }
902
903 of_node_put(root);
904
905 pci_devs_phb_init();
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000906}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000907