1 // SPDX-License-Identifier: GPL-2.0+
8 #include <asm/arch/sci/sci.h>
9 #include <asm/arch/sys_proto.h>
10 #include <dm/ofnode.h>
11 #include <fdt_support.h>
12 #include <linux/libfdt.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 static bool check_owned_resource(sc_rsrc_t rsrc_id)
20 owned = sc_rm_is_resource_owned(-1, rsrc_id);
25 static int disable_fdt_node(void *blob, int nodeoffset)
28 const char *status = "disabled";
31 rc = fdt_setprop(blob, nodeoffset, "status", status,
34 if (rc == -FDT_ERR_NOSPACE) {
35 ret = fdt_increase_size(blob, 512);
40 } while (rc == -FDT_ERR_NOSPACE);
45 static void update_fdt_with_owned_resources(void *blob)
48 * Traverses the fdt nodes, check its power domain and use
49 * the resource id in the power domain for checking whether
50 * it is owned by current partition
52 struct fdtdec_phandle_args args;
53 int offset = 0, depth = 0;
57 for (offset = fdt_next_node(blob, offset, &depth); offset > 0;
58 offset = fdt_next_node(blob, offset, &depth)) {
59 debug("Node name: %s, depth %d\n",
60 fdt_get_name(blob, offset, NULL), depth);
62 if (!fdt_get_property(blob, offset, "power-domains", NULL)) {
63 debug(" - ignoring node %s\n",
64 fdt_get_name(blob, offset, NULL));
68 if (!fdtdec_get_is_enabled(blob, offset)) {
69 debug(" - ignoring node %s\n",
70 fdt_get_name(blob, offset, NULL));
76 rc = fdtdec_parse_phandle_with_args(blob, offset,
78 "#power-domain-cells",
83 printf("Parse power-domains of %s wrong: %d\n",
84 fdt_get_name(blob, offset, NULL), rc);
88 rsrc_id = args.args[0];
90 if (!check_owned_resource(rsrc_id)) {
91 rc = disable_fdt_node(blob, offset);
93 printf("Disable %s rsrc %u not owned\n",
94 fdt_get_name(blob, offset, NULL),
97 printf("Unable to disable %s, err=%s\n",
98 fdt_get_name(blob, offset, NULL),
106 static int config_smmu_resource_sid(int rsrc, int sid)
110 err = sc_rm_set_master_sid(-1, rsrc, sid);
111 debug("set_master_sid rsrc=%d sid=0x%x err=%d\n", rsrc, sid, err);
112 if (err != SC_ERR_NONE) {
113 if (!check_owned_resource(rsrc)) {
114 printf("%s rsrc[%d] not owned\n", __func__, rsrc);
117 pr_err("fail set_master_sid rsrc=%d sid=0x%x err=%d\n", rsrc, sid, err);
124 static int config_smmu_fdt_device_sid(void *blob, int device_offset, int sid)
126 const char *name = fdt_get_name(blob, device_offset, NULL);
127 struct fdtdec_phandle_args args;
133 prop = fdt_getprop(blob, device_offset, "fsl,sc_rsrc_id", &proplen);
137 debug("configure node %s sid 0x%x for %d resources\n",
138 name, sid, (int)(proplen / sizeof(fdt32_t)));
139 for (i = 0; i < proplen / sizeof(fdt32_t); ++i) {
140 ret = config_smmu_resource_sid(fdt32_to_cpu(prop[i]),
151 ret = fdtdec_parse_phandle_with_args(blob, device_offset,
153 "#power-domain-cells",
155 if (ret == -ENOENT) {
158 printf("Parse power-domains of node %s wrong: %d\n",
159 fdt_get_name(blob, device_offset, NULL), ret);
163 debug("configure node %s sid 0x%x rsrc=%d\n",
167 ret = config_smmu_resource_sid(rsrc, sid);
175 static int config_smmu_fdt(void *blob)
177 int offset, proplen, i, ret;
181 /* Legacy smmu bindings, still used by xen. */
182 offset = fdt_node_offset_by_compatible(blob, 0, "arm,mmu-500");
183 prop = fdt_getprop(blob, offset, "mmu-masters", &proplen);
184 if (offset > 0 && prop) {
185 debug("found legacy mmu-masters property\n");
187 for (i = 0; i < proplen / 8; ++i) {
188 u32 phandle = fdt32_to_cpu(prop[2 * i]);
189 int sid = fdt32_to_cpu(prop[2 * i + 1]);
192 device_offset = fdt_node_offset_by_phandle(blob,
194 if (device_offset < 0) {
195 pr_err("Not find device from mmu_masters: %d",
199 ret = config_smmu_fdt_device_sid(blob, device_offset,
205 /* Ignore new bindings if old bindings found, just like linux. */
209 /* Generic smmu bindings */
211 while ((offset = fdt_next_node(blob, offset, NULL)) > 0) {
212 name = fdt_get_name(blob, offset, NULL);
213 prop = fdt_getprop(blob, offset, "iommus", &proplen);
216 debug("node %s iommus proplen %d\n", name, proplen);
219 int sid = fdt32_to_cpu(prop[1]);
221 config_smmu_fdt_device_sid(blob, offset, sid);
222 } else if (proplen != 4) {
223 debug("node %s ignore unexpected iommus proplen=%d\n",
231 static int ft_add_optee_node(void *fdt, bd_t *bd)
233 const char *path, *subpath;
237 * No TEE space allocated indicating no TEE running, so no
238 * need to add optee node in dts
240 if (!boot_pointer[1])
243 offs = fdt_increase_size(fdt, 512);
245 printf("No Space for dtb\n");
250 offs = fdt_path_offset(fdt, path);
253 offs = fdt_path_offset(fdt, path);
256 printf("Could not find root node.\n");
260 subpath = "firmware";
261 offs = fdt_add_subnode(fdt, offs, subpath);
263 printf("Could not create %s node.\n", subpath);
269 offs = fdt_add_subnode(fdt, offs, subpath);
271 printf("Could not create %s node.\n", subpath);
275 fdt_setprop_string(fdt, offs, "compatible", "linaro,optee-tz");
276 fdt_setprop_string(fdt, offs, "method", "smc");
281 int ft_system_setup(void *blob, bd_t *bd)
286 if (CONFIG_BOOTAUX_RESERVED_MEM_BASE) {
287 off = fdt_add_mem_rsv(blob, CONFIG_BOOTAUX_RESERVED_MEM_BASE,
288 CONFIG_BOOTAUX_RESERVED_MEM_SIZE);
290 printf("Failed to reserve memory for bootaux: %s\n",
294 update_fdt_with_owned_resources(blob);
297 ret = config_smmu_fdt(blob);
302 return ft_add_optee_node(blob, bd);