xref: /qemu/rust/qemu-api/src/sysbus.rs (revision 716d89f9cc14faf784d83c945c40b7e8256ae525)
1 // Copyright 2024 Red Hat, Inc.
2 // Author(s): Paolo Bonzini <pbonzini@redhat.com>
3 // SPDX-License-Identifier: GPL-2.0-or-later
4 
5 use std::{ffi::CStr, ptr::addr_of};
6 
7 pub use bindings::{SysBusDevice, SysBusDeviceClass};
8 
9 use crate::{
10     bindings, cell::bql_locked, irq::InterruptSource, prelude::*, qdev::DeviceClass,
11     qom::ClassInitImpl,
12 };
13 
14 unsafe impl ObjectType for SysBusDevice {
15     type Class = SysBusDeviceClass;
16     const TYPE_NAME: &'static CStr =
17         unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_SYS_BUS_DEVICE) };
18 }
19 
20 // TODO: add SysBusDeviceImpl
21 impl<T> ClassInitImpl<SysBusDeviceClass> for T
22 where
23     T: ClassInitImpl<DeviceClass>,
24 {
25     fn class_init(sdc: &mut SysBusDeviceClass) {
26         <T as ClassInitImpl<DeviceClass>>::class_init(&mut sdc.parent_class);
27     }
28 }
29 
30 impl SysBusDevice {
31     /// Return `self` cast to a mutable pointer, for use in calls to C code.
32     const fn as_mut_ptr(&self) -> *mut SysBusDevice {
33         addr_of!(*self) as *mut _
34     }
35 
36     /// Expose an interrupt source outside the device as a qdev GPIO output.
37     /// Note that the ordering of calls to `init_irq` is important, since
38     /// whoever creates the sysbus device will refer to the interrupts with
39     /// a number that corresponds to the order of calls to `init_irq`.
40     pub fn init_irq(&self, irq: &InterruptSource) {
41         assert!(bql_locked());
42         unsafe {
43             bindings::sysbus_init_irq(self.as_mut_ptr(), irq.as_ptr());
44         }
45     }
46 }
47