ioremap.c 15 KB

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