summary refs log tree commit diff stats
path: root/rust/hw/char/pl011/src/device_class.rs
blob: b7ab31af02d7bb50ae94be0b153baafc7ccfa375 (plain) (blame)
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
// Copyright 2024, Linaro Limited
// Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
// SPDX-License-Identifier: GPL-2.0-or-later

use core::ptr::NonNull;

use qemu_api::{bindings::*, definitions::ObjectImpl};

use crate::device::PL011State;

#[used]
pub static VMSTATE_PL011: VMStateDescription = VMStateDescription {
    name: PL011State::TYPE_INFO.name,
    unmigratable: true,
    ..unsafe { ::core::mem::MaybeUninit::<VMStateDescription>::zeroed().assume_init() }
};

qemu_api::declare_properties! {
    PL011_PROPERTIES,
    qemu_api::define_property!(
        c"chardev",
        PL011State,
        char_backend,
        unsafe { &qdev_prop_chr },
        CharBackend
    ),
    qemu_api::define_property!(
        c"migrate-clk",
        PL011State,
        migrate_clock,
        unsafe { &qdev_prop_bool },
        bool
    ),
}

qemu_api::device_class_init! {
    pl011_class_init,
    props => PL011_PROPERTIES,
    realize_fn => Some(pl011_realize),
    legacy_reset_fn => Some(pl011_reset),
    vmsd => VMSTATE_PL011,
}

/// # Safety
///
/// We expect the FFI user of this function to pass a valid pointer, that has
/// the same size as [`PL011State`]. We also expect the device is
/// readable/writeable from one thread at any time.
#[no_mangle]
pub unsafe extern "C" fn pl011_realize(dev: *mut DeviceState, _errp: *mut *mut Error) {
    unsafe {
        assert!(!dev.is_null());
        let mut state = NonNull::new_unchecked(dev.cast::<PL011State>());
        state.as_mut().realize();
    }
}

/// # Safety
///
/// We expect the FFI user of this function to pass a valid pointer, that has
/// the same size as [`PL011State`]. We also expect the device is
/// readable/writeable from one thread at any time.
#[no_mangle]
pub unsafe extern "C" fn pl011_reset(dev: *mut DeviceState) {
    unsafe {
        assert!(!dev.is_null());
        let mut state = NonNull::new_unchecked(dev.cast::<PL011State>());
        state.as_mut().reset();
    }
}