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