162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * OpenRISC Linux
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Linux architectural port borrowing liberally from similar works of
662306a36Sopenharmony_ci * others.  All original copyrights apply as per the original source
762306a36Sopenharmony_ci * declaration.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * OpenRISC implementation:
1062306a36Sopenharmony_ci * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
1162306a36Sopenharmony_ci * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
1262306a36Sopenharmony_ci * et al.
1362306a36Sopenharmony_ci */
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#ifndef __ASM_OPENRISC_FIXMAP_H
1662306a36Sopenharmony_ci#define __ASM_OPENRISC_FIXMAP_H
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci/* Why exactly do we need 2 empty pages between the top of the fixed
1962306a36Sopenharmony_ci * addresses and the top of virtual memory?  Something is using that
2062306a36Sopenharmony_ci * memory space but not sure what right now... If you find it, leave
2162306a36Sopenharmony_ci * a comment here.
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_ci#define FIXADDR_TOP	((unsigned long) (-2*PAGE_SIZE))
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#include <linux/kernel.h>
2662306a36Sopenharmony_ci#include <linux/bug.h>
2762306a36Sopenharmony_ci#include <asm/page.h>
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/*
3062306a36Sopenharmony_ci * On OpenRISC we use these special fixed_addresses for doing ioremap
3162306a36Sopenharmony_ci * early in the boot process before memory initialization is complete.
3262306a36Sopenharmony_ci * This is used, in particular, by the early serial console code.
3362306a36Sopenharmony_ci *
3462306a36Sopenharmony_ci * It's not really 'fixmap', per se, but fits loosely into the same
3562306a36Sopenharmony_ci * paradigm.
3662306a36Sopenharmony_ci */
3762306a36Sopenharmony_cienum fixed_addresses {
3862306a36Sopenharmony_ci	/*
3962306a36Sopenharmony_ci	 * FIX_IOREMAP entries are useful for mapping physical address
4062306a36Sopenharmony_ci	 * space before ioremap() is useable, e.g. really early in boot
4162306a36Sopenharmony_ci	 * before kmalloc() is working.
4262306a36Sopenharmony_ci	 */
4362306a36Sopenharmony_ci#define FIX_N_IOREMAPS  32
4462306a36Sopenharmony_ci	FIX_IOREMAP_BEGIN,
4562306a36Sopenharmony_ci	FIX_IOREMAP_END = FIX_IOREMAP_BEGIN + FIX_N_IOREMAPS - 1,
4662306a36Sopenharmony_ci	__end_of_fixed_addresses
4762306a36Sopenharmony_ci};
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci#define FIXADDR_SIZE		(__end_of_fixed_addresses << PAGE_SHIFT)
5062306a36Sopenharmony_ci/* FIXADDR_BOTTOM might be a better name here... */
5162306a36Sopenharmony_ci#define FIXADDR_START		(FIXADDR_TOP - FIXADDR_SIZE)
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci#define __fix_to_virt(x)	(FIXADDR_TOP - ((x) << PAGE_SHIFT))
5462306a36Sopenharmony_ci#define __virt_to_fix(x)	((FIXADDR_TOP - ((x)&PAGE_MASK)) >> PAGE_SHIFT)
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/*
5762306a36Sopenharmony_ci * 'index to address' translation. If anyone tries to use the idx
5862306a36Sopenharmony_ci * directly without tranlation, we catch the bug with a NULL-deference
5962306a36Sopenharmony_ci * kernel oops. Illegal ranges of incoming indices are caught too.
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_cistatic __always_inline unsigned long fix_to_virt(const unsigned int idx)
6262306a36Sopenharmony_ci{
6362306a36Sopenharmony_ci	/*
6462306a36Sopenharmony_ci	 * this branch gets completely eliminated after inlining,
6562306a36Sopenharmony_ci	 * except when someone tries to use fixaddr indices in an
6662306a36Sopenharmony_ci	 * illegal way. (such as mixing up address types or using
6762306a36Sopenharmony_ci	 * out-of-range indices).
6862306a36Sopenharmony_ci	 *
6962306a36Sopenharmony_ci	 * If it doesn't get removed, the linker will complain
7062306a36Sopenharmony_ci	 * loudly with a reasonably clear error message..
7162306a36Sopenharmony_ci	 */
7262306a36Sopenharmony_ci	if (idx >= __end_of_fixed_addresses)
7362306a36Sopenharmony_ci		BUG();
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	return __fix_to_virt(idx);
7662306a36Sopenharmony_ci}
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_cistatic inline unsigned long virt_to_fix(const unsigned long vaddr)
7962306a36Sopenharmony_ci{
8062306a36Sopenharmony_ci	BUG_ON(vaddr >= FIXADDR_TOP || vaddr < FIXADDR_START);
8162306a36Sopenharmony_ci	return __virt_to_fix(vaddr);
8262306a36Sopenharmony_ci}
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci#endif
85