2 * Copyright (C) 2012-2017 ARM Limited or its affiliates.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 #include "ssi_driver.h"
18 #include "ssi_sram_mgr.h"
21 * struct ssi_sram_mgr_ctx -Internal RAM context manager
22 * @sram_free_offset: the offset to the non-allocated area
24 struct ssi_sram_mgr_ctx {
25 ssi_sram_addr_t sram_free_offset;
29 * ssi_sram_mgr_fini() - Cleanup SRAM pool.
31 * @drvdata: Associated device driver context
33 void ssi_sram_mgr_fini(struct ssi_drvdata *drvdata)
35 struct ssi_sram_mgr_ctx *smgr_ctx = drvdata->sram_mgr_handle;
37 /* Free "this" context */
39 memset(smgr_ctx, 0, sizeof(struct ssi_sram_mgr_ctx));
45 * ssi_sram_mgr_init() - Initializes SRAM pool.
46 * The pool starts right at the beginning of SRAM.
47 * Returns zero for success, negative value otherwise.
49 * @drvdata: Associated device driver context
51 int ssi_sram_mgr_init(struct ssi_drvdata *drvdata)
53 /* Allocate "this" context */
54 drvdata->sram_mgr_handle = kzalloc(sizeof(struct ssi_sram_mgr_ctx),
57 if (!drvdata->sram_mgr_handle)
64 * Allocated buffer from SRAM pool.
65 * Note: Caller is responsible to free the LAST allocated buffer.
66 * This function does not taking care of any fragmentation may occur
67 * by the order of calls to alloc/free.
70 * \param size The requested bytes to allocate
72 ssi_sram_addr_t cc_sram_alloc(struct ssi_drvdata *drvdata, u32 size)
74 struct ssi_sram_mgr_ctx *smgr_ctx = drvdata->sram_mgr_handle;
75 struct device *dev = drvdata_to_dev(drvdata);
78 if (unlikely((size & 0x3) != 0)) {
79 dev_err(dev, "Requested buffer size (%u) is not multiple of 4",
81 return NULL_SRAM_ADDR;
83 if (unlikely(size > (SSI_CC_SRAM_SIZE - smgr_ctx->sram_free_offset))) {
84 dev_err(dev, "Not enough space to allocate %u B (at offset %llu)\n",
85 size, smgr_ctx->sram_free_offset);
86 return NULL_SRAM_ADDR;
89 p = smgr_ctx->sram_free_offset;
90 smgr_ctx->sram_free_offset += size;
91 dev_dbg(dev, "Allocated %u B @ %u\n", size, (unsigned int)p);
96 * cc_set_sram_desc() - Create const descriptors sequence to
97 * set values in given array into SRAM.
98 * Note: each const value can't exceed word size.
100 * @src: A pointer to array of words to set as consts.
101 * @dst: The target SRAM buffer to set into
102 * @nelements: The number of words in "src" array
103 * @seq: A pointer to the given IN/OUT descriptor sequence
104 * @seq_len: A pointer to the given IN/OUT sequence length
106 void cc_set_sram_desc(const u32 *src, ssi_sram_addr_t dst,
107 unsigned int nelement, struct cc_hw_desc *seq,
108 unsigned int *seq_len)
111 unsigned int idx = *seq_len;
113 for (i = 0; i < nelement; i++, idx++) {
114 hw_desc_init(&seq[idx]);
115 set_din_const(&seq[idx], src[i], sizeof(u32));
116 set_dout_sram(&seq[idx], dst + (i * sizeof(u32)), sizeof(u32));
117 set_flow_mode(&seq[idx], BYPASS);