pci.c 31.4 KB
Newer Older
1
/* pci.c: UltraSparc PCI controller support.
L
Linus Torvalds 已提交
2 3 4 5
 *
 * Copyright (C) 1997, 1998, 1999 David S. Miller (davem@redhat.com)
 * Copyright (C) 1998, 1999 Eddie C. Dost   (ecd@skynet.be)
 * Copyright (C) 1999 Jakub Jelinek   (jj@ultra.linux.cz)
6 7 8
 *
 * OF tree based PCI bus probing taken from the PowerPC port
 * with minor modifications, see there for credits.
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/sched.h>
#include <linux/capability.h>
#include <linux/errno.h>
17
#include <linux/pci.h>
18 19
#include <linux/msi.h>
#include <linux/irq.h>
L
Linus Torvalds 已提交
20 21 22 23 24 25 26
#include <linux/init.h>

#include <asm/uaccess.h>
#include <asm/pgtable.h>
#include <asm/irq.h>
#include <asm/ebus.h>
#include <asm/isa.h>
27
#include <asm/prom.h>
28
#include <asm/apb.h>
L
Linus Torvalds 已提交
29

30 31
#include "pci_impl.h"

L
Linus Torvalds 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
unsigned long pci_memspace_mask = 0xffffffffUL;

#ifndef CONFIG_PCI
/* A "nop" PCI implementation. */
asmlinkage int sys_pciconfig_read(unsigned long bus, unsigned long dfn,
				  unsigned long off, unsigned long len,
				  unsigned char *buf)
{
	return 0;
}
asmlinkage int sys_pciconfig_write(unsigned long bus, unsigned long dfn,
				   unsigned long off, unsigned long len,
				   unsigned char *buf)
{
	return 0;
}
#else

/* List of all PCI controllers found in the system. */
51
struct pci_pbm_info *pci_pbm_root = NULL;
L
Linus Torvalds 已提交
52

53 54
/* Each PBM found gets a unique index. */
int pci_num_pbms = 0;
L
Linus Torvalds 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

volatile int pci_poke_in_progress;
volatile int pci_poke_cpu = -1;
volatile int pci_poke_faulted;

static DEFINE_SPINLOCK(pci_poke_lock);

void pci_config_read8(u8 *addr, u8 *ret)
{
	unsigned long flags;
	u8 byte;

	spin_lock_irqsave(&pci_poke_lock, flags);
	pci_poke_cpu = smp_processor_id();
	pci_poke_in_progress = 1;
	pci_poke_faulted = 0;
	__asm__ __volatile__("membar #Sync\n\t"
			     "lduba [%1] %2, %0\n\t"
			     "membar #Sync"
			     : "=r" (byte)
			     : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
			     : "memory");
	pci_poke_in_progress = 0;
	pci_poke_cpu = -1;
	if (!pci_poke_faulted)
		*ret = byte;
	spin_unlock_irqrestore(&pci_poke_lock, flags);
}

void pci_config_read16(u16 *addr, u16 *ret)
{
	unsigned long flags;
	u16 word;

	spin_lock_irqsave(&pci_poke_lock, flags);
	pci_poke_cpu = smp_processor_id();
	pci_poke_in_progress = 1;
	pci_poke_faulted = 0;
	__asm__ __volatile__("membar #Sync\n\t"
			     "lduha [%1] %2, %0\n\t"
			     "membar #Sync"
			     : "=r" (word)
			     : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
			     : "memory");
	pci_poke_in_progress = 0;
	pci_poke_cpu = -1;
	if (!pci_poke_faulted)
		*ret = word;
	spin_unlock_irqrestore(&pci_poke_lock, flags);
}

void pci_config_read32(u32 *addr, u32 *ret)
{
	unsigned long flags;
	u32 dword;

	spin_lock_irqsave(&pci_poke_lock, flags);
	pci_poke_cpu = smp_processor_id();
	pci_poke_in_progress = 1;
	pci_poke_faulted = 0;
	__asm__ __volatile__("membar #Sync\n\t"
			     "lduwa [%1] %2, %0\n\t"
			     "membar #Sync"
			     : "=r" (dword)
			     : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
			     : "memory");
	pci_poke_in_progress = 0;
	pci_poke_cpu = -1;
	if (!pci_poke_faulted)
		*ret = dword;
	spin_unlock_irqrestore(&pci_poke_lock, flags);
}

void pci_config_write8(u8 *addr, u8 val)
{
	unsigned long flags;

	spin_lock_irqsave(&pci_poke_lock, flags);
	pci_poke_cpu = smp_processor_id();
	pci_poke_in_progress = 1;
	pci_poke_faulted = 0;
	__asm__ __volatile__("membar #Sync\n\t"
			     "stba %0, [%1] %2\n\t"
			     "membar #Sync"
			     : /* no outputs */
			     : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
			     : "memory");
	pci_poke_in_progress = 0;
	pci_poke_cpu = -1;
	spin_unlock_irqrestore(&pci_poke_lock, flags);
}

void pci_config_write16(u16 *addr, u16 val)
{
	unsigned long flags;

	spin_lock_irqsave(&pci_poke_lock, flags);
	pci_poke_cpu = smp_processor_id();
	pci_poke_in_progress = 1;
	pci_poke_faulted = 0;
	__asm__ __volatile__("membar #Sync\n\t"
			     "stha %0, [%1] %2\n\t"
			     "membar #Sync"
			     : /* no outputs */
			     : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
			     : "memory");
	pci_poke_in_progress = 0;
	pci_poke_cpu = -1;
	spin_unlock_irqrestore(&pci_poke_lock, flags);
}

void pci_config_write32(u32 *addr, u32 val)
{
	unsigned long flags;

	spin_lock_irqsave(&pci_poke_lock, flags);
	pci_poke_cpu = smp_processor_id();
	pci_poke_in_progress = 1;
	pci_poke_faulted = 0;
	__asm__ __volatile__("membar #Sync\n\t"
			     "stwa %0, [%1] %2\n\t"
			     "membar #Sync"
			     : /* no outputs */
			     : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
			     : "memory");
	pci_poke_in_progress = 0;
	pci_poke_cpu = -1;
	spin_unlock_irqrestore(&pci_poke_lock, flags);
}

/* Probe for all PCI controllers in the system. */
186 187 188 189 190 191
extern void sabre_init(struct device_node *, const char *);
extern void psycho_init(struct device_node *, const char *);
extern void schizo_init(struct device_node *, const char *);
extern void schizo_plus_init(struct device_node *, const char *);
extern void tomatillo_init(struct device_node *, const char *);
extern void sun4v_pci_init(struct device_node *, const char *);
192
extern void fire_pci_init(struct device_node *, const char *);
L
Linus Torvalds 已提交
193 194 195

static struct {
	char *model_name;
196
	void (*init)(struct device_node *, const char *);
L
Linus Torvalds 已提交
197 198 199 200 201 202 203 204 205 206 207 208
} pci_controller_table[] __initdata = {
	{ "SUNW,sabre", sabre_init },
	{ "pci108e,a000", sabre_init },
	{ "pci108e,a001", sabre_init },
	{ "SUNW,psycho", psycho_init },
	{ "pci108e,8000", psycho_init },
	{ "SUNW,schizo", schizo_init },
	{ "pci108e,8001", schizo_init },
	{ "SUNW,schizo+", schizo_plus_init },
	{ "pci108e,8002", schizo_plus_init },
	{ "SUNW,tomatillo", tomatillo_init },
	{ "pci108e,a801", tomatillo_init },
209
	{ "SUNW,sun4v-pci", sun4v_pci_init },
210
	{ "pciex108e,80f0", fire_pci_init },
L
Linus Torvalds 已提交
211 212 213 214
};
#define PCI_NUM_CONTROLLER_TYPES (sizeof(pci_controller_table) / \
				  sizeof(pci_controller_table[0]))

215
static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
L
Linus Torvalds 已提交
216 217 218 219 220 221 222
{
	int i;

	for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
		if (!strncmp(model_name,
			     pci_controller_table[i].model_name,
			     namelen)) {
223
			pci_controller_table[i].init(dp, model_name);
L
Linus Torvalds 已提交
224 225 226 227 228 229 230
			return 1;
		}
	}

	return 0;
}

231
static int __init pci_is_controller(const char *model_name, int namelen, struct device_node *dp)
L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244
{
	int i;

	for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
		if (!strncmp(model_name,
			     pci_controller_table[i].model_name,
			     namelen)) {
			return 1;
		}
	}
	return 0;
}

245
static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
L
Linus Torvalds 已提交
246
{
247
	struct device_node *dp;
L
Linus Torvalds 已提交
248 249
	int count = 0;

250 251
	for_each_node_by_name(dp, "pci") {
		struct property *prop;
L
Linus Torvalds 已提交
252 253
		int len;

254 255 256 257 258 259
		prop = of_find_property(dp, "model", &len);
		if (!prop)
			prop = of_find_property(dp, "compatible", &len);

		if (prop) {
			const char *model = prop->value;
L
Linus Torvalds 已提交
260 261 262 263
			int item_len = 0;

			/* Our value may be a multi-valued string in the
			 * case of some compatible properties. For sanity,
264 265 266
			 * only try the first one.
			 */
			while (model[item_len] && len) {
L
Linus Torvalds 已提交
267 268 269 270
				len--;
				item_len++;
			}

271
			if (handler(model, item_len, dp))
L
Linus Torvalds 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285
				count++;
		}
	}

	return count;
}


/* Is there some PCI controller in the system?  */
int __init pcic_present(void)
{
	return pci_controller_scan(pci_is_controller);
}

286
const struct pci_iommu_ops *pci_iommu_ops;
287 288
EXPORT_SYMBOL(pci_iommu_ops);

289
extern const struct pci_iommu_ops pci_sun4u_iommu_ops,
290 291
	pci_sun4v_iommu_ops;

L
Linus Torvalds 已提交
292 293
/* Find each controller in the system, attach and initialize
 * software state structure for each and link into the
294
 * pci_pbm_root.  Setup the controller enough such
L
Linus Torvalds 已提交
295 296 297 298
 * that bus scanning can be done.
 */
static void __init pci_controller_probe(void)
{
299 300 301 302 303
	if (tlb_type == hypervisor)
		pci_iommu_ops = &pci_sun4v_iommu_ops;
	else
		pci_iommu_ops = &pci_sun4u_iommu_ops;

L
Linus Torvalds 已提交
304 305 306 307 308
	printk("PCI: Probing for controllers.\n");

	pci_controller_scan(pci_controller_init);
}

309 310 311 312 313 314 315 316 317 318 319 320 321 322
static int ofpci_verbose;

static int __init ofpci_debug(char *str)
{
	int val = 0;

	get_option(&str, &val);
	if (val)
		ofpci_verbose = 1;
	return 1;
}

__setup("ofpci_debug=", ofpci_debug);

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
static unsigned long pci_parse_of_flags(u32 addr0)
{
	unsigned long flags = 0;

	if (addr0 & 0x02000000) {
		flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
		flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
		flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
		if (addr0 & 0x40000000)
			flags |= IORESOURCE_PREFETCH
				 | PCI_BASE_ADDRESS_MEM_PREFETCH;
	} else if (addr0 & 0x01000000)
		flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
	return flags;
}

/* The of_device layer has translated all of the assigned-address properties
 * into physical address resources, we only have to figure out the register
 * mapping.
 */
static void pci_parse_of_addrs(struct of_device *op,
			       struct device_node *node,
			       struct pci_dev *dev)
{
	struct resource *op_res;
	const u32 *addrs;
	int proplen;

	addrs = of_get_property(node, "assigned-addresses", &proplen);
	if (!addrs)
		return;
354 355 356
	if (ofpci_verbose)
		printk("    parse addresses (%d bytes) @ %p\n",
		       proplen, addrs);
357 358 359 360 361 362 363 364 365 366
	op_res = &op->resource[0];
	for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
		struct resource *res;
		unsigned long flags;
		int i;

		flags = pci_parse_of_flags(addrs[0]);
		if (!flags)
			continue;
		i = addrs[0] & 0xff;
367 368 369
		if (ofpci_verbose)
			printk("  start: %lx, end: %lx, i: %x\n",
			       op_res->start, op_res->end, i);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

		if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
			res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
		} else if (i == dev->rom_base_reg) {
			res = &dev->resource[PCI_ROM_RESOURCE];
			flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
		} else {
			printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
			continue;
		}
		res->start = op_res->start;
		res->end = op_res->end;
		res->flags = flags;
		res->name = pci_name(dev);
	}
}

struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
				  struct device_node *node,
389 390
				  struct pci_bus *bus, int devfn,
				  int host_controller)
391 392 393 394
{
	struct dev_archdata *sd;
	struct pci_dev *dev;
	const char *type;
395
	u32 class;
396

397
	dev = alloc_pci_dev();
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
	if (!dev)
		return NULL;

	sd = &dev->dev.archdata;
	sd->iommu = pbm->iommu;
	sd->stc = &pbm->stc;
	sd->host_controller = pbm;
	sd->prom_node = node;
	sd->op = of_find_device_by_node(node);
	sd->msi_num = 0xffffffff;

	type = of_get_property(node, "device_type", NULL);
	if (type == NULL)
		type = "";

413 414 415
	if (ofpci_verbose)
		printk("    create device, devfn: %x, type: %s\n",
		       devfn, type);
416 417 418 419 420 421 422 423

	dev->bus = bus;
	dev->sysdata = node;
	dev->dev.parent = bus->bridge;
	dev->dev.bus = &pci_bus_type;
	dev->devfn = devfn;
	dev->multifunction = 0;		/* maybe a lie? */

424 425 426 427 428 429
	if (host_controller) {
		dev->vendor = 0x108e;
		dev->device = 0x8000;
		dev->subsystem_vendor = 0x0000;
		dev->subsystem_device = 0x0000;
		dev->cfg_size = 256;
430 431 432
		dev->class = PCI_CLASS_BRIDGE_HOST << 8;
		sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
			0x00, PCI_SLOT(devfn), PCI_FUNC(devfn));
433 434 435 436 437 438 439 440 441
	} else {
		dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
		dev->device = of_getintprop_default(node, "device-id", 0xffff);
		dev->subsystem_vendor =
			of_getintprop_default(node, "subsystem-vendor-id", 0);
		dev->subsystem_device =
			of_getintprop_default(node, "subsystem-id", 0);

		dev->cfg_size = pci_cfg_space_size(dev);
442

443 444 445 446 447 448 449 450
		/* We can't actually use the firmware value, we have
		 * to read what is in the register right now.  One
		 * reason is that in the case of IDE interfaces the
		 * firmware can sample the value before the the IDE
		 * interface is programmed into native mode.
		 */
		pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
		dev->class = class >> 8;
A
Auke Kok 已提交
451
		dev->revision = class & 0xff;
452 453 454

		sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
			dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
455
	}
456 457 458
	if (ofpci_verbose)
		printk("    class: 0x%x device name: %s\n",
		       dev->class, pci_name(dev));
459

460 461 462 463 464 465 466
	/* I have seen IDE devices which will not respond to
	 * the bmdma simplex check reads if bus mastering is
	 * disabled.
	 */
	if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
		pci_set_master(dev);

467 468 469
	dev->current_state = 4;		/* unknown power state */
	dev->error_state = pci_channel_io_normal;

470
	if (host_controller) {
471 472
		dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
		dev->rom_base_reg = PCI_ROM_ADDRESS1;
473
		dev->irq = PCI_IRQ_NONE;
474
	} else {
475 476 477 478 479 480 481 482 483
		if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
			/* a PCI-PCI bridge */
			dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
			dev->rom_base_reg = PCI_ROM_ADDRESS1;
		} else if (!strcmp(type, "cardbus")) {
			dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
		} else {
			dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
			dev->rom_base_reg = PCI_ROM_ADDRESS;
484

485 486 487 488
			dev->irq = sd->op->irqs[0];
			if (dev->irq == 0xffffffff)
				dev->irq = PCI_IRQ_NONE;
		}
489 490 491
	}
	pci_parse_of_addrs(sd->op, node, dev);

492 493
	if (ofpci_verbose)
		printk("    adding to system ...\n");
494 495 496 497 498 499

	pci_device_add(dev, bus);

	return dev;
}

500
static void __devinit apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
{
	u32 idx, first, last;

	first = 8;
	last = 0;
	for (idx = 0; idx < 8; idx++) {
		if ((map & (1 << idx)) != 0) {
			if (first > idx)
				first = idx;
			if (last < idx)
				last = idx;
		}
	}

	*first_p = first;
	*last_p = last;
}

519 520
static void pci_resource_adjust(struct resource *res,
				struct resource *root)
521 522 523 524 525
{
	res->start += root->start;
	res->end += root->start;
}

526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
/* For PCI bus devices which lack a 'ranges' property we interrogate
 * the config space values to set the resources, just like the generic
 * Linux PCI probing code does.
 */
static void __devinit pci_cfg_fake_ranges(struct pci_dev *dev,
					  struct pci_bus *bus,
					  struct pci_pbm_info *pbm)
{
	struct resource *res;
	u8 io_base_lo, io_limit_lo;
	u16 mem_base_lo, mem_limit_lo;
	unsigned long base, limit;

	pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
	pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
	base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
	limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;

	if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
		u16 io_base_hi, io_limit_hi;

		pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
		pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
		base |= (io_base_hi << 16);
		limit |= (io_limit_hi << 16);
	}

	res = bus->resource[0];
	if (base <= limit) {
		res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
		if (!res->start)
			res->start = base;
		if (!res->end)
			res->end = limit + 0xfff;
		pci_resource_adjust(res, &pbm->io_space);
	}

	pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
	pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
	base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
	limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;

	res = bus->resource[1];
	if (base <= limit) {
		res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
			      IORESOURCE_MEM);
		res->start = base;
		res->end = limit + 0xfffff;
		pci_resource_adjust(res, &pbm->mem_space);
	}

	pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
	pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
	base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
	limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;

	if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
		u32 mem_base_hi, mem_limit_hi;

		pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
		pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);

		/*
		 * Some bridges set the base > limit by default, and some
		 * (broken) BIOSes do not initialize them.  If we find
		 * this, just assume they are not being used.
		 */
		if (mem_base_hi <= mem_limit_hi) {
			base |= ((long) mem_base_hi) << 32;
			limit |= ((long) mem_limit_hi) << 32;
		}
	}

	res = bus->resource[2];
	if (base <= limit) {
		res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
			      IORESOURCE_MEM | IORESOURCE_PREFETCH);
		res->start = base;
		res->end = limit + 0xfffff;
		pci_resource_adjust(res, &pbm->mem_space);
	}
}

609 610 611
/* Cook up fake bus resources for SUNW,simba PCI bridges which lack
 * a proper 'ranges' property.
 */
612 613 614
static void __devinit apb_fake_ranges(struct pci_dev *dev,
				      struct pci_bus *bus,
				      struct pci_pbm_info *pbm)
615 616 617 618 619 620 621 622 623 624 625
{
	struct resource *res;
	u32 first, last;
	u8 map;

	pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
	apb_calc_first_last(map, &first, &last);
	res = bus->resource[0];
	res->start = (first << 21);
	res->end = (last << 21) + ((1 << 21) - 1);
	res->flags = IORESOURCE_IO;
626
	pci_resource_adjust(res, &pbm->io_space);
627 628 629 630 631 632 633

	pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
	apb_calc_first_last(map, &first, &last);
	res = bus->resource[1];
	res->start = (first << 21);
	res->end = (last << 21) + ((1 << 21) - 1);
	res->flags = IORESOURCE_MEM;
634
	pci_resource_adjust(res, &pbm->mem_space);
635 636
}

637 638 639
static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
				      struct device_node *node,
				      struct pci_bus *bus);
640 641 642

#define GET_64BIT(prop, i)	((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])

643 644 645
static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
					 struct device_node *node,
					 struct pci_dev *dev)
646 647 648
{
	struct pci_bus *bus;
	const u32 *busrange, *ranges;
649
	int len, i, simba;
650 651 652 653
	struct resource *res;
	unsigned int flags;
	u64 size;

654 655
	if (ofpci_verbose)
		printk("of_scan_pci_bridge(%s)\n", node->full_name);
656 657 658 659 660 661 662 663 664

	/* parse bus-range property */
	busrange = of_get_property(node, "bus-range", &len);
	if (busrange == NULL || len != 8) {
		printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
		       node->full_name);
		return;
	}
	ranges = of_get_property(node, "ranges", &len);
665
	simba = 0;
666
	if (ranges == NULL) {
667
		const char *model = of_get_property(node, "model", NULL);
668
		if (model && !strcmp(model, "SUNW,simba"))
669
			simba = 1;
670 671 672 673 674 675 676 677 678 679 680 681 682
	}

	bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
	if (!bus) {
		printk(KERN_ERR "Failed to create pci bus for %s\n",
		       node->full_name);
		return;
	}

	bus->primary = dev->bus->number;
	bus->subordinate = busrange[1];
	bus->bridge_ctl = 0;

683
	/* parse ranges property, or cook one up by hand for Simba */
684 685 686 687 688 689 690
	/* PCI #address-cells == 3 and #size-cells == 2 always */
	res = &dev->resource[PCI_BRIDGE_RESOURCES];
	for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
		res->flags = 0;
		bus->resource[i] = res;
		++res;
	}
691 692
	if (simba) {
		apb_fake_ranges(dev, bus, pbm);
693 694 695 696
		goto after_ranges;
	} else if (ranges == NULL) {
		pci_cfg_fake_ranges(dev, bus, pbm);
		goto after_ranges;
697
	}
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
	i = 1;
	for (; len >= 32; len -= 32, ranges += 8) {
		struct resource *root;

		flags = pci_parse_of_flags(ranges[0]);
		size = GET_64BIT(ranges, 6);
		if (flags == 0 || size == 0)
			continue;
		if (flags & IORESOURCE_IO) {
			res = bus->resource[0];
			if (res->flags) {
				printk(KERN_ERR "PCI: ignoring extra I/O range"
				       " for bridge %s\n", node->full_name);
				continue;
			}
			root = &pbm->io_space;
		} else {
			if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
				printk(KERN_ERR "PCI: too many memory ranges"
				       " for bridge %s\n", node->full_name);
				continue;
			}
			res = bus->resource[i];
			++i;
			root = &pbm->mem_space;
		}

		res->start = GET_64BIT(ranges, 1);
		res->end = res->start + size - 1;
		res->flags = flags;

		/* Another way to implement this would be to add an of_device
		 * layer routine that can calculate a resource for a given
		 * range property value in a PCI device.
		 */
733
		pci_resource_adjust(res, root);
734
	}
735
after_ranges:
736 737
	sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
		bus->number);
738 739
	if (ofpci_verbose)
		printk("    bus name: %s\n", bus->name);
740 741 742 743

	pci_of_scan_bus(pbm, node, bus);
}

744 745 746
static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
				      struct device_node *node,
				      struct pci_bus *bus)
747 748 749 750 751 752
{
	struct device_node *child;
	const u32 *reg;
	int reglen, devfn;
	struct pci_dev *dev;

753 754 755
	if (ofpci_verbose)
		printk("PCI: scan_bus[%s] bus no %d\n",
		       node->full_name, bus->number);
756 757 758

	child = NULL;
	while ((child = of_get_next_child(node, child)) != NULL) {
759 760
		if (ofpci_verbose)
			printk("  * %s\n", child->full_name);
761 762 763 764 765 766
		reg = of_get_property(child, "reg", &reglen);
		if (reg == NULL || reglen < 20)
			continue;
		devfn = (reg[0] >> 8) & 0xff;

		/* create a new pci_dev for this device */
767
		dev = of_create_pci_dev(pbm, child, bus, devfn, 0);
768 769
		if (!dev)
			continue;
770 771 772
		if (ofpci_verbose)
			printk("PCI: dev header type: %x\n",
			       dev->hdr_type);
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796

		if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
		    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
			of_scan_pci_bridge(pbm, child, dev);
	}
}

static ssize_t
show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
{
	struct pci_dev *pdev;
	struct device_node *dp;

	pdev = to_pci_dev(dev);
	dp = pdev->dev.archdata.prom_node;

	return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
}

static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);

static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
{
	struct pci_dev *dev;
797
	struct pci_bus *child_bus;
798 799 800 801 802 803 804 805 806 807 808 809
	int err;

	list_for_each_entry(dev, &bus->devices, bus_list) {
		/* we don't really care if we can create this file or
		 * not, but we need to assign the result of the call
		 * or the world will fall under alien invasion and
		 * everybody will be frozen on a spaceship ready to be
		 * eaten on alpha centauri by some green and jelly
		 * humanoid.
		 */
		err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
	}
810 811
	list_for_each_entry(child_bus, &bus->children, node)
		pci_bus_register_of_sysfs(child_bus);
812 813
}

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
int pci_host_bridge_read_pci_cfg(struct pci_bus *bus_dev,
				 unsigned int devfn,
				 int where, int size,
				 u32 *value)
{
	static u8 fake_pci_config[] = {
		0x8e, 0x10, /* Vendor: 0x108e (Sun) */
		0x00, 0x80, /* Device: 0x8000 (PBM) */
		0x46, 0x01, /* Command: 0x0146 (SERR, PARITY, MASTER, MEM) */
		0xa0, 0x22, /* Status: 0x02a0 (DEVSEL_MED, FB2B, 66MHZ) */
		0x00, 0x00, 0x00, 0x06, /* Class: 0x06000000 host bridge */
		0x00, /* Cacheline: 0x00 */
		0x40, /* Latency: 0x40 */
		0x00, /* Header-Type: 0x00 normal */
	};

	*value = 0;
	if (where >= 0 && where < sizeof(fake_pci_config) &&
	    (where + size) >= 0 &&
	    (where + size) < sizeof(fake_pci_config) &&
	    size <= sizeof(u32)) {
		while (size--) {
			*value <<= 8;
			*value |= fake_pci_config[where + size];
		}
	}

	return PCIBIOS_SUCCESSFUL;
}

int pci_host_bridge_write_pci_cfg(struct pci_bus *bus_dev,
				  unsigned int devfn,
				  int where, int size,
				  u32 value)
{
	return PCIBIOS_SUCCESSFUL;
}

852
struct pci_bus * __devinit pci_scan_one_pbm(struct pci_pbm_info *pbm)
853 854
{
	struct device_node *node = pbm->prom_node;
855
	struct pci_dev *host_pdev;
856 857 858 859 860
	struct pci_bus *bus;

	printk("PCI: Scanning PBM %s\n", node->full_name);

	/* XXX parent device? XXX */
861
	bus = pci_create_bus(NULL, pbm->pci_first_busno, pbm->pci_ops, pbm);
862 863 864 865 866 867 868 869 870 871 872
	if (!bus) {
		printk(KERN_ERR "Failed to create bus for %s\n",
		       node->full_name);
		return NULL;
	}
	bus->secondary = pbm->pci_first_busno;
	bus->subordinate = pbm->pci_last_busno;

	bus->resource[0] = &pbm->io_space;
	bus->resource[1] = &pbm->mem_space;

873 874 875 876
	/* Create the dummy host bridge and link it in.  */
	host_pdev = of_create_pci_dev(pbm, node, bus, 0x00, 1);
	bus->self = host_pdev;

877 878 879 880 881 882 883
	pci_of_scan_bus(pbm, node, bus);
	pci_bus_add_devices(bus);
	pci_bus_register_of_sysfs(bus);

	return bus;
}

L
Linus Torvalds 已提交
884 885
static void __init pci_scan_each_controller_bus(void)
{
886
	struct pci_pbm_info *pbm;
L
Linus Torvalds 已提交
887

888 889
	for (pbm = pci_pbm_root; pbm; pbm = pbm->next)
		pbm->scan_bus(pbm);
L
Linus Torvalds 已提交
890 891 892 893 894 895 896
}

extern void power_init(void);

static int __init pcibios_init(void)
{
	pci_controller_probe();
897
	if (pci_pbm_root == NULL)
L
Linus Torvalds 已提交
898 899 900 901 902 903 904 905 906 907 908 909 910
		return 0;

	pci_scan_each_controller_bus();

	isa_init();
	ebus_init();
	power_init();

	return 0;
}

subsys_initcall(pcibios_init);

911
void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
L
Linus Torvalds 已提交
912 913 914 915 916 917 918 919 920 921
{
	struct pci_pbm_info *pbm = pbus->sysdata;

	/* Generic PCI bus probing sets these to point at
	 * &io{port,mem}_resouce which is wrong for us.
	 */
	pbus->resource[0] = &pbm->io_space;
	pbus->resource[1] = &pbm->mem_space;
}

922
struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
L
Linus Torvalds 已提交
923 924
{
	struct pci_pbm_info *pbm = pdev->bus->sysdata;
925
	struct resource *root = NULL;
L
Linus Torvalds 已提交
926

927
	if (r->flags & IORESOURCE_IO)
L
Linus Torvalds 已提交
928
		root = &pbm->io_space;
929
	if (r->flags & IORESOURCE_MEM)
L
Linus Torvalds 已提交
930 931
		root = &pbm->mem_space;

932
	return root;
L
Linus Torvalds 已提交
933 934 935 936 937 938 939
}

void pcibios_update_irq(struct pci_dev *pdev, int irq)
{
}

void pcibios_align_resource(void *data, struct resource *res,
940
			    resource_size_t size, resource_size_t align)
L
Linus Torvalds 已提交
941 942 943
{
}

944
int pcibios_enable_device(struct pci_dev *dev, int mask)
L
Linus Torvalds 已提交
945
{
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
	u16 cmd, oldcmd;
	int i;

	pci_read_config_word(dev, PCI_COMMAND, &cmd);
	oldcmd = cmd;

	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
		struct resource *res = &dev->resource[i];

		/* Only set up the requested stuff */
		if (!(mask & (1<<i)))
			continue;

		if (res->flags & IORESOURCE_IO)
			cmd |= PCI_COMMAND_IO;
		if (res->flags & IORESOURCE_MEM)
			cmd |= PCI_COMMAND_MEMORY;
	}

	if (cmd != oldcmd) {
		printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
		       pci_name(dev), cmd);
                /* Enable the appropriate bits in the PCI command register.  */
		pci_write_config_word(dev, PCI_COMMAND, cmd);
	}
L
Linus Torvalds 已提交
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
	return 0;
}

void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
			     struct resource *res)
{
	struct pci_pbm_info *pbm = pdev->bus->sysdata;
	struct resource zero_res, *root;

	zero_res.start = 0;
	zero_res.end = 0;
	zero_res.flags = res->flags;

	if (res->flags & IORESOURCE_IO)
		root = &pbm->io_space;
	else
		root = &pbm->mem_space;

989
	pci_resource_adjust(&zero_res, root);
L
Linus Torvalds 已提交
990 991 992 993

	region->start = res->start - zero_res.start;
	region->end = res->end - zero_res.start;
}
994
EXPORT_SYMBOL(pcibios_resource_to_bus);
L
Linus Torvalds 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
			     struct pci_bus_region *region)
{
	struct pci_pbm_info *pbm = pdev->bus->sysdata;
	struct resource *root;

	res->start = region->start;
	res->end = region->end;

	if (res->flags & IORESOURCE_IO)
		root = &pbm->io_space;
	else
		root = &pbm->mem_space;

1010
	pci_resource_adjust(res, root);
L
Linus Torvalds 已提交
1011
}
1012
EXPORT_SYMBOL(pcibios_bus_to_resource);
L
Linus Torvalds 已提交
1013

1014
char * __devinit pcibios_setup(char *str)
L
Linus Torvalds 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
{
	return str;
}

/* Platform support for /proc/bus/pci/X/Y mmap()s. */

/* If the user uses a host-bridge as the PCI device, he may use
 * this to perform a raw mmap() of the I/O or MEM space behind
 * that controller.
 *
 * This can be useful for execution of x86 PCI bios initialization code
 * on a PCI card, like the xfree86 int10 stuff does.
 */
static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
				      enum pci_mmap_state mmap_state)
{
1031
	struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
L
Linus Torvalds 已提交
1032 1033
	unsigned long space_size, user_offset, user_size;

1034 1035 1036
	if (mmap_state == pci_mmap_io) {
		space_size = (pbm->io_space.end -
			      pbm->io_space.start) + 1;
L
Linus Torvalds 已提交
1037
	} else {
1038 1039
		space_size = (pbm->mem_space.end -
			      pbm->mem_space.start) + 1;
L
Linus Torvalds 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
	}

	/* Make sure the request is in range. */
	user_offset = vma->vm_pgoff << PAGE_SHIFT;
	user_size = vma->vm_end - vma->vm_start;

	if (user_offset >= space_size ||
	    (user_offset + user_size) > space_size)
		return -EINVAL;

1050 1051 1052
	if (mmap_state == pci_mmap_io) {
		vma->vm_pgoff = (pbm->io_space.start +
				 user_offset) >> PAGE_SHIFT;
L
Linus Torvalds 已提交
1053
	} else {
1054 1055
		vma->vm_pgoff = (pbm->mem_space.start +
				 user_offset) >> PAGE_SHIFT;
L
Linus Torvalds 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
	}

	return 0;
}

/* Adjust vm_pgoff of VMA such that it is the physical page offset corresponding
 * to the 32-bit pci bus offset for DEV requested by the user.
 *
 * Basically, the user finds the base address for his device which he wishes
 * to mmap.  They read the 32-bit value from the config space base register,
 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
 *
 * Returns negative error code on failure, zero on success.
 */
static int __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
				  enum pci_mmap_state mmap_state)
{
	unsigned long user_offset = vma->vm_pgoff << PAGE_SHIFT;
	unsigned long user32 = user_offset & pci_memspace_mask;
	unsigned long largest_base, this_base, addr32;
	int i;

	if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
		return __pci_mmap_make_offset_bus(dev, vma, mmap_state);

	/* Figure out which base address this is for. */
	largest_base = 0UL;
	for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
		struct resource *rp = &dev->resource[i];

		/* Active? */
		if (!rp->flags)
			continue;

		/* Same type? */
		if (i == PCI_ROM_RESOURCE) {
			if (mmap_state != pci_mmap_mem)
				continue;
		} else {
			if ((mmap_state == pci_mmap_io &&
			     (rp->flags & IORESOURCE_IO) == 0) ||
			    (mmap_state == pci_mmap_mem &&
			     (rp->flags & IORESOURCE_MEM) == 0))
				continue;
		}

		this_base = rp->start;

		addr32 = (this_base & PAGE_MASK) & pci_memspace_mask;

		if (mmap_state == pci_mmap_io)
			addr32 &= 0xffffff;

		if (addr32 <= user32 && this_base > largest_base)
			largest_base = this_base;
	}

	if (largest_base == 0UL)
		return -EINVAL;

	/* Now construct the final physical address. */
	if (mmap_state == pci_mmap_io)
		vma->vm_pgoff = (((largest_base & ~0xffffffUL) | user32) >> PAGE_SHIFT);
	else
		vma->vm_pgoff = (((largest_base & ~(pci_memspace_mask)) | user32) >> PAGE_SHIFT);

	return 0;
}

/* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
 * mapping.
 */
static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
					    enum pci_mmap_state mmap_state)
{
	vma->vm_flags |= (VM_IO | VM_RESERVED);
}

/* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
 * device mapping.
 */
static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
					     enum pci_mmap_state mmap_state)
{
1141
	/* Our io_remap_pfn_range takes care of this, do nothing.  */
L
Linus Torvalds 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
}

/* Perform the actual remap of the pages for a PCI device mapping, as appropriate
 * for this architecture.  The region in the process to map is described by vm_start
 * and vm_end members of VMA, the base physical address is found in vm_pgoff.
 * The pci device structure is provided so that architectures may make mapping
 * decisions on a per-device or per-bus basis.
 *
 * Returns a negative error code on failure, zero on success.
 */
int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
			enum pci_mmap_state mmap_state,
			int write_combine)
{
	int ret;

	ret = __pci_mmap_make_offset(dev, vma, mmap_state);
	if (ret < 0)
		return ret;

	__pci_mmap_set_flags(dev, vma, mmap_state);
	__pci_mmap_set_pgprot(dev, vma, mmap_state);

1165
	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
L
Linus Torvalds 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
	ret = io_remap_pfn_range(vma, vma->vm_start,
				 vma->vm_pgoff,
				 vma->vm_end - vma->vm_start,
				 vma->vm_page_prot);
	if (ret)
		return ret;

	return 0;
}

/* Return the domain nuber for this pci bus */

int pci_domain_nr(struct pci_bus *pbus)
{
	struct pci_pbm_info *pbm = pbus->sysdata;
	int ret;

	if (pbm == NULL || pbm->parent == NULL) {
		ret = -ENXIO;
	} else {
1186
		ret = pbm->index;
L
Linus Torvalds 已提交
1187 1188 1189 1190 1191 1192
	}

	return ret;
}
EXPORT_SYMBOL(pci_domain_nr);

1193 1194 1195
#ifdef CONFIG_PCI_MSI
int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
{
1196
	struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1197
	int virt_irq;
1198

1199
	if (!pbm->setup_msi_irq)
1200 1201
		return -EINVAL;

1202
	return pbm->setup_msi_irq(&virt_irq, pdev, desc);
1203 1204 1205 1206
}

void arch_teardown_msi_irq(unsigned int virt_irq)
{
1207
	struct msi_desc *entry = get_irq_msi(virt_irq);
1208
	struct pci_dev *pdev = entry->dev;
1209
	struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1210

1211
	if (!pbm->teardown_msi_irq)
1212 1213
		return;

1214
	return pbm->teardown_msi_irq(virt_irq, pdev);
1215 1216 1217
}
#endif /* !(CONFIG_PCI_MSI) */

1218 1219
struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
{
1220
	return pdev->dev.archdata.prom_node;
1221 1222 1223
}
EXPORT_SYMBOL(pci_device_to_OF_node);

L
Linus Torvalds 已提交
1224
#endif /* !(CONFIG_PCI) */