pgtable.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/sched.h>
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/mm.h>
  18. #include <linux/swap.h>
  19. #include <linux/highmem.h>
  20. #include <linux/slab.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/cpumask.h>
  24. #include <linux/module.h>
  25. #include <linux/io.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/smp.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/pgalloc.h>
  30. #include <asm/fixmap.h>
  31. #include <asm/tlb.h>
  32. #include <asm/tlbflush.h>
  33. #include <asm/homecache.h>
  34. #define K(x) ((x) << (PAGE_SHIFT-10))
  35. /*
  36. * The normal show_free_areas() is too verbose on Tile, with dozens
  37. * of processors and often four NUMA zones each with high and lowmem.
  38. */
  39. void show_mem(unsigned int filter)
  40. {
  41. struct zone *zone;
  42. pr_err("Active:%lu inactive:%lu dirty:%lu writeback:%lu unstable:%lu"
  43. " free:%lu\n slab:%lu mapped:%lu pagetables:%lu bounce:%lu"
  44. " pagecache:%lu swap:%lu\n",
  45. (global_page_state(NR_ACTIVE_ANON) +
  46. global_page_state(NR_ACTIVE_FILE)),
  47. (global_page_state(NR_INACTIVE_ANON) +
  48. global_page_state(NR_INACTIVE_FILE)),
  49. global_page_state(NR_FILE_DIRTY),
  50. global_page_state(NR_WRITEBACK),
  51. global_page_state(NR_UNSTABLE_NFS),
  52. global_page_state(NR_FREE_PAGES),
  53. (global_page_state(NR_SLAB_RECLAIMABLE) +
  54. global_page_state(NR_SLAB_UNRECLAIMABLE)),
  55. global_page_state(NR_FILE_MAPPED),
  56. global_page_state(NR_PAGETABLE),
  57. global_page_state(NR_BOUNCE),
  58. global_page_state(NR_FILE_PAGES),
  59. get_nr_swap_pages());
  60. for_each_zone(zone) {
  61. unsigned long flags, order, total = 0, largest_order = -1;
  62. if (!populated_zone(zone))
  63. continue;
  64. spin_lock_irqsave(&zone->lock, flags);
  65. for (order = 0; order < MAX_ORDER; order++) {
  66. int nr = zone->free_area[order].nr_free;
  67. total += nr << order;
  68. if (nr)
  69. largest_order = order;
  70. }
  71. spin_unlock_irqrestore(&zone->lock, flags);
  72. pr_err("Node %d %7s: %lukB (largest %luKb)\n",
  73. zone_to_nid(zone), zone->name,
  74. K(total), largest_order ? K(1UL) << largest_order : 0);
  75. }
  76. }
  77. /*
  78. * Associate a virtual page frame with a given physical page frame
  79. * and protection flags for that frame.
  80. */
  81. static void set_pte_pfn(unsigned long vaddr, unsigned long pfn, pgprot_t flags)
  82. {
  83. pgd_t *pgd;
  84. pud_t *pud;
  85. pmd_t *pmd;
  86. pte_t *pte;
  87. pgd = swapper_pg_dir + pgd_index(vaddr);
  88. if (pgd_none(*pgd)) {
  89. BUG();
  90. return;
  91. }
  92. pud = pud_offset(pgd, vaddr);
  93. if (pud_none(*pud)) {
  94. BUG();
  95. return;
  96. }
  97. pmd = pmd_offset(pud, vaddr);
  98. if (pmd_none(*pmd)) {
  99. BUG();
  100. return;
  101. }
  102. pte = pte_offset_kernel(pmd, vaddr);
  103. /* <pfn,flags> stored as-is, to permit clearing entries */
  104. set_pte(pte, pfn_pte(pfn, flags));
  105. /*
  106. * It's enough to flush this one mapping.
  107. * This appears conservative since it is only called
  108. * from __set_fixmap.
  109. */
  110. local_flush_tlb_page(NULL, vaddr, PAGE_SIZE);
  111. }
  112. void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t flags)
  113. {
  114. unsigned long address = __fix_to_virt(idx);
  115. if (idx >= __end_of_fixed_addresses) {
  116. BUG();
  117. return;
  118. }
  119. set_pte_pfn(address, phys >> PAGE_SHIFT, flags);
  120. }
  121. #if defined(CONFIG_HIGHPTE)
  122. pte_t *_pte_offset_map(pmd_t *dir, unsigned long address)
  123. {
  124. pte_t *pte = kmap_atomic(pmd_page(*dir)) +
  125. (pmd_ptfn(*dir) << HV_LOG2_PAGE_TABLE_ALIGN) & ~PAGE_MASK;
  126. return &pte[pte_index(address)];
  127. }
  128. #endif
  129. /**
  130. * shatter_huge_page() - ensure a given address is mapped by a small page.
  131. *
  132. * This function converts a huge PTE mapping kernel LOWMEM into a bunch
  133. * of small PTEs with the same caching. No cache flush required, but we
  134. * must do a global TLB flush.
  135. *
  136. * Any caller that wishes to modify a kernel mapping that might
  137. * have been made with a huge page should call this function,
  138. * since doing so properly avoids race conditions with installing the
  139. * newly-shattered page and then flushing all the TLB entries.
  140. *
  141. * @addr: Address at which to shatter any existing huge page.
  142. */
  143. void shatter_huge_page(unsigned long addr)
  144. {
  145. pgd_t *pgd;
  146. pud_t *pud;
  147. pmd_t *pmd;
  148. unsigned long flags = 0; /* happy compiler */
  149. #ifdef __PAGETABLE_PMD_FOLDED
  150. struct list_head *pos;
  151. #endif
  152. /* Get a pointer to the pmd entry that we need to change. */
  153. addr &= HPAGE_MASK;
  154. BUG_ON(pgd_addr_invalid(addr));
  155. BUG_ON(addr < PAGE_OFFSET); /* only for kernel LOWMEM */
  156. pgd = swapper_pg_dir + pgd_index(addr);
  157. pud = pud_offset(pgd, addr);
  158. BUG_ON(!pud_present(*pud));
  159. pmd = pmd_offset(pud, addr);
  160. BUG_ON(!pmd_present(*pmd));
  161. if (!pmd_huge_page(*pmd))
  162. return;
  163. spin_lock_irqsave(&init_mm.page_table_lock, flags);
  164. if (!pmd_huge_page(*pmd)) {
  165. /* Lost the race to convert the huge page. */
  166. spin_unlock_irqrestore(&init_mm.page_table_lock, flags);
  167. return;
  168. }
  169. /* Shatter the huge page into the preallocated L2 page table. */
  170. pmd_populate_kernel(&init_mm, pmd,
  171. get_prealloc_pte(pte_pfn(*(pte_t *)pmd)));
  172. #ifdef __PAGETABLE_PMD_FOLDED
  173. /* Walk every pgd on the system and update the pmd there. */
  174. spin_lock(&pgd_lock);
  175. list_for_each(pos, &pgd_list) {
  176. pmd_t *copy_pmd;
  177. pgd = list_to_pgd(pos) + pgd_index(addr);
  178. pud = pud_offset(pgd, addr);
  179. copy_pmd = pmd_offset(pud, addr);
  180. __set_pmd(copy_pmd, *pmd);
  181. }
  182. spin_unlock(&pgd_lock);
  183. #endif
  184. /* Tell every cpu to notice the change. */
  185. flush_remote(0, 0, NULL, addr, HPAGE_SIZE, HPAGE_SIZE,
  186. cpu_possible_mask, NULL, 0);
  187. /* Hold the lock until the TLB flush is finished to avoid races. */
  188. spin_unlock_irqrestore(&init_mm.page_table_lock, flags);
  189. }
  190. /*
  191. * List of all pgd's needed so it can invalidate entries in both cached
  192. * and uncached pgd's. This is essentially codepath-based locking
  193. * against pageattr.c; it is the unique case in which a valid change
  194. * of kernel pagetables can't be lazily synchronized by vmalloc faults.
  195. * vmalloc faults work because attached pagetables are never freed.
  196. *
  197. * The lock is always taken with interrupts disabled, unlike on x86
  198. * and other platforms, because we need to take the lock in
  199. * shatter_huge_page(), which may be called from an interrupt context.
  200. * We are not at risk from the tlbflush IPI deadlock that was seen on
  201. * x86, since we use the flush_remote() API to have the hypervisor do
  202. * the TLB flushes regardless of irq disabling.
  203. */
  204. DEFINE_SPINLOCK(pgd_lock);
  205. LIST_HEAD(pgd_list);
  206. static inline void pgd_list_add(pgd_t *pgd)
  207. {
  208. list_add(pgd_to_list(pgd), &pgd_list);
  209. }
  210. static inline void pgd_list_del(pgd_t *pgd)
  211. {
  212. list_del(pgd_to_list(pgd));
  213. }
  214. #define KERNEL_PGD_INDEX_START pgd_index(PAGE_OFFSET)
  215. #define KERNEL_PGD_PTRS (PTRS_PER_PGD - KERNEL_PGD_INDEX_START)
  216. static void pgd_ctor(pgd_t *pgd)
  217. {
  218. unsigned long flags;
  219. memset(pgd, 0, KERNEL_PGD_INDEX_START*sizeof(pgd_t));
  220. spin_lock_irqsave(&pgd_lock, flags);
  221. #ifndef __tilegx__
  222. /*
  223. * Check that the user interrupt vector has no L2.
  224. * It never should for the swapper, and new page tables
  225. * should always start with an empty user interrupt vector.
  226. */
  227. BUG_ON(((u64 *)swapper_pg_dir)[pgd_index(MEM_USER_INTRPT)] != 0);
  228. #endif
  229. memcpy(pgd + KERNEL_PGD_INDEX_START,
  230. swapper_pg_dir + KERNEL_PGD_INDEX_START,
  231. KERNEL_PGD_PTRS * sizeof(pgd_t));
  232. pgd_list_add(pgd);
  233. spin_unlock_irqrestore(&pgd_lock, flags);
  234. }
  235. static void pgd_dtor(pgd_t *pgd)
  236. {
  237. unsigned long flags; /* can be called from interrupt context */
  238. spin_lock_irqsave(&pgd_lock, flags);
  239. pgd_list_del(pgd);
  240. spin_unlock_irqrestore(&pgd_lock, flags);
  241. }
  242. pgd_t *pgd_alloc(struct mm_struct *mm)
  243. {
  244. pgd_t *pgd = kmem_cache_alloc(pgd_cache, GFP_KERNEL);
  245. if (pgd)
  246. pgd_ctor(pgd);
  247. return pgd;
  248. }
  249. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  250. {
  251. pgd_dtor(pgd);
  252. kmem_cache_free(pgd_cache, pgd);
  253. }
  254. #define L2_USER_PGTABLE_PAGES (1 << L2_USER_PGTABLE_ORDER)
  255. struct page *pte_alloc_one(struct mm_struct *mm, unsigned long address)
  256. {
  257. gfp_t flags = GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO;
  258. struct page *p;
  259. #if L2_USER_PGTABLE_ORDER > 0
  260. int i;
  261. #endif
  262. #ifdef CONFIG_HIGHPTE
  263. flags |= __GFP_HIGHMEM;
  264. #endif
  265. p = alloc_pages(flags, L2_USER_PGTABLE_ORDER);
  266. if (p == NULL)
  267. return NULL;
  268. #if L2_USER_PGTABLE_ORDER > 0
  269. /*
  270. * Make every page have a page_count() of one, not just the first.
  271. * We don't use __GFP_COMP since it doesn't look like it works
  272. * correctly with tlb_remove_page().
  273. */
  274. for (i = 1; i < L2_USER_PGTABLE_PAGES; ++i) {
  275. init_page_count(p+i);
  276. inc_zone_page_state(p+i, NR_PAGETABLE);
  277. }
  278. #endif
  279. pgtable_page_ctor(p);
  280. return p;
  281. }
  282. /*
  283. * Free page immediately (used in __pte_alloc if we raced with another
  284. * process). We have to correct whatever pte_alloc_one() did before
  285. * returning the pages to the allocator.
  286. */
  287. void pte_free(struct mm_struct *mm, struct page *p)
  288. {
  289. int i;
  290. pgtable_page_dtor(p);
  291. __free_page(p);
  292. for (i = 1; i < L2_USER_PGTABLE_PAGES; ++i) {
  293. __free_page(p+i);
  294. dec_zone_page_state(p+i, NR_PAGETABLE);
  295. }
  296. }
  297. void __pte_free_tlb(struct mmu_gather *tlb, struct page *pte,
  298. unsigned long address)
  299. {
  300. int i;
  301. pgtable_page_dtor(pte);
  302. tlb_remove_page(tlb, pte);
  303. for (i = 1; i < L2_USER_PGTABLE_PAGES; ++i) {
  304. tlb_remove_page(tlb, pte + i);
  305. dec_zone_page_state(pte + i, NR_PAGETABLE);
  306. }
  307. }
  308. #ifndef __tilegx__
  309. /*
  310. * FIXME: needs to be atomic vs hypervisor writes. For now we make the
  311. * window of vulnerability a bit smaller by doing an unlocked 8-bit update.
  312. */
  313. int ptep_test_and_clear_young(struct vm_area_struct *vma,
  314. unsigned long addr, pte_t *ptep)
  315. {
  316. #if HV_PTE_INDEX_ACCESSED < 8 || HV_PTE_INDEX_ACCESSED >= 16
  317. # error Code assumes HV_PTE "accessed" bit in second byte
  318. #endif
  319. u8 *tmp = (u8 *)ptep;
  320. u8 second_byte = tmp[1];
  321. if (!(second_byte & (1 << (HV_PTE_INDEX_ACCESSED - 8))))
  322. return 0;
  323. tmp[1] = second_byte & ~(1 << (HV_PTE_INDEX_ACCESSED - 8));
  324. return 1;
  325. }
  326. /*
  327. * This implementation is atomic vs hypervisor writes, since the hypervisor
  328. * always writes the low word (where "accessed" and "dirty" are) and this
  329. * routine only writes the high word.
  330. */
  331. void ptep_set_wrprotect(struct mm_struct *mm,
  332. unsigned long addr, pte_t *ptep)
  333. {
  334. #if HV_PTE_INDEX_WRITABLE < 32
  335. # error Code assumes HV_PTE "writable" bit in high word
  336. #endif
  337. u32 *tmp = (u32 *)ptep;
  338. tmp[1] = tmp[1] & ~(1 << (HV_PTE_INDEX_WRITABLE - 32));
  339. }
  340. #endif
  341. pte_t *virt_to_pte(struct mm_struct* mm, unsigned long addr)
  342. {
  343. pgd_t *pgd;
  344. pud_t *pud;
  345. pmd_t *pmd;
  346. if (pgd_addr_invalid(addr))
  347. return NULL;
  348. pgd = mm ? pgd_offset(mm, addr) : swapper_pg_dir + pgd_index(addr);
  349. pud = pud_offset(pgd, addr);
  350. if (!pud_present(*pud))
  351. return NULL;
  352. pmd = pmd_offset(pud, addr);
  353. if (pmd_huge_page(*pmd))
  354. return (pte_t *)pmd;
  355. if (!pmd_present(*pmd))
  356. return NULL;
  357. return pte_offset_kernel(pmd, addr);
  358. }
  359. pgprot_t set_remote_cache_cpu(pgprot_t prot, int cpu)
  360. {
  361. unsigned int width = smp_width;
  362. int x = cpu % width;
  363. int y = cpu / width;
  364. BUG_ON(y >= smp_height);
  365. BUG_ON(hv_pte_get_mode(prot) != HV_PTE_MODE_CACHE_TILE_L3);
  366. BUG_ON(cpu < 0 || cpu >= NR_CPUS);
  367. BUG_ON(!cpu_is_valid_lotar(cpu));
  368. return hv_pte_set_lotar(prot, HV_XY_TO_LOTAR(x, y));
  369. }
  370. int get_remote_cache_cpu(pgprot_t prot)
  371. {
  372. HV_LOTAR lotar = hv_pte_get_lotar(prot);
  373. int x = HV_LOTAR_X(lotar);
  374. int y = HV_LOTAR_Y(lotar);
  375. BUG_ON(hv_pte_get_mode(prot) != HV_PTE_MODE_CACHE_TILE_L3);
  376. return x + y * smp_width;
  377. }
  378. /*
  379. * Convert a kernel VA to a PA and homing information.
  380. */
  381. int va_to_cpa_and_pte(void *va, unsigned long long *cpa, pte_t *pte)
  382. {
  383. struct page *page = virt_to_page(va);
  384. pte_t null_pte = { 0 };
  385. *cpa = __pa(va);
  386. /* Note that this is not writing a page table, just returning a pte. */
  387. *pte = pte_set_home(null_pte, page_home(page));
  388. return 0; /* return non-zero if not hfh? */
  389. }
  390. EXPORT_SYMBOL(va_to_cpa_and_pte);
  391. void __set_pte(pte_t *ptep, pte_t pte)
  392. {
  393. #ifdef __tilegx__
  394. *ptep = pte;
  395. #else
  396. # if HV_PTE_INDEX_PRESENT >= 32 || HV_PTE_INDEX_MIGRATING >= 32
  397. # error Must write the present and migrating bits last
  398. # endif
  399. if (pte_present(pte)) {
  400. ((u32 *)ptep)[1] = (u32)(pte_val(pte) >> 32);
  401. barrier();
  402. ((u32 *)ptep)[0] = (u32)(pte_val(pte));
  403. } else {
  404. ((u32 *)ptep)[0] = (u32)(pte_val(pte));
  405. barrier();
  406. ((u32 *)ptep)[1] = (u32)(pte_val(pte) >> 32);
  407. }
  408. #endif /* __tilegx__ */
  409. }
  410. void set_pte(pte_t *ptep, pte_t pte)
  411. {
  412. if (pte_present(pte) &&
  413. (!CHIP_HAS_MMIO() || hv_pte_get_mode(pte) != HV_PTE_MODE_MMIO)) {
  414. /* The PTE actually references physical memory. */
  415. unsigned long pfn = pte_pfn(pte);
  416. if (pfn_valid(pfn)) {
  417. /* Update the home of the PTE from the struct page. */
  418. pte = pte_set_home(pte, page_home(pfn_to_page(pfn)));
  419. } else if (hv_pte_get_mode(pte) == 0) {
  420. /* remap_pfn_range(), etc, must supply PTE mode. */
  421. panic("set_pte(): out-of-range PFN and mode 0\n");
  422. }
  423. }
  424. __set_pte(ptep, pte);
  425. }
  426. /* Can this mm load a PTE with cached_priority set? */
  427. static inline int mm_is_priority_cached(struct mm_struct *mm)
  428. {
  429. return mm->context.priority_cached;
  430. }
  431. /*
  432. * Add a priority mapping to an mm_context and
  433. * notify the hypervisor if this is the first one.
  434. */
  435. void start_mm_caching(struct mm_struct *mm)
  436. {
  437. if (!mm_is_priority_cached(mm)) {
  438. mm->context.priority_cached = -1U;
  439. hv_set_caching(-1U);
  440. }
  441. }
  442. /*
  443. * Validate and return the priority_cached flag. We know if it's zero
  444. * that we don't need to scan, since we immediately set it non-zero
  445. * when we first consider a MAP_CACHE_PRIORITY mapping.
  446. *
  447. * We only _try_ to acquire the mmap_sem semaphore; if we can't acquire it,
  448. * since we're in an interrupt context (servicing switch_mm) we don't
  449. * worry about it and don't unset the "priority_cached" field.
  450. * Presumably we'll come back later and have more luck and clear
  451. * the value then; for now we'll just keep the cache marked for priority.
  452. */
  453. static unsigned int update_priority_cached(struct mm_struct *mm)
  454. {
  455. if (mm->context.priority_cached && down_write_trylock(&mm->mmap_sem)) {
  456. struct vm_area_struct *vm;
  457. for (vm = mm->mmap; vm; vm = vm->vm_next) {
  458. if (hv_pte_get_cached_priority(vm->vm_page_prot))
  459. break;
  460. }
  461. if (vm == NULL)
  462. mm->context.priority_cached = 0;
  463. up_write(&mm->mmap_sem);
  464. }
  465. return mm->context.priority_cached;
  466. }
  467. /* Set caching correctly for an mm that we are switching to. */
  468. void check_mm_caching(struct mm_struct *prev, struct mm_struct *next)
  469. {
  470. if (!mm_is_priority_cached(next)) {
  471. /*
  472. * If the new mm doesn't use priority caching, just see if we
  473. * need the hv_set_caching(), or can assume it's already zero.
  474. */
  475. if (mm_is_priority_cached(prev))
  476. hv_set_caching(0);
  477. } else {
  478. hv_set_caching(update_priority_cached(next));
  479. }
  480. }
  481. #if CHIP_HAS_MMIO()
  482. /* Map an arbitrary MMIO address, homed according to pgprot, into VA space. */
  483. void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size,
  484. pgprot_t home)
  485. {
  486. void *addr;
  487. struct vm_struct *area;
  488. unsigned long offset, last_addr;
  489. pgprot_t pgprot;
  490. /* Don't allow wraparound or zero size */
  491. last_addr = phys_addr + size - 1;
  492. if (!size || last_addr < phys_addr)
  493. return NULL;
  494. /* Create a read/write, MMIO VA mapping homed at the requested shim. */
  495. pgprot = PAGE_KERNEL;
  496. pgprot = hv_pte_set_mode(pgprot, HV_PTE_MODE_MMIO);
  497. pgprot = hv_pte_set_lotar(pgprot, hv_pte_get_lotar(home));
  498. /*
  499. * Mappings have to be page-aligned
  500. */
  501. offset = phys_addr & ~PAGE_MASK;
  502. phys_addr &= PAGE_MASK;
  503. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  504. /*
  505. * Ok, go for it..
  506. */
  507. area = get_vm_area(size, VM_IOREMAP /* | other flags? */);
  508. if (!area)
  509. return NULL;
  510. area->phys_addr = phys_addr;
  511. addr = area->addr;
  512. if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
  513. phys_addr, pgprot)) {
  514. remove_vm_area((void *)(PAGE_MASK & (unsigned long) addr));
  515. return NULL;
  516. }
  517. return (__force void __iomem *) (offset + (char *)addr);
  518. }
  519. EXPORT_SYMBOL(ioremap_prot);
  520. /* Map a PCI MMIO bus address into VA space. */
  521. void __iomem *ioremap(resource_size_t phys_addr, unsigned long size)
  522. {
  523. panic("ioremap for PCI MMIO is not supported");
  524. }
  525. EXPORT_SYMBOL(ioremap);
  526. /* Unmap an MMIO VA mapping. */
  527. void iounmap(volatile void __iomem *addr_in)
  528. {
  529. volatile void __iomem *addr = (volatile void __iomem *)
  530. (PAGE_MASK & (unsigned long __force)addr_in);
  531. #if 1
  532. vunmap((void * __force)addr);
  533. #else
  534. /* x86 uses this complicated flow instead of vunmap(). Is
  535. * there any particular reason we should do the same? */
  536. struct vm_struct *p, *o;
  537. /* Use the vm area unlocked, assuming the caller
  538. ensures there isn't another iounmap for the same address
  539. in parallel. Reuse of the virtual address is prevented by
  540. leaving it in the global lists until we're done with it.
  541. cpa takes care of the direct mappings. */
  542. read_lock(&vmlist_lock);
  543. for (p = vmlist; p; p = p->next) {
  544. if (p->addr == addr)
  545. break;
  546. }
  547. read_unlock(&vmlist_lock);
  548. if (!p) {
  549. pr_err("iounmap: bad address %p\n", addr);
  550. dump_stack();
  551. return;
  552. }
  553. /* Finally remove it */
  554. o = remove_vm_area((void *)addr);
  555. BUG_ON(p != o || o == NULL);
  556. kfree(p);
  557. #endif
  558. }
  559. EXPORT_SYMBOL(iounmap);
  560. #endif /* CHIP_HAS_MMIO() */