staging: most: core: add a match function for the bus
[linux/fpc-iii.git] / drivers / soc / qcom / smem.c
blob0b94d62fad2bf8632c57e31a9b3bbf3ab971d11a
1 /*
2 * Copyright (c) 2015, Sony Mobile Communications AB.
3 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/hwspinlock.h>
16 #include <linux/io.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/of_address.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/soc/qcom/smem.h>
25 * The Qualcomm shared memory system is a allocate only heap structure that
26 * consists of one of more memory areas that can be accessed by the processors
27 * in the SoC.
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
40 * access the data.
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
51 * their data.
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.
63 * To synchronize allocations in the shared memory heaps a remote spinlock must
64 * be held - currently lock number 3 of the sfpb or tcsr is used for this on all
65 * platforms.
70 * The version member of the smem header contains an array of versions for the
71 * various software components in the SoC. We verify that the boot loader
72 * version is a valid version as a sanity check.
74 #define SMEM_MASTER_SBL_VERSION_INDEX 7
75 #define SMEM_GLOBAL_HEAP_VERSION 11
76 #define SMEM_GLOBAL_PART_VERSION 12
79 * The first 8 items are only to be allocated by the boot loader while
80 * initializing the heap.
82 #define SMEM_ITEM_LAST_FIXED 8
84 /* Highest accepted item number, for both global and private heaps */
85 #define SMEM_ITEM_COUNT 512
87 /* Processor/host identifier for the application processor */
88 #define SMEM_HOST_APPS 0
90 /* Processor/host identifier for the global partition */
91 #define SMEM_GLOBAL_HOST 0xfffe
93 /* Max number of processors/hosts in a system */
94 #define SMEM_HOST_COUNT 10
96 /**
97 * struct smem_proc_comm - proc_comm communication struct (legacy)
98 * @command: current command to be executed
99 * @status: status of the currently requested command
100 * @params: parameters to the command
102 struct smem_proc_comm {
103 __le32 command;
104 __le32 status;
105 __le32 params[2];
109 * struct smem_global_entry - entry to reference smem items on the heap
110 * @allocated: boolean to indicate if this entry is used
111 * @offset: offset to the allocated space
112 * @size: size of the allocated space, 8 byte aligned
113 * @aux_base: base address for the memory region used by this unit, or 0 for
114 * the default region. bits 0,1 are reserved
116 struct smem_global_entry {
117 __le32 allocated;
118 __le32 offset;
119 __le32 size;
120 __le32 aux_base; /* bits 1:0 reserved */
122 #define AUX_BASE_MASK 0xfffffffc
125 * struct smem_header - header found in beginning of primary smem region
126 * @proc_comm: proc_comm communication interface (legacy)
127 * @version: array of versions for the various subsystems
128 * @initialized: boolean to indicate that smem is initialized
129 * @free_offset: index of the first unallocated byte in smem
130 * @available: number of bytes available for allocation
131 * @reserved: reserved field, must be 0
132 * toc: array of references to items
134 struct smem_header {
135 struct smem_proc_comm proc_comm[4];
136 __le32 version[32];
137 __le32 initialized;
138 __le32 free_offset;
139 __le32 available;
140 __le32 reserved;
141 struct smem_global_entry toc[SMEM_ITEM_COUNT];
145 * struct smem_ptable_entry - one entry in the @smem_ptable list
146 * @offset: offset, within the main shared memory region, of the partition
147 * @size: size of the partition
148 * @flags: flags for the partition (currently unused)
149 * @host0: first processor/host with access to this partition
150 * @host1: second processor/host with access to this partition
151 * @cacheline: alignment for "cached" entries
152 * @reserved: reserved entries for later use
154 struct smem_ptable_entry {
155 __le32 offset;
156 __le32 size;
157 __le32 flags;
158 __le16 host0;
159 __le16 host1;
160 __le32 cacheline;
161 __le32 reserved[7];
165 * struct smem_ptable - partition table for the private partitions
166 * @magic: magic number, must be SMEM_PTABLE_MAGIC
167 * @version: version of the partition table
168 * @num_entries: number of partitions in the table
169 * @reserved: for now reserved entries
170 * @entry: list of @smem_ptable_entry for the @num_entries partitions
172 struct smem_ptable {
173 u8 magic[4];
174 __le32 version;
175 __le32 num_entries;
176 __le32 reserved[5];
177 struct smem_ptable_entry entry[];
180 static const u8 SMEM_PTABLE_MAGIC[] = { 0x24, 0x54, 0x4f, 0x43 }; /* "$TOC" */
183 * struct smem_partition_header - header of the partitions
184 * @magic: magic number, must be SMEM_PART_MAGIC
185 * @host0: first processor/host with access to this partition
186 * @host1: second processor/host with access to this partition
187 * @size: size of the partition
188 * @offset_free_uncached: offset to the first free byte of uncached memory in
189 * this partition
190 * @offset_free_cached: offset to the first free byte of cached memory in this
191 * partition
192 * @reserved: for now reserved entries
194 struct smem_partition_header {
195 u8 magic[4];
196 __le16 host0;
197 __le16 host1;
198 __le32 size;
199 __le32 offset_free_uncached;
200 __le32 offset_free_cached;
201 __le32 reserved[3];
204 static const u8 SMEM_PART_MAGIC[] = { 0x24, 0x50, 0x52, 0x54 };
207 * struct smem_private_entry - header of each item in the private partition
208 * @canary: magic number, must be SMEM_PRIVATE_CANARY
209 * @item: identifying number of the smem item
210 * @size: size of the data, including padding bytes
211 * @padding_data: number of bytes of padding of data
212 * @padding_hdr: number of bytes of padding between the header and the data
213 * @reserved: for now reserved entry
215 struct smem_private_entry {
216 u16 canary; /* bytes are the same so no swapping needed */
217 __le16 item;
218 __le32 size; /* includes padding bytes */
219 __le16 padding_data;
220 __le16 padding_hdr;
221 __le32 reserved;
223 #define SMEM_PRIVATE_CANARY 0xa5a5
226 * struct smem_info - smem region info located after the table of contents
227 * @magic: magic number, must be SMEM_INFO_MAGIC
228 * @size: size of the smem region
229 * @base_addr: base address of the smem region
230 * @reserved: for now reserved entry
231 * @num_items: highest accepted item number
233 struct smem_info {
234 u8 magic[4];
235 __le32 size;
236 __le32 base_addr;
237 __le32 reserved;
238 __le16 num_items;
241 static const u8 SMEM_INFO_MAGIC[] = { 0x53, 0x49, 0x49, 0x49 }; /* SIII */
244 * struct smem_region - representation of a chunk of memory used for smem
245 * @aux_base: identifier of aux_mem base
246 * @virt_base: virtual base address of memory with this aux_mem identifier
247 * @size: size of the memory region
249 struct smem_region {
250 u32 aux_base;
251 void __iomem *virt_base;
252 size_t size;
256 * struct qcom_smem - device data for the smem device
257 * @dev: device pointer
258 * @hwlock: reference to a hwspinlock
259 * @global_partition: pointer to global partition when in use
260 * @global_cacheline: cacheline size for global partition
261 * @partitions: list of pointers to partitions affecting the current
262 * processor/host
263 * @cacheline: list of cacheline sizes for each host
264 * @item_count: max accepted item number
265 * @num_regions: number of @regions
266 * @regions: list of the memory regions defining the shared memory
268 struct qcom_smem {
269 struct device *dev;
271 struct hwspinlock *hwlock;
273 struct smem_partition_header *global_partition;
274 size_t global_cacheline;
275 struct smem_partition_header *partitions[SMEM_HOST_COUNT];
276 size_t cacheline[SMEM_HOST_COUNT];
277 u32 item_count;
279 unsigned num_regions;
280 struct smem_region regions[0];
283 static struct smem_private_entry *
284 phdr_to_last_uncached_entry(struct smem_partition_header *phdr)
286 void *p = phdr;
288 return p + le32_to_cpu(phdr->offset_free_uncached);
291 static void *phdr_to_first_cached_entry(struct smem_partition_header *phdr,
292 size_t cacheline)
294 void *p = phdr;
296 return p + le32_to_cpu(phdr->size) - ALIGN(sizeof(*phdr), cacheline);
299 static void *phdr_to_last_cached_entry(struct smem_partition_header *phdr)
301 void *p = phdr;
303 return p + le32_to_cpu(phdr->offset_free_cached);
306 static struct smem_private_entry *
307 phdr_to_first_uncached_entry(struct smem_partition_header *phdr)
309 void *p = phdr;
311 return p + sizeof(*phdr);
314 static struct smem_private_entry *
315 uncached_entry_next(struct smem_private_entry *e)
317 void *p = e;
319 return p + sizeof(*e) + le16_to_cpu(e->padding_hdr) +
320 le32_to_cpu(e->size);
323 static struct smem_private_entry *
324 cached_entry_next(struct smem_private_entry *e, size_t cacheline)
326 void *p = e;
328 return p - le32_to_cpu(e->size) - ALIGN(sizeof(*e), cacheline);
331 static void *uncached_entry_to_item(struct smem_private_entry *e)
333 void *p = e;
335 return p + sizeof(*e) + le16_to_cpu(e->padding_hdr);
338 static void *cached_entry_to_item(struct smem_private_entry *e)
340 void *p = e;
342 return p - le32_to_cpu(e->size);
345 /* Pointer to the one and only smem handle */
346 static struct qcom_smem *__smem;
348 /* Timeout (ms) for the trylock of remote spinlocks */
349 #define HWSPINLOCK_TIMEOUT 1000
351 static int qcom_smem_alloc_private(struct qcom_smem *smem,
352 struct smem_partition_header *phdr,
353 unsigned item,
354 size_t size)
356 struct smem_private_entry *hdr, *end;
357 size_t alloc_size;
358 void *cached;
360 hdr = phdr_to_first_uncached_entry(phdr);
361 end = phdr_to_last_uncached_entry(phdr);
362 cached = phdr_to_last_cached_entry(phdr);
364 while (hdr < end) {
365 if (hdr->canary != SMEM_PRIVATE_CANARY) {
366 dev_err(smem->dev,
367 "Found invalid canary in hosts %d:%d partition\n",
368 phdr->host0, phdr->host1);
369 return -EINVAL;
372 if (le16_to_cpu(hdr->item) == item)
373 return -EEXIST;
375 hdr = uncached_entry_next(hdr);
378 /* Check that we don't grow into the cached region */
379 alloc_size = sizeof(*hdr) + ALIGN(size, 8);
380 if ((void *)hdr + alloc_size >= cached) {
381 dev_err(smem->dev, "Out of memory\n");
382 return -ENOSPC;
385 hdr->canary = SMEM_PRIVATE_CANARY;
386 hdr->item = cpu_to_le16(item);
387 hdr->size = cpu_to_le32(ALIGN(size, 8));
388 hdr->padding_data = cpu_to_le16(le32_to_cpu(hdr->size) - size);
389 hdr->padding_hdr = 0;
392 * Ensure the header is written before we advance the free offset, so
393 * that remote processors that does not take the remote spinlock still
394 * gets a consistent view of the linked list.
396 wmb();
397 le32_add_cpu(&phdr->offset_free_uncached, alloc_size);
399 return 0;
402 static int qcom_smem_alloc_global(struct qcom_smem *smem,
403 unsigned item,
404 size_t size)
406 struct smem_global_entry *entry;
407 struct smem_header *header;
409 header = smem->regions[0].virt_base;
410 entry = &header->toc[item];
411 if (entry->allocated)
412 return -EEXIST;
414 size = ALIGN(size, 8);
415 if (WARN_ON(size > le32_to_cpu(header->available)))
416 return -ENOMEM;
418 entry->offset = header->free_offset;
419 entry->size = cpu_to_le32(size);
422 * Ensure the header is consistent before we mark the item allocated,
423 * so that remote processors will get a consistent view of the item
424 * even though they do not take the spinlock on read.
426 wmb();
427 entry->allocated = cpu_to_le32(1);
429 le32_add_cpu(&header->free_offset, size);
430 le32_add_cpu(&header->available, -size);
432 return 0;
436 * qcom_smem_alloc() - allocate space for a smem item
437 * @host: remote processor id, or -1
438 * @item: smem item handle
439 * @size: number of bytes to be allocated
441 * Allocate space for a given smem item of size @size, given that the item is
442 * not yet allocated.
444 int qcom_smem_alloc(unsigned host, unsigned item, size_t size)
446 struct smem_partition_header *phdr;
447 unsigned long flags;
448 int ret;
450 if (!__smem)
451 return -EPROBE_DEFER;
453 if (item < SMEM_ITEM_LAST_FIXED) {
454 dev_err(__smem->dev,
455 "Rejecting allocation of static entry %d\n", item);
456 return -EINVAL;
459 if (WARN_ON(item >= __smem->item_count))
460 return -EINVAL;
462 ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
463 HWSPINLOCK_TIMEOUT,
464 &flags);
465 if (ret)
466 return ret;
468 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
469 phdr = __smem->partitions[host];
470 ret = qcom_smem_alloc_private(__smem, phdr, item, size);
471 } else if (__smem->global_partition) {
472 phdr = __smem->global_partition;
473 ret = qcom_smem_alloc_private(__smem, phdr, item, size);
474 } else {
475 ret = qcom_smem_alloc_global(__smem, item, size);
478 hwspin_unlock_irqrestore(__smem->hwlock, &flags);
480 return ret;
482 EXPORT_SYMBOL(qcom_smem_alloc);
484 static void *qcom_smem_get_global(struct qcom_smem *smem,
485 unsigned item,
486 size_t *size)
488 struct smem_header *header;
489 struct smem_region *area;
490 struct smem_global_entry *entry;
491 u32 aux_base;
492 unsigned i;
494 header = smem->regions[0].virt_base;
495 entry = &header->toc[item];
496 if (!entry->allocated)
497 return ERR_PTR(-ENXIO);
499 aux_base = le32_to_cpu(entry->aux_base) & AUX_BASE_MASK;
501 for (i = 0; i < smem->num_regions; i++) {
502 area = &smem->regions[i];
504 if (area->aux_base == aux_base || !aux_base) {
505 if (size != NULL)
506 *size = le32_to_cpu(entry->size);
507 return area->virt_base + le32_to_cpu(entry->offset);
511 return ERR_PTR(-ENOENT);
514 static void *qcom_smem_get_private(struct qcom_smem *smem,
515 struct smem_partition_header *phdr,
516 size_t cacheline,
517 unsigned item,
518 size_t *size)
520 struct smem_private_entry *e, *end;
522 e = phdr_to_first_uncached_entry(phdr);
523 end = phdr_to_last_uncached_entry(phdr);
525 while (e < end) {
526 if (e->canary != SMEM_PRIVATE_CANARY)
527 goto invalid_canary;
529 if (le16_to_cpu(e->item) == item) {
530 if (size != NULL)
531 *size = le32_to_cpu(e->size) -
532 le16_to_cpu(e->padding_data);
534 return uncached_entry_to_item(e);
537 e = uncached_entry_next(e);
540 /* Item was not found in the uncached list, search the cached list */
542 e = phdr_to_first_cached_entry(phdr, cacheline);
543 end = phdr_to_last_cached_entry(phdr);
545 while (e > end) {
546 if (e->canary != SMEM_PRIVATE_CANARY)
547 goto invalid_canary;
549 if (le16_to_cpu(e->item) == item) {
550 if (size != NULL)
551 *size = le32_to_cpu(e->size) -
552 le16_to_cpu(e->padding_data);
554 return cached_entry_to_item(e);
557 e = cached_entry_next(e, cacheline);
560 return ERR_PTR(-ENOENT);
562 invalid_canary:
563 dev_err(smem->dev, "Found invalid canary in hosts %d:%d partition\n",
564 phdr->host0, phdr->host1);
566 return ERR_PTR(-EINVAL);
570 * qcom_smem_get() - resolve ptr of size of a smem item
571 * @host: the remote processor, or -1
572 * @item: smem item handle
573 * @size: pointer to be filled out with size of the item
575 * Looks up smem item and returns pointer to it. Size of smem
576 * item is returned in @size.
578 void *qcom_smem_get(unsigned host, unsigned item, size_t *size)
580 struct smem_partition_header *phdr;
581 unsigned long flags;
582 size_t cacheln;
583 int ret;
584 void *ptr = ERR_PTR(-EPROBE_DEFER);
586 if (!__smem)
587 return ptr;
589 if (WARN_ON(item >= __smem->item_count))
590 return ERR_PTR(-EINVAL);
592 ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
593 HWSPINLOCK_TIMEOUT,
594 &flags);
595 if (ret)
596 return ERR_PTR(ret);
598 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
599 phdr = __smem->partitions[host];
600 cacheln = __smem->cacheline[host];
601 ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
602 } else if (__smem->global_partition) {
603 phdr = __smem->global_partition;
604 cacheln = __smem->global_cacheline;
605 ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
606 } else {
607 ptr = qcom_smem_get_global(__smem, item, size);
610 hwspin_unlock_irqrestore(__smem->hwlock, &flags);
612 return ptr;
615 EXPORT_SYMBOL(qcom_smem_get);
618 * qcom_smem_get_free_space() - retrieve amount of free space in a partition
619 * @host: the remote processor identifying a partition, or -1
621 * To be used by smem clients as a quick way to determine if any new
622 * allocations has been made.
624 int qcom_smem_get_free_space(unsigned host)
626 struct smem_partition_header *phdr;
627 struct smem_header *header;
628 unsigned ret;
630 if (!__smem)
631 return -EPROBE_DEFER;
633 if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
634 phdr = __smem->partitions[host];
635 ret = le32_to_cpu(phdr->offset_free_cached) -
636 le32_to_cpu(phdr->offset_free_uncached);
637 } else if (__smem->global_partition) {
638 phdr = __smem->global_partition;
639 ret = le32_to_cpu(phdr->offset_free_cached) -
640 le32_to_cpu(phdr->offset_free_uncached);
641 } else {
642 header = __smem->regions[0].virt_base;
643 ret = le32_to_cpu(header->available);
646 return ret;
648 EXPORT_SYMBOL(qcom_smem_get_free_space);
650 static int qcom_smem_get_sbl_version(struct qcom_smem *smem)
652 struct smem_header *header;
653 __le32 *versions;
655 header = smem->regions[0].virt_base;
656 versions = header->version;
658 return le32_to_cpu(versions[SMEM_MASTER_SBL_VERSION_INDEX]);
661 static struct smem_ptable *qcom_smem_get_ptable(struct qcom_smem *smem)
663 struct smem_ptable *ptable;
664 u32 version;
666 ptable = smem->regions[0].virt_base + smem->regions[0].size - SZ_4K;
667 if (memcmp(ptable->magic, SMEM_PTABLE_MAGIC, sizeof(ptable->magic)))
668 return ERR_PTR(-ENOENT);
670 version = le32_to_cpu(ptable->version);
671 if (version != 1) {
672 dev_err(smem->dev,
673 "Unsupported partition header version %d\n", version);
674 return ERR_PTR(-EINVAL);
676 return ptable;
679 static u32 qcom_smem_get_item_count(struct qcom_smem *smem)
681 struct smem_ptable *ptable;
682 struct smem_info *info;
684 ptable = qcom_smem_get_ptable(smem);
685 if (IS_ERR_OR_NULL(ptable))
686 return SMEM_ITEM_COUNT;
688 info = (struct smem_info *)&ptable->entry[ptable->num_entries];
689 if (memcmp(info->magic, SMEM_INFO_MAGIC, sizeof(info->magic)))
690 return SMEM_ITEM_COUNT;
692 return le16_to_cpu(info->num_items);
695 static int qcom_smem_set_global_partition(struct qcom_smem *smem)
697 struct smem_partition_header *header;
698 struct smem_ptable_entry *entry = NULL;
699 struct smem_ptable *ptable;
700 u32 host0, host1, size;
701 int i;
703 ptable = qcom_smem_get_ptable(smem);
704 if (IS_ERR(ptable))
705 return PTR_ERR(ptable);
707 for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
708 entry = &ptable->entry[i];
709 host0 = le16_to_cpu(entry->host0);
710 host1 = le16_to_cpu(entry->host1);
712 if (host0 == SMEM_GLOBAL_HOST && host0 == host1)
713 break;
716 if (!entry) {
717 dev_err(smem->dev, "Missing entry for global partition\n");
718 return -EINVAL;
721 if (!le32_to_cpu(entry->offset) || !le32_to_cpu(entry->size)) {
722 dev_err(smem->dev, "Invalid entry for global partition\n");
723 return -EINVAL;
726 if (smem->global_partition) {
727 dev_err(smem->dev, "Already found the global partition\n");
728 return -EINVAL;
731 header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
732 host0 = le16_to_cpu(header->host0);
733 host1 = le16_to_cpu(header->host1);
735 if (memcmp(header->magic, SMEM_PART_MAGIC, sizeof(header->magic))) {
736 dev_err(smem->dev, "Global partition has invalid magic\n");
737 return -EINVAL;
740 if (host0 != SMEM_GLOBAL_HOST && host1 != SMEM_GLOBAL_HOST) {
741 dev_err(smem->dev, "Global partition hosts are invalid\n");
742 return -EINVAL;
745 if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
746 dev_err(smem->dev, "Global partition has invalid size\n");
747 return -EINVAL;
750 size = le32_to_cpu(header->offset_free_uncached);
751 if (size > le32_to_cpu(header->size)) {
752 dev_err(smem->dev,
753 "Global partition has invalid free pointer\n");
754 return -EINVAL;
757 smem->global_partition = header;
758 smem->global_cacheline = le32_to_cpu(entry->cacheline);
760 return 0;
763 static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
764 unsigned int local_host)
766 struct smem_partition_header *header;
767 struct smem_ptable_entry *entry;
768 struct smem_ptable *ptable;
769 unsigned int remote_host;
770 u32 host0, host1;
771 int i;
773 ptable = qcom_smem_get_ptable(smem);
774 if (IS_ERR(ptable))
775 return PTR_ERR(ptable);
777 for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
778 entry = &ptable->entry[i];
779 host0 = le16_to_cpu(entry->host0);
780 host1 = le16_to_cpu(entry->host1);
782 if (host0 != local_host && host1 != local_host)
783 continue;
785 if (!le32_to_cpu(entry->offset))
786 continue;
788 if (!le32_to_cpu(entry->size))
789 continue;
791 if (host0 == local_host)
792 remote_host = host1;
793 else
794 remote_host = host0;
796 if (remote_host >= SMEM_HOST_COUNT) {
797 dev_err(smem->dev,
798 "Invalid remote host %d\n",
799 remote_host);
800 return -EINVAL;
803 if (smem->partitions[remote_host]) {
804 dev_err(smem->dev,
805 "Already found a partition for host %d\n",
806 remote_host);
807 return -EINVAL;
810 header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
811 host0 = le16_to_cpu(header->host0);
812 host1 = le16_to_cpu(header->host1);
814 if (memcmp(header->magic, SMEM_PART_MAGIC,
815 sizeof(header->magic))) {
816 dev_err(smem->dev,
817 "Partition %d has invalid magic\n", i);
818 return -EINVAL;
821 if (host0 != local_host && host1 != local_host) {
822 dev_err(smem->dev,
823 "Partition %d hosts are invalid\n", i);
824 return -EINVAL;
827 if (host0 != remote_host && host1 != remote_host) {
828 dev_err(smem->dev,
829 "Partition %d hosts are invalid\n", i);
830 return -EINVAL;
833 if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
834 dev_err(smem->dev,
835 "Partition %d has invalid size\n", i);
836 return -EINVAL;
839 if (le32_to_cpu(header->offset_free_uncached) > le32_to_cpu(header->size)) {
840 dev_err(smem->dev,
841 "Partition %d has invalid free pointer\n", i);
842 return -EINVAL;
845 smem->partitions[remote_host] = header;
846 smem->cacheline[remote_host] = le32_to_cpu(entry->cacheline);
849 return 0;
852 static int qcom_smem_map_memory(struct qcom_smem *smem, struct device *dev,
853 const char *name, int i)
855 struct device_node *np;
856 struct resource r;
857 int ret;
859 np = of_parse_phandle(dev->of_node, name, 0);
860 if (!np) {
861 dev_err(dev, "No %s specified\n", name);
862 return -EINVAL;
865 ret = of_address_to_resource(np, 0, &r);
866 of_node_put(np);
867 if (ret)
868 return ret;
870 smem->regions[i].aux_base = (u32)r.start;
871 smem->regions[i].size = resource_size(&r);
872 smem->regions[i].virt_base = devm_ioremap_wc(dev, r.start, resource_size(&r));
873 if (!smem->regions[i].virt_base)
874 return -ENOMEM;
876 return 0;
879 static int qcom_smem_probe(struct platform_device *pdev)
881 struct smem_header *header;
882 struct qcom_smem *smem;
883 size_t array_size;
884 int num_regions;
885 int hwlock_id;
886 u32 version;
887 int ret;
889 num_regions = 1;
890 if (of_find_property(pdev->dev.of_node, "qcom,rpm-msg-ram", NULL))
891 num_regions++;
893 array_size = num_regions * sizeof(struct smem_region);
894 smem = devm_kzalloc(&pdev->dev, sizeof(*smem) + array_size, GFP_KERNEL);
895 if (!smem)
896 return -ENOMEM;
898 smem->dev = &pdev->dev;
899 smem->num_regions = num_regions;
901 ret = qcom_smem_map_memory(smem, &pdev->dev, "memory-region", 0);
902 if (ret)
903 return ret;
905 if (num_regions > 1 && (ret = qcom_smem_map_memory(smem, &pdev->dev,
906 "qcom,rpm-msg-ram", 1)))
907 return ret;
909 header = smem->regions[0].virt_base;
910 if (le32_to_cpu(header->initialized) != 1 ||
911 le32_to_cpu(header->reserved)) {
912 dev_err(&pdev->dev, "SMEM is not initialized by SBL\n");
913 return -EINVAL;
916 version = qcom_smem_get_sbl_version(smem);
917 switch (version >> 16) {
918 case SMEM_GLOBAL_PART_VERSION:
919 ret = qcom_smem_set_global_partition(smem);
920 if (ret < 0)
921 return ret;
922 smem->item_count = qcom_smem_get_item_count(smem);
923 break;
924 case SMEM_GLOBAL_HEAP_VERSION:
925 smem->item_count = SMEM_ITEM_COUNT;
926 break;
927 default:
928 dev_err(&pdev->dev, "Unsupported SMEM version 0x%x\n", version);
929 return -EINVAL;
932 ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
933 if (ret < 0 && ret != -ENOENT)
934 return ret;
936 hwlock_id = of_hwspin_lock_get_id(pdev->dev.of_node, 0);
937 if (hwlock_id < 0) {
938 if (hwlock_id != -EPROBE_DEFER)
939 dev_err(&pdev->dev, "failed to retrieve hwlock\n");
940 return hwlock_id;
943 smem->hwlock = hwspin_lock_request_specific(hwlock_id);
944 if (!smem->hwlock)
945 return -ENXIO;
947 __smem = smem;
949 return 0;
952 static int qcom_smem_remove(struct platform_device *pdev)
954 hwspin_lock_free(__smem->hwlock);
955 __smem = NULL;
957 return 0;
960 static const struct of_device_id qcom_smem_of_match[] = {
961 { .compatible = "qcom,smem" },
964 MODULE_DEVICE_TABLE(of, qcom_smem_of_match);
966 static struct platform_driver qcom_smem_driver = {
967 .probe = qcom_smem_probe,
968 .remove = qcom_smem_remove,
969 .driver = {
970 .name = "qcom-smem",
971 .of_match_table = qcom_smem_of_match,
972 .suppress_bind_attrs = true,
976 static int __init qcom_smem_init(void)
978 return platform_driver_register(&qcom_smem_driver);
980 arch_initcall(qcom_smem_init);
982 static void __exit qcom_smem_exit(void)
984 platform_driver_unregister(&qcom_smem_driver);
986 module_exit(qcom_smem_exit)
988 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
989 MODULE_DESCRIPTION("Qualcomm Shared Memory Manager");
990 MODULE_LICENSE("GPL v2");