ioremap.c 12 KB

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