xref: /openbmc/qemu/rust/qemu-api/src/device_class.rs (revision 03a573b9)
1 // Copyright 2024, Linaro Limited
2 // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
3 // SPDX-License-Identifier: GPL-2.0-or-later
4 
5 use std::sync::OnceLock;
6 
7 use crate::bindings::Property;
8 
9 #[macro_export]
10 macro_rules! device_class_init {
11     ($func:ident, props => $props:ident, realize_fn => $realize_fn:expr, legacy_reset_fn => $legacy_reset_fn:expr, vmsd => $vmsd:ident$(,)*) => {
12         pub unsafe extern "C" fn $func(
13             klass: *mut $crate::bindings::ObjectClass,
14             _: *mut ::core::ffi::c_void,
15         ) {
16             let mut dc =
17                 ::core::ptr::NonNull::new(klass.cast::<$crate::bindings::DeviceClass>()).unwrap();
18             unsafe {
19                 dc.as_mut().realize = $realize_fn;
20                 dc.as_mut().vmsd = &$vmsd;
21                 $crate::bindings::device_class_set_legacy_reset(dc.as_mut(), $legacy_reset_fn);
22                 $crate::bindings::device_class_set_props(dc.as_mut(), $props.as_mut_ptr());
23             }
24         }
25     };
26 }
27 
28 #[macro_export]
29 macro_rules! define_property {
30     ($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr, default = $defval:expr$(,)*) => {
31         $crate::bindings::Property {
32             // use associated function syntax for type checking
33             name: ::core::ffi::CStr::as_ptr($name),
34             info: $prop,
35             offset: ::core::mem::offset_of!($state, $field)
36                 .try_into()
37                 .expect("Could not fit offset value to type"),
38             set_default: true,
39             defval: $crate::bindings::Property__bindgen_ty_1 { u: $defval as u64 },
40             ..unsafe { ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init() }
41         }
42     };
43     ($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr$(,)*) => {
44         $crate::bindings::Property {
45             // use associated function syntax for type checking
46             name: ::core::ffi::CStr::as_ptr($name),
47             info: $prop,
48             offset: ::core::mem::offset_of!($state, $field)
49                 .try_into()
50                 .expect("Could not fit offset value to type"),
51             set_default: false,
52             ..unsafe { ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init() }
53         }
54     };
55 }
56 
57 #[repr(C)]
58 pub struct Properties<const N: usize>(pub OnceLock<[Property; N]>, pub fn() -> [Property; N]);
59 
60 impl<const N: usize> Properties<N> {
61     pub fn as_mut_ptr(&mut self) -> *mut Property {
62         _ = self.0.get_or_init(self.1);
63         self.0.get_mut().unwrap().as_mut_ptr()
64     }
65 }
66 
67 #[macro_export]
68 macro_rules! declare_properties {
69     ($ident:ident, $($prop:expr),*$(,)*) => {
70 
71         const fn _calc_prop_len() -> usize {
72             let mut len = 1;
73             $({
74                 _ = stringify!($prop);
75                 len += 1;
76             })*
77             len
78         }
79         const PROP_LEN: usize = _calc_prop_len();
80 
81         fn _make_properties() -> [$crate::bindings::Property; PROP_LEN] {
82             [
83                 $($prop),*,
84                     unsafe { ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init() },
85             ]
86         }
87 
88         pub static mut $ident: $crate::device_class::Properties<PROP_LEN> = $crate::device_class::Properties(::std::sync::OnceLock::new(), _make_properties);
89     };
90 }
91 
92 #[macro_export]
93 macro_rules! vm_state_description {
94     ($(#[$outer:meta])*
95      $name:ident,
96      $(name: $vname:expr,)*
97      $(unmigratable: $um_val:expr,)*
98     ) => {
99         #[used]
100         $(#[$outer])*
101         pub static $name: $crate::bindings::VMStateDescription = $crate::bindings::VMStateDescription {
102             $(name: {
103                 #[used]
104                 static VMSTATE_NAME: &::core::ffi::CStr = $vname;
105                 $vname.as_ptr()
106             },)*
107             unmigratable: true,
108             ..unsafe { ::core::mem::MaybeUninit::<$crate::bindings::VMStateDescription>::zeroed().assume_init() }
109         };
110     }
111 }
112