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