ioremap.c 12 KB

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