1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2ea0375afSGary R Hook /* 3ea0375afSGary R Hook * AMD Cryptographic Coprocessor (CCP) driver 4ea0375afSGary R Hook * 568cc652fSGary R Hook * Copyright (C) 2013,2017 Advanced Micro Devices, Inc. 6ea0375afSGary R Hook * 7ea0375afSGary R Hook * Author: Tom Lendacky <thomas.lendacky@amd.com> 8fba8855cSGary R Hook * Author: Gary R Hook <gary.hook@amd.com> 9ea0375afSGary R Hook */ 10ea0375afSGary R Hook 11ea0375afSGary R Hook #include <linux/module.h> 12ea0375afSGary R Hook #include <linux/kernel.h> 13ea0375afSGary R Hook #include <linux/pci.h> 14ea0375afSGary R Hook #include <linux/kthread.h> 15ea0375afSGary R Hook #include <linux/interrupt.h> 16ea0375afSGary R Hook #include <linux/ccp.h> 17ea0375afSGary R Hook 18ea0375afSGary R Hook #include "ccp-dev.h" 19ea0375afSGary R Hook 2058a690b7SGary R Hook static u32 ccp_alloc_ksb(struct ccp_cmd_queue *cmd_q, unsigned int count) 2158a690b7SGary R Hook { 2258a690b7SGary R Hook int start; 2358a690b7SGary R Hook struct ccp_device *ccp = cmd_q->ccp; 2458a690b7SGary R Hook 2558a690b7SGary R Hook for (;;) { 2658a690b7SGary R Hook mutex_lock(&ccp->sb_mutex); 2758a690b7SGary R Hook 2858a690b7SGary R Hook start = (u32)bitmap_find_next_zero_area(ccp->sb, 2958a690b7SGary R Hook ccp->sb_count, 3058a690b7SGary R Hook ccp->sb_start, 3158a690b7SGary R Hook count, 0); 3258a690b7SGary R Hook if (start <= ccp->sb_count) { 3358a690b7SGary R Hook bitmap_set(ccp->sb, start, count); 3458a690b7SGary R Hook 3558a690b7SGary R Hook mutex_unlock(&ccp->sb_mutex); 3658a690b7SGary R Hook break; 3758a690b7SGary R Hook } 3858a690b7SGary R Hook 3958a690b7SGary R Hook ccp->sb_avail = 0; 4058a690b7SGary R Hook 4158a690b7SGary R Hook mutex_unlock(&ccp->sb_mutex); 4258a690b7SGary R Hook 4358a690b7SGary R Hook /* Wait for KSB entries to become available */ 4458a690b7SGary R Hook if (wait_event_interruptible(ccp->sb_queue, ccp->sb_avail)) 4558a690b7SGary R Hook return 0; 4658a690b7SGary R Hook } 4758a690b7SGary R Hook 4858a690b7SGary R Hook return KSB_START + start; 4958a690b7SGary R Hook } 5058a690b7SGary R Hook 5158a690b7SGary R Hook static void ccp_free_ksb(struct ccp_cmd_queue *cmd_q, unsigned int start, 5258a690b7SGary R Hook unsigned int count) 5358a690b7SGary R Hook { 5458a690b7SGary R Hook struct ccp_device *ccp = cmd_q->ccp; 5558a690b7SGary R Hook 5658a690b7SGary R Hook if (!start) 5758a690b7SGary R Hook return; 5858a690b7SGary R Hook 5958a690b7SGary R Hook mutex_lock(&ccp->sb_mutex); 6058a690b7SGary R Hook 6158a690b7SGary R Hook bitmap_clear(ccp->sb, start - KSB_START, count); 6258a690b7SGary R Hook 6358a690b7SGary R Hook ccp->sb_avail = 1; 6458a690b7SGary R Hook 6558a690b7SGary R Hook mutex_unlock(&ccp->sb_mutex); 6658a690b7SGary R Hook 6758a690b7SGary R Hook wake_up_interruptible_all(&ccp->sb_queue); 6858a690b7SGary R Hook } 6958a690b7SGary R Hook 70bb4e89b3SGary R Hook static unsigned int ccp_get_free_slots(struct ccp_cmd_queue *cmd_q) 71bb4e89b3SGary R Hook { 72bb4e89b3SGary R Hook return CMD_Q_DEPTH(ioread32(cmd_q->reg_status)); 73bb4e89b3SGary R Hook } 74bb4e89b3SGary R Hook 75ea0375afSGary R Hook static int ccp_do_cmd(struct ccp_op *op, u32 *cr, unsigned int cr_count) 76ea0375afSGary R Hook { 77ea0375afSGary R Hook struct ccp_cmd_queue *cmd_q = op->cmd_q; 78ea0375afSGary R Hook struct ccp_device *ccp = cmd_q->ccp; 79ea0375afSGary R Hook void __iomem *cr_addr; 80ea0375afSGary R Hook u32 cr0, cmd; 81ea0375afSGary R Hook unsigned int i; 82ea0375afSGary R Hook int ret = 0; 83ea0375afSGary R Hook 84ea0375afSGary R Hook /* We could read a status register to see how many free slots 85ea0375afSGary R Hook * are actually available, but reading that register resets it 86ea0375afSGary R Hook * and you could lose some error information. 87ea0375afSGary R Hook */ 88ea0375afSGary R Hook cmd_q->free_slots--; 89ea0375afSGary R Hook 90ea0375afSGary R Hook cr0 = (cmd_q->id << REQ0_CMD_Q_SHIFT) 91ea0375afSGary R Hook | (op->jobid << REQ0_JOBID_SHIFT) 92ea0375afSGary R Hook | REQ0_WAIT_FOR_WRITE; 93ea0375afSGary R Hook 94ea0375afSGary R Hook if (op->soc) 95ea0375afSGary R Hook cr0 |= REQ0_STOP_ON_COMPLETE 96ea0375afSGary R Hook | REQ0_INT_ON_COMPLETE; 97ea0375afSGary R Hook 98ea0375afSGary R Hook if (op->ioc || !cmd_q->free_slots) 99ea0375afSGary R Hook cr0 |= REQ0_INT_ON_COMPLETE; 100ea0375afSGary R Hook 101ea0375afSGary R Hook /* Start at CMD_REQ1 */ 102ea0375afSGary R Hook cr_addr = ccp->io_regs + CMD_REQ0 + CMD_REQ_INCR; 103ea0375afSGary R Hook 104ea0375afSGary R Hook mutex_lock(&ccp->req_mutex); 105ea0375afSGary R Hook 106ea0375afSGary R Hook /* Write CMD_REQ1 through CMD_REQx first */ 107ea0375afSGary R Hook for (i = 0; i < cr_count; i++, cr_addr += CMD_REQ_INCR) 108ea0375afSGary R Hook iowrite32(*(cr + i), cr_addr); 109ea0375afSGary R Hook 110ea0375afSGary R Hook /* Tell the CCP to start */ 111ea0375afSGary R Hook wmb(); 112ea0375afSGary R Hook iowrite32(cr0, ccp->io_regs + CMD_REQ0); 113ea0375afSGary R Hook 114ea0375afSGary R Hook mutex_unlock(&ccp->req_mutex); 115ea0375afSGary R Hook 116ea0375afSGary R Hook if (cr0 & REQ0_INT_ON_COMPLETE) { 117ea0375afSGary R Hook /* Wait for the job to complete */ 118ea0375afSGary R Hook ret = wait_event_interruptible(cmd_q->int_queue, 119ea0375afSGary R Hook cmd_q->int_rcvd); 120ea0375afSGary R Hook if (ret || cmd_q->cmd_error) { 121ea0375afSGary R Hook /* On error delete all related jobs from the queue */ 122ea0375afSGary R Hook cmd = (cmd_q->id << DEL_Q_ID_SHIFT) 123ea0375afSGary R Hook | op->jobid; 12481422badSGary R Hook if (cmd_q->cmd_error) 12581422badSGary R Hook ccp_log_error(cmd_q->ccp, 12681422badSGary R Hook cmd_q->cmd_error); 127ea0375afSGary R Hook 128ea0375afSGary R Hook iowrite32(cmd, ccp->io_regs + DEL_CMD_Q_JOB); 129ea0375afSGary R Hook 130ea0375afSGary R Hook if (!ret) 131ea0375afSGary R Hook ret = -EIO; 132ea0375afSGary R Hook } else if (op->soc) { 133ea0375afSGary R Hook /* Delete just head job from the queue on SoC */ 134ea0375afSGary R Hook cmd = DEL_Q_ACTIVE 135ea0375afSGary R Hook | (cmd_q->id << DEL_Q_ID_SHIFT) 136ea0375afSGary R Hook | op->jobid; 137ea0375afSGary R Hook 138ea0375afSGary R Hook iowrite32(cmd, ccp->io_regs + DEL_CMD_Q_JOB); 139ea0375afSGary R Hook } 140ea0375afSGary R Hook 141ea0375afSGary R Hook cmd_q->free_slots = CMD_Q_DEPTH(cmd_q->q_status); 142ea0375afSGary R Hook 143ea0375afSGary R Hook cmd_q->int_rcvd = 0; 144ea0375afSGary R Hook } 145ea0375afSGary R Hook 146ea0375afSGary R Hook return ret; 147ea0375afSGary R Hook } 148ea0375afSGary R Hook 149ea0375afSGary R Hook static int ccp_perform_aes(struct ccp_op *op) 150ea0375afSGary R Hook { 151ea0375afSGary R Hook u32 cr[6]; 152ea0375afSGary R Hook 153ea0375afSGary R Hook /* Fill out the register contents for REQ1 through REQ6 */ 154ea0375afSGary R Hook cr[0] = (CCP_ENGINE_AES << REQ1_ENGINE_SHIFT) 155ea0375afSGary R Hook | (op->u.aes.type << REQ1_AES_TYPE_SHIFT) 156ea0375afSGary R Hook | (op->u.aes.mode << REQ1_AES_MODE_SHIFT) 157ea0375afSGary R Hook | (op->u.aes.action << REQ1_AES_ACTION_SHIFT) 158956ee21aSGary R Hook | (op->sb_key << REQ1_KEY_KSB_SHIFT); 159ea0375afSGary R Hook cr[1] = op->src.u.dma.length - 1; 160ea0375afSGary R Hook cr[2] = ccp_addr_lo(&op->src.u.dma); 161956ee21aSGary R Hook cr[3] = (op->sb_ctx << REQ4_KSB_SHIFT) 162ea0375afSGary R Hook | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 163ea0375afSGary R Hook | ccp_addr_hi(&op->src.u.dma); 164ea0375afSGary R Hook cr[4] = ccp_addr_lo(&op->dst.u.dma); 165ea0375afSGary R Hook cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 166ea0375afSGary R Hook | ccp_addr_hi(&op->dst.u.dma); 167ea0375afSGary R Hook 168ea0375afSGary R Hook if (op->u.aes.mode == CCP_AES_MODE_CFB) 169ea0375afSGary R Hook cr[0] |= ((0x7f) << REQ1_AES_CFB_SIZE_SHIFT); 170ea0375afSGary R Hook 171ea0375afSGary R Hook if (op->eom) 172ea0375afSGary R Hook cr[0] |= REQ1_EOM; 173ea0375afSGary R Hook 174ea0375afSGary R Hook if (op->init) 175ea0375afSGary R Hook cr[0] |= REQ1_INIT; 176ea0375afSGary R Hook 177ea0375afSGary R Hook return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 178ea0375afSGary R Hook } 179ea0375afSGary R Hook 180ea0375afSGary R Hook static int ccp_perform_xts_aes(struct ccp_op *op) 181ea0375afSGary R Hook { 182ea0375afSGary R Hook u32 cr[6]; 183ea0375afSGary R Hook 184ea0375afSGary R Hook /* Fill out the register contents for REQ1 through REQ6 */ 185ea0375afSGary R Hook cr[0] = (CCP_ENGINE_XTS_AES_128 << REQ1_ENGINE_SHIFT) 186ea0375afSGary R Hook | (op->u.xts.action << REQ1_AES_ACTION_SHIFT) 187ea0375afSGary R Hook | (op->u.xts.unit_size << REQ1_XTS_AES_SIZE_SHIFT) 188956ee21aSGary R Hook | (op->sb_key << REQ1_KEY_KSB_SHIFT); 189ea0375afSGary R Hook cr[1] = op->src.u.dma.length - 1; 190ea0375afSGary R Hook cr[2] = ccp_addr_lo(&op->src.u.dma); 191956ee21aSGary R Hook cr[3] = (op->sb_ctx << REQ4_KSB_SHIFT) 192ea0375afSGary R Hook | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 193ea0375afSGary R Hook | ccp_addr_hi(&op->src.u.dma); 194ea0375afSGary R Hook cr[4] = ccp_addr_lo(&op->dst.u.dma); 195ea0375afSGary R Hook cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 196ea0375afSGary R Hook | ccp_addr_hi(&op->dst.u.dma); 197ea0375afSGary R Hook 198ea0375afSGary R Hook if (op->eom) 199ea0375afSGary R Hook cr[0] |= REQ1_EOM; 200ea0375afSGary R Hook 201ea0375afSGary R Hook if (op->init) 202ea0375afSGary R Hook cr[0] |= REQ1_INIT; 203ea0375afSGary R Hook 204ea0375afSGary R Hook return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 205ea0375afSGary R Hook } 206ea0375afSGary R Hook 207ea0375afSGary R Hook static int ccp_perform_sha(struct ccp_op *op) 208ea0375afSGary R Hook { 209ea0375afSGary R Hook u32 cr[6]; 210ea0375afSGary R Hook 211ea0375afSGary R Hook /* Fill out the register contents for REQ1 through REQ6 */ 212ea0375afSGary R Hook cr[0] = (CCP_ENGINE_SHA << REQ1_ENGINE_SHIFT) 213ea0375afSGary R Hook | (op->u.sha.type << REQ1_SHA_TYPE_SHIFT) 214ea0375afSGary R Hook | REQ1_INIT; 215ea0375afSGary R Hook cr[1] = op->src.u.dma.length - 1; 216ea0375afSGary R Hook cr[2] = ccp_addr_lo(&op->src.u.dma); 217956ee21aSGary R Hook cr[3] = (op->sb_ctx << REQ4_KSB_SHIFT) 218ea0375afSGary R Hook | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 219ea0375afSGary R Hook | ccp_addr_hi(&op->src.u.dma); 220ea0375afSGary R Hook 221ea0375afSGary R Hook if (op->eom) { 222ea0375afSGary R Hook cr[0] |= REQ1_EOM; 223ea0375afSGary R Hook cr[4] = lower_32_bits(op->u.sha.msg_bits); 224ea0375afSGary R Hook cr[5] = upper_32_bits(op->u.sha.msg_bits); 225ea0375afSGary R Hook } else { 226ea0375afSGary R Hook cr[4] = 0; 227ea0375afSGary R Hook cr[5] = 0; 228ea0375afSGary R Hook } 229ea0375afSGary R Hook 230ea0375afSGary R Hook return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 231ea0375afSGary R Hook } 232ea0375afSGary R Hook 233ea0375afSGary R Hook static int ccp_perform_rsa(struct ccp_op *op) 234ea0375afSGary R Hook { 235ea0375afSGary R Hook u32 cr[6]; 236ea0375afSGary R Hook 237ea0375afSGary R Hook /* Fill out the register contents for REQ1 through REQ6 */ 238ea0375afSGary R Hook cr[0] = (CCP_ENGINE_RSA << REQ1_ENGINE_SHIFT) 239ea0375afSGary R Hook | (op->u.rsa.mod_size << REQ1_RSA_MOD_SIZE_SHIFT) 240956ee21aSGary R Hook | (op->sb_key << REQ1_KEY_KSB_SHIFT) 241ea0375afSGary R Hook | REQ1_EOM; 242ea0375afSGary R Hook cr[1] = op->u.rsa.input_len - 1; 243ea0375afSGary R Hook cr[2] = ccp_addr_lo(&op->src.u.dma); 244956ee21aSGary R Hook cr[3] = (op->sb_ctx << REQ4_KSB_SHIFT) 245ea0375afSGary R Hook | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 246ea0375afSGary R Hook | ccp_addr_hi(&op->src.u.dma); 247ea0375afSGary R Hook cr[4] = ccp_addr_lo(&op->dst.u.dma); 248ea0375afSGary R Hook cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 249ea0375afSGary R Hook | ccp_addr_hi(&op->dst.u.dma); 250ea0375afSGary R Hook 251ea0375afSGary R Hook return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 252ea0375afSGary R Hook } 253ea0375afSGary R Hook 254ea0375afSGary R Hook static int ccp_perform_passthru(struct ccp_op *op) 255ea0375afSGary R Hook { 256ea0375afSGary R Hook u32 cr[6]; 257ea0375afSGary R Hook 258ea0375afSGary R Hook /* Fill out the register contents for REQ1 through REQ6 */ 259ea0375afSGary R Hook cr[0] = (CCP_ENGINE_PASSTHRU << REQ1_ENGINE_SHIFT) 260ea0375afSGary R Hook | (op->u.passthru.bit_mod << REQ1_PT_BW_SHIFT) 261ea0375afSGary R Hook | (op->u.passthru.byte_swap << REQ1_PT_BS_SHIFT); 262ea0375afSGary R Hook 263ea0375afSGary R Hook if (op->src.type == CCP_MEMTYPE_SYSTEM) 264ea0375afSGary R Hook cr[1] = op->src.u.dma.length - 1; 265ea0375afSGary R Hook else 266ea0375afSGary R Hook cr[1] = op->dst.u.dma.length - 1; 267ea0375afSGary R Hook 268ea0375afSGary R Hook if (op->src.type == CCP_MEMTYPE_SYSTEM) { 269ea0375afSGary R Hook cr[2] = ccp_addr_lo(&op->src.u.dma); 270ea0375afSGary R Hook cr[3] = (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 271ea0375afSGary R Hook | ccp_addr_hi(&op->src.u.dma); 272ea0375afSGary R Hook 273ea0375afSGary R Hook if (op->u.passthru.bit_mod != CCP_PASSTHRU_BITWISE_NOOP) 274956ee21aSGary R Hook cr[3] |= (op->sb_key << REQ4_KSB_SHIFT); 275ea0375afSGary R Hook } else { 276956ee21aSGary R Hook cr[2] = op->src.u.sb * CCP_SB_BYTES; 277956ee21aSGary R Hook cr[3] = (CCP_MEMTYPE_SB << REQ4_MEMTYPE_SHIFT); 278ea0375afSGary R Hook } 279ea0375afSGary R Hook 280ea0375afSGary R Hook if (op->dst.type == CCP_MEMTYPE_SYSTEM) { 281ea0375afSGary R Hook cr[4] = ccp_addr_lo(&op->dst.u.dma); 282ea0375afSGary R Hook cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 283ea0375afSGary R Hook | ccp_addr_hi(&op->dst.u.dma); 284ea0375afSGary R Hook } else { 285956ee21aSGary R Hook cr[4] = op->dst.u.sb * CCP_SB_BYTES; 286956ee21aSGary R Hook cr[5] = (CCP_MEMTYPE_SB << REQ6_MEMTYPE_SHIFT); 287ea0375afSGary R Hook } 288ea0375afSGary R Hook 289ea0375afSGary R Hook if (op->eom) 290ea0375afSGary R Hook cr[0] |= REQ1_EOM; 291ea0375afSGary R Hook 292ea0375afSGary R Hook return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 293ea0375afSGary R Hook } 294ea0375afSGary R Hook 295ea0375afSGary R Hook static int ccp_perform_ecc(struct ccp_op *op) 296ea0375afSGary R Hook { 297ea0375afSGary R Hook u32 cr[6]; 298ea0375afSGary R Hook 299ea0375afSGary R Hook /* Fill out the register contents for REQ1 through REQ6 */ 300ea0375afSGary R Hook cr[0] = REQ1_ECC_AFFINE_CONVERT 301ea0375afSGary R Hook | (CCP_ENGINE_ECC << REQ1_ENGINE_SHIFT) 302ea0375afSGary R Hook | (op->u.ecc.function << REQ1_ECC_FUNCTION_SHIFT) 303ea0375afSGary R Hook | REQ1_EOM; 304ea0375afSGary R Hook cr[1] = op->src.u.dma.length - 1; 305ea0375afSGary R Hook cr[2] = ccp_addr_lo(&op->src.u.dma); 306ea0375afSGary R Hook cr[3] = (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT) 307ea0375afSGary R Hook | ccp_addr_hi(&op->src.u.dma); 308ea0375afSGary R Hook cr[4] = ccp_addr_lo(&op->dst.u.dma); 309ea0375afSGary R Hook cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT) 310ea0375afSGary R Hook | ccp_addr_hi(&op->dst.u.dma); 311ea0375afSGary R Hook 312ea0375afSGary R Hook return ccp_do_cmd(op, cr, ARRAY_SIZE(cr)); 313ea0375afSGary R Hook } 314ea0375afSGary R Hook 3157b537b24SGary R Hook static void ccp_disable_queue_interrupts(struct ccp_device *ccp) 3167b537b24SGary R Hook { 3177b537b24SGary R Hook iowrite32(0x00, ccp->io_regs + IRQ_MASK_REG); 3187b537b24SGary R Hook } 3197b537b24SGary R Hook 3207b537b24SGary R Hook static void ccp_enable_queue_interrupts(struct ccp_device *ccp) 3217b537b24SGary R Hook { 3227b537b24SGary R Hook iowrite32(ccp->qim, ccp->io_regs + IRQ_MASK_REG); 3237b537b24SGary R Hook } 3247b537b24SGary R Hook 3257b537b24SGary R Hook static void ccp_irq_bh(unsigned long data) 3267b537b24SGary R Hook { 3277b537b24SGary R Hook struct ccp_device *ccp = (struct ccp_device *)data; 3287b537b24SGary R Hook struct ccp_cmd_queue *cmd_q; 3297b537b24SGary R Hook u32 q_int, status; 3307b537b24SGary R Hook unsigned int i; 3317b537b24SGary R Hook 3327b537b24SGary R Hook status = ioread32(ccp->io_regs + IRQ_STATUS_REG); 3337b537b24SGary R Hook 3347b537b24SGary R Hook for (i = 0; i < ccp->cmd_q_count; i++) { 3357b537b24SGary R Hook cmd_q = &ccp->cmd_q[i]; 3367b537b24SGary R Hook 3377b537b24SGary R Hook q_int = status & (cmd_q->int_ok | cmd_q->int_err); 3387b537b24SGary R Hook if (q_int) { 3397b537b24SGary R Hook cmd_q->int_status = status; 3407b537b24SGary R Hook cmd_q->q_status = ioread32(cmd_q->reg_status); 3417b537b24SGary R Hook cmd_q->q_int_status = ioread32(cmd_q->reg_int_status); 3427b537b24SGary R Hook 3437b537b24SGary R Hook /* On error, only save the first error value */ 3447b537b24SGary R Hook if ((q_int & cmd_q->int_err) && !cmd_q->cmd_error) 3457b537b24SGary R Hook cmd_q->cmd_error = CMD_Q_ERROR(cmd_q->q_status); 3467b537b24SGary R Hook 3477b537b24SGary R Hook cmd_q->int_rcvd = 1; 3487b537b24SGary R Hook 3497b537b24SGary R Hook /* Acknowledge the interrupt and wake the kthread */ 3507b537b24SGary R Hook iowrite32(q_int, ccp->io_regs + IRQ_STATUS_REG); 3517b537b24SGary R Hook wake_up_interruptible(&cmd_q->int_queue); 3527b537b24SGary R Hook } 3537b537b24SGary R Hook } 3547b537b24SGary R Hook ccp_enable_queue_interrupts(ccp); 3557b537b24SGary R Hook } 3567b537b24SGary R Hook 3577b537b24SGary R Hook static irqreturn_t ccp_irq_handler(int irq, void *data) 3587b537b24SGary R Hook { 359720419f0SBrijesh Singh struct ccp_device *ccp = (struct ccp_device *)data; 3607b537b24SGary R Hook 3617b537b24SGary R Hook ccp_disable_queue_interrupts(ccp); 3627b537b24SGary R Hook if (ccp->use_tasklet) 3637b537b24SGary R Hook tasklet_schedule(&ccp->irq_tasklet); 3647b537b24SGary R Hook else 3657b537b24SGary R Hook ccp_irq_bh((unsigned long)ccp); 3667b537b24SGary R Hook 3677b537b24SGary R Hook return IRQ_HANDLED; 3687b537b24SGary R Hook } 3697b537b24SGary R Hook 370ea0375afSGary R Hook static int ccp_init(struct ccp_device *ccp) 371ea0375afSGary R Hook { 372ea0375afSGary R Hook struct device *dev = ccp->dev; 373ea0375afSGary R Hook struct ccp_cmd_queue *cmd_q; 374ea0375afSGary R Hook struct dma_pool *dma_pool; 375ea0375afSGary R Hook char dma_pool_name[MAX_DMAPOOL_NAME_LEN]; 3767b537b24SGary R Hook unsigned int qmr, i; 377ea0375afSGary R Hook int ret; 378ea0375afSGary R Hook 379ea0375afSGary R Hook /* Find available queues */ 3807b537b24SGary R Hook ccp->qim = 0; 381ea0375afSGary R Hook qmr = ioread32(ccp->io_regs + Q_MASK_REG); 382ea0375afSGary R Hook for (i = 0; i < MAX_HW_QUEUES; i++) { 383ea0375afSGary R Hook if (!(qmr & (1 << i))) 384ea0375afSGary R Hook continue; 385ea0375afSGary R Hook 386ea0375afSGary R Hook /* Allocate a dma pool for this queue */ 387ea0375afSGary R Hook snprintf(dma_pool_name, sizeof(dma_pool_name), "%s_q%d", 388ea0375afSGary R Hook ccp->name, i); 389ea0375afSGary R Hook dma_pool = dma_pool_create(dma_pool_name, dev, 390ea0375afSGary R Hook CCP_DMAPOOL_MAX_SIZE, 391ea0375afSGary R Hook CCP_DMAPOOL_ALIGN, 0); 392ea0375afSGary R Hook if (!dma_pool) { 393ea0375afSGary R Hook dev_err(dev, "unable to allocate dma pool\n"); 394ea0375afSGary R Hook ret = -ENOMEM; 395ea0375afSGary R Hook goto e_pool; 396ea0375afSGary R Hook } 397ea0375afSGary R Hook 398ea0375afSGary R Hook cmd_q = &ccp->cmd_q[ccp->cmd_q_count]; 399ea0375afSGary R Hook ccp->cmd_q_count++; 400ea0375afSGary R Hook 401ea0375afSGary R Hook cmd_q->ccp = ccp; 402ea0375afSGary R Hook cmd_q->id = i; 403ea0375afSGary R Hook cmd_q->dma_pool = dma_pool; 404ea0375afSGary R Hook 405ea0375afSGary R Hook /* Reserve 2 KSB regions for the queue */ 406956ee21aSGary R Hook cmd_q->sb_key = KSB_START + ccp->sb_start++; 407956ee21aSGary R Hook cmd_q->sb_ctx = KSB_START + ccp->sb_start++; 408956ee21aSGary R Hook ccp->sb_count -= 2; 409ea0375afSGary R Hook 410ea0375afSGary R Hook /* Preset some register values and masks that are queue 411ea0375afSGary R Hook * number dependent 412ea0375afSGary R Hook */ 413ea0375afSGary R Hook cmd_q->reg_status = ccp->io_regs + CMD_Q_STATUS_BASE + 414ea0375afSGary R Hook (CMD_Q_STATUS_INCR * i); 415ea0375afSGary R Hook cmd_q->reg_int_status = ccp->io_regs + CMD_Q_INT_STATUS_BASE + 416ea0375afSGary R Hook (CMD_Q_STATUS_INCR * i); 417ea0375afSGary R Hook cmd_q->int_ok = 1 << (i * 2); 418ea0375afSGary R Hook cmd_q->int_err = 1 << ((i * 2) + 1); 419ea0375afSGary R Hook 420bb4e89b3SGary R Hook cmd_q->free_slots = ccp_get_free_slots(cmd_q); 421ea0375afSGary R Hook 422ea0375afSGary R Hook init_waitqueue_head(&cmd_q->int_queue); 423ea0375afSGary R Hook 424ea0375afSGary R Hook /* Build queue interrupt mask (two interrupts per queue) */ 4257b537b24SGary R Hook ccp->qim |= cmd_q->int_ok | cmd_q->int_err; 426ea0375afSGary R Hook 427ea0375afSGary R Hook #ifdef CONFIG_ARM64 428ea0375afSGary R Hook /* For arm64 set the recommended queue cache settings */ 429ea0375afSGary R Hook iowrite32(ccp->axcache, ccp->io_regs + CMD_Q_CACHE_BASE + 430ea0375afSGary R Hook (CMD_Q_CACHE_INC * i)); 431ea0375afSGary R Hook #endif 432ea0375afSGary R Hook 433ea0375afSGary R Hook dev_dbg(dev, "queue #%u available\n", i); 434ea0375afSGary R Hook } 435ea0375afSGary R Hook if (ccp->cmd_q_count == 0) { 436ea0375afSGary R Hook dev_notice(dev, "no command queues available\n"); 437ea0375afSGary R Hook ret = -EIO; 438ea0375afSGary R Hook goto e_pool; 439ea0375afSGary R Hook } 440ea0375afSGary R Hook dev_notice(dev, "%u command queues available\n", ccp->cmd_q_count); 441ea0375afSGary R Hook 442ea0375afSGary R Hook /* Disable and clear interrupts until ready */ 4437b537b24SGary R Hook ccp_disable_queue_interrupts(ccp); 444ea0375afSGary R Hook for (i = 0; i < ccp->cmd_q_count; i++) { 445ea0375afSGary R Hook cmd_q = &ccp->cmd_q[i]; 446ea0375afSGary R Hook 447ea0375afSGary R Hook ioread32(cmd_q->reg_int_status); 448ea0375afSGary R Hook ioread32(cmd_q->reg_status); 449ea0375afSGary R Hook } 4507b537b24SGary R Hook iowrite32(ccp->qim, ccp->io_regs + IRQ_STATUS_REG); 451ea0375afSGary R Hook 452ea0375afSGary R Hook /* Request an irq */ 453f4d18d65SBrijesh Singh ret = sp_request_ccp_irq(ccp->sp, ccp_irq_handler, ccp->name, ccp); 454ea0375afSGary R Hook if (ret) { 455ea0375afSGary R Hook dev_err(dev, "unable to allocate an IRQ\n"); 456ea0375afSGary R Hook goto e_pool; 457ea0375afSGary R Hook } 458ea0375afSGary R Hook 4597b537b24SGary R Hook /* Initialize the ISR tasklet? */ 4607b537b24SGary R Hook if (ccp->use_tasklet) 4617b537b24SGary R Hook tasklet_init(&ccp->irq_tasklet, ccp_irq_bh, 4627b537b24SGary R Hook (unsigned long)ccp); 4637b537b24SGary R Hook 4644b394a23SGary R Hook dev_dbg(dev, "Starting threads...\n"); 465ea0375afSGary R Hook /* Create a kthread for each queue */ 466ea0375afSGary R Hook for (i = 0; i < ccp->cmd_q_count; i++) { 467ea0375afSGary R Hook struct task_struct *kthread; 468ea0375afSGary R Hook 469ea0375afSGary R Hook cmd_q = &ccp->cmd_q[i]; 470ea0375afSGary R Hook 471ea0375afSGary R Hook kthread = kthread_create(ccp_cmd_queue_thread, cmd_q, 472ea0375afSGary R Hook "%s-q%u", ccp->name, cmd_q->id); 473ea0375afSGary R Hook if (IS_ERR(kthread)) { 474ea0375afSGary R Hook dev_err(dev, "error creating queue thread (%ld)\n", 475ea0375afSGary R Hook PTR_ERR(kthread)); 476ea0375afSGary R Hook ret = PTR_ERR(kthread); 477ea0375afSGary R Hook goto e_kthread; 478ea0375afSGary R Hook } 479ea0375afSGary R Hook 480ea0375afSGary R Hook cmd_q->kthread = kthread; 481ea0375afSGary R Hook wake_up_process(kthread); 482ea0375afSGary R Hook } 483ea0375afSGary R Hook 4844b394a23SGary R Hook dev_dbg(dev, "Enabling interrupts...\n"); 4854b394a23SGary R Hook /* Enable interrupts */ 4867b537b24SGary R Hook ccp_enable_queue_interrupts(ccp); 4874b394a23SGary R Hook 4884b394a23SGary R Hook dev_dbg(dev, "Registering device...\n"); 4894b394a23SGary R Hook ccp_add_device(ccp); 4904b394a23SGary R Hook 491084935b2SGary R Hook ret = ccp_register_rng(ccp); 492084935b2SGary R Hook if (ret) 493ea0375afSGary R Hook goto e_kthread; 494ea0375afSGary R Hook 49558ea8abfSGary R Hook /* Register the DMA engine support */ 49658ea8abfSGary R Hook ret = ccp_dmaengine_register(ccp); 49758ea8abfSGary R Hook if (ret) 49858ea8abfSGary R Hook goto e_hwrng; 49958ea8abfSGary R Hook 500ea0375afSGary R Hook return 0; 501ea0375afSGary R Hook 50258ea8abfSGary R Hook e_hwrng: 503084935b2SGary R Hook ccp_unregister_rng(ccp); 50458ea8abfSGary R Hook 505ea0375afSGary R Hook e_kthread: 506ea0375afSGary R Hook for (i = 0; i < ccp->cmd_q_count; i++) 507ea0375afSGary R Hook if (ccp->cmd_q[i].kthread) 508ea0375afSGary R Hook kthread_stop(ccp->cmd_q[i].kthread); 509ea0375afSGary R Hook 510f4d18d65SBrijesh Singh sp_free_ccp_irq(ccp->sp, ccp); 511ea0375afSGary R Hook 512ea0375afSGary R Hook e_pool: 513ea0375afSGary R Hook for (i = 0; i < ccp->cmd_q_count; i++) 514ea0375afSGary R Hook dma_pool_destroy(ccp->cmd_q[i].dma_pool); 515ea0375afSGary R Hook 516ea0375afSGary R Hook return ret; 517ea0375afSGary R Hook } 518ea0375afSGary R Hook 519ea0375afSGary R Hook static void ccp_destroy(struct ccp_device *ccp) 520ea0375afSGary R Hook { 521ea0375afSGary R Hook struct ccp_cmd_queue *cmd_q; 522ea0375afSGary R Hook struct ccp_cmd *cmd; 5237b537b24SGary R Hook unsigned int i; 524ea0375afSGary R Hook 5254b394a23SGary R Hook /* Unregister the DMA engine */ 5264b394a23SGary R Hook ccp_dmaengine_unregister(ccp); 5274b394a23SGary R Hook 5284b394a23SGary R Hook /* Unregister the RNG */ 529084935b2SGary R Hook ccp_unregister_rng(ccp); 5304b394a23SGary R Hook 5314b394a23SGary R Hook /* Remove this device from the list of available units */ 532ea0375afSGary R Hook ccp_del_device(ccp); 533ea0375afSGary R Hook 534ea0375afSGary R Hook /* Disable and clear interrupts */ 5357b537b24SGary R Hook ccp_disable_queue_interrupts(ccp); 536ea0375afSGary R Hook for (i = 0; i < ccp->cmd_q_count; i++) { 537ea0375afSGary R Hook cmd_q = &ccp->cmd_q[i]; 538ea0375afSGary R Hook 539ea0375afSGary R Hook ioread32(cmd_q->reg_int_status); 540ea0375afSGary R Hook ioread32(cmd_q->reg_status); 541ea0375afSGary R Hook } 5427b537b24SGary R Hook iowrite32(ccp->qim, ccp->io_regs + IRQ_STATUS_REG); 543ea0375afSGary R Hook 5448256e683SGary R Hook /* Stop the queue kthreads */ 5458256e683SGary R Hook for (i = 0; i < ccp->cmd_q_count; i++) 5468256e683SGary R Hook if (ccp->cmd_q[i].kthread) 5478256e683SGary R Hook kthread_stop(ccp->cmd_q[i].kthread); 5488256e683SGary R Hook 549f4d18d65SBrijesh Singh sp_free_ccp_irq(ccp->sp, ccp); 550ea0375afSGary R Hook 551ea0375afSGary R Hook for (i = 0; i < ccp->cmd_q_count; i++) 552ea0375afSGary R Hook dma_pool_destroy(ccp->cmd_q[i].dma_pool); 553ea0375afSGary R Hook 554ea0375afSGary R Hook /* Flush the cmd and backlog queue */ 555ea0375afSGary R Hook while (!list_empty(&ccp->cmd)) { 556ea0375afSGary R Hook /* Invoke the callback directly with an error code */ 557ea0375afSGary R Hook cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry); 558ea0375afSGary R Hook list_del(&cmd->entry); 559ea0375afSGary R Hook cmd->callback(cmd->data, -ENODEV); 560ea0375afSGary R Hook } 561ea0375afSGary R Hook while (!list_empty(&ccp->backlog)) { 562ea0375afSGary R Hook /* Invoke the callback directly with an error code */ 563ea0375afSGary R Hook cmd = list_first_entry(&ccp->backlog, struct ccp_cmd, entry); 564ea0375afSGary R Hook list_del(&cmd->entry); 565ea0375afSGary R Hook cmd->callback(cmd->data, -ENODEV); 566ea0375afSGary R Hook } 567ea0375afSGary R Hook } 568ea0375afSGary R Hook 569bc197b2aSJulia Lawall static const struct ccp_actions ccp3_actions = { 570a43eb985SGary R Hook .aes = ccp_perform_aes, 571a43eb985SGary R Hook .xts_aes = ccp_perform_xts_aes, 572990672d4SGary R Hook .des3 = NULL, 573a43eb985SGary R Hook .sha = ccp_perform_sha, 574a43eb985SGary R Hook .rsa = ccp_perform_rsa, 575a43eb985SGary R Hook .passthru = ccp_perform_passthru, 576a43eb985SGary R Hook .ecc = ccp_perform_ecc, 57758a690b7SGary R Hook .sballoc = ccp_alloc_ksb, 57858a690b7SGary R Hook .sbfree = ccp_free_ksb, 579ea0375afSGary R Hook .init = ccp_init, 580ea0375afSGary R Hook .destroy = ccp_destroy, 581bb4e89b3SGary R Hook .get_free_slots = ccp_get_free_slots, 582ea0375afSGary R Hook .irqhandler = ccp_irq_handler, 583ea0375afSGary R Hook }; 584ea0375afSGary R Hook 585970e8303SBrijesh Singh const struct ccp_vdata ccpv3_platform = { 586970e8303SBrijesh Singh .version = CCP_VERSION(3, 0), 587970e8303SBrijesh Singh .setup = NULL, 588970e8303SBrijesh Singh .perform = &ccp3_actions, 589970e8303SBrijesh Singh .offset = 0, 590970e8303SBrijesh Singh }; 591970e8303SBrijesh Singh 5929ddb9dc6SGary R Hook const struct ccp_vdata ccpv3 = { 593ea0375afSGary R Hook .version = CCP_VERSION(3, 0), 5944b394a23SGary R Hook .setup = NULL, 595ea0375afSGary R Hook .perform = &ccp3_actions, 596fba8855cSGary R Hook .offset = 0x20000, 597e28c190dSGary R Hook .rsamax = CCP_RSA_MAX_WIDTH, 598ea0375afSGary R Hook }; 599