--- zzzz-none-000/linux-4.4.271/drivers/soc/qcom/smem.c 2021-06-03 06:22:09.000000000 +0000 +++ hawkeye-5590-750/linux-4.4.271/drivers/soc/qcom/smem.c 2023-04-19 10:22:29.000000000 +0000 @@ -1,6 +1,6 @@ /* * Copyright (c) 2015, Sony Mobile Communications AB. - * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. + * Copyright (c) 2012-2013, 2017, 2020 The Linux Foundation. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 and @@ -20,6 +20,9 @@ #include #include #include +#include + +#include /* * The Qualcomm shared memory system is a allocate only heap structure that @@ -52,8 +55,13 @@ * * 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. + * 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 @@ -62,13 +70,13 @@ */ /* - * Item 3 of the global heap contains an array of versions for the various - * software components in the SoC. We verify that the boot loader version is - * what the expected version (SMEM_EXPECTED_VERSION) as a sanity check. - */ -#define SMEM_ITEM_VERSION 3 -#define SMEM_MASTER_SBL_VERSION_INDEX 7 -#define SMEM_EXPECTED_VERSION 11 + * 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 @@ -82,6 +90,9 @@ /* 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 9 @@ -140,6 +151,7 @@ * @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 { @@ -148,7 +160,8 @@ __le32 flags; __le16 host0; __le16 host1; - __le32 reserved[8]; + __le32 cacheline; + __le32 reserved[7]; }; /** @@ -213,6 +226,24 @@ #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; +}; + +static const u8 SMEM_INFO_MAGIC[] = { 0x53, 0x49, 0x49, 0x49 }; /* SIII */ + +/** * 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 @@ -228,8 +259,14 @@ * struct qcom_smem - device data for the smem device * @dev: device pointer * @hwlock: reference to a hwspinlock + * @global_partition: pointer to global partition when in use + * @global_cacheline: cacheline size for global partition * @partitions: list of pointers to partitions affecting the current + * @ptable_entries: list of pointers to partitions table entry of current * processor/host + * @global_entry: Pointer to global partition table entry + * @cacheline: list of cacheline sizes for each host + * @item_count: max accepted item number * @num_regions: number of @regions * @regions: list of the memory regions defining the shared memory */ @@ -238,21 +275,184 @@ struct hwspinlock *hwlock; - struct smem_partition_header *partitions[SMEM_HOST_COUNT]; + struct smem_ptable_entry *global_partition_entry; + struct smem_ptable_entry *ptable_entries[SMEM_HOST_COUNT]; + u32 item_count; unsigned num_regions; struct smem_region regions[0]; }; +#define CPU_NAME_MAX_SIZE 16 + +static int __init print_soc_version_info(void) +{ + uint32_t minor_number = UINT_MAX; + uint32_t major_number = UINT_MAX; + uint32_t cpu_type = UINT_MAX; + char cpu_type_name[CPU_NAME_MAX_SIZE]; + + major_number = read_ipq_soc_version_major(); + if (major_number <= 0) + pr_err("Read of property:soc_version_major from node failed\n"); + + minor_number = read_ipq_soc_version_minor(); + if (minor_number <= 0) + pr_err("Read of property:soc_version_minor from node failed\n"); + + cpu_type = read_ipq_cpu_type(); + if (cpu_type <= 0) + pr_err("Read of property:cpu_type from node failed\n"); + + switch (cpu_type) { + case 272: + strlcpy(cpu_type_name, "IPQ4018", CPU_NAME_MAX_SIZE); + break; + case 273: + strlcpy(cpu_type_name, "IPQ4019", CPU_NAME_MAX_SIZE); + break; + case 287: + strlcpy(cpu_type_name, "IPQ4028", CPU_NAME_MAX_SIZE); + break; + case 288: + strlcpy(cpu_type_name, "IPQ4029", CPU_NAME_MAX_SIZE); + break; + case 201: + strlcpy(cpu_type_name, "IPQ8062", CPU_NAME_MAX_SIZE); + break; + case 202: + strlcpy(cpu_type_name, "IPQ8064", CPU_NAME_MAX_SIZE); + break; + case 203: + strlcpy(cpu_type_name, "IPQ8066", CPU_NAME_MAX_SIZE); + break; + case 204: + strlcpy(cpu_type_name, "IPQ8068", CPU_NAME_MAX_SIZE); + break; + case 280: + strlcpy(cpu_type_name, "IPQ8065", CPU_NAME_MAX_SIZE); + break; + case 281: + strlcpy(cpu_type_name, "IPQ8069", CPU_NAME_MAX_SIZE); + break; + case 323: + strlcpy(cpu_type_name, "IPQ8074", CPU_NAME_MAX_SIZE); + break; + case 342: + strlcpy(cpu_type_name, "IPQ8072", CPU_NAME_MAX_SIZE); + break; + case 343: + strlcpy(cpu_type_name, "IPQ8076", CPU_NAME_MAX_SIZE); + break; + case 344: + strlcpy(cpu_type_name, "IPQ8078", CPU_NAME_MAX_SIZE); + break; + case 375: + strlcpy(cpu_type_name, "IPQ8070", CPU_NAME_MAX_SIZE); + break; + case 376: + strlcpy(cpu_type_name, "IPQ8071", CPU_NAME_MAX_SIZE); + break; + case 389: + strlcpy(cpu_type_name, "IPQ8072A", CPU_NAME_MAX_SIZE); + break; + case 390: + strlcpy(cpu_type_name, "IPQ8074A", CPU_NAME_MAX_SIZE); + break; + case 391: + strlcpy(cpu_type_name, "IPQ8076A", CPU_NAME_MAX_SIZE); + break; + case 392: + strlcpy(cpu_type_name, "IPQ8078A", CPU_NAME_MAX_SIZE); + break; + case 395: + strlcpy(cpu_type_name, "IPQ8070A", CPU_NAME_MAX_SIZE); + break; + case 396: + strlcpy(cpu_type_name, "IPQ8071A", CPU_NAME_MAX_SIZE); + break; + case 397: + strlcpy(cpu_type_name, "IPQ8172", CPU_NAME_MAX_SIZE); + break; + case 398: + strlcpy(cpu_type_name, "IPQ8173", CPU_NAME_MAX_SIZE); + break; + case 399: + strlcpy(cpu_type_name, "IPQ8174", CPU_NAME_MAX_SIZE); + break; + case 402: + strlcpy(cpu_type_name, "IPQ6018", CPU_NAME_MAX_SIZE); + break; + case 403: + strlcpy(cpu_type_name, "IPQ6028", CPU_NAME_MAX_SIZE); + break; + case 421: + strlcpy(cpu_type_name, "IPQ6000", CPU_NAME_MAX_SIZE); + break; + case 422: + strlcpy(cpu_type_name, "IPQ6010", CPU_NAME_MAX_SIZE); + break; + case 453: + strlcpy(cpu_type_name, "IPQ6005", CPU_NAME_MAX_SIZE); + break; + case 446: + strlcpy(cpu_type_name, "IPQ5010", CPU_NAME_MAX_SIZE); + break; + case 447: + strlcpy(cpu_type_name, "IPQ5018", CPU_NAME_MAX_SIZE); + break; + case 448: + strlcpy(cpu_type_name, "IPQ5028", CPU_NAME_MAX_SIZE); + break; + case 503: + strlcpy(cpu_type_name, "IPQ5000", CPU_NAME_MAX_SIZE); + break; + case 504: + strlcpy(cpu_type_name, "IPQ0509", CPU_NAME_MAX_SIZE); + break; + case 505: + strlcpy(cpu_type_name, "IPQ0518", CPU_NAME_MAX_SIZE); + break; + default: + strlcpy(cpu_type_name, "unavail", CPU_NAME_MAX_SIZE); + break; + } + + pr_info("CPU: %s, SoC Version: %u.%u\n", cpu_type_name, + major_number, minor_number); + + return 0; +} + +/* Pointer to the one and only smem handle */ +static struct qcom_smem *__smem; + +/* Timeout (ms) for the trylock of remote spinlocks */ +#define HWSPINLOCK_TIMEOUT 1000 + +static struct smem_partition_header * +ptable_entry_to_phdr(struct smem_ptable_entry *entry) +{ + return __smem->regions[0].virt_base + le32_to_cpu(entry->offset); +} + static struct smem_private_entry * -phdr_to_last_private_entry(struct smem_partition_header *phdr) +phdr_to_last_uncached_entry(struct smem_partition_header *phdr) { void *p = phdr; return p + le32_to_cpu(phdr->offset_free_uncached); } -static void *phdr_to_first_cached_entry(struct smem_partition_header *phdr) +static void *phdr_to_first_cached_entry(struct smem_partition_header *phdr, + size_t cacheline) +{ + void *p = phdr; + + return p + le32_to_cpu(phdr->size) - ALIGN(sizeof(*phdr), cacheline); +} + +static void *phdr_to_last_cached_entry(struct smem_partition_header *phdr) { void *p = phdr; @@ -260,7 +460,7 @@ } static struct smem_private_entry * -phdr_to_first_private_entry(struct smem_partition_header *phdr) +phdr_to_first_uncached_entry(struct smem_partition_header *phdr) { void *p = phdr; @@ -268,7 +468,7 @@ } static struct smem_private_entry * -private_entry_next(struct smem_private_entry *e) +uncached_entry_next(struct smem_private_entry *e) { void *p = e; @@ -276,47 +476,64 @@ le32_to_cpu(e->size); } -static void *entry_to_item(struct smem_private_entry *e) +static struct smem_private_entry * +cached_entry_next(struct smem_private_entry *e, size_t cacheline) +{ + void *p = e; + + return p - le32_to_cpu(e->size) - ALIGN(sizeof(*e), cacheline); +} + +static void *uncached_entry_to_item(struct smem_private_entry *e) { void *p = e; return p + sizeof(*e) + le16_to_cpu(e->padding_hdr); } -/* Pointer to the one and only smem handle */ -static struct qcom_smem *__smem; +static void *cached_entry_to_item(struct smem_private_entry *e) +{ + void *p = e; -/* Timeout (ms) for the trylock of remote spinlocks */ -#define HWSPINLOCK_TIMEOUT 1000 + return p - le32_to_cpu(e->size); +} static int qcom_smem_alloc_private(struct qcom_smem *smem, - unsigned host, + struct smem_ptable_entry *entry, unsigned item, size_t size) { - struct smem_partition_header *phdr; struct smem_private_entry *hdr, *end; + struct smem_partition_header *phdr; size_t alloc_size; void *cached; + void *p_end; + + phdr = ptable_entry_to_phdr(entry); + p_end = (void *)phdr + le32_to_cpu(entry->size); + + hdr = phdr_to_first_uncached_entry(phdr); + end = phdr_to_last_uncached_entry(phdr); + cached = phdr_to_last_cached_entry(phdr); - phdr = smem->partitions[host]; - hdr = phdr_to_first_private_entry(phdr); - end = phdr_to_last_private_entry(phdr); - cached = phdr_to_first_cached_entry(phdr); + if (WARN_ON((void *)end > p_end || (void *)cached > p_end)) + return -EINVAL; while (hdr < end) { if (hdr->canary != SMEM_PRIVATE_CANARY) { dev_err(smem->dev, - "Found invalid canary in host %d partition\n", - host); + "Found invalid canary in host %d:%d partition\n", + phdr->host0, phdr->host1); return -EINVAL; } if (le16_to_cpu(hdr->item) == item) return -EEXIST; - hdr = private_entry_next(hdr); + 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); @@ -346,11 +563,8 @@ unsigned item, size_t size) { - struct smem_header *header; struct smem_global_entry *entry; - - if (WARN_ON(item >= SMEM_ITEM_COUNT)) - return -EINVAL; + struct smem_header *header; header = smem->regions[0].virt_base; entry = &header->toc[item]; @@ -389,6 +603,7 @@ */ int qcom_smem_alloc(unsigned host, unsigned item, size_t size) { + struct smem_ptable_entry *entry; unsigned long flags; int ret; @@ -401,16 +616,24 @@ 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]) - ret = qcom_smem_alloc_private(__smem, host, item, size); - else + if (host < SMEM_HOST_COUNT && __smem->ptable_entries[host]) { + entry = __smem->ptable_entries[host]; + ret = qcom_smem_alloc_private(__smem, entry, item, size); + } else if (__smem->global_partition_entry) { + entry = __smem->global_partition_entry; + ret = qcom_smem_alloc_private(__smem, entry, item, size); + } else { ret = qcom_smem_alloc_global(__smem, item, size); + } hwspin_unlock_irqrestore(__smem->hwlock, &flags); @@ -422,15 +645,14 @@ unsigned item, size_t *size) { + struct smem_global_entry *entry; struct smem_header *header; struct smem_region *area; - struct smem_global_entry *entry; + u64 entry_offset; + u32 e_size; u32 aux_base; unsigned i; - if (WARN_ON(item >= SMEM_ITEM_COUNT)) - return ERR_PTR(-EINVAL); - header = smem->regions[0].virt_base; entry = &header->toc[item]; if (!entry->allocated) @@ -442,9 +664,16 @@ area = &smem->regions[i]; if (area->aux_base == aux_base || !aux_base) { + e_size = le32_to_cpu(entry->size); + entry_offset = le32_to_cpu(entry->offset); + + if (WARN_ON(e_size + entry_offset > area->size)) + return ERR_PTR(-EINVAL); + if (size != NULL) - *size = le32_to_cpu(entry->size); - return area->virt_base + le32_to_cpu(entry->offset); + *size = e_size; + + return area->virt_base + entry_offset; } } @@ -452,37 +681,93 @@ } static void *qcom_smem_get_private(struct qcom_smem *smem, - unsigned host, + struct smem_ptable_entry *entry, unsigned item, size_t *size) { struct smem_partition_header *phdr; struct smem_private_entry *e, *end; + void *item_ptr, *p_end; + u32 partition_size; + size_t cacheline; + u32 padding_data; + u32 e_size; + + phdr = ptable_entry_to_phdr(entry); + partition_size = le32_to_cpu(entry->size); + p_end = (void *)phdr + partition_size; + cacheline = le32_to_cpu(entry->cacheline); - phdr = smem->partitions[host]; - e = phdr_to_first_private_entry(phdr); - end = phdr_to_last_private_entry(phdr); + e = phdr_to_first_uncached_entry(phdr); + end = phdr_to_last_uncached_entry(phdr); + + if (WARN_ON((void *)end > p_end)) + return ERR_PTR(-EINVAL); while (e < end) { - if (e->canary != SMEM_PRIVATE_CANARY) { - dev_err(smem->dev, - "Found invalid canary in host %d partition\n", - host); - return ERR_PTR(-EINVAL); + if (e->canary != SMEM_PRIVATE_CANARY) + goto invalid_canary; + + if (le16_to_cpu(e->item) == item) { + if (size != NULL) { + e_size = le32_to_cpu(e->size); + padding_data = le16_to_cpu(e->padding_data); + if (e_size < partition_size + && padding_data < e_size) + *size = e_size - padding_data; + else + return ERR_PTR(-EINVAL); + } + item_ptr = uncached_entry_to_item(e); + if (WARN_ON(item_ptr > p_end)) + return ERR_PTR(-EINVAL); + + return item_ptr; } + e = uncached_entry_next(e); + } + + /* Item was not found in the uncached list, search the cached list */ + + e = phdr_to_first_cached_entry(phdr, cacheline); + end = phdr_to_last_cached_entry(phdr); + + while (e > end) { + if (e->canary != SMEM_PRIVATE_CANARY) + goto invalid_canary; + if (le16_to_cpu(e->item) == item) { - if (size != NULL) - *size = le32_to_cpu(e->size) - - le16_to_cpu(e->padding_data); + if (size != NULL) { + e_size = le32_to_cpu(e->size); + padding_data = le16_to_cpu(e->padding_data); + + if (e_size < partition_size + && padding_data < e_size) + *size = e_size - padding_data; + else + return ERR_PTR(-EINVAL); + } + + item_ptr = cached_entry_to_item(e); + if (WARN_ON(item_ptr > p_end)) + return ERR_PTR(-EINVAL); - return entry_to_item(e); + return item_ptr; } - e = private_entry_next(e); + e = cached_entry_next(e, cacheline); } + if (WARN_ON((void *)e > p_end)) + return ERR_PTR(-EINVAL); return ERR_PTR(-ENOENT); + +invalid_canary: + dev_err(smem->dev, "Found invalid canary in hosts %d:%d partition\n", + phdr->host0, phdr->host1); + + return ERR_PTR(-EINVAL); } /** @@ -496,6 +781,7 @@ */ void *qcom_smem_get(unsigned host, unsigned item, size_t *size) { + struct smem_ptable_entry *entry; unsigned long flags; int ret; void *ptr = ERR_PTR(-EPROBE_DEFER); @@ -503,17 +789,24 @@ if (!__smem) return ptr; + if (WARN_ON(item >= __smem->item_count)) + return ERR_PTR(-EINVAL); + ret = hwspin_lock_timeout_irqsave(__smem->hwlock, HWSPINLOCK_TIMEOUT, &flags); if (ret) return ERR_PTR(ret); - if (host < SMEM_HOST_COUNT && __smem->partitions[host]) - ptr = qcom_smem_get_private(__smem, host, item, size); - else + if (host < SMEM_HOST_COUNT && __smem->ptable_entries[host]) { + entry = __smem->ptable_entries[host]; + ptr = qcom_smem_get_private(__smem, entry, item, size); + } else if (__smem->global_partition_entry) { + entry = __smem->global_partition_entry; + ptr = qcom_smem_get_private(__smem, entry, item, size); + } else { ptr = qcom_smem_get_global(__smem, item, size); - + } hwspin_unlock_irqrestore(__smem->hwlock, &flags); return ptr; @@ -531,65 +824,195 @@ int qcom_smem_get_free_space(unsigned host) { struct smem_partition_header *phdr; + struct smem_ptable_entry *entry; struct smem_header *header; unsigned ret; if (!__smem) return -EPROBE_DEFER; - if (host < SMEM_HOST_COUNT && __smem->partitions[host]) { - phdr = __smem->partitions[host]; + if (host < SMEM_HOST_COUNT && __smem->ptable_entries[host]) { + entry = __smem->ptable_entries[host]; + phdr = ptable_entry_to_phdr(entry); + ret = le32_to_cpu(phdr->offset_free_cached) - le32_to_cpu(phdr->offset_free_uncached); + + if (ret > le32_to_cpu(entry->size)) + return -EINVAL; + } else if (__smem->global_partition_entry) { + entry = __smem->global_partition_entry; + phdr = ptable_entry_to_phdr(entry); + + ret = le32_to_cpu(phdr->offset_free_cached) - + le32_to_cpu(phdr->offset_free_uncached); + if (ret > le32_to_cpu(entry->size)) + return -EINVAL; } else { header = __smem->regions[0].virt_base; ret = le32_to_cpu(header->available); + + if (ret > __smem->regions[0].size) + return -EINVAL; } return ret; } EXPORT_SYMBOL(qcom_smem_get_free_space); -static int qcom_smem_get_sbl_version(struct qcom_smem *smem) +/** + * qcom_smem_virt_to_phys() - return the physical address associated + * with an smem item pointer (previously returned by qcom_smem_get() + * @p: the virtual address to convert + * + * Returns 0 if the pointer provided is not within any smem region. + */ +phys_addr_t qcom_smem_virt_to_phys(void *p) { - __le32 *versions; - size_t size; + unsigned i; - versions = qcom_smem_get_global(smem, SMEM_ITEM_VERSION, &size); - if (IS_ERR(versions)) { - dev_err(smem->dev, "Unable to read the version item\n"); - return -ENOENT; - } + for (i = 0; i < __smem->num_regions; i++) { + struct smem_region *region = &__smem->regions[i]; - if (size < sizeof(unsigned) * SMEM_MASTER_SBL_VERSION_INDEX) { - dev_err(smem->dev, "Version item is too small\n"); - return -EINVAL; + if (p < region->virt_base) + continue; + if (p < region->virt_base + region->size) { + u64 offset = p - region->virt_base; + + return (phys_addr_t)region->aux_base + offset; + } } + return 0; +} +EXPORT_SYMBOL(qcom_smem_virt_to_phys); + +static int qcom_smem_get_sbl_version(struct qcom_smem *smem) +{ + struct smem_header *header; + __le32 *versions; + + header = smem->regions[0].virt_base; + versions = header->version; + return le32_to_cpu(versions[SMEM_MASTER_SBL_VERSION_INDEX]); } -static int qcom_smem_enumerate_partitions(struct qcom_smem *smem, - unsigned local_host) +static struct smem_ptable *qcom_smem_get_ptable(struct qcom_smem *smem) { - struct smem_partition_header *header; - struct smem_ptable_entry *entry; struct smem_ptable *ptable; - unsigned remote_host; - u32 version, host0, host1; - int i; + u32 version; ptable = smem->regions[0].virt_base + smem->regions[0].size - SZ_4K; if (memcmp(ptable->magic, SMEM_PTABLE_MAGIC, sizeof(ptable->magic))) - return 0; + return NULL; version = le32_to_cpu(ptable->version); if (version != 1) { dev_err(smem->dev, "Unsupported partition header version %d\n", version); + return ERR_PTR(-EINVAL); + } + return ptable; +} + +static u32 qcom_smem_get_dynamic_item(struct qcom_smem *smem) +{ + struct smem_ptable *ptable; + struct smem_info *info; + + ptable = qcom_smem_get_ptable(smem); + if (IS_ERR_OR_NULL(ptable)) + return SMEM_ITEM_COUNT; + + info = (struct smem_info *)&ptable->entry[ptable->num_entries]; + if (memcmp(info->magic, SMEM_INFO_MAGIC, sizeof(info->magic))) + return SMEM_ITEM_COUNT; + + return le16_to_cpu(info->num_items); +} + +static int qcom_smem_set_global_partition(struct qcom_smem *smem) +{ + struct smem_partition_header *header; + struct smem_ptable_entry *entry = NULL; + struct smem_ptable *ptable; + u32 host0, host1, size; + int i; + + ptable = qcom_smem_get_ptable(smem); + if (IS_ERR_OR_NULL(ptable)) + return -EINVAL; + + for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) { + entry = &ptable->entry[i]; + host0 = le16_to_cpu(entry->host0); + host1 = le16_to_cpu(entry->host1); + + if (host0 == SMEM_GLOBAL_HOST && host0 == host1) + break; + } + + if (!entry) { + dev_err(smem->dev, "Missing entry for global partition\n"); + return -EINVAL; + } + + if (!le32_to_cpu(entry->offset) || !le32_to_cpu(entry->size)) { + dev_err(smem->dev, "Invalid entry for global partition\n"); return -EINVAL; } + if (smem->global_partition_entry) { + dev_err(smem->dev, "Already found the global partition\n"); + return -EINVAL; + } + + header = smem->regions[0].virt_base + le32_to_cpu(entry->offset); + host0 = le16_to_cpu(header->host0); + host1 = le16_to_cpu(header->host1); + + if (memcmp(header->magic, SMEM_PART_MAGIC, sizeof(header->magic))) { + dev_err(smem->dev, "Global partition has invalid magic\n"); + return -EINVAL; + } + + if (host0 != SMEM_GLOBAL_HOST && host1 != SMEM_GLOBAL_HOST) { + dev_err(smem->dev, "Global partition hosts are invalid\n"); + return -EINVAL; + } + + if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) { + dev_err(smem->dev, "Global partition has invalid size\n"); + return -EINVAL; + } + + size = le32_to_cpu(header->offset_free_uncached); + if (size > le32_to_cpu(header->size)) { + dev_err(smem->dev, + "Global partition has invalid free pointer\n"); + return -EINVAL; + } + + smem->global_partition_entry = entry; + + return 0; +} + +static int qcom_smem_enumerate_partitions(struct qcom_smem *smem, + unsigned int local_host) +{ + struct smem_partition_header *header; + struct smem_ptable_entry *entry; + struct smem_ptable *ptable; + unsigned int remote_host; + u32 host0, host1; + int i; + + ptable = qcom_smem_get_ptable(smem); + if (IS_ERR_OR_NULL(ptable)) + return PTR_ERR(ptable); + for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) { entry = &ptable->entry[i]; host0 = le16_to_cpu(entry->host0); @@ -616,7 +1039,7 @@ return -EINVAL; } - if (smem->partitions[remote_host]) { + if (smem->ptable_entries[remote_host]) { dev_err(smem->dev, "Already found a partition for host %d\n", remote_host); @@ -658,7 +1081,7 @@ return -EINVAL; } - smem->partitions[remote_host] = header; + smem->ptable_entries[remote_host] = entry; } return 0; @@ -702,6 +1125,10 @@ u32 version; int ret; + hwlock_id = of_hwspin_lock_get_id(pdev->dev.of_node, 0); + if (hwlock_id < 0) + return -EPROBE_DEFER; + num_regions = 1; if (of_find_property(pdev->dev.of_node, "qcom,rpm-msg-ram", NULL)) num_regions++; @@ -730,7 +1157,17 @@ } version = qcom_smem_get_sbl_version(smem); - if (version >> 16 != SMEM_EXPECTED_VERSION) { + 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_dynamic_item(smem); + break; + case SMEM_GLOBAL_HEAP_VERSION: + smem->item_count = SMEM_ITEM_COUNT; + break; + default: dev_err(&pdev->dev, "Unsupported SMEM version 0x%x\n", version); return -EINVAL; } @@ -739,12 +1176,6 @@ if (ret < 0) return ret; - hwlock_id = of_hwspin_lock_get_id(pdev->dev.of_node, 0); - if (hwlock_id < 0) { - dev_err(&pdev->dev, "failed to retrieve hwlock\n"); - return hwlock_id; - } - smem->hwlock = hwspin_lock_request_specific(hwlock_id); if (!smem->hwlock) return -ENXIO; @@ -780,6 +1211,7 @@ static int __init qcom_smem_init(void) { + print_soc_version_info(); return platform_driver_register(&qcom_smem_driver); } arch_initcall(qcom_smem_init);