| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * arch/sh/mm/cache-sh7705.c | 
 | 3 |  * | 
 | 4 |  * Copyright (C) 1999, 2000  Niibe Yutaka | 
 | 5 |  * Copyright (C) 2004  Alex Song | 
 | 6 |  * | 
 | 7 |  * This file is subject to the terms and conditions of the GNU General Public | 
 | 8 |  * License.  See the file "COPYING" in the main directory of this archive | 
 | 9 |  * for more details. | 
| Paul Mundt | 39e688a | 2007-03-05 19:46:47 +0900 | [diff] [blame] | 10 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/init.h> | 
 | 13 | #include <linux/mman.h> | 
 | 14 | #include <linux/mm.h> | 
| Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 15 | #include <linux/fs.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/threads.h> | 
 | 17 | #include <asm/addrspace.h> | 
 | 18 | #include <asm/page.h> | 
 | 19 | #include <asm/pgtable.h> | 
 | 20 | #include <asm/processor.h> | 
 | 21 | #include <asm/cache.h> | 
 | 22 | #include <asm/io.h> | 
 | 23 | #include <asm/uaccess.h> | 
 | 24 | #include <asm/pgalloc.h> | 
 | 25 | #include <asm/mmu_context.h> | 
 | 26 | #include <asm/cacheflush.h> | 
 | 27 |  | 
| Paul Mundt | 0f08f33 | 2006-09-27 17:03:56 +0900 | [diff] [blame] | 28 | /* | 
 | 29 |  * The 32KB cache on the SH7705 suffers from the same synonym problem | 
 | 30 |  * as SH4 CPUs | 
 | 31 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static inline void cache_wback_all(void) | 
 | 33 | { | 
 | 34 | 	unsigned long ways, waysize, addrstart; | 
 | 35 |  | 
| Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 36 | 	ways = current_cpu_data.dcache.ways; | 
 | 37 | 	waysize = current_cpu_data.dcache.sets; | 
 | 38 | 	waysize <<= current_cpu_data.dcache.entry_shift; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 |  | 
 | 40 | 	addrstart = CACHE_OC_ADDRESS_ARRAY; | 
 | 41 |  | 
 | 42 | 	do { | 
 | 43 | 		unsigned long addr; | 
 | 44 |  | 
 | 45 | 		for (addr = addrstart; | 
 | 46 | 		     addr < addrstart + waysize; | 
| Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 47 | 		     addr += current_cpu_data.dcache.linesz) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | 			unsigned long data; | 
 | 49 | 			int v = SH_CACHE_UPDATED | SH_CACHE_VALID; | 
 | 50 |  | 
| Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 51 | 			data = __raw_readl(addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 |  | 
 | 53 | 			if ((data & v) == v) | 
| Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 54 | 				__raw_writel(data & ~v, addr); | 
| Paul Mundt | 39e688a | 2007-03-05 19:46:47 +0900 | [diff] [blame] | 55 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | 		} | 
 | 57 |  | 
| Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 58 | 		addrstart += current_cpu_data.dcache.way_incr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | 	} while (--ways); | 
 | 60 | } | 
 | 61 |  | 
 | 62 | /* | 
 | 63 |  * Write back the range of D-cache, and purge the I-cache. | 
 | 64 |  * | 
 | 65 |  * Called from kernel/module.c:sys_init_module and routine for a.out format. | 
 | 66 |  */ | 
| Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 67 | static void sh7705_flush_icache_range(void *args) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { | 
| Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 69 | 	struct flusher_data *data = args; | 
 | 70 | 	unsigned long start, end; | 
 | 71 |  | 
 | 72 | 	start = data->addr1; | 
 | 73 | 	end = data->addr2; | 
 | 74 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | 	__flush_wback_region((void *)start, end - start); | 
 | 76 | } | 
 | 77 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /* | 
 | 79 |  * Writeback&Invalidate the D-cache of the page | 
 | 80 |  */ | 
| Paul Mundt | 2dc2f8e | 2010-01-21 16:05:25 +0900 | [diff] [blame] | 81 | static void __flush_dcache_page(unsigned long phys) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { | 
 | 83 | 	unsigned long ways, waysize, addrstart; | 
| Paul Mundt | 983f4c5 | 2009-09-01 21:12:55 +0900 | [diff] [blame] | 84 | 	unsigned long flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 |  | 
 | 86 | 	phys |= SH_CACHE_VALID; | 
 | 87 |  | 
 | 88 | 	/* | 
 | 89 | 	 * Here, phys is the physical address of the page. We check all the | 
 | 90 | 	 * tags in the cache for those with the same page number as this page | 
 | 91 | 	 * (by masking off the lowest 2 bits of the 19-bit tag; these bits are | 
 | 92 | 	 * derived from the offset within in the 4k page). Matching valid | 
 | 93 | 	 * entries are invalidated. | 
 | 94 | 	 * | 
 | 95 | 	 * Since 2 bits of the cache index are derived from the virtual page | 
 | 96 | 	 * number, knowing this would reduce the number of cache entries to be | 
 | 97 | 	 * searched by a factor of 4. However this function exists to deal with | 
 | 98 | 	 * potential cache aliasing, therefore the optimisation is probably not | 
 | 99 | 	 * possible. | 
 | 100 | 	 */ | 
| Paul Mundt | 983f4c5 | 2009-09-01 21:12:55 +0900 | [diff] [blame] | 101 | 	local_irq_save(flags); | 
| Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 102 | 	jump_to_uncached(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 |  | 
| Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 104 | 	ways = current_cpu_data.dcache.ways; | 
 | 105 | 	waysize = current_cpu_data.dcache.sets; | 
 | 106 | 	waysize <<= current_cpu_data.dcache.entry_shift; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 |  | 
 | 108 | 	addrstart = CACHE_OC_ADDRESS_ARRAY; | 
 | 109 |  | 
 | 110 | 	do { | 
 | 111 | 		unsigned long addr; | 
 | 112 |  | 
 | 113 | 		for (addr = addrstart; | 
 | 114 | 		     addr < addrstart + waysize; | 
| Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 115 | 		     addr += current_cpu_data.dcache.linesz) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | 			unsigned long data; | 
 | 117 |  | 
| Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 118 | 			data = __raw_readl(addr) & (0x1ffffC00 | SH_CACHE_VALID); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | 		        if (data == phys) { | 
 | 120 | 				data &= ~(SH_CACHE_VALID | SH_CACHE_UPDATED); | 
| Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 121 | 				__raw_writel(data, addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | 			} | 
 | 123 | 		} | 
 | 124 |  | 
| Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 125 | 		addrstart += current_cpu_data.dcache.way_incr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | 	} while (--ways); | 
 | 127 |  | 
| Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 128 | 	back_to_cached(); | 
| Paul Mundt | 983f4c5 | 2009-09-01 21:12:55 +0900 | [diff] [blame] | 129 | 	local_irq_restore(flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } | 
 | 131 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | /* | 
 | 133 |  * Write back & invalidate the D-cache of the page. | 
 | 134 |  * (To avoid "alias" issues) | 
 | 135 |  */ | 
| Paul Mundt | c8c2df9 | 2009-09-15 09:47:35 +0900 | [diff] [blame] | 136 | static void sh7705_flush_dcache_page(void *arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { | 
| Paul Mundt | c8c2df9 | 2009-09-15 09:47:35 +0900 | [diff] [blame] | 138 | 	struct page *page = arg; | 
| Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 139 | 	struct address_space *mapping = page_mapping(page); | 
 | 140 |  | 
 | 141 | 	if (mapping && !mapping_mapped(mapping)) | 
| Paul Mundt | 55661fc | 2010-12-01 15:39:51 +0900 | [diff] [blame] | 142 | 		clear_bit(PG_dcache_clean, &page->flags); | 
| Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 143 | 	else | 
| Matt Fleming | 8bd642b | 2009-10-06 21:22:24 +0000 | [diff] [blame] | 144 | 		__flush_dcache_page(__pa(page_address(page))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } | 
 | 146 |  | 
| Paul Mundt | 2dc2f8e | 2010-01-21 16:05:25 +0900 | [diff] [blame] | 147 | static void sh7705_flush_cache_all(void *args) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { | 
| Paul Mundt | 983f4c5 | 2009-09-01 21:12:55 +0900 | [diff] [blame] | 149 | 	unsigned long flags; | 
 | 150 |  | 
 | 151 | 	local_irq_save(flags); | 
| Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 152 | 	jump_to_uncached(); | 
| Paul Mundt | 983f4c5 | 2009-09-01 21:12:55 +0900 | [diff] [blame] | 153 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | 	cache_wback_all(); | 
| Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 155 | 	back_to_cached(); | 
| Paul Mundt | 983f4c5 | 2009-09-01 21:12:55 +0900 | [diff] [blame] | 156 | 	local_irq_restore(flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } | 
 | 158 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | /* | 
 | 160 |  * Write back and invalidate I/D-caches for the page. | 
 | 161 |  * | 
 | 162 |  * ADDRESS: Virtual Address (U0 address) | 
 | 163 |  */ | 
| Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 164 | static void sh7705_flush_cache_page(void *args) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { | 
| Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 166 | 	struct flusher_data *data = args; | 
 | 167 | 	unsigned long pfn = data->addr2; | 
 | 168 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | 	__flush_dcache_page(pfn << PAGE_SHIFT); | 
 | 170 | } | 
 | 171 |  | 
 | 172 | /* | 
 | 173 |  * This is called when a page-cache page is about to be mapped into a | 
 | 174 |  * user process' address space.  It offers an opportunity for a | 
 | 175 |  * port to ensure d-cache/i-cache coherency if necessary. | 
 | 176 |  * | 
 | 177 |  * Not entirely sure why this is necessary on SH3 with 32K cache but | 
 | 178 |  * without it we get occasional "Memory fault" when loading a program. | 
 | 179 |  */ | 
| Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 180 | static void sh7705_flush_icache_page(void *page) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { | 
 | 182 | 	__flush_purge_region(page_address(page), PAGE_SIZE); | 
 | 183 | } | 
| Paul Mundt | 0d051d9 | 2009-08-15 12:53:39 +0900 | [diff] [blame] | 184 |  | 
 | 185 | void __init sh7705_cache_init(void) | 
 | 186 | { | 
| Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 187 | 	local_flush_icache_range	= sh7705_flush_icache_range; | 
 | 188 | 	local_flush_dcache_page		= sh7705_flush_dcache_page; | 
 | 189 | 	local_flush_cache_all		= sh7705_flush_cache_all; | 
 | 190 | 	local_flush_cache_mm		= sh7705_flush_cache_all; | 
 | 191 | 	local_flush_cache_dup_mm	= sh7705_flush_cache_all; | 
 | 192 | 	local_flush_cache_range		= sh7705_flush_cache_all; | 
 | 193 | 	local_flush_cache_page		= sh7705_flush_cache_page; | 
 | 194 | 	local_flush_icache_page		= sh7705_flush_icache_page; | 
| Paul Mundt | 0d051d9 | 2009-08-15 12:53:39 +0900 | [diff] [blame] | 195 | } |