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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <asm/mmu_context.h> |
| 12 | #include <asm/cacheflush.h> |
| 13 | |
Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 14 | #define CACHE_ALIAS (current_cpu_data.dcache.alias_mask) |
Paul Mundt | 8b39526 | 2006-09-27 14:38:02 +0900 | [diff] [blame] | 15 | |
Paul Mundt | 8cf1a74 | 2007-07-24 13:28:26 +0900 | [diff] [blame] | 16 | static inline void *kmap_coherent(struct page *page, unsigned long addr) |
| 17 | { |
| 18 | enum fixed_addresses idx; |
| 19 | unsigned long vaddr, flags; |
| 20 | pte_t pte; |
| 21 | |
| 22 | inc_preempt_count(); |
| 23 | |
| 24 | idx = (addr & current_cpu_data.dcache.alias_mask) >> PAGE_SHIFT; |
| 25 | vaddr = __fix_to_virt(FIX_CMAP_END - idx); |
| 26 | pte = mk_pte(page, PAGE_KERNEL); |
| 27 | |
| 28 | local_irq_save(flags); |
| 29 | flush_tlb_one(get_asid(), vaddr); |
| 30 | local_irq_restore(flags); |
| 31 | |
| 32 | update_mmu_cache(NULL, vaddr, pte); |
| 33 | |
| 34 | return (void *)vaddr; |
| 35 | } |
| 36 | |
| 37 | static inline void kunmap_coherent(struct page *page) |
| 38 | { |
| 39 | dec_preempt_count(); |
| 40 | preempt_check_resched(); |
| 41 | } |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* |
| 44 | * clear_user_page |
| 45 | * @to: P1 address |
| 46 | * @address: U0 address to be mapped |
| 47 | * @page: page (virt_to_page(to)) |
| 48 | */ |
| 49 | void clear_user_page(void *to, unsigned long address, struct page *page) |
| 50 | { |
Paul Mundt | 39e688a | 2007-03-05 19:46:47 +0900 | [diff] [blame] | 51 | __set_bit(PG_mapped, &page->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | if (((address ^ (unsigned long)to) & CACHE_ALIAS) == 0) |
| 53 | clear_page(to); |
| 54 | else { |
Paul Mundt | 8cf1a74 | 2007-07-24 13:28:26 +0900 | [diff] [blame] | 55 | void *vto = kmap_coherent(page, address); |
| 56 | __clear_user_page(vto, to); |
| 57 | kunmap_coherent(vto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * copy_user_page |
| 63 | * @to: P1 address |
| 64 | * @from: P1 address |
| 65 | * @address: U0 address to be mapped |
| 66 | * @page: page (virt_to_page(to)) |
| 67 | */ |
Paul Mundt | 8b39526 | 2006-09-27 14:38:02 +0900 | [diff] [blame] | 68 | void copy_user_page(void *to, void *from, unsigned long address, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | struct page *page) |
| 70 | { |
Paul Mundt | 39e688a | 2007-03-05 19:46:47 +0900 | [diff] [blame] | 71 | __set_bit(PG_mapped, &page->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | if (((address ^ (unsigned long)to) & CACHE_ALIAS) == 0) |
| 73 | copy_page(to, from); |
| 74 | else { |
Paul Mundt | 8cf1a74 | 2007-07-24 13:28:26 +0900 | [diff] [blame] | 75 | void *vfrom = kmap_coherent(page, address); |
| 76 | __copy_user_page(vfrom, from, to); |
| 77 | kunmap_coherent(vfrom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
Paul Mundt | 39e688a | 2007-03-05 19:46:47 +0900 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * For SH-4, we have our own implementation for ptep_get_and_clear |
| 83 | */ |
| 84 | inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep) |
| 85 | { |
| 86 | pte_t pte = *ptep; |
| 87 | |
| 88 | pte_clear(mm, addr, ptep); |
| 89 | if (!pte_not_present(pte)) { |
| 90 | unsigned long pfn = pte_pfn(pte); |
| 91 | if (pfn_valid(pfn)) { |
| 92 | struct page *page = pfn_to_page(pfn); |
| 93 | struct address_space *mapping = page_mapping(page); |
| 94 | if (!mapping || !mapping_writably_mapped(mapping)) |
| 95 | __clear_bit(PG_mapped, &page->flags); |
| 96 | } |
| 97 | } |
| 98 | return pte; |
| 99 | } |