Commit 07be6f855bdcbca200e4685ea557b2529187e29f

Authored by Silvano di Ninno
Committed by Ye Li
1 parent 47a04423cb

TEE-502 imx: refactor optee bindings addition

- Remove code duplication betwee imx8 and imx8m support
- add reserved memory node to prevent Linux accessing optee reserved memory

Signed-off-by: Silvano di Ninno <silvano.dininno@nxp.com>
Reviewed-by: Ye Li <ye.li@nxp.com>
(cherry picked from commit 8beac7ef22c16b72ad337b44a0516436a4a0d00c)

Showing 5 changed files with 172 additions and 98 deletions Side-by-side Diff

arch/arm/include/asm/mach-imx/optee.h
  1 +/* SPDX-License-Identifier: GPL-2.0+ */
  2 +/*
  3 + * Copyright 2020 NXP
  4 + */
  5 +#ifndef __IMX_OPTEE_H__
  6 +#define __IMX_OPTEE_H__
  7 +
  8 +#include <common.h>
  9 +
  10 +#define OPTEE_SHM_SIZE 0x00400000
  11 +int ft_add_optee_node(void *fdt, bd_t *bd);
  12 +#endif
arch/arm/mach-imx/Makefile
... ... @@ -33,6 +33,9 @@
33 33 obj-$(CONFIG_CMD_PRIBLOB) += priblob.o
34 34 obj-$(CONFIG_SPL_BUILD) += spl.o
35 35 endif
  36 +ifeq ($(SOC),$(filter $(SOC),imx8m imx8))
  37 +obj-y += dt_optee.o
  38 +endif
36 39 ifeq ($(SOC),$(filter $(SOC),mx7))
37 40 obj-y += cpu.o
38 41 obj-$(CONFIG_SYS_I2C_MXC) += i2c-mxv7.o
arch/arm/mach-imx/dt_optee.c
  1 +// SPDX-License-Identifier: GPL-2.0+
  2 +/*
  3 + * Copyright 2020 NXP
  4 + */
  5 +#include <common.h>
  6 +#include <asm/arch/sys_proto.h>
  7 +#include <asm/mach-imx/optee.h>
  8 +#include <errno.h>
  9 +#include <fdt_support.h>
  10 +
  11 +#ifdef CONFIG_OF_SYSTEM_SETUP
  12 +static void set_dt_val(void *data, uint32_t cell_size, uint64_t val)
  13 +{
  14 + if (cell_size == 1) {
  15 + fdt32_t v = cpu_to_fdt32((uint32_t)val);
  16 +
  17 + memcpy(data, &v, sizeof(v));
  18 + } else {
  19 + fdt64_t v = cpu_to_fdt64(val);
  20 +
  21 + memcpy(data, &v, sizeof(v));
  22 + }
  23 +}
  24 +
  25 +static int add_dt_path_subnode(void *fdt, const char *path, const char *subnode)
  26 +{
  27 + int offs;
  28 +
  29 + offs = fdt_path_offset(fdt, path);
  30 + if (offs < 0)
  31 + return -1;
  32 +
  33 + offs = fdt_add_subnode(fdt, offs, subnode);
  34 + if (offs < 0)
  35 + return -1;
  36 + return offs;
  37 +}
  38 +
  39 +static int add_res_mem_dt_node(void *fdt, const char *name, phys_addr_t pa,
  40 + size_t size)
  41 +{
  42 + int offs = 0;
  43 + int ret = 0;
  44 + int addr_size = -1;
  45 + int len_size = -1;
  46 + bool found = true;
  47 + char subnode_name[80] = { 0 };
  48 +
  49 + offs = fdt_path_offset(fdt, "/reserved-memory");
  50 +
  51 + if (offs < 0) {
  52 + found = false;
  53 + offs = 0;
  54 + }
  55 +
  56 + len_size = fdt_size_cells(fdt, offs);
  57 + if (len_size < 0)
  58 + return -1;
  59 + addr_size = fdt_address_cells(fdt, offs);
  60 + if (addr_size < 0)
  61 + return -1;
  62 +
  63 + if (!found) {
  64 + offs = add_dt_path_subnode(fdt, "/", "reserved-memory");
  65 + if (offs < 0)
  66 + return -1;
  67 +
  68 + ret = fdt_setprop_cell(fdt, offs, "#address-cells", addr_size);
  69 + if (ret < 0)
  70 + return -1;
  71 + ret = fdt_setprop_cell(fdt, offs, "#size-cells", len_size);
  72 + if (ret < 0)
  73 + return -1;
  74 + ret = fdt_setprop(fdt, offs, "ranges", NULL, 0);
  75 + if (ret < 0)
  76 + return -1;
  77 + }
  78 +
  79 + snprintf(subnode_name, sizeof(subnode_name), "%s@0x%llx", name, pa);
  80 + offs = fdt_add_subnode(fdt, offs, subnode_name);
  81 + if (offs >= 0) {
  82 + u32 data[FDT_MAX_NCELLS * 2];
  83 +
  84 + set_dt_val(data, addr_size, pa);
  85 + set_dt_val(data + addr_size, len_size, size);
  86 + ret = fdt_setprop(fdt, offs, "reg", data,
  87 + sizeof(uint32_t) * (addr_size + len_size));
  88 + if (ret < 0)
  89 + return -1;
  90 + ret = fdt_setprop(fdt, offs, "no-map", NULL, 0);
  91 + if (ret < 0)
  92 + return -1;
  93 + } else {
  94 + return -1;
  95 + }
  96 + return 0;
  97 +}
  98 +
  99 +int ft_add_optee_node(void *fdt, bd_t *bd)
  100 +{
  101 + const char *path, *subpath;
  102 + int ret = 0;
  103 + int offs;
  104 + phys_addr_t optee_start;
  105 + size_t optee_size;
  106 +
  107 + /*
  108 + * No TEE space allocated indicating no TEE running, so no
  109 + * need to add optee node in dts
  110 + */
  111 + if (!rom_pointer[1])
  112 + return 0;
  113 +
  114 + optee_start = (phys_addr_t)rom_pointer[0];
  115 + optee_size = rom_pointer[1] - OPTEE_SHM_SIZE;
  116 +
  117 + offs = fdt_increase_size(fdt, 512);
  118 + if (offs) {
  119 + printf("No Space for dtb\n");
  120 + return -1;
  121 + }
  122 +
  123 + path = "/firmware";
  124 + offs = fdt_path_offset(fdt, path);
  125 + if (offs < 0) {
  126 + offs = add_dt_path_subnode(fdt, "/", "firmware");
  127 + if (offs < 0)
  128 + return -1;
  129 + }
  130 +
  131 + subpath = "optee";
  132 + offs = fdt_add_subnode(fdt, offs, subpath);
  133 + if (offs < 0) {
  134 + printf("Could not create %s node.\n", subpath);
  135 + return -1;
  136 + }
  137 +
  138 + fdt_setprop_string(fdt, offs, "compatible", "linaro,optee-tz");
  139 + fdt_setprop_string(fdt, offs, "method", "smc");
  140 +
  141 + ret = add_res_mem_dt_node(fdt, "optee_core", optee_start, optee_size);
  142 + if (ret < 0) {
  143 + printf("Could not create optee_core node.\n");
  144 + return -1;
  145 + }
  146 +
  147 + ret = add_res_mem_dt_node(fdt, "optee_shm", optee_start + optee_size,
  148 + OPTEE_SHM_SIZE);
  149 + if (ret < 0) {
  150 + printf("Could not create optee_shm node.\n");
  151 + return -1;
  152 + }
  153 + return ret;
  154 +}
  155 +#endif
arch/arm/mach-imx/imx8/fdt.c
... ... @@ -6,6 +6,7 @@
6 6 #include <common.h>
7 7 #include <asm/arch/sci/sci.h>
8 8 #include <asm/arch/sys_proto.h>
  9 +#include <asm/mach-imx/optee.h>
9 10 #include <dm/ofnode.h>
10 11 #include <fdt_support.h>
11 12 #include <linux/libfdt.h>
... ... @@ -568,56 +569,6 @@
568 569 name, proplen);
569 570 }
570 571 }
571   -
572   - return 0;
573   -}
574   -
575   -static int ft_add_optee_node(void *fdt, bd_t *bd)
576   -{
577   - const char *path, *subpath;
578   - int offs;
579   -
580   - /*
581   - * No TEE space allocated indicating no TEE running, so no
582   - * need to add optee node in dts
583   - */
584   - if (!boot_pointer[1])
585   - return 0;
586   -
587   - offs = fdt_increase_size(fdt, 512);
588   - if (offs) {
589   - printf("No Space for dtb\n");
590   - return 1;
591   - }
592   -
593   - path = "/firmware";
594   - offs = fdt_path_offset(fdt, path);
595   - if (offs < 0) {
596   - path = "/";
597   - offs = fdt_path_offset(fdt, path);
598   -
599   - if (offs < 0) {
600   - printf("Could not find root node.\n");
601   - return offs;
602   - }
603   -
604   - subpath = "firmware";
605   - offs = fdt_add_subnode(fdt, offs, subpath);
606   - if (offs < 0) {
607   - printf("Could not create %s node.\n", subpath);
608   - return offs;
609   - }
610   - }
611   -
612   - subpath = "optee";
613   - offs = fdt_add_subnode(fdt, offs, subpath);
614   - if (offs < 0) {
615   - printf("Could not create %s node.\n", subpath);
616   - return offs;
617   - }
618   -
619   - fdt_setprop_string(fdt, offs, "compatible", "linaro,optee-tz");
620   - fdt_setprop_string(fdt, offs, "method", "smc");
621 572  
622 573 return 0;
623 574 }
arch/arm/mach-imx/imx8m/soc.c
... ... @@ -13,6 +13,7 @@
13 13 #include <asm/arch/sys_proto.h>
14 14 #include <asm/mach-imx/hab.h>
15 15 #include <asm/mach-imx/boot_mode.h>
  16 +#include <asm/mach-imx/optee.h>
16 17 #include <asm/mach-imx/syscounter.h>
17 18 #include <asm/armv8/mmu.h>
18 19 #include <dm/uclass.h>
... ... @@ -479,54 +480,6 @@
479 480 #endif
480 481  
481 482 #ifdef CONFIG_OF_SYSTEM_SETUP
482   -static int ft_add_optee_node(void *fdt, bd_t *bd)
483   -{
484   - const char *path, *subpath;
485   - int offs;
486   -
487   - /*
488   - * No TEE space allocated indicating no TEE running, so no
489   - * need to add optee node in dts
490   - */
491   - if (!rom_pointer[1])
492   - return 0;
493   -
494   - offs = fdt_increase_size(fdt, 512);
495   - if (offs) {
496   - printf("No Space for dtb\n");
497   - return 1;
498   - }
499   -
500   - path = "/firmware";
501   - offs = fdt_path_offset(fdt, path);
502   - if (offs < 0) {
503   - path = "/";
504   - offs = fdt_path_offset(fdt, path);
505   -
506   - if (offs < 0) {
507   - printf("Could not find root node.\n");
508   - return 1;
509   - }
510   -
511   - subpath = "firmware";
512   - offs = fdt_add_subnode(fdt, offs, subpath);
513   - if (offs < 0) {
514   - printf("Could not create %s node.\n", subpath);
515   - }
516   - }
517   -
518   - subpath = "optee";
519   - offs = fdt_add_subnode(fdt, offs, subpath);
520   - if (offs < 0) {
521   - printf("Could not create %s node.\n", subpath);
522   - }
523   -
524   - fdt_setprop_string(fdt, offs, "compatible", "linaro,optee-tz");
525   - fdt_setprop_string(fdt, offs, "method", "smc");
526   -
527   - return 0;
528   -}
529   -
530 483 static int disable_fdt_nodes(void *blob, const char *nodes_path[], int size_array)
531 484 {
532 485 int i = 0;