Merge branch 'work.sysctl' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-microblaze.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/iommu.h>
12 #include <linux/io.h>
13 #include <linux/soc/qcom/smem.h>
14
15 #include "ipa.h"
16 #include "ipa_reg.h"
17 #include "ipa_data.h"
18 #include "ipa_cmd.h"
19 #include "ipa_mem.h"
20 #include "ipa_table.h"
21 #include "gsi_trans.h"
22
23 /* "Canary" value placed between memory regions to detect overflow */
24 #define IPA_MEM_CANARY_VAL              cpu_to_le32(0xdeadbeef)
25
26 /* SMEM host id representing the modem. */
27 #define QCOM_SMEM_HOST_MODEM    1
28
29 /* Add an immediate command to a transaction that zeroes a memory region */
30 static void
31 ipa_mem_zero_region_add(struct gsi_trans *trans, const struct ipa_mem *mem)
32 {
33         struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
34         dma_addr_t addr = ipa->zero_addr;
35
36         if (!mem->size)
37                 return;
38
39         ipa_cmd_dma_shared_mem_add(trans, mem->offset, mem->size, addr, true);
40 }
41
42 /**
43  * ipa_mem_setup() - Set up IPA AP and modem shared memory areas
44  *
45  * Set up the shared memory regions in IPA local memory.  This involves
46  * zero-filling memory regions, and in the case of header memory, telling
47  * the IPA where it's located.
48  *
49  * This function performs the initial setup of this memory.  If the modem
50  * crashes, its regions are re-zeroed in ipa_mem_zero_modem().
51  *
52  * The AP informs the modem where its portions of memory are located
53  * in a QMI exchange that occurs at modem startup.
54  *
55  * @Return:     0 if successful, or a negative error code
56  */
57 int ipa_mem_setup(struct ipa *ipa)
58 {
59         dma_addr_t addr = ipa->zero_addr;
60         struct gsi_trans *trans;
61         u32 offset;
62         u16 size;
63
64         /* Get a transaction to define the header memory region and to zero
65          * the processing context and modem memory regions.
66          */
67         trans = ipa_cmd_trans_alloc(ipa, 4);
68         if (!trans) {
69                 dev_err(&ipa->pdev->dev, "no transaction for memory setup\n");
70                 return -EBUSY;
71         }
72
73         /* Initialize IPA-local header memory.  The modem and AP header
74          * regions are contiguous, and initialized together.
75          */
76         offset = ipa->mem[IPA_MEM_MODEM_HEADER].offset;
77         size = ipa->mem[IPA_MEM_MODEM_HEADER].size;
78         size += ipa->mem[IPA_MEM_AP_HEADER].size;
79
80         ipa_cmd_hdr_init_local_add(trans, offset, size, addr);
81
82         ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_PROC_CTX]);
83
84         ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_AP_PROC_CTX]);
85
86         ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM]);
87
88         gsi_trans_commit_wait(trans);
89
90         /* Tell the hardware where the processing context area is located */
91         iowrite32(ipa->mem_offset + offset,
92                   ipa->reg_virt + IPA_REG_LOCAL_PKT_PROC_CNTXT_BASE_OFFSET);
93
94         return 0;
95 }
96
97 void ipa_mem_teardown(struct ipa *ipa)
98 {
99         /* Nothing to do */
100 }
101
102 #ifdef IPA_VALIDATE
103
104 static bool ipa_mem_valid(struct ipa *ipa, enum ipa_mem_id mem_id)
105 {
106         const struct ipa_mem *mem = &ipa->mem[mem_id];
107         struct device *dev = &ipa->pdev->dev;
108         u16 size_multiple;
109
110         /* Other than modem memory, sizes must be a multiple of 8 */
111         size_multiple = mem_id == IPA_MEM_MODEM ? 4 : 8;
112         if (mem->size % size_multiple)
113                 dev_err(dev, "region %u size not a multiple of %u bytes\n",
114                         mem_id, size_multiple);
115         else if (mem->offset % 8)
116                 dev_err(dev, "region %u offset not 8-byte aligned\n", mem_id);
117         else if (mem->offset < mem->canary_count * sizeof(__le32))
118                 dev_err(dev, "region %u offset too small for %hu canaries\n",
119                         mem_id, mem->canary_count);
120         else if (mem->offset + mem->size > ipa->mem_size)
121                 dev_err(dev, "region %u ends beyond memory limit (0x%08x)\n",
122                         mem_id, ipa->mem_size);
123         else
124                 return true;
125
126         return false;
127 }
128
129 #else /* !IPA_VALIDATE */
130
131 static bool ipa_mem_valid(struct ipa *ipa, enum ipa_mem_id mem_id)
132 {
133         return true;
134 }
135
136 #endif /*! IPA_VALIDATE */
137
138 /**
139  * ipa_mem_config() - Configure IPA shared memory
140  *
141  * @Return:     0 if successful, or a negative error code
142  */
143 int ipa_mem_config(struct ipa *ipa)
144 {
145         struct device *dev = &ipa->pdev->dev;
146         enum ipa_mem_id mem_id;
147         dma_addr_t addr;
148         u32 mem_size;
149         void *virt;
150         u32 val;
151
152         /* Check the advertised location and size of the shared memory area */
153         val = ioread32(ipa->reg_virt + IPA_REG_SHARED_MEM_SIZE_OFFSET);
154
155         /* The fields in the register are in 8 byte units */
156         ipa->mem_offset = 8 * u32_get_bits(val, SHARED_MEM_BADDR_FMASK);
157         /* Make sure the end is within the region's mapped space */
158         mem_size = 8 * u32_get_bits(val, SHARED_MEM_SIZE_FMASK);
159
160         /* If the sizes don't match, issue a warning */
161         if (ipa->mem_offset + mem_size > ipa->mem_size) {
162                 dev_warn(dev, "ignoring larger reported memory size: 0x%08x\n",
163                         mem_size);
164         } else if (ipa->mem_offset + mem_size < ipa->mem_size) {
165                 dev_warn(dev, "limiting IPA memory size to 0x%08x\n",
166                          mem_size);
167                 ipa->mem_size = mem_size;
168         }
169
170         /* Prealloc DMA memory for zeroing regions */
171         virt = dma_alloc_coherent(dev, IPA_MEM_MAX, &addr, GFP_KERNEL);
172         if (!virt)
173                 return -ENOMEM;
174         ipa->zero_addr = addr;
175         ipa->zero_virt = virt;
176         ipa->zero_size = IPA_MEM_MAX;
177
178         /* Verify each defined memory region is valid, and if indicated
179          * for the region, write "canary" values in the space prior to
180          * the region's base address.
181          */
182         for (mem_id = 0; mem_id < IPA_MEM_COUNT; mem_id++) {
183                 const struct ipa_mem *mem = &ipa->mem[mem_id];
184                 u16 canary_count;
185                 __le32 *canary;
186
187                 /* Validate all regions (even undefined ones) */
188                 if (!ipa_mem_valid(ipa, mem_id))
189                         goto err_dma_free;
190
191                 /* Skip over undefined regions */
192                 if (!mem->offset && !mem->size)
193                         continue;
194
195                 canary_count = mem->canary_count;
196                 if (!canary_count)
197                         continue;
198
199                 /* Write canary values in the space before the region */
200                 canary = ipa->mem_virt + ipa->mem_offset + mem->offset;
201                 do
202                         *--canary = IPA_MEM_CANARY_VAL;
203                 while (--canary_count);
204         }
205
206         /* Make sure filter and route table memory regions are valid */
207         if (!ipa_table_valid(ipa))
208                 goto err_dma_free;
209
210         /* Validate memory-related properties relevant to immediate commands */
211         if (!ipa_cmd_data_valid(ipa))
212                 goto err_dma_free;
213
214         /* Verify the microcontroller ring alignment (0 is OK too) */
215         if (ipa->mem[IPA_MEM_UC_EVENT_RING].offset % 1024) {
216                 dev_err(dev, "microcontroller ring not 1024-byte aligned\n");
217                 goto err_dma_free;
218         }
219
220         return 0;
221
222 err_dma_free:
223         dma_free_coherent(dev, IPA_MEM_MAX, ipa->zero_virt, ipa->zero_addr);
224
225         return -EINVAL;
226 }
227
228 /* Inverse of ipa_mem_config() */
229 void ipa_mem_deconfig(struct ipa *ipa)
230 {
231         struct device *dev = &ipa->pdev->dev;
232
233         dma_free_coherent(dev, ipa->zero_size, ipa->zero_virt, ipa->zero_addr);
234         ipa->zero_size = 0;
235         ipa->zero_virt = NULL;
236         ipa->zero_addr = 0;
237 }
238
239 /**
240  * ipa_mem_zero_modem() - Zero IPA-local memory regions owned by the modem
241  *
242  * Zero regions of IPA-local memory used by the modem.  These are configured
243  * (and initially zeroed) by ipa_mem_setup(), but if the modem crashes and
244  * restarts via SSR we need to re-initialize them.  A QMI message tells the
245  * modem where to find regions of IPA local memory it needs to know about
246  * (these included).
247  */
248 int ipa_mem_zero_modem(struct ipa *ipa)
249 {
250         struct gsi_trans *trans;
251
252         /* Get a transaction to zero the modem memory, modem header,
253          * and modem processing context regions.
254          */
255         trans = ipa_cmd_trans_alloc(ipa, 3);
256         if (!trans) {
257                 dev_err(&ipa->pdev->dev,
258                         "no transaction to zero modem memory\n");
259                 return -EBUSY;
260         }
261
262         ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_HEADER]);
263
264         ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_PROC_CTX]);
265
266         ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM]);
267
268         gsi_trans_commit_wait(trans);
269
270         return 0;
271 }
272
273 /**
274  * ipa_imem_init() - Initialize IMEM memory used by the IPA
275  * @ipa:        IPA pointer
276  * @addr:       Physical address of the IPA region in IMEM
277  * @size:       Size (bytes) of the IPA region in IMEM
278  *
279  * IMEM is a block of shared memory separate from system DRAM, and
280  * a portion of this memory is available for the IPA to use.  The
281  * modem accesses this memory directly, but the IPA accesses it
282  * via the IOMMU, using the AP's credentials.
283  *
284  * If this region exists (size > 0) we map it for read/write access
285  * through the IOMMU using the IPA device.
286  *
287  * Note: @addr and @size are not guaranteed to be page-aligned.
288  */
289 static int ipa_imem_init(struct ipa *ipa, unsigned long addr, size_t size)
290 {
291         struct device *dev = &ipa->pdev->dev;
292         struct iommu_domain *domain;
293         unsigned long iova;
294         phys_addr_t phys;
295         int ret;
296
297         if (!size)
298                 return 0;       /* IMEM memory not used */
299
300         domain = iommu_get_domain_for_dev(dev);
301         if (!domain) {
302                 dev_err(dev, "no IOMMU domain found for IMEM\n");
303                 return -EINVAL;
304         }
305
306         /* Align the address down and the size up to page boundaries */
307         phys = addr & PAGE_MASK;
308         size = PAGE_ALIGN(size + addr - phys);
309         iova = phys;    /* We just want a direct mapping */
310
311         ret = iommu_map(domain, iova, phys, size, IOMMU_READ | IOMMU_WRITE);
312         if (ret)
313                 return ret;
314
315         ipa->imem_iova = iova;
316         ipa->imem_size = size;
317
318         return 0;
319 }
320
321 static void ipa_imem_exit(struct ipa *ipa)
322 {
323         struct iommu_domain *domain;
324         struct device *dev;
325
326         if (!ipa->imem_size)
327                 return;
328
329         dev = &ipa->pdev->dev;
330         domain = iommu_get_domain_for_dev(dev);
331         if (domain) {
332                 size_t size;
333
334                 size = iommu_unmap(domain, ipa->imem_iova, ipa->imem_size);
335                 if (size != ipa->imem_size)
336                         dev_warn(dev, "unmapped %zu IMEM bytes, expected %lu\n",
337                                  size, ipa->imem_size);
338         } else {
339                 dev_err(dev, "couldn't get IPA IOMMU domain for IMEM\n");
340         }
341
342         ipa->imem_size = 0;
343         ipa->imem_iova = 0;
344 }
345
346 /**
347  * ipa_smem_init() - Initialize SMEM memory used by the IPA
348  * @ipa:        IPA pointer
349  * @item:       Item ID of SMEM memory
350  * @size:       Size (bytes) of SMEM memory region
351  *
352  * SMEM is a managed block of shared DRAM, from which numbered "items"
353  * can be allocated.  One item is designated for use by the IPA.
354  *
355  * The modem accesses SMEM memory directly, but the IPA accesses it
356  * via the IOMMU, using the AP's credentials.
357  *
358  * If size provided is non-zero, we allocate it and map it for
359  * access through the IOMMU.
360  *
361  * Note: @size and the item address are is not guaranteed to be page-aligned.
362  */
363 static int ipa_smem_init(struct ipa *ipa, u32 item, size_t size)
364 {
365         struct device *dev = &ipa->pdev->dev;
366         struct iommu_domain *domain;
367         unsigned long iova;
368         phys_addr_t phys;
369         phys_addr_t addr;
370         size_t actual;
371         void *virt;
372         int ret;
373
374         if (!size)
375                 return 0;       /* SMEM memory not used */
376
377         /* SMEM is memory shared between the AP and another system entity
378          * (in this case, the modem).  An allocation from SMEM is persistent
379          * until the AP reboots; there is no way to free an allocated SMEM
380          * region.  Allocation only reserves the space; to use it you need
381          * to "get" a pointer it (this implies no reference counting).
382          * The item might have already been allocated, in which case we
383          * use it unless the size isn't what we expect.
384          */
385         ret = qcom_smem_alloc(QCOM_SMEM_HOST_MODEM, item, size);
386         if (ret && ret != -EEXIST) {
387                 dev_err(dev, "error %d allocating size %zu SMEM item %u\n",
388                         ret, size, item);
389                 return ret;
390         }
391
392         /* Now get the address of the SMEM memory region */
393         virt = qcom_smem_get(QCOM_SMEM_HOST_MODEM, item, &actual);
394         if (IS_ERR(virt)) {
395                 ret = PTR_ERR(virt);
396                 dev_err(dev, "error %d getting SMEM item %u\n", ret, item);
397                 return ret;
398         }
399
400         /* In case the region was already allocated, verify the size */
401         if (ret && actual != size) {
402                 dev_err(dev, "SMEM item %u has size %zu, expected %zu\n",
403                         item, actual, size);
404                 return -EINVAL;
405         }
406
407         domain = iommu_get_domain_for_dev(dev);
408         if (!domain) {
409                 dev_err(dev, "no IOMMU domain found for SMEM\n");
410                 return -EINVAL;
411         }
412
413         /* Align the address down and the size up to a page boundary */
414         addr = qcom_smem_virt_to_phys(virt) & PAGE_MASK;
415         phys = addr & PAGE_MASK;
416         size = PAGE_ALIGN(size + addr - phys);
417         iova = phys;    /* We just want a direct mapping */
418
419         ret = iommu_map(domain, iova, phys, size, IOMMU_READ | IOMMU_WRITE);
420         if (ret)
421                 return ret;
422
423         ipa->smem_iova = iova;
424         ipa->smem_size = size;
425
426         return 0;
427 }
428
429 static void ipa_smem_exit(struct ipa *ipa)
430 {
431         struct device *dev = &ipa->pdev->dev;
432         struct iommu_domain *domain;
433
434         domain = iommu_get_domain_for_dev(dev);
435         if (domain) {
436                 size_t size;
437
438                 size = iommu_unmap(domain, ipa->smem_iova, ipa->smem_size);
439                 if (size != ipa->smem_size)
440                         dev_warn(dev, "unmapped %zu SMEM bytes, expected %lu\n",
441                                  size, ipa->smem_size);
442
443         } else {
444                 dev_err(dev, "couldn't get IPA IOMMU domain for SMEM\n");
445         }
446
447         ipa->smem_size = 0;
448         ipa->smem_iova = 0;
449 }
450
451 /* Perform memory region-related initialization */
452 int ipa_mem_init(struct ipa *ipa, const struct ipa_mem_data *mem_data)
453 {
454         struct device *dev = &ipa->pdev->dev;
455         struct resource *res;
456         int ret;
457
458         if (mem_data->local_count > IPA_MEM_COUNT) {
459                 dev_err(dev, "to many memory regions (%u > %u)\n",
460                         mem_data->local_count, IPA_MEM_COUNT);
461                 return -EINVAL;
462         }
463
464         ret = dma_set_mask_and_coherent(&ipa->pdev->dev, DMA_BIT_MASK(64));
465         if (ret) {
466                 dev_err(dev, "error %d setting DMA mask\n", ret);
467                 return ret;
468         }
469
470         res = platform_get_resource_byname(ipa->pdev, IORESOURCE_MEM,
471                                            "ipa-shared");
472         if (!res) {
473                 dev_err(dev,
474                         "DT error getting \"ipa-shared\" memory property\n");
475                 return -ENODEV;
476         }
477
478         ipa->mem_virt = memremap(res->start, resource_size(res), MEMREMAP_WC);
479         if (!ipa->mem_virt) {
480                 dev_err(dev, "unable to remap \"ipa-shared\" memory\n");
481                 return -ENOMEM;
482         }
483
484         ipa->mem_addr = res->start;
485         ipa->mem_size = resource_size(res);
486
487         /* The ipa->mem[] array is indexed by enum ipa_mem_id values */
488         ipa->mem = mem_data->local;
489
490         ret = ipa_imem_init(ipa, mem_data->imem_addr, mem_data->imem_size);
491         if (ret)
492                 goto err_unmap;
493
494         ret = ipa_smem_init(ipa, mem_data->smem_id, mem_data->smem_size);
495         if (ret)
496                 goto err_imem_exit;
497
498         return 0;
499
500 err_imem_exit:
501         ipa_imem_exit(ipa);
502 err_unmap:
503         memunmap(ipa->mem_virt);
504
505         return ret;
506 }
507
508 /* Inverse of ipa_mem_init() */
509 void ipa_mem_exit(struct ipa *ipa)
510 {
511         ipa_smem_exit(ipa);
512         ipa_imem_exit(ipa);
513         memunmap(ipa->mem_virt);
514 }