Path: blob/master/drivers/crypto/ccree/cc_request_mgr.c
26282 views
// SPDX-License-Identifier: GPL-2.01/* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */23#include <linux/kernel.h>4#include <linux/nospec.h>5#include "cc_driver.h"6#include "cc_buffer_mgr.h"7#include "cc_request_mgr.h"8#include "cc_pm.h"910#define CC_MAX_POLL_ITER 1011/* The highest descriptor count in used */12#define CC_MAX_DESC_SEQ_LEN 231314struct cc_req_mgr_handle {15/* Request manager resources */16unsigned int hw_queue_size; /* HW capability */17unsigned int min_free_hw_slots;18unsigned int max_used_sw_slots;19struct cc_crypto_req req_queue[MAX_REQUEST_QUEUE_SIZE];20u32 req_queue_head;21u32 req_queue_tail;22u32 axi_completed;23u32 q_free_slots;24/* This lock protects access to HW register25* that must be single request at a time26*/27spinlock_t hw_lock;28struct cc_hw_desc compl_desc;29u8 *dummy_comp_buff;30dma_addr_t dummy_comp_buff_dma;3132/* backlog queue */33struct list_head backlog;34unsigned int bl_len;35spinlock_t bl_lock; /* protect backlog queue */3637#ifdef COMP_IN_WQ38struct workqueue_struct *workq;39struct delayed_work compwork;40#else41struct tasklet_struct comptask;42#endif43};4445struct cc_bl_item {46struct cc_crypto_req creq;47struct cc_hw_desc desc[CC_MAX_DESC_SEQ_LEN];48unsigned int len;49struct list_head list;50bool notif;51};5253static const u32 cc_cpp_int_masks[CC_CPP_NUM_ALGS][CC_CPP_NUM_SLOTS] = {54{ BIT(CC_HOST_IRR_REE_OP_ABORTED_AES_0_INT_BIT_SHIFT),55BIT(CC_HOST_IRR_REE_OP_ABORTED_AES_1_INT_BIT_SHIFT),56BIT(CC_HOST_IRR_REE_OP_ABORTED_AES_2_INT_BIT_SHIFT),57BIT(CC_HOST_IRR_REE_OP_ABORTED_AES_3_INT_BIT_SHIFT),58BIT(CC_HOST_IRR_REE_OP_ABORTED_AES_4_INT_BIT_SHIFT),59BIT(CC_HOST_IRR_REE_OP_ABORTED_AES_5_INT_BIT_SHIFT),60BIT(CC_HOST_IRR_REE_OP_ABORTED_AES_6_INT_BIT_SHIFT),61BIT(CC_HOST_IRR_REE_OP_ABORTED_AES_7_INT_BIT_SHIFT) },62{ BIT(CC_HOST_IRR_REE_OP_ABORTED_SM_0_INT_BIT_SHIFT),63BIT(CC_HOST_IRR_REE_OP_ABORTED_SM_1_INT_BIT_SHIFT),64BIT(CC_HOST_IRR_REE_OP_ABORTED_SM_2_INT_BIT_SHIFT),65BIT(CC_HOST_IRR_REE_OP_ABORTED_SM_3_INT_BIT_SHIFT),66BIT(CC_HOST_IRR_REE_OP_ABORTED_SM_4_INT_BIT_SHIFT),67BIT(CC_HOST_IRR_REE_OP_ABORTED_SM_5_INT_BIT_SHIFT),68BIT(CC_HOST_IRR_REE_OP_ABORTED_SM_6_INT_BIT_SHIFT),69BIT(CC_HOST_IRR_REE_OP_ABORTED_SM_7_INT_BIT_SHIFT) }70};7172static void comp_handler(unsigned long devarg);73#ifdef COMP_IN_WQ74static void comp_work_handler(struct work_struct *work);75#endif7677static inline u32 cc_cpp_int_mask(enum cc_cpp_alg alg, int slot)78{79alg = array_index_nospec(alg, CC_CPP_NUM_ALGS);80slot = array_index_nospec(slot, CC_CPP_NUM_SLOTS);8182return cc_cpp_int_masks[alg][slot];83}8485void cc_req_mgr_fini(struct cc_drvdata *drvdata)86{87struct cc_req_mgr_handle *req_mgr_h = drvdata->request_mgr_handle;88struct device *dev = drvdata_to_dev(drvdata);8990if (!req_mgr_h)91return; /* Not allocated */9293if (req_mgr_h->dummy_comp_buff_dma) {94dma_free_coherent(dev, sizeof(u32), req_mgr_h->dummy_comp_buff,95req_mgr_h->dummy_comp_buff_dma);96}9798dev_dbg(dev, "max_used_hw_slots=%d\n", (req_mgr_h->hw_queue_size -99req_mgr_h->min_free_hw_slots));100dev_dbg(dev, "max_used_sw_slots=%d\n", req_mgr_h->max_used_sw_slots);101102#ifdef COMP_IN_WQ103destroy_workqueue(req_mgr_h->workq);104#else105/* Kill tasklet */106tasklet_kill(&req_mgr_h->comptask);107#endif108kfree_sensitive(req_mgr_h);109drvdata->request_mgr_handle = NULL;110}111112int cc_req_mgr_init(struct cc_drvdata *drvdata)113{114struct cc_req_mgr_handle *req_mgr_h;115struct device *dev = drvdata_to_dev(drvdata);116int rc = 0;117118req_mgr_h = kzalloc(sizeof(*req_mgr_h), GFP_KERNEL);119if (!req_mgr_h) {120rc = -ENOMEM;121goto req_mgr_init_err;122}123124drvdata->request_mgr_handle = req_mgr_h;125126spin_lock_init(&req_mgr_h->hw_lock);127spin_lock_init(&req_mgr_h->bl_lock);128INIT_LIST_HEAD(&req_mgr_h->backlog);129130#ifdef COMP_IN_WQ131dev_dbg(dev, "Initializing completion workqueue\n");132req_mgr_h->workq = create_singlethread_workqueue("ccree");133if (!req_mgr_h->workq) {134dev_err(dev, "Failed creating work queue\n");135rc = -ENOMEM;136goto req_mgr_init_err;137}138INIT_DELAYED_WORK(&req_mgr_h->compwork, comp_work_handler);139#else140dev_dbg(dev, "Initializing completion tasklet\n");141tasklet_init(&req_mgr_h->comptask, comp_handler,142(unsigned long)drvdata);143#endif144req_mgr_h->hw_queue_size = cc_ioread(drvdata,145CC_REG(DSCRPTR_QUEUE_SRAM_SIZE));146dev_dbg(dev, "hw_queue_size=0x%08X\n", req_mgr_h->hw_queue_size);147if (req_mgr_h->hw_queue_size < MIN_HW_QUEUE_SIZE) {148dev_err(dev, "Invalid HW queue size = %u (Min. required is %u)\n",149req_mgr_h->hw_queue_size, MIN_HW_QUEUE_SIZE);150rc = -ENOMEM;151goto req_mgr_init_err;152}153req_mgr_h->min_free_hw_slots = req_mgr_h->hw_queue_size;154req_mgr_h->max_used_sw_slots = 0;155156/* Allocate DMA word for "dummy" completion descriptor use */157req_mgr_h->dummy_comp_buff =158dma_alloc_coherent(dev, sizeof(u32),159&req_mgr_h->dummy_comp_buff_dma,160GFP_KERNEL);161if (!req_mgr_h->dummy_comp_buff) {162dev_err(dev, "Not enough memory to allocate DMA (%zu) dropped buffer\n",163sizeof(u32));164rc = -ENOMEM;165goto req_mgr_init_err;166}167168/* Init. "dummy" completion descriptor */169hw_desc_init(&req_mgr_h->compl_desc);170set_din_const(&req_mgr_h->compl_desc, 0, sizeof(u32));171set_dout_dlli(&req_mgr_h->compl_desc, req_mgr_h->dummy_comp_buff_dma,172sizeof(u32), NS_BIT, 1);173set_flow_mode(&req_mgr_h->compl_desc, BYPASS);174set_queue_last_ind(drvdata, &req_mgr_h->compl_desc);175176return 0;177178req_mgr_init_err:179cc_req_mgr_fini(drvdata);180return rc;181}182183static void enqueue_seq(struct cc_drvdata *drvdata, struct cc_hw_desc seq[],184unsigned int seq_len)185{186int i, w;187void __iomem *reg = drvdata->cc_base + CC_REG(DSCRPTR_QUEUE_WORD0);188struct device *dev = drvdata_to_dev(drvdata);189190/*191* We do indeed write all 6 command words to the same192* register. The HW supports this.193*/194195for (i = 0; i < seq_len; i++) {196for (w = 0; w <= 5; w++)197writel_relaxed(seq[i].word[w], reg);198199if (cc_dump_desc)200dev_dbg(dev, "desc[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",201i, seq[i].word[0], seq[i].word[1],202seq[i].word[2], seq[i].word[3],203seq[i].word[4], seq[i].word[5]);204}205}206207/**208* request_mgr_complete() - Completion will take place if and only if user209* requested completion by cc_send_sync_request().210*211* @dev: Device pointer212* @dx_compl_h: The completion event to signal213* @dummy: unused error code214*/215static void request_mgr_complete(struct device *dev, void *dx_compl_h,216int dummy)217{218struct completion *this_compl = dx_compl_h;219220complete(this_compl);221}222223static int cc_queues_status(struct cc_drvdata *drvdata,224struct cc_req_mgr_handle *req_mgr_h,225unsigned int total_seq_len)226{227unsigned long poll_queue;228struct device *dev = drvdata_to_dev(drvdata);229230/* SW queue is checked only once as it will not231* be changed during the poll because the spinlock_bh232* is held by the thread233*/234if (((req_mgr_h->req_queue_head + 1) & (MAX_REQUEST_QUEUE_SIZE - 1)) ==235req_mgr_h->req_queue_tail) {236dev_err(dev, "SW FIFO is full. req_queue_head=%d sw_fifo_len=%d\n",237req_mgr_h->req_queue_head, MAX_REQUEST_QUEUE_SIZE);238return -ENOSPC;239}240241if (req_mgr_h->q_free_slots >= total_seq_len)242return 0;243244/* Wait for space in HW queue. Poll constant num of iterations. */245for (poll_queue = 0; poll_queue < CC_MAX_POLL_ITER ; poll_queue++) {246req_mgr_h->q_free_slots =247cc_ioread(drvdata, CC_REG(DSCRPTR_QUEUE_CONTENT));248if (req_mgr_h->q_free_slots < req_mgr_h->min_free_hw_slots)249req_mgr_h->min_free_hw_slots = req_mgr_h->q_free_slots;250251if (req_mgr_h->q_free_slots >= total_seq_len) {252/* If there is enough place return */253return 0;254}255256dev_dbg(dev, "HW FIFO is full. q_free_slots=%d total_seq_len=%d\n",257req_mgr_h->q_free_slots, total_seq_len);258}259/* No room in the HW queue try again later */260dev_dbg(dev, "HW FIFO full, timeout. req_queue_head=%d sw_fifo_len=%d q_free_slots=%d total_seq_len=%d\n",261req_mgr_h->req_queue_head, MAX_REQUEST_QUEUE_SIZE,262req_mgr_h->q_free_slots, total_seq_len);263return -ENOSPC;264}265266/**267* cc_do_send_request() - Enqueue caller request to crypto hardware.268* Need to be called with HW lock held and PM running269*270* @drvdata: Associated device driver context271* @cc_req: The request to enqueue272* @desc: The crypto sequence273* @len: The crypto sequence length274* @add_comp: If "true": add an artificial dout DMA to mark completion275*276*/277static void cc_do_send_request(struct cc_drvdata *drvdata,278struct cc_crypto_req *cc_req,279struct cc_hw_desc *desc, unsigned int len,280bool add_comp)281{282struct cc_req_mgr_handle *req_mgr_h = drvdata->request_mgr_handle;283unsigned int used_sw_slots;284unsigned int total_seq_len = len; /*initial sequence length*/285struct device *dev = drvdata_to_dev(drvdata);286287used_sw_slots = ((req_mgr_h->req_queue_head -288req_mgr_h->req_queue_tail) &289(MAX_REQUEST_QUEUE_SIZE - 1));290if (used_sw_slots > req_mgr_h->max_used_sw_slots)291req_mgr_h->max_used_sw_slots = used_sw_slots;292293/* Enqueue request - must be locked with HW lock*/294req_mgr_h->req_queue[req_mgr_h->req_queue_head] = *cc_req;295req_mgr_h->req_queue_head = (req_mgr_h->req_queue_head + 1) &296(MAX_REQUEST_QUEUE_SIZE - 1);297298dev_dbg(dev, "Enqueue request head=%u\n", req_mgr_h->req_queue_head);299300/*301* We are about to push command to the HW via the command registers302* that may reference host memory. We need to issue a memory barrier303* to make sure there are no outstanding memory writes304*/305wmb();306307/* STAT_PHASE_4: Push sequence */308309enqueue_seq(drvdata, desc, len);310311if (add_comp) {312enqueue_seq(drvdata, &req_mgr_h->compl_desc, 1);313total_seq_len++;314}315316if (req_mgr_h->q_free_slots < total_seq_len) {317/* This situation should never occur. Maybe indicating problem318* with resuming power. Set the free slot count to 0 and hope319* for the best.320*/321dev_err(dev, "HW free slot count mismatch.");322req_mgr_h->q_free_slots = 0;323} else {324/* Update the free slots in HW queue */325req_mgr_h->q_free_slots -= total_seq_len;326}327}328329static void cc_enqueue_backlog(struct cc_drvdata *drvdata,330struct cc_bl_item *bli)331{332struct cc_req_mgr_handle *mgr = drvdata->request_mgr_handle;333struct device *dev = drvdata_to_dev(drvdata);334335spin_lock_bh(&mgr->bl_lock);336list_add_tail(&bli->list, &mgr->backlog);337++mgr->bl_len;338dev_dbg(dev, "+++bl len: %d\n", mgr->bl_len);339spin_unlock_bh(&mgr->bl_lock);340tasklet_schedule(&mgr->comptask);341}342343static void cc_proc_backlog(struct cc_drvdata *drvdata)344{345struct cc_req_mgr_handle *mgr = drvdata->request_mgr_handle;346struct cc_bl_item *bli;347struct cc_crypto_req *creq;348void *req;349struct device *dev = drvdata_to_dev(drvdata);350int rc;351352spin_lock(&mgr->bl_lock);353354while (mgr->bl_len) {355bli = list_first_entry(&mgr->backlog, struct cc_bl_item, list);356dev_dbg(dev, "---bl len: %d\n", mgr->bl_len);357358spin_unlock(&mgr->bl_lock);359360361creq = &bli->creq;362req = creq->user_arg;363364/*365* Notify the request we're moving out of the backlog366* but only if we haven't done so already.367*/368if (!bli->notif) {369creq->user_cb(dev, req, -EINPROGRESS);370bli->notif = true;371}372373spin_lock(&mgr->hw_lock);374375rc = cc_queues_status(drvdata, mgr, bli->len);376if (rc) {377/*378* There is still no room in the FIFO for379* this request. Bail out. We'll return here380* on the next completion irq.381*/382spin_unlock(&mgr->hw_lock);383return;384}385386cc_do_send_request(drvdata, &bli->creq, bli->desc, bli->len,387false);388spin_unlock(&mgr->hw_lock);389390/* Remove ourselves from the backlog list */391spin_lock(&mgr->bl_lock);392list_del(&bli->list);393--mgr->bl_len;394kfree(bli);395}396397spin_unlock(&mgr->bl_lock);398}399400int cc_send_request(struct cc_drvdata *drvdata, struct cc_crypto_req *cc_req,401struct cc_hw_desc *desc, unsigned int len,402struct crypto_async_request *req)403{404int rc;405struct cc_req_mgr_handle *mgr = drvdata->request_mgr_handle;406struct device *dev = drvdata_to_dev(drvdata);407bool backlog_ok = req->flags & CRYPTO_TFM_REQ_MAY_BACKLOG;408gfp_t flags = cc_gfp_flags(req);409struct cc_bl_item *bli;410411rc = cc_pm_get(dev);412if (rc) {413dev_err(dev, "cc_pm_get returned %x\n", rc);414return rc;415}416417spin_lock_bh(&mgr->hw_lock);418rc = cc_queues_status(drvdata, mgr, len);419420#ifdef CC_DEBUG_FORCE_BACKLOG421if (backlog_ok)422rc = -ENOSPC;423#endif /* CC_DEBUG_FORCE_BACKLOG */424425if (rc == -ENOSPC && backlog_ok) {426spin_unlock_bh(&mgr->hw_lock);427428bli = kmalloc(sizeof(*bli), flags);429if (!bli) {430cc_pm_put_suspend(dev);431return -ENOMEM;432}433434memcpy(&bli->creq, cc_req, sizeof(*cc_req));435memcpy(&bli->desc, desc, len * sizeof(*desc));436bli->len = len;437bli->notif = false;438cc_enqueue_backlog(drvdata, bli);439return -EBUSY;440}441442if (!rc) {443cc_do_send_request(drvdata, cc_req, desc, len, false);444rc = -EINPROGRESS;445}446447spin_unlock_bh(&mgr->hw_lock);448return rc;449}450451int cc_send_sync_request(struct cc_drvdata *drvdata,452struct cc_crypto_req *cc_req, struct cc_hw_desc *desc,453unsigned int len)454{455int rc;456struct device *dev = drvdata_to_dev(drvdata);457struct cc_req_mgr_handle *mgr = drvdata->request_mgr_handle;458459init_completion(&cc_req->seq_compl);460cc_req->user_cb = request_mgr_complete;461cc_req->user_arg = &cc_req->seq_compl;462463rc = cc_pm_get(dev);464if (rc) {465dev_err(dev, "cc_pm_get returned %x\n", rc);466return rc;467}468469while (true) {470spin_lock_bh(&mgr->hw_lock);471rc = cc_queues_status(drvdata, mgr, len + 1);472473if (!rc)474break;475476spin_unlock_bh(&mgr->hw_lock);477wait_for_completion_interruptible(&drvdata->hw_queue_avail);478reinit_completion(&drvdata->hw_queue_avail);479}480481cc_do_send_request(drvdata, cc_req, desc, len, true);482spin_unlock_bh(&mgr->hw_lock);483wait_for_completion(&cc_req->seq_compl);484return 0;485}486487/**488* send_request_init() - Enqueue caller request to crypto hardware during init489* process.490* Assume this function is not called in the middle of a flow,491* since we set QUEUE_LAST_IND flag in the last descriptor.492*493* @drvdata: Associated device driver context494* @desc: The crypto sequence495* @len: The crypto sequence length496*497* Return:498* Returns "0" upon success499*/500int send_request_init(struct cc_drvdata *drvdata, struct cc_hw_desc *desc,501unsigned int len)502{503struct cc_req_mgr_handle *req_mgr_h = drvdata->request_mgr_handle;504unsigned int total_seq_len = len; /*initial sequence length*/505int rc = 0;506507/* Wait for space in HW and SW FIFO. Poll for as much as FIFO_TIMEOUT.508*/509rc = cc_queues_status(drvdata, req_mgr_h, total_seq_len);510if (rc)511return rc;512513set_queue_last_ind(drvdata, &desc[(len - 1)]);514515/*516* We are about to push command to the HW via the command registers517* that may reference host memory. We need to issue a memory barrier518* to make sure there are no outstanding memory writes519*/520wmb();521enqueue_seq(drvdata, desc, len);522523/* Update the free slots in HW queue */524req_mgr_h->q_free_slots =525cc_ioread(drvdata, CC_REG(DSCRPTR_QUEUE_CONTENT));526527return 0;528}529530void complete_request(struct cc_drvdata *drvdata)531{532struct cc_req_mgr_handle *request_mgr_handle =533drvdata->request_mgr_handle;534535complete(&drvdata->hw_queue_avail);536#ifdef COMP_IN_WQ537queue_delayed_work(request_mgr_handle->workq,538&request_mgr_handle->compwork, 0);539#else540tasklet_schedule(&request_mgr_handle->comptask);541#endif542}543544#ifdef COMP_IN_WQ545static void comp_work_handler(struct work_struct *work)546{547struct cc_drvdata *drvdata =548container_of(work, struct cc_drvdata, compwork.work);549550comp_handler((unsigned long)drvdata);551}552#endif553554static void proc_completions(struct cc_drvdata *drvdata)555{556struct cc_crypto_req *cc_req;557struct device *dev = drvdata_to_dev(drvdata);558struct cc_req_mgr_handle *request_mgr_handle =559drvdata->request_mgr_handle;560unsigned int *tail = &request_mgr_handle->req_queue_tail;561unsigned int *head = &request_mgr_handle->req_queue_head;562int rc;563u32 mask;564565while (request_mgr_handle->axi_completed) {566request_mgr_handle->axi_completed--;567568/* Dequeue request */569if (*head == *tail) {570/* We are supposed to handle a completion but our571* queue is empty. This is not normal. Return and572* hope for the best.573*/574dev_err(dev, "Request queue is empty head == tail %u\n",575*head);576break;577}578579cc_req = &request_mgr_handle->req_queue[*tail];580581if (cc_req->cpp.is_cpp) {582583dev_dbg(dev, "CPP request completion slot: %d alg:%d\n",584cc_req->cpp.slot, cc_req->cpp.alg);585mask = cc_cpp_int_mask(cc_req->cpp.alg,586cc_req->cpp.slot);587rc = (drvdata->irq & mask ? -EPERM : 0);588dev_dbg(dev, "Got mask: %x irq: %x rc: %d\n", mask,589drvdata->irq, rc);590} else {591dev_dbg(dev, "None CPP request completion\n");592rc = 0;593}594595if (cc_req->user_cb)596cc_req->user_cb(dev, cc_req->user_arg, rc);597*tail = (*tail + 1) & (MAX_REQUEST_QUEUE_SIZE - 1);598dev_dbg(dev, "Dequeue request tail=%u\n", *tail);599dev_dbg(dev, "Request completed. axi_completed=%d\n",600request_mgr_handle->axi_completed);601cc_pm_put_suspend(dev);602}603}604605static inline u32 cc_axi_comp_count(struct cc_drvdata *drvdata)606{607return FIELD_GET(AXIM_MON_COMP_VALUE,608cc_ioread(drvdata, drvdata->axim_mon_offset));609}610611/* Deferred service handler, run as interrupt-fired tasklet */612static void comp_handler(unsigned long devarg)613{614struct cc_drvdata *drvdata = (struct cc_drvdata *)devarg;615struct cc_req_mgr_handle *request_mgr_handle =616drvdata->request_mgr_handle;617struct device *dev = drvdata_to_dev(drvdata);618u32 irq;619620dev_dbg(dev, "Completion handler called!\n");621irq = (drvdata->irq & drvdata->comp_mask);622623/* To avoid the interrupt from firing as we unmask it,624* we clear it now625*/626cc_iowrite(drvdata, CC_REG(HOST_ICR), irq);627628/* Avoid race with above clear: Test completion counter once more */629630request_mgr_handle->axi_completed += cc_axi_comp_count(drvdata);631632dev_dbg(dev, "AXI completion after updated: %d\n",633request_mgr_handle->axi_completed);634635while (request_mgr_handle->axi_completed) {636do {637drvdata->irq |= cc_ioread(drvdata, CC_REG(HOST_IRR));638irq = (drvdata->irq & drvdata->comp_mask);639proc_completions(drvdata);640641/* At this point (after proc_completions()),642* request_mgr_handle->axi_completed is 0.643*/644request_mgr_handle->axi_completed +=645cc_axi_comp_count(drvdata);646} while (request_mgr_handle->axi_completed > 0);647648cc_iowrite(drvdata, CC_REG(HOST_ICR), irq);649650request_mgr_handle->axi_completed += cc_axi_comp_count(drvdata);651}652653/* after verifying that there is nothing to do,654* unmask AXI completion interrupt655*/656cc_iowrite(drvdata, CC_REG(HOST_IMR),657cc_ioread(drvdata, CC_REG(HOST_IMR)) & ~drvdata->comp_mask);658659cc_proc_backlog(drvdata);660dev_dbg(dev, "Comp. handler done.\n");661}662663664