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