xref: /OK3568_Linux_fs/kernel/arch/openrisc/include/asm/fixmap.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * OpenRISC Linux
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Linux architectural port borrowing liberally from similar works of
6*4882a593Smuzhiyun  * others.  All original copyrights apply as per the original source
7*4882a593Smuzhiyun  * declaration.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * OpenRISC implementation:
10*4882a593Smuzhiyun  * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
11*4882a593Smuzhiyun  * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
12*4882a593Smuzhiyun  * et al.
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #ifndef __ASM_OPENRISC_FIXMAP_H
16*4882a593Smuzhiyun #define __ASM_OPENRISC_FIXMAP_H
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /* Why exactly do we need 2 empty pages between the top of the fixed
19*4882a593Smuzhiyun  * addresses and the top of virtual memory?  Something is using that
20*4882a593Smuzhiyun  * memory space but not sure what right now... If you find it, leave
21*4882a593Smuzhiyun  * a comment here.
22*4882a593Smuzhiyun  */
23*4882a593Smuzhiyun #define FIXADDR_TOP	((unsigned long) (-2*PAGE_SIZE))
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <linux/kernel.h>
26*4882a593Smuzhiyun #include <linux/bug.h>
27*4882a593Smuzhiyun #include <asm/page.h>
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun  * On OpenRISC we use these special fixed_addresses for doing ioremap
31*4882a593Smuzhiyun  * early in the boot process before memory initialization is complete.
32*4882a593Smuzhiyun  * This is used, in particular, by the early serial console code.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * It's not really 'fixmap', per se, but fits loosely into the same
35*4882a593Smuzhiyun  * paradigm.
36*4882a593Smuzhiyun  */
37*4882a593Smuzhiyun enum fixed_addresses {
38*4882a593Smuzhiyun 	/*
39*4882a593Smuzhiyun 	 * FIX_IOREMAP entries are useful for mapping physical address
40*4882a593Smuzhiyun 	 * space before ioremap() is useable, e.g. really early in boot
41*4882a593Smuzhiyun 	 * before kmalloc() is working.
42*4882a593Smuzhiyun 	 */
43*4882a593Smuzhiyun #define FIX_N_IOREMAPS  32
44*4882a593Smuzhiyun 	FIX_IOREMAP_BEGIN,
45*4882a593Smuzhiyun 	FIX_IOREMAP_END = FIX_IOREMAP_BEGIN + FIX_N_IOREMAPS - 1,
46*4882a593Smuzhiyun 	__end_of_fixed_addresses
47*4882a593Smuzhiyun };
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #define FIXADDR_SIZE		(__end_of_fixed_addresses << PAGE_SHIFT)
50*4882a593Smuzhiyun /* FIXADDR_BOTTOM might be a better name here... */
51*4882a593Smuzhiyun #define FIXADDR_START		(FIXADDR_TOP - FIXADDR_SIZE)
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #define __fix_to_virt(x)	(FIXADDR_TOP - ((x) << PAGE_SHIFT))
54*4882a593Smuzhiyun #define __virt_to_fix(x)	((FIXADDR_TOP - ((x)&PAGE_MASK)) >> PAGE_SHIFT)
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun /*
57*4882a593Smuzhiyun  * 'index to address' translation. If anyone tries to use the idx
58*4882a593Smuzhiyun  * directly without tranlation, we catch the bug with a NULL-deference
59*4882a593Smuzhiyun  * kernel oops. Illegal ranges of incoming indices are caught too.
60*4882a593Smuzhiyun  */
fix_to_virt(const unsigned int idx)61*4882a593Smuzhiyun static __always_inline unsigned long fix_to_virt(const unsigned int idx)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	/*
64*4882a593Smuzhiyun 	 * this branch gets completely eliminated after inlining,
65*4882a593Smuzhiyun 	 * except when someone tries to use fixaddr indices in an
66*4882a593Smuzhiyun 	 * illegal way. (such as mixing up address types or using
67*4882a593Smuzhiyun 	 * out-of-range indices).
68*4882a593Smuzhiyun 	 *
69*4882a593Smuzhiyun 	 * If it doesn't get removed, the linker will complain
70*4882a593Smuzhiyun 	 * loudly with a reasonably clear error message..
71*4882a593Smuzhiyun 	 */
72*4882a593Smuzhiyun 	if (idx >= __end_of_fixed_addresses)
73*4882a593Smuzhiyun 		BUG();
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	return __fix_to_virt(idx);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
virt_to_fix(const unsigned long vaddr)78*4882a593Smuzhiyun static inline unsigned long virt_to_fix(const unsigned long vaddr)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	BUG_ON(vaddr >= FIXADDR_TOP || vaddr < FIXADDR_START);
81*4882a593Smuzhiyun 	return __virt_to_fix(vaddr);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun #endif
85