// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) 2015, Sony Mobile Communications AB. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
*/
/* * The Qualcomm shared memory system is a allocate only heap structure that * consists of one of more memory areas that can be accessed by the processors * in the SoC. * * All systems contains a global heap, accessible by all processors in the SoC, * with a table of contents data structure (@smem_header) at the beginning of * the main shared memory block. * * The global header contains meta data for allocations as well as a fixed list * of 512 entries (@smem_global_entry) that can be initialized to reference * parts of the shared memory space. * * * In addition to this global heap a set of "private" heaps can be set up at * boot time with access restrictions so that only certain processor pairs can * access the data. * * These partitions are referenced from an optional partition table * (@smem_ptable), that is found 4kB from the end of the main smem region. The * partition table entries (@smem_ptable_entry) lists the involved processors * (or hosts) and their location in the main shared memory region. * * Each partition starts with a header (@smem_partition_header) that identifies * the partition and holds properties for the two internal memory regions. The * two regions are cached and non-cached memory respectively. Each region * contain a link list of allocation headers (@smem_private_entry) followed by * their data. * * Items in the non-cached region are allocated from the start of the partition * while items in the cached region are allocated from the end. The free area * is hence the region between the cached and non-cached offsets. The header of * cached items comes after the data. * * Version 12 (SMEM_GLOBAL_PART_VERSION) changes the item alloc/get procedure * for the global heap. A new global partition is created from the global heap * region with partition type (SMEM_GLOBAL_HOST) and the max smem item count is * set by the bootloader. * * To synchronize allocations in the shared memory heaps a remote spinlock must * be held - currently lock number 3 of the sfpb or tcsr is used for this on all * platforms. *
*/
/* * The version member of the smem header contains an array of versions for the * various software components in the SoC. We verify that the boot loader * version is a valid version as a sanity check.
*/ #define SMEM_MASTER_SBL_VERSION_INDEX 7 #define SMEM_GLOBAL_HEAP_VERSION 11 #define SMEM_GLOBAL_PART_VERSION 12
/* * The first 8 items are only to be allocated by the boot loader while * initializing the heap.
*/ #define SMEM_ITEM_LAST_FIXED 8
/* Highest accepted item number, for both global and private heaps */ #define SMEM_ITEM_COUNT 512
/* Processor/host identifier for the application processor */ #define SMEM_HOST_APPS 0
/* Processor/host identifier for the global partition */ #define SMEM_GLOBAL_HOST 0xfffe
/* Max number of processors/hosts in a system */ #define SMEM_HOST_COUNT 25
/** * struct smem_proc_comm - proc_comm communication struct (legacy) * @command: current command to be executed * @status: status of the currently requested command * @params: parameters to the command
*/ struct smem_proc_comm {
__le32 command;
__le32 status;
__le32 params[2];
};
/** * struct smem_global_entry - entry to reference smem items on the heap * @allocated: boolean to indicate if this entry is used * @offset: offset to the allocated space * @size: size of the allocated space, 8 byte aligned * @aux_base: base address for the memory region used by this unit, or 0 for * the default region. bits 0,1 are reserved
*/ struct smem_global_entry {
__le32 allocated;
__le32 offset;
__le32 size;
__le32 aux_base; /* bits 1:0 reserved */
}; #define AUX_BASE_MASK 0xfffffffc
/** * struct smem_header - header found in beginning of primary smem region * @proc_comm: proc_comm communication interface (legacy) * @version: array of versions for the various subsystems * @initialized: boolean to indicate that smem is initialized * @free_offset: index of the first unallocated byte in smem * @available: number of bytes available for allocation * @reserved: reserved field, must be 0 * @toc: array of references to items
*/ struct smem_header { struct smem_proc_comm proc_comm[4];
__le32 version[32];
__le32 initialized;
__le32 free_offset;
__le32 available;
__le32 reserved; struct smem_global_entry toc[SMEM_ITEM_COUNT];
};
/** * struct smem_ptable_entry - one entry in the @smem_ptable list * @offset: offset, within the main shared memory region, of the partition * @size: size of the partition * @flags: flags for the partition (currently unused) * @host0: first processor/host with access to this partition * @host1: second processor/host with access to this partition * @cacheline: alignment for "cached" entries * @reserved: reserved entries for later use
*/ struct smem_ptable_entry {
__le32 offset;
__le32 size;
__le32 flags;
__le16 host0;
__le16 host1;
__le32 cacheline;
__le32 reserved[7];
};
/** * struct smem_ptable - partition table for the private partitions * @magic: magic number, must be SMEM_PTABLE_MAGIC * @version: version of the partition table * @num_entries: number of partitions in the table * @reserved: for now reserved entries * @entry: list of @smem_ptable_entry for the @num_entries partitions
*/ struct smem_ptable {
u8 magic[4];
__le32 version;
__le32 num_entries;
__le32 reserved[5]; struct smem_ptable_entry entry[];
};
/** * struct smem_partition_header - header of the partitions * @magic: magic number, must be SMEM_PART_MAGIC * @host0: first processor/host with access to this partition * @host1: second processor/host with access to this partition * @size: size of the partition * @offset_free_uncached: offset to the first free byte of uncached memory in * this partition * @offset_free_cached: offset to the first free byte of cached memory in this * partition * @reserved: for now reserved entries
*/ struct smem_partition_header {
u8 magic[4];
__le16 host0;
__le16 host1;
__le32 size;
__le32 offset_free_uncached;
__le32 offset_free_cached;
__le32 reserved[3];
};
/** * struct smem_private_entry - header of each item in the private partition * @canary: magic number, must be SMEM_PRIVATE_CANARY * @item: identifying number of the smem item * @size: size of the data, including padding bytes * @padding_data: number of bytes of padding of data * @padding_hdr: number of bytes of padding between the header and the data * @reserved: for now reserved entry
*/ struct smem_private_entry {
u16 canary; /* bytes are the same so no swapping needed */
__le16 item;
__le32 size; /* includes padding bytes */
__le16 padding_data;
__le16 padding_hdr;
__le32 reserved;
}; #define SMEM_PRIVATE_CANARY 0xa5a5
/** * struct smem_info - smem region info located after the table of contents * @magic: magic number, must be SMEM_INFO_MAGIC * @size: size of the smem region * @base_addr: base address of the smem region * @reserved: for now reserved entry * @num_items: highest accepted item number
*/ struct smem_info {
u8 magic[4];
__le32 size;
__le32 base_addr;
__le32 reserved;
__le16 num_items;
};
/** * struct smem_region - representation of a chunk of memory used for smem * @aux_base: identifier of aux_mem base * @virt_base: virtual base address of memory with this aux_mem identifier * @size: size of the memory region
*/ struct smem_region {
phys_addr_t aux_base; void __iomem *virt_base;
size_t size;
};
/** * struct qcom_smem - device data for the smem device * @dev: device pointer * @hwlock: reference to a hwspinlock * @ptable: virtual base of partition table * @global_partition: describes for global partition when in use * @partitions: list of partitions of current processor/host * @item_count: max accepted item number * @socinfo: platform device pointer * @num_regions: number of @regions * @regions: list of the memory regions defining the shared memory
*/ struct qcom_smem { struct device *dev;
/* Pointer to the one and only smem handle */ staticstruct qcom_smem *__smem;
/* Timeout (ms) for the trylock of remote spinlocks */ #define HWSPINLOCK_TIMEOUT 1000
/* The qcom hwspinlock id is always plus one from the smem host id */ #define SMEM_HOST_ID_TO_HWSPINLOCK_ID(__x) ((__x) + 1)
/** * qcom_smem_bust_hwspin_lock_by_host() - bust the smem hwspinlock for a host * @host: remote processor id * * Busts the hwspin_lock for the given smem host id. This helper is intended * for remoteproc drivers that manage remoteprocs with an equivalent smem * driver instance in the remote firmware. Drivers can force a release of the * smem hwspin_lock if the rproc unexpectedly goes into a bad state. * * Context: Process context. * * Returns: 0 on success, otherwise negative errno.
*/ int qcom_smem_bust_hwspin_lock_by_host(unsignedint host)
{ /* This function is for remote procs, so ignore SMEM_HOST_APPS */ if (host == SMEM_HOST_APPS || host >= SMEM_HOST_COUNT) return -EINVAL;
while (hdr < end) { if (hdr->canary != SMEM_PRIVATE_CANARY) goto bad_canary; if (le16_to_cpu(hdr->item) == item) return -EEXIST;
hdr = uncached_entry_next(hdr);
}
if (WARN_ON((void *)hdr > p_end)) return -EINVAL;
/* Check that we don't grow into the cached region */
alloc_size = sizeof(*hdr) + ALIGN(size, 8); if ((void *)hdr + alloc_size > cached) {
dev_err(smem->dev, "Out of memory\n"); return -ENOSPC;
}
/* * Ensure the header is written before we advance the free offset, so * that remote processors that does not take the remote spinlock still * gets a consistent view of the linked list.
*/
wmb();
le32_add_cpu(&phdr->offset_free_uncached, alloc_size);
/* * Ensure the header is consistent before we mark the item allocated, * so that remote processors will get a consistent view of the item * even though they do not take the spinlock on read.
*/
wmb();
entry->allocated = cpu_to_le32(1);
/** * qcom_smem_alloc() - allocate space for a smem item * @host: remote processor id, or -1 * @item: smem item handle * @size: number of bytes to be allocated * * Allocate space for a given smem item of size @size, given that the item is * not yet allocated. * * Return: 0 on success, negative errno on failure.
*/ int qcom_smem_alloc(unsigned host, unsigned item, size_t size)
{ struct smem_partition *part; unsignedlong flags; int ret;
if (!__smem) return -EPROBE_DEFER;
if (item < SMEM_ITEM_LAST_FIXED) {
dev_err(__smem->dev, "Rejecting allocation of static entry %d\n", item); return -EINVAL;
}
if (WARN_ON(item >= __smem->item_count)) return -EINVAL;
ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
HWSPINLOCK_TIMEOUT,
&flags); if (ret) return ret;
if (host < SMEM_HOST_COUNT && __smem->partitions[host].virt_base) {
part = &__smem->partitions[host];
ret = qcom_smem_alloc_private(__smem, part, item, size);
} elseif (__smem->global_partition.virt_base) {
part = &__smem->global_partition;
ret = qcom_smem_alloc_private(__smem, part, item, size);
} else {
ret = qcom_smem_alloc_global(__smem, item, size);
}
/** * qcom_smem_get() - resolve ptr of size of a smem item * @host: the remote processor, or -1 * @item: smem item handle * @size: pointer to be filled out with size of the item * * Looks up smem item and returns pointer to it. Size of smem * item is returned in @size. * * Return: a pointer to an SMEM item on success, ERR_PTR() on failure.
*/ void *qcom_smem_get(unsigned host, unsigned item, size_t *size)
{ struct smem_partition *part; void *ptr = ERR_PTR(-EPROBE_DEFER);
if (!__smem) return ptr;
if (WARN_ON(item >= __smem->item_count)) return ERR_PTR(-EINVAL);
/** * qcom_smem_get_free_space() - retrieve amount of free space in a partition * @host: the remote processor identifying a partition, or -1 * * To be used by smem clients as a quick way to determine if any new * allocations has been made. * * Return: number of available bytes on success, negative errno on failure.
*/ int qcom_smem_get_free_space(unsigned host)
{ struct smem_partition *part; struct smem_partition_header *phdr; struct smem_header *header; unsigned ret;
if (!__smem) return -EPROBE_DEFER;
if (host < SMEM_HOST_COUNT && __smem->partitions[host].virt_base) {
part = &__smem->partitions[host];
phdr = part->virt_base;
ret = le32_to_cpu(phdr->offset_free_cached) -
le32_to_cpu(phdr->offset_free_uncached);
if (ret > le32_to_cpu(part->size)) return -EINVAL;
} elseif (__smem->global_partition.virt_base) {
part = &__smem->global_partition;
phdr = part->virt_base;
ret = le32_to_cpu(phdr->offset_free_cached) -
le32_to_cpu(phdr->offset_free_uncached);
if (ret > le32_to_cpu(part->size)) return -EINVAL;
} else {
header = __smem->regions[0].virt_base;
ret = le32_to_cpu(header->available);
if (ret > __smem->regions[0].size) return -EINVAL;
}
/** * qcom_smem_get_soc_id() - return the SoC ID * @id: On success, we return the SoC ID here. * * Look up SoC ID from HW/SW build ID and return it. * * Return: 0 on success, negative errno on failure.
*/ int qcom_smem_get_soc_id(u32 *id)
{ struct socinfo *info;
info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_HW_SW_BUILD_ID, NULL); if (IS_ERR(info)) return PTR_ERR(info);
/** * qcom_smem_get_feature_code() - return the feature code * @code: On success, return the feature code here. * * Look up the feature code identifier from SMEM and return it. * * Return: 0 on success, negative errno on failure.
*/ int qcom_smem_get_feature_code(u32 *code)
{ struct socinfo *info;
u32 raw_code;
info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_HW_SW_BUILD_ID, NULL); if (IS_ERR(info)) return PTR_ERR(info);
/* This only makes sense for socinfo >= 16 */ if (__le32_to_cpu(info->fmt) < SOCINFO_VERSION(0, 16)) return -EOPNOTSUPP;
raw_code = __le32_to_cpu(info->feature_code);
/* Ensure the value makes sense */ if (raw_code > SOCINFO_FC_INT_MAX)
raw_code = SOCINFO_FC_UNKNOWN;
staticint qcom_smem_set_global_partition(struct qcom_smem *smem)
{ struct smem_partition_header *header; struct smem_ptable_entry *entry; struct smem_ptable *ptable; bool found = false; int i;
if (smem->global_partition.virt_base) {
dev_err(smem->dev, "Already found the global partition\n"); return -EINVAL;
}
ptable = qcom_smem_get_ptable(smem); if (IS_ERR(ptable)) return PTR_ERR(ptable);
for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
entry = &ptable->entry[i]; if (!le32_to_cpu(entry->offset)) continue; if (!le32_to_cpu(entry->size)) continue;
if (le16_to_cpu(entry->host0) != SMEM_GLOBAL_HOST) continue;
if (le16_to_cpu(entry->host1) == SMEM_GLOBAL_HOST) {
found = true; break;
}
}
if (!found) {
dev_err(smem->dev, "Missing entry for global partition\n"); return -EINVAL;
}
header = qcom_smem_partition_header(smem, entry,
SMEM_GLOBAL_HOST, SMEM_GLOBAL_HOST); if (!header) return -EINVAL;
ptable = qcom_smem_get_ptable(smem); if (IS_ERR(ptable)) return PTR_ERR(ptable);
for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
entry = &ptable->entry[i]; if (!le32_to_cpu(entry->offset)) continue; if (!le32_to_cpu(entry->size)) continue;
rmem = of_reserved_mem_lookup(pdev->dev.of_node); if (rmem) {
smem->regions[0].aux_base = rmem->base;
smem->regions[0].size = rmem->size;
} else { /* * Fall back to the memory-region reference, if we're not a * reserved-memory node.
*/
ret = qcom_smem_resolve_mem(smem, "memory-region", &smem->regions[0]); if (ret) return ret;
}
if (num_regions > 1) {
ret = qcom_smem_resolve_mem(smem, "qcom,rpm-msg-ram", &smem->regions[1]); if (ret) return ret;
}
ret = qcom_smem_map_toc(smem, &smem->regions[0]); if (ret) return ret;
for (i = 1; i < num_regions; i++) {
smem->regions[i].virt_base = devm_ioremap_wc(&pdev->dev,
smem->regions[i].aux_base,
smem->regions[i].size); if (!smem->regions[i].virt_base) {
dev_err(&pdev->dev, "failed to remap %pa\n", &smem->regions[i].aux_base); return -ENOMEM;
}
}
header = smem->regions[0].virt_base; if (le32_to_cpu(header->initialized) != 1 ||
le32_to_cpu(header->reserved)) {
dev_err(&pdev->dev, "SMEM is not initialized by SBL\n"); return -EINVAL;
}
hwlock_id = of_hwspin_lock_get_id(pdev->dev.of_node, 0); if (hwlock_id < 0) return dev_err_probe(&pdev->dev, hwlock_id, "failed to retrieve hwlock\n");
smem->hwlock = hwspin_lock_request_specific(hwlock_id); if (!smem->hwlock) return -ENXIO;
ret = hwspin_lock_timeout_irqsave(smem->hwlock, HWSPINLOCK_TIMEOUT, &flags); if (ret) return ret;
size = readl_relaxed(&header->available) + readl_relaxed(&header->free_offset);
hwspin_unlock_irqrestore(smem->hwlock, &flags);
version = qcom_smem_get_sbl_version(smem); /* * smem header mapping is required only in heap version scheme, so unmap * it here. It will be remapped in qcom_smem_map_global() when whole * partition is mapped again.
*/
devm_iounmap(smem->dev, smem->regions[0].virt_base); switch (version >> 16) { case SMEM_GLOBAL_PART_VERSION:
ret = qcom_smem_set_global_partition(smem); if (ret < 0) return ret;
smem->item_count = qcom_smem_get_item_count(smem); break; case SMEM_GLOBAL_HEAP_VERSION:
qcom_smem_map_global(smem, size);
smem->item_count = SMEM_ITEM_COUNT; break; default:
dev_err(&pdev->dev, "Unsupported SMEM version 0x%x\n", version); return -EINVAL;
}
BUILD_BUG_ON(SMEM_HOST_APPS >= SMEM_HOST_COUNT);
ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS); if (ret < 0 && ret != -ENOENT) return ret;
__smem = smem;
smem->socinfo = platform_device_register_data(&pdev->dev, "qcom-socinfo",
PLATFORM_DEVID_NONE, NULL,
0); if (IS_ERR(smem->socinfo))
dev_dbg(&pdev->dev, "failed to register socinfo device\n");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.