vm_memory/udmabuf/
mod.rs

1// Copyright 2022 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5pub mod sys;
6
7use std::io::Error as IoError;
8
9use base::SafeDescriptor;
10use remain::sorted;
11pub use sys::UdmabufDriver;
12use thiserror::Error;
13
14use crate::GuestAddress;
15use crate::GuestMemory;
16use crate::GuestMemoryError;
17
18#[sorted]
19#[derive(Error, Debug)]
20pub enum UdmabufError {
21    #[error("failed to create buffer: {0:?}")]
22    DmabufCreationFail(IoError),
23    #[error("failed to open udmabuf driver: {0:?}")]
24    DriverOpenFailed(IoError),
25    #[error("failed to get region offset: {0:?}")]
26    InvalidOffset(GuestMemoryError),
27    #[error("All guest addresses must aligned to 4KiB")]
28    NotPageAligned,
29    #[error("udmabuf is not supported on this platform")]
30    UdmabufUnsupported,
31}
32
33/// The result of an operation in this file.
34pub type UdmabufResult<T> = std::result::Result<T, UdmabufError>;
35
36/// Trait that the platform-specific type `UdmabufDriver` needs to implement.
37pub trait UdmabufDriverTrait {
38    /// Opens the udmabuf device on success.
39    fn new() -> UdmabufResult<Self>
40    where
41        Self: Sized;
42
43    /// Creates a dma-buf fd for the given scatter-gather list of guest memory pages (`iovecs`).
44    fn create_udmabuf(
45        &self,
46        mem: &GuestMemory,
47        iovecs: &[(GuestAddress, usize)],
48    ) -> UdmabufResult<SafeDescriptor>;
49}