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
1001
1002
1003
1004
1005
1006
1007
1008
|
#include <asm/cacheflush.h>
#include <linux/slab.h>
#include <linux/dma-mapping.h>
#include "m4u_priv.h"
typedef struct {
imu_pgd_t *pgd;
imu_pte_t *pte;
unsigned int mva;
unsigned long pa;
unsigned int size;
int valid;
} m4u_pte_info_t;
static inline void m4u_set_pgd_val(imu_pgd_t *pgd, unsigned int val)
{
COM_WriteReg32((unsigned long)&(imu_pgd_val(*pgd)), val);
}
static inline void read_lock_domain(m4u_domain_t *domain)
{
mutex_lock(&domain->pgtable_mutex);
}
static inline void read_unlock_domain(m4u_domain_t *domain)
{
mutex_unlock(&domain->pgtable_mutex);
}
static inline void write_lock_domain(m4u_domain_t *domain)
{
mutex_lock(&domain->pgtable_mutex);
}
static inline void write_unlock_domain(m4u_domain_t *domain)
{
mutex_unlock(&domain->pgtable_mutex);
}
/* should not hold pg_lock when call this func. */
inline int m4u_get_pt_type(m4u_domain_t *domain, unsigned int mva)
{
imu_pgd_t *pgd;
imu_pte_t *pte;
int ret;
read_lock_domain(domain);
pgd = imu_pgd_offset(domain, mva);
if (F_PGD_TYPE_IS_PAGE(*pgd)) {
pte = imu_pte_offset_map(pgd, mva);
if (F_PTE_TYPE_GET(imu_pte_val(*pte)) == F_PTE_TYPE_LARGE) {
imu_pte_unmap(pte);
ret = MMU_PT_TYPE_LARGE_PAGE;
} else if (F_PTE_TYPE_GET(imu_pte_val(*pte)) == F_PTE_TYPE_SMALL) {
imu_pte_unmap(pte);
ret = MMU_PT_TYPE_SMALL_PAGE;
} else {
imu_pte_unmap(pte);
ret = -1;
}
} else if (F_PGD_TYPE_IS_SECTION(*pgd)) {
ret = MMU_PT_TYPE_SECTION;
} else if (F_PGD_TYPE_IS_SUPERSECTION(*pgd)) {
ret = MMU_PT_TYPE_SUPERSECTION;
} else {
ret = -1;
}
read_unlock_domain(domain);
return ret;
}
static inline unsigned int m4u_get_pt_type_size(int type)
{
if (type == MMU_PT_TYPE_SMALL_PAGE)
return MMU_SMALL_PAGE_SIZE;
else if (type == MMU_PT_TYPE_LARGE_PAGE)
return MMU_LARGE_PAGE_SIZE;
else if (type == MMU_PT_TYPE_SECTION)
return MMU_SECTION_SIZE;
else if (type == MMU_PT_TYPE_SUPERSECTION)
return MMU_SUPERSECTION_SIZE;
else
return -1;
}
/***********************************************************/
/** print pte info to log or sequncial file
if data is NULL, info is out put to kernel log by pr log
if pte is valid, we will print like va->pgd->pte->pa
if pte is invalid, we print as many info as we can.
* @return NULL
* @remark
* @see
* @author K Zhang @date 2013/11/18
************************************************************/
void *__m4u_print_pte(m4u_pte_info_t *info, void *data)
{
if (info->valid) {
if (info->size == SZ_4K) {
M4U_PRINT_LOG_OR_SEQ(data, "mva(0x%x)-->pgd(0x%x)-->pte(0x%x)-->pa(0x%lx) small\n",
info->mva, imu_pgd_val(*info->pgd), imu_pte_val(*info->pte), info->pa);
} else if (info->size == SZ_64K) {
M4U_PRINT_LOG_OR_SEQ(data, "mva(0x%x)-->pgd(0x%x)-->pte(0x%x)-->pa(0x%lx) large\n",
info->mva, imu_pgd_val(*info->pgd), imu_pte_val(*info->pte), info->pa);
} else if (info->size == SZ_1M) {
M4U_PRINT_LOG_OR_SEQ(data, "mva(0x%x)-->pgd(0x%x)-->pa(0x%lx) section\n",
info->mva, imu_pgd_val(*info->pgd), info->pa);
} else if (info->size == SZ_16M) {
M4U_PRINT_LOG_OR_SEQ(data, "mva(0x%x)-->pgd(0x%x)-->pa(0x%lx) super\n",
info->mva, imu_pgd_val(*info->pgd), info->pa);
}
} else {
M4U_PRINT_LOG_OR_SEQ(data, "va(0x%x)", info->mva);
M4U_PRINT_LOG_OR_SEQ(data, "-->pgd(0x%x)", imu_pgd_val(*info->pgd));
if (info->pte)
M4U_PRINT_LOG_OR_SEQ(data, "-->pte(0x%x)", imu_pte_val(*info->pte));
M4U_PRINT_LOG_OR_SEQ(data, " invalid\n");
}
return NULL;
}
/* domain->pgtable_mutex should be held */
int m4u_get_pte_info(m4u_domain_t *domain, unsigned int mva, m4u_pte_info_t *pte_info)
{
imu_pgd_t *pgd;
imu_pte_t *pte = NULL;
unsigned int pa = 0;
unsigned int size;
int valid = 1;
pgd = imu_pgd_offset(domain, mva);
if (F_PGD_TYPE_IS_PAGE(*pgd)) {
pte = imu_pte_offset_map(pgd, mva);
if (F_PTE_TYPE_GET(imu_pte_val(*pte)) == F_PTE_TYPE_LARGE) {
pa = imu_pte_val(*pte) & F_PTE_PA_LARGE_MSK;
pa |= mva & (~F_PTE_PA_LARGE_MSK);
size = MMU_LARGE_PAGE_SIZE;
} else if (F_PTE_TYPE_GET(imu_pte_val(*pte)) == F_PTE_TYPE_SMALL) {
pa = imu_pte_val(*pte) & F_PTE_PA_SMALL_MSK;
pa |= mva & (~F_PTE_PA_SMALL_MSK);
size = MMU_SMALL_PAGE_SIZE;
} else {
valid = 0;
size = MMU_SMALL_PAGE_SIZE;
}
} else {
pte = NULL;
if (F_PGD_TYPE_IS_SECTION(*pgd)) {
pa = imu_pgd_val(*pgd) & F_PGD_PA_SECTION_MSK;
pa |= mva & (~F_PGD_PA_SECTION_MSK);
size = MMU_SECTION_SIZE;
} else if (F_PGD_TYPE_IS_SUPERSECTION(*pgd)) {
pa = imu_pgd_val(*pgd) & F_PGD_PA_SUPERSECTION_MSK;
pa |= mva & (~F_PGD_PA_SUPERSECTION_MSK);
size = MMU_SUPERSECTION_SIZE;
} else {
valid = 0;
size = MMU_SECTION_SIZE;
}
}
pte_info->pgd = pgd;
pte_info->pte = pte;
pte_info->mva = mva;
pte_info->pa = pa;
pte_info->size = size;
pte_info->valid = valid;
return 0;
}
typedef void *(m4u_pte_fn_t) (m4u_pte_info_t *pte_info, void *data);
/***********************************************************/
/** interate all pte, and call fn for each pte.
* @param domain
* @param fn -- to be called for each pte
* @param data -- private data for fn
*
* @return NULL of success, non-NULL if interrupted by fn.
* @remark
1. fn will only be called when pte is valid.
2. if fn return non-NULL, the iteration will return imediately.
* @see
* @author K Zhang @date 2013/11/18
************************************************************/
void *m4u_for_each_pte(m4u_domain_t *domain, m4u_pte_fn_t *fn, void *data)
{
unsigned int mva = 0;
void *ret;
m4u_pte_info_t pte_info;
read_lock_domain(domain);
while (1) {
m4u_get_pte_info(domain, mva, &pte_info);
if (pte_info.valid) {
ret = fn(&pte_info, data);
if (ret) {
read_unlock_domain(domain);
return ret;
}
}
if (mva + pte_info.size < mva) /* over flow */
break;
mva += pte_info.size;
}
read_unlock_domain(domain);
return NULL;
}
/* dump pte info for mva, no matter it's valid or not */
/* this function doesn't lock pgtable lock. */
void m4u_dump_pte_nolock(m4u_domain_t *domain, unsigned int mva)
{
m4u_pte_info_t pte_info;
m4u_get_pte_info(domain, mva, &pte_info);
__m4u_print_pte(&pte_info, NULL);
}
int m4u_pte_invalid(m4u_domain_t *domain, unsigned int mva)
{
m4u_pte_info_t info;
m4u_get_pte_info(domain, mva, &info);
return (info.pa != 0) && info.valid;
}
void m4u_dump_pte(m4u_domain_t *domain, unsigned int mva)
{
read_lock_domain(domain);
m4u_dump_pte_nolock(domain, mva);
read_unlock_domain(domain);
}
unsigned long m4u_get_pte(m4u_domain_t *domain, unsigned int mva)
{
m4u_pte_info_t pte_info;
read_lock_domain(domain);
m4u_get_pte_info(domain, mva, &pte_info);
read_unlock_domain(domain);
return pte_info.pa;
}
/***********************************************************/
/** dump pagetable to sequncial file or kernel log.
* @param domain -- domain to dump
* @param seq -- seq file. if NULL, we will dump to kernel log
*
* @remark this func will lock pgtable_lock, it may sleep.
* @author K Zhang @date 2013/11/18
************************************************************/
void m4u_dump_pgtable(m4u_domain_t *domain, struct seq_file *seq)
{
M4U_PRINT_LOG_OR_SEQ(seq, "m4u dump pgtable start ==============>\n");
m4u_for_each_pte(domain, __m4u_print_pte, seq);
M4U_PRINT_LOG_OR_SEQ(seq, "m4u dump pgtable done ==============>\n");
}
/* M4U_PROT_CACHE indicates M4U_PROT_SHARE, which route transaction to CCI*/
static inline unsigned int m4u_prot_fixup(unsigned int prot)
{
/* don't support read/write protect */
/*
if(unlikely(!(prot & (M4U_PROT_READ|M4U_PROT_WRITE))))
prot |= M4U_PROT_READ|M4U_PROT_WRITE;
if(unlikely((prot&M4U_PROT_WRITE) && !(prot&M4U_PROT_READ)))
prot |= M4U_PROT_WRITE;
*/
if (prot & M4U_PROT_CACHE)
prot |= M4U_PROT_SHARE;
return prot;
}
/***********************************************************/
/** convert m4u_prot to hardware pgd/pte attribute
* @param prot -- m4u_prot flags
*
* @return pgd or pte attribute
* @remark
* @see
* @author K Zhang @date 2013/11/18
************************************************************/
static inline unsigned int __m4u_get_pgd_attr_16M(unsigned int prot)
{
unsigned int pgprot;
pgprot = F_PGD_TYPE_SUPERSECTION;
pgprot |= (prot & M4U_PROT_SEC) ? 0 : F_PGD_NS_BIT_SECTION(1);
pgprot |= (prot & M4U_PROT_SHARE) ? F_PGD_S_BIT : 0;
pgprot |= (prot & M4U_PROT_CACHE) ? (F_PGD_C_BIT | F_PGD_B_BIT) : 0;
if (gM4U_4G_DRAM_Mode)
pgprot |= F_PGD_BIT32_BIT;
return pgprot;
}
static inline unsigned int __m4u_get_pgd_attr_1M(unsigned int prot)
{
unsigned int pgprot;
pgprot = F_PGD_TYPE_SECTION;
pgprot |= (prot & M4U_PROT_SEC) ? 0 : F_PGD_NS_BIT_SECTION(1);
pgprot |= (prot & M4U_PROT_SHARE) ? F_PGD_S_BIT : 0;
pgprot |= (prot & M4U_PROT_CACHE) ? (F_PGD_C_BIT | F_PGD_B_BIT) : 0;
if (gM4U_4G_DRAM_Mode)
pgprot |= F_PGD_BIT32_BIT;
return pgprot;
}
static inline unsigned int __m4u_get_pgd_attr_page(unsigned int prot)
{
unsigned int pgprot;
pgprot = F_PGD_TYPE_PAGE;
pgprot |= (prot & M4U_PROT_SEC) ? 0 : F_PGD_NS_BIT_PAGE(1);
return pgprot;
}
static inline unsigned int __m4u_get_pte_attr_64K(unsigned int prot)
{
unsigned int pgprot;
pgprot = F_PTE_TYPE_LARGE;
pgprot |= (prot & M4U_PROT_SHARE) ? F_PTE_S_BIT : 0;
pgprot |= (prot & M4U_PROT_CACHE) ? (F_PGD_C_BIT | F_PGD_B_BIT) : 0;
if (gM4U_4G_DRAM_Mode)
pgprot |= F_PTE_BIT32_BIT;
return pgprot;
}
static inline unsigned int __m4u_get_pte_attr_4K(unsigned int prot)
{
unsigned int pgprot;
pgprot = F_PTE_TYPE_SMALL;
pgprot |= (prot & M4U_PROT_SHARE) ? F_PTE_S_BIT : 0;
pgprot |= (prot & M4U_PROT_CACHE) ? (F_PGD_C_BIT | F_PGD_B_BIT) : 0;
if (gM4U_4G_DRAM_Mode)
pgprot |= F_PTE_BIT32_BIT;
return pgprot;
}
/***********************************************************/
/** cache flush for modified pte.
notes: because pte is allocated using slab, cache sync is needed.
*
* @author K Zhang @date 2013/11/18
************************************************************/
int m4u_clean_pte(m4u_domain_t *domain, unsigned int mva, unsigned int size)
{
imu_pgd_t *pgd;
unsigned long long tmp_mva = (unsigned long long)mva;
unsigned long long end_plus_1 = tmp_mva + (unsigned long long)size;
while (tmp_mva < end_plus_1) {
pgd = imu_pgd_offset(domain, tmp_mva);
if (F_PGD_TYPE_IS_PAGE(*pgd)) {
imu_pte_t *pte, *pte_end;
unsigned long long next_mva, sync_entry_nr;
pte = imu_pte_offset_map(pgd, tmp_mva);
if (!pte) {
/* invalid pte: goto next pgd entry */
tmp_mva = m4u_calc_next_mva(tmp_mva, end_plus_1, MMU_SECTION_SIZE);
continue;
}
next_mva = m4u_calc_next_mva(tmp_mva, end_plus_1, MMU_SECTION_SIZE);
sync_entry_nr = (next_mva - tmp_mva) >> 12; /*(next_mva - tmp_mva) / MMU_SMALL_PAGE_SIZE*/
pte_end = pte + sync_entry_nr;
/* do cache sync for [pte, pte_end) */
dmac_flush_range((void *)pte, (void *)pte_end);
/* M4UMSG("dmac_flush_range: 0x%p ~ 0x%p\n", pte, pte_end); */
imu_pte_unmap(pte);
tmp_mva = next_mva;
} else if (F_PGD_TYPE_IS_SUPERSECTION(*pgd)) {
/* for superseciton: don't need to sync. */
tmp_mva = m4u_calc_next_mva(tmp_mva, end_plus_1, MMU_SUPERSECTION_SIZE);
} else {
/* for section/invalid: don't need to sync */
tmp_mva = m4u_calc_next_mva(tmp_mva, end_plus_1, MMU_SECTION_SIZE);
}
}
return 0;
}
struct kmem_cache *gM4u_pte_kmem = NULL;
int m4u_pte_allocator_init(void)
{
gM4u_pte_kmem = kmem_cache_create("m4u_pte", IMU_BYTES_PER_PTE, IMU_BYTES_PER_PTE, 0, NULL);
M4UINFO("%s: gM4u_pte_kmem = 0x%p, IMU_BYTES_PER_PTE = %d\n", __func__, gM4u_pte_kmem,
(unsigned int)IMU_BYTES_PER_PTE);
if (IS_ERR_OR_NULL(gM4u_pte_kmem)) {
M4UMSG("error in %s: ret = %p\n", __func__, gM4u_pte_kmem);
return -1;
}
return 0;
}
/***********************************************************/
/** allocate a new pte
* @param domain
* @param pgd -- pgd to allocate for
* @param pgprot
*
* @return 0 -- pte is allocated
1 -- pte is not allocated, because it's allocated by others
<0 -- error
* @remark
* @see
* @author K Zhang @date 2013/11/18
************************************************************/
int m4u_alloc_pte(m4u_domain_t *domain, imu_pgd_t *pgd, unsigned int pgprot)
{
void *pte_new_va;
phys_addr_t pte_new;
/* pte_new_va = (unsigned int)kzalloc(IMU_BYTES_PER_PTE, GFP_KERNEL); */
/* pte_new_va = (unsigned int)get_zeroed_page(GFP_KERNEL); */
pte_new_va = kmem_cache_zalloc(gM4u_pte_kmem, GFP_KERNEL);
if (unlikely(!pte_new_va)) {
m4u_aee_print("%s: fail, nomemory\n", __func__);
return -ENOMEM;
}
pte_new = __pa(pte_new_va);
/* check pte alignment -- must 1K align */
if (unlikely(pte_new & (IMU_BYTES_PER_PTE - 1))) {
m4u_aee_print("%s: fail, not algin pa=0x%p, va=0x%p\n", __func__,
(void *)(uintptr_t)pte_new, pte_new_va);
/* kfree(pte_new_va); */
kmem_cache_free(gM4u_pte_kmem, (void *)pte_new_va);
return -ENOMEM;
}
/* lock and check again */
/* because someone else may have allocated for this pgd first */
if (likely(!imu_pgd_val(*pgd))) {
m4u_set_pgd_val(pgd, (unsigned int)(pte_new) | pgprot);
M4ULOG_LOW("%s: pgd: 0x%lx, pte_va:0x%lx, pte_pa: 0x%lx, value: 0x%x\n",
__func__, (unsigned long)pgd, (unsigned long)pte_new_va,
(unsigned long)pte_new, (unsigned int)(pte_new) | pgprot);
return 0;
} else {
/* allocated by other thread */
/* kfree(__va(pte_new)); */
M4ULOG_LOW("m4u pte allocated by others: pgd=0x%p\n", pgd);
kmem_cache_free(gM4u_pte_kmem, (void *)pte_new_va);
return 1;
}
}
int m4u_free_pte(m4u_domain_t *domain, imu_pgd_t *pgd)
{
imu_pte_t *pte_old;
pte_old = imu_pte_map(pgd);
m4u_set_pgd_val(pgd, 0);
/* kfree(pte_old); */
/* free_page(pte_old); */
kmem_cache_free(gM4u_pte_kmem, pte_old);
return 0;
}
/***********************************************************/
/** m4u_map_XX functions.
map mva<->pa
notes: these function doesn't clean pte and invalid tlb
for performance concern.
callers should clean pte + invalid tlb after mapping.
* @author K Zhang @date 2013/11/19
************************************************************/
int m4u_map_16M(m4u_domain_t *m4u_domain, unsigned int mva, unsigned long pa, unsigned int prot)
{
int i;
imu_pgd_t *pgd;
unsigned int pgprot;
unsigned int padscpt;
if ((mva & (~F_PGD_PA_SUPERSECTION_MSK)) != ((unsigned int)pa & (~F_PGD_PA_SUPERSECTION_MSK))) {
m4u_aee_print("error to mk_pte: mva=0x%x, pa=0x%lx, type=%s\n", mva, pa, "supersection");
return -EINVAL;
}
mva &= F_PGD_PA_SUPERSECTION_MSK;
if (pa > 0xffffffffL)
padscpt = (unsigned int)pa & (F_PTE_PA_SMALL_MSK | F_PGD_BIT32_BIT);
else
padscpt = (unsigned int)pa & F_PGD_PA_SUPERSECTION_MSK;
pgprot = __m4u_get_pgd_attr_16M(prot);
pgd = imu_pgd_offset(m4u_domain, mva);
M4ULOG_LOW("%s: mva: 0x%x, pgd: 0x%lx (0x%lx + 0x%x), pa: 0x%lx, value: 0x%x\n",
__func__, mva, (unsigned long)pgd, (unsigned long)((m4u_domain)->pgd),
imu_pgd_index(mva), pa, padscpt | pgprot);
for (i = 0; i < 16; i++) {
if (unlikely(imu_pgd_val(*pgd))) {
m4u_aee_print("%s: mva=0x%x, pgd=0x%x, i=%d\n", __func__, mva, imu_pgd_val(*pgd), i);
goto err_out;
}
m4u_set_pgd_val(pgd, padscpt | pgprot);
pgd++;
}
return 0;
err_out:
for (pgd--; i > 0; i--) {
m4u_set_pgd_val(pgd, 0);
pgd--;
}
return -1;
}
int m4u_map_1M(m4u_domain_t *m4u_domain, unsigned int mva, unsigned long pa, unsigned int prot)
{
imu_pgd_t *pgd;
unsigned int pgprot;
unsigned int padscpt;
if ((mva & (~F_PGD_PA_SECTION_MSK)) != ((unsigned int)pa & (~F_PGD_PA_SECTION_MSK))) {
m4u_aee_print("error to mk_pte: mva=0x%x, pa=0x%lx, type=%s\n", mva, pa, "section");
return -EINVAL;
}
mva &= F_PGD_PA_SECTION_MSK;
if (pa > 0xffffffffL)
padscpt = (unsigned int)pa & (F_PTE_PA_SMALL_MSK | F_PGD_BIT32_BIT);
else
padscpt = (unsigned int)pa & F_PGD_PA_SECTION_MSK;
pgprot = __m4u_get_pgd_attr_1M(prot);
pgd = imu_pgd_offset(m4u_domain, mva);
if (unlikely(imu_pgd_val(*pgd))) {
m4u_aee_print("%s: mva=0x%x, pgd=0x%x\n", __func__, mva, imu_pgd_val(*pgd));
return -1;
}
m4u_set_pgd_val(pgd, padscpt | pgprot);
M4ULOG_LOW("%s: mva: 0x%x, pgd: 0x%lx (0x%lx + 0x%x), pa: 0x%lx, value: 0x%x\n",
__func__, mva, (unsigned long)pgd, (unsigned long)((m4u_domain)->pgd),
imu_pgd_index(mva), pa, padscpt | pgprot);
return 0;
}
int m4u_map_64K(m4u_domain_t *m4u_domain, unsigned int mva, unsigned long pa, unsigned int prot)
{
int ret, i;
imu_pgd_t *pgd;
imu_pte_t *pte;
unsigned int pte_new, pgprot;
unsigned int padscpt;
if ((mva & (~F_PTE_PA_LARGE_MSK)) != ((unsigned int)pa & (~F_PTE_PA_LARGE_MSK))) {
m4u_aee_print("error to mk_pte: mva=0x%x, pa=0x%lx, type=%s\n", mva, pa, "large page");
return -EINVAL;
}
mva &= F_PTE_PA_LARGE_MSK;
if (pa > 0xffffffffL)
padscpt = (unsigned int)pa & (F_PTE_PA_SMALL_MSK | F_PTE_BIT32_BIT);
else
padscpt = (unsigned int)pa & F_PTE_PA_LARGE_MSK;
pgprot = __m4u_get_pgd_attr_page(prot);
pgd = imu_pgd_offset(m4u_domain, mva);
if (!imu_pgd_val(*pgd)) {
ret = m4u_alloc_pte(m4u_domain, pgd, pgprot);
if (ret < 0)
return ret;
else if (ret > 0)
pte_new = 0;
else
pte_new = 1;
} else {
if (unlikely((imu_pgd_val(*pgd) & (~F_PGD_PA_PAGETABLE_MSK)) != pgprot)) {
m4u_aee_print("%s: mva=0x%x, pgd=0x%x, pgprot=0x%x\n",
__func__, mva, imu_pgd_val(*pgd), pgprot);
return -1;
}
pte_new = 0;
}
pgprot = __m4u_get_pte_attr_64K(prot);
pte = imu_pte_offset_map(pgd, mva);
M4ULOG_LOW("%s: mva: 0x%x, pte: 0x%p (0x%lx + 0x%x), pa: 0x%lx, value: 0x%x\n",
__func__, mva, &imu_pte_val(*pte), (unsigned long)imu_pte_map(pgd),
imu_pte_index(mva), pa, padscpt | pgprot);
for (i = 0; i < 16; i++) {
if (unlikely(imu_pte_val(pte[i]))) {
m4u_aee_print("%s: pte=0x%x, i=%d\n", __func__, imu_pte_val(pte[i]), i);
goto err_out;
}
imu_pte_val(pte[i]) = padscpt | pgprot;
}
imu_pte_unmap(pte);
return 0;
err_out:
for (i--; i >= 0; i--)
imu_pte_val(pte[i]) = 0;
imu_pte_unmap(pte);
if (pte_new)
m4u_free_pte(m4u_domain, pgd);
return -1;
}
int m4u_map_4K(m4u_domain_t *m4u_domain, unsigned int mva, unsigned long pa, unsigned int prot)
{
int ret, pte_new;
imu_pgd_t *pgd;
imu_pte_t *pte;
unsigned int pgprot;
unsigned int padscpt;
if ((mva & (~F_PTE_PA_SMALL_MSK)) != ((unsigned int)pa & (~F_PTE_PA_SMALL_MSK))) {
m4u_aee_print("error to mk_pte: mva=0x%x, pa=0x%lx, type=%s\n", mva, pa, "small page");
return -EINVAL;
}
mva &= F_PTE_PA_SMALL_MSK;
if (pa > 0xffffffffL)
padscpt = (unsigned int)pa & (F_PTE_PA_SMALL_MSK | F_PTE_BIT32_BIT);
else
padscpt = (unsigned int)pa & F_PTE_PA_SMALL_MSK;
pgprot = __m4u_get_pgd_attr_page(prot);
pgd = imu_pgd_offset(m4u_domain, mva);
if (!imu_pgd_val(*pgd)) {
ret = m4u_alloc_pte(m4u_domain, pgd, pgprot);
if (ret < 0)
return ret;
else if (ret > 0)
pte_new = 0;
else
pte_new = 1;
} else {
if (unlikely((imu_pgd_val(*pgd) & (~F_PGD_PA_PAGETABLE_MSK)) != pgprot)) {
m4u_aee_print("%s: mva=0x%x, pgd=0x%x, pgprot=0x%x\n",
__func__, mva, imu_pgd_val(*pgd), pgprot);
return -1;
}
pte_new = 0;
}
pgprot = __m4u_get_pte_attr_4K(prot);
pte = imu_pte_offset_map(pgd, mva);
if (unlikely(imu_pte_val(*pte))) {
m4u_aee_print("%s: pte=0x%x\n", __func__, imu_pte_val(*pte));
goto err_out;
}
imu_pte_val(*pte) = padscpt | pgprot;
M4ULOG_LOW("%s: mva: 0x%x, pte: 0x%p (0x%lx + 0x%x), pa: 0x%lx, value: 0x%x\n",
__func__, mva, &imu_pte_val(*pte), (unsigned long)imu_pte_map(pgd),
imu_pte_index(mva), pa, padscpt | imu_pte_val(*pte));
imu_pte_unmap(pte);
return 0;
err_out:
imu_pte_unmap(pte);
if (pte_new)
m4u_free_pte(m4u_domain, pgd);
return -1;
}
/* notes: both iova & paddr should be aligned. */
static inline int m4u_map_phys_align(m4u_domain_t *m4u_domain, unsigned int iova,
unsigned long paddr, unsigned int size, unsigned int prot)
{
int ret;
if (size == SZ_16M)
ret = m4u_map_16M(m4u_domain, iova, paddr, prot);
else if (size == SZ_1M)
ret = m4u_map_1M(m4u_domain, iova, paddr, prot);
else if (size == SZ_64K)
ret = m4u_map_64K(m4u_domain, iova, paddr, prot);
else if (size == SZ_4K)
ret = m4u_map_4K(m4u_domain, iova, paddr, prot);
else {
m4u_aee_print("%s: fail size=0x%x\n", __func__, size);
return -1;
}
return ret;
}
/***********************************************************/
/** map a physical continuous memory to iova (mva).
* @param m4u_domain domain
* @param iova -- iova (mva)
* @param paddr -- physical address
* @param size -- size
* @param prot -- m4u_prot
*
* @return 0 on success, others on fail
* @remark
* @see refer to kernel/drivers/iommu/iommu.c iommu_map()
* @author K Zhang @date 2013/11/19
************************************************************/
int m4u_map_phys_range(m4u_domain_t *m4u_domain, unsigned int iova,
unsigned long paddr, unsigned int size, unsigned int prot)
{
unsigned int min_pagesz;
int ret = 0;
/* find out the minimum page size supported */
min_pagesz = 1 << __ffs(m4u_domain->pgsize_bitmap);
/*
* both the virtual address and the physical one, as well as
* the size of the mapping, must be aligned (at least) to the
* size of the smallest page supported by the hardware
*/
if (!IS_ALIGNED(iova | (unsigned int)paddr | size, min_pagesz)) {
M4UMSG("unaligned: iova 0x%x pa 0x%lx size 0x%x min_pagesz 0x%x\n",
iova, paddr, size, min_pagesz);
return -EINVAL;
}
while (size) {
unsigned long pgsize, addr_merge = (unsigned long)iova | paddr;
unsigned int pgsize_idx;
/* Max page size that still fits into 'size' */
pgsize_idx = __fls(size);
/* need to consider alignment requirements ? */
if (likely(addr_merge)) {
/* Max page size allowed by both iova and paddr */
unsigned int align_pgsize_idx = __ffs(addr_merge);
pgsize_idx = min(pgsize_idx, align_pgsize_idx);
}
/* build a mask of acceptable page sizes */
pgsize = (1UL << (pgsize_idx + 1)) - 1;
/* throw away page sizes not supported by the hardware */
pgsize &= m4u_domain->pgsize_bitmap;
/* make sure we're still sane */
BUG_ON(!pgsize);
/* pick the biggest page */
pgsize_idx = __fls(pgsize);
pgsize = 1UL << pgsize_idx;
M4ULOG_LOW("mapping: iova 0x%x pa 0x%lx pgsize %lu\n", iova, paddr, pgsize);
#if (M4U_DVT == MMU_PT_TYPE_SMALL_PAGE)
if (pgsize > SZ_4K)
pgsize = SZ_4K;
#endif
#if (M4U_DVT == MMU_PT_TYPE_LARGE_PAGE)
if (pgsize > SZ_64K)
pgsize = SZ_64K;
#endif
#if (M4U_DVT == MMU_PT_TYPE_SECTION)
if (pgsize > SZ_1M)
pgsize = SZ_1M;
#endif
#if (M4U_DVT == MMU_PT_TYPE_SUPERSECTION)
if (pgsize > SZ_16M)
pgsize = SZ_16M;
#endif
ret = m4u_map_phys_align(m4u_domain, iova, paddr, pgsize, prot);
if (ret)
break;
iova += pgsize;
paddr += pgsize;
size -= pgsize;
}
/* unroll mapping in case something went wrong */
if (ret)
m4u_unmap(m4u_domain, iova, size);
return ret;
}
int m4u_map_sgtable(m4u_domain_t *m4u_domain, unsigned int mva,
struct sg_table *sg_table, unsigned int size, unsigned int prot)
{
int i, ret;
struct scatterlist *sg;
unsigned int map_mva = mva, map_end = mva + size;
prot = m4u_prot_fixup(prot);
write_lock_domain(m4u_domain);
for_each_sg(sg_table->sgl, sg, sg_table->nents, i) {
dma_addr_t pa;
unsigned int len;
pa = get_sg_phys(sg);
len = sg_dma_len(sg);
#ifdef CONFIG_NEED_SG_DMA_LENGTH
if (0 == sg_dma_address(sg))
len = sg->length;
#endif
M4ULOG_LOW("%s: for_each_sg i: %d, len: %d, mva: 0x%x\n", __func__, i, len, map_mva);
if (map_mva + len > map_end) {
M4UMSG("%s: map_mva(0x%x)+len(0x%x)>end(0x%x)\n", __func__, map_mva, len, map_end);
break;
}
if (len == SZ_4K) { /* for most cases */
ret = m4u_map_4K(m4u_domain, map_mva, pa, prot);
} else {
ret = m4u_map_phys_range(m4u_domain, map_mva, pa, len, prot);
}
if (ret) {
M4UMSG("%s: ret: %d, i: %d, sg->dma: 0x%lx, sg->phy: 0x%lx, sg->offset: 0x%x\n",
__func__, ret, i, (unsigned long)sg_dma_address(sg),
(unsigned long)sg_phys(sg), sg->offset);
goto err_out;
} else {
map_mva += len;
}
}
if (map_mva < map_end) {
M4UMSG("%s: map_mva(0x%x) < map_end(0x%x)\n", __func__, map_mva, map_end);
goto err_out;
}
m4u_clean_pte(m4u_domain, mva, size);
m4u_invalid_tlb_by_range(m4u_domain, mva, mva + size - 1);
write_unlock_domain(m4u_domain);
return 0;
err_out:
write_unlock_domain(m4u_domain);
m4u_unmap(m4u_domain, mva, size);
return -EINVAL;
}
int m4u_check_free_pte(m4u_domain_t *domain, imu_pgd_t *pgd)
{
imu_pte_t *pte;
int i;
pte = imu_pte_map(pgd);
for (i = 0; i < IMU_PTRS_PER_PTE; i++) {
if (imu_pte_val(*pte) != 0)
break;
}
if (i == IMU_PTRS_PER_PTE) {
m4u_free_pte(domain, pgd);
m4u_set_pgd_val(pgd, 0);
return 0;
} else {
return 1;
}
}
int m4u_unmap(m4u_domain_t *domain, unsigned int mva, unsigned int size)
{
imu_pgd_t *pgd;
int i, ret;
unsigned int start = mva;
unsigned long end_plus_1 = (unsigned long)mva + size;
write_lock_domain(domain);
do {
pgd = imu_pgd_offset(domain, mva);
if (F_PGD_TYPE_IS_PAGE(*pgd)) {
imu_pte_t *pte;
unsigned int pte_offset;
unsigned int num_to_clean;
pte_offset = imu_pte_index(mva);
num_to_clean =
min((unsigned int)((end_plus_1 - mva) / PAGE_SIZE),
(unsigned int)(IMU_PTRS_PER_PTE - pte_offset));
pte = imu_pte_offset_map(pgd, mva);
memset(pte, 0, num_to_clean << 2);
ret = m4u_check_free_pte(domain, pgd);
if (ret == 1) { /* pte is not freed, need to flush pte */
m4u_clean_pte(domain, mva, num_to_clean << PAGE_SHIFT);
}
mva += num_to_clean << PAGE_SHIFT;
} else if (F_PGD_TYPE_IS_SECTION(*pgd)) {
m4u_set_pgd_val(pgd, 0);
mva += MMU_SECTION_SIZE;
} else if (F_PGD_TYPE_IS_SUPERSECTION(*pgd)) {
imu_pgd_t *start = imu_supersection_start(pgd);
if (unlikely(start != pgd))
m4u_aee_print("%s: suppersec not align, mva=0x%x, pgd=0x%x\n",
__func__, mva, imu_pgd_val(*pgd));
for (i = 0; i < 16; i++)
imu_pgd_val(start[i]) = 0;
mva = (mva + MMU_SUPERSECTION_SIZE) & (~(MMU_SUPERSECTION_SIZE - 1)); /* must align */
} else {
mva += MMU_SECTION_SIZE;
}
} while (mva < end_plus_1 && mva); /*if the last mva is 0xfff00000, the new mva may be 0 */
m4u_invalid_tlb_by_range(domain, start, end_plus_1 - 1);
write_unlock_domain(domain);
return 0;
}
int m4u_debug_pgtable_show(struct seq_file *s, void *unused)
{
m4u_dump_pgtable(s->private, s);
return 0;
}
int m4u_debug_pgtable_open(struct inode *inode, struct file *file)
{
return single_open(file, m4u_debug_pgtable_show, inode->i_private);
}
const struct file_operations m4u_debug_pgtable_fops = {
.open = m4u_debug_pgtable_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
int m4u_pgtable_init(struct m4u_device *m4u_dev, m4u_domain_t *m4u_domain)
{
/* ======= alloc pagetable======================= */
m4u_domain->pgd =
dma_alloc_coherent(m4u_dev->pDev[0], M4U_PGD_SIZE, &(m4u_domain->pgd_pa), GFP_KERNEL);
if (!(m4u_domain->pgd)) {
M4UMSG("dma_alloc_coherent error! dma memory not available.\n");
return -1;
}
if ((unsigned int)(m4u_domain->pgd_pa) & (M4U_PGD_SIZE - 1)) {
M4UMSG("dma_alloc_coherent memory not align. 0x%lx\n",
(unsigned long)(m4u_domain->pgd_pa));
return -1;
}
M4UINFO("dma_alloc_coherent success! pagetable_va=0x%lx, pagetable_pa=0x%lx.\n",
(unsigned long)(m4u_domain->pgd), (unsigned long)(m4u_domain->pgd_pa));
memset((void *)m4u_domain->pgd, 0, M4U_PGD_SIZE);
/* ======= alloc pagetable done======================= */
if (0 != m4u_pte_allocator_init())
return -1;
debugfs_create_file("pgtable", 0644, m4u_dev->debug_root, m4u_domain, &m4u_debug_pgtable_fops);
return 0;
}
|