xref: /qemu/rust/qemu-api/src/sysbus.rs (revision 4be0fce498d0a08f18b3a9accdb9ded79484d30a)
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::{bindings, cell::bql_locked, irq::InterruptSource, prelude::*};
10 
11 unsafe impl ObjectType for SysBusDevice {
12     type Class = SysBusDeviceClass;
13     const TYPE_NAME: &'static CStr =
14         unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_SYS_BUS_DEVICE) };
15 }
16 
17 impl SysBusDevice {
18     /// Return `self` cast to a mutable pointer, for use in calls to C code.
19     const fn as_mut_ptr(&self) -> *mut SysBusDevice {
20         addr_of!(*self) as *mut _
21     }
22 
23     /// Expose an interrupt source outside the device as a qdev GPIO output.
24     /// Note that the ordering of calls to `init_irq` is important, since
25     /// whoever creates the sysbus device will refer to the interrupts with
26     /// a number that corresponds to the order of calls to `init_irq`.
27     pub fn init_irq(&self, irq: &InterruptSource) {
28         assert!(bql_locked());
29         unsafe {
30             bindings::sysbus_init_irq(self.as_mut_ptr(), irq.as_ptr());
31         }
32     }
33 }
34