/
0015-rproc-pru-PRU-remoteproc-updated-to-work-with-virtio.patch
2491 lines (2446 loc) · 62.4 KB
/
0015-rproc-pru-PRU-remoteproc-updated-to-work-with-virtio.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From 56e702be78c1db47600e52e776ef84e3f54863ec Mon Sep 17 00:00:00 2001
From: Pantelis Antoniou <panto@antoniou-consulting.com>
Date: Thu, 11 Jul 2013 19:38:54 +0300
Subject: [PATCH 15/19] rproc: pru: PRU remoteproc updated to work with virtio
+ complex h/w setup
Major update for the PRU rproc driver. Implemented logic for interrupt event
routing, virtio support, dual PRU support, system calls and many more.
Signed-off-by: Pantelis Antoniou <panto@antoniou-consulting.com>
---
drivers/remoteproc/Kconfig | 1 +
drivers/remoteproc/pru_rproc.c | 2370 ++++++++++++++++++++++++++++++++++++++--
2 files changed, 2251 insertions(+), 120 deletions(-)
diff --git a/drivers/remoteproc/Kconfig b/drivers/remoteproc/Kconfig
index 690097b..13cb3ca 100644
--- a/drivers/remoteproc/Kconfig
+++ b/drivers/remoteproc/Kconfig
@@ -46,6 +46,7 @@ config PRU_RPROC
depends on EXPERIMENTAL
depends on HAS_DMA && SOC_AM33XX
select REMOTEPROC
+ select RPMSG
default n
help
Say y or m here to support AM33xx PRU
diff --git a/drivers/remoteproc/pru_rproc.c b/drivers/remoteproc/pru_rproc.c
index 113ad80..e3ec63b 100644
--- a/drivers/remoteproc/pru_rproc.c
+++ b/drivers/remoteproc/pru_rproc.c
@@ -1,6 +1,6 @@
/*
* PRU driver for TI's AM33xx series of SoCs
- *
+ *
* Copyright (C) 2013 Pantelis Antoniou <panto@antoniou-consulting.com>
*
* This file is licensed under the terms of the GNU General Public License
@@ -22,199 +22,2329 @@
#include <linux/pm_runtime.h>
#include <linux/pinctrl/consumer.h>
#include <linux/io.h>
+#include <plat/mailbox.h>
+#include <linux/virtio_ids.h>
+#include <linux/elf.h>
+#include <linux/byteorder/generic.h>
+#include <linux/virtio.h>
+#include <linux/virtio_ring.h>
+#include <asm/atomic.h>
#include "remoteproc_internal.h"
+/* PRU_EVTOUT0 is halt (system call) */
+
+/* maximum PRUs */
+#define MAX_PRUS 2
+
+/* sysevent targets (ARM + PRU) */
+#define MAX_TARGETS (1 + MAX_PRUS)
+
+/* sysevent target ids */
+#define TARGET_ARM 0
+#define TARGET_PRU(x) (1 + (x))
+#define TARGET_PRU_TO_PRU_IDX(x) ({ \
+ unsigned int _x = (x) - TARGET_PRU(0); \
+ _x < MAX_PRUS ? _x : -1; \
+ })
+#define TARGET_FROM_TO_IDX(_from, _to) \
+ ((_from) * MAX_TARGETS + (_to))
+#define TARGET_ARM_TO_PRU_IDX(x) \
+ TARGET_FROM_TO_IDX(TARGET_ARM, TARGET_PRU(x))
+#define TARGET_PRU_TO_ARM_IDX(x) \
+ TARGET_FROM_TO_IDX(TARGET_PRU(x), TARGET_ARM)
+
+/* maximum interrupts routed to host */
+#define MAX_ARM_PRU_INTS 8
+
+/* maximum number of sys events */
+#define MAX_PRU_SYS_EVENTS 64
+
+/* maximum number of interrupt channels */
+#define MAX_PRU_CHANNELS 10
+
+/* maximum number of host interrupts (ARM + 1 for each PRU) */
+#define MIN_PRU_HOST_INT 2
+#define MAX_PRU_HOST_INT 10
+
+struct pruproc;
+struct pruproc_core;
+
+/* maximum 4 vdevs per PRU */
+#define PRU_VDEV_MAX 4
+#define PRU_VRING_MAX (RVDEV_NUM_VRINGS * PRU_VDEV_MAX)
+
+struct pru_vring_info {
+ struct fw_rsc_vdev_vring *rsc;
+ struct vring vr;
+ void __iomem *va;
+ dma_addr_t pa;
+ u32 da;
+ struct rproc_vring *rvring;
+};
+
+/* per PRU core control structure */
+struct pruproc_core {
+ int idx;
+ struct pruproc *pruproc;
+ struct rproc *rproc;
+
+ u32 pctrl;
+ u32 pdbg;
+
+ u32 iram, iram_sz, iram_da; /* global, size, own */
+ u32 dram, dram_sz, dram_da, dram_oda; /* global, size, own, other */
+
+ const char *fw_name;
+ unsigned int is_elf : 1;
+ u32 entry_point;
+
+ struct resource_table *table;
+ int table_size;
+ /* copy of the resource table in device accessible area */
+ void * __iomem dev_table_va;
+ dma_addr_t dev_table_pa;
+
+ int num_vdevs;
+ struct fw_rsc_vdev *rsc_vdev[PRU_VDEV_MAX];
+ int vdev_vring_start[PRU_VDEV_MAX];
+ int vdev_vring_count[PRU_VDEV_MAX];
+
+ /* total for all the vdevs */
+ int num_vrings;
+ struct pru_vring_info vring_info[PRU_VRING_MAX];
+
+ /* sysevent the host generates when kicking any vring */
+ int host_vring_sysev;
+
+ /* sysevent the pru generates when kicking any vring */
+ int pru_vring_sysev;
+
+ /* boots */
+ atomic_t bootcnt;
+};
+
+struct pru_sysev_target {
+ unsigned int valid : 1; /* sysevent is valid */
+ unsigned int vring : 1; /* sysevent is vring related */
+ int source; /* 0=ARM, 1=PRU0, 2=PRU1 */
+ int target; /* 0=ARM, 1=PRU0, 2=PRU1 */
+};
+
/* PRU control structure */
struct pruproc {
- struct rproc *rproc;
struct platform_device *pdev;
- struct resource_table *table;
void __iomem *vaddr;
dma_addr_t paddr;
+ struct omap_mbox *mbox;
+ struct notifier_block nb;
+ u32 pintc;
+ u32 pdram, pdram_sz, pdram_da;
+
+ int num_irqs;
+ int irqs[MAX_ARM_PRU_INTS];
+ int events[MAX_ARM_PRU_INTS];
+ int sysev_to_ch[MAX_PRU_SYS_EVENTS];
+ int ch_to_host[MAX_PRU_CHANNELS];
+ int target_to_sysev[MAX_TARGETS * MAX_TARGETS];
+
+ /* map an incoming sysevent to kind and handler */
+ struct pru_sysev_target sysev_to_target[MAX_PRU_SYS_EVENTS];
+
+ /* number of prus */
+ u32 num_prus;
+ struct pruproc_core **pruc;
+ struct pruproc_core *pru_to_pruc[MAX_PRUS];
};
-/* Loads the firmware to shared memory. */
-static int pruproc_load_segments(struct rproc *rproc, const struct firmware *fw)
+/* global memory map (for am33xx) (almost the same as local) */
+#define PRU_DATA_RAM0 0x00000
+#define PRU_DATA_RAM1 0x02000
+#define PRU_SHARED_DATA_RAM 0x10000
+#define PRU_INTC 0x20000
+#define PRU_PRU0_CONTROL 0x22000
+#define PRU_PRU0_DEBUG 0x22400
+#define PRU_PRU1_CONTROL 0x24000
+#define PRU_PRU1_DEBUG 0x24400
+#define PRU_CFG 0x26000
+#define PRU_UART0 0x28000
+#define PRU_IEP 0x2E000
+#define PRU_ECAP0 0x30000
+#define PRU_MII_RT_CFG 0x32000
+#define PRU_MII_MDIO 0x32400
+#define PRU_INSN_RAM0 0x34000
+#define PRU_INSN_RAM1 0x38000
+
+/* PRU CONTROL */
+#define PCTRL_CONTROL 0x0000
+#define CONTROL_SOFT_RST_N 0x0001
+#define CONTROL_ENABLE 0x0002
+#define CONTROL_SLEEPING 0x0004
+#define CONTROL_COUNTER_ENABLE 0x0008
+#define CONTROL_SINGLE_STEP 0x0100
+#define CONTROL_RUNSTATE 0x4000
+
+#define PCTRL_STATUS 0x0004
+#define PCTRL_WAKEUP_EN 0x0008
+#define PCTRL_CYCLE 0x000C
+#define PCTRL_STALL 0x0010
+#define PCTRL_CTBIR0 0x0020
+#define PCTRL_CTBIR1 0x0024
+#define PCTRL_CTPPR0 0x0028
+#define PCTRL_CTPPR1 0x002C
+
+/* PRU DEBUG */
+#define PDBG_GPREG(x) (0x0000 + (x) * 4)
+#define PDBG_CT_REG(x) (0x0080 + (x) * 4)
+
+/* PRU INTC */
+#define PINTC_REVID 0x0000
+#define PINTC_CR 0x0004
+#define PINTC_GER 0x0010
+#define PINTC_GNLR 0x001C
+#define PINTC_SISR 0x0020
+#define PINTC_SICR 0x0024
+#define PINTC_EISR 0x0028
+#define PINTC_EICR 0x002C
+#define PINTC_HIEISR 0x0034
+#define PINTC_HIDISR 0x0038
+#define PINTC_GPIR 0x0080
+#define PINTC_SRSR0 0x0200
+#define PINTC_SRSR1 0x0204
+#define PINTC_SECR0 0x0280
+#define PINTC_SECR1 0x0284
+#define PINTC_ESR0 0x0300
+#define PINTC_ESR1 0x0304
+#define PINTC_ECR0 0x0380
+#define PINTC_ECR1 0x0384
+#define PINTC_CMR0 0x0400
+#define PINTC_CMR1 0x0404
+#define PINTC_CMR2 0x0408
+#define PINTC_CMR3 0x040C
+#define PINTC_CMR4 0x0410
+#define PINTC_CMR5 0x0414
+#define PINTC_CMR6 0x0418
+#define PINTC_CMR7 0x041C
+#define PINTC_CMR8 0x0420
+#define PINTC_CMR9 0x0424
+#define PINTC_CMR10 0x0428
+#define PINTC_CMR11 0x042C
+#define PINTC_CMR12 0x0430
+#define PINTC_CMR13 0x0434
+#define PINTC_CMR14 0x0438
+#define PINTC_CMR15 0x043C
+#define PINTC_HMR0 0x0800
+#define PINTC_HMR1 0x0804
+#define PINTC_HMR2 0x0808
+#define PINTC_HIPIR0 0x0900
+#define PINTC_HIPIR1 0x0904
+#define PINTC_HIPIR2 0x0908
+#define PINTC_HIPIR3 0x090C
+#define PINTC_HIPIR4 0x0910
+#define PINTC_HIPIR5 0x0914
+#define PINTC_HIPIR6 0x0918
+#define PINTC_HIPIR7 0x091C
+#define PINTC_HIPIR8 0x0920
+#define PINTC_HIPIR9 0x0924
+#define PINTC_SIPR0 0x0D00
+#define PINTC_SIPR1 0x0D04
+#define PINTC_SITR0 0x0D80
+#define PINTC_SITR1 0x0D84
+#define PINTC_HINLR0 0x1100
+#define PINTC_HINLR1 0x1104
+#define PINTC_HINLR2 0x1108
+#define PINTC_HINLR3 0x110C
+#define PINTC_HINLR4 0x1110
+#define PINTC_HINLR5 0x1114
+#define PINTC_HINLR6 0x1118
+#define PINTC_HINLR7 0x111C
+#define PINTC_HINLR8 0x1120
+#define PINTC_HINLR9 0x1124
+#define PINTC_HIER 0x1500
+
+#define HIPIR_NOPEND 0x80000000
+
+static inline u32 pru_read_reg(struct pruproc *pp, unsigned int reg)
{
- struct pruproc *pruproc = rproc->priv;
+ return __raw_readl(pp->vaddr + reg);
+}
- dev_dbg(&pruproc->pdev->dev, "%s\n", __func__);
+static inline void pru_write_reg(struct pruproc *pp, unsigned int reg,
+ u32 val)
+{
+ __raw_writel(val, pp->vaddr + reg);
+}
- return 0;
+static inline u32 pintc_read_reg(struct pruproc *pp, unsigned int reg)
+{
+ return pru_read_reg(pp, reg + pp->pintc);
}
-/* Find the resource table inside the remote processor's firmware. */
-static struct resource_table *
-pruproc_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
- int *tablesz)
+static inline void pintc_write_reg(struct pruproc *pp, unsigned int reg,
+ u32 val)
{
- struct pruproc *pruproc = rproc->priv;
- struct resource_table *table;
+ return pru_write_reg(pp, reg + pp->pintc, val);
+}
- table = devm_kzalloc(&pruproc->pdev->dev, sizeof(*table), GFP_KERNEL);
+static inline u32 pcntrl_read_reg(struct pruproc_core *ppc, unsigned int reg)
+{
+ return pru_read_reg(ppc->pruproc, reg + ppc->pctrl);
+}
- dev_dbg(&pruproc->pdev->dev, "%s\n", __func__);
+static inline void pcntrl_write_reg(struct pruproc_core *ppc, unsigned int reg,
+ u32 val)
+{
+ return pru_write_reg(ppc->pruproc, reg + ppc->pctrl, val);
+}
- return table;
+static inline u32 pdbg_read_reg(struct pruproc_core *ppc, unsigned int reg)
+{
+ return pru_read_reg(ppc->pruproc, reg + ppc->pdbg);
}
-static int pruproc_sanity_check(struct rproc *rproc, const struct firmware *fw)
+static inline void pdbg_write_reg(struct pruproc_core *ppc, unsigned int reg,
+ u32 val)
{
- return 0;
+ return pru_write_reg(ppc->pruproc, reg + ppc->pdbg, val);
}
-/* PRU firmware handler operations */
-const struct rproc_fw_ops pruproc_fw_ops = {
- .find_rsc_table = pruproc_find_rsc_table,
- .load = pruproc_load_segments,
- .sanity_check = pruproc_sanity_check,
-};
+/* convert 32 bit Data device address to a virtual (and physical) host addr */
+static void * __iomem pru_d_da_to_va(struct pruproc_core *ppc, u32 da,
+ dma_addr_t *pa)
+{
+ struct pruproc *pp = ppc->pruproc;
+ u32 offset;
-/* Kick the modem with specified notification id */
-static void pruproc_kick(struct rproc *rproc, int vqid)
+ /* check own data ram first */
+ if (da >= ppc->dram_da && da < ppc->dram_da + ppc->dram_sz)
+ offset = ppc->dram + da - ppc->dram_da;
+ /* check other's data ram */
+ else if (da >= ppc->dram_oda && da < ppc->dram_oda + ppc->dram_sz)
+ offset = ppc->dram + da - ppc->dram_oda;
+ /* shared data */
+ else if (da >= pp->pdram_da && da < pp->pdram_da + pp->pdram_sz)
+ offset = pp->pdram + da - pp->pdram_da;
+ else
+ return NULL;
+
+ if (pa)
+ *pa = pp->paddr + offset;
+ return pp->vaddr + offset;
+}
+
+/* convert physical address to device address */
+static u32 pru_d_pa_to_da(struct pruproc_core *ppc, dma_addr_t pa)
{
- struct pruproc *pruproc = rproc->priv;
+ /* we don't support mapping in the GPMC space */
+ if (pa < 0x00080000)
+ return 0xffffffff;
+
+ /* we also don't map internal memories */
- dev_dbg(&pruproc->pdev->dev, "kick vqid:%d\n", vqid);
+ return (u32)pa;
}
-/* Start the PRU modem */
-static int pruproc_start(struct rproc *rproc)
+static void * __iomem pru_i_da_to_va(struct pruproc_core *ppc, u32 da,
+ dma_addr_t *pa)
+{
+ struct pruproc *pp = ppc->pruproc;
+ u32 offset;
+
+ /* check whether is within the iram space */
+ if (da >= ppc->iram_da && da < ppc->iram-da + ppc->iram_sz)
+ offset = ppc->iram + da - ppc->iram_da;
+ else
+ return NULL;
+
+ if (pa)
+ *pa = pp->paddr + offset;
+ return pp->vaddr + offset;
+}
+
+static void * __iomem pru_d_da_to_va_block(struct pruproc_core *ppc, u32 da,
+ dma_addr_t *pa, int size)
+{
+ void * __iomem va_start;
+ void * __iomem va_end;
+
+ /* make sure size is not bogus */
+ if (size <= 0)
+ return NULL;
+
+ /* get start vaddr of device address (D) */
+ va_start = pru_d_da_to_va(ppc, da, pa);
+ if (va_start == NULL)
+ return NULL;
+
+ /* get end vaddr of device address (D) */
+ va_end = pru_d_da_to_va(ppc, da + size - 1, NULL);
+ if (va_end == NULL)
+ return NULL;
+
+ /* for the block to be valid the VAs must be consecutive */
+ if ((va_end - va_start) != (size - 1))
+ return NULL;
+
+ /* all good */
+ return va_start;
+}
+
+static void * __iomem pru_i_da_to_va_block(struct pruproc_core *ppc, u32 da,
+ dma_addr_t *pa, int size)
+{
+ void * __iomem va_start;
+ void * __iomem va_end;
+
+ /* make sure size is not bogus */
+ if (size <= 0)
+ return NULL;
+
+ /* get start vaddr of device address (I) */
+ va_start = pru_i_da_to_va(ppc, da, pa);
+ if (va_start == NULL)
+ return NULL;
+
+ /* get end vaddr of device address (I) */
+ va_end = pru_i_da_to_va(ppc, da + size - 1, NULL);
+ if (va_end == NULL)
+ return NULL;
+
+ /* for the block to be valid the VAs must be consecutive */
+ if ((va_end - va_start) != (size - 1))
+ return NULL;
+
+ /* all good */
+ return va_start;
+}
+
+static int pru_i_read_u32(struct pruproc_core *ppc, u32 da, u32 *val)
{
- struct pruproc *pruproc = rproc->priv;
+ void * __iomem va;
+
+ /* verify it's a word address */
+ if (da & 3)
+ return -EINVAL;
- dev_dbg(&pruproc->pdev->dev, "start pru\n");
+ /* get vaddr of device address (I) */
+ va = pru_i_da_to_va_block(ppc, da, NULL, sizeof(u32));
+ if (va == NULL)
+ return -EFAULT;
+
+ if (val)
+ *val = le32_to_cpu(*(u32 *)va);
return 0;
}
-/* Stop the PRU modem */
-static int pruproc_stop(struct rproc *rproc)
+static int pru_d_read_u32(struct pruproc_core *ppc, u32 da, u32 *val)
{
- struct pruproc *pruproc = rproc->priv;
+ void * __iomem va;
+
+ /* verify it's a word address */
+ if (da & 3)
+ return -EINVAL;
- dev_dbg(&pruproc->pdev->dev, "stop PRU\n");
+ /* get vaddr of device address (I) */
+ va = pru_d_da_to_va_block(ppc, da, NULL, sizeof(u32));
+ if (va == NULL)
+ return -EINVAL;
+
+ if (val)
+ *val = le32_to_cpu(*(u32 *)va);
return 0;
}
-static struct rproc_ops pruproc_ops = {
- .start = pruproc_start,
- .stop = pruproc_stop,
- .kick = pruproc_kick,
-};
+static int pru_i_write_u32(struct pruproc_core *ppc, u32 da, u32 val)
+{
+ void * __iomem va;
-/* PRU is unregistered */
-static int pruproc_remove(struct platform_device *pdev)
+ /* verify it's a word address */
+ if (da & 3)
+ return -EINVAL;
+
+ /* get vaddr of device address (I) */
+ va = pru_i_da_to_va_block(ppc, da, NULL, sizeof(u32));
+ if (va == NULL)
+ return -EINVAL;
+
+ *(u32 *)va = cpu_to_le32(val);
+
+ return 0;
+}
+
+static int pru_d_write_u32(struct pruproc_core *ppc, u32 da, u32 val)
{
- struct pruproc *pruproc = platform_get_drvdata(pdev);
+ void * __iomem va;
- dev_dbg(&pdev->dev, "remove pru\n");
+ /* verify it's a word address */
+ if (da & 3)
+ return -EINVAL;
- /* Unregister as remoteproc device */
- rproc_del(pruproc->rproc);
- rproc_put(pruproc->rproc);
+ /* get vaddr of device address (I) */
+ va = pru_d_da_to_va_block(ppc, da, NULL, sizeof(u32));
+ if (va == NULL)
+ return -EINVAL;
- platform_set_drvdata(pdev, NULL);
+ *(u32 *)va = cpu_to_le32(val);
+ return 0;
+}
+static int pruproc_bin_sanity_check(struct rproc *rproc, const struct firmware *fw)
+{
return 0;
}
-/* Handle probe of a modem device */
-static int pruproc_probe(struct platform_device *pdev)
+/* Loads the firmware to shared memory. */
+static int pruproc_bin_load_segments(struct rproc *rproc, const struct firmware *fw)
{
- struct device *dev = &pdev->dev;
- struct pruproc *pruproc;
- struct rproc *rproc;
- struct resource *res;
- struct pinctrl *pinctrl;
- int err;
+ struct pruproc_core *ppc = rproc->priv;
+ struct device *dev = &rproc->dev;
+ void __iomem *va;
- dev_dbg(dev, "probe pru\n");
+ pcntrl_write_reg(ppc, PCTRL_CONTROL, CONTROL_SOFT_RST_N);
- /* get pinctrl */
- pinctrl = devm_pinctrl_get_select_default(dev);
- if (IS_ERR(pinctrl)) {
- err = PTR_ERR(pinctrl);
- /* deferring probe */
- if (err == -EPROBE_DEFER) {
- dev_warn(dev, "deferring proble\n");
- return err;
- }
- dev_warn(dev, "pins are not configured from the driver\n");
+ /* binary starts from 0 */
+ ppc->entry_point = 0;
+
+ va = pru_i_da_to_va_block(ppc, ppc->entry_point, NULL, fw->size);
+ if (va == NULL) {
+ dev_err(dev, "FW is larger than available space (%u)\n",
+ fw->size);
+ return -ENOMEM;
}
- /* we only work on OF */
- if (dev->of_node == NULL) {
- dev_err(dev, "Only OF configuration supported\n");
- err = -ENODEV;
- goto fail_of_node;
+ /* just copy */
+ memcpy(va, fw->data, fw->size);
+
+ return 0;
+}
+
+static int
+pruproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
+{
+ const char *name = rproc->firmware;
+ struct device *dev = &rproc->dev;
+ struct elf32_hdr *ehdr;
+ char class;
+
+ if (!fw) {
+ dev_err(dev, "failed to load %s\n", name);
+ return -EINVAL;
}
- pm_runtime_enable(dev);
- err = pm_runtime_get_sync(dev);
- if (err != 0) {
- dev_err(dev, "pm_runtime_get_sync failed\n");
- goto fail_pm_runtime_get_sync;
+ if (fw->size < sizeof(struct elf32_hdr)) {
+ dev_err(dev, "Image is too small\n");
+ return -EINVAL;
}
- err = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
- if (err) {
- dev_err(dev, "dma_set_coherent_mask: %d\n", err);
- goto fail_dma_set_coherent_mask;
+ ehdr = (struct elf32_hdr *)fw->data;
+
+ /* We only support ELF32 at this point */
+ class = ehdr->e_ident[EI_CLASS];
+ if (class != ELFCLASS32) {
+ dev_err(dev, "Unsupported class: %d\n", class);
+ return -EINVAL;
}
- rproc = rproc_alloc(dev, pdev->name, &pruproc_ops,
- "prutest.bin", sizeof(*pruproc));
- if (!rproc) {
- dev_err(dev, "rproc_alloc failed\n");
- err = -ENOMEM;
- goto fail_rproc_alloc;
+ /* PRU is little endian */
+ if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
+ dev_err(dev, "Unsupported firmware endianness\n");
+ return -EINVAL;
}
- pruproc = rproc->priv;
- pruproc->pdev = pdev;
- pruproc->rproc = rproc;
+ if (fw->size < le32_to_cpu(ehdr->e_shoff) +
+ sizeof(struct elf32_shdr)) {
+ dev_err(dev, "Image is too small\n");
+ return -EINVAL;
+ }
- platform_set_drvdata(pdev, pruproc);
+ if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+ dev_err(dev, "Image is corrupted (bad magic)\n");
+ return -EINVAL;
+ }
- /* Set the PRU specific firmware handler */
- rproc->fw_ops = &pruproc_fw_ops;
+ if (le16_to_cpu(ehdr->e_phnum) == 0) {
+ dev_err(dev, "No loadable segments\n");
+ return -EINVAL;
+ }
- /* Register as a remoteproc device */
- err = rproc_add(rproc);
- if (err) {
- dev_err(dev, "rproc_add failed\n");
- goto fail_rproc_add;
+ if (le32_to_cpu(ehdr->e_phoff) > fw->size) {
+ dev_err(dev, "Firmware size is too small\n");
+ return -EINVAL;
}
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- if (res == NULL) {
- dev_err(dev, "failed to parse MEM resource\n");
- goto fail_platform_get_resource;
+ return 0;
+}
+
+static int
+pruproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
+{
+ struct device *dev = &rproc->dev;
+ struct pruproc_core *ppc = rproc->priv;
+ struct elf32_hdr *ehdr;
+ struct elf32_phdr *phdr;
+ int i, ret = 0;
+ const u8 *elf_data = fw->data;
+ u32 da, memsz, filesz, offset, flags;
+ void * __iomem va;
+
+ pcntrl_write_reg(ppc, PCTRL_CONTROL, CONTROL_SOFT_RST_N);
+
+ ehdr = (struct elf32_hdr *)elf_data;
+ phdr = (struct elf32_phdr *)(elf_data + le32_to_cpu(ehdr->e_phoff));
+
+ /* go through the available ELF segments */
+ for (i = 0; i < le16_to_cpu(ehdr->e_phnum); i++, phdr++) {
+
+ da = le32_to_cpu(phdr->p_paddr);
+ memsz = le32_to_cpu(phdr->p_memsz);
+ filesz = le32_to_cpu(phdr->p_filesz);
+ offset = le32_to_cpu(phdr->p_offset);
+ flags = le32_to_cpu(phdr->p_flags);
+
+ if (le32_to_cpu(phdr->p_type) != PT_LOAD)
+ continue;
+
+ dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x"
+ " flags %c%c%c\n",
+ le32_to_cpu(phdr->p_type),
+ da, memsz, filesz,
+ (flags & PF_R) ? 'R' : '-',
+ (flags & PF_W) ? 'W' : '-',
+ (flags & PF_X) ? 'E' : '-');
+
+ /* PRU is not a unified address space architecture */
+ /* we need to map differently executable & data segments */
+
+ if (filesz > memsz) {
+ dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
+ filesz, memsz);
+ ret = -EINVAL;
+ break;
+ }
+
+ if (offset + filesz > fw->size) {
+ dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
+ offset + filesz, fw->size);
+ ret = -EINVAL;
+ break;
+ }
+
+ /* we can't use rproc_da_to_va (it relies on carveouts) */
+
+ /* text? to code area */
+ if (flags & PF_X)
+ va = pru_i_da_to_va_block(ppc, da, NULL, memsz);
+ else
+ va = pru_d_da_to_va_block(ppc, da, NULL, memsz);
+
+ /* check if valid section */
+ if (va == NULL) {
+ dev_err(dev, "fw: #%d @0x%x sz 0x%x bad\n",
+ i, da, memsz);
+ ret = -EINVAL;
+ break;
+ }
+
+ /* put the segment where the remote processor expects it */
+ if (filesz > 0)
+ memcpy(va, elf_data + offset, filesz);
+ else
+ memset(va, 0, memsz);
}
- pruproc->vaddr = devm_ioremap(dev, res->start, resource_size(res));
- if (pruproc->vaddr == NULL) {
- dev_err(dev, "failed to parse MEM resource\n");
- goto fail_devm_ioremap;
+ ppc->entry_point = le32_to_cpu(ehdr->e_entry);
+
+ return ret;
+}
+
+static
+u32 pruproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
+{
+ struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
+
+ return le32_to_cpu(ehdr->e_entry);
+}
+
+/* just return the built-firmware resources */
+static struct resource_table *
+pruproc_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
+ int *tablesz)
+{
+ struct pruproc_core *ppc = rproc->priv;
+
+ /* no resource table for this PRU */
+ if (ppc->table == NULL) {
+ *tablesz = 0;
+ return NULL;
}
- dev_info(dev, "Loaded OK\n");
+ *tablesz = ppc->table_size;
+ return ppc->table;
+}
+
+void *get_resource_type(struct resource_table *res,
+ int type, int idx)
+{
+ struct fw_rsc_hdr *rsc_hdr;
+ int i, j;
+
+ j = 0;
+ for (i = 0; i < res->num; i++) {
+ rsc_hdr = (void *)res + res->offset[i];
+ if (type >= 0 && rsc_hdr->type != type)
+ continue;
+ if (j == idx)
+ return &rsc_hdr->data[0];
+ j++;
+ }
+
+ return NULL;
+}
+
+/* update the device's firmware resource with the allocated values */
+static int update_dev_rsc_table(struct pruproc_core *ppc)
+{
+ struct rproc *rproc = ppc->rproc;
+ struct device *dev = &rproc->dev;
+ struct rproc_vdev *rvdev;
+ struct rproc_vring *rvring;
+ struct pru_vring_info *vri;
+ struct fw_rsc_vdev *rsc_vdev;
+ int vdev_idx, i, cnt, err, j, vring_start, num_vrings;
+
+ /* no table; do nothing */
+ if (ppc->table == NULL)
+ return 0;
+
+ /* copy the resource table here */
+ memcpy(ppc->dev_table_va, ppc->table, ppc->table_size);
+
+ /* everything is initialized; fill in the real da & notify ids */
+ for (vdev_idx = 0; vdev_idx < ppc->num_vdevs; vdev_idx++) {
+ vring_start = ppc->vdev_vring_start[vdev_idx];
+ num_vrings = ppc->vdev_vring_count[vdev_idx];
+
+ /* find rvdev */
+ cnt = 0;
+ list_for_each_entry(rvdev, &rproc->rvdevs, node) {
+ if (cnt == vdev_idx)
+ break;
+ cnt++;
+ }
+ if (cnt != vdev_idx) {
+ dev_warn(dev, "rsc_vdev not found (continuing anyway)\n");
+ rvdev = NULL;
+ }
+
+ /* locate the vdev resource in the device memory */
+ rsc_vdev = get_resource_type(ppc->dev_table_va, RSC_VDEV,
+ vdev_idx);
+ if (rsc_vdev == NULL) {
+ dev_err(dev, "PRU#%d Failed to get RSV_VDEV #%d\n",
+ ppc->idx, vdev_idx);
+ err = -EINVAL;
+ goto err_fail;
+ }
+
+ for (i = 0; i < num_vrings; i++) {
+ j = vring_start + i;
+
+ vri = &ppc->vring_info[j];
+
+ if (rvdev != NULL)
+ rvring = &rvdev->vring[i];
+ else
+ rvring = NULL;
+
+ /* keep track of rproc's rvring */
+ vri->rvring = rvring;
+
+ rsc_vdev->vring[i].da = vri->da;
+ if (rvring != NULL)
+ rsc_vdev->vring[i].notifyid = rvring->notifyid;
+ else
+ rsc_vdev->vring[i].notifyid = -1; /* mark that it's not ready yet */
+
+ dev_info(dev, "VDEV#%d VR#%d da=0x%08x notifyid=0x%08x\n",
+ vdev_idx, i, vri->da, rvring->notifyid);
+ }
+ }
+
+#if 0
+ j = 0;
+ list_for_each_entry(rvdev, &rproc->rvdevs, node) {
+
+ /* get copied resource's VDEV */
+ rsc_vdev = get_resource_type(ppc->dev_table_va, RSC_VDEV,
+ vdev_idx);
+ if (rsc_vdev == NULL) {
+ dev_err(dev, "Failed to get RSV_VDEV #%d\n", vdev_idx);
+ err = -EINVAL;
+ goto err_fail;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(rvdev->vring); i++) {
+ rvring = &rvdev->vring[i];
+
+ if (j >= ARRAY_SIZE(ppc->vring_info)) {
+ dev_err(dev, "Too many vrings\n");
+ err = -ENOENT;
+ goto err_fail;
+ }
+
+ vri = &ppc->vring_info[j];
+
+ /* keep track of rproc's rvring */
+ vri->rvring = rvring;
+
+ rsc_vdev->vring[i].da = vri->da;
+ rsc_vdev->vring[i].notifyid = rvring->notifyid;
+
+ dev_info(dev, "VDEV#%d VR#%d da=0x%08x notifyid=0x%08x\n",
+ vdev_idx, i, vri->da, rvring->notifyid);
+
+ j++;
+ }
+
+ vdev_idx++;
+ }
+#endif
return 0;
-fail_devm_ioremap:
-fail_platform_get_resource:
- rproc_del(rproc);
-fail_rproc_add:
- platform_set_drvdata(pdev, NULL);
- rproc_put(rproc);
-fail_rproc_alloc:
-fail_dma_set_coherent_mask:
-fail_of_node:
- pm_runtime_disable(dev);
-fail_pm_runtime_get_sync:
+err_fail:
+ return err;
+
+}
+
+/* PRU binary firmware handler operations */
+const struct rproc_fw_ops pruproc_bin_fw_ops = {
+ .find_rsc_table = pruproc_find_rsc_table,
+ .load = pruproc_bin_load_segments,
+ .sanity_check = pruproc_bin_sanity_check,
+};
+
+/* PRU elf handler operations */
+const struct rproc_fw_ops pruproc_elf_fw_ops = {
+ .find_rsc_table = pruproc_find_rsc_table,
+ .load = pruproc_elf_load_segments,
+ .sanity_check = pruproc_elf_sanity_check,
+ .get_boot_addr = pruproc_elf_get_boot_addr,
+};
+
+void dump_vring(struct vring *vring)
+{
+ int i;
+ struct vring_desc *vd;
+
+ pr_info("vring: num=%u desc @%p\n", vring->num, vring->desc);
+
+ for (i = 0; i < vring->num; i++) {
+ vd = &vring->desc[i];
+ pr_info(" d#%2d: a 0x%016llx l 0x%08x f 0x%04x n 0x%04x\n",
+ i, vd->addr, vd->len, vd->flags, vd->next);
+ }
+ pr_info(" avail: flags 0x%04x idx 0x%04x\n",
+ vring->avail->flags, vring->avail->idx);
+ for (i = 0; i < vring->num; i++) {
+ pr_info(" a#%2d: ring 0x%04x\n", i,
+ vring->avail->ring[i]);
+ }
+ pr_info(" avail: used_event_idx 0x%04x\n",
+ vring->avail->ring[vring->num]);
+
+ pr_info(" used: flags 0x%04x idx 0x%04x\n",
+ vring->used->flags, vring->used->idx);
+ for (i = 0; i < vring->num; i++) {
+ pr_info(" u#%2d: id 0x%08x len 0x%08x\n", i,
+ vring->used->ring[i].id,
+ vring->used->ring[i].len);