pci.c 12.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *	linux/arch/alpha/kernel/pci.c
 *
 * Extruded from code written by
 *	Dave Rusling (david.rusling@reo.mts.dec.com)
 *	David Mosberger (davidm@cs.arizona.edu)
 */

/* 2.3.x PCI/resources, 1999 Andrea Arcangeli <andrea@suse.de> */

/*
 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
 *	     PCI-PCI bridges cleanup
 */
15
#include <linux/config.h>
Linus Torvalds's avatar
Linus Torvalds committed
16 17 18 19 20 21
#include <linux/string.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/kernel.h>
#include <linux/bootmem.h>
22
#include <linux/module.h>
23
#include <linux/cache.h>
24
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <asm/machvec.h>

#include "proto.h"
#include "pci_impl.h"


/*
 * Some string constants used by the various core logics. 
 */

const char *const pci_io_names[] = {
  "PCI IO bus 0", "PCI IO bus 1", "PCI IO bus 2", "PCI IO bus 3",
  "PCI IO bus 4", "PCI IO bus 5", "PCI IO bus 6", "PCI IO bus 7"
};

const char *const pci_mem_names[] = {
  "PCI mem bus 0", "PCI mem bus 1", "PCI mem bus 2", "PCI mem bus 3",
  "PCI mem bus 4", "PCI mem bus 5", "PCI mem bus 6", "PCI mem bus 7"
};

const char pci_hae0_name[] = "HAE0";

47
/* Indicate whether we respect the PCI setup left by console. */
48 49 50 51 52
/*
 * Make this long-lived  so that we know when shutting down
 * whether we probed only or not.
 */
int pci_probe_only;
Linus Torvalds's avatar
Linus Torvalds committed
53 54

/*
Linus Torvalds's avatar
Linus Torvalds committed
55
 * The PCI controller list.
Linus Torvalds's avatar
Linus Torvalds committed
56 57
 */

Linus Torvalds's avatar
Linus Torvalds committed
58 59
struct pci_controller *hose_head, **hose_tail = &hose_head;
struct pci_controller *pci_isa_hose;
Linus Torvalds's avatar
Linus Torvalds committed
60 61 62 63 64 65 66 67 68 69

/*
 * Quirks.
 */

static void __init
quirk_isa_bridge(struct pci_dev *dev)
{
	dev->class = PCI_CLASS_BRIDGE_ISA << 8;
}
70
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82378, quirk_isa_bridge);
Linus Torvalds's avatar
Linus Torvalds committed
71

Linus Torvalds's avatar
Linus Torvalds committed
72 73 74
static void __init
quirk_cypress(struct pci_dev *dev)
{
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77 78 79 80 81
	/* The Notorious Cy82C693 chip.  */

	/* The Cypress IDE controller doesn't support native mode, but it
	   has programmable addresses of IDE command/control registers.
	   This violates PCI specifications, confuses the IDE subsystem and
	   causes resource conflicts between the primary HD_CMD register and
	   the floppy controller.  Ugh.  Fix that.  */
Linus Torvalds's avatar
Linus Torvalds committed
82 83 84 85
	if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE) {
		dev->resource[0].flags = 0;
		dev->resource[1].flags = 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
86 87 88

	/* The Cypress bridge responds on the PCI bus in the address range
	   0xffff0000-0xffffffff (conventional x86 BIOS ROM).  There is no
Linus Torvalds's avatar
Linus Torvalds committed
89 90 91
	   way to turn this off.  The bridge also supports several extended
	   BIOS ranges (disabled after power-up), and some consoles do turn
	   them on.  So if we use a large direct-map window, or a large SG
Ivan Kokshaysky's avatar
Ivan Kokshaysky committed
92
	   window, we must avoid the entire 0xfff00000-0xffffffff region.  */
Linus Torvalds's avatar
Linus Torvalds committed
93
	else if (dev->class >> 8 == PCI_CLASS_BRIDGE_ISA) {
Ivan Kokshaysky's avatar
Ivan Kokshaysky committed
94
		if (__direct_map_base + __direct_map_size >= 0xfff00000UL)
Ivan Kokshaysky's avatar
Ivan Kokshaysky committed
95
			__direct_map_size = 0xfff00000UL - __direct_map_base;
Ivan Kokshaysky's avatar
Ivan Kokshaysky committed
96
		else {
Linus Torvalds's avatar
Linus Torvalds committed
97 98
			struct pci_controller *hose = dev->sysdata;
			struct pci_iommu_arena *pci = hose->sg_pci;
Ivan Kokshaysky's avatar
Ivan Kokshaysky committed
99
			if (pci && pci->dma_base + pci->size >= 0xfff00000UL)
Ivan Kokshaysky's avatar
Ivan Kokshaysky committed
100
				pci->size = 0xfff00000UL - pci->dma_base;
Linus Torvalds's avatar
Linus Torvalds committed
101
		}
Linus Torvalds's avatar
Linus Torvalds committed
102 103
	}
}
104
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693, quirk_cypress);
Linus Torvalds's avatar
Linus Torvalds committed
105

106 107 108 109 110 111
/* Called for each device after PCI setup is done. */
static void __init
pcibios_fixup_final(struct pci_dev *dev)
{
	unsigned int class = dev->class >> 8;

112
	if (class == PCI_CLASS_BRIDGE_ISA || class == PCI_CLASS_BRIDGE_EISA) {
113 114 115 116
		dev->dma_mask = MAX_ISA_DMA_ADDRESS - 1;
		isa_bridge = dev;
	}
}
117
DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_final);
118

119 120
/* Just declaring that the power-of-ten prefixes are actually the
   power-of-two ones doesn't make it true :) */
Linus Torvalds's avatar
Linus Torvalds committed
121 122 123 124 125
#define KB			1024
#define MB			(1024*KB)
#define GB			(1024*MB)

void
126 127
pcibios_align_resource(void *data, struct resource *res,
		       unsigned long size, unsigned long align)
Linus Torvalds's avatar
Linus Torvalds committed
128 129
{
	struct pci_dev *dev = data;
Linus Torvalds's avatar
Linus Torvalds committed
130
	struct pci_controller *hose = dev->sysdata;
Linus Torvalds's avatar
Linus Torvalds committed
131 132 133 134 135 136 137 138 139 140 141
	unsigned long alignto;
	unsigned long start = res->start;

	if (res->flags & IORESOURCE_IO) {
		/* Make sure we start at our min on all hoses */
		if (start - hose->io_space->start < PCIBIOS_MIN_IO)
			start = PCIBIOS_MIN_IO + hose->io_space->start;

		/*
		 * Put everything into 0x00-0xff region modulo 0x400
		 */
Linus Torvalds's avatar
Linus Torvalds committed
142
		if (start & 0x300)
Linus Torvalds's avatar
Linus Torvalds committed
143 144 145 146 147
			start = (start + 0x3ff) & ~0x3ff;
	}
	else if	(res->flags & IORESOURCE_MEM) {
		/* Make sure we start at our min on all hoses */
		if (start - hose->mem_space->start < PCIBIOS_MIN_MEM)
Linus Torvalds's avatar
Linus Torvalds committed
148
			start = PCIBIOS_MIN_MEM + hose->mem_space->start;
Linus Torvalds's avatar
Linus Torvalds committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

		/*
		 * The following holds at least for the Low Cost
		 * Alpha implementation of the PCI interface:
		 *
		 * In sparse memory address space, the first
		 * octant (16MB) of every 128MB segment is
		 * aliased to the very first 16 MB of the
		 * address space (i.e., it aliases the ISA
		 * memory address space).  Thus, we try to
		 * avoid allocating PCI devices in that range.
		 * Can be allocated in 2nd-7th octant only.
		 * Devices that need more than 112MB of
		 * address space must be accessed through
		 * dense memory space only!
		 */

		/* Align to multiple of size of minimum base.  */
167
		alignto = max(0x1000UL, align);
Linus Torvalds's avatar
Linus Torvalds committed
168
		start = ALIGN(start, alignto);
Linus Torvalds's avatar
Linus Torvalds committed
169
		if (hose->sparse_mem_base && size <= 7 * 16*MB) {
Linus Torvalds's avatar
Linus Torvalds committed
170 171 172 173 174
			if (((start / (16*MB)) & 0x7) == 0) {
				start &= ~(128*MB - 1);
				start += 16*MB;
				start  = ALIGN(start, alignto);
			}
Linus Torvalds's avatar
Linus Torvalds committed
175
			if (start/(128*MB) != (start + size - 1)/(128*MB)) {
Linus Torvalds's avatar
Linus Torvalds committed
176 177 178 179 180 181 182 183 184 185 186 187 188
				start &= ~(128*MB - 1);
				start += (128 + 16)*MB;
				start  = ALIGN(start, alignto);
			}
		}
	}

	res->start = start;
}
#undef KB
#undef MB
#undef GB

189
static int __init
Linus Torvalds's avatar
Linus Torvalds committed
190 191
pcibios_init(void)
{
192 193 194
	if (alpha_mv.init_pci)
		alpha_mv.init_pci();
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
195 196
}

197 198
subsys_initcall(pcibios_init);

Linus Torvalds's avatar
Linus Torvalds committed
199 200 201 202 203 204
char * __init
pcibios_setup(char *str)
{
	return str;
}

205 206 207 208 209 210 211 212 213 214 215 216 217 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 248 249 250
#ifdef ALPHA_RESTORE_SRM_SETUP
static struct pdev_srm_saved_conf *srm_saved_configs;

void __init
pdev_save_srm_config(struct pci_dev *dev)
{
	struct pdev_srm_saved_conf *tmp;
	static int printed = 0;

	if (!alpha_using_srm || pci_probe_only)
		return;

	if (!printed) {
		printk(KERN_INFO "pci: enabling save/restore of SRM state\n");
		printed = 1;
	}

	tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
	if (!tmp) {
		printk(KERN_ERR "%s: kmalloc() failed!\n", __FUNCTION__);
		return;
	}
	tmp->next = srm_saved_configs;
	tmp->dev = dev;

	pci_save_state(dev, tmp->regs);

	srm_saved_configs = tmp;
}

void
pci_restore_srm_config(void)
{
	struct pdev_srm_saved_conf *tmp;

	/* No need to restore if probed only. */
	if (pci_probe_only)
		return;

	/* Restore SRM config. */
	for (tmp = srm_saved_configs; tmp; tmp = tmp->next) {
		pci_restore_state(tmp->dev, tmp->regs);
	}
}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
251 252 253 254 255 256 257 258 259 260 261
void __init
pcibios_fixup_resource(struct resource *res, struct resource *root)
{
	res->start += root->start;
	res->end += root->start;
}

void __init
pcibios_fixup_device_resources(struct pci_dev *dev, struct pci_bus *bus)
{
	/* Update device resources.  */
Linus Torvalds's avatar
Linus Torvalds committed
262
	struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
Linus Torvalds's avatar
Linus Torvalds committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	int i;

	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
		if (!dev->resource[i].start)
			continue;
		if (dev->resource[i].flags & IORESOURCE_IO)
			pcibios_fixup_resource(&dev->resource[i],
					       hose->io_space);
		else if (dev->resource[i].flags & IORESOURCE_MEM)
			pcibios_fixup_resource(&dev->resource[i],
					       hose->mem_space);
	}
}

void __init
pcibios_fixup_bus(struct pci_bus *bus)
{
Alan Cox's avatar
Alan Cox committed
280
	/* Propagate hose info into the subordinate devices.  */
Linus Torvalds's avatar
Linus Torvalds committed
281

Linus Torvalds's avatar
Linus Torvalds committed
282
	struct pci_controller *hose = bus->sysdata;
Linus Torvalds's avatar
Linus Torvalds committed
283 284 285 286
	struct list_head *ln;
	struct pci_dev *dev = bus->self;

	if (!dev) {
Ivan Kokshaysky's avatar
Ivan Kokshaysky committed
287 288 289 290 291
		/* Root bus. */
		u32 pci_mem_end;
		u32 sg_base = hose->sg_pci ? hose->sg_pci->dma_base : ~0;
		unsigned long end;

Linus Torvalds's avatar
Linus Torvalds committed
292 293
		bus->resource[0] = hose->io_space;
		bus->resource[1] = hose->mem_space;
Ivan Kokshaysky's avatar
Ivan Kokshaysky committed
294 295 296 297 298 299 300

		/* Adjust hose mem_space limit to prevent PCI allocations
		   in the iommu windows. */
		pci_mem_end = min((u32)__direct_map_base, sg_base) - 1;
		end = hose->mem_space->start + pci_mem_end;
		if (hose->mem_space->end > end)
			hose->mem_space->end = end;
301 302 303 304 305
 	} else if (pci_probe_only &&
 		   (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
 		pci_read_bridge_bases(bus);
 		pcibios_fixup_device_resources(dev, bus);
	} 
Linus Torvalds's avatar
Linus Torvalds committed
306 307 308

	for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
		struct pci_dev *dev = pci_dev_b(ln);
309 310

		pdev_save_srm_config(dev);
Linus Torvalds's avatar
Linus Torvalds committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
			pcibios_fixup_device_resources(dev, bus);
	}
}

void __init
pcibios_update_irq(struct pci_dev *dev, int irq)
{
	pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
}

/* Most Alphas have straight-forward swizzling needs.  */

u8 __init
common_swizzle(struct pci_dev *dev, u8 *pinp)
{
327
	u8 pin = *pinp;
Linus Torvalds's avatar
Linus Torvalds committed
328

329 330 331 332 333 334
	while (dev->bus->parent) {
		pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
		/* Move up the chain of bridges. */
		dev = dev->bus->self;
        }
	*pinp = pin;
Linus Torvalds's avatar
Linus Torvalds committed
335

336
	/* The slot is the slot of the last bridge. */
Linus Torvalds's avatar
Linus Torvalds committed
337 338 339
	return PCI_SLOT(dev->devfn);
}

340
void __devinit
341 342
pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
			 struct resource *res)
Linus Torvalds's avatar
Linus Torvalds committed
343
{
344 345 346 347 348 349 350
	struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
	unsigned long offset = 0;

	if (res->flags & IORESOURCE_IO)
		offset = hose->io_space->start;
	else if (res->flags & IORESOURCE_MEM)
		offset = hose->mem_space->start;
Linus Torvalds's avatar
Linus Torvalds committed
351

352 353
	region->start = res->start - offset;
	region->end = res->end - offset;
Linus Torvalds's avatar
Linus Torvalds committed
354 355
}

356 357 358 359
#ifdef CONFIG_HOTPLUG
EXPORT_SYMBOL(pcibios_resource_to_bus);
#endif

Linus Torvalds's avatar
Linus Torvalds committed
360
int
Ivan Kokshaysky's avatar
Ivan Kokshaysky committed
361
pcibios_enable_device(struct pci_dev *dev, int mask)
Linus Torvalds's avatar
Linus Torvalds committed
362
{
Ivan Kokshaysky's avatar
Ivan Kokshaysky committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	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];

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

	if (cmd != oldcmd) {
		printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
380
		       pci_name(dev), cmd);
Ivan Kokshaysky's avatar
Ivan Kokshaysky committed
381 382 383
		/* Enable the appropriate bits in the PCI command register.  */
		pci_write_config_word(dev, PCI_COMMAND, cmd);
	}
Linus Torvalds's avatar
Linus Torvalds committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	return 0;
}

/*
 *  If we set up a device for bus mastering, we need to check the latency
 *  timer as certain firmware forgets to set it properly, as seen
 *  on SX164 and LX164 with SRM.
 */
void
pcibios_set_master(struct pci_dev *dev)
{
	u8 lat;
	pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
	if (lat >= 16) return;
	printk("PCI: Setting latency timer of device %s to 64\n",
399
							pci_name(dev));
Linus Torvalds's avatar
Linus Torvalds committed
400 401 402
	pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
}

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
static void __init
pcibios_claim_one_bus(struct pci_bus *b)
{
	struct list_head *ld;
	struct pci_bus *child_bus;

	for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
		struct pci_dev *dev = pci_dev_b(ld);
		int i;

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

			if (r->parent || !r->start || !r->flags)
				continue;
			pci_claim_resource(dev, i);
		}
	}

	list_for_each_entry(child_bus, &b->children, node)
		pcibios_claim_one_bus(child_bus);
}

426 427 428 429 430 431 432
static void __init
pcibios_claim_console_setup(void)
{
	struct list_head *lb;

	for(lb = pci_root_buses.next; lb != &pci_root_buses; lb = lb->next) {
		struct pci_bus *b = pci_bus_b(lb);
433
		pcibios_claim_one_bus(b);
434 435 436
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
437 438 439
void __init
common_init_pci(void)
{
Linus Torvalds's avatar
Linus Torvalds committed
440
	struct pci_controller *hose;
Linus Torvalds's avatar
Linus Torvalds committed
441 442
	struct pci_bus *bus;
	int next_busno;
443
	int need_domain_info = 0;
Linus Torvalds's avatar
Linus Torvalds committed
444

Linus Torvalds's avatar
Linus Torvalds committed
445
	/* Scan all of the recorded PCI controllers.  */
Linus Torvalds's avatar
Linus Torvalds committed
446 447 448
	for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
		bus = pci_scan_bus(next_busno, alpha_mv.pci_ops, hose);
		hose->bus = bus;
449 450 451 452 453 454 455 456
		hose->need_domain_info = need_domain_info;
		next_busno = bus->subordinate + 1;
		/* Don't allow 8-bit bus number overflow inside the hose -
		   reserve some space for bridges. */ 
		if (next_busno > 224) {
			next_busno = 0;
			need_domain_info = 1;
		}
Linus Torvalds's avatar
Linus Torvalds committed
457 458
	}

459 460
	if (pci_probe_only)
		pcibios_claim_console_setup();
Ivan Kokshaysky's avatar
Ivan Kokshaysky committed
461 462

	pci_assign_unassigned_resources();
Linus Torvalds's avatar
Linus Torvalds committed
463 464 465 466
	pci_fixup_irqs(alpha_mv.pci_swizzle, alpha_mv.pci_map_irq);
}


Linus Torvalds's avatar
Linus Torvalds committed
467 468
struct pci_controller * __init
alloc_pci_controller(void)
Linus Torvalds's avatar
Linus Torvalds committed
469
{
Linus Torvalds's avatar
Linus Torvalds committed
470
	struct pci_controller *hose;
Linus Torvalds's avatar
Linus Torvalds committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496

	hose = alloc_bootmem(sizeof(*hose));

	*hose_tail = hose;
	hose_tail = &hose->next;

	return hose;
}

struct resource * __init
alloc_resource(void)
{
	struct resource *res;

	res = alloc_bootmem(sizeof(*res));

	return res;
}


/* Provide information on locations of various I/O regions in physical
   memory.  Do this on a per-card basis so that we choose the right hose.  */

asmlinkage long
sys_pciconfig_iobase(long which, unsigned long bus, unsigned long dfn)
{
Linus Torvalds's avatar
Linus Torvalds committed
497
	struct pci_controller *hose;
Linus Torvalds's avatar
Linus Torvalds committed
498 499 500 501 502 503 504 505
	struct pci_dev *dev;

	/* from hose or from bus.devfn */
	if (which & IOBASE_FROM_HOSE) {
		for(hose = hose_head; hose; hose = hose->next) 
			if (hose->index == bus) break;
		if (!hose) return -ENODEV;
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
506 507 508 509 510 511 512 513 514
		/* Special hook for ISA access.  */
		if (bus == 0 && dfn == 0) {
			hose = pci_isa_hose;
		} else {
			dev = pci_find_slot(bus, dfn);
			if (!dev)
				return -ENODEV;
			hose = dev->sysdata;
		}
Linus Torvalds's avatar
Linus Torvalds committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
	}

	switch (which & ~IOBASE_FROM_HOSE) {
	case IOBASE_HOSE:
		return hose->index;
	case IOBASE_SPARSE_MEM:
		return hose->sparse_mem_base;
	case IOBASE_DENSE_MEM:
		return hose->dense_mem_base;
	case IOBASE_SPARSE_IO:
		return hose->sparse_io_base;
	case IOBASE_DENSE_IO:
		return hose->dense_io_base;
	case IOBASE_ROOT_BUS:
		return hose->bus->number;
	}

	return -EOPNOTSUPP;
}