ioremap.c 12 KB

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