1 /*
2  * Copyright (c) 2019-2020, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 #include <assert.h>
7 
8 #include <common/debug.h>
9 #include <common/fdt_wrappers.h>
10 #include <libfdt.h>
11 #include <plat/arm/common/fconf_sdei_getter.h>
12 
13 #define PRIVATE_EVENT_NUM(i) private_events[3 * (i)]
14 #define PRIVATE_EVENT_INTR(i) private_events[3 * (i) + 1]
15 #define PRIVATE_EVENT_FLAGS(i) private_events[3 * (i) + 2]
16 
17 #define SHARED_EVENT_NUM(i) shared_events[3 * (i)]
18 #define SHARED_EVENT_INTR(i) shared_events[3 * (i) + 1]
19 #define SHARED_EVENT_FLAGS(i) shared_events[3 * (i) + 2]
20 
21 struct sdei_dyn_config_t sdei_dyn_config;
22 
fconf_populate_sdei_dyn_config(uintptr_t config)23 int fconf_populate_sdei_dyn_config(uintptr_t config)
24 {
25 	uint32_t i;
26 	int node, err;
27 	uint32_t private_events[PLAT_SDEI_DP_EVENT_MAX_CNT * 3];
28 	uint32_t shared_events[PLAT_SDEI_DS_EVENT_MAX_CNT * 3];
29 
30 	const void *dtb = (void *)config;
31 
32 	/* Check that the node offset points to compatible property */
33 	node = fdt_node_offset_by_compatible(dtb, -1, "arm,sdei-1.0");
34 	if (node < 0) {
35 		ERROR("FCONF: Can't find 'arm,sdei-1.0' compatible node in dtb\n");
36 		return node;
37 	}
38 
39 	/* Read number of private mappings */
40 	err = fdt_read_uint32(dtb, node, "private_event_count",
41 				&sdei_dyn_config.private_ev_cnt);
42 	if (err < 0) {
43 		ERROR("FCONF: Read cell failed for 'private_event_count': %u\n",
44 				sdei_dyn_config.private_ev_cnt);
45 		return err;
46 	}
47 
48 	/* Check if the value is in range */
49 	if (sdei_dyn_config.private_ev_cnt > PLAT_SDEI_DP_EVENT_MAX_CNT) {
50 		ERROR("FCONF: Invalid value for 'private_event_count': %u\n",
51 				sdei_dyn_config.private_ev_cnt);
52 		return -1;
53 	}
54 
55 	/* Read private mappings */
56 	err = fdt_read_uint32_array(dtb, node, "private_events",
57 				sdei_dyn_config.private_ev_cnt * 3, private_events);
58 	if (err < 0) {
59 		ERROR("FCONF: Read cell failed for 'private_events': %d\n", err);
60 		return err;
61 	}
62 
63 	/* Move data to fconf struct */
64 	for (i = 0; i < sdei_dyn_config.private_ev_cnt; i++) {
65 		sdei_dyn_config.private_ev_nums[i]  = PRIVATE_EVENT_NUM(i);
66 		sdei_dyn_config.private_ev_intrs[i] = PRIVATE_EVENT_INTR(i);
67 		sdei_dyn_config.private_ev_flags[i] = PRIVATE_EVENT_FLAGS(i);
68 	}
69 
70 	/* Read number of shared mappings */
71 	err = fdt_read_uint32(dtb, node, "shared_event_count",
72 				&sdei_dyn_config.shared_ev_cnt);
73 	if (err < 0) {
74 		ERROR("FCONF: Read cell failed for 'shared_event_count'\n");
75 		return err;
76 	}
77 
78 	/* Check if the value is in range */
79 	if (sdei_dyn_config.shared_ev_cnt > PLAT_SDEI_DS_EVENT_MAX_CNT) {
80 		ERROR("FCONF: Invalid value for 'shared_event_count': %u\n",
81 				sdei_dyn_config.shared_ev_cnt);
82 		return -1;
83 	}
84 
85 	/* Read shared mappings */
86 	err = fdt_read_uint32_array(dtb, node, "shared_events",
87 				sdei_dyn_config.shared_ev_cnt * 3, shared_events);
88 	if (err < 0) {
89 		ERROR("FCONF: Read cell failed for 'shared_events': %d\n", err);
90 		return err;
91 	}
92 
93 	/* Move data to fconf struct */
94 	for (i = 0; i < sdei_dyn_config.shared_ev_cnt; i++) {
95 		sdei_dyn_config.shared_ev_nums[i]  = SHARED_EVENT_NUM(i);
96 		sdei_dyn_config.shared_ev_intrs[i] = SHARED_EVENT_INTR(i);
97 		sdei_dyn_config.shared_ev_flags[i] = SHARED_EVENT_FLAGS(i);
98 	}
99 
100 	return 0;
101 }
102 
103 FCONF_REGISTER_POPULATOR(HW_CONFIG, sdei, fconf_populate_sdei_dyn_config);
104