2024-10-18 16:30:56 +02:00
|
|
|
// Copyright 2024, Linaro Limited
|
|
|
|
|
// Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
|
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
2024-12-13 17:54:33 +01:00
|
|
|
use std::{ffi::CStr, ptr::addr_of};
|
2024-10-18 16:30:56 +02:00
|
|
|
|
2025-09-08 12:49:52 +02:00
|
|
|
use bql::BqlCell;
|
2025-09-08 12:49:56 +02:00
|
|
|
use hwcore::{DeviceImpl, DeviceState, ResettablePhasesImpl, SysBusDevice};
|
2025-09-08 12:49:51 +02:00
|
|
|
use migration::{VMStateDescription, VMStateDescriptionBuilder};
|
2025-09-08 12:49:53 +02:00
|
|
|
use qom::{prelude::*, ObjectImpl, ParentField};
|
2025-09-08 12:49:50 +02:00
|
|
|
use util::bindings::{module_call_init, module_init_type};
|
2024-10-18 16:30:56 +02:00
|
|
|
|
2024-11-05 22:34:13 +01:00
|
|
|
// Test that macros can compile.
|
2025-09-08 12:49:39 +02:00
|
|
|
pub const VMSTATE: VMStateDescription<DummyState> = VMStateDescriptionBuilder::<DummyState>::new()
|
|
|
|
|
.name(c"name")
|
|
|
|
|
.unmigratable()
|
|
|
|
|
.build();
|
2024-11-05 22:34:13 +01:00
|
|
|
|
|
|
|
|
#[repr(C)]
|
2025-09-08 12:50:02 +02:00
|
|
|
#[derive(qom::Object, hwcore::Device)]
|
2024-11-05 22:34:13 +01:00
|
|
|
pub struct DummyState {
|
2024-12-11 11:38:20 +01:00
|
|
|
parent: ParentField<DeviceState>,
|
2025-09-08 12:49:38 +02:00
|
|
|
#[property(rename = "migrate-clk", default = true)]
|
2024-11-05 22:34:13 +01:00
|
|
|
migrate_clock: bool,
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 14:32:16 +01:00
|
|
|
qom_isa!(DummyState: Object, DeviceState);
|
|
|
|
|
|
2025-01-17 11:59:55 +01:00
|
|
|
pub struct DummyClass {
|
|
|
|
|
parent_class: <DeviceState as ObjectType>::Class,
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 12:36:42 +01:00
|
|
|
impl DummyClass {
|
|
|
|
|
pub fn class_init<T: DeviceImpl>(self: &mut DummyClass) {
|
2025-02-13 12:37:43 +01:00
|
|
|
self.parent_class.class_init::<T>();
|
2025-02-13 12:36:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-05 22:34:13 +01:00
|
|
|
unsafe impl ObjectType for DummyState {
|
2025-01-17 11:59:55 +01:00
|
|
|
type Class = DummyClass;
|
2025-05-02 10:39:08 +02:00
|
|
|
const TYPE_NAME: &'static CStr = c"dummy";
|
2024-11-05 22:34:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ObjectImpl for DummyState {
|
|
|
|
|
type ParentType = DeviceState;
|
|
|
|
|
const ABSTRACT: bool = false;
|
2025-02-13 12:36:42 +01:00
|
|
|
const CLASS_INIT: fn(&mut DummyClass) = DummyClass::class_init::<Self>;
|
2024-11-05 22:34:13 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-17 11:26:48 +01:00
|
|
|
impl ResettablePhasesImpl for DummyState {}
|
|
|
|
|
|
2024-11-05 22:34:13 +01:00
|
|
|
impl DeviceImpl for DummyState {
|
2025-09-08 12:49:41 +02:00
|
|
|
const VMSTATE: Option<VMStateDescription<Self>> = Some(VMSTATE);
|
2024-11-05 22:34:13 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-17 11:59:55 +01:00
|
|
|
#[repr(C)]
|
2025-09-08 12:50:02 +02:00
|
|
|
#[derive(qom::Object, hwcore::Device)]
|
2025-01-17 11:59:55 +01:00
|
|
|
pub struct DummyChildState {
|
|
|
|
|
parent: ParentField<DummyState>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qom_isa!(DummyChildState: Object, DeviceState, DummyState);
|
|
|
|
|
|
|
|
|
|
pub struct DummyChildClass {
|
|
|
|
|
parent_class: <DummyState as ObjectType>::Class,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe impl ObjectType for DummyChildState {
|
|
|
|
|
type Class = DummyChildClass;
|
2025-05-02 10:39:08 +02:00
|
|
|
const TYPE_NAME: &'static CStr = c"dummy_child";
|
2025-01-17 11:59:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ObjectImpl for DummyChildState {
|
|
|
|
|
type ParentType = DummyState;
|
|
|
|
|
const ABSTRACT: bool = false;
|
2025-02-13 12:36:42 +01:00
|
|
|
const CLASS_INIT: fn(&mut DummyChildClass) = DummyChildClass::class_init::<Self>;
|
2025-01-17 11:59:55 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-17 11:26:48 +01:00
|
|
|
impl ResettablePhasesImpl for DummyChildState {}
|
2025-01-17 11:59:55 +01:00
|
|
|
impl DeviceImpl for DummyChildState {}
|
|
|
|
|
|
2025-02-13 12:36:42 +01:00
|
|
|
impl DummyChildClass {
|
|
|
|
|
pub fn class_init<T: DeviceImpl>(self: &mut DummyChildClass) {
|
|
|
|
|
self.parent_class.class_init::<T>();
|
2025-01-17 11:59:55 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-05 22:34:13 +01:00
|
|
|
fn init_qom() {
|
|
|
|
|
static ONCE: BqlCell<bool> = BqlCell::new(false);
|
|
|
|
|
|
2025-09-08 12:49:52 +02:00
|
|
|
bql::start_test();
|
2024-11-05 22:34:13 +01:00
|
|
|
if !ONCE.get() {
|
|
|
|
|
unsafe {
|
|
|
|
|
module_call_init(module_init_type::MODULE_INIT_QOM);
|
|
|
|
|
}
|
|
|
|
|
ONCE.set(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-18 16:30:56 +02:00
|
|
|
#[test]
|
2024-11-05 22:34:13 +01:00
|
|
|
/// Create and immediately drop an instance.
|
|
|
|
|
fn test_object_new() {
|
|
|
|
|
init_qom();
|
2024-10-31 14:27:36 +01:00
|
|
|
drop(DummyState::new());
|
|
|
|
|
drop(DummyChildState::new());
|
2024-11-06 00:01:57 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-17 12:00:01 +01:00
|
|
|
#[test]
|
|
|
|
|
#[allow(clippy::redundant_clone)]
|
|
|
|
|
/// Create, clone and then drop an instance.
|
|
|
|
|
fn test_clone() {
|
|
|
|
|
init_qom();
|
2024-10-31 14:27:36 +01:00
|
|
|
let p = DummyState::new();
|
2025-01-17 12:00:01 +01:00
|
|
|
assert_eq!(p.clone().typename(), "dummy");
|
|
|
|
|
drop(p);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 00:01:57 +01:00
|
|
|
#[test]
|
|
|
|
|
/// Try invoking a method on an object.
|
|
|
|
|
fn test_typename() {
|
|
|
|
|
init_qom();
|
2024-10-31 14:27:36 +01:00
|
|
|
let p = DummyState::new();
|
|
|
|
|
assert_eq!(p.typename(), "dummy");
|
2024-10-18 16:30:56 +02:00
|
|
|
}
|
2024-12-19 14:32:16 +01:00
|
|
|
|
|
|
|
|
// a note on all "cast" tests: usually, especially for downcasts the desired
|
|
|
|
|
// class would be placed on the right, for example:
|
|
|
|
|
//
|
|
|
|
|
// let sbd_ref = p.dynamic_cast::<SysBusDevice>();
|
|
|
|
|
//
|
|
|
|
|
// Here I am doing the opposite to check that the resulting type is correct.
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[allow(clippy::shadow_unrelated)]
|
|
|
|
|
/// Test casts on shared references.
|
|
|
|
|
fn test_cast() {
|
|
|
|
|
init_qom();
|
2024-10-31 14:27:36 +01:00
|
|
|
let p = DummyState::new();
|
|
|
|
|
let p_ptr: *mut DummyState = p.as_mut_ptr();
|
|
|
|
|
let p_ref: &mut DummyState = unsafe { &mut *p_ptr };
|
2024-12-19 14:32:16 +01:00
|
|
|
|
|
|
|
|
let obj_ref: &Object = p_ref.upcast();
|
2024-10-31 14:27:36 +01:00
|
|
|
assert_eq!(addr_of!(*obj_ref), p_ptr.cast());
|
2024-12-19 14:32:16 +01:00
|
|
|
|
|
|
|
|
let sbd_ref: Option<&SysBusDevice> = obj_ref.dynamic_cast();
|
|
|
|
|
assert!(sbd_ref.is_none());
|
|
|
|
|
|
|
|
|
|
let dev_ref: Option<&DeviceState> = obj_ref.downcast();
|
2024-10-31 14:27:36 +01:00
|
|
|
assert_eq!(addr_of!(*dev_ref.unwrap()), p_ptr.cast());
|
2024-12-19 14:32:16 +01:00
|
|
|
|
|
|
|
|
// SAFETY: the cast is wrong, but the value is only used for comparison
|
|
|
|
|
unsafe {
|
|
|
|
|
let sbd_ref: &SysBusDevice = obj_ref.unsafe_cast();
|
2024-10-31 14:27:36 +01:00
|
|
|
assert_eq!(addr_of!(*sbd_ref), p_ptr.cast());
|
2024-12-19 14:32:16 +01:00
|
|
|
}
|
|
|
|
|
}
|