ioremap.c 15.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8
/*
 * Re-map IO memory to kernel address space so that we can access it.
 * This is needed for high PCI addresses that aren't mapped in the
 * 640k-1MB IO memory area on PC's
 *
 * (C) Copyright 1995 1996 Linus Torvalds
 */

9
#include <linux/bootmem.h>
L
Linus Torvalds 已提交
10
#include <linux/init.h>
11
#include <linux/io.h>
T
Thomas Gleixner 已提交
12 13 14
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
P
Pekka Paalanen 已提交
15
#include <linux/mmiotrace.h>
T
Thomas Gleixner 已提交
16

L
Linus Torvalds 已提交
17
#include <asm/cacheflush.h>
T
Thomas Gleixner 已提交
18 19
#include <asm/e820.h>
#include <asm/fixmap.h>
L
Linus Torvalds 已提交
20
#include <asm/pgtable.h>
T
Thomas Gleixner 已提交
21
#include <asm/tlbflush.h>
22
#include <asm/pgalloc.h>
23
#include <asm/pat.h>
L
Linus Torvalds 已提交
24

T
Thomas Gleixner 已提交
25 26 27 28 29 30 31 32 33 34
#ifdef CONFIG_X86_64

unsigned long __phys_addr(unsigned long x)
{
	if (x >= __START_KERNEL_map)
		return x - __START_KERNEL_map + phys_base;
	return x - PAGE_OFFSET;
}
EXPORT_SYMBOL(__phys_addr);

35 36 37 38 39 40 41 42 43 44 45 46
static inline int phys_addr_valid(unsigned long addr)
{
	return addr < (1UL << boot_cpu_data.x86_phys_bits);
}

#else

static inline int phys_addr_valid(unsigned long addr)
{
	return 1;
}

T
Thomas Gleixner 已提交
47 48
#endif

49 50
int page_is_ram(unsigned long pagenr)
{
51
	resource_size_t addr, end;
52 53
	int i;

54 55 56 57 58 59 60 61
	/*
	 * A special case is the first 4Kb of memory;
	 * This is a BIOS owned area, not kernel ram, but generally
	 * not listed as such in the E820 table.
	 */
	if (pagenr == 0)
		return 0;

A
Arjan van de Ven 已提交
62 63 64 65 66 67 68
	/*
	 * Second special case: Some BIOSen report the PC BIOS
	 * area (640->1Mb) as ram even though it is not.
	 */
	if (pagenr >= (BIOS_BEGIN >> PAGE_SHIFT) &&
		    pagenr < (BIOS_END >> PAGE_SHIFT))
		return 0;
69

70 71 72 73 74 75 76 77
	for (i = 0; i < e820.nr_map; i++) {
		/*
		 * Not usable memory:
		 */
		if (e820.map[i].type != E820_RAM)
			continue;
		addr = (e820.map[i].addr + PAGE_SIZE-1) >> PAGE_SHIFT;
		end = (e820.map[i].addr + e820.map[i].size) >> PAGE_SHIFT;
78 79


80 81 82 83 84 85
		if ((pagenr >= addr) && (pagenr < end))
			return 1;
	}
	return 0;
}

86 87 88 89
/*
 * Fix up the linear direct mapping of the kernel to avoid cache attribute
 * conflicts.
 */
90 91
int ioremap_change_attr(unsigned long vaddr, unsigned long size,
			       unsigned long prot_val)
92
{
93
	unsigned long nrpages = size >> PAGE_SHIFT;
94
	int err;
95

96 97
	switch (prot_val) {
	case _PAGE_CACHE_UC:
98
	default:
99
		err = _set_memory_uc(vaddr, nrpages);
100
		break;
101 102 103
	case _PAGE_CACHE_WC:
		err = _set_memory_wc(vaddr, nrpages);
		break;
104
	case _PAGE_CACHE_WB:
105
		err = _set_memory_wb(vaddr, nrpages);
106 107
		break;
	}
108 109 110 111

	return err;
}

L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119 120
/*
 * Remap an arbitrary physical address space into the kernel virtual
 * address space. Needed when the kernel wants to access high addresses
 * directly.
 *
 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
 * have to convert them into an offset in a page-aligned mapping, but the
 * caller shouldn't need to know that small detail.
 */
121 122
static void __iomem *__ioremap_caller(resource_size_t phys_addr,
		unsigned long size, unsigned long prot_val, void *caller)
L
Linus Torvalds 已提交
123
{
124 125
	unsigned long pfn, offset, vaddr;
	resource_size_t last_addr;
126 127
	const resource_size_t unaligned_phys_addr = phys_addr;
	const unsigned long unaligned_size = size;
128
	struct vm_struct *area;
129
	unsigned long new_prot_val;
130
	pgprot_t prot;
131
	int retval;
P
Pekka Paalanen 已提交
132
	void __iomem *ret_addr;
L
Linus Torvalds 已提交
133 134 135 136 137 138

	/* Don't allow wraparound or zero size */
	last_addr = phys_addr + size - 1;
	if (!size || last_addr < phys_addr)
		return NULL;

139
	if (!phys_addr_valid(phys_addr)) {
140
		printk(KERN_WARNING "ioremap: invalid physical address %llx\n",
141
		       (unsigned long long)phys_addr);
142 143 144 145
		WARN_ON_ONCE(1);
		return NULL;
	}

L
Linus Torvalds 已提交
146 147 148
	/*
	 * Don't remap the low PCI/ISA area, it's always mapped..
	 */
149
	if (is_ISA_range(phys_addr, last_addr))
T
Thomas Gleixner 已提交
150
		return (__force void __iomem *)phys_to_virt(phys_addr);
L
Linus Torvalds 已提交
151 152 153 154

	/*
	 * Don't allow anybody to remap normal RAM that we're using..
	 */
A
Andres Salomon 已提交
155 156 157
	for (pfn = phys_addr >> PAGE_SHIFT;
				(pfn << PAGE_SHIFT) < (last_addr & PAGE_MASK);
				pfn++) {
158

159 160 161
		int is_ram = page_is_ram(pfn);

		if (is_ram && pfn_valid(pfn) && !PageReserved(pfn_to_page(pfn)))
T
Thomas Gleixner 已提交
162
			return NULL;
163
		WARN_ON_ONCE(is_ram);
L
Linus Torvalds 已提交
164 165
	}

166 167 168 169 170 171 172
	/*
	 * Mappings have to be page-aligned
	 */
	offset = phys_addr & ~PAGE_MASK;
	phys_addr &= PAGE_MASK;
	size = PAGE_ALIGN(last_addr+1) - phys_addr;

A
Andi Kleen 已提交
173
	retval = reserve_memtype(phys_addr, (u64)phys_addr + size,
174 175
						prot_val, &new_prot_val);
	if (retval) {
176
		pr_debug("Warning: reserve_memtype returned %d\n", retval);
177 178 179 180
		return NULL;
	}

	if (prot_val != new_prot_val) {
181 182 183
		/*
		 * Do not fallback to certain memory types with certain
		 * requested type:
184 185
		 * - request is uc-, return cannot be write-back
		 * - request is uc-, return cannot be write-combine
186
		 * - request is write-combine, return cannot be write-back
187
		 */
188
		if ((prot_val == _PAGE_CACHE_UC_MINUS &&
189 190 191
		     (new_prot_val == _PAGE_CACHE_WB ||
		      new_prot_val == _PAGE_CACHE_WC)) ||
		    (prot_val == _PAGE_CACHE_WC &&
192
		     new_prot_val == _PAGE_CACHE_WB)) {
193
			pr_debug(
194
		"ioremap error for 0x%llx-0x%llx, requested 0x%lx, got 0x%lx\n",
195 196
				(unsigned long long)phys_addr,
				(unsigned long long)(phys_addr + size),
197
				prot_val, new_prot_val);
198 199 200 201 202 203
			free_memtype(phys_addr, phys_addr + size);
			return NULL;
		}
		prot_val = new_prot_val;
	}

204 205
	switch (prot_val) {
	case _PAGE_CACHE_UC:
206
	default:
I
Ingo Molnar 已提交
207
		prot = PAGE_KERNEL_NOCACHE;
208
		break;
209 210 211
	case _PAGE_CACHE_UC_MINUS:
		prot = PAGE_KERNEL_UC_MINUS;
		break;
212 213 214
	case _PAGE_CACHE_WC:
		prot = PAGE_KERNEL_WC;
		break;
215
	case _PAGE_CACHE_WB:
216 217 218
		prot = PAGE_KERNEL;
		break;
	}
219

L
Linus Torvalds 已提交
220 221 222
	/*
	 * Ok, go for it..
	 */
223
	area = get_vm_area_caller(size, VM_IOREMAP, caller);
L
Linus Torvalds 已提交
224 225 226
	if (!area)
		return NULL;
	area->phys_addr = phys_addr;
T
Thomas Gleixner 已提交
227 228
	vaddr = (unsigned long) area->addr;
	if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot)) {
229
		free_memtype(phys_addr, phys_addr + size);
230
		free_vm_area(area);
L
Linus Torvalds 已提交
231 232
		return NULL;
	}
233

234
	if (ioremap_change_attr(vaddr, size, prot_val) < 0) {
235
		free_memtype(phys_addr, phys_addr + size);
T
Thomas Gleixner 已提交
236
		vunmap(area->addr);
237 238 239
		return NULL;
	}

P
Pekka Paalanen 已提交
240
	ret_addr = (void __iomem *) (vaddr + offset);
241
	mmiotrace_ioremap(unaligned_phys_addr, unaligned_size, ret_addr);
P
Pekka Paalanen 已提交
242 243

	return ret_addr;
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251 252 253 254
}

/**
 * ioremap_nocache     -   map bus memory into CPU space
 * @offset:    bus address of the memory
 * @size:      size of the resource to map
 *
 * ioremap_nocache performs a platform specific sequence of operations to
 * make bus memory CPU accessible via the readb/readw/readl/writeb/
 * writew/writel functions and the other mmio helpers. The returned
 * address is not guaranteed to be usable directly as a virtual
255
 * address.
L
Linus Torvalds 已提交
256 257 258
 *
 * This version of ioremap ensures that the memory is marked uncachable
 * on the CPU as well as honouring existing caching rules from things like
259
 * the PCI bus. Note that there are other caches and buffers on many
L
Linus Torvalds 已提交
260 261 262 263
 * busses. In particular driver authors should read up on PCI writes
 *
 * It's useful if some control registers are in such an area and
 * write combining or read caching is not desirable:
264
 *
L
Linus Torvalds 已提交
265 266
 * Must be freed with iounmap.
 */
267
void __iomem *ioremap_nocache(resource_size_t phys_addr, unsigned long size)
L
Linus Torvalds 已提交
268
{
269 270
	/*
	 * Ideally, this should be:
271
	 *	pat_enabled ? _PAGE_CACHE_UC : _PAGE_CACHE_UC_MINUS;
272 273 274 275 276 277 278
	 *
	 * Till we fix all X drivers to use ioremap_wc(), we will use
	 * UC MINUS.
	 */
	unsigned long val = _PAGE_CACHE_UC_MINUS;

	return __ioremap_caller(phys_addr, size, val,
279
				__builtin_return_address(0));
L
Linus Torvalds 已提交
280
}
281
EXPORT_SYMBOL(ioremap_nocache);
L
Linus Torvalds 已提交
282

283 284 285 286 287 288 289 290 291 292 293 294
/**
 * ioremap_wc	-	map memory into CPU space write combined
 * @offset:	bus address of the memory
 * @size:	size of the resource to map
 *
 * This version of ioremap ensures that the memory is marked write combining.
 * Write combining allows faster writes to some hardware devices.
 *
 * Must be freed with iounmap.
 */
void __iomem *ioremap_wc(unsigned long phys_addr, unsigned long size)
{
295
	if (pat_enabled)
296 297
		return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WC,
					__builtin_return_address(0));
298 299 300 301 302
	else
		return ioremap_nocache(phys_addr, size);
}
EXPORT_SYMBOL(ioremap_wc);

303
void __iomem *ioremap_cache(resource_size_t phys_addr, unsigned long size)
T
Thomas Gleixner 已提交
304
{
305 306
	return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WB,
				__builtin_return_address(0));
T
Thomas Gleixner 已提交
307 308 309
}
EXPORT_SYMBOL(ioremap_cache);

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
static void __iomem *ioremap_default(resource_size_t phys_addr,
					unsigned long size)
{
	unsigned long flags;
	void *ret;
	int err;

	/*
	 * - WB for WB-able memory and no other conflicting mappings
	 * - UC_MINUS for non-WB-able memory with no other conflicting mappings
	 * - Inherit from confliting mappings otherwise
	 */
	err = reserve_memtype(phys_addr, phys_addr + size, -1, &flags);
	if (err < 0)
		return NULL;

	ret = (void *) __ioremap_caller(phys_addr, size, flags,
					__builtin_return_address(0));

	free_memtype(phys_addr, phys_addr + size);
	return (void __iomem *)ret;
}

333 334 335 336 337 338 339 340
void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size,
				unsigned long prot_val)
{
	return __ioremap_caller(phys_addr, size, (prot_val & _PAGE_CACHE_MASK),
				__builtin_return_address(0));
}
EXPORT_SYMBOL(ioremap_prot);

341 342 343 344 345 346
/**
 * iounmap - Free a IO remapping
 * @addr: virtual address from ioremap_*
 *
 * Caller must ensure there is only one unmapping for the same pointer.
 */
L
Linus Torvalds 已提交
347 348
void iounmap(volatile void __iomem *addr)
{
349
	struct vm_struct *p, *o;
A
Andrew Morton 已提交
350 351

	if ((void __force *)addr <= high_memory)
L
Linus Torvalds 已提交
352 353 354 355 356 357 358
		return;

	/*
	 * __ioremap special-cases the PCI/ISA range by not instantiating a
	 * vm_area and by simply returning an address into the kernel mapping
	 * of ISA space.   So handle that here.
	 */
359 360
	if ((void __force *)addr >= phys_to_virt(ISA_START_ADDRESS) &&
	    (void __force *)addr < phys_to_virt(ISA_END_ADDRESS))
L
Linus Torvalds 已提交
361 362
		return;

363 364
	addr = (volatile void __iomem *)
		(PAGE_MASK & (unsigned long __force)addr);
365

P
Pekka Paalanen 已提交
366 367
	mmiotrace_iounmap(addr);

368 369 370 371 372 373 374
	/* Use the vm area unlocked, assuming the caller
	   ensures there isn't another iounmap for the same address
	   in parallel. Reuse of the virtual address is prevented by
	   leaving it in the global lists until we're done with it.
	   cpa takes care of the direct mappings. */
	read_lock(&vmlist_lock);
	for (p = vmlist; p; p = p->next) {
375
		if (p->addr == (void __force *)addr)
376 377 378 379 380
			break;
	}
	read_unlock(&vmlist_lock);

	if (!p) {
381
		printk(KERN_ERR "iounmap: bad address %p\n", addr);
A
Andrew Morton 已提交
382
		dump_stack();
383
		return;
L
Linus Torvalds 已提交
384 385
	}

386 387
	free_memtype(p->phys_addr, p->phys_addr + get_vm_area_size(p));

388
	/* Finally remove it */
389
	o = remove_vm_area((void __force *)addr);
390
	BUG_ON(p != o || o == NULL);
391
	kfree(p);
L
Linus Torvalds 已提交
392
}
393
EXPORT_SYMBOL(iounmap);
L
Linus Torvalds 已提交
394

395 396 397 398 399 400 401 402 403 404 405 406 407
/*
 * Convert a physical pointer to a virtual kernel pointer for /dev/mem
 * access
 */
void *xlate_dev_mem_ptr(unsigned long phys)
{
	void *addr;
	unsigned long start = phys & PAGE_MASK;

	/* If page is RAM, we can use __va. Otherwise ioremap and unmap. */
	if (page_is_ram(start >> PAGE_SHIFT))
		return __va(phys);

I
Ingo Molnar 已提交
408
	addr = (void __force *)ioremap_default(start, PAGE_SIZE);
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
	if (addr)
		addr = (void *)((unsigned long)addr | (phys & ~PAGE_MASK));

	return addr;
}

void unxlate_dev_mem_ptr(unsigned long phys, void *addr)
{
	if (page_is_ram(phys >> PAGE_SHIFT))
		return;

	iounmap((void __iomem *)((unsigned long)addr & PAGE_MASK));
	return;
}

I
Ingo Molnar 已提交
424 425 426 427 428 429
int __initdata early_ioremap_debug;

static int __init early_ioremap_debug_setup(char *str)
{
	early_ioremap_debug = 1;

430
	return 0;
I
Ingo Molnar 已提交
431
}
432
early_param("early_ioremap_debug", early_ioremap_debug_setup);
I
Ingo Molnar 已提交
433

434
static __initdata int after_paging_init;
435
static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss;
436

437
static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
438
{
439 440 441
	/* Don't assume we're using swapper_pg_dir at this point */
	pgd_t *base = __va(read_cr3());
	pgd_t *pgd = &base[pgd_index(addr)];
442 443 444 445
	pud_t *pud = pud_offset(pgd, addr);
	pmd_t *pmd = pmd_offset(pud, addr);

	return pmd;
446 447
}

448
static inline pte_t * __init early_ioremap_pte(unsigned long addr)
449
{
450
	return &bm_pte[pte_index(addr)];
451 452
}

453
void __init early_ioremap_init(void)
454
{
455
	pmd_t *pmd;
456

I
Ingo Molnar 已提交
457
	if (early_ioremap_debug)
I
Ingo Molnar 已提交
458
		printk(KERN_INFO "early_ioremap_init()\n");
I
Ingo Molnar 已提交
459

460
	pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
461
	memset(bm_pte, 0, sizeof(bm_pte));
462
	pmd_populate_kernel(&init_mm, pmd, bm_pte);
463

464
	/*
465
	 * The boot-ioremap range spans multiple pmds, for which
466 467
	 * we are not prepared:
	 */
468
	if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
469
		WARN_ON(1);
470 471
		printk(KERN_WARNING "pmd %p != %p\n",
		       pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
472
		printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
473
			fix_to_virt(FIX_BTMAP_BEGIN));
474
		printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END):   %08lx\n",
475
			fix_to_virt(FIX_BTMAP_END));
476 477 478 479

		printk(KERN_WARNING "FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
		printk(KERN_WARNING "FIX_BTMAP_BEGIN:     %d\n",
		       FIX_BTMAP_BEGIN);
480
	}
481 482
}

483
void __init early_ioremap_clear(void)
484
{
485
	pmd_t *pmd;
486

I
Ingo Molnar 已提交
487
	if (early_ioremap_debug)
I
Ingo Molnar 已提交
488
		printk(KERN_INFO "early_ioremap_clear()\n");
I
Ingo Molnar 已提交
489

490 491
	pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
	pmd_clear(pmd);
492
	paravirt_release_pte(__pa(bm_pte) >> PAGE_SHIFT);
493 494 495
	__flush_tlb_all();
}

496
void __init early_ioremap_reset(void)
497 498
{
	enum fixed_addresses idx;
499 500
	unsigned long addr, phys;
	pte_t *pte;
501 502

	after_paging_init = 1;
H
Huang, Ying 已提交
503
	for (idx = FIX_BTMAP_BEGIN; idx >= FIX_BTMAP_END; idx--) {
504
		addr = fix_to_virt(idx);
505
		pte = early_ioremap_pte(addr);
506 507
		if (pte_present(*pte)) {
			phys = pte_val(*pte) & PAGE_MASK;
508 509 510 511 512
			set_fixmap(idx, phys);
		}
	}
}

513
static void __init __early_set_fixmap(enum fixed_addresses idx,
514 515
				   unsigned long phys, pgprot_t flags)
{
516 517
	unsigned long addr = __fix_to_virt(idx);
	pte_t *pte;
518 519 520 521 522

	if (idx >= __end_of_fixed_addresses) {
		BUG();
		return;
	}
523
	pte = early_ioremap_pte(addr);
524

525
	if (pgprot_val(flags))
526
		set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
527
	else
528
		pte_clear(&init_mm, addr, pte);
529 530 531
	__flush_tlb_one(addr);
}

532
static inline void __init early_set_fixmap(enum fixed_addresses idx,
533 534 535 536 537
					unsigned long phys)
{
	if (after_paging_init)
		set_fixmap(idx, phys);
	else
538
		__early_set_fixmap(idx, phys, PAGE_KERNEL);
539 540
}

541
static inline void __init early_clear_fixmap(enum fixed_addresses idx)
542 543 544 545
{
	if (after_paging_init)
		clear_fixmap(idx);
	else
546
		__early_set_fixmap(idx, 0, __pgprot(0));
547 548
}

I
Ingo Molnar 已提交
549 550 551

int __initdata early_ioremap_nested;

552 553 554 555 556 557
static int __init check_early_ioremap_leak(void)
{
	if (!early_ioremap_nested)
		return 0;

	printk(KERN_WARNING
558 559
	       "Debug warning: early ioremap leak of %d areas detected.\n",
	       early_ioremap_nested);
560
	printk(KERN_WARNING
561
	       "please boot with early_ioremap_debug and report the dmesg.\n");
562 563 564 565 566 567
	WARN_ON(1);

	return 1;
}
late_initcall(check_early_ioremap_leak);

568
void __init *early_ioremap(unsigned long phys_addr, unsigned long size)
L
Linus Torvalds 已提交
569 570
{
	unsigned long offset, last_addr;
I
Ingo Molnar 已提交
571 572 573 574 575 576
	unsigned int nrpages, nesting;
	enum fixed_addresses idx0, idx;

	WARN_ON(system_state != SYSTEM_BOOTING);

	nesting = early_ioremap_nested;
I
Ingo Molnar 已提交
577
	if (early_ioremap_debug) {
I
Ingo Molnar 已提交
578
		printk(KERN_INFO "early_ioremap(%08lx, %08lx) [%d] => ",
579
		       phys_addr, size, nesting);
I
Ingo Molnar 已提交
580 581
		dump_stack();
	}
L
Linus Torvalds 已提交
582 583 584

	/* Don't allow wraparound or zero size */
	last_addr = phys_addr + size - 1;
585 586
	if (!size || last_addr < phys_addr) {
		WARN_ON(1);
L
Linus Torvalds 已提交
587
		return NULL;
588
	}
L
Linus Torvalds 已提交
589

590 591
	if (nesting >= FIX_BTMAPS_NESTING) {
		WARN_ON(1);
I
Ingo Molnar 已提交
592
		return NULL;
593
	}
I
Ingo Molnar 已提交
594
	early_ioremap_nested++;
L
Linus Torvalds 已提交
595 596 597 598 599 600 601 602 603 604 605
	/*
	 * Mappings have to be page-aligned
	 */
	offset = phys_addr & ~PAGE_MASK;
	phys_addr &= PAGE_MASK;
	size = PAGE_ALIGN(last_addr) - phys_addr;

	/*
	 * Mappings have to fit in the FIX_BTMAP area.
	 */
	nrpages = size >> PAGE_SHIFT;
606 607
	if (nrpages > NR_FIX_BTMAPS) {
		WARN_ON(1);
L
Linus Torvalds 已提交
608
		return NULL;
609
	}
L
Linus Torvalds 已提交
610 611 612 613

	/*
	 * Ok, go for it..
	 */
I
Ingo Molnar 已提交
614 615
	idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
	idx = idx0;
L
Linus Torvalds 已提交
616
	while (nrpages > 0) {
617
		early_set_fixmap(idx, phys_addr);
L
Linus Torvalds 已提交
618 619 620 621
		phys_addr += PAGE_SIZE;
		--idx;
		--nrpages;
	}
I
Ingo Molnar 已提交
622 623
	if (early_ioremap_debug)
		printk(KERN_CONT "%08lx + %08lx\n", offset, fix_to_virt(idx0));
I
Ingo Molnar 已提交
624

625
	return (void *) (offset + fix_to_virt(idx0));
L
Linus Torvalds 已提交
626 627
}

628
void __init early_iounmap(void *addr, unsigned long size)
L
Linus Torvalds 已提交
629 630 631 632 633
{
	unsigned long virt_addr;
	unsigned long offset;
	unsigned int nrpages;
	enum fixed_addresses idx;
634
	int nesting;
I
Ingo Molnar 已提交
635 636

	nesting = --early_ioremap_nested;
637 638
	if (WARN_ON(nesting < 0))
		return;
L
Linus Torvalds 已提交
639

I
Ingo Molnar 已提交
640
	if (early_ioremap_debug) {
I
Ingo Molnar 已提交
641
		printk(KERN_INFO "early_iounmap(%p, %08lx) [%d]\n", addr,
642
		       size, nesting);
I
Ingo Molnar 已提交
643 644 645
		dump_stack();
	}

L
Linus Torvalds 已提交
646
	virt_addr = (unsigned long)addr;
647 648
	if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
		WARN_ON(1);
L
Linus Torvalds 已提交
649
		return;
650
	}
L
Linus Torvalds 已提交
651 652 653
	offset = virt_addr & ~PAGE_MASK;
	nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;

I
Ingo Molnar 已提交
654
	idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
L
Linus Torvalds 已提交
655
	while (nrpages > 0) {
656
		early_clear_fixmap(idx);
L
Linus Torvalds 已提交
657 658 659 660
		--idx;
		--nrpages;
	}
}
I
Ingo Molnar 已提交
661 662 663 664 665

void __this_fixmap_does_not_exist(void)
{
	WARN_ON(1);
}