ioremap.c 14 KB

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