blob: 906295740ff8d4563b8db5aae57968d3db066d7c [file] [log] [blame]
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +01001// Copyright 2022, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Miscellaneous helper functions.
16
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000017use core::arch::asm;
18
Pierre-Clément Tosif0f9b8b2022-10-19 10:12:49 +010019pub const SIZE_4KB: usize = 4 << 10;
Pierre-Clément Tosia1d3ea32022-11-01 15:05:11 +000020pub const SIZE_2MB: usize = 2 << 20;
Pierre-Clément Tosif0f9b8b2022-10-19 10:12:49 +010021
Pierre-Clément Tosie8726e42022-10-17 13:35:27 +010022/// Computes the largest multiple of the provided alignment smaller or equal to the address.
23///
24/// Note: the result is undefined if alignment isn't a power of two.
25pub const fn unchecked_align_down(addr: usize, alignment: usize) -> usize {
26 addr & !(alignment - 1)
27}
28
29/// Safe wrapper around unchecked_align_up() that validates its assumptions and doesn't wrap.
30pub const fn align_up(addr: usize, alignment: usize) -> Option<usize> {
31 if !alignment.is_power_of_two() {
32 None
33 } else if let Some(s) = addr.checked_add(alignment - 1) {
34 Some(unchecked_align_down(s, alignment))
35 } else {
36 None
37 }
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +010038}
39
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +010040/// Computes the address of the 4KiB page containing a given address.
Pierre-Clément Tosi446136e2022-10-19 10:10:42 +010041pub const fn page_4kb_of(addr: usize) -> usize {
Pierre-Clément Tosie8726e42022-10-17 13:35:27 +010042 unchecked_align_down(addr, SIZE_4KB)
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +010043}
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000044
45#[inline]
46fn min_dcache_line_size() -> usize {
47 const DMINLINE_SHIFT: usize = 16;
48 const DMINLINE_MASK: usize = 0xf;
49 let ctr_el0: usize;
50
51 unsafe { asm!("mrs {x}, ctr_el0", x = out(reg) ctr_el0) }
52
53 // DminLine: log2 of the number of words in the smallest cache line of all the data caches.
54 let dminline = (ctr_el0 >> DMINLINE_SHIFT) & DMINLINE_MASK;
55
56 1 << dminline
57}
58
59#[inline]
60/// Flush data cache over the entire slice.
61pub fn flush_region(start: usize, size: usize) {
62 let line_size = min_dcache_line_size();
63 let end = start + size;
64 let start = unchecked_align_down(start, line_size);
65
66 for line in (start..end).step_by(line_size) {
67 // SAFETY - Clearing cache lines shouldn't have Rust-visible side effects.
68 unsafe { asm!("dc cvau, {x}", x = in(reg) line) }
69 }
70}