xref: /openbmc/linux/arch/mips/include/asm/maar.h (revision 2874c5fd)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2ab9988a3SPaul Burton /*
3ab9988a3SPaul Burton  * Copyright (C) 2014 Imagination Technologies
4fb615d61SPaul Burton  * Author: Paul Burton <paul.burton@mips.com>
5ab9988a3SPaul Burton  */
6ab9988a3SPaul Burton 
7ab9988a3SPaul Burton #ifndef __MIPS_ASM_MIPS_MAAR_H__
8ab9988a3SPaul Burton #define __MIPS_ASM_MIPS_MAAR_H__
9ab9988a3SPaul Burton 
10ab9988a3SPaul Burton #include <asm/hazards.h>
11ab9988a3SPaul Burton #include <asm/mipsregs.h>
12ab9988a3SPaul Burton 
13ab9988a3SPaul Burton /**
14ab9988a3SPaul Burton  * platform_maar_init() - perform platform-level MAAR configuration
15ab9988a3SPaul Burton  * @num_pairs:	The number of MAAR pairs present in the system.
16ab9988a3SPaul Burton  *
17ab9988a3SPaul Burton  * Platforms should implement this function such that it configures as many
18ab9988a3SPaul Burton  * MAAR pairs as required, from 0 up to the maximum of num_pairs-1, and returns
19ab9988a3SPaul Burton  * the number that were used. Any further MAARs will be configured to be
20ab9988a3SPaul Burton  * invalid. The default implementation of this function will simply indicate
21ab9988a3SPaul Burton  * that it has configured 0 MAAR pairs.
22ab9988a3SPaul Burton  *
23ab9988a3SPaul Burton  * Return:	The number of MAAR pairs configured.
24ab9988a3SPaul Burton  */
2527d8d449SBjorn Helgaas unsigned platform_maar_init(unsigned num_pairs);
26ab9988a3SPaul Burton 
27ab9988a3SPaul Burton /**
28ab9988a3SPaul Burton  * write_maar_pair() - write to a pair of MAARs
29ab9988a3SPaul Burton  * @idx:	The index of the pair (ie. use MAARs idx*2 & (idx*2)+1).
30ab9988a3SPaul Burton  * @lower:	The lowest address that the MAAR pair will affect. Must be
31ab9988a3SPaul Burton  *		aligned to a 2^16 byte boundary.
32ab9988a3SPaul Burton  * @upper:	The highest address that the MAAR pair will affect. Must be
33ab9988a3SPaul Burton  *		aligned to one byte before a 2^16 byte boundary.
34ab9988a3SPaul Burton  * @attrs:	The accessibility attributes to program, eg. MIPS_MAAR_S. The
35f359a111SJames Hogan  *		MIPS_MAAR_VL attribute will automatically be set.
36ab9988a3SPaul Burton  *
37ab9988a3SPaul Burton  * Program the pair of MAAR registers specified by idx to apply the attributes
38ab9988a3SPaul Burton  * specified by attrs to the range of addresses from lower to higher.
39ab9988a3SPaul Burton  */
40ab9988a3SPaul Burton static inline void write_maar_pair(unsigned idx, phys_addr_t lower,
41ab9988a3SPaul Burton 				   phys_addr_t upper, unsigned attrs)
42ab9988a3SPaul Burton {
43ab9988a3SPaul Burton 	/* Addresses begin at bit 16, but are shifted right 4 bits */
44ab9988a3SPaul Burton 	BUG_ON(lower & (0xffff | ~(MIPS_MAAR_ADDR << 4)));
45ab9988a3SPaul Burton 	BUG_ON(((upper & 0xffff) != 0xffff)
46ab9988a3SPaul Burton 		|| ((upper & ~0xffffull) & ~(MIPS_MAAR_ADDR << 4)));
47ab9988a3SPaul Burton 
48f359a111SJames Hogan 	/* Automatically set MIPS_MAAR_VL */
49f359a111SJames Hogan 	attrs |= MIPS_MAAR_VL;
50ab9988a3SPaul Burton 
51f359a111SJames Hogan 	/* Write the upper address & attributes (only MIPS_MAAR_VL matters) */
52ab9988a3SPaul Burton 	write_c0_maari(idx << 1);
53ab9988a3SPaul Burton 	back_to_back_c0_hazard();
54ab9988a3SPaul Burton 	write_c0_maar(((upper >> 4) & MIPS_MAAR_ADDR) | attrs);
55ab9988a3SPaul Burton 	back_to_back_c0_hazard();
56ab9988a3SPaul Burton 
57ab9988a3SPaul Burton 	/* Write the lower address & attributes */
58ab9988a3SPaul Burton 	write_c0_maari((idx << 1) | 0x1);
59ab9988a3SPaul Burton 	back_to_back_c0_hazard();
60ab9988a3SPaul Burton 	write_c0_maar((lower >> 4) | attrs);
61ab9988a3SPaul Burton 	back_to_back_c0_hazard();
62ab9988a3SPaul Burton }
63ab9988a3SPaul Burton 
64ab9988a3SPaul Burton /**
65e060f6edSPaul Burton  * maar_init() - initialise MAARs
66e060f6edSPaul Burton  *
67e060f6edSPaul Burton  * Performs initialisation of MAARs for the current CPU, making use of the
68e060f6edSPaul Burton  * platforms implementation of platform_maar_init where necessary and
69e060f6edSPaul Burton  * duplicating the setup it provides on secondary CPUs.
70e060f6edSPaul Burton  */
71e060f6edSPaul Burton extern void maar_init(void);
72e060f6edSPaul Burton 
73e060f6edSPaul Burton /**
74ab9988a3SPaul Burton  * struct maar_config - MAAR configuration data
75ab9988a3SPaul Burton  * @lower:	The lowest address that the MAAR pair will affect. Must be
76ab9988a3SPaul Burton  *		aligned to a 2^16 byte boundary.
77ab9988a3SPaul Burton  * @upper:	The highest address that the MAAR pair will affect. Must be
78ab9988a3SPaul Burton  *		aligned to one byte before a 2^16 byte boundary.
79ab9988a3SPaul Burton  * @attrs:	The accessibility attributes to program, eg. MIPS_MAAR_S. The
80f359a111SJames Hogan  *		MIPS_MAAR_VL attribute will automatically be set.
81ab9988a3SPaul Burton  *
82ab9988a3SPaul Burton  * Describes the configuration of a pair of Memory Accessibility Attribute
83ab9988a3SPaul Burton  * Registers - applying attributes from attrs to the range of physical
84ab9988a3SPaul Burton  * addresses from lower to upper inclusive.
85ab9988a3SPaul Burton  */
86ab9988a3SPaul Burton struct maar_config {
87ab9988a3SPaul Burton 	phys_addr_t lower;
88ab9988a3SPaul Burton 	phys_addr_t upper;
89ab9988a3SPaul Burton 	unsigned attrs;
90ab9988a3SPaul Burton };
91ab9988a3SPaul Burton 
92ab9988a3SPaul Burton /**
93ab9988a3SPaul Burton  * maar_config() - configure MAARs according to provided data
94ab9988a3SPaul Burton  * @cfg:	Pointer to an array of struct maar_config.
95ab9988a3SPaul Burton  * @num_cfg:	The number of structs in the cfg array.
96ab9988a3SPaul Burton  * @num_pairs:	The number of MAAR pairs present in the system.
97ab9988a3SPaul Burton  *
98ab9988a3SPaul Burton  * Configures as many MAARs as are present and specified in the cfg
99ab9988a3SPaul Burton  * array with the values taken from the cfg array.
100ab9988a3SPaul Burton  *
101ab9988a3SPaul Burton  * Return:	The number of MAAR pairs configured.
102ab9988a3SPaul Burton  */
103ab9988a3SPaul Burton static inline unsigned maar_config(const struct maar_config *cfg,
104ab9988a3SPaul Burton 				   unsigned num_cfg, unsigned num_pairs)
105ab9988a3SPaul Burton {
106ab9988a3SPaul Burton 	unsigned i;
107ab9988a3SPaul Burton 
108ab9988a3SPaul Burton 	for (i = 0; i < min(num_cfg, num_pairs); i++)
109ab9988a3SPaul Burton 		write_maar_pair(i, cfg[i].lower, cfg[i].upper, cfg[i].attrs);
110ab9988a3SPaul Burton 
111ab9988a3SPaul Burton 	return i;
112ab9988a3SPaul Burton }
113ab9988a3SPaul Burton 
114ab9988a3SPaul Burton #endif /* __MIPS_ASM_MIPS_MAAR_H__ */
115