2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
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.
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
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
19 #include <linux/swap.h>
20 #include <linux/highmem.h>
21 #include <linux/slab.h>
22 #include <linux/pagemap.h>
23 #include <linux/spinlock.h>
24 #include <linux/cpumask.h>
25 #include <linux/module.h>
27 #include <linux/vmalloc.h>
28 #include <linux/smp.h>
30 #include <asm/system.h>
31 #include <asm/pgtable.h>
32 #include <asm/pgalloc.h>
33 #include <asm/fixmap.h>
35 #include <asm/tlbflush.h>
36 #include <asm/homecache.h>
38 #define K(x) ((x) << (PAGE_SHIFT-10))
41 * The normal show_free_areas() is too verbose on Tile, with dozens
42 * of processors and often four NUMA zones each with high and lowmem.
44 void show_mem(unsigned int filter
)
48 pr_err("Active:%lu inactive:%lu dirty:%lu writeback:%lu unstable:%lu"
49 " free:%lu\n slab:%lu mapped:%lu pagetables:%lu bounce:%lu"
50 " pagecache:%lu swap:%lu\n",
51 (global_page_state(NR_ACTIVE_ANON
) +
52 global_page_state(NR_ACTIVE_FILE
)),
53 (global_page_state(NR_INACTIVE_ANON
) +
54 global_page_state(NR_INACTIVE_FILE
)),
55 global_page_state(NR_FILE_DIRTY
),
56 global_page_state(NR_WRITEBACK
),
57 global_page_state(NR_UNSTABLE_NFS
),
58 global_page_state(NR_FREE_PAGES
),
59 (global_page_state(NR_SLAB_RECLAIMABLE
) +
60 global_page_state(NR_SLAB_UNRECLAIMABLE
)),
61 global_page_state(NR_FILE_MAPPED
),
62 global_page_state(NR_PAGETABLE
),
63 global_page_state(NR_BOUNCE
),
64 global_page_state(NR_FILE_PAGES
),
68 unsigned long flags
, order
, total
= 0, largest_order
= -1;
70 if (!populated_zone(zone
))
73 spin_lock_irqsave(&zone
->lock
, flags
);
74 for (order
= 0; order
< MAX_ORDER
; order
++) {
75 int nr
= zone
->free_area
[order
].nr_free
;
78 largest_order
= order
;
80 spin_unlock_irqrestore(&zone
->lock
, flags
);
81 pr_err("Node %d %7s: %lukB (largest %luKb)\n",
82 zone_to_nid(zone
), zone
->name
,
83 K(total
), largest_order
? K(1UL) << largest_order
: 0);
88 * Associate a virtual page frame with a given physical page frame
89 * and protection flags for that frame.
91 static void set_pte_pfn(unsigned long vaddr
, unsigned long pfn
, pgprot_t flags
)
98 pgd
= swapper_pg_dir
+ pgd_index(vaddr
);
103 pud
= pud_offset(pgd
, vaddr
);
104 if (pud_none(*pud
)) {
108 pmd
= pmd_offset(pud
, vaddr
);
109 if (pmd_none(*pmd
)) {
113 pte
= pte_offset_kernel(pmd
, vaddr
);
114 /* <pfn,flags> stored as-is, to permit clearing entries */
115 set_pte(pte
, pfn_pte(pfn
, flags
));
118 * It's enough to flush this one mapping.
119 * This appears conservative since it is only called
122 local_flush_tlb_page(NULL
, vaddr
, PAGE_SIZE
);
125 void __set_fixmap(enum fixed_addresses idx
, unsigned long phys
, pgprot_t flags
)
127 unsigned long address
= __fix_to_virt(idx
);
129 if (idx
>= __end_of_fixed_addresses
) {
133 set_pte_pfn(address
, phys
>> PAGE_SHIFT
, flags
);
136 #if defined(CONFIG_HIGHPTE)
137 pte_t
*_pte_offset_map(pmd_t
*dir
, unsigned long address
)
139 pte_t
*pte
= kmap_atomic(pmd_page(*dir
)) +
140 (pmd_ptfn(*dir
) << HV_LOG2_PAGE_TABLE_ALIGN
) & ~PAGE_MASK
;
141 return &pte
[pte_index(address
)];
146 * shatter_huge_page() - ensure a given address is mapped by a small page.
148 * This function converts a huge PTE mapping kernel LOWMEM into a bunch
149 * of small PTEs with the same caching. No cache flush required, but we
150 * must do a global TLB flush.
152 * Any caller that wishes to modify a kernel mapping that might
153 * have been made with a huge page should call this function,
154 * since doing so properly avoids race conditions with installing the
155 * newly-shattered page and then flushing all the TLB entries.
157 * @addr: Address at which to shatter any existing huge page.
159 void shatter_huge_page(unsigned long addr
)
164 unsigned long flags
= 0; /* happy compiler */
165 #ifdef __PAGETABLE_PMD_FOLDED
166 struct list_head
*pos
;
169 /* Get a pointer to the pmd entry that we need to change. */
171 BUG_ON(pgd_addr_invalid(addr
));
172 BUG_ON(addr
< PAGE_OFFSET
); /* only for kernel LOWMEM */
173 pgd
= swapper_pg_dir
+ pgd_index(addr
);
174 pud
= pud_offset(pgd
, addr
);
175 BUG_ON(!pud_present(*pud
));
176 pmd
= pmd_offset(pud
, addr
);
177 BUG_ON(!pmd_present(*pmd
));
178 if (!pmd_huge_page(*pmd
))
182 * Grab the pgd_lock, since we may need it to walk the pgd_list,
183 * and since we need some kind of lock here to avoid races.
185 spin_lock_irqsave(&pgd_lock
, flags
);
186 if (!pmd_huge_page(*pmd
)) {
187 /* Lost the race to convert the huge page. */
188 spin_unlock_irqrestore(&pgd_lock
, flags
);
192 /* Shatter the huge page into the preallocated L2 page table. */
193 pmd_populate_kernel(&init_mm
, pmd
,
194 get_prealloc_pte(pte_pfn(*(pte_t
*)pmd
)));
196 #ifdef __PAGETABLE_PMD_FOLDED
197 /* Walk every pgd on the system and update the pmd there. */
198 list_for_each(pos
, &pgd_list
) {
200 pgd
= list_to_pgd(pos
) + pgd_index(addr
);
201 pud
= pud_offset(pgd
, addr
);
202 copy_pmd
= pmd_offset(pud
, addr
);
203 __set_pmd(copy_pmd
, *pmd
);
207 /* Tell every cpu to notice the change. */
208 flush_remote(0, 0, NULL
, addr
, HPAGE_SIZE
, HPAGE_SIZE
,
209 cpu_possible_mask
, NULL
, 0);
211 /* Hold the lock until the TLB flush is finished to avoid races. */
212 spin_unlock_irqrestore(&pgd_lock
, flags
);
216 * List of all pgd's needed so it can invalidate entries in both cached
217 * and uncached pgd's. This is essentially codepath-based locking
218 * against pageattr.c; it is the unique case in which a valid change
219 * of kernel pagetables can't be lazily synchronized by vmalloc faults.
220 * vmalloc faults work because attached pagetables are never freed.
221 * The locking scheme was chosen on the basis of manfred's
222 * recommendations and having no core impact whatsoever.
225 DEFINE_SPINLOCK(pgd_lock
);
228 static inline void pgd_list_add(pgd_t
*pgd
)
230 list_add(pgd_to_list(pgd
), &pgd_list
);
233 static inline void pgd_list_del(pgd_t
*pgd
)
235 list_del(pgd_to_list(pgd
));
238 #define KERNEL_PGD_INDEX_START pgd_index(PAGE_OFFSET)
239 #define KERNEL_PGD_PTRS (PTRS_PER_PGD - KERNEL_PGD_INDEX_START)
241 static void pgd_ctor(pgd_t
*pgd
)
245 memset(pgd
, 0, KERNEL_PGD_INDEX_START
*sizeof(pgd_t
));
246 spin_lock_irqsave(&pgd_lock
, flags
);
250 * Check that the user interrupt vector has no L2.
251 * It never should for the swapper, and new page tables
252 * should always start with an empty user interrupt vector.
254 BUG_ON(((u64
*)swapper_pg_dir
)[pgd_index(MEM_USER_INTRPT
)] != 0);
257 memcpy(pgd
+ KERNEL_PGD_INDEX_START
,
258 swapper_pg_dir
+ KERNEL_PGD_INDEX_START
,
259 KERNEL_PGD_PTRS
* sizeof(pgd_t
));
262 spin_unlock_irqrestore(&pgd_lock
, flags
);
265 static void pgd_dtor(pgd_t
*pgd
)
267 unsigned long flags
; /* can be called from interrupt context */
269 spin_lock_irqsave(&pgd_lock
, flags
);
271 spin_unlock_irqrestore(&pgd_lock
, flags
);
274 pgd_t
*pgd_alloc(struct mm_struct
*mm
)
276 pgd_t
*pgd
= kmem_cache_alloc(pgd_cache
, GFP_KERNEL
);
282 void pgd_free(struct mm_struct
*mm
, pgd_t
*pgd
)
285 kmem_cache_free(pgd_cache
, pgd
);
289 #define L2_USER_PGTABLE_PAGES (1 << L2_USER_PGTABLE_ORDER)
291 struct page
*pte_alloc_one(struct mm_struct
*mm
, unsigned long address
)
293 gfp_t flags
= GFP_KERNEL
|__GFP_REPEAT
|__GFP_ZERO
;
295 #if L2_USER_PGTABLE_ORDER > 0
299 #ifdef CONFIG_HIGHPTE
300 flags
|= __GFP_HIGHMEM
;
303 p
= alloc_pages(flags
, L2_USER_PGTABLE_ORDER
);
307 #if L2_USER_PGTABLE_ORDER > 0
309 * Make every page have a page_count() of one, not just the first.
310 * We don't use __GFP_COMP since it doesn't look like it works
311 * correctly with tlb_remove_page().
313 for (i
= 1; i
< L2_USER_PGTABLE_PAGES
; ++i
) {
314 init_page_count(p
+i
);
315 inc_zone_page_state(p
+i
, NR_PAGETABLE
);
319 pgtable_page_ctor(p
);
324 * Free page immediately (used in __pte_alloc if we raced with another
325 * process). We have to correct whatever pte_alloc_one() did before
326 * returning the pages to the allocator.
328 void pte_free(struct mm_struct
*mm
, struct page
*p
)
332 pgtable_page_dtor(p
);
335 for (i
= 1; i
< L2_USER_PGTABLE_PAGES
; ++i
) {
337 dec_zone_page_state(p
+i
, NR_PAGETABLE
);
341 void __pte_free_tlb(struct mmu_gather
*tlb
, struct page
*pte
,
342 unsigned long address
)
346 pgtable_page_dtor(pte
);
347 tlb_remove_page(tlb
, pte
);
349 for (i
= 1; i
< L2_USER_PGTABLE_PAGES
; ++i
) {
350 tlb_remove_page(tlb
, pte
+ i
);
351 dec_zone_page_state(pte
+ i
, NR_PAGETABLE
);
358 * FIXME: needs to be atomic vs hypervisor writes. For now we make the
359 * window of vulnerability a bit smaller by doing an unlocked 8-bit update.
361 int ptep_test_and_clear_young(struct vm_area_struct
*vma
,
362 unsigned long addr
, pte_t
*ptep
)
364 #if HV_PTE_INDEX_ACCESSED < 8 || HV_PTE_INDEX_ACCESSED >= 16
365 # error Code assumes HV_PTE "accessed" bit in second byte
367 u8
*tmp
= (u8
*)ptep
;
368 u8 second_byte
= tmp
[1];
369 if (!(second_byte
& (1 << (HV_PTE_INDEX_ACCESSED
- 8))))
371 tmp
[1] = second_byte
& ~(1 << (HV_PTE_INDEX_ACCESSED
- 8));
376 * This implementation is atomic vs hypervisor writes, since the hypervisor
377 * always writes the low word (where "accessed" and "dirty" are) and this
378 * routine only writes the high word.
380 void ptep_set_wrprotect(struct mm_struct
*mm
,
381 unsigned long addr
, pte_t
*ptep
)
383 #if HV_PTE_INDEX_WRITABLE < 32
384 # error Code assumes HV_PTE "writable" bit in high word
386 u32
*tmp
= (u32
*)ptep
;
387 tmp
[1] = tmp
[1] & ~(1 << (HV_PTE_INDEX_WRITABLE
- 32));
392 pte_t
*virt_to_pte(struct mm_struct
* mm
, unsigned long addr
)
398 if (pgd_addr_invalid(addr
))
401 pgd
= mm
? pgd_offset(mm
, addr
) : swapper_pg_dir
+ pgd_index(addr
);
402 pud
= pud_offset(pgd
, addr
);
403 if (!pud_present(*pud
))
405 pmd
= pmd_offset(pud
, addr
);
406 if (pmd_huge_page(*pmd
))
408 if (!pmd_present(*pmd
))
410 return pte_offset_kernel(pmd
, addr
);
413 pgprot_t
set_remote_cache_cpu(pgprot_t prot
, int cpu
)
415 unsigned int width
= smp_width
;
418 BUG_ON(y
>= smp_height
);
419 BUG_ON(hv_pte_get_mode(prot
) != HV_PTE_MODE_CACHE_TILE_L3
);
420 BUG_ON(cpu
< 0 || cpu
>= NR_CPUS
);
421 BUG_ON(!cpu_is_valid_lotar(cpu
));
422 return hv_pte_set_lotar(prot
, HV_XY_TO_LOTAR(x
, y
));
425 int get_remote_cache_cpu(pgprot_t prot
)
427 HV_LOTAR lotar
= hv_pte_get_lotar(prot
);
428 int x
= HV_LOTAR_X(lotar
);
429 int y
= HV_LOTAR_Y(lotar
);
430 BUG_ON(hv_pte_get_mode(prot
) != HV_PTE_MODE_CACHE_TILE_L3
);
431 return x
+ y
* smp_width
;
435 * Convert a kernel VA to a PA and homing information.
437 int va_to_cpa_and_pte(void *va
, unsigned long long *cpa
, pte_t
*pte
)
439 struct page
*page
= virt_to_page(va
);
440 pte_t null_pte
= { 0 };
444 /* Note that this is not writing a page table, just returning a pte. */
445 *pte
= pte_set_home(null_pte
, page_home(page
));
447 return 0; /* return non-zero if not hfh? */
449 EXPORT_SYMBOL(va_to_cpa_and_pte
);
451 void __set_pte(pte_t
*ptep
, pte_t pte
)
456 # if HV_PTE_INDEX_PRESENT >= 32 || HV_PTE_INDEX_MIGRATING >= 32
457 # error Must write the present and migrating bits last
459 if (pte_present(pte
)) {
460 ((u32
*)ptep
)[1] = (u32
)(pte_val(pte
) >> 32);
462 ((u32
*)ptep
)[0] = (u32
)(pte_val(pte
));
464 ((u32
*)ptep
)[0] = (u32
)(pte_val(pte
));
466 ((u32
*)ptep
)[1] = (u32
)(pte_val(pte
) >> 32);
468 #endif /* __tilegx__ */
471 void set_pte(pte_t
*ptep
, pte_t pte
)
473 struct page
*page
= pfn_to_page(pte_pfn(pte
));
475 /* Update the home of a PTE if necessary */
476 pte
= pte_set_home(pte
, page_home(page
));
478 __set_pte(ptep
, pte
);
481 /* Can this mm load a PTE with cached_priority set? */
482 static inline int mm_is_priority_cached(struct mm_struct
*mm
)
484 return mm
->context
.priority_cached
;
488 * Add a priority mapping to an mm_context and
489 * notify the hypervisor if this is the first one.
491 void start_mm_caching(struct mm_struct
*mm
)
493 if (!mm_is_priority_cached(mm
)) {
494 mm
->context
.priority_cached
= -1U;
500 * Validate and return the priority_cached flag. We know if it's zero
501 * that we don't need to scan, since we immediately set it non-zero
502 * when we first consider a MAP_CACHE_PRIORITY mapping.
504 * We only _try_ to acquire the mmap_sem semaphore; if we can't acquire it,
505 * since we're in an interrupt context (servicing switch_mm) we don't
506 * worry about it and don't unset the "priority_cached" field.
507 * Presumably we'll come back later and have more luck and clear
508 * the value then; for now we'll just keep the cache marked for priority.
510 static unsigned int update_priority_cached(struct mm_struct
*mm
)
512 if (mm
->context
.priority_cached
&& down_write_trylock(&mm
->mmap_sem
)) {
513 struct vm_area_struct
*vm
;
514 for (vm
= mm
->mmap
; vm
; vm
= vm
->vm_next
) {
515 if (hv_pte_get_cached_priority(vm
->vm_page_prot
))
519 mm
->context
.priority_cached
= 0;
520 up_write(&mm
->mmap_sem
);
522 return mm
->context
.priority_cached
;
525 /* Set caching correctly for an mm that we are switching to. */
526 void check_mm_caching(struct mm_struct
*prev
, struct mm_struct
*next
)
528 if (!mm_is_priority_cached(next
)) {
530 * If the new mm doesn't use priority caching, just see if we
531 * need the hv_set_caching(), or can assume it's already zero.
533 if (mm_is_priority_cached(prev
))
536 hv_set_caching(update_priority_cached(next
));
542 /* Map an arbitrary MMIO address, homed according to pgprot, into VA space. */
543 void __iomem
*ioremap_prot(resource_size_t phys_addr
, unsigned long size
,
547 struct vm_struct
*area
;
548 unsigned long offset
, last_addr
;
551 /* Don't allow wraparound or zero size */
552 last_addr
= phys_addr
+ size
- 1;
553 if (!size
|| last_addr
< phys_addr
)
556 /* Create a read/write, MMIO VA mapping homed at the requested shim. */
557 pgprot
= PAGE_KERNEL
;
558 pgprot
= hv_pte_set_mode(pgprot
, HV_PTE_MODE_MMIO
);
559 pgprot
= hv_pte_set_lotar(pgprot
, hv_pte_get_lotar(home
));
562 * Mappings have to be page-aligned
564 offset
= phys_addr
& ~PAGE_MASK
;
565 phys_addr
&= PAGE_MASK
;
566 size
= PAGE_ALIGN(last_addr
+1) - phys_addr
;
571 area
= get_vm_area(size
, VM_IOREMAP
/* | other flags? */);
574 area
->phys_addr
= phys_addr
;
576 if (ioremap_page_range((unsigned long)addr
, (unsigned long)addr
+ size
,
577 phys_addr
, pgprot
)) {
578 remove_vm_area((void *)(PAGE_MASK
& (unsigned long) addr
));
581 return (__force
void __iomem
*) (offset
+ (char *)addr
);
583 EXPORT_SYMBOL(ioremap_prot
);
585 /* Map a PCI MMIO bus address into VA space. */
586 void __iomem
*ioremap(resource_size_t phys_addr
, unsigned long size
)
588 panic("ioremap for PCI MMIO is not supported");
590 EXPORT_SYMBOL(ioremap
);
592 /* Unmap an MMIO VA mapping. */
593 void iounmap(volatile void __iomem
*addr_in
)
595 volatile void __iomem
*addr
= (volatile void __iomem
*)
596 (PAGE_MASK
& (unsigned long __force
)addr_in
);
598 vunmap((void * __force
)addr
);
600 /* x86 uses this complicated flow instead of vunmap(). Is
601 * there any particular reason we should do the same? */
602 struct vm_struct
*p
, *o
;
604 /* Use the vm area unlocked, assuming the caller
605 ensures there isn't another iounmap for the same address
606 in parallel. Reuse of the virtual address is prevented by
607 leaving it in the global lists until we're done with it.
608 cpa takes care of the direct mappings. */
609 read_lock(&vmlist_lock
);
610 for (p
= vmlist
; p
; p
= p
->next
) {
614 read_unlock(&vmlist_lock
);
617 pr_err("iounmap: bad address %p\n", addr
);
622 /* Finally remove it */
623 o
= remove_vm_area((void *)addr
);
624 BUG_ON(p
!= o
|| o
== NULL
);
628 EXPORT_SYMBOL(iounmap
);
630 #endif /* CHIP_HAS_MMIO() */