]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/soc/qcom/smem.c
Merge branch 'tracking-qcomlt-scm' into integration-linux-qcomlt
[karo-tx-linux.git] / drivers / soc / qcom / smem.c
1 /*
2  * Copyright (c) 2015, Sony Mobile Communications AB.
3  * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
4  *
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.
8  *
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.
13  */
14
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>
23 #include <linux/debugfs.h>
24
25 /*
26  * The Qualcomm shared memory system is a allocate only heap structure that
27  * consists of one of more memory areas that can be accessed by the processors
28  * in the SoC.
29  *
30  * All systems contains a global heap, accessible by all processors in the SoC,
31  * with a table of contents data structure (@smem_header) at the beginning of
32  * the main shared memory block.
33  *
34  * The global header contains meta data for allocations as well as a fixed list
35  * of 512 entries (@smem_global_entry) that can be initialized to reference
36  * parts of the shared memory space.
37  *
38  *
39  * In addition to this global heap a set of "private" heaps can be set up at
40  * boot time with access restrictions so that only certain processor pairs can
41  * access the data.
42  *
43  * These partitions are referenced from an optional partition table
44  * (@smem_ptable), that is found 4kB from the end of the main smem region. The
45  * partition table entries (@smem_ptable_entry) lists the involved processors
46  * (or hosts) and their location in the main shared memory region.
47  *
48  * Each partition starts with a header (@smem_partition_header) that identifies
49  * the partition and holds properties for the two internal memory regions. The
50  * two regions are cached and non-cached memory respectively. Each region
51  * contain a link list of allocation headers (@smem_private_entry) followed by
52  * their data.
53  *
54  * Items in the non-cached region are allocated from the start of the partition
55  * while items in the cached region are allocated from the end. The free area
56  * is hence the region between the cached and non-cached offsets.
57  *
58  *
59  * To synchronize allocations in the shared memory heaps a remote spinlock must
60  * be held - currently lock number 3 of the sfpb or tcsr is used for this on all
61  * platforms.
62  *
63  */
64
65 /*
66  * Item 3 of the global heap contains an array of versions for the various
67  * software components in the SoC. We verify that the boot loader version is
68  * what the expected version (SMEM_EXPECTED_VERSION) as a sanity check.
69  */
70 #define SMEM_ITEM_VERSION       3
71 #define  SMEM_MASTER_SBL_VERSION_INDEX  7
72 #define  SMEM_EXPECTED_VERSION          11
73
74 /*
75  * The first 8 items are only to be allocated by the boot loader while
76  * initializing the heap.
77  */
78 #define SMEM_ITEM_LAST_FIXED    8
79
80 /* Highest accepted item number, for both global and private heaps */
81 #define SMEM_ITEM_COUNT         512
82
83 /* Processor/host identifier for the application processor */
84 #define SMEM_HOST_APPS          0
85
86 /* Max number of processors/hosts in a system */
87 #define SMEM_HOST_COUNT         9
88
89 #define SMEM_HEAP_INFO  1
90
91 /**
92   * struct smem_proc_comm - proc_comm communication struct (legacy)
93   * @command:   current command to be executed
94   * @status:    status of the currently requested command
95   * @params:    parameters to the command
96   */
97 struct smem_proc_comm {
98         __le32 command;
99         __le32 status;
100         __le32 params[2];
101 };
102
103 /**
104  * struct smem_global_entry - entry to reference smem items on the heap
105  * @allocated:  boolean to indicate if this entry is used
106  * @offset:     offset to the allocated space
107  * @size:       size of the allocated space, 8 byte aligned
108  * @aux_base:   base address for the memory region used by this unit, or 0 for
109  *              the default region. bits 0,1 are reserved
110  */
111 struct smem_global_entry {
112         __le32 allocated;
113         __le32 offset;
114         __le32 size;
115         __le32 aux_base; /* bits 1:0 reserved */
116 };
117 #define AUX_BASE_MASK           0xfffffffc
118
119 /**
120  * struct smem_header - header found in beginning of primary smem region
121  * @proc_comm:          proc_comm communication interface (legacy)
122  * @version:            array of versions for the various subsystems
123  * @initialized:        boolean to indicate that smem is initialized
124  * @free_offset:        index of the first unallocated byte in smem
125  * @available:          number of bytes available for allocation
126  * @reserved:           reserved field, must be 0
127  * toc:                 array of references to items
128  */
129 struct smem_header {
130         struct smem_proc_comm proc_comm[4];
131         __le32 version[32];
132         __le32 initialized;
133         __le32 free_offset;
134         __le32 available;
135         __le32 reserved;
136         struct smem_global_entry toc[SMEM_ITEM_COUNT];
137 };
138
139 /**
140  * struct smem_ptable_entry - one entry in the @smem_ptable list
141  * @offset:     offset, within the main shared memory region, of the partition
142  * @size:       size of the partition
143  * @flags:      flags for the partition (currently unused)
144  * @host0:      first processor/host with access to this partition
145  * @host1:      second processor/host with access to this partition
146  * @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 reserved[8];
155 };
156
157 /**
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
164  */
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[];
171 };
172
173 static const u8 SMEM_PTABLE_MAGIC[] = { 0x24, 0x54, 0x4f, 0x43 }; /* "$TOC" */
174
175 /**
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
186  */
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];
195 };
196
197 static const u8 SMEM_PART_MAGIC[] = { 0x24, 0x50, 0x52, 0x54 };
198
199 /**
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
207  */
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;
215 };
216 #define SMEM_PRIVATE_CANARY     0xa5a5
217
218 /**
219  * struct smem_region - representation of a chunk of memory used for smem
220  * @aux_base:   identifier of aux_mem base
221  * @virt_base:  virtual base address of memory with this aux_mem identifier
222  * @size:       size of the memory region
223  */
224 struct smem_region {
225         u32 aux_base;
226         void __iomem *virt_base;
227         size_t size;
228 };
229
230 /**
231  * struct qcom_smem - device data for the smem device
232  * @dev:        device pointer
233  * @hwlock:     reference to a hwspinlock
234  * @partitions: list of pointers to partitions affecting the current
235  *              processor/host
236  * @num_regions: number of @regions
237  * @regions:    list of the memory regions defining the shared memory
238  */
239 struct qcom_smem {
240         struct device *dev;
241
242         struct hwspinlock *hwlock;
243
244         struct smem_partition_header *partitions[SMEM_HOST_COUNT];
245
246         struct dentry *dent;
247         u32 version;
248
249         unsigned num_regions;
250         struct smem_region regions[0];
251 };
252
253 static struct smem_private_entry *
254 phdr_to_last_private_entry(struct smem_partition_header *phdr)
255 {
256         void *p = phdr;
257
258         return p + le32_to_cpu(phdr->offset_free_uncached);
259 }
260
261 static void *phdr_to_first_cached_entry(struct smem_partition_header *phdr)
262 {
263         void *p = phdr;
264
265         return p + le32_to_cpu(phdr->offset_free_cached);
266 }
267
268 static struct smem_private_entry *
269 phdr_to_first_private_entry(struct smem_partition_header *phdr)
270 {
271         void *p = phdr;
272
273         return p + sizeof(*phdr);
274 }
275
276 static struct smem_private_entry *
277 private_entry_next(struct smem_private_entry *e)
278 {
279         void *p = e;
280
281         return p + sizeof(*e) + le16_to_cpu(e->padding_hdr) +
282                le32_to_cpu(e->size);
283 }
284
285 static void *entry_to_item(struct smem_private_entry *e)
286 {
287         void *p = e;
288
289         return p + sizeof(*e) + le16_to_cpu(e->padding_hdr);
290 }
291
292 /* Pointer to the one and only smem handle */
293 static struct qcom_smem *__smem;
294
295 /* Timeout (ms) for the trylock of remote spinlocks */
296 #define HWSPINLOCK_TIMEOUT      1000
297
298 static int qcom_smem_alloc_private(struct qcom_smem *smem,
299                                    unsigned host,
300                                    unsigned item,
301                                    size_t size)
302 {
303         struct smem_partition_header *phdr;
304         struct smem_private_entry *hdr, *end;
305         size_t alloc_size;
306         void *cached;
307
308         phdr = smem->partitions[host];
309         hdr = phdr_to_first_private_entry(phdr);
310         end = phdr_to_last_private_entry(phdr);
311         cached = phdr_to_first_cached_entry(phdr);
312
313         while (hdr < end) {
314                 if (hdr->canary != SMEM_PRIVATE_CANARY) {
315                         dev_err(smem->dev,
316                                 "Found invalid canary in host %d partition\n",
317                                 host);
318                         return -EINVAL;
319                 }
320
321                 if (le16_to_cpu(hdr->item) == item)
322                         return -EEXIST;
323
324                 hdr = private_entry_next(hdr);
325         }
326
327         /* Check that we don't grow into the cached region */
328         alloc_size = sizeof(*hdr) + ALIGN(size, 8);
329         if ((void *)hdr + alloc_size >= cached) {
330                 dev_err(smem->dev, "Out of memory\n");
331                 return -ENOSPC;
332         }
333
334         hdr->canary = SMEM_PRIVATE_CANARY;
335         hdr->item = cpu_to_le16(item);
336         hdr->size = cpu_to_le32(ALIGN(size, 8));
337         hdr->padding_data = cpu_to_le16(le32_to_cpu(hdr->size) - size);
338         hdr->padding_hdr = 0;
339
340         /*
341          * Ensure the header is written before we advance the free offset, so
342          * that remote processors that does not take the remote spinlock still
343          * gets a consistent view of the linked list.
344          */
345         wmb();
346         le32_add_cpu(&phdr->offset_free_uncached, alloc_size);
347
348         return 0;
349 }
350
351 static int qcom_smem_alloc_global(struct qcom_smem *smem,
352                                   unsigned item,
353                                   size_t size)
354 {
355         struct smem_header *header;
356         struct smem_global_entry *entry;
357
358         if (WARN_ON(item >= SMEM_ITEM_COUNT))
359                 return -EINVAL;
360
361         header = smem->regions[0].virt_base;
362         entry = &header->toc[item];
363         if (entry->allocated)
364                 return -EEXIST;
365
366         size = ALIGN(size, 8);
367         if (WARN_ON(size > le32_to_cpu(header->available)))
368                 return -ENOMEM;
369
370         entry->offset = header->free_offset;
371         entry->size = cpu_to_le32(size);
372
373         /*
374          * Ensure the header is consistent before we mark the item allocated,
375          * so that remote processors will get a consistent view of the item
376          * even though they do not take the spinlock on read.
377          */
378         wmb();
379         entry->allocated = cpu_to_le32(1);
380
381         le32_add_cpu(&header->free_offset, size);
382         le32_add_cpu(&header->available, -size);
383
384         return 0;
385 }
386
387 /**
388  * qcom_smem_alloc() - allocate space for a smem item
389  * @host:       remote processor id, or -1
390  * @item:       smem item handle
391  * @size:       number of bytes to be allocated
392  *
393  * Allocate space for a given smem item of size @size, given that the item is
394  * not yet allocated.
395  */
396 int qcom_smem_alloc(unsigned host, unsigned item, size_t size)
397 {
398         unsigned long flags;
399         int ret;
400
401         if (!__smem)
402                 return -EPROBE_DEFER;
403
404         if (item < SMEM_ITEM_LAST_FIXED) {
405                 dev_err(__smem->dev,
406                         "Rejecting allocation of static entry %d\n", item);
407                 return -EINVAL;
408         }
409
410         ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
411                                           HWSPINLOCK_TIMEOUT,
412                                           &flags);
413         if (ret)
414                 return ret;
415
416         if (host < SMEM_HOST_COUNT && __smem->partitions[host])
417                 ret = qcom_smem_alloc_private(__smem, host, item, size);
418         else
419                 ret = qcom_smem_alloc_global(__smem, item, size);
420
421         hwspin_unlock_irqrestore(__smem->hwlock, &flags);
422
423         return ret;
424 }
425 EXPORT_SYMBOL(qcom_smem_alloc);
426
427 static void *qcom_smem_get_global(struct qcom_smem *smem,
428                                   unsigned item,
429                                   size_t *size)
430 {
431         struct smem_header *header;
432         struct smem_region *area;
433         struct smem_global_entry *entry;
434         u32 aux_base;
435         unsigned i;
436
437         if (WARN_ON(item >= SMEM_ITEM_COUNT))
438                 return ERR_PTR(-EINVAL);
439
440         header = smem->regions[0].virt_base;
441         entry = &header->toc[item];
442         if (!entry->allocated)
443                 return ERR_PTR(-ENXIO);
444
445         aux_base = le32_to_cpu(entry->aux_base) & AUX_BASE_MASK;
446
447         for (i = 0; i < smem->num_regions; i++) {
448                 area = &smem->regions[i];
449
450                 if (area->aux_base == aux_base || !aux_base) {
451                         if (size != NULL)
452                                 *size = le32_to_cpu(entry->size);
453                         return area->virt_base + le32_to_cpu(entry->offset);
454                 }
455         }
456
457         return ERR_PTR(-ENOENT);
458 }
459
460 static void *qcom_smem_get_private(struct qcom_smem *smem,
461                                    unsigned host,
462                                    unsigned item,
463                                    size_t *size)
464 {
465         struct smem_partition_header *phdr;
466         struct smem_private_entry *e, *end;
467
468         phdr = smem->partitions[host];
469         e = phdr_to_first_private_entry(phdr);
470         end = phdr_to_last_private_entry(phdr);
471
472         while (e < end) {
473                 if (e->canary != SMEM_PRIVATE_CANARY) {
474                         dev_err(smem->dev,
475                                 "Found invalid canary in host %d partition\n",
476                                 host);
477                         return ERR_PTR(-EINVAL);
478                 }
479
480                 if (le16_to_cpu(e->item) == item) {
481                         if (size != NULL)
482                                 *size = le32_to_cpu(e->size) -
483                                         le16_to_cpu(e->padding_data);
484
485                         return entry_to_item(e);
486                 }
487
488                 e = private_entry_next(e);
489         }
490
491         return ERR_PTR(-ENOENT);
492 }
493
494 /**
495  * qcom_smem_get() - resolve ptr of size of a smem item
496  * @host:       the remote processor, or -1
497  * @item:       smem item handle
498  * @size:       pointer to be filled out with size of the item
499  *
500  * Looks up smem item and returns pointer to it. Size of smem
501  * item is returned in @size.
502  */
503 void *qcom_smem_get(unsigned host, unsigned item, size_t *size)
504 {
505         unsigned long flags;
506         int ret;
507         void *ptr = ERR_PTR(-EPROBE_DEFER);
508
509         if (!__smem)
510                 return ptr;
511
512         ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
513                                           HWSPINLOCK_TIMEOUT,
514                                           &flags);
515         if (ret)
516                 return ERR_PTR(ret);
517
518         if (host < SMEM_HOST_COUNT && __smem->partitions[host])
519                 ptr = qcom_smem_get_private(__smem, host, item, size);
520         else
521                 ptr = qcom_smem_get_global(__smem, item, size);
522
523         hwspin_unlock_irqrestore(__smem->hwlock, &flags);
524
525         return ptr;
526
527 }
528 EXPORT_SYMBOL(qcom_smem_get);
529
530 /**
531  * qcom_smem_get_free_space() - retrieve amount of free space in a partition
532  * @host:       the remote processor identifying a partition, or -1
533  *
534  * To be used by smem clients as a quick way to determine if any new
535  * allocations has been made.
536  */
537 int qcom_smem_get_free_space(unsigned host)
538 {
539         struct smem_partition_header *phdr;
540         struct smem_header *header;
541         unsigned ret;
542
543         if (!__smem)
544                 return -EPROBE_DEFER;
545
546         if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
547                 phdr = __smem->partitions[host];
548                 ret = le32_to_cpu(phdr->offset_free_cached) -
549                       le32_to_cpu(phdr->offset_free_uncached);
550         } else {
551                 header = __smem->regions[0].virt_base;
552                 ret = le32_to_cpu(header->available);
553         }
554
555         return ret;
556 }
557 EXPORT_SYMBOL(qcom_smem_get_free_space);
558
559 static int qcom_smem_get_sbl_version(struct qcom_smem *smem)
560 {
561         __le32 *versions;
562         size_t size;
563
564         versions = qcom_smem_get_global(smem, SMEM_ITEM_VERSION, &size);
565         if (IS_ERR(versions)) {
566                 dev_err(smem->dev, "Unable to read the version item\n");
567                 return -ENOENT;
568         }
569
570         if (size < sizeof(unsigned) * SMEM_MASTER_SBL_VERSION_INDEX) {
571                 dev_err(smem->dev, "Version item is too small\n");
572                 return -EINVAL;
573         }
574
575         return le32_to_cpu(versions[SMEM_MASTER_SBL_VERSION_INDEX]);
576 }
577
578 static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
579                                           unsigned local_host)
580 {
581         struct smem_partition_header *header;
582         struct smem_ptable_entry *entry;
583         struct smem_ptable *ptable;
584         unsigned remote_host;
585         u32 version, host0, host1;
586         int i;
587
588         ptable = smem->regions[0].virt_base + smem->regions[0].size - SZ_4K;
589         if (memcmp(ptable->magic, SMEM_PTABLE_MAGIC, sizeof(ptable->magic)))
590                 return 0;
591
592         version = le32_to_cpu(ptable->version);
593         if (version != 1) {
594                 dev_err(smem->dev,
595                         "Unsupported partition header version %d\n", version);
596                 return -EINVAL;
597         }
598
599         for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
600                 entry = &ptable->entry[i];
601                 host0 = le16_to_cpu(entry->host0);
602                 host1 = le16_to_cpu(entry->host1);
603
604                 if (host0 != local_host && host1 != local_host)
605                         continue;
606
607                 if (!le32_to_cpu(entry->offset))
608                         continue;
609
610                 if (!le32_to_cpu(entry->size))
611                         continue;
612
613                 if (host0 == local_host)
614                         remote_host = host1;
615                 else
616                         remote_host = host0;
617
618                 if (remote_host >= SMEM_HOST_COUNT) {
619                         dev_err(smem->dev,
620                                 "Invalid remote host %d\n",
621                                 remote_host);
622                         return -EINVAL;
623                 }
624
625                 if (smem->partitions[remote_host]) {
626                         dev_err(smem->dev,
627                                 "Already found a partition for host %d\n",
628                                 remote_host);
629                         return -EINVAL;
630                 }
631
632                 header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
633                 host0 = le16_to_cpu(header->host0);
634                 host1 = le16_to_cpu(header->host1);
635
636                 if (memcmp(header->magic, SMEM_PART_MAGIC,
637                             sizeof(header->magic))) {
638                         dev_err(smem->dev,
639                                 "Partition %d has invalid magic\n", i);
640                         return -EINVAL;
641                 }
642
643                 if (host0 != local_host && host1 != local_host) {
644                         dev_err(smem->dev,
645                                 "Partition %d hosts are invalid\n", i);
646                         return -EINVAL;
647                 }
648
649                 if (host0 != remote_host && host1 != remote_host) {
650                         dev_err(smem->dev,
651                                 "Partition %d hosts are invalid\n", i);
652                         return -EINVAL;
653                 }
654
655                 if (header->size != entry->size) {
656                         dev_err(smem->dev,
657                                 "Partition %d has invalid size\n", i);
658                         return -EINVAL;
659                 }
660
661                 if (le32_to_cpu(header->offset_free_uncached) > le32_to_cpu(header->size)) {
662                         dev_err(smem->dev,
663                                 "Partition %d has invalid free pointer\n", i);
664                         return -EINVAL;
665                 }
666
667                 smem->partitions[remote_host] = header;
668         }
669
670         return 0;
671 }
672
673 static int qcom_smem_map_memory(struct qcom_smem *smem, struct device *dev,
674                                 const char *name, int i)
675 {
676         struct device_node *np;
677         struct resource r;
678         int ret;
679
680         np = of_parse_phandle(dev->of_node, name, 0);
681         if (!np) {
682                 dev_err(dev, "No %s specified\n", name);
683                 return -EINVAL;
684         }
685
686         ret = of_address_to_resource(np, 0, &r);
687         of_node_put(np);
688         if (ret)
689                 return ret;
690
691         smem->regions[i].aux_base = (u32)r.start;
692         smem->regions[i].size = resource_size(&r);
693         smem->regions[i].virt_base = devm_ioremap_nocache(dev, r.start,
694                                                           resource_size(&r));
695         if (!smem->regions[i].virt_base)
696                 return -ENOMEM;
697
698         return 0;
699 }
700
701 static void smem_debug_read_mem(struct seq_file *s)
702 {
703         u32 *info;
704         size_t size;
705         int ret, i;
706         long flags;
707
708         info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_HEAP_INFO, &size);
709
710         if (IS_ERR(info))
711                 seq_printf(s, "Can't get global heap information pool\n");
712         else {
713                 seq_printf(s, "global heap\n");
714                 seq_printf(s, "   initialized: %d offset: %08x avail: %08x\n",
715                                 info[0], info[1], info[2]);
716
717                 for (i = 0; i < 512; i++) {
718                         info = qcom_smem_get(QCOM_SMEM_HOST_ANY, i, &size);
719                         if (IS_ERR(info))
720                                 continue;
721
722                         seq_printf(s, "      [%d]: p: %p s: %li\n", i, info,
723                                         size);
724                 }
725         }
726
727         seq_printf(s, "\nSecure partitions accessible from APPS:\n");
728
729         ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
730                                           HWSPINLOCK_TIMEOUT,
731                                           &flags);
732
733         for (i = 0; i < SMEM_HOST_COUNT; i++) {
734                 struct smem_partition_header *part_hdr = __smem->partitions[i];
735                 void *p;
736
737                 if (!part_hdr)
738                         continue;
739
740                 if (part_hdr->magic != SMEM_PART_MAGIC) {
741                         seq_printf(s, "   part[%d]: incorrect magic\n", i);
742                         continue;
743                 }
744
745                 seq_printf(s, "   part[%d]: (%d <-> %d) size: %d off: %08x\n",
746                         i, part_hdr->host0, part_hdr->host1, part_hdr->size,
747                         part_hdr->offset_free_uncached);
748
749                 p = (void *)part_hdr + sizeof(*part_hdr);
750                 while (p < (void *)part_hdr + part_hdr->offset_free_uncached) {
751                         struct smem_private_entry *entry = p;
752
753                         seq_printf(s,
754                                 "          [%d]: %s size: %d pd: %d\n",
755                                 entry->item,
756                                 (entry->canary == SMEM_PRIVATE_CANARY) ?
757                                         "valid" : "invalid",
758                                 entry->size,
759                                 entry->padding_data);
760
761                         p += sizeof(*entry) + entry->padding_hdr + entry->size;
762                 }
763         }
764
765         hwspin_unlock_irqrestore(__smem->hwlock, &flags);
766 }
767
768 static void smem_debug_read_version(struct seq_file *s)
769 {
770         seq_printf(s, "SBL version: %08x\n", __smem->version >> 16);
771 }
772
773 static int debugfs_show(struct seq_file *s, void *data)
774 {
775         void (*show)(struct seq_file *) = s->private;
776
777         show(s);
778
779         return 0;
780 }
781
782 static int smem_debug_open(struct inode *inode, struct file *file)
783 {
784         return single_open(file, debugfs_show, inode->i_private);
785 }
786
787
788 static const struct file_operations smem_debug_ops = {
789         .open = smem_debug_open,
790         .release = single_release,
791         .read = seq_read,
792         .llseek = seq_lseek,
793 };
794
795
796
797 static int qcom_smem_probe(struct platform_device *pdev)
798 {
799         struct smem_header *header;
800         struct qcom_smem *smem;
801         size_t array_size;
802         int num_regions;
803         int hwlock_id;
804         int ret;
805
806         num_regions = 1;
807         if (of_find_property(pdev->dev.of_node, "qcom,rpm-msg-ram", NULL))
808                 num_regions++;
809
810         array_size = num_regions * sizeof(struct smem_region);
811         smem = devm_kzalloc(&pdev->dev, sizeof(*smem) + array_size, GFP_KERNEL);
812         if (!smem)
813                 return -ENOMEM;
814
815         smem->dev = &pdev->dev;
816         smem->num_regions = num_regions;
817
818         ret = qcom_smem_map_memory(smem, &pdev->dev, "memory-region", 0);
819         if (ret)
820                 return ret;
821
822         if (num_regions > 1 && (ret = qcom_smem_map_memory(smem, &pdev->dev,
823                                         "qcom,rpm-msg-ram", 1)))
824                 return ret;
825
826         header = smem->regions[0].virt_base;
827         if (le32_to_cpu(header->initialized) != 1 ||
828             le32_to_cpu(header->reserved)) {
829                 dev_err(&pdev->dev, "SMEM is not initialized by SBL\n");
830                 return -EINVAL;
831         }
832
833         smem->version = qcom_smem_get_sbl_version(smem);
834         if (smem->version >> 16 != SMEM_EXPECTED_VERSION) {
835                 dev_err(&pdev->dev, "Unsupported SMEM version 0x%x\n", smem->version);
836                 return -EINVAL;
837         }
838
839         ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
840         if (ret < 0)
841                 return ret;
842
843         hwlock_id = of_hwspin_lock_get_id(pdev->dev.of_node, 0);
844         if (hwlock_id < 0) {
845                 dev_err(&pdev->dev, "failed to retrieve hwlock\n");
846                 return hwlock_id;
847         }
848
849         smem->hwlock = hwspin_lock_request_specific(hwlock_id);
850         if (!smem->hwlock)
851                 return -ENXIO;
852
853         __smem = smem;
854
855         /* setup debugfs information */
856         __smem->dent = debugfs_create_dir("smem", 0);
857         if (IS_ERR(__smem->dent))
858                 dev_info(smem->dev, "unable to create debugfs\n");
859
860         if (!debugfs_create_file("mem", 0444, __smem->dent, smem_debug_read_mem, &smem_debug_ops))
861                 dev_err(smem->dev, "couldnt create mem file\n");
862         if (!debugfs_create_file("version", 0444, __smem->dent, smem_debug_read_version,
863                                         &smem_debug_ops))
864                 dev_err(smem->dev, "couldnt create mem file\n");
865
866         return 0;
867 }
868
869 static int qcom_smem_remove(struct platform_device *pdev)
870 {
871         hwspin_lock_free(__smem->hwlock);
872         __smem = NULL;
873
874         return 0;
875 }
876
877 static const struct of_device_id qcom_smem_of_match[] = {
878         { .compatible = "qcom,smem" },
879         {}
880 };
881 MODULE_DEVICE_TABLE(of, qcom_smem_of_match);
882
883 static struct platform_driver qcom_smem_driver = {
884         .probe = qcom_smem_probe,
885         .remove = qcom_smem_remove,
886         .driver  = {
887                 .name = "qcom-smem",
888                 .of_match_table = qcom_smem_of_match,
889                 .suppress_bind_attrs = true,
890         },
891 };
892
893 static int __init qcom_smem_init(void)
894 {
895         return platform_driver_register(&qcom_smem_driver);
896 }
897 arch_initcall(qcom_smem_init);
898
899 static void __exit qcom_smem_exit(void)
900 {
901         platform_driver_unregister(&qcom_smem_driver);
902 }
903 module_exit(qcom_smem_exit)
904
905 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
906 MODULE_DESCRIPTION("Qualcomm Shared Memory Manager");
907 MODULE_LICENSE("GPL v2");