xref: /qemu/rust/qemu-api/src/sysbus.rs (revision a630055df39e1960275d0e273af036f794b15662)
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, 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 SysBusDeviceImpl
29 impl<T> ClassInitImpl<SysBusDeviceClass> for T
30 where
31     T: ClassInitImpl<DeviceClass>,
32 {
33     fn class_init(sdc: &mut SysBusDeviceClass) {
34         <T as ClassInitImpl<DeviceClass>>::class_init(&mut sdc.parent_class);
35     }
36 }
37 
38 /// Trait for methods of [`SysBusDevice`] and its subclasses.
39 pub trait SysBusDeviceMethods: ObjectDeref
40 where
41     Self::Target: IsA<SysBusDevice>,
42 {
43     /// Expose a memory region to the board so that it can give it an address
44     /// in guest memory.  Note that the ordering of calls to `init_mmio` is
45     /// important, since whoever creates the sysbus device will refer to the
46     /// region with a number that corresponds to the order of calls to
47     /// `init_mmio`.
48     fn init_mmio(&self, iomem: &MemoryRegion) {
49         assert!(bql_locked());
50         unsafe {
51             bindings::sysbus_init_mmio(self.as_mut_ptr(), iomem.as_mut_ptr());
52         }
53     }
54 
55     /// Expose an interrupt source outside the device as a qdev GPIO output.
56     /// Note that the ordering of calls to `init_irq` is important, since
57     /// whoever creates the sysbus device will refer to the interrupts with
58     /// a number that corresponds to the order of calls to `init_irq`.
59     fn init_irq(&self, irq: &InterruptSource) {
60         assert!(bql_locked());
61         unsafe {
62             bindings::sysbus_init_irq(self.as_mut_ptr(), irq.as_ptr());
63         }
64     }
65 
66     // TODO: do we want a type like GuestAddress here?
67     fn mmio_map(&self, id: u32, addr: u64) {
68         assert!(bql_locked());
69         let id: i32 = id.try_into().unwrap();
70         unsafe {
71             bindings::sysbus_mmio_map(self.as_mut_ptr(), id, addr);
72         }
73     }
74 
75     // Owned<> is used here because sysbus_connect_irq (via
76     // object_property_set_link) adds a reference to the IRQState,
77     // which can prolong its life
78     fn connect_irq(&self, id: u32, irq: &Owned<IRQState>) {
79         assert!(bql_locked());
80         let id: i32 = id.try_into().unwrap();
81         unsafe {
82             bindings::sysbus_connect_irq(self.as_mut_ptr(), id, irq.as_mut_ptr());
83         }
84     }
85 
86     fn sysbus_realize(&self) {
87         // TODO: return an Error
88         assert!(bql_locked());
89         unsafe {
90             bindings::sysbus_realize(self.as_mut_ptr(), addr_of_mut!(bindings::error_fatal));
91         }
92     }
93 }
94 
95 impl<R: ObjectDeref> SysBusDeviceMethods for R where R::Target: IsA<SysBusDevice> {}
96