1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015, Sony Mobile Communications AB.
4 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
7 #include <linux/hwspinlock.h>
9 #include <linux/module.h>
11 #include <linux/of_address.h>
12 #include <linux/of_reserved_mem.h>
13 #include <linux/platform_device.h>
14 #include <linux/sizes.h>
15 #include <linux/slab.h>
16 #include <linux/soc/qcom/smem.h>
19 * The Qualcomm shared memory system is a allocate only heap structure that
20 * consists of one of more memory areas that can be accessed by the processors
23 * All systems contains a global heap, accessible by all processors in the SoC,
24 * with a table of contents data structure (@smem_header) at the beginning of
25 * the main shared memory block.
27 * The global header contains meta data for allocations as well as a fixed list
28 * of 512 entries (@smem_global_entry) that can be initialized to reference
29 * parts of the shared memory space.
32 * In addition to this global heap a set of "private" heaps can be set up at
33 * boot time with access restrictions so that only certain processor pairs can
36 * These partitions are referenced from an optional partition table
37 * (@smem_ptable), that is found 4kB from the end of the main smem region. The
38 * partition table entries (@smem_ptable_entry) lists the involved processors
39 * (or hosts) and their location in the main shared memory region.
41 * Each partition starts with a header (@smem_partition_header) that identifies
42 * the partition and holds properties for the two internal memory regions. The
43 * two regions are cached and non-cached memory respectively. Each region
44 * contain a link list of allocation headers (@smem_private_entry) followed by
47 * Items in the non-cached region are allocated from the start of the partition
48 * while items in the cached region are allocated from the end. The free area
49 * is hence the region between the cached and non-cached offsets. The header of
50 * cached items comes after the data.
52 * Version 12 (SMEM_GLOBAL_PART_VERSION) changes the item alloc/get procedure
53 * for the global heap. A new global partition is created from the global heap
54 * region with partition type (SMEM_GLOBAL_HOST) and the max smem item count is
55 * set by the bootloader.
57 * To synchronize allocations in the shared memory heaps a remote spinlock must
58 * be held - currently lock number 3 of the sfpb or tcsr is used for this on all
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 15
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;
199 * struct smem_partition - describes smem partition
200 * @virt_base: starting virtual address of partition
201 * @phys_base: starting physical address of partition
202 * @cacheline: alignment for "cached" entries
203 * @size: size of partition
205 struct smem_partition {
206 void __iomem *virt_base;
207 phys_addr_t phys_base;
212 static const u8 SMEM_PART_MAGIC[] = { 0x24, 0x50, 0x52, 0x54 };
215 * struct smem_private_entry - header of each item in the private partition
216 * @canary: magic number, must be SMEM_PRIVATE_CANARY
217 * @item: identifying number of the smem item
218 * @size: size of the data, including padding bytes
219 * @padding_data: number of bytes of padding of data
220 * @padding_hdr: number of bytes of padding between the header and the data
221 * @reserved: for now reserved entry
223 struct smem_private_entry {
224 u16 canary; /* bytes are the same so no swapping needed */
226 __le32 size; /* includes padding bytes */
231 #define SMEM_PRIVATE_CANARY 0xa5a5
234 * struct smem_info - smem region info located after the table of contents
235 * @magic: magic number, must be SMEM_INFO_MAGIC
236 * @size: size of the smem region
237 * @base_addr: base address of the smem region
238 * @reserved: for now reserved entry
239 * @num_items: highest accepted item number
249 static const u8 SMEM_INFO_MAGIC[] = { 0x53, 0x49, 0x49, 0x49 }; /* SIII */
252 * struct smem_region - representation of a chunk of memory used for smem
253 * @aux_base: identifier of aux_mem base
254 * @virt_base: virtual base address of memory with this aux_mem identifier
255 * @size: size of the memory region
258 phys_addr_t aux_base;
259 void __iomem *virt_base;
264 * struct qcom_smem - device data for the smem device
265 * @dev: device pointer
266 * @hwlock: reference to a hwspinlock
267 * @ptable: virtual base of partition table
268 * @global_partition: describes for global partition when in use
269 * @partitions: list of partitions of current processor/host
270 * @item_count: max accepted item number
271 * @socinfo: platform device pointer
272 * @num_regions: number of @regions
273 * @regions: list of the memory regions defining the shared memory
278 struct hwspinlock *hwlock;
281 struct platform_device *socinfo;
282 struct smem_ptable *ptable;
283 struct smem_partition global_partition;
284 struct smem_partition partitions[SMEM_HOST_COUNT];
286 unsigned num_regions;
287 struct smem_region regions[];
291 phdr_to_last_uncached_entry(struct smem_partition_header *phdr)
295 return p + le32_to_cpu(phdr->offset_free_uncached);
298 static struct smem_private_entry *
299 phdr_to_first_cached_entry(struct smem_partition_header *phdr,
303 struct smem_private_entry *e;
305 return p + le32_to_cpu(phdr->size) - ALIGN(sizeof(*e), cacheline);
309 phdr_to_last_cached_entry(struct smem_partition_header *phdr)
313 return p + le32_to_cpu(phdr->offset_free_cached);
316 static struct smem_private_entry *
317 phdr_to_first_uncached_entry(struct smem_partition_header *phdr)
321 return p + sizeof(*phdr);
324 static struct smem_private_entry *
325 uncached_entry_next(struct smem_private_entry *e)
329 return p + sizeof(*e) + le16_to_cpu(e->padding_hdr) +
330 le32_to_cpu(e->size);
333 static struct smem_private_entry *
334 cached_entry_next(struct smem_private_entry *e, size_t cacheline)
338 return p - le32_to_cpu(e->size) - ALIGN(sizeof(*e), cacheline);
341 static void *uncached_entry_to_item(struct smem_private_entry *e)
345 return p + sizeof(*e) + le16_to_cpu(e->padding_hdr);
348 static void *cached_entry_to_item(struct smem_private_entry *e)
352 return p - le32_to_cpu(e->size);
355 /* Pointer to the one and only smem handle */
356 static struct qcom_smem *__smem;
358 /* Timeout (ms) for the trylock of remote spinlocks */
359 #define HWSPINLOCK_TIMEOUT 1000
361 static int qcom_smem_alloc_private(struct qcom_smem *smem,
362 struct smem_partition *part,
366 struct smem_private_entry *hdr, *end;
367 struct smem_partition_header *phdr;
372 phdr = (struct smem_partition_header __force *)part->virt_base;
373 p_end = (void *)phdr + part->size;
375 hdr = phdr_to_first_uncached_entry(phdr);
376 end = phdr_to_last_uncached_entry(phdr);
377 cached = phdr_to_last_cached_entry(phdr);
379 if (WARN_ON((void *)end > p_end || cached > p_end))
383 if (hdr->canary != SMEM_PRIVATE_CANARY)
385 if (le16_to_cpu(hdr->item) == item)
388 hdr = uncached_entry_next(hdr);
391 if (WARN_ON((void *)hdr > p_end))
394 /* Check that we don't grow into the cached region */
395 alloc_size = sizeof(*hdr) + ALIGN(size, 8);
396 if ((void *)hdr + alloc_size > cached) {
397 dev_err(smem->dev, "Out of memory\n");
401 hdr->canary = SMEM_PRIVATE_CANARY;
402 hdr->item = cpu_to_le16(item);
403 hdr->size = cpu_to_le32(ALIGN(size, 8));
404 hdr->padding_data = cpu_to_le16(le32_to_cpu(hdr->size) - size);
405 hdr->padding_hdr = 0;
408 * Ensure the header is written before we advance the free offset, so
409 * that remote processors that does not take the remote spinlock still
410 * gets a consistent view of the linked list.
413 le32_add_cpu(&phdr->offset_free_uncached, alloc_size);
417 dev_err(smem->dev, "Found invalid canary in hosts %hu:%hu partition\n",
418 le16_to_cpu(phdr->host0), le16_to_cpu(phdr->host1));
423 static int qcom_smem_alloc_global(struct qcom_smem *smem,
427 struct smem_global_entry *entry;
428 struct smem_header *header;
430 header = smem->regions[0].virt_base;
431 entry = &header->toc[item];
432 if (entry->allocated)
435 size = ALIGN(size, 8);
436 if (WARN_ON(size > le32_to_cpu(header->available)))
439 entry->offset = header->free_offset;
440 entry->size = cpu_to_le32(size);
443 * Ensure the header is consistent before we mark the item allocated,
444 * so that remote processors will get a consistent view of the item
445 * even though they do not take the spinlock on read.
448 entry->allocated = cpu_to_le32(1);
450 le32_add_cpu(&header->free_offset, size);
451 le32_add_cpu(&header->available, -size);
457 * qcom_smem_alloc() - allocate space for a smem item
458 * @host: remote processor id, or -1
459 * @item: smem item handle
460 * @size: number of bytes to be allocated
462 * Allocate space for a given smem item of size @size, given that the item is
465 int qcom_smem_alloc(unsigned host, unsigned item, size_t size)
467 struct smem_partition *part;
472 return -EPROBE_DEFER;
474 if (item < SMEM_ITEM_LAST_FIXED) {
476 "Rejecting allocation of static entry %d\n", item);
480 if (WARN_ON(item >= __smem->item_count))
483 ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
489 if (host < SMEM_HOST_COUNT && __smem->partitions[host].virt_base) {
490 part = &__smem->partitions[host];
491 ret = qcom_smem_alloc_private(__smem, part, item, size);
492 } else if (__smem->global_partition.virt_base) {
493 part = &__smem->global_partition;
494 ret = qcom_smem_alloc_private(__smem, part, item, size);
496 ret = qcom_smem_alloc_global(__smem, item, size);
499 hwspin_unlock_irqrestore(__smem->hwlock, &flags);
503 EXPORT_SYMBOL(qcom_smem_alloc);
505 static void *qcom_smem_get_global(struct qcom_smem *smem,
509 struct smem_header *header;
510 struct smem_region *region;
511 struct smem_global_entry *entry;
517 header = smem->regions[0].virt_base;
518 entry = &header->toc[item];
519 if (!entry->allocated)
520 return ERR_PTR(-ENXIO);
522 aux_base = le32_to_cpu(entry->aux_base) & AUX_BASE_MASK;
524 for (i = 0; i < smem->num_regions; i++) {
525 region = &smem->regions[i];
527 if ((u32)region->aux_base == aux_base || !aux_base) {
528 e_size = le32_to_cpu(entry->size);
529 entry_offset = le32_to_cpu(entry->offset);
531 if (WARN_ON(e_size + entry_offset > region->size))
532 return ERR_PTR(-EINVAL);
537 return region->virt_base + entry_offset;
541 return ERR_PTR(-ENOENT);
544 static void *qcom_smem_get_private(struct qcom_smem *smem,
545 struct smem_partition *part,
549 struct smem_private_entry *e, *end;
550 struct smem_partition_header *phdr;
551 void *item_ptr, *p_end;
555 phdr = (struct smem_partition_header __force *)part->virt_base;
556 p_end = (void *)phdr + part->size;
558 e = phdr_to_first_uncached_entry(phdr);
559 end = phdr_to_last_uncached_entry(phdr);
562 if (e->canary != SMEM_PRIVATE_CANARY)
565 if (le16_to_cpu(e->item) == item) {
567 e_size = le32_to_cpu(e->size);
568 padding_data = le16_to_cpu(e->padding_data);
570 if (WARN_ON(e_size > part->size || padding_data > e_size))
571 return ERR_PTR(-EINVAL);
573 *size = e_size - padding_data;
576 item_ptr = uncached_entry_to_item(e);
577 if (WARN_ON(item_ptr > p_end))
578 return ERR_PTR(-EINVAL);
583 e = uncached_entry_next(e);
586 if (WARN_ON((void *)e > p_end))
587 return ERR_PTR(-EINVAL);
589 /* Item was not found in the uncached list, search the cached list */
591 e = phdr_to_first_cached_entry(phdr, part->cacheline);
592 end = phdr_to_last_cached_entry(phdr);
594 if (WARN_ON((void *)e < (void *)phdr || (void *)end > p_end))
595 return ERR_PTR(-EINVAL);
598 if (e->canary != SMEM_PRIVATE_CANARY)
601 if (le16_to_cpu(e->item) == item) {
603 e_size = le32_to_cpu(e->size);
604 padding_data = le16_to_cpu(e->padding_data);
606 if (WARN_ON(e_size > part->size || padding_data > e_size))
607 return ERR_PTR(-EINVAL);
609 *size = e_size - padding_data;
612 item_ptr = cached_entry_to_item(e);
613 if (WARN_ON(item_ptr < (void *)phdr))
614 return ERR_PTR(-EINVAL);
619 e = cached_entry_next(e, part->cacheline);
622 if (WARN_ON((void *)e < (void *)phdr))
623 return ERR_PTR(-EINVAL);
625 return ERR_PTR(-ENOENT);
628 dev_err(smem->dev, "Found invalid canary in hosts %hu:%hu partition\n",
629 le16_to_cpu(phdr->host0), le16_to_cpu(phdr->host1));
631 return ERR_PTR(-EINVAL);
635 * qcom_smem_get() - resolve ptr of size of a smem item
636 * @host: the remote processor, or -1
637 * @item: smem item handle
638 * @size: pointer to be filled out with size of the item
640 * Looks up smem item and returns pointer to it. Size of smem
641 * item is returned in @size.
643 void *qcom_smem_get(unsigned host, unsigned item, size_t *size)
645 struct smem_partition *part;
648 void *ptr = ERR_PTR(-EPROBE_DEFER);
653 if (WARN_ON(item >= __smem->item_count))
654 return ERR_PTR(-EINVAL);
656 ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
662 if (host < SMEM_HOST_COUNT && __smem->partitions[host].virt_base) {
663 part = &__smem->partitions[host];
664 ptr = qcom_smem_get_private(__smem, part, item, size);
665 } else if (__smem->global_partition.virt_base) {
666 part = &__smem->global_partition;
667 ptr = qcom_smem_get_private(__smem, part, item, size);
669 ptr = qcom_smem_get_global(__smem, item, size);
672 hwspin_unlock_irqrestore(__smem->hwlock, &flags);
677 EXPORT_SYMBOL(qcom_smem_get);
680 * qcom_smem_get_free_space() - retrieve amount of free space in a partition
681 * @host: the remote processor identifying a partition, or -1
683 * To be used by smem clients as a quick way to determine if any new
684 * allocations has been made.
686 int qcom_smem_get_free_space(unsigned host)
688 struct smem_partition *part;
689 struct smem_partition_header *phdr;
690 struct smem_header *header;
694 return -EPROBE_DEFER;
696 if (host < SMEM_HOST_COUNT && __smem->partitions[host].virt_base) {
697 part = &__smem->partitions[host];
698 phdr = part->virt_base;
699 ret = le32_to_cpu(phdr->offset_free_cached) -
700 le32_to_cpu(phdr->offset_free_uncached);
702 if (ret > le32_to_cpu(part->size))
704 } else if (__smem->global_partition.virt_base) {
705 part = &__smem->global_partition;
706 phdr = part->virt_base;
707 ret = le32_to_cpu(phdr->offset_free_cached) -
708 le32_to_cpu(phdr->offset_free_uncached);
710 if (ret > le32_to_cpu(part->size))
713 header = __smem->regions[0].virt_base;
714 ret = le32_to_cpu(header->available);
716 if (ret > __smem->regions[0].size)
722 EXPORT_SYMBOL(qcom_smem_get_free_space);
724 static bool addr_in_range(void __iomem *base, size_t size, void *addr)
726 return base && (addr >= base && addr < base + size);
730 * qcom_smem_virt_to_phys() - return the physical address associated
731 * with an smem item pointer (previously returned by qcom_smem_get()
732 * @p: the virtual address to convert
734 * Returns 0 if the pointer provided is not within any smem region.
736 phys_addr_t qcom_smem_virt_to_phys(void *p)
738 struct smem_partition *part;
739 struct smem_region *area;
743 for (i = 0; i < SMEM_HOST_COUNT; i++) {
744 part = &__smem->partitions[i];
746 if (addr_in_range(part->virt_base, part->size, p)) {
747 offset = p - part->virt_base;
749 return (phys_addr_t)part->phys_base + offset;
753 part = &__smem->global_partition;
755 if (addr_in_range(part->virt_base, part->size, p)) {
756 offset = p - part->virt_base;
758 return (phys_addr_t)part->phys_base + offset;
761 for (i = 0; i < __smem->num_regions; i++) {
762 area = &__smem->regions[i];
764 if (addr_in_range(area->virt_base, area->size, p)) {
765 offset = p - area->virt_base;
767 return (phys_addr_t)area->aux_base + offset;
773 EXPORT_SYMBOL(qcom_smem_virt_to_phys);
775 static int qcom_smem_get_sbl_version(struct qcom_smem *smem)
777 struct smem_header *header;
780 header = smem->regions[0].virt_base;
781 versions = header->version;
783 return le32_to_cpu(versions[SMEM_MASTER_SBL_VERSION_INDEX]);
786 static struct smem_ptable *qcom_smem_get_ptable(struct qcom_smem *smem)
788 struct smem_ptable *ptable;
791 ptable = smem->ptable;
792 if (memcmp(ptable->magic, SMEM_PTABLE_MAGIC, sizeof(ptable->magic)))
793 return ERR_PTR(-ENOENT);
795 version = le32_to_cpu(ptable->version);
798 "Unsupported partition header version %d\n", version);
799 return ERR_PTR(-EINVAL);
804 static u32 qcom_smem_get_item_count(struct qcom_smem *smem)
806 struct smem_ptable *ptable;
807 struct smem_info *info;
809 ptable = qcom_smem_get_ptable(smem);
810 if (IS_ERR_OR_NULL(ptable))
811 return SMEM_ITEM_COUNT;
813 info = (struct smem_info *)&ptable->entry[ptable->num_entries];
814 if (memcmp(info->magic, SMEM_INFO_MAGIC, sizeof(info->magic)))
815 return SMEM_ITEM_COUNT;
817 return le16_to_cpu(info->num_items);
821 * Validate the partition header for a partition whose partition
822 * table entry is supplied. Returns a pointer to its header if
823 * valid, or a null pointer otherwise.
825 static struct smem_partition_header *
826 qcom_smem_partition_header(struct qcom_smem *smem,
827 struct smem_ptable_entry *entry, u16 host0, u16 host1)
829 struct smem_partition_header *header;
833 phys_addr = smem->regions[0].aux_base + le32_to_cpu(entry->offset);
834 header = devm_ioremap_wc(smem->dev, phys_addr, le32_to_cpu(entry->size));
839 if (memcmp(header->magic, SMEM_PART_MAGIC, sizeof(header->magic))) {
840 dev_err(smem->dev, "bad partition magic %4ph\n", header->magic);
844 if (host0 != le16_to_cpu(header->host0)) {
845 dev_err(smem->dev, "bad host0 (%hu != %hu)\n",
846 host0, le16_to_cpu(header->host0));
849 if (host1 != le16_to_cpu(header->host1)) {
850 dev_err(smem->dev, "bad host1 (%hu != %hu)\n",
851 host1, le16_to_cpu(header->host1));
855 size = le32_to_cpu(header->size);
856 if (size != le32_to_cpu(entry->size)) {
857 dev_err(smem->dev, "bad partition size (%u != %u)\n",
858 size, le32_to_cpu(entry->size));
862 if (le32_to_cpu(header->offset_free_uncached) > size) {
863 dev_err(smem->dev, "bad partition free uncached (%u > %u)\n",
864 le32_to_cpu(header->offset_free_uncached), size);
871 static int qcom_smem_set_global_partition(struct qcom_smem *smem)
873 struct smem_partition_header *header;
874 struct smem_ptable_entry *entry;
875 struct smem_ptable *ptable;
879 if (smem->global_partition.virt_base) {
880 dev_err(smem->dev, "Already found the global partition\n");
884 ptable = qcom_smem_get_ptable(smem);
886 return PTR_ERR(ptable);
888 for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
889 entry = &ptable->entry[i];
890 if (!le32_to_cpu(entry->offset))
892 if (!le32_to_cpu(entry->size))
895 if (le16_to_cpu(entry->host0) != SMEM_GLOBAL_HOST)
898 if (le16_to_cpu(entry->host1) == SMEM_GLOBAL_HOST) {
905 dev_err(smem->dev, "Missing entry for global partition\n");
909 header = qcom_smem_partition_header(smem, entry,
910 SMEM_GLOBAL_HOST, SMEM_GLOBAL_HOST);
914 smem->global_partition.virt_base = (void __iomem *)header;
915 smem->global_partition.phys_base = smem->regions[0].aux_base +
916 le32_to_cpu(entry->offset);
917 smem->global_partition.size = le32_to_cpu(entry->size);
918 smem->global_partition.cacheline = le32_to_cpu(entry->cacheline);
924 qcom_smem_enumerate_partitions(struct qcom_smem *smem, u16 local_host)
926 struct smem_partition_header *header;
927 struct smem_ptable_entry *entry;
928 struct smem_ptable *ptable;
933 ptable = qcom_smem_get_ptable(smem);
935 return PTR_ERR(ptable);
937 for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
938 entry = &ptable->entry[i];
939 if (!le32_to_cpu(entry->offset))
941 if (!le32_to_cpu(entry->size))
944 host0 = le16_to_cpu(entry->host0);
945 host1 = le16_to_cpu(entry->host1);
946 if (host0 == local_host)
948 else if (host1 == local_host)
953 if (remote_host >= SMEM_HOST_COUNT) {
954 dev_err(smem->dev, "bad host %u\n", remote_host);
958 if (smem->partitions[remote_host].virt_base) {
959 dev_err(smem->dev, "duplicate host %u\n", remote_host);
963 header = qcom_smem_partition_header(smem, entry, host0, host1);
967 smem->partitions[remote_host].virt_base = (void __iomem *)header;
968 smem->partitions[remote_host].phys_base = smem->regions[0].aux_base +
969 le32_to_cpu(entry->offset);
970 smem->partitions[remote_host].size = le32_to_cpu(entry->size);
971 smem->partitions[remote_host].cacheline = le32_to_cpu(entry->cacheline);
977 static int qcom_smem_map_toc(struct qcom_smem *smem, struct smem_region *region)
981 /* map starting 4K for smem header */
982 region->virt_base = devm_ioremap_wc(smem->dev, region->aux_base, SZ_4K);
983 ptable_start = region->aux_base + region->size - SZ_4K;
984 /* map last 4k for toc */
985 smem->ptable = devm_ioremap_wc(smem->dev, ptable_start, SZ_4K);
987 if (!region->virt_base || !smem->ptable)
993 static int qcom_smem_map_global(struct qcom_smem *smem, u32 size)
997 phys_addr = smem->regions[0].aux_base;
999 smem->regions[0].size = size;
1000 smem->regions[0].virt_base = devm_ioremap_wc(smem->dev, phys_addr, size);
1002 if (!smem->regions[0].virt_base)
1008 static int qcom_smem_resolve_mem(struct qcom_smem *smem, const char *name,
1009 struct smem_region *region)
1011 struct device *dev = smem->dev;
1012 struct device_node *np;
1016 np = of_parse_phandle(dev->of_node, name, 0);
1018 dev_err(dev, "No %s specified\n", name);
1022 ret = of_address_to_resource(np, 0, &r);
1027 region->aux_base = r.start;
1028 region->size = resource_size(&r);
1033 static int qcom_smem_probe(struct platform_device *pdev)
1035 struct smem_header *header;
1036 struct reserved_mem *rmem;
1037 struct qcom_smem *smem;
1038 unsigned long flags;
1048 if (of_find_property(pdev->dev.of_node, "qcom,rpm-msg-ram", NULL))
1051 array_size = num_regions * sizeof(struct smem_region);
1052 smem = devm_kzalloc(&pdev->dev, sizeof(*smem) + array_size, GFP_KERNEL);
1056 smem->dev = &pdev->dev;
1057 smem->num_regions = num_regions;
1059 rmem = of_reserved_mem_lookup(pdev->dev.of_node);
1061 smem->regions[0].aux_base = rmem->base;
1062 smem->regions[0].size = rmem->size;
1065 * Fall back to the memory-region reference, if we're not a
1066 * reserved-memory node.
1068 ret = qcom_smem_resolve_mem(smem, "memory-region", &smem->regions[0]);
1073 if (num_regions > 1) {
1074 ret = qcom_smem_resolve_mem(smem, "qcom,rpm-msg-ram", &smem->regions[1]);
1080 ret = qcom_smem_map_toc(smem, &smem->regions[0]);
1084 for (i = 1; i < num_regions; i++) {
1085 smem->regions[i].virt_base = devm_ioremap_wc(&pdev->dev,
1086 smem->regions[i].aux_base,
1087 smem->regions[i].size);
1088 if (!smem->regions[i].virt_base) {
1089 dev_err(&pdev->dev, "failed to remap %pa\n", &smem->regions[i].aux_base);
1094 header = smem->regions[0].virt_base;
1095 if (le32_to_cpu(header->initialized) != 1 ||
1096 le32_to_cpu(header->reserved)) {
1097 dev_err(&pdev->dev, "SMEM is not initialized by SBL\n");
1101 hwlock_id = of_hwspin_lock_get_id(pdev->dev.of_node, 0);
1102 if (hwlock_id < 0) {
1103 if (hwlock_id != -EPROBE_DEFER)
1104 dev_err(&pdev->dev, "failed to retrieve hwlock\n");
1108 smem->hwlock = hwspin_lock_request_specific(hwlock_id);
1112 ret = hwspin_lock_timeout_irqsave(smem->hwlock, HWSPINLOCK_TIMEOUT, &flags);
1115 size = readl_relaxed(&header->available) + readl_relaxed(&header->free_offset);
1116 hwspin_unlock_irqrestore(smem->hwlock, &flags);
1118 version = qcom_smem_get_sbl_version(smem);
1120 * smem header mapping is required only in heap version scheme, so unmap
1121 * it here. It will be remapped in qcom_smem_map_global() when whole
1122 * partition is mapped again.
1124 devm_iounmap(smem->dev, smem->regions[0].virt_base);
1125 switch (version >> 16) {
1126 case SMEM_GLOBAL_PART_VERSION:
1127 ret = qcom_smem_set_global_partition(smem);
1130 smem->item_count = qcom_smem_get_item_count(smem);
1132 case SMEM_GLOBAL_HEAP_VERSION:
1133 qcom_smem_map_global(smem, size);
1134 smem->item_count = SMEM_ITEM_COUNT;
1137 dev_err(&pdev->dev, "Unsupported SMEM version 0x%x\n", version);
1141 BUILD_BUG_ON(SMEM_HOST_APPS >= SMEM_HOST_COUNT);
1142 ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
1143 if (ret < 0 && ret != -ENOENT)
1148 smem->socinfo = platform_device_register_data(&pdev->dev, "qcom-socinfo",
1149 PLATFORM_DEVID_NONE, NULL,
1151 if (IS_ERR(smem->socinfo))
1152 dev_dbg(&pdev->dev, "failed to register socinfo device\n");
1157 static int qcom_smem_remove(struct platform_device *pdev)
1159 platform_device_unregister(__smem->socinfo);
1161 hwspin_lock_free(__smem->hwlock);
1167 static const struct of_device_id qcom_smem_of_match[] = {
1168 { .compatible = "qcom,smem" },
1171 MODULE_DEVICE_TABLE(of, qcom_smem_of_match);
1173 static struct platform_driver qcom_smem_driver = {
1174 .probe = qcom_smem_probe,
1175 .remove = qcom_smem_remove,
1177 .name = "qcom-smem",
1178 .of_match_table = qcom_smem_of_match,
1179 .suppress_bind_attrs = true,
1183 static int __init qcom_smem_init(void)
1185 return platform_driver_register(&qcom_smem_driver);
1187 arch_initcall(qcom_smem_init);
1189 static void __exit qcom_smem_exit(void)
1191 platform_driver_unregister(&qcom_smem_driver);
1193 module_exit(qcom_smem_exit)
1196 MODULE_DESCRIPTION("Qualcomm Shared Memory Manager");
1197 MODULE_LICENSE("GPL v2");