Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SPDX-License-Identifier: GPL-2.0-only OR MIT
- #![recursion_limit = "2048"]
- //! Apple PMPv1 driver
- //!
- //! Copyright (C) The Asahi Linux Contributors
- #![no_std]
- #![cfg(CONFIG_RUST_APPLE_PMP_V1= "y")]
- use core::{
- mem,
- slice,
- };
- use kernel::{
- bindings,
- device::{
- self,
- Core,
- },
- platform,
- devres::Devres,
- dma,
- io::mem::IoMem,
- iosys_map::IoSysMapRef,
- module_platform_driver,
- new_mutex,
- of,
- prelude::*,
- soc::apple::rtkit,
- sync::{
- Arc,
- Mutex,
- },
- types::{
- ARef,
- ForeignOwnable,
- },
- };
- const PMP_MMIO_SIZE: usize = 0x10000;
- const ASC_MMIO_SIZE: usize = 0x4000;
- const PMP_ENDPOINT: u8 = 0x20;
- const OPC_STARTUP: u64 = 0;
- const OPC_CONFIGURE: u64 = 0x10;
- const OPC_CONFIGURE_ACK: u64 = 0x20;
- const OPC_INIT1: u64 = 0x200;
- const OPC_INIT1_ACK: u64 = 0x201;
- const OPC_INIT2: u64 = 0x202;
- const OPC_INIT2_ACK: u64 = 0x203;
- const OPC_DEVPWR: u64 = 0x20e;
- const OPC_DEVPWR_SYNC: u64 = 0x208;
- const OPC_DEVPWR_ACK: u64 = 0x209;
- const OPC_CHANGESTATE1: u64 = 0x20c;
- const OPC_CHANGESTATE1_ACK: u64 = 0x20d;
- const OPC_CHANGESTATE2: u64 = 0x20a;
- const OPC_CHANGESTATE2_ACK: u64 = 0x20b;
- const OPC_SHIFT: u32 = 48;
- type ShMem = dma::CoherentAllocation<u8>;
- struct PmpState {
- devpwr_status: u64
- }
- impl PmpState {
- fn new() -> Result<Self> {
- Ok(PmpState { devpwr_status: 0 })
- }
- fn get_device_power_status(&self, dev: u64) -> bool {
- self.devpwr_status & (1 << dev) == 1
- }
- fn set_device_power_status(&mut self, dev: u64) {
- self.devpwr_status ^= 1 << dev;
- }
- }
- #[pin_data]
- struct PmpData {
- dev: ARef<device::Device>,
- pmp_mmio: Pin<KBox<Devres<IoMem<PMP_MMIO_SIZE>>>>,
- asc_mmio: Pin<KBox<Devres<IoMem<ASC_MMIO_SIZE>>>>,
- #[pin]
- rtkit: Mutex<Option<rtkit::RtKit<PmpData>>>,
- #[pin]
- state: Mutex<PmpState>,
- shmem: ShMem,
- }
- fn build_shmem(dev: &platform::Device<device::Core>) -> Result<ShMem> {
- let mut shmem =
- dma::CoherentAllocation::<u8>::alloc_coherent(dev.as_ref(), PMP_MMIO_SIZE, GFP_KERNEL)?;
- let fwnode = dev.as_ref().fwnode().ok_or(EIO)?;
- // SAFETY:
- // Only this driver communicates with PMP. PMP waits for Configure message
- // thus no r/w operations from the shared memory.
- unsafe {
- shmem.as_slice_mut(0, shmem.count())?.fill(0);
- }
- Ok(shmem)
- }
- impl PmpData {
- fn new(pdev: &platform::Device<Core>) -> Result<Arc<PmpData>> {
- let pmp_req = pdev.io_request_by_name(c"pmp").ok_or(EINVAL)?;
- let pmp_mmio = KBox::pin_init(pmp_req.iomap_sized::<PMP_MMIO_SIZE>(), GFP_KERNEL)?;
- let asc_req = pdev.io_request_by_name(c"asc").ok_or(EINVAL)?;
- let asc_mmio = KBox::pin_init(asc_req.iomap_sized::<ASC_MMIO_SIZE>(), GFP_KERNEL)?;
- Arc::pin_init(
- try_pin_init!(
- PmpData {
- dev: pdev.as_ref().into(),
- pmp_mmio,
- asc_mmio,
- rtkit <- new_mutex!(None),
- state <- new_mutex!(PmpState::new()?),
- shmem: build_shmem(pdev)?,
- }
- ),
- GFP_KERNEL,
- )
- }
- fn start(&self) -> Result<()> {
- let mut guard = self.rtkit.lock();
- let mut rtk = guard.as_mut().as_pin_mut().unwrap();
- rtk.as_mut().wake()?;
- rtk.start_endpoint(PMP_ENDPOINT)
- }
- fn startup(&self) -> Result<u64> {
- self.send_init_config()?;
- Ok(0)
- }
- fn send_init_config(&self) -> Result<u64> {
- let mut dva: u64 = 0xc0000000;
- let mut maps: KVec<u8> = KVec::<u8>::new();
- let node = self.dev.fwnode().ok_or(EIO)?;
- let n_entries = node.property_count_elem::<u64>(c"apple,init-ranges")? / 2;
- let regions = node
- .property_read_array_vec::<u64>(c"apple,init-ranges", n_entries * 2)?
- .required_by(&self.dev)?;
- let domain = unsafe { bindings::iommu_get_domain_for_dev(self.dev.as_raw()) };
- let mut err = 0;
- let mut map_addresses = |reg_indx_start: usize, reg_indx_end: usize, dva: u64| -> Result<()> {
- for i in reg_indx_start..reg_indx_end {
- let addr = regions[i * 2];
- let size: u64 = regions[i * 2 + 1];
- if size == 0 {
- maps.extend_from_slice(&0u64.to_le_bytes(), GFP_KERNEL)?;
- maps.extend_from_slice(&0u64.to_le_bytes(), GFP_KERNEL)?;
- continue;
- }
- pr_info!("map {:#x} -> {:#x} [{:#x}]\n", addr, dva, size);
- unsafe {
- err = bindings::iommu_map(
- domain,
- 0xc0000000 as usize,
- addr,
- size as usize,
- (bindings::IOMMU_READ | bindings::IOMMU_WRITE | bindings::IOMMU_MMIO) as i32,
- bindings::GFP_KERNEL,
- );
- if err != 0 {
- break;
- }
- }
- maps.extend_from_slice(&dva.to_le_bytes(), GFP_KERNEL)?;
- maps.extend_from_slice(&size.to_le_bytes(), GFP_KERNEL)?;
- }
- Ok(())
- };
- // 1st region
- map_addresses(3, 17, dva)?;
- // TODO
- // if err != 0 {
- // return Err(Error::from_errno(err));
- // }
- // 2nd region
- dva = 0xc1000000;
- map_addresses(17, 18, dva)?;
- // 3rd region
- dva = 0xc2000000;
- map_addresses(18, 24, dva)?;
- // so on...
- dva = 0xc3000000;
- map_addresses(24, 30, dva)?;
- dva = 0xc4000000;
- map_addresses(30, 33, dva)?;
- dva = 0xc5000000;
- map_addresses(33, 35, dva)?;
- dva = 0xc0024000;
- map_addresses(35, 37, dva)?;
- dva = 0xc6000000;
- map_addresses(37, 38, dva)?;
- dva = 0xc1004000;
- map_addresses(38, 39, dva)?;
- dva = 0xc3074000;
- map_addresses(39, 42, dva)?;
- dva = 0xc2074000;
- map_addresses(42, 43, dva)?;
- dva = 0xc1008000;
- map_addresses(43, 44, dva)?;
- let configure_msg = (OPC_CONFIGURE << OPC_SHIFT) | (self.shmem.dma_handle() >> 11);
- Ok(configure_msg)
- }
- fn recv_message(&self, msg: u64) -> Result<()> {
- let opc = (msg >> OPC_SHIFT) & 0xFF;
- let reply = match opc {
- OPC_STARTUP => self.startup()?,
- OPC_CONFIGURE_ACK =>
- OPC_INIT1 << OPC_SHIFT | (1 << 15) + 3,
- OPC_INIT1_ACK =>
- OPC_INIT2 << OPC_SHIFT | (1 << 15),
- OPC_INIT2_ACK => 0,
- _ => {
- dev_err!(self.dev, "Got unknown message {}", msg);
- return Err(EIO);
- }
- };
- if reply != 0 {
- let mut rtk_guard = self.rtkit.lock();
- let rtk = rtk_guard.as_mut().as_pin_mut().unwrap();
- rtk.send_message(PMP_ENDPOINT, reply)?;
- }
- Ok(())
- }
- }
- #[allow(dead_code)]
- struct PmpDriverV1(Arc<PmpData>);
- kernel::of_device_table!(
- OF_TABLE,
- MODULE_OF_TABLE,
- (),
- [(of::DeviceId::new(c"apple,t8103-pmp-v1"), ())]
- );
- unsafe impl Send for PmpData {}
- unsafe impl Sync for PmpData {}
- struct NoBuffer;
- impl rtkit::Buffer for NoBuffer {
- fn iova(&self) -> Result<usize> {
- unreachable!()
- }
- fn buf(&mut self) -> Result<IoSysMapRef<'_, u8>> {
- unreachable!()
- }
- }
- #[vtable]
- impl rtkit::Operations for PmpData {
- type Data = Arc<PmpData>;
- type Buffer = NoBuffer;
- fn recv_message(data: <Self::Data as ForeignOwnable>::Borrowed<'_>, _ep: u8, msg: u64) {
- let ret = data.recv_message(msg);
- if let Err(e) = ret {
- dev_err!(data.dev, "Failed to handle rtkit message, error: {:?}", e);
- }
- }
- fn crashed(data: <Self::Data as ForeignOwnable>::Borrowed<'_>, _crashlog: Option<&[u8]>) {
- dev_err!(data.dev, "PMP firmware crashed");
- }
- }
- impl platform::Driver for PmpDriverV1 {
- type IdInfo = ();
- const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
- fn probe(pdev: &platform::Device<Core>, _info: Option<&()>) -> impl PinInit<Self, Error> {
- let dev: ARef<device::Device> = pdev.as_ref().into();
- let data = PmpData::new(pdev)?;
- let rtkit = rtkit::RtKit::<PmpData>::new(&dev, None, 0, data.clone())?;
- *data.rtkit.lock() = Some(rtkit);
- data.start()?;
- Ok(PmpDriverV1(data))
- }
- }
- module_platform_driver! {
- type: PmpDriverV1,
- name: "apple_pmp_v1",
- license: "Dual MIT/GPL",
- }
Advertisement
Add Comment
Please, Sign In to add comment