ioremap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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/slab.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/mmiotrace.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. #include "physaddr.h"
  22. /*
  23. * Fix up the linear direct mapping of the kernel to avoid cache attribute
  24. * conflicts.
  25. */
  26. int ioremap_change_attr(unsigned long vaddr, unsigned long size,
  27. enum page_cache_mode pcm)
  28. {
  29. unsigned long nrpages = size >> PAGE_SHIFT;
  30. int err;
  31. switch (pcm) {
  32. case _PAGE_CACHE_MODE_UC:
  33. default:
  34. err = _set_memory_uc(vaddr, nrpages);
  35. break;
  36. case _PAGE_CACHE_MODE_WC:
  37. err = _set_memory_wc(vaddr, nrpages);
  38. break;
  39. case _PAGE_CACHE_MODE_WT:
  40. err = _set_memory_wt(vaddr, nrpages);
  41. break;
  42. case _PAGE_CACHE_MODE_WB:
  43. err = _set_memory_wb(vaddr, nrpages);
  44. break;
  45. }
  46. return err;
  47. }
  48. static int __ioremap_check_ram(unsigned long start_pfn, unsigned long nr_pages,
  49. void *arg)
  50. {
  51. unsigned long i;
  52. for (i = 0; i < nr_pages; ++i)
  53. if (pfn_valid(start_pfn + i) &&
  54. !PageReserved(pfn_to_page(start_pfn + i)))
  55. return 1;
  56. return 0;
  57. }
  58. /*
  59. * Remap an arbitrary physical address space into the kernel virtual
  60. * address space. It transparently creates kernel huge I/O mapping when
  61. * the physical address is aligned by a huge page size (1GB or 2MB) and
  62. * the requested size is at least the huge page size.
  63. *
  64. * NOTE: MTRRs can override PAT memory types with a 4KB granularity.
  65. * Therefore, the mapping code falls back to use a smaller page toward 4KB
  66. * when a mapping range is covered by non-WB type of MTRRs.
  67. *
  68. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  69. * have to convert them into an offset in a page-aligned mapping, but the
  70. * caller shouldn't need to know that small detail.
  71. */
  72. static void __iomem *__ioremap_caller(resource_size_t phys_addr,
  73. unsigned long size, enum page_cache_mode pcm, void *caller)
  74. {
  75. unsigned long offset, vaddr;
  76. resource_size_t pfn, last_pfn, last_addr;
  77. const resource_size_t unaligned_phys_addr = phys_addr;
  78. const unsigned long unaligned_size = size;
  79. struct vm_struct *area;
  80. enum page_cache_mode new_pcm;
  81. pgprot_t prot;
  82. int retval;
  83. void __iomem *ret_addr;
  84. /* Don't allow wraparound or zero size */
  85. last_addr = phys_addr + size - 1;
  86. if (!size || last_addr < phys_addr)
  87. return NULL;
  88. if (!phys_addr_valid(phys_addr)) {
  89. printk(KERN_WARNING "ioremap: invalid physical address %llx\n",
  90. (unsigned long long)phys_addr);
  91. WARN_ON_ONCE(1);
  92. return NULL;
  93. }
  94. /*
  95. * Don't remap the low PCI/ISA area, it's always mapped..
  96. */
  97. if (is_ISA_range(phys_addr, last_addr))
  98. return (__force void __iomem *)phys_to_virt(phys_addr);
  99. /*
  100. * Don't allow anybody to remap normal RAM that we're using..
  101. */
  102. pfn = phys_addr >> PAGE_SHIFT;
  103. last_pfn = last_addr >> PAGE_SHIFT;
  104. if (walk_system_ram_range(pfn, last_pfn - pfn + 1, NULL,
  105. __ioremap_check_ram) == 1) {
  106. WARN_ONCE(1, "ioremap on RAM at %pa - %pa\n",
  107. &phys_addr, &last_addr);
  108. return NULL;
  109. }
  110. /*
  111. * Mappings have to be page-aligned
  112. */
  113. offset = phys_addr & ~PAGE_MASK;
  114. phys_addr &= PHYSICAL_PAGE_MASK;
  115. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  116. retval = reserve_memtype(phys_addr, (u64)phys_addr + size,
  117. pcm, &new_pcm);
  118. if (retval) {
  119. printk(KERN_ERR "ioremap reserve_memtype failed %d\n", retval);
  120. return NULL;
  121. }
  122. if (pcm != new_pcm) {
  123. if (!is_new_memtype_allowed(phys_addr, size, pcm, new_pcm)) {
  124. printk(KERN_ERR
  125. "ioremap error for 0x%llx-0x%llx, requested 0x%x, got 0x%x\n",
  126. (unsigned long long)phys_addr,
  127. (unsigned long long)(phys_addr + size),
  128. pcm, new_pcm);
  129. goto err_free_memtype;
  130. }
  131. pcm = new_pcm;
  132. }
  133. prot = PAGE_KERNEL_IO;
  134. switch (pcm) {
  135. case _PAGE_CACHE_MODE_UC:
  136. default:
  137. prot = __pgprot(pgprot_val(prot) |
  138. cachemode2protval(_PAGE_CACHE_MODE_UC));
  139. break;
  140. case _PAGE_CACHE_MODE_UC_MINUS:
  141. prot = __pgprot(pgprot_val(prot) |
  142. cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS));
  143. break;
  144. case _PAGE_CACHE_MODE_WC:
  145. prot = __pgprot(pgprot_val(prot) |
  146. cachemode2protval(_PAGE_CACHE_MODE_WC));
  147. break;
  148. case _PAGE_CACHE_MODE_WT:
  149. prot = __pgprot(pgprot_val(prot) |
  150. cachemode2protval(_PAGE_CACHE_MODE_WT));
  151. break;
  152. case _PAGE_CACHE_MODE_WB:
  153. break;
  154. }
  155. /*
  156. * Ok, go for it..
  157. */
  158. area = get_vm_area_caller(size, VM_IOREMAP, caller);
  159. if (!area)
  160. goto err_free_memtype;
  161. area->phys_addr = phys_addr;
  162. vaddr = (unsigned long) area->addr;
  163. if (kernel_map_sync_memtype(phys_addr, size, pcm))
  164. goto err_free_area;
  165. if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot))
  166. goto err_free_area;
  167. ret_addr = (void __iomem *) (vaddr + offset);
  168. mmiotrace_ioremap(unaligned_phys_addr, unaligned_size, ret_addr);
  169. /*
  170. * Check if the request spans more than any BAR in the iomem resource
  171. * tree.
  172. */
  173. if (iomem_map_sanity_check(unaligned_phys_addr, unaligned_size))
  174. pr_warn("caller %pS mapping multiple BARs\n", caller);
  175. return ret_addr;
  176. err_free_area:
  177. free_vm_area(area);
  178. err_free_memtype:
  179. free_memtype(phys_addr, phys_addr + size);
  180. return NULL;
  181. }
  182. /**
  183. * ioremap_nocache - map bus memory into CPU space
  184. * @phys_addr: bus address of the memory
  185. * @size: size of the resource to map
  186. *
  187. * ioremap_nocache performs a platform specific sequence of operations to
  188. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  189. * writew/writel functions and the other mmio helpers. The returned
  190. * address is not guaranteed to be usable directly as a virtual
  191. * address.
  192. *
  193. * This version of ioremap ensures that the memory is marked uncachable
  194. * on the CPU as well as honouring existing caching rules from things like
  195. * the PCI bus. Note that there are other caches and buffers on many
  196. * busses. In particular driver authors should read up on PCI writes
  197. *
  198. * It's useful if some control registers are in such an area and
  199. * write combining or read caching is not desirable:
  200. *
  201. * Must be freed with iounmap.
  202. */
  203. void __iomem *ioremap_nocache(resource_size_t phys_addr, unsigned long size)
  204. {
  205. /*
  206. * Ideally, this should be:
  207. * pat_enabled() ? _PAGE_CACHE_MODE_UC : _PAGE_CACHE_MODE_UC_MINUS;
  208. *
  209. * Till we fix all X drivers to use ioremap_wc(), we will use
  210. * UC MINUS. Drivers that are certain they need or can already
  211. * be converted over to strong UC can use ioremap_uc().
  212. */
  213. enum page_cache_mode pcm = _PAGE_CACHE_MODE_UC_MINUS;
  214. return __ioremap_caller(phys_addr, size, pcm,
  215. __builtin_return_address(0));
  216. }
  217. EXPORT_SYMBOL(ioremap_nocache);
  218. /**
  219. * ioremap_uc - map bus memory into CPU space as strongly uncachable
  220. * @phys_addr: bus address of the memory
  221. * @size: size of the resource to map
  222. *
  223. * ioremap_uc performs a platform specific sequence of operations to
  224. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  225. * writew/writel functions and the other mmio helpers. The returned
  226. * address is not guaranteed to be usable directly as a virtual
  227. * address.
  228. *
  229. * This version of ioremap ensures that the memory is marked with a strong
  230. * preference as completely uncachable on the CPU when possible. For non-PAT
  231. * systems this ends up setting page-attribute flags PCD=1, PWT=1. For PAT
  232. * systems this will set the PAT entry for the pages as strong UC. This call
  233. * will honor existing caching rules from things like the PCI bus. Note that
  234. * there are other caches and buffers on many busses. In particular driver
  235. * authors should read up on PCI writes.
  236. *
  237. * It's useful if some control registers are in such an area and
  238. * write combining or read caching is not desirable:
  239. *
  240. * Must be freed with iounmap.
  241. */
  242. void __iomem *ioremap_uc(resource_size_t phys_addr, unsigned long size)
  243. {
  244. enum page_cache_mode pcm = _PAGE_CACHE_MODE_UC;
  245. return __ioremap_caller(phys_addr, size, pcm,
  246. __builtin_return_address(0));
  247. }
  248. EXPORT_SYMBOL_GPL(ioremap_uc);
  249. /**
  250. * ioremap_wc - map memory into CPU space write combined
  251. * @phys_addr: bus address of the memory
  252. * @size: size of the resource to map
  253. *
  254. * This version of ioremap ensures that the memory is marked write combining.
  255. * Write combining allows faster writes to some hardware devices.
  256. *
  257. * Must be freed with iounmap.
  258. */
  259. void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size)
  260. {
  261. return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WC,
  262. __builtin_return_address(0));
  263. }
  264. EXPORT_SYMBOL(ioremap_wc);
  265. /**
  266. * ioremap_wt - map memory into CPU space write through
  267. * @phys_addr: bus address of the memory
  268. * @size: size of the resource to map
  269. *
  270. * This version of ioremap ensures that the memory is marked write through.
  271. * Write through stores data into memory while keeping the cache up-to-date.
  272. *
  273. * Must be freed with iounmap.
  274. */
  275. void __iomem *ioremap_wt(resource_size_t phys_addr, unsigned long size)
  276. {
  277. return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WT,
  278. __builtin_return_address(0));
  279. }
  280. EXPORT_SYMBOL(ioremap_wt);
  281. void __iomem *ioremap_cache(resource_size_t phys_addr, unsigned long size)
  282. {
  283. return __ioremap_caller(phys_addr, size, _PAGE_CACHE_MODE_WB,
  284. __builtin_return_address(0));
  285. }
  286. EXPORT_SYMBOL(ioremap_cache);
  287. void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size,
  288. unsigned long prot_val)
  289. {
  290. return __ioremap_caller(phys_addr, size,
  291. pgprot2cachemode(__pgprot(prot_val)),
  292. __builtin_return_address(0));
  293. }
  294. EXPORT_SYMBOL(ioremap_prot);
  295. /**
  296. * iounmap - Free a IO remapping
  297. * @addr: virtual address from ioremap_*
  298. *
  299. * Caller must ensure there is only one unmapping for the same pointer.
  300. */
  301. void iounmap(volatile void __iomem *addr)
  302. {
  303. struct vm_struct *p, *o;
  304. if ((void __force *)addr <= high_memory)
  305. return;
  306. /*
  307. * __ioremap special-cases the PCI/ISA range by not instantiating a
  308. * vm_area and by simply returning an address into the kernel mapping
  309. * of ISA space. So handle that here.
  310. */
  311. if ((void __force *)addr >= phys_to_virt(ISA_START_ADDRESS) &&
  312. (void __force *)addr < phys_to_virt(ISA_END_ADDRESS))
  313. return;
  314. mmiotrace_iounmap(addr);
  315. addr = (volatile void __iomem *)
  316. (PAGE_MASK & (unsigned long __force)addr);
  317. /* Use the vm area unlocked, assuming the caller
  318. ensures there isn't another iounmap for the same address
  319. in parallel. Reuse of the virtual address is prevented by
  320. leaving it in the global lists until we're done with it.
  321. cpa takes care of the direct mappings. */
  322. p = find_vm_area((void __force *)addr);
  323. if (!p) {
  324. printk(KERN_ERR "iounmap: bad address %p\n", addr);
  325. dump_stack();
  326. return;
  327. }
  328. free_memtype(p->phys_addr, p->phys_addr + get_vm_area_size(p));
  329. /* Finally remove it */
  330. o = remove_vm_area((void __force *)addr);
  331. BUG_ON(p != o || o == NULL);
  332. kfree(p);
  333. }
  334. EXPORT_SYMBOL(iounmap);
  335. int __init arch_ioremap_pud_supported(void)
  336. {
  337. #ifdef CONFIG_X86_64
  338. return boot_cpu_has(X86_FEATURE_GBPAGES);
  339. #else
  340. return 0;
  341. #endif
  342. }
  343. int __init arch_ioremap_pmd_supported(void)
  344. {
  345. return boot_cpu_has(X86_FEATURE_PSE);
  346. }
  347. /*
  348. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  349. * access
  350. */
  351. void *xlate_dev_mem_ptr(phys_addr_t phys)
  352. {
  353. unsigned long start = phys & PAGE_MASK;
  354. unsigned long offset = phys & ~PAGE_MASK;
  355. void *vaddr;
  356. /* If page is RAM, we can use __va. Otherwise ioremap and unmap. */
  357. if (page_is_ram(start >> PAGE_SHIFT))
  358. return __va(phys);
  359. vaddr = ioremap_cache(start, PAGE_SIZE);
  360. /* Only add the offset on success and return NULL if the ioremap() failed: */
  361. if (vaddr)
  362. vaddr += offset;
  363. return vaddr;
  364. }
  365. void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
  366. {
  367. if (page_is_ram(phys >> PAGE_SHIFT))
  368. return;
  369. iounmap((void __iomem *)((unsigned long)addr & PAGE_MASK));
  370. }
  371. static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss;
  372. static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
  373. {
  374. /* Don't assume we're using swapper_pg_dir at this point */
  375. pgd_t *base = __va(read_cr3());
  376. pgd_t *pgd = &base[pgd_index(addr)];
  377. pud_t *pud = pud_offset(pgd, addr);
  378. pmd_t *pmd = pmd_offset(pud, addr);
  379. return pmd;
  380. }
  381. static inline pte_t * __init early_ioremap_pte(unsigned long addr)
  382. {
  383. return &bm_pte[pte_index(addr)];
  384. }
  385. bool __init is_early_ioremap_ptep(pte_t *ptep)
  386. {
  387. return ptep >= &bm_pte[0] && ptep < &bm_pte[PAGE_SIZE/sizeof(pte_t)];
  388. }
  389. void __init early_ioremap_init(void)
  390. {
  391. pmd_t *pmd;
  392. #ifdef CONFIG_X86_64
  393. BUILD_BUG_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1));
  394. #else
  395. WARN_ON((fix_to_virt(0) + PAGE_SIZE) & ((1 << PMD_SHIFT) - 1));
  396. #endif
  397. early_ioremap_setup();
  398. pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
  399. memset(bm_pte, 0, sizeof(bm_pte));
  400. pmd_populate_kernel(&init_mm, pmd, bm_pte);
  401. /*
  402. * The boot-ioremap range spans multiple pmds, for which
  403. * we are not prepared:
  404. */
  405. #define __FIXADDR_TOP (-PAGE_SIZE)
  406. BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
  407. != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
  408. #undef __FIXADDR_TOP
  409. if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
  410. WARN_ON(1);
  411. printk(KERN_WARNING "pmd %p != %p\n",
  412. pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
  413. printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
  414. fix_to_virt(FIX_BTMAP_BEGIN));
  415. printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END): %08lx\n",
  416. fix_to_virt(FIX_BTMAP_END));
  417. printk(KERN_WARNING "FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
  418. printk(KERN_WARNING "FIX_BTMAP_BEGIN: %d\n",
  419. FIX_BTMAP_BEGIN);
  420. }
  421. }
  422. void __init __early_set_fixmap(enum fixed_addresses idx,
  423. phys_addr_t phys, pgprot_t flags)
  424. {
  425. unsigned long addr = __fix_to_virt(idx);
  426. pte_t *pte;
  427. if (idx >= __end_of_fixed_addresses) {
  428. BUG();
  429. return;
  430. }
  431. pte = early_ioremap_pte(addr);
  432. if (pgprot_val(flags))
  433. set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
  434. else
  435. pte_clear(&init_mm, addr, pte);
  436. __flush_tlb_one(addr);
  437. }