1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2019-2021 Linaro Ltd.
7 #include <linux/types.h>
8 #include <linux/bitfield.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/iommu.h>
13 #include <linux/soc/qcom/smem.h>
20 #include "ipa_table.h"
21 #include "gsi_trans.h"
23 /* "Canary" value placed between memory regions to detect overflow */
24 #define IPA_MEM_CANARY_VAL cpu_to_le32(0xdeadbeef)
26 /* SMEM host id representing the modem. */
27 #define QCOM_SMEM_HOST_MODEM 1
29 /* Add an immediate command to a transaction that zeroes a memory region */
31 ipa_mem_zero_region_add(struct gsi_trans *trans, const struct ipa_mem *mem)
33 struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
34 dma_addr_t addr = ipa->zero_addr;
39 ipa_cmd_dma_shared_mem_add(trans, mem->offset, mem->size, addr, true);
43 * ipa_mem_setup() - Set up IPA AP and modem shared memory areas
46 * Set up the shared memory regions in IPA local memory. This involves
47 * zero-filling memory regions, and in the case of header memory, telling
48 * the IPA where it's located.
50 * This function performs the initial setup of this memory. If the modem
51 * crashes, its regions are re-zeroed in ipa_mem_zero_modem().
53 * The AP informs the modem where its portions of memory are located
54 * in a QMI exchange that occurs at modem startup.
56 * There is no need for a matching ipa_mem_teardown() function.
58 * Return: 0 if successful, or a negative error code
60 int ipa_mem_setup(struct ipa *ipa)
62 dma_addr_t addr = ipa->zero_addr;
63 struct gsi_trans *trans;
68 /* Get a transaction to define the header memory region and to zero
69 * the processing context and modem memory regions.
71 trans = ipa_cmd_trans_alloc(ipa, 4);
73 dev_err(&ipa->pdev->dev, "no transaction for memory setup\n");
77 /* Initialize IPA-local header memory. The modem and AP header
78 * regions are contiguous, and initialized together.
80 offset = ipa->mem[IPA_MEM_MODEM_HEADER].offset;
81 size = ipa->mem[IPA_MEM_MODEM_HEADER].size;
82 size += ipa->mem[IPA_MEM_AP_HEADER].size;
84 ipa_cmd_hdr_init_local_add(trans, offset, size, addr);
86 ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_PROC_CTX]);
88 ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_AP_PROC_CTX]);
90 ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM]);
92 gsi_trans_commit_wait(trans);
94 /* Tell the hardware where the processing context area is located */
95 offset = ipa->mem_offset + ipa->mem[IPA_MEM_MODEM_PROC_CTX].offset;
96 val = proc_cntxt_base_addr_encoded(ipa->version, offset);
97 iowrite32(val, ipa->reg_virt + IPA_REG_LOCAL_PKT_PROC_CNTXT_OFFSET);
104 static bool ipa_mem_valid(struct ipa *ipa, enum ipa_mem_id mem_id)
106 const struct ipa_mem *mem = &ipa->mem[mem_id];
107 struct device *dev = &ipa->pdev->dev;
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);
129 #else /* !IPA_VALIDATE */
131 static bool ipa_mem_valid(struct ipa *ipa, enum ipa_mem_id mem_id)
136 #endif /*! IPA_VALIDATE */
139 * ipa_mem_config() - Configure IPA shared memory
142 * Return: 0 if successful, or a negative error code
144 int ipa_mem_config(struct ipa *ipa)
146 struct device *dev = &ipa->pdev->dev;
147 enum ipa_mem_id mem_id;
153 /* Check the advertised location and size of the shared memory area */
154 val = ioread32(ipa->reg_virt + IPA_REG_SHARED_MEM_SIZE_OFFSET);
156 /* The fields in the register are in 8 byte units */
157 ipa->mem_offset = 8 * u32_get_bits(val, SHARED_MEM_BADDR_FMASK);
158 /* Make sure the end is within the region's mapped space */
159 mem_size = 8 * u32_get_bits(val, SHARED_MEM_SIZE_FMASK);
161 /* If the sizes don't match, issue a warning */
162 if (ipa->mem_offset + mem_size < ipa->mem_size) {
163 dev_warn(dev, "limiting IPA memory size to 0x%08x\n",
165 ipa->mem_size = mem_size;
166 } else if (ipa->mem_offset + mem_size > ipa->mem_size) {
167 dev_dbg(dev, "ignoring larger reported memory size: 0x%08x\n",
171 /* Prealloc DMA memory for zeroing regions */
172 virt = dma_alloc_coherent(dev, IPA_MEM_MAX, &addr, GFP_KERNEL);
175 ipa->zero_addr = addr;
176 ipa->zero_virt = virt;
177 ipa->zero_size = IPA_MEM_MAX;
179 /* Verify each defined memory region is valid, and if indicated
180 * for the region, write "canary" values in the space prior to
181 * the region's base address.
183 for (mem_id = 0; mem_id < IPA_MEM_COUNT; mem_id++) {
184 const struct ipa_mem *mem = &ipa->mem[mem_id];
188 /* Validate all regions (even undefined ones) */
189 if (!ipa_mem_valid(ipa, mem_id))
192 /* Skip over undefined regions */
193 if (!mem->offset && !mem->size)
196 canary_count = mem->canary_count;
200 /* Write canary values in the space before the region */
201 canary = ipa->mem_virt + ipa->mem_offset + mem->offset;
203 *--canary = IPA_MEM_CANARY_VAL;
204 while (--canary_count);
207 /* Make sure filter and route table memory regions are valid */
208 if (!ipa_table_valid(ipa))
211 /* Validate memory-related properties relevant to immediate commands */
212 if (!ipa_cmd_data_valid(ipa))
215 /* Verify the microcontroller ring alignment (0 is OK too) */
216 if (ipa->mem[IPA_MEM_UC_EVENT_RING].offset % 1024) {
217 dev_err(dev, "microcontroller ring not 1024-byte aligned\n");
224 dma_free_coherent(dev, IPA_MEM_MAX, ipa->zero_virt, ipa->zero_addr);
229 /* Inverse of ipa_mem_config() */
230 void ipa_mem_deconfig(struct ipa *ipa)
232 struct device *dev = &ipa->pdev->dev;
234 dma_free_coherent(dev, ipa->zero_size, ipa->zero_virt, ipa->zero_addr);
236 ipa->zero_virt = NULL;
241 * ipa_mem_zero_modem() - Zero IPA-local memory regions owned by the modem
244 * Zero regions of IPA-local memory used by the modem. These are configured
245 * (and initially zeroed) by ipa_mem_setup(), but if the modem crashes and
246 * restarts via SSR we need to re-initialize them. A QMI message tells the
247 * modem where to find regions of IPA local memory it needs to know about
250 int ipa_mem_zero_modem(struct ipa *ipa)
252 struct gsi_trans *trans;
254 /* Get a transaction to zero the modem memory, modem header,
255 * and modem processing context regions.
257 trans = ipa_cmd_trans_alloc(ipa, 3);
259 dev_err(&ipa->pdev->dev,
260 "no transaction to zero modem memory\n");
264 ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_HEADER]);
266 ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_PROC_CTX]);
268 ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM]);
270 gsi_trans_commit_wait(trans);
276 * ipa_imem_init() - Initialize IMEM memory used by the IPA
278 * @addr: Physical address of the IPA region in IMEM
279 * @size: Size (bytes) of the IPA region in IMEM
281 * IMEM is a block of shared memory separate from system DRAM, and
282 * a portion of this memory is available for the IPA to use. The
283 * modem accesses this memory directly, but the IPA accesses it
284 * via the IOMMU, using the AP's credentials.
286 * If this region exists (size > 0) we map it for read/write access
287 * through the IOMMU using the IPA device.
289 * Note: @addr and @size are not guaranteed to be page-aligned.
291 static int ipa_imem_init(struct ipa *ipa, unsigned long addr, size_t size)
293 struct device *dev = &ipa->pdev->dev;
294 struct iommu_domain *domain;
300 return 0; /* IMEM memory not used */
302 domain = iommu_get_domain_for_dev(dev);
304 dev_err(dev, "no IOMMU domain found for IMEM\n");
308 /* Align the address down and the size up to page boundaries */
309 phys = addr & PAGE_MASK;
310 size = PAGE_ALIGN(size + addr - phys);
311 iova = phys; /* We just want a direct mapping */
313 ret = iommu_map(domain, iova, phys, size, IOMMU_READ | IOMMU_WRITE);
317 ipa->imem_iova = iova;
318 ipa->imem_size = size;
323 static void ipa_imem_exit(struct ipa *ipa)
325 struct iommu_domain *domain;
331 dev = &ipa->pdev->dev;
332 domain = iommu_get_domain_for_dev(dev);
336 size = iommu_unmap(domain, ipa->imem_iova, ipa->imem_size);
337 if (size != ipa->imem_size)
338 dev_warn(dev, "unmapped %zu IMEM bytes, expected %zu\n",
339 size, ipa->imem_size);
341 dev_err(dev, "couldn't get IPA IOMMU domain for IMEM\n");
349 * ipa_smem_init() - Initialize SMEM memory used by the IPA
351 * @item: Item ID of SMEM memory
352 * @size: Size (bytes) of SMEM memory region
354 * SMEM is a managed block of shared DRAM, from which numbered "items"
355 * can be allocated. One item is designated for use by the IPA.
357 * The modem accesses SMEM memory directly, but the IPA accesses it
358 * via the IOMMU, using the AP's credentials.
360 * If size provided is non-zero, we allocate it and map it for
361 * access through the IOMMU.
363 * Note: @size and the item address are is not guaranteed to be page-aligned.
365 static int ipa_smem_init(struct ipa *ipa, u32 item, size_t size)
367 struct device *dev = &ipa->pdev->dev;
368 struct iommu_domain *domain;
377 return 0; /* SMEM memory not used */
379 /* SMEM is memory shared between the AP and another system entity
380 * (in this case, the modem). An allocation from SMEM is persistent
381 * until the AP reboots; there is no way to free an allocated SMEM
382 * region. Allocation only reserves the space; to use it you need
383 * to "get" a pointer it (this implies no reference counting).
384 * The item might have already been allocated, in which case we
385 * use it unless the size isn't what we expect.
387 ret = qcom_smem_alloc(QCOM_SMEM_HOST_MODEM, item, size);
388 if (ret && ret != -EEXIST) {
389 dev_err(dev, "error %d allocating size %zu SMEM item %u\n",
394 /* Now get the address of the SMEM memory region */
395 virt = qcom_smem_get(QCOM_SMEM_HOST_MODEM, item, &actual);
398 dev_err(dev, "error %d getting SMEM item %u\n", ret, item);
402 /* In case the region was already allocated, verify the size */
403 if (ret && actual != size) {
404 dev_err(dev, "SMEM item %u has size %zu, expected %zu\n",
409 domain = iommu_get_domain_for_dev(dev);
411 dev_err(dev, "no IOMMU domain found for SMEM\n");
415 /* Align the address down and the size up to a page boundary */
416 addr = qcom_smem_virt_to_phys(virt) & PAGE_MASK;
417 phys = addr & PAGE_MASK;
418 size = PAGE_ALIGN(size + addr - phys);
419 iova = phys; /* We just want a direct mapping */
421 ret = iommu_map(domain, iova, phys, size, IOMMU_READ | IOMMU_WRITE);
425 ipa->smem_iova = iova;
426 ipa->smem_size = size;
431 static void ipa_smem_exit(struct ipa *ipa)
433 struct device *dev = &ipa->pdev->dev;
434 struct iommu_domain *domain;
436 domain = iommu_get_domain_for_dev(dev);
440 size = iommu_unmap(domain, ipa->smem_iova, ipa->smem_size);
441 if (size != ipa->smem_size)
442 dev_warn(dev, "unmapped %zu SMEM bytes, expected %zu\n",
443 size, ipa->smem_size);
446 dev_err(dev, "couldn't get IPA IOMMU domain for SMEM\n");
453 /* Perform memory region-related initialization */
454 int ipa_mem_init(struct ipa *ipa, const struct ipa_mem_data *mem_data)
456 struct device *dev = &ipa->pdev->dev;
457 struct resource *res;
460 if (mem_data->local_count > IPA_MEM_COUNT) {
461 dev_err(dev, "to many memory regions (%u > %u)\n",
462 mem_data->local_count, IPA_MEM_COUNT);
466 ret = dma_set_mask_and_coherent(&ipa->pdev->dev, DMA_BIT_MASK(64));
468 dev_err(dev, "error %d setting DMA mask\n", ret);
472 res = platform_get_resource_byname(ipa->pdev, IORESOURCE_MEM,
476 "DT error getting \"ipa-shared\" memory property\n");
480 ipa->mem_virt = memremap(res->start, resource_size(res), MEMREMAP_WC);
481 if (!ipa->mem_virt) {
482 dev_err(dev, "unable to remap \"ipa-shared\" memory\n");
486 ipa->mem_addr = res->start;
487 ipa->mem_size = resource_size(res);
489 /* The ipa->mem[] array is indexed by enum ipa_mem_id values */
490 ipa->mem = mem_data->local;
492 ret = ipa_imem_init(ipa, mem_data->imem_addr, mem_data->imem_size);
496 ret = ipa_smem_init(ipa, mem_data->smem_id, mem_data->smem_size);
505 memunmap(ipa->mem_virt);
510 /* Inverse of ipa_mem_init() */
511 void ipa_mem_exit(struct ipa *ipa)
515 memunmap(ipa->mem_virt);