xref: /openbmc/qemu/rust/qemu-api/src/device_class.rs (revision ce4a144c)
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 
5*ce4a144cSPaolo Bonzini use std::ffi::CStr;
6*ce4a144cSPaolo Bonzini 
7*ce4a144cSPaolo Bonzini use crate::bindings;
8*ce4a144cSPaolo Bonzini 
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         pub unsafe extern "C" fn $func(
135a5110d2SManos Pitsidianakis             klass: *mut $crate::bindings::ObjectClass,
149f7d4520SPaolo Bonzini             _: *mut ::std::os::raw::c_void,
155a5110d2SManos Pitsidianakis         ) {
165a5110d2SManos Pitsidianakis             let mut dc =
175a5110d2SManos Pitsidianakis                 ::core::ptr::NonNull::new(klass.cast::<$crate::bindings::DeviceClass>()).unwrap();
187b72c7ddSPaolo Bonzini             unsafe {
195a5110d2SManos Pitsidianakis                 dc.as_mut().realize = $realize_fn;
205a5110d2SManos Pitsidianakis                 dc.as_mut().vmsd = &$vmsd;
215a5110d2SManos Pitsidianakis                 $crate::bindings::device_class_set_legacy_reset(dc.as_mut(), $legacy_reset_fn);
22c92c447fSPaolo Bonzini                 $crate::bindings::device_class_set_props(dc.as_mut(), $props.as_ptr());
235a5110d2SManos Pitsidianakis             }
247b72c7ddSPaolo Bonzini         }
255a5110d2SManos Pitsidianakis     };
265a5110d2SManos Pitsidianakis }
275a5110d2SManos Pitsidianakis 
285a5110d2SManos Pitsidianakis #[macro_export]
295a5110d2SManos Pitsidianakis macro_rules! define_property {
30f3518400SJunjie Mao     ($name:expr, $state:ty, $field:ident, $prop:expr, $type:expr, default = $defval:expr$(,)*) => {
315a5110d2SManos Pitsidianakis         $crate::bindings::Property {
3203a573b9SPaolo Bonzini             // use associated function syntax for type checking
339f7d4520SPaolo Bonzini             name: ::std::ffi::CStr::as_ptr($name),
345a5110d2SManos Pitsidianakis             info: $prop,
35f3518400SJunjie Mao             offset: $crate::offset_of!($state, $field) as isize,
365a5110d2SManos Pitsidianakis             set_default: true,
3703a573b9SPaolo Bonzini             defval: $crate::bindings::Property__bindgen_ty_1 { u: $defval as u64 },
386e50bde1SPaolo Bonzini             ..$crate::zeroable::Zeroable::ZERO
395a5110d2SManos Pitsidianakis         }
405a5110d2SManos Pitsidianakis     };
41f3518400SJunjie Mao     ($name:expr, $state:ty, $field:ident, $prop:expr, $type:expr$(,)*) => {
425a5110d2SManos Pitsidianakis         $crate::bindings::Property {
4303a573b9SPaolo Bonzini             // use associated function syntax for type checking
449f7d4520SPaolo Bonzini             name: ::std::ffi::CStr::as_ptr($name),
455a5110d2SManos Pitsidianakis             info: $prop,
46f3518400SJunjie Mao             offset: $crate::offset_of!($state, $field) as isize,
475a5110d2SManos Pitsidianakis             set_default: false,
486e50bde1SPaolo Bonzini             ..$crate::zeroable::Zeroable::ZERO
495a5110d2SManos Pitsidianakis         }
505a5110d2SManos Pitsidianakis     };
515a5110d2SManos Pitsidianakis }
525a5110d2SManos Pitsidianakis 
535a5110d2SManos Pitsidianakis #[macro_export]
545a5110d2SManos Pitsidianakis macro_rules! declare_properties {
555a5110d2SManos Pitsidianakis     ($ident:ident, $($prop:expr),*$(,)*) => {
56c92c447fSPaolo Bonzini         pub static $ident: [$crate::bindings::Property; {
575a5110d2SManos Pitsidianakis             let mut len = 1;
585a5110d2SManos Pitsidianakis             $({
595a5110d2SManos Pitsidianakis                 _ = stringify!($prop);
605a5110d2SManos Pitsidianakis                 len += 1;
615a5110d2SManos Pitsidianakis             })*
625a5110d2SManos Pitsidianakis             len
63c92c447fSPaolo Bonzini         }] = [
645a5110d2SManos Pitsidianakis             $($prop),*,
656e50bde1SPaolo Bonzini             $crate::zeroable::Zeroable::ZERO,
66c92c447fSPaolo Bonzini         ];
675a5110d2SManos Pitsidianakis     };
685a5110d2SManos Pitsidianakis }
69*ce4a144cSPaolo Bonzini 
70*ce4a144cSPaolo Bonzini // workaround until we can use --generate-cstr in bindgen.
71*ce4a144cSPaolo Bonzini pub const TYPE_DEVICE: &CStr =
72*ce4a144cSPaolo Bonzini     unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_DEVICE) };
73*ce4a144cSPaolo Bonzini pub const TYPE_SYS_BUS_DEVICE: &CStr =
74*ce4a144cSPaolo Bonzini     unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_SYS_BUS_DEVICE) };
75