ioremap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. * Re-map IO memory to kernel address space so that we can access it.
  3. * This is needed for high PCI addresses that aren't mapped in the
  4. * 640k-1MB IO memory area on PC's
  5. *
  6. * (C) Copyright 1995 1996 Linus Torvalds
  7. */
  8. #include <linux/bootmem.h>
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/mmiotrace.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/e820.h>
  17. #include <asm/fixmap.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/tlbflush.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/pat.h>
  22. static inline int phys_addr_valid(resource_size_t addr)
  23. {
  24. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  25. return !(addr >> boot_cpu_data.x86_phys_bits);
  26. #else
  27. return 1;
  28. #endif
  29. }
  30. #ifdef CONFIG_X86_64
  31. unsigned long __phys_addr(unsigned long x)
  32. {
  33. if (x >= __START_KERNEL_map) {
  34. x -= __START_KERNEL_map;
  35. VIRTUAL_BUG_ON(x >= KERNEL_IMAGE_SIZE);
  36. x += phys_base;
  37. } else {
  38. VIRTUAL_BUG_ON(x < PAGE_OFFSET);
  39. x -= PAGE_OFFSET;
  40. VIRTUAL_BUG_ON(!phys_addr_valid(x));
  41. }
  42. return x;
  43. }
  44. EXPORT_SYMBOL(__phys_addr);
  45. bool __virt_addr_valid(unsigned long x)
  46. {
  47. if (x >= __START_KERNEL_map) {
  48. x -= __START_KERNEL_map;
  49. if (x >= KERNEL_IMAGE_SIZE)
  50. return false;
  51. x += phys_base;
  52. } else {
  53. if (x < PAGE_OFFSET)
  54. return false;
  55. x -= PAGE_OFFSET;
  56. if (!phys_addr_valid(x))
  57. return false;
  58. }
  59. return pfn_valid(x >> PAGE_SHIFT);
  60. }
  61. EXPORT_SYMBOL(__virt_addr_valid);
  62. #else
  63. #ifdef CONFIG_DEBUG_VIRTUAL
  64. unsigned long __phys_addr(unsigned long x)
  65. {
  66. /* VMALLOC_* aren't constants */
  67. VIRTUAL_BUG_ON(x < PAGE_OFFSET);
  68. VIRTUAL_BUG_ON(__vmalloc_start_set && is_vmalloc_addr((void *) x));
  69. return x - PAGE_OFFSET;
  70. }
  71. EXPORT_SYMBOL(__phys_addr);
  72. #endif
  73. bool __virt_addr_valid(unsigned long x)
  74. {
  75. if (x < PAGE_OFFSET)
  76. return false;
  77. if (__vmalloc_start_set && is_vmalloc_addr((void *) x))
  78. return false;
  79. if (x >= FIXADDR_START)
  80. return false;
  81. return pfn_valid((x - PAGE_OFFSET) >> PAGE_SHIFT);
  82. }
  83. EXPORT_SYMBOL(__virt_addr_valid);
  84. #endif
  85. int page_is_ram(unsigned long pagenr)
  86. {
  87. resource_size_t addr, end;
  88. int i;
  89. /*
  90. * A special case is the first 4Kb of memory;
  91. * This is a BIOS owned area, not kernel ram, but generally
  92. * not listed as such in the E820 table.
  93. */
  94. if (pagenr == 0)
  95. return 0;
  96. /*
  97. * Second special case: Some BIOSen report the PC BIOS
  98. * area (640->1Mb) as ram even though it is not.
  99. */
  100. if (pagenr >= (BIOS_BEGIN >> PAGE_SHIFT) &&
  101. pagenr < (BIOS_END >> PAGE_SHIFT))
  102. return 0;
  103. for (i = 0; i < e820.nr_map; i++) {
  104. /*
  105. * Not usable memory:
  106. */
  107. if (e820.map[i].type != E820_RAM)
  108. continue;
  109. addr = (e820.map[i].addr + PAGE_SIZE-1) >> PAGE_SHIFT;
  110. end = (e820.map[i].addr + e820.map[i].size) >> PAGE_SHIFT;
  111. if ((pagenr >= addr) && (pagenr < end))
  112. return 1;
  113. }
  114. return 0;
  115. }
  116. /*
  117. * Fix up the linear direct mapping of the kernel to avoid cache attribute
  118. * conflicts.
  119. */
  120. int ioremap_change_attr(unsigned long vaddr, unsigned long size,
  121. unsigned long prot_val)
  122. {
  123. unsigned long nrpages = size >> PAGE_SHIFT;
  124. int err;
  125. switch (prot_val) {
  126. case _PAGE_CACHE_UC:
  127. default:
  128. err = _set_memory_uc(vaddr, nrpages);
  129. break;
  130. case _PAGE_CACHE_WC:
  131. err = _set_memory_wc(vaddr, nrpages);
  132. break;
  133. case _PAGE_CACHE_WB:
  134. err = _set_memory_wb(vaddr, nrpages);
  135. break;
  136. }
  137. return err;
  138. }
  139. /*
  140. * Remap an arbitrary physical address space into the kernel virtual
  141. * address space. Needed when the kernel wants to access high addresses
  142. * directly.
  143. *
  144. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  145. * have to convert them into an offset in a page-aligned mapping, but the
  146. * caller shouldn't need to know that small detail.
  147. */
  148. static void __iomem *__ioremap_caller(resource_size_t phys_addr,
  149. unsigned long size, unsigned long prot_val, void *caller)
  150. {
  151. unsigned long pfn, offset, vaddr;
  152. resource_size_t last_addr;
  153. const resource_size_t unaligned_phys_addr = phys_addr;
  154. const unsigned long unaligned_size = size;
  155. struct vm_struct *area;
  156. unsigned long new_prot_val;
  157. pgprot_t prot;
  158. int retval;
  159. void __iomem *ret_addr;
  160. /* Don't allow wraparound or zero size */
  161. last_addr = phys_addr + size - 1;
  162. if (!size || last_addr < phys_addr)
  163. return NULL;
  164. if (!phys_addr_valid(phys_addr)) {
  165. printk(KERN_WARNING "ioremap: invalid physical address %llx\n",
  166. (unsigned long long)phys_addr);
  167. WARN_ON_ONCE(1);
  168. return NULL;
  169. }
  170. /*
  171. * Don't remap the low PCI/ISA area, it's always mapped..
  172. */
  173. if (is_ISA_range(phys_addr, last_addr))
  174. return (__force void __iomem *)phys_to_virt(phys_addr);
  175. /*
  176. * Check if the request spans more than any BAR in the iomem resource
  177. * tree.
  178. */
  179. WARN_ONCE(iomem_map_sanity_check(phys_addr, size),
  180. KERN_INFO "Info: mapping multiple BARs. Your kernel is fine.");
  181. /*
  182. * Don't allow anybody to remap normal RAM that we're using..
  183. */
  184. for (pfn = phys_addr >> PAGE_SHIFT;
  185. (pfn << PAGE_SHIFT) < (last_addr & PAGE_MASK);
  186. pfn++) {
  187. int is_ram = page_is_ram(pfn);
  188. if (is_ram && pfn_valid(pfn) && !PageReserved(pfn_to_page(pfn)))
  189. return NULL;
  190. WARN_ON_ONCE(is_ram);
  191. }
  192. /*
  193. * Mappings have to be page-aligned
  194. */
  195. offset = phys_addr & ~PAGE_MASK;
  196. phys_addr &= PAGE_MASK;
  197. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  198. retval = reserve_memtype(phys_addr, (u64)phys_addr + size,
  199. prot_val, &new_prot_val);
  200. if (retval) {
  201. pr_debug("Warning: reserve_memtype returned %d\n", retval);
  202. return NULL;
  203. }
  204. if (prot_val != new_prot_val) {
  205. /*
  206. * Do not fallback to certain memory types with certain
  207. * requested type:
  208. * - request is uc-, return cannot be write-back
  209. * - request is uc-, return cannot be write-combine
  210. * - request is write-combine, return cannot be write-back
  211. */
  212. if ((prot_val == _PAGE_CACHE_UC_MINUS &&
  213. (new_prot_val == _PAGE_CACHE_WB ||
  214. new_prot_val == _PAGE_CACHE_WC)) ||
  215. (prot_val == _PAGE_CACHE_WC &&
  216. new_prot_val == _PAGE_CACHE_WB)) {
  217. pr_debug(
  218. "ioremap error for 0x%llx-0x%llx, requested 0x%lx, got 0x%lx\n",
  219. (unsigned long long)phys_addr,
  220. (unsigned long long)(phys_addr + size),
  221. prot_val, new_prot_val);
  222. free_memtype(phys_addr, phys_addr + size);
  223. return NULL;
  224. }
  225. prot_val = new_prot_val;
  226. }
  227. switch (prot_val) {
  228. case _PAGE_CACHE_UC:
  229. default:
  230. prot = PAGE_KERNEL_IO_NOCACHE;
  231. break;
  232. case _PAGE_CACHE_UC_MINUS:
  233. prot = PAGE_KERNEL_IO_UC_MINUS;
  234. break;
  235. case _PAGE_CACHE_WC:
  236. prot = PAGE_KERNEL_IO_WC;
  237. break;
  238. case _PAGE_CACHE_WB:
  239. prot = PAGE_KERNEL_IO;
  240. break;
  241. }
  242. /*
  243. * Ok, go for it..
  244. */
  245. area = get_vm_area_caller(size, VM_IOREMAP, caller);
  246. if (!area)
  247. return NULL;
  248. area->phys_addr = phys_addr;
  249. vaddr = (unsigned long) area->addr;
  250. if (kernel_map_sync_memtype(phys_addr, size, prot_val)) {
  251. free_memtype(phys_addr, phys_addr + size);
  252. free_vm_area(area);
  253. return NULL;
  254. }
  255. if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot)) {
  256. free_memtype(phys_addr, phys_addr + size);
  257. free_vm_area(area);
  258. return NULL;
  259. }
  260. ret_addr = (void __iomem *) (vaddr + offset);
  261. mmiotrace_ioremap(unaligned_phys_addr, unaligned_size, ret_addr);
  262. return ret_addr;
  263. }
  264. /**
  265. * ioremap_nocache - map bus memory into CPU space
  266. * @offset: bus address of the memory
  267. * @size: size of the resource to map
  268. *
  269. * ioremap_nocache performs a platform specific sequence of operations to
  270. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  271. * writew/writel functions and the other mmio helpers. The returned
  272. * address is not guaranteed to be usable directly as a virtual
  273. * address.
  274. *
  275. * This version of ioremap ensures that the memory is marked uncachable
  276. * on the CPU as well as honouring existing caching rules from things like
  277. * the PCI bus. Note that there are other caches and buffers on many
  278. * busses. In particular driver authors should read up on PCI writes
  279. *
  280. * It's useful if some control registers are in such an area and
  281. * write combining or read caching is not desirable:
  282. *
  283. * Must be freed with iounmap.
  284. */
  285. void __iomem *ioremap_nocache(resource_size_t phys_addr, unsigned long size)
  286. {
  287. /*
  288. * Ideally, this should be:
  289. * pat_enabled ? _PAGE_CACHE_UC : _PAGE_CACHE_UC_MINUS;
  290. *
  291. * Till we fix all X drivers to use ioremap_wc(), we will use
  292. * UC MINUS.
  293. */
  294. unsigned long val = _PAGE_CACHE_UC_MINUS;
  295. return __ioremap_caller(phys_addr, size, val,
  296. __builtin_return_address(0));
  297. }
  298. EXPORT_SYMBOL(ioremap_nocache);
  299. /**
  300. * ioremap_wc - map memory into CPU space write combined
  301. * @offset: bus address of the memory
  302. * @size: size of the resource to map
  303. *
  304. * This version of ioremap ensures that the memory is marked write combining.
  305. * Write combining allows faster writes to some hardware devices.
  306. *
  307. * Must be freed with iounmap.
  308. */
  309. void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size)
  310. {
  311. if (pat_enabled)
  312. return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WC,
  313. __builtin_return_address(0));
  314. else
  315. return ioremap_nocache(phys_addr, size);
  316. }
  317. EXPORT_SYMBOL(ioremap_wc);
  318. void __iomem *ioremap_cache(resource_size_t phys_addr, unsigned long size)
  319. {
  320. return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WB,
  321. __builtin_return_address(0));
  322. }
  323. EXPORT_SYMBOL(ioremap_cache);
  324. static void __iomem *ioremap_default(resource_size_t phys_addr,
  325. unsigned long size)
  326. {
  327. unsigned long flags;
  328. void __iomem *ret;
  329. int err;
  330. /*
  331. * - WB for WB-able memory and no other conflicting mappings
  332. * - UC_MINUS for non-WB-able memory with no other conflicting mappings
  333. * - Inherit from confliting mappings otherwise
  334. */
  335. err = reserve_memtype(phys_addr, phys_addr + size,
  336. _PAGE_CACHE_WB, &flags);
  337. if (err < 0)
  338. return NULL;
  339. ret = __ioremap_caller(phys_addr, size, flags,
  340. __builtin_return_address(0));
  341. free_memtype(phys_addr, phys_addr + size);
  342. return ret;
  343. }
  344. void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size,
  345. unsigned long prot_val)
  346. {
  347. return __ioremap_caller(phys_addr, size, (prot_val & _PAGE_CACHE_MASK),
  348. __builtin_return_address(0));
  349. }
  350. EXPORT_SYMBOL(ioremap_prot);
  351. /**
  352. * iounmap - Free a IO remapping
  353. * @addr: virtual address from ioremap_*
  354. *
  355. * Caller must ensure there is only one unmapping for the same pointer.
  356. */
  357. void iounmap(volatile void __iomem *addr)
  358. {
  359. struct vm_struct *p, *o;
  360. if ((void __force *)addr <= high_memory)
  361. return;
  362. /*
  363. * __ioremap special-cases the PCI/ISA range by not instantiating a
  364. * vm_area and by simply returning an address into the kernel mapping
  365. * of ISA space. So handle that here.
  366. */
  367. if ((void __force *)addr >= phys_to_virt(ISA_START_ADDRESS) &&
  368. (void __force *)addr < phys_to_virt(ISA_END_ADDRESS))
  369. return;
  370. addr = (volatile void __iomem *)
  371. (PAGE_MASK & (unsigned long __force)addr);
  372. mmiotrace_iounmap(addr);
  373. /* Use the vm area unlocked, assuming the caller
  374. ensures there isn't another iounmap for the same address
  375. in parallel. Reuse of the virtual address is prevented by
  376. leaving it in the global lists until we're done with it.
  377. cpa takes care of the direct mappings. */
  378. read_lock(&vmlist_lock);
  379. for (p = vmlist; p; p = p->next) {
  380. if (p->addr == (void __force *)addr)
  381. break;
  382. }
  383. read_unlock(&vmlist_lock);
  384. if (!p) {
  385. printk(KERN_ERR "iounmap: bad address %p\n", addr);
  386. dump_stack();
  387. return;
  388. }
  389. free_memtype(p->phys_addr, p->phys_addr + get_vm_area_size(p));
  390. /* Finally remove it */
  391. o = remove_vm_area((void __force *)addr);
  392. BUG_ON(p != o || o == NULL);
  393. kfree(p);
  394. }
  395. EXPORT_SYMBOL(iounmap);
  396. /*
  397. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  398. * access
  399. */
  400. void *xlate_dev_mem_ptr(unsigned long phys)
  401. {
  402. void *addr;
  403. unsigned long start = phys & PAGE_MASK;
  404. /* If page is RAM, we can use __va. Otherwise ioremap and unmap. */
  405. if (page_is_ram(start >> PAGE_SHIFT))
  406. return __va(phys);
  407. addr = (void __force *)ioremap_default(start, PAGE_SIZE);
  408. if (addr)
  409. addr = (void *)((unsigned long)addr | (phys & ~PAGE_MASK));
  410. return addr;
  411. }
  412. void unxlate_dev_mem_ptr(unsigned long phys, void *addr)
  413. {
  414. if (page_is_ram(phys >> PAGE_SHIFT))
  415. return;
  416. iounmap((void __iomem *)((unsigned long)addr & PAGE_MASK));
  417. return;
  418. }
  419. static int __initdata early_ioremap_debug;
  420. static int __init early_ioremap_debug_setup(char *str)
  421. {
  422. early_ioremap_debug = 1;
  423. return 0;
  424. }
  425. early_param("early_ioremap_debug", early_ioremap_debug_setup);
  426. static __initdata int after_paging_init;
  427. static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss;
  428. static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
  429. {
  430. /* Don't assume we're using swapper_pg_dir at this point */
  431. pgd_t *base = __va(read_cr3());
  432. pgd_t *pgd = &base[pgd_index(addr)];
  433. pud_t *pud = pud_offset(pgd, addr);
  434. pmd_t *pmd = pmd_offset(pud, addr);
  435. return pmd;
  436. }
  437. static inline pte_t * __init early_ioremap_pte(unsigned long addr)
  438. {
  439. return &bm_pte[pte_index(addr)];
  440. }
  441. static unsigned long slot_virt[FIX_BTMAPS_SLOTS] __initdata;
  442. void __init early_ioremap_init(void)
  443. {
  444. pmd_t *pmd;
  445. int i;
  446. if (early_ioremap_debug)
  447. printk(KERN_INFO "early_ioremap_init()\n");
  448. for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
  449. slot_virt[i] = __fix_to_virt(FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*i);
  450. pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
  451. memset(bm_pte, 0, sizeof(bm_pte));
  452. pmd_populate_kernel(&init_mm, pmd, bm_pte);
  453. /*
  454. * The boot-ioremap range spans multiple pmds, for which
  455. * we are not prepared:
  456. */
  457. if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
  458. WARN_ON(1);
  459. printk(KERN_WARNING "pmd %p != %p\n",
  460. pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
  461. printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
  462. fix_to_virt(FIX_BTMAP_BEGIN));
  463. printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END): %08lx\n",
  464. fix_to_virt(FIX_BTMAP_END));
  465. printk(KERN_WARNING "FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
  466. printk(KERN_WARNING "FIX_BTMAP_BEGIN: %d\n",
  467. FIX_BTMAP_BEGIN);
  468. }
  469. }
  470. void __init early_ioremap_reset(void)
  471. {
  472. after_paging_init = 1;
  473. }
  474. static void __init __early_set_fixmap(enum fixed_addresses idx,
  475. phys_addr_t phys, pgprot_t flags)
  476. {
  477. unsigned long addr = __fix_to_virt(idx);
  478. pte_t *pte;
  479. if (idx >= __end_of_fixed_addresses) {
  480. BUG();
  481. return;
  482. }
  483. pte = early_ioremap_pte(addr);
  484. if (pgprot_val(flags))
  485. set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
  486. else
  487. pte_clear(&init_mm, addr, pte);
  488. __flush_tlb_one(addr);
  489. }
  490. static inline void __init early_set_fixmap(enum fixed_addresses idx,
  491. phys_addr_t phys, pgprot_t prot)
  492. {
  493. if (after_paging_init)
  494. __set_fixmap(idx, phys, prot);
  495. else
  496. __early_set_fixmap(idx, phys, prot);
  497. }
  498. static inline void __init early_clear_fixmap(enum fixed_addresses idx)
  499. {
  500. if (after_paging_init)
  501. clear_fixmap(idx);
  502. else
  503. __early_set_fixmap(idx, 0, __pgprot(0));
  504. }
  505. static void __iomem *prev_map[FIX_BTMAPS_SLOTS] __initdata;
  506. static unsigned long prev_size[FIX_BTMAPS_SLOTS] __initdata;
  507. static int __init check_early_ioremap_leak(void)
  508. {
  509. int count = 0;
  510. int i;
  511. for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
  512. if (prev_map[i])
  513. count++;
  514. if (!count)
  515. return 0;
  516. WARN(1, KERN_WARNING
  517. "Debug warning: early ioremap leak of %d areas detected.\n",
  518. count);
  519. printk(KERN_WARNING
  520. "please boot with early_ioremap_debug and report the dmesg.\n");
  521. return 1;
  522. }
  523. late_initcall(check_early_ioremap_leak);
  524. static void __init __iomem *
  525. __early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot)
  526. {
  527. unsigned long offset;
  528. resource_size_t last_addr;
  529. unsigned int nrpages;
  530. enum fixed_addresses idx0, idx;
  531. int i, slot;
  532. WARN_ON(system_state != SYSTEM_BOOTING);
  533. slot = -1;
  534. for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
  535. if (!prev_map[i]) {
  536. slot = i;
  537. break;
  538. }
  539. }
  540. if (slot < 0) {
  541. printk(KERN_INFO "early_iomap(%08llx, %08lx) not found slot\n",
  542. (u64)phys_addr, size);
  543. WARN_ON(1);
  544. return NULL;
  545. }
  546. if (early_ioremap_debug) {
  547. printk(KERN_INFO "early_ioremap(%08llx, %08lx) [%d] => ",
  548. (u64)phys_addr, size, slot);
  549. dump_stack();
  550. }
  551. /* Don't allow wraparound or zero size */
  552. last_addr = phys_addr + size - 1;
  553. if (!size || last_addr < phys_addr) {
  554. WARN_ON(1);
  555. return NULL;
  556. }
  557. prev_size[slot] = size;
  558. /*
  559. * Mappings have to be page-aligned
  560. */
  561. offset = phys_addr & ~PAGE_MASK;
  562. phys_addr &= PAGE_MASK;
  563. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  564. /*
  565. * Mappings have to fit in the FIX_BTMAP area.
  566. */
  567. nrpages = size >> PAGE_SHIFT;
  568. if (nrpages > NR_FIX_BTMAPS) {
  569. WARN_ON(1);
  570. return NULL;
  571. }
  572. /*
  573. * Ok, go for it..
  574. */
  575. idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
  576. idx = idx0;
  577. while (nrpages > 0) {
  578. early_set_fixmap(idx, phys_addr, prot);
  579. phys_addr += PAGE_SIZE;
  580. --idx;
  581. --nrpages;
  582. }
  583. if (early_ioremap_debug)
  584. printk(KERN_CONT "%08lx + %08lx\n", offset, slot_virt[slot]);
  585. prev_map[slot] = (void __iomem *)(offset + slot_virt[slot]);
  586. return prev_map[slot];
  587. }
  588. /* Remap an IO device */
  589. void __init __iomem *
  590. early_ioremap(resource_size_t phys_addr, unsigned long size)
  591. {
  592. return __early_ioremap(phys_addr, size, PAGE_KERNEL_IO);
  593. }
  594. /* Remap memory */
  595. void __init __iomem *
  596. early_memremap(resource_size_t phys_addr, unsigned long size)
  597. {
  598. return __early_ioremap(phys_addr, size, PAGE_KERNEL);
  599. }
  600. void __init early_iounmap(void __iomem *addr, unsigned long size)
  601. {
  602. unsigned long virt_addr;
  603. unsigned long offset;
  604. unsigned int nrpages;
  605. enum fixed_addresses idx;
  606. int i, slot;
  607. slot = -1;
  608. for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
  609. if (prev_map[i] == addr) {
  610. slot = i;
  611. break;
  612. }
  613. }
  614. if (slot < 0) {
  615. printk(KERN_INFO "early_iounmap(%p, %08lx) not found slot\n",
  616. addr, size);
  617. WARN_ON(1);
  618. return;
  619. }
  620. if (prev_size[slot] != size) {
  621. printk(KERN_INFO "early_iounmap(%p, %08lx) [%d] size not consistent %08lx\n",
  622. addr, size, slot, prev_size[slot]);
  623. WARN_ON(1);
  624. return;
  625. }
  626. if (early_ioremap_debug) {
  627. printk(KERN_INFO "early_iounmap(%p, %08lx) [%d]\n", addr,
  628. size, slot);
  629. dump_stack();
  630. }
  631. virt_addr = (unsigned long)addr;
  632. if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
  633. WARN_ON(1);
  634. return;
  635. }
  636. offset = virt_addr & ~PAGE_MASK;
  637. nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
  638. idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
  639. while (nrpages > 0) {
  640. early_clear_fixmap(idx);
  641. --idx;
  642. --nrpages;
  643. }
  644. prev_map[slot] = NULL;
  645. }