@@ -94,19 +94,22 @@ static inline unsigned long get_trans_granule(void)
94
94
* When ARMv8.4-TTL exists, TLBI operations take an additional hint for
95
95
* the level at which the invalidation must take place. If the level is
96
96
* wrong, no invalidation may take place. In the case where the level
97
- * cannot be easily determined, a 0 value for the level parameter will
98
- * perform a non-hinted invalidation.
97
+ * cannot be easily determined, the value TLBI_TTL_UNKNOWN will perform
98
+ * a non-hinted invalidation. Any provided level outside the hint range
99
+ * will also cause fall-back to non-hinted invalidation.
99
100
*
100
101
* For Stage-2 invalidation, use the level values provided to that effect
101
102
* in asm/stage2_pgtable.h.
102
103
*/
103
104
#define TLBI_TTL_MASK GENMASK_ULL(47, 44)
104
105
106
+ #define TLBI_TTL_UNKNOWN INT_MAX
107
+
105
108
#define __tlbi_level (op , addr , level ) do { \
106
109
u64 arg = addr; \
107
110
\
108
111
if (alternative_has_cap_unlikely(ARM64_HAS_ARMv8_4_TTL) && \
109
- level) { \
112
+ level >= 0 && level <= 3 ) { \
110
113
u64 ttl = level & 3; \
111
114
ttl |= get_trans_granule() << 2; \
112
115
arg &= ~TLBI_TTL_MASK; \
@@ -122,28 +125,34 @@ static inline unsigned long get_trans_granule(void)
122
125
} while (0)
123
126
124
127
/*
125
- * This macro creates a properly formatted VA operand for the TLB RANGE.
126
- * The value bit assignments are:
128
+ * This macro creates a properly formatted VA operand for the TLB RANGE. The
129
+ * value bit assignments are:
127
130
*
128
131
* +----------+------+-------+-------+-------+----------------------+
129
132
* | ASID | TG | SCALE | NUM | TTL | BADDR |
130
133
* +-----------------+-------+-------+-------+----------------------+
131
134
* |63 48|47 46|45 44|43 39|38 37|36 0|
132
135
*
133
- * The address range is determined by below formula:
134
- * [BADDR, BADDR + (NUM + 1) * 2^(5*SCALE + 1) * PAGESIZE)
136
+ * The address range is determined by below formula: [BADDR, BADDR + (NUM + 1) *
137
+ * 2^(5*SCALE + 1) * PAGESIZE)
138
+ *
139
+ * Note that the first argument, baddr, is pre-shifted; If LPA2 is in use, BADDR
140
+ * holds addr[52:16]. Else BADDR holds page number. See for example ARM DDI
141
+ * 0487J.a section C5.5.60 "TLBI VAE1IS, TLBI VAE1ISNXS, TLB Invalidate by VA,
142
+ * EL1, Inner Shareable".
135
143
*
136
144
*/
137
- #define __TLBI_VADDR_RANGE (addr , asid , scale , num , ttl ) \
138
- ({ \
139
- unsigned long __ta = (addr) >> PAGE_SHIFT; \
140
- __ta &= GENMASK_ULL(36, 0); \
141
- __ta |= (unsigned long)(ttl) << 37; \
142
- __ta |= (unsigned long)(num) << 39; \
143
- __ta |= (unsigned long)(scale) << 44; \
144
- __ta |= get_trans_granule() << 46; \
145
- __ta |= (unsigned long)(asid) << 48; \
146
- __ta; \
145
+ #define __TLBI_VADDR_RANGE (baddr , asid , scale , num , ttl ) \
146
+ ({ \
147
+ unsigned long __ta = (baddr); \
148
+ unsigned long __ttl = (ttl >= 1 && ttl <= 3) ? ttl : 0; \
149
+ __ta &= GENMASK_ULL(36, 0); \
150
+ __ta |= __ttl << 37; \
151
+ __ta |= (unsigned long)(num) << 39; \
152
+ __ta |= (unsigned long)(scale) << 44; \
153
+ __ta |= get_trans_granule() << 46; \
154
+ __ta |= (unsigned long)(asid) << 48; \
155
+ __ta; \
147
156
})
148
157
149
158
/* These macros are used by the TLBI RANGE feature. */
@@ -216,12 +225,16 @@ static inline unsigned long get_trans_granule(void)
216
225
* CPUs, ensuring that any walk-cache entries associated with the
217
226
* translation are also invalidated.
218
227
*
219
- * __flush_tlb_range(vma, start, end, stride, last_level)
228
+ * __flush_tlb_range(vma, start, end, stride, last_level, tlb_level )
220
229
* Invalidate the virtual-address range '[start, end)' on all
221
230
* CPUs for the user address space corresponding to 'vma->mm'.
222
231
* The invalidation operations are issued at a granularity
223
232
* determined by 'stride' and only affect any walk-cache entries
224
- * if 'last_level' is equal to false.
233
+ * if 'last_level' is equal to false. tlb_level is the level at
234
+ * which the invalidation must take place. If the level is wrong,
235
+ * no invalidation may take place. In the case where the level
236
+ * cannot be easily determined, the value TLBI_TTL_UNKNOWN will
237
+ * perform a non-hinted invalidation.
225
238
*
226
239
*
227
240
* Finally, take a look at asm/tlb.h to see how tlb_flush() is implemented
@@ -345,34 +358,44 @@ static inline void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
345
358
* @tlb_level: Translation Table level hint, if known
346
359
* @tlbi_user: If 'true', call an additional __tlbi_user()
347
360
* (typically for user ASIDs). 'flase' for IPA instructions
361
+ * @lpa2: If 'true', the lpa2 scheme is used as set out below
348
362
*
349
363
* When the CPU does not support TLB range operations, flush the TLB
350
364
* entries one by one at the granularity of 'stride'. If the TLB
351
365
* range ops are supported, then:
352
366
*
353
- * 1. If 'pages' is odd, flush the first page through non-range
354
- * operations;
367
+ * 1. If FEAT_LPA2 is in use, the start address of a range operation must be
368
+ * 64KB aligned, so flush pages one by one until the alignment is reached
369
+ * using the non-range operations. This step is skipped if LPA2 is not in
370
+ * use.
371
+ *
372
+ * 2. The minimum range granularity is decided by 'scale', so multiple range
373
+ * TLBI operations may be required. Start from scale = 3, flush the largest
374
+ * possible number of pages ((num+1)*2^(5*scale+1)) that fit into the
375
+ * requested range, then decrement scale and continue until one or zero pages
376
+ * are left. We must start from highest scale to ensure 64KB start alignment
377
+ * is maintained in the LPA2 case.
355
378
*
356
- * 2. For remaining pages: the minimum range granularity is decided
357
- * by 'scale', so multiple range TLBI operations may be required.
358
- * Start from scale = 0, flush the corresponding number of pages
359
- * ((num+1)*2^(5*scale+1) starting from 'addr'), then increase it
360
- * until no pages left.
379
+ * 3. If there is 1 page remaining, flush it through non-range operations. Range
380
+ * operations can only span an even number of pages. We save this for last to
381
+ * ensure 64KB start alignment is maintained for the LPA2 case.
361
382
*
362
383
* Note that certain ranges can be represented by either num = 31 and
363
384
* scale or num = 0 and scale + 1. The loop below favours the latter
364
385
* since num is limited to 30 by the __TLBI_RANGE_NUM() macro.
365
386
*/
366
387
#define __flush_tlb_range_op (op , start , pages , stride , \
367
- asid , tlb_level , tlbi_user ) \
388
+ asid , tlb_level , tlbi_user , lpa2 ) \
368
389
do { \
369
390
int num = 0; \
370
- int scale = 0; \
391
+ int scale = 3; \
392
+ int shift = lpa2 ? 16 : PAGE_SHIFT; \
371
393
unsigned long addr; \
372
394
\
373
395
while (pages > 0) { \
374
396
if (!system_supports_tlb_range() || \
375
- pages % 2 == 1) { \
397
+ pages == 1 || \
398
+ (lpa2 && start != ALIGN(start, SZ_64K))) { \
376
399
addr = __TLBI_VADDR(start, asid); \
377
400
__tlbi_level(op, addr, tlb_level); \
378
401
if (tlbi_user) \
@@ -384,20 +407,20 @@ do { \
384
407
\
385
408
num = __TLBI_RANGE_NUM(pages, scale); \
386
409
if (num >= 0) { \
387
- addr = __TLBI_VADDR_RANGE(start, asid, scale, \
388
- num, tlb_level); \
410
+ addr = __TLBI_VADDR_RANGE(start >> shift , asid, \
411
+ scale, num, tlb_level); \
389
412
__tlbi(r##op, addr); \
390
413
if (tlbi_user) \
391
414
__tlbi_user(r##op, addr); \
392
415
start += __TLBI_RANGE_PAGES(num, scale) << PAGE_SHIFT; \
393
416
pages -= __TLBI_RANGE_PAGES(num, scale); \
394
417
} \
395
- scale++ ; \
418
+ scale-- ; \
396
419
} \
397
420
} while (0)
398
421
399
422
#define __flush_s2_tlb_range_op (op , start , pages , stride , tlb_level ) \
400
- __flush_tlb_range_op(op, start, pages, stride, 0, tlb_level, false)
423
+ __flush_tlb_range_op(op, start, pages, stride, 0, tlb_level, false, kvm_lpa2_is_enabled());
401
424
402
425
static inline void __flush_tlb_range (struct vm_area_struct * vma ,
403
426
unsigned long start , unsigned long end ,
@@ -427,9 +450,11 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma,
427
450
asid = ASID (vma -> vm_mm );
428
451
429
452
if (last_level )
430
- __flush_tlb_range_op (vale1is , start , pages , stride , asid , tlb_level , true);
453
+ __flush_tlb_range_op (vale1is , start , pages , stride , asid ,
454
+ tlb_level , true, lpa2_is_enabled ());
431
455
else
432
- __flush_tlb_range_op (vae1is , start , pages , stride , asid , tlb_level , true);
456
+ __flush_tlb_range_op (vae1is , start , pages , stride , asid ,
457
+ tlb_level , true, lpa2_is_enabled ());
433
458
434
459
dsb (ish );
435
460
mmu_notifier_arch_invalidate_secondary_tlbs (vma -> vm_mm , start , end );
@@ -441,9 +466,10 @@ static inline void flush_tlb_range(struct vm_area_struct *vma,
441
466
/*
442
467
* We cannot use leaf-only invalidation here, since we may be invalidating
443
468
* table entries as part of collapsing hugepages or moving page tables.
444
- * Set the tlb_level to 0 because we can not get enough information here.
469
+ * Set the tlb_level to TLBI_TTL_UNKNOWN because we can not get enough
470
+ * information here.
445
471
*/
446
- __flush_tlb_range (vma , start , end , PAGE_SIZE , false, 0 );
472
+ __flush_tlb_range (vma , start , end , PAGE_SIZE , false, TLBI_TTL_UNKNOWN );
447
473
}
448
474
449
475
static inline void flush_tlb_kernel_range (unsigned long start , unsigned long end )
0 commit comments