Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright (c) 2015-2022 Linaro Limited
0004  */
0005 
0006 #ifndef __TEE_DRV_H
0007 #define __TEE_DRV_H
0008 
0009 #include <linux/device.h>
0010 #include <linux/idr.h>
0011 #include <linux/kref.h>
0012 #include <linux/list.h>
0013 #include <linux/mod_devicetable.h>
0014 #include <linux/tee.h>
0015 #include <linux/types.h>
0016 #include <linux/uuid.h>
0017 
0018 /*
0019  * The file describes the API provided by the generic TEE driver to the
0020  * specific TEE driver.
0021  */
0022 
0023 #define TEE_SHM_DYNAMIC     BIT(0)  /* Dynamic shared memory registered */
0024                     /* in secure world */
0025 #define TEE_SHM_USER_MAPPED BIT(1)  /* Memory mapped in user space */
0026 #define TEE_SHM_POOL        BIT(2)  /* Memory allocated from pool */
0027 #define TEE_SHM_PRIV        BIT(3)  /* Memory private to TEE driver */
0028 
0029 struct device;
0030 struct tee_device;
0031 struct tee_shm;
0032 struct tee_shm_pool;
0033 
0034 /**
0035  * struct tee_context - driver specific context on file pointer data
0036  * @teedev: pointer to this drivers struct tee_device
0037  * @list_shm:   List of shared memory object owned by this context
0038  * @data:   driver specific context data, managed by the driver
0039  * @refcount:   reference counter for this structure
0040  * @releasing:  flag that indicates if context is being released right now.
0041  *      It is needed to break circular dependency on context during
0042  *              shared memory release.
0043  * @supp_nowait: flag that indicates that requests in this context should not
0044  *              wait for tee-supplicant daemon to be started if not present
0045  *              and just return with an error code. It is needed for requests
0046  *              that arises from TEE based kernel drivers that should be
0047  *              non-blocking in nature.
0048  * @cap_memref_null: flag indicating if the TEE Client support shared
0049  *                   memory buffer with a NULL pointer.
0050  */
0051 struct tee_context {
0052     struct tee_device *teedev;
0053     void *data;
0054     struct kref refcount;
0055     bool releasing;
0056     bool supp_nowait;
0057     bool cap_memref_null;
0058 };
0059 
0060 struct tee_param_memref {
0061     size_t shm_offs;
0062     size_t size;
0063     struct tee_shm *shm;
0064 };
0065 
0066 struct tee_param_value {
0067     u64 a;
0068     u64 b;
0069     u64 c;
0070 };
0071 
0072 struct tee_param {
0073     u64 attr;
0074     union {
0075         struct tee_param_memref memref;
0076         struct tee_param_value value;
0077     } u;
0078 };
0079 
0080 /**
0081  * struct tee_driver_ops - driver operations vtable
0082  * @get_version:    returns version of driver
0083  * @open:       called when the device file is opened
0084  * @release:        release this open file
0085  * @open_session:   open a new session
0086  * @close_session:  close a session
0087  * @invoke_func:    invoke a trusted function
0088  * @cancel_req:     request cancel of an ongoing invoke or open
0089  * @supp_recv:      called for supplicant to get a command
0090  * @supp_send:      called for supplicant to send a response
0091  * @shm_register:   register shared memory buffer in TEE
0092  * @shm_unregister: unregister shared memory buffer in TEE
0093  */
0094 struct tee_driver_ops {
0095     void (*get_version)(struct tee_device *teedev,
0096                 struct tee_ioctl_version_data *vers);
0097     int (*open)(struct tee_context *ctx);
0098     void (*release)(struct tee_context *ctx);
0099     int (*open_session)(struct tee_context *ctx,
0100                 struct tee_ioctl_open_session_arg *arg,
0101                 struct tee_param *param);
0102     int (*close_session)(struct tee_context *ctx, u32 session);
0103     int (*invoke_func)(struct tee_context *ctx,
0104                struct tee_ioctl_invoke_arg *arg,
0105                struct tee_param *param);
0106     int (*cancel_req)(struct tee_context *ctx, u32 cancel_id, u32 session);
0107     int (*supp_recv)(struct tee_context *ctx, u32 *func, u32 *num_params,
0108              struct tee_param *param);
0109     int (*supp_send)(struct tee_context *ctx, u32 ret, u32 num_params,
0110              struct tee_param *param);
0111     int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm,
0112                 struct page **pages, size_t num_pages,
0113                 unsigned long start);
0114     int (*shm_unregister)(struct tee_context *ctx, struct tee_shm *shm);
0115 };
0116 
0117 /**
0118  * struct tee_desc - Describes the TEE driver to the subsystem
0119  * @name:   name of driver
0120  * @ops:    driver operations vtable
0121  * @owner:  module providing the driver
0122  * @flags:  Extra properties of driver, defined by TEE_DESC_* below
0123  */
0124 #define TEE_DESC_PRIVILEGED 0x1
0125 struct tee_desc {
0126     const char *name;
0127     const struct tee_driver_ops *ops;
0128     struct module *owner;
0129     u32 flags;
0130 };
0131 
0132 /**
0133  * tee_device_alloc() - Allocate a new struct tee_device instance
0134  * @teedesc:    Descriptor for this driver
0135  * @dev:    Parent device for this device
0136  * @pool:   Shared memory pool, NULL if not used
0137  * @driver_data: Private driver data for this device
0138  *
0139  * Allocates a new struct tee_device instance. The device is
0140  * removed by tee_device_unregister().
0141  *
0142  * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
0143  */
0144 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
0145                     struct device *dev,
0146                     struct tee_shm_pool *pool,
0147                     void *driver_data);
0148 
0149 /**
0150  * tee_device_register() - Registers a TEE device
0151  * @teedev: Device to register
0152  *
0153  * tee_device_unregister() need to be called to remove the @teedev if
0154  * this function fails.
0155  *
0156  * @returns < 0 on failure
0157  */
0158 int tee_device_register(struct tee_device *teedev);
0159 
0160 /**
0161  * tee_device_unregister() - Removes a TEE device
0162  * @teedev: Device to unregister
0163  *
0164  * This function should be called to remove the @teedev even if
0165  * tee_device_register() hasn't been called yet. Does nothing if
0166  * @teedev is NULL.
0167  */
0168 void tee_device_unregister(struct tee_device *teedev);
0169 
0170 /**
0171  * tee_session_calc_client_uuid() - Calculates client UUID for session
0172  * @uuid:       Resulting UUID
0173  * @connection_method:  Connection method for session (TEE_IOCTL_LOGIN_*)
0174  * @connectuon_data:    Connection data for opening session
0175  *
0176  * Based on connection method calculates UUIDv5 based client UUID.
0177  *
0178  * For group based logins verifies that calling process has specified
0179  * credentials.
0180  *
0181  * @return < 0 on failure
0182  */
0183 int tee_session_calc_client_uuid(uuid_t *uuid, u32 connection_method,
0184                  const u8 connection_data[TEE_IOCTL_UUID_LEN]);
0185 
0186 /**
0187  * struct tee_shm - shared memory object
0188  * @ctx:    context using the object
0189  * @paddr:  physical address of the shared memory
0190  * @kaddr:  virtual address of the shared memory
0191  * @size:   size of shared memory
0192  * @offset: offset of buffer in user space
0193  * @pages:  locked pages from userspace
0194  * @num_pages:  number of locked pages
0195  * @refcount:   reference counter
0196  * @flags:  defined by TEE_SHM_* in tee_drv.h
0197  * @id:     unique id of a shared memory object on this device, shared
0198  *      with user space
0199  * @sec_world_id:
0200  *      secure world assigned id of this shared memory object, not
0201  *      used by all drivers
0202  *
0203  * This pool is only supposed to be accessed directly from the TEE
0204  * subsystem and from drivers that implements their own shm pool manager.
0205  */
0206 struct tee_shm {
0207     struct tee_context *ctx;
0208     phys_addr_t paddr;
0209     void *kaddr;
0210     size_t size;
0211     unsigned int offset;
0212     struct page **pages;
0213     size_t num_pages;
0214     refcount_t refcount;
0215     u32 flags;
0216     int id;
0217     u64 sec_world_id;
0218 };
0219 
0220 /**
0221  * struct tee_shm_pool - shared memory pool
0222  * @ops:        operations
0223  * @private_data:   private data for the shared memory manager
0224  */
0225 struct tee_shm_pool {
0226     const struct tee_shm_pool_ops *ops;
0227     void *private_data;
0228 };
0229 
0230 /**
0231  * struct tee_shm_pool_ops - shared memory pool operations
0232  * @alloc:      called when allocating shared memory
0233  * @free:       called when freeing shared memory
0234  * @destroy_pool:   called when destroying the pool
0235  */
0236 struct tee_shm_pool_ops {
0237     int (*alloc)(struct tee_shm_pool *pool, struct tee_shm *shm,
0238              size_t size, size_t align);
0239     void (*free)(struct tee_shm_pool *pool, struct tee_shm *shm);
0240     void (*destroy_pool)(struct tee_shm_pool *pool);
0241 };
0242 
0243 /*
0244  * tee_shm_pool_alloc_res_mem() - Create a shm manager for reserved memory
0245  * @vaddr:  Virtual address of start of pool
0246  * @paddr:  Physical address of start of pool
0247  * @size:   Size in bytes of the pool
0248  *
0249  * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
0250  */
0251 struct tee_shm_pool *tee_shm_pool_alloc_res_mem(unsigned long vaddr,
0252                         phys_addr_t paddr, size_t size,
0253                         int min_alloc_order);
0254 
0255 /**
0256  * tee_shm_pool_free() - Free a shared memory pool
0257  * @pool:   The shared memory pool to free
0258  *
0259  * The must be no remaining shared memory allocated from this pool when
0260  * this function is called.
0261  */
0262 static inline void tee_shm_pool_free(struct tee_shm_pool *pool)
0263 {
0264     pool->ops->destroy_pool(pool);
0265 }
0266 
0267 /**
0268  * tee_get_drvdata() - Return driver_data pointer
0269  * @returns the driver_data pointer supplied to tee_register().
0270  */
0271 void *tee_get_drvdata(struct tee_device *teedev);
0272 
0273 struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size);
0274 struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size);
0275 
0276 struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx,
0277                         void *addr, size_t length);
0278 
0279 /**
0280  * tee_shm_is_dynamic() - Check if shared memory object is of the dynamic kind
0281  * @shm:    Shared memory handle
0282  * @returns true if object is dynamic shared memory
0283  */
0284 static inline bool tee_shm_is_dynamic(struct tee_shm *shm)
0285 {
0286     return shm && (shm->flags & TEE_SHM_DYNAMIC);
0287 }
0288 
0289 /**
0290  * tee_shm_free() - Free shared memory
0291  * @shm:    Handle to shared memory to free
0292  */
0293 void tee_shm_free(struct tee_shm *shm);
0294 
0295 /**
0296  * tee_shm_put() - Decrease reference count on a shared memory handle
0297  * @shm:    Shared memory handle
0298  */
0299 void tee_shm_put(struct tee_shm *shm);
0300 
0301 /**
0302  * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
0303  * @shm:    Shared memory handle
0304  * @offs:   Offset from start of this shared memory
0305  * @returns virtual address of the shared memory + offs if offs is within
0306  *  the bounds of this shared memory, else an ERR_PTR
0307  */
0308 void *tee_shm_get_va(struct tee_shm *shm, size_t offs);
0309 
0310 /**
0311  * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
0312  * @shm:    Shared memory handle
0313  * @offs:   Offset from start of this shared memory
0314  * @pa:     Physical address to return
0315  * @returns 0 if offs is within the bounds of this shared memory, else an
0316  *  error code.
0317  */
0318 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa);
0319 
0320 /**
0321  * tee_shm_get_size() - Get size of shared memory buffer
0322  * @shm:    Shared memory handle
0323  * @returns size of shared memory
0324  */
0325 static inline size_t tee_shm_get_size(struct tee_shm *shm)
0326 {
0327     return shm->size;
0328 }
0329 
0330 /**
0331  * tee_shm_get_pages() - Get list of pages that hold shared buffer
0332  * @shm:    Shared memory handle
0333  * @num_pages:  Number of pages will be stored there
0334  * @returns pointer to pages array
0335  */
0336 static inline struct page **tee_shm_get_pages(struct tee_shm *shm,
0337                           size_t *num_pages)
0338 {
0339     *num_pages = shm->num_pages;
0340     return shm->pages;
0341 }
0342 
0343 /**
0344  * tee_shm_get_page_offset() - Get shared buffer offset from page start
0345  * @shm:    Shared memory handle
0346  * @returns page offset of shared buffer
0347  */
0348 static inline size_t tee_shm_get_page_offset(struct tee_shm *shm)
0349 {
0350     return shm->offset;
0351 }
0352 
0353 /**
0354  * tee_shm_get_id() - Get id of a shared memory object
0355  * @shm:    Shared memory handle
0356  * @returns id
0357  */
0358 static inline int tee_shm_get_id(struct tee_shm *shm)
0359 {
0360     return shm->id;
0361 }
0362 
0363 /**
0364  * tee_shm_get_from_id() - Find shared memory object and increase reference
0365  * count
0366  * @ctx:    Context owning the shared memory
0367  * @id:     Id of shared memory object
0368  * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
0369  */
0370 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id);
0371 
0372 /**
0373  * tee_client_open_context() - Open a TEE context
0374  * @start:  if not NULL, continue search after this context
0375  * @match:  function to check TEE device
0376  * @data:   data for match function
0377  * @vers:   if not NULL, version data of TEE device of the context returned
0378  *
0379  * This function does an operation similar to open("/dev/teeX") in user space.
0380  * A returned context must be released with tee_client_close_context().
0381  *
0382  * Returns a TEE context of the first TEE device matched by the match()
0383  * callback or an ERR_PTR.
0384  */
0385 struct tee_context *
0386 tee_client_open_context(struct tee_context *start,
0387             int (*match)(struct tee_ioctl_version_data *,
0388                      const void *),
0389             const void *data, struct tee_ioctl_version_data *vers);
0390 
0391 /**
0392  * tee_client_close_context() - Close a TEE context
0393  * @ctx:    TEE context to close
0394  *
0395  * Note that all sessions previously opened with this context will be
0396  * closed when this function is called.
0397  */
0398 void tee_client_close_context(struct tee_context *ctx);
0399 
0400 /**
0401  * tee_client_get_version() - Query version of TEE
0402  * @ctx:    TEE context to TEE to query
0403  * @vers:   Pointer to version data
0404  */
0405 void tee_client_get_version(struct tee_context *ctx,
0406                 struct tee_ioctl_version_data *vers);
0407 
0408 /**
0409  * tee_client_open_session() - Open a session to a Trusted Application
0410  * @ctx:    TEE context
0411  * @arg:    Open session arguments, see description of
0412  *      struct tee_ioctl_open_session_arg
0413  * @param:  Parameters passed to the Trusted Application
0414  *
0415  * Returns < 0 on error else see @arg->ret for result. If @arg->ret
0416  * is TEEC_SUCCESS the session identifier is available in @arg->session.
0417  */
0418 int tee_client_open_session(struct tee_context *ctx,
0419                 struct tee_ioctl_open_session_arg *arg,
0420                 struct tee_param *param);
0421 
0422 /**
0423  * tee_client_close_session() - Close a session to a Trusted Application
0424  * @ctx:    TEE Context
0425  * @session:    Session id
0426  *
0427  * Return < 0 on error else 0, regardless the session will not be
0428  * valid after this function has returned.
0429  */
0430 int tee_client_close_session(struct tee_context *ctx, u32 session);
0431 
0432 /**
0433  * tee_client_invoke_func() - Invoke a function in a Trusted Application
0434  * @ctx:    TEE Context
0435  * @arg:    Invoke arguments, see description of
0436  *      struct tee_ioctl_invoke_arg
0437  * @param:  Parameters passed to the Trusted Application
0438  *
0439  * Returns < 0 on error else see @arg->ret for result.
0440  */
0441 int tee_client_invoke_func(struct tee_context *ctx,
0442                struct tee_ioctl_invoke_arg *arg,
0443                struct tee_param *param);
0444 
0445 /**
0446  * tee_client_cancel_req() - Request cancellation of the previous open-session
0447  * or invoke-command operations in a Trusted Application
0448  * @ctx:       TEE Context
0449  * @arg:       Cancellation arguments, see description of
0450  *             struct tee_ioctl_cancel_arg
0451  *
0452  * Returns < 0 on error else 0 if the cancellation was successfully requested.
0453  */
0454 int tee_client_cancel_req(struct tee_context *ctx,
0455               struct tee_ioctl_cancel_arg *arg);
0456 
0457 static inline bool tee_param_is_memref(struct tee_param *param)
0458 {
0459     switch (param->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
0460     case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
0461     case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
0462     case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
0463         return true;
0464     default:
0465         return false;
0466     }
0467 }
0468 
0469 extern struct bus_type tee_bus_type;
0470 
0471 /**
0472  * struct tee_client_device - tee based device
0473  * @id:         device identifier
0474  * @dev:        device structure
0475  */
0476 struct tee_client_device {
0477     struct tee_client_device_id id;
0478     struct device dev;
0479 };
0480 
0481 #define to_tee_client_device(d) container_of(d, struct tee_client_device, dev)
0482 
0483 /**
0484  * struct tee_client_driver - tee client driver
0485  * @id_table:       device id table supported by this driver
0486  * @driver:     driver structure
0487  */
0488 struct tee_client_driver {
0489     const struct tee_client_device_id *id_table;
0490     struct device_driver driver;
0491 };
0492 
0493 #define to_tee_client_driver(d) \
0494         container_of(d, struct tee_client_driver, driver)
0495 
0496 /**
0497  * teedev_open() - Open a struct tee_device
0498  * @teedev: Device to open
0499  *
0500  * @return a pointer to struct tee_context on success or an ERR_PTR on failure.
0501  */
0502 struct tee_context *teedev_open(struct tee_device *teedev);
0503 
0504 /**
0505  * teedev_close_context() - closes a struct tee_context
0506  * @ctx:    The struct tee_context to close
0507  */
0508 void teedev_close_context(struct tee_context *ctx);
0509 
0510 #endif /*__TEE_DRV_H*/