Lines Matching +full:ipa +full:- +full:modem +full:- +full:to +full:- +full:ap

1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2019-2024 Linaro Ltd.
7 #include <linux/dma-mapping.h>
16 #include "ipa.h"
23 /* "Canary" value placed between memory regions to detect overflow */
26 /* SMEM host id representing the modem. */
31 const struct ipa_mem *ipa_mem_find(struct ipa *ipa, enum ipa_mem_id mem_id)
35 for (i = 0; i < ipa->mem_count; i++) {
36 const struct ipa_mem *mem = &ipa->mem[i];
38 if (mem->id == mem_id)
45 /* Add an immediate command to a transaction that zeroes a memory region */
49 struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
50 const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id);
51 dma_addr_t addr = ipa->zero_addr;
53 if (!mem->size)
56 ipa_cmd_dma_shared_mem_add(trans, mem->offset, mem->size, addr, true);
60 * ipa_mem_setup() - Set up IPA AP and modem shared memory areas
61 * @ipa: IPA pointer
63 * Set up the shared memory regions in IPA local memory. This involves
64 * zero-filling memory regions, and in the case of header memory, telling
65 * the IPA where it's located.
67 * This function performs the initial setup of this memory. If the modem
68 * crashes, its regions are re-zeroed in ipa_mem_zero_modem().
70 * The AP informs the modem where its portions of memory are located
71 * in a QMI exchange that occurs at modem startup.
77 int ipa_mem_setup(struct ipa *ipa)
79 dma_addr_t addr = ipa->zero_addr;
87 /* Get a transaction to define the header memory region and to zero
88 * the processing context and modem memory regions.
90 trans = ipa_cmd_trans_alloc(ipa, 4);
92 dev_err(ipa->dev, "no transaction for memory setup\n");
93 return -EBUSY;
96 /* Initialize IPA-local header memory. The AP header region, if
97 * present, is contiguous with and follows the modem header region,
100 mem = ipa_mem_find(ipa, IPA_MEM_MODEM_HEADER);
101 offset = mem->offset;
102 size = mem->size;
103 mem = ipa_mem_find(ipa, IPA_MEM_AP_HEADER);
105 size += mem->size;
116 mem = ipa_mem_find(ipa, IPA_MEM_MODEM_PROC_CTX);
117 offset = ipa->mem_offset + mem->offset;
119 reg = ipa_reg(ipa, LOCAL_PKT_PROC_CNTXT);
121 iowrite32(val, ipa->reg_virt + reg_offset(reg));
126 /* Is the given memory region ID is valid for the current IPA version? */
127 static bool ipa_mem_id_valid(struct ipa *ipa, enum ipa_mem_id mem_id)
129 enum ipa_version version = ipa->version;
188 static bool ipa_mem_id_required(struct ipa *ipa, enum ipa_mem_id mem_id)
209 return ipa->version >= IPA_VERSION_4_0;
212 return ipa->version >= IPA_VERSION_4_0 &&
213 ipa->version != IPA_VERSION_5_0;
220 static bool ipa_mem_valid_one(struct ipa *ipa, const struct ipa_mem *mem)
222 enum ipa_mem_id mem_id = mem->id;
223 struct device *dev = ipa->dev;
226 /* Make sure the memory region is valid for this version of IPA */
227 if (!ipa_mem_id_valid(ipa, mem_id)) {
232 if (!mem->size && !mem->canary_count) {
237 /* Other than modem memory, sizes must be a multiple of 8 */
239 if (mem->size % size_multiple)
242 else if (mem->offset % 8)
243 dev_err(dev, "region %u offset not 8-byte aligned\n", mem_id);
244 else if (mem->offset < mem->canary_count * sizeof(__le32))
246 mem_id, mem->canary_count);
247 else if (mem_id == IPA_MEM_END_MARKER && mem->size)
248 dev_err(dev, "non-zero end marker region size\n");
256 static bool ipa_mem_valid(struct ipa *ipa, const struct ipa_mem_data *mem_data)
259 struct device *dev = ipa->dev;
263 if (mem_data->local_count > IPA_MEM_COUNT) {
265 mem_data->local_count, IPA_MEM_COUNT);
269 for (i = 0; i < mem_data->local_count; i++) {
270 const struct ipa_mem *mem = &mem_data->local[i];
272 if (__test_and_set_bit(mem->id, regions)) {
273 dev_err(dev, "duplicate memory region %u\n", mem->id);
277 /* Defined regions have non-zero size and/or canary count */
278 if (!ipa_mem_valid_one(ipa, mem))
284 if (ipa_mem_id_required(ipa, mem_id))
292 /* Do all memory regions fit within the IPA local memory? */
293 static bool ipa_mem_size_valid(struct ipa *ipa)
295 struct device *dev = ipa->dev;
296 u32 limit = ipa->mem_size;
299 for (i = 0; i < ipa->mem_count; i++) {
300 const struct ipa_mem *mem = &ipa->mem[i];
302 if (mem->offset + mem->size <= limit)
306 mem->id, limit);
315 * ipa_mem_config() - Configure IPA shared memory
316 * @ipa: IPA pointer
320 int ipa_mem_config(struct ipa *ipa)
322 struct device *dev = ipa->dev;
332 reg = ipa_reg(ipa, SHARED_MEM_SIZE);
333 val = ioread32(ipa->reg_virt + reg_offset(reg));
336 ipa->mem_offset = 8 * reg_decode(reg, MEM_BADDR, val);
342 if (ipa->mem_offset + mem_size < ipa->mem_size) {
343 dev_warn(dev, "limiting IPA memory size to 0x%08x\n",
345 ipa->mem_size = mem_size;
346 } else if (ipa->mem_offset + mem_size > ipa->mem_size) {
352 if (!ipa_mem_size_valid(ipa))
353 return -EINVAL;
358 return -ENOMEM;
359 ipa->zero_addr = addr;
360 ipa->zero_virt = virt;
361 ipa->zero_size = IPA_MEM_MAX;
364 * space prior to the region's base address if indicated.
366 for (i = 0; i < ipa->mem_count; i++) {
367 u16 canary_count = ipa->mem[i].canary_count;
374 canary = ipa->mem_virt + ipa->mem_offset + ipa->mem[i].offset;
376 *--canary = IPA_MEM_CANARY_VAL;
377 while (--canary_count);
381 mem = ipa_mem_find(ipa, IPA_MEM_UC_EVENT_RING);
382 if (mem && mem->offset % 1024) {
383 dev_err(dev, "microcontroller ring not 1024-byte aligned\n");
390 dma_free_coherent(dev, IPA_MEM_MAX, ipa->zero_virt, ipa->zero_addr);
392 return -EINVAL;
396 void ipa_mem_deconfig(struct ipa *ipa)
398 struct device *dev = ipa->dev;
400 dma_free_coherent(dev, ipa->zero_size, ipa->zero_virt, ipa->zero_addr);
401 ipa->zero_size = 0;
402 ipa->zero_virt = NULL;
403 ipa->zero_addr = 0;
407 * ipa_mem_zero_modem() - Zero IPA-local memory regions owned by the modem
408 * @ipa: IPA pointer
410 * Zero regions of IPA-local memory used by the modem. These are configured
411 * (and initially zeroed) by ipa_mem_setup(), but if the modem crashes and
412 * restarts via SSR we need to re-initialize them. A QMI message tells the
413 * modem where to find regions of IPA local memory it needs to know about
416 int ipa_mem_zero_modem(struct ipa *ipa)
420 /* Get a transaction to zero the modem memory, modem header,
421 * and modem processing context regions.
423 trans = ipa_cmd_trans_alloc(ipa, 3);
425 dev_err(ipa->dev, "no transaction to zero modem memory\n");
426 return -EBUSY;
439 * ipa_imem_init() - Initialize IMEM memory used by the IPA
440 * @ipa: IPA pointer
441 * @addr: Physical address of the IPA region in IMEM
442 * @size: Size (bytes) of the IPA region in IMEM
445 * a portion of this memory is available for the IPA to use. The
446 * modem accesses this memory directly, but the IPA accesses it
447 * via the IOMMU, using the AP's credentials.
450 * through the IOMMU using the IPA device.
452 * Note: @addr and @size are not guaranteed to be page-aligned.
454 static int ipa_imem_init(struct ipa *ipa, unsigned long addr, size_t size)
456 struct device *dev = ipa->dev;
468 return -EINVAL;
471 /* Align the address down and the size up to page boundaries */
473 size = PAGE_ALIGN(size + addr - phys);
481 ipa->imem_iova = iova;
482 ipa->imem_size = size;
487 static void ipa_imem_exit(struct ipa *ipa)
489 struct device *dev = ipa->dev;
492 if (!ipa->imem_size)
499 size = iommu_unmap(domain, ipa->imem_iova, ipa->imem_size);
500 if (size != ipa->imem_size)
502 size, ipa->imem_size);
504 dev_err(dev, "couldn't get IPA IOMMU domain for IMEM\n");
507 ipa->imem_size = 0;
508 ipa->imem_iova = 0;
512 * ipa_smem_init() - Initialize SMEM memory used by the IPA
513 * @ipa: IPA pointer
517 * can be allocated. One item is designated for use by the IPA.
519 * The modem accesses SMEM memory directly, but the IPA accesses it
520 * via the IOMMU, using the AP's credentials.
522 * If size provided is non-zero, we allocate it and map it for
525 * Note: @size and the item address are is not guaranteed to be page-aligned.
527 static int ipa_smem_init(struct ipa *ipa, size_t size)
529 struct device *dev = ipa->dev;
541 /* SMEM is memory shared between the AP and another system entity
542 * (in this case, the modem). An allocation from SMEM is persistent
543 * until the AP reboots; there is no way to free an allocated SMEM
544 * region. Allocation only reserves the space; to use it you need
545 * to "get" a pointer it (this does not imply reference counting).
550 if (ret && ret != -EEXIST) {
568 return -EINVAL;
574 return -EINVAL;
577 /* Align the address down and the size up to a page boundary */
580 size = PAGE_ALIGN(size + addr - phys);
588 ipa->smem_iova = iova;
589 ipa->smem_size = size;
594 static void ipa_smem_exit(struct ipa *ipa)
596 struct device *dev = ipa->dev;
603 size = iommu_unmap(domain, ipa->smem_iova, ipa->smem_size);
604 if (size != ipa->smem_size)
606 size, ipa->smem_size);
609 dev_err(dev, "couldn't get IPA IOMMU domain for SMEM\n");
612 ipa->smem_size = 0;
613 ipa->smem_iova = 0;
616 /* Perform memory region-related initialization */
617 int ipa_mem_init(struct ipa *ipa, struct platform_device *pdev,
620 struct device *dev = &pdev->dev;
625 if (!ipa_mem_valid(ipa, mem_data))
626 return -EINVAL;
628 ipa->mem_count = mem_data->local_count;
629 ipa->mem = mem_data->local;
632 if (!ipa_table_mem_valid(ipa, false))
633 return -EINVAL;
634 if (!ipa_table_mem_valid(ipa, true))
635 return -EINVAL;
643 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ipa-shared");
646 "DT error getting \"ipa-shared\" memory property\n");
647 return -ENODEV;
650 ipa->mem_virt = memremap(res->start, resource_size(res), MEMREMAP_WC);
651 if (!ipa->mem_virt) {
652 dev_err(dev, "unable to remap \"ipa-shared\" memory\n");
653 return -ENOMEM;
656 ipa->mem_addr = res->start;
657 ipa->mem_size = resource_size(res);
659 ret = ipa_imem_init(ipa, mem_data->imem_addr, mem_data->imem_size);
663 ret = ipa_smem_init(ipa, mem_data->smem_size);
670 ipa_imem_exit(ipa);
672 memunmap(ipa->mem_virt);
678 void ipa_mem_exit(struct ipa *ipa)
680 ipa_smem_exit(ipa);
681 ipa_imem_exit(ipa);
682 memunmap(ipa->mem_virt);