virtio_gpu/
gpu.rs

1use std::sync::{Arc, Mutex};
2
3use virtio_drivers::{
4    device::gpu::VirtIOGpu,
5    transport::{pci::VirtioPciError, Transport},
6};
7
8use crate::{hal::TwzHal, transport::TwizzlerTransport};
9
10type DeviceImpl<T> = VirtIOGpu<TwzHal, T>;
11
12#[derive(Clone)]
13pub struct DeviceWrapper<T: Transport> {
14    inner: Arc<Mutex<DeviceImpl<T>>>,
15}
16
17impl<T: Transport> DeviceWrapper<T> {
18    fn new(dev: DeviceImpl<T>) -> Self {
19        DeviceWrapper {
20            inner: Arc::new(Mutex::new(dev)),
21        }
22    }
23
24    pub fn with_device<R>(&self, f: impl FnOnce(&mut DeviceImpl<T>) -> R) -> R {
25        f(&mut *self.inner.lock().unwrap())
26    }
27}
28
29// Gets the Virtio Net struct which implements the device used for smoltcp. Use this to create a
30// smoltcp interface to send and receive packets. NOTE: Only the first device used will work
31// properly
32pub fn get_device(
33    notifier: std::sync::mpsc::Sender<Option<()>>,
34) -> Result<DeviceWrapper<TwizzlerTransport>, VirtioPciError> {
35    let gpu = VirtIOGpu::<TwzHal, TwizzlerTransport>::new(TwizzlerTransport::new(notifier)?)
36        .expect("failed to create gpu driver");
37    Ok(DeviceWrapper::<TwizzlerTransport>::new(gpu))
38}