Skip to content

Commit 36629d5

Browse files
committed
Merge branch '20250911-qcom-tee-using-tee-ss-without-mem-obj-v12-2-17f07a942b8d@oss.qualcomm.com' of https://git.kernel.org/pub/scm/linux/kernel/git/qcom/linux
firmware: qcom: tzmem: export shm_bridge create/delete firmware: qcom: scm: add support for object invocation Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
2 parents dbc2868 + 4b70009 commit 36629d5

5 files changed

Lines changed: 199 additions & 11 deletions

File tree

drivers/firmware/qcom/qcom_scm.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,122 @@ static int qcom_scm_qseecom_init(struct qcom_scm *scm)
20932093

20942094
#endif /* CONFIG_QCOM_QSEECOM */
20952095

2096+
/**
2097+
* qcom_scm_qtee_invoke_smc() - Invoke a QTEE object.
2098+
* @inbuf: start address of memory area used for inbound buffer.
2099+
* @inbuf_size: size of the memory area used for inbound buffer.
2100+
* @outbuf: start address of memory area used for outbound buffer.
2101+
* @outbuf_size: size of the memory area used for outbound buffer.
2102+
* @result: result of QTEE object invocation.
2103+
* @response_type: response type returned by QTEE.
2104+
*
2105+
* @response_type determines how the contents of @inbuf and @outbuf
2106+
* should be processed.
2107+
*
2108+
* Return: On success, return 0 or <0 on failure.
2109+
*/
2110+
int qcom_scm_qtee_invoke_smc(phys_addr_t inbuf, size_t inbuf_size,
2111+
phys_addr_t outbuf, size_t outbuf_size,
2112+
u64 *result, u64 *response_type)
2113+
{
2114+
struct qcom_scm_desc desc = {
2115+
.svc = QCOM_SCM_SVC_SMCINVOKE,
2116+
.cmd = QCOM_SCM_SMCINVOKE_INVOKE,
2117+
.owner = ARM_SMCCC_OWNER_TRUSTED_OS,
2118+
.args[0] = inbuf,
2119+
.args[1] = inbuf_size,
2120+
.args[2] = outbuf,
2121+
.args[3] = outbuf_size,
2122+
.arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_RW, QCOM_SCM_VAL,
2123+
QCOM_SCM_RW, QCOM_SCM_VAL),
2124+
};
2125+
struct qcom_scm_res res;
2126+
int ret;
2127+
2128+
ret = qcom_scm_call(__scm->dev, &desc, &res);
2129+
if (ret)
2130+
return ret;
2131+
2132+
if (response_type)
2133+
*response_type = res.result[0];
2134+
2135+
if (result)
2136+
*result = res.result[1];
2137+
2138+
return 0;
2139+
}
2140+
EXPORT_SYMBOL(qcom_scm_qtee_invoke_smc);
2141+
2142+
/**
2143+
* qcom_scm_qtee_callback_response() - Submit response for callback request.
2144+
* @buf: start address of memory area used for outbound buffer.
2145+
* @buf_size: size of the memory area used for outbound buffer.
2146+
* @result: Result of QTEE object invocation.
2147+
* @response_type: Response type returned by QTEE.
2148+
*
2149+
* @response_type determines how the contents of @buf should be processed.
2150+
*
2151+
* Return: On success, return 0 or <0 on failure.
2152+
*/
2153+
int qcom_scm_qtee_callback_response(phys_addr_t buf, size_t buf_size,
2154+
u64 *result, u64 *response_type)
2155+
{
2156+
struct qcom_scm_desc desc = {
2157+
.svc = QCOM_SCM_SVC_SMCINVOKE,
2158+
.cmd = QCOM_SCM_SMCINVOKE_CB_RSP,
2159+
.owner = ARM_SMCCC_OWNER_TRUSTED_OS,
2160+
.args[0] = buf,
2161+
.args[1] = buf_size,
2162+
.arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_RW, QCOM_SCM_VAL),
2163+
};
2164+
struct qcom_scm_res res;
2165+
int ret;
2166+
2167+
ret = qcom_scm_call(__scm->dev, &desc, &res);
2168+
if (ret)
2169+
return ret;
2170+
2171+
if (response_type)
2172+
*response_type = res.result[0];
2173+
2174+
if (result)
2175+
*result = res.result[1];
2176+
2177+
return 0;
2178+
}
2179+
EXPORT_SYMBOL(qcom_scm_qtee_callback_response);
2180+
2181+
static void qcom_scm_qtee_free(void *data)
2182+
{
2183+
struct platform_device *qtee_dev = data;
2184+
2185+
platform_device_unregister(qtee_dev);
2186+
}
2187+
2188+
static void qcom_scm_qtee_init(struct qcom_scm *scm)
2189+
{
2190+
struct platform_device *qtee_dev;
2191+
u64 result, response_type;
2192+
int ret;
2193+
2194+
/*
2195+
* Probe for smcinvoke support. This will fail due to invalid buffers,
2196+
* but first, it checks whether the call is supported in QTEE syscall
2197+
* handler. If it is not supported, -EIO is returned.
2198+
*/
2199+
ret = qcom_scm_qtee_invoke_smc(0, 0, 0, 0, &result, &response_type);
2200+
if (ret == -EIO)
2201+
return;
2202+
2203+
/* Setup QTEE interface device. */
2204+
qtee_dev = platform_device_register_data(scm->dev, "qcomtee",
2205+
PLATFORM_DEVID_NONE, NULL, 0);
2206+
if (IS_ERR(qtee_dev))
2207+
return;
2208+
2209+
devm_add_action_or_reset(scm->dev, qcom_scm_qtee_free, qtee_dev);
2210+
}
2211+
20962212
/**
20972213
* qcom_scm_is_available() - Checks if SCM is available
20982214
*/
@@ -2325,6 +2441,9 @@ static int qcom_scm_probe(struct platform_device *pdev)
23252441
ret = qcom_scm_qseecom_init(scm);
23262442
WARN(ret < 0, "failed to initialize qseecom: %d\n", ret);
23272443

2444+
/* Initialize the QTEE object interface. */
2445+
qcom_scm_qtee_init(scm);
2446+
23282447
return 0;
23292448
}
23302449

drivers/firmware/qcom/qcom_scm.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ int qcom_scm_shm_bridge_enable(struct device *scm_dev);
156156
#define QCOM_SCM_SVC_GPU 0x28
157157
#define QCOM_SCM_SVC_GPU_INIT_REGS 0x01
158158

159+
/* ARM_SMCCC_OWNER_TRUSTED_OS calls */
160+
161+
#define QCOM_SCM_SVC_SMCINVOKE 0x06
162+
#define QCOM_SCM_SMCINVOKE_INVOKE_LEGACY 0x00
163+
#define QCOM_SCM_SMCINVOKE_CB_RSP 0x01
164+
#define QCOM_SCM_SMCINVOKE_INVOKE 0x02
165+
159166
/* common error codes */
160167
#define QCOM_SCM_V2_EBUSY -12
161168
#define QCOM_SCM_ENOMEM -5

drivers/firmware/qcom/qcom_tzmem.c

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,69 @@ static int qcom_tzmem_init(void)
109109
return 0;
110110
}
111111

112-
static int qcom_tzmem_init_area(struct qcom_tzmem_area *area)
112+
/**
113+
* qcom_tzmem_shm_bridge_create() - Create a SHM bridge.
114+
* @paddr: Physical address of the memory to share.
115+
* @size: Size of the memory to share.
116+
* @handle: Handle to the SHM bridge.
117+
*
118+
* On platforms that support SHM bridge, this function creates a SHM bridge
119+
* for the given memory region with QTEE. The handle returned by this function
120+
* must be passed to qcom_tzmem_shm_bridge_delete() to free the SHM bridge.
121+
*
122+
* Return: On success, returns 0; on failure, returns < 0.
123+
*/
124+
int qcom_tzmem_shm_bridge_create(phys_addr_t paddr, size_t size, u64 *handle)
113125
{
114126
u64 pfn_and_ns_perm, ipfn_and_s_perm, size_and_flags;
115127
int ret;
116128

117129
if (!qcom_tzmem_using_shm_bridge)
118130
return 0;
119131

120-
pfn_and_ns_perm = (u64)area->paddr | QCOM_SCM_PERM_RW;
121-
ipfn_and_s_perm = (u64)area->paddr | QCOM_SCM_PERM_RW;
122-
size_and_flags = area->size | (1 << QCOM_SHM_BRIDGE_NUM_VM_SHIFT);
132+
pfn_and_ns_perm = paddr | QCOM_SCM_PERM_RW;
133+
ipfn_and_s_perm = paddr | QCOM_SCM_PERM_RW;
134+
size_and_flags = size | (1 << QCOM_SHM_BRIDGE_NUM_VM_SHIFT);
135+
136+
ret = qcom_scm_shm_bridge_create(pfn_and_ns_perm, ipfn_and_s_perm,
137+
size_and_flags, QCOM_SCM_VMID_HLOS,
138+
handle);
139+
if (ret) {
140+
dev_err(qcom_tzmem_dev,
141+
"SHM Bridge failed: ret %d paddr 0x%pa, size %zu\n",
142+
ret, &paddr, size);
143+
144+
return ret;
145+
}
146+
147+
return 0;
148+
}
149+
EXPORT_SYMBOL_GPL(qcom_tzmem_shm_bridge_create);
150+
151+
/**
152+
* qcom_tzmem_shm_bridge_delete() - Delete a SHM bridge.
153+
* @handle: Handle to the SHM bridge.
154+
*
155+
* On platforms that support SHM bridge, this function deletes the SHM bridge
156+
* for the given memory region. The handle must be the same as the one
157+
* returned by qcom_tzmem_shm_bridge_create().
158+
*/
159+
void qcom_tzmem_shm_bridge_delete(u64 handle)
160+
{
161+
if (qcom_tzmem_using_shm_bridge)
162+
qcom_scm_shm_bridge_delete(handle);
163+
}
164+
EXPORT_SYMBOL_GPL(qcom_tzmem_shm_bridge_delete);
165+
166+
static int qcom_tzmem_init_area(struct qcom_tzmem_area *area)
167+
{
168+
int ret;
123169

124170
u64 *handle __free(kfree) = kzalloc(sizeof(*handle), GFP_KERNEL);
125171
if (!handle)
126172
return -ENOMEM;
127173

128-
ret = qcom_scm_shm_bridge_create(pfn_and_ns_perm, ipfn_and_s_perm,
129-
size_and_flags, QCOM_SCM_VMID_HLOS,
130-
handle);
174+
ret = qcom_tzmem_shm_bridge_create(area->paddr, area->size, handle);
131175
if (ret)
132176
return ret;
133177

@@ -140,10 +184,7 @@ static void qcom_tzmem_cleanup_area(struct qcom_tzmem_area *area)
140184
{
141185
u64 *handle = area->priv;
142186

143-
if (!qcom_tzmem_using_shm_bridge)
144-
return;
145-
146-
qcom_scm_shm_bridge_delete(*handle);
187+
qcom_tzmem_shm_bridge_delete(*handle);
147188
kfree(handle);
148189
}
149190

include/linux/firmware/qcom/qcom_scm.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,10 @@ static inline int qcom_scm_qseecom_app_send(u32 app_id,
175175

176176
#endif /* CONFIG_QCOM_QSEECOM */
177177

178+
int qcom_scm_qtee_invoke_smc(phys_addr_t inbuf, size_t inbuf_size,
179+
phys_addr_t outbuf, size_t outbuf_size,
180+
u64 *result, u64 *response_type);
181+
int qcom_scm_qtee_callback_response(phys_addr_t buf, size_t buf_size,
182+
u64 *result, u64 *response_type);
183+
178184
#endif

include/linux/firmware/qcom/qcom_tzmem.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,19 @@ DEFINE_FREE(qcom_tzmem, void *, if (_T) qcom_tzmem_free(_T))
5353

5454
phys_addr_t qcom_tzmem_to_phys(void *ptr);
5555

56+
#if IS_ENABLED(CONFIG_QCOM_TZMEM_MODE_SHMBRIDGE)
57+
int qcom_tzmem_shm_bridge_create(phys_addr_t paddr, size_t size, u64 *handle);
58+
void qcom_tzmem_shm_bridge_delete(u64 handle);
59+
#else
60+
static inline int qcom_tzmem_shm_bridge_create(phys_addr_t paddr,
61+
size_t size, u64 *handle)
62+
{
63+
return 0;
64+
}
65+
66+
static inline void qcom_tzmem_shm_bridge_delete(u64 handle)
67+
{
68+
}
69+
#endif
70+
5671
#endif /* __QCOM_TZMEM */

0 commit comments

Comments
 (0)