xref: /openbmc/linux/drivers/net/ipa/ipa_mem.c (revision 94588c1b)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2019-2021 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  * There is no need for a matching ipa_mem_teardown() function.
57  *
58  * Return:	0 if successful, or a negative error code
59  */
60 int ipa_mem_setup(struct ipa *ipa)
61 {
62 	dma_addr_t addr = ipa->zero_addr;
63 	struct gsi_trans *trans;
64 	u32 offset;
65 	u16 size;
66 	u32 val;
67 
68 	/* Get a transaction to define the header memory region and to zero
69 	 * the processing context and modem memory regions.
70 	 */
71 	trans = ipa_cmd_trans_alloc(ipa, 4);
72 	if (!trans) {
73 		dev_err(&ipa->pdev->dev, "no transaction for memory setup\n");
74 		return -EBUSY;
75 	}
76 
77 	/* Initialize IPA-local header memory.  The modem and AP header
78 	 * regions are contiguous, and initialized together.
79 	 */
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;
83 
84 	ipa_cmd_hdr_init_local_add(trans, offset, size, addr);
85 
86 	ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_PROC_CTX]);
87 
88 	ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_AP_PROC_CTX]);
89 
90 	ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM]);
91 
92 	gsi_trans_commit_wait(trans);
93 
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);
98 
99 	return 0;
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  * @ipa:	IPA pointer
141  *
142  * Return:	0 if successful, or a negative error code
143  */
144 int ipa_mem_config(struct ipa *ipa)
145 {
146 	struct device *dev = &ipa->pdev->dev;
147 	enum ipa_mem_id mem_id;
148 	dma_addr_t addr;
149 	u32 mem_size;
150 	void *virt;
151 	u32 val;
152 
153 	/* Check the advertised location and size of the shared memory area */
154 	val = ioread32(ipa->reg_virt + IPA_REG_SHARED_MEM_SIZE_OFFSET);
155 
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);
160 
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",
164 			 mem_size);
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",
168 			mem_size);
169 	}
170 
171 	/* Prealloc DMA memory for zeroing regions */
172 	virt = dma_alloc_coherent(dev, IPA_MEM_MAX, &addr, GFP_KERNEL);
173 	if (!virt)
174 		return -ENOMEM;
175 	ipa->zero_addr = addr;
176 	ipa->zero_virt = virt;
177 	ipa->zero_size = IPA_MEM_MAX;
178 
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.
182 	 */
183 	for (mem_id = 0; mem_id < ipa->mem_count; mem_id++) {
184 		const struct ipa_mem *mem = &ipa->mem[mem_id];
185 		u16 canary_count;
186 		__le32 *canary;
187 
188 		/* Validate all regions (even undefined ones) */
189 		if (!ipa_mem_valid(ipa, mem_id))
190 			goto err_dma_free;
191 
192 		/* Skip over undefined regions */
193 		if (!mem->offset && !mem->size)
194 			continue;
195 
196 		canary_count = mem->canary_count;
197 		if (!canary_count)
198 			continue;
199 
200 		/* Write canary values in the space before the region */
201 		canary = ipa->mem_virt + ipa->mem_offset + mem->offset;
202 		do
203 			*--canary = IPA_MEM_CANARY_VAL;
204 		while (--canary_count);
205 	}
206 
207 	/* Make sure filter and route table memory regions are valid */
208 	if (!ipa_table_valid(ipa))
209 		goto err_dma_free;
210 
211 	/* Validate memory-related properties relevant to immediate commands */
212 	if (!ipa_cmd_data_valid(ipa))
213 		goto err_dma_free;
214 
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");
218 		goto err_dma_free;
219 	}
220 
221 	return 0;
222 
223 err_dma_free:
224 	dma_free_coherent(dev, IPA_MEM_MAX, ipa->zero_virt, ipa->zero_addr);
225 
226 	return -EINVAL;
227 }
228 
229 /* Inverse of ipa_mem_config() */
230 void ipa_mem_deconfig(struct ipa *ipa)
231 {
232 	struct device *dev = &ipa->pdev->dev;
233 
234 	dma_free_coherent(dev, ipa->zero_size, ipa->zero_virt, ipa->zero_addr);
235 	ipa->zero_size = 0;
236 	ipa->zero_virt = NULL;
237 	ipa->zero_addr = 0;
238 }
239 
240 /**
241  * ipa_mem_zero_modem() - Zero IPA-local memory regions owned by the modem
242  * @ipa:	IPA pointer
243  *
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
248  * (these included).
249  */
250 int ipa_mem_zero_modem(struct ipa *ipa)
251 {
252 	struct gsi_trans *trans;
253 
254 	/* Get a transaction to zero the modem memory, modem header,
255 	 * and modem processing context regions.
256 	 */
257 	trans = ipa_cmd_trans_alloc(ipa, 3);
258 	if (!trans) {
259 		dev_err(&ipa->pdev->dev,
260 			"no transaction to zero modem memory\n");
261 		return -EBUSY;
262 	}
263 
264 	ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_HEADER]);
265 
266 	ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM_PROC_CTX]);
267 
268 	ipa_mem_zero_region_add(trans, &ipa->mem[IPA_MEM_MODEM]);
269 
270 	gsi_trans_commit_wait(trans);
271 
272 	return 0;
273 }
274 
275 /**
276  * ipa_imem_init() - Initialize IMEM memory used by the IPA
277  * @ipa:	IPA pointer
278  * @addr:	Physical address of the IPA region in IMEM
279  * @size:	Size (bytes) of the IPA region in IMEM
280  *
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.
285  *
286  * If this region exists (size > 0) we map it for read/write access
287  * through the IOMMU using the IPA device.
288  *
289  * Note: @addr and @size are not guaranteed to be page-aligned.
290  */
291 static int ipa_imem_init(struct ipa *ipa, unsigned long addr, size_t size)
292 {
293 	struct device *dev = &ipa->pdev->dev;
294 	struct iommu_domain *domain;
295 	unsigned long iova;
296 	phys_addr_t phys;
297 	int ret;
298 
299 	if (!size)
300 		return 0;	/* IMEM memory not used */
301 
302 	domain = iommu_get_domain_for_dev(dev);
303 	if (!domain) {
304 		dev_err(dev, "no IOMMU domain found for IMEM\n");
305 		return -EINVAL;
306 	}
307 
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 */
312 
313 	ret = iommu_map(domain, iova, phys, size, IOMMU_READ | IOMMU_WRITE);
314 	if (ret)
315 		return ret;
316 
317 	ipa->imem_iova = iova;
318 	ipa->imem_size = size;
319 
320 	return 0;
321 }
322 
323 static void ipa_imem_exit(struct ipa *ipa)
324 {
325 	struct iommu_domain *domain;
326 	struct device *dev;
327 
328 	if (!ipa->imem_size)
329 		return;
330 
331 	dev = &ipa->pdev->dev;
332 	domain = iommu_get_domain_for_dev(dev);
333 	if (domain) {
334 		size_t size;
335 
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);
340 	} else {
341 		dev_err(dev, "couldn't get IPA IOMMU domain for IMEM\n");
342 	}
343 
344 	ipa->imem_size = 0;
345 	ipa->imem_iova = 0;
346 }
347 
348 /**
349  * ipa_smem_init() - Initialize SMEM memory used by the IPA
350  * @ipa:	IPA pointer
351  * @item:	Item ID of SMEM memory
352  * @size:	Size (bytes) of SMEM memory region
353  *
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.
356  *
357  * The modem accesses SMEM memory directly, but the IPA accesses it
358  * via the IOMMU, using the AP's credentials.
359  *
360  * If size provided is non-zero, we allocate it and map it for
361  * access through the IOMMU.
362  *
363  * Note: @size and the item address are is not guaranteed to be page-aligned.
364  */
365 static int ipa_smem_init(struct ipa *ipa, u32 item, size_t size)
366 {
367 	struct device *dev = &ipa->pdev->dev;
368 	struct iommu_domain *domain;
369 	unsigned long iova;
370 	phys_addr_t phys;
371 	phys_addr_t addr;
372 	size_t actual;
373 	void *virt;
374 	int ret;
375 
376 	if (!size)
377 		return 0;	/* SMEM memory not used */
378 
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.
386 	 */
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",
390 			ret, size, item);
391 		return ret;
392 	}
393 
394 	/* Now get the address of the SMEM memory region */
395 	virt = qcom_smem_get(QCOM_SMEM_HOST_MODEM, item, &actual);
396 	if (IS_ERR(virt)) {
397 		ret = PTR_ERR(virt);
398 		dev_err(dev, "error %d getting SMEM item %u\n", ret, item);
399 		return ret;
400 	}
401 
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",
405 			item, actual, size);
406 		return -EINVAL;
407 	}
408 
409 	domain = iommu_get_domain_for_dev(dev);
410 	if (!domain) {
411 		dev_err(dev, "no IOMMU domain found for SMEM\n");
412 		return -EINVAL;
413 	}
414 
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 */
420 
421 	ret = iommu_map(domain, iova, phys, size, IOMMU_READ | IOMMU_WRITE);
422 	if (ret)
423 		return ret;
424 
425 	ipa->smem_iova = iova;
426 	ipa->smem_size = size;
427 
428 	return 0;
429 }
430 
431 static void ipa_smem_exit(struct ipa *ipa)
432 {
433 	struct device *dev = &ipa->pdev->dev;
434 	struct iommu_domain *domain;
435 
436 	domain = iommu_get_domain_for_dev(dev);
437 	if (domain) {
438 		size_t size;
439 
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);
444 
445 	} else {
446 		dev_err(dev, "couldn't get IPA IOMMU domain for SMEM\n");
447 	}
448 
449 	ipa->smem_size = 0;
450 	ipa->smem_iova = 0;
451 }
452 
453 /* Perform memory region-related initialization */
454 int ipa_mem_init(struct ipa *ipa, const struct ipa_mem_data *mem_data)
455 {
456 	struct device *dev = &ipa->pdev->dev;
457 	struct resource *res;
458 	int ret;
459 
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);
463 		return -EINVAL;
464 	}
465 
466 	ret = dma_set_mask_and_coherent(&ipa->pdev->dev, DMA_BIT_MASK(64));
467 	if (ret) {
468 		dev_err(dev, "error %d setting DMA mask\n", ret);
469 		return ret;
470 	}
471 
472 	res = platform_get_resource_byname(ipa->pdev, IORESOURCE_MEM,
473 					   "ipa-shared");
474 	if (!res) {
475 		dev_err(dev,
476 			"DT error getting \"ipa-shared\" memory property\n");
477 		return -ENODEV;
478 	}
479 
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");
483 		return -ENOMEM;
484 	}
485 
486 	ipa->mem_addr = res->start;
487 	ipa->mem_size = resource_size(res);
488 
489 	/* The ipa->mem[] array is indexed by enum ipa_mem_id values */
490 	ipa->mem_count = mem_data->local_count;
491 	ipa->mem = mem_data->local;
492 
493 	ret = ipa_imem_init(ipa, mem_data->imem_addr, mem_data->imem_size);
494 	if (ret)
495 		goto err_unmap;
496 
497 	ret = ipa_smem_init(ipa, mem_data->smem_id, mem_data->smem_size);
498 	if (ret)
499 		goto err_imem_exit;
500 
501 	return 0;
502 
503 err_imem_exit:
504 	ipa_imem_exit(ipa);
505 err_unmap:
506 	memunmap(ipa->mem_virt);
507 
508 	return ret;
509 }
510 
511 /* Inverse of ipa_mem_init() */
512 void ipa_mem_exit(struct ipa *ipa)
513 {
514 	ipa_smem_exit(ipa);
515 	ipa_imem_exit(ipa);
516 	memunmap(ipa->mem_virt);
517 }
518