ioremap.c 11 KB

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