18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/arch/m68k/mm/memory.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 1995 Hamish Macdonald 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/module.h> 98c2ecf20Sopenharmony_ci#include <linux/mm.h> 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/string.h> 128c2ecf20Sopenharmony_ci#include <linux/types.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci#include <linux/pagemap.h> 158c2ecf20Sopenharmony_ci#include <linux/gfp.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include <asm/setup.h> 188c2ecf20Sopenharmony_ci#include <asm/segment.h> 198c2ecf20Sopenharmony_ci#include <asm/page.h> 208c2ecf20Sopenharmony_ci#include <asm/traps.h> 218c2ecf20Sopenharmony_ci#include <asm/machdep.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci/* invalidate page in both caches */ 258c2ecf20Sopenharmony_cistatic inline void clear040(unsigned long paddr) 268c2ecf20Sopenharmony_ci{ 278c2ecf20Sopenharmony_ci asm volatile ( 288c2ecf20Sopenharmony_ci "nop\n\t" 298c2ecf20Sopenharmony_ci ".chip 68040\n\t" 308c2ecf20Sopenharmony_ci "cinvp %%bc,(%0)\n\t" 318c2ecf20Sopenharmony_ci ".chip 68k" 328c2ecf20Sopenharmony_ci : : "a" (paddr)); 338c2ecf20Sopenharmony_ci} 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci/* invalidate page in i-cache */ 368c2ecf20Sopenharmony_cistatic inline void cleari040(unsigned long paddr) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci asm volatile ( 398c2ecf20Sopenharmony_ci "nop\n\t" 408c2ecf20Sopenharmony_ci ".chip 68040\n\t" 418c2ecf20Sopenharmony_ci "cinvp %%ic,(%0)\n\t" 428c2ecf20Sopenharmony_ci ".chip 68k" 438c2ecf20Sopenharmony_ci : : "a" (paddr)); 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/* push page in both caches */ 478c2ecf20Sopenharmony_ci/* RZ: cpush %bc DOES invalidate %ic, regardless of DPI */ 488c2ecf20Sopenharmony_cistatic inline void push040(unsigned long paddr) 498c2ecf20Sopenharmony_ci{ 508c2ecf20Sopenharmony_ci asm volatile ( 518c2ecf20Sopenharmony_ci "nop\n\t" 528c2ecf20Sopenharmony_ci ".chip 68040\n\t" 538c2ecf20Sopenharmony_ci "cpushp %%bc,(%0)\n\t" 548c2ecf20Sopenharmony_ci ".chip 68k" 558c2ecf20Sopenharmony_ci : : "a" (paddr)); 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci/* push and invalidate page in both caches, must disable ints 598c2ecf20Sopenharmony_ci * to avoid invalidating valid data */ 608c2ecf20Sopenharmony_cistatic inline void pushcl040(unsigned long paddr) 618c2ecf20Sopenharmony_ci{ 628c2ecf20Sopenharmony_ci unsigned long flags; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci local_irq_save(flags); 658c2ecf20Sopenharmony_ci push040(paddr); 668c2ecf20Sopenharmony_ci if (CPU_IS_060) 678c2ecf20Sopenharmony_ci clear040(paddr); 688c2ecf20Sopenharmony_ci local_irq_restore(flags); 698c2ecf20Sopenharmony_ci} 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci/* 728c2ecf20Sopenharmony_ci * 040: Hit every page containing an address in the range paddr..paddr+len-1. 738c2ecf20Sopenharmony_ci * (Low order bits of the ea of a CINVP/CPUSHP are "don't care"s). 748c2ecf20Sopenharmony_ci * Hit every page until there is a page or less to go. Hit the next page, 758c2ecf20Sopenharmony_ci * and the one after that if the range hits it. 768c2ecf20Sopenharmony_ci */ 778c2ecf20Sopenharmony_ci/* ++roman: A little bit more care is required here: The CINVP instruction 788c2ecf20Sopenharmony_ci * invalidates cache entries WITHOUT WRITING DIRTY DATA BACK! So the beginning 798c2ecf20Sopenharmony_ci * and the end of the region must be treated differently if they are not 808c2ecf20Sopenharmony_ci * exactly at the beginning or end of a page boundary. Else, maybe too much 818c2ecf20Sopenharmony_ci * data becomes invalidated and thus lost forever. CPUSHP does what we need: 828c2ecf20Sopenharmony_ci * it invalidates the page after pushing dirty data to memory. (Thanks to Jes 838c2ecf20Sopenharmony_ci * for discovering the problem!) 848c2ecf20Sopenharmony_ci */ 858c2ecf20Sopenharmony_ci/* ... but on the '060, CPUSH doesn't invalidate (for us, since we have set 868c2ecf20Sopenharmony_ci * the DPI bit in the CACR; would it cause problems with temporarily changing 878c2ecf20Sopenharmony_ci * this?). So we have to push first and then additionally to invalidate. 888c2ecf20Sopenharmony_ci */ 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci/* 928c2ecf20Sopenharmony_ci * cache_clear() semantics: Clear any cache entries for the area in question, 938c2ecf20Sopenharmony_ci * without writing back dirty entries first. This is useful if the data will 948c2ecf20Sopenharmony_ci * be overwritten anyway, e.g. by DMA to memory. The range is defined by a 958c2ecf20Sopenharmony_ci * _physical_ address. 968c2ecf20Sopenharmony_ci */ 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_civoid cache_clear (unsigned long paddr, int len) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci if (CPU_IS_COLDFIRE) { 1018c2ecf20Sopenharmony_ci clear_cf_bcache(0, DCACHE_MAX_ADDR); 1028c2ecf20Sopenharmony_ci } else if (CPU_IS_040_OR_060) { 1038c2ecf20Sopenharmony_ci int tmp; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci /* 1068c2ecf20Sopenharmony_ci * We need special treatment for the first page, in case it 1078c2ecf20Sopenharmony_ci * is not page-aligned. Page align the addresses to work 1088c2ecf20Sopenharmony_ci * around bug I17 in the 68060. 1098c2ecf20Sopenharmony_ci */ 1108c2ecf20Sopenharmony_ci if ((tmp = -paddr & (PAGE_SIZE - 1))) { 1118c2ecf20Sopenharmony_ci pushcl040(paddr & PAGE_MASK); 1128c2ecf20Sopenharmony_ci if ((len -= tmp) <= 0) 1138c2ecf20Sopenharmony_ci return; 1148c2ecf20Sopenharmony_ci paddr += tmp; 1158c2ecf20Sopenharmony_ci } 1168c2ecf20Sopenharmony_ci tmp = PAGE_SIZE; 1178c2ecf20Sopenharmony_ci paddr &= PAGE_MASK; 1188c2ecf20Sopenharmony_ci while ((len -= tmp) >= 0) { 1198c2ecf20Sopenharmony_ci clear040(paddr); 1208c2ecf20Sopenharmony_ci paddr += tmp; 1218c2ecf20Sopenharmony_ci } 1228c2ecf20Sopenharmony_ci if ((len += tmp)) 1238c2ecf20Sopenharmony_ci /* a page boundary gets crossed at the end */ 1248c2ecf20Sopenharmony_ci pushcl040(paddr); 1258c2ecf20Sopenharmony_ci } 1268c2ecf20Sopenharmony_ci else /* 68030 or 68020 */ 1278c2ecf20Sopenharmony_ci asm volatile ("movec %/cacr,%/d0\n\t" 1288c2ecf20Sopenharmony_ci "oriw %0,%/d0\n\t" 1298c2ecf20Sopenharmony_ci "movec %/d0,%/cacr" 1308c2ecf20Sopenharmony_ci : : "i" (FLUSH_I_AND_D) 1318c2ecf20Sopenharmony_ci : "d0"); 1328c2ecf20Sopenharmony_ci#ifdef CONFIG_M68K_L2_CACHE 1338c2ecf20Sopenharmony_ci if(mach_l2_flush) 1348c2ecf20Sopenharmony_ci mach_l2_flush(0); 1358c2ecf20Sopenharmony_ci#endif 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cache_clear); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci/* 1418c2ecf20Sopenharmony_ci * cache_push() semantics: Write back any dirty cache data in the given area, 1428c2ecf20Sopenharmony_ci * and invalidate the range in the instruction cache. It needs not (but may) 1438c2ecf20Sopenharmony_ci * invalidate those entries also in the data cache. The range is defined by a 1448c2ecf20Sopenharmony_ci * _physical_ address. 1458c2ecf20Sopenharmony_ci */ 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_civoid cache_push (unsigned long paddr, int len) 1488c2ecf20Sopenharmony_ci{ 1498c2ecf20Sopenharmony_ci if (CPU_IS_COLDFIRE) { 1508c2ecf20Sopenharmony_ci flush_cf_bcache(0, DCACHE_MAX_ADDR); 1518c2ecf20Sopenharmony_ci } else if (CPU_IS_040_OR_060) { 1528c2ecf20Sopenharmony_ci int tmp = PAGE_SIZE; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci /* 1558c2ecf20Sopenharmony_ci * on 68040 or 68060, push cache lines for pages in the range; 1568c2ecf20Sopenharmony_ci * on the '040 this also invalidates the pushed lines, but not on 1578c2ecf20Sopenharmony_ci * the '060! 1588c2ecf20Sopenharmony_ci */ 1598c2ecf20Sopenharmony_ci len += paddr & (PAGE_SIZE - 1); 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci /* 1628c2ecf20Sopenharmony_ci * Work around bug I17 in the 68060 affecting some instruction 1638c2ecf20Sopenharmony_ci * lines not being invalidated properly. 1648c2ecf20Sopenharmony_ci */ 1658c2ecf20Sopenharmony_ci paddr &= PAGE_MASK; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci do { 1688c2ecf20Sopenharmony_ci push040(paddr); 1698c2ecf20Sopenharmony_ci paddr += tmp; 1708c2ecf20Sopenharmony_ci } while ((len -= tmp) > 0); 1718c2ecf20Sopenharmony_ci } 1728c2ecf20Sopenharmony_ci /* 1738c2ecf20Sopenharmony_ci * 68030/68020 have no writeback cache. On the other hand, 1748c2ecf20Sopenharmony_ci * cache_push is actually a superset of cache_clear (the lines 1758c2ecf20Sopenharmony_ci * get written back and invalidated), so we should make sure 1768c2ecf20Sopenharmony_ci * to perform the corresponding actions. After all, this is getting 1778c2ecf20Sopenharmony_ci * called in places where we've just loaded code, or whatever, so 1788c2ecf20Sopenharmony_ci * flushing the icache is appropriate; flushing the dcache shouldn't 1798c2ecf20Sopenharmony_ci * be required. 1808c2ecf20Sopenharmony_ci */ 1818c2ecf20Sopenharmony_ci else /* 68030 or 68020 */ 1828c2ecf20Sopenharmony_ci asm volatile ("movec %/cacr,%/d0\n\t" 1838c2ecf20Sopenharmony_ci "oriw %0,%/d0\n\t" 1848c2ecf20Sopenharmony_ci "movec %/d0,%/cacr" 1858c2ecf20Sopenharmony_ci : : "i" (FLUSH_I) 1868c2ecf20Sopenharmony_ci : "d0"); 1878c2ecf20Sopenharmony_ci#ifdef CONFIG_M68K_L2_CACHE 1888c2ecf20Sopenharmony_ci if(mach_l2_flush) 1898c2ecf20Sopenharmony_ci mach_l2_flush(1); 1908c2ecf20Sopenharmony_ci#endif 1918c2ecf20Sopenharmony_ci} 1928c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cache_push); 1938c2ecf20Sopenharmony_ci 194