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/of_access.h>
12 #include <dm/of_addr.h>
14 #include <linux/ioport.h>
18 DECLARE_GLOBAL_DATA_PTR;
21 * The Qualcomm shared memory system is an allocate-only heap structure that
22 * consists of one of more memory areas that can be accessed by the processors
25 * All systems contains a global heap, accessible by all processors in the SoC,
26 * with a table of contents data structure (@smem_header) at the beginning of
27 * the main shared memory block.
29 * The global header contains meta data for allocations as well as a fixed list
30 * of 512 entries (@smem_global_entry) that can be initialized to reference
31 * parts of the shared memory space.
34 * In addition to this global heap, a set of "private" heaps can be set up at
35 * boot time with access restrictions so that only certain processor pairs can
38 * These partitions are referenced from an optional partition table
39 * (@smem_ptable), that is found 4kB from the end of the main smem region. The
40 * partition table entries (@smem_ptable_entry) lists the involved processors
41 * (or hosts) and their location in the main shared memory region.
43 * Each partition starts with a header (@smem_partition_header) that identifies
44 * the partition and holds properties for the two internal memory regions. The
45 * two regions are cached and non-cached memory respectively. Each region
46 * contain a link list of allocation headers (@smem_private_entry) followed by
49 * Items in the non-cached region are allocated from the start of the partition
50 * while items in the cached region are allocated from the end. The free area
51 * is hence the region between the cached and non-cached offsets. The header of
52 * cached items comes after the data.
54 * Version 12 (SMEM_GLOBAL_PART_VERSION) changes the item alloc/get procedure
55 * for the global heap. A new global partition is created from the global heap
56 * region with partition type (SMEM_GLOBAL_HOST) and the max smem item count is
57 * set by the bootloader.
62 * The version member of the smem header contains an array of versions for the
63 * various software components in the SoC. We verify that the boot loader
64 * version is a valid version as a sanity check.
66 #define SMEM_MASTER_SBL_VERSION_INDEX 7
67 #define SMEM_GLOBAL_HEAP_VERSION 11
68 #define SMEM_GLOBAL_PART_VERSION 12
71 * The first 8 items are only to be allocated by the boot loader while
72 * initializing the heap.
74 #define SMEM_ITEM_LAST_FIXED 8
76 /* Highest accepted item number, for both global and private heaps */
77 #define SMEM_ITEM_COUNT 512
79 /* Processor/host identifier for the application processor */
80 #define SMEM_HOST_APPS 0
82 /* Processor/host identifier for the global partition */
83 #define SMEM_GLOBAL_HOST 0xfffe
85 /* Max number of processors/hosts in a system */
86 #define SMEM_HOST_COUNT 10
89 * struct smem_proc_comm - proc_comm communication struct (legacy)
90 * @command: current command to be executed
91 * @status: status of the currently requested command
92 * @params: parameters to the command
94 struct smem_proc_comm {
101 * struct smem_global_entry - entry to reference smem items on the heap
102 * @allocated: boolean to indicate if this entry is used
103 * @offset: offset to the allocated space
104 * @size: size of the allocated space, 8 byte aligned
105 * @aux_base: base address for the memory region used by this unit, or 0 for
106 * the default region. bits 0,1 are reserved
108 struct smem_global_entry {
112 __le32 aux_base; /* bits 1:0 reserved */
114 #define AUX_BASE_MASK 0xfffffffc
117 * struct smem_header - header found in beginning of primary smem region
118 * @proc_comm: proc_comm communication interface (legacy)
119 * @version: array of versions for the various subsystems
120 * @initialized: boolean to indicate that smem is initialized
121 * @free_offset: index of the first unallocated byte in smem
122 * @available: number of bytes available for allocation
123 * @reserved: reserved field, must be 0
124 * toc: array of references to items
127 struct smem_proc_comm proc_comm[4];
133 struct smem_global_entry toc[SMEM_ITEM_COUNT];
137 * struct smem_ptable_entry - one entry in the @smem_ptable list
138 * @offset: offset, within the main shared memory region, of the partition
139 * @size: size of the partition
140 * @flags: flags for the partition (currently unused)
141 * @host0: first processor/host with access to this partition
142 * @host1: second processor/host with access to this partition
143 * @cacheline: alignment for "cached" entries
144 * @reserved: reserved entries for later use
146 struct smem_ptable_entry {
157 * struct smem_ptable - partition table for the private partitions
158 * @magic: magic number, must be SMEM_PTABLE_MAGIC
159 * @version: version of the partition table
160 * @num_entries: number of partitions in the table
161 * @reserved: for now reserved entries
162 * @entry: list of @smem_ptable_entry for the @num_entries partitions
169 struct smem_ptable_entry entry[];
172 static const u8 SMEM_PTABLE_MAGIC[] = { 0x24, 0x54, 0x4f, 0x43 }; /* "$TOC" */
175 * struct smem_partition_header - header of the partitions
176 * @magic: magic number, must be SMEM_PART_MAGIC
177 * @host0: first processor/host with access to this partition
178 * @host1: second processor/host with access to this partition
179 * @size: size of the partition
180 * @offset_free_uncached: offset to the first free byte of uncached memory in
182 * @offset_free_cached: offset to the first free byte of cached memory in this
184 * @reserved: for now reserved entries
186 struct smem_partition_header {
191 __le32 offset_free_uncached;
192 __le32 offset_free_cached;
196 static const u8 SMEM_PART_MAGIC[] = { 0x24, 0x50, 0x52, 0x54 };
199 * struct smem_private_entry - header of each item in the private partition
200 * @canary: magic number, must be SMEM_PRIVATE_CANARY
201 * @item: identifying number of the smem item
202 * @size: size of the data, including padding bytes
203 * @padding_data: number of bytes of padding of data
204 * @padding_hdr: number of bytes of padding between the header and the data
205 * @reserved: for now reserved entry
207 struct smem_private_entry {
208 u16 canary; /* bytes are the same so no swapping needed */
210 __le32 size; /* includes padding bytes */
215 #define SMEM_PRIVATE_CANARY 0xa5a5
218 * struct smem_info - smem region info located after the table of contents
219 * @magic: magic number, must be SMEM_INFO_MAGIC
220 * @size: size of the smem region
221 * @base_addr: base address of the smem region
222 * @reserved: for now reserved entry
223 * @num_items: highest accepted item number
233 static const u8 SMEM_INFO_MAGIC[] = { 0x53, 0x49, 0x49, 0x49 }; /* SIII */
236 * struct smem_region - representation of a chunk of memory used for smem
237 * @aux_base: identifier of aux_mem base
238 * @virt_base: virtual base address of memory with this aux_mem identifier
239 * @size: size of the memory region
243 void __iomem *virt_base;
248 * struct qcom_smem - device data for the smem device
249 * @dev: device pointer
250 * @global_partition: pointer to global partition when in use
251 * @global_cacheline: cacheline size for global partition
252 * @partitions: list of pointers to partitions affecting the current
254 * @cacheline: list of cacheline sizes for each host
255 * @item_count: max accepted item number
256 * @num_regions: number of @regions
257 * @regions: list of the memory regions defining the shared memory
262 struct smem_partition_header *global_partition;
263 size_t global_cacheline;
264 struct smem_partition_header *partitions[SMEM_HOST_COUNT];
265 size_t cacheline[SMEM_HOST_COUNT];
268 unsigned int num_regions;
269 struct smem_region regions[0];
272 static struct smem_private_entry *
273 phdr_to_last_uncached_entry(struct smem_partition_header *phdr)
277 return p + le32_to_cpu(phdr->offset_free_uncached);
280 static void *phdr_to_first_cached_entry(struct smem_partition_header *phdr,
285 return p + le32_to_cpu(phdr->size) - ALIGN(sizeof(*phdr), cacheline);
288 static void *phdr_to_last_cached_entry(struct smem_partition_header *phdr)
292 return p + le32_to_cpu(phdr->offset_free_cached);
295 static struct smem_private_entry *
296 phdr_to_first_uncached_entry(struct smem_partition_header *phdr)
300 return p + sizeof(*phdr);
303 static struct smem_private_entry *
304 uncached_entry_next(struct smem_private_entry *e)
308 return p + sizeof(*e) + le16_to_cpu(e->padding_hdr) +
309 le32_to_cpu(e->size);
312 static struct smem_private_entry *
313 cached_entry_next(struct smem_private_entry *e, size_t cacheline)
317 return p - le32_to_cpu(e->size) - ALIGN(sizeof(*e), cacheline);
320 static void *uncached_entry_to_item(struct smem_private_entry *e)
324 return p + sizeof(*e) + le16_to_cpu(e->padding_hdr);
327 static void *cached_entry_to_item(struct smem_private_entry *e)
331 return p - le32_to_cpu(e->size);
334 /* Pointer to the one and only smem handle */
335 static struct qcom_smem *__smem;
337 static int qcom_smem_alloc_private(struct qcom_smem *smem,
338 struct smem_partition_header *phdr,
342 struct smem_private_entry *hdr, *end;
346 hdr = phdr_to_first_uncached_entry(phdr);
347 end = phdr_to_last_uncached_entry(phdr);
348 cached = phdr_to_last_cached_entry(phdr);
351 if (hdr->canary != SMEM_PRIVATE_CANARY) {
353 "Found invalid canary in hosts %d:%d partition\n",
354 phdr->host0, phdr->host1);
358 if (le16_to_cpu(hdr->item) == item)
361 hdr = uncached_entry_next(hdr);
364 /* Check that we don't grow into the cached region */
365 alloc_size = sizeof(*hdr) + ALIGN(size, 8);
366 if ((void *)hdr + alloc_size >= cached) {
367 dev_err(smem->dev, "Out of memory\n");
371 hdr->canary = SMEM_PRIVATE_CANARY;
372 hdr->item = cpu_to_le16(item);
373 hdr->size = cpu_to_le32(ALIGN(size, 8));
374 hdr->padding_data = cpu_to_le16(le32_to_cpu(hdr->size) - size);
375 hdr->padding_hdr = 0;
378 * Ensure the header is written before we advance the free offset, so
379 * that remote processors that does not take the remote spinlock still
380 * gets a consistent view of the linked list.
383 le32_add_cpu(&phdr->offset_free_uncached, alloc_size);
388 static int qcom_smem_alloc_global(struct qcom_smem *smem,
392 struct smem_global_entry *entry;
393 struct smem_header *header;
395 header = smem->regions[0].virt_base;
396 entry = &header->toc[item];
397 if (entry->allocated)
400 size = ALIGN(size, 8);
401 if (WARN_ON(size > le32_to_cpu(header->available)))
404 entry->offset = header->free_offset;
405 entry->size = cpu_to_le32(size);
408 * Ensure the header is consistent before we mark the item allocated,
409 * so that remote processors will get a consistent view of the item
410 * even though they do not take the spinlock on read.
413 entry->allocated = cpu_to_le32(1);
415 le32_add_cpu(&header->free_offset, size);
416 le32_add_cpu(&header->available, -size);
422 * qcom_smem_alloc() - allocate space for a smem item
423 * @host: remote processor id, or -1
424 * @item: smem item handle
425 * @size: number of bytes to be allocated
427 * Allocate space for a given smem item of size @size, given that the item is
430 static int qcom_smem_alloc(unsigned int host, unsigned int item, size_t size)
432 struct smem_partition_header *phdr;
436 return -EPROBE_DEFER;
438 if (item < SMEM_ITEM_LAST_FIXED) {
440 "Rejecting allocation of static entry %d\n", item);
444 if (WARN_ON(item >= __smem->item_count))
447 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
448 phdr = __smem->partitions[host];
449 ret = qcom_smem_alloc_private(__smem, phdr, item, size);
450 } else if (__smem->global_partition) {
451 phdr = __smem->global_partition;
452 ret = qcom_smem_alloc_private(__smem, phdr, item, size);
454 ret = qcom_smem_alloc_global(__smem, item, size);
460 static void *qcom_smem_get_global(struct qcom_smem *smem,
464 struct smem_header *header;
465 struct smem_region *area;
466 struct smem_global_entry *entry;
470 header = smem->regions[0].virt_base;
471 entry = &header->toc[item];
472 if (!entry->allocated)
473 return ERR_PTR(-ENXIO);
475 aux_base = le32_to_cpu(entry->aux_base) & AUX_BASE_MASK;
477 for (i = 0; i < smem->num_regions; i++) {
478 area = &smem->regions[i];
480 if (area->aux_base == aux_base || !aux_base) {
482 *size = le32_to_cpu(entry->size);
483 return area->virt_base + le32_to_cpu(entry->offset);
487 return ERR_PTR(-ENOENT);
490 static void *qcom_smem_get_private(struct qcom_smem *smem,
491 struct smem_partition_header *phdr,
496 struct smem_private_entry *e, *end;
498 e = phdr_to_first_uncached_entry(phdr);
499 end = phdr_to_last_uncached_entry(phdr);
502 if (e->canary != SMEM_PRIVATE_CANARY)
505 if (le16_to_cpu(e->item) == item) {
507 *size = le32_to_cpu(e->size) -
508 le16_to_cpu(e->padding_data);
510 return uncached_entry_to_item(e);
513 e = uncached_entry_next(e);
516 /* Item was not found in the uncached list, search the cached list */
518 e = phdr_to_first_cached_entry(phdr, cacheline);
519 end = phdr_to_last_cached_entry(phdr);
522 if (e->canary != SMEM_PRIVATE_CANARY)
525 if (le16_to_cpu(e->item) == item) {
527 *size = le32_to_cpu(e->size) -
528 le16_to_cpu(e->padding_data);
530 return cached_entry_to_item(e);
533 e = cached_entry_next(e, cacheline);
536 return ERR_PTR(-ENOENT);
539 dev_err(smem->dev, "Found invalid canary in hosts %d:%d partition\n",
540 phdr->host0, phdr->host1);
542 return ERR_PTR(-EINVAL);
546 * qcom_smem_get() - resolve ptr of size of a smem item
547 * @host: the remote processor, or -1
548 * @item: smem item handle
549 * @size: pointer to be filled out with size of the item
551 * Looks up smem item and returns pointer to it. Size of smem
552 * item is returned in @size.
554 static void *qcom_smem_get(unsigned int host, unsigned int item, size_t *size)
556 struct smem_partition_header *phdr;
558 void *ptr = ERR_PTR(-EPROBE_DEFER);
563 if (WARN_ON(item >= __smem->item_count))
564 return ERR_PTR(-EINVAL);
566 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
567 phdr = __smem->partitions[host];
568 cacheln = __smem->cacheline[host];
569 ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
570 } else if (__smem->global_partition) {
571 phdr = __smem->global_partition;
572 cacheln = __smem->global_cacheline;
573 ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
575 ptr = qcom_smem_get_global(__smem, item, size);
583 * qcom_smem_get_free_space() - retrieve amount of free space in a partition
584 * @host: the remote processor identifying a partition, or -1
586 * To be used by smem clients as a quick way to determine if any new
587 * allocations has been made.
589 static int qcom_smem_get_free_space(unsigned int host)
591 struct smem_partition_header *phdr;
592 struct smem_header *header;
596 return -EPROBE_DEFER;
598 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
599 phdr = __smem->partitions[host];
600 ret = le32_to_cpu(phdr->offset_free_cached) -
601 le32_to_cpu(phdr->offset_free_uncached);
602 } else if (__smem->global_partition) {
603 phdr = __smem->global_partition;
604 ret = le32_to_cpu(phdr->offset_free_cached) -
605 le32_to_cpu(phdr->offset_free_uncached);
607 header = __smem->regions[0].virt_base;
608 ret = le32_to_cpu(header->available);
614 static int qcom_smem_get_sbl_version(struct qcom_smem *smem)
616 struct smem_header *header;
619 header = smem->regions[0].virt_base;
620 versions = header->version;
622 return le32_to_cpu(versions[SMEM_MASTER_SBL_VERSION_INDEX]);
625 static struct smem_ptable *qcom_smem_get_ptable(struct qcom_smem *smem)
627 struct smem_ptable *ptable;
630 ptable = smem->regions[0].virt_base + smem->regions[0].size - SZ_4K;
631 if (memcmp(ptable->magic, SMEM_PTABLE_MAGIC, sizeof(ptable->magic)))
632 return ERR_PTR(-ENOENT);
634 version = le32_to_cpu(ptable->version);
637 "Unsupported partition header version %d\n", version);
638 return ERR_PTR(-EINVAL);
643 static u32 qcom_smem_get_item_count(struct qcom_smem *smem)
645 struct smem_ptable *ptable;
646 struct smem_info *info;
648 ptable = qcom_smem_get_ptable(smem);
649 if (IS_ERR_OR_NULL(ptable))
650 return SMEM_ITEM_COUNT;
652 info = (struct smem_info *)&ptable->entry[ptable->num_entries];
653 if (memcmp(info->magic, SMEM_INFO_MAGIC, sizeof(info->magic)))
654 return SMEM_ITEM_COUNT;
656 return le16_to_cpu(info->num_items);
659 static int qcom_smem_set_global_partition(struct qcom_smem *smem)
661 struct smem_partition_header *header;
662 struct smem_ptable_entry *entry = NULL;
663 struct smem_ptable *ptable;
664 u32 host0, host1, size;
667 ptable = qcom_smem_get_ptable(smem);
669 return PTR_ERR(ptable);
671 for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
672 entry = &ptable->entry[i];
673 host0 = le16_to_cpu(entry->host0);
674 host1 = le16_to_cpu(entry->host1);
676 if (host0 == SMEM_GLOBAL_HOST && host0 == host1)
681 dev_err(smem->dev, "Missing entry for global partition\n");
685 if (!le32_to_cpu(entry->offset) || !le32_to_cpu(entry->size)) {
686 dev_err(smem->dev, "Invalid entry for global partition\n");
690 if (smem->global_partition) {
691 dev_err(smem->dev, "Already found the global partition\n");
695 header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
696 host0 = le16_to_cpu(header->host0);
697 host1 = le16_to_cpu(header->host1);
699 if (memcmp(header->magic, SMEM_PART_MAGIC, sizeof(header->magic))) {
700 dev_err(smem->dev, "Global partition has invalid magic\n");
704 if (host0 != SMEM_GLOBAL_HOST && host1 != SMEM_GLOBAL_HOST) {
705 dev_err(smem->dev, "Global partition hosts are invalid\n");
709 if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
710 dev_err(smem->dev, "Global partition has invalid size\n");
714 size = le32_to_cpu(header->offset_free_uncached);
715 if (size > le32_to_cpu(header->size)) {
717 "Global partition has invalid free pointer\n");
721 smem->global_partition = header;
722 smem->global_cacheline = le32_to_cpu(entry->cacheline);
727 static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
728 unsigned int local_host)
730 struct smem_partition_header *header;
731 struct smem_ptable_entry *entry;
732 struct smem_ptable *ptable;
733 unsigned int remote_host;
737 ptable = qcom_smem_get_ptable(smem);
739 return PTR_ERR(ptable);
741 for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
742 entry = &ptable->entry[i];
743 host0 = le16_to_cpu(entry->host0);
744 host1 = le16_to_cpu(entry->host1);
746 if (host0 != local_host && host1 != local_host)
749 if (!le32_to_cpu(entry->offset))
752 if (!le32_to_cpu(entry->size))
755 if (host0 == local_host)
760 if (remote_host >= SMEM_HOST_COUNT) {
762 "Invalid remote host %d\n",
767 if (smem->partitions[remote_host]) {
769 "Already found a partition for host %d\n",
774 header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
775 host0 = le16_to_cpu(header->host0);
776 host1 = le16_to_cpu(header->host1);
778 if (memcmp(header->magic, SMEM_PART_MAGIC,
779 sizeof(header->magic))) {
781 "Partition %d has invalid magic\n", i);
785 if (host0 != local_host && host1 != local_host) {
787 "Partition %d hosts are invalid\n", i);
791 if (host0 != remote_host && host1 != remote_host) {
793 "Partition %d hosts are invalid\n", i);
797 if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
799 "Partition %d has invalid size\n", i);
803 if (le32_to_cpu(header->offset_free_uncached) > le32_to_cpu(header->size)) {
805 "Partition %d has invalid free pointer\n", i);
809 smem->partitions[remote_host] = header;
810 smem->cacheline[remote_host] = le32_to_cpu(entry->cacheline);
816 static int qcom_smem_map_memory(struct qcom_smem *smem, struct udevice *dev,
817 const char *name, int i)
819 struct fdt_resource r;
821 int node = dev_of_offset(dev);
823 ret = fdtdec_lookup_phandle(gd->fdt_blob, node, name);
825 dev_err(dev, "No %s specified\n", name);
829 ret = fdt_get_resource(gd->fdt_blob, ret, "reg", 0, &r);
833 smem->regions[i].aux_base = (u32)r.start;
834 smem->regions[i].size = fdt_resource_size(&r);
835 smem->regions[i].virt_base = devm_ioremap(dev, r.start, fdt_resource_size(&r));
836 if (!smem->regions[i].virt_base)
842 static int qcom_smem_probe(struct udevice *dev)
844 struct smem_header *header;
845 struct qcom_smem *smem;
850 int node = dev_of_offset(dev);
853 if (fdtdec_lookup_phandle(gd->fdt_blob, node, "qcomrpm-msg-ram") >= 0)
856 array_size = num_regions * sizeof(struct smem_region);
857 smem = devm_kzalloc(dev, sizeof(*smem) + array_size, GFP_KERNEL);
862 smem->num_regions = num_regions;
864 ret = qcom_smem_map_memory(smem, dev, "memory-region", 0);
868 if (num_regions > 1) {
869 ret = qcom_smem_map_memory(smem, dev,
870 "qcom,rpm-msg-ram", 1);
875 header = smem->regions[0].virt_base;
876 if (le32_to_cpu(header->initialized) != 1 ||
877 le32_to_cpu(header->reserved)) {
878 dev_err(&pdev->dev, "SMEM is not initialized by SBL\n");
882 version = qcom_smem_get_sbl_version(smem);
883 switch (version >> 16) {
884 case SMEM_GLOBAL_PART_VERSION:
885 ret = qcom_smem_set_global_partition(smem);
888 smem->item_count = qcom_smem_get_item_count(smem);
890 case SMEM_GLOBAL_HEAP_VERSION:
891 smem->item_count = SMEM_ITEM_COUNT;
894 dev_err(dev, "Unsupported SMEM version 0x%x\n", version);
898 ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
899 if (ret < 0 && ret != -ENOENT)
907 static int qcom_smem_remove(struct udevice *dev)
914 const struct udevice_id qcom_smem_of_match[] = {
915 { .compatible = "qcom,smem" },
919 static const struct smem_ops msm_smem_ops = {
920 .alloc = qcom_smem_alloc,
921 .get = qcom_smem_get,
922 .get_free_space = qcom_smem_get_free_space,
925 U_BOOT_DRIVER(qcom_smem) = {
928 .of_match = qcom_smem_of_match,
929 .ops = &msm_smem_ops,
930 .probe = qcom_smem_probe,
931 .remove = qcom_smem_remove,