blob: 65c8ac1a7e20b862af318ec1e2047bddcc59b3b7 [file] [log] [blame]
Peng Fan9f779fa2019-08-26 08:11:56 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 NXP
4 */
5
6#include <common.h>
7#include <asm/arch/sci/sci.h>
Peng Fan01cacf92019-08-26 08:12:06 +00008#include <asm/arch/sys_proto.h>
Peng Fan9f779fa2019-08-26 08:11:56 +00009#include <dm/ofnode.h>
10#include <fdt_support.h>
11
12DECLARE_GLOBAL_DATA_PTR;
13
14static bool check_owned_resource(sc_rsrc_t rsrc_id)
15{
16 bool owned;
17
18 owned = sc_rm_is_resource_owned(-1, rsrc_id);
19
20 return owned;
21}
22
23static int disable_fdt_node(void *blob, int nodeoffset)
24{
25 int rc, ret;
26 const char *status = "disabled";
27
28 do {
29 rc = fdt_setprop(blob, nodeoffset, "status", status,
30 strlen(status) + 1);
31 if (rc) {
32 if (rc == -FDT_ERR_NOSPACE) {
33 ret = fdt_increase_size(blob, 512);
34 if (ret)
35 return ret;
36 }
37 }
38 } while (rc == -FDT_ERR_NOSPACE);
39
40 return rc;
41}
42
43static void update_fdt_with_owned_resources(void *blob)
44{
45 /*
46 * Traverses the fdt nodes, check its power domain and use
47 * the resource id in the power domain for checking whether
48 * it is owned by current partition
49 */
50 struct fdtdec_phandle_args args;
51 int offset = 0, depth = 0;
52 u32 rsrc_id;
53 int rc, i;
54
55 for (offset = fdt_next_node(blob, offset, &depth); offset > 0;
56 offset = fdt_next_node(blob, offset, &depth)) {
57 debug("Node name: %s, depth %d\n",
58 fdt_get_name(blob, offset, NULL), depth);
59
60 if (!fdt_get_property(blob, offset, "power-domains", NULL)) {
61 debug(" - ignoring node %s\n",
62 fdt_get_name(blob, offset, NULL));
63 continue;
64 }
65
66 if (!fdtdec_get_is_enabled(blob, offset)) {
67 debug(" - ignoring node %s\n",
68 fdt_get_name(blob, offset, NULL));
69 continue;
70 }
71
72 i = 0;
73 while (true) {
74 rc = fdtdec_parse_phandle_with_args(blob, offset,
75 "power-domains",
76 "#power-domain-cells",
77 0, i++, &args);
78 if (rc == -ENOENT) {
79 break;
80 } else if (rc) {
81 printf("Parse power-domains of %s wrong: %d\n",
82 fdt_get_name(blob, offset, NULL), rc);
83 continue;
84 }
85
86 rsrc_id = args.args[0];
87
88 if (!check_owned_resource(rsrc_id)) {
89 rc = disable_fdt_node(blob, offset);
90 if (!rc) {
91 printf("Disable %s rsrc %u not owned\n",
92 fdt_get_name(blob, offset, NULL),
93 rsrc_id);
94 } else {
95 printf("Unable to disable %s, err=%s\n",
96 fdt_get_name(blob, offset, NULL),
97 fdt_strerror(rc));
98 }
99 }
100 }
101 }
102}
103
Peng Fan01cacf92019-08-26 08:12:06 +0000104static int config_smmu_resource_sid(int rsrc, int sid)
105{
106 int err;
107
108 if (!check_owned_resource(rsrc)) {
109 printf("%s rsrc[%d] not owned\n", __func__, rsrc);
110 return -1;
111 }
112 err = sc_rm_set_master_sid(-1, rsrc, sid);
113 debug("set_master_sid rsrc=%d sid=0x%x err=%d\n", rsrc, sid, err);
114 if (err != SC_ERR_NONE) {
115 pr_err("fail set_master_sid rsrc=%d sid=0x%x err=%d\n", rsrc, sid, err);
116 return -EINVAL;
117 }
118
119 return 0;
120}
121
122static int config_smmu_fdt_device_sid(void *blob, int device_offset, int sid)
123{
124 const char *name = fdt_get_name(blob, device_offset, NULL);
125 struct fdtdec_phandle_args args;
126 int rsrc, ret;
127 int proplen;
128 const fdt32_t *prop;
129 int i;
130
131 prop = fdt_getprop(blob, device_offset, "fsl,sc_rsrc_id", &proplen);
132 if (prop) {
133 int i;
134
135 debug("configure node %s sid 0x%x for %d resources\n",
136 name, sid, (int)(proplen / sizeof(fdt32_t)));
137 for (i = 0; i < proplen / sizeof(fdt32_t); ++i) {
138 ret = config_smmu_resource_sid(fdt32_to_cpu(prop[i]),
139 sid);
140 if (ret)
141 return ret;
142 }
143
144 return 0;
145 }
146
147 i = 0;
148 while (true) {
149 ret = fdtdec_parse_phandle_with_args(blob, device_offset,
150 "power-domains",
151 "#power-domain-cells",
152 0, i++, &args);
153 if (ret == -ENOENT) {
154 break;
155 } else if (ret) {
156 printf("Parse power-domains of node %s wrong: %d\n",
157 fdt_get_name(blob, device_offset, NULL), ret);
158 continue;
159 }
160
161 debug("configure node %s sid 0x%x rsrc=%d\n",
162 name, sid, rsrc);
163 rsrc = args.args[0];
164
165 ret = config_smmu_resource_sid(rsrc, sid);
166 if (ret)
167 break;
168 }
169
170 return ret;
171}
172
173static int config_smmu_fdt(void *blob)
174{
175 int offset, proplen, i, ret;
176 const fdt32_t *prop;
177 const char *name;
178
179 /* Legacy smmu bindings, still used by xen. */
180 offset = fdt_node_offset_by_compatible(blob, 0, "arm,mmu-500");
181 prop = fdt_getprop(blob, offset, "mmu-masters", &proplen);
182 if (offset > 0 && prop) {
183 debug("found legacy mmu-masters property\n");
184
185 for (i = 0; i < proplen / 8; ++i) {
186 u32 phandle = fdt32_to_cpu(prop[2 * i]);
187 int sid = fdt32_to_cpu(prop[2 * i + 1]);
188 int device_offset;
189
190 device_offset = fdt_node_offset_by_phandle(blob,
191 phandle);
192 if (device_offset < 0) {
193 pr_err("Not find device from mmu_masters: %d",
194 device_offset);
195 continue;
196 }
197 ret = config_smmu_fdt_device_sid(blob, device_offset,
198 sid);
199 if (ret)
200 return ret;
201 }
202
203 /* Ignore new bindings if old bindings found, just like linux. */
204 return 0;
205 }
206
207 /* Generic smmu bindings */
208 offset = 0;
209 while ((offset = fdt_next_node(blob, offset, NULL)) > 0) {
210 name = fdt_get_name(blob, offset, NULL);
211 prop = fdt_getprop(blob, offset, "iommus", &proplen);
212 if (!prop)
213 continue;
214 debug("node %s iommus proplen %d\n", name, proplen);
215
216 if (proplen == 12) {
217 int sid = fdt32_to_cpu(prop[1]);
218
219 config_smmu_fdt_device_sid(blob, offset, sid);
220 } else if (proplen != 4) {
221 debug("node %s ignore unexpected iommus proplen=%d\n",
222 name, proplen);
223 }
224 }
225
226 return 0;
227}
228
Peng Fan94e4d022019-08-26 08:12:13 +0000229static int ft_add_optee_node(void *fdt, bd_t *bd)
230{
231 const char *path, *subpath;
232 int offs;
233
234 /*
235 * No TEE space allocated indicating no TEE running, so no
236 * need to add optee node in dts
237 */
238 if (!boot_pointer[1])
239 return 0;
240
241 offs = fdt_increase_size(fdt, 512);
242 if (offs) {
243 printf("No Space for dtb\n");
244 return 1;
245 }
246
247 path = "/firmware";
248 offs = fdt_path_offset(fdt, path);
249 if (offs < 0) {
250 path = "/";
251 offs = fdt_path_offset(fdt, path);
252
253 if (offs < 0) {
254 printf("Could not find root node.\n");
255 return offs;
256 }
257
258 subpath = "firmware";
259 offs = fdt_add_subnode(fdt, offs, subpath);
260 if (offs < 0) {
261 printf("Could not create %s node.\n", subpath);
262 return offs;
263 }
264 }
265
266 subpath = "optee";
267 offs = fdt_add_subnode(fdt, offs, subpath);
268 if (offs < 0) {
269 printf("Could not create %s node.\n", subpath);
270 return offs;
271 }
272
273 fdt_setprop_string(fdt, offs, "compatible", "linaro,optee-tz");
274 fdt_setprop_string(fdt, offs, "method", "smc");
275
276 return 0;
277}
278
Peng Fan9f779fa2019-08-26 08:11:56 +0000279int ft_system_setup(void *blob, bd_t *bd)
280{
Peng Fan01cacf92019-08-26 08:12:06 +0000281 int ret;
282
Peng Fan9f779fa2019-08-26 08:11:56 +0000283 update_fdt_with_owned_resources(blob);
284
Peng Fan01cacf92019-08-26 08:12:06 +0000285 if (is_imx8qm()) {
286 ret = config_smmu_fdt(blob);
287 if (ret)
288 return ret;
289 }
290
Peng Fan94e4d022019-08-26 08:12:13 +0000291 return ft_add_optee_node(blob, bd);
Peng Fan9f779fa2019-08-26 08:11:56 +0000292}