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/device_compat.h>
12 #include <dm/devres.h>
13 #include <dm/of_access.h>
14 #include <dm/of_addr.h>
16 #include <linux/bug.h>
17 #include <linux/err.h>
18 #include <linux/ioport.h>
22 DECLARE_GLOBAL_DATA_PTR;
25 * The Qualcomm shared memory system is an allocate-only heap structure that
26 * consists of one of more memory areas that can be accessed by the processors
29 * All systems contains a global heap, accessible by all processors in the SoC,
30 * with a table of contents data structure (@smem_header) at the beginning of
31 * the main shared memory block.
33 * The global header contains meta data for allocations as well as a fixed list
34 * of 512 entries (@smem_global_entry) that can be initialized to reference
35 * parts of the shared memory space.
38 * In addition to this global heap, a set of "private" heaps can be set up at
39 * boot time with access restrictions so that only certain processor pairs can
42 * These partitions are referenced from an optional partition table
43 * (@smem_ptable), that is found 4kB from the end of the main smem region. The
44 * partition table entries (@smem_ptable_entry) lists the involved processors
45 * (or hosts) and their location in the main shared memory region.
47 * Each partition starts with a header (@smem_partition_header) that identifies
48 * the partition and holds properties for the two internal memory regions. The
49 * two regions are cached and non-cached memory respectively. Each region
50 * contain a link list of allocation headers (@smem_private_entry) followed by
53 * Items in the non-cached region are allocated from the start of the partition
54 * while items in the cached region are allocated from the end. The free area
55 * is hence the region between the cached and non-cached offsets. The header of
56 * cached items comes after the data.
58 * Version 12 (SMEM_GLOBAL_PART_VERSION) changes the item alloc/get procedure
59 * for the global heap. A new global partition is created from the global heap
60 * region with partition type (SMEM_GLOBAL_HOST) and the max smem item count is
61 * set by the bootloader.
66 * The version member of the smem header contains an array of versions for the
67 * various software components in the SoC. We verify that the boot loader
68 * version is a valid version as a sanity check.
70 #define SMEM_MASTER_SBL_VERSION_INDEX 7
71 #define SMEM_GLOBAL_HEAP_VERSION 11
72 #define SMEM_GLOBAL_PART_VERSION 12
75 * The first 8 items are only to be allocated by the boot loader while
76 * initializing the heap.
78 #define SMEM_ITEM_LAST_FIXED 8
80 /* Highest accepted item number, for both global and private heaps */
81 #define SMEM_ITEM_COUNT 512
83 /* Processor/host identifier for the application processor */
84 #define SMEM_HOST_APPS 0
86 /* Processor/host identifier for the global partition */
87 #define SMEM_GLOBAL_HOST 0xfffe
89 /* Max number of processors/hosts in a system */
90 #define SMEM_HOST_COUNT 10
93 * struct smem_proc_comm - proc_comm communication struct (legacy)
94 * @command: current command to be executed
95 * @status: status of the currently requested command
96 * @params: parameters to the command
98 struct smem_proc_comm {
105 * struct smem_global_entry - entry to reference smem items on the heap
106 * @allocated: boolean to indicate if this entry is used
107 * @offset: offset to the allocated space
108 * @size: size of the allocated space, 8 byte aligned
109 * @aux_base: base address for the memory region used by this unit, or 0 for
110 * the default region. bits 0,1 are reserved
112 struct smem_global_entry {
116 __le32 aux_base; /* bits 1:0 reserved */
118 #define AUX_BASE_MASK 0xfffffffc
121 * struct smem_header - header found in beginning of primary smem region
122 * @proc_comm: proc_comm communication interface (legacy)
123 * @version: array of versions for the various subsystems
124 * @initialized: boolean to indicate that smem is initialized
125 * @free_offset: index of the first unallocated byte in smem
126 * @available: number of bytes available for allocation
127 * @reserved: reserved field, must be 0
128 * toc: array of references to items
131 struct smem_proc_comm proc_comm[4];
137 struct smem_global_entry toc[SMEM_ITEM_COUNT];
141 * struct smem_ptable_entry - one entry in the @smem_ptable list
142 * @offset: offset, within the main shared memory region, of the partition
143 * @size: size of the partition
144 * @flags: flags for the partition (currently unused)
145 * @host0: first processor/host with access to this partition
146 * @host1: second processor/host with access to this partition
147 * @cacheline: alignment for "cached" entries
148 * @reserved: reserved entries for later use
150 struct smem_ptable_entry {
161 * struct smem_ptable - partition table for the private partitions
162 * @magic: magic number, must be SMEM_PTABLE_MAGIC
163 * @version: version of the partition table
164 * @num_entries: number of partitions in the table
165 * @reserved: for now reserved entries
166 * @entry: list of @smem_ptable_entry for the @num_entries partitions
173 struct smem_ptable_entry entry[];
176 static const u8 SMEM_PTABLE_MAGIC[] = { 0x24, 0x54, 0x4f, 0x43 }; /* "$TOC" */
179 * struct smem_partition_header - header of the partitions
180 * @magic: magic number, must be SMEM_PART_MAGIC
181 * @host0: first processor/host with access to this partition
182 * @host1: second processor/host with access to this partition
183 * @size: size of the partition
184 * @offset_free_uncached: offset to the first free byte of uncached memory in
186 * @offset_free_cached: offset to the first free byte of cached memory in this
188 * @reserved: for now reserved entries
190 struct smem_partition_header {
195 __le32 offset_free_uncached;
196 __le32 offset_free_cached;
200 static const u8 SMEM_PART_MAGIC[] = { 0x24, 0x50, 0x52, 0x54 };
203 * struct smem_private_entry - header of each item in the private partition
204 * @canary: magic number, must be SMEM_PRIVATE_CANARY
205 * @item: identifying number of the smem item
206 * @size: size of the data, including padding bytes
207 * @padding_data: number of bytes of padding of data
208 * @padding_hdr: number of bytes of padding between the header and the data
209 * @reserved: for now reserved entry
211 struct smem_private_entry {
212 u16 canary; /* bytes are the same so no swapping needed */
214 __le32 size; /* includes padding bytes */
219 #define SMEM_PRIVATE_CANARY 0xa5a5
222 * struct smem_info - smem region info located after the table of contents
223 * @magic: magic number, must be SMEM_INFO_MAGIC
224 * @size: size of the smem region
225 * @base_addr: base address of the smem region
226 * @reserved: for now reserved entry
227 * @num_items: highest accepted item number
237 static const u8 SMEM_INFO_MAGIC[] = { 0x53, 0x49, 0x49, 0x49 }; /* SIII */
240 * struct smem_region - representation of a chunk of memory used for smem
241 * @aux_base: identifier of aux_mem base
242 * @virt_base: virtual base address of memory with this aux_mem identifier
243 * @size: size of the memory region
247 void __iomem *virt_base;
252 * struct qcom_smem - device data for the smem device
253 * @dev: device pointer
254 * @global_partition: pointer to global partition when in use
255 * @global_cacheline: cacheline size for global partition
256 * @partitions: list of pointers to partitions affecting the current
258 * @cacheline: list of cacheline sizes for each host
259 * @item_count: max accepted item number
260 * @num_regions: number of @regions
261 * @regions: list of the memory regions defining the shared memory
266 struct smem_partition_header *global_partition;
267 size_t global_cacheline;
268 struct smem_partition_header *partitions[SMEM_HOST_COUNT];
269 size_t cacheline[SMEM_HOST_COUNT];
272 unsigned int num_regions;
273 struct smem_region regions[0];
276 static struct smem_private_entry *
277 phdr_to_last_uncached_entry(struct smem_partition_header *phdr)
281 return p + le32_to_cpu(phdr->offset_free_uncached);
284 static void *phdr_to_first_cached_entry(struct smem_partition_header *phdr,
289 return p + le32_to_cpu(phdr->size) - ALIGN(sizeof(*phdr), cacheline);
292 static void *phdr_to_last_cached_entry(struct smem_partition_header *phdr)
296 return p + le32_to_cpu(phdr->offset_free_cached);
299 static struct smem_private_entry *
300 phdr_to_first_uncached_entry(struct smem_partition_header *phdr)
304 return p + sizeof(*phdr);
307 static struct smem_private_entry *
308 uncached_entry_next(struct smem_private_entry *e)
312 return p + sizeof(*e) + le16_to_cpu(e->padding_hdr) +
313 le32_to_cpu(e->size);
316 static struct smem_private_entry *
317 cached_entry_next(struct smem_private_entry *e, size_t cacheline)
321 return p - le32_to_cpu(e->size) - ALIGN(sizeof(*e), cacheline);
324 static void *uncached_entry_to_item(struct smem_private_entry *e)
328 return p + sizeof(*e) + le16_to_cpu(e->padding_hdr);
331 static void *cached_entry_to_item(struct smem_private_entry *e)
335 return p - le32_to_cpu(e->size);
338 /* Pointer to the one and only smem handle */
339 static struct qcom_smem *__smem;
341 static int qcom_smem_alloc_private(struct qcom_smem *smem,
342 struct smem_partition_header *phdr,
346 struct smem_private_entry *hdr, *end;
350 hdr = phdr_to_first_uncached_entry(phdr);
351 end = phdr_to_last_uncached_entry(phdr);
352 cached = phdr_to_last_cached_entry(phdr);
355 if (hdr->canary != SMEM_PRIVATE_CANARY) {
357 "Found invalid canary in hosts %d:%d partition\n",
358 phdr->host0, phdr->host1);
362 if (le16_to_cpu(hdr->item) == item)
365 hdr = uncached_entry_next(hdr);
368 /* Check that we don't grow into the cached region */
369 alloc_size = sizeof(*hdr) + ALIGN(size, 8);
370 if ((void *)hdr + alloc_size >= cached) {
371 dev_err(smem->dev, "Out of memory\n");
375 hdr->canary = SMEM_PRIVATE_CANARY;
376 hdr->item = cpu_to_le16(item);
377 hdr->size = cpu_to_le32(ALIGN(size, 8));
378 hdr->padding_data = cpu_to_le16(le32_to_cpu(hdr->size) - size);
379 hdr->padding_hdr = 0;
382 * Ensure the header is written before we advance the free offset, so
383 * that remote processors that does not take the remote spinlock still
384 * gets a consistent view of the linked list.
387 le32_add_cpu(&phdr->offset_free_uncached, alloc_size);
392 static int qcom_smem_alloc_global(struct qcom_smem *smem,
396 struct smem_global_entry *entry;
397 struct smem_header *header;
399 header = smem->regions[0].virt_base;
400 entry = &header->toc[item];
401 if (entry->allocated)
404 size = ALIGN(size, 8);
405 if (WARN_ON(size > le32_to_cpu(header->available)))
408 entry->offset = header->free_offset;
409 entry->size = cpu_to_le32(size);
412 * Ensure the header is consistent before we mark the item allocated,
413 * so that remote processors will get a consistent view of the item
414 * even though they do not take the spinlock on read.
417 entry->allocated = cpu_to_le32(1);
419 le32_add_cpu(&header->free_offset, size);
420 le32_add_cpu(&header->available, -size);
426 * qcom_smem_alloc() - allocate space for a smem item
427 * @host: remote processor id, or -1
428 * @item: smem item handle
429 * @size: number of bytes to be allocated
431 * Allocate space for a given smem item of size @size, given that the item is
434 static int qcom_smem_alloc(unsigned int host, unsigned int item, size_t size)
436 struct smem_partition_header *phdr;
440 return -EPROBE_DEFER;
442 if (item < SMEM_ITEM_LAST_FIXED) {
444 "Rejecting allocation of static entry %d\n", item);
448 if (WARN_ON(item >= __smem->item_count))
451 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
452 phdr = __smem->partitions[host];
453 ret = qcom_smem_alloc_private(__smem, phdr, item, size);
454 } else if (__smem->global_partition) {
455 phdr = __smem->global_partition;
456 ret = qcom_smem_alloc_private(__smem, phdr, item, size);
458 ret = qcom_smem_alloc_global(__smem, item, size);
464 static void *qcom_smem_get_global(struct qcom_smem *smem,
468 struct smem_header *header;
469 struct smem_region *area;
470 struct smem_global_entry *entry;
474 header = smem->regions[0].virt_base;
475 entry = &header->toc[item];
476 if (!entry->allocated)
477 return ERR_PTR(-ENXIO);
479 aux_base = le32_to_cpu(entry->aux_base) & AUX_BASE_MASK;
481 for (i = 0; i < smem->num_regions; i++) {
482 area = &smem->regions[i];
484 if (area->aux_base == aux_base || !aux_base) {
486 *size = le32_to_cpu(entry->size);
487 return area->virt_base + le32_to_cpu(entry->offset);
491 return ERR_PTR(-ENOENT);
494 static void *qcom_smem_get_private(struct qcom_smem *smem,
495 struct smem_partition_header *phdr,
500 struct smem_private_entry *e, *end;
502 e = phdr_to_first_uncached_entry(phdr);
503 end = phdr_to_last_uncached_entry(phdr);
506 if (e->canary != SMEM_PRIVATE_CANARY)
509 if (le16_to_cpu(e->item) == item) {
511 *size = le32_to_cpu(e->size) -
512 le16_to_cpu(e->padding_data);
514 return uncached_entry_to_item(e);
517 e = uncached_entry_next(e);
520 /* Item was not found in the uncached list, search the cached list */
522 e = phdr_to_first_cached_entry(phdr, cacheline);
523 end = phdr_to_last_cached_entry(phdr);
526 if (e->canary != SMEM_PRIVATE_CANARY)
529 if (le16_to_cpu(e->item) == item) {
531 *size = le32_to_cpu(e->size) -
532 le16_to_cpu(e->padding_data);
534 return cached_entry_to_item(e);
537 e = cached_entry_next(e, cacheline);
540 return ERR_PTR(-ENOENT);
543 dev_err(smem->dev, "Found invalid canary in hosts %d:%d partition\n",
544 phdr->host0, phdr->host1);
546 return ERR_PTR(-EINVAL);
550 * qcom_smem_get() - resolve ptr of size of a smem item
551 * @host: the remote processor, or -1
552 * @item: smem item handle
553 * @size: pointer to be filled out with size of the item
555 * Looks up smem item and returns pointer to it. Size of smem
556 * item is returned in @size.
558 static void *qcom_smem_get(unsigned int host, unsigned int item, size_t *size)
560 struct smem_partition_header *phdr;
562 void *ptr = ERR_PTR(-EPROBE_DEFER);
567 if (WARN_ON(item >= __smem->item_count))
568 return ERR_PTR(-EINVAL);
570 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
571 phdr = __smem->partitions[host];
572 cacheln = __smem->cacheline[host];
573 ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
574 } else if (__smem->global_partition) {
575 phdr = __smem->global_partition;
576 cacheln = __smem->global_cacheline;
577 ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
579 ptr = qcom_smem_get_global(__smem, item, size);
587 * qcom_smem_get_free_space() - retrieve amount of free space in a partition
588 * @host: the remote processor identifying a partition, or -1
590 * To be used by smem clients as a quick way to determine if any new
591 * allocations has been made.
593 static int qcom_smem_get_free_space(unsigned int host)
595 struct smem_partition_header *phdr;
596 struct smem_header *header;
600 return -EPROBE_DEFER;
602 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
603 phdr = __smem->partitions[host];
604 ret = le32_to_cpu(phdr->offset_free_cached) -
605 le32_to_cpu(phdr->offset_free_uncached);
606 } else if (__smem->global_partition) {
607 phdr = __smem->global_partition;
608 ret = le32_to_cpu(phdr->offset_free_cached) -
609 le32_to_cpu(phdr->offset_free_uncached);
611 header = __smem->regions[0].virt_base;
612 ret = le32_to_cpu(header->available);
618 static int qcom_smem_get_sbl_version(struct qcom_smem *smem)
620 struct smem_header *header;
623 header = smem->regions[0].virt_base;
624 versions = header->version;
626 return le32_to_cpu(versions[SMEM_MASTER_SBL_VERSION_INDEX]);
629 static struct smem_ptable *qcom_smem_get_ptable(struct qcom_smem *smem)
631 struct smem_ptable *ptable;
634 ptable = smem->regions[0].virt_base + smem->regions[0].size - SZ_4K;
635 if (memcmp(ptable->magic, SMEM_PTABLE_MAGIC, sizeof(ptable->magic)))
636 return ERR_PTR(-ENOENT);
638 version = le32_to_cpu(ptable->version);
641 "Unsupported partition header version %d\n", version);
642 return ERR_PTR(-EINVAL);
647 static u32 qcom_smem_get_item_count(struct qcom_smem *smem)
649 struct smem_ptable *ptable;
650 struct smem_info *info;
652 ptable = qcom_smem_get_ptable(smem);
653 if (IS_ERR_OR_NULL(ptable))
654 return SMEM_ITEM_COUNT;
656 info = (struct smem_info *)&ptable->entry[ptable->num_entries];
657 if (memcmp(info->magic, SMEM_INFO_MAGIC, sizeof(info->magic)))
658 return SMEM_ITEM_COUNT;
660 return le16_to_cpu(info->num_items);
663 static int qcom_smem_set_global_partition(struct qcom_smem *smem)
665 struct smem_partition_header *header;
666 struct smem_ptable_entry *entry = NULL;
667 struct smem_ptable *ptable;
668 u32 host0, host1, size;
671 ptable = qcom_smem_get_ptable(smem);
673 return PTR_ERR(ptable);
675 for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
676 entry = &ptable->entry[i];
677 host0 = le16_to_cpu(entry->host0);
678 host1 = le16_to_cpu(entry->host1);
680 if (host0 == SMEM_GLOBAL_HOST && host0 == host1)
685 dev_err(smem->dev, "Missing entry for global partition\n");
689 if (!le32_to_cpu(entry->offset) || !le32_to_cpu(entry->size)) {
690 dev_err(smem->dev, "Invalid entry for global partition\n");
694 if (smem->global_partition) {
695 dev_err(smem->dev, "Already found the global partition\n");
699 header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
700 host0 = le16_to_cpu(header->host0);
701 host1 = le16_to_cpu(header->host1);
703 if (memcmp(header->magic, SMEM_PART_MAGIC, sizeof(header->magic))) {
704 dev_err(smem->dev, "Global partition has invalid magic\n");
708 if (host0 != SMEM_GLOBAL_HOST && host1 != SMEM_GLOBAL_HOST) {
709 dev_err(smem->dev, "Global partition hosts are invalid\n");
713 if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
714 dev_err(smem->dev, "Global partition has invalid size\n");
718 size = le32_to_cpu(header->offset_free_uncached);
719 if (size > le32_to_cpu(header->size)) {
721 "Global partition has invalid free pointer\n");
725 smem->global_partition = header;
726 smem->global_cacheline = le32_to_cpu(entry->cacheline);
731 static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
732 unsigned int local_host)
734 struct smem_partition_header *header;
735 struct smem_ptable_entry *entry;
736 struct smem_ptable *ptable;
737 unsigned int remote_host;
741 ptable = qcom_smem_get_ptable(smem);
743 return PTR_ERR(ptable);
745 for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
746 entry = &ptable->entry[i];
747 host0 = le16_to_cpu(entry->host0);
748 host1 = le16_to_cpu(entry->host1);
750 if (host0 != local_host && host1 != local_host)
753 if (!le32_to_cpu(entry->offset))
756 if (!le32_to_cpu(entry->size))
759 if (host0 == local_host)
764 if (remote_host >= SMEM_HOST_COUNT) {
766 "Invalid remote host %d\n",
771 if (smem->partitions[remote_host]) {
773 "Already found a partition for host %d\n",
778 header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
779 host0 = le16_to_cpu(header->host0);
780 host1 = le16_to_cpu(header->host1);
782 if (memcmp(header->magic, SMEM_PART_MAGIC,
783 sizeof(header->magic))) {
785 "Partition %d has invalid magic\n", i);
789 if (host0 != local_host && host1 != local_host) {
791 "Partition %d hosts are invalid\n", i);
795 if (host0 != remote_host && host1 != remote_host) {
797 "Partition %d hosts are invalid\n", i);
801 if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
803 "Partition %d has invalid size\n", i);
807 if (le32_to_cpu(header->offset_free_uncached) > le32_to_cpu(header->size)) {
809 "Partition %d has invalid free pointer\n", i);
813 smem->partitions[remote_host] = header;
814 smem->cacheline[remote_host] = le32_to_cpu(entry->cacheline);
820 static int qcom_smem_map_memory(struct qcom_smem *smem, struct udevice *dev,
821 const char *name, int i)
823 struct fdt_resource r;
825 int node = dev_of_offset(dev);
827 ret = fdtdec_lookup_phandle(gd->fdt_blob, node, name);
829 dev_err(dev, "No %s specified\n", name);
833 ret = fdt_get_resource(gd->fdt_blob, ret, "reg", 0, &r);
837 smem->regions[i].aux_base = (u32)r.start;
838 smem->regions[i].size = fdt_resource_size(&r);
839 smem->regions[i].virt_base = devm_ioremap(dev, r.start, fdt_resource_size(&r));
840 if (!smem->regions[i].virt_base)
846 static int qcom_smem_probe(struct udevice *dev)
848 struct smem_header *header;
849 struct qcom_smem *smem;
854 int node = dev_of_offset(dev);
857 if (fdtdec_lookup_phandle(gd->fdt_blob, node, "qcomrpm-msg-ram") >= 0)
860 array_size = num_regions * sizeof(struct smem_region);
861 smem = devm_kzalloc(dev, sizeof(*smem) + array_size, GFP_KERNEL);
866 smem->num_regions = num_regions;
868 ret = qcom_smem_map_memory(smem, dev, "memory-region", 0);
872 if (num_regions > 1) {
873 ret = qcom_smem_map_memory(smem, dev,
874 "qcom,rpm-msg-ram", 1);
879 header = smem->regions[0].virt_base;
880 if (le32_to_cpu(header->initialized) != 1 ||
881 le32_to_cpu(header->reserved)) {
882 dev_err(&pdev->dev, "SMEM is not initialized by SBL\n");
886 version = qcom_smem_get_sbl_version(smem);
887 switch (version >> 16) {
888 case SMEM_GLOBAL_PART_VERSION:
889 ret = qcom_smem_set_global_partition(smem);
892 smem->item_count = qcom_smem_get_item_count(smem);
894 case SMEM_GLOBAL_HEAP_VERSION:
895 smem->item_count = SMEM_ITEM_COUNT;
898 dev_err(dev, "Unsupported SMEM version 0x%x\n", version);
902 ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
903 if (ret < 0 && ret != -ENOENT)
911 static int qcom_smem_remove(struct udevice *dev)
918 const struct udevice_id qcom_smem_of_match[] = {
919 { .compatible = "qcom,smem" },
923 static const struct smem_ops msm_smem_ops = {
924 .alloc = qcom_smem_alloc,
925 .get = qcom_smem_get,
926 .get_free_space = qcom_smem_get_free_space,
929 U_BOOT_DRIVER(qcom_smem) = {
932 .of_match = qcom_smem_of_match,
933 .ops = &msm_smem_ops,
934 .probe = qcom_smem_probe,
935 .remove = qcom_smem_remove,