xref: /openbmc/qemu/rust/hw/char/pl011/src/memory_ops.rs (revision 9f7d4520)
137fdb2f5SManos Pitsidianakis // Copyright 2024, Linaro Limited
237fdb2f5SManos Pitsidianakis // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
337fdb2f5SManos Pitsidianakis // SPDX-License-Identifier: GPL-2.0-or-later
437fdb2f5SManos Pitsidianakis 
56e50bde1SPaolo Bonzini use core::ptr::NonNull;
6*9f7d4520SPaolo Bonzini use std::os::raw::{c_uint, c_void};
737fdb2f5SManos Pitsidianakis 
86e50bde1SPaolo Bonzini use qemu_api::{bindings::*, zeroable::Zeroable};
937fdb2f5SManos Pitsidianakis 
1037fdb2f5SManos Pitsidianakis use crate::device::PL011State;
1137fdb2f5SManos Pitsidianakis 
1237fdb2f5SManos Pitsidianakis pub static PL011_OPS: MemoryRegionOps = MemoryRegionOps {
1337fdb2f5SManos Pitsidianakis     read: Some(pl011_read),
1437fdb2f5SManos Pitsidianakis     write: Some(pl011_write),
1537fdb2f5SManos Pitsidianakis     read_with_attrs: None,
1637fdb2f5SManos Pitsidianakis     write_with_attrs: None,
1737fdb2f5SManos Pitsidianakis     endianness: device_endian::DEVICE_NATIVE_ENDIAN,
186e50bde1SPaolo Bonzini     valid: Zeroable::ZERO,
1937fdb2f5SManos Pitsidianakis     impl_: MemoryRegionOps__bindgen_ty_2 {
2037fdb2f5SManos Pitsidianakis         min_access_size: 4,
2137fdb2f5SManos Pitsidianakis         max_access_size: 4,
226e50bde1SPaolo Bonzini         ..Zeroable::ZERO
2337fdb2f5SManos Pitsidianakis     },
2437fdb2f5SManos Pitsidianakis };
2537fdb2f5SManos Pitsidianakis 
pl011_read(opaque: *mut c_void, addr: hwaddr, size: c_uint) -> u6426*9f7d4520SPaolo Bonzini unsafe extern "C" fn pl011_read(opaque: *mut c_void, addr: hwaddr, size: c_uint) -> u64 {
2737fdb2f5SManos Pitsidianakis     assert!(!opaque.is_null());
2837fdb2f5SManos Pitsidianakis     let mut state = unsafe { NonNull::new_unchecked(opaque.cast::<PL011State>()) };
2937fdb2f5SManos Pitsidianakis     let val = unsafe { state.as_mut().read(addr, size) };
3037fdb2f5SManos Pitsidianakis     match val {
3137fdb2f5SManos Pitsidianakis         std::ops::ControlFlow::Break(val) => val,
3237fdb2f5SManos Pitsidianakis         std::ops::ControlFlow::Continue(val) => {
3337fdb2f5SManos Pitsidianakis             // SAFETY: self.char_backend is a valid CharBackend instance after it's been
3437fdb2f5SManos Pitsidianakis             // initialized in realize().
3537fdb2f5SManos Pitsidianakis             let cb_ptr = unsafe { core::ptr::addr_of_mut!(state.as_mut().char_backend) };
3637fdb2f5SManos Pitsidianakis             unsafe { qemu_chr_fe_accept_input(cb_ptr) };
3737fdb2f5SManos Pitsidianakis 
3837fdb2f5SManos Pitsidianakis             val
3937fdb2f5SManos Pitsidianakis         }
4037fdb2f5SManos Pitsidianakis     }
4137fdb2f5SManos Pitsidianakis }
4237fdb2f5SManos Pitsidianakis 
pl011_write(opaque: *mut c_void, addr: hwaddr, data: u64, _size: c_uint)43*9f7d4520SPaolo Bonzini unsafe extern "C" fn pl011_write(opaque: *mut c_void, addr: hwaddr, data: u64, _size: c_uint) {
4437fdb2f5SManos Pitsidianakis     unsafe {
4537fdb2f5SManos Pitsidianakis         assert!(!opaque.is_null());
4637fdb2f5SManos Pitsidianakis         let mut state = NonNull::new_unchecked(opaque.cast::<PL011State>());
4737fdb2f5SManos Pitsidianakis         state.as_mut().write(addr, data)
4837fdb2f5SManos Pitsidianakis     }
4937fdb2f5SManos Pitsidianakis }
50