1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2019-2020 Linaro Ltd.
7 #include <linux/types.h>
8 #include <linux/bitfield.h>
10 #include <linux/dma-mapping.h>
18 #include "ipa_table.h"
19 #include "gsi_trans.h"
21 /* "Canary" value placed between memory regions to detect overflow */
22 #define IPA_MEM_CANARY_VAL cpu_to_le32(0xdeadbeef)
24 /* Add an immediate command to a transaction that zeroes a memory region */
26 ipa_mem_zero_region_add(struct gsi_trans *trans, const struct ipa_mem *mem)
28 struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
29 dma_addr_t addr = ipa->zero_addr;
34 ipa_cmd_dma_shared_mem_add(trans, mem->offset, mem->size, addr, true);
38 * ipa_mem_setup() - Set up IPA AP and modem shared memory areas
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.
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().
47 * The AP informs the modem where its portions of memory are located
48 * in a QMI exchange that occurs at modem startup.
50 * @Return: 0 if successful, or a negative error code
52 int ipa_mem_setup(struct ipa *ipa)
54 dma_addr_t addr = ipa->zero_addr;
55 struct gsi_trans *trans;
59 /* Get a transaction to define the header memory region and to zero
60 * the processing context and modem memory regions.
62 trans = ipa_cmd_trans_alloc(ipa, 4);
64 dev_err(&ipa->pdev->dev, "no transaction for memory setup\n");
68 /* Initialize IPA-local header memory. The modem and AP header
69 * regions are contiguous, and initialized together.
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;
75 ipa_cmd_hdr_init_local_add(trans, offset, size, addr);
77 ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_PROC_CTX]);
79 ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_AP_PROC_CTX]);
81 ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM]);
83 gsi_trans_commit_wait(trans);
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);
92 void ipa_mem_teardown(struct ipa *ipa)
99 static bool ipa_mem_valid(struct ipa *ipa, enum ipa_mem_id mem_id)
101 const struct ipa_mem *mem = &ipa->mem[mem_id];
102 struct device *dev = &ipa->pdev->dev;
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);
124 #else /* !IPA_VALIDATE */
126 static bool ipa_mem_valid(struct ipa *ipa, enum ipa_mem_id mem_id)
131 #endif /*! IPA_VALIDATE */
134 * ipa_mem_config() - Configure IPA shared memory
136 * @Return: 0 if successful, or a negative error code
138 int ipa_mem_config(struct ipa *ipa)
140 struct device *dev = &ipa->pdev->dev;
141 enum ipa_mem_id mem_id;
147 /* Check the advertised location and size of the shared memory area */
148 val = ioread32(ipa->reg_virt + IPA_REG_SHARED_MEM_SIZE_OFFSET);
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);
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",
159 } else if (ipa->mem_offset + mem_size < ipa->mem_size) {
160 dev_warn(dev, "limiting IPA memory size to 0x%08x\n",
162 ipa->mem_size = mem_size;
165 /* Prealloc DMA memory for zeroing regions */
166 virt = dma_alloc_coherent(dev, IPA_MEM_MAX, &addr, GFP_KERNEL);
169 ipa->zero_addr = addr;
170 ipa->zero_virt = virt;
171 ipa->zero_size = IPA_MEM_MAX;
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.
177 for (mem_id = 0; mem_id < IPA_MEM_COUNT; mem_id++) {
178 const struct ipa_mem *mem = &ipa->mem[mem_id];
182 /* Validate all regions (even undefined ones) */
183 if (!ipa_mem_valid(ipa, mem_id))
186 /* Skip over undefined regions */
187 if (!mem->offset && !mem->size)
190 canary_count = mem->canary_count;
194 /* Write canary values in the space before the region */
195 canary = ipa->mem_virt + ipa->mem_offset + mem->offset;
197 *--canary = IPA_MEM_CANARY_VAL;
198 while (--canary_count);
201 /* Make sure filter and route table memory regions are valid */
202 if (!ipa_table_valid(ipa))
205 /* Validate memory-related properties relevant to immediate commands */
206 if (!ipa_cmd_data_valid(ipa))
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");
218 dma_free_coherent(dev, IPA_MEM_MAX, ipa->zero_virt, ipa->zero_addr);
223 /* Inverse of ipa_mem_config() */
224 void ipa_mem_deconfig(struct ipa *ipa)
226 struct device *dev = &ipa->pdev->dev;
228 dma_free_coherent(dev, ipa->zero_size, ipa->zero_virt, ipa->zero_addr);
230 ipa->zero_virt = NULL;
235 * ipa_mem_zero_modem() - Zero IPA-local memory regions owned by the modem
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
243 int ipa_mem_zero_modem(struct ipa *ipa)
245 struct gsi_trans *trans;
247 /* Get a transaction to zero the modem memory, modem header,
248 * and modem processing context regions.
250 trans = ipa_cmd_trans_alloc(ipa, 3);
252 dev_err(&ipa->pdev->dev,
253 "no transaction to zero modem memory\n");
257 ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_HEADER]);
259 ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_PROC_CTX]);
261 ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM]);
263 gsi_trans_commit_wait(trans);
268 /* Perform memory region-related initialization */
269 int ipa_mem_init(struct ipa *ipa, u32 count, const struct ipa_mem *mem)
271 struct device *dev = &ipa->pdev->dev;
272 struct resource *res;
275 if (count > IPA_MEM_COUNT) {
276 dev_err(dev, "to many memory regions (%u > %u)\n",
277 count, IPA_MEM_COUNT);
281 ret = dma_set_mask_and_coherent(&ipa->pdev->dev, DMA_BIT_MASK(64));
283 dev_err(dev, "error %d setting DMA mask\n", ret);
287 res = platform_get_resource_byname(ipa->pdev, IORESOURCE_MEM,
291 "DT error getting \"ipa-shared\" memory property\n");
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");
301 ipa->mem_addr = res->start;
302 ipa->mem_size = resource_size(res);
304 /* The ipa->mem[] array is indexed by enum ipa_mem_id values */
310 /* Inverse of ipa_mem_init() */
311 void ipa_mem_exit(struct ipa *ipa)
313 memunmap(ipa->mem_virt);