pgtable.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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/system.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/pgalloc.h>
  31. #include <asm/fixmap.h>
  32. #include <asm/tlb.h>
  33. #include <asm/tlbflush.h>
  34. #include <asm/homecache.h>
  35. #define K(x) ((x) << (PAGE_SHIFT-10))
  36. /*
  37. * The normal show_free_areas() is too verbose on Tile, with dozens
  38. * of processors and often four NUMA zones each with high and lowmem.
  39. */
  40. void show_mem(void)
  41. {
  42. struct zone *zone;
  43. pr_err("Active:%lu inactive:%lu dirty:%lu writeback:%lu unstable:%lu"
  44. " free:%lu\n slab:%lu mapped:%lu pagetables:%lu bounce:%lu"
  45. " pagecache:%lu swap:%lu\n",
  46. (global_page_state(NR_ACTIVE_ANON) +
  47. global_page_state(NR_ACTIVE_FILE)),
  48. (global_page_state(NR_INACTIVE_ANON) +
  49. global_page_state(NR_INACTIVE_FILE)),
  50. global_page_state(NR_FILE_DIRTY),
  51. global_page_state(NR_WRITEBACK),
  52. global_page_state(NR_UNSTABLE_NFS),
  53. global_page_state(NR_FREE_PAGES),
  54. (global_page_state(NR_SLAB_RECLAIMABLE) +
  55. global_page_state(NR_SLAB_UNRECLAIMABLE)),
  56. global_page_state(NR_FILE_MAPPED),
  57. global_page_state(NR_PAGETABLE),
  58. global_page_state(NR_BOUNCE),
  59. global_page_state(NR_FILE_PAGES),
  60. nr_swap_pages);
  61. for_each_zone(zone) {
  62. unsigned long flags, order, total = 0, largest_order = -1;
  63. if (!populated_zone(zone))
  64. continue;
  65. spin_lock_irqsave(&zone->lock, flags);
  66. for (order = 0; order < MAX_ORDER; order++) {
  67. int nr = zone->free_area[order].nr_free;
  68. total += nr << order;
  69. if (nr)
  70. largest_order = order;
  71. }
  72. spin_unlock_irqrestore(&zone->lock, flags);
  73. pr_err("Node %d %7s: %lukB (largest %luKb)\n",
  74. zone_to_nid(zone), zone->name,
  75. K(total), largest_order ? K(1UL) << largest_order : 0);
  76. }
  77. }
  78. /*
  79. * Associate a virtual page frame with a given physical page frame
  80. * and protection flags for that frame.
  81. */
  82. static void set_pte_pfn(unsigned long vaddr, unsigned long pfn, pgprot_t flags)
  83. {
  84. pgd_t *pgd;
  85. pud_t *pud;
  86. pmd_t *pmd;
  87. pte_t *pte;
  88. pgd = swapper_pg_dir + pgd_index(vaddr);
  89. if (pgd_none(*pgd)) {
  90. BUG();
  91. return;
  92. }
  93. pud = pud_offset(pgd, vaddr);
  94. if (pud_none(*pud)) {
  95. BUG();
  96. return;
  97. }
  98. pmd = pmd_offset(pud, vaddr);
  99. if (pmd_none(*pmd)) {
  100. BUG();
  101. return;
  102. }
  103. pte = pte_offset_kernel(pmd, vaddr);
  104. /* <pfn,flags> stored as-is, to permit clearing entries */
  105. set_pte(pte, pfn_pte(pfn, flags));
  106. /*
  107. * It's enough to flush this one mapping.
  108. * This appears conservative since it is only called
  109. * from __set_fixmap.
  110. */
  111. local_flush_tlb_page(NULL, vaddr, PAGE_SIZE);
  112. }
  113. void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t flags)
  114. {
  115. unsigned long address = __fix_to_virt(idx);
  116. if (idx >= __end_of_fixed_addresses) {
  117. BUG();
  118. return;
  119. }
  120. set_pte_pfn(address, phys >> PAGE_SHIFT, flags);
  121. }
  122. #if defined(CONFIG_HIGHPTE)
  123. pte_t *_pte_offset_map(pmd_t *dir, unsigned long address)
  124. {
  125. pte_t *pte = kmap_atomic(pmd_page(*dir)) +
  126. (pmd_ptfn(*dir) << HV_LOG2_PAGE_TABLE_ALIGN) & ~PAGE_MASK;
  127. return &pte[pte_index(address)];
  128. }
  129. #endif
  130. /*
  131. * List of all pgd's needed so it can invalidate entries in both cached
  132. * and uncached pgd's. This is essentially codepath-based locking
  133. * against pageattr.c; it is the unique case in which a valid change
  134. * of kernel pagetables can't be lazily synchronized by vmalloc faults.
  135. * vmalloc faults work because attached pagetables are never freed.
  136. * The locking scheme was chosen on the basis of manfred's
  137. * recommendations and having no core impact whatsoever.
  138. * -- wli
  139. */
  140. DEFINE_SPINLOCK(pgd_lock);
  141. LIST_HEAD(pgd_list);
  142. static inline void pgd_list_add(pgd_t *pgd)
  143. {
  144. list_add(pgd_to_list(pgd), &pgd_list);
  145. }
  146. static inline void pgd_list_del(pgd_t *pgd)
  147. {
  148. list_del(pgd_to_list(pgd));
  149. }
  150. #define KERNEL_PGD_INDEX_START pgd_index(PAGE_OFFSET)
  151. #define KERNEL_PGD_PTRS (PTRS_PER_PGD - KERNEL_PGD_INDEX_START)
  152. static void pgd_ctor(pgd_t *pgd)
  153. {
  154. unsigned long flags;
  155. memset(pgd, 0, KERNEL_PGD_INDEX_START*sizeof(pgd_t));
  156. spin_lock_irqsave(&pgd_lock, flags);
  157. #ifndef __tilegx__
  158. /*
  159. * Check that the user interrupt vector has no L2.
  160. * It never should for the swapper, and new page tables
  161. * should always start with an empty user interrupt vector.
  162. */
  163. BUG_ON(((u64 *)swapper_pg_dir)[pgd_index(MEM_USER_INTRPT)] != 0);
  164. #endif
  165. clone_pgd_range(pgd + KERNEL_PGD_INDEX_START,
  166. swapper_pg_dir + KERNEL_PGD_INDEX_START,
  167. KERNEL_PGD_PTRS);
  168. pgd_list_add(pgd);
  169. spin_unlock_irqrestore(&pgd_lock, flags);
  170. }
  171. static void pgd_dtor(pgd_t *pgd)
  172. {
  173. unsigned long flags; /* can be called from interrupt context */
  174. spin_lock_irqsave(&pgd_lock, flags);
  175. pgd_list_del(pgd);
  176. spin_unlock_irqrestore(&pgd_lock, flags);
  177. }
  178. pgd_t *pgd_alloc(struct mm_struct *mm)
  179. {
  180. pgd_t *pgd = kmem_cache_alloc(pgd_cache, GFP_KERNEL);
  181. if (pgd)
  182. pgd_ctor(pgd);
  183. return pgd;
  184. }
  185. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  186. {
  187. pgd_dtor(pgd);
  188. kmem_cache_free(pgd_cache, pgd);
  189. }
  190. #define L2_USER_PGTABLE_PAGES (1 << L2_USER_PGTABLE_ORDER)
  191. struct page *pte_alloc_one(struct mm_struct *mm, unsigned long address)
  192. {
  193. gfp_t flags = GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO|__GFP_COMP;
  194. struct page *p;
  195. #ifdef CONFIG_HIGHPTE
  196. flags |= __GFP_HIGHMEM;
  197. #endif
  198. p = alloc_pages(flags, L2_USER_PGTABLE_ORDER);
  199. if (p == NULL)
  200. return NULL;
  201. pgtable_page_ctor(p);
  202. return p;
  203. }
  204. /*
  205. * Free page immediately (used in __pte_alloc if we raced with another
  206. * process). We have to correct whatever pte_alloc_one() did before
  207. * returning the pages to the allocator.
  208. */
  209. void pte_free(struct mm_struct *mm, struct page *p)
  210. {
  211. pgtable_page_dtor(p);
  212. __free_pages(p, L2_USER_PGTABLE_ORDER);
  213. }
  214. void __pte_free_tlb(struct mmu_gather *tlb, struct page *pte,
  215. unsigned long address)
  216. {
  217. int i;
  218. pgtable_page_dtor(pte);
  219. tlb->need_flush = 1;
  220. if (tlb_fast_mode(tlb)) {
  221. struct page *pte_pages[L2_USER_PGTABLE_PAGES];
  222. for (i = 0; i < L2_USER_PGTABLE_PAGES; ++i)
  223. pte_pages[i] = pte + i;
  224. free_pages_and_swap_cache(pte_pages, L2_USER_PGTABLE_PAGES);
  225. return;
  226. }
  227. for (i = 0; i < L2_USER_PGTABLE_PAGES; ++i) {
  228. tlb->pages[tlb->nr++] = pte + i;
  229. if (tlb->nr >= FREE_PTE_NR)
  230. tlb_flush_mmu(tlb, 0, 0);
  231. }
  232. }
  233. #ifndef __tilegx__
  234. /*
  235. * FIXME: needs to be atomic vs hypervisor writes. For now we make the
  236. * window of vulnerability a bit smaller by doing an unlocked 8-bit update.
  237. */
  238. int ptep_test_and_clear_young(struct vm_area_struct *vma,
  239. unsigned long addr, pte_t *ptep)
  240. {
  241. #if HV_PTE_INDEX_ACCESSED < 8 || HV_PTE_INDEX_ACCESSED >= 16
  242. # error Code assumes HV_PTE "accessed" bit in second byte
  243. #endif
  244. u8 *tmp = (u8 *)ptep;
  245. u8 second_byte = tmp[1];
  246. if (!(second_byte & (1 << (HV_PTE_INDEX_ACCESSED - 8))))
  247. return 0;
  248. tmp[1] = second_byte & ~(1 << (HV_PTE_INDEX_ACCESSED - 8));
  249. return 1;
  250. }
  251. /*
  252. * This implementation is atomic vs hypervisor writes, since the hypervisor
  253. * always writes the low word (where "accessed" and "dirty" are) and this
  254. * routine only writes the high word.
  255. */
  256. void ptep_set_wrprotect(struct mm_struct *mm,
  257. unsigned long addr, pte_t *ptep)
  258. {
  259. #if HV_PTE_INDEX_WRITABLE < 32
  260. # error Code assumes HV_PTE "writable" bit in high word
  261. #endif
  262. u32 *tmp = (u32 *)ptep;
  263. tmp[1] = tmp[1] & ~(1 << (HV_PTE_INDEX_WRITABLE - 32));
  264. }
  265. #endif
  266. pte_t *virt_to_pte(struct mm_struct* mm, unsigned long addr)
  267. {
  268. pgd_t *pgd;
  269. pud_t *pud;
  270. pmd_t *pmd;
  271. if (pgd_addr_invalid(addr))
  272. return NULL;
  273. pgd = mm ? pgd_offset(mm, addr) : swapper_pg_dir + pgd_index(addr);
  274. pud = pud_offset(pgd, addr);
  275. if (!pud_present(*pud))
  276. return NULL;
  277. pmd = pmd_offset(pud, addr);
  278. if (pmd_huge_page(*pmd))
  279. return (pte_t *)pmd;
  280. if (!pmd_present(*pmd))
  281. return NULL;
  282. return pte_offset_kernel(pmd, addr);
  283. }
  284. pgprot_t set_remote_cache_cpu(pgprot_t prot, int cpu)
  285. {
  286. unsigned int width = smp_width;
  287. int x = cpu % width;
  288. int y = cpu / width;
  289. BUG_ON(y >= smp_height);
  290. BUG_ON(hv_pte_get_mode(prot) != HV_PTE_MODE_CACHE_TILE_L3);
  291. BUG_ON(cpu < 0 || cpu >= NR_CPUS);
  292. BUG_ON(!cpu_is_valid_lotar(cpu));
  293. return hv_pte_set_lotar(prot, HV_XY_TO_LOTAR(x, y));
  294. }
  295. int get_remote_cache_cpu(pgprot_t prot)
  296. {
  297. HV_LOTAR lotar = hv_pte_get_lotar(prot);
  298. int x = HV_LOTAR_X(lotar);
  299. int y = HV_LOTAR_Y(lotar);
  300. BUG_ON(hv_pte_get_mode(prot) != HV_PTE_MODE_CACHE_TILE_L3);
  301. return x + y * smp_width;
  302. }
  303. void set_pte_order(pte_t *ptep, pte_t pte, int order)
  304. {
  305. unsigned long pfn = pte_pfn(pte);
  306. struct page *page = pfn_to_page(pfn);
  307. /* Update the home of a PTE if necessary */
  308. pte = pte_set_home(pte, page_home(page));
  309. #ifdef __tilegx__
  310. *ptep = pte;
  311. #else
  312. /*
  313. * When setting a PTE, write the high bits first, then write
  314. * the low bits. This sets the "present" bit only after the
  315. * other bits are in place. If a particular PTE update
  316. * involves transitioning from one valid PTE to another, it
  317. * may be necessary to call set_pte_order() more than once,
  318. * transitioning via a suitable intermediate state.
  319. * Note that this sequence also means that if we are transitioning
  320. * from any migrating PTE to a non-migrating one, we will not
  321. * see a half-updated PTE with the migrating bit off.
  322. */
  323. #if HV_PTE_INDEX_PRESENT >= 32 || HV_PTE_INDEX_MIGRATING >= 32
  324. # error Must write the present and migrating bits last
  325. #endif
  326. ((u32 *)ptep)[1] = (u32)(pte_val(pte) >> 32);
  327. barrier();
  328. ((u32 *)ptep)[0] = (u32)(pte_val(pte));
  329. #endif
  330. }
  331. /* Can this mm load a PTE with cached_priority set? */
  332. static inline int mm_is_priority_cached(struct mm_struct *mm)
  333. {
  334. return mm->context.priority_cached;
  335. }
  336. /*
  337. * Add a priority mapping to an mm_context and
  338. * notify the hypervisor if this is the first one.
  339. */
  340. void start_mm_caching(struct mm_struct *mm)
  341. {
  342. if (!mm_is_priority_cached(mm)) {
  343. mm->context.priority_cached = -1U;
  344. hv_set_caching(-1U);
  345. }
  346. }
  347. /*
  348. * Validate and return the priority_cached flag. We know if it's zero
  349. * that we don't need to scan, since we immediately set it non-zero
  350. * when we first consider a MAP_CACHE_PRIORITY mapping.
  351. *
  352. * We only _try_ to acquire the mmap_sem semaphore; if we can't acquire it,
  353. * since we're in an interrupt context (servicing switch_mm) we don't
  354. * worry about it and don't unset the "priority_cached" field.
  355. * Presumably we'll come back later and have more luck and clear
  356. * the value then; for now we'll just keep the cache marked for priority.
  357. */
  358. static unsigned int update_priority_cached(struct mm_struct *mm)
  359. {
  360. if (mm->context.priority_cached && down_write_trylock(&mm->mmap_sem)) {
  361. struct vm_area_struct *vm;
  362. for (vm = mm->mmap; vm; vm = vm->vm_next) {
  363. if (hv_pte_get_cached_priority(vm->vm_page_prot))
  364. break;
  365. }
  366. if (vm == NULL)
  367. mm->context.priority_cached = 0;
  368. up_write(&mm->mmap_sem);
  369. }
  370. return mm->context.priority_cached;
  371. }
  372. /* Set caching correctly for an mm that we are switching to. */
  373. void check_mm_caching(struct mm_struct *prev, struct mm_struct *next)
  374. {
  375. if (!mm_is_priority_cached(next)) {
  376. /*
  377. * If the new mm doesn't use priority caching, just see if we
  378. * need the hv_set_caching(), or can assume it's already zero.
  379. */
  380. if (mm_is_priority_cached(prev))
  381. hv_set_caching(0);
  382. } else {
  383. hv_set_caching(update_priority_cached(next));
  384. }
  385. }
  386. #if CHIP_HAS_MMIO()
  387. /* Map an arbitrary MMIO address, homed according to pgprot, into VA space. */
  388. void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size,
  389. pgprot_t home)
  390. {
  391. void *addr;
  392. struct vm_struct *area;
  393. unsigned long offset, last_addr;
  394. pgprot_t pgprot;
  395. /* Don't allow wraparound or zero size */
  396. last_addr = phys_addr + size - 1;
  397. if (!size || last_addr < phys_addr)
  398. return NULL;
  399. /* Create a read/write, MMIO VA mapping homed at the requested shim. */
  400. pgprot = PAGE_KERNEL;
  401. pgprot = hv_pte_set_mode(pgprot, HV_PTE_MODE_MMIO);
  402. pgprot = hv_pte_set_lotar(pgprot, hv_pte_get_lotar(home));
  403. /*
  404. * Mappings have to be page-aligned
  405. */
  406. offset = phys_addr & ~PAGE_MASK;
  407. phys_addr &= PAGE_MASK;
  408. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  409. /*
  410. * Ok, go for it..
  411. */
  412. area = get_vm_area(size, VM_IOREMAP /* | other flags? */);
  413. if (!area)
  414. return NULL;
  415. area->phys_addr = phys_addr;
  416. addr = area->addr;
  417. if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
  418. phys_addr, pgprot)) {
  419. remove_vm_area((void *)(PAGE_MASK & (unsigned long) addr));
  420. return NULL;
  421. }
  422. return (__force void __iomem *) (offset + (char *)addr);
  423. }
  424. EXPORT_SYMBOL(ioremap_prot);
  425. /* Map a PCI MMIO bus address into VA space. */
  426. void __iomem *ioremap(resource_size_t phys_addr, unsigned long size)
  427. {
  428. panic("ioremap for PCI MMIO is not supported");
  429. }
  430. EXPORT_SYMBOL(ioremap);
  431. /* Unmap an MMIO VA mapping. */
  432. void iounmap(volatile void __iomem *addr_in)
  433. {
  434. volatile void __iomem *addr = (volatile void __iomem *)
  435. (PAGE_MASK & (unsigned long __force)addr_in);
  436. #if 1
  437. vunmap((void * __force)addr);
  438. #else
  439. /* x86 uses this complicated flow instead of vunmap(). Is
  440. * there any particular reason we should do the same? */
  441. struct vm_struct *p, *o;
  442. /* Use the vm area unlocked, assuming the caller
  443. ensures there isn't another iounmap for the same address
  444. in parallel. Reuse of the virtual address is prevented by
  445. leaving it in the global lists until we're done with it.
  446. cpa takes care of the direct mappings. */
  447. read_lock(&vmlist_lock);
  448. for (p = vmlist; p; p = p->next) {
  449. if (p->addr == addr)
  450. break;
  451. }
  452. read_unlock(&vmlist_lock);
  453. if (!p) {
  454. pr_err("iounmap: bad address %p\n", addr);
  455. dump_stack();
  456. return;
  457. }
  458. /* Finally remove it */
  459. o = remove_vm_area((void *)addr);
  460. BUG_ON(p != o || o == NULL);
  461. kfree(p);
  462. #endif
  463. }
  464. EXPORT_SYMBOL(iounmap);
  465. #endif /* CHIP_HAS_MMIO() */