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