711ce626aae5d282e3d2fab3632a7d9a79e4a7c8
[oweals/u-boot.git] / drivers / smem / msm_smem.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
6  */
7
8 #include <common.h>
9 #include <errno.h>
10 #include <dm.h>
11 #include <dm/devres.h>
12 #include <dm/of_access.h>
13 #include <dm/of_addr.h>
14 #include <asm/io.h>
15 #include <linux/err.h>
16 #include <linux/ioport.h>
17 #include <linux/io.h>
18 #include <smem.h>
19
20 DECLARE_GLOBAL_DATA_PTR;
21
22 /*
23  * The Qualcomm shared memory system is an allocate-only heap structure that
24  * consists of one of more memory areas that can be accessed by the processors
25  * in the SoC.
26  *
27  * All systems contains a global heap, accessible by all processors in the SoC,
28  * with a table of contents data structure (@smem_header) at the beginning of
29  * the main shared memory block.
30  *
31  * The global header contains meta data for allocations as well as a fixed list
32  * of 512 entries (@smem_global_entry) that can be initialized to reference
33  * parts of the shared memory space.
34  *
35  *
36  * In addition to this global heap, a set of "private" heaps can be set up at
37  * boot time with access restrictions so that only certain processor pairs can
38  * access the data.
39  *
40  * These partitions are referenced from an optional partition table
41  * (@smem_ptable), that is found 4kB from the end of the main smem region. The
42  * partition table entries (@smem_ptable_entry) lists the involved processors
43  * (or hosts) and their location in the main shared memory region.
44  *
45  * Each partition starts with a header (@smem_partition_header) that identifies
46  * the partition and holds properties for the two internal memory regions. The
47  * two regions are cached and non-cached memory respectively. Each region
48  * contain a link list of allocation headers (@smem_private_entry) followed by
49  * their data.
50  *
51  * Items in the non-cached region are allocated from the start of the partition
52  * while items in the cached region are allocated from the end. The free area
53  * is hence the region between the cached and non-cached offsets. The header of
54  * cached items comes after the data.
55  *
56  * Version 12 (SMEM_GLOBAL_PART_VERSION) changes the item alloc/get procedure
57  * for the global heap. A new global partition is created from the global heap
58  * region with partition type (SMEM_GLOBAL_HOST) and the max smem item count is
59  * set by the bootloader.
60  *
61  */
62
63 /*
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.
67  */
68 #define SMEM_MASTER_SBL_VERSION_INDEX   7
69 #define SMEM_GLOBAL_HEAP_VERSION        11
70 #define SMEM_GLOBAL_PART_VERSION        12
71
72 /*
73  * The first 8 items are only to be allocated by the boot loader while
74  * initializing the heap.
75  */
76 #define SMEM_ITEM_LAST_FIXED    8
77
78 /* Highest accepted item number, for both global and private heaps */
79 #define SMEM_ITEM_COUNT         512
80
81 /* Processor/host identifier for the application processor */
82 #define SMEM_HOST_APPS          0
83
84 /* Processor/host identifier for the global partition */
85 #define SMEM_GLOBAL_HOST        0xfffe
86
87 /* Max number of processors/hosts in a system */
88 #define SMEM_HOST_COUNT         10
89
90 /**
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
95  */
96 struct smem_proc_comm {
97         __le32 command;
98         __le32 status;
99         __le32 params[2];
100 };
101
102 /**
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
109  */
110 struct smem_global_entry {
111         __le32 allocated;
112         __le32 offset;
113         __le32 size;
114         __le32 aux_base; /* bits 1:0 reserved */
115 };
116 #define AUX_BASE_MASK           0xfffffffc
117
118 /**
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
127  */
128 struct smem_header {
129         struct smem_proc_comm proc_comm[4];
130         __le32 version[32];
131         __le32 initialized;
132         __le32 free_offset;
133         __le32 available;
134         __le32 reserved;
135         struct smem_global_entry toc[SMEM_ITEM_COUNT];
136 };
137
138 /**
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
147  */
148 struct smem_ptable_entry {
149         __le32 offset;
150         __le32 size;
151         __le32 flags;
152         __le16 host0;
153         __le16 host1;
154         __le32 cacheline;
155         __le32 reserved[7];
156 };
157
158 /**
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
165  */
166 struct smem_ptable {
167         u8 magic[4];
168         __le32 version;
169         __le32 num_entries;
170         __le32 reserved[5];
171         struct smem_ptable_entry entry[];
172 };
173
174 static const u8 SMEM_PTABLE_MAGIC[] = { 0x24, 0x54, 0x4f, 0x43 }; /* "$TOC" */
175
176 /**
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
183  *              this partition
184  * @offset_free_cached: offset to the first free byte of cached memory in this
185  *              partition
186  * @reserved:   for now reserved entries
187  */
188 struct smem_partition_header {
189         u8 magic[4];
190         __le16 host0;
191         __le16 host1;
192         __le32 size;
193         __le32 offset_free_uncached;
194         __le32 offset_free_cached;
195         __le32 reserved[3];
196 };
197
198 static const u8 SMEM_PART_MAGIC[] = { 0x24, 0x50, 0x52, 0x54 };
199
200 /**
201  * struct smem_private_entry - header of each item in the private partition
202  * @canary:     magic number, must be SMEM_PRIVATE_CANARY
203  * @item:       identifying number of the smem item
204  * @size:       size of the data, including padding bytes
205  * @padding_data: number of bytes of padding of data
206  * @padding_hdr: number of bytes of padding between the header and the data
207  * @reserved:   for now reserved entry
208  */
209 struct smem_private_entry {
210         u16 canary; /* bytes are the same so no swapping needed */
211         __le16 item;
212         __le32 size; /* includes padding bytes */
213         __le16 padding_data;
214         __le16 padding_hdr;
215         __le32 reserved;
216 };
217 #define SMEM_PRIVATE_CANARY     0xa5a5
218
219 /**
220  * struct smem_info - smem region info located after the table of contents
221  * @magic:      magic number, must be SMEM_INFO_MAGIC
222  * @size:       size of the smem region
223  * @base_addr:  base address of the smem region
224  * @reserved:   for now reserved entry
225  * @num_items:  highest accepted item number
226  */
227 struct smem_info {
228         u8 magic[4];
229         __le32 size;
230         __le32 base_addr;
231         __le32 reserved;
232         __le16 num_items;
233 };
234
235 static const u8 SMEM_INFO_MAGIC[] = { 0x53, 0x49, 0x49, 0x49 }; /* SIII */
236
237 /**
238  * struct smem_region - representation of a chunk of memory used for smem
239  * @aux_base:   identifier of aux_mem base
240  * @virt_base:  virtual base address of memory with this aux_mem identifier
241  * @size:       size of the memory region
242  */
243 struct smem_region {
244         u32 aux_base;
245         void __iomem *virt_base;
246         size_t size;
247 };
248
249 /**
250  * struct qcom_smem - device data for the smem device
251  * @dev:        device pointer
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  * @num_regions: number of @regions
259  * @regions:    list of the memory regions defining the shared memory
260  */
261 struct qcom_smem {
262         struct udevice *dev;
263
264         struct smem_partition_header *global_partition;
265         size_t global_cacheline;
266         struct smem_partition_header *partitions[SMEM_HOST_COUNT];
267         size_t cacheline[SMEM_HOST_COUNT];
268         u32 item_count;
269
270         unsigned int num_regions;
271         struct smem_region regions[0];
272 };
273
274 static struct smem_private_entry *
275 phdr_to_last_uncached_entry(struct smem_partition_header *phdr)
276 {
277         void *p = phdr;
278
279         return p + le32_to_cpu(phdr->offset_free_uncached);
280 }
281
282 static void *phdr_to_first_cached_entry(struct smem_partition_header *phdr,
283                                         size_t cacheline)
284 {
285         void *p = phdr;
286
287         return p + le32_to_cpu(phdr->size) - ALIGN(sizeof(*phdr), cacheline);
288 }
289
290 static void *phdr_to_last_cached_entry(struct smem_partition_header *phdr)
291 {
292         void *p = phdr;
293
294         return p + le32_to_cpu(phdr->offset_free_cached);
295 }
296
297 static struct smem_private_entry *
298 phdr_to_first_uncached_entry(struct smem_partition_header *phdr)
299 {
300         void *p = phdr;
301
302         return p + sizeof(*phdr);
303 }
304
305 static struct smem_private_entry *
306 uncached_entry_next(struct smem_private_entry *e)
307 {
308         void *p = e;
309
310         return p + sizeof(*e) + le16_to_cpu(e->padding_hdr) +
311                le32_to_cpu(e->size);
312 }
313
314 static struct smem_private_entry *
315 cached_entry_next(struct smem_private_entry *e, size_t cacheline)
316 {
317         void *p = e;
318
319         return p - le32_to_cpu(e->size) - ALIGN(sizeof(*e), cacheline);
320 }
321
322 static void *uncached_entry_to_item(struct smem_private_entry *e)
323 {
324         void *p = e;
325
326         return p + sizeof(*e) + le16_to_cpu(e->padding_hdr);
327 }
328
329 static void *cached_entry_to_item(struct smem_private_entry *e)
330 {
331         void *p = e;
332
333         return p - le32_to_cpu(e->size);
334 }
335
336 /* Pointer to the one and only smem handle */
337 static struct qcom_smem *__smem;
338
339 static int qcom_smem_alloc_private(struct qcom_smem *smem,
340                                    struct smem_partition_header *phdr,
341                                    unsigned int item,
342                                    size_t size)
343 {
344         struct smem_private_entry *hdr, *end;
345         size_t alloc_size;
346         void *cached;
347
348         hdr = phdr_to_first_uncached_entry(phdr);
349         end = phdr_to_last_uncached_entry(phdr);
350         cached = phdr_to_last_cached_entry(phdr);
351
352         while (hdr < end) {
353                 if (hdr->canary != SMEM_PRIVATE_CANARY) {
354                         dev_err(smem->dev,
355                                 "Found invalid canary in hosts %d:%d partition\n",
356                                 phdr->host0, phdr->host1);
357                         return -EINVAL;
358                 }
359
360                 if (le16_to_cpu(hdr->item) == item)
361                         return -EEXIST;
362
363                 hdr = uncached_entry_next(hdr);
364         }
365
366         /* Check that we don't grow into the cached region */
367         alloc_size = sizeof(*hdr) + ALIGN(size, 8);
368         if ((void *)hdr + alloc_size >= cached) {
369                 dev_err(smem->dev, "Out of memory\n");
370                 return -ENOSPC;
371         }
372
373         hdr->canary = SMEM_PRIVATE_CANARY;
374         hdr->item = cpu_to_le16(item);
375         hdr->size = cpu_to_le32(ALIGN(size, 8));
376         hdr->padding_data = cpu_to_le16(le32_to_cpu(hdr->size) - size);
377         hdr->padding_hdr = 0;
378
379         /*
380          * Ensure the header is written before we advance the free offset, so
381          * that remote processors that does not take the remote spinlock still
382          * gets a consistent view of the linked list.
383          */
384         dmb();
385         le32_add_cpu(&phdr->offset_free_uncached, alloc_size);
386
387         return 0;
388 }
389
390 static int qcom_smem_alloc_global(struct qcom_smem *smem,
391                                   unsigned int item,
392                                   size_t size)
393 {
394         struct smem_global_entry *entry;
395         struct smem_header *header;
396
397         header = smem->regions[0].virt_base;
398         entry = &header->toc[item];
399         if (entry->allocated)
400                 return -EEXIST;
401
402         size = ALIGN(size, 8);
403         if (WARN_ON(size > le32_to_cpu(header->available)))
404                 return -ENOMEM;
405
406         entry->offset = header->free_offset;
407         entry->size = cpu_to_le32(size);
408
409         /*
410          * Ensure the header is consistent before we mark the item allocated,
411          * so that remote processors will get a consistent view of the item
412          * even though they do not take the spinlock on read.
413          */
414         dmb();
415         entry->allocated = cpu_to_le32(1);
416
417         le32_add_cpu(&header->free_offset, size);
418         le32_add_cpu(&header->available, -size);
419
420         return 0;
421 }
422
423 /**
424  * qcom_smem_alloc() - allocate space for a smem item
425  * @host:       remote processor id, or -1
426  * @item:       smem item handle
427  * @size:       number of bytes to be allocated
428  *
429  * Allocate space for a given smem item of size @size, given that the item is
430  * not yet allocated.
431  */
432 static int qcom_smem_alloc(unsigned int host, unsigned int item, size_t size)
433 {
434         struct smem_partition_header *phdr;
435         int ret;
436
437         if (!__smem)
438                 return -EPROBE_DEFER;
439
440         if (item < SMEM_ITEM_LAST_FIXED) {
441                 dev_err(__smem->dev,
442                         "Rejecting allocation of static entry %d\n", item);
443                 return -EINVAL;
444         }
445
446         if (WARN_ON(item >= __smem->item_count))
447                 return -EINVAL;
448
449         if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
450                 phdr = __smem->partitions[host];
451                 ret = qcom_smem_alloc_private(__smem, phdr, item, size);
452         } else if (__smem->global_partition) {
453                 phdr = __smem->global_partition;
454                 ret = qcom_smem_alloc_private(__smem, phdr, item, size);
455         } else {
456                 ret = qcom_smem_alloc_global(__smem, item, size);
457         }
458
459         return ret;
460 }
461
462 static void *qcom_smem_get_global(struct qcom_smem *smem,
463                                   unsigned int item,
464                                   size_t *size)
465 {
466         struct smem_header *header;
467         struct smem_region *area;
468         struct smem_global_entry *entry;
469         u32 aux_base;
470         unsigned int i;
471
472         header = smem->regions[0].virt_base;
473         entry = &header->toc[item];
474         if (!entry->allocated)
475                 return ERR_PTR(-ENXIO);
476
477         aux_base = le32_to_cpu(entry->aux_base) & AUX_BASE_MASK;
478
479         for (i = 0; i < smem->num_regions; i++) {
480                 area = &smem->regions[i];
481
482                 if (area->aux_base == aux_base || !aux_base) {
483                         if (size != NULL)
484                                 *size = le32_to_cpu(entry->size);
485                         return area->virt_base + le32_to_cpu(entry->offset);
486                 }
487         }
488
489         return ERR_PTR(-ENOENT);
490 }
491
492 static void *qcom_smem_get_private(struct qcom_smem *smem,
493                                    struct smem_partition_header *phdr,
494                                    size_t cacheline,
495                                    unsigned int item,
496                                    size_t *size)
497 {
498         struct smem_private_entry *e, *end;
499
500         e = phdr_to_first_uncached_entry(phdr);
501         end = phdr_to_last_uncached_entry(phdr);
502
503         while (e < end) {
504                 if (e->canary != SMEM_PRIVATE_CANARY)
505                         goto invalid_canary;
506
507                 if (le16_to_cpu(e->item) == item) {
508                         if (size != NULL)
509                                 *size = le32_to_cpu(e->size) -
510                                         le16_to_cpu(e->padding_data);
511
512                         return uncached_entry_to_item(e);
513                 }
514
515                 e = uncached_entry_next(e);
516         }
517
518         /* Item was not found in the uncached list, search the cached list */
519
520         e = phdr_to_first_cached_entry(phdr, cacheline);
521         end = phdr_to_last_cached_entry(phdr);
522
523         while (e > end) {
524                 if (e->canary != SMEM_PRIVATE_CANARY)
525                         goto invalid_canary;
526
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);
531
532                         return cached_entry_to_item(e);
533                 }
534
535                 e = cached_entry_next(e, cacheline);
536         }
537
538         return ERR_PTR(-ENOENT);
539
540 invalid_canary:
541         dev_err(smem->dev, "Found invalid canary in hosts %d:%d partition\n",
542                         phdr->host0, phdr->host1);
543
544         return ERR_PTR(-EINVAL);
545 }
546
547 /**
548  * qcom_smem_get() - resolve ptr of size of a smem item
549  * @host:       the remote processor, or -1
550  * @item:       smem item handle
551  * @size:       pointer to be filled out with size of the item
552  *
553  * Looks up smem item and returns pointer to it. Size of smem
554  * item is returned in @size.
555  */
556 static void *qcom_smem_get(unsigned int host, unsigned int item, size_t *size)
557 {
558         struct smem_partition_header *phdr;
559         size_t cacheln;
560         void *ptr = ERR_PTR(-EPROBE_DEFER);
561
562         if (!__smem)
563                 return ptr;
564
565         if (WARN_ON(item >= __smem->item_count))
566                 return ERR_PTR(-EINVAL);
567
568         if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
569                 phdr = __smem->partitions[host];
570                 cacheln = __smem->cacheline[host];
571                 ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
572         } else if (__smem->global_partition) {
573                 phdr = __smem->global_partition;
574                 cacheln = __smem->global_cacheline;
575                 ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
576         } else {
577                 ptr = qcom_smem_get_global(__smem, item, size);
578         }
579
580         return ptr;
581
582 }
583
584 /**
585  * qcom_smem_get_free_space() - retrieve amount of free space in a partition
586  * @host:       the remote processor identifying a partition, or -1
587  *
588  * To be used by smem clients as a quick way to determine if any new
589  * allocations has been made.
590  */
591 static int qcom_smem_get_free_space(unsigned int host)
592 {
593         struct smem_partition_header *phdr;
594         struct smem_header *header;
595         unsigned int ret;
596
597         if (!__smem)
598                 return -EPROBE_DEFER;
599
600         if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
601                 phdr = __smem->partitions[host];
602                 ret = le32_to_cpu(phdr->offset_free_cached) -
603                       le32_to_cpu(phdr->offset_free_uncached);
604         } else if (__smem->global_partition) {
605                 phdr = __smem->global_partition;
606                 ret = le32_to_cpu(phdr->offset_free_cached) -
607                       le32_to_cpu(phdr->offset_free_uncached);
608         } else {
609                 header = __smem->regions[0].virt_base;
610                 ret = le32_to_cpu(header->available);
611         }
612
613         return ret;
614 }
615
616 static int qcom_smem_get_sbl_version(struct qcom_smem *smem)
617 {
618         struct smem_header *header;
619         __le32 *versions;
620
621         header = smem->regions[0].virt_base;
622         versions = header->version;
623
624         return le32_to_cpu(versions[SMEM_MASTER_SBL_VERSION_INDEX]);
625 }
626
627 static struct smem_ptable *qcom_smem_get_ptable(struct qcom_smem *smem)
628 {
629         struct smem_ptable *ptable;
630         u32 version;
631
632         ptable = smem->regions[0].virt_base + smem->regions[0].size - SZ_4K;
633         if (memcmp(ptable->magic, SMEM_PTABLE_MAGIC, sizeof(ptable->magic)))
634                 return ERR_PTR(-ENOENT);
635
636         version = le32_to_cpu(ptable->version);
637         if (version != 1) {
638                 dev_err(smem->dev,
639                         "Unsupported partition header version %d\n", version);
640                 return ERR_PTR(-EINVAL);
641         }
642         return ptable;
643 }
644
645 static u32 qcom_smem_get_item_count(struct qcom_smem *smem)
646 {
647         struct smem_ptable *ptable;
648         struct smem_info *info;
649
650         ptable = qcom_smem_get_ptable(smem);
651         if (IS_ERR_OR_NULL(ptable))
652                 return SMEM_ITEM_COUNT;
653
654         info = (struct smem_info *)&ptable->entry[ptable->num_entries];
655         if (memcmp(info->magic, SMEM_INFO_MAGIC, sizeof(info->magic)))
656                 return SMEM_ITEM_COUNT;
657
658         return le16_to_cpu(info->num_items);
659 }
660
661 static int qcom_smem_set_global_partition(struct qcom_smem *smem)
662 {
663         struct smem_partition_header *header;
664         struct smem_ptable_entry *entry = NULL;
665         struct smem_ptable *ptable;
666         u32 host0, host1, size;
667         int i;
668
669         ptable = qcom_smem_get_ptable(smem);
670         if (IS_ERR(ptable))
671                 return PTR_ERR(ptable);
672
673         for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
674                 entry = &ptable->entry[i];
675                 host0 = le16_to_cpu(entry->host0);
676                 host1 = le16_to_cpu(entry->host1);
677
678                 if (host0 == SMEM_GLOBAL_HOST && host0 == host1)
679                         break;
680         }
681
682         if (!entry) {
683                 dev_err(smem->dev, "Missing entry for global partition\n");
684                 return -EINVAL;
685         }
686
687         if (!le32_to_cpu(entry->offset) || !le32_to_cpu(entry->size)) {
688                 dev_err(smem->dev, "Invalid entry for global partition\n");
689                 return -EINVAL;
690         }
691
692         if (smem->global_partition) {
693                 dev_err(smem->dev, "Already found the global partition\n");
694                 return -EINVAL;
695         }
696
697         header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
698         host0 = le16_to_cpu(header->host0);
699         host1 = le16_to_cpu(header->host1);
700
701         if (memcmp(header->magic, SMEM_PART_MAGIC, sizeof(header->magic))) {
702                 dev_err(smem->dev, "Global partition has invalid magic\n");
703                 return -EINVAL;
704         }
705
706         if (host0 != SMEM_GLOBAL_HOST && host1 != SMEM_GLOBAL_HOST) {
707                 dev_err(smem->dev, "Global partition hosts are invalid\n");
708                 return -EINVAL;
709         }
710
711         if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
712                 dev_err(smem->dev, "Global partition has invalid size\n");
713                 return -EINVAL;
714         }
715
716         size = le32_to_cpu(header->offset_free_uncached);
717         if (size > le32_to_cpu(header->size)) {
718                 dev_err(smem->dev,
719                         "Global partition has invalid free pointer\n");
720                 return -EINVAL;
721         }
722
723         smem->global_partition = header;
724         smem->global_cacheline = le32_to_cpu(entry->cacheline);
725
726         return 0;
727 }
728
729 static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
730                                           unsigned int local_host)
731 {
732         struct smem_partition_header *header;
733         struct smem_ptable_entry *entry;
734         struct smem_ptable *ptable;
735         unsigned int remote_host;
736         u32 host0, host1;
737         int i;
738
739         ptable = qcom_smem_get_ptable(smem);
740         if (IS_ERR(ptable))
741                 return PTR_ERR(ptable);
742
743         for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
744                 entry = &ptable->entry[i];
745                 host0 = le16_to_cpu(entry->host0);
746                 host1 = le16_to_cpu(entry->host1);
747
748                 if (host0 != local_host && host1 != local_host)
749                         continue;
750
751                 if (!le32_to_cpu(entry->offset))
752                         continue;
753
754                 if (!le32_to_cpu(entry->size))
755                         continue;
756
757                 if (host0 == local_host)
758                         remote_host = host1;
759                 else
760                         remote_host = host0;
761
762                 if (remote_host >= SMEM_HOST_COUNT) {
763                         dev_err(smem->dev,
764                                 "Invalid remote host %d\n",
765                                 remote_host);
766                         return -EINVAL;
767                 }
768
769                 if (smem->partitions[remote_host]) {
770                         dev_err(smem->dev,
771                                 "Already found a partition for host %d\n",
772                                 remote_host);
773                         return -EINVAL;
774                 }
775
776                 header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
777                 host0 = le16_to_cpu(header->host0);
778                 host1 = le16_to_cpu(header->host1);
779
780                 if (memcmp(header->magic, SMEM_PART_MAGIC,
781                             sizeof(header->magic))) {
782                         dev_err(smem->dev,
783                                 "Partition %d has invalid magic\n", i);
784                         return -EINVAL;
785                 }
786
787                 if (host0 != local_host && host1 != local_host) {
788                         dev_err(smem->dev,
789                                 "Partition %d hosts are invalid\n", i);
790                         return -EINVAL;
791                 }
792
793                 if (host0 != remote_host && host1 != remote_host) {
794                         dev_err(smem->dev,
795                                 "Partition %d hosts are invalid\n", i);
796                         return -EINVAL;
797                 }
798
799                 if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
800                         dev_err(smem->dev,
801                                 "Partition %d has invalid size\n", i);
802                         return -EINVAL;
803                 }
804
805                 if (le32_to_cpu(header->offset_free_uncached) > le32_to_cpu(header->size)) {
806                         dev_err(smem->dev,
807                                 "Partition %d has invalid free pointer\n", i);
808                         return -EINVAL;
809                 }
810
811                 smem->partitions[remote_host] = header;
812                 smem->cacheline[remote_host] = le32_to_cpu(entry->cacheline);
813         }
814
815         return 0;
816 }
817
818 static int qcom_smem_map_memory(struct qcom_smem *smem, struct udevice *dev,
819                                 const char *name, int i)
820 {
821         struct fdt_resource r;
822         int ret;
823         int node = dev_of_offset(dev);
824
825         ret = fdtdec_lookup_phandle(gd->fdt_blob, node, name);
826         if (ret < 0) {
827                 dev_err(dev, "No %s specified\n", name);
828                 return -EINVAL;
829         }
830
831         ret = fdt_get_resource(gd->fdt_blob, ret, "reg", 0, &r);
832         if (ret)
833                 return ret;
834
835         smem->regions[i].aux_base = (u32)r.start;
836         smem->regions[i].size = fdt_resource_size(&r);
837         smem->regions[i].virt_base = devm_ioremap(dev, r.start, fdt_resource_size(&r));
838         if (!smem->regions[i].virt_base)
839                 return -ENOMEM;
840
841         return 0;
842 }
843
844 static int qcom_smem_probe(struct udevice *dev)
845 {
846         struct smem_header *header;
847         struct qcom_smem *smem;
848         size_t array_size;
849         int num_regions;
850         u32 version;
851         int ret;
852         int node = dev_of_offset(dev);
853
854         num_regions = 1;
855         if (fdtdec_lookup_phandle(gd->fdt_blob, node, "qcomrpm-msg-ram") >= 0)
856                 num_regions++;
857
858         array_size = num_regions * sizeof(struct smem_region);
859         smem = devm_kzalloc(dev, sizeof(*smem) + array_size, GFP_KERNEL);
860         if (!smem)
861                 return -ENOMEM;
862
863         smem->dev = dev;
864         smem->num_regions = num_regions;
865
866         ret = qcom_smem_map_memory(smem, dev, "memory-region", 0);
867         if (ret)
868                 return ret;
869
870         if (num_regions > 1) {
871                 ret = qcom_smem_map_memory(smem, dev,
872                                         "qcom,rpm-msg-ram", 1);
873                 if (ret)
874                         return ret;
875         }
876
877         header = smem->regions[0].virt_base;
878         if (le32_to_cpu(header->initialized) != 1 ||
879             le32_to_cpu(header->reserved)) {
880                 dev_err(&pdev->dev, "SMEM is not initialized by SBL\n");
881                 return -EINVAL;
882         }
883
884         version = qcom_smem_get_sbl_version(smem);
885         switch (version >> 16) {
886         case SMEM_GLOBAL_PART_VERSION:
887                 ret = qcom_smem_set_global_partition(smem);
888                 if (ret < 0)
889                         return ret;
890                 smem->item_count = qcom_smem_get_item_count(smem);
891                 break;
892         case SMEM_GLOBAL_HEAP_VERSION:
893                 smem->item_count = SMEM_ITEM_COUNT;
894                 break;
895         default:
896                 dev_err(dev, "Unsupported SMEM version 0x%x\n", version);
897                 return -EINVAL;
898         }
899
900         ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
901         if (ret < 0 && ret != -ENOENT)
902                 return ret;
903
904         __smem = smem;
905
906         return 0;
907 }
908
909 static int qcom_smem_remove(struct udevice *dev)
910 {
911         __smem = NULL;
912
913         return 0;
914 }
915
916 const struct udevice_id qcom_smem_of_match[] = {
917         { .compatible = "qcom,smem" },
918         { }
919 };
920
921 static const struct smem_ops msm_smem_ops = {
922         .alloc = qcom_smem_alloc,
923         .get = qcom_smem_get,
924         .get_free_space = qcom_smem_get_free_space,
925 };
926
927 U_BOOT_DRIVER(qcom_smem) = {
928         .name   = "qcom_smem",
929         .id     = UCLASS_SMEM,
930         .of_match = qcom_smem_of_match,
931         .ops = &msm_smem_ops,
932         .probe = qcom_smem_probe,
933         .remove = qcom_smem_remove,
934 };