1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015, Sony Mobile Communications AB.
4 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
5 * Copyright (c) 2018, Ramon Fried <ramon.fried@gmail.com>
11 #include <dm/devres.h>
12 #include <dm/of_access.h>
13 #include <dm/of_addr.h>
15 #include <linux/err.h>
16 #include <linux/ioport.h>
20 DECLARE_GLOBAL_DATA_PTR;
23 * The Qualcomm shared memory system is an allocate-only heap structure that
24 * consists of one of more memory areas that can be accessed by the processors
27 * All systems contains a global heap, accessible by all processors in the SoC,
28 * with a table of contents data structure (@smem_header) at the beginning of
29 * the main shared memory block.
31 * The global header contains meta data for allocations as well as a fixed list
32 * of 512 entries (@smem_global_entry) that can be initialized to reference
33 * parts of the shared memory space.
36 * In addition to this global heap, a set of "private" heaps can be set up at
37 * boot time with access restrictions so that only certain processor pairs can
40 * These partitions are referenced from an optional partition table
41 * (@smem_ptable), that is found 4kB from the end of the main smem region. The
42 * partition table entries (@smem_ptable_entry) lists the involved processors
43 * (or hosts) and their location in the main shared memory region.
45 * Each partition starts with a header (@smem_partition_header) that identifies
46 * the partition and holds properties for the two internal memory regions. The
47 * two regions are cached and non-cached memory respectively. Each region
48 * contain a link list of allocation headers (@smem_private_entry) followed by
51 * Items in the non-cached region are allocated from the start of the partition
52 * while items in the cached region are allocated from the end. The free area
53 * is hence the region between the cached and non-cached offsets. The header of
54 * cached items comes after the data.
56 * Version 12 (SMEM_GLOBAL_PART_VERSION) changes the item alloc/get procedure
57 * for the global heap. A new global partition is created from the global heap
58 * region with partition type (SMEM_GLOBAL_HOST) and the max smem item count is
59 * set by the bootloader.
64 * The version member of the smem header contains an array of versions for the
65 * various software components in the SoC. We verify that the boot loader
66 * version is a valid version as a sanity check.
68 #define SMEM_MASTER_SBL_VERSION_INDEX 7
69 #define SMEM_GLOBAL_HEAP_VERSION 11
70 #define SMEM_GLOBAL_PART_VERSION 12
73 * The first 8 items are only to be allocated by the boot loader while
74 * initializing the heap.
76 #define SMEM_ITEM_LAST_FIXED 8
78 /* Highest accepted item number, for both global and private heaps */
79 #define SMEM_ITEM_COUNT 512
81 /* Processor/host identifier for the application processor */
82 #define SMEM_HOST_APPS 0
84 /* Processor/host identifier for the global partition */
85 #define SMEM_GLOBAL_HOST 0xfffe
87 /* Max number of processors/hosts in a system */
88 #define SMEM_HOST_COUNT 10
91 * struct smem_proc_comm - proc_comm communication struct (legacy)
92 * @command: current command to be executed
93 * @status: status of the currently requested command
94 * @params: parameters to the command
96 struct smem_proc_comm {
103 * struct smem_global_entry - entry to reference smem items on the heap
104 * @allocated: boolean to indicate if this entry is used
105 * @offset: offset to the allocated space
106 * @size: size of the allocated space, 8 byte aligned
107 * @aux_base: base address for the memory region used by this unit, or 0 for
108 * the default region. bits 0,1 are reserved
110 struct smem_global_entry {
114 __le32 aux_base; /* bits 1:0 reserved */
116 #define AUX_BASE_MASK 0xfffffffc
119 * struct smem_header - header found in beginning of primary smem region
120 * @proc_comm: proc_comm communication interface (legacy)
121 * @version: array of versions for the various subsystems
122 * @initialized: boolean to indicate that smem is initialized
123 * @free_offset: index of the first unallocated byte in smem
124 * @available: number of bytes available for allocation
125 * @reserved: reserved field, must be 0
126 * toc: array of references to items
129 struct smem_proc_comm proc_comm[4];
135 struct smem_global_entry toc[SMEM_ITEM_COUNT];
139 * struct smem_ptable_entry - one entry in the @smem_ptable list
140 * @offset: offset, within the main shared memory region, of the partition
141 * @size: size of the partition
142 * @flags: flags for the partition (currently unused)
143 * @host0: first processor/host with access to this partition
144 * @host1: second processor/host with access to this partition
145 * @cacheline: alignment for "cached" entries
146 * @reserved: reserved entries for later use
148 struct smem_ptable_entry {
159 * struct smem_ptable - partition table for the private partitions
160 * @magic: magic number, must be SMEM_PTABLE_MAGIC
161 * @version: version of the partition table
162 * @num_entries: number of partitions in the table
163 * @reserved: for now reserved entries
164 * @entry: list of @smem_ptable_entry for the @num_entries partitions
171 struct smem_ptable_entry entry[];
174 static const u8 SMEM_PTABLE_MAGIC[] = { 0x24, 0x54, 0x4f, 0x43 }; /* "$TOC" */
177 * struct smem_partition_header - header of the partitions
178 * @magic: magic number, must be SMEM_PART_MAGIC
179 * @host0: first processor/host with access to this partition
180 * @host1: second processor/host with access to this partition
181 * @size: size of the partition
182 * @offset_free_uncached: offset to the first free byte of uncached memory in
184 * @offset_free_cached: offset to the first free byte of cached memory in this
186 * @reserved: for now reserved entries
188 struct smem_partition_header {
193 __le32 offset_free_uncached;
194 __le32 offset_free_cached;
198 static const u8 SMEM_PART_MAGIC[] = { 0x24, 0x50, 0x52, 0x54 };
201 * struct smem_private_entry - header of each item in the private partition
202 * @canary: magic number, must be SMEM_PRIVATE_CANARY
203 * @item: identifying number of the smem item
204 * @size: size of the data, including padding bytes
205 * @padding_data: number of bytes of padding of data
206 * @padding_hdr: number of bytes of padding between the header and the data
207 * @reserved: for now reserved entry
209 struct smem_private_entry {
210 u16 canary; /* bytes are the same so no swapping needed */
212 __le32 size; /* includes padding bytes */
217 #define SMEM_PRIVATE_CANARY 0xa5a5
220 * struct smem_info - smem region info located after the table of contents
221 * @magic: magic number, must be SMEM_INFO_MAGIC
222 * @size: size of the smem region
223 * @base_addr: base address of the smem region
224 * @reserved: for now reserved entry
225 * @num_items: highest accepted item number
235 static const u8 SMEM_INFO_MAGIC[] = { 0x53, 0x49, 0x49, 0x49 }; /* SIII */
238 * struct smem_region - representation of a chunk of memory used for smem
239 * @aux_base: identifier of aux_mem base
240 * @virt_base: virtual base address of memory with this aux_mem identifier
241 * @size: size of the memory region
245 void __iomem *virt_base;
250 * struct qcom_smem - device data for the smem device
251 * @dev: device pointer
252 * @global_partition: pointer to global partition when in use
253 * @global_cacheline: cacheline size for global partition
254 * @partitions: list of pointers to partitions affecting the current
256 * @cacheline: list of cacheline sizes for each host
257 * @item_count: max accepted item number
258 * @num_regions: number of @regions
259 * @regions: list of the memory regions defining the shared memory
264 struct smem_partition_header *global_partition;
265 size_t global_cacheline;
266 struct smem_partition_header *partitions[SMEM_HOST_COUNT];
267 size_t cacheline[SMEM_HOST_COUNT];
270 unsigned int num_regions;
271 struct smem_region regions[0];
274 static struct smem_private_entry *
275 phdr_to_last_uncached_entry(struct smem_partition_header *phdr)
279 return p + le32_to_cpu(phdr->offset_free_uncached);
282 static void *phdr_to_first_cached_entry(struct smem_partition_header *phdr,
287 return p + le32_to_cpu(phdr->size) - ALIGN(sizeof(*phdr), cacheline);
290 static void *phdr_to_last_cached_entry(struct smem_partition_header *phdr)
294 return p + le32_to_cpu(phdr->offset_free_cached);
297 static struct smem_private_entry *
298 phdr_to_first_uncached_entry(struct smem_partition_header *phdr)
302 return p + sizeof(*phdr);
305 static struct smem_private_entry *
306 uncached_entry_next(struct smem_private_entry *e)
310 return p + sizeof(*e) + le16_to_cpu(e->padding_hdr) +
311 le32_to_cpu(e->size);
314 static struct smem_private_entry *
315 cached_entry_next(struct smem_private_entry *e, size_t cacheline)
319 return p - le32_to_cpu(e->size) - ALIGN(sizeof(*e), cacheline);
322 static void *uncached_entry_to_item(struct smem_private_entry *e)
326 return p + sizeof(*e) + le16_to_cpu(e->padding_hdr);
329 static void *cached_entry_to_item(struct smem_private_entry *e)
333 return p - le32_to_cpu(e->size);
336 /* Pointer to the one and only smem handle */
337 static struct qcom_smem *__smem;
339 static int qcom_smem_alloc_private(struct qcom_smem *smem,
340 struct smem_partition_header *phdr,
344 struct smem_private_entry *hdr, *end;
348 hdr = phdr_to_first_uncached_entry(phdr);
349 end = phdr_to_last_uncached_entry(phdr);
350 cached = phdr_to_last_cached_entry(phdr);
353 if (hdr->canary != SMEM_PRIVATE_CANARY) {
355 "Found invalid canary in hosts %d:%d partition\n",
356 phdr->host0, phdr->host1);
360 if (le16_to_cpu(hdr->item) == item)
363 hdr = uncached_entry_next(hdr);
366 /* Check that we don't grow into the cached region */
367 alloc_size = sizeof(*hdr) + ALIGN(size, 8);
368 if ((void *)hdr + alloc_size >= cached) {
369 dev_err(smem->dev, "Out of memory\n");
373 hdr->canary = SMEM_PRIVATE_CANARY;
374 hdr->item = cpu_to_le16(item);
375 hdr->size = cpu_to_le32(ALIGN(size, 8));
376 hdr->padding_data = cpu_to_le16(le32_to_cpu(hdr->size) - size);
377 hdr->padding_hdr = 0;
380 * Ensure the header is written before we advance the free offset, so
381 * that remote processors that does not take the remote spinlock still
382 * gets a consistent view of the linked list.
385 le32_add_cpu(&phdr->offset_free_uncached, alloc_size);
390 static int qcom_smem_alloc_global(struct qcom_smem *smem,
394 struct smem_global_entry *entry;
395 struct smem_header *header;
397 header = smem->regions[0].virt_base;
398 entry = &header->toc[item];
399 if (entry->allocated)
402 size = ALIGN(size, 8);
403 if (WARN_ON(size > le32_to_cpu(header->available)))
406 entry->offset = header->free_offset;
407 entry->size = cpu_to_le32(size);
410 * Ensure the header is consistent before we mark the item allocated,
411 * so that remote processors will get a consistent view of the item
412 * even though they do not take the spinlock on read.
415 entry->allocated = cpu_to_le32(1);
417 le32_add_cpu(&header->free_offset, size);
418 le32_add_cpu(&header->available, -size);
424 * qcom_smem_alloc() - allocate space for a smem item
425 * @host: remote processor id, or -1
426 * @item: smem item handle
427 * @size: number of bytes to be allocated
429 * Allocate space for a given smem item of size @size, given that the item is
432 static int qcom_smem_alloc(unsigned int host, unsigned int item, size_t size)
434 struct smem_partition_header *phdr;
438 return -EPROBE_DEFER;
440 if (item < SMEM_ITEM_LAST_FIXED) {
442 "Rejecting allocation of static entry %d\n", item);
446 if (WARN_ON(item >= __smem->item_count))
449 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
450 phdr = __smem->partitions[host];
451 ret = qcom_smem_alloc_private(__smem, phdr, item, size);
452 } else if (__smem->global_partition) {
453 phdr = __smem->global_partition;
454 ret = qcom_smem_alloc_private(__smem, phdr, item, size);
456 ret = qcom_smem_alloc_global(__smem, item, size);
462 static void *qcom_smem_get_global(struct qcom_smem *smem,
466 struct smem_header *header;
467 struct smem_region *area;
468 struct smem_global_entry *entry;
472 header = smem->regions[0].virt_base;
473 entry = &header->toc[item];
474 if (!entry->allocated)
475 return ERR_PTR(-ENXIO);
477 aux_base = le32_to_cpu(entry->aux_base) & AUX_BASE_MASK;
479 for (i = 0; i < smem->num_regions; i++) {
480 area = &smem->regions[i];
482 if (area->aux_base == aux_base || !aux_base) {
484 *size = le32_to_cpu(entry->size);
485 return area->virt_base + le32_to_cpu(entry->offset);
489 return ERR_PTR(-ENOENT);
492 static void *qcom_smem_get_private(struct qcom_smem *smem,
493 struct smem_partition_header *phdr,
498 struct smem_private_entry *e, *end;
500 e = phdr_to_first_uncached_entry(phdr);
501 end = phdr_to_last_uncached_entry(phdr);
504 if (e->canary != SMEM_PRIVATE_CANARY)
507 if (le16_to_cpu(e->item) == item) {
509 *size = le32_to_cpu(e->size) -
510 le16_to_cpu(e->padding_data);
512 return uncached_entry_to_item(e);
515 e = uncached_entry_next(e);
518 /* Item was not found in the uncached list, search the cached list */
520 e = phdr_to_first_cached_entry(phdr, cacheline);
521 end = phdr_to_last_cached_entry(phdr);
524 if (e->canary != SMEM_PRIVATE_CANARY)
527 if (le16_to_cpu(e->item) == item) {
529 *size = le32_to_cpu(e->size) -
530 le16_to_cpu(e->padding_data);
532 return cached_entry_to_item(e);
535 e = cached_entry_next(e, cacheline);
538 return ERR_PTR(-ENOENT);
541 dev_err(smem->dev, "Found invalid canary in hosts %d:%d partition\n",
542 phdr->host0, phdr->host1);
544 return ERR_PTR(-EINVAL);
548 * qcom_smem_get() - resolve ptr of size of a smem item
549 * @host: the remote processor, or -1
550 * @item: smem item handle
551 * @size: pointer to be filled out with size of the item
553 * Looks up smem item and returns pointer to it. Size of smem
554 * item is returned in @size.
556 static void *qcom_smem_get(unsigned int host, unsigned int item, size_t *size)
558 struct smem_partition_header *phdr;
560 void *ptr = ERR_PTR(-EPROBE_DEFER);
565 if (WARN_ON(item >= __smem->item_count))
566 return ERR_PTR(-EINVAL);
568 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
569 phdr = __smem->partitions[host];
570 cacheln = __smem->cacheline[host];
571 ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
572 } else if (__smem->global_partition) {
573 phdr = __smem->global_partition;
574 cacheln = __smem->global_cacheline;
575 ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
577 ptr = qcom_smem_get_global(__smem, item, size);
585 * qcom_smem_get_free_space() - retrieve amount of free space in a partition
586 * @host: the remote processor identifying a partition, or -1
588 * To be used by smem clients as a quick way to determine if any new
589 * allocations has been made.
591 static int qcom_smem_get_free_space(unsigned int host)
593 struct smem_partition_header *phdr;
594 struct smem_header *header;
598 return -EPROBE_DEFER;
600 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
601 phdr = __smem->partitions[host];
602 ret = le32_to_cpu(phdr->offset_free_cached) -
603 le32_to_cpu(phdr->offset_free_uncached);
604 } else if (__smem->global_partition) {
605 phdr = __smem->global_partition;
606 ret = le32_to_cpu(phdr->offset_free_cached) -
607 le32_to_cpu(phdr->offset_free_uncached);
609 header = __smem->regions[0].virt_base;
610 ret = le32_to_cpu(header->available);
616 static int qcom_smem_get_sbl_version(struct qcom_smem *smem)
618 struct smem_header *header;
621 header = smem->regions[0].virt_base;
622 versions = header->version;
624 return le32_to_cpu(versions[SMEM_MASTER_SBL_VERSION_INDEX]);
627 static struct smem_ptable *qcom_smem_get_ptable(struct qcom_smem *smem)
629 struct smem_ptable *ptable;
632 ptable = smem->regions[0].virt_base + smem->regions[0].size - SZ_4K;
633 if (memcmp(ptable->magic, SMEM_PTABLE_MAGIC, sizeof(ptable->magic)))
634 return ERR_PTR(-ENOENT);
636 version = le32_to_cpu(ptable->version);
639 "Unsupported partition header version %d\n", version);
640 return ERR_PTR(-EINVAL);
645 static u32 qcom_smem_get_item_count(struct qcom_smem *smem)
647 struct smem_ptable *ptable;
648 struct smem_info *info;
650 ptable = qcom_smem_get_ptable(smem);
651 if (IS_ERR_OR_NULL(ptable))
652 return SMEM_ITEM_COUNT;
654 info = (struct smem_info *)&ptable->entry[ptable->num_entries];
655 if (memcmp(info->magic, SMEM_INFO_MAGIC, sizeof(info->magic)))
656 return SMEM_ITEM_COUNT;
658 return le16_to_cpu(info->num_items);
661 static int qcom_smem_set_global_partition(struct qcom_smem *smem)
663 struct smem_partition_header *header;
664 struct smem_ptable_entry *entry = NULL;
665 struct smem_ptable *ptable;
666 u32 host0, host1, size;
669 ptable = qcom_smem_get_ptable(smem);
671 return PTR_ERR(ptable);
673 for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
674 entry = &ptable->entry[i];
675 host0 = le16_to_cpu(entry->host0);
676 host1 = le16_to_cpu(entry->host1);
678 if (host0 == SMEM_GLOBAL_HOST && host0 == host1)
683 dev_err(smem->dev, "Missing entry for global partition\n");
687 if (!le32_to_cpu(entry->offset) || !le32_to_cpu(entry->size)) {
688 dev_err(smem->dev, "Invalid entry for global partition\n");
692 if (smem->global_partition) {
693 dev_err(smem->dev, "Already found the global partition\n");
697 header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
698 host0 = le16_to_cpu(header->host0);
699 host1 = le16_to_cpu(header->host1);
701 if (memcmp(header->magic, SMEM_PART_MAGIC, sizeof(header->magic))) {
702 dev_err(smem->dev, "Global partition has invalid magic\n");
706 if (host0 != SMEM_GLOBAL_HOST && host1 != SMEM_GLOBAL_HOST) {
707 dev_err(smem->dev, "Global partition hosts are invalid\n");
711 if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
712 dev_err(smem->dev, "Global partition has invalid size\n");
716 size = le32_to_cpu(header->offset_free_uncached);
717 if (size > le32_to_cpu(header->size)) {
719 "Global partition has invalid free pointer\n");
723 smem->global_partition = header;
724 smem->global_cacheline = le32_to_cpu(entry->cacheline);
729 static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
730 unsigned int local_host)
732 struct smem_partition_header *header;
733 struct smem_ptable_entry *entry;
734 struct smem_ptable *ptable;
735 unsigned int remote_host;
739 ptable = qcom_smem_get_ptable(smem);
741 return PTR_ERR(ptable);
743 for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
744 entry = &ptable->entry[i];
745 host0 = le16_to_cpu(entry->host0);
746 host1 = le16_to_cpu(entry->host1);
748 if (host0 != local_host && host1 != local_host)
751 if (!le32_to_cpu(entry->offset))
754 if (!le32_to_cpu(entry->size))
757 if (host0 == local_host)
762 if (remote_host >= SMEM_HOST_COUNT) {
764 "Invalid remote host %d\n",
769 if (smem->partitions[remote_host]) {
771 "Already found a partition for host %d\n",
776 header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
777 host0 = le16_to_cpu(header->host0);
778 host1 = le16_to_cpu(header->host1);
780 if (memcmp(header->magic, SMEM_PART_MAGIC,
781 sizeof(header->magic))) {
783 "Partition %d has invalid magic\n", i);
787 if (host0 != local_host && host1 != local_host) {
789 "Partition %d hosts are invalid\n", i);
793 if (host0 != remote_host && host1 != remote_host) {
795 "Partition %d hosts are invalid\n", i);
799 if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
801 "Partition %d has invalid size\n", i);
805 if (le32_to_cpu(header->offset_free_uncached) > le32_to_cpu(header->size)) {
807 "Partition %d has invalid free pointer\n", i);
811 smem->partitions[remote_host] = header;
812 smem->cacheline[remote_host] = le32_to_cpu(entry->cacheline);
818 static int qcom_smem_map_memory(struct qcom_smem *smem, struct udevice *dev,
819 const char *name, int i)
821 struct fdt_resource r;
823 int node = dev_of_offset(dev);
825 ret = fdtdec_lookup_phandle(gd->fdt_blob, node, name);
827 dev_err(dev, "No %s specified\n", name);
831 ret = fdt_get_resource(gd->fdt_blob, ret, "reg", 0, &r);
835 smem->regions[i].aux_base = (u32)r.start;
836 smem->regions[i].size = fdt_resource_size(&r);
837 smem->regions[i].virt_base = devm_ioremap(dev, r.start, fdt_resource_size(&r));
838 if (!smem->regions[i].virt_base)
844 static int qcom_smem_probe(struct udevice *dev)
846 struct smem_header *header;
847 struct qcom_smem *smem;
852 int node = dev_of_offset(dev);
855 if (fdtdec_lookup_phandle(gd->fdt_blob, node, "qcomrpm-msg-ram") >= 0)
858 array_size = num_regions * sizeof(struct smem_region);
859 smem = devm_kzalloc(dev, sizeof(*smem) + array_size, GFP_KERNEL);
864 smem->num_regions = num_regions;
866 ret = qcom_smem_map_memory(smem, dev, "memory-region", 0);
870 if (num_regions > 1) {
871 ret = qcom_smem_map_memory(smem, dev,
872 "qcom,rpm-msg-ram", 1);
877 header = smem->regions[0].virt_base;
878 if (le32_to_cpu(header->initialized) != 1 ||
879 le32_to_cpu(header->reserved)) {
880 dev_err(&pdev->dev, "SMEM is not initialized by SBL\n");
884 version = qcom_smem_get_sbl_version(smem);
885 switch (version >> 16) {
886 case SMEM_GLOBAL_PART_VERSION:
887 ret = qcom_smem_set_global_partition(smem);
890 smem->item_count = qcom_smem_get_item_count(smem);
892 case SMEM_GLOBAL_HEAP_VERSION:
893 smem->item_count = SMEM_ITEM_COUNT;
896 dev_err(dev, "Unsupported SMEM version 0x%x\n", version);
900 ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
901 if (ret < 0 && ret != -ENOENT)
909 static int qcom_smem_remove(struct udevice *dev)
916 const struct udevice_id qcom_smem_of_match[] = {
917 { .compatible = "qcom,smem" },
921 static const struct smem_ops msm_smem_ops = {
922 .alloc = qcom_smem_alloc,
923 .get = qcom_smem_get,
924 .get_free_space = qcom_smem_get_free_space,
927 U_BOOT_DRIVER(qcom_smem) = {
930 .of_match = qcom_smem_of_match,
931 .ops = &msm_smem_ops,
932 .probe = qcom_smem_probe,
933 .remove = qcom_smem_remove,