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
359ee195fdSSerge Semin * MIPS_MAAR_VL/MIPS_MAAR_VH attributes 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 */
write_maar_pair(unsigned idx,phys_addr_t lower,phys_addr_t upper,unsigned attrs)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
519ee195fdSSerge Semin /*
529ee195fdSSerge Semin * Write the upper address & attributes (both MIPS_MAAR_VL and
539ee195fdSSerge Semin * MIPS_MAAR_VH matter)
549ee195fdSSerge Semin */
55ab9988a3SPaul Burton write_c0_maari(idx << 1);
56ab9988a3SPaul Burton back_to_back_c0_hazard();
57ab9988a3SPaul Burton write_c0_maar(((upper >> 4) & MIPS_MAAR_ADDR) | attrs);
58ab9988a3SPaul Burton back_to_back_c0_hazard();
599ee195fdSSerge Semin #ifdef CONFIG_XPA
609ee195fdSSerge Semin upper >>= MIPS_MAARX_ADDR_SHIFT;
619ee195fdSSerge Semin writex_c0_maar(((upper >> 4) & MIPS_MAARX_ADDR) | MIPS_MAARX_VH);
629ee195fdSSerge Semin back_to_back_c0_hazard();
639ee195fdSSerge Semin #endif
64ab9988a3SPaul Burton
65ab9988a3SPaul Burton /* Write the lower address & attributes */
66ab9988a3SPaul Burton write_c0_maari((idx << 1) | 0x1);
67ab9988a3SPaul Burton back_to_back_c0_hazard();
68ab9988a3SPaul Burton write_c0_maar((lower >> 4) | attrs);
69ab9988a3SPaul Burton back_to_back_c0_hazard();
709ee195fdSSerge Semin #ifdef CONFIG_XPA
719ee195fdSSerge Semin lower >>= MIPS_MAARX_ADDR_SHIFT;
729ee195fdSSerge Semin writex_c0_maar(((lower >> 4) & MIPS_MAARX_ADDR) | MIPS_MAARX_VH);
739ee195fdSSerge Semin back_to_back_c0_hazard();
749ee195fdSSerge Semin #endif
75ab9988a3SPaul Burton }
76ab9988a3SPaul Burton
77ab9988a3SPaul Burton /**
78e060f6edSPaul Burton * maar_init() - initialise MAARs
79e060f6edSPaul Burton *
80e060f6edSPaul Burton * Performs initialisation of MAARs for the current CPU, making use of the
81e060f6edSPaul Burton * platforms implementation of platform_maar_init where necessary and
82e060f6edSPaul Burton * duplicating the setup it provides on secondary CPUs.
83e060f6edSPaul Burton */
84e060f6edSPaul Burton extern void maar_init(void);
85e060f6edSPaul Burton
86e060f6edSPaul Burton /**
87ab9988a3SPaul Burton * struct maar_config - MAAR configuration data
88ab9988a3SPaul Burton * @lower: The lowest address that the MAAR pair will affect. Must be
89ab9988a3SPaul Burton * aligned to a 2^16 byte boundary.
90ab9988a3SPaul Burton * @upper: The highest address that the MAAR pair will affect. Must be
91ab9988a3SPaul Burton * aligned to one byte before a 2^16 byte boundary.
92ab9988a3SPaul Burton * @attrs: The accessibility attributes to program, eg. MIPS_MAAR_S. The
93f359a111SJames Hogan * MIPS_MAAR_VL attribute will automatically be set.
94ab9988a3SPaul Burton *
95ab9988a3SPaul Burton * Describes the configuration of a pair of Memory Accessibility Attribute
96ab9988a3SPaul Burton * Registers - applying attributes from attrs to the range of physical
97ab9988a3SPaul Burton * addresses from lower to upper inclusive.
98ab9988a3SPaul Burton */
99ab9988a3SPaul Burton struct maar_config {
100ab9988a3SPaul Burton phys_addr_t lower;
101ab9988a3SPaul Burton phys_addr_t upper;
102ab9988a3SPaul Burton unsigned attrs;
103ab9988a3SPaul Burton };
104ab9988a3SPaul Burton
105ab9988a3SPaul Burton /**
106ab9988a3SPaul Burton * maar_config() - configure MAARs according to provided data
107ab9988a3SPaul Burton * @cfg: Pointer to an array of struct maar_config.
108ab9988a3SPaul Burton * @num_cfg: The number of structs in the cfg array.
109ab9988a3SPaul Burton * @num_pairs: The number of MAAR pairs present in the system.
110ab9988a3SPaul Burton *
111ab9988a3SPaul Burton * Configures as many MAARs as are present and specified in the cfg
112ab9988a3SPaul Burton * array with the values taken from the cfg array.
113ab9988a3SPaul Burton *
114ab9988a3SPaul Burton * Return: The number of MAAR pairs configured.
115ab9988a3SPaul Burton */
maar_config(const struct maar_config * cfg,unsigned num_cfg,unsigned num_pairs)116ab9988a3SPaul Burton static inline unsigned maar_config(const struct maar_config *cfg,
117ab9988a3SPaul Burton unsigned num_cfg, unsigned num_pairs)
118ab9988a3SPaul Burton {
119ab9988a3SPaul Burton unsigned i;
120ab9988a3SPaul Burton
121ab9988a3SPaul Burton for (i = 0; i < min(num_cfg, num_pairs); i++)
122ab9988a3SPaul Burton write_maar_pair(i, cfg[i].lower, cfg[i].upper, cfg[i].attrs);
123ab9988a3SPaul Burton
124ab9988a3SPaul Burton return i;
125ab9988a3SPaul Burton }
126ab9988a3SPaul Burton
127ab9988a3SPaul Burton #endif /* __MIPS_ASM_MIPS_MAAR_H__ */
128