2019-03-07 13:56:43 +00:00
|
|
|
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
2019-05-08 10:22:53 +00:00
|
|
|
// found in the LICENSE-BSD-3-Clause file.
|
2019-03-07 13:56:43 +00:00
|
|
|
|
|
|
|
//! Handles routing to devices in an address space.
|
|
|
|
|
|
|
|
use std::cmp::{Ord, Ordering, PartialEq, PartialOrd};
|
|
|
|
use std::collections::btree_map::BTreeMap;
|
2020-03-04 15:04:32 +00:00
|
|
|
use std::sync::{Arc, Mutex, RwLock, Weak};
|
2019-10-29 01:15:08 +00:00
|
|
|
use std::{convert, error, fmt, io, result};
|
2019-03-07 13:56:43 +00:00
|
|
|
|
|
|
|
/// Trait for devices that respond to reads or writes in an arbitrary address space.
|
|
|
|
///
|
|
|
|
/// The device does not care where it exists in address space as each method is only given an offset
|
|
|
|
/// into its allocated portion of address space.
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
pub trait BusDevice: Send {
|
|
|
|
/// Reads at `offset` from this device
|
2019-07-02 23:22:43 +00:00
|
|
|
fn read(&mut self, base: u64, offset: u64, data: &mut [u8]) {}
|
2019-03-07 13:56:43 +00:00
|
|
|
/// Writes at `offset` into this device
|
2019-07-02 23:22:43 +00:00
|
|
|
fn write(&mut self, base: u64, offset: u64, data: &[u8]) {}
|
2019-03-07 13:56:43 +00:00
|
|
|
/// Triggers the `irq_mask` interrupt on this device
|
|
|
|
fn interrupt(&self, irq_mask: u32) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
/// The insertion failed because the new device overlapped with an old device.
|
|
|
|
Overlap,
|
2019-10-21 17:32:51 +00:00
|
|
|
/// Failed to operate on zero sized range.
|
|
|
|
ZeroSizedRange,
|
|
|
|
/// Failed to find address range.
|
|
|
|
MissingAddressRange,
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T> = result::Result<T, Error>;
|
|
|
|
|
2019-10-29 01:15:08 +00:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "bus_error: {:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for Error {}
|
|
|
|
|
|
|
|
impl convert::From<Error> for io::Error {
|
|
|
|
fn from(e: Error) -> Self {
|
|
|
|
io::Error::new(io::ErrorKind::Other, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 23:10:41 +00:00
|
|
|
/// Holds a base and length representing the address space occupied by a `BusDevice`.
|
|
|
|
///
|
|
|
|
/// * base - The address at which the range start.
|
|
|
|
/// * len - The length of the range in bytes.
|
2019-03-07 13:56:43 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2019-07-02 23:10:41 +00:00
|
|
|
pub struct BusRange {
|
|
|
|
pub base: u64,
|
|
|
|
pub len: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BusRange {
|
|
|
|
/// Returns true if there is overlap with the given range.
|
|
|
|
pub fn overlaps(&self, base: u64, len: u64) -> bool {
|
|
|
|
self.base < (base + len) && base < self.base + self.len
|
|
|
|
}
|
|
|
|
}
|
2019-03-07 13:56:43 +00:00
|
|
|
|
|
|
|
impl Eq for BusRange {}
|
|
|
|
|
|
|
|
impl PartialEq for BusRange {
|
|
|
|
fn eq(&self, other: &BusRange) -> bool {
|
2019-07-02 23:10:41 +00:00
|
|
|
self.base == other.base
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for BusRange {
|
|
|
|
fn cmp(&self, other: &BusRange) -> Ordering {
|
2019-07-02 23:10:41 +00:00
|
|
|
self.base.cmp(&other.base)
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for BusRange {
|
|
|
|
fn partial_cmp(&self, other: &BusRange) -> Option<Ordering> {
|
2019-07-02 23:10:41 +00:00
|
|
|
self.base.partial_cmp(&other.base)
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A device container for routing reads and writes over some address space.
|
|
|
|
///
|
|
|
|
/// This doesn't have any restrictions on what kind of device or address space this applies to. The
|
|
|
|
/// only restriction is that no two devices can overlap in this address space.
|
2019-10-23 21:06:13 +00:00
|
|
|
#[derive(Default)]
|
2019-03-07 13:56:43 +00:00
|
|
|
pub struct Bus {
|
2020-03-04 15:04:32 +00:00
|
|
|
devices: RwLock<BTreeMap<BusRange, Weak<Mutex<dyn BusDevice>>>>,
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Bus {
|
|
|
|
/// Constructs an a bus with an empty address space.
|
|
|
|
pub fn new() -> Bus {
|
|
|
|
Bus {
|
2019-10-23 21:06:13 +00:00
|
|
|
devices: RwLock::new(BTreeMap::new()),
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 21:06:13 +00:00
|
|
|
fn first_before(&self, addr: u64) -> Option<(BusRange, Arc<Mutex<dyn BusDevice>>)> {
|
|
|
|
let devices = self.devices.read().unwrap();
|
|
|
|
let (range, dev) = devices
|
2019-07-02 23:10:41 +00:00
|
|
|
.range(..=BusRange { base: addr, len: 1 })
|
|
|
|
.rev()
|
|
|
|
.next()?;
|
2020-11-24 17:25:02 +00:00
|
|
|
dev.upgrade().map(|d| (*range, d.clone()))
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 17:32:51 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-10-23 21:06:13 +00:00
|
|
|
pub fn resolve(&self, addr: u64) -> Option<(u64, u64, Arc<Mutex<dyn BusDevice>>)> {
|
2019-07-02 23:10:41 +00:00
|
|
|
if let Some((range, dev)) = self.first_before(addr) {
|
|
|
|
let offset = addr - range.base;
|
|
|
|
if offset < range.len {
|
2019-07-02 23:13:06 +00:00
|
|
|
return Some((range.base, offset, dev));
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Puts the given device at the given address space.
|
2019-10-23 21:06:13 +00:00
|
|
|
pub fn insert(&self, device: Arc<Mutex<dyn BusDevice>>, base: u64, len: u64) -> Result<()> {
|
2019-03-07 13:56:43 +00:00
|
|
|
if len == 0 {
|
2019-10-21 17:32:51 +00:00
|
|
|
return Err(Error::ZeroSizedRange);
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 23:10:41 +00:00
|
|
|
// Reject all cases where the new device's range overlaps with an existing device.
|
|
|
|
if self
|
|
|
|
.devices
|
2019-10-23 21:06:13 +00:00
|
|
|
.read()
|
|
|
|
.unwrap()
|
2019-07-02 23:10:41 +00:00
|
|
|
.iter()
|
|
|
|
.any(|(range, _dev)| range.overlaps(base, len))
|
|
|
|
{
|
2019-03-07 13:56:43 +00:00
|
|
|
return Err(Error::Overlap);
|
|
|
|
}
|
|
|
|
|
2019-07-02 23:10:41 +00:00
|
|
|
if self
|
|
|
|
.devices
|
2019-10-23 21:06:13 +00:00
|
|
|
.write()
|
|
|
|
.unwrap()
|
2020-03-04 15:04:32 +00:00
|
|
|
.insert(BusRange { base, len }, Arc::downgrade(&device))
|
2019-07-02 23:10:41 +00:00
|
|
|
.is_some()
|
|
|
|
{
|
2019-03-07 13:56:43 +00:00
|
|
|
return Err(Error::Overlap);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-21 17:32:51 +00:00
|
|
|
/// Removes the device at the given address space range.
|
2019-10-23 21:06:13 +00:00
|
|
|
pub fn remove(&self, base: u64, len: u64) -> Result<()> {
|
2019-10-21 17:32:51 +00:00
|
|
|
if len == 0 {
|
|
|
|
return Err(Error::ZeroSizedRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
let bus_range = BusRange { base, len };
|
|
|
|
|
2019-10-23 21:06:13 +00:00
|
|
|
if self.devices.write().unwrap().remove(&bus_range).is_none() {
|
2019-10-21 17:32:51 +00:00
|
|
|
return Err(Error::MissingAddressRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-06 14:55:08 +00:00
|
|
|
/// Removes all entries referencing the given device.
|
|
|
|
pub fn remove_by_device(&self, device: &Arc<Mutex<dyn BusDevice>>) -> Result<()> {
|
|
|
|
let mut device_list = self.devices.write().unwrap();
|
|
|
|
let mut remove_key_list = Vec::new();
|
|
|
|
|
|
|
|
for (key, value) in device_list.iter() {
|
|
|
|
if Arc::ptr_eq(&value.upgrade().unwrap(), device) {
|
|
|
|
remove_key_list.push(*key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key in remove_key_list.iter() {
|
|
|
|
device_list.remove(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-21 17:32:51 +00:00
|
|
|
/// Updates the address range for an existing device.
|
|
|
|
pub fn update_range(
|
2019-10-23 21:06:13 +00:00
|
|
|
&self,
|
2019-10-21 17:32:51 +00:00
|
|
|
old_base: u64,
|
|
|
|
old_len: u64,
|
|
|
|
new_base: u64,
|
|
|
|
new_len: u64,
|
|
|
|
) -> Result<()> {
|
|
|
|
// Retrieve the device corresponding to the range
|
|
|
|
let device = if let Some((_, _, dev)) = self.resolve(old_base) {
|
|
|
|
dev.clone()
|
|
|
|
} else {
|
|
|
|
return Err(Error::MissingAddressRange);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove the old address range
|
|
|
|
self.remove(old_base, old_len)?;
|
|
|
|
|
|
|
|
// Insert the new address range
|
|
|
|
self.insert(device, new_base, new_len)
|
|
|
|
}
|
|
|
|
|
2019-03-07 13:56:43 +00:00
|
|
|
/// Reads data from the device that owns the range containing `addr` and puts it into `data`.
|
|
|
|
///
|
|
|
|
/// Returns true on success, otherwise `data` is untouched.
|
2020-09-04 06:42:33 +00:00
|
|
|
pub fn read(&self, addr: u64, data: &mut [u8]) -> Result<()> {
|
2019-07-02 23:22:43 +00:00
|
|
|
if let Some((base, offset, dev)) = self.resolve(addr) {
|
2019-03-07 13:56:43 +00:00
|
|
|
// OK to unwrap as lock() failing is a serious error condition and should panic.
|
|
|
|
dev.lock()
|
|
|
|
.expect("Failed to acquire device lock")
|
2019-07-02 23:22:43 +00:00
|
|
|
.read(base, offset, data);
|
2020-09-04 06:42:33 +00:00
|
|
|
Ok(())
|
2019-03-07 13:56:43 +00:00
|
|
|
} else {
|
2020-09-04 06:42:33 +00:00
|
|
|
Err(Error::MissingAddressRange)
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Writes `data` to the device that owns the range containing `addr`.
|
|
|
|
///
|
|
|
|
/// Returns true on success, otherwise `data` is untouched.
|
2020-09-04 06:42:33 +00:00
|
|
|
pub fn write(&self, addr: u64, data: &[u8]) -> Result<()> {
|
2019-07-02 23:22:43 +00:00
|
|
|
if let Some((base, offset, dev)) = self.resolve(addr) {
|
2019-03-07 13:56:43 +00:00
|
|
|
// OK to unwrap as lock() failing is a serious error condition and should panic.
|
|
|
|
dev.lock()
|
|
|
|
.expect("Failed to acquire device lock")
|
2019-07-02 23:22:43 +00:00
|
|
|
.write(base, offset, data);
|
2020-09-04 06:42:33 +00:00
|
|
|
Ok(())
|
2019-03-07 13:56:43 +00:00
|
|
|
} else {
|
2020-09-04 06:42:33 +00:00
|
|
|
Err(Error::MissingAddressRange)
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
struct DummyDevice;
|
|
|
|
impl BusDevice for DummyDevice {}
|
|
|
|
|
|
|
|
struct ConstantDevice;
|
|
|
|
impl BusDevice for ConstantDevice {
|
2019-07-16 09:27:31 +00:00
|
|
|
fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) {
|
2019-03-07 13:56:43 +00:00
|
|
|
for (i, v) in data.iter_mut().enumerate() {
|
|
|
|
*v = (offset as u8) + (i as u8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 09:27:31 +00:00
|
|
|
fn write(&mut self, _base: u64, offset: u64, data: &[u8]) {
|
2019-03-07 13:56:43 +00:00
|
|
|
for (i, v) in data.iter().enumerate() {
|
|
|
|
assert_eq!(*v, (offset as u8) + (i as u8))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bus_insert() {
|
2019-10-23 21:06:13 +00:00
|
|
|
let bus = Bus::new();
|
2019-03-07 13:56:43 +00:00
|
|
|
let dummy = Arc::new(Mutex::new(DummyDevice));
|
|
|
|
assert!(bus.insert(dummy.clone(), 0x10, 0).is_err());
|
|
|
|
assert!(bus.insert(dummy.clone(), 0x10, 0x10).is_ok());
|
|
|
|
|
|
|
|
let result = bus.insert(dummy.clone(), 0x0f, 0x10);
|
|
|
|
assert!(result.is_err());
|
|
|
|
assert_eq!(format!("{:?}", result), "Err(Overlap)");
|
|
|
|
|
|
|
|
assert!(bus.insert(dummy.clone(), 0x10, 0x10).is_err());
|
|
|
|
assert!(bus.insert(dummy.clone(), 0x10, 0x15).is_err());
|
|
|
|
assert!(bus.insert(dummy.clone(), 0x12, 0x15).is_err());
|
|
|
|
assert!(bus.insert(dummy.clone(), 0x12, 0x01).is_err());
|
|
|
|
assert!(bus.insert(dummy.clone(), 0x0, 0x20).is_err());
|
|
|
|
assert!(bus.insert(dummy.clone(), 0x20, 0x05).is_ok());
|
|
|
|
assert!(bus.insert(dummy.clone(), 0x25, 0x05).is_ok());
|
|
|
|
assert!(bus.insert(dummy.clone(), 0x0, 0x10).is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bus_read_write() {
|
2019-10-23 21:06:13 +00:00
|
|
|
let bus = Bus::new();
|
2019-03-07 13:56:43 +00:00
|
|
|
let dummy = Arc::new(Mutex::new(DummyDevice));
|
|
|
|
assert!(bus.insert(dummy.clone(), 0x10, 0x10).is_ok());
|
2020-09-04 06:42:33 +00:00
|
|
|
assert!(bus.read(0x10, &mut [0, 0, 0, 0]).is_ok());
|
|
|
|
assert!(bus.write(0x10, &[0, 0, 0, 0]).is_ok());
|
|
|
|
assert!(bus.read(0x11, &mut [0, 0, 0, 0]).is_ok());
|
|
|
|
assert!(bus.write(0x11, &[0, 0, 0, 0]).is_ok());
|
|
|
|
assert!(bus.read(0x16, &mut [0, 0, 0, 0]).is_ok());
|
|
|
|
assert!(bus.write(0x16, &[0, 0, 0, 0]).is_ok());
|
|
|
|
assert!(bus.read(0x20, &mut [0, 0, 0, 0]).is_err());
|
|
|
|
assert!(bus.write(0x20, &mut [0, 0, 0, 0]).is_err());
|
|
|
|
assert!(bus.read(0x06, &mut [0, 0, 0, 0]).is_err());
|
|
|
|
assert!(bus.write(0x06, &mut [0, 0, 0, 0]).is_err());
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bus_read_write_values() {
|
2019-10-23 21:06:13 +00:00
|
|
|
let bus = Bus::new();
|
2019-03-07 13:56:43 +00:00
|
|
|
let dummy = Arc::new(Mutex::new(ConstantDevice));
|
|
|
|
assert!(bus.insert(dummy.clone(), 0x10, 0x10).is_ok());
|
|
|
|
|
|
|
|
let mut values = [0, 1, 2, 3];
|
2020-09-04 06:42:33 +00:00
|
|
|
assert!(bus.read(0x10, &mut values).is_ok());
|
2019-03-07 13:56:43 +00:00
|
|
|
assert_eq!(values, [0, 1, 2, 3]);
|
2020-09-04 06:42:33 +00:00
|
|
|
assert!(bus.write(0x10, &values).is_ok());
|
|
|
|
assert!(bus.read(0x15, &mut values).is_ok());
|
2019-03-07 13:56:43 +00:00
|
|
|
assert_eq!(values, [5, 6, 7, 8]);
|
2020-09-04 06:42:33 +00:00
|
|
|
assert!(bus.write(0x15, &values).is_ok());
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-10-23 21:06:13 +00:00
|
|
|
fn busrange_cmp() {
|
2019-07-02 23:10:41 +00:00
|
|
|
let range = BusRange { base: 0x10, len: 2 };
|
|
|
|
assert_eq!(range, BusRange { base: 0x10, len: 3 });
|
|
|
|
assert_eq!(range, BusRange { base: 0x10, len: 2 });
|
2019-03-07 13:56:43 +00:00
|
|
|
|
2019-07-02 23:10:41 +00:00
|
|
|
assert!(range < BusRange { base: 0x12, len: 1 });
|
|
|
|
assert!(range < BusRange { base: 0x12, len: 3 });
|
2019-03-07 13:56:43 +00:00
|
|
|
|
2019-07-02 23:10:41 +00:00
|
|
|
assert_eq!(range, range.clone());
|
2019-03-07 13:56:43 +00:00
|
|
|
|
2019-10-23 21:06:13 +00:00
|
|
|
let bus = Bus::new();
|
2019-03-07 13:56:43 +00:00
|
|
|
let mut data = [1, 2, 3, 4];
|
2020-03-04 15:04:32 +00:00
|
|
|
let device = Arc::new(Mutex::new(DummyDevice));
|
|
|
|
assert!(bus.insert(device.clone(), 0x10, 0x10).is_ok());
|
2020-09-04 06:42:33 +00:00
|
|
|
assert!(bus.write(0x10, &mut data).is_ok());
|
|
|
|
assert!(bus.read(0x10, &mut data).is_ok());
|
2019-03-07 13:56:43 +00:00
|
|
|
assert_eq!(data, [1, 2, 3, 4]);
|
|
|
|
}
|
2019-07-02 23:10:41 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bus_range_overlap() {
|
|
|
|
let a = BusRange {
|
|
|
|
base: 0x1000,
|
|
|
|
len: 0x400,
|
|
|
|
};
|
|
|
|
assert!(a.overlaps(0x1000, 0x400));
|
|
|
|
assert!(a.overlaps(0xf00, 0x400));
|
|
|
|
assert!(a.overlaps(0x1000, 0x01));
|
|
|
|
assert!(a.overlaps(0xfff, 0x02));
|
|
|
|
assert!(a.overlaps(0x1100, 0x100));
|
|
|
|
assert!(a.overlaps(0x13ff, 0x100));
|
|
|
|
assert!(!a.overlaps(0x1400, 0x100));
|
|
|
|
assert!(!a.overlaps(0xf00, 0x100));
|
|
|
|
}
|
2019-03-07 13:56:43 +00:00
|
|
|
}
|