Skip to main content

kernel/
sizes.rs

1// SPDX-License-Identifier: GPL-2.0
2
3//! Commonly used sizes.
4//!
5//! C headers: [`include/linux/sizes.h`](srctree/include/linux/sizes.h).
6//!
7//! The top-level `SZ_*` constants are [`usize`]-typed, for use in kernel page
8//! arithmetic and similar CPU-side work.
9//!
10//! The [`SizeConstants`] trait provides the same constants as associated constants
11//! on [`u32`], [`u64`], and [`usize`], for use in device address spaces where
12//! the address width depends on the hardware. Device drivers frequently need
13//! these constants as [`u64`] (or [`u32`]) rather than [`usize`], because
14//! device address spaces are sized independently of the CPU pointer width.
15//!
16//! # Examples
17//!
18//! ```
19//! use kernel::{
20//!     page::PAGE_SIZE,
21//!     sizes::{
22//!         SizeConstants,
23//!         SZ_1M, //
24//!     }, //
25//! };
26//!
27//! // Module-level constants continue to work without a type qualifier.
28//! let num_pages_in_1m = SZ_1M / PAGE_SIZE;
29//!
30//! // Trait associated constants require a type qualifier.
31//! let heap_size = 14 * u64::SZ_1M;
32//! let small = u32::SZ_4K;
33//! ```
34
35macro_rules! define_sizes {
36    ($($type:ty),* $(,)?) => {
37        define_sizes!(@internal [$($type),*]
38            /// `0x0000_0400`.
39            SZ_1K,
40            /// `0x0000_0800`.
41            SZ_2K,
42            /// `0x0000_1000`.
43            SZ_4K,
44            /// `0x0000_2000`.
45            SZ_8K,
46            /// `0x0000_4000`.
47            SZ_16K,
48            /// `0x0000_8000`.
49            SZ_32K,
50            /// `0x0001_0000`.
51            SZ_64K,
52            /// `0x0002_0000`.
53            SZ_128K,
54            /// `0x0004_0000`.
55            SZ_256K,
56            /// `0x0008_0000`.
57            SZ_512K,
58            /// `0x0010_0000`.
59            SZ_1M,
60            /// `0x0020_0000`.
61            SZ_2M,
62            /// `0x0040_0000`.
63            SZ_4M,
64            /// `0x0080_0000`.
65            SZ_8M,
66            /// `0x0100_0000`.
67            SZ_16M,
68            /// `0x0200_0000`.
69            SZ_32M,
70            /// `0x0400_0000`.
71            SZ_64M,
72            /// `0x0800_0000`.
73            SZ_128M,
74            /// `0x1000_0000`.
75            SZ_256M,
76            /// `0x2000_0000`.
77            SZ_512M,
78            /// `0x4000_0000`.
79            SZ_1G,
80            /// `0x8000_0000`.
81            SZ_2G,
82        );
83    };
84
85    (@internal [$($type:ty),*] $($names_and_metas:tt)*) => {
86        define_sizes!(@consts_and_trait $($names_and_metas)*);
87        define_sizes!(@impls [$($type),*] $($names_and_metas)*);
88    };
89
90    (@consts_and_trait $($(#[$meta:meta])* $name:ident,)*) => {
91        $(
92            $(#[$meta])*
93            pub const $name: usize = bindings::$name as usize;
94        )*
95
96        /// Size constants for device address spaces.
97        ///
98        /// Implemented for [`u32`], [`u64`], and [`usize`] so drivers can
99        /// choose the width that matches their hardware. All `SZ_*` values fit
100        /// in a [`u32`], so all implementations are lossless.
101        ///
102        /// # Examples
103        ///
104        /// ```
105        /// use kernel::sizes::SizeConstants;
106        ///
107        /// let gpu_heap = 14 * u64::SZ_1M;
108        /// let mmio_window = u32::SZ_16M;
109        /// ```
110        pub trait SizeConstants {
111            $(
112                $(#[$meta])*
113                const $name: Self;
114            )*
115        }
116    };
117
118    (@impls [] $($(#[$meta:meta])* $name:ident,)*) => {};
119
120    (@impls [$first:ty $(, $rest:ty)*] $($(#[$meta:meta])* $name:ident,)*) => {
121        impl SizeConstants for $first {
122            $(
123                const $name: Self = {
124                    assert!((self::$name as u128) <= (<$first>::MAX as u128));
125                    self::$name as $first
126                };
127            )*
128        }
129
130        define_sizes!(@impls [$($rest),*] $($(#[$meta])* $name,)*);
131    };
132}
133
134define_sizes!(u32, u64, usize);