blob: 48d4c55848dd3684f52259c030e63893cb113cac [file] [log] [blame]
Alice Wangf47b2342023-06-02 11:51:57 +00001// Copyright 2023, 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//! Utility functions for memory management.
16
Alice Wang3fa9b802023-06-06 07:52:31 +000017use crate::read_sysreg;
Alice Wangeacb7382023-06-05 12:53:54 +000018use crate::util::unchecked_align_down;
Alice Wang3fa9b802023-06-06 07:52:31 +000019use core::arch::asm;
Alice Wangf47b2342023-06-02 11:51:57 +000020use core::ptr::NonNull;
Alice Wang3fa9b802023-06-06 07:52:31 +000021use zeroize::Zeroize;
Alice Wangf47b2342023-06-02 11:51:57 +000022
Alice Wangeacb7382023-06-05 12:53:54 +000023/// The size of a 4KB memory in bytes.
24pub const SIZE_4KB: usize = 4 << 10;
Pierre-Clément Tosic332fae2023-06-22 11:37:12 +000025/// The size of a 64KB memory in bytes.
26pub const SIZE_64KB: usize = 64 << 10;
Pierre-Clément Tosif3681e82023-06-22 11:38:22 +000027/// The size of a 128KB memory in bytes.
28pub const SIZE_128KB: usize = 128 << 10;
Alice Wangeacb7382023-06-05 12:53:54 +000029/// The size of a 2MB memory in bytes.
30pub const SIZE_2MB: usize = 2 << 20;
31/// The size of a 4MB memory in bytes.
32pub const SIZE_4MB: usize = 4 << 20;
33
Alice Wang4b3cc112023-06-06 12:22:53 +000034/// The page size in bytes assumed by vmbase - 4 KiB.
35pub const PAGE_SIZE: usize = SIZE_4KB;
36
Alice Wang3fa9b802023-06-06 07:52:31 +000037/// Reads the number of words in the smallest cache line of all the data caches and unified caches.
38#[inline]
39pub fn min_dcache_line_size() -> usize {
40 const DMINLINE_SHIFT: usize = 16;
41 const DMINLINE_MASK: usize = 0xf;
42 let ctr_el0 = read_sysreg!("ctr_el0");
43
44 // DminLine: log2 of the number of words in the smallest cache line of all the data caches.
45 let dminline = (ctr_el0 >> DMINLINE_SHIFT) & DMINLINE_MASK;
46
47 1 << dminline
48}
49
50/// Flush `size` bytes of data cache by virtual address.
51#[inline]
52pub(super) fn flush_region(start: usize, size: usize) {
53 let line_size = min_dcache_line_size();
54 let end = start + size;
55 let start = unchecked_align_down(start, line_size);
56
57 for line in (start..end).step_by(line_size) {
Andrew Walbranc06e7342023-07-05 14:00:51 +000058 // SAFETY: Clearing cache lines shouldn't have Rust-visible side effects.
Alice Wang3fa9b802023-06-06 07:52:31 +000059 unsafe {
60 asm!(
61 "dc cvau, {x}",
62 x = in(reg) line,
63 options(nomem, nostack, preserves_flags),
64 )
65 }
66 }
67}
68
69/// Flushes the slice to the point of unification.
70#[inline]
71pub fn flush(reg: &[u8]) {
72 flush_region(reg.as_ptr() as usize, reg.len())
73}
74
75/// Overwrites the slice with zeroes, to the point of unification.
76#[inline]
77pub fn flushed_zeroize(reg: &mut [u8]) {
78 reg.zeroize();
79 flush(reg)
80}
81
Alice Wangeacb7382023-06-05 12:53:54 +000082/// Computes the address of the 4KiB page containing a given address.
83pub const fn page_4kb_of(addr: usize) -> usize {
84 unchecked_align_down(addr, SIZE_4KB)
85}
86
Alice Wangf47b2342023-06-02 11:51:57 +000087/// Returns the intermediate physical address corresponding to the given virtual address.
88///
89/// As we use identity mapping for everything, this is just a cast, but it's useful to use it to be
90/// explicit about where we are converting from virtual to physical address.
91pub fn virt_to_phys(vaddr: NonNull<u8>) -> usize {
92 vaddr.as_ptr() as _
93}
94
95/// Returns a pointer for the virtual address corresponding to the given non-zero intermediate
96/// physical address.
97///
98/// Panics if `paddr` is 0.
99pub fn phys_to_virt(paddr: usize) -> NonNull<u8> {
100 NonNull::new(paddr as _).unwrap()
101}