ioremap.c 12 KB

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