Lines Matching +full:memory +full:- +full:region

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>
23 /* "Canary" value placed between memory regions to detect overflow */
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()
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
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
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
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()
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()
99 offset = mem->offset; in ipa_mem_setup()
100 size = mem->size; in ipa_mem_setup()
103 size += mem->size; in ipa_mem_setup()
115 offset = ipa->mem_offset + mem->offset; 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? */
127 enum ipa_version version = ipa->version; in ipa_mem_id_valid()
149 case IPA_MEM_END_MARKER: /* pseudo region */ in ipa_mem_id_valid()
179 /* Must the given memory region be present in the configuration? */
202 return ipa->version >= IPA_VERSION_4_0; in ipa_mem_id_required()
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()
217 dev_err(dev, "region id %u not valid\n", mem_id); in ipa_mem_valid_one()
221 if (!mem->size && !mem->canary_count) { in ipa_mem_valid_one()
222 dev_err(dev, "empty memory region %u\n", mem_id); 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()
229 dev_err(dev, "region %u size not a multiple of %u bytes\n", 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()
234 dev_err(dev, "region %u offset too small for %hu canaries\n", 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()
244 /* Verify each defined memory region is valid. */
248 struct device *dev = &ipa->pdev->dev; in ipa_mem_valid()
252 if (mem_data->local_count > IPA_MEM_COUNT) { in ipa_mem_valid()
253 dev_err(dev, "too many memory regions (%u > %u)\n", 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()
274 dev_err(dev, "required memory region %u missing\n", in ipa_mem_valid()
281 /* Do all memory regions fit within the IPA local memory? */
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()
294 dev_err(dev, "region %u ends beyond memory limit (0x%08x)\n", in ipa_mem_size_valid()
295 mem->id, limit); in ipa_mem_size_valid()
304 * ipa_mem_config() - Configure IPA shared memory
311 struct device *dev = &ipa->pdev->dev; in ipa_mem_config()
320 /* Check the advertised location and size of the shared memory area */ 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()
327 /* Make sure the end is within the region's mapped space */ 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()
336 dev_dbg(dev, "ignoring larger reported memory size: 0x%08x\n", in ipa_mem_config()
340 /* We know our memory size; make sure regions are all in range */ in ipa_mem_config()
342 return -EINVAL; in ipa_mem_config()
344 /* Prealloc DMA memory for zeroing regions */ 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()
352 /* For each defined region, write "canary" values in the 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()
362 /* Write canary values in the space before the region */ 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()
369 /* Make sure filter and route table memory regions are valid */ in ipa_mem_config()
373 /* Validate memory-related properties relevant to immediate commands */ 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()
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
407 * Zero regions of IPA-local memory used by the modem. These are configured
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
417 /* Get a transaction to zero the modem memory, modem header, 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
439 * @addr: Physical address of the IPA region in IMEM
440 * @size: Size (bytes) of the IPA region in IMEM
442 * IMEM is a block of shared memory separate from system DRAM, and
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
447 * If this region exists (size > 0) we map it for read/write access
450 * Note: @addr and @size are not guaranteed to be page-aligned.
454 struct device *dev = &ipa->pdev->dev; in ipa_imem_init()
461 return 0; /* IMEM memory not used */ in ipa_imem_init()
466 return -EINVAL; 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()
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()
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
512 * @item: Item ID of SMEM memory
513 * @size: Size (bytes) of SMEM memory region
518 * The modem accesses SMEM memory directly, but the IPA accesses it
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.
528 struct device *dev = &ipa->pdev->dev; in ipa_smem_init()
538 return 0; /* SMEM memory not used */ in ipa_smem_init()
540 /* SMEM is memory shared between the AP and another system entity in ipa_smem_init()
543 * region. Allocation only reserves the space; to use it you need in ipa_smem_init()
549 if (ret && ret != -EEXIST) { in ipa_smem_init()
555 /* Now get the address of the SMEM memory region */ in ipa_smem_init()
563 /* In case the region was already allocated, verify the size */ in ipa_smem_init()
567 return -EINVAL; in ipa_smem_init()
573 return -EINVAL; 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()
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()
610 ipa->smem_size = 0; in ipa_smem_exit()
611 ipa->smem_iova = 0; in ipa_smem_exit()
614 /* Perform memory region-related initialization */
617 struct device *dev = &ipa->pdev->dev; in ipa_mem_init()
621 /* Make sure the set of defined memory regions is valid */ 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()
664 memunmap(ipa->mem_virt); in ipa_mem_init()
674 memunmap(ipa->mem_virt); in ipa_mem_exit()