OSDN Git Service

soc: qcom: ipa: clocking, interrupts, and memory
[uclinux-h8/linux.git] / drivers / net / ipa / ipa_mem.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2019-2020 Linaro Ltd.
5  */
6
7 #include <linux/types.h>
8 #include <linux/bitfield.h>
9 #include <linux/bug.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/io.h>
12
13 #include "ipa.h"
14 #include "ipa_reg.h"
15 #include "ipa_cmd.h"
16 #include "ipa_mem.h"
17 #include "ipa_data.h"
18 #include "ipa_table.h"
19 #include "gsi_trans.h"
20
21 /* "Canary" value placed between memory regions to detect overflow */
22 #define IPA_MEM_CANARY_VAL              cpu_to_le32(0xdeadbeef)
23
24 /* Add an immediate command to a transaction that zeroes a memory region */
25 static void
26 ipa_mem_zero_region_add(struct gsi_trans *trans, const struct ipa_mem *mem)
27 {
28         struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
29         dma_addr_t addr = ipa->zero_addr;
30
31         if (!mem->size)
32                 return;
33
34         ipa_cmd_dma_shared_mem_add(trans, mem->offset, mem->size, addr, true);
35 }
36
37 /**
38  * ipa_mem_setup() - Set up IPA AP and modem shared memory areas
39  *
40  * Set up the shared memory regions in IPA local memory.  This involves
41  * zero-filling memory regions, and in the case of header memory, telling
42  * the IPA where it's located.
43  *
44  * This function performs the initial setup of this memory.  If the modem
45  * crashes, its regions are re-zeroed in ipa_mem_zero_modem().
46  *
47  * The AP informs the modem where its portions of memory are located
48  * in a QMI exchange that occurs at modem startup.
49  *
50  * @Return:     0 if successful, or a negative error code
51  */
52 int ipa_mem_setup(struct ipa *ipa)
53 {
54         dma_addr_t addr = ipa->zero_addr;
55         struct gsi_trans *trans;
56         u32 offset;
57         u16 size;
58
59         /* Get a transaction to define the header memory region and to zero
60          * the processing context and modem memory regions.
61          */
62         trans = ipa_cmd_trans_alloc(ipa, 4);
63         if (!trans) {
64                 dev_err(&ipa->pdev->dev, "no transaction for memory setup\n");
65                 return -EBUSY;
66         }
67
68         /* Initialize IPA-local header memory.  The modem and AP header
69          * regions are contiguous, and initialized together.
70          */
71         offset = ipa->mem[IPA_MEM_MODEM_HEADER].offset;
72         size = ipa->mem[IPA_MEM_MODEM_HEADER].size;
73         size += ipa->mem[IPA_MEM_AP_HEADER].size;
74
75         ipa_cmd_hdr_init_local_add(trans, offset, size, addr);
76
77         ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_PROC_CTX]);
78
79         ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_AP_PROC_CTX]);
80
81         ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM]);
82
83         gsi_trans_commit_wait(trans);
84
85         /* Tell the hardware where the processing context area is located */
86         iowrite32(ipa->mem_offset + offset,
87                   ipa->reg_virt + IPA_REG_LOCAL_PKT_PROC_CNTXT_BASE_OFFSET);
88
89         return 0;
90 }
91
92 void ipa_mem_teardown(struct ipa *ipa)
93 {
94         /* Nothing to do */
95 }
96
97 #ifdef IPA_VALIDATE
98
99 static bool ipa_mem_valid(struct ipa *ipa, enum ipa_mem_id mem_id)
100 {
101         const struct ipa_mem *mem = &ipa->mem[mem_id];
102         struct device *dev = &ipa->pdev->dev;
103         u16 size_multiple;
104
105         /* Other than modem memory, sizes must be a multiple of 8 */
106         size_multiple = mem_id == IPA_MEM_MODEM ? 4 : 8;
107         if (mem->size % size_multiple)
108                 dev_err(dev, "region %u size not a multiple of %u bytes\n",
109                         mem_id, size_multiple);
110         else if (mem->offset % 8)
111                 dev_err(dev, "region %u offset not 8-byte aligned\n", mem_id);
112         else if (mem->offset < mem->canary_count * sizeof(__le32))
113                 dev_err(dev, "region %u offset too small for %hu canaries\n",
114                         mem_id, mem->canary_count);
115         else if (mem->offset + mem->size > ipa->mem_size)
116                 dev_err(dev, "region %u ends beyond memory limit (0x%08x)\n",
117                         mem_id, ipa->mem_size);
118         else
119                 return true;
120
121         return false;
122 }
123
124 #else /* !IPA_VALIDATE */
125
126 static bool ipa_mem_valid(struct ipa *ipa, enum ipa_mem_id mem_id)
127 {
128         return true;
129 }
130
131 #endif /*! IPA_VALIDATE */
132
133 /**
134  * ipa_mem_config() - Configure IPA shared memory
135  *
136  * @Return:     0 if successful, or a negative error code
137  */
138 int ipa_mem_config(struct ipa *ipa)
139 {
140         struct device *dev = &ipa->pdev->dev;
141         enum ipa_mem_id mem_id;
142         dma_addr_t addr;
143         u32 mem_size;
144         void *virt;
145         u32 val;
146
147         /* Check the advertised location and size of the shared memory area */
148         val = ioread32(ipa->reg_virt + IPA_REG_SHARED_MEM_SIZE_OFFSET);
149
150         /* The fields in the register are in 8 byte units */
151         ipa->mem_offset = 8 * u32_get_bits(val, SHARED_MEM_BADDR_FMASK);
152         /* Make sure the end is within the region's mapped space */
153         mem_size = 8 * u32_get_bits(val, SHARED_MEM_SIZE_FMASK);
154
155         /* If the sizes don't match, issue a warning */
156         if (ipa->mem_offset + mem_size > ipa->mem_size) {
157                 dev_warn(dev, "ignoring larger reported memory size: 0x%08x\n",
158                         mem_size);
159         } else if (ipa->mem_offset + mem_size < ipa->mem_size) {
160                 dev_warn(dev, "limiting IPA memory size to 0x%08x\n",
161                          mem_size);
162                 ipa->mem_size = mem_size;
163         }
164
165         /* Prealloc DMA memory for zeroing regions */
166         virt = dma_alloc_coherent(dev, IPA_MEM_MAX, &addr, GFP_KERNEL);
167         if (!virt)
168                 return -ENOMEM;
169         ipa->zero_addr = addr;
170         ipa->zero_virt = virt;
171         ipa->zero_size = IPA_MEM_MAX;
172
173         /* Verify each defined memory region is valid, and if indicated
174          * for the region, write "canary" values in the space prior to
175          * the region's base address.
176          */
177         for (mem_id = 0; mem_id < IPA_MEM_COUNT; mem_id++) {
178                 const struct ipa_mem *mem = &ipa->mem[mem_id];
179                 u16 canary_count;
180                 __le32 *canary;
181
182                 /* Validate all regions (even undefined ones) */
183                 if (!ipa_mem_valid(ipa, mem_id))
184                         goto err_dma_free;
185
186                 /* Skip over undefined regions */
187                 if (!mem->offset && !mem->size)
188                         continue;
189
190                 canary_count = mem->canary_count;
191                 if (!canary_count)
192                         continue;
193
194                 /* Write canary values in the space before the region */
195                 canary = ipa->mem_virt + ipa->mem_offset + mem->offset;
196                 do
197                         *--canary = IPA_MEM_CANARY_VAL;
198                 while (--canary_count);
199         }
200
201         /* Make sure filter and route table memory regions are valid */
202         if (!ipa_table_valid(ipa))
203                 goto err_dma_free;
204
205         /* Validate memory-related properties relevant to immediate commands */
206         if (!ipa_cmd_data_valid(ipa))
207                 goto err_dma_free;
208
209         /* Verify the microcontroller ring alignment (0 is OK too) */
210         if (ipa->mem[IPA_MEM_UC_EVENT_RING].offset % 1024) {
211                 dev_err(dev, "microcontroller ring not 1024-byte aligned\n");
212                 goto err_dma_free;
213         }
214
215         return 0;
216
217 err_dma_free:
218         dma_free_coherent(dev, IPA_MEM_MAX, ipa->zero_virt, ipa->zero_addr);
219
220         return -EINVAL;
221 }
222
223 /* Inverse of ipa_mem_config() */
224 void ipa_mem_deconfig(struct ipa *ipa)
225 {
226         struct device *dev = &ipa->pdev->dev;
227
228         dma_free_coherent(dev, ipa->zero_size, ipa->zero_virt, ipa->zero_addr);
229         ipa->zero_size = 0;
230         ipa->zero_virt = NULL;
231         ipa->zero_addr = 0;
232 }
233
234 /**
235  * ipa_mem_zero_modem() - Zero IPA-local memory regions owned by the modem
236  *
237  * Zero regions of IPA-local memory used by the modem.  These are configured
238  * (and initially zeroed) by ipa_mem_setup(), but if the modem crashes and
239  * restarts via SSR we need to re-initialize them.  A QMI message tells the
240  * modem where to find regions of IPA local memory it needs to know about
241  * (these included).
242  */
243 int ipa_mem_zero_modem(struct ipa *ipa)
244 {
245         struct gsi_trans *trans;
246
247         /* Get a transaction to zero the modem memory, modem header,
248          * and modem processing context regions.
249          */
250         trans = ipa_cmd_trans_alloc(ipa, 3);
251         if (!trans) {
252                 dev_err(&ipa->pdev->dev,
253                         "no transaction to zero modem memory\n");
254                 return -EBUSY;
255         }
256
257         ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_HEADER]);
258
259         ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_PROC_CTX]);
260
261         ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM]);
262
263         gsi_trans_commit_wait(trans);
264
265         return 0;
266 }
267
268 /* Perform memory region-related initialization */
269 int ipa_mem_init(struct ipa *ipa, u32 count, const struct ipa_mem *mem)
270 {
271         struct device *dev = &ipa->pdev->dev;
272         struct resource *res;
273         int ret;
274
275         if (count > IPA_MEM_COUNT) {
276                 dev_err(dev, "to many memory regions (%u > %u)\n",
277                         count, IPA_MEM_COUNT);
278                 return -EINVAL;
279         }
280
281         ret = dma_set_mask_and_coherent(&ipa->pdev->dev, DMA_BIT_MASK(64));
282         if (ret) {
283                 dev_err(dev, "error %d setting DMA mask\n", ret);
284                 return ret;
285         }
286
287         res = platform_get_resource_byname(ipa->pdev, IORESOURCE_MEM,
288                                            "ipa-shared");
289         if (!res) {
290                 dev_err(dev,
291                         "DT error getting \"ipa-shared\" memory property\n");
292                 return -ENODEV;
293         }
294
295         ipa->mem_virt = memremap(res->start, resource_size(res), MEMREMAP_WC);
296         if (!ipa->mem_virt) {
297                 dev_err(dev, "unable to remap \"ipa-shared\" memory\n");
298                 return -ENOMEM;
299         }
300
301         ipa->mem_addr = res->start;
302         ipa->mem_size = resource_size(res);
303
304         /* The ipa->mem[] array is indexed by enum ipa_mem_id values */
305         ipa->mem = mem;
306
307         return 0;
308 }
309
310 /* Inverse of ipa_mem_init() */
311 void ipa_mem_exit(struct ipa *ipa)
312 {
313         memunmap(ipa->mem_virt);
314 }