ioremap.c 12 KB

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