i386.c 11.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 *	Low-Level PCI Access for i386 machines
 *
 * Copyright 1993, 1994 Drew Eckhardt
 *      Visionary Computing
 *      (Unix and Linux consulting and custom programming)
 *      Drew@Colorado.EDU
 *      +1 (303) 786-7975
 *
 * Drew's work was sponsored by:
 *	iX Multiuser Multitasking Magazine
 *	Hannover, Germany
 *	hm@ix.de
 *
 * Copyright 1997--2000 Martin Mares <mj@ucw.cz>
 *
 * For more information, please consult the following manuals (look at
 * http://www.pcisig.com/ for how to get them):
 *
 * PCI BIOS Specification
 * PCI Local Bus Specification
 * PCI to PCI Bridge Specification
 * PCI System Design Guide
 *
 */

#include <linux/types.h>
#include <linux/kernel.h>
29
#include <linux/export.h>
L
Linus Torvalds 已提交
30 31 32 33
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/errno.h>
34 35 36
#include <linux/bootmem.h>

#include <asm/pat.h>
37
#include <asm/e820.h>
38
#include <asm/pci_x86.h>
39
#include <asm/io_apic.h>
L
Linus Torvalds 已提交
40 41


42 43 44 45 46 47 48 49 50 51 52 53
/*
 * This list of dynamic mappings is for temporarily maintaining
 * original BIOS BAR addresses for possible reinstatement.
 */
struct pcibios_fwaddrmap {
	struct list_head list;
	struct pci_dev *dev;
	resource_size_t fw_addr[DEVICE_COUNT_RESOURCE];
};

static LIST_HEAD(pcibios_fwaddrmappings);
static DEFINE_SPINLOCK(pcibios_fwaddrmap_lock);
54
static bool pcibios_fw_addr_done;
55 56 57 58 59 60

/* Must be called with 'pcibios_fwaddrmap_lock' lock held. */
static struct pcibios_fwaddrmap *pcibios_fwaddrmap_lookup(struct pci_dev *dev)
{
	struct pcibios_fwaddrmap *map;

61
	WARN_ON_SMP(!spin_is_locked(&pcibios_fwaddrmap_lock));
62

63 64 65 66 67 68 69 70 71 72 73 74 75
	list_for_each_entry(map, &pcibios_fwaddrmappings, list)
		if (map->dev == dev)
			return map;

	return NULL;
}

static void
pcibios_save_fw_addr(struct pci_dev *dev, int idx, resource_size_t fw_addr)
{
	unsigned long flags;
	struct pcibios_fwaddrmap *map;

76 77 78
	if (pcibios_fw_addr_done)
		return;

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
	spin_lock_irqsave(&pcibios_fwaddrmap_lock, flags);
	map = pcibios_fwaddrmap_lookup(dev);
	if (!map) {
		spin_unlock_irqrestore(&pcibios_fwaddrmap_lock, flags);
		map = kzalloc(sizeof(*map), GFP_KERNEL);
		if (!map)
			return;

		map->dev = pci_dev_get(dev);
		map->fw_addr[idx] = fw_addr;
		INIT_LIST_HEAD(&map->list);

		spin_lock_irqsave(&pcibios_fwaddrmap_lock, flags);
		list_add_tail(&map->list, &pcibios_fwaddrmappings);
	} else
		map->fw_addr[idx] = fw_addr;
	spin_unlock_irqrestore(&pcibios_fwaddrmap_lock, flags);
}

resource_size_t pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx)
{
	unsigned long flags;
	struct pcibios_fwaddrmap *map;
	resource_size_t fw_addr = 0;

104 105 106
	if (pcibios_fw_addr_done)
		return 0;

107 108 109 110 111 112 113 114 115
	spin_lock_irqsave(&pcibios_fwaddrmap_lock, flags);
	map = pcibios_fwaddrmap_lookup(dev);
	if (map)
		fw_addr = map->fw_addr[idx];
	spin_unlock_irqrestore(&pcibios_fwaddrmap_lock, flags);

	return fw_addr;
}

116
static void __init pcibios_fw_addr_list_del(void)
117 118 119 120 121 122 123 124 125 126 127
{
	unsigned long flags;
	struct pcibios_fwaddrmap *entry, *next;

	spin_lock_irqsave(&pcibios_fwaddrmap_lock, flags);
	list_for_each_entry_safe(entry, next, &pcibios_fwaddrmappings, list) {
		list_del(&entry->list);
		pci_dev_put(entry->dev);
		kfree(entry);
	}
	spin_unlock_irqrestore(&pcibios_fwaddrmap_lock, flags);
128
	pcibios_fw_addr_done = true;
129 130
}

131 132 133 134
static int
skip_isa_ioresource_align(struct pci_dev *dev) {

	if ((pci_probe & PCI_CAN_SKIP_ISA_ALIGN) &&
135
	    !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
136 137 138 139
		return 1;
	return 0;
}

L
Linus Torvalds 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152
/*
 * We need to avoid collisions with `mirrored' VGA ports
 * and other strange ISA hardware, so we always want the
 * addresses to be allocated in the 0x000-0x0ff region
 * modulo 0x400.
 *
 * Why? Because some silly external IO cards only decode
 * the low 10 bits of the IO address. The 0x00-0xff region
 * is reserved for motherboard devices that decode all 16
 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
 * but we want to try to avoid allocating at 0x2900-0x2bff
 * which might have be mirrored at 0x0100-0x03ff..
 */
153
resource_size_t
154
pcibios_align_resource(void *data, const struct resource *res,
155
			resource_size_t size, resource_size_t align)
L
Linus Torvalds 已提交
156
{
157
	struct pci_dev *dev = data;
158
	resource_size_t start = res->start;
159

L
Linus Torvalds 已提交
160
	if (res->flags & IORESOURCE_IO) {
161 162 163 164
		if (skip_isa_ioresource_align(dev))
			return start;
		if (start & 0x300)
			start = (start + 0x3ff) & ~0x3ff;
L
Linus Torvalds 已提交
165
	}
166
	return start;
L
Linus Torvalds 已提交
167
}
168
EXPORT_SYMBOL(pcibios_align_resource);
L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

/*
 *  Handle resources of PCI devices.  If the world were perfect, we could
 *  just allocate all the resource regions and do nothing more.  It isn't.
 *  On the other hand, we cannot just re-allocate all devices, as it would
 *  require us to know lots of host bridge internals.  So we attempt to
 *  keep as much of the original configuration as possible, but tweak it
 *  when it's found to be wrong.
 *
 *  Known BIOS problems we have to work around:
 *	- I/O or memory regions not configured
 *	- regions configured, but not enabled in the command register
 *	- bogus I/O addresses above 64K used
 *	- expansion ROMs left enabled (this may sound harmless, but given
 *	  the fact the PCI specs explicitly allow address decoders to be
 *	  shared between expansion ROMs and other resource regions, it's
 *	  at least dangerous)
186
 *	- bad resource sizes or overlaps with other regions
L
Linus Torvalds 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
 *
 *  Our solution:
 *	(1) Allocate resources for all buses behind PCI-to-PCI bridges.
 *	    This gives us fixed barriers on where we can allocate.
 *	(2) Allocate resources for all enabled devices.  If there is
 *	    a collision, just mark the resource as unallocated. Also
 *	    disable expansion ROMs during this step.
 *	(3) Try to allocate resources for disabled devices.  If the
 *	    resources were assigned correctly, everything goes well,
 *	    if they weren't, they won't disturb allocation of other
 *	    resources.
 *	(4) Assign new addresses to resources which were either
 *	    not configured at all or misconfigured.  If explicitly
 *	    requested by the user, configure expansion ROM address
 *	    as well.
 */

204
static void pcibios_allocate_bridge_resources(struct pci_dev *dev)
L
Linus Torvalds 已提交
205 206
{
	int idx;
M
Matthew Wilcox 已提交
207
	struct resource *r;
L
Linus Torvalds 已提交
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	for (idx = PCI_BRIDGE_RESOURCES; idx < PCI_NUM_RESOURCES; idx++) {
		r = &dev->resource[idx];
		if (!r->flags)
			continue;
		if (!r->start || pci_claim_resource(dev, idx) < 0) {
			/*
			 * Something is wrong with the region.
			 * Invalidate the resource to prevent
			 * child resource allocations in this
			 * range.
			 */
			r->start = r->end = 0;
			r->flags = 0;
		}
	}
}

226
static void pcibios_allocate_bus_resources(struct pci_bus *bus)
227
{
228
	struct pci_bus *child;
229

L
Linus Torvalds 已提交
230
	/* Depth-First Search on bus tree */
231 232 233 234
	if (bus->self)
		pcibios_allocate_bridge_resources(bus->self);
	list_for_each_entry(child, &bus->children, node)
		pcibios_allocate_bus_resources(child);
L
Linus Torvalds 已提交
235 236
}

237 238 239 240 241
struct pci_check_idx_range {
	int start;
	int end;
};

242
static void pcibios_allocate_dev_resources(struct pci_dev *dev, int pass)
L
Linus Torvalds 已提交
243
{
244
	int idx, disabled, i;
L
Linus Torvalds 已提交
245
	u16 command;
M
Matthew Wilcox 已提交
246
	struct resource *r;
L
Linus Torvalds 已提交
247

248 249 250 251 252 253 254
	struct pci_check_idx_range idx_range[] = {
		{ PCI_STD_RESOURCES, PCI_STD_RESOURCE_END },
#ifdef CONFIG_PCI_IOV
		{ PCI_IOV_RESOURCES, PCI_IOV_RESOURCE_END },
#endif
	};

255 256
	pci_read_config_word(dev, PCI_COMMAND, &command);
	for (i = 0; i < ARRAY_SIZE(idx_range); i++)
257
		for (idx = idx_range[i].start; idx <= idx_range[i].end; idx++) {
L
Linus Torvalds 已提交
258
			r = &dev->resource[idx];
259
			if (r->parent)	/* Already allocated */
L
Linus Torvalds 已提交
260
				continue;
261
			if (!r->start)	/* Address not assigned at all */
L
Linus Torvalds 已提交
262 263 264 265 266 267
				continue;
			if (r->flags & IORESOURCE_IO)
				disabled = !(command & PCI_COMMAND_IO);
			else
				disabled = !(command & PCI_COMMAND_MEMORY);
			if (pass == disabled) {
268
				dev_dbg(&dev->dev,
269
					"BAR %d: reserving %pr (d=%d, p=%d)\n",
270
					idx, r, disabled, pass);
M
Matthew Wilcox 已提交
271
				if (pci_claim_resource(dev, idx) < 0) {
L
Linus Torvalds 已提交
272
					/* We'll assign a new address later */
273 274
					pcibios_save_fw_addr(dev,
							idx, r->start);
L
Linus Torvalds 已提交
275 276 277 278 279
					r->end -= r->start;
					r->start = 0;
				}
			}
		}
280 281 282 283 284 285 286 287 288 289
	if (!pass) {
		r = &dev->resource[PCI_ROM_RESOURCE];
		if (r->flags & IORESOURCE_ROM_ENABLE) {
			/* Turn the ROM off, leave the resource region,
			 * but keep it unregistered. */
			u32 reg;
			dev_dbg(&dev->dev, "disabling ROM %pR\n", r);
			r->flags &= ~IORESOURCE_ROM_ENABLE;
			pci_read_config_dword(dev, dev->rom_base_reg, &reg);
			pci_write_config_dword(dev, dev->rom_base_reg,
R
Randy Dunlap 已提交
290
						reg & ~PCI_ROM_ADDRESS_ENABLE);
L
Linus Torvalds 已提交
291 292 293 294
		}
	}
}

295
static void pcibios_allocate_resources(struct pci_bus *bus, int pass)
296
{
297 298
	struct pci_dev *dev;
	struct pci_bus *child;
299

300
	list_for_each_entry(dev, &bus->devices, bus_list) {
301
		pcibios_allocate_dev_resources(dev, pass);
302 303 304 305 306

		child = dev->subordinate;
		if (child)
			pcibios_allocate_resources(child, pass);
	}
307 308
}

309
static void pcibios_allocate_dev_rom_resource(struct pci_dev *dev)
L
Linus Torvalds 已提交
310
{
M
Matthew Wilcox 已提交
311
	struct resource *r;
L
Linus Torvalds 已提交
312

313 314 315 316 317 318 319 320 321 322 323 324
	/*
	 * Try to use BIOS settings for ROMs, otherwise let
	 * pci_assign_unassigned_resources() allocate the new
	 * addresses.
	 */
	r = &dev->resource[PCI_ROM_RESOURCE];
	if (!r->flags || !r->start)
		return;

	if (pci_claim_resource(dev, PCI_ROM_RESOURCE) < 0) {
		r->end -= r->start;
		r->start = 0;
L
Linus Torvalds 已提交
325
	}
326
}
327
static void pcibios_allocate_rom_resources(struct pci_bus *bus)
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
{
	struct pci_dev *dev;
	struct pci_bus *child;

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

		child = dev->subordinate;
		if (child)
			pcibios_allocate_rom_resources(child);
	}
}

static int __init pcibios_assign_resources(void)
{
	struct pci_bus *bus;

	if (!(pci_probe & PCI_ASSIGN_ROMS))
		list_for_each_entry(bus, &pci_root_buses, node)
			pcibios_allocate_rom_resources(bus);
348 349

	pci_assign_unassigned_resources();
350
	pcibios_fw_addr_list_del();
351

L
Linus Torvalds 已提交
352 353 354 355 356
	return 0;
}

void __init pcibios_resource_survey(void)
{
357 358
	struct pci_bus *bus;

L
Linus Torvalds 已提交
359
	DBG("PCI: Allocating resources\n");
360 361 362 363 364 365 366 367

	list_for_each_entry(bus, &pci_root_buses, node)
		pcibios_allocate_bus_resources(bus);

	list_for_each_entry(bus, &pci_root_buses, node)
		pcibios_allocate_resources(bus, 0);
	list_for_each_entry(bus, &pci_root_buses, node)
		pcibios_allocate_resources(bus, 1);
368 369

	e820_reserve_resources_late();
370 371
	/*
	 * Insert the IO APIC resources after PCI initialization has
L
Lucas De Marchi 已提交
372
	 * occurred to handle IO APICS that are mapped in on a BAR in
373 374 375
	 * PCI space, but before trying to assign unassigned pci res.
	 */
	ioapic_insert_resources();
L
Linus Torvalds 已提交
376 377 378 379 380 381 382 383
}

/**
 * called in fs_initcall (one below subsys_initcall),
 * give a chance for motherboard reserve resources
 */
fs_initcall(pcibios_assign_resources);

384
static const struct vm_operations_struct pci_mmap_ops = {
385
	.access = generic_access_phys,
386 387
};

L
Linus Torvalds 已提交
388 389 390 391 392 393 394 395 396 397 398 399
int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
			enum pci_mmap_state mmap_state, int write_combine)
{
	unsigned long prot;

	/* I/O space cannot be accessed via normal processor loads and
	 * stores on this platform.
	 */
	if (mmap_state == pci_mmap_io)
		return -EINVAL;

	prot = pgprot_val(vma->vm_page_prot);
400 401 402 403 404 405 406 407 408

	/*
 	 * Return error if pat is not enabled and write_combine is requested.
 	 * Caller can followup with UC MINUS request and add a WC mtrr if there
 	 * is a free mtrr slot.
 	 */
	if (!pat_enabled && write_combine)
		return -EINVAL;

409
	if (pat_enabled && write_combine)
410
		prot |= _PAGE_CACHE_WC;
411
	else if (pat_enabled || boot_cpu_data.x86 > 3)
412 413 414
		/*
		 * ioremap() and ioremap_nocache() defaults to UC MINUS for now.
		 * To avoid attribute conflicts, request UC MINUS here
L
Lucas De Marchi 已提交
415
		 * as well.
416 417
		 */
		prot |= _PAGE_CACHE_UC_MINUS;
418

419 420
	prot |= _PAGE_IOMAP;	/* creating a mapping for IO */

L
Linus Torvalds 已提交
421 422
	vma->vm_page_prot = __pgprot(prot);

423 424 425
	if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
			       vma->vm_end - vma->vm_start,
			       vma->vm_page_prot))
L
Linus Torvalds 已提交
426 427
		return -EAGAIN;

428 429
	vma->vm_ops = &pci_mmap_ops;

L
Linus Torvalds 已提交
430 431
	return 0;
}