Skip to main content

mesa3d_util/
defines.rs

1// Copyright 2025 Google
2// SPDX-License-Identifier: MIT
3
4use std::fmt;
5use std::time::Duration;
6
7use crate::error::MesaError;
8use crate::error::MesaResult;
9use crate::OwnedDescriptor;
10
11/// Mapped memory caching flags (see virtio_gpu spec)
12pub const MESA_MAP_CACHE_MASK: u32 = 0x0f;
13pub const MESA_MAP_CACHE_CACHED: u32 = 0x01;
14pub const MESA_MAP_CACHE_UNCACHED: u32 = 0x02;
15pub const MESA_MAP_CACHE_WC: u32 = 0x03;
16/// Access flags (not in virtio_gpu spec)
17pub const MESA_MAP_ACCESS_MASK: u32 = 0xf0;
18pub const MESA_MAP_ACCESS_READ: u32 = 0x10;
19pub const MESA_MAP_ACCESS_WRITE: u32 = 0x20;
20pub const MESA_MAP_ACCESS_RW: u32 = 0x30;
21
22/// Mesa handle types (memory and sync in same namespace)
23pub const MESA_HANDLE_TYPE_MEM_OPAQUE_FD: u32 = 0x0001;
24pub const MESA_HANDLE_TYPE_MEM_DMABUF: u32 = 0x0002;
25pub const MESA_HANDLE_TYPE_MEM_OPAQUE_WIN32: u32 = 0x0003;
26pub const MESA_HANDLE_TYPE_MEM_SHM: u32 = 0x0004;
27pub const MESA_HANDLE_TYPE_MEM_ZIRCON: u32 = 0x0005;
28
29pub const MESA_HANDLE_TYPE_SIGNAL_OPAQUE_FD: u32 = 0x0010;
30pub const MESA_HANDLE_TYPE_SIGNAL_SYNC_FD: u32 = 0x0020;
31pub const MESA_HANDLE_TYPE_SIGNAL_OPAQUE_WIN32: u32 = 0x0030;
32pub const MESA_HANDLE_TYPE_SIGNAL_ZIRCON: u32 = 0x0040;
33pub const MESA_HANDLE_TYPE_SIGNAL_EVENT_FD: u32 = 0x0050;
34
35/// Handle to OS-specific memory or synchronization objects.
36pub struct MesaHandle {
37    pub os_handle: OwnedDescriptor,
38    pub handle_type: u32,
39}
40
41impl MesaHandle {
42    /// Clones an existing Mesahandle, by using OS specific mechanisms.
43    pub fn try_clone(&self) -> MesaResult<MesaHandle> {
44        let clone = self
45            .os_handle
46            .try_clone()
47            .map_err(|_| MesaError::InvalidMesaHandle)?;
48        Ok(MesaHandle {
49            os_handle: clone,
50            handle_type: self.handle_type,
51        })
52    }
53}
54
55#[repr(C)]
56#[derive(Copy, Clone, Debug)]
57pub struct MesaMapping {
58    pub ptr: u64,
59    pub size: u64,
60}
61
62impl fmt::Debug for MesaHandle {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        f.debug_struct("Handle debug").finish()
65    }
66}
67
68pub enum TubeType {
69    Stream,
70    Packet,
71}
72
73pub enum WaitTimeout {
74    Finite(Duration),
75    NoTimeout,
76}
77
78pub struct WaitEvent {
79    pub connection_id: u64,
80    pub hung_up: bool,
81    pub readable: bool,
82}
83
84#[allow(dead_code)]
85pub const WAIT_CONTEXT_MAX: usize = 16;
86
87pub enum DescriptorType {
88    Unknown,
89    Memory(u32),
90    WritePipe,
91}
92
93/// # Safety
94///
95/// Caller must ensure that MappedRegion's lifetime contains the lifetime of
96/// pointer returned.
97pub unsafe trait MappedRegion: Send + Sync {
98    /// Returns a pointer to the beginning of the memory region. Should only be
99    /// used for passing this region to ioctls for setting guest memory.
100    fn as_ptr(&self) -> *mut u8;
101
102    /// Returns the size of the memory region in bytes.
103    fn size(&self) -> usize;
104
105    /// Returns mesa mapping representation of the region
106    fn as_mesa_mapping(&self) -> MesaMapping;
107}
108
109#[macro_export]
110macro_rules! log_status {
111    ($result:expr) => {
112        match $result {
113            Ok(_) => (),
114            Err(e) => error!("Error recieved: {}", e),
115        }
116    };
117}