ioremap.c 19 KB

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