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