xref: /openbmc/qemu/rust/qemu-api/src/device_class.rs (revision 7b72c7dd)
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         #[no_mangle]
13         pub unsafe extern "C" fn $func(
14             klass: *mut $crate::bindings::ObjectClass,
15             _: *mut ::core::ffi::c_void,
16         ) {
17             let mut dc =
18                 ::core::ptr::NonNull::new(klass.cast::<$crate::bindings::DeviceClass>()).unwrap();
19             unsafe {
20                 dc.as_mut().realize = $realize_fn;
21                 dc.as_mut().vmsd = &$vmsd;
22                 $crate::bindings::device_class_set_legacy_reset(dc.as_mut(), $legacy_reset_fn);
23                 $crate::bindings::device_class_set_props(dc.as_mut(), $props.as_mut_ptr());
24             }
25         }
26     };
27 }
28 
29 #[macro_export]
30 macro_rules! define_property {
31     ($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr, default = $defval:expr$(,)*) => {
32         $crate::bindings::Property {
33             name: {
34                 #[used]
35                 static _TEMP: &::core::ffi::CStr = $name;
36                 _TEMP.as_ptr()
37             },
38             info: $prop,
39             offset: ::core::mem::offset_of!($state, $field)
40                 .try_into()
41                 .expect("Could not fit offset value to type"),
42             bitnr: 0,
43             bitmask: 0,
44             set_default: true,
45             defval: $crate::bindings::Property__bindgen_ty_1 { u: $defval.into() },
46             arrayoffset: 0,
47             arrayinfo: ::core::ptr::null(),
48             arrayfieldsize: 0,
49             link_type: ::core::ptr::null(),
50         }
51     };
52     ($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr$(,)*) => {
53         $crate::bindings::Property {
54             name: {
55                 #[used]
56                 static _TEMP: &::core::ffi::CStr = $name;
57                 _TEMP.as_ptr()
58             },
59             info: $prop,
60             offset: ::core::mem::offset_of!($state, $field)
61                 .try_into()
62                 .expect("Could not fit offset value to type"),
63             bitnr: 0,
64             bitmask: 0,
65             set_default: false,
66             defval: $crate::bindings::Property__bindgen_ty_1 { i: 0 },
67             arrayoffset: 0,
68             arrayinfo: ::core::ptr::null(),
69             arrayfieldsize: 0,
70             link_type: ::core::ptr::null(),
71         }
72     };
73 }
74 
75 #[repr(C)]
76 pub struct Properties<const N: usize>(pub OnceLock<[Property; N]>, pub fn() -> [Property; N]);
77 
78 impl<const N: usize> Properties<N> {
79     pub fn as_mut_ptr(&mut self) -> *mut Property {
80         _ = self.0.get_or_init(self.1);
81         self.0.get_mut().unwrap().as_mut_ptr()
82     }
83 }
84 
85 #[macro_export]
86 macro_rules! declare_properties {
87     ($ident:ident, $($prop:expr),*$(,)*) => {
88 
89         const fn _calc_prop_len() -> usize {
90             let mut len = 1;
91             $({
92                 _ = stringify!($prop);
93                 len += 1;
94             })*
95             len
96         }
97         const PROP_LEN: usize = _calc_prop_len();
98 
99         fn _make_properties() -> [$crate::bindings::Property; PROP_LEN] {
100             [
101                 $($prop),*,
102                     unsafe { ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init() },
103             ]
104         }
105 
106         #[no_mangle]
107         pub static mut $ident: $crate::device_class::Properties<PROP_LEN> = $crate::device_class::Properties(::std::sync::OnceLock::new(), _make_properties);
108     };
109 }
110 
111 #[macro_export]
112 macro_rules! vm_state_description {
113     ($(#[$outer:meta])*
114      $name:ident,
115      $(name: $vname:expr,)*
116      $(unmigratable: $um_val:expr,)*
117     ) => {
118         #[used]
119         $(#[$outer])*
120         pub static $name: $crate::bindings::VMStateDescription = $crate::bindings::VMStateDescription {
121             $(name: {
122                 #[used]
123                 static VMSTATE_NAME: &::core::ffi::CStr = $vname;
124                 $vname.as_ptr()
125             },)*
126             unmigratable: true,
127             ..unsafe { ::core::mem::MaybeUninit::<$crate::bindings::VMStateDescription>::zeroed().assume_init() }
128         };
129     }
130 }
131