forked from freebsd/freebsd-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapple_rtkit.rs
128 lines (103 loc) · 3.8 KB
/
apple_rtkit.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2024 Ayrton Muñoz
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#![no_std]
use kpi::bus::{Register, ResourceSpec};
use kpi::device::{Device, BusProbe, SoftcInit};
use kpi::driver;
use kpi::ofw::XRef;
use kpi::cell::Mutable;
use kpi::sync::Arc;
use rtkit::{ManagesRTKit, PwrState, RTKit};
const CPU_CTRL: u64 = 0x44;
const CPU_CTRL_RUN: u32 = 1 << 4;
// this does not need to be explicitly null-terminated
const SPEC: [ResourceSpec; 2] = [
ResourceSpec::new(SYS_RES_MEMORY, 0), /* asc */
ResourceSpec::new(SYS_RES_MEMORY, 1), /* sram */
];
#[derive(Debug)]
pub struct AppleRTKitSoftc {
mem: Mutable<[Register; 2]>,
rtkit: Arc<RTKit>,
}
impl ManagesRTKit for AppleRTKitDriver {
fn get_rtkit(sc: &Self::Softc) -> &Arc<RTKit> {
&sc.rtkit
}
}
impl DeviceIf for AppleRTKitDriver {
type Softc = AppleRTKitSoftc;
fn device_probe(&self, dev: Device) -> Result<BusProbe> {
if !ofw_bus_status_okay(dev) {
return Err(ENXIO);
}
if !ofw_bus_is_compatible(dev, c"apple,rtk-helper-asc4") {
return Err(ENXIO);
}
device_set_desc(dev, c"Apple RTKit helper");
Ok(BUS_PROBE_SPECIFIC)
}
fn device_attach(&self, dev: Device) -> Result<SoftcInit> {
let resources = bus_alloc_resources(dev, SPEC)?;
let regs = resources.map(|r| r.whole_register());
let mem = Mutable::new(regs);
let node = ofw_bus_get_node(dev);
let xref = OF_xref_from_node(node);
OF_device_register_xref(dev, xref);
let rtkit = Arc::new(RTKit::new(dev)?, M_NOWAIT);
let sc = AppleRTKitSoftc {
mem,
rtkit,
};
Ok(self.init_softc(dev, sc))
}
fn device_detach(&self, _dev: Device) -> Result<()> {
unreachable!("device cannot be detached")
}
}
impl AppleRTKitDriver {
fn apple_rtkit_boot(&self, helper: XRef) -> Result<()> {
let dev = OF_device_from_xref(helper)?;
let sc = self.get_softc(dev);
let mut mem = sc.mem.get_mut();
let ctrl = mem[0].read_4(CPU_CTRL);
mem[0].write_4(CPU_CTRL, ctrl | CPU_CTRL_RUN);
self.rtkit_boot(dev)?;
sc.rtkit.set_iop(PwrState::On)?;
sc.rtkit.set_ap(PwrState::On)?;
Ok(())
}
}
driver!(apple_rtkit_driver, c"apple_rtkit", AppleRTKitDriver, apple_rtkit_methods,
device_probe apple_rtkit_probe,
device_attach apple_rtkit_attach,
device_detach apple_rtkit_detach,
{
int apple_rtkit_boot(phandle_t helper);
}
);