Skip to main content

strat9_kernel/dma/
direction.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23use core::fmt::Debug;
24
25/// Compile-time direction constraint for DMA buffers.
26///
27/// Implemented by three marker types:
28/// - [`DmaToDevice`] : write-only from the CPU perspective
29/// - [`DmaFromDevice`] : read-only from the CPU perspective
30/// - [`DmaBidirectional`] : full read/write
31///
32/// The associated constants are checked at compile time via
33/// `const { assert!(D::CAN_READ) }` blocks, preventing e.g.
34/// reading from a write-only buffer at zero runtime cost.
35pub trait DmaDirection: Debug + sealed::Sealed {
36    /// Whether the CPU may read data transferred from the device.
37    const CAN_READ: bool;
38    /// Whether the CPU may write data to be transferred to the device.
39    const CAN_WRITE: bool;
40}
41
42mod sealed {
43    pub trait Sealed {}
44}
45
46/// Write-only: CPU writes data, device reads it.
47#[derive(Debug, Clone, Copy)]
48pub struct DmaToDevice;
49
50impl sealed::Sealed for DmaToDevice {}
51impl DmaDirection for DmaToDevice {
52    const CAN_READ: bool = false;
53    const CAN_WRITE: bool = true;
54}
55
56/// Read-only: device writes data, CPU reads it.
57#[derive(Debug, Clone, Copy)]
58pub struct DmaFromDevice;
59
60impl sealed::Sealed for DmaFromDevice {}
61impl DmaDirection for DmaFromDevice {
62    const CAN_READ: bool = true;
63    const CAN_WRITE: bool = false;
64}
65
66/// Full read/write in both directions.
67#[derive(Debug, Clone, Copy)]
68pub struct DmaBidirectional;
69
70impl sealed::Sealed for DmaBidirectional {}
71impl DmaDirection for DmaBidirectional {
72    const CAN_READ: bool = true;
73    const CAN_WRITE: bool = true;
74}