1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2019 Broadcom.
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/sizes.h>
9 #include <linux/slab.h>
10 #include <linux/tee_drv.h>
11 #include <linux/uuid.h>
13 #include <linux/firmware/broadcom/tee_bnxt_fw.h>
15 #define MAX_SHM_MEM_SZ SZ_4M
17 #define MAX_TEE_PARAM_ARRY_MEMB 4
21 * TA_CMD_BNXT_FASTBOOT - boot bnxt device by copying f/w into sram
29 * TEE_SUCCESS - Invoke command success
30 * TEE_ERROR_ITEM_NOT_FOUND - Corrupt f/w image found on memory
32 TA_CMD_BNXT_FASTBOOT = 0,
35 * TA_CMD_BNXT_COPY_COREDUMP - copy the core dump into shm
37 * param[0] (inout memref) - Coredump buffer memory reference
38 * param[1] (in value) - value.a: offset, data to be copied from
39 * value.b: size of data to be copied
44 * TEE_SUCCESS - Invoke command success
45 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
46 * TEE_ERROR_ITEM_NOT_FOUND - Corrupt core dump
48 TA_CMD_BNXT_COPY_COREDUMP = 3,
52 * struct tee_bnxt_fw_private - OP-TEE bnxt private data
53 * @dev: OP-TEE based bnxt device.
54 * @ctx: OP-TEE context handler.
55 * @session_id: TA session identifier.
57 struct tee_bnxt_fw_private {
59 struct tee_context *ctx;
61 struct tee_shm *fw_shm_pool;
64 static struct tee_bnxt_fw_private pvt_data;
66 static void prepare_args(int cmd,
67 struct tee_ioctl_invoke_arg *arg,
68 struct tee_param *param)
70 memset(arg, 0, sizeof(*arg));
71 memset(param, 0, MAX_TEE_PARAM_ARRY_MEMB * sizeof(*param));
74 arg->session = pvt_data.session_id;
75 arg->num_params = MAX_TEE_PARAM_ARRY_MEMB;
77 /* Fill invoke cmd params */
79 case TA_CMD_BNXT_COPY_COREDUMP:
80 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT;
81 param[0].u.memref.shm = pvt_data.fw_shm_pool;
82 param[0].u.memref.size = MAX_SHM_MEM_SZ;
83 param[0].u.memref.shm_offs = 0;
84 param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
86 case TA_CMD_BNXT_FASTBOOT:
94 * tee_bnxt_fw_load() - Load the bnxt firmware
95 * Uses an OP-TEE call to start a secure
97 * Returns 0 on success, negative errno otherwise.
99 int tee_bnxt_fw_load(void)
102 struct tee_ioctl_invoke_arg arg;
103 struct tee_param param[MAX_TEE_PARAM_ARRY_MEMB];
108 prepare_args(TA_CMD_BNXT_FASTBOOT, &arg, param);
110 ret = tee_client_invoke_func(pvt_data.ctx, &arg, param);
111 if (ret < 0 || arg.ret != 0) {
112 dev_err(pvt_data.dev,
113 "TA_CMD_BNXT_FASTBOOT invoke failed TEE err: %x, ret:%x\n",
120 EXPORT_SYMBOL(tee_bnxt_fw_load);
123 * tee_bnxt_copy_coredump() - Copy coredump from the allocated memory
124 * Uses an OP-TEE call to copy coredump
125 * @buf: destination buffer where core dump is copied into
126 * @offset: offset from the base address of core dump area
127 * @size: size of the dump
129 * Returns 0 on success, negative errno otherwise.
131 int tee_bnxt_copy_coredump(void *buf, u32 offset, u32 size)
133 struct tee_ioctl_invoke_arg arg;
134 struct tee_param param[MAX_TEE_PARAM_ARRY_MEMB];
143 prepare_args(TA_CMD_BNXT_COPY_COREDUMP, &arg, param);
148 nbytes = min_t(u32, rbytes, param[0].u.memref.size);
150 /* Fill additional invoke cmd params */
151 param[1].u.value.a = offset;
152 param[1].u.value.b = nbytes;
154 ret = tee_client_invoke_func(pvt_data.ctx, &arg, param);
155 if (ret < 0 || arg.ret != 0) {
156 dev_err(pvt_data.dev,
157 "TA_CMD_BNXT_COPY_COREDUMP invoke failed TEE err: %x, ret:%x\n",
162 core_data = tee_shm_get_va(pvt_data.fw_shm_pool, 0);
163 if (IS_ERR(core_data)) {
164 dev_err(pvt_data.dev, "tee_shm_get_va failed\n");
165 return PTR_ERR(core_data);
168 memcpy(buf, core_data, nbytes);
177 EXPORT_SYMBOL(tee_bnxt_copy_coredump);
179 static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
181 return (ver->impl_id == TEE_IMPL_ID_OPTEE);
184 static int tee_bnxt_fw_probe(struct device *dev)
186 struct tee_client_device *bnxt_device = to_tee_client_device(dev);
187 int ret, err = -ENODEV;
188 struct tee_ioctl_open_session_arg sess_arg;
189 struct tee_shm *fw_shm_pool;
191 memset(&sess_arg, 0, sizeof(sess_arg));
193 /* Open context with TEE driver */
194 pvt_data.ctx = tee_client_open_context(NULL, optee_ctx_match, NULL,
196 if (IS_ERR(pvt_data.ctx))
199 /* Open session with Bnxt load Trusted App */
200 memcpy(sess_arg.uuid, bnxt_device->id.uuid.b, TEE_IOCTL_UUID_LEN);
201 sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
202 sess_arg.num_params = 0;
204 ret = tee_client_open_session(pvt_data.ctx, &sess_arg, NULL);
205 if (ret < 0 || sess_arg.ret != 0) {
206 dev_err(dev, "tee_client_open_session failed, err: %x\n",
211 pvt_data.session_id = sess_arg.session;
215 fw_shm_pool = tee_shm_alloc_kernel_buf(pvt_data.ctx, MAX_SHM_MEM_SZ);
216 if (IS_ERR(fw_shm_pool)) {
217 dev_err(pvt_data.dev, "tee_shm_alloc_kernel_buf failed\n");
218 err = PTR_ERR(fw_shm_pool);
222 pvt_data.fw_shm_pool = fw_shm_pool;
227 tee_client_close_session(pvt_data.ctx, pvt_data.session_id);
229 tee_client_close_context(pvt_data.ctx);
234 static int tee_bnxt_fw_remove(struct device *dev)
236 tee_shm_free(pvt_data.fw_shm_pool);
237 tee_client_close_session(pvt_data.ctx, pvt_data.session_id);
238 tee_client_close_context(pvt_data.ctx);
244 static void tee_bnxt_fw_shutdown(struct device *dev)
246 tee_shm_free(pvt_data.fw_shm_pool);
247 tee_client_close_session(pvt_data.ctx, pvt_data.session_id);
248 tee_client_close_context(pvt_data.ctx);
252 static const struct tee_client_device_id tee_bnxt_fw_id_table[] = {
253 {UUID_INIT(0x6272636D, 0x2019, 0x0716,
254 0x42, 0x43, 0x4D, 0x5F, 0x53, 0x43, 0x48, 0x49)},
258 MODULE_DEVICE_TABLE(tee, tee_bnxt_fw_id_table);
260 static struct tee_client_driver tee_bnxt_fw_driver = {
261 .id_table = tee_bnxt_fw_id_table,
263 .name = KBUILD_MODNAME,
264 .bus = &tee_bus_type,
265 .probe = tee_bnxt_fw_probe,
266 .remove = tee_bnxt_fw_remove,
267 .shutdown = tee_bnxt_fw_shutdown,
271 static int __init tee_bnxt_fw_mod_init(void)
273 return driver_register(&tee_bnxt_fw_driver.driver);
276 static void __exit tee_bnxt_fw_mod_exit(void)
278 driver_unregister(&tee_bnxt_fw_driver.driver);
281 module_init(tee_bnxt_fw_mod_init);
282 module_exit(tee_bnxt_fw_mod_exit);
284 MODULE_AUTHOR("Vikas Gupta <vikas.gupta@broadcom.com>");
285 MODULE_DESCRIPTION("Broadcom bnxt firmware manager");
286 MODULE_LICENSE("GPL v2");