162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  linux/arch/m68k/mm/memory.c
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Copyright (C) 1995  Hamish Macdonald
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/module.h>
962306a36Sopenharmony_ci#include <linux/mm.h>
1062306a36Sopenharmony_ci#include <linux/kernel.h>
1162306a36Sopenharmony_ci#include <linux/string.h>
1262306a36Sopenharmony_ci#include <linux/types.h>
1362306a36Sopenharmony_ci#include <linux/init.h>
1462306a36Sopenharmony_ci#include <linux/pagemap.h>
1562306a36Sopenharmony_ci#include <linux/gfp.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <asm/setup.h>
1862306a36Sopenharmony_ci#include <asm/page.h>
1962306a36Sopenharmony_ci#include <asm/traps.h>
2062306a36Sopenharmony_ci#include <asm/machdep.h>
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/* invalidate page in both caches */
2462306a36Sopenharmony_cistatic inline void clear040(unsigned long paddr)
2562306a36Sopenharmony_ci{
2662306a36Sopenharmony_ci	asm volatile (
2762306a36Sopenharmony_ci		"nop\n\t"
2862306a36Sopenharmony_ci		".chip 68040\n\t"
2962306a36Sopenharmony_ci		"cinvp %%bc,(%0)\n\t"
3062306a36Sopenharmony_ci		".chip 68k"
3162306a36Sopenharmony_ci		: : "a" (paddr));
3262306a36Sopenharmony_ci}
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/* invalidate page in i-cache */
3562306a36Sopenharmony_cistatic inline void cleari040(unsigned long paddr)
3662306a36Sopenharmony_ci{
3762306a36Sopenharmony_ci	asm volatile (
3862306a36Sopenharmony_ci		"nop\n\t"
3962306a36Sopenharmony_ci		".chip 68040\n\t"
4062306a36Sopenharmony_ci		"cinvp %%ic,(%0)\n\t"
4162306a36Sopenharmony_ci		".chip 68k"
4262306a36Sopenharmony_ci		: : "a" (paddr));
4362306a36Sopenharmony_ci}
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/* push page in both caches */
4662306a36Sopenharmony_ci/* RZ: cpush %bc DOES invalidate %ic, regardless of DPI */
4762306a36Sopenharmony_cistatic inline void push040(unsigned long paddr)
4862306a36Sopenharmony_ci{
4962306a36Sopenharmony_ci	asm volatile (
5062306a36Sopenharmony_ci		"nop\n\t"
5162306a36Sopenharmony_ci		".chip 68040\n\t"
5262306a36Sopenharmony_ci		"cpushp %%bc,(%0)\n\t"
5362306a36Sopenharmony_ci		".chip 68k"
5462306a36Sopenharmony_ci		: : "a" (paddr));
5562306a36Sopenharmony_ci}
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci/* push and invalidate page in both caches, must disable ints
5862306a36Sopenharmony_ci * to avoid invalidating valid data */
5962306a36Sopenharmony_cistatic inline void pushcl040(unsigned long paddr)
6062306a36Sopenharmony_ci{
6162306a36Sopenharmony_ci	unsigned long flags;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	local_irq_save(flags);
6462306a36Sopenharmony_ci	push040(paddr);
6562306a36Sopenharmony_ci	if (CPU_IS_060)
6662306a36Sopenharmony_ci		clear040(paddr);
6762306a36Sopenharmony_ci	local_irq_restore(flags);
6862306a36Sopenharmony_ci}
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci/*
7162306a36Sopenharmony_ci * 040: Hit every page containing an address in the range paddr..paddr+len-1.
7262306a36Sopenharmony_ci * (Low order bits of the ea of a CINVP/CPUSHP are "don't care"s).
7362306a36Sopenharmony_ci * Hit every page until there is a page or less to go. Hit the next page,
7462306a36Sopenharmony_ci * and the one after that if the range hits it.
7562306a36Sopenharmony_ci */
7662306a36Sopenharmony_ci/* ++roman: A little bit more care is required here: The CINVP instruction
7762306a36Sopenharmony_ci * invalidates cache entries WITHOUT WRITING DIRTY DATA BACK! So the beginning
7862306a36Sopenharmony_ci * and the end of the region must be treated differently if they are not
7962306a36Sopenharmony_ci * exactly at the beginning or end of a page boundary. Else, maybe too much
8062306a36Sopenharmony_ci * data becomes invalidated and thus lost forever. CPUSHP does what we need:
8162306a36Sopenharmony_ci * it invalidates the page after pushing dirty data to memory. (Thanks to Jes
8262306a36Sopenharmony_ci * for discovering the problem!)
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_ci/* ... but on the '060, CPUSH doesn't invalidate (for us, since we have set
8562306a36Sopenharmony_ci * the DPI bit in the CACR; would it cause problems with temporarily changing
8662306a36Sopenharmony_ci * this?). So we have to push first and then additionally to invalidate.
8762306a36Sopenharmony_ci */
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci/*
9162306a36Sopenharmony_ci * cache_clear() semantics: Clear any cache entries for the area in question,
9262306a36Sopenharmony_ci * without writing back dirty entries first. This is useful if the data will
9362306a36Sopenharmony_ci * be overwritten anyway, e.g. by DMA to memory. The range is defined by a
9462306a36Sopenharmony_ci * _physical_ address.
9562306a36Sopenharmony_ci */
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_civoid cache_clear (unsigned long paddr, int len)
9862306a36Sopenharmony_ci{
9962306a36Sopenharmony_ci    if (CPU_IS_COLDFIRE) {
10062306a36Sopenharmony_ci	clear_cf_bcache(0, DCACHE_MAX_ADDR);
10162306a36Sopenharmony_ci    } else if (CPU_IS_040_OR_060) {
10262306a36Sopenharmony_ci	int tmp;
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	/*
10562306a36Sopenharmony_ci	 * We need special treatment for the first page, in case it
10662306a36Sopenharmony_ci	 * is not page-aligned. Page align the addresses to work
10762306a36Sopenharmony_ci	 * around bug I17 in the 68060.
10862306a36Sopenharmony_ci	 */
10962306a36Sopenharmony_ci	if ((tmp = -paddr & (PAGE_SIZE - 1))) {
11062306a36Sopenharmony_ci	    pushcl040(paddr & PAGE_MASK);
11162306a36Sopenharmony_ci	    if ((len -= tmp) <= 0)
11262306a36Sopenharmony_ci		return;
11362306a36Sopenharmony_ci	    paddr += tmp;
11462306a36Sopenharmony_ci	}
11562306a36Sopenharmony_ci	tmp = PAGE_SIZE;
11662306a36Sopenharmony_ci	paddr &= PAGE_MASK;
11762306a36Sopenharmony_ci	while ((len -= tmp) >= 0) {
11862306a36Sopenharmony_ci	    clear040(paddr);
11962306a36Sopenharmony_ci	    paddr += tmp;
12062306a36Sopenharmony_ci	}
12162306a36Sopenharmony_ci	if ((len += tmp))
12262306a36Sopenharmony_ci	    /* a page boundary gets crossed at the end */
12362306a36Sopenharmony_ci	    pushcl040(paddr);
12462306a36Sopenharmony_ci    }
12562306a36Sopenharmony_ci    else /* 68030 or 68020 */
12662306a36Sopenharmony_ci	asm volatile ("movec %/cacr,%/d0\n\t"
12762306a36Sopenharmony_ci		      "oriw %0,%/d0\n\t"
12862306a36Sopenharmony_ci		      "movec %/d0,%/cacr"
12962306a36Sopenharmony_ci		      : : "i" (FLUSH_I_AND_D)
13062306a36Sopenharmony_ci		      : "d0");
13162306a36Sopenharmony_ci#ifdef CONFIG_M68K_L2_CACHE
13262306a36Sopenharmony_ci    if(mach_l2_flush)
13362306a36Sopenharmony_ci	mach_l2_flush(0);
13462306a36Sopenharmony_ci#endif
13562306a36Sopenharmony_ci}
13662306a36Sopenharmony_ciEXPORT_SYMBOL(cache_clear);
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci/*
14062306a36Sopenharmony_ci * cache_push() semantics: Write back any dirty cache data in the given area,
14162306a36Sopenharmony_ci * and invalidate the range in the instruction cache. It needs not (but may)
14262306a36Sopenharmony_ci * invalidate those entries also in the data cache. The range is defined by a
14362306a36Sopenharmony_ci * _physical_ address.
14462306a36Sopenharmony_ci */
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_civoid cache_push (unsigned long paddr, int len)
14762306a36Sopenharmony_ci{
14862306a36Sopenharmony_ci    if (CPU_IS_COLDFIRE) {
14962306a36Sopenharmony_ci	flush_cf_bcache(0, DCACHE_MAX_ADDR);
15062306a36Sopenharmony_ci    } else if (CPU_IS_040_OR_060) {
15162306a36Sopenharmony_ci	int tmp = PAGE_SIZE;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	/*
15462306a36Sopenharmony_ci         * on 68040 or 68060, push cache lines for pages in the range;
15562306a36Sopenharmony_ci	 * on the '040 this also invalidates the pushed lines, but not on
15662306a36Sopenharmony_ci	 * the '060!
15762306a36Sopenharmony_ci	 */
15862306a36Sopenharmony_ci	len += paddr & (PAGE_SIZE - 1);
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	/*
16162306a36Sopenharmony_ci	 * Work around bug I17 in the 68060 affecting some instruction
16262306a36Sopenharmony_ci	 * lines not being invalidated properly.
16362306a36Sopenharmony_ci	 */
16462306a36Sopenharmony_ci	paddr &= PAGE_MASK;
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	do {
16762306a36Sopenharmony_ci	    push040(paddr);
16862306a36Sopenharmony_ci	    paddr += tmp;
16962306a36Sopenharmony_ci	} while ((len -= tmp) > 0);
17062306a36Sopenharmony_ci    }
17162306a36Sopenharmony_ci    /*
17262306a36Sopenharmony_ci     * 68030/68020 have no writeback cache. On the other hand,
17362306a36Sopenharmony_ci     * cache_push is actually a superset of cache_clear (the lines
17462306a36Sopenharmony_ci     * get written back and invalidated), so we should make sure
17562306a36Sopenharmony_ci     * to perform the corresponding actions. After all, this is getting
17662306a36Sopenharmony_ci     * called in places where we've just loaded code, or whatever, so
17762306a36Sopenharmony_ci     * flushing the icache is appropriate; flushing the dcache shouldn't
17862306a36Sopenharmony_ci     * be required.
17962306a36Sopenharmony_ci     */
18062306a36Sopenharmony_ci    else /* 68030 or 68020 */
18162306a36Sopenharmony_ci	asm volatile ("movec %/cacr,%/d0\n\t"
18262306a36Sopenharmony_ci		      "oriw %0,%/d0\n\t"
18362306a36Sopenharmony_ci		      "movec %/d0,%/cacr"
18462306a36Sopenharmony_ci		      : : "i" (FLUSH_I)
18562306a36Sopenharmony_ci		      : "d0");
18662306a36Sopenharmony_ci#ifdef CONFIG_M68K_L2_CACHE
18762306a36Sopenharmony_ci    if(mach_l2_flush)
18862306a36Sopenharmony_ci	mach_l2_flush(1);
18962306a36Sopenharmony_ci#endif
19062306a36Sopenharmony_ci}
19162306a36Sopenharmony_ciEXPORT_SYMBOL(cache_push);
19262306a36Sopenharmony_ci
193