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