WIP FPC-III support
[linux/fpc-iii.git] / drivers / soc / qcom / smem.c
blob7251827bac88d02b20c8f4c30790150f74356c5b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2015, Sony Mobile Communications AB.
4 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
5 */
7 #include <linux/hwspinlock.h>
8 #include <linux/io.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/of_address.h>
12 #include <linux/platform_device.h>
13 #include <linux/sizes.h>
14 #include <linux/slab.h>
15 #include <linux/soc/qcom/smem.h>
18 * The Qualcomm shared memory system is a allocate only heap structure that
19 * consists of one of more memory areas that can be accessed by the processors
20 * in the SoC.
22 * All systems contains a global heap, accessible by all processors in the SoC,
23 * with a table of contents data structure (@smem_header) at the beginning of
24 * the main shared memory block.
26 * The global header contains meta data for allocations as well as a fixed list
27 * of 512 entries (@smem_global_entry) that can be initialized to reference
28 * parts of the shared memory space.
31 * In addition to this global heap a set of "private" heaps can be set up at
32 * boot time with access restrictions so that only certain processor pairs can
33 * access the data.
35 * These partitions are referenced from an optional partition table
36 * (@smem_ptable), that is found 4kB from the end of the main smem region. The
37 * partition table entries (@smem_ptable_entry) lists the involved processors
38 * (or hosts) and their location in the main shared memory region.
40 * Each partition starts with a header (@smem_partition_header) that identifies
41 * the partition and holds properties for the two internal memory regions. The
42 * two regions are cached and non-cached memory respectively. Each region
43 * contain a link list of allocation headers (@smem_private_entry) followed by
44 * their data.
46 * Items in the non-cached region are allocated from the start of the partition
47 * while items in the cached region are allocated from the end. The free area
48 * is hence the region between the cached and non-cached offsets. The header of
49 * cached items comes after the data.
51 * Version 12 (SMEM_GLOBAL_PART_VERSION) changes the item alloc/get procedure
52 * for the global heap. A new global partition is created from the global heap
53 * region with partition type (SMEM_GLOBAL_HOST) and the max smem item count is
54 * set by the bootloader.
56 * To synchronize allocations in the shared memory heaps a remote spinlock must
57 * be held - currently lock number 3 of the sfpb or tcsr is used for this on all
58 * platforms.
63 * The version member of the smem header contains an array of versions for the
64 * various software components in the SoC. We verify that the boot loader
65 * version is a valid version as a sanity check.
67 #define SMEM_MASTER_SBL_VERSION_INDEX 7
68 #define SMEM_GLOBAL_HEAP_VERSION 11
69 #define SMEM_GLOBAL_PART_VERSION 12
72 * The first 8 items are only to be allocated by the boot loader while
73 * initializing the heap.
75 #define SMEM_ITEM_LAST_FIXED 8
77 /* Highest accepted item number, for both global and private heaps */
78 #define SMEM_ITEM_COUNT 512
80 /* Processor/host identifier for the application processor */
81 #define SMEM_HOST_APPS 0
83 /* Processor/host identifier for the global partition */
84 #define SMEM_GLOBAL_HOST 0xfffe
86 /* Max number of processors/hosts in a system */
87 #define SMEM_HOST_COUNT 11
89 /**
90 * struct smem_proc_comm - proc_comm communication struct (legacy)
91 * @command: current command to be executed
92 * @status: status of the currently requested command
93 * @params: parameters to the command
95 struct smem_proc_comm {
96 __le32 command;
97 __le32 status;
98 __le32 params[2];
102 * struct smem_global_entry - entry to reference smem items on the heap
103 * @allocated: boolean to indicate if this entry is used
104 * @offset: offset to the allocated space
105 * @size: size of the allocated space, 8 byte aligned
106 * @aux_base: base address for the memory region used by this unit, or 0 for
107 * the default region. bits 0,1 are reserved
109 struct smem_global_entry {
110 __le32 allocated;
111 __le32 offset;
112 __le32 size;
113 __le32 aux_base; /* bits 1:0 reserved */
115 #define AUX_BASE_MASK 0xfffffffc
118 * struct smem_header - header found in beginning of primary smem region
119 * @proc_comm: proc_comm communication interface (legacy)
120 * @version: array of versions for the various subsystems
121 * @initialized: boolean to indicate that smem is initialized
122 * @free_offset: index of the first unallocated byte in smem
123 * @available: number of bytes available for allocation
124 * @reserved: reserved field, must be 0
125 * @toc: array of references to items
127 struct smem_header {
128 struct smem_proc_comm proc_comm[4];
129 __le32 version[32];
130 __le32 initialized;
131 __le32 free_offset;
132 __le32 available;
133 __le32 reserved;
134 struct smem_global_entry toc[SMEM_ITEM_COUNT];
138 * struct smem_ptable_entry - one entry in the @smem_ptable list
139 * @offset: offset, within the main shared memory region, of the partition
140 * @size: size of the partition
141 * @flags: flags for the partition (currently unused)
142 * @host0: first processor/host with access to this partition
143 * @host1: second processor/host with access to this partition
144 * @cacheline: alignment for "cached" entries
145 * @reserved: reserved entries for later use
147 struct smem_ptable_entry {
148 __le32 offset;
149 __le32 size;
150 __le32 flags;
151 __le16 host0;
152 __le16 host1;
153 __le32 cacheline;
154 __le32 reserved[7];
158 * struct smem_ptable - partition table for the private partitions
159 * @magic: magic number, must be SMEM_PTABLE_MAGIC
160 * @version: version of the partition table
161 * @num_entries: number of partitions in the table
162 * @reserved: for now reserved entries
163 * @entry: list of @smem_ptable_entry for the @num_entries partitions
165 struct smem_ptable {
166 u8 magic[4];
167 __le32 version;
168 __le32 num_entries;
169 __le32 reserved[5];
170 struct smem_ptable_entry entry[];
173 static const u8 SMEM_PTABLE_MAGIC[] = { 0x24, 0x54, 0x4f, 0x43 }; /* "$TOC" */
176 * struct smem_partition_header - header of the partitions
177 * @magic: magic number, must be SMEM_PART_MAGIC
178 * @host0: first processor/host with access to this partition
179 * @host1: second processor/host with access to this partition
180 * @size: size of the partition
181 * @offset_free_uncached: offset to the first free byte of uncached memory in
182 * this partition
183 * @offset_free_cached: offset to the first free byte of cached memory in this
184 * partition
185 * @reserved: for now reserved entries
187 struct smem_partition_header {
188 u8 magic[4];
189 __le16 host0;
190 __le16 host1;
191 __le32 size;
192 __le32 offset_free_uncached;
193 __le32 offset_free_cached;
194 __le32 reserved[3];
197 static const u8 SMEM_PART_MAGIC[] = { 0x24, 0x50, 0x52, 0x54 };
200 * struct smem_private_entry - header of each item in the private partition
201 * @canary: magic number, must be SMEM_PRIVATE_CANARY
202 * @item: identifying number of the smem item
203 * @size: size of the data, including padding bytes
204 * @padding_data: number of bytes of padding of data
205 * @padding_hdr: number of bytes of padding between the header and the data
206 * @reserved: for now reserved entry
208 struct smem_private_entry {
209 u16 canary; /* bytes are the same so no swapping needed */
210 __le16 item;
211 __le32 size; /* includes padding bytes */
212 __le16 padding_data;
213 __le16 padding_hdr;
214 __le32 reserved;
216 #define SMEM_PRIVATE_CANARY 0xa5a5
219 * struct smem_info - smem region info located after the table of contents
220 * @magic: magic number, must be SMEM_INFO_MAGIC
221 * @size: size of the smem region
222 * @base_addr: base address of the smem region
223 * @reserved: for now reserved entry
224 * @num_items: highest accepted item number
226 struct smem_info {
227 u8 magic[4];
228 __le32 size;
229 __le32 base_addr;
230 __le32 reserved;
231 __le16 num_items;
234 static const u8 SMEM_INFO_MAGIC[] = { 0x53, 0x49, 0x49, 0x49 }; /* SIII */
237 * struct smem_region - representation of a chunk of memory used for smem
238 * @aux_base: identifier of aux_mem base
239 * @virt_base: virtual base address of memory with this aux_mem identifier
240 * @size: size of the memory region
242 struct smem_region {
243 u32 aux_base;
244 void __iomem *virt_base;
245 size_t size;
249 * struct qcom_smem - device data for the smem device
250 * @dev: device pointer
251 * @hwlock: reference to a hwspinlock
252 * @global_partition: pointer to global partition when in use
253 * @global_cacheline: cacheline size for global partition
254 * @partitions: list of pointers to partitions affecting the current
255 * processor/host
256 * @cacheline: list of cacheline sizes for each host
257 * @item_count: max accepted item number
258 * @socinfo: platform device pointer
259 * @num_regions: number of @regions
260 * @regions: list of the memory regions defining the shared memory
262 struct qcom_smem {
263 struct device *dev;
265 struct hwspinlock *hwlock;
267 struct smem_partition_header *global_partition;
268 size_t global_cacheline;
269 struct smem_partition_header *partitions[SMEM_HOST_COUNT];
270 size_t cacheline[SMEM_HOST_COUNT];
271 u32 item_count;
272 struct platform_device *socinfo;
274 unsigned num_regions;
275 struct smem_region regions[];
278 static void *
279 phdr_to_last_uncached_entry(struct smem_partition_header *phdr)
281 void *p = phdr;
283 return p + le32_to_cpu(phdr->offset_free_uncached);
286 static struct smem_private_entry *
287 phdr_to_first_cached_entry(struct smem_partition_header *phdr,
288 size_t cacheline)
290 void *p = phdr;
291 struct smem_private_entry *e;
293 return p + le32_to_cpu(phdr->size) - ALIGN(sizeof(*e), cacheline);
296 static void *
297 phdr_to_last_cached_entry(struct smem_partition_header *phdr)
299 void *p = phdr;
301 return p + le32_to_cpu(phdr->offset_free_cached);
304 static struct smem_private_entry *
305 phdr_to_first_uncached_entry(struct smem_partition_header *phdr)
307 void *p = phdr;
309 return p + sizeof(*phdr);
312 static struct smem_private_entry *
313 uncached_entry_next(struct smem_private_entry *e)
315 void *p = e;
317 return p + sizeof(*e) + le16_to_cpu(e->padding_hdr) +
318 le32_to_cpu(e->size);
321 static struct smem_private_entry *
322 cached_entry_next(struct smem_private_entry *e, size_t cacheline)
324 void *p = e;
326 return p - le32_to_cpu(e->size) - ALIGN(sizeof(*e), cacheline);
329 static void *uncached_entry_to_item(struct smem_private_entry *e)
331 void *p = e;
333 return p + sizeof(*e) + le16_to_cpu(e->padding_hdr);
336 static void *cached_entry_to_item(struct smem_private_entry *e)
338 void *p = e;
340 return p - le32_to_cpu(e->size);
343 /* Pointer to the one and only smem handle */
344 static struct qcom_smem *__smem;
346 /* Timeout (ms) for the trylock of remote spinlocks */
347 #define HWSPINLOCK_TIMEOUT 1000
349 static int qcom_smem_alloc_private(struct qcom_smem *smem,
350 struct smem_partition_header *phdr,
351 unsigned item,
352 size_t size)
354 struct smem_private_entry *hdr, *end;
355 size_t alloc_size;
356 void *cached;
358 hdr = phdr_to_first_uncached_entry(phdr);
359 end = phdr_to_last_uncached_entry(phdr);
360 cached = phdr_to_last_cached_entry(phdr);
362 while (hdr < end) {
363 if (hdr->canary != SMEM_PRIVATE_CANARY)
364 goto bad_canary;
365 if (le16_to_cpu(hdr->item) == item)
366 return -EEXIST;
368 hdr = uncached_entry_next(hdr);
371 /* Check that we don't grow into the cached region */
372 alloc_size = sizeof(*hdr) + ALIGN(size, 8);
373 if ((void *)hdr + alloc_size > cached) {
374 dev_err(smem->dev, "Out of memory\n");
375 return -ENOSPC;
378 hdr->canary = SMEM_PRIVATE_CANARY;
379 hdr->item = cpu_to_le16(item);
380 hdr->size = cpu_to_le32(ALIGN(size, 8));
381 hdr->padding_data = cpu_to_le16(le32_to_cpu(hdr->size) - size);
382 hdr->padding_hdr = 0;
385 * Ensure the header is written before we advance the free offset, so
386 * that remote processors that does not take the remote spinlock still
387 * gets a consistent view of the linked list.
389 wmb();
390 le32_add_cpu(&phdr->offset_free_uncached, alloc_size);
392 return 0;
393 bad_canary:
394 dev_err(smem->dev, "Found invalid canary in hosts %hu:%hu partition\n",
395 le16_to_cpu(phdr->host0), le16_to_cpu(phdr->host1));
397 return -EINVAL;
400 static int qcom_smem_alloc_global(struct qcom_smem *smem,
401 unsigned item,
402 size_t size)
404 struct smem_global_entry *entry;
405 struct smem_header *header;
407 header = smem->regions[0].virt_base;
408 entry = &header->toc[item];
409 if (entry->allocated)
410 return -EEXIST;
412 size = ALIGN(size, 8);
413 if (WARN_ON(size > le32_to_cpu(header->available)))
414 return -ENOMEM;
416 entry->offset = header->free_offset;
417 entry->size = cpu_to_le32(size);
420 * Ensure the header is consistent before we mark the item allocated,
421 * so that remote processors will get a consistent view of the item
422 * even though they do not take the spinlock on read.
424 wmb();
425 entry->allocated = cpu_to_le32(1);
427 le32_add_cpu(&header->free_offset, size);
428 le32_add_cpu(&header->available, -size);
430 return 0;
434 * qcom_smem_alloc() - allocate space for a smem item
435 * @host: remote processor id, or -1
436 * @item: smem item handle
437 * @size: number of bytes to be allocated
439 * Allocate space for a given smem item of size @size, given that the item is
440 * not yet allocated.
442 int qcom_smem_alloc(unsigned host, unsigned item, size_t size)
444 struct smem_partition_header *phdr;
445 unsigned long flags;
446 int ret;
448 if (!__smem)
449 return -EPROBE_DEFER;
451 if (item < SMEM_ITEM_LAST_FIXED) {
452 dev_err(__smem->dev,
453 "Rejecting allocation of static entry %d\n", item);
454 return -EINVAL;
457 if (WARN_ON(item >= __smem->item_count))
458 return -EINVAL;
460 ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
461 HWSPINLOCK_TIMEOUT,
462 &flags);
463 if (ret)
464 return ret;
466 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
467 phdr = __smem->partitions[host];
468 ret = qcom_smem_alloc_private(__smem, phdr, item, size);
469 } else if (__smem->global_partition) {
470 phdr = __smem->global_partition;
471 ret = qcom_smem_alloc_private(__smem, phdr, item, size);
472 } else {
473 ret = qcom_smem_alloc_global(__smem, item, size);
476 hwspin_unlock_irqrestore(__smem->hwlock, &flags);
478 return ret;
480 EXPORT_SYMBOL(qcom_smem_alloc);
482 static void *qcom_smem_get_global(struct qcom_smem *smem,
483 unsigned item,
484 size_t *size)
486 struct smem_header *header;
487 struct smem_region *region;
488 struct smem_global_entry *entry;
489 u32 aux_base;
490 unsigned i;
492 header = smem->regions[0].virt_base;
493 entry = &header->toc[item];
494 if (!entry->allocated)
495 return ERR_PTR(-ENXIO);
497 aux_base = le32_to_cpu(entry->aux_base) & AUX_BASE_MASK;
499 for (i = 0; i < smem->num_regions; i++) {
500 region = &smem->regions[i];
502 if (region->aux_base == aux_base || !aux_base) {
503 if (size != NULL)
504 *size = le32_to_cpu(entry->size);
505 return region->virt_base + le32_to_cpu(entry->offset);
509 return ERR_PTR(-ENOENT);
512 static void *qcom_smem_get_private(struct qcom_smem *smem,
513 struct smem_partition_header *phdr,
514 size_t cacheline,
515 unsigned item,
516 size_t *size)
518 struct smem_private_entry *e, *end;
520 e = phdr_to_first_uncached_entry(phdr);
521 end = phdr_to_last_uncached_entry(phdr);
523 while (e < end) {
524 if (e->canary != SMEM_PRIVATE_CANARY)
525 goto invalid_canary;
527 if (le16_to_cpu(e->item) == item) {
528 if (size != NULL)
529 *size = le32_to_cpu(e->size) -
530 le16_to_cpu(e->padding_data);
532 return uncached_entry_to_item(e);
535 e = uncached_entry_next(e);
538 /* Item was not found in the uncached list, search the cached list */
540 e = phdr_to_first_cached_entry(phdr, cacheline);
541 end = phdr_to_last_cached_entry(phdr);
543 while (e > end) {
544 if (e->canary != SMEM_PRIVATE_CANARY)
545 goto invalid_canary;
547 if (le16_to_cpu(e->item) == item) {
548 if (size != NULL)
549 *size = le32_to_cpu(e->size) -
550 le16_to_cpu(e->padding_data);
552 return cached_entry_to_item(e);
555 e = cached_entry_next(e, cacheline);
558 return ERR_PTR(-ENOENT);
560 invalid_canary:
561 dev_err(smem->dev, "Found invalid canary in hosts %hu:%hu partition\n",
562 le16_to_cpu(phdr->host0), le16_to_cpu(phdr->host1));
564 return ERR_PTR(-EINVAL);
568 * qcom_smem_get() - resolve ptr of size of a smem item
569 * @host: the remote processor, or -1
570 * @item: smem item handle
571 * @size: pointer to be filled out with size of the item
573 * Looks up smem item and returns pointer to it. Size of smem
574 * item is returned in @size.
576 void *qcom_smem_get(unsigned host, unsigned item, size_t *size)
578 struct smem_partition_header *phdr;
579 unsigned long flags;
580 size_t cacheln;
581 int ret;
582 void *ptr = ERR_PTR(-EPROBE_DEFER);
584 if (!__smem)
585 return ptr;
587 if (WARN_ON(item >= __smem->item_count))
588 return ERR_PTR(-EINVAL);
590 ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
591 HWSPINLOCK_TIMEOUT,
592 &flags);
593 if (ret)
594 return ERR_PTR(ret);
596 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
597 phdr = __smem->partitions[host];
598 cacheln = __smem->cacheline[host];
599 ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
600 } else if (__smem->global_partition) {
601 phdr = __smem->global_partition;
602 cacheln = __smem->global_cacheline;
603 ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
604 } else {
605 ptr = qcom_smem_get_global(__smem, item, size);
608 hwspin_unlock_irqrestore(__smem->hwlock, &flags);
610 return ptr;
613 EXPORT_SYMBOL(qcom_smem_get);
616 * qcom_smem_get_free_space() - retrieve amount of free space in a partition
617 * @host: the remote processor identifying a partition, or -1
619 * To be used by smem clients as a quick way to determine if any new
620 * allocations has been made.
622 int qcom_smem_get_free_space(unsigned host)
624 struct smem_partition_header *phdr;
625 struct smem_header *header;
626 unsigned ret;
628 if (!__smem)
629 return -EPROBE_DEFER;
631 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
632 phdr = __smem->partitions[host];
633 ret = le32_to_cpu(phdr->offset_free_cached) -
634 le32_to_cpu(phdr->offset_free_uncached);
635 } else if (__smem->global_partition) {
636 phdr = __smem->global_partition;
637 ret = le32_to_cpu(phdr->offset_free_cached) -
638 le32_to_cpu(phdr->offset_free_uncached);
639 } else {
640 header = __smem->regions[0].virt_base;
641 ret = le32_to_cpu(header->available);
644 return ret;
646 EXPORT_SYMBOL(qcom_smem_get_free_space);
649 * qcom_smem_virt_to_phys() - return the physical address associated
650 * with an smem item pointer (previously returned by qcom_smem_get()
651 * @p: the virtual address to convert
653 * Returns 0 if the pointer provided is not within any smem region.
655 phys_addr_t qcom_smem_virt_to_phys(void *p)
657 unsigned i;
659 for (i = 0; i < __smem->num_regions; i++) {
660 struct smem_region *region = &__smem->regions[i];
662 if (p < region->virt_base)
663 continue;
664 if (p < region->virt_base + region->size) {
665 u64 offset = p - region->virt_base;
667 return (phys_addr_t)region->aux_base + offset;
671 return 0;
673 EXPORT_SYMBOL(qcom_smem_virt_to_phys);
675 static int qcom_smem_get_sbl_version(struct qcom_smem *smem)
677 struct smem_header *header;
678 __le32 *versions;
680 header = smem->regions[0].virt_base;
681 versions = header->version;
683 return le32_to_cpu(versions[SMEM_MASTER_SBL_VERSION_INDEX]);
686 static struct smem_ptable *qcom_smem_get_ptable(struct qcom_smem *smem)
688 struct smem_ptable *ptable;
689 u32 version;
691 ptable = smem->regions[0].virt_base + smem->regions[0].size - SZ_4K;
692 if (memcmp(ptable->magic, SMEM_PTABLE_MAGIC, sizeof(ptable->magic)))
693 return ERR_PTR(-ENOENT);
695 version = le32_to_cpu(ptable->version);
696 if (version != 1) {
697 dev_err(smem->dev,
698 "Unsupported partition header version %d\n", version);
699 return ERR_PTR(-EINVAL);
701 return ptable;
704 static u32 qcom_smem_get_item_count(struct qcom_smem *smem)
706 struct smem_ptable *ptable;
707 struct smem_info *info;
709 ptable = qcom_smem_get_ptable(smem);
710 if (IS_ERR_OR_NULL(ptable))
711 return SMEM_ITEM_COUNT;
713 info = (struct smem_info *)&ptable->entry[ptable->num_entries];
714 if (memcmp(info->magic, SMEM_INFO_MAGIC, sizeof(info->magic)))
715 return SMEM_ITEM_COUNT;
717 return le16_to_cpu(info->num_items);
721 * Validate the partition header for a partition whose partition
722 * table entry is supplied. Returns a pointer to its header if
723 * valid, or a null pointer otherwise.
725 static struct smem_partition_header *
726 qcom_smem_partition_header(struct qcom_smem *smem,
727 struct smem_ptable_entry *entry, u16 host0, u16 host1)
729 struct smem_partition_header *header;
730 u32 size;
732 header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
734 if (memcmp(header->magic, SMEM_PART_MAGIC, sizeof(header->magic))) {
735 dev_err(smem->dev, "bad partition magic %02x %02x %02x %02x\n",
736 header->magic[0], header->magic[1],
737 header->magic[2], header->magic[3]);
738 return NULL;
741 if (host0 != le16_to_cpu(header->host0)) {
742 dev_err(smem->dev, "bad host0 (%hu != %hu)\n",
743 host0, le16_to_cpu(header->host0));
744 return NULL;
746 if (host1 != le16_to_cpu(header->host1)) {
747 dev_err(smem->dev, "bad host1 (%hu != %hu)\n",
748 host1, le16_to_cpu(header->host1));
749 return NULL;
752 size = le32_to_cpu(header->size);
753 if (size != le32_to_cpu(entry->size)) {
754 dev_err(smem->dev, "bad partition size (%u != %u)\n",
755 size, le32_to_cpu(entry->size));
756 return NULL;
759 if (le32_to_cpu(header->offset_free_uncached) > size) {
760 dev_err(smem->dev, "bad partition free uncached (%u > %u)\n",
761 le32_to_cpu(header->offset_free_uncached), size);
762 return NULL;
765 return header;
768 static int qcom_smem_set_global_partition(struct qcom_smem *smem)
770 struct smem_partition_header *header;
771 struct smem_ptable_entry *entry;
772 struct smem_ptable *ptable;
773 bool found = false;
774 int i;
776 if (smem->global_partition) {
777 dev_err(smem->dev, "Already found the global partition\n");
778 return -EINVAL;
781 ptable = qcom_smem_get_ptable(smem);
782 if (IS_ERR(ptable))
783 return PTR_ERR(ptable);
785 for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
786 entry = &ptable->entry[i];
787 if (!le32_to_cpu(entry->offset))
788 continue;
789 if (!le32_to_cpu(entry->size))
790 continue;
792 if (le16_to_cpu(entry->host0) != SMEM_GLOBAL_HOST)
793 continue;
795 if (le16_to_cpu(entry->host1) == SMEM_GLOBAL_HOST) {
796 found = true;
797 break;
801 if (!found) {
802 dev_err(smem->dev, "Missing entry for global partition\n");
803 return -EINVAL;
806 header = qcom_smem_partition_header(smem, entry,
807 SMEM_GLOBAL_HOST, SMEM_GLOBAL_HOST);
808 if (!header)
809 return -EINVAL;
811 smem->global_partition = header;
812 smem->global_cacheline = le32_to_cpu(entry->cacheline);
814 return 0;
817 static int
818 qcom_smem_enumerate_partitions(struct qcom_smem *smem, u16 local_host)
820 struct smem_partition_header *header;
821 struct smem_ptable_entry *entry;
822 struct smem_ptable *ptable;
823 unsigned int remote_host;
824 u16 host0, host1;
825 int i;
827 ptable = qcom_smem_get_ptable(smem);
828 if (IS_ERR(ptable))
829 return PTR_ERR(ptable);
831 for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
832 entry = &ptable->entry[i];
833 if (!le32_to_cpu(entry->offset))
834 continue;
835 if (!le32_to_cpu(entry->size))
836 continue;
838 host0 = le16_to_cpu(entry->host0);
839 host1 = le16_to_cpu(entry->host1);
840 if (host0 == local_host)
841 remote_host = host1;
842 else if (host1 == local_host)
843 remote_host = host0;
844 else
845 continue;
847 if (remote_host >= SMEM_HOST_COUNT) {
848 dev_err(smem->dev, "bad host %hu\n", remote_host);
849 return -EINVAL;
852 if (smem->partitions[remote_host]) {
853 dev_err(smem->dev, "duplicate host %hu\n", remote_host);
854 return -EINVAL;
857 header = qcom_smem_partition_header(smem, entry, host0, host1);
858 if (!header)
859 return -EINVAL;
861 smem->partitions[remote_host] = header;
862 smem->cacheline[remote_host] = le32_to_cpu(entry->cacheline);
865 return 0;
868 static int qcom_smem_map_memory(struct qcom_smem *smem, struct device *dev,
869 const char *name, int i)
871 struct device_node *np;
872 struct resource r;
873 resource_size_t size;
874 int ret;
876 np = of_parse_phandle(dev->of_node, name, 0);
877 if (!np) {
878 dev_err(dev, "No %s specified\n", name);
879 return -EINVAL;
882 ret = of_address_to_resource(np, 0, &r);
883 of_node_put(np);
884 if (ret)
885 return ret;
886 size = resource_size(&r);
888 smem->regions[i].virt_base = devm_ioremap_wc(dev, r.start, size);
889 if (!smem->regions[i].virt_base)
890 return -ENOMEM;
891 smem->regions[i].aux_base = (u32)r.start;
892 smem->regions[i].size = size;
894 return 0;
897 static int qcom_smem_probe(struct platform_device *pdev)
899 struct smem_header *header;
900 struct qcom_smem *smem;
901 size_t array_size;
902 int num_regions;
903 int hwlock_id;
904 u32 version;
905 int ret;
907 num_regions = 1;
908 if (of_find_property(pdev->dev.of_node, "qcom,rpm-msg-ram", NULL))
909 num_regions++;
911 array_size = num_regions * sizeof(struct smem_region);
912 smem = devm_kzalloc(&pdev->dev, sizeof(*smem) + array_size, GFP_KERNEL);
913 if (!smem)
914 return -ENOMEM;
916 smem->dev = &pdev->dev;
917 smem->num_regions = num_regions;
919 ret = qcom_smem_map_memory(smem, &pdev->dev, "memory-region", 0);
920 if (ret)
921 return ret;
923 if (num_regions > 1 && (ret = qcom_smem_map_memory(smem, &pdev->dev,
924 "qcom,rpm-msg-ram", 1)))
925 return ret;
927 header = smem->regions[0].virt_base;
928 if (le32_to_cpu(header->initialized) != 1 ||
929 le32_to_cpu(header->reserved)) {
930 dev_err(&pdev->dev, "SMEM is not initialized by SBL\n");
931 return -EINVAL;
934 version = qcom_smem_get_sbl_version(smem);
935 switch (version >> 16) {
936 case SMEM_GLOBAL_PART_VERSION:
937 ret = qcom_smem_set_global_partition(smem);
938 if (ret < 0)
939 return ret;
940 smem->item_count = qcom_smem_get_item_count(smem);
941 break;
942 case SMEM_GLOBAL_HEAP_VERSION:
943 smem->item_count = SMEM_ITEM_COUNT;
944 break;
945 default:
946 dev_err(&pdev->dev, "Unsupported SMEM version 0x%x\n", version);
947 return -EINVAL;
950 BUILD_BUG_ON(SMEM_HOST_APPS >= SMEM_HOST_COUNT);
951 ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
952 if (ret < 0 && ret != -ENOENT)
953 return ret;
955 hwlock_id = of_hwspin_lock_get_id(pdev->dev.of_node, 0);
956 if (hwlock_id < 0) {
957 if (hwlock_id != -EPROBE_DEFER)
958 dev_err(&pdev->dev, "failed to retrieve hwlock\n");
959 return hwlock_id;
962 smem->hwlock = hwspin_lock_request_specific(hwlock_id);
963 if (!smem->hwlock)
964 return -ENXIO;
966 __smem = smem;
968 smem->socinfo = platform_device_register_data(&pdev->dev, "qcom-socinfo",
969 PLATFORM_DEVID_NONE, NULL,
971 if (IS_ERR(smem->socinfo))
972 dev_dbg(&pdev->dev, "failed to register socinfo device\n");
974 return 0;
977 static int qcom_smem_remove(struct platform_device *pdev)
979 platform_device_unregister(__smem->socinfo);
981 hwspin_lock_free(__smem->hwlock);
982 __smem = NULL;
984 return 0;
987 static const struct of_device_id qcom_smem_of_match[] = {
988 { .compatible = "qcom,smem" },
991 MODULE_DEVICE_TABLE(of, qcom_smem_of_match);
993 static struct platform_driver qcom_smem_driver = {
994 .probe = qcom_smem_probe,
995 .remove = qcom_smem_remove,
996 .driver = {
997 .name = "qcom-smem",
998 .of_match_table = qcom_smem_of_match,
999 .suppress_bind_attrs = true,
1003 static int __init qcom_smem_init(void)
1005 return platform_driver_register(&qcom_smem_driver);
1007 arch_initcall(qcom_smem_init);
1009 static void __exit qcom_smem_exit(void)
1011 platform_driver_unregister(&qcom_smem_driver);
1013 module_exit(qcom_smem_exit)
1015 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
1016 MODULE_DESCRIPTION("Qualcomm Shared Memory Manager");
1017 MODULE_LICENSE("GPL v2");