ioremap.c 15 KB

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