ioremap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 <linux/mmiotrace.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/e820.h>
  17. #include <asm/fixmap.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/tlbflush.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/pat.h>
  22. #include "physaddr.h"
  23. /*
  24. * Fix up the linear direct mapping of the kernel to avoid cache attribute
  25. * conflicts.
  26. */
  27. int ioremap_change_attr(unsigned long vaddr, unsigned long size,
  28. unsigned long prot_val)
  29. {
  30. unsigned long nrpages = size >> PAGE_SHIFT;
  31. int err;
  32. switch (prot_val) {
  33. case _PAGE_CACHE_UC:
  34. default:
  35. err = _set_memory_uc(vaddr, nrpages);
  36. break;
  37. case _PAGE_CACHE_WC:
  38. err = _set_memory_wc(vaddr, nrpages);
  39. break;
  40. case _PAGE_CACHE_WB:
  41. err = _set_memory_wb(vaddr, nrpages);
  42. break;
  43. }
  44. return err;
  45. }
  46. /*
  47. * Remap an arbitrary physical address space into the kernel virtual
  48. * address space. Needed when the kernel wants to access high addresses
  49. * directly.
  50. *
  51. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  52. * have to convert them into an offset in a page-aligned mapping, but the
  53. * caller shouldn't need to know that small detail.
  54. */
  55. static void __iomem *__ioremap_caller(resource_size_t phys_addr,
  56. unsigned long size, unsigned long prot_val, void *caller)
  57. {
  58. unsigned long offset, vaddr;
  59. resource_size_t pfn, last_pfn, last_addr;
  60. const resource_size_t unaligned_phys_addr = phys_addr;
  61. const unsigned long unaligned_size = size;
  62. struct vm_struct *area;
  63. unsigned long new_prot_val;
  64. pgprot_t prot;
  65. int retval;
  66. void __iomem *ret_addr;
  67. /* Don't allow wraparound or zero size */
  68. last_addr = phys_addr + size - 1;
  69. if (!size || last_addr < phys_addr)
  70. return NULL;
  71. if (!phys_addr_valid(phys_addr)) {
  72. printk(KERN_WARNING "ioremap: invalid physical address %llx\n",
  73. (unsigned long long)phys_addr);
  74. WARN_ON_ONCE(1);
  75. return NULL;
  76. }
  77. /*
  78. * Don't remap the low PCI/ISA area, it's always mapped..
  79. */
  80. if (is_ISA_range(phys_addr, last_addr))
  81. return (__force void __iomem *)phys_to_virt(phys_addr);
  82. /*
  83. * Check if the request spans more than any BAR in the iomem resource
  84. * tree.
  85. */
  86. WARN_ONCE(iomem_map_sanity_check(phys_addr, size),
  87. KERN_INFO "Info: mapping multiple BARs. Your kernel is fine.");
  88. /*
  89. * Don't allow anybody to remap normal RAM that we're using..
  90. */
  91. last_pfn = last_addr >> PAGE_SHIFT;
  92. for (pfn = phys_addr >> PAGE_SHIFT; pfn <= last_pfn; pfn++) {
  93. int is_ram = page_is_ram(pfn);
  94. if (is_ram && pfn_valid(pfn) && !PageReserved(pfn_to_page(pfn)))
  95. return NULL;
  96. WARN_ON_ONCE(is_ram);
  97. }
  98. /*
  99. * Mappings have to be page-aligned
  100. */
  101. offset = phys_addr & ~PAGE_MASK;
  102. phys_addr &= PHYSICAL_PAGE_MASK;
  103. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  104. retval = reserve_memtype(phys_addr, (u64)phys_addr + size,
  105. prot_val, &new_prot_val);
  106. if (retval) {
  107. printk(KERN_ERR "ioremap reserve_memtype failed %d\n", retval);
  108. return NULL;
  109. }
  110. if (prot_val != new_prot_val) {
  111. if (!is_new_memtype_allowed(phys_addr, size,
  112. prot_val, new_prot_val)) {
  113. printk(KERN_ERR
  114. "ioremap error for 0x%llx-0x%llx, requested 0x%lx, got 0x%lx\n",
  115. (unsigned long long)phys_addr,
  116. (unsigned long long)(phys_addr + size),
  117. prot_val, new_prot_val);
  118. goto err_free_memtype;
  119. }
  120. prot_val = new_prot_val;
  121. }
  122. switch (prot_val) {
  123. case _PAGE_CACHE_UC:
  124. default:
  125. prot = PAGE_KERNEL_IO_NOCACHE;
  126. break;
  127. case _PAGE_CACHE_UC_MINUS:
  128. prot = PAGE_KERNEL_IO_UC_MINUS;
  129. break;
  130. case _PAGE_CACHE_WC:
  131. prot = PAGE_KERNEL_IO_WC;
  132. break;
  133. case _PAGE_CACHE_WB:
  134. prot = PAGE_KERNEL_IO;
  135. break;
  136. }
  137. /*
  138. * Ok, go for it..
  139. */
  140. area = get_vm_area_caller(size, VM_IOREMAP, caller);
  141. if (!area)
  142. goto err_free_memtype;
  143. area->phys_addr = phys_addr;
  144. vaddr = (unsigned long) area->addr;
  145. if (kernel_map_sync_memtype(phys_addr, size, prot_val))
  146. goto err_free_area;
  147. if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot))
  148. goto err_free_area;
  149. ret_addr = (void __iomem *) (vaddr + offset);
  150. mmiotrace_ioremap(unaligned_phys_addr, unaligned_size, ret_addr);
  151. return ret_addr;
  152. err_free_area:
  153. free_vm_area(area);
  154. err_free_memtype:
  155. free_memtype(phys_addr, phys_addr + size);
  156. return NULL;
  157. }
  158. /**
  159. * ioremap_nocache - map bus memory into CPU space
  160. * @offset: bus address of the memory
  161. * @size: size of the resource to map
  162. *
  163. * ioremap_nocache performs a platform specific sequence of operations to
  164. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  165. * writew/writel functions and the other mmio helpers. The returned
  166. * address is not guaranteed to be usable directly as a virtual
  167. * address.
  168. *
  169. * This version of ioremap ensures that the memory is marked uncachable
  170. * on the CPU as well as honouring existing caching rules from things like
  171. * the PCI bus. Note that there are other caches and buffers on many
  172. * busses. In particular driver authors should read up on PCI writes
  173. *
  174. * It's useful if some control registers are in such an area and
  175. * write combining or read caching is not desirable:
  176. *
  177. * Must be freed with iounmap.
  178. */
  179. void __iomem *ioremap_nocache(resource_size_t phys_addr, unsigned long size)
  180. {
  181. /*
  182. * Ideally, this should be:
  183. * pat_enabled ? _PAGE_CACHE_UC : _PAGE_CACHE_UC_MINUS;
  184. *
  185. * Till we fix all X drivers to use ioremap_wc(), we will use
  186. * UC MINUS.
  187. */
  188. unsigned long val = _PAGE_CACHE_UC_MINUS;
  189. return __ioremap_caller(phys_addr, size, val,
  190. __builtin_return_address(0));
  191. }
  192. EXPORT_SYMBOL(ioremap_nocache);
  193. /**
  194. * ioremap_wc - map memory into CPU space write combined
  195. * @offset: bus address of the memory
  196. * @size: size of the resource to map
  197. *
  198. * This version of ioremap ensures that the memory is marked write combining.
  199. * Write combining allows faster writes to some hardware devices.
  200. *
  201. * Must be freed with iounmap.
  202. */
  203. void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size)
  204. {
  205. if (pat_enabled)
  206. return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WC,
  207. __builtin_return_address(0));
  208. else
  209. return ioremap_nocache(phys_addr, size);
  210. }
  211. EXPORT_SYMBOL(ioremap_wc);
  212. void __iomem *ioremap_cache(resource_size_t phys_addr, unsigned long size)
  213. {
  214. return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WB,
  215. __builtin_return_address(0));
  216. }
  217. EXPORT_SYMBOL(ioremap_cache);
  218. void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size,
  219. unsigned long prot_val)
  220. {
  221. return __ioremap_caller(phys_addr, size, (prot_val & _PAGE_CACHE_MASK),
  222. __builtin_return_address(0));
  223. }
  224. EXPORT_SYMBOL(ioremap_prot);
  225. /**
  226. * iounmap - Free a IO remapping
  227. * @addr: virtual address from ioremap_*
  228. *
  229. * Caller must ensure there is only one unmapping for the same pointer.
  230. */
  231. void iounmap(volatile void __iomem *addr)
  232. {
  233. struct vm_struct *p, *o;
  234. if ((void __force *)addr <= high_memory)
  235. return;
  236. /*
  237. * __ioremap special-cases the PCI/ISA range by not instantiating a
  238. * vm_area and by simply returning an address into the kernel mapping
  239. * of ISA space. So handle that here.
  240. */
  241. if ((void __force *)addr >= phys_to_virt(ISA_START_ADDRESS) &&
  242. (void __force *)addr < phys_to_virt(ISA_END_ADDRESS))
  243. return;
  244. addr = (volatile void __iomem *)
  245. (PAGE_MASK & (unsigned long __force)addr);
  246. mmiotrace_iounmap(addr);
  247. /* Use the vm area unlocked, assuming the caller
  248. ensures there isn't another iounmap for the same address
  249. in parallel. Reuse of the virtual address is prevented by
  250. leaving it in the global lists until we're done with it.
  251. cpa takes care of the direct mappings. */
  252. read_lock(&vmlist_lock);
  253. for (p = vmlist; p; p = p->next) {
  254. if (p->addr == (void __force *)addr)
  255. break;
  256. }
  257. read_unlock(&vmlist_lock);
  258. if (!p) {
  259. printk(KERN_ERR "iounmap: bad address %p\n", addr);
  260. dump_stack();
  261. return;
  262. }
  263. free_memtype(p->phys_addr, p->phys_addr + get_vm_area_size(p));
  264. /* Finally remove it */
  265. o = remove_vm_area((void __force *)addr);
  266. BUG_ON(p != o || o == NULL);
  267. kfree(p);
  268. }
  269. EXPORT_SYMBOL(iounmap);
  270. /*
  271. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  272. * access
  273. */
  274. void *xlate_dev_mem_ptr(unsigned long phys)
  275. {
  276. void *addr;
  277. unsigned long start = phys & PAGE_MASK;
  278. /* If page is RAM, we can use __va. Otherwise ioremap and unmap. */
  279. if (page_is_ram(start >> PAGE_SHIFT))
  280. return __va(phys);
  281. addr = (void __force *)ioremap_cache(start, PAGE_SIZE);
  282. if (addr)
  283. addr = (void *)((unsigned long)addr | (phys & ~PAGE_MASK));
  284. return addr;
  285. }
  286. void unxlate_dev_mem_ptr(unsigned long phys, void *addr)
  287. {
  288. if (page_is_ram(phys >> PAGE_SHIFT))
  289. return;
  290. iounmap((void __iomem *)((unsigned long)addr & PAGE_MASK));
  291. return;
  292. }
  293. static int __initdata early_ioremap_debug;
  294. static int __init early_ioremap_debug_setup(char *str)
  295. {
  296. early_ioremap_debug = 1;
  297. return 0;
  298. }
  299. early_param("early_ioremap_debug", early_ioremap_debug_setup);
  300. static __initdata int after_paging_init;
  301. static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss;
  302. static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
  303. {
  304. /* Don't assume we're using swapper_pg_dir at this point */
  305. pgd_t *base = __va(read_cr3());
  306. pgd_t *pgd = &base[pgd_index(addr)];
  307. pud_t *pud = pud_offset(pgd, addr);
  308. pmd_t *pmd = pmd_offset(pud, addr);
  309. return pmd;
  310. }
  311. static inline pte_t * __init early_ioremap_pte(unsigned long addr)
  312. {
  313. return &bm_pte[pte_index(addr)];
  314. }
  315. static unsigned long slot_virt[FIX_BTMAPS_SLOTS] __initdata;
  316. void __init early_ioremap_init(void)
  317. {
  318. pmd_t *pmd;
  319. int i;
  320. if (early_ioremap_debug)
  321. printk(KERN_INFO "early_ioremap_init()\n");
  322. for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
  323. slot_virt[i] = __fix_to_virt(FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*i);
  324. pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
  325. memset(bm_pte, 0, sizeof(bm_pte));
  326. pmd_populate_kernel(&init_mm, pmd, bm_pte);
  327. /*
  328. * The boot-ioremap range spans multiple pmds, for which
  329. * we are not prepared:
  330. */
  331. #define __FIXADDR_TOP (-PAGE_SIZE)
  332. BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
  333. != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
  334. #undef __FIXADDR_TOP
  335. if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
  336. WARN_ON(1);
  337. printk(KERN_WARNING "pmd %p != %p\n",
  338. pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
  339. printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
  340. fix_to_virt(FIX_BTMAP_BEGIN));
  341. printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END): %08lx\n",
  342. fix_to_virt(FIX_BTMAP_END));
  343. printk(KERN_WARNING "FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
  344. printk(KERN_WARNING "FIX_BTMAP_BEGIN: %d\n",
  345. FIX_BTMAP_BEGIN);
  346. }
  347. }
  348. void __init early_ioremap_reset(void)
  349. {
  350. after_paging_init = 1;
  351. }
  352. static void __init __early_set_fixmap(enum fixed_addresses idx,
  353. phys_addr_t phys, pgprot_t flags)
  354. {
  355. unsigned long addr = __fix_to_virt(idx);
  356. pte_t *pte;
  357. if (idx >= __end_of_fixed_addresses) {
  358. BUG();
  359. return;
  360. }
  361. pte = early_ioremap_pte(addr);
  362. if (pgprot_val(flags))
  363. set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
  364. else
  365. pte_clear(&init_mm, addr, pte);
  366. __flush_tlb_one(addr);
  367. }
  368. static inline void __init early_set_fixmap(enum fixed_addresses idx,
  369. phys_addr_t phys, pgprot_t prot)
  370. {
  371. if (after_paging_init)
  372. __set_fixmap(idx, phys, prot);
  373. else
  374. __early_set_fixmap(idx, phys, prot);
  375. }
  376. static inline void __init early_clear_fixmap(enum fixed_addresses idx)
  377. {
  378. if (after_paging_init)
  379. clear_fixmap(idx);
  380. else
  381. __early_set_fixmap(idx, 0, __pgprot(0));
  382. }
  383. static void __iomem *prev_map[FIX_BTMAPS_SLOTS] __initdata;
  384. static unsigned long prev_size[FIX_BTMAPS_SLOTS] __initdata;
  385. void __init fixup_early_ioremap(void)
  386. {
  387. int i;
  388. for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
  389. if (prev_map[i]) {
  390. WARN_ON(1);
  391. break;
  392. }
  393. }
  394. early_ioremap_init();
  395. }
  396. static int __init check_early_ioremap_leak(void)
  397. {
  398. int count = 0;
  399. int i;
  400. for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
  401. if (prev_map[i])
  402. count++;
  403. if (!count)
  404. return 0;
  405. WARN(1, KERN_WARNING
  406. "Debug warning: early ioremap leak of %d areas detected.\n",
  407. count);
  408. printk(KERN_WARNING
  409. "please boot with early_ioremap_debug and report the dmesg.\n");
  410. return 1;
  411. }
  412. late_initcall(check_early_ioremap_leak);
  413. static void __init __iomem *
  414. __early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot)
  415. {
  416. unsigned long offset;
  417. resource_size_t last_addr;
  418. unsigned int nrpages;
  419. enum fixed_addresses idx0, idx;
  420. int i, slot;
  421. WARN_ON(system_state != SYSTEM_BOOTING);
  422. slot = -1;
  423. for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
  424. if (!prev_map[i]) {
  425. slot = i;
  426. break;
  427. }
  428. }
  429. if (slot < 0) {
  430. printk(KERN_INFO "early_iomap(%08llx, %08lx) not found slot\n",
  431. (u64)phys_addr, size);
  432. WARN_ON(1);
  433. return NULL;
  434. }
  435. if (early_ioremap_debug) {
  436. printk(KERN_INFO "early_ioremap(%08llx, %08lx) [%d] => ",
  437. (u64)phys_addr, size, slot);
  438. dump_stack();
  439. }
  440. /* Don't allow wraparound or zero size */
  441. last_addr = phys_addr + size - 1;
  442. if (!size || last_addr < phys_addr) {
  443. WARN_ON(1);
  444. return NULL;
  445. }
  446. prev_size[slot] = size;
  447. /*
  448. * Mappings have to be page-aligned
  449. */
  450. offset = phys_addr & ~PAGE_MASK;
  451. phys_addr &= PAGE_MASK;
  452. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  453. /*
  454. * Mappings have to fit in the FIX_BTMAP area.
  455. */
  456. nrpages = size >> PAGE_SHIFT;
  457. if (nrpages > NR_FIX_BTMAPS) {
  458. WARN_ON(1);
  459. return NULL;
  460. }
  461. /*
  462. * Ok, go for it..
  463. */
  464. idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
  465. idx = idx0;
  466. while (nrpages > 0) {
  467. early_set_fixmap(idx, phys_addr, prot);
  468. phys_addr += PAGE_SIZE;
  469. --idx;
  470. --nrpages;
  471. }
  472. if (early_ioremap_debug)
  473. printk(KERN_CONT "%08lx + %08lx\n", offset, slot_virt[slot]);
  474. prev_map[slot] = (void __iomem *)(offset + slot_virt[slot]);
  475. return prev_map[slot];
  476. }
  477. /* Remap an IO device */
  478. void __init __iomem *
  479. early_ioremap(resource_size_t phys_addr, unsigned long size)
  480. {
  481. return __early_ioremap(phys_addr, size, PAGE_KERNEL_IO);
  482. }
  483. /* Remap memory */
  484. void __init __iomem *
  485. early_memremap(resource_size_t phys_addr, unsigned long size)
  486. {
  487. return __early_ioremap(phys_addr, size, PAGE_KERNEL);
  488. }
  489. void __init early_iounmap(void __iomem *addr, unsigned long size)
  490. {
  491. unsigned long virt_addr;
  492. unsigned long offset;
  493. unsigned int nrpages;
  494. enum fixed_addresses idx;
  495. int i, slot;
  496. slot = -1;
  497. for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
  498. if (prev_map[i] == addr) {
  499. slot = i;
  500. break;
  501. }
  502. }
  503. if (slot < 0) {
  504. printk(KERN_INFO "early_iounmap(%p, %08lx) not found slot\n",
  505. addr, size);
  506. WARN_ON(1);
  507. return;
  508. }
  509. if (prev_size[slot] != size) {
  510. printk(KERN_INFO "early_iounmap(%p, %08lx) [%d] size not consistent %08lx\n",
  511. addr, size, slot, prev_size[slot]);
  512. WARN_ON(1);
  513. return;
  514. }
  515. if (early_ioremap_debug) {
  516. printk(KERN_INFO "early_iounmap(%p, %08lx) [%d]\n", addr,
  517. size, slot);
  518. dump_stack();
  519. }
  520. virt_addr = (unsigned long)addr;
  521. if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
  522. WARN_ON(1);
  523. return;
  524. }
  525. offset = virt_addr & ~PAGE_MASK;
  526. nrpages = PAGE_ALIGN(offset + size) >> PAGE_SHIFT;
  527. idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
  528. while (nrpages > 0) {
  529. early_clear_fixmap(idx);
  530. --idx;
  531. --nrpages;
  532. }
  533. prev_map[slot] = NULL;
  534. }