Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/sh/mm/pg-sh4.c |
| 3 | * |
| 4 | * Copyright (C) 1999, 2000, 2002 Niibe Yutaka |
Paul Mundt | 8cf1a74 | 2007-07-24 13:28:26 +0900 | [diff] [blame] | 5 | * Copyright (C) 2002 - 2007 Paul Mundt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Released under the terms of the GNU GPL v2.0. |
| 8 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/mm.h> |
Paul Mundt | 52e2778 | 2006-11-21 11:09:41 +0900 | [diff] [blame] | 10 | #include <linux/mutex.h> |
Paul Mundt | e06c4e5 | 2007-07-31 13:01:43 +0900 | [diff] [blame] | 11 | #include <linux/fs.h> |
Paul Mundt | 7747b9a | 2007-11-05 16:12:32 +0900 | [diff] [blame] | 12 | #include <linux/highmem.h> |
| 13 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <asm/mmu_context.h> |
| 15 | #include <asm/cacheflush.h> |
| 16 | |
Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 17 | #define CACHE_ALIAS (current_cpu_data.dcache.alias_mask) |
Paul Mundt | 8b39526 | 2006-09-27 14:38:02 +0900 | [diff] [blame] | 18 | |
Paul Mundt | 8cf1a74 | 2007-07-24 13:28:26 +0900 | [diff] [blame] | 19 | static inline void *kmap_coherent(struct page *page, unsigned long addr) |
| 20 | { |
| 21 | enum fixed_addresses idx; |
| 22 | unsigned long vaddr, flags; |
| 23 | pte_t pte; |
| 24 | |
| 25 | inc_preempt_count(); |
| 26 | |
| 27 | idx = (addr & current_cpu_data.dcache.alias_mask) >> PAGE_SHIFT; |
| 28 | vaddr = __fix_to_virt(FIX_CMAP_END - idx); |
| 29 | pte = mk_pte(page, PAGE_KERNEL); |
| 30 | |
| 31 | local_irq_save(flags); |
| 32 | flush_tlb_one(get_asid(), vaddr); |
| 33 | local_irq_restore(flags); |
| 34 | |
| 35 | update_mmu_cache(NULL, vaddr, pte); |
| 36 | |
| 37 | return (void *)vaddr; |
| 38 | } |
| 39 | |
| 40 | static inline void kunmap_coherent(struct page *page) |
| 41 | { |
| 42 | dec_preempt_count(); |
| 43 | preempt_check_resched(); |
| 44 | } |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | /* |
| 47 | * clear_user_page |
| 48 | * @to: P1 address |
| 49 | * @address: U0 address to be mapped |
| 50 | * @page: page (virt_to_page(to)) |
| 51 | */ |
| 52 | void clear_user_page(void *to, unsigned long address, struct page *page) |
| 53 | { |
Paul Mundt | 39e688a | 2007-03-05 19:46:47 +0900 | [diff] [blame] | 54 | __set_bit(PG_mapped, &page->flags); |
Paul Mundt | ba1789e | 2007-11-05 16:18:16 +0900 | [diff] [blame] | 55 | |
| 56 | clear_page(to); |
| 57 | if ((((address & PAGE_MASK) ^ (unsigned long)to) & CACHE_ALIAS)) |
| 58 | __flush_wback_region(to, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Paul Mundt | ba1789e | 2007-11-05 16:18:16 +0900 | [diff] [blame] | 61 | void copy_to_user_page(struct vm_area_struct *vma, struct page *page, |
| 62 | unsigned long vaddr, void *dst, const void *src, |
| 63 | unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
Paul Mundt | ba1789e | 2007-11-05 16:18:16 +0900 | [diff] [blame] | 65 | void *vto; |
| 66 | |
Paul Mundt | 39e688a | 2007-03-05 19:46:47 +0900 | [diff] [blame] | 67 | __set_bit(PG_mapped, &page->flags); |
Paul Mundt | ba1789e | 2007-11-05 16:18:16 +0900 | [diff] [blame] | 68 | |
| 69 | vto = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK); |
| 70 | memcpy(vto, src, len); |
| 71 | kunmap_coherent(vto); |
| 72 | |
| 73 | if (vma->vm_flags & VM_EXEC) |
| 74 | flush_cache_page(vma, vaddr, page_to_pfn(page)); |
| 75 | } |
| 76 | |
| 77 | void copy_from_user_page(struct vm_area_struct *vma, struct page *page, |
| 78 | unsigned long vaddr, void *dst, const void *src, |
| 79 | unsigned long len) |
| 80 | { |
| 81 | void *vfrom; |
| 82 | |
| 83 | __set_bit(PG_mapped, &page->flags); |
| 84 | |
| 85 | vfrom = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK); |
| 86 | memcpy(dst, vfrom, len); |
| 87 | kunmap_coherent(vfrom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
Paul Mundt | 39e688a | 2007-03-05 19:46:47 +0900 | [diff] [blame] | 89 | |
Paul Mundt | 7747b9a | 2007-11-05 16:12:32 +0900 | [diff] [blame] | 90 | void copy_user_highpage(struct page *to, struct page *from, |
| 91 | unsigned long vaddr, struct vm_area_struct *vma) |
| 92 | { |
| 93 | void *vfrom, *vto; |
| 94 | |
| 95 | __set_bit(PG_mapped, &to->flags); |
| 96 | |
| 97 | vto = kmap_atomic(to, KM_USER1); |
| 98 | vfrom = kmap_coherent(from, vaddr); |
| 99 | copy_page(vto, vfrom); |
| 100 | kunmap_coherent(vfrom); |
| 101 | |
| 102 | if (((vaddr ^ (unsigned long)vto) & CACHE_ALIAS)) |
| 103 | __flush_wback_region(vto, PAGE_SIZE); |
| 104 | |
| 105 | kunmap_atomic(vto, KM_USER1); |
| 106 | /* Make sure this page is cleared on other CPU's too before using it */ |
| 107 | smp_wmb(); |
| 108 | } |
| 109 | EXPORT_SYMBOL(copy_user_highpage); |
| 110 | |
Paul Mundt | 39e688a | 2007-03-05 19:46:47 +0900 | [diff] [blame] | 111 | /* |
| 112 | * For SH-4, we have our own implementation for ptep_get_and_clear |
| 113 | */ |
| 114 | inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep) |
| 115 | { |
| 116 | pte_t pte = *ptep; |
| 117 | |
| 118 | pte_clear(mm, addr, ptep); |
| 119 | if (!pte_not_present(pte)) { |
| 120 | unsigned long pfn = pte_pfn(pte); |
| 121 | if (pfn_valid(pfn)) { |
| 122 | struct page *page = pfn_to_page(pfn); |
| 123 | struct address_space *mapping = page_mapping(page); |
| 124 | if (!mapping || !mapping_writably_mapped(mapping)) |
| 125 | __clear_bit(PG_mapped, &page->flags); |
| 126 | } |
| 127 | } |
| 128 | return pte; |
| 129 | } |