hbm 0.1.6

A hardware buffer allocator
Documentation
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright 2024 Google LLC
// SPDX-License-Identifier: MIT

//! BO-related types.
//!
//! This module defines `Bo`.

use super::backends::{
    Backend, Class, Constraint, CopyBuffer, CopyBufferImage, Extent, Flags, Handle, Layout,
    MemoryType,
};
use super::device::Device;
use super::formats;
use super::types::{Access, Error, Format, Mapping, Result, Size};
use super::utils;
use std::os::fd::{BorrowedFd, OwnedFd};
use std::sync::{Arc, Mutex};

struct BoState {
    bound: bool,
    mt: MemoryType,

    mapping: Option<Mapping>,
    map_count: u32,
}

/// A buffer object (BO).
///
/// A BO is an abstraction of a hardware buffer object.
pub struct Bo {
    device: Arc<Device>,
    handle: Handle,

    flags: Flags,
    format: Format,
    backend_index: usize,
    extent: Extent,

    state: Mutex<BoState>,
}

fn merge_class_to_constraint(con: Option<Constraint>, class: &Class) -> Result<Option<Constraint>> {
    if con.is_none() && class.constraint.is_none() {
        return Ok(None);
    }

    let mut con = con.unwrap_or_default();
    if let Some(other) = &class.constraint {
        con.merge(other.clone());
    }

    if !con.modifiers.is_empty() {
        con.modifiers = con
            .modifiers
            .iter()
            .filter(|m1| class.modifiers.iter().any(|m2| *m1 == m2))
            .copied()
            .collect();
        if con.modifiers.is_empty() {
            return Error::unsupported();
        }
    }

    Ok(Some(con))
}

impl Bo {
    fn new(device: Arc<Device>, handle: Handle, class: &Class, extent: Extent) -> Self {
        let state = BoState {
            bound: false,
            mt: MemoryType::empty(),
            mapping: None,
            map_count: 0,
        };

        Self {
            device,
            handle,
            flags: class.flags,
            format: class.format,
            backend_index: class.backend_index,
            extent,
            state: Mutex::new(state),
        }
    }

    /// Creates a BO with an optional constraint.
    pub fn with_constraint(
        device: Arc<Device>,
        class: &Class,
        extent: Extent,
        con: Option<Constraint>,
    ) -> Result<Self> {
        if !class.validate(extent) {
            return Error::user();
        }

        let con = merge_class_to_constraint(con, class)?;

        let backend = device.backend(class.backend_index);
        let handle = backend.with_constraint(class, extent, con)?;
        let bo = Self::new(device, handle, class, extent);

        Ok(bo)
    }

    /// Creates a BO with an explicit physical layout.
    ///
    /// When importing, `dmabuf` can be specified to further restrict the supported memory types.
    pub fn with_layout(
        device: Arc<Device>,
        class: &Class,
        extent: Extent,
        layout: Layout,
        dmabuf: Option<BorrowedFd>,
    ) -> Result<Self> {
        if !class.validate(extent) {
            return Error::user();
        }

        let backend = device.backend(class.backend_index);
        let handle = backend.with_layout(class, extent, layout, dmabuf)?;
        let bo = Self::new(device, handle, class, extent);

        Ok(bo)
    }

    fn can_external(&self) -> bool {
        self.flags.contains(Flags::EXTERNAL)
    }

    fn can_map(&self) -> bool {
        self.flags.contains(Flags::MAP)
    }

    fn can_copy(&self) -> bool {
        self.flags.contains(Flags::COPY)
    }

    fn is_buffer(&self) -> bool {
        self.format.is_invalid()
    }

    fn backend(&self) -> &dyn Backend {
        self.device.backend(self.backend_index)
    }

    /// Returns the physical layout.
    pub fn layout(&self) -> Layout {
        self.backend().layout(&self.handle)
    }

    /// Returns the supported memory types.
    ///
    /// When not importing, the supported memory types can be pre-determined to some degree.  If
    /// two BOs have the same `format`, `modifier`, `flags`, and `usage`, they have the same
    /// supported memory types.
    ///
    /// If two BOs have the same `format` and `modifier`, and if the first BO's `flags` and `usage`
    /// is a subset of the second's, then the first BO's supported memory types is a superset of
    /// the second's.
    ///
    /// When importing, the supported memory types are further restricted by the imported dma-bufs.
    pub fn memory_types(&self) -> Vec<MemoryType> {
        self.backend().memory_types(&self.handle)
    }

    /// Allocates or imports a memory, and binds the memory to a BO.
    ///
    /// A BO without a memory bound cannot be exported, mapped, nor copied.
    ///
    /// As a note, two HBM BOs can refer to the same kernel space BO due to export/import.
    pub fn bind_memory(&mut self, mt: MemoryType, dmabuf: Option<OwnedFd>) -> Result<()> {
        if dmabuf.is_some() && !self.can_external() {
            return Error::user();
        }

        let mut state = self.state.lock().unwrap();
        if state.bound {
            return Error::user();
        }

        let backend = self.device.backend(self.backend_index);
        backend.bind_memory(&mut self.handle, mt, dmabuf)?;

        state.bound = true;
        state.mt = mt;

        Ok(())
    }

    /// Exports a BO as a dma-buf.
    ///
    /// A name can optionally be set for the dma-buf.
    ///
    /// As a note, two userspace dma-buf fds can refer to the same kernel space dma-buf object.
    /// The name is attached to the kernel space dma-buf object, not the userspace dma-buf fds.
    pub fn export_dma_buf(&self, name: Option<&str>) -> Result<OwnedFd> {
        if !self.can_external() {
            return Error::user();
        }

        let state = self.state.lock().unwrap();
        if !state.bound {
            return Error::user();
        }

        self.backend().export_dma_buf(&self.handle, name)
    }

    /// Maps a BO for CPU access.
    ///
    /// Recursive mapping is allowed and returns the same mapping.
    pub fn map(&mut self) -> Result<Mapping> {
        if !self.can_map() {
            return Error::user();
        }

        let mut state = self.state.lock().unwrap();
        if !state.bound || !state.mt.contains(MemoryType::MAPPABLE) {
            return Error::user();
        }

        if state.map_count == 0 {
            let mapping = self.backend().map(&self.handle)?;
            state.mapping = Some(mapping);
            state.map_count = 1;
        } else {
            state.map_count += 1;
        }

        Ok(state.mapping.unwrap())
    }

    /// Unmaps a BO.
    pub fn unmap(&mut self) {
        let mut state = self.state.lock().unwrap();

        match state.map_count {
            0 => (),
            1 => {
                let mapping = state.mapping.take().unwrap();
                self.backend().unmap(&self.handle, mapping);
                state.map_count = 0;
            }
            _ => state.map_count -= 1,
        }
    }

    /// Flushes the CPU cache for the BO mapping.
    ///
    /// If the memory type is coherent, the CPU cache is not flushed.
    pub fn flush(&self) {
        let state = self.state.lock().unwrap();

        if state.map_count > 0 && !state.mt.contains(MemoryType::COHERENT) {
            self.backend().flush(&self.handle);
        }
    }

    /// Invalidates the CPU cache for the BO mapping.
    ///
    /// If the memory type is coherent, the CPU cache is not invalidated.
    pub fn invalidate(&self) {
        let state = self.state.lock().unwrap();

        if state.map_count > 0 && !state.mt.contains(MemoryType::COHERENT) {
            self.backend().invalidate(&self.handle);
        }
    }

    // this should not be used if the mutex needs to remain locked for synchronization
    fn is_bound(&self) -> bool {
        let state = self.state.lock().unwrap();
        state.bound
    }

    fn validate_copy(&self, src: &Bo) -> bool {
        self.can_copy() && self.is_bound() && src.can_copy() && src.is_bound()
    }

    fn validate_copy_buffer(&self, src: &Bo, copy: &CopyBuffer) -> bool {
        if !self.validate_copy(src) || !self.is_buffer() || !src.is_buffer() {
            return false;
        }

        let src_size = src.extent.size();
        let dst_size = self.extent.size();

        copy.size > 0
            && copy.src_offset <= src_size
            && copy.size <= src_size - copy.src_offset
            && copy.dst_offset <= dst_size
            && copy.size <= dst_size - copy.dst_offset
    }

    fn validate_copy_buffer_image(&self, src: &Bo, copy: &CopyBufferImage) -> bool {
        if !self.validate_copy(src) || (self.is_buffer() == src.is_buffer()) {
            return false;
        }

        let size;
        let mut width;
        let mut height;
        let fmt;
        if self.is_buffer() {
            size = self.extent.size();
            width = src.extent.width();
            height = src.extent.height();
            fmt = src.format;
        } else {
            size = src.extent.size();
            width = self.extent.width();
            height = self.extent.height();
            fmt = self.format;
        }

        let fmt_class = formats::format_class(fmt).unwrap();
        let plane_count = fmt_class.plane_count as u32;
        if copy.plane >= plane_count {
            return false;
        }

        let bpp = fmt_class.block_size[copy.plane as usize] as Size;
        width /= fmt_class.block_extent[copy.plane as usize].0 as u32;
        height /= fmt_class.block_extent[copy.plane as usize].1 as u32;

        if copy.offset % bpp != 0
            || copy.stride % bpp != 0
            || copy.stride / bpp < copy.width as Size
        {
            return false;
        }

        copy.width > 0
            && copy.height > 0
            && copy.offset <= size
            && copy.stride <= (size - copy.offset) / copy.height as Size
            && copy.x <= width
            && copy.y <= height
            && copy.width <= width - copy.x
            && copy.height <= height - copy.y
    }

    fn wait_copy(&self, sync_fd: Option<OwnedFd>, wait: bool) -> Option<OwnedFd> {
        if wait {
            sync_fd.and_then(|sync_fd| {
                let _ = utils::poll(sync_fd, Access::Read);
                None
            })
        } else {
            sync_fd
        }
    }

    /// Copies between two BOs that are both buffers.
    ///
    /// `sync_fd` is an optional sync file that the copy operation waits for.
    ///
    /// If `wait` is true, this function never returns any sync file.  Otherwise, it may
    /// return a sync file associated with the copy operation.
    pub fn copy_buffer(
        &self,
        src: &Bo,
        copy: CopyBuffer,
        sync_fd: Option<OwnedFd>,
        wait: bool,
    ) -> Result<Option<OwnedFd>> {
        if !self.validate_copy_buffer(src, &copy) {
            return Error::user();
        }

        self.backend()
            .copy_buffer(&self.handle, &src.handle, copy, sync_fd)
            .map(|sync_fd| self.wait_copy(sync_fd, wait))
    }

    /// Copies between two BOs where one is a buffer and one is an image.
    ///
    /// `sync_fd` is an optional sync file that the copy operation waits for.
    ///
    /// If `wait` is true, this function never returns any sync file.  Otherwise, it may
    /// return a sync file associated with the copy operation.
    pub fn copy_buffer_image(
        &self,
        src: &Bo,
        copy: CopyBufferImage,
        sync_fd: Option<OwnedFd>,
        wait: bool,
    ) -> Result<Option<OwnedFd>> {
        if !self.validate_copy_buffer_image(src, &copy) {
            return Error::user();
        }

        self.backend()
            .copy_buffer_image(&self.handle, &src.handle, copy, sync_fd)
            .map(|sync_fd| self.wait_copy(sync_fd, wait))
    }
}

impl Drop for Bo {
    fn drop(&mut self) {
        self.unmap();
        self.backend().free(&self.handle);
    }
}