pci.c 26.5 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
#include <linux/export.h>
L
Linus Torvalds 已提交
12 13 14 15 16
#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
#include <linux/init.h>
D
David S. Miller 已提交
21 22
#include <linux/of.h>
#include <linux/of_device.h>
L
Linus Torvalds 已提交
23 24 25 26

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

30
#include "pci_impl.h"
31
#include "kernel.h"
32

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

36 37
/* Each PBM found gets a unique index. */
int pci_num_pbms = 0;
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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

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);
}

168 169 170 171 172 173 174 175 176 177 178 179 180 181
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);

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
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.
 */
202
static void pci_parse_of_addrs(struct platform_device *op,
203 204 205 206 207 208 209 210 211 212
			       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;
213 214 215
	if (ofpci_verbose)
		printk("    parse addresses (%d bytes) @ %p\n",
		       proplen, addrs);
216 217 218 219 220 221 222 223 224 225
	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;
226
		if (ofpci_verbose)
227
			printk("  start: %llx, end: %llx, i: %x\n",
228
			       op_res->start, op_res->end, i);
229 230 231 232 233

		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];
234 235
			flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE
			      | IORESOURCE_SIZEALIGN;
236 237 238 239 240 241 242 243 244 245 246
		} 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);
	}
}

247 248 249
static struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
					 struct device_node *node,
					 struct pci_bus *bus, int devfn)
250 251
{
	struct dev_archdata *sd;
252
	struct pci_slot *slot;
253
	struct platform_device *op;
254 255
	struct pci_dev *dev;
	const char *type;
256
	u32 class;
257

258
	dev = pci_alloc_dev(bus);
259 260 261 262 263 264 265
	if (!dev)
		return NULL;

	sd = &dev->dev.archdata;
	sd->iommu = pbm->iommu;
	sd->stc = &pbm->stc;
	sd->host_controller = pbm;
266
	sd->op = op = of_find_device_by_node(node);
267
	sd->numa_node = pbm->numa_node;
268

269
	sd = &op->dev.archdata;
270 271
	sd->iommu = pbm->iommu;
	sd->stc = &pbm->stc;
272
	sd->numa_node = pbm->numa_node;
273

274 275 276
	if (!strcmp(node->name, "ebus"))
		of_propagate_archdata(op);

277 278 279 280
	type = of_get_property(node, "device_type", NULL);
	if (type == NULL)
		type = "";

281 282 283
	if (ofpci_verbose)
		printk("    create device, devfn: %x, type: %s\n",
		       devfn, type);
284 285 286 287

	dev->sysdata = node;
	dev->dev.parent = bus->bridge;
	dev->dev.bus = &pci_bus_type;
288
	dev->dev.of_node = of_node_get(node);
289 290
	dev->devfn = devfn;
	dev->multifunction = 0;		/* maybe a lie? */
291 292 293 294 295
	set_pcie_port_type(dev);

	list_for_each_entry(slot, &dev->bus->slots, list)
		if (PCI_SLOT(dev->devfn) == slot->number)
			dev->slot = slot;
296

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	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);

	/* 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;
	dev->revision = class & 0xff;

316
	dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
317
		dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
318

319 320 321
	if (ofpci_verbose)
		printk("    class: 0x%x device name: %s\n",
		       dev->class, pci_name(dev));
322

323 324 325 326 327 328 329
	/* 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);

330
	dev->current_state = PCI_UNKNOWN;	/* unknown power state */
331
	dev->error_state = pci_channel_io_normal;
332
	dev->dma_mask = 0xffffffff;
333

334
	if (!strcmp(node->name, "pci")) {
335
		/* a PCI-PCI bridge */
336 337
		dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
		dev->rom_base_reg = PCI_ROM_ADDRESS1;
338 339
	} else if (!strcmp(type, "cardbus")) {
		dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
340
	} else {
341 342
		dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
		dev->rom_base_reg = PCI_ROM_ADDRESS;
343

344
		dev->irq = sd->op->archdata.irqs[0];
345 346
		if (dev->irq == 0xffffffff)
			dev->irq = PCI_IRQ_NONE;
347
	}
348

349 350
	pci_parse_of_addrs(sd->op, node, dev);

351 352
	if (ofpci_verbose)
		printk("    adding to system ...\n");
353 354 355 356 357 358

	pci_device_add(dev, bus);

	return dev;
}

359
static void apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
{
	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;
}

/* Cook up fake bus resources for SUNW,simba PCI bridges which lack
 * a proper 'ranges' property.
 */
381 382 383
static void apb_fake_ranges(struct pci_dev *dev,
			    struct pci_bus *bus,
			    struct pci_pbm_info *pbm)
384
{
385
	struct pci_bus_region region;
386 387 388 389 390 391 392 393
	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->flags = IORESOURCE_IO;
394 395
	region.start = (first << 21);
	region.end = (last << 21) + ((1 << 21) - 1);
396
	pcibios_bus_to_resource(dev->bus, res, &region);
397 398 399 400 401

	pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
	apb_calc_first_last(map, &first, &last);
	res = bus->resource[1];
	res->flags = IORESOURCE_MEM;
402 403
	region.start = (first << 29);
	region.end = (last << 29) + ((1 << 29) - 1);
404
	pcibios_bus_to_resource(dev->bus, res, &region);
405 406
}

407 408 409
static void pci_of_scan_bus(struct pci_pbm_info *pbm,
			    struct device_node *node,
			    struct pci_bus *bus);
410 411 412

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

413 414 415
static void of_scan_pci_bridge(struct pci_pbm_info *pbm,
			       struct device_node *node,
			       struct pci_dev *dev)
416 417 418
{
	struct pci_bus *bus;
	const u32 *busrange, *ranges;
419
	int len, i, simba;
420
	struct pci_bus_region region;
421 422 423 424
	struct resource *res;
	unsigned int flags;
	u64 size;

425 426
	if (ofpci_verbose)
		printk("of_scan_pci_bridge(%s)\n", node->full_name);
427 428 429 430 431 432 433 434

	/* 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;
	}
435 436 437 438 439

	if (ofpci_verbose)
		printk("    Bridge bus range [%u --> %u]\n",
		       busrange[0], busrange[1]);

440
	ranges = of_get_property(node, "ranges", &len);
441
	simba = 0;
442
	if (ranges == NULL) {
443
		const char *model = of_get_property(node, "model", NULL);
444
		if (model && !strcmp(model, "SUNW,simba"))
445
			simba = 1;
446 447 448 449 450 451 452 453 454 455
	}

	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;
456
	pci_bus_insert_busn_res(bus, busrange[0], busrange[1]);
457 458
	bus->bridge_ctl = 0;

459 460 461 462
	if (ofpci_verbose)
		printk("    Bridge ranges[%p] simba[%d]\n",
		       ranges, simba);

463
	/* parse ranges property, or cook one up by hand for Simba */
464 465 466 467 468 469 470
	/* 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;
	}
471 472
	if (simba) {
		apb_fake_ranges(dev, bus, pbm);
473 474
		goto after_ranges;
	} else if (ranges == NULL) {
475
		pci_read_bridge_bases(bus);
476
		goto after_ranges;
477
	}
478 479
	i = 1;
	for (; len >= 32; len -= 32, ranges += 8) {
480 481 482 483 484 485 486 487
		u64 start;

		if (ofpci_verbose)
			printk("    RAW Range[%08x:%08x:%08x:%08x:%08x:%08x:"
			       "%08x:%08x]\n",
			       ranges[0], ranges[1], ranges[2], ranges[3],
			       ranges[4], ranges[5], ranges[6], ranges[7]);

488 489 490 491
		flags = pci_parse_of_flags(ranges[0]);
		size = GET_64BIT(ranges, 6);
		if (flags == 0 || size == 0)
			continue;
492 493 494 495 496 497 498 499 500 501 502

		/* On PCI-Express systems, PCI bridges that have no devices downstream
		 * have a bogus size value where the first 32-bit cell is 0xffffffff.
		 * This results in a bogus range where start + size overflows.
		 *
		 * Just skip these otherwise the kernel will complain when the resource
		 * tries to be claimed.
		 */
		if (size >> 32 == 0xffffffff)
			continue;

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
		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;
			}
		} 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;
		}

		res->flags = flags;
521
		region.start = start = GET_64BIT(ranges, 1);
522
		region.end = region.start + size - 1;
523 524 525 526 527

		if (ofpci_verbose)
			printk("      Using flags[%08x] start[%016llx] size[%016llx]\n",
			       flags, start, size);

528
		pcibios_bus_to_resource(dev->bus, res, &region);
529
	}
530
after_ranges:
531 532
	sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
		bus->number);
533 534
	if (ofpci_verbose)
		printk("    bus name: %s\n", bus->name);
535 536 537 538

	pci_of_scan_bus(pbm, node, bus);
}

539 540 541
static void pci_of_scan_bus(struct pci_pbm_info *pbm,
			    struct device_node *node,
			    struct pci_bus *bus)
542 543 544
{
	struct device_node *child;
	const u32 *reg;
545
	int reglen, devfn, prev_devfn;
546 547
	struct pci_dev *dev;

548 549 550
	if (ofpci_verbose)
		printk("PCI: scan_bus[%s] bus no %d\n",
		       node->full_name, bus->number);
551 552

	child = NULL;
553
	prev_devfn = -1;
554
	while ((child = of_get_next_child(node, child)) != NULL) {
555 556
		if (ofpci_verbose)
			printk("  * %s\n", child->full_name);
557 558 559
		reg = of_get_property(child, "reg", &reglen);
		if (reg == NULL || reglen < 20)
			continue;
560

561 562
		devfn = (reg[0] >> 8) & 0xff;

563 564 565 566 567 568 569 570 571
		/* This is a workaround for some device trees
		 * which list PCI devices twice.  On the V100
		 * for example, device number 3 is listed twice.
		 * Once as "pm" and once again as "lomp".
		 */
		if (devfn == prev_devfn)
			continue;
		prev_devfn = devfn;

572
		/* create a new pci_dev for this device */
573
		dev = of_create_pci_dev(pbm, child, bus, devfn);
574 575
		if (!dev)
			continue;
576 577 578
		if (ofpci_verbose)
			printk("PCI: dev header type: %x\n",
			       dev->hdr_type);
579

580
		if (pci_is_bridge(dev))
581 582 583 584 585 586 587 588 589 590 591
			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);
592
	dp = pdev->dev.of_node;
593 594 595 596 597 598

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

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

599
static void pci_bus_register_of_sysfs(struct pci_bus *bus)
600 601
{
	struct pci_dev *dev;
602
	struct pci_bus *child_bus;
603 604 605 606 607 608 609 610 611 612 613
	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);
614
		(void) err;
615
	}
616 617
	list_for_each_entry(child_bus, &bus->children, node)
		pci_bus_register_of_sysfs(child_bus);
618 619
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
static void pci_claim_bus_resources(struct pci_bus *bus)
{
	struct pci_bus *child_bus;
	struct pci_dev *dev;

	list_for_each_entry(dev, &bus->devices, bus_list) {
		int i;

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

			if (r->parent || !r->start || !r->flags)
				continue;

			if (ofpci_verbose)
				printk("PCI: Claiming %s: "
				       "Resource %d: %016llx..%016llx [%x]\n",
				       pci_name(dev), i,
				       (unsigned long long)r->start,
				       (unsigned long long)r->end,
				       (unsigned int)r->flags);

642
			pci_claim_resource(dev, i);
643 644 645 646 647 648 649
		}
	}

	list_for_each_entry(child_bus, &bus->children, node)
		pci_claim_bus_resources(child_bus);
}

650 651
struct pci_bus *pci_scan_one_pbm(struct pci_pbm_info *pbm,
				 struct device *parent)
652
{
653
	LIST_HEAD(resources);
654
	struct device_node *node = pbm->op->dev.of_node;
655 656 657 658
	struct pci_bus *bus;

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

659 660 661 662
	pci_add_resource_offset(&resources, &pbm->io_space,
				pbm->io_space.start);
	pci_add_resource_offset(&resources, &pbm->mem_space,
				pbm->mem_space.start);
663 664 665 666
	pbm->busn.start = pbm->pci_first_busno;
	pbm->busn.end	= pbm->pci_last_busno;
	pbm->busn.flags	= IORESOURCE_BUS;
	pci_add_resource(&resources, &pbm->busn);
667 668
	bus = pci_create_root_bus(parent, pbm->pci_first_busno, pbm->pci_ops,
				  pbm, &resources);
669 670 671
	if (!bus) {
		printk(KERN_ERR "Failed to create bus for %s\n",
		       node->full_name);
672
		pci_free_resource_list(&resources);
673 674 675 676 677 678 679
		return NULL;
	}

	pci_of_scan_bus(pbm, node, bus);
	pci_bus_add_devices(bus);
	pci_bus_register_of_sysfs(bus);

680 681
	pci_claim_bus_resources(bus);

682 683 684
	return bus;
}

685
void pcibios_fixup_bus(struct pci_bus *pbus)
L
Linus Torvalds 已提交
686 687 688
{
}

689
resource_size_t pcibios_align_resource(void *data, const struct resource *res,
690
				resource_size_t size, resource_size_t align)
L
Linus Torvalds 已提交
691
{
692
	return res->start;
L
Linus Torvalds 已提交
693 694
}

695
int pcibios_enable_device(struct pci_dev *dev, int mask)
L
Linus Torvalds 已提交
696
{
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
	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 已提交
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
	return 0;
}

/* 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)
{
737
	struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
L
Linus Torvalds 已提交
738 739
	unsigned long space_size, user_offset, user_size;

740
	if (mmap_state == pci_mmap_io) {
741
		space_size = resource_size(&pbm->io_space);
L
Linus Torvalds 已提交
742
	} else {
743
		space_size = resource_size(&pbm->mem_space);
L
Linus Torvalds 已提交
744 745 746 747 748 749 750 751 752 753
	}

	/* 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;

754 755 756
	if (mmap_state == pci_mmap_io) {
		vma->vm_pgoff = (pbm->io_space.start +
				 user_offset) >> PAGE_SHIFT;
L
Linus Torvalds 已提交
757
	} else {
758 759
		vma->vm_pgoff = (pbm->mem_space.start +
				 user_offset) >> PAGE_SHIFT;
L
Linus Torvalds 已提交
760 761 762 763 764
	}

	return 0;
}

765 766
/* 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.
L
Linus Torvalds 已提交
767 768 769 770 771 772 773 774
 *
 * 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.
 */
775 776
static int __pci_mmap_make_offset(struct pci_dev *pdev,
				  struct vm_area_struct *vma,
L
Linus Torvalds 已提交
777 778
				  enum pci_mmap_state mmap_state)
{
779 780
	unsigned long user_paddr, user_size;
	int i, err;
L
Linus Torvalds 已提交
781

782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
	/* First compute the physical address in vma->vm_pgoff,
	 * making sure the user offset is within range in the
	 * appropriate PCI space.
	 */
	err = __pci_mmap_make_offset_bus(pdev, vma, mmap_state);
	if (err)
		return err;

	/* If this is a mapping on a host bridge, any address
	 * is OK.
	 */
	if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
		return err;

	/* Otherwise make sure it's in the range for one of the
	 * device's resources.
	 */
	user_paddr = vma->vm_pgoff << PAGE_SHIFT;
	user_size = vma->vm_end - vma->vm_start;
L
Linus Torvalds 已提交
801 802

	for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
803
		struct resource *rp = &pdev->resource[i];
804
		resource_size_t aligned_end;
L
Linus Torvalds 已提交
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

		/* 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;
		}

822 823 824 825 826 827 828
		/* Align the resource end to the next page address.
		 * PAGE_SIZE intentionally added instead of (PAGE_SIZE - 1),
		 * because actually we need the address of the next byte
		 * after rp->end.
		 */
		aligned_end = (rp->end + PAGE_SIZE) & PAGE_MASK;

829
		if ((rp->start <= user_paddr) &&
830
		    (user_paddr + user_size) <= aligned_end)
831
			break;
L
Linus Torvalds 已提交
832 833
	}

834
	if (i > PCI_ROM_RESOURCE)
L
Linus Torvalds 已提交
835 836 837 838 839 840 841 842 843 844 845
		return -EINVAL;

	return 0;
}

/* 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)
{
846
	/* Our io_remap_pfn_range takes care of this, do nothing.  */
L
Linus Torvalds 已提交
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
}

/* 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_pgprot(dev, vma, mmap_state);

869
	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
L
Linus Torvalds 已提交
870 871 872 873 874 875 876 877 878 879
	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;
}

880 881 882 883 884 885 886 887 888 889
#ifdef CONFIG_NUMA
int pcibus_to_node(struct pci_bus *pbus)
{
	struct pci_pbm_info *pbm = pbus->sysdata;

	return pbm->numa_node;
}
EXPORT_SYMBOL(pcibus_to_node);
#endif

890
/* Return the domain number for this pci bus */
L
Linus Torvalds 已提交
891 892 893 894 895 896

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

897
	if (!pbm) {
L
Linus Torvalds 已提交
898 899
		ret = -ENXIO;
	} else {
900
		ret = pbm->index;
L
Linus Torvalds 已提交
901 902 903 904 905 906
	}

	return ret;
}
EXPORT_SYMBOL(pci_domain_nr);

907 908 909
#ifdef CONFIG_PCI_MSI
int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
{
910
	struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
911
	unsigned int irq;
912

913
	if (!pbm->setup_msi_irq)
914 915
		return -EINVAL;

916
	return pbm->setup_msi_irq(&irq, pdev, desc);
917 918
}

919
void arch_teardown_msi_irq(unsigned int irq)
920
{
921
	struct msi_desc *entry = irq_get_msi_desc(irq);
922
	struct pci_dev *pdev = entry->dev;
923
	struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
924

925
	if (pbm->teardown_msi_irq)
926
		pbm->teardown_msi_irq(irq, pdev);
927 928 929
}
#endif /* !(CONFIG_PCI_MSI) */

930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
{
	struct pci_dev *ali_isa_bridge;
	u8 val;

	/* ALI sound chips generate 31-bits of DMA, a special register
	 * determines what bit 31 is emitted as.
	 */
	ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
					 PCI_DEVICE_ID_AL_M1533,
					 NULL);

	pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
	if (set_bit)
		val |= 0x01;
	else
		val &= ~0x01;
	pci_write_config_byte(ali_isa_bridge, 0x7e, val);
	pci_dev_put(ali_isa_bridge);
}

951
int pci64_dma_supported(struct pci_dev *pdev, u64 device_mask)
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
{
	u64 dma_addr_mask;

	if (pdev == NULL) {
		dma_addr_mask = 0xffffffff;
	} else {
		struct iommu *iommu = pdev->dev.archdata.iommu;

		dma_addr_mask = iommu->dma_addr_mask;

		if (pdev->vendor == PCI_VENDOR_ID_AL &&
		    pdev->device == PCI_DEVICE_ID_AL_M5451 &&
		    device_mask == 0x7fffffff) {
			ali_sound_dma_hack(pdev,
					   (dma_addr_mask & 0x80000000) != 0);
			return 1;
		}
	}

	if (device_mask >= (1UL << 32UL))
		return 0;

	return (device_mask & dma_addr_mask) == dma_addr_mask;
}

977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
void pci_resource_to_user(const struct pci_dev *pdev, int bar,
			  const struct resource *rp, resource_size_t *start,
			  resource_size_t *end)
{
	struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
	unsigned long offset;

	if (rp->flags & IORESOURCE_IO)
		offset = pbm->io_space.start;
	else
		offset = pbm->mem_space.start;

	*start = rp->start - offset;
	*end = rp->end - offset;
}
T
Tejun Heo 已提交
992

993 994 995 996 997
void pcibios_set_master(struct pci_dev *dev)
{
	/* No special bus mastering setup handling */
}

T
Tejun Heo 已提交
998 999 1000 1001 1002 1003
static int __init pcibios_init(void)
{
	pci_dfl_cache_line_size = 64 >> 2;
	return 0;
}
subsys_initcall(pcibios_init);
1004 1005

#ifdef CONFIG_SYSFS
1006
static void pci_bus_slot_names(struct device_node *node, struct pci_bus *bus)
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
{
	const struct pci_slot_names {
		u32	slot_mask;
		char	names[0];
	} *prop;
	const char *sp;
	int len, i;
	u32 mask;

	prop = of_get_property(node, "slot-names", &len);
	if (!prop)
		return;

	mask = prop->slot_mask;
	sp = prop->names;

	if (ofpci_verbose)
		printk("PCI: Making slots for [%s] mask[0x%02x]\n",
		       node->full_name, mask);

	i = 0;
	while (mask) {
		struct pci_slot *pci_slot;
		u32 this_bit = 1 << i;

		if (!(mask & this_bit)) {
			i++;
			continue;
		}

		if (ofpci_verbose)
			printk("PCI: Making slot [%s]\n", sp);

		pci_slot = pci_create_slot(bus, i, sp, NULL);
		if (IS_ERR(pci_slot))
			printk(KERN_ERR "PCI: pci_create_slot returned %ld\n",
			       PTR_ERR(pci_slot));

		sp += strlen(sp) + 1;
		mask &= ~this_bit;
		i++;
	}
}

static int __init of_pci_slot_init(void)
{
	struct pci_bus *pbus = NULL;

	while ((pbus = pci_find_next_bus(pbus)) != NULL) {
		struct device_node *node;

		if (pbus->self) {
			/* PCI->PCI bridge */
1060
			node = pbus->self->dev.of_node;
1061 1062 1063 1064
		} else {
			struct pci_pbm_info *pbm = pbus->sysdata;

			/* Host PCI controller */
1065
			node = pbm->op->dev.of_node;
1066 1067 1068 1069 1070 1071 1072
		}

		pci_bus_slot_names(node, pbus);
	}

	return 0;
}
1073
device_initcall(of_pci_slot_init);
1074
#endif