ioremap.c 16 KB

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