• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 pub mod sys;
6 
7 use std::io::Error as IoError;
8 
9 use base::SafeDescriptor;
10 use remain::sorted;
11 pub use sys::UdmabufDriver;
12 use thiserror::Error;
13 
14 use crate::GuestAddress;
15 use crate::GuestMemory;
16 use crate::GuestMemoryError;
17 
18 #[sorted]
19 #[derive(Error, Debug)]
20 pub 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.
34 pub type UdmabufResult<T> = std::result::Result<T, UdmabufError>;
35 
36 /// Trait that the platform-specific type `UdmabufDriver` needs to implement.
37 pub trait UdmabufDriverTrait {
38     /// Opens the udmabuf device on success.
new() -> UdmabufResult<Self> where Self: Sized39     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`).
create_udmabuf( &self, mem: &GuestMemory, iovecs: &[(GuestAddress, usize)], ) -> UdmabufResult<SafeDescriptor>44     fn create_udmabuf(
45         &self,
46         mem: &GuestMemory,
47         iovecs: &[(GuestAddress, usize)],
48     ) -> UdmabufResult<SafeDescriptor>;
49 }
50