staging: ccree: make long func call sites readable
[linux-2.6-microblaze.git] / drivers / staging / ccree / ssi_sram_mgr.c
1 /*
2  * Copyright (C) 2012-2017 ARM Limited or its affiliates.
3  *
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.
7  *
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.
12  *
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/>.
15  */
16
17 #include "ssi_driver.h"
18 #include "ssi_sram_mgr.h"
19
20 /**
21  * struct ssi_sram_mgr_ctx -Internal RAM context manager
22  * @sram_free_offset:   the offset to the non-allocated area
23  */
24 struct ssi_sram_mgr_ctx {
25         ssi_sram_addr_t sram_free_offset;
26 };
27
28 /**
29  * ssi_sram_mgr_fini() - Cleanup SRAM pool.
30  *
31  * @drvdata: Associated device driver context
32  */
33 void ssi_sram_mgr_fini(struct ssi_drvdata *drvdata)
34 {
35         struct ssi_sram_mgr_ctx *smgr_ctx = drvdata->sram_mgr_handle;
36
37         /* Free "this" context */
38         if (smgr_ctx) {
39                 memset(smgr_ctx, 0, sizeof(struct ssi_sram_mgr_ctx));
40                 kfree(smgr_ctx);
41         }
42 }
43
44 /**
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.
48  *
49  * @drvdata: Associated device driver context
50  */
51 int ssi_sram_mgr_init(struct ssi_drvdata *drvdata)
52 {
53         /* Allocate "this" context */
54         drvdata->sram_mgr_handle = kzalloc(sizeof(struct ssi_sram_mgr_ctx),
55                                            GFP_KERNEL);
56
57         if (!drvdata->sram_mgr_handle)
58                 return -ENOMEM;
59
60         return 0;
61 }
62
63 /*!
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.
68  *
69  * \param drvdata
70  * \param size The requested bytes to allocate
71  */
72 ssi_sram_addr_t cc_sram_alloc(struct ssi_drvdata *drvdata, u32 size)
73 {
74         struct ssi_sram_mgr_ctx *smgr_ctx = drvdata->sram_mgr_handle;
75         struct device *dev = drvdata_to_dev(drvdata);
76         ssi_sram_addr_t p;
77
78         if (unlikely((size & 0x3) != 0)) {
79                 dev_err(dev, "Requested buffer size (%u) is not multiple of 4",
80                         size);
81                 return NULL_SRAM_ADDR;
82         }
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;
87         }
88
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);
92         return p;
93 }
94
95 /**
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.
99  *
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
105  */
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)
109 {
110         u32 i;
111         unsigned int idx = *seq_len;
112
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);
118         }
119
120         *seq_len = idx;
121 }
122