ioremap.c 12 KB

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