^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: MIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * AMD Trusted Execution Environment (TEE) interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Author: Rijo Thomas <Rijo-john.Thomas@amd.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Devaraj Rangasamy <Devaraj.Rangasamy@amd.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright 2019 Advanced Micro Devices, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/psp-sev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/psp-tee.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "psp-dev.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "tee-dev.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static bool psp_dead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int tee_alloc_ring(struct psp_tee_device *tee, int ring_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct ring_buf_manager *rb_mgr = &tee->rb_mgr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) void *start_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (!ring_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* We need actual physical address instead of DMA address, since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Trusted OS running on AMD Secure Processor will map this region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) start_addr = (void *)__get_free_pages(GFP_KERNEL, get_order(ring_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (!start_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) memset(start_addr, 0x0, ring_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) rb_mgr->ring_start = start_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) rb_mgr->ring_size = ring_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) rb_mgr->ring_pa = __psp_pa(start_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) mutex_init(&rb_mgr->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static void tee_free_ring(struct psp_tee_device *tee)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct ring_buf_manager *rb_mgr = &tee->rb_mgr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!rb_mgr->ring_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) free_pages((unsigned long)rb_mgr->ring_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) get_order(rb_mgr->ring_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) rb_mgr->ring_start = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) rb_mgr->ring_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) rb_mgr->ring_pa = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) mutex_destroy(&rb_mgr->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int tee_wait_cmd_poll(struct psp_tee_device *tee, unsigned int timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned int *reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /* ~10ms sleep per loop => nloop = timeout * 100 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int nloop = timeout * 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) while (--nloop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) *reg = ioread32(tee->io_regs + tee->vdata->cmdresp_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (*reg & PSP_CMDRESP_RESP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) usleep_range(10000, 10100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) dev_err(tee->dev, "tee: command timed out, disabling PSP\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) psp_dead = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct tee_init_ring_cmd *tee_alloc_cmd_buffer(struct psp_tee_device *tee)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct tee_init_ring_cmd *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) cmd->hi_addr = upper_32_bits(tee->rb_mgr.ring_pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) cmd->low_addr = lower_32_bits(tee->rb_mgr.ring_pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) cmd->size = tee->rb_mgr.ring_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) dev_dbg(tee->dev, "tee: ring address: high = 0x%x low = 0x%x size = %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) cmd->hi_addr, cmd->low_addr, cmd->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static inline void tee_free_cmd_buffer(struct tee_init_ring_cmd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) kfree(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int tee_init_ring(struct psp_tee_device *tee)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int ring_size = MAX_RING_BUFFER_ENTRIES * sizeof(struct tee_ring_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct tee_init_ring_cmd *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) phys_addr_t cmd_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) unsigned int reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) BUILD_BUG_ON(sizeof(struct tee_ring_cmd) != 1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ret = tee_alloc_ring(tee, ring_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) dev_err(tee->dev, "tee: ring allocation failed %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) tee->rb_mgr.wptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) cmd = tee_alloc_cmd_buffer(tee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (!cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) tee_free_ring(tee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) cmd_buffer = __psp_pa((void *)cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /* Send command buffer details to Trusted OS by writing to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * CPU-PSP message registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) iowrite32(lower_32_bits(cmd_buffer),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) tee->io_regs + tee->vdata->cmdbuff_addr_lo_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) iowrite32(upper_32_bits(cmd_buffer),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) tee->io_regs + tee->vdata->cmdbuff_addr_hi_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) iowrite32(TEE_RING_INIT_CMD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) tee->io_regs + tee->vdata->cmdresp_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) ret = tee_wait_cmd_poll(tee, TEE_DEFAULT_TIMEOUT, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) dev_err(tee->dev, "tee: ring init command timed out\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) tee_free_ring(tee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) goto free_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (reg & PSP_CMDRESP_ERR_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) dev_err(tee->dev, "tee: ring init command failed (%#010x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) reg & PSP_CMDRESP_ERR_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) tee_free_ring(tee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) free_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) tee_free_cmd_buffer(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static void tee_destroy_ring(struct psp_tee_device *tee)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) unsigned int reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (!tee->rb_mgr.ring_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (psp_dead)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) goto free_ring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) iowrite32(TEE_RING_DESTROY_CMD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) tee->io_regs + tee->vdata->cmdresp_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ret = tee_wait_cmd_poll(tee, TEE_DEFAULT_TIMEOUT, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) dev_err(tee->dev, "tee: ring destroy command timed out\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) } else if (reg & PSP_CMDRESP_ERR_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) dev_err(tee->dev, "tee: ring destroy command failed (%#010x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) reg & PSP_CMDRESP_ERR_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) free_ring:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) tee_free_ring(tee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int tee_dev_init(struct psp_device *psp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct device *dev = psp->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct psp_tee_device *tee;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) tee = devm_kzalloc(dev, sizeof(*tee), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (!tee)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) goto e_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) psp->tee_data = tee;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) tee->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) tee->psp = psp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) tee->io_regs = psp->io_regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) tee->vdata = (struct tee_vdata *)psp->vdata->tee;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (!tee->vdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) dev_err(dev, "tee: missing driver data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) goto e_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) ret = tee_init_ring(tee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) dev_err(dev, "tee: failed to init ring buffer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) goto e_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) dev_notice(dev, "tee enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) e_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) psp->tee_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) dev_notice(dev, "tee initialization failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) void tee_dev_destroy(struct psp_device *psp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct psp_tee_device *tee = psp->tee_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (!tee)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) tee_destroy_ring(tee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) static int tee_submit_cmd(struct psp_tee_device *tee, enum tee_cmd_id cmd_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) void *buf, size_t len, struct tee_ring_cmd **resp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) struct tee_ring_cmd *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) int nloop = 1000, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) u32 rptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) *resp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) mutex_lock(&tee->rb_mgr.mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /* Loop until empty entry found in ring buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* Get pointer to ring buffer command entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) cmd = (struct tee_ring_cmd *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) (tee->rb_mgr.ring_start + tee->rb_mgr.wptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) rptr = ioread32(tee->io_regs + tee->vdata->ring_rptr_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /* Check if ring buffer is full or command entry is waiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * for response from TEE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (!(tee->rb_mgr.wptr + sizeof(struct tee_ring_cmd) == rptr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) cmd->flag == CMD_WAITING_FOR_RESPONSE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) dev_dbg(tee->dev, "tee: ring buffer full. rptr = %u wptr = %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) rptr, tee->rb_mgr.wptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /* Wait if ring buffer is full or TEE is processing data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) mutex_unlock(&tee->rb_mgr.mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) schedule_timeout_interruptible(msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) mutex_lock(&tee->rb_mgr.mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) } while (--nloop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (!nloop &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) (tee->rb_mgr.wptr + sizeof(struct tee_ring_cmd) == rptr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) cmd->flag == CMD_WAITING_FOR_RESPONSE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) dev_err(tee->dev, "tee: ring buffer full. rptr = %u wptr = %u response flag %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) rptr, tee->rb_mgr.wptr, cmd->flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) /* Do not submit command if PSP got disabled while processing any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * command in another thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (psp_dead) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /* Write command data into ring buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) cmd->cmd_id = cmd_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) cmd->cmd_state = TEE_CMD_STATE_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) memset(&cmd->buf[0], 0, sizeof(cmd->buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) memcpy(&cmd->buf[0], buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /* Indicate driver is waiting for response */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) cmd->flag = CMD_WAITING_FOR_RESPONSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /* Update local copy of write pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) tee->rb_mgr.wptr += sizeof(struct tee_ring_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (tee->rb_mgr.wptr >= tee->rb_mgr.ring_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) tee->rb_mgr.wptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /* Trigger interrupt to Trusted OS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) iowrite32(tee->rb_mgr.wptr, tee->io_regs + tee->vdata->ring_wptr_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /* The response is provided by Trusted OS in same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * location as submitted data entry within ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) *resp = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) mutex_unlock(&tee->rb_mgr.mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static int tee_wait_cmd_completion(struct psp_tee_device *tee,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct tee_ring_cmd *resp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) unsigned int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /* ~5ms sleep per loop => nloop = timeout * 200 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) int nloop = timeout * 200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) while (--nloop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (resp->cmd_state == TEE_CMD_STATE_COMPLETED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) usleep_range(5000, 5100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) dev_err(tee->dev, "tee: command 0x%x timed out, disabling PSP\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) resp->cmd_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) psp_dead = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) int psp_tee_process_cmd(enum tee_cmd_id cmd_id, void *buf, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) u32 *status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct psp_device *psp = psp_get_master_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) struct psp_tee_device *tee;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct tee_ring_cmd *resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (!buf || !status || !len || len > sizeof(resp->buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) *status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (!psp || !psp->tee_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (psp_dead)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) tee = psp->tee_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) ret = tee_submit_cmd(tee, cmd_id, buf, len, &resp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) ret = tee_wait_cmd_completion(tee, resp, TEE_DEFAULT_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) resp->flag = CMD_RESPONSE_TIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) memcpy(buf, &resp->buf[0], len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) *status = resp->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) resp->flag = CMD_RESPONSE_COPIED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) EXPORT_SYMBOL(psp_tee_process_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) int psp_check_tee_status(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) struct psp_device *psp = psp_get_master_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (!psp || !psp->tee_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) EXPORT_SYMBOL(psp_check_tee_status);