ioremap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. enum ioremap_mode {
  21. IOR_MODE_UNCACHED,
  22. IOR_MODE_CACHED,
  23. };
  24. #ifdef CONFIG_X86_64
  25. unsigned long __phys_addr(unsigned long x)
  26. {
  27. if (x >= __START_KERNEL_map)
  28. return x - __START_KERNEL_map + phys_base;
  29. return x - PAGE_OFFSET;
  30. }
  31. EXPORT_SYMBOL(__phys_addr);
  32. #endif
  33. int page_is_ram(unsigned long pagenr)
  34. {
  35. unsigned long addr, end;
  36. int i;
  37. /*
  38. * A special case is the first 4Kb of memory;
  39. * This is a BIOS owned area, not kernel ram, but generally
  40. * not listed as such in the E820 table.
  41. */
  42. if (pagenr == 0)
  43. return 0;
  44. /*
  45. * Second special case: Some BIOSen report the PC BIOS
  46. * area (640->1Mb) as ram even though it is not.
  47. */
  48. if (pagenr >= (BIOS_BEGIN >> PAGE_SHIFT) &&
  49. pagenr < (BIOS_END >> PAGE_SHIFT))
  50. return 0;
  51. for (i = 0; i < e820.nr_map; i++) {
  52. /*
  53. * Not usable memory:
  54. */
  55. if (e820.map[i].type != E820_RAM)
  56. continue;
  57. addr = (e820.map[i].addr + PAGE_SIZE-1) >> PAGE_SHIFT;
  58. end = (e820.map[i].addr + e820.map[i].size) >> PAGE_SHIFT;
  59. if ((pagenr >= addr) && (pagenr < end))
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. /*
  65. * Fix up the linear direct mapping of the kernel to avoid cache attribute
  66. * conflicts.
  67. */
  68. static int ioremap_change_attr(unsigned long vaddr, unsigned long size,
  69. enum ioremap_mode mode)
  70. {
  71. unsigned long nrpages = size >> PAGE_SHIFT;
  72. int err;
  73. switch (mode) {
  74. case IOR_MODE_UNCACHED:
  75. default:
  76. err = set_memory_uc(vaddr, nrpages);
  77. break;
  78. case IOR_MODE_CACHED:
  79. err = set_memory_wb(vaddr, nrpages);
  80. break;
  81. }
  82. return err;
  83. }
  84. /*
  85. * Remap an arbitrary physical address space into the kernel virtual
  86. * address space. Needed when the kernel wants to access high addresses
  87. * directly.
  88. *
  89. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  90. * have to convert them into an offset in a page-aligned mapping, but the
  91. * caller shouldn't need to know that small detail.
  92. */
  93. static void __iomem *__ioremap(unsigned long phys_addr, unsigned long size,
  94. enum ioremap_mode mode)
  95. {
  96. unsigned long pfn, offset, last_addr, vaddr;
  97. struct vm_struct *area;
  98. pgprot_t prot;
  99. /* Don't allow wraparound or zero size */
  100. last_addr = phys_addr + size - 1;
  101. if (!size || last_addr < phys_addr)
  102. return NULL;
  103. /*
  104. * Don't remap the low PCI/ISA area, it's always mapped..
  105. */
  106. if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
  107. return (__force void __iomem *)phys_to_virt(phys_addr);
  108. /*
  109. * Don't allow anybody to remap normal RAM that we're using..
  110. */
  111. for (pfn = phys_addr >> PAGE_SHIFT; pfn < max_pfn_mapped &&
  112. (pfn << PAGE_SHIFT) < last_addr; pfn++) {
  113. if (page_is_ram(pfn) && pfn_valid(pfn) &&
  114. !PageReserved(pfn_to_page(pfn)))
  115. return NULL;
  116. }
  117. WARN_ON_ONCE(page_is_ram(pfn));
  118. switch (mode) {
  119. case IOR_MODE_UNCACHED:
  120. default:
  121. prot = PAGE_KERNEL_NOCACHE;
  122. break;
  123. case IOR_MODE_CACHED:
  124. prot = PAGE_KERNEL;
  125. break;
  126. }
  127. /*
  128. * Mappings have to be page-aligned
  129. */
  130. offset = phys_addr & ~PAGE_MASK;
  131. phys_addr &= PAGE_MASK;
  132. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  133. /*
  134. * Ok, go for it..
  135. */
  136. area = get_vm_area(size, VM_IOREMAP);
  137. if (!area)
  138. return NULL;
  139. area->phys_addr = phys_addr;
  140. vaddr = (unsigned long) area->addr;
  141. if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot)) {
  142. remove_vm_area((void *)(vaddr & PAGE_MASK));
  143. return NULL;
  144. }
  145. if (ioremap_change_attr(vaddr, size, mode) < 0) {
  146. vunmap(area->addr);
  147. return NULL;
  148. }
  149. return (void __iomem *) (vaddr + offset);
  150. }
  151. /**
  152. * ioremap_nocache - map bus memory into CPU space
  153. * @offset: bus address of the memory
  154. * @size: size of the resource to map
  155. *
  156. * ioremap_nocache performs a platform specific sequence of operations to
  157. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  158. * writew/writel functions and the other mmio helpers. The returned
  159. * address is not guaranteed to be usable directly as a virtual
  160. * address.
  161. *
  162. * This version of ioremap ensures that the memory is marked uncachable
  163. * on the CPU as well as honouring existing caching rules from things like
  164. * the PCI bus. Note that there are other caches and buffers on many
  165. * busses. In particular driver authors should read up on PCI writes
  166. *
  167. * It's useful if some control registers are in such an area and
  168. * write combining or read caching is not desirable:
  169. *
  170. * Must be freed with iounmap.
  171. */
  172. void __iomem *ioremap_nocache(unsigned long phys_addr, unsigned long size)
  173. {
  174. return __ioremap(phys_addr, size, IOR_MODE_UNCACHED);
  175. }
  176. EXPORT_SYMBOL(ioremap_nocache);
  177. void __iomem *ioremap_cache(unsigned long phys_addr, unsigned long size)
  178. {
  179. return __ioremap(phys_addr, size, IOR_MODE_CACHED);
  180. }
  181. EXPORT_SYMBOL(ioremap_cache);
  182. /**
  183. * iounmap - Free a IO remapping
  184. * @addr: virtual address from ioremap_*
  185. *
  186. * Caller must ensure there is only one unmapping for the same pointer.
  187. */
  188. void iounmap(volatile void __iomem *addr)
  189. {
  190. struct vm_struct *p, *o;
  191. if ((void __force *)addr <= high_memory)
  192. return;
  193. /*
  194. * __ioremap special-cases the PCI/ISA range by not instantiating a
  195. * vm_area and by simply returning an address into the kernel mapping
  196. * of ISA space. So handle that here.
  197. */
  198. if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
  199. addr < phys_to_virt(ISA_END_ADDRESS))
  200. return;
  201. addr = (volatile void __iomem *)
  202. (PAGE_MASK & (unsigned long __force)addr);
  203. /* Use the vm area unlocked, assuming the caller
  204. ensures there isn't another iounmap for the same address
  205. in parallel. Reuse of the virtual address is prevented by
  206. leaving it in the global lists until we're done with it.
  207. cpa takes care of the direct mappings. */
  208. read_lock(&vmlist_lock);
  209. for (p = vmlist; p; p = p->next) {
  210. if (p->addr == addr)
  211. break;
  212. }
  213. read_unlock(&vmlist_lock);
  214. if (!p) {
  215. printk(KERN_ERR "iounmap: bad address %p\n", addr);
  216. dump_stack();
  217. return;
  218. }
  219. /* Finally remove it */
  220. o = remove_vm_area((void *)addr);
  221. BUG_ON(p != o || o == NULL);
  222. kfree(p);
  223. }
  224. EXPORT_SYMBOL(iounmap);
  225. #ifdef CONFIG_X86_32
  226. int __initdata early_ioremap_debug;
  227. static int __init early_ioremap_debug_setup(char *str)
  228. {
  229. early_ioremap_debug = 1;
  230. return 0;
  231. }
  232. early_param("early_ioremap_debug", early_ioremap_debug_setup);
  233. static __initdata int after_paging_init;
  234. static __initdata pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)]
  235. __attribute__((aligned(PAGE_SIZE)));
  236. static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
  237. {
  238. /* Don't assume we're using swapper_pg_dir at this point */
  239. pgd_t *base = __va(read_cr3());
  240. pgd_t *pgd = &base[pgd_index(addr)];
  241. pud_t *pud = pud_offset(pgd, addr);
  242. pmd_t *pmd = pmd_offset(pud, addr);
  243. return pmd;
  244. }
  245. static inline pte_t * __init early_ioremap_pte(unsigned long addr)
  246. {
  247. return &bm_pte[pte_index(addr)];
  248. }
  249. void __init early_ioremap_init(void)
  250. {
  251. pmd_t *pmd;
  252. if (early_ioremap_debug)
  253. printk(KERN_INFO "early_ioremap_init()\n");
  254. pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
  255. memset(bm_pte, 0, sizeof(bm_pte));
  256. pmd_populate_kernel(&init_mm, pmd, bm_pte);
  257. /*
  258. * The boot-ioremap range spans multiple pmds, for which
  259. * we are not prepared:
  260. */
  261. if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
  262. WARN_ON(1);
  263. printk(KERN_WARNING "pmd %p != %p\n",
  264. pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
  265. printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
  266. fix_to_virt(FIX_BTMAP_BEGIN));
  267. printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END): %08lx\n",
  268. fix_to_virt(FIX_BTMAP_END));
  269. printk(KERN_WARNING "FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
  270. printk(KERN_WARNING "FIX_BTMAP_BEGIN: %d\n",
  271. FIX_BTMAP_BEGIN);
  272. }
  273. }
  274. void __init early_ioremap_clear(void)
  275. {
  276. pmd_t *pmd;
  277. if (early_ioremap_debug)
  278. printk(KERN_INFO "early_ioremap_clear()\n");
  279. pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
  280. pmd_clear(pmd);
  281. paravirt_release_pt(__pa(bm_pte) >> PAGE_SHIFT);
  282. __flush_tlb_all();
  283. }
  284. void __init early_ioremap_reset(void)
  285. {
  286. enum fixed_addresses idx;
  287. unsigned long addr, phys;
  288. pte_t *pte;
  289. after_paging_init = 1;
  290. for (idx = FIX_BTMAP_BEGIN; idx >= FIX_BTMAP_END; idx--) {
  291. addr = fix_to_virt(idx);
  292. pte = early_ioremap_pte(addr);
  293. if (pte_present(*pte)) {
  294. phys = pte_val(*pte) & PAGE_MASK;
  295. set_fixmap(idx, phys);
  296. }
  297. }
  298. }
  299. static void __init __early_set_fixmap(enum fixed_addresses idx,
  300. unsigned long phys, pgprot_t flags)
  301. {
  302. unsigned long addr = __fix_to_virt(idx);
  303. pte_t *pte;
  304. if (idx >= __end_of_fixed_addresses) {
  305. BUG();
  306. return;
  307. }
  308. pte = early_ioremap_pte(addr);
  309. if (pgprot_val(flags))
  310. set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
  311. else
  312. pte_clear(NULL, addr, pte);
  313. __flush_tlb_one(addr);
  314. }
  315. static inline void __init early_set_fixmap(enum fixed_addresses idx,
  316. unsigned long phys)
  317. {
  318. if (after_paging_init)
  319. set_fixmap(idx, phys);
  320. else
  321. __early_set_fixmap(idx, phys, PAGE_KERNEL);
  322. }
  323. static inline void __init early_clear_fixmap(enum fixed_addresses idx)
  324. {
  325. if (after_paging_init)
  326. clear_fixmap(idx);
  327. else
  328. __early_set_fixmap(idx, 0, __pgprot(0));
  329. }
  330. int __initdata early_ioremap_nested;
  331. static int __init check_early_ioremap_leak(void)
  332. {
  333. if (!early_ioremap_nested)
  334. return 0;
  335. printk(KERN_WARNING
  336. "Debug warning: early ioremap leak of %d areas detected.\n",
  337. early_ioremap_nested);
  338. printk(KERN_WARNING
  339. "please boot with early_ioremap_debug and report the dmesg.\n");
  340. WARN_ON(1);
  341. return 1;
  342. }
  343. late_initcall(check_early_ioremap_leak);
  344. void __init *early_ioremap(unsigned long phys_addr, unsigned long size)
  345. {
  346. unsigned long offset, last_addr;
  347. unsigned int nrpages, nesting;
  348. enum fixed_addresses idx0, idx;
  349. WARN_ON(system_state != SYSTEM_BOOTING);
  350. nesting = early_ioremap_nested;
  351. if (early_ioremap_debug) {
  352. printk(KERN_INFO "early_ioremap(%08lx, %08lx) [%d] => ",
  353. phys_addr, size, nesting);
  354. dump_stack();
  355. }
  356. /* Don't allow wraparound or zero size */
  357. last_addr = phys_addr + size - 1;
  358. if (!size || last_addr < phys_addr) {
  359. WARN_ON(1);
  360. return NULL;
  361. }
  362. if (nesting >= FIX_BTMAPS_NESTING) {
  363. WARN_ON(1);
  364. return NULL;
  365. }
  366. early_ioremap_nested++;
  367. /*
  368. * Mappings have to be page-aligned
  369. */
  370. offset = phys_addr & ~PAGE_MASK;
  371. phys_addr &= PAGE_MASK;
  372. size = PAGE_ALIGN(last_addr) - phys_addr;
  373. /*
  374. * Mappings have to fit in the FIX_BTMAP area.
  375. */
  376. nrpages = size >> PAGE_SHIFT;
  377. if (nrpages > NR_FIX_BTMAPS) {
  378. WARN_ON(1);
  379. return NULL;
  380. }
  381. /*
  382. * Ok, go for it..
  383. */
  384. idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
  385. idx = idx0;
  386. while (nrpages > 0) {
  387. early_set_fixmap(idx, phys_addr);
  388. phys_addr += PAGE_SIZE;
  389. --idx;
  390. --nrpages;
  391. }
  392. if (early_ioremap_debug)
  393. printk(KERN_CONT "%08lx + %08lx\n", offset, fix_to_virt(idx0));
  394. return (void *) (offset + fix_to_virt(idx0));
  395. }
  396. void __init early_iounmap(void *addr, unsigned long size)
  397. {
  398. unsigned long virt_addr;
  399. unsigned long offset;
  400. unsigned int nrpages;
  401. enum fixed_addresses idx;
  402. unsigned int nesting;
  403. nesting = --early_ioremap_nested;
  404. WARN_ON(nesting < 0);
  405. if (early_ioremap_debug) {
  406. printk(KERN_INFO "early_iounmap(%p, %08lx) [%d]\n", addr,
  407. size, nesting);
  408. dump_stack();
  409. }
  410. virt_addr = (unsigned long)addr;
  411. if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
  412. WARN_ON(1);
  413. return;
  414. }
  415. offset = virt_addr & ~PAGE_MASK;
  416. nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
  417. idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
  418. while (nrpages > 0) {
  419. early_clear_fixmap(idx);
  420. --idx;
  421. --nrpages;
  422. }
  423. }
  424. void __this_fixmap_does_not_exist(void)
  425. {
  426. WARN_ON(1);
  427. }
  428. #endif /* CONFIG_X86_32 */