1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Driver for Qualcomm Secure Execution Environment (SEE) interface (QSEECOM).
4  * Responsible for setting up and managing QSEECOM client devices.
5  *
6  * Copyright (C) 2023 Maximilian Luz <[email protected]>
7  */
8 
9 #ifndef __QCOM_QSEECOM_H
10 #define __QCOM_QSEECOM_H
11 
12 #include <linux/auxiliary_bus.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/types.h>
15 
16 #include <linux/firmware/qcom/qcom_scm.h>
17 
18 /**
19  * struct qseecom_client - QSEECOM client device.
20  * @aux_dev: Underlying auxiliary device.
21  * @app_id: ID of the loaded application.
22  */
23 struct qseecom_client {
24 	struct auxiliary_device aux_dev;
25 	u32 app_id;
26 };
27 
28 /**
29  * qseecom_scm_dev() - Get the SCM device associated with the QSEECOM client.
30  * @client: The QSEECOM client device.
31  *
32  * Returns the SCM device under which the provided QSEECOM client device
33  * operates. This function is intended to be used for DMA allocations.
34  */
35 static inline struct device *qseecom_scm_dev(struct qseecom_client *client)
36 {
37 	return client->aux_dev.dev.parent->parent;
38 }
39 
40 /**
41  * qseecom_dma_alloc() - Allocate DMA memory for a QSEECOM client.
42  * @client:     The QSEECOM client to allocate the memory for.
43  * @size:       The number of bytes to allocate.
44  * @dma_handle: Pointer to where the DMA address should be stored.
45  * @gfp:        Allocation flags.
46  *
47  * Wrapper function for dma_alloc_coherent(), allocating DMA memory usable for
48  * TZ/QSEECOM communication. Refer to dma_alloc_coherent() for details.
49  */
50 static inline void *qseecom_dma_alloc(struct qseecom_client *client, size_t size,
51 				      dma_addr_t *dma_handle, gfp_t gfp)
52 {
53 	return dma_alloc_coherent(qseecom_scm_dev(client), size, dma_handle, gfp);
54 }
55 
56 /**
57  * dma_free_coherent() - Free QSEECOM DMA memory.
58  * @client:     The QSEECOM client for which the memory has been allocated.
59  * @size:       The number of bytes allocated.
60  * @cpu_addr:   Virtual memory address to free.
61  * @dma_handle: DMA memory address to free.
62  *
63  * Wrapper function for dma_free_coherent(), freeing memory previously
64  * allocated with qseecom_dma_alloc(). Refer to dma_free_coherent() for
65  * details.
66  */
67 static inline void qseecom_dma_free(struct qseecom_client *client, size_t size,
68 				    void *cpu_addr, dma_addr_t dma_handle)
69 {
70 	return dma_free_coherent(qseecom_scm_dev(client), size, cpu_addr, dma_handle);
71 }
72 
73 /**
74  * qcom_qseecom_app_send() - Send to and receive data from a given QSEE app.
75  * @client:   The QSEECOM client associated with the target app.
76  * @req:      Request buffer sent to the app (must be TZ memory).
77  * @req_size: Size of the request buffer.
78  * @rsp:      Response buffer, written to by the app (must be TZ memory).
79  * @rsp_size: Size of the response buffer.
80  *
81  * Sends a request to the QSEE app associated with the given client and read
82  * back its response. The caller must provide two DMA memory regions, one for
83  * the request and one for the response, and fill out the @req region with the
84  * respective (app-specific) request data. The QSEE app reads this and returns
85  * its response in the @rsp region.
86  *
87  * Note: This is a convenience wrapper around qcom_scm_qseecom_app_send().
88  * Clients should prefer to use this wrapper.
89  *
90  * Return: Zero on success, nonzero on failure.
91  */
92 static inline int qcom_qseecom_app_send(struct qseecom_client *client,
93 					void *req, size_t req_size,
94 					void *rsp, size_t rsp_size)
95 {
96 	return qcom_scm_qseecom_app_send(client->app_id, req, req_size, rsp, rsp_size);
97 }
98 
99 #endif /* __QCOM_QSEECOM_H */
100