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
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2023-2026 ARM Ltd.
*/
#include <linux/arm-rmi-cmds.h>
#include <linux/cpufeature.h>
#include <linux/memblock.h>
#include <linux/memory.h>
#include <linux/mmzone.h>
#include <linux/slab.h>
#include <asm/memory.h>
#include <asm/pgtable-hwdef.h>
/* RMM v2.0 defines RmiFeatureRegister0 to RmiFeatureRegister4. */
static unsigned long rmi_feat_reg_cache[5] __ro_after_init;
static bool arm64_rmm_active;
static bool arm64_rmi_is_available;
/* is_rmm_active: Returns if the RMM is ACTIVE */
bool is_rmm_active(void)
{
return READ_ONCE(arm64_rmm_active);
}
EXPORT_SYMBOL_GPL(is_rmm_active);
/*
* is_rmi_available: Returns if the RMM is ACTIVE and is configured with static
* carveout for metadata nad L1GPTs.
*/
bool is_rmi_available(void)
{
return READ_ONCE(arm64_rmi_is_available);
}
EXPORT_SYMBOL_GPL(is_rmi_available);
/*
* rmi_granule_range_delegate() - Delegate granules
* @base: PA of the first granule of the range
* @top: PA of the first granule after the range
* @out_top: PA of the first granule not delegated
*
* Delegate a range of granule for use by the realm world. If the entire range
* was delegated then @out_top == @top, otherwise the function should be called
* again with @base == @out_top.
*
* Return: 0 on success, positive RMI result code or negative Linux error code
*/
static long rmi_granule_range_delegate(unsigned long base,
unsigned long top,
unsigned long *out_top)
{
struct arm_smccc_1_2_regs regs = {
SMC_RMI_GRANULE_RANGE_DELEGATE, base, top
};
long ret = rmi_sro_execute(®s);
if (ret == RMI_SUCCESS && out_top)
*out_top = regs.a1;
return ret;
}
/*
* rmi_delegate_range: Delegate a physically contiguous range.
* We iterate over the range until we hit an error. So we may
* return an error, but with a partially delegated range. The
* caller must always look at the @out_phys to figure out, how
* much progress was made.
*
* @phys: Base of the physical address range
* @size: Size of the physical address range
* @out_phys: Top of the range that was completed. This is always
* valid, irrespective of the result.
*
* Returns RMI_SUCCESS on successful completion. Otherwise, returns
* the Linux error number or the RMI status code as described
* by the RMM spec for RMI_GRANULE_DELEGATE_RANGE or RMI_BLOCKED.
*/
int rmi_delegate_range(phys_addr_t phys,
unsigned long size,
phys_addr_t *out_phys)
{
long ret = 0;
unsigned long top = phys + size;
unsigned long out_top;
while (phys < top) {
ret = rmi_granule_range_delegate(phys, top, &out_top);
if (ret != RMI_SUCCESS)
break;
/*
* Buggy RMM ? Let the caller handle the failure.
* We can't know how far the RMM delegated in this
* iteration, so we return the best known good limit.
* RMM can deal with granules already in "undelegated"
* in a given range. So, it is fine for the caller to
* try the range we return.
*/
if (WARN_ON(out_top <= phys)) {
ret = -ENXIO;
break;
}
phys = out_top;
}
if (out_phys)
*out_phys = phys;
return ret;
}
EXPORT_SYMBOL_GPL(rmi_delegate_range);
/*
* rmi_granule_range_undelegate() - Undelegate a range of granules
* @base: Base PA of the target range
* @top: Top PA of the target range
* @out_top: Returns the top PA of range whose state is undelegated
*
* Undelegate a range of granules to allow use by the normal world. Will fail
* if the granules are in use by RMM. RMM can ignore granules that are already
* undelegated and thus is safe to be called on a range with a mix of delegated
* and undelegated granules.
*
* Return: 0 on success, positive RMI result code or negative Linux error code
*/
static long rmi_granule_range_undelegate(unsigned long base,
unsigned long top,
unsigned long *out_top)
{
struct arm_smccc_1_2_regs regs = {
SMC_RMI_GRANULE_RANGE_UNDELEGATE, base, top
};
long ret = rmi_sro_execute(®s);
if (ret == RMI_SUCCESS && out_top)
*out_top = regs.a1;
return ret;
}
int rmi_undelegate_range(phys_addr_t phys,
unsigned long size)
{
long ret = 0;
unsigned long top = phys + size;
unsigned long out_top;
while (phys < top) {
ret = rmi_granule_range_undelegate(phys, top, &out_top);
if (ret != RMI_SUCCESS)
break;
/* Buggy RMM ? Let the caller leak the pages */
if (WARN_ON(out_top <= phys))
return -ENXIO;
phys = out_top;
}
return ret;
}
EXPORT_SYMBOL_GPL(rmi_undelegate_range);
/*
* rmi_free_delegated_page: Undelegate and free a page that has been previously
* delegated to the Realm world. If we are unable to undelegate it, the page is
* leaked.
* NOTE: Do not use this helper if the page could be concurrently operated by
* another thread, as it may get leaked if the undelegation fails due to RMI_BLOCKED
*/
int rmi_free_delegated_page(phys_addr_t phys)
{
if (WARN_ON_ONCE(rmi_undelegate_page(phys))) {
/* Undelegate failed: leak the page */
return -EBUSY;
}
free_page((unsigned long)phys_to_virt(phys));
return 0;
}
EXPORT_SYMBOL_GPL(rmi_free_delegated_page);
/*
* Convert the RmiAddrBlockSize to actual size. This is used in RmiDonateReq
* and RmiAddrRangeDesc*.
*/
static unsigned long rmi_addr_block_size_to_bytes(unsigned long block_size_fld)
{
return BIT(ARM64_HW_PGTABLE_LEVEL_SHIFT(3 - block_size_fld));
}
/*
* free_addr_range: Free memory described by the address range entry, which may
* be partially consumed by RMM.
*
* @entry: RMI_ADDR_RANGE descriptor
* @consumed_size: Page aligned size consumed by the RMM from the address range.
*
* If the state of the address is DELEGATED, undelegate it back, before freeing.
* Leaks the memory if we cannot undelegate the range.
*/
static void free_addr_range(unsigned long entry, unsigned long consumed_size)
{
unsigned long phys = RMI_ADDR_RANGE_ADDR(entry);
unsigned long block_size_fld = RMI_ADDR_RANGE_BLOCK_SIZE(entry);
unsigned long count = RMI_ADDR_RANGE_COUNT(entry);
unsigned long state = RMI_ADDR_RANGE_STATE(entry);
unsigned long size = rmi_addr_block_size_to_bytes(block_size_fld) * count;
WARN_ON(!PAGE_ALIGNED(phys) || !PAGE_ALIGNED(consumed_size));
/* We shouldn't see this in reclaim path, leak it for now */
if (WARN_ON((state != RMI_OP_MEM_DELEGATED) &&
(state != RMI_OP_MEM_UNDELEGATED)))
return;
/* Adjust the address and size for partially consumed entry */
phys += consumed_size;
size -= consumed_size;
/*
* Undelegate the pages back if required. If we can't
* change them back, leak the pages.
*/
if (state == RMI_OP_MEM_DELEGATED &&
WARN_ON(rmi_undelegate_range(phys, size)))
return;
free_pages_exact(phys_to_virt(phys), size);
}
static void rmi_op_continue(unsigned long sro_handle, unsigned long flags,
struct arm_smccc_1_2_regs *out_regs)
{
*out_regs = (struct arm_smccc_1_2_regs) {
SMC_RMI_OP_CONTINUE, sro_handle, flags
};
rmi_smccc_invoke(out_regs);
}
static void rmi_op_cancel(unsigned long sro_handle,
struct arm_smccc_1_2_regs *out_regs)
{
*out_regs = (struct arm_smccc_1_2_regs) {
SMC_RMI_OP_CANCEL, sro_handle
};
rmi_smccc_invoke(out_regs);
}
static void rmi_op_mem_donate(unsigned long sro_handle, unsigned long list_addr,
unsigned long list_count, unsigned long flags,
struct arm_smccc_1_2_regs *out_regs)
{
*out_regs = (struct arm_smccc_1_2_regs) {
SMC_RMI_OP_MEM_DONATE, sro_handle, list_addr, list_count, flags
};
/*
* The output donated count (a1) is always valid, irrespective
* of the return result. i.e., 0 if there was an error
*/
rmi_smccc_invoke(out_regs);
}
static void rmi_op_mem_reclaim(unsigned long sro_handle,
unsigned long list_addr,
unsigned long list_count,
struct arm_smccc_1_2_regs *out_regs)
{
*out_regs = (struct arm_smccc_1_2_regs) {
SMC_RMI_OP_MEM_RECLAIM, sro_handle, list_addr, list_count
};
rmi_smccc_invoke(out_regs);
}
static int rmi_sro_ensure_capacity(struct rmi_sro_state *sro,
unsigned long count)
{
if (WARN_ON_ONCE(sro->addr_count > RMI_MAX_ADDR_LIST))
return -EOVERFLOW;
if (count > RMI_MAX_ADDR_LIST - sro->addr_count)
return -ENOSPC;
return 0;
}
static int rmi_sro_donate_contig(struct rmi_sro_state *sro,
unsigned long sro_handle,
unsigned long donatereq,
struct arm_smccc_1_2_regs *out_regs,
gfp_t gfp)
{
unsigned long block_size_fld = RMI_DONATE_BLOCK_SIZE(donatereq);
unsigned long block_size = rmi_addr_block_size_to_bytes(block_size_fld);
unsigned long count = RMI_DONATE_COUNT(donatereq);
unsigned long state = RMI_DONATE_STATE(donatereq);
unsigned long size = block_size * count;
unsigned long addr_range;
unsigned long donated_size;
int ret;
void *virt;
phys_addr_t phys;
/*
* The RMM specification requires contiguous allocations are always a
* power of 2
*/
if (WARN_ON_ONCE(!is_power_of_2(size)))
return -EINVAL;
/*
* RMM clamps the Maximum value of RmiOpMemDonateReq:count to prevent
* overflow in the RMI_ADDR_RANGE_COUNT field.
*/
if (WARN_ON_ONCE(count > FIELD_MAX(RMI_ADDR_RANGE_COUNT_MASK)))
return -EINVAL;
/*
* We can't allocate blocks more than what the buddy allocator can
* satisfy. TODO: Add support for larger allocations.
*/
if (get_order(size) > MAX_PAGE_ORDER)
return -ENOMEM;
/* Reuse the cached address range if we have one */
for (int i = 0; i < sro->addr_count; i++) {
unsigned long entry = sro->addr_list[i];
if (RMI_ADDR_RANGE_BLOCK_SIZE(entry) == block_size_fld &&
RMI_ADDR_RANGE_COUNT(entry) == count &&
RMI_ADDR_RANGE_STATE(entry) == state &&
IS_ALIGNED(RMI_ADDR_RANGE_ADDR(entry), size)) {
sro->addr_count--;
swap(sro->addr_list[sro->addr_count],
sro->addr_list[i]);
goto mem_donate;
}
}
ret = rmi_sro_ensure_capacity(sro, 1);
/* In the odd case we have filled up the cache, free up the entire list */
if (ret)
rmi_sro_free(sro);
virt = alloc_pages_exact(size, gfp);
if (!virt)
return -ENOMEM;
phys = virt_to_phys(virt);
if (state == RMI_OP_MEM_DELEGATED) {
phys_addr_t delegated_phys;
if (rmi_delegate_range(phys, size, &delegated_phys)) {
if (!rmi_undelegate_range(phys, delegated_phys - phys))
free_pages_exact(virt, size);
return -ENXIO;
}
}
addr_range = phys & RMI_ADDR_RANGE_ADDR_MASK;
FIELD_MODIFY(RMI_ADDR_RANGE_BLOCK_SIZE_MASK, &addr_range, block_size_fld);
FIELD_MODIFY(RMI_ADDR_RANGE_COUNT_MASK, &addr_range, count);
FIELD_MODIFY(RMI_ADDR_RANGE_STATE_MASK, &addr_range, state);
sro->addr_list[sro->addr_count] = addr_range;
mem_donate:
rmi_op_mem_donate(sro_handle,
virt_to_phys(&sro->addr_list[sro->addr_count]), 1,
0, out_regs);
donated_size = out_regs->a1 << PAGE_SHIFT;
if (WARN_ON(out_regs->a1 > (size >> PAGE_SHIFT)))
donated_size = size;
/* All granules consumed by the RMM */
if (donated_size == size)
return 0;
/*
* No granules were consumed by the RMM, cache them. This is a valid
* case, where the RMM was donated memory by another competing thread.
*/
if (donated_size == 0) {
sro->addr_count++;
return 0;
}
/* The granules were partially consumed, reclaim the unused ones. */
free_addr_range(sro->addr_list[sro->addr_count], donated_size);
return 0;
}
static int rmi_sro_donate_noncontig(struct rmi_sro_state *sro,
unsigned long sro_handle,
unsigned long donatereq,
struct arm_smccc_1_2_regs *out_regs,
gfp_t gfp)
{
unsigned long block_size_fld = RMI_DONATE_BLOCK_SIZE(donatereq);
unsigned long block_size = rmi_addr_block_size_to_bytes(block_size_fld);
unsigned long count = RMI_DONATE_COUNT(donatereq);
unsigned long state = RMI_DONATE_STATE(donatereq);
unsigned long found = 0;
unsigned long donated_granules;
unsigned long granules_per_block = block_size >> PAGE_SHIFT;
unsigned long consumed_blocks;
int addr_list_start = sro->addr_count;
int ret, i, src;
/*
* We can't allocate blocks more than what the buddy allocator can
* satisfy. TODO: Add support for larger allocations.
*/
if (get_order(block_size) > MAX_PAGE_ORDER)
return -ENOMEM;
/*
* Maximum value of RmiOpMemDonateReq:count is 2^(PAGE_SHIFT-2) - 1.
* But we further clamp it down by the number of entries we can do
* in one go. The RMM can request the remaining in the next iteration.
*/
if (count > RMI_MAX_ADDR_LIST)
count = RMI_MAX_ADDR_LIST;
/* Gather the suitable entries to the end of the list */
i = 0;
while (i < addr_list_start && found < count) {
unsigned long entry = sro->addr_list[i];
if (RMI_ADDR_RANGE_BLOCK_SIZE(entry) == block_size_fld &&
RMI_ADDR_RANGE_COUNT(entry) == 1 &&
RMI_ADDR_RANGE_STATE(entry) == state) {
addr_list_start--;
swap(sro->addr_list[addr_list_start],
sro->addr_list[i]);
found++;
/* Continue from the swapped in entry */
continue;
}
/* Skip past the entry */
i++;
}
ret = rmi_sro_ensure_capacity(sro, count - found);
if (ret) {
/* If we have found some entries, donate them and try again */
if (found)
goto mem_donate;
/* Otherwise free up the list and start again */
rmi_sro_free(sro);
/* Reset the addr_list_start to match sro->addr_count */
addr_list_start = 0;
}
for (; found < count; found++) {
unsigned long addr_range;
void *virt = alloc_pages_exact(block_size, gfp);
phys_addr_t phys;
if (!virt)
return -ENOMEM;
phys = virt_to_phys(virt);
if (state == RMI_OP_MEM_DELEGATED) {
phys_addr_t delegated_phys;
if (rmi_delegate_range(phys, block_size, &delegated_phys)) {
if (!rmi_undelegate_range(phys, delegated_phys - phys))
free_pages_exact(virt, block_size);
return -ENXIO;
}
}
addr_range = phys & RMI_ADDR_RANGE_ADDR_MASK;
FIELD_MODIFY(RMI_ADDR_RANGE_BLOCK_SIZE_MASK, &addr_range, block_size_fld);
FIELD_MODIFY(RMI_ADDR_RANGE_COUNT_MASK, &addr_range, 1);
FIELD_MODIFY(RMI_ADDR_RANGE_STATE_MASK, &addr_range, state);
sro->addr_list[sro->addr_count++] = addr_range;
}
mem_donate:
rmi_op_mem_donate(sro_handle,
virt_to_phys(&sro->addr_list[addr_list_start]),
found, 0, out_regs);
donated_granules = out_regs->a1;
/*
* The RMM shouldn't report more granules than we provided, but clamp
* just in case.
*/
if (WARN_ON_ONCE(donated_granules > found * granules_per_block))
donated_granules = found * granules_per_block;
/*
* The RMM reports the consumed memory in terms of granules, but we
* track in the address lists in block-sized ranges. So divide to get
* the number of (complete) consumed blocks.
*/
consumed_blocks = donated_granules / granules_per_block;
if (donated_granules % granules_per_block) {
/*
* A block has been partially consumed, the start is owned by
* the RMM, the tail is owned by the host
*/
unsigned long entry =
sro->addr_list[addr_list_start + consumed_blocks];
unsigned long donated_size =
(donated_granules % granules_per_block) << PAGE_SHIFT;
free_addr_range(entry, donated_size);
/*
* This block is now fully 'consumed' (either held by the RMM or
* freed)
*/
consumed_blocks++;
}
/*
* Keep just the blocks the RMM didn't use in addr_list
* RMM claimed consumed_blocks entries from addr_list_start.
* Move the entries left out at the end i.e.,
* [ addr_list_start + consumed_blocks, addr_list_start + found)
* to the rest of the valid entries and adjust the addr_count to
* reflect the available entries.
*/
for (i = 0, src = addr_list_start + consumed_blocks;
i < found - consumed_blocks; i++)
sro->addr_list[addr_list_start + i] = sro->addr_list[src + i];
sro->addr_count -= consumed_blocks;
return 0;
}
static int rmi_sro_donate(struct rmi_sro_state *sro,
unsigned long sro_handle,
unsigned long donatereq,
struct arm_smccc_1_2_regs *regs,
gfp_t gfp)
{
unsigned long state = RMI_DONATE_STATE(donatereq);
if (WARN_ON_ONCE(!RMI_DONATE_COUNT(donatereq)))
return -EINVAL;
/*
* We do not support RMI_OP_MEM_CONDITIONAL yet. This is only required
* for use in RMI_GRANULE_TRACKING_SET, which we don't support yet.
*/
if (WARN_ON_ONCE((state != RMI_OP_MEM_DELEGATED) &&
(state != RMI_OP_MEM_UNDELEGATED)))
return -EINVAL;
if (RMI_DONATE_CONTIG(donatereq) == RMI_OP_MEM_CONTIG)
return rmi_sro_donate_contig(sro, sro_handle, donatereq, regs, gfp);
else
return rmi_sro_donate_noncontig(sro, sro_handle, donatereq, regs, gfp);
}
static int rmi_sro_reclaim(struct rmi_sro_state *sro,
unsigned long sro_handle,
struct arm_smccc_1_2_regs *out_regs)
{
unsigned long capacity;
/*
* We don't do a partial free of the entries. So for now free the
* entire address list as we prepare to reclaim more from the RMM.
*/
if (rmi_sro_ensure_capacity(sro, 1))
rmi_sro_free(sro);
capacity = RMI_MAX_ADDR_LIST - sro->addr_count;
rmi_op_mem_reclaim(sro_handle,
virt_to_phys(&sro->addr_list[sro->addr_count]),
capacity, out_regs);
/*
* RMI_OP_MEM_RECLAIM always return RMI_INCOMPLETE, except when the
* input parameters were invalid.
*/
if (WARN_ON_ONCE(RMI_RESULT_STATUS(out_regs->a0) != RMI_INCOMPLETE))
return -EINVAL;
if (WARN_ON_ONCE(out_regs->a1 > capacity))
out_regs->a1 = capacity;
sro->addr_count += out_regs->a1;
return 0;
}
void rmi_sro_free(struct rmi_sro_state *sro)
{
/* Handle the worse */
if (WARN_ON(sro->addr_count < 0))
return;
if (WARN_ON(sro->addr_count > RMI_MAX_ADDR_LIST))
sro->addr_count = RMI_MAX_ADDR_LIST;
for (int i = 0; i < sro->addr_count; i++)
free_addr_range(sro->addr_list[i], 0);
sro->addr_count = 0;
}
EXPORT_SYMBOL_GPL(rmi_sro_free);
long rmi_sro_memxfer_execute(struct rmi_sro_state *sro, gfp_t gfp)
{
struct arm_smccc_1_2_regs *regs = &sro->regs;
bool cancelled = false;
unsigned long sro_handle;
rmi_smccc_invoke(regs);
sro_handle = regs->a1;
while (RMI_RESULT_STATUS(regs->a0) == RMI_INCOMPLETE) {
bool can_cancel = RMI_RESULT_CAN_CANCEL(regs->a0) == RMI_OP_CAN_CANCEL;
int ret = 0;
switch (RMI_RESULT_MEMREQ(regs->a0)) {
case RMI_OP_MEM_REQ_NONE:
rmi_op_continue(sro_handle, RMI_CONTINUE_KEEP_GOING,
regs);
break;
case RMI_OP_MEM_REQ_DONATE:
ret = rmi_sro_donate(sro, sro_handle, regs->a2, regs,
gfp);
break;
case RMI_OP_MEM_REQ_RECLAIM:
ret = rmi_sro_reclaim(sro, sro_handle, regs);
break;
default:
WARN_ON_ONCE(1);
ret = -ENXIO;
}
if (ret) {
/*
* All memory donating SROs must be cancellable. So a
* failure in memory allocation shouldn't be an issue.
* However, if we encounter a random failure (e.g.,
* buggy RMM), don't loop forever, just give up.
*/
if (WARN_ON_ONCE(!can_cancel))
return ret;
/*
* If we have already cancelled, and came back here due
* to an error in MEMREQ, then there is no point
* in going in loops.
*/
if (WARN_ON_ONCE(cancelled))
break;
rmi_op_cancel(sro_handle, regs);
cancelled = true;
if (WARN_ON_ONCE(RMI_RESULT_STATUS(regs->a0) != RMI_INCOMPLETE))
return ret;
}
}
if (cancelled)
return -ECANCELED;
return regs->a0;
}
EXPORT_SYMBOL_GPL(rmi_sro_memxfer_execute);
/*
* rmi_sro_execute: Execute an RMI command that is stateful but not memory
* transferring. Takes regs, filled with the FIDs and the arguments in place.
*
* Returns :
* -ECANCELED - If the operation had to be aborted and SRO was cancellable.
* Otherwise, returns the result of the RMI command.
*/
long rmi_sro_execute(struct arm_smccc_1_2_regs *regs)
{
bool cancelled = false;
unsigned long sro_handle;
rmi_smccc_invoke(regs);
sro_handle = regs->a1;
while (RMI_RESULT_STATUS(regs->a0) == RMI_INCOMPLETE) {
bool can_cancel = RMI_RESULT_CAN_CANCEL(regs->a0) == RMI_OP_CAN_CANCEL;
switch (RMI_RESULT_MEMREQ(regs->a0)) {
case RMI_OP_MEM_REQ_NONE:
rmi_op_continue(sro_handle, RMI_CONTINUE_KEEP_GOING,
regs);
break;
default:
WARN_ON_ONCE(1);
if (!can_cancel)
return regs->a0;
/*
* We can't get here normally, but handle this anyway
* for a buggy RMM implementation.
*/
if (cancelled)
return -ECANCELED;
rmi_op_cancel(sro_handle, regs);
cancelled = true;
}
}
if (cancelled)
return -ECANCELED;
return regs->a0;
}
EXPORT_SYMBOL_GPL(rmi_sro_execute);
static int rmi_check_version(void)
{
unsigned short version_major, version_minor;
unsigned long host_version = RMI_ABI_VERSION(RMI_ABI_MAJOR_VERSION,
RMI_ABI_MINOR_VERSION);
unsigned long aa64pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
struct arm_smccc_1_2_regs res = {
SMC_RMI_VERSION, host_version,
};
/* If RME isn't supported, then RMI can't be */
if (cpuid_feature_extract_unsigned_field(aa64pfr0, ID_AA64PFR0_EL1_RME_SHIFT) == 0)
return -ENXIO;
if (arm_smccc_1_1_get_conduit() != SMCCC_CONDUIT_SMC)
return -ENXIO;
rmi_smccc_invoke(&res);
if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
return -ENXIO;
version_major = RMI_ABI_VERSION_GET_MAJOR(res.a1);
version_minor = RMI_ABI_VERSION_GET_MINOR(res.a1);
if (res.a0 != RMI_SUCCESS) {
unsigned short high_version_major, high_version_minor;
high_version_major = RMI_ABI_VERSION_GET_MAJOR(res.a2);
high_version_minor = RMI_ABI_VERSION_GET_MINOR(res.a2);
pr_err("Unsupported RMI ABI (v%d.%d - v%d.%d) we want v%d.%d\n",
version_major, version_minor,
high_version_major, high_version_minor,
RMI_ABI_MAJOR_VERSION,
RMI_ABI_MINOR_VERSION);
return -ENXIO;
}
pr_info("RMI ABI version %d.%d\n", version_major, version_minor);
return 0;
}
static int rmi_read_features(void)
{
/*
* Since we've negotiated a compatible version these feature registers
* should always be available
*/
for (int i = 0; i < ARRAY_SIZE(rmi_feat_reg_cache); i++) {
struct arm_smccc_1_2_regs args = {
SMC_RMI_FEATURES, i,
};
rmi_smccc_invoke(&args);
if (WARN_ON(args.a0 != RMI_SUCCESS))
return -EINVAL;
rmi_feat_reg_cache[i] = args.a1;
}
return 0;
}
unsigned long rmi_feat_reg(unsigned int index)
{
if (WARN_ON(index >= ARRAY_SIZE(rmi_feat_reg_cache)))
return 0;
return rmi_feat_reg_cache[index];
}
EXPORT_SYMBOL_GPL(rmi_feat_reg);
static int rmi_rmm_config_set(unsigned long cfg_ptr)
{
struct arm_smccc_1_2_regs regs = {
SMC_RMI_RMM_CONFIG_SET, cfg_ptr,
};
rmi_smccc_invoke(®s);
return regs.a0;
}
static int rmi_configure(void)
{
unsigned long granule_feature;
unsigned long granule_size;
int ret;
switch (PAGE_SIZE) {
case SZ_4K:
granule_size = RMI_GRANULE_SIZE_4KB;
granule_feature = RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_4KB;
break;
case SZ_16K:
granule_size = RMI_GRANULE_SIZE_16KB;
granule_feature = RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_16KB;
break;
case SZ_64K:
granule_size = RMI_GRANULE_SIZE_64KB;
granule_feature = RMI_FEATURE_REGISTER_1_RMI_GRAN_SZ_64KB;
break;
default:
BUILD_BUG();
}
if (!(rmi_feat_reg(1) & granule_feature)) {
pr_err("RMM does not support %luKB granules\n",
PAGE_SIZE >> 10);
return -ENXIO;
}
struct rmm_config *config __free(free_page) =
(struct rmm_config *)get_zeroed_page(GFP_KERNEL);
if (!config) {
pr_err("Unable to allocate memory for RMM config\n");
return -ENOMEM;
}
config->rmi_granule_size = granule_size;
/*
* For now we set the tracking_region_size to 0 which is the only option
* for 4KB PAGE_SIZE (1GB for 4KB PAGE_SIZE, 32MB/512MB for 16KB/64KB).
* TODO: Support other tracking sizes via Kconfig option for other
* PAGE_SIZES
*/
config->tracking_region_size = 0;
ret = rmi_rmm_config_set(virt_to_phys(config));
if (ret) {
pr_err("RMM config set failed (%d)\n", ret);
return -EINVAL;
}
return 0;
}
/**
* rmi_granule_tracking_get() - Get configuration of a Granule tracking region
* @start: Base PA of the tracking region
* @end: End of the PA region
* @out_category: Memory category
* @out_state: Tracking region state
* @out_top: Top of the memory region
*
* Return: RMI return code
*/
static int rmi_granule_tracking_get(unsigned long start,
unsigned long end,
unsigned long *out_category,
unsigned long *out_state,
unsigned long *out_top)
{
struct arm_smccc_1_2_regs regs = {
SMC_RMI_GRANULE_TRACKING_GET, start, end,
};
rmi_smccc_invoke(®s);
if (regs.a0 != RMI_SUCCESS)
return regs.a0;
if (out_category)
*out_category = regs.a1;
if (out_state)
*out_state = regs.a2;
if (out_top)
*out_top = regs.a3;
return RMI_SUCCESS;
}
/*
* Make sure the area is tracked by RMM at FINE granularity.
* We do not support changing the tracking yet.
*/
static int rmi_verify_memory_tracking(phys_addr_t start, phys_addr_t end)
{
while (start < end) {
unsigned long ret, category, state, next;
ret = rmi_granule_tracking_get(start, end, &category, &state, &next);
if (ret != RMI_SUCCESS) {
pr_err("RMI_GRANULE_TRACKING_GET failed for %llx-%llx: (%lu)\n",
start, end, ret);
return -ENXIO;
}
if (WARN_ON(next <= start))
return -ENXIO;
if (state != RMI_TRACKING_FINE ||
category != RMI_MEM_CATEGORY_CONVENTIONAL) {
/* TODO: Set granule tracking in this case */
pr_err("Granule tracking for region isn't fine/conventional: %llx-%lx\n",
start, next);
return -ENODEV;
}
start = next;
}
return 0;
}
/*
* rmi_gpt_info - Query the GPT info for the given PAR.
* @start: Base of the physical address region
* @end: Top of the physical address region
* @out_top: Top of the physical address region for which the GPT @out_gpt_par_state
* is valid
* @out_gpt_par_state: State of the GPT covered by [start, out_top)
*/
static long rmi_gpt_info(unsigned long start, unsigned long end,
unsigned long *out_top,
unsigned long *out_gpt_par_state)
{
struct arm_smccc_1_2_regs regs = {
SMC_RMI_GPT_INFO, start, end,
};
rmi_smccc_invoke(®s);
if (regs.a0 != RMI_SUCCESS)
return regs.a0;
if (out_top)
*out_top = regs.a1;
if (out_gpt_par_state)
*out_gpt_par_state = regs.a2;
return RMI_SUCCESS;
}
/*
* We do not support creating L1 GPTs yet. So, make sure that all the regions
* are managed by the firmware.
*/
static int rmi_verify_gpt_firmware_managed(phys_addr_t start, phys_addr_t end)
{
unsigned long l0gpt_sz;
unsigned long next, par_state;
l0gpt_sz = 1UL << (30 + FIELD_GET(RMI_FEATURE_REGISTER_1_L0GPTSZ,
rmi_feat_reg(1)));
start = ALIGN_DOWN(start, l0gpt_sz);
end = ALIGN(end, l0gpt_sz);
while (start < end) {
long ret = rmi_gpt_info(start, end, &next, &par_state);
if (ret != RMI_SUCCESS) {
pr_err("RMI_GPT_INFO failed for %llx-%llx: (%ld)\n",
start, end, ret);
return -ENXIO;
}
if (WARN_ON(next <= start))
return -ENXIO;
if (par_state != RMI_GPT_PAR_PLAT) {
pr_err("GPT for the region is not managed by firmware %llx-%lx\n",
start, next);
return -ENODEV;
}
start = next;
}
return 0;
}
static int rmi_prepare_memory(phys_addr_t start, phys_addr_t end)
{
int ret;
if (start >= end)
return -EINVAL;
ret = rmi_verify_memory_tracking(start, end);
if (ret)
return ret;
return rmi_verify_gpt_firmware_managed(start, end);
}
static int rmi_init_metadata(void)
{
phys_addr_t start, end;
struct memblock_region *r;
for_each_mem_region(r) {
int ret;
/* Firmware-reserved NOMAP regions are not usable system RAM */
if (memblock_is_nomap(r))
continue;
start = PAGE_ALIGN(r->base);
/*
* We always deal with PAGE_SIZE and if in the odd case this
* region boundary is not PAGE aligned, we stick to the page
* that we can use from the region.
*/
end = PAGE_ALIGN_DOWN(r->base + r->size);
/* Too small ? */
if (start >= end)
continue;
ret = rmi_prepare_memory(start, end);
if (ret)
return ret;
}
return 0;
}
static int rmi_memory_notifier(struct notifier_block *nb,
unsigned long action, void *data)
{
struct memory_notify *arg = data;
phys_addr_t start, end;
int ret;
if (action != MEM_GOING_ONLINE)
return NOTIFY_DONE;
start = PFN_PHYS(arg->start_pfn);
end = PFN_PHYS(arg->start_pfn + arg->nr_pages);
/*
* If the hotplugged memory region is in ZONE_MOVABLE, neither the
* guest-memfd nor the kernel allocations can come from there. So we are
* fine to skip this.
* For boot time memory, we don't allow this discount, as the
* decision to move boot memory pages to ZONE_MOVABLE is purely
* a kernel choice and the firmware must have decided what it covers.
*/
if (is_zone_movable_page(pfn_to_page(arg->start_pfn)))
return NOTIFY_DONE;
ret = rmi_prepare_memory(start, end);
return notifier_from_errno(ret);
}
static struct notifier_block rmi_memory_nb = {
.notifier_call = rmi_memory_notifier,
};
static int rmi_init_memory(void)
{
int ret;
ret = rmi_init_metadata();
if (ret)
return ret;
return register_memory_notifier(&rmi_memory_nb);
}
static int __init arm64_init_rmi(void)
{
int ret;
/* If we can't agree on the RMI ABI version, don't proceed further */
ret = rmi_check_version();
if (ret)
return ret;
ret = rmi_read_features();
if (ret)
return ret;
ret = rmi_configure();
if (ret)
return ret;
/* Activate the RMM */
struct rmi_sro_state *sro __free(kfree) = kmalloc_obj(*sro);
if (!sro)
return -ENOMEM;
ret = rmi_sro_memxfer_cmd(sro, GFP_KERNEL, SMC_RMI_RMM_ACTIVATE);
if (ret) {
pr_err("RMM activate failed (%d)\n", ret);
ret = ret < 0 ? ret : -ENXIO;
return ret;
}
WRITE_ONCE(arm64_rmm_active, true);
ret = rmi_init_memory();
if (ret) {
/* Deactivate the RMM */
if (!WARN_ON(rmi_sro_memxfer_cmd(sro, GFP_KERNEL, SMC_RMI_RMM_DEACTIVATE)))
WRITE_ONCE(arm64_rmm_active, false);
return ret;
}
/* Mark the RMM is usable */
WRITE_ONCE(arm64_rmi_is_available, true);
pr_info("RMI configured\n");
return 0;
}
/*
* Note arm64_init_rmi() must be called before kvm_init_rmi() otherwise KVM
* will not support realm guests. subsys_initcall() is called before
* module_init() (used for KVM) so this is OK.
*/
subsys_initcall(arm64_init_rmi);
|