ioremap.c 15 KB

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