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/err.h>
17 #include <linux/ioport.h>
21 DECLARE_GLOBAL_DATA_PTR;
24 * The Qualcomm shared memory system is an allocate-only heap structure that
25 * consists of one of more memory areas that can be accessed by the processors
28 * All systems contains a global heap, accessible by all processors in the SoC,
29 * with a table of contents data structure (@smem_header) at the beginning of
30 * the main shared memory block.
32 * The global header contains meta data for allocations as well as a fixed list
33 * of 512 entries (@smem_global_entry) that can be initialized to reference
34 * parts of the shared memory space.
37 * In addition to this global heap, a set of "private" heaps can be set up at
38 * boot time with access restrictions so that only certain processor pairs can
41 * These partitions are referenced from an optional partition table
42 * (@smem_ptable), that is found 4kB from the end of the main smem region. The
43 * partition table entries (@smem_ptable_entry) lists the involved processors
44 * (or hosts) and their location in the main shared memory region.
46 * Each partition starts with a header (@smem_partition_header) that identifies
47 * the partition and holds properties for the two internal memory regions. The
48 * two regions are cached and non-cached memory respectively. Each region
49 * contain a link list of allocation headers (@smem_private_entry) followed by
52 * Items in the non-cached region are allocated from the start of the partition
53 * while items in the cached region are allocated from the end. The free area
54 * is hence the region between the cached and non-cached offsets. The header of
55 * cached items comes after the data.
57 * Version 12 (SMEM_GLOBAL_PART_VERSION) changes the item alloc/get procedure
58 * for the global heap. A new global partition is created from the global heap
59 * region with partition type (SMEM_GLOBAL_HOST) and the max smem item count is
60 * set by the bootloader.
65 * The version member of the smem header contains an array of versions for the
66 * various software components in the SoC. We verify that the boot loader
67 * version is a valid version as a sanity check.
69 #define SMEM_MASTER_SBL_VERSION_INDEX 7
70 #define SMEM_GLOBAL_HEAP_VERSION 11
71 #define SMEM_GLOBAL_PART_VERSION 12
74 * The first 8 items are only to be allocated by the boot loader while
75 * initializing the heap.
77 #define SMEM_ITEM_LAST_FIXED 8
79 /* Highest accepted item number, for both global and private heaps */
80 #define SMEM_ITEM_COUNT 512
82 /* Processor/host identifier for the application processor */
83 #define SMEM_HOST_APPS 0
85 /* Processor/host identifier for the global partition */
86 #define SMEM_GLOBAL_HOST 0xfffe
88 /* Max number of processors/hosts in a system */
89 #define SMEM_HOST_COUNT 10
92 * struct smem_proc_comm - proc_comm communication struct (legacy)
93 * @command: current command to be executed
94 * @status: status of the currently requested command
95 * @params: parameters to the command
97 struct smem_proc_comm {
104 * struct smem_global_entry - entry to reference smem items on the heap
105 * @allocated: boolean to indicate if this entry is used
106 * @offset: offset to the allocated space
107 * @size: size of the allocated space, 8 byte aligned
108 * @aux_base: base address for the memory region used by this unit, or 0 for
109 * the default region. bits 0,1 are reserved
111 struct smem_global_entry {
115 __le32 aux_base; /* bits 1:0 reserved */
117 #define AUX_BASE_MASK 0xfffffffc
120 * struct smem_header - header found in beginning of primary smem region
121 * @proc_comm: proc_comm communication interface (legacy)
122 * @version: array of versions for the various subsystems
123 * @initialized: boolean to indicate that smem is initialized
124 * @free_offset: index of the first unallocated byte in smem
125 * @available: number of bytes available for allocation
126 * @reserved: reserved field, must be 0
127 * toc: array of references to items
130 struct smem_proc_comm proc_comm[4];
136 struct smem_global_entry toc[SMEM_ITEM_COUNT];
140 * struct smem_ptable_entry - one entry in the @smem_ptable list
141 * @offset: offset, within the main shared memory region, of the partition
142 * @size: size of the partition
143 * @flags: flags for the partition (currently unused)
144 * @host0: first processor/host with access to this partition
145 * @host1: second processor/host with access to this partition
146 * @cacheline: alignment for "cached" entries
147 * @reserved: reserved entries for later use
149 struct smem_ptable_entry {
160 * struct smem_ptable - partition table for the private partitions
161 * @magic: magic number, must be SMEM_PTABLE_MAGIC
162 * @version: version of the partition table
163 * @num_entries: number of partitions in the table
164 * @reserved: for now reserved entries
165 * @entry: list of @smem_ptable_entry for the @num_entries partitions
172 struct smem_ptable_entry entry[];
175 static const u8 SMEM_PTABLE_MAGIC[] = { 0x24, 0x54, 0x4f, 0x43 }; /* "$TOC" */
178 * struct smem_partition_header - header of the partitions
179 * @magic: magic number, must be SMEM_PART_MAGIC
180 * @host0: first processor/host with access to this partition
181 * @host1: second processor/host with access to this partition
182 * @size: size of the partition
183 * @offset_free_uncached: offset to the first free byte of uncached memory in
185 * @offset_free_cached: offset to the first free byte of cached memory in this
187 * @reserved: for now reserved entries
189 struct smem_partition_header {
194 __le32 offset_free_uncached;
195 __le32 offset_free_cached;
199 static const u8 SMEM_PART_MAGIC[] = { 0x24, 0x50, 0x52, 0x54 };
202 * struct smem_private_entry - header of each item in the private partition
203 * @canary: magic number, must be SMEM_PRIVATE_CANARY
204 * @item: identifying number of the smem item
205 * @size: size of the data, including padding bytes
206 * @padding_data: number of bytes of padding of data
207 * @padding_hdr: number of bytes of padding between the header and the data
208 * @reserved: for now reserved entry
210 struct smem_private_entry {
211 u16 canary; /* bytes are the same so no swapping needed */
213 __le32 size; /* includes padding bytes */
218 #define SMEM_PRIVATE_CANARY 0xa5a5
221 * struct smem_info - smem region info located after the table of contents
222 * @magic: magic number, must be SMEM_INFO_MAGIC
223 * @size: size of the smem region
224 * @base_addr: base address of the smem region
225 * @reserved: for now reserved entry
226 * @num_items: highest accepted item number
236 static const u8 SMEM_INFO_MAGIC[] = { 0x53, 0x49, 0x49, 0x49 }; /* SIII */
239 * struct smem_region - representation of a chunk of memory used for smem
240 * @aux_base: identifier of aux_mem base
241 * @virt_base: virtual base address of memory with this aux_mem identifier
242 * @size: size of the memory region
246 void __iomem *virt_base;
251 * struct qcom_smem - device data for the smem device
252 * @dev: device pointer
253 * @global_partition: pointer to global partition when in use
254 * @global_cacheline: cacheline size for global partition
255 * @partitions: list of pointers to partitions affecting the current
257 * @cacheline: list of cacheline sizes for each host
258 * @item_count: max accepted item number
259 * @num_regions: number of @regions
260 * @regions: list of the memory regions defining the shared memory
265 struct smem_partition_header *global_partition;
266 size_t global_cacheline;
267 struct smem_partition_header *partitions[SMEM_HOST_COUNT];
268 size_t cacheline[SMEM_HOST_COUNT];
271 unsigned int num_regions;
272 struct smem_region regions[0];
275 static struct smem_private_entry *
276 phdr_to_last_uncached_entry(struct smem_partition_header *phdr)
280 return p + le32_to_cpu(phdr->offset_free_uncached);
283 static void *phdr_to_first_cached_entry(struct smem_partition_header *phdr,
288 return p + le32_to_cpu(phdr->size) - ALIGN(sizeof(*phdr), cacheline);
291 static void *phdr_to_last_cached_entry(struct smem_partition_header *phdr)
295 return p + le32_to_cpu(phdr->offset_free_cached);
298 static struct smem_private_entry *
299 phdr_to_first_uncached_entry(struct smem_partition_header *phdr)
303 return p + sizeof(*phdr);
306 static struct smem_private_entry *
307 uncached_entry_next(struct smem_private_entry *e)
311 return p + sizeof(*e) + le16_to_cpu(e->padding_hdr) +
312 le32_to_cpu(e->size);
315 static struct smem_private_entry *
316 cached_entry_next(struct smem_private_entry *e, size_t cacheline)
320 return p - le32_to_cpu(e->size) - ALIGN(sizeof(*e), cacheline);
323 static void *uncached_entry_to_item(struct smem_private_entry *e)
327 return p + sizeof(*e) + le16_to_cpu(e->padding_hdr);
330 static void *cached_entry_to_item(struct smem_private_entry *e)
334 return p - le32_to_cpu(e->size);
337 /* Pointer to the one and only smem handle */
338 static struct qcom_smem *__smem;
340 static int qcom_smem_alloc_private(struct qcom_smem *smem,
341 struct smem_partition_header *phdr,
345 struct smem_private_entry *hdr, *end;
349 hdr = phdr_to_first_uncached_entry(phdr);
350 end = phdr_to_last_uncached_entry(phdr);
351 cached = phdr_to_last_cached_entry(phdr);
354 if (hdr->canary != SMEM_PRIVATE_CANARY) {
356 "Found invalid canary in hosts %d:%d partition\n",
357 phdr->host0, phdr->host1);
361 if (le16_to_cpu(hdr->item) == item)
364 hdr = uncached_entry_next(hdr);
367 /* Check that we don't grow into the cached region */
368 alloc_size = sizeof(*hdr) + ALIGN(size, 8);
369 if ((void *)hdr + alloc_size >= cached) {
370 dev_err(smem->dev, "Out of memory\n");
374 hdr->canary = SMEM_PRIVATE_CANARY;
375 hdr->item = cpu_to_le16(item);
376 hdr->size = cpu_to_le32(ALIGN(size, 8));
377 hdr->padding_data = cpu_to_le16(le32_to_cpu(hdr->size) - size);
378 hdr->padding_hdr = 0;
381 * Ensure the header is written before we advance the free offset, so
382 * that remote processors that does not take the remote spinlock still
383 * gets a consistent view of the linked list.
386 le32_add_cpu(&phdr->offset_free_uncached, alloc_size);
391 static int qcom_smem_alloc_global(struct qcom_smem *smem,
395 struct smem_global_entry *entry;
396 struct smem_header *header;
398 header = smem->regions[0].virt_base;
399 entry = &header->toc[item];
400 if (entry->allocated)
403 size = ALIGN(size, 8);
404 if (WARN_ON(size > le32_to_cpu(header->available)))
407 entry->offset = header->free_offset;
408 entry->size = cpu_to_le32(size);
411 * Ensure the header is consistent before we mark the item allocated,
412 * so that remote processors will get a consistent view of the item
413 * even though they do not take the spinlock on read.
416 entry->allocated = cpu_to_le32(1);
418 le32_add_cpu(&header->free_offset, size);
419 le32_add_cpu(&header->available, -size);
425 * qcom_smem_alloc() - allocate space for a smem item
426 * @host: remote processor id, or -1
427 * @item: smem item handle
428 * @size: number of bytes to be allocated
430 * Allocate space for a given smem item of size @size, given that the item is
433 static int qcom_smem_alloc(unsigned int host, unsigned int item, size_t size)
435 struct smem_partition_header *phdr;
439 return -EPROBE_DEFER;
441 if (item < SMEM_ITEM_LAST_FIXED) {
443 "Rejecting allocation of static entry %d\n", item);
447 if (WARN_ON(item >= __smem->item_count))
450 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
451 phdr = __smem->partitions[host];
452 ret = qcom_smem_alloc_private(__smem, phdr, item, size);
453 } else if (__smem->global_partition) {
454 phdr = __smem->global_partition;
455 ret = qcom_smem_alloc_private(__smem, phdr, item, size);
457 ret = qcom_smem_alloc_global(__smem, item, size);
463 static void *qcom_smem_get_global(struct qcom_smem *smem,
467 struct smem_header *header;
468 struct smem_region *area;
469 struct smem_global_entry *entry;
473 header = smem->regions[0].virt_base;
474 entry = &header->toc[item];
475 if (!entry->allocated)
476 return ERR_PTR(-ENXIO);
478 aux_base = le32_to_cpu(entry->aux_base) & AUX_BASE_MASK;
480 for (i = 0; i < smem->num_regions; i++) {
481 area = &smem->regions[i];
483 if (area->aux_base == aux_base || !aux_base) {
485 *size = le32_to_cpu(entry->size);
486 return area->virt_base + le32_to_cpu(entry->offset);
490 return ERR_PTR(-ENOENT);
493 static void *qcom_smem_get_private(struct qcom_smem *smem,
494 struct smem_partition_header *phdr,
499 struct smem_private_entry *e, *end;
501 e = phdr_to_first_uncached_entry(phdr);
502 end = phdr_to_last_uncached_entry(phdr);
505 if (e->canary != SMEM_PRIVATE_CANARY)
508 if (le16_to_cpu(e->item) == item) {
510 *size = le32_to_cpu(e->size) -
511 le16_to_cpu(e->padding_data);
513 return uncached_entry_to_item(e);
516 e = uncached_entry_next(e);
519 /* Item was not found in the uncached list, search the cached list */
521 e = phdr_to_first_cached_entry(phdr, cacheline);
522 end = phdr_to_last_cached_entry(phdr);
525 if (e->canary != SMEM_PRIVATE_CANARY)
528 if (le16_to_cpu(e->item) == item) {
530 *size = le32_to_cpu(e->size) -
531 le16_to_cpu(e->padding_data);
533 return cached_entry_to_item(e);
536 e = cached_entry_next(e, cacheline);
539 return ERR_PTR(-ENOENT);
542 dev_err(smem->dev, "Found invalid canary in hosts %d:%d partition\n",
543 phdr->host0, phdr->host1);
545 return ERR_PTR(-EINVAL);
549 * qcom_smem_get() - resolve ptr of size of a smem item
550 * @host: the remote processor, or -1
551 * @item: smem item handle
552 * @size: pointer to be filled out with size of the item
554 * Looks up smem item and returns pointer to it. Size of smem
555 * item is returned in @size.
557 static void *qcom_smem_get(unsigned int host, unsigned int item, size_t *size)
559 struct smem_partition_header *phdr;
561 void *ptr = ERR_PTR(-EPROBE_DEFER);
566 if (WARN_ON(item >= __smem->item_count))
567 return ERR_PTR(-EINVAL);
569 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
570 phdr = __smem->partitions[host];
571 cacheln = __smem->cacheline[host];
572 ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
573 } else if (__smem->global_partition) {
574 phdr = __smem->global_partition;
575 cacheln = __smem->global_cacheline;
576 ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
578 ptr = qcom_smem_get_global(__smem, item, size);
586 * qcom_smem_get_free_space() - retrieve amount of free space in a partition
587 * @host: the remote processor identifying a partition, or -1
589 * To be used by smem clients as a quick way to determine if any new
590 * allocations has been made.
592 static int qcom_smem_get_free_space(unsigned int host)
594 struct smem_partition_header *phdr;
595 struct smem_header *header;
599 return -EPROBE_DEFER;
601 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
602 phdr = __smem->partitions[host];
603 ret = le32_to_cpu(phdr->offset_free_cached) -
604 le32_to_cpu(phdr->offset_free_uncached);
605 } else if (__smem->global_partition) {
606 phdr = __smem->global_partition;
607 ret = le32_to_cpu(phdr->offset_free_cached) -
608 le32_to_cpu(phdr->offset_free_uncached);
610 header = __smem->regions[0].virt_base;
611 ret = le32_to_cpu(header->available);
617 static int qcom_smem_get_sbl_version(struct qcom_smem *smem)
619 struct smem_header *header;
622 header = smem->regions[0].virt_base;
623 versions = header->version;
625 return le32_to_cpu(versions[SMEM_MASTER_SBL_VERSION_INDEX]);
628 static struct smem_ptable *qcom_smem_get_ptable(struct qcom_smem *smem)
630 struct smem_ptable *ptable;
633 ptable = smem->regions[0].virt_base + smem->regions[0].size - SZ_4K;
634 if (memcmp(ptable->magic, SMEM_PTABLE_MAGIC, sizeof(ptable->magic)))
635 return ERR_PTR(-ENOENT);
637 version = le32_to_cpu(ptable->version);
640 "Unsupported partition header version %d\n", version);
641 return ERR_PTR(-EINVAL);
646 static u32 qcom_smem_get_item_count(struct qcom_smem *smem)
648 struct smem_ptable *ptable;
649 struct smem_info *info;
651 ptable = qcom_smem_get_ptable(smem);
652 if (IS_ERR_OR_NULL(ptable))
653 return SMEM_ITEM_COUNT;
655 info = (struct smem_info *)&ptable->entry[ptable->num_entries];
656 if (memcmp(info->magic, SMEM_INFO_MAGIC, sizeof(info->magic)))
657 return SMEM_ITEM_COUNT;
659 return le16_to_cpu(info->num_items);
662 static int qcom_smem_set_global_partition(struct qcom_smem *smem)
664 struct smem_partition_header *header;
665 struct smem_ptable_entry *entry = NULL;
666 struct smem_ptable *ptable;
667 u32 host0, host1, size;
670 ptable = qcom_smem_get_ptable(smem);
672 return PTR_ERR(ptable);
674 for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
675 entry = &ptable->entry[i];
676 host0 = le16_to_cpu(entry->host0);
677 host1 = le16_to_cpu(entry->host1);
679 if (host0 == SMEM_GLOBAL_HOST && host0 == host1)
684 dev_err(smem->dev, "Missing entry for global partition\n");
688 if (!le32_to_cpu(entry->offset) || !le32_to_cpu(entry->size)) {
689 dev_err(smem->dev, "Invalid entry for global partition\n");
693 if (smem->global_partition) {
694 dev_err(smem->dev, "Already found the global partition\n");
698 header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
699 host0 = le16_to_cpu(header->host0);
700 host1 = le16_to_cpu(header->host1);
702 if (memcmp(header->magic, SMEM_PART_MAGIC, sizeof(header->magic))) {
703 dev_err(smem->dev, "Global partition has invalid magic\n");
707 if (host0 != SMEM_GLOBAL_HOST && host1 != SMEM_GLOBAL_HOST) {
708 dev_err(smem->dev, "Global partition hosts are invalid\n");
712 if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
713 dev_err(smem->dev, "Global partition has invalid size\n");
717 size = le32_to_cpu(header->offset_free_uncached);
718 if (size > le32_to_cpu(header->size)) {
720 "Global partition has invalid free pointer\n");
724 smem->global_partition = header;
725 smem->global_cacheline = le32_to_cpu(entry->cacheline);
730 static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
731 unsigned int local_host)
733 struct smem_partition_header *header;
734 struct smem_ptable_entry *entry;
735 struct smem_ptable *ptable;
736 unsigned int remote_host;
740 ptable = qcom_smem_get_ptable(smem);
742 return PTR_ERR(ptable);
744 for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
745 entry = &ptable->entry[i];
746 host0 = le16_to_cpu(entry->host0);
747 host1 = le16_to_cpu(entry->host1);
749 if (host0 != local_host && host1 != local_host)
752 if (!le32_to_cpu(entry->offset))
755 if (!le32_to_cpu(entry->size))
758 if (host0 == local_host)
763 if (remote_host >= SMEM_HOST_COUNT) {
765 "Invalid remote host %d\n",
770 if (smem->partitions[remote_host]) {
772 "Already found a partition for host %d\n",
777 header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
778 host0 = le16_to_cpu(header->host0);
779 host1 = le16_to_cpu(header->host1);
781 if (memcmp(header->magic, SMEM_PART_MAGIC,
782 sizeof(header->magic))) {
784 "Partition %d has invalid magic\n", i);
788 if (host0 != local_host && host1 != local_host) {
790 "Partition %d hosts are invalid\n", i);
794 if (host0 != remote_host && host1 != remote_host) {
796 "Partition %d hosts are invalid\n", i);
800 if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
802 "Partition %d has invalid size\n", i);
806 if (le32_to_cpu(header->offset_free_uncached) > le32_to_cpu(header->size)) {
808 "Partition %d has invalid free pointer\n", i);
812 smem->partitions[remote_host] = header;
813 smem->cacheline[remote_host] = le32_to_cpu(entry->cacheline);
819 static int qcom_smem_map_memory(struct qcom_smem *smem, struct udevice *dev,
820 const char *name, int i)
822 struct fdt_resource r;
824 int node = dev_of_offset(dev);
826 ret = fdtdec_lookup_phandle(gd->fdt_blob, node, name);
828 dev_err(dev, "No %s specified\n", name);
832 ret = fdt_get_resource(gd->fdt_blob, ret, "reg", 0, &r);
836 smem->regions[i].aux_base = (u32)r.start;
837 smem->regions[i].size = fdt_resource_size(&r);
838 smem->regions[i].virt_base = devm_ioremap(dev, r.start, fdt_resource_size(&r));
839 if (!smem->regions[i].virt_base)
845 static int qcom_smem_probe(struct udevice *dev)
847 struct smem_header *header;
848 struct qcom_smem *smem;
853 int node = dev_of_offset(dev);
856 if (fdtdec_lookup_phandle(gd->fdt_blob, node, "qcomrpm-msg-ram") >= 0)
859 array_size = num_regions * sizeof(struct smem_region);
860 smem = devm_kzalloc(dev, sizeof(*smem) + array_size, GFP_KERNEL);
865 smem->num_regions = num_regions;
867 ret = qcom_smem_map_memory(smem, dev, "memory-region", 0);
871 if (num_regions > 1) {
872 ret = qcom_smem_map_memory(smem, dev,
873 "qcom,rpm-msg-ram", 1);
878 header = smem->regions[0].virt_base;
879 if (le32_to_cpu(header->initialized) != 1 ||
880 le32_to_cpu(header->reserved)) {
881 dev_err(&pdev->dev, "SMEM is not initialized by SBL\n");
885 version = qcom_smem_get_sbl_version(smem);
886 switch (version >> 16) {
887 case SMEM_GLOBAL_PART_VERSION:
888 ret = qcom_smem_set_global_partition(smem);
891 smem->item_count = qcom_smem_get_item_count(smem);
893 case SMEM_GLOBAL_HEAP_VERSION:
894 smem->item_count = SMEM_ITEM_COUNT;
897 dev_err(dev, "Unsupported SMEM version 0x%x\n", version);
901 ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
902 if (ret < 0 && ret != -ENOENT)
910 static int qcom_smem_remove(struct udevice *dev)
917 const struct udevice_id qcom_smem_of_match[] = {
918 { .compatible = "qcom,smem" },
922 static const struct smem_ops msm_smem_ops = {
923 .alloc = qcom_smem_alloc,
924 .get = qcom_smem_get,
925 .get_free_space = qcom_smem_get_free_space,
928 U_BOOT_DRIVER(qcom_smem) = {
931 .of_match = qcom_smem_of_match,
932 .ops = &msm_smem_ops,
933 .probe = qcom_smem_probe,
934 .remove = qcom_smem_remove,