xref: /openbmc/qemu/rust/qemu-api/src/device_class.rs (revision 7b72c7dd)
15a5110d2SManos Pitsidianakis // Copyright 2024, Linaro Limited
25a5110d2SManos Pitsidianakis // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
35a5110d2SManos Pitsidianakis // SPDX-License-Identifier: GPL-2.0-or-later
45a5110d2SManos Pitsidianakis 
55a5110d2SManos Pitsidianakis use std::sync::OnceLock;
65a5110d2SManos Pitsidianakis 
75a5110d2SManos Pitsidianakis use crate::bindings::Property;
85a5110d2SManos Pitsidianakis 
95a5110d2SManos Pitsidianakis #[macro_export]
105a5110d2SManos Pitsidianakis macro_rules! device_class_init {
115a5110d2SManos Pitsidianakis     ($func:ident, props => $props:ident, realize_fn => $realize_fn:expr, legacy_reset_fn => $legacy_reset_fn:expr, vmsd => $vmsd:ident$(,)*) => {
125a5110d2SManos Pitsidianakis         #[no_mangle]
135a5110d2SManos Pitsidianakis         pub unsafe extern "C" fn $func(
145a5110d2SManos Pitsidianakis             klass: *mut $crate::bindings::ObjectClass,
155a5110d2SManos Pitsidianakis             _: *mut ::core::ffi::c_void,
165a5110d2SManos Pitsidianakis         ) {
175a5110d2SManos Pitsidianakis             let mut dc =
185a5110d2SManos Pitsidianakis                 ::core::ptr::NonNull::new(klass.cast::<$crate::bindings::DeviceClass>()).unwrap();
19*7b72c7ddSPaolo Bonzini             unsafe {
205a5110d2SManos Pitsidianakis                 dc.as_mut().realize = $realize_fn;
215a5110d2SManos Pitsidianakis                 dc.as_mut().vmsd = &$vmsd;
225a5110d2SManos Pitsidianakis                 $crate::bindings::device_class_set_legacy_reset(dc.as_mut(), $legacy_reset_fn);
235a5110d2SManos Pitsidianakis                 $crate::bindings::device_class_set_props(dc.as_mut(), $props.as_mut_ptr());
245a5110d2SManos Pitsidianakis             }
25*7b72c7ddSPaolo Bonzini         }
265a5110d2SManos Pitsidianakis     };
275a5110d2SManos Pitsidianakis }
285a5110d2SManos Pitsidianakis 
295a5110d2SManos Pitsidianakis #[macro_export]
305a5110d2SManos Pitsidianakis macro_rules! define_property {
315a5110d2SManos Pitsidianakis     ($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr, default = $defval:expr$(,)*) => {
325a5110d2SManos Pitsidianakis         $crate::bindings::Property {
335a5110d2SManos Pitsidianakis             name: {
345a5110d2SManos Pitsidianakis                 #[used]
355a5110d2SManos Pitsidianakis                 static _TEMP: &::core::ffi::CStr = $name;
365a5110d2SManos Pitsidianakis                 _TEMP.as_ptr()
375a5110d2SManos Pitsidianakis             },
385a5110d2SManos Pitsidianakis             info: $prop,
395a5110d2SManos Pitsidianakis             offset: ::core::mem::offset_of!($state, $field)
405a5110d2SManos Pitsidianakis                 .try_into()
415a5110d2SManos Pitsidianakis                 .expect("Could not fit offset value to type"),
425a5110d2SManos Pitsidianakis             bitnr: 0,
435a5110d2SManos Pitsidianakis             bitmask: 0,
445a5110d2SManos Pitsidianakis             set_default: true,
455a5110d2SManos Pitsidianakis             defval: $crate::bindings::Property__bindgen_ty_1 { u: $defval.into() },
465a5110d2SManos Pitsidianakis             arrayoffset: 0,
475a5110d2SManos Pitsidianakis             arrayinfo: ::core::ptr::null(),
485a5110d2SManos Pitsidianakis             arrayfieldsize: 0,
495a5110d2SManos Pitsidianakis             link_type: ::core::ptr::null(),
505a5110d2SManos Pitsidianakis         }
515a5110d2SManos Pitsidianakis     };
525a5110d2SManos Pitsidianakis     ($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr$(,)*) => {
535a5110d2SManos Pitsidianakis         $crate::bindings::Property {
545a5110d2SManos Pitsidianakis             name: {
555a5110d2SManos Pitsidianakis                 #[used]
565a5110d2SManos Pitsidianakis                 static _TEMP: &::core::ffi::CStr = $name;
575a5110d2SManos Pitsidianakis                 _TEMP.as_ptr()
585a5110d2SManos Pitsidianakis             },
595a5110d2SManos Pitsidianakis             info: $prop,
605a5110d2SManos Pitsidianakis             offset: ::core::mem::offset_of!($state, $field)
615a5110d2SManos Pitsidianakis                 .try_into()
625a5110d2SManos Pitsidianakis                 .expect("Could not fit offset value to type"),
635a5110d2SManos Pitsidianakis             bitnr: 0,
645a5110d2SManos Pitsidianakis             bitmask: 0,
655a5110d2SManos Pitsidianakis             set_default: false,
665a5110d2SManos Pitsidianakis             defval: $crate::bindings::Property__bindgen_ty_1 { i: 0 },
675a5110d2SManos Pitsidianakis             arrayoffset: 0,
685a5110d2SManos Pitsidianakis             arrayinfo: ::core::ptr::null(),
695a5110d2SManos Pitsidianakis             arrayfieldsize: 0,
705a5110d2SManos Pitsidianakis             link_type: ::core::ptr::null(),
715a5110d2SManos Pitsidianakis         }
725a5110d2SManos Pitsidianakis     };
735a5110d2SManos Pitsidianakis }
745a5110d2SManos Pitsidianakis 
755a5110d2SManos Pitsidianakis #[repr(C)]
765a5110d2SManos Pitsidianakis pub struct Properties<const N: usize>(pub OnceLock<[Property; N]>, pub fn() -> [Property; N]);
775a5110d2SManos Pitsidianakis 
785a5110d2SManos Pitsidianakis impl<const N: usize> Properties<N> {
795a5110d2SManos Pitsidianakis     pub fn as_mut_ptr(&mut self) -> *mut Property {
805a5110d2SManos Pitsidianakis         _ = self.0.get_or_init(self.1);
815a5110d2SManos Pitsidianakis         self.0.get_mut().unwrap().as_mut_ptr()
825a5110d2SManos Pitsidianakis     }
835a5110d2SManos Pitsidianakis }
845a5110d2SManos Pitsidianakis 
855a5110d2SManos Pitsidianakis #[macro_export]
865a5110d2SManos Pitsidianakis macro_rules! declare_properties {
875a5110d2SManos Pitsidianakis     ($ident:ident, $($prop:expr),*$(,)*) => {
885a5110d2SManos Pitsidianakis 
895a5110d2SManos Pitsidianakis         const fn _calc_prop_len() -> usize {
905a5110d2SManos Pitsidianakis             let mut len = 1;
915a5110d2SManos Pitsidianakis             $({
925a5110d2SManos Pitsidianakis                 _ = stringify!($prop);
935a5110d2SManos Pitsidianakis                 len += 1;
945a5110d2SManos Pitsidianakis             })*
955a5110d2SManos Pitsidianakis             len
965a5110d2SManos Pitsidianakis         }
975a5110d2SManos Pitsidianakis         const PROP_LEN: usize = _calc_prop_len();
985a5110d2SManos Pitsidianakis 
995a5110d2SManos Pitsidianakis         fn _make_properties() -> [$crate::bindings::Property; PROP_LEN] {
1005a5110d2SManos Pitsidianakis             [
1015a5110d2SManos Pitsidianakis                 $($prop),*,
1025a5110d2SManos Pitsidianakis                     unsafe { ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init() },
1035a5110d2SManos Pitsidianakis             ]
1045a5110d2SManos Pitsidianakis         }
1055a5110d2SManos Pitsidianakis 
1065a5110d2SManos Pitsidianakis         #[no_mangle]
1075a5110d2SManos Pitsidianakis         pub static mut $ident: $crate::device_class::Properties<PROP_LEN> = $crate::device_class::Properties(::std::sync::OnceLock::new(), _make_properties);
1085a5110d2SManos Pitsidianakis     };
1095a5110d2SManos Pitsidianakis }
1105a5110d2SManos Pitsidianakis 
1115a5110d2SManos Pitsidianakis #[macro_export]
1125a5110d2SManos Pitsidianakis macro_rules! vm_state_description {
1135a5110d2SManos Pitsidianakis     ($(#[$outer:meta])*
1145a5110d2SManos Pitsidianakis      $name:ident,
1155a5110d2SManos Pitsidianakis      $(name: $vname:expr,)*
1165a5110d2SManos Pitsidianakis      $(unmigratable: $um_val:expr,)*
1175a5110d2SManos Pitsidianakis     ) => {
1185a5110d2SManos Pitsidianakis         #[used]
1195a5110d2SManos Pitsidianakis         $(#[$outer])*
1205a5110d2SManos Pitsidianakis         pub static $name: $crate::bindings::VMStateDescription = $crate::bindings::VMStateDescription {
1215a5110d2SManos Pitsidianakis             $(name: {
1225a5110d2SManos Pitsidianakis                 #[used]
1235a5110d2SManos Pitsidianakis                 static VMSTATE_NAME: &::core::ffi::CStr = $vname;
1245a5110d2SManos Pitsidianakis                 $vname.as_ptr()
1255a5110d2SManos Pitsidianakis             },)*
1265a5110d2SManos Pitsidianakis             unmigratable: true,
1275a5110d2SManos Pitsidianakis             ..unsafe { ::core::mem::MaybeUninit::<$crate::bindings::VMStateDescription>::zeroed().assume_init() }
1285a5110d2SManos Pitsidianakis         };
1295a5110d2SManos Pitsidianakis     }
1305a5110d2SManos Pitsidianakis }
131