1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2026, STMicroelectronics - All Rights Reserved 4 */ 5 6 #include <linux/bus/stm32_firewall.h> 7 #include <linux/bus/stm32_firewall_device.h> 8 #include <linux/device.h> 9 #include <linux/err.h> 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/of_platform.h> 14 #include <linux/platform_device.h> 15 #include <linux/pm_runtime.h> 16 #include <linux/tee_drv.h> 17 #include <linux/types.h> 18 19 enum stm32_dbg_profile { 20 PERIPHERAL_DBG_PROFILE = 0, 21 HDP_DBG_PROFILE = 1, 22 }; 23 24 enum stm32_dbg_pta_command { 25 /* 26 * PTA_CMD_GRANT_DBG_ACCESS - Verify the debug configuration against the given debug profile 27 * and grant access or not 28 * 29 * [in] value[0].a Debug profile to grant access to. 30 */ 31 PTA_CMD_GRANT_DBG_ACCESS, 32 }; 33 34 /** 35 * struct stm32_dbg_bus - OP-TEE based STM32 debug bus private data 36 * @dev: STM32 debug bus device. 37 * @ctx: OP-TEE context handler. 38 */ 39 struct stm32_dbg_bus { 40 struct device *dev; 41 struct tee_context *ctx; 42 }; 43 44 /* Expect at most 1 instance of this driver */ 45 static struct stm32_dbg_bus *stm32_dbg_bus_priv; 46 47 static int stm32_dbg_pta_open_session(u32 *id) 48 { 49 struct tee_client_device *dbg_bus_dev = to_tee_client_device(stm32_dbg_bus_priv->dev); 50 struct tee_ioctl_open_session_arg sess_arg; 51 int ret; 52 53 memset(&sess_arg, 0, sizeof(sess_arg)); 54 export_uuid(sess_arg.uuid, &dbg_bus_dev->id.uuid); 55 sess_arg.clnt_login = TEE_IOCTL_LOGIN_REE_KERNEL; 56 57 ret = tee_client_open_session(stm32_dbg_bus_priv->ctx, &sess_arg, NULL); 58 if (ret < 0 || sess_arg.ret) { 59 dev_err(stm32_dbg_bus_priv->dev, "Failed opening tee session, err: %#x\n", 60 sess_arg.ret); 61 return -EOPNOTSUPP; 62 } 63 64 *id = sess_arg.session; 65 66 return 0; 67 } 68 69 static void stm32_dbg_pta_close_session(u32 id) 70 { 71 tee_client_close_session(stm32_dbg_bus_priv->ctx, id); 72 } 73 74 static int stm32_dbg_bus_grant_access(struct stm32_firewall_controller *ctrl, u32 dbg_profile) 75 { 76 struct tee_ioctl_invoke_arg inv_arg = {0}; 77 struct tee_param param[1] = {0}; 78 u32 session_id; 79 int ret; 80 81 if (dbg_profile != PERIPHERAL_DBG_PROFILE && dbg_profile != HDP_DBG_PROFILE) 82 return -EOPNOTSUPP; 83 84 ret = stm32_dbg_pta_open_session(&session_id); 85 if (ret) 86 return ret; 87 88 inv_arg.func = PTA_CMD_GRANT_DBG_ACCESS; 89 inv_arg.session = session_id; 90 inv_arg.num_params = 1; 91 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT; 92 param[0].u.value.a = dbg_profile; 93 94 ret = tee_client_invoke_func(stm32_dbg_bus_priv->ctx, &inv_arg, param); 95 if (ret < 0 || inv_arg.ret != 0) { 96 dev_dbg(stm32_dbg_bus_priv->dev, 97 "When invoking function, err %x, TEE returns: %x\n", ret, inv_arg.ret); 98 if (!ret) 99 ret = -EACCES; 100 } 101 102 stm32_dbg_pta_close_session(session_id); 103 104 return ret; 105 } 106 107 /* Implement mandatory release_access ops even if it does nothing*/ 108 static void stm32_dbg_bus_release_access(struct stm32_firewall_controller *ctrl, u32 dbg_profile) 109 { 110 } 111 112 static int stm32_dbg_bus_plat_probe(struct platform_device *pdev) 113 { 114 struct stm32_firewall_controller *dbg_controller; 115 int ret; 116 117 /* Defer if OP-TEE service is not yet available */ 118 if (!stm32_dbg_bus_priv) 119 return -EPROBE_DEFER; 120 121 dbg_controller = devm_kzalloc(&pdev->dev, sizeof(*dbg_controller), GFP_KERNEL); 122 if (!dbg_controller) 123 return dev_err_probe(&pdev->dev, -ENOMEM, "Couldn't allocate debug controller\n"); 124 125 dbg_controller->dev = &pdev->dev; 126 dbg_controller->mmio = NULL; 127 dbg_controller->name = dev_driver_string(dbg_controller->dev); 128 dbg_controller->type = STM32_PERIPHERAL_FIREWALL; 129 dbg_controller->grant_access = stm32_dbg_bus_grant_access; 130 dbg_controller->release_access = stm32_dbg_bus_release_access; 131 132 ret = stm32_firewall_controller_register(dbg_controller); 133 if (ret) { 134 dev_err(dbg_controller->dev, "Couldn't register as a firewall controller: %d", ret); 135 return ret; 136 } 137 138 ret = stm32_firewall_populate_bus(dbg_controller); 139 if (ret) { 140 dev_err(dbg_controller->dev, "Couldn't populate debug bus: %d", ret); 141 stm32_firewall_controller_unregister(dbg_controller); 142 return ret; 143 } 144 145 pm_runtime_enable(&pdev->dev); 146 147 ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev); 148 if (ret) { 149 dev_err(dbg_controller->dev, "Couldn't populate the node: %d", ret); 150 stm32_firewall_controller_unregister(dbg_controller); 151 return ret; 152 } 153 154 return 0; 155 } 156 157 static const struct of_device_id stm32_dbg_bus_of_match[] = { 158 { .compatible = "st,stm32mp131-dbg-bus", }, 159 { .compatible = "st,stm32mp151-dbg-bus", }, 160 { }, 161 }; 162 MODULE_DEVICE_TABLE(of, stm32_dbg_bus_of_match); 163 164 static struct platform_driver stm32_dbg_bus_driver = { 165 .probe = stm32_dbg_bus_plat_probe, 166 .driver = { 167 .name = "stm32-dbg-bus", 168 .of_match_table = stm32_dbg_bus_of_match, 169 }, 170 }; 171 172 static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data) 173 { 174 return (ver->impl_id == TEE_IMPL_ID_OPTEE); 175 } 176 177 static void stm32_dbg_bus_remove(struct tee_client_device *tee_dev) 178 { 179 tee_client_close_context(stm32_dbg_bus_priv->ctx); 180 stm32_dbg_bus_priv = NULL; 181 182 of_platform_depopulate(&tee_dev->dev); 183 } 184 185 static int stm32_dbg_bus_probe(struct tee_client_device *tee_dev) 186 { 187 struct device *dev = &tee_dev->dev; 188 struct stm32_dbg_bus *priv; 189 int ret = 0; 190 191 if (stm32_dbg_bus_priv) 192 return dev_err_probe(dev, -EBUSY, 193 "A STM32 debug bus device is already initialized\n"); 194 195 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 196 if (!priv) 197 return -ENOMEM; 198 199 /* Open context with TEE driver */ 200 priv->ctx = tee_client_open_context(NULL, optee_ctx_match, NULL, NULL); 201 if (IS_ERR_OR_NULL(priv->ctx)) 202 return dev_err_probe(dev, PTR_ERR_OR_ZERO(priv->ctx), "Cannot open TEE context\n"); 203 204 stm32_dbg_bus_priv = priv; 205 stm32_dbg_bus_priv->dev = dev; 206 207 return ret; 208 } 209 210 static const struct tee_client_device_id optee_dbg_bus_id_table[] = { 211 {UUID_INIT(0xdd05bc8b, 0x9f3b, 0x49f0, 212 0xb6, 0x49, 0x01, 0xaa, 0x10, 0xc1, 0xc2, 0x10)}, 213 {} 214 }; 215 216 static struct tee_client_driver stm32_optee_dbg_bus_driver = { 217 .id_table = optee_dbg_bus_id_table, 218 .probe = stm32_dbg_bus_probe, 219 .remove = stm32_dbg_bus_remove, 220 .driver = { 221 .name = "optee_dbg_bus", 222 }, 223 }; 224 225 static void __exit stm32_optee_dbg_bus_driver_exit(void) 226 { 227 platform_driver_unregister(&stm32_dbg_bus_driver); 228 tee_client_driver_unregister(&stm32_optee_dbg_bus_driver); 229 } 230 module_exit(stm32_optee_dbg_bus_driver_exit); 231 232 static int __init stm32_optee_dbg_bus_driver_init(void) 233 { 234 int err; 235 236 err = tee_client_driver_register(&stm32_optee_dbg_bus_driver); 237 if (err) 238 return err; 239 240 err = platform_driver_register(&stm32_dbg_bus_driver); 241 if (err) 242 tee_client_driver_unregister(&stm32_optee_dbg_bus_driver); 243 244 return err; 245 } 246 module_init(stm32_optee_dbg_bus_driver_init); 247 248 MODULE_LICENSE("GPL"); 249 MODULE_AUTHOR("Gatien Chevallier <gatien.chevallier@foss.st.com>"); 250 MODULE_DESCRIPTION("OP-TEE based STM32 debug access bus driver"); 251