1 // SPDX-License-Identifier: GPL-2.0+
8 #include <asm/arch/sci/sci.h>
9 #include <asm/arch/sys_proto.h>
10 #include <asm/global_data.h>
11 #include <dm/ofnode.h>
12 #include <fdt_support.h>
13 #include <linux/libfdt.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 static bool check_owned_resource(sc_rsrc_t rsrc_id)
21 owned = sc_rm_is_resource_owned(-1, rsrc_id);
26 static int disable_fdt_node(void *blob, int nodeoffset)
29 const char *status = "disabled";
32 rc = fdt_setprop(blob, nodeoffset, "status", status,
35 if (rc == -FDT_ERR_NOSPACE) {
36 ret = fdt_increase_size(blob, 512);
41 } while (rc == -FDT_ERR_NOSPACE);
46 static void update_fdt_with_owned_resources(void *blob)
49 * Traverses the fdt nodes, check its power domain and use
50 * the resource id in the power domain for checking whether
51 * it is owned by current partition
53 struct fdtdec_phandle_args args;
54 int offset = 0, depth = 0;
58 for (offset = fdt_next_node(blob, offset, &depth); offset > 0;
59 offset = fdt_next_node(blob, offset, &depth)) {
60 debug("Node name: %s, depth %d\n",
61 fdt_get_name(blob, offset, NULL), depth);
63 if (!fdt_get_property(blob, offset, "power-domains", NULL)) {
64 debug(" - ignoring node %s\n",
65 fdt_get_name(blob, offset, NULL));
69 if (!fdtdec_get_is_enabled(blob, offset)) {
70 debug(" - ignoring node %s\n",
71 fdt_get_name(blob, offset, NULL));
77 rc = fdtdec_parse_phandle_with_args(blob, offset,
79 "#power-domain-cells",
84 printf("Parse power-domains of %s wrong: %d\n",
85 fdt_get_name(blob, offset, NULL), rc);
89 rsrc_id = args.args[0];
91 if (!check_owned_resource(rsrc_id)) {
92 rc = disable_fdt_node(blob, offset);
94 printf("Disable %s rsrc %u not owned\n",
95 fdt_get_name(blob, offset, NULL),
98 printf("Unable to disable %s, err=%s\n",
99 fdt_get_name(blob, offset, NULL),
107 static int config_smmu_resource_sid(int rsrc, int sid)
111 err = sc_rm_set_master_sid(-1, rsrc, sid);
112 debug("set_master_sid rsrc=%d sid=0x%x err=%d\n", rsrc, sid, err);
113 if (err != SC_ERR_NONE) {
114 if (!check_owned_resource(rsrc)) {
115 printf("%s rsrc[%d] not owned\n", __func__, rsrc);
118 pr_err("fail set_master_sid rsrc=%d sid=0x%x err=%d\n", rsrc, sid, err);
125 static int config_smmu_fdt_device_sid(void *blob, int device_offset, int sid)
127 const char *name = fdt_get_name(blob, device_offset, NULL);
128 struct fdtdec_phandle_args args;
134 prop = fdt_getprop(blob, device_offset, "fsl,sc_rsrc_id", &proplen);
138 debug("configure node %s sid 0x%x for %d resources\n",
139 name, sid, (int)(proplen / sizeof(fdt32_t)));
140 for (i = 0; i < proplen / sizeof(fdt32_t); ++i) {
141 ret = config_smmu_resource_sid(fdt32_to_cpu(prop[i]),
152 ret = fdtdec_parse_phandle_with_args(blob, device_offset,
154 "#power-domain-cells",
156 if (ret == -ENOENT) {
159 printf("Parse power-domains of node %s wrong: %d\n",
160 fdt_get_name(blob, device_offset, NULL), ret);
164 debug("configure node %s sid 0x%x rsrc=%d\n",
168 ret = config_smmu_resource_sid(rsrc, sid);
176 static int config_smmu_fdt(void *blob)
178 int offset, proplen, i, ret;
182 /* Legacy smmu bindings, still used by xen. */
183 offset = fdt_node_offset_by_compatible(blob, 0, "arm,mmu-500");
184 prop = fdt_getprop(blob, offset, "mmu-masters", &proplen);
185 if (offset > 0 && prop) {
186 debug("found legacy mmu-masters property\n");
188 for (i = 0; i < proplen / 8; ++i) {
189 u32 phandle = fdt32_to_cpu(prop[2 * i]);
190 int sid = fdt32_to_cpu(prop[2 * i + 1]);
193 device_offset = fdt_node_offset_by_phandle(blob,
195 if (device_offset < 0) {
196 pr_err("Not find device from mmu_masters: %d",
200 ret = config_smmu_fdt_device_sid(blob, device_offset,
206 /* Ignore new bindings if old bindings found, just like linux. */
210 /* Generic smmu bindings */
212 while ((offset = fdt_next_node(blob, offset, NULL)) > 0) {
213 name = fdt_get_name(blob, offset, NULL);
214 prop = fdt_getprop(blob, offset, "iommus", &proplen);
217 debug("node %s iommus proplen %d\n", name, proplen);
220 int sid = fdt32_to_cpu(prop[1]);
222 config_smmu_fdt_device_sid(blob, offset, sid);
223 } else if (proplen != 4) {
224 debug("node %s ignore unexpected iommus proplen=%d\n",
232 static int ft_add_optee_node(void *fdt, struct bd_info *bd)
234 const char *path, *subpath;
238 * No TEE space allocated indicating no TEE running, so no
239 * need to add optee node in dts
241 if (!boot_pointer[1])
244 offs = fdt_increase_size(fdt, 512);
246 printf("No Space for dtb\n");
251 offs = fdt_path_offset(fdt, path);
254 offs = fdt_path_offset(fdt, path);
257 printf("Could not find root node.\n");
261 subpath = "firmware";
262 offs = fdt_add_subnode(fdt, offs, subpath);
264 printf("Could not create %s node.\n", subpath);
270 offs = fdt_add_subnode(fdt, offs, subpath);
272 printf("Could not create %s node.\n", subpath);
276 fdt_setprop_string(fdt, offs, "compatible", "linaro,optee-tz");
277 fdt_setprop_string(fdt, offs, "method", "smc");
282 int ft_system_setup(void *blob, struct bd_info *bd)
287 if (CONFIG_BOOTAUX_RESERVED_MEM_BASE) {
288 off = fdt_add_mem_rsv(blob, CONFIG_BOOTAUX_RESERVED_MEM_BASE,
289 CONFIG_BOOTAUX_RESERVED_MEM_SIZE);
291 printf("Failed to reserve memory for bootaux: %s\n",
295 update_fdt_with_owned_resources(blob);
298 ret = config_smmu_fdt(blob);
303 return ft_add_optee_node(blob, bd);