Lines Matching +full:ipa +full:- +full:ap +full:- +full:to +full:- +full:modem
1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2019-2022 Linaro Ltd.
10 #include <linux/dma-mapping.h>
15 #include "ipa.h"
23 /* "Canary" value placed between memory regions to detect overflow */
26 /* SMEM host id representing the modem. */
29 const struct ipa_mem *ipa_mem_find(struct ipa *ipa, enum ipa_mem_id mem_id) in ipa_mem_find() argument
33 for (i = 0; i < ipa->mem_count; i++) { in ipa_mem_find()
34 const struct ipa_mem *mem = &ipa->mem[i]; in ipa_mem_find()
36 if (mem->id == mem_id) in ipa_mem_find()
43 /* Add an immediate command to a transaction that zeroes a memory region */
47 struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi); in ipa_mem_zero_region_add() local
48 const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id); in ipa_mem_zero_region_add()
49 dma_addr_t addr = ipa->zero_addr; in ipa_mem_zero_region_add()
51 if (!mem->size) in ipa_mem_zero_region_add()
54 ipa_cmd_dma_shared_mem_add(trans, mem->offset, mem->size, addr, true); in ipa_mem_zero_region_add()
58 * ipa_mem_setup() - Set up IPA AP and modem shared memory areas
59 * @ipa: IPA pointer
61 * Set up the shared memory regions in IPA local memory. This involves
62 * zero-filling memory regions, and in the case of header memory, telling
63 * the IPA where it's located.
65 * This function performs the initial setup of this memory. If the modem
66 * crashes, its regions are re-zeroed in ipa_mem_zero_modem().
68 * The AP informs the modem where its portions of memory are located
69 * in a QMI exchange that occurs at modem startup.
75 int ipa_mem_setup(struct ipa *ipa) in ipa_mem_setup() argument
77 dma_addr_t addr = ipa->zero_addr; in ipa_mem_setup()
85 /* Get a transaction to define the header memory region and to zero in ipa_mem_setup()
86 * the processing context and modem memory regions. in ipa_mem_setup()
88 trans = ipa_cmd_trans_alloc(ipa, 4); in ipa_mem_setup()
90 dev_err(&ipa->pdev->dev, "no transaction for memory setup\n"); in ipa_mem_setup()
91 return -EBUSY; in ipa_mem_setup()
94 /* Initialize IPA-local header memory. The AP header region, if in ipa_mem_setup()
95 * present, is contiguous with and follows the modem header region, in ipa_mem_setup()
98 mem = ipa_mem_find(ipa, IPA_MEM_MODEM_HEADER); in ipa_mem_setup()
99 offset = mem->offset; in ipa_mem_setup()
100 size = mem->size; in ipa_mem_setup()
101 mem = ipa_mem_find(ipa, IPA_MEM_AP_HEADER); in ipa_mem_setup()
103 size += mem->size; in ipa_mem_setup()
114 mem = ipa_mem_find(ipa, IPA_MEM_MODEM_PROC_CTX); in ipa_mem_setup()
115 offset = ipa->mem_offset + mem->offset; in ipa_mem_setup()
117 reg = ipa_reg(ipa, LOCAL_PKT_PROC_CNTXT); in ipa_mem_setup()
119 iowrite32(val, ipa->reg_virt + ipa_reg_offset(reg)); in ipa_mem_setup()
124 /* Is the given memory region ID is valid for the current IPA version? */
125 static bool ipa_mem_id_valid(struct ipa *ipa, enum ipa_mem_id mem_id) in ipa_mem_id_valid() argument
127 enum ipa_version version = ipa->version; in ipa_mem_id_valid()
180 static bool ipa_mem_id_required(struct ipa *ipa, enum ipa_mem_id mem_id) in ipa_mem_id_required() argument
202 return ipa->version >= IPA_VERSION_4_0; in ipa_mem_id_required()
209 static bool ipa_mem_valid_one(struct ipa *ipa, const struct ipa_mem *mem) in ipa_mem_valid_one() argument
211 struct device *dev = &ipa->pdev->dev; in ipa_mem_valid_one()
212 enum ipa_mem_id mem_id = mem->id; in ipa_mem_valid_one()
215 /* Make sure the memory region is valid for this version of IPA */ in ipa_mem_valid_one()
216 if (!ipa_mem_id_valid(ipa, mem_id)) { in ipa_mem_valid_one()
221 if (!mem->size && !mem->canary_count) { in ipa_mem_valid_one()
226 /* Other than modem memory, sizes must be a multiple of 8 */ in ipa_mem_valid_one()
228 if (mem->size % size_multiple) in ipa_mem_valid_one()
231 else if (mem->offset % 8) in ipa_mem_valid_one()
232 dev_err(dev, "region %u offset not 8-byte aligned\n", mem_id); in ipa_mem_valid_one()
233 else if (mem->offset < mem->canary_count * sizeof(__le32)) in ipa_mem_valid_one()
235 mem_id, mem->canary_count); in ipa_mem_valid_one()
236 else if (mem_id == IPA_MEM_END_MARKER && mem->size) in ipa_mem_valid_one()
237 dev_err(dev, "non-zero end marker region size\n"); in ipa_mem_valid_one()
245 static bool ipa_mem_valid(struct ipa *ipa, const struct ipa_mem_data *mem_data) in ipa_mem_valid() argument
248 struct device *dev = &ipa->pdev->dev; in ipa_mem_valid()
252 if (mem_data->local_count > IPA_MEM_COUNT) { in ipa_mem_valid()
254 mem_data->local_count, IPA_MEM_COUNT); in ipa_mem_valid()
258 for (i = 0; i < mem_data->local_count; i++) { in ipa_mem_valid()
259 const struct ipa_mem *mem = &mem_data->local[i]; in ipa_mem_valid()
261 if (__test_and_set_bit(mem->id, regions)) { in ipa_mem_valid()
262 dev_err(dev, "duplicate memory region %u\n", mem->id); in ipa_mem_valid()
266 /* Defined regions have non-zero size and/or canary count */ in ipa_mem_valid()
267 if (!ipa_mem_valid_one(ipa, mem)) in ipa_mem_valid()
273 if (ipa_mem_id_required(ipa, mem_id)) in ipa_mem_valid()
281 /* Do all memory regions fit within the IPA local memory? */
282 static bool ipa_mem_size_valid(struct ipa *ipa) in ipa_mem_size_valid() argument
284 struct device *dev = &ipa->pdev->dev; in ipa_mem_size_valid()
285 u32 limit = ipa->mem_size; in ipa_mem_size_valid()
288 for (i = 0; i < ipa->mem_count; i++) { in ipa_mem_size_valid()
289 const struct ipa_mem *mem = &ipa->mem[i]; in ipa_mem_size_valid()
291 if (mem->offset + mem->size <= limit) in ipa_mem_size_valid()
295 mem->id, limit); in ipa_mem_size_valid()
304 * ipa_mem_config() - Configure IPA shared memory
305 * @ipa: IPA pointer
309 int ipa_mem_config(struct ipa *ipa) in ipa_mem_config() argument
311 struct device *dev = &ipa->pdev->dev; in ipa_mem_config()
321 reg = ipa_reg(ipa, SHARED_MEM_SIZE); in ipa_mem_config()
322 val = ioread32(ipa->reg_virt + ipa_reg_offset(reg)); in ipa_mem_config()
325 ipa->mem_offset = 8 * ipa_reg_decode(reg, MEM_BADDR, val); in ipa_mem_config()
331 if (ipa->mem_offset + mem_size < ipa->mem_size) { in ipa_mem_config()
332 dev_warn(dev, "limiting IPA memory size to 0x%08x\n", in ipa_mem_config()
334 ipa->mem_size = mem_size; in ipa_mem_config()
335 } else if (ipa->mem_offset + mem_size > ipa->mem_size) { in ipa_mem_config()
341 if (!ipa_mem_size_valid(ipa)) in ipa_mem_config()
342 return -EINVAL; in ipa_mem_config()
347 return -ENOMEM; in ipa_mem_config()
348 ipa->zero_addr = addr; in ipa_mem_config()
349 ipa->zero_virt = virt; in ipa_mem_config()
350 ipa->zero_size = IPA_MEM_MAX; in ipa_mem_config()
353 * space prior to the region's base address if indicated. in ipa_mem_config()
355 for (i = 0; i < ipa->mem_count; i++) { in ipa_mem_config()
356 u16 canary_count = ipa->mem[i].canary_count; in ipa_mem_config()
363 canary = ipa->mem_virt + ipa->mem_offset + ipa->mem[i].offset; in ipa_mem_config()
365 *--canary = IPA_MEM_CANARY_VAL; in ipa_mem_config()
366 while (--canary_count); in ipa_mem_config()
370 if (!ipa_table_valid(ipa)) in ipa_mem_config()
373 /* Validate memory-related properties relevant to immediate commands */ in ipa_mem_config()
374 if (!ipa_cmd_data_valid(ipa)) in ipa_mem_config()
378 mem = ipa_mem_find(ipa, IPA_MEM_UC_EVENT_RING); in ipa_mem_config()
379 if (mem && mem->offset % 1024) { in ipa_mem_config()
380 dev_err(dev, "microcontroller ring not 1024-byte aligned\n"); in ipa_mem_config()
387 dma_free_coherent(dev, IPA_MEM_MAX, ipa->zero_virt, ipa->zero_addr); in ipa_mem_config()
389 return -EINVAL; in ipa_mem_config()
393 void ipa_mem_deconfig(struct ipa *ipa) in ipa_mem_deconfig() argument
395 struct device *dev = &ipa->pdev->dev; in ipa_mem_deconfig()
397 dma_free_coherent(dev, ipa->zero_size, ipa->zero_virt, ipa->zero_addr); in ipa_mem_deconfig()
398 ipa->zero_size = 0; in ipa_mem_deconfig()
399 ipa->zero_virt = NULL; in ipa_mem_deconfig()
400 ipa->zero_addr = 0; in ipa_mem_deconfig()
404 * ipa_mem_zero_modem() - Zero IPA-local memory regions owned by the modem
405 * @ipa: IPA pointer
407 * Zero regions of IPA-local memory used by the modem. These are configured
408 * (and initially zeroed) by ipa_mem_setup(), but if the modem crashes and
409 * restarts via SSR we need to re-initialize them. A QMI message tells the
410 * modem where to find regions of IPA local memory it needs to know about
413 int ipa_mem_zero_modem(struct ipa *ipa) in ipa_mem_zero_modem() argument
417 /* Get a transaction to zero the modem memory, modem header, in ipa_mem_zero_modem()
418 * and modem processing context regions. in ipa_mem_zero_modem()
420 trans = ipa_cmd_trans_alloc(ipa, 3); in ipa_mem_zero_modem()
422 dev_err(&ipa->pdev->dev, in ipa_mem_zero_modem()
423 "no transaction to zero modem memory\n"); in ipa_mem_zero_modem()
424 return -EBUSY; in ipa_mem_zero_modem()
437 * ipa_imem_init() - Initialize IMEM memory used by the IPA
438 * @ipa: IPA pointer
439 * @addr: Physical address of the IPA region in IMEM
440 * @size: Size (bytes) of the IPA region in IMEM
443 * a portion of this memory is available for the IPA to use. The
444 * modem accesses this memory directly, but the IPA accesses it
445 * via the IOMMU, using the AP's credentials.
448 * through the IOMMU using the IPA device.
450 * Note: @addr and @size are not guaranteed to be page-aligned.
452 static int ipa_imem_init(struct ipa *ipa, unsigned long addr, size_t size) in ipa_imem_init() argument
454 struct device *dev = &ipa->pdev->dev; in ipa_imem_init()
466 return -EINVAL; in ipa_imem_init()
469 /* Align the address down and the size up to page boundaries */ in ipa_imem_init()
471 size = PAGE_ALIGN(size + addr - phys); in ipa_imem_init()
478 ipa->imem_iova = iova; in ipa_imem_init()
479 ipa->imem_size = size; in ipa_imem_init()
484 static void ipa_imem_exit(struct ipa *ipa) in ipa_imem_exit() argument
489 if (!ipa->imem_size) in ipa_imem_exit()
492 dev = &ipa->pdev->dev; in ipa_imem_exit()
497 size = iommu_unmap(domain, ipa->imem_iova, ipa->imem_size); in ipa_imem_exit()
498 if (size != ipa->imem_size) in ipa_imem_exit()
500 size, ipa->imem_size); in ipa_imem_exit()
502 dev_err(dev, "couldn't get IPA IOMMU domain for IMEM\n"); in ipa_imem_exit()
505 ipa->imem_size = 0; in ipa_imem_exit()
506 ipa->imem_iova = 0; in ipa_imem_exit()
510 * ipa_smem_init() - Initialize SMEM memory used by the IPA
511 * @ipa: IPA pointer
516 * can be allocated. One item is designated for use by the IPA.
518 * The modem accesses SMEM memory directly, but the IPA accesses it
519 * via the IOMMU, using the AP's credentials.
521 * If size provided is non-zero, we allocate it and map it for
524 * Note: @size and the item address are is not guaranteed to be page-aligned.
526 static int ipa_smem_init(struct ipa *ipa, u32 item, size_t size) in ipa_smem_init() argument
528 struct device *dev = &ipa->pdev->dev; in ipa_smem_init()
540 /* SMEM is memory shared between the AP and another system entity in ipa_smem_init()
541 * (in this case, the modem). An allocation from SMEM is persistent in ipa_smem_init()
542 * until the AP reboots; there is no way to free an allocated SMEM in ipa_smem_init()
543 * region. Allocation only reserves the space; to use it you need in ipa_smem_init()
544 * to "get" a pointer it (this does not imply reference counting). in ipa_smem_init()
549 if (ret && ret != -EEXIST) { in ipa_smem_init()
567 return -EINVAL; in ipa_smem_init()
573 return -EINVAL; in ipa_smem_init()
576 /* Align the address down and the size up to a page boundary */ in ipa_smem_init()
579 size = PAGE_ALIGN(size + addr - phys); in ipa_smem_init()
586 ipa->smem_iova = iova; in ipa_smem_init()
587 ipa->smem_size = size; in ipa_smem_init()
592 static void ipa_smem_exit(struct ipa *ipa) in ipa_smem_exit() argument
594 struct device *dev = &ipa->pdev->dev; in ipa_smem_exit()
601 size = iommu_unmap(domain, ipa->smem_iova, ipa->smem_size); in ipa_smem_exit()
602 if (size != ipa->smem_size) in ipa_smem_exit()
604 size, ipa->smem_size); in ipa_smem_exit()
607 dev_err(dev, "couldn't get IPA IOMMU domain for SMEM\n"); in ipa_smem_exit()
610 ipa->smem_size = 0; in ipa_smem_exit()
611 ipa->smem_iova = 0; in ipa_smem_exit()
614 /* Perform memory region-related initialization */
615 int ipa_mem_init(struct ipa *ipa, const struct ipa_mem_data *mem_data) in ipa_mem_init() argument
617 struct device *dev = &ipa->pdev->dev; in ipa_mem_init()
622 if (!ipa_mem_valid(ipa, mem_data)) in ipa_mem_init()
623 return -EINVAL; in ipa_mem_init()
625 ipa->mem_count = mem_data->local_count; in ipa_mem_init()
626 ipa->mem = mem_data->local; in ipa_mem_init()
628 ret = dma_set_mask_and_coherent(&ipa->pdev->dev, DMA_BIT_MASK(64)); in ipa_mem_init()
634 res = platform_get_resource_byname(ipa->pdev, IORESOURCE_MEM, in ipa_mem_init()
635 "ipa-shared"); in ipa_mem_init()
638 "DT error getting \"ipa-shared\" memory property\n"); in ipa_mem_init()
639 return -ENODEV; in ipa_mem_init()
642 ipa->mem_virt = memremap(res->start, resource_size(res), MEMREMAP_WC); in ipa_mem_init()
643 if (!ipa->mem_virt) { in ipa_mem_init()
644 dev_err(dev, "unable to remap \"ipa-shared\" memory\n"); in ipa_mem_init()
645 return -ENOMEM; in ipa_mem_init()
648 ipa->mem_addr = res->start; in ipa_mem_init()
649 ipa->mem_size = resource_size(res); in ipa_mem_init()
651 ret = ipa_imem_init(ipa, mem_data->imem_addr, mem_data->imem_size); in ipa_mem_init()
655 ret = ipa_smem_init(ipa, mem_data->smem_id, mem_data->smem_size); in ipa_mem_init()
662 ipa_imem_exit(ipa); in ipa_mem_init()
664 memunmap(ipa->mem_virt); in ipa_mem_init()
670 void ipa_mem_exit(struct ipa *ipa) in ipa_mem_exit() argument
672 ipa_smem_exit(ipa); in ipa_mem_exit()
673 ipa_imem_exit(ipa); in ipa_mem_exit()
674 memunmap(ipa->mem_virt); in ipa_mem_exit()