pci.c 18.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * Copyright (C) 2001 Allan Trautman, IBM Corporation
 *
 * iSeries specific routines for PCI.
5
 *
L
Linus Torvalds 已提交
6 7 8 9 10 11
 * Based on code from pci.c and iSeries_pci.c 32bit
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
12
 *
L
Linus Torvalds 已提交
13 14 15 16
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
17
 *
L
Linus Torvalds 已提交
18 19 20 21 22
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */
#include <linux/kernel.h>
23
#include <linux/list.h>
L
Linus Torvalds 已提交
24 25 26 27 28 29 30 31 32 33 34
#include <linux/string.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/pci.h>

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/prom.h>
#include <asm/machdep.h>
#include <asm/pci-bridge.h>
#include <asm/iommu.h>
35
#include <asm/abs_addr.h>
36
#include <asm/firmware.h>
L
Linus Torvalds 已提交
37

38
#include <asm/iseries/hv_call_xm.h>
39
#include <asm/iseries/mf.h>
40
#include <asm/iseries/iommu.h>
L
Linus Torvalds 已提交
41

42
#include <asm/ppc-pci.h>
L
Linus Torvalds 已提交
43

44
#include "irq.h"
45
#include "pci.h"
46
#include "call_pci.h"
47

48 49
#define PCI_RETRY_MAX	3
static int limit_pci_retries = 1;	/* Set Retry Error on. */
L
Linus Torvalds 已提交
50 51 52 53 54 55 56 57 58

/*
 * Table defines
 * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
 */
#define IOMM_TABLE_MAX_ENTRIES	1024
#define IOMM_TABLE_ENTRY_SIZE	0x0000000000400000UL
#define BASE_IO_MEMORY		0xE000000000000000UL

59
static unsigned long max_io_memory = BASE_IO_MEMORY;
L
Linus Torvalds 已提交
60 61 62 63 64
static long current_iomm_table_entry;

/*
 * Lookup Tables.
 */
65 66
static struct device_node *iomm_table[IOMM_TABLE_MAX_ENTRIES];
static u8 iobar_table[IOMM_TABLE_MAX_ENTRIES];
L
Linus Torvalds 已提交
67

68
static const char pci_io_text[] = "iSeries PCI I/O";
L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82
static DEFINE_SPINLOCK(iomm_table_lock);

/*
 * iomm_table_allocate_entry
 *
 * Adds pci_dev entry in address translation table
 *
 * - Allocates the number of entries required in table base on BAR
 *   size.
 * - Allocates starting at BASE_IO_MEMORY and increases.
 * - The size is round up to be a multiple of entry size.
 * - CurrentIndex is incremented to keep track of the last entry.
 * - Builds the resource entry for allocated BARs.
 */
83
static void __init iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
L
Linus Torvalds 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97
{
	struct resource *bar_res = &dev->resource[bar_num];
	long bar_size = pci_resource_len(dev, bar_num);

	/*
	 * No space to allocate, quick exit, skip Allocation.
	 */
	if (bar_size == 0)
		return;
	/*
	 * Set Resource values.
	 */
	spin_lock(&iomm_table_lock);
	bar_res->name = pci_io_text;
98
	bar_res->start = BASE_IO_MEMORY +
L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106 107 108 109 110
		IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
	bar_res->end = bar_res->start + bar_size - 1;
	/*
	 * Allocate the number of table entries needed for BAR.
	 */
	while (bar_size > 0 ) {
		iomm_table[current_iomm_table_entry] = dev->sysdata;
		iobar_table[current_iomm_table_entry] = bar_num;
		bar_size -= IOMM_TABLE_ENTRY_SIZE;
		++current_iomm_table_entry;
	}
	max_io_memory = BASE_IO_MEMORY +
111
		IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125
	spin_unlock(&iomm_table_lock);
}

/*
 * allocate_device_bars
 *
 * - Allocates ALL pci_dev BAR's and updates the resources with the
 *   BAR value.  BARS with zero length will have the resources
 *   The HvCallPci_getBarParms is used to get the size of the BAR
 *   space.  It calls iomm_table_allocate_entry to allocate
 *   each entry.
 * - Loops through The Bar resources(0 - 5) including the ROM
 *   is resource(6).
 */
126
static void __init allocate_device_bars(struct pci_dev *dev)
L
Linus Torvalds 已提交
127 128 129
{
	int bar_num;

130
	for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num)
L
Linus Torvalds 已提交
131 132 133 134 135 136 137 138 139 140
		iomm_table_allocate_entry(dev, bar_num);
}

/*
 * Log error information to system console.
 * Filter out the device not there errors.
 * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
 * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
 * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
 */
141 142
static void pci_log_error(char *error, int bus, int subbus,
		int agent, int hv_res)
L
Linus Torvalds 已提交
143
{
144
	if (hv_res == 0x0302)
L
Linus Torvalds 已提交
145 146
		return;
	printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
147
	       error, bus, subbus, agent, hv_res);
L
Linus Torvalds 已提交
148 149
}

150 151 152
/*
 * Look down the chain to find the matching Device Device
 */
153
static struct device_node *find_device_node(int bus, int devfn)
154 155 156 157 158 159 160 161 162 163 164 165
{
	struct device_node *node;

	for (node = NULL; (node = of_find_all_nodes(node)); ) {
		struct pci_dn *pdn = PCI_DN(node);

		if (pdn && (bus == pdn->busno) && (devfn == pdn->devfn))
			return node;
	}
	return NULL;
}

L
Linus Torvalds 已提交
166
/*
167
 * iSeries_pci_final_fixup(void)
L
Linus Torvalds 已提交
168 169 170 171
 */
void __init iSeries_pci_final_fixup(void)
{
	struct pci_dev *pdev = NULL;
172
	struct device_node *node;
173
	int num_dev = 0;
L
Linus Torvalds 已提交
174 175 176 177 178 179

	/* Fix up at the device node and pci_dev relationship */
	mf_display_src(0xC9000100);

	printk("pcibios_final_fixup\n");
	for_each_pci_dev(pdev) {
180
		node = find_device_node(pdev->bus->number, pdev->devfn);
L
Linus Torvalds 已提交
181 182 183 184
		printk("pci dev %p (%x.%x), node %p\n", pdev,
		       pdev->bus->number, pdev->devfn, node);

		if (node != NULL) {
185
			struct pci_dn *pdn = PCI_DN(node);
186
			const u32 *agent;
187

188
			agent = of_get_property(node, "linux,agent-id", NULL);
189 190 191 192 193 194 195 196
			if ((pdn != NULL) && (agent != NULL)) {
				u8 irq = iSeries_allocate_IRQ(pdn->busno, 0,
						pdn->bussubno);
				int err;

				err = HvCallXm_connectBusUnit(pdn->busno, pdn->bussubno,
						*agent, irq);
				if (err)
197
					pci_log_error("Connect Bus Unit",
198 199 200 201 202 203 204
						pdn->busno, pdn->bussubno, *agent, err);
				else {
					err = HvCallPci_configStore8(pdn->busno, pdn->bussubno,
							*agent,
							PCI_INTERRUPT_LINE,
							irq);
					if (err)
205
						pci_log_error("PciCfgStore Irq Failed!",
206 207 208 209 210 211
							pdn->busno, pdn->bussubno, *agent, err);
				}
				if (!err)
					pdev->irq = irq;
			}

212
			++num_dev;
213
			pdev->sysdata = node;
214
			PCI_DN(node)->pcidev = pdev;
L
Linus Torvalds 已提交
215
			allocate_device_bars(pdev);
216
			iSeries_Device_Information(pdev, num_dev);
217
			iommu_devnode_init_iSeries(pdev, node);
L
Linus Torvalds 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
		} else
			printk("PCI: Device Tree not found for 0x%016lX\n",
					(unsigned long)pdev);
	}
	iSeries_activate_IRQs();
	mf_display_src(0xC9000200);
}

/*
 * Config space read and write functions.
 * For now at least, we look for the device node for the bus and devfn
 * that we are asked to access.  It may be possible to translate the devfn
 * to a subbus and deviceid more directly.
 */
static u64 hv_cfg_read_func[4]  = {
	HvCallPciConfigLoad8, HvCallPciConfigLoad16,
	HvCallPciConfigLoad32, HvCallPciConfigLoad32
};

static u64 hv_cfg_write_func[4] = {
	HvCallPciConfigStore8, HvCallPciConfigStore16,
	HvCallPciConfigStore32, HvCallPciConfigStore32
};

/*
 * Read PCI config space
 */
static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
		int offset, int size, u32 *val)
{
248
	struct device_node *node = find_device_node(bus->number, devfn);
L
Linus Torvalds 已提交
249 250 251 252 253 254 255 256 257 258 259
	u64 fn;
	struct HvCallPci_LoadReturn ret;

	if (node == NULL)
		return PCIBIOS_DEVICE_NOT_FOUND;
	if (offset > 255) {
		*val = ~0;
		return PCIBIOS_BAD_REGISTER_NUMBER;
	}

	fn = hv_cfg_read_func[(size - 1) & 3];
260
	HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
L
Linus Torvalds 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

	if (ret.rc != 0) {
		*val = ~0;
		return PCIBIOS_DEVICE_NOT_FOUND;	/* or something */
	}

	*val = ret.value;
	return 0;
}

/*
 * Write PCI config space
 */

static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
		int offset, int size, u32 val)
{
278
	struct device_node *node = find_device_node(bus->number, devfn);
L
Linus Torvalds 已提交
279 280 281 282 283 284 285 286 287
	u64 fn;
	u64 ret;

	if (node == NULL)
		return PCIBIOS_DEVICE_NOT_FOUND;
	if (offset > 255)
		return PCIBIOS_BAD_REGISTER_NUMBER;

	fn = hv_cfg_write_func[(size - 1) & 3];
288
	ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
L
Linus Torvalds 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

	if (ret != 0)
		return PCIBIOS_DEVICE_NOT_FOUND;

	return 0;
}

static struct pci_ops iSeries_pci_ops = {
	.read = iSeries_pci_read_config,
	.write = iSeries_pci_write_config
};

/*
 * Check Return Code
 * -> On Failure, print and log information.
 *    Increment Retry Count, if exceeds max, panic partition.
 *
 * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
 * PCI: Device 23.90 ReadL Retry( 1)
 * PCI: Device 23.90 ReadL Retry Successful(1)
 */
310
static int check_return_code(char *type, struct device_node *dn,
311
		int *retry, u64 ret)
L
Linus Torvalds 已提交
312 313
{
	if (ret != 0)  {
314
		struct pci_dn *pdn = PCI_DN(dn);
315

316
		(*retry)++;
L
Linus Torvalds 已提交
317
		printk("PCI: %s: Device 0x%04X:%02X  I/O Error(%2d): 0x%04X\n",
318
				type, pdn->busno, pdn->devfn,
319
				*retry, (int)ret);
L
Linus Torvalds 已提交
320 321 322 323
		/*
		 * Bump the retry and check for retry count exceeded.
		 * If, Exceeded, panic the system.
		 */
324 325
		if (((*retry) > PCI_RETRY_MAX) &&
				(limit_pci_retries > 0)) {
L
Linus Torvalds 已提交
326
			mf_display_src(0xB6000103);
327
			panic_timeout = 0;
L
Linus Torvalds 已提交
328 329 330 331 332
			panic("PCI: Hardware I/O Error, SRC B6000103, "
					"Automatic Reboot Disabled.\n");
		}
		return -1;	/* Retry Try */
	}
333
	return 0;
L
Linus Torvalds 已提交
334 335 336 337 338 339 340
}

/*
 * Translate the I/O Address into a device node, bar, and bar offset.
 * Note: Make sure the passed variable end up on the stack to avoid
 * the exposure of being device global.
 */
341
static inline struct device_node *xlate_iomm_address(
342 343
		const volatile void __iomem *addr,
		u64 *dsaptr, u64 *bar_offset)
L
Linus Torvalds 已提交
344
{
345 346 347 348
	unsigned long orig_addr;
	unsigned long base_addr;
	unsigned long ind;
	struct device_node *dn;
L
Linus Torvalds 已提交
349

350 351
	orig_addr = (unsigned long __force)addr;
	if ((orig_addr < BASE_IO_MEMORY) || (orig_addr >= max_io_memory))
L
Linus Torvalds 已提交
352
		return NULL;
353 354 355 356 357 358 359 360
	base_addr = orig_addr - BASE_IO_MEMORY;
	ind = base_addr / IOMM_TABLE_ENTRY_SIZE;
	dn = iomm_table[ind];

	if (dn != NULL) {
		int barnum = iobar_table[ind];
		*dsaptr = iseries_ds_addr(dn) | (barnum << 24);
		*bar_offset = base_addr % IOMM_TABLE_ENTRY_SIZE;
L
Linus Torvalds 已提交
361
	} else
362 363
		panic("PCI: Invalid PCI IO address detected!\n");
	return dn;
L
Linus Torvalds 已提交
364 365 366 367 368
}

/*
 * Read MM I/O Instructions for the iSeries
 * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
369
 * else, data is returned in Big Endian format.
L
Linus Torvalds 已提交
370
 */
371
static u8 iSeries_read_byte(const volatile void __iomem *addr)
L
Linus Torvalds 已提交
372
{
373
	u64 bar_offset;
L
Linus Torvalds 已提交
374
	u64 dsa;
375
	int retry = 0;
L
Linus Torvalds 已提交
376
	struct HvCallPci_LoadReturn ret;
377 378
	struct device_node *dn =
		xlate_iomm_address(addr, &dsa, &bar_offset);
L
Linus Torvalds 已提交
379

380
	if (dn == NULL) {
L
Linus Torvalds 已提交
381 382 383 384 385 386 387 388
		static unsigned long last_jiffies;
		static int num_printed;

		if ((jiffies - last_jiffies) > 60 * HZ) {
			last_jiffies = jiffies;
			num_printed = 0;
		}
		if (num_printed++ < 10)
389 390
			printk(KERN_ERR "iSeries_read_byte: invalid access at IO address %p\n",
			       addr);
L
Linus Torvalds 已提交
391 392 393
		return 0xff;
	}
	do {
394 395
		HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, bar_offset, 0);
	} while (check_return_code("RDB", dn, &retry, ret.rc) != 0);
L
Linus Torvalds 已提交
396

397
	return ret.value;
L
Linus Torvalds 已提交
398 399
}

400
static u16 iSeries_read_word(const volatile void __iomem *addr)
L
Linus Torvalds 已提交
401
{
402
	u64 bar_offset;
L
Linus Torvalds 已提交
403
	u64 dsa;
404
	int retry = 0;
L
Linus Torvalds 已提交
405
	struct HvCallPci_LoadReturn ret;
406 407
	struct device_node *dn =
		xlate_iomm_address(addr, &dsa, &bar_offset);
L
Linus Torvalds 已提交
408

409
	if (dn == NULL) {
L
Linus Torvalds 已提交
410 411 412 413 414 415 416 417
		static unsigned long last_jiffies;
		static int num_printed;

		if ((jiffies - last_jiffies) > 60 * HZ) {
			last_jiffies = jiffies;
			num_printed = 0;
		}
		if (num_printed++ < 10)
418 419
			printk(KERN_ERR "iSeries_read_word: invalid access at IO address %p\n",
			       addr);
L
Linus Torvalds 已提交
420 421 422 423
		return 0xffff;
	}
	do {
		HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
424 425
				bar_offset, 0);
	} while (check_return_code("RDW", dn, &retry, ret.rc) != 0);
L
Linus Torvalds 已提交
426

427
	return ret.value;
L
Linus Torvalds 已提交
428 429
}

430
static u32 iSeries_read_long(const volatile void __iomem *addr)
L
Linus Torvalds 已提交
431
{
432
	u64 bar_offset;
L
Linus Torvalds 已提交
433
	u64 dsa;
434
	int retry = 0;
L
Linus Torvalds 已提交
435
	struct HvCallPci_LoadReturn ret;
436 437
	struct device_node *dn =
		xlate_iomm_address(addr, &dsa, &bar_offset);
L
Linus Torvalds 已提交
438

439
	if (dn == NULL) {
L
Linus Torvalds 已提交
440 441 442 443 444 445 446 447
		static unsigned long last_jiffies;
		static int num_printed;

		if ((jiffies - last_jiffies) > 60 * HZ) {
			last_jiffies = jiffies;
			num_printed = 0;
		}
		if (num_printed++ < 10)
448 449
			printk(KERN_ERR "iSeries_read_long: invalid access at IO address %p\n",
			       addr);
L
Linus Torvalds 已提交
450 451 452 453
		return 0xffffffff;
	}
	do {
		HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
454 455
				bar_offset, 0);
	} while (check_return_code("RDL", dn, &retry, ret.rc) != 0);
L
Linus Torvalds 已提交
456

457
	return ret.value;
L
Linus Torvalds 已提交
458 459 460 461 462 463
}

/*
 * Write MM I/O Instructions for the iSeries
 *
 */
464
static void iSeries_write_byte(u8 data, volatile void __iomem *addr)
L
Linus Torvalds 已提交
465
{
466
	u64 bar_offset;
L
Linus Torvalds 已提交
467
	u64 dsa;
468
	int retry = 0;
L
Linus Torvalds 已提交
469
	u64 rc;
470 471
	struct device_node *dn =
		xlate_iomm_address(addr, &dsa, &bar_offset);
L
Linus Torvalds 已提交
472

473
	if (dn == NULL) {
L
Linus Torvalds 已提交
474 475 476 477 478 479 480 481
		static unsigned long last_jiffies;
		static int num_printed;

		if ((jiffies - last_jiffies) > 60 * HZ) {
			last_jiffies = jiffies;
			num_printed = 0;
		}
		if (num_printed++ < 10)
482
			printk(KERN_ERR "iSeries_write_byte: invalid access at IO address %p\n", addr);
L
Linus Torvalds 已提交
483 484 485
		return;
	}
	do {
486 487
		rc = HvCall4(HvCallPciBarStore8, dsa, bar_offset, data, 0);
	} while (check_return_code("WWB", dn, &retry, rc) != 0);
L
Linus Torvalds 已提交
488 489
}

490
static void iSeries_write_word(u16 data, volatile void __iomem *addr)
L
Linus Torvalds 已提交
491
{
492
	u64 bar_offset;
L
Linus Torvalds 已提交
493
	u64 dsa;
494
	int retry = 0;
L
Linus Torvalds 已提交
495
	u64 rc;
496 497
	struct device_node *dn =
		xlate_iomm_address(addr, &dsa, &bar_offset);
L
Linus Torvalds 已提交
498

499
	if (dn == NULL) {
L
Linus Torvalds 已提交
500 501 502 503 504 505 506 507
		static unsigned long last_jiffies;
		static int num_printed;

		if ((jiffies - last_jiffies) > 60 * HZ) {
			last_jiffies = jiffies;
			num_printed = 0;
		}
		if (num_printed++ < 10)
508 509
			printk(KERN_ERR "iSeries_write_word: invalid access at IO address %p\n",
			       addr);
L
Linus Torvalds 已提交
510 511 512
		return;
	}
	do {
513 514
		rc = HvCall4(HvCallPciBarStore16, dsa, bar_offset, data, 0);
	} while (check_return_code("WWW", dn, &retry, rc) != 0);
L
Linus Torvalds 已提交
515 516
}

517
static void iSeries_write_long(u32 data, volatile void __iomem *addr)
L
Linus Torvalds 已提交
518
{
519
	u64 bar_offset;
L
Linus Torvalds 已提交
520
	u64 dsa;
521
	int retry = 0;
L
Linus Torvalds 已提交
522
	u64 rc;
523 524
	struct device_node *dn =
		xlate_iomm_address(addr, &dsa, &bar_offset);
L
Linus Torvalds 已提交
525

526
	if (dn == NULL) {
L
Linus Torvalds 已提交
527 528 529 530 531 532 533 534
		static unsigned long last_jiffies;
		static int num_printed;

		if ((jiffies - last_jiffies) > 60 * HZ) {
			last_jiffies = jiffies;
			num_printed = 0;
		}
		if (num_printed++ < 10)
535 536
			printk(KERN_ERR "iSeries_write_long: invalid access at IO address %p\n",
			       addr);
L
Linus Torvalds 已提交
537 538 539
		return;
	}
	do {
540 541
		rc = HvCall4(HvCallPciBarStore32, dsa, bar_offset, data, 0);
	} while (check_return_code("WWL", dn, &retry, rc) != 0);
L
Linus Torvalds 已提交
542
}
543

544
static u8 iseries_readb(const volatile void __iomem *addr)
545
{
546
	return iSeries_read_byte(addr);
547 548
}

549
static u16 iseries_readw(const volatile void __iomem *addr)
550
{
551
	return le16_to_cpu(iSeries_read_word(addr));
552 553
}

554
static u32 iseries_readl(const volatile void __iomem *addr)
555
{
556
	return le32_to_cpu(iSeries_read_long(addr));
557 558
}

559
static u16 iseries_readw_be(const volatile void __iomem *addr)
560
{
561
	return iSeries_read_word(addr);
562 563
}

564
static u32 iseries_readl_be(const volatile void __iomem *addr)
565
{
566
	return iSeries_read_long(addr);
567 568
}

569
static void iseries_writeb(u8 data, volatile void __iomem *addr)
570
{
571
	iSeries_write_byte(data, addr);
572 573
}

574
static void iseries_writew(u16 data, volatile void __iomem *addr)
575
{
576
	iSeries_write_word(cpu_to_le16(data), addr);
577 578
}

579
static void iseries_writel(u32 data, volatile void __iomem *addr)
580
{
581
	iSeries_write_long(cpu_to_le32(data), addr);
582 583
}

584
static void iseries_writew_be(u16 data, volatile void __iomem *addr)
585
{
586
	iSeries_write_word(data, addr);
587 588
}

589
static void iseries_writel_be(u32 data, volatile void __iomem *addr)
590
{
591
	iSeries_write_long(data, addr);
592 593
}

594 595
static void iseries_readsb(const volatile void __iomem *addr, void *buf,
			   unsigned long count)
596
{
597 598
	u8 *dst = buf;
	while(count-- > 0)
599
		*(dst++) = iSeries_read_byte(addr);
600 601
}

602 603
static void iseries_readsw(const volatile void __iomem *addr, void *buf,
			   unsigned long count)
604
{
605 606
	u16 *dst = buf;
	while(count-- > 0)
607
		*(dst++) = iSeries_read_word(addr);
608 609
}

610 611
static void iseries_readsl(const volatile void __iomem *addr, void *buf,
			   unsigned long count)
612
{
613 614
	u32 *dst = buf;
	while(count-- > 0)
615
		*(dst++) = iSeries_read_long(addr);
616 617
}

618 619
static void iseries_writesb(volatile void __iomem *addr, const void *buf,
			    unsigned long count)
620
{
621 622
	const u8 *src = buf;
	while(count-- > 0)
623
		iSeries_write_byte(*(src++), addr);
624 625
}

626 627
static void iseries_writesw(volatile void __iomem *addr, const void *buf,
			    unsigned long count)
628
{
629 630
	const u16 *src = buf;
	while(count-- > 0)
631
		iSeries_write_word(*(src++), addr);
632 633
}

634 635
static void iseries_writesl(volatile void __iomem *addr, const void *buf,
			    unsigned long count)
636
{
637 638
	const u32 *src = buf;
	while(count-- > 0)
639
		iSeries_write_long(*(src++), addr);
640 641
}

642 643
static void iseries_memset_io(volatile void __iomem *addr, int c,
			      unsigned long n)
644
{
645
	volatile char __iomem *d = addr;
646

647
	while (n-- > 0)
648
		iSeries_write_byte(c, d++);
649 650
}

651 652
static void iseries_memcpy_fromio(void *dest, const volatile void __iomem *src,
				  unsigned long n)
653
{
654 655
	char *d = dest;
	const volatile char __iomem *s = src;
656

657
	while (n-- > 0)
658
		*d++ = iSeries_read_byte(s++);
659 660
}

661 662
static void iseries_memcpy_toio(volatile void __iomem *dest, const void *src,
				unsigned long n)
663
{
664 665
	const char *s = src;
	volatile char __iomem *d = dest;
666

667
	while (n-- > 0)
668
		iSeries_write_byte(*s++, d++);
669 670
}

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
/* We only set MMIO ops. The default PIO ops will be default
 * to the MMIO ops + pci_io_base which is 0 on iSeries as
 * expected so both should work.
 *
 * Note that we don't implement the readq/writeq versions as
 * I don't know of an HV call for doing so. Thus, the default
 * operation will be used instead, which will fault a the value
 * return by iSeries for MMIO addresses always hits a non mapped
 * area. This is as good as the BUG() we used to have there.
 */
static struct ppc_pci_io __initdata iseries_pci_io = {
	.readb = iseries_readb,
	.readw = iseries_readw,
	.readl = iseries_readl,
	.readw_be = iseries_readw_be,
	.readl_be = iseries_readl_be,
	.writeb = iseries_writeb,
	.writew = iseries_writew,
	.writel = iseries_writel,
	.writew_be = iseries_writew_be,
	.writel_be = iseries_writel_be,
	.readsb = iseries_readsb,
	.readsw = iseries_readsw,
	.readsl = iseries_readsl,
	.writesb = iseries_writesb,
	.writesw = iseries_writesw,
	.writesl = iseries_writesl,
	.memset_io = iseries_memset_io,
	.memcpy_fromio = iseries_memcpy_fromio,
	.memcpy_toio = iseries_memcpy_toio,
};
702

703 704 705 706 707 708 709 710 711 712
/*
 * iSeries_pcibios_init
 *
 * Description:
 *   This function checks for all possible system PCI host bridges that connect
 *   PCI buses.  The system hypervisor is queried as to the guest partition
 *   ownership status.  A pci_controller is built for any bus which is partially
 *   owned or fully owned by this guest partition.
 */
void __init iSeries_pcibios_init(void)
713
{
714 715 716
	struct pci_controller *phb;
	struct device_node *root = of_find_node_by_path("/");
	struct device_node *node = NULL;
717

718 719
	/* Install IO hooks */
	ppc_pci_io = iseries_pci_io;
720

721 722 723 724 725
	/* iSeries has no IO space in the common sense, it needs to set
	 * the IO base to 0
	 */
	pci_io_base = 0;

726 727 728 729 730 731 732 733
	if (root == NULL) {
		printk(KERN_CRIT "iSeries_pcibios_init: can't find root "
				"of device tree\n");
		return;
	}
	while ((node = of_get_next_child(root, node)) != NULL) {
		HvBusNumber bus;
		const u32 *busp;
734

735 736
		if ((node->type == NULL) || (strcmp(node->type, "pci") != 0))
			continue;
737

738
		busp = of_get_property(node, "bus-range", NULL);
739 740 741 742 743 744 745
		if (busp == NULL)
			continue;
		bus = *busp;
		printk("bus %d appears to exist\n", bus);
		phb = pcibios_alloc_controller(node);
		if (phb == NULL)
			continue;
746

747
		phb->pci_mem_offset = bus;
748 749 750 751
		phb->first_busno = bus;
		phb->last_busno = bus;
		phb->ops = &iSeries_pci_ops;
	}
752

753
	of_node_put(root);
754

755
	pci_devs_phb_init();
756
}
757