1 // SPDX-License-Identifier: GPL-2.0+
7 #include <asm/arch/sci/sci.h>
8 #include <asm/arch/sys_proto.h>
10 #include <fdt_support.h>
12 DECLARE_GLOBAL_DATA_PTR;
14 static bool check_owned_resource(sc_rsrc_t rsrc_id)
18 owned = sc_rm_is_resource_owned(-1, rsrc_id);
23 static int disable_fdt_node(void *blob, int nodeoffset)
26 const char *status = "disabled";
29 rc = fdt_setprop(blob, nodeoffset, "status", status,
32 if (rc == -FDT_ERR_NOSPACE) {
33 ret = fdt_increase_size(blob, 512);
38 } while (rc == -FDT_ERR_NOSPACE);
43 static void update_fdt_with_owned_resources(void *blob)
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
50 struct fdtdec_phandle_args args;
51 int offset = 0, depth = 0;
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);
60 if (!fdt_get_property(blob, offset, "power-domains", NULL)) {
61 debug(" - ignoring node %s\n",
62 fdt_get_name(blob, offset, NULL));
66 if (!fdtdec_get_is_enabled(blob, offset)) {
67 debug(" - ignoring node %s\n",
68 fdt_get_name(blob, offset, NULL));
74 rc = fdtdec_parse_phandle_with_args(blob, offset,
76 "#power-domain-cells",
81 printf("Parse power-domains of %s wrong: %d\n",
82 fdt_get_name(blob, offset, NULL), rc);
86 rsrc_id = args.args[0];
88 if (!check_owned_resource(rsrc_id)) {
89 rc = disable_fdt_node(blob, offset);
91 printf("Disable %s rsrc %u not owned\n",
92 fdt_get_name(blob, offset, NULL),
95 printf("Unable to disable %s, err=%s\n",
96 fdt_get_name(blob, offset, NULL),
104 static int config_smmu_resource_sid(int rsrc, int sid)
108 if (!check_owned_resource(rsrc)) {
109 printf("%s rsrc[%d] not owned\n", __func__, rsrc);
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);
122 static int config_smmu_fdt_device_sid(void *blob, int device_offset, int sid)
124 const char *name = fdt_get_name(blob, device_offset, NULL);
125 struct fdtdec_phandle_args args;
131 prop = fdt_getprop(blob, device_offset, "fsl,sc_rsrc_id", &proplen);
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]),
149 ret = fdtdec_parse_phandle_with_args(blob, device_offset,
151 "#power-domain-cells",
153 if (ret == -ENOENT) {
156 printf("Parse power-domains of node %s wrong: %d\n",
157 fdt_get_name(blob, device_offset, NULL), ret);
161 debug("configure node %s sid 0x%x rsrc=%d\n",
165 ret = config_smmu_resource_sid(rsrc, sid);
173 static int config_smmu_fdt(void *blob)
175 int offset, proplen, i, ret;
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");
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]);
190 device_offset = fdt_node_offset_by_phandle(blob,
192 if (device_offset < 0) {
193 pr_err("Not find device from mmu_masters: %d",
197 ret = config_smmu_fdt_device_sid(blob, device_offset,
203 /* Ignore new bindings if old bindings found, just like linux. */
207 /* Generic smmu bindings */
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);
214 debug("node %s iommus proplen %d\n", name, proplen);
217 int sid = fdt32_to_cpu(prop[1]);
219 config_smmu_fdt_device_sid(blob, offset, sid);
220 } else if (proplen != 4) {
221 debug("node %s ignore unexpected iommus proplen=%d\n",
229 static int ft_add_optee_node(void *fdt, bd_t *bd)
231 const char *path, *subpath;
235 * No TEE space allocated indicating no TEE running, so no
236 * need to add optee node in dts
238 if (!boot_pointer[1])
241 offs = fdt_increase_size(fdt, 512);
243 printf("No Space for dtb\n");
248 offs = fdt_path_offset(fdt, path);
251 offs = fdt_path_offset(fdt, path);
254 printf("Could not find root node.\n");
258 subpath = "firmware";
259 offs = fdt_add_subnode(fdt, offs, subpath);
261 printf("Could not create %s node.\n", subpath);
267 offs = fdt_add_subnode(fdt, offs, subpath);
269 printf("Could not create %s node.\n", subpath);
273 fdt_setprop_string(fdt, offs, "compatible", "linaro,optee-tz");
274 fdt_setprop_string(fdt, offs, "method", "smc");
279 int ft_system_setup(void *blob, bd_t *bd)
283 update_fdt_with_owned_resources(blob);
286 ret = config_smmu_fdt(blob);
291 return ft_add_optee_node(blob, bd);