xref: /OK3568_Linux_fs/kernel/arch/mips/lib/uncached.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * This file is subject to the terms and conditions of the GNU General Public
3*4882a593Smuzhiyun  * License.  See the file "COPYING" in the main directory of this archive
4*4882a593Smuzhiyun  * for more details.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 2005 Thiemo Seufer
7*4882a593Smuzhiyun  * Copyright (C) 2005  MIPS Technologies, Inc.	All rights reserved.
8*4882a593Smuzhiyun  *	Author: Maciej W. Rozycki <macro@mips.com>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <asm/addrspace.h>
13*4882a593Smuzhiyun #include <asm/bug.h>
14*4882a593Smuzhiyun #include <asm/cacheflush.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #ifndef CKSEG2
17*4882a593Smuzhiyun #define CKSEG2 CKSSEG
18*4882a593Smuzhiyun #endif
19*4882a593Smuzhiyun #ifndef TO_PHYS_MASK
20*4882a593Smuzhiyun #define TO_PHYS_MASK -1
21*4882a593Smuzhiyun #endif
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun  * FUNC is executed in one of the uncached segments, depending on its
25*4882a593Smuzhiyun  * original address as follows:
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * 1. If the original address is in CKSEG0 or CKSEG1, then the uncached
28*4882a593Smuzhiyun  *    segment used is CKSEG1.
29*4882a593Smuzhiyun  * 2. If the original address is in XKPHYS, then the uncached segment
30*4882a593Smuzhiyun  *    used is XKPHYS(2).
31*4882a593Smuzhiyun  * 3. Otherwise it's a bug.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * The same remapping is done with the stack pointer.  Stack handling
34*4882a593Smuzhiyun  * works because we don't handle stack arguments or more complex return
35*4882a593Smuzhiyun  * values, so we can avoid sharing the same stack area between a cached
36*4882a593Smuzhiyun  * and the uncached mode.
37*4882a593Smuzhiyun  */
run_uncached(void * func)38*4882a593Smuzhiyun unsigned long run_uncached(void *func)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	register long ret __asm__("$2");
41*4882a593Smuzhiyun 	long lfunc = (long)func, ufunc;
42*4882a593Smuzhiyun 	long usp;
43*4882a593Smuzhiyun 	long sp;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	__asm__("move %0, $sp" : "=r" (sp));
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	if (sp >= (long)CKSEG0 && sp < (long)CKSEG2)
48*4882a593Smuzhiyun 		usp = CKSEG1ADDR(sp);
49*4882a593Smuzhiyun #ifdef CONFIG_64BIT
50*4882a593Smuzhiyun 	else if ((long long)sp >= (long long)PHYS_TO_XKPHYS(0, 0) &&
51*4882a593Smuzhiyun 		 (long long)sp < (long long)PHYS_TO_XKPHYS(8, 0))
52*4882a593Smuzhiyun 		usp = PHYS_TO_XKPHYS(K_CALG_UNCACHED,
53*4882a593Smuzhiyun 				     XKPHYS_TO_PHYS((long long)sp));
54*4882a593Smuzhiyun #endif
55*4882a593Smuzhiyun 	else {
56*4882a593Smuzhiyun 		BUG();
57*4882a593Smuzhiyun 		usp = sp;
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun 	if (lfunc >= (long)CKSEG0 && lfunc < (long)CKSEG2)
60*4882a593Smuzhiyun 		ufunc = CKSEG1ADDR(lfunc);
61*4882a593Smuzhiyun #ifdef CONFIG_64BIT
62*4882a593Smuzhiyun 	else if ((long long)lfunc >= (long long)PHYS_TO_XKPHYS(0, 0) &&
63*4882a593Smuzhiyun 		 (long long)lfunc < (long long)PHYS_TO_XKPHYS(8, 0))
64*4882a593Smuzhiyun 		ufunc = PHYS_TO_XKPHYS(K_CALG_UNCACHED,
65*4882a593Smuzhiyun 				       XKPHYS_TO_PHYS((long long)lfunc));
66*4882a593Smuzhiyun #endif
67*4882a593Smuzhiyun 	else {
68*4882a593Smuzhiyun 		BUG();
69*4882a593Smuzhiyun 		ufunc = lfunc;
70*4882a593Smuzhiyun 	}
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	__asm__ __volatile__ (
73*4882a593Smuzhiyun 		"	move	$16, $sp\n"
74*4882a593Smuzhiyun 		"	move	$sp, %1\n"
75*4882a593Smuzhiyun 		"	jalr	%2\n"
76*4882a593Smuzhiyun 		"	move	$sp, $16"
77*4882a593Smuzhiyun 		: "=r" (ret)
78*4882a593Smuzhiyun 		: "r" (usp), "r" (ufunc)
79*4882a593Smuzhiyun 		: "$16", "$31");
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	return ret;
82*4882a593Smuzhiyun }
83