1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//! Manage events for a device, including mailbox messages and interrupts.

use std::{
    collections::VecDeque,
    sync::{atomic::Ordering, Arc, Mutex},
};

use futures::future::select_all;
use twizzler_abi::{
    device::{
        BusType, DeviceInterruptFlags, DeviceRepr, InterruptVector, MailboxPriority,
        NUM_DEVICE_INTERRUPTS,
    },
    kso::KactionError,
    syscall::{ThreadSyncFlags, ThreadSyncReference, ThreadSyncSleep},
};
use twizzler_async::{Async, AsyncSetup};

use super::Device;

struct DeviceEventStreamInner {
    msg_queue: Vec<VecDeque<u64>>,
}

impl DeviceEventStreamInner {
    fn new() -> Self {
        Self {
            msg_queue: (0..(MailboxPriority::Num as usize))
                .into_iter()
                .map(|_| VecDeque::new())
                .collect(),
        }
    }
}

struct IntInner {
    inum: usize,
    repr: Arc<Device>,
}

impl IntInner {
    fn repr(&self) -> &DeviceRepr {
        self.repr.repr()
    }

    fn new(repr: Arc<Device>, inum: usize) -> Self {
        Self { inum, repr }
    }
}

impl AsyncSetup for IntInner {
    type Error = bool;

    const WOULD_BLOCK: Self::Error = true;

    fn setup_sleep(&self) -> twizzler_abi::syscall::ThreadSyncSleep {
        let repr = self.repr();
        repr.setup_interrupt_sleep(self.inum)
    }
}

struct MailboxInner {
    repr: Arc<Device>,
    inum: usize,
}

impl Unpin for MailboxInner {}
impl Unpin for IntInner {}

impl MailboxInner {
    fn repr(&self) -> &DeviceRepr {
        self.repr.repr()
    }

    fn new(repr: Arc<Device>, inum: usize) -> Self {
        Self { inum, repr }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
/// Possible errors for interrupt allocation.
pub enum InterruptAllocationError {
    /// The device has run out of interrupt vectors that can be used.
    NoMoreInterrupts,
    /// Some option was unsupported.
    Unsupported,
    /// The kernel encountered an error.
    KernelError(KactionError),
}

impl AsyncSetup for MailboxInner {
    type Error = bool;

    const WOULD_BLOCK: Self::Error = true;

    fn setup_sleep(&self) -> twizzler_abi::syscall::ThreadSyncSleep {
        ThreadSyncSleep::new(
            ThreadSyncReference::Virtual(&self.repr().mailboxes[self.inum]),
            0,
            twizzler_abi::syscall::ThreadSyncOp::Equal,
            ThreadSyncFlags::empty(),
        )
    }
}

/// A manager for device events, including interrupt handling.
pub struct DeviceEventStream {
    inner: Mutex<DeviceEventStreamInner>,
    asyncs: Vec<Async<IntInner>>,
    async_mb: Vec<Async<MailboxInner>>,
    device: Arc<Device>,
}

/// A handle for an allocated interrupt on a device.
pub struct InterruptInfo {
    es: Arc<DeviceEventStream>,
    _vec: InterruptVector,
    devint: u32,
    inum: usize,
}

impl InterruptInfo {
    /// Wait until the next interrupt occurs.
    pub async fn next(&self) -> Option<u64> {
        self.es.next(self.inum).await
    }

    /// Get the interrupt number for programming the device.
    pub fn devint(&self) -> u32 {
        self.devint
    }
}

impl Drop for InterruptInfo {
    fn drop(&mut self) {
        self.es.free_interrupt(self)
    }
}

impl DeviceEventStream {
    pub(crate) fn free_interrupt(&self, _ii: &InterruptInfo) {
        // TODO
    }

    /// Allocate a new interrupt on this device.
    pub(crate) fn allocate_interrupt(
        self: &Arc<Self>,
    ) -> Result<InterruptInfo, InterruptAllocationError> {
        // SAFETY: We grab ownership of the interrupt repr data via the atomic swap.
        for i in 0..NUM_DEVICE_INTERRUPTS {
            if self.device.repr().interrupts[i]
                .taken
                .swap(1, std::sync::atomic::Ordering::SeqCst)
                == 0
            {
                let (vec, devint) = match self.device.bus_type() {
                    BusType::Pcie => self.device.allocate_interrupt(i)?,
                    _ => return Err(InterruptAllocationError::Unsupported),
                };
                self.device
                    .repr_mut()
                    .register_interrupt(i, vec, DeviceInterruptFlags::empty());
                return Ok(InterruptInfo {
                    es: self.clone(),
                    _vec: vec,
                    devint,
                    inum: i,
                });
            }
        }
        Err(InterruptAllocationError::NoMoreInterrupts)
    }

    pub(crate) fn new(device: Arc<Device>) -> Self {
        let asyncs = (0..NUM_DEVICE_INTERRUPTS)
            .into_iter()
            .map(|i| Async::new(IntInner::new(device.clone(), i)))
            .collect();
        let async_mb = (0..(MailboxPriority::Num as usize))
            .into_iter()
            .map(|i| Async::new(MailboxInner::new(device.clone(), i)))
            .collect();
        Self {
            inner: Mutex::new(DeviceEventStreamInner::new()),
            asyncs,
            async_mb,
            device,
        }
    }

    fn repr(&self) -> &DeviceRepr {
        self.device.repr()
    }

    pub(crate) fn check_mailbox(&self, pri: MailboxPriority) -> Option<u64> {
        let mut inner = self.inner.lock().unwrap();
        inner.msg_queue[pri as usize].pop_front()
    }

    fn future_of_int(
        &self,
        inum: usize,
    ) -> impl std::future::Future<Output = Result<(usize, u64), bool>> + '_ {
        Box::pin(self.asyncs[inum].run_with(move |ii| {
            ii.repr()
                .check_for_interrupt(ii.inum)
                .ok_or(true)
                .map(|x| (inum, x))
        }))
    }

    fn future_of_mb(
        &self,
        inum: usize,
    ) -> impl std::future::Future<Output = Result<(usize, u64), bool>> + '_ {
        Box::pin(self.async_mb[inum].run_with(move |ii| {
            ii.repr()
                .check_for_mailbox(ii.inum)
                .ok_or(true)
                .map(|x| (inum, x))
        }))
    }

    fn check_add_msg(&self, i: usize) {
        if let Some(x) = self.repr().check_for_mailbox(i) {
            self.inner.lock().unwrap().msg_queue[i].push_back(x)
        }
    }

    pub(crate) async fn next(&self, int: usize) -> Option<u64> {
        if self.repr().interrupts[int].taken.load(Ordering::SeqCst) == 0 {
            return None;
        }
        if let Some(x) = self.repr().check_for_interrupt(int) {
            return Some(x);
        }

        let fut = self.future_of_int(int);
        fut.await.ok().map(|x| x.1)
    }

    pub(crate) async fn next_msg(&self, min: MailboxPriority) -> (MailboxPriority, u64) {
        loop {
            for i in 0..(MailboxPriority::Num as usize) {
                self.check_add_msg(i);
            }

            for i in ((min as usize)..(MailboxPriority::Num as usize)).rev() {
                if let Some(x) = self.check_mailbox(i.try_into().unwrap()) {
                    return (i.try_into().unwrap(), x);
                }
            }

            let futs = ((min as usize)..(MailboxPriority::Num as usize))
                .into_iter()
                .map(|i| self.future_of_mb(i));

            let (pri, x) = select_all(futs).await.0.unwrap();
            self.inner.lock().unwrap().msg_queue[pri].push_back(x);
        }
    }
}