Skip to content

Commit 8aa1e3a

Browse files
qc-azarrabiandersson
authored andcommitted
firmware: qcom: tzmem: export shm_bridge create/delete
Anyone with access to contiguous physical memory should be able to share memory with QTEE using shm_bridge. Tested-by: Neil Armstrong <neil.armstrong@linaro.org> Tested-by: Harshal Dev <quic_hdev@quicinc.com> Reviewed-by: Kuldeep Singh <quic_kuldsing@quicinc.com> Signed-off-by: Amirreza Zarrabi <amirreza.zarrabi@oss.qualcomm.com> Link: https://lore.kernel.org/r/20250911-qcom-tee-using-tee-ss-without-mem-obj-v12-1-17f07a942b8d@oss.qualcomm.com Signed-off-by: Bjorn Andersson <andersson@kernel.org>
1 parent 8f5ae30 commit 8aa1e3a

2 files changed

Lines changed: 67 additions & 11 deletions

File tree

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_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)