ioremap.c 13 KB

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