xref: /rk3399_ARM-atf/drivers/arm/ccn/ccn.c (revision fd6007de64fd7e16f6d96972643434c04a77f1c6)
1 /*
2  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <arch.h>
32 #include <assert.h>
33 #include <bakery_lock.h>
34 #include <ccn.h>
35 #include <debug.h>
36 #include <errno.h>
37 #include <mmio.h>
38 #include "ccn_private.h"
39 
40 static const ccn_desc_t *ccn_plat_desc;
41 #if IMAGE_BL31
42 DEFINE_BAKERY_LOCK(ccn_lock);
43 #endif
44 
45 /*******************************************************************************
46  * This function takes the base address of the CCN's programmer's view (PV), a
47  * region ID of one of the 256 regions (0-255) and a register offset within the
48  * region. It converts the first two parameters into a base address and uses it
49  * to read the register at the offset.
50  ******************************************************************************/
51 static inline unsigned long long ccn_reg_read(uintptr_t periphbase,
52 			     unsigned int region_id,
53 			     unsigned int register_offset)
54 {
55 	uintptr_t region_base;
56 
57 	assert(periphbase);
58 	assert(region_id < REGION_ID_LIMIT);
59 
60 	region_base = periphbase + region_id_to_base(region_id);
61 	return mmio_read_64(region_base + register_offset);
62 }
63 
64 /*******************************************************************************
65  * This function takes the base address of the CCN's programmer's view (PV), a
66  * region ID of one of the 256 regions (0-255), a register offset within the
67  * region and a value. It converts the first two parameters into a base address
68  * and uses it to write the value in the register at the offset.
69  ******************************************************************************/
70 static inline void ccn_reg_write(uintptr_t periphbase,
71 			  unsigned int region_id,
72 			  unsigned int register_offset,
73 			  unsigned long long value)
74 {
75 	uintptr_t region_base;
76 
77 	assert(periphbase);
78 	assert(region_id < REGION_ID_LIMIT);
79 
80 	region_base = periphbase + region_id_to_base(region_id);
81 	mmio_write_64(region_base + register_offset, value);
82 }
83 
84 #if DEBUG
85 
86 typedef struct rn_info {
87 		unsigned char node_desc[MAX_RN_NODES];
88 	} rn_info_t;
89 
90 /*******************************************************************************
91  * This function takes the base address of the CCN's programmer's view (PV) and
92  * the node ID of a Request Node (RN-D or RN-I). It returns the maximum number
93  * of master interfaces resident on that node. This number is equal to the least
94  * significant two bits of the node type ID + 1.
95  ******************************************************************************/
96 static unsigned int ccn_get_rni_mcount(uintptr_t periphbase,
97 				       unsigned int rn_id)
98 {
99 	unsigned int rn_type_id;
100 
101 	/* Use the node id to find the type of RN-I/D node */
102 	rn_type_id = get_node_type(ccn_reg_read(periphbase,
103 						rn_id + RNI_REGION_ID_START,
104 						REGION_ID_OFFSET));
105 
106 	/* Return the number master interfaces based on node type */
107 	return rn_type_id_to_master_cnt(rn_type_id);
108 }
109 
110 /*******************************************************************************
111  * This function reads the CCN registers to find the following information about
112  * the ACE/ACELite/ACELite+DVM/CHI interfaces resident on the various types of
113  * Request Nodes (RN-Fs, RN-Is and RN-Ds) in the system:
114  *
115  * 1. The total number of such interfaces that this CCN IP supports. This is the
116  *    cumulative number of interfaces across all Request node types. It is
117  *    passed back as the return value of this function.
118  *
119  * 2. The maximum number of interfaces of a type resident on a Request node of
120  *    one of the three types. This information is populated in the 'info'
121  *    array provided by the caller as described next.
122  *
123  *    The array has 64 entries. Each entry corresponds to a Request node. The
124  *    Miscellaneous node's programmer's view has RN-F, RN-I and RN-D ID
125  *    registers. For each RN-I and RN-D ID indicated as being present in these
126  *    registers, its identification register (offset 0xFF00) is read. This
127  *    register specifies the maximum number of master interfaces the node
128  *    supports. For RN-Fs it is assumed that there can be only a single fully
129  *    coherent master resident on each node. The counts for each type of node
130  *    are use to populate the array entry at the index corresponding to the node
131  *    ID i.e. rn_info[node ID] = <number of master interfaces>
132  ******************************************************************************/
133 static unsigned int ccn_get_rn_master_info(uintptr_t periphbase,
134 					   rn_info_t *info)
135 {
136 	unsigned int num_masters = 0;
137 	rn_types_t rn_type;
138 
139 	assert (info);
140 
141 	for (rn_type = RN_TYPE_RNF; rn_type < NUM_RN_TYPES; rn_type++) {
142 		unsigned int mn_reg_off, node_id;
143 		unsigned long long rn_bitmap;
144 
145 		/*
146 		 * RN-F, RN-I, RN-D node registers in the MN region occupy
147 		 * contiguous 16 byte apart offsets.
148 		 */
149 		mn_reg_off = MN_RNF_NODEID_OFFSET + (rn_type << 4);
150 		rn_bitmap = ccn_reg_read(periphbase, MN_REGION_ID, mn_reg_off);
151 
152 		FOR_EACH_PRESENT_NODE_ID(node_id, rn_bitmap) {
153 			unsigned int node_mcount;
154 
155 			/*
156 			 * A RN-F does not have a node type since it does not
157 			 * export a programmer's interface. It can only have a
158 			 * single fully coherent master residing on it. If the
159 			 * offset of the MN(Miscellaneous Node) register points
160 			 * to a RN-I/D node then the master count is set to the
161 			 * maximum number of master interfaces that can possibly
162 			 * reside on the node.
163 			 */
164 			node_mcount = (mn_reg_off == MN_RNF_NODEID_OFFSET ? 1 :
165 				       ccn_get_rni_mcount(periphbase, node_id));
166 
167 			/*
168 			 * Use this value to increment the maximum possible
169 			 * master interfaces in the system.
170 			 */
171 			num_masters += node_mcount;
172 
173 			/*
174 			 * Update the entry in 'info' for this node ID with
175 			 * the maximum number of masters than can sit on
176 			 * it. This information will be used to validate the
177 			 * node information passed by the platform later.
178 			 */
179 			info->node_desc[node_id] = node_mcount;
180 		}
181 	}
182 
183 	return num_masters;
184 }
185 
186 /*******************************************************************************
187  * This function validates parameters passed by the platform (in a debug build).
188  * It collects information about the maximum number of master interfaces that:
189  * a) the CCN IP can accommodate and
190  * b) can exist on each Request node.
191  * It compares this with the information provided by the platform to determine
192  * the validity of the latter.
193  ******************************************************************************/
194 static void ccn_validate_plat_params(const ccn_desc_t *plat_desc)
195 {
196 	unsigned int master_id, num_rn_masters;
197 	rn_info_t info = { {0} };
198 
199 	assert(plat_desc);
200 	assert(plat_desc->periphbase);
201 	assert(plat_desc->master_to_rn_id_map);
202 	assert(plat_desc->num_masters);
203 	assert(plat_desc->num_masters < CCN_MAX_RN_MASTERS);
204 
205 	/*
206 	 * Find the number and properties of fully coherent, IO coherent and IO
207 	 * coherent + DVM master interfaces
208 	 */
209 	num_rn_masters = ccn_get_rn_master_info(plat_desc->periphbase, &info);
210 	assert(plat_desc->num_masters < num_rn_masters);
211 
212 	/*
213 	 * Iterate through the Request nodes specified by the platform.
214 	 * Decrement the count of the masters in the 'info' array for each
215 	 * Request node encountered. If the count would drop below 0 then the
216 	 * platform's view of this aspect of CCN configuration is incorrect.
217 	 */
218 	for (master_id = 0; master_id < plat_desc->num_masters; master_id++) {
219 		unsigned int node_id;
220 
221 		node_id = plat_desc->master_to_rn_id_map[master_id];
222 		assert(node_id < MAX_RN_NODES);
223 		assert(info.node_desc[node_id]);
224 		info.node_desc[node_id]--;
225 	}
226 }
227 #endif /* DEBUG */
228 
229 /*******************************************************************************
230  * This function validates parameters passed by the platform (in a debug build)
231  * and initialises its internal data structures. A lock is required to prevent
232  * simultaneous CCN operations at runtime (only BL31) to add and remove Request
233  * nodes from coherency.
234  ******************************************************************************/
235 void ccn_init(const ccn_desc_t *plat_desc)
236 {
237 #if DEBUG
238 	ccn_validate_plat_params(plat_desc);
239 #endif
240 
241 	ccn_plat_desc = plat_desc;
242 }
243 
244 /*******************************************************************************
245  * This function converts a bit map of master interface IDs to a bit map of the
246  * Request node IDs that they reside on.
247  ******************************************************************************/
248 static unsigned long long ccn_master_to_rn_id_map(unsigned long long master_map)
249 {
250 	unsigned long long rn_id_map = 0;
251 	unsigned int node_id, iface_id;
252 
253 	assert(master_map);
254 	assert(ccn_plat_desc);
255 
256 	FOR_EACH_PRESENT_MASTER_INTERFACE(iface_id, master_map) {
257 
258 		/* Convert the master ID into the node ID */
259 		node_id = ccn_plat_desc->master_to_rn_id_map[iface_id];
260 
261 		/* Set the bit corresponding to this node ID */
262 		rn_id_map |= (1UL << node_id);
263 	}
264 
265 	return rn_id_map;
266 }
267 
268 /*******************************************************************************
269  * This function executes the necessary operations to add or remove Request node
270  * IDs specified in the 'rn_id_map' bitmap from the snoop/DVM domains specified
271  * in the 'hn_id_map'. The 'region_id' specifies the ID of the first HN-F/HN-I
272  * on which the operation should be performed. 'op_reg_offset' specifies the
273  * type of operation (add/remove). 'stat_reg_offset' specifies the register
274  * which should be polled to determine if the operation has completed or not.
275  ******************************************************************************/
276 static void ccn_snoop_dvm_do_op(unsigned long long rn_id_map,
277 				unsigned long long hn_id_map,
278 				unsigned int region_id,
279 				unsigned int op_reg_offset,
280 				unsigned int stat_reg_offset)
281 {
282 	unsigned int start_region_id;
283 
284 	assert(ccn_plat_desc);
285 	assert(ccn_plat_desc->periphbase);
286 
287 #if IMAGE_BL31
288 	bakery_lock_get(&ccn_lock);
289 #endif
290 	start_region_id = region_id;
291 	FOR_EACH_PRESENT_REGION_ID(start_region_id, hn_id_map) {
292 		ccn_reg_write(ccn_plat_desc->periphbase,
293 			      start_region_id,
294 			      op_reg_offset,
295 			      rn_id_map);
296 	}
297 
298 	start_region_id = region_id;
299 
300 	FOR_EACH_PRESENT_REGION_ID(start_region_id, hn_id_map) {
301 		WAIT_FOR_DOMAIN_CTRL_OP_COMPLETION(start_region_id,
302 						   stat_reg_offset,
303 						   op_reg_offset,
304 						   rn_id_map);
305 	}
306 
307 #if IMAGE_BL31
308 	bakery_lock_release(&ccn_lock);
309 #endif
310 }
311 
312 /*******************************************************************************
313  * This function reads the bitmap of Home nodes on the basis of the
314  * 'mn_hn_id_reg_offset' parameter from the Miscellaneous node's (MN)
315  * programmer's view. The MN has a register which carries the bitmap of present
316  * Home nodes of each type i.e. HN-Fs, HN-Is & HN-Ds. It calls
317  * 'ccn_snoop_dvm_do_op()' with this information to perform the actual
318  * operation.
319  ******************************************************************************/
320 static void ccn_snoop_dvm_domain_common(unsigned long long rn_id_map,
321 					unsigned int hn_op_reg_offset,
322 					unsigned int hn_stat_reg_offset,
323 					unsigned int mn_hn_id_reg_offset,
324 					unsigned int hn_region_id)
325 {
326 	unsigned long long mn_hn_id_map;
327 
328 	assert(ccn_plat_desc);
329 	assert(ccn_plat_desc->periphbase);
330 
331 	mn_hn_id_map = ccn_reg_read(ccn_plat_desc->periphbase,
332 				    MN_REGION_ID,
333 				    mn_hn_id_reg_offset);
334 	ccn_snoop_dvm_do_op(rn_id_map,
335 			    mn_hn_id_map,
336 			    hn_region_id,
337 			    hn_op_reg_offset,
338 			    hn_stat_reg_offset);
339 }
340 
341 /*******************************************************************************
342  * The following functions provide the boot and runtime API to the platform for
343  * adding and removing master interfaces from the snoop/DVM domains. A bitmap of
344  * master interfaces IDs is passed as a parameter. It is converted into a bitmap
345  * of Request node IDs using the mapping provided by the platform while
346  * initialising the driver.
347  * For example, consider a dual cluster system where the clusters have values 0
348  * & 1 in the affinity level 1 field of their respective MPIDRs. While
349  * initialising this driver, the platform provides the mapping between each
350  * cluster and the corresponding Request node. To add or remove a cluster from
351  * the snoop and dvm domain, the bit position corresponding to the cluster ID
352  * should be set in the 'master_iface_map' i.e. to remove both clusters the
353  * bitmap would equal 0x11.
354  ******************************************************************************/
355 void ccn_enter_snoop_dvm_domain(unsigned long long master_iface_map)
356 {
357 	unsigned long long rn_id_map;
358 
359 	rn_id_map = ccn_master_to_rn_id_map(master_iface_map);
360 	ccn_snoop_dvm_domain_common(rn_id_map,
361 				    HNF_SDC_SET_OFFSET,
362 				    HNF_SDC_STAT_OFFSET,
363 				    MN_HNF_NODEID_OFFSET,
364 				    HNF_REGION_ID_START);
365 
366 	ccn_snoop_dvm_domain_common(rn_id_map,
367 				    MN_DDC_SET_OFF,
368 				    MN_DDC_STAT_OFFSET,
369 				    MN_HNI_NODEID_OFFSET,
370 				    MN_REGION_ID);
371 }
372 
373 void ccn_exit_snoop_dvm_domain(unsigned long long master_iface_map)
374 {
375 	unsigned long long rn_id_map;
376 
377 	rn_id_map = ccn_master_to_rn_id_map(master_iface_map);
378 	ccn_snoop_dvm_domain_common(rn_id_map,
379 				    HNF_SDC_CLR_OFFSET,
380 				    HNF_SDC_STAT_OFFSET,
381 				    MN_HNF_NODEID_OFFSET,
382 				    HNF_REGION_ID_START);
383 
384 	ccn_snoop_dvm_domain_common(rn_id_map,
385 				    MN_DDC_CLR_OFFSET,
386 				    MN_DDC_STAT_OFFSET,
387 				    MN_HNI_NODEID_OFFSET,
388 				    MN_REGION_ID);
389 }
390 
391 void ccn_enter_dvm_domain(unsigned long long master_iface_map)
392 {
393 	unsigned long long rn_id_map;
394 
395 	rn_id_map = ccn_master_to_rn_id_map(master_iface_map);
396 	ccn_snoop_dvm_domain_common(rn_id_map,
397 				    MN_DDC_SET_OFF,
398 				    MN_DDC_STAT_OFFSET,
399 				    MN_HNI_NODEID_OFFSET,
400 				    MN_REGION_ID);
401 }
402 
403 void ccn_exit_dvm_domain(unsigned long long master_iface_map)
404 {
405 	unsigned long long rn_id_map;
406 
407 	rn_id_map = ccn_master_to_rn_id_map(master_iface_map);
408 	ccn_snoop_dvm_domain_common(rn_id_map,
409 				    MN_DDC_CLR_OFFSET,
410 				    MN_DDC_STAT_OFFSET,
411 				    MN_HNI_NODEID_OFFSET,
412 				    MN_REGION_ID);
413 }
414 
415 /*******************************************************************************
416  * This function returns the run mode of all the L3 cache partitions in the
417  * system. The state is expected to be one of NO_L3, SF_ONLY, L3_HAM or
418  * L3_FAM. Instead of comparing the states reported by all HN-Fs, the state of
419  * the first present HN-F node is reported. Since the driver does not export an
420  * interface to program them seperately, there is no reason to perform this
421  * check. An HN-F could report that the L3 cache is transitioning from one mode
422  * to another e.g. HNF_PM_NOL3_2_SFONLY. In this case, the function waits for
423  * the transition to complete and reports the final state.
424  ******************************************************************************/
425 unsigned int ccn_get_l3_run_mode(void)
426 {
427 	unsigned long long hnf_pstate_stat;
428 
429 	assert(ccn_plat_desc);
430 	assert(ccn_plat_desc->periphbase);
431 
432 	/*
433 	 * Wait for a L3 cache paritition to enter any run mode. The pstate
434 	 * parameter is read from an HN-F P-state status register. A non-zero
435 	 * value in bits[1:0] means that the cache is transitioning to a run
436 	 * mode.
437 	 */
438 	do {
439 		hnf_pstate_stat = ccn_reg_read(ccn_plat_desc->periphbase,
440 					       HNF_REGION_ID_START,
441 					       HNF_PSTATE_STAT_OFFSET);
442 	} while (hnf_pstate_stat & 0x3);
443 
444 	return PSTATE_TO_RUN_MODE(hnf_pstate_stat);
445 }
446 
447 /*******************************************************************************
448  * This function sets the run mode of all the L3 cache partitions in the
449  * system to one of NO_L3, SF_ONLY, L3_HAM or L3_FAM depending upon the state
450  * specified by the 'mode' argument.
451  ******************************************************************************/
452 void ccn_set_l3_run_mode(unsigned int mode)
453 {
454 	unsigned long long mn_hnf_id_map, hnf_pstate_stat;
455 	unsigned int region_id;
456 
457 	assert(ccn_plat_desc);
458 	assert(ccn_plat_desc->periphbase);
459 	assert(mode <= CCN_L3_RUN_MODE_FAM);
460 
461 	mn_hnf_id_map = ccn_reg_read(ccn_plat_desc->periphbase,
462 				     MN_REGION_ID,
463 				     MN_HNF_NODEID_OFFSET);
464 	region_id = HNF_REGION_ID_START;
465 
466 	/* Program the desired run mode */
467 	FOR_EACH_PRESENT_REGION_ID(region_id, mn_hnf_id_map) {
468 		ccn_reg_write(ccn_plat_desc->periphbase,
469 			      region_id,
470 			      HNF_PSTATE_REQ_OFFSET,
471 			      mode);
472 	}
473 
474 	/* Wait for the caches to transition to the run mode */
475 	region_id = HNF_REGION_ID_START;
476 	FOR_EACH_PRESENT_REGION_ID(region_id, mn_hnf_id_map) {
477 		/*
478 		 * Wait for a L3 cache paritition to enter a target run
479 		 * mode. The pstate parameter is read from an HN-F P-state
480 		 * status register.
481 		 */
482 		do {
483 			hnf_pstate_stat = ccn_reg_read(ccn_plat_desc->periphbase,
484 					       region_id,
485 					       HNF_PSTATE_STAT_OFFSET);
486 		} while (((hnf_pstate_stat & HNF_PSTATE_MASK) >> 2) != mode);
487 	}
488 }
489 
490 /*******************************************************************************
491  * This function configures system address map and provides option to enable the
492  * 3SN striping mode of Slave node operation. The Slave node IDs and the Top
493  * Address bit1 and bit0 are provided as parameters to this function. This
494  * configuration is needed only if network contains a single SN-F or 3 SN-F and
495  * must be completed before the first request by the system to normal memory.
496  ******************************************************************************/
497 void ccn_program_sys_addrmap(unsigned int sn0_id,
498 		 unsigned int sn1_id,
499 		 unsigned int sn2_id,
500 		 unsigned int top_addr_bit0,
501 		 unsigned int top_addr_bit1,
502 		 unsigned char three_sn_en)
503 {
504 	unsigned long long mn_hnf_id_map, hnf_sam_ctrl_value;
505 	unsigned int region_id;
506 
507 	assert(ccn_plat_desc);
508 	assert(ccn_plat_desc->periphbase);
509 
510 	mn_hnf_id_map = ccn_reg_read(ccn_plat_desc->periphbase,
511 				     MN_REGION_ID,
512 				     MN_HNF_NODEID_OFFSET);
513 	region_id = HNF_REGION_ID_START;
514 	hnf_sam_ctrl_value = MAKE_HNF_SAM_CTRL_VALUE(sn0_id,
515 						     sn1_id,
516 						     sn2_id,
517 						     top_addr_bit0,
518 						     top_addr_bit1,
519 						     three_sn_en);
520 
521 	FOR_EACH_PRESENT_REGION_ID(region_id, mn_hnf_id_map) {
522 
523 		/* Program the SAM control register */
524 		ccn_reg_write(ccn_plat_desc->periphbase,
525 			      region_id,
526 			      HNF_SAM_CTRL_OFFSET,
527 			      hnf_sam_ctrl_value);
528 	}
529 
530 }
531