xref: /qemu/rust/qemu-api/src/sysbus.rs (revision 3212da0033530ae896d31d90d5e81a772fc37088)
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 //! Bindings to access `sysbus` functionality from Rust.
6 
7 use std::{ffi::CStr, ptr::addr_of_mut};
8 
9 pub use bindings::{SysBusDevice, SysBusDeviceClass};
10 
11 use crate::{
12     bindings,
13     cell::bql_locked,
14     irq::{IRQState, InterruptSource},
15     memory::MemoryRegion,
16     prelude::*,
17     qdev::{DeviceClass, DeviceImpl, DeviceState},
18     qom::{ClassInitImpl, Owned},
19 };
20 
21 unsafe impl ObjectType for SysBusDevice {
22     type Class = SysBusDeviceClass;
23     const TYPE_NAME: &'static CStr =
24         unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_SYS_BUS_DEVICE) };
25 }
26 qom_isa!(SysBusDevice: DeviceState, Object);
27 
28 // TODO: add virtual methods
29 pub trait SysBusDeviceImpl: DeviceImpl + IsA<SysBusDevice> {}
30 
31 impl<T> ClassInitImpl<SysBusDeviceClass> for T
32 where
33     T: SysBusDeviceImpl + ClassInitImpl<DeviceClass>,
34 {
35     fn class_init(sdc: &mut SysBusDeviceClass) {
36         <T as ClassInitImpl<DeviceClass>>::class_init(&mut sdc.parent_class);
37     }
38 }
39 
40 /// Trait for methods of [`SysBusDevice`] and its subclasses.
41 pub trait SysBusDeviceMethods: ObjectDeref
42 where
43     Self::Target: IsA<SysBusDevice>,
44 {
45     /// Expose a memory region to the board so that it can give it an address
46     /// in guest memory.  Note that the ordering of calls to `init_mmio` is
47     /// important, since whoever creates the sysbus device will refer to the
48     /// region with a number that corresponds to the order of calls to
49     /// `init_mmio`.
50     fn init_mmio(&self, iomem: &MemoryRegion) {
51         assert!(bql_locked());
52         unsafe {
53             bindings::sysbus_init_mmio(self.as_mut_ptr(), iomem.as_mut_ptr());
54         }
55     }
56 
57     /// Expose an interrupt source outside the device as a qdev GPIO output.
58     /// Note that the ordering of calls to `init_irq` is important, since
59     /// whoever creates the sysbus device will refer to the interrupts with
60     /// a number that corresponds to the order of calls to `init_irq`.
61     fn init_irq(&self, irq: &InterruptSource) {
62         assert!(bql_locked());
63         unsafe {
64             bindings::sysbus_init_irq(self.as_mut_ptr(), irq.as_ptr());
65         }
66     }
67 
68     // TODO: do we want a type like GuestAddress here?
69     fn mmio_map(&self, id: u32, addr: u64) {
70         assert!(bql_locked());
71         let id: i32 = id.try_into().unwrap();
72         unsafe {
73             bindings::sysbus_mmio_map(self.as_mut_ptr(), id, addr);
74         }
75     }
76 
77     // Owned<> is used here because sysbus_connect_irq (via
78     // object_property_set_link) adds a reference to the IRQState,
79     // which can prolong its life
80     fn connect_irq(&self, id: u32, irq: &Owned<IRQState>) {
81         assert!(bql_locked());
82         let id: i32 = id.try_into().unwrap();
83         unsafe {
84             bindings::sysbus_connect_irq(self.as_mut_ptr(), id, irq.as_mut_ptr());
85         }
86     }
87 
88     fn sysbus_realize(&self) {
89         // TODO: return an Error
90         assert!(bql_locked());
91         unsafe {
92             bindings::sysbus_realize(self.as_mut_ptr(), addr_of_mut!(bindings::error_fatal));
93         }
94     }
95 }
96 
97 impl<R: ObjectDeref> SysBusDeviceMethods for R where R::Target: IsA<SysBusDevice> {}
98