// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) 2015, Sony Mobile Communications AB. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. */ #include <linux/hwspinlock.h> #include <linux/io.h> #include <linux/module.h> #include <linux/of.h> #include <linux/of_address.h> #include <linux/of_reserved_mem.h> #include <linux/platform_device.h> #include <linux/sizes.h> #include <linux/slab.h> #include <linux/soc/qcom/smem.h> #include <linux/soc/qcom/socinfo.h> /* * 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 … #define SMEM_GLOBAL_HEAP_VERSION … #define SMEM_GLOBAL_PART_VERSION … /* * The first 8 items are only to be allocated by the boot loader while * initializing the heap. */ #define SMEM_ITEM_LAST_FIXED … /* Highest accepted item number, for both global and private heaps */ #define SMEM_ITEM_COUNT … /* Processor/host identifier for the application processor */ #define SMEM_HOST_APPS … /* Processor/host identifier for the global partition */ #define SMEM_GLOBAL_HOST … /* Max number of processors/hosts in a system */ #define SMEM_HOST_COUNT … /** * 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 { … }; /** * 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 { … }; #define AUX_BASE_MASK … /** * 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_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 { … }; /** * 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 { … }; static const u8 SMEM_PTABLE_MAGIC[] = …; /* "$TOC" */ /** * 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 { … }; /** * struct smem_partition - describes smem partition * @virt_base: starting virtual address of partition * @phys_base: starting physical address of partition * @cacheline: alignment for "cached" entries * @size: size of partition */ struct smem_partition { … }; static const u8 SMEM_PART_MAGIC[] = …; /** * 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 { … }; #define SMEM_PRIVATE_CANARY … /** * 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 { … }; static const u8 SMEM_INFO_MAGIC[] = …; /* 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 * @size: size of the memory region */ struct smem_region { … }; /** * 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 { … }; static void * phdr_to_last_uncached_entry(struct smem_partition_header *phdr) { … } static struct smem_private_entry * phdr_to_first_cached_entry(struct smem_partition_header *phdr, size_t cacheline) { … } static void * phdr_to_last_cached_entry(struct smem_partition_header *phdr) { … } static struct smem_private_entry * phdr_to_first_uncached_entry(struct smem_partition_header *phdr) { … } static struct smem_private_entry * uncached_entry_next(struct smem_private_entry *e) { … } static struct smem_private_entry * cached_entry_next(struct smem_private_entry *e, size_t cacheline) { … } static void *uncached_entry_to_item(struct smem_private_entry *e) { … } static void *cached_entry_to_item(struct smem_private_entry *e) { … } /* Pointer to the one and only smem handle */ static struct qcom_smem *__smem; /* Timeout (ms) for the trylock of remote spinlocks */ #define HWSPINLOCK_TIMEOUT … /* The qcom hwspinlock id is always plus one from the smem host id */ #define SMEM_HOST_ID_TO_HWSPINLOCK_ID(__x) … /** * 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(unsigned int host) { … } EXPORT_SYMBOL_GPL(…); /** * qcom_smem_is_available() - Check if SMEM is available * * Return: true if SMEM is available, false otherwise. */ bool qcom_smem_is_available(void) { … } EXPORT_SYMBOL_GPL(…); static int qcom_smem_alloc_private(struct qcom_smem *smem, struct smem_partition *part, unsigned item, size_t size) { … } static int qcom_smem_alloc_global(struct qcom_smem *smem, unsigned item, size_t size) { … } /** * 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. */ int qcom_smem_alloc(unsigned host, unsigned item, size_t size) { … } EXPORT_SYMBOL_GPL(…); static void *qcom_smem_get_global(struct qcom_smem *smem, unsigned item, size_t *size) { … } static void *qcom_smem_get_private(struct qcom_smem *smem, struct smem_partition *part, unsigned item, size_t *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. */ void *qcom_smem_get(unsigned host, unsigned item, size_t *size) { … } EXPORT_SYMBOL_GPL(…); /** * 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. */ int qcom_smem_get_free_space(unsigned host) { … } EXPORT_SYMBOL_GPL(…); static bool addr_in_range(void __iomem *base, size_t size, void *addr) { … } /** * 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) { … } EXPORT_SYMBOL_GPL(…); /** * 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) { … } EXPORT_SYMBOL_GPL(…); /** * 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) { … } EXPORT_SYMBOL_GPL(…); static int qcom_smem_get_sbl_version(struct qcom_smem *smem) { … } static struct smem_ptable *qcom_smem_get_ptable(struct qcom_smem *smem) { … } static u32 qcom_smem_get_item_count(struct qcom_smem *smem) { … } /* * Validate the partition header for a partition whose partition * table entry is supplied. Returns a pointer to its header if * valid, or a null pointer otherwise. */ static struct smem_partition_header * qcom_smem_partition_header(struct qcom_smem *smem, struct smem_ptable_entry *entry, u16 host0, u16 host1) { … } static int qcom_smem_set_global_partition(struct qcom_smem *smem) { … } static int qcom_smem_enumerate_partitions(struct qcom_smem *smem, u16 local_host) { … } static int qcom_smem_map_toc(struct qcom_smem *smem, struct smem_region *region) { … } static int qcom_smem_map_global(struct qcom_smem *smem, u32 size) { … } static int qcom_smem_resolve_mem(struct qcom_smem *smem, const char *name, struct smem_region *region) { … } static int qcom_smem_probe(struct platform_device *pdev) { … } static void qcom_smem_remove(struct platform_device *pdev) { … } static const struct of_device_id qcom_smem_of_match[] = …; MODULE_DEVICE_TABLE(of, qcom_smem_of_match); static struct platform_driver qcom_smem_driver = …; static int __init qcom_smem_init(void) { … } arch_initcall(qcom_smem_init); static void __exit qcom_smem_exit(void) { … } module_exit(…) MODULE_AUTHOR(…) …; MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …;