mirror of
https://github.com/Creased/RCM-Emulator.git
synced 2026-12-06 02:48:11 +01:00
Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e72741878 | ||
|
|
afc3dfe28a | ||
|
|
41245dc22b | ||
|
|
e17c315f87 | ||
|
|
a9b184d0ea | ||
|
|
ee96a1b8ca | ||
|
|
e2feebf9f7 | ||
|
|
61e1fb981a | ||
|
|
20949b4242 | ||
|
|
d5bf5922ed |
No files matched your search
@@ -16,6 +16,13 @@ emu_run*.log
|
||||
emu_capture*.txt
|
||||
win_run.log
|
||||
last_audio*.wav
|
||||
*.log
|
||||
*.rgba
|
||||
/*.png
|
||||
|
||||
# Generated by linux/build.sh, tests/kernelcpu/build.sh and linux/mkdtb.sh
|
||||
*_blob.h
|
||||
linux/*.dtb
|
||||
|
||||
# User data — never commit dumps or keys
|
||||
*.img
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
[submodule "third_party/imgui"]
|
||||
path = third_party/imgui
|
||||
url = https://github.com/ocornut/imgui.git
|
||||
ignore = untracked
|
||||
@@ -28,6 +28,16 @@ CXXFLAGS = -Wall -g -O2 -std=c++17 -MMD -MP -I. -I$(IMGUI_DIR) -I$(IMGUI_DIR)/ba
|
||||
LDFLAGS =
|
||||
LIBS = -lunicorn -lSDL2 -lpthread
|
||||
|
||||
# UNICORN_DIR=<prefix>: build against a Unicorn installed there (statically)
|
||||
# instead of the system one. Booting a kernel on CPU0 needs Unicorn 2.1 or
|
||||
# later: 2.0.x checks guest accesses against its memory map by VIRTUAL
|
||||
# address once the guest MMU is on, so every access through a kernel's
|
||||
# TTBR1 mapping looks unmapped. Everything else works on either. Run
|
||||
# `make clean` when switching, since the objects depend on its headers.
|
||||
ifneq ($(strip $(UNICORN_DIR)),)
|
||||
CXXFLAGS += -I$(UNICORN_DIR)/include
|
||||
endif
|
||||
|
||||
# pkg-config for SDL2 if available
|
||||
SDL2_CFLAGS := $(shell pkg-config --cflags sdl2 2>/dev/null)
|
||||
SDL2_LIBS := $(shell pkg-config --libs sdl2 2>/dev/null)
|
||||
@@ -37,6 +47,11 @@ ifneq ($(SDL2_CFLAGS),)
|
||||
LIBS = -lunicorn $(SDL2_LIBS) -lpthread
|
||||
endif
|
||||
|
||||
# After the SDL2 block above, which sets LIBS again.
|
||||
ifneq ($(strip $(UNICORN_DIR)),)
|
||||
LIBS := $(UNICORN_DIR)/lib/libunicorn.a $(if $(SDL2_LIBS),$(SDL2_LIBS),-lSDL2) -lpthread -lm
|
||||
endif
|
||||
|
||||
# ---- Windows cross-build ---------------------------------------------------
|
||||
#
|
||||
# `make windows` builds rcm_emu.exe with the MinGW toolchain. Both deps come
|
||||
@@ -98,6 +113,9 @@ tests/ccplex/payload.bin: tests/ccplex/payload.c tests/ccplex/link.ld
|
||||
$(TEST_CC) $(TEST_CFLAGS) -T tests/ccplex/link.ld -o tests/ccplex/payload.elf $<
|
||||
$(TEST_OBJCOPY) -O binary tests/ccplex/payload.elf $@
|
||||
|
||||
tests/kernelcpu/payload.bin: tests/kernelcpu/payload.c tests/kernelcpu/cpu0.S tests/kernelcpu/link.ld
|
||||
sh tests/kernelcpu/build.sh
|
||||
|
||||
tests/se/payload.bin: tests/se/payload.c tests/se/vectors.h tests/se/link.ld
|
||||
$(TEST_CC) $(TEST_CFLAGS) -T tests/se/link.ld -o tests/se/payload.elf $<
|
||||
$(TEST_OBJCOPY) -O binary tests/se/payload.elf $@
|
||||
@@ -116,8 +134,9 @@ tests/display/payload_%.bin: tests/display/payload.c tests/display/link.ld
|
||||
$(TEST_CC) $(TEST_CFLAGS) -DSCENARIO=$* -T tests/display/link.ld -o tests/display/payload_$*.elf $<
|
||||
$(TEST_OBJCOPY) -O binary tests/display/payload_$*.elf $@
|
||||
|
||||
test: $(OUTPUT) tests/ccplex/payload.bin tests/se/payload.bin $(USB_PAYLOADS) $(DISPLAY_PAYLOADS)
|
||||
test: $(OUTPUT) tests/ccplex/payload.bin tests/kernelcpu/payload.bin tests/se/payload.bin $(USB_PAYLOADS) $(DISPLAY_PAYLOADS)
|
||||
sh tests/ccplex/run.sh ./$(OUTPUT) tests/ccplex/payload.bin
|
||||
sh tests/kernelcpu/run.sh ./$(OUTPUT) tests/kernelcpu/payload.bin
|
||||
sh tests/se/run.sh ./$(OUTPUT) tests/se/payload.bin
|
||||
sh tests/usb/run.sh ./$(OUTPUT) tests/usb
|
||||
sh tests/display/run.sh ./$(OUTPUT) tests/display
|
||||
|
||||
@@ -29,13 +29,21 @@ slow or destructive.
|
||||
- Replay deterministic button sequences via `--auto-pin-recovery` or
|
||||
`--auto-te-script`, so a payload flow can be exercised from a CI run or a
|
||||
one-shot script. `--input-script` scripts buttons and touchscreen taps for
|
||||
any payload.
|
||||
any payload, and can save the screen at a given point of a run.
|
||||
- Plug an emulated PC into the USB-C port (`--usb-host`): Nyx's USB mass
|
||||
storage and gamepad gadgets enumerate on both the Erista (USB2) and Mariko
|
||||
(XUSB) device controllers, and the PC reads a mass storage disk back,
|
||||
checks it against the SD image, writes a few sectors back unchanged and
|
||||
ejects it. With `--usb-host nbd` the PC serves the disk over NBD instead,
|
||||
so it can be mounted on the machine running the emulator.
|
||||
- Boot a Linux kernel on CPU0 (switchroot's L4T 4.9), with a GIC-400, the
|
||||
generic timer, PSCI and the clock controller's reset values for it. See
|
||||
[Booting Linux on CPU0](#booting-linux-on-cpu0).
|
||||
- Put a dock on the USB-C port (`--dock`): a Nintendo dock, a third-party
|
||||
USB-C adapter or a Nintendo charger on the BM92T36, the dock's
|
||||
DisplayPort-to-HDMI converter on DPAUX1, and a TV in a second window that
|
||||
shows what display head B sends through SOR1. hwtest-rcm's Dock Test runs
|
||||
its whole handshake and TV link against it.
|
||||
- Tweak emulated hardware live (battery, charger, thermal, USB-PD, PMIC,
|
||||
fuses, SD insertion, the USB host) from a side window. See [Live hardware tweaks](#live-hardware-tweaks).
|
||||
- Inspect each Tegra UART port (TX history) and inject keystrokes into the
|
||||
@@ -55,6 +63,16 @@ sudo apt-get install -y build-essential libunicorn-dev libsdl2-dev
|
||||
make
|
||||
```
|
||||
|
||||
Booting a kernel on CPU0 needs Unicorn 2.1 or later: 2.0.x, which some
|
||||
distributions still ship, checks guest accesses by virtual address once the
|
||||
guest MMU is on. Build one into a prefix and point the Makefile at it, which
|
||||
links it statically (`make clean` first when switching):
|
||||
|
||||
```bash
|
||||
make UNICORN_DIR=$HOME/opt/unicorn-2.1
|
||||
ldd rcm_emu | grep unicorn # prints nothing when the static one is in
|
||||
```
|
||||
|
||||
### Build (Docker)
|
||||
|
||||
```bash
|
||||
@@ -180,10 +198,37 @@ run: CPU rail off (MAX77621 VOUT_EN / MAX77620 GPIO5 EN pin)`. PCIe and
|
||||
MSELECT answer CPU0 only; the BPMP reads all ones from them, as measured on
|
||||
real consoles. Details in [DESIGN.md](DESIGN.md#ccplex-cpu0).
|
||||
|
||||
### Booting Linux on CPU0
|
||||
|
||||
`linux/` boots an arm64 kernel Image, switchroot's L4T 4.9 for instance, the
|
||||
way a bootloader would hand over to it:
|
||||
|
||||
```bash
|
||||
linux/build.sh # linux/boot.bin (arm-none-eabi + aarch64 gcc)
|
||||
linux/mkdtb.sh tegra210-odin.dtb odin-emu.dtb # console on UART-B, one CPU
|
||||
linux/run.sh Image odin-emu.dtb boot.log 120
|
||||
```
|
||||
|
||||
`--load FILE@ADDR` places the Image at 0x80080000 and the device tree at
|
||||
0x83000000 before the payload starts; `linux/boot.bin` sets the clocks and
|
||||
the CPU rail, releases CPU0 into `linux/shim.S`, which drops to EL1 with the
|
||||
device tree in x0, and halts the BPMP. For the kernel, CPU0 has a GIC-400
|
||||
(distributor and CPU interface), the generic timer counting emulated time
|
||||
at 19.2 MHz, interrupts taken at block boundaries, BRK and undefined
|
||||
instructions taken to its own vectors (WARN and BUG), PSCI on SMC, WFI
|
||||
sleeping to the next timer, TMR10..13 as clock event timers, and every
|
||||
unmodelled page of the peripheral band mapped as plain registers on first
|
||||
access. The clock controller's registers read their TRM reset values until
|
||||
written ([t210/car_reset.h](t210/car_reset.h)), since the kernel's clock
|
||||
driver walks hundreds of them. `RCM_EMU_CPU0_PROFILE=1` prints where CPU0 is
|
||||
once a second of real time, to tell a hung kernel from a slow one. The
|
||||
console comes out on UART-B; the saved runs reach the kernel's core
|
||||
initialisation.
|
||||
|
||||
## Tests
|
||||
|
||||
```bash
|
||||
make test # CCPLEX, SE, USB + display regression payloads (arm-none-eabi-gcc, python3)
|
||||
make test # CCPLEX, kernel CPU, SE, USB + display regression payloads (arm-none-eabi-gcc, aarch64 gcc, python3)
|
||||
tests/hwtest/build.sh build-hwtest # build hwtest-rcm (+ gcc-aarch64-linux-gnu)
|
||||
tests/hwtest/run.sh ./rcm_emu build-hwtest/hwtest-rcm/build/hwtest.bin
|
||||
```
|
||||
@@ -192,6 +237,8 @@ tests/hwtest/run.sh ./rcm_emu build-hwtest/hwtest-rcm/build/hwtest.bin
|
||||
refusal of an unpowered release, the EL3 entry, the mailbox, a stopped clock,
|
||||
WFE parking and the reset, then runs again with a dead CPU rail and dead
|
||||
bucks (`--fault`) and checks the RAM repair is held and the core stays dark;
|
||||
one drops CPU0 to EL1 and checks it takes timer interrupts through the GIC,
|
||||
a BRK into its own vector and a PSCI call over SMC;
|
||||
one checks the Security Engine's RSA, SHA-256, AES
|
||||
and RNG against vectors computed in Python; one brings up a USB mass storage
|
||||
gadget on each device controller (and a HID gadget) for the `--usb-host` PC to
|
||||
@@ -322,7 +369,9 @@ needed.
|
||||
| `--wifi-radio` | `healthy` \| `faulty` \| `absent` | The WLAN half of the same CYW4356: a PCI Express endpoint on root port 1, reachable only from CPU0 (the BPMP reads all ones, as on silicon). `healthy` trains the link, enumerates as `14E4:43EC` and answers a ChipCommon ChipID read with `0x4356`; `faulty` still trains and enumerates but reads all ones on the backplane (live PCIe front-end, dead radio die); `absent` never leaves detect. The model also enforces the datasheet's power-up ordering, so a payload that releases PERST# too early gets a link that stays down and a `[pcie]` line naming the reason. Overrides `[wifi] radio` in the ini. Default `healthy`. |
|
||||
| `--fault` | `NAME[,NAME…]` \| `list` | Break a chip or a rail; repeatable. A **chip** fault (`max77812`, `max77621_cpu`, `max77621_gpu`, `bq24193`, `max17050`, `tmp451`, `bm92t36`) makes the part NAK its I2C address, refuse writes and take every rail it supplies down with it. A **rail** fault (`vdd_cpu`) leaves the regulator answering and reading back as enabled while its output never comes up, so only what the rail feeds notices: the CPU RAM repair never completes and CPU0 cannot run. `list` prints the names and exits. Faults survive a payload reboot, like the hardware they model, and add to `[faults]` in the ini (`NAME=dead`). See [DESIGN.md](DESIGN.md#fault-injection). |
|
||||
| `--usb-host` | `nbd[:port]` (optional) | A PC on the USB-C port. It enumerates whatever USB gadget the payload brings up; a mass storage disk is read back (checked against the SD image), its last 8 sectors are written back unchanged and read again unless it is write-protected, and it is ejected; HID reports are polled. With `nbd` the disk is served over NBD on 127.0.0.1 (port 10809 by default) until the client disconnects, then ejected - see [Mounting a gadget's disk](#mounting-a-gadgets-disk). Logged as `[usb-host]`; `RCM_USB_TRACE=1` traces every transfer. Overrides `[usb] host` and `[usb] nbd_port` in the ini. |
|
||||
| `--input-script` | file or spec | Scripted input keyed to emulated time: `<ms> P\|U\|D [hold_ms]` presses POWER / VOL+ / VOL-, `<ms> TAP <x> <y> [hold_ms]` taps the touchscreen at (x, y) in the 1280x720 picture, `+N` times an event N ms after the previous one. Events are separated by `,`, `;` or newlines. |
|
||||
| `--load` | `FILE@ADDR` | Place a file in DRAM before the payload starts, the way a bootloader leaves a kernel and its device tree; repeatable. See [Booting Linux on CPU0](#booting-linux-on-cpu0). |
|
||||
| `--dock` | `nintendo` \| `thirdparty` \| `charger` \| `none`, `:notv` | What is on the USB-C port, behind the BM92T36: a Nintendo dock (057E:2003, 15 V, Nintendo's alt mode, DisplayPort on pin D), a third-party adapter (Fresco Logic 1D5C:7102, four power profiles, the DisplayPort SVID) or the Nintendo charger (057E:2004, no video). A dock's DisplayPort-to-HDMI converter answers on DPAUX1 (DPCD 1.2, the TV's EDID over I2C-over-AUX), and SOR1 judges link training lane by lane against the board's wiring; once trained, head B's frames show in a TV window. `:notv` leaves the dock's HDMI empty. `RCM_EMU_TV_SNAP=file` saves the TV picture, `RCM_EMU_DPTRACE=file` traces DPAUX1 and SOR1. Overrides `[dock] kind` and `[dock] tv` in the ini. Default `none`. |
|
||||
| `--input-script` | file or spec | Scripted input keyed to emulated time: `<ms> P\|U\|D [hold_ms]` presses POWER / VOL+ / VOL-, `<ms> TAP <x> <y> [hold_ms]` taps the touchscreen at (x, y) in the 1280x720 picture, `<ms> SNAP <file>` saves the next displayed frame to `<file>` (raw B, G, R, A like `last_fb.rgba`, size in the matching `.meta`), `+N` times an event N ms after the previous one. Events are separated by `,`, `;` or newlines. |
|
||||
| `--auto-pin-recovery` | (none) | Drive the Lockpick PIN-recovery menu without user input. |
|
||||
| `--auto-te-script` | (none) | Drive `recover_pin.te` in TegraExplorer without input. |
|
||||
|
||||
@@ -334,14 +383,16 @@ flowchart LR
|
||||
root --> top["main.cpp<br/>emu_state.h<br/>Makefile<br/>Dockerfile<br/>README.md / DESIGN.md"]
|
||||
root --> t210["t210/<br/>(SoC peripheral models)"]
|
||||
root --> display["display/"]
|
||||
root --> tests["tests/<br/>ccplex/, se/, usb/, display/, hwtest/"]
|
||||
root --> linux["linux/<br/>kernel boot: boot.c, shim.S, run.sh"]
|
||||
root --> tests["tests/<br/>ccplex/, kernelcpu/, se/, usb/, display/, hwtest/"]
|
||||
|
||||
t210 --> mmio["mmio.{h,cpp}<br/>memory_map.h<br/>regcache.h, tegra_bl.h"]
|
||||
t210 --> cpus["bpmp.{h,cpp} BPMP clock<br/>ccplex.{h,cpp} CPU0 (A57)"]
|
||||
t210 --> cpus["bpmp.{h,cpp} BPMP clock<br/>ccplex.{h,cpp} CPU0 (A57)<br/>gic.{h,cpp} GIC-400"]
|
||||
t210 --> pcie["pcie.{h,cpp}<br/>root complex + CYW4356"]
|
||||
t210 --> se["se_engine.{h,cpp}<br/>AES-128, SHA-256, RSA, RNG"]
|
||||
t210 --> i2c["i2c3.{h,cpp}<br/>STMFTS / FTS4 touch"]
|
||||
t210 --> usb["usb.{h,cpp}<br/>USB device controllers + host PC"]
|
||||
t210 --> dock["dock.{h,cpp}<br/>BM92T36, the dock, DPAUX1, SOR1"]
|
||||
|
||||
display --> sdl["sdl_display.{h,cpp}<br/>DC window scan-out<br/>+ SDL2"]
|
||||
```
|
||||
|
||||
@@ -140,6 +140,8 @@ void reset_to_defaults(EmuState *s) {
|
||||
X(usb_pd_inserted, "usb_pd", "inserted", 0) \
|
||||
X(usb_pd_voltage_mv, "usb_pd", "voltage_mv", 0) \
|
||||
X(usb_pd_amperage_ma, "usb_pd", "amperage_ma", 0) \
|
||||
X(dock_kind, "dock", "kind", 0) \
|
||||
X(dock_tv, "dock", "tv", 0) \
|
||||
X(usb_host, "usb", "host", 0) \
|
||||
X(usb_nbd_port, "usb", "nbd_port", 0) \
|
||||
/* SoC / PMIC */ \
|
||||
|
||||
+128
-16
@@ -1,4 +1,7 @@
|
||||
#include "sdl_display.h"
|
||||
#include "../t210/dock.h"
|
||||
#include "../t210/mmio.h"
|
||||
#include <string>
|
||||
#include "config_window.h"
|
||||
#include "console_window.h"
|
||||
#include "../emu_state.h"
|
||||
@@ -290,8 +293,9 @@ uint32_t dst_factor(uint32_t sel, uint32_t k1, uint32_t k2, uint32_t sa) {
|
||||
}
|
||||
}
|
||||
|
||||
// Draw window w onto the panel picture.
|
||||
void draw_window(const Win &w, std::vector<uint32_t> &panel) {
|
||||
// Draw window w onto the panel picture (pw x ph, row-major).
|
||||
void draw_window(const Win &w, std::vector<uint32_t> &panel, uint32_t pw = kPanelW,
|
||||
uint32_t ph = kPanelH) {
|
||||
bool blend = !(w.blend_layer & (1u << 24));
|
||||
uint32_t k1 = (w.blend_layer >> 8) & 0xFF, k2 = (w.blend_layer >> 16) & 0xFF;
|
||||
uint32_t ssel = w.blend_match & 7, dsel = (w.blend_match >> 4) & 7;
|
||||
@@ -304,12 +308,12 @@ void draw_window(const Win &w, std::vector<uint32_t> &panel) {
|
||||
txs[ox] = (uint32_t)((uint64_t)ox * w.pre_w / w.out_w);
|
||||
for (uint32_t oy = 0; oy < w.out_h; oy++) {
|
||||
int32_t py = w.y + (int32_t)oy;
|
||||
if (py < 0 || py >= (int32_t)kPanelH)
|
||||
if (py < 0 || py >= (int32_t)ph)
|
||||
continue;
|
||||
uint32_t ty = (uint32_t)((uint64_t)oy * w.pre_h / w.out_h);
|
||||
for (uint32_t ox = 0; ox < w.out_w; ox++) {
|
||||
int32_t px = w.x + (int32_t)ox;
|
||||
if (px < 0 || px >= (int32_t)kPanelW)
|
||||
if (px < 0 || px >= (int32_t)pw)
|
||||
continue;
|
||||
uint32_t tx = txs[ox];
|
||||
// (tx, ty) is post-turn; walk it back to the surface.
|
||||
@@ -322,7 +326,7 @@ void draw_window(const Win &w, std::vector<uint32_t> &panel) {
|
||||
if (off + w.bpp > w.len)
|
||||
continue;
|
||||
uint32_t s = depth_argb(w.mem + off, w.depth);
|
||||
uint32_t &d = panel[(size_t)py * kPanelW + px];
|
||||
uint32_t &d = panel[(size_t)py * pw + px];
|
||||
if (!blend) {
|
||||
d = s | 0xFF000000u;
|
||||
continue;
|
||||
@@ -339,9 +343,98 @@ void draw_window(const Win &w, std::vector<uint32_t> &panel) {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- The TV on the dock ------------------------------------------------------
|
||||
//
|
||||
// A second window, shown while a dock with video is configured: head B's
|
||||
// windows composed at the head's active size, as SOR1 sends them, when the
|
||||
// dock model says frames reach the TV; otherwise a dark screen, and the
|
||||
// window title says why there is no picture.
|
||||
SDL_Window *tv_window = nullptr;
|
||||
SDL_Renderer *tv_renderer = nullptr;
|
||||
SDL_Texture *tv_texture = nullptr;
|
||||
uint32_t tv_tex_w = 0, tv_tex_h = 0;
|
||||
std::string tv_title;
|
||||
|
||||
void tv_update(EmuState *state, uc_engine *uc) {
|
||||
uint8_t k = state->dock_kind.load();
|
||||
bool want = (k == 1 || k == 2) && state->dock_tv.load();
|
||||
if (!want) {
|
||||
if (tv_window)
|
||||
SDL_HideWindow(tv_window);
|
||||
return;
|
||||
}
|
||||
if (!tv_window) {
|
||||
tv_window = SDL_CreateWindow("TV (dock HDMI)", SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED, 960, 540,
|
||||
SDL_WINDOW_RESIZABLE);
|
||||
if (!tv_window)
|
||||
return;
|
||||
tv_renderer = SDL_CreateRenderer(tv_window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (!tv_renderer)
|
||||
tv_renderer = SDL_CreateRenderer(tv_window, -1, SDL_RENDERER_SOFTWARE);
|
||||
}
|
||||
SDL_ShowWindow(tv_window);
|
||||
TvState t;
|
||||
dock_tv_state(state, &t);
|
||||
uint32_t act = mmio_bus_read(state, 0x54240000 + 0x409 * 4, 4);
|
||||
uint32_t w = act & 0x1FFF, h = (act >> 16) & 0x1FFF;
|
||||
char title[256];
|
||||
if (t.video && w && h)
|
||||
snprintf(title, sizeof(title), "TV (dock HDMI) - %ux%u, %s", w, h, t.status);
|
||||
else
|
||||
snprintf(title, sizeof(title), "TV (dock HDMI) - no signal: %s", t.status);
|
||||
if (tv_title != title) {
|
||||
tv_title = title;
|
||||
SDL_SetWindowTitle(tv_window, title);
|
||||
printf("[tv] %s\n", title + 16);
|
||||
fflush(stdout);
|
||||
}
|
||||
SDL_SetRenderDrawColor(tv_renderer, 8, 8, 24, 255);
|
||||
SDL_RenderClear(tv_renderer);
|
||||
if (t.video && w && h && w <= 4096 && h <= 4096) {
|
||||
std::vector<uint32_t> pic((size_t)w * h, 0xFF000000u);
|
||||
std::vector<Win> wins;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Win wn;
|
||||
if (load_window(state, uc, i, state->dcb_win_active[i], -1, wn))
|
||||
wins.push_back(std::move(wn));
|
||||
}
|
||||
std::stable_sort(wins.begin(), wins.end(),
|
||||
[](const Win &a, const Win &b) { return a.layer > b.layer; });
|
||||
for (const Win &wn : wins)
|
||||
draw_window(wn, pic, w, h);
|
||||
if (!tv_texture || tv_tex_w != w || tv_tex_h != h) {
|
||||
if (tv_texture)
|
||||
SDL_DestroyTexture(tv_texture);
|
||||
tv_texture = SDL_CreateTexture(tv_renderer, SDL_PIXELFORMAT_ARGB8888,
|
||||
SDL_TEXTUREACCESS_STREAMING, (int)w, (int)h);
|
||||
tv_tex_w = w;
|
||||
tv_tex_h = h;
|
||||
}
|
||||
if (tv_texture) {
|
||||
SDL_UpdateTexture(tv_texture, nullptr, pic.data(), (int)(w * 4));
|
||||
SDL_RenderCopy(tv_renderer, tv_texture, nullptr, nullptr);
|
||||
}
|
||||
if (const char *snap = getenv("RCM_EMU_TV_SNAP")) {
|
||||
static uint64_t last = 0;
|
||||
if (state->emu_usec - last > 500000) {
|
||||
last = state->emu_usec;
|
||||
if (FILE *f = fopen(snap, "wb")) {
|
||||
fwrite(&w, 4, 1, f);
|
||||
fwrite(&h, 4, 1, f);
|
||||
fwrite(pic.data(), 4, pic.size(), f);
|
||||
fclose(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
SDL_RenderPresent(tv_renderer);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void sdl_display_update(EmuState *state, uc_engine *uc) {
|
||||
tv_update(state, uc);
|
||||
if (!renderer)
|
||||
return;
|
||||
|
||||
@@ -475,6 +568,25 @@ void sdl_display_update(EmuState *state, uc_engine *uc) {
|
||||
SDL_UpdateTexture(texture, nullptr, g_frame.data(), out_w * 4);
|
||||
}
|
||||
|
||||
// Snapshots the input script asked for, from this frame.
|
||||
std::string snap;
|
||||
while (!g_frame.empty() && state->snap_take(&snap)) {
|
||||
if (FILE *f = fopen(snap.c_str(), "wb")) {
|
||||
fwrite(g_frame.data(), 1, g_frame.size() * 4, f);
|
||||
fclose(f);
|
||||
std::string meta = snap;
|
||||
if (meta.size() > 5 && meta.compare(meta.size() - 5, 5, ".rgba") == 0)
|
||||
meta.resize(meta.size() - 5);
|
||||
meta += ".meta";
|
||||
if (FILE *fm = fopen(meta.c_str(), "w")) {
|
||||
fprintf(fm, "%u %u\n", out_w, out_h);
|
||||
fclose(fm);
|
||||
}
|
||||
printf("[snap] %s (%ux%u)\n", snap.c_str(), out_w, out_h);
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
|
||||
// Snapshot for PNG conversion - at most once a second (~60 frames), and
|
||||
// only when the frame has changed since the last one written, so an idle
|
||||
// payload does not rewrite a 3.5 MB file every second.
|
||||
@@ -632,14 +744,20 @@ bool sdl_display_poll_events(EmuState *state, uc_engine *uc) {
|
||||
uint32_t step = shift ? 4096 : 128;
|
||||
switch (event.key.keysym.sym) {
|
||||
case SDLK_UP:
|
||||
if (!state->btn_vol_up)
|
||||
// One press of 150 ms guest time per keydown (the host's
|
||||
// auto-repeat makes more while the key is held).
|
||||
if (!state->btn_vol_up) {
|
||||
printf("[input] DOWN VOL_UP @emu_usec=%llu\n", (unsigned long long)state->emu_usec);
|
||||
state->btn_vol_up = true;
|
||||
state->kbd_vol_up_until = state->emu_usec + 150000;
|
||||
state->btn_vol_up = true;
|
||||
}
|
||||
break;
|
||||
case SDLK_DOWN:
|
||||
if (!state->btn_vol_down)
|
||||
if (!state->btn_vol_down) {
|
||||
printf("[input] DOWN VOL_DOWN @emu_usec=%llu\n", (unsigned long long)state->emu_usec);
|
||||
state->btn_vol_down = true;
|
||||
state->kbd_vol_down_until = state->emu_usec + 150000;
|
||||
state->btn_vol_down = true;
|
||||
}
|
||||
break;
|
||||
case SDLK_RETURN:
|
||||
if (!state->btn_power)
|
||||
@@ -707,14 +825,8 @@ bool sdl_display_poll_events(EmuState *state, uc_engine *uc) {
|
||||
if (event.type == SDL_KEYUP) {
|
||||
switch (event.key.keysym.sym) {
|
||||
case SDLK_UP:
|
||||
if (state->btn_vol_up)
|
||||
printf("[input] UP VOL_UP @emu_usec=%llu\n", (unsigned long long)state->emu_usec);
|
||||
state->btn_vol_up = false;
|
||||
break;
|
||||
case SDLK_DOWN:
|
||||
if (state->btn_vol_down)
|
||||
printf("[input] UP VOL_DOWN @emu_usec=%llu\n", (unsigned long long)state->emu_usec);
|
||||
state->btn_vol_down = false;
|
||||
// The press ends on its own, 150 ms of guest time after it began.
|
||||
break;
|
||||
case SDLK_RETURN:
|
||||
if (state->btn_power)
|
||||
|
||||
+49
@@ -215,6 +215,12 @@ struct EmuState {
|
||||
std::atomic<bool> btn_vol_up{false};
|
||||
std::atomic<bool> btn_vol_down{false};
|
||||
std::atomic<bool> btn_power{false};
|
||||
// A keyboard VOL press ends at this guest time (0: none running). The
|
||||
// arrow keys press for a fixed guest duration: guest time outruns the
|
||||
// wall clock while the guest sleeps, so a held key's wall time means
|
||||
// little to the guest.
|
||||
std::atomic<uint64_t> kbd_vol_up_until{0};
|
||||
std::atomic<uint64_t> kbd_vol_down_until{0};
|
||||
|
||||
// Touchscreen state (FTS4/STMFTS stub on I2C3 @ slave 0x49).
|
||||
// Updated by SDL mouse events; consumed by i2c3_*/STMFTS code on CPU thread.
|
||||
@@ -250,6 +256,28 @@ struct EmuState {
|
||||
tc_q_tail.store(tail + 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Framebuffer snapshots asked for by the input script (SNAP), written by
|
||||
// the display thread from the next frame it shows. CPU thread produces,
|
||||
// display thread consumes. A spin lock, not std::mutex: the Windows
|
||||
// build's MinGW uses the win32 thread model, which has none.
|
||||
std::atomic<bool> snap_busy{false};
|
||||
std::vector<std::string> snap_paths;
|
||||
void snap_request(const std::string &path) {
|
||||
while (snap_busy.exchange(true, std::memory_order_acquire)) {}
|
||||
snap_paths.push_back(path);
|
||||
snap_busy.store(false, std::memory_order_release);
|
||||
}
|
||||
bool snap_take(std::string *path) {
|
||||
while (snap_busy.exchange(true, std::memory_order_acquire)) {}
|
||||
bool got = !snap_paths.empty();
|
||||
if (got) {
|
||||
*path = snap_paths.front();
|
||||
snap_paths.erase(snap_paths.begin());
|
||||
}
|
||||
snap_busy.store(false, std::memory_order_release);
|
||||
return got;
|
||||
}
|
||||
// last_rot mirrors the rotation last applied by sdl_display_update.
|
||||
// Read from the SDL event handler to invert the display→window transform.
|
||||
std::atomic<uint32_t> last_rot{0};
|
||||
@@ -265,6 +293,12 @@ struct EmuState {
|
||||
uint32_t dc_win[4][dcwin::kRegs] = {};
|
||||
uint32_t dc_win_active[4][dcwin::kRegs] = {};
|
||||
uint32_t dc_window_header = 0; // DC_CMD_DISPLAY_WINDOW_HEADER
|
||||
// Display head B (DC1, 0x54240000): the head that feeds SOR1 and the
|
||||
// dock. Same window model as head A, scanned out to the TV window.
|
||||
uint32_t dcb_win[4][dcwin::kRegs] = {};
|
||||
uint32_t dcb_win_active[4][dcwin::kRegs] = {};
|
||||
uint32_t dcb_window_header = 0;
|
||||
bool dcb_programmed = false;
|
||||
// False from reset until the payload first activates a window. Until
|
||||
// then the scan-out shows the emulator's own framebuffer (FB_BASE).
|
||||
bool dc_programmed = false;
|
||||
@@ -474,6 +508,19 @@ struct EmuState {
|
||||
|
||||
// USB-PD controller (BM92T36, I2C1 @ 0x18). One synthesized fixed PDO.
|
||||
std::atomic<bool> usb_pd_inserted{false};
|
||||
// The dock on the USB-C port (t210/dock.h DockKind) and whether a TV is
|
||||
// on its HDMI output. [dock] kind=0 none, 1 Nintendo dock, 2 third-party
|
||||
// adapter, 3 Nintendo charger; tv=0/1. --dock overrides.
|
||||
std::atomic<uint8_t> dock_kind{0};
|
||||
std::atomic<bool> dock_tv{true};
|
||||
// A clone of the Nintendo dock: 1 (--dock nintendo:clone) answers the
|
||||
// reads HOS docks with (state 0x16, 0x0B, 0x18) but not the LED or hub
|
||||
// messages; 2 (nintendo:mute) answers none of the dock's own messages.
|
||||
std::atomic<uint8_t> dock_clone{0};
|
||||
// --dock ...:latehpd: the TV's HPD comes 25 s after the DisplayPort
|
||||
// configuration instead of 150 ms (a VIA Labs adapter, 2109:0103,
|
||||
// raised it after hwtest's 1.5 s wait; 25 s outlasts any wait).
|
||||
std::atomic<bool> dock_late_hpd{false};
|
||||
std::atomic<uint16_t> usb_pd_voltage_mv{5000};
|
||||
std::atomic<uint16_t> usb_pd_amperage_ma{1500};
|
||||
|
||||
@@ -596,6 +643,8 @@ struct EmuState {
|
||||
static constexpr size_t N_UARTS = 5;
|
||||
std::string uart_tx_log[N_UARTS];
|
||||
std::deque<uint8_t> uart_rx_fifo[N_UARTS];
|
||||
// Bytes the emulated PC sends to a USB serial port (USBSER events).
|
||||
std::deque<uint8_t> usb_serial_rx;
|
||||
};
|
||||
|
||||
#endif // EMU_STATE_H
|
||||
+76
-4
@@ -17,6 +17,22 @@
|
||||
// touches the screen at (x, y) in the 1280x720 landscape picture bdk's
|
||||
// touch driver reports (Nyx's coordinates), and lifts after hold_ms.
|
||||
//
|
||||
// <when> USBSER <text>
|
||||
//
|
||||
// types text into the USB serial port the PC has open (escapes as UART).
|
||||
//
|
||||
// <when> USBSERFILE <file>
|
||||
//
|
||||
// sends a file's raw contents through that port, as UARTFILE does on
|
||||
// UART-B.
|
||||
//
|
||||
// <when> SNAP <file>
|
||||
//
|
||||
// writes the next frame the display shows to <file>, raw like last_fb.rgba
|
||||
// (B, G, R, A bytes) with its size in the matching .meta. For screenshots
|
||||
// at a known point of a run: guest time runs faster than the wall clock,
|
||||
// so the once-a-second last_fb.rgba cannot be timed from outside.
|
||||
//
|
||||
// '#' starts a comment. Example - walk 6 entries down a menu and select:
|
||||
//
|
||||
// rcm_emu payload.bin --input-script "3000 D, + D, + D, + D, + D, + D, + P"
|
||||
@@ -42,7 +58,7 @@
|
||||
|
||||
namespace {
|
||||
|
||||
enum class Key { Power, VolUp, VolDown, Uart, UartFile, Tap };
|
||||
enum class Key { Power, VolUp, VolDown, Uart, UartFile, UsbSer, UsbSerFile, Tap, Snap };
|
||||
|
||||
struct Event {
|
||||
uint64_t at_us = 0; // press time
|
||||
@@ -50,7 +66,7 @@ struct Event {
|
||||
Key key = Key::Power;
|
||||
bool pressed = false;
|
||||
bool released = false;
|
||||
std::string data; // UART / UARTFILE payload
|
||||
std::string data; // UART / UARTFILE payload, SNAP file
|
||||
uint16_t x = 0, y = 0; // TAP: panel-raw coordinates
|
||||
};
|
||||
|
||||
@@ -67,7 +83,10 @@ const char *key_name(Key k) {
|
||||
case Key::VolUp: return "VOL+";
|
||||
case Key::Uart: return "UART";
|
||||
case Key::UartFile: return "UARTFILE";
|
||||
case Key::UsbSer: return "USBSER";
|
||||
case Key::UsbSerFile: return "USBSERFILE";
|
||||
case Key::Tap: return "TAP";
|
||||
case Key::Snap: return "SNAP";
|
||||
default: return "VOL-";
|
||||
}
|
||||
}
|
||||
@@ -88,7 +107,10 @@ bool parse_key(const std::string &tok, Key *out) {
|
||||
// UARTFILE pushes a file's raw contents, for binary protocol work.
|
||||
if (s == "UART") { *out = Key::Uart; return true; }
|
||||
if (s == "UARTFILE") { *out = Key::UartFile; return true; }
|
||||
if (s == "USBSER") { *out = Key::UsbSer; return true; }
|
||||
if (s == "USBSERFILE") { *out = Key::UsbSerFile; return true; }
|
||||
if (s == "TAP" || s == "T") { *out = Key::Tap; return true; }
|
||||
if (s == "SNAP") { *out = Key::Snap; return true; }
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -175,7 +197,8 @@ bool parse_text(const std::string &text) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ev.key == Key::Uart || ev.key == Key::UartFile) {
|
||||
if (ev.key == Key::Uart || ev.key == Key::UartFile || ev.key == Key::UsbSer ||
|
||||
ev.key == Key::UsbSerFile || ev.key == Key::Snap) {
|
||||
// Everything after the keyword is the payload; keep interior spacing.
|
||||
size_t kpos = raw.find(tok[1]);
|
||||
std::string rest = (kpos == std::string::npos)
|
||||
@@ -186,7 +209,7 @@ bool parse_text(const std::string &text) {
|
||||
while (!rest.empty() && (rest.back() == ' ' || rest.back() == '\t' ||
|
||||
rest.back() == '\r'))
|
||||
rest.pop_back();
|
||||
ev.data = (ev.key == Key::Uart) ? unescape(rest) : rest;
|
||||
ev.data = (ev.key == Key::Uart || ev.key == Key::UsbSer) ? unescape(rest) : rest;
|
||||
ev.until_us = ev.at_us; // instantaneous, nothing to release
|
||||
prev_release_us = ev.at_us;
|
||||
g_events.push_back(ev);
|
||||
@@ -261,9 +284,58 @@ void input_script_tick(EmuState &state) {
|
||||
// Events are ordered by press time; stop at the first future one.
|
||||
if (!ev.pressed && now < ev.at_us) break;
|
||||
|
||||
// SNAP is one-shot too: hand the file name to the display thread.
|
||||
if (ev.key == Key::Snap) {
|
||||
if (now < ev.at_us)
|
||||
break;
|
||||
state.snap_request(ev.data);
|
||||
ev.pressed = ev.released = true;
|
||||
printf("[input-script] SNAP %s @%llu us (%zu/%zu)\n", ev.data.c_str(),
|
||||
(unsigned long long)now, i + 1, g_events.size());
|
||||
fflush(stdout);
|
||||
if (i == g_done) {
|
||||
g_done++;
|
||||
if (g_done == g_events.size())
|
||||
printf("[input-script] sequence complete\n");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// UART events are one-shot: push the bytes into UART_B's receive FIFO
|
||||
// (index 1 = UART_B, the port payloads use for their debug console) and
|
||||
// retire the event immediately.
|
||||
if (ev.key == Key::UsbSer || ev.key == Key::UsbSerFile) {
|
||||
if (now >= ev.at_us && !ev.pressed) {
|
||||
std::string bytes = ev.data;
|
||||
if (ev.key == Key::UsbSerFile) {
|
||||
// A file's raw contents, for sending a file to the payload.
|
||||
bytes.clear();
|
||||
if (FILE *f = fopen(ev.data.c_str(), "rb")) {
|
||||
char buf[4096];
|
||||
size_t n;
|
||||
while ((n = fread(buf, 1, sizeof(buf), f)) > 0)
|
||||
bytes.append(buf, n);
|
||||
fclose(f);
|
||||
} else {
|
||||
fprintf(stderr, "[input-script] cannot open USBSERFILE '%s'\n",
|
||||
ev.data.c_str());
|
||||
}
|
||||
}
|
||||
for (unsigned char ch : bytes)
|
||||
state.usb_serial_rx.push_back(ch);
|
||||
ev.pressed = ev.released = true;
|
||||
printf("[input-script] USB serial <- %zu byte(s) @%llu us (%zu/%zu)\n",
|
||||
bytes.size(), (unsigned long long)now, i + 1, g_events.size());
|
||||
fflush(stdout);
|
||||
if (i == g_done) {
|
||||
g_done++;
|
||||
if (g_done == g_events.size())
|
||||
printf("[input-script] sequence complete\n");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (ev.key == Key::Uart || ev.key == Key::UartFile) {
|
||||
if (now >= ev.at_us && !ev.pressed) {
|
||||
std::string bytes = ev.data;
|
||||
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* BPMP side of booting a Linux kernel on the emulator's CPU0.
|
||||
*
|
||||
* run.sh has the emulator place the kernel Image at 0x80080000 and the
|
||||
* device tree at 0x83000000 (--load), as a bootloader would have. This
|
||||
* payload powers CPU0 the way bdk's ccplex_boot_cpu0() does (Erista rail),
|
||||
* releases it at shim.S - which drops to EL1 and enters the kernel - and
|
||||
* then halts the BPMP for good, so every emulated cycle goes to CPU0.
|
||||
*/
|
||||
|
||||
typedef unsigned int u32;
|
||||
|
||||
#define REG(a) (*(volatile u32 *)(a))
|
||||
|
||||
#define TIMERUS 0x60005010u
|
||||
#define CAR 0x60006000u
|
||||
#define FLOW 0x60007000u
|
||||
#define SB 0x6000C200u
|
||||
#define UARTB_THR 0x70006040u
|
||||
#define PMC 0x7000E400u
|
||||
#define I2C5 0x7000D000u
|
||||
|
||||
#define CPU_ENTRY 0xA0000000u
|
||||
#define KERNEL 0x80080000u
|
||||
|
||||
static void putc_(char c) { REG(UARTB_THR) = (u32)(unsigned char)c; }
|
||||
|
||||
static void puts_(const char *s) {
|
||||
while (*s)
|
||||
putc_(*s++);
|
||||
}
|
||||
|
||||
static void puthex(u32 v) {
|
||||
for (int i = 28; i >= 0; i -= 4)
|
||||
putc_("0123456789ABCDEF"[(v >> i) & 0xF]);
|
||||
}
|
||||
|
||||
static void udelay(u32 us) {
|
||||
u32 start = REG(TIMERUS);
|
||||
while (REG(TIMERUS) - start < us)
|
||||
;
|
||||
}
|
||||
|
||||
/* bdk-style normal-mode I2C write: [reg, val] in CMD_DATA1, 2 bytes, GO. */
|
||||
static void i2c5_write(u32 dev, u32 reg, u32 val) {
|
||||
REG(I2C5 + 0x04) = dev << 1; /* CMD_ADDR0 */
|
||||
REG(I2C5 + 0x0C) = reg | (val << 8); /* CMD_DATA1 */
|
||||
REG(I2C5 + 0x00) = (1u << 1) | (1u << 9); /* CNFG: 2 bytes, write, GO */
|
||||
}
|
||||
|
||||
static void pwrgate_on(u32 part) {
|
||||
if (!(REG(PMC + 0x38) & (1u << part))) /* PWRGATE_STATUS */
|
||||
REG(PMC + 0x30) = part | (1u << 8); /* PWRGATE_TOGGLE */
|
||||
}
|
||||
|
||||
#define RST_CPU0 ((1u << 30) | (1u << 24) | (1u << 16) | (1u << 0))
|
||||
#define RST_NONCPU (1u << 29)
|
||||
|
||||
static void release_cpu0(void) {
|
||||
REG(SB + 0x30) = CPU_ENTRY | 1u; /* SB_AA64_RESET_LOW */
|
||||
REG(SB + 0x34) = 0; /* SB_AA64_RESET_HIGH */
|
||||
REG(CAR + 0x454) = RST_NONCPU; /* RST_CPUG_CMPLX_CLR */
|
||||
REG(CAR + 0x454) = RST_CPU0;
|
||||
}
|
||||
|
||||
static void hold_cpu0(void) {
|
||||
REG(CAR + 0x450) = RST_CPU0; /* RST_CPUG_CMPLX_SET */
|
||||
REG(CAR + 0x450) = RST_NONCPU;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#include "shim_blob.h"
|
||||
|
||||
__attribute__((section(".text.start"), noreturn))
|
||||
void _start(void) {
|
||||
puts_("linux boot: kernel header ");
|
||||
puthex(((volatile u32 *)KERNEL)[14]); /* 'ARM\x64' at offset 0x38 */
|
||||
putc_('\n');
|
||||
for (u32 i = 0; i < sizeof(shim_blob) / sizeof(shim_blob[0]); i++)
|
||||
((volatile u32 *)CPU_ENTRY)[i] = shim_blob[i];
|
||||
|
||||
/* The clocks a bootloader has set before any OS runs: bdk hw_init()'s
|
||||
* _config_oscillators(), as hekate leaves them for switchroot's chain.
|
||||
* The kernel's super-clock code BUG()s on a burst policy still in
|
||||
* STANDBY, which is what these registers hold until someone writes
|
||||
* them. */
|
||||
REG(CAR + 0x55C) = (REG(CAR + 0x55C) & 0xFFFFFFF3) | 4; /* CLK_M divisor 2 */
|
||||
REG(0x700F0000 + 0x20) = 19200000; /* SYSCTR0_CNTFID0 */
|
||||
REG(0x60005014) = 0x45F; /* TIMERUS_USEC_CFG, 19.2 MHz */
|
||||
REG(CAR + 0x050) = 0x50000071; /* OSC_CTRL: 38.4 MHz */
|
||||
REG(CAR + 0x030) = 0x10; /* CLK_SYSTEM_RATE */
|
||||
REG(CAR + 0x400) = 0; /* CLK_SOURCE_SYS */
|
||||
REG(CAR + 0x028) = 0x20004444; /* SCLK_BURST_POLICY: run, PLLP_OUT2 */
|
||||
REG(CAR + 0x02C) = 0x80000000; /* SUPER_SCLK_DIVIDER */
|
||||
REG(CAR + 0x030) = 2; /* HCLK /1, PCLK /3 */
|
||||
REG(CAR + 0x370) = 0x20000000; /* CCLKLP_BURST_POLICY: run, clk_m */
|
||||
|
||||
i2c5_write(0x3C, 0x40, 0x1C); /* MAX77620 AME_GPIO: GPIO5 plain */
|
||||
i2c5_write(0x3C, 0x3B, 0x09); /* GPIO5: push-pull, out high */
|
||||
i2c5_write(0x1B, 0x00, 0x80 | 55); /* MAX77621 VOUT: on, 0.950 V */
|
||||
REG(CAR + 0x0E0) = (1u << 30) | (2u << 20) | (156u << 8) | 2u; /* PLLX */
|
||||
REG(CAR + 0x3B4) = 4u << 1; /* CLK_SOURCE_MSELECT */
|
||||
REG(CAR + 0x440) = 1u << 3; /* CLK_ENB_V_SET: MSELECT */
|
||||
REG(CAR + 0x020) = 0x20008888; /* CCLK_BURST: PLLX_OUT0_LJ */
|
||||
REG(CAR + 0x024) = 1u << 31; /* SUPER_CCLK_DIVIDER */
|
||||
REG(CAR + 0x440) = 1u << 0; /* CLK_ENB_V_SET: CPUG */
|
||||
pwrgate_on(0); /* CRAIL */
|
||||
pwrgate_on(15); /* C0NC */
|
||||
pwrgate_on(14); /* CE0 */
|
||||
REG(FLOW + 0x40) = 1; /* RAM_REPAIR_REQ */
|
||||
while (!(REG(FLOW + 0x40) & 2))
|
||||
;
|
||||
REG(CAR + 0x434) = 1u << 3; /* RST_DEV_V_CLR: MSELECT */
|
||||
release_cpu0();
|
||||
puts_("linux boot: CPU0 released, BPMP halting\n");
|
||||
|
||||
/* bdk's bpmp_halt(): WAITEVENT | JTAG, no wake source. */
|
||||
REG(FLOW + 0x04) = (2u << 29) | (1u << 28);
|
||||
for (;;)
|
||||
;
|
||||
}
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
#!/bin/sh
|
||||
# Builds linux/boot.bin: shim.S (an AArch64 assembler) embedded in the BPMP payload.
|
||||
set -e
|
||||
D=$(dirname "$0")
|
||||
# devkitPro when it is installed, else the distribution's cross compilers
|
||||
# (gcc-aarch64-linux-gnu, gcc-arm-none-eabi), as on CI.
|
||||
DKP=${DEVKITPRO:-/opt/devkitpro}
|
||||
if [ -z "$A64" ]; then
|
||||
A64=aarch64-linux-gnu-
|
||||
if [ -x "$DKP/devkitA64/bin/aarch64-none-elf-gcc" ]; then A64=$DKP/devkitA64/bin/aarch64-none-elf-; fi
|
||||
fi
|
||||
if [ -z "$ARM" ]; then
|
||||
ARM=arm-none-eabi-
|
||||
if [ -x "$DKP/devkitARM/bin/arm-none-eabi-gcc" ]; then ARM=$DKP/devkitARM/bin/arm-none-eabi-; fi
|
||||
fi
|
||||
${A64}gcc -c -o "$D/shim.o" "$D/shim.S"
|
||||
${A64}ld -Ttext=0xA0000000 -o "$D/shim.elf" "$D/shim.o"
|
||||
${A64}objcopy -O binary "$D/shim.elf" "$D/shim.bin"
|
||||
python3 - "$D/shim.bin" "$D/shim_blob.h" <<'PY'
|
||||
import sys, struct
|
||||
b = open(sys.argv[1], "rb").read()
|
||||
b += b"\0" * (-len(b) % 4)
|
||||
w = struct.unpack("<%dI" % (len(b) // 4), b)
|
||||
with open(sys.argv[2], "w") as f:
|
||||
f.write("/* Generated by build.sh from shim.S. */\nstatic const u32 shim_blob[] = {\n")
|
||||
for i in range(0, len(w), 6):
|
||||
f.write(" " + ", ".join("0x%08x" % x for x in w[i:i + 6]) + ",\n")
|
||||
f.write("};\n")
|
||||
PY
|
||||
${ARM}gcc -march=armv4t -marm -Os -ffreestanding -nostdlib -fno-builtin -I"$D" \
|
||||
-T "$D/link.ld" -o "$D/boot.elf" "$D/boot.c"
|
||||
${ARM}objcopy -O binary "$D/boot.elf" "$D/boot.bin"
|
||||
echo "built $D/boot.bin"
|
||||
@@ -0,0 +1,10 @@
|
||||
/* Loaded and entered at 0x40010000, like any RCM payload. */
|
||||
ENTRY(_start)
|
||||
SECTIONS {
|
||||
. = 0x40010000;
|
||||
.text : { KEEP(*(.text.start)) *(.text*) }
|
||||
.rodata : { *(.rodata*) }
|
||||
.data : { *(.data*) }
|
||||
.bss : { *(.bss*) *(COMMON) }
|
||||
/DISCARD/ : { *(.comment) *(.note*) *(.ARM.attributes) *(.ARM.exidx*) }
|
||||
}
|
||||
Executable
+23
@@ -0,0 +1,23 @@
|
||||
#!/bin/sh
|
||||
# linux/mkdtb.sh IN.dtb OUT.dtb - the switchroot device tree with what the
|
||||
# emulator boot needs: a console on UART-B (the port the emulator prints),
|
||||
# kept past init, one CPU, verbose logging. The bootloader normally edits
|
||||
# bootargs the same way; nothing else changes.
|
||||
set -e
|
||||
DTC=${DTC:-$HOME/l4t/kernel/kernel-4.9/scripts/dtc/dtc}
|
||||
IN=$1; OUT=$2
|
||||
T=$(mktemp -d)
|
||||
"$DTC" -I dtb -O dts -q -o "$T/in.dts" "$IN"
|
||||
ARGS=${BOOTARGS:-"earlycon=uart8250,mmio32,0x70006040 keep_bootcon console=tty0 maxcpus=1 nosmp loglevel=8 ignore_loglevel clk_ignore_unused printk.time=1"}
|
||||
python3 - "$T/in.dts" "$T/out.dts" "$ARGS" <<'PY'
|
||||
import re, sys
|
||||
s = open(sys.argv[1]).read()
|
||||
args = sys.argv[3]
|
||||
s, n = re.subn(r'(\n\tchosen \{[^}]*?bootargs = )"[^"]*";', lambda m: m.group(1) + '"' + args + '";', s, count=1)
|
||||
if n != 1:
|
||||
raise SystemExit("no /chosen bootargs found")
|
||||
open(sys.argv[2], "w").write(s)
|
||||
PY
|
||||
"$DTC" -I dts -O dtb -q -o "$OUT" "$T/out.dts"
|
||||
rm -rf "$T"
|
||||
echo "built $OUT"
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
#!/bin/sh
|
||||
# linux/run.sh IMAGE DTB [LOG] [SECS] [extra rcm_emu args...]
|
||||
#
|
||||
# Boots a Linux kernel (an arm64 Image, e.g. switchroot's L4T 4.9) on the
|
||||
# emulator's CPU0: the emulator places the Image at 0x80080000 and the
|
||||
# device tree at 0x83000000, linux/boot.bin powers CPU0 and hands over at
|
||||
# EL1 through linux/shim.S. Run linux/mkdtb.sh on the device tree first so
|
||||
# it carries an earlycon on UART-B, which the emulator prints.
|
||||
#
|
||||
# Needs an emulator built against Unicorn 2.1 or later (make UNICORN_DIR=...).
|
||||
set -e
|
||||
D=$(cd "$(dirname "$0")" && pwd)
|
||||
E=$(cd "$D/.." && pwd)
|
||||
IMAGE=$1; DTB=$2; LOG=${3:-$D/boot.log}; SECS=${4:-600}
|
||||
shift 4 2>/dev/null || shift $#
|
||||
export PATH=/opt/devkitpro/devkitARM/bin:/opt/devkitpro/devkitA64/bin:$PATH
|
||||
[ -f "$D/boot.bin" ] || sh "$D/build.sh"
|
||||
cd "$E"
|
||||
SDL_VIDEODRIVER=${SDL_VIDEODRIVER:-dummy} RCM_EMU_NO_AUDIO=1 timeout "$SECS" \
|
||||
./rcm_emu "$D/boot.bin" --oem erista \
|
||||
--load "$IMAGE@0x80080000" --load "$DTB@0x83000000" "$@" > "$LOG" 2>&1 || true
|
||||
echo "log: $LOG ($(grep -a -c '\[uartB\]' "$LOG") kernel lines)"
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* The hand-over a kernel expects from its bootloader, as switchroot's chain
|
||||
* (hekate, ATF, U-Boot) leaves it: AArch64 EL1h, non-secure, MMU and caches
|
||||
* off, x0 = the device tree, x1..x3 = 0 (Documentation/arm64/booting.txt).
|
||||
*
|
||||
* CPU0 enters here at EL3. EL2 is configured so EL1 runs AArch64 with the
|
||||
* timers and FP/SIMD untrapped, then one ERET lands in the kernel at EL1.
|
||||
* PSCI calls the kernel makes over SMC are answered by the emulator, which
|
||||
* plays the secure monitor.
|
||||
*/
|
||||
.equ KERNEL, 0x80080000
|
||||
.equ DTB, 0x83000000
|
||||
|
||||
.text
|
||||
.global _start
|
||||
_start:
|
||||
mov x0, #0x431 /* SCR_EL3: RW, RES1, NS */
|
||||
msr scr_el3, x0
|
||||
msr cptr_el3, xzr /* FP/SIMD not trapped to EL3 */
|
||||
mov x0, #0x33FF
|
||||
msr cptr_el2, x0 /* RES1 bits, nothing trapped to EL2 */
|
||||
mov x0, #(1 << 31) /* HCR_EL2.RW: EL1 is AArch64 */
|
||||
msr hcr_el2, x0
|
||||
mov x0, #3 /* EL1 may use the physical timer */
|
||||
msr cnthctl_el2, x0
|
||||
msr cntvoff_el2, xzr
|
||||
ldr x0, =0x30C50830 /* SCTLR_EL1: RES1, MMU and caches off */
|
||||
msr sctlr_el1, x0
|
||||
mov x0, #0x3C5 /* EL1h, DAIF masked */
|
||||
msr spsr_el3, x0
|
||||
ldr x0, =KERNEL
|
||||
msr elr_el3, x0
|
||||
ldr x0, =DTB
|
||||
mov x1, xzr
|
||||
mov x2, xzr
|
||||
mov x3, xzr
|
||||
eret
|
||||
.ltorg
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "trace.h"
|
||||
#include "t210/bpmp.h"
|
||||
#include "t210/ccplex.h"
|
||||
#include "t210/dock.h"
|
||||
#include "t210/memory_map.h"
|
||||
#include "t210/mmio.h"
|
||||
#include "display/sdl_display.h"
|
||||
@@ -340,6 +341,29 @@ int main(int argc, char *argv[]) {
|
||||
rawnand_prefix = argv[++i];
|
||||
} else if (strcmp(argv[i], "--prod-keys") == 0 && i + 1 < argc) {
|
||||
prod_keys_path = argv[++i];
|
||||
} else if (strcmp(argv[i], "--dock") == 0 && i + 1 < argc) {
|
||||
// What is on the USB-C port. Same contract as --oem: overrides
|
||||
// the ini. "nintendo", "thirdparty" (Ugreen-style), "charger",
|
||||
// "none"; append ":notv" for a dock with nothing on its HDMI,
|
||||
// ":clone" for one that answers the reads HOS docks with but
|
||||
// not the LED or hub messages, ":mute" for one that answers
|
||||
// none of the Nintendo dock's own messages, ":latehpd" for a
|
||||
// TV whose HPD comes 25 s after DisplayPort is configured.
|
||||
const char *d = argv[++i];
|
||||
uint8_t k = 0xFF;
|
||||
if (!strncmp(d, "nintendo", 8)) k = 1;
|
||||
else if (!strncmp(d, "thirdparty", 10) || !strncmp(d, "ugreen", 6)) k = 2;
|
||||
else if (!strncmp(d, "charger", 7)) k = 3;
|
||||
else if (!strncmp(d, "none", 4)) k = 0;
|
||||
if (k == 0xFF) {
|
||||
fprintf(stderr, "[emu] Unknown --dock '%s'; expected nintendo, thirdparty, charger or none\n", d);
|
||||
return 1;
|
||||
}
|
||||
state.dock_kind = k;
|
||||
state.dock_tv = strstr(d, ":notv") == nullptr;
|
||||
state.dock_clone = strstr(d, ":mute") ? 2 : strstr(d, ":clone") ? 1 : 0;
|
||||
state.dock_late_hpd = strstr(d, ":latehpd") != nullptr;
|
||||
printf("[emu] Dock: %s%s [overrides ini]\n", d, "");
|
||||
} else if (strcmp(argv[i], "--oem") == 0 && i + 1 < argc) {
|
||||
const char *oem = argv[++i];
|
||||
if (strcmp(oem, "mariko") == 0 || strcmp(oem, "t210b01") == 0) {
|
||||
@@ -510,6 +534,40 @@ int main(int argc, char *argv[]) {
|
||||
state.payload_len = payload_size;
|
||||
if (!uc) { free(payload); return 1; }
|
||||
|
||||
// --load FILE@ADDR (repeatable): place a file in DRAM before the payload
|
||||
// starts, the way a bootloader would have left a kernel and its device
|
||||
// tree there. The payload decides what to do with it.
|
||||
for (int i = 2; i + 1 < argc; i++) {
|
||||
if (strcmp(argv[i], "--load") != 0)
|
||||
continue;
|
||||
const char *spec = argv[++i];
|
||||
const char *at = strrchr(spec, '@');
|
||||
if (!at) {
|
||||
fprintf(stderr, "[error] --load expects FILE@ADDR, got '%s'\n", spec);
|
||||
return 1;
|
||||
}
|
||||
std::string path(spec, at - spec);
|
||||
uint64_t addr = strtoull(at + 1, nullptr, 0);
|
||||
FILE *f = fopen(path.c_str(), "rb");
|
||||
if (!f) {
|
||||
fprintf(stderr, "[error] --load cannot open '%s'\n", path.c_str());
|
||||
return 1;
|
||||
}
|
||||
fseek(f, 0, SEEK_END);
|
||||
long len = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
if (len < 0 || addr < DRAM_BASE || addr + (uint64_t)len > DRAM_BASE + DRAM_WINDOW_SIZE) {
|
||||
fprintf(stderr, "[error] --load '%s' (%ld bytes) does not fit in DRAM at 0x%llX\n",
|
||||
path.c_str(), len, (unsigned long long)addr);
|
||||
fclose(f);
|
||||
return 1;
|
||||
}
|
||||
size_t got = fread(state.dram_low_ptr + (addr - DRAM_BASE), 1, (size_t)len, f);
|
||||
fclose(f);
|
||||
printf("[loader] --load %s: %zu bytes at 0x%08llX\n", path.c_str(), got,
|
||||
(unsigned long long)addr);
|
||||
}
|
||||
|
||||
// Initialize SDL2 display
|
||||
if (!sdl_display_init()) {
|
||||
fprintf(stderr, "[error] Failed to initialize display\n");
|
||||
@@ -719,6 +777,18 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
// Scripted button input (--input-script).
|
||||
input_script_tick(state);
|
||||
// Keyboard VOL presses end after their fixed guest duration.
|
||||
for (int k = 0; k < 2; k++) {
|
||||
std::atomic<uint64_t> &until = k ? state.kbd_vol_down_until : state.kbd_vol_up_until;
|
||||
std::atomic<bool> &btn = k ? state.btn_vol_down : state.btn_vol_up;
|
||||
uint64_t u = until.load();
|
||||
if (u && state.emu_usec >= u) {
|
||||
until = 0;
|
||||
btn = false;
|
||||
printf("[input] UP %s @emu_usec=%llu\n", k ? "VOL_DOWN" : "VOL_UP ",
|
||||
(unsigned long long)state.emu_usec);
|
||||
}
|
||||
}
|
||||
|
||||
// Touch injection for the Nyx GUI popup (eMMC Issues Warning).
|
||||
// Nyx initializes after the IPL stage; we inject a tap once enough
|
||||
@@ -756,6 +826,8 @@ int main(int argc, char *argv[]) {
|
||||
state.emu_usec += CPU0_QUANTUM_INSTRUCTIONS / 10;
|
||||
state.bpmp_slept_us += CPU0_QUANTUM_INSTRUCTIONS / 10;
|
||||
ccplex_run(&state, state.emu_usec);
|
||||
dock_tick(&state);
|
||||
display_tick(&state);
|
||||
if (state.reboot_requested.load())
|
||||
break;
|
||||
continue;
|
||||
@@ -779,6 +851,8 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
// CPU0 catches up to the BPMP's clock.
|
||||
ccplex_run(&state, state.emu_usec);
|
||||
dock_tick(&state);
|
||||
display_tick(&state);
|
||||
if (state.reboot_requested.load())
|
||||
break;
|
||||
}
|
||||
|
||||
+527
@@ -0,0 +1,527 @@
|
||||
// Fake Bluetooth controllers for the CYW4356 model. See btpeer.h.
|
||||
#include "btpeer.h"
|
||||
#include "../emu_state.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int UART_D = 3;
|
||||
|
||||
struct Peer {
|
||||
const char *name;
|
||||
uint8_t addr[6]; // little-endian, as HCI carries it
|
||||
uint8_t type; // the controller's own: 1 left Joy-Con, 2 right, 3 Pro
|
||||
bool needs_security; // answers "security block" until encrypted
|
||||
bool needs_bonding; // refuses Simple Pairing without bonding
|
||||
int8_t rssi;
|
||||
};
|
||||
|
||||
// Nintendo OUIs (98:B6:E9 and DC:68:EB), last three bytes made up.
|
||||
const Peer kPeers[] = {
|
||||
{"Pro Controller", {0x21, 0x43, 0x65, 0xE9, 0xB6, 0x98}, 3, true, true, -47},
|
||||
{"Joy-Con (R)", {0x9A, 0xBC, 0xDE, 0xEB, 0x68, 0xDC}, 2, false, false, -61},
|
||||
};
|
||||
constexpr int kNPeers = sizeof(kPeers) / sizeof(kPeers[0]);
|
||||
const uint8_t kRadioAddr[6] = {0x10, 0x32, 0x54, 0xEB, 0x68, 0xDC};
|
||||
|
||||
// Bits of the 24-bit button field each controller cycles through.
|
||||
const uint8_t kBitsPro[] = {3, 2, 1, 0, 17, 16, 19, 18, 22, 6, 23, 7, 8, 9, 12, 13, 11, 10};
|
||||
const uint8_t kBitsR[] = {3, 2, 1, 0, 6, 7, 9, 12, 10, 5, 4};
|
||||
|
||||
constexpr uint16_t kHandle = 0x0040;
|
||||
constexpr uint64_t kReportUs = 15000; // a report every 15 ms
|
||||
constexpr uint64_t kPressUs = 600000; // each button: 300 ms down, 300 up
|
||||
constexpr size_t kHostBacklog = 600; // the radio holds reports past this
|
||||
|
||||
struct Pending {
|
||||
uint64_t due;
|
||||
int tag; // 1: inquiry traffic, cancelled with it
|
||||
std::vector<uint8_t> bytes;
|
||||
};
|
||||
|
||||
struct Chan {
|
||||
uint16_t host_cid = 0;
|
||||
uint16_t peer_cid = 0;
|
||||
bool open = false, host_cfg = false, peer_cfg = false;
|
||||
};
|
||||
|
||||
struct State {
|
||||
std::vector<Pending> q;
|
||||
int conn = -1; // index into kPeers
|
||||
bool encrypted = false;
|
||||
bool info_asked = false;
|
||||
Chan ch[2]; // HID control 0x11, interrupt 0x13
|
||||
bool streaming = false, full = false;
|
||||
uint64_t stream_at = 0, next_report = 0;
|
||||
uint8_t timer = 0;
|
||||
uint8_t sig_id = 0x40;
|
||||
uint8_t last_auth_req = 0;
|
||||
} st;
|
||||
|
||||
uint64_t now_us(EmuState *s) { return s->emu_usec; }
|
||||
|
||||
void later(EmuState *s, uint64_t us, std::vector<uint8_t> b, int tag = 0) {
|
||||
st.q.push_back({now_us(s) + us, tag, std::move(b)});
|
||||
}
|
||||
|
||||
std::vector<uint8_t> event(uint8_t code, std::vector<uint8_t> p) {
|
||||
std::vector<uint8_t> b = {0x04, code, (uint8_t)p.size()};
|
||||
b.insert(b.end(), p.begin(), p.end());
|
||||
return b;
|
||||
}
|
||||
|
||||
void put_addr(std::vector<uint8_t> &v, const uint8_t *a) { v.insert(v.end(), a, a + 6); }
|
||||
|
||||
std::vector<uint8_t> cmd_complete(uint16_t op, std::vector<uint8_t> ret) {
|
||||
std::vector<uint8_t> p = {0x01, (uint8_t)op, (uint8_t)(op >> 8)};
|
||||
p.insert(p.end(), ret.begin(), ret.end());
|
||||
return event(0x0E, p);
|
||||
}
|
||||
|
||||
std::vector<uint8_t> cmd_status(uint16_t op, uint8_t status) {
|
||||
return event(0x0F, {status, 0x01, (uint8_t)op, (uint8_t)(op >> 8)});
|
||||
}
|
||||
|
||||
int peer_of(const uint8_t *addr) {
|
||||
for (int i = 0; i < kNPeers; i++)
|
||||
if (!memcmp(kPeers[i].addr, addr, 6))
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// An L2CAP frame to the host, in one ACL packet.
|
||||
std::vector<uint8_t> acl(uint16_t cid, const std::vector<uint8_t> &d) {
|
||||
uint16_t hf = kHandle | 0x2000;
|
||||
uint16_t l2 = (uint16_t)d.size(), len = l2 + 4;
|
||||
std::vector<uint8_t> b = {0x02, (uint8_t)hf, (uint8_t)(hf >> 8), (uint8_t)len,
|
||||
(uint8_t)(len >> 8), (uint8_t)l2, (uint8_t)(l2 >> 8),
|
||||
(uint8_t)cid, (uint8_t)(cid >> 8)};
|
||||
b.insert(b.end(), d.begin(), d.end());
|
||||
return b;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> sig(uint8_t code, uint8_t id, std::vector<uint8_t> d) {
|
||||
std::vector<uint8_t> b = {code, id, (uint8_t)d.size(), (uint8_t)(d.size() >> 8)};
|
||||
b.insert(b.end(), d.begin(), d.end());
|
||||
return acl(0x0001, b);
|
||||
}
|
||||
|
||||
void drop_link() {
|
||||
st.conn = -1;
|
||||
st.encrypted = st.info_asked = false;
|
||||
st.ch[0] = Chan{};
|
||||
st.ch[1] = Chan{};
|
||||
st.streaming = st.full = false;
|
||||
}
|
||||
|
||||
// The buttons and sticks at time t into the stream.
|
||||
void input_now(uint64_t t, uint8_t *btn3, uint8_t *sticks6) {
|
||||
const Peer &p = kPeers[st.conn];
|
||||
const uint8_t *bits = p.type == 3 ? kBitsPro : kBitsR;
|
||||
size_t nbits = p.type == 3 ? sizeof(kBitsPro) : sizeof(kBitsR);
|
||||
uint32_t b = 0;
|
||||
uint64_t step = t / kPressUs;
|
||||
if (t % kPressUs < kPressUs / 2)
|
||||
b = 1u << bits[step % nbits];
|
||||
btn3[0] = (uint8_t)b;
|
||||
btn3[1] = (uint8_t)(b >> 8);
|
||||
btn3[2] = (uint8_t)(b >> 16);
|
||||
double a = (double)(t % 2000000) / 2000000.0 * 6.2831853;
|
||||
auto pack = [](uint8_t *o, double ang, bool on) {
|
||||
uint16_t x = on ? (uint16_t)(0x800 + 0x500 * cos(ang)) : 0x800;
|
||||
uint16_t y = on ? (uint16_t)(0x800 + 0x500 * sin(ang)) : 0x800;
|
||||
o[0] = (uint8_t)x;
|
||||
o[1] = (uint8_t)((x >> 8) | (y << 4));
|
||||
o[2] = (uint8_t)(y >> 4);
|
||||
};
|
||||
pack(sticks6, a, p.type != 2); // left stick: Pro and left Joy-Con
|
||||
pack(sticks6 + 3, -a, p.type != 1); // right stick: Pro and right Joy-Con
|
||||
}
|
||||
|
||||
// Input report 0x21 (a subcommand's answer) or 0x30, on the input channel.
|
||||
std::vector<uint8_t> report(uint8_t id, uint64_t t, const std::vector<uint8_t> &reply) {
|
||||
std::vector<uint8_t> r(1 + 49, 0);
|
||||
r[0] = 0xA1; // HID DATA, input
|
||||
r[1] = id;
|
||||
r[2] = st.timer++;
|
||||
r[3] = 0x8E; // battery full, powered by battery
|
||||
input_now(t, &r[4], &r[7]);
|
||||
r[13] = 0x00; // vibration
|
||||
for (size_t i = 0; i < reply.size() && 14 + i < r.size(); i++)
|
||||
r[14 + i] = reply[i];
|
||||
return acl(st.ch[1].host_cid, r);
|
||||
}
|
||||
|
||||
void hid_output(EmuState *s, const uint8_t *d, uint32_t n) {
|
||||
if (n < 12 || d[0] != 0xA2 || d[1] != 0x01)
|
||||
return;
|
||||
uint8_t sub = d[11];
|
||||
uint64_t t = st.streaming ? now_us(s) - st.stream_at : 0;
|
||||
std::vector<uint8_t> reply;
|
||||
switch (sub) {
|
||||
case 0x02: { // device info
|
||||
const Peer &p = kPeers[st.conn];
|
||||
reply = {0x82, 0x02, 0x04, 0x21, p.type, 0x02};
|
||||
for (int i = 5; i >= 0; i--)
|
||||
reply.push_back(p.addr[i]);
|
||||
reply.push_back(0x01);
|
||||
reply.push_back(0x01);
|
||||
break;
|
||||
}
|
||||
case 0x03:
|
||||
if (n > 12 && d[12] == 0x30) {
|
||||
st.full = true;
|
||||
printf("[btpeer] %s: full reports from now on\n", kPeers[st.conn].name);
|
||||
}
|
||||
reply = {0x80, 0x03};
|
||||
break;
|
||||
default:
|
||||
reply = {0x80, sub};
|
||||
break;
|
||||
}
|
||||
later(s, 8000, report(0x21, t, reply));
|
||||
}
|
||||
|
||||
void signal_in(EmuState *s, const uint8_t *d, uint32_t n) {
|
||||
while (n >= 4) {
|
||||
uint8_t code = d[0], id = d[1];
|
||||
uint32_t len = d[2] | d[3] << 8;
|
||||
const uint8_t *p = d + 4;
|
||||
if (len > n - 4)
|
||||
return;
|
||||
switch (code) {
|
||||
case 0x02: { // Connection Request
|
||||
uint16_t psm = p[0] | p[1] << 8, scid = p[2] | p[3] << 8;
|
||||
int i = psm == 0x11 ? 0 : psm == 0x13 ? 1 : -1;
|
||||
if (i < 0) {
|
||||
later(s, 2000, sig(0x03, id, {0, 0, (uint8_t)scid, (uint8_t)(scid >> 8), 2, 0, 0, 0}));
|
||||
break;
|
||||
}
|
||||
if (kPeers[st.conn].needs_security && !st.encrypted) {
|
||||
printf("[btpeer] %s: PSM %02X before encryption, security block\n",
|
||||
kPeers[st.conn].name, psm);
|
||||
later(s, 2000, sig(0x03, id, {0, 0, (uint8_t)scid, (uint8_t)(scid >> 8), 3, 0, 0, 0}));
|
||||
break;
|
||||
}
|
||||
Chan &c = st.ch[i];
|
||||
c = Chan{};
|
||||
c.host_cid = scid;
|
||||
c.peer_cid = 0x0060 + i;
|
||||
c.open = true;
|
||||
later(s, 2000, sig(0x03, id, {(uint8_t)c.peer_cid, (uint8_t)(c.peer_cid >> 8),
|
||||
(uint8_t)scid, (uint8_t)(scid >> 8), 0, 0, 0, 0}));
|
||||
// Its own settings, MTU 672, as a controller sends them.
|
||||
later(s, 4000, sig(0x04, st.sig_id++, {(uint8_t)scid, (uint8_t)(scid >> 8), 0, 0,
|
||||
0x01, 0x02, 0xA0, 0x02}));
|
||||
if (!st.info_asked) {
|
||||
st.info_asked = true;
|
||||
later(s, 3000, sig(0x0A, st.sig_id++, {0x02, 0x00}));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0x04: { // the host's Configuration Request
|
||||
uint16_t dcid = p[0] | p[1] << 8;
|
||||
for (Chan &c : st.ch)
|
||||
if (c.open && c.peer_cid == dcid) {
|
||||
c.host_cfg = true;
|
||||
later(s, 2000, sig(0x05, id, {(uint8_t)c.host_cid, (uint8_t)(c.host_cid >> 8),
|
||||
0, 0, 0, 0}));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0x05: { // the host's answer to ours
|
||||
uint16_t scid = p[0] | p[1] << 8, res = p[4] | p[5] << 8;
|
||||
for (Chan &c : st.ch)
|
||||
if (c.open && c.peer_cid == scid && res == 0)
|
||||
c.peer_cfg = true;
|
||||
break;
|
||||
}
|
||||
case 0x0B: // Information Response
|
||||
printf("[btpeer] the host answered the information request (result %d)\n",
|
||||
len >= 4 ? p[2] | p[3] << 8 : -1);
|
||||
break;
|
||||
case 0x06: { // Disconnection Request
|
||||
later(s, 2000, sig(0x07, id, {p[0], p[1], p[2], p[3]}));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
d += 4 + len;
|
||||
n -= 4 + len;
|
||||
}
|
||||
Chan &intr = st.ch[1];
|
||||
if (!st.streaming && st.ch[0].host_cfg && st.ch[0].peer_cfg && intr.host_cfg &&
|
||||
intr.peer_cfg) {
|
||||
st.streaming = true;
|
||||
st.stream_at = now_us(s);
|
||||
st.next_report = now_us(s) + kReportUs;
|
||||
printf("[btpeer] %s: both HID channels open\n", kPeers[st.conn].name);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void btpeer_reset(EmuState *state) {
|
||||
(void)state;
|
||||
st.q.clear();
|
||||
drop_link();
|
||||
}
|
||||
|
||||
bool btpeer_command(EmuState *s, uint16_t op, const uint8_t *p, uint32_t n) {
|
||||
switch (op) {
|
||||
case 0x1009: { // Read_BD_ADDR
|
||||
std::vector<uint8_t> r = {0x00};
|
||||
put_addr(r, kRadioAddr);
|
||||
later(s, 500, cmd_complete(op, r));
|
||||
return true;
|
||||
}
|
||||
case 0x1003: // Read_Local_Supported_Features
|
||||
// Byte 6 bit 3: Secure Simple Pairing, as the CYW4356 has it.
|
||||
later(s, 500, cmd_complete(op, {0x00, 0xBF, 0xFE, 0x8F, 0xFE, 0xDB, 0xFF, 0x7B, 0x87}));
|
||||
return true;
|
||||
case 0x040D: { // PIN_Code_Request_Reply
|
||||
std::vector<uint8_t> r = {0x00};
|
||||
put_addr(r, p);
|
||||
later(s, 500, cmd_complete(op, r));
|
||||
return true;
|
||||
}
|
||||
case 0x1005: // Read_Buffer_Size: 1021 x 8 ACL
|
||||
later(s, 500, cmd_complete(op, {0x00, 0xFD, 0x03, 0x40, 0x08, 0x00, 0x08, 0x00}));
|
||||
return true;
|
||||
case 0x0C01: case 0x0C56: case 0x0C45: case 0x0C13: case 0x0C24: case 0x0C1A:
|
||||
case 0x080F: // Write_Default_Link_Policy_Settings
|
||||
later(s, 500, cmd_complete(op, {0x00}));
|
||||
return true;
|
||||
|
||||
case 0x0401: { // Inquiry
|
||||
later(s, 500, cmd_status(op, 0x00));
|
||||
uint64_t len_us = (n >= 4 ? p[3] : 8) * 1280000ull;
|
||||
for (int i = 0; i < kNPeers; i++) {
|
||||
const Peer &pr = kPeers[i];
|
||||
std::vector<uint8_t> e = {0x01};
|
||||
put_addr(e, pr.addr);
|
||||
e.insert(e.end(), {0x01, 0x00, 0x08, 0x25, 0x00, 0x34, 0x12, (uint8_t)pr.rssi});
|
||||
later(s, 1500000ull + i * 1200000ull, event(0x22, e), 1);
|
||||
}
|
||||
later(s, len_us, event(0x01, {0x00}), 1);
|
||||
printf("[btpeer] inquiry started\n");
|
||||
return true;
|
||||
}
|
||||
case 0x0402: { // Inquiry_Cancel
|
||||
std::vector<Pending> keep;
|
||||
for (Pending &q : st.q)
|
||||
if (q.tag != 1)
|
||||
keep.push_back(std::move(q));
|
||||
st.q.swap(keep);
|
||||
later(s, 500, cmd_complete(op, {0x00}));
|
||||
printf("[btpeer] inquiry cancelled\n");
|
||||
return true;
|
||||
}
|
||||
case 0x0419: { // Remote_Name_Request
|
||||
later(s, 500, cmd_status(op, 0x00));
|
||||
int i = n >= 6 ? peer_of(p) : -1;
|
||||
std::vector<uint8_t> e = {(uint8_t)(i < 0 ? 0x04 : 0x00)};
|
||||
put_addr(e, p);
|
||||
std::vector<uint8_t> name(248, 0);
|
||||
if (i >= 0)
|
||||
memcpy(name.data(), kPeers[i].name, strlen(kPeers[i].name));
|
||||
e.insert(e.end(), name.begin(), name.end());
|
||||
later(s, i < 0 ? 5120000 : 300000, event(0x07, e));
|
||||
return true;
|
||||
}
|
||||
case 0x0405: { // Create_Connection
|
||||
later(s, 500, cmd_status(op, 0x00));
|
||||
int i = n >= 6 ? peer_of(p) : -1;
|
||||
std::vector<uint8_t> e = {(uint8_t)(i < 0 ? 0x04 : 0x00), (uint8_t)kHandle,
|
||||
(uint8_t)(kHandle >> 8)};
|
||||
put_addr(e, p);
|
||||
e.push_back(0x01);
|
||||
e.push_back(0x00);
|
||||
if (i >= 0) {
|
||||
drop_link();
|
||||
st.conn = i;
|
||||
printf("[btpeer] %s: connected\n", kPeers[i].name);
|
||||
}
|
||||
later(s, i < 0 ? 5120000 : 600000, event(0x03, e));
|
||||
return true;
|
||||
}
|
||||
case 0x0408: { // Create_Connection_Cancel
|
||||
std::vector<uint8_t> r = {0x00};
|
||||
put_addr(r, p);
|
||||
later(s, 500, cmd_complete(op, r));
|
||||
return true;
|
||||
}
|
||||
case 0x0406: { // Disconnect
|
||||
later(s, 500, cmd_status(op, 0x00));
|
||||
later(s, 50000, event(0x05, {0x00, (uint8_t)kHandle, (uint8_t)(kHandle >> 8), 0x16}));
|
||||
if (st.conn >= 0)
|
||||
printf("[btpeer] %s: the host disconnected\n", kPeers[st.conn].name);
|
||||
drop_link();
|
||||
return true;
|
||||
}
|
||||
case 0x0411: // Authentication_Requested
|
||||
later(s, 500, cmd_status(op, 0x00));
|
||||
if (st.conn >= 0) {
|
||||
std::vector<uint8_t> e;
|
||||
put_addr(e, kPeers[st.conn].addr);
|
||||
later(s, 20000, event(0x17, e)); // Link Key Request
|
||||
}
|
||||
return true;
|
||||
case 0x040C: case 0x040B: { // Link key: none, or one
|
||||
std::vector<uint8_t> r = {0x00};
|
||||
put_addr(r, p);
|
||||
later(s, 500, cmd_complete(op, r));
|
||||
if (st.conn < 0)
|
||||
return true;
|
||||
std::vector<uint8_t> e;
|
||||
put_addr(e, kPeers[st.conn].addr);
|
||||
if (op == 0x040B)
|
||||
later(s, 30000, event(0x06, {0x00, (uint8_t)kHandle, (uint8_t)(kHandle >> 8)}));
|
||||
else
|
||||
later(s, 20000, event(0x31, e)); // IO Capability Request
|
||||
return true;
|
||||
}
|
||||
case 0x042B: { // IO_Capability_Request_Reply
|
||||
std::vector<uint8_t> r = {0x00};
|
||||
put_addr(r, p);
|
||||
later(s, 500, cmd_complete(op, r));
|
||||
if (st.conn < 0)
|
||||
return true;
|
||||
const Peer &pr = kPeers[st.conn];
|
||||
st.last_auth_req = n >= 9 ? p[8] : 0;
|
||||
std::vector<uint8_t> e;
|
||||
put_addr(e, pr.addr);
|
||||
std::vector<uint8_t> resp = e;
|
||||
resp.insert(resp.end(), {0x03, 0x00, (uint8_t)(pr.needs_bonding ? 0x04 : 0x00)});
|
||||
later(s, 10000, event(0x32, resp)); // IO Capability Response
|
||||
if (pr.needs_bonding && !(st.last_auth_req & 0x06)) {
|
||||
printf("[btpeer] %s: refuses pairing without bonding\n", pr.name);
|
||||
std::vector<uint8_t> spc = {0x05};
|
||||
put_addr(spc, pr.addr);
|
||||
later(s, 30000, event(0x36, spc));
|
||||
later(s, 35000, event(0x06, {0x05, (uint8_t)kHandle, (uint8_t)(kHandle >> 8)}));
|
||||
return true;
|
||||
}
|
||||
std::vector<uint8_t> conf = e;
|
||||
conf.insert(conf.end(), {0x40, 0xE2, 0x01, 0x00});
|
||||
later(s, 20000, event(0x33, conf)); // User Confirmation Request
|
||||
return true;
|
||||
}
|
||||
case 0x042C: { // User_Confirmation_Request_Reply
|
||||
std::vector<uint8_t> r = {0x00};
|
||||
put_addr(r, p);
|
||||
later(s, 500, cmd_complete(op, r));
|
||||
if (st.conn < 0)
|
||||
return true;
|
||||
const Peer &pr = kPeers[st.conn];
|
||||
std::vector<uint8_t> spc = {0x00};
|
||||
put_addr(spc, pr.addr);
|
||||
// The Pro Controller fails its first pairing of a run, as a real one
|
||||
// can, so a host's second try is exercised too.
|
||||
static int pro_pairings;
|
||||
if (pr.type == 3 && pro_pairings++ == 0) {
|
||||
printf("[btpeer] %s: first pairing fails (authentication failure)\n", pr.name);
|
||||
spc[0] = 0x05;
|
||||
later(s, 30000, event(0x36, spc));
|
||||
later(s, 35000, event(0x06, {0x05, (uint8_t)kHandle, (uint8_t)(kHandle >> 8)}));
|
||||
return true;
|
||||
}
|
||||
later(s, 30000, event(0x36, spc)); // Simple Pairing Complete
|
||||
std::vector<uint8_t> lk;
|
||||
put_addr(lk, pr.addr);
|
||||
for (int i = 0; i < 16; i++)
|
||||
lk.push_back((uint8_t)(0xA0 + i));
|
||||
lk.push_back(0x04); // unauthenticated combination key
|
||||
if (st.last_auth_req & 0x06)
|
||||
later(s, 35000, event(0x18, lk)); // only kept when bonding
|
||||
later(s, 40000, event(0x06, {0x00, (uint8_t)kHandle, (uint8_t)(kHandle >> 8)}));
|
||||
printf("[btpeer] %s: paired (%s)\n", pr.name,
|
||||
(st.last_auth_req & 0x06) ? "bonding" : "no bonding");
|
||||
return true;
|
||||
}
|
||||
case 0x0413: // Set_Connection_Encryption
|
||||
later(s, 500, cmd_status(op, 0x00));
|
||||
if (st.conn >= 0) {
|
||||
st.encrypted = n >= 3 && p[2];
|
||||
later(s, 20000, event(0x08, {0x00, (uint8_t)kHandle, (uint8_t)(kHandle >> 8),
|
||||
(uint8_t)(st.encrypted ? 1 : 0)}));
|
||||
}
|
||||
return true;
|
||||
case 0x040A: // Reject_Connection_Request
|
||||
later(s, 500, cmd_status(op, 0x00));
|
||||
return true;
|
||||
case 0x040E: case 0x042F: { // PIN / passkey negative replies
|
||||
std::vector<uint8_t> r = {0x00};
|
||||
put_addr(r, p);
|
||||
later(s, 500, cmd_complete(op, r));
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void btpeer_acl(EmuState *s, const uint8_t *pkt, uint32_t n) {
|
||||
if (n < 8 || st.conn < 0)
|
||||
return;
|
||||
uint16_t hf = pkt[0] | pkt[1] << 8;
|
||||
if ((hf & 0x0FFF) != kHandle)
|
||||
return;
|
||||
// Every packet the host sends is acknowledged, as the radio does.
|
||||
later(s, 1000, event(0x13, {0x01, (uint8_t)kHandle, (uint8_t)(kHandle >> 8), 0x01, 0x00}));
|
||||
uint16_t l2 = pkt[4] | pkt[5] << 8, cid = pkt[6] | pkt[7] << 8;
|
||||
if (8u + l2 > n)
|
||||
return;
|
||||
const uint8_t *d = pkt + 8;
|
||||
if (cid == 0x0001)
|
||||
signal_in(s, d, l2);
|
||||
else if (cid == st.ch[1].peer_cid && st.ch[1].open)
|
||||
hid_output(s, d, l2);
|
||||
}
|
||||
|
||||
void btpeer_tick(EmuState *s) {
|
||||
uint64_t now = now_us(s);
|
||||
auto &fifo = s->uart_rx_fifo[UART_D];
|
||||
// What is due, oldest first.
|
||||
for (;;) {
|
||||
size_t best = st.q.size();
|
||||
for (size_t i = 0; i < st.q.size(); i++)
|
||||
if (st.q[i].due <= now && (best == st.q.size() || st.q[i].due < st.q[best].due))
|
||||
best = i;
|
||||
if (best == st.q.size())
|
||||
break;
|
||||
for (uint8_t b : st.q[best].bytes)
|
||||
fifo.push_back(b);
|
||||
st.q.erase(st.q.begin() + best);
|
||||
}
|
||||
if (!st.streaming || st.conn < 0)
|
||||
return;
|
||||
// Reports. A host that does not read holds the radio up: past a backlog
|
||||
// the reports wait instead of piling up, as with flow control.
|
||||
while (st.next_report <= now) {
|
||||
if (fifo.size() > kHostBacklog) {
|
||||
st.next_report = now + kReportUs;
|
||||
break;
|
||||
}
|
||||
uint64_t t = st.next_report - st.stream_at;
|
||||
std::vector<uint8_t> b;
|
||||
if (st.full) {
|
||||
b = report(0x30, t, {});
|
||||
} else {
|
||||
// Before it is asked for full reports: the simple 0x3F one.
|
||||
std::vector<uint8_t> r(1 + 12, 0);
|
||||
r[0] = 0xA1;
|
||||
r[1] = 0x3F;
|
||||
r[4] = 0x08; // hat centred
|
||||
b = acl(st.ch[1].host_cid, r);
|
||||
}
|
||||
for (uint8_t x : b)
|
||||
fifo.push_back(x);
|
||||
st.next_report += kReportUs;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Fake Bluetooth controllers for the CYW4356 model (mmio.cpp): what a
|
||||
// payload acting as an HCI host finds and talks to once the radio is up.
|
||||
//
|
||||
// Two peers in pairing mode: a Pro Controller that insists on Secure Simple
|
||||
// Pairing with bonding before it opens its HID channels, and a right Joy-Con
|
||||
// that needs no security at all, so both paths of a host are exercised.
|
||||
// Once a host has both HID channels open, the connected one streams input
|
||||
// reports in guest time, pressing each of its buttons in turn and sweeping
|
||||
// its sticks, so a screenshot shows the host's view moving.
|
||||
//
|
||||
// This proves a host against this model's reading of the protocol, not
|
||||
// against a real controller.
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
||||
struct EmuState;
|
||||
|
||||
// The radio went through a power-on reset: forget every connection.
|
||||
void btpeer_reset(EmuState *state);
|
||||
// An HCI command the reset model does not answer itself. True when handled.
|
||||
bool btpeer_command(EmuState *state, uint16_t opcode, const uint8_t *p,
|
||||
uint32_t n);
|
||||
// One ACL packet from the host: handle/flags (2), length (2), then data.
|
||||
void btpeer_acl(EmuState *state, const uint8_t *pkt, uint32_t n);
|
||||
// Deliver what is due by now. Called on every UART-D register access.
|
||||
void btpeer_tick(EmuState *state);
|
||||
@@ -0,0 +1,297 @@
|
||||
// Generated from the Tegra X1 TRM (DP-07225-001 v1.3p), chapter 5.2:
|
||||
// CLK_RST_CONTROLLER register reset values, unknown bits as 0.
|
||||
// Regenerate with trm_car_resets.py; do not edit by hand.
|
||||
#ifndef T210_CAR_RESET_H
|
||||
#define T210_CAR_RESET_H
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
struct CarReset { uint16_t off; uint32_t val; const char *name; };
|
||||
static const CarReset kCarReset[] = {
|
||||
{0x000, 0x00000000, "RST_SOURCE"},
|
||||
{0x004, 0x1CD3D000, "RST_DEVICES_L"},
|
||||
{0x008, 0x87D1F320, "RST_DEVICES_H"},
|
||||
{0x00C, 0x828EC5FA, "RST_DEVICES_U"},
|
||||
{0x010, 0x80000130, "CLK_OUT_ENB_L"},
|
||||
{0x014, 0x00000080, "CLK_OUT_ENB_H"},
|
||||
{0x018, 0x01F00200, "CLK_OUT_ENB_U"},
|
||||
{0x024, 0x00000000, "SUPER_CCLK_DIVIDER"},
|
||||
{0x028, 0x10000000, "SCLK_BURST_POLICY"},
|
||||
{0x02C, 0x00000000, "SUPER_SCLK_DIVIDER"},
|
||||
{0x030, 0x00000000, "CLK_SYSTEM_RATE"},
|
||||
{0x044, 0x00000000, "CLK_MASK_ARM"},
|
||||
{0x048, 0x00000000, "MISC_CLK_ENB"},
|
||||
{0x050, 0x500003F1, "OSC_CTRL"},
|
||||
{0x05C, 0x00000000, "OSC_FREQ_DET_STATUS"},
|
||||
{0x068, 0x00005C00, "PLLE_SS_CNTL"},
|
||||
{0x06C, 0x00000010, "PLLE_MISC1"},
|
||||
{0x080, 0x00108002, "PLLC_BASE"},
|
||||
{0x084, 0x00000002, "PLLC_OUT"},
|
||||
{0x088, 0x40000000, "PLLC_MISC"},
|
||||
{0x08C, 0x08000000, "PLLC_MISC_1"},
|
||||
{0x090, 0x00002A02, "PLLM_BASE"},
|
||||
{0x098, 0x00000000, "PLLM_MISC1"},
|
||||
{0x09C, 0x00000010, "PLLM_MISC2"},
|
||||
{0x0A0, 0x00115408, "PLLP_BASE"},
|
||||
{0x0A4, 0x00000002, "PLLP_OUTA"},
|
||||
{0x0A8, 0x00020002, "PLLP_OUTB"},
|
||||
{0x0AC, 0x00040000, "PLLP_MISC"},
|
||||
{0x0B0, 0x00803003, "PLLA_BASE"},
|
||||
{0x0B4, 0x00000002, "PLLA_OUT"},
|
||||
{0x0B8, 0x00000000, "PLLA_MISC1"},
|
||||
{0x0BC, 0x12000000, "PLLA_MISC"},
|
||||
{0x0C0, 0x00011902, "PLLU_BASE"},
|
||||
{0x0C4, 0x00020002, "PLLU_OUTA"},
|
||||
{0x0C8, 0x00000000, "PLLU_MISC1"},
|
||||
{0x0CC, 0x00000000, "PLLU_MISC"},
|
||||
{0x0D0, 0x00211001, "PLLD_BASE"},
|
||||
{0x0D8, 0x00000000, "PLLD_MISC1"},
|
||||
{0x0DC, 0x00000000, "PLLD_MISC"},
|
||||
{0x0E0, 0x00002401, "PLLX_BASE"},
|
||||
{0x0E4, 0x00000000, "PLLX_MISC"},
|
||||
{0x0E8, 0x0E007D02, "PLLE_BASE"},
|
||||
{0x0EC, 0x00000000, "PLLE_MISC"},
|
||||
{0x0F0, 0x00000000, "PLLE_SS_CNTL1"},
|
||||
{0x0F4, 0x00000000, "PLLE_SS_CNTL2"},
|
||||
{0x0F8, 0x00000000, "LVL2_CLK_GATE_OVRA"},
|
||||
{0x0FC, 0x00000000, "LVL2_CLK_GATE_OVRB"},
|
||||
{0x100, 0xD0000000, "CLK_SOURCE_I2S2"},
|
||||
{0x104, 0xD0000000, "CLK_SOURCE_I2S3"},
|
||||
{0x108, 0xC0000000, "CLK_SOURCE_SPDIF_OUT"},
|
||||
{0x10C, 0x00000000, "CLK_SOURCE_SPDIF_IN"},
|
||||
{0x110, 0xC0000000, "CLK_SOURCE_PWM"},
|
||||
{0x118, 0xC0000000, "CLK_SOURCE_SPI2"},
|
||||
{0x11C, 0xC0000000, "CLK_SOURCE_SPI3"},
|
||||
{0x124, 0xC0000000, "CLK_SOURCE_I2C1"},
|
||||
{0x134, 0xC0000000, "CLK_SOURCE_SPI1"},
|
||||
{0x138, 0xC0000000, "CLK_SOURCE_DISP1"},
|
||||
{0x13C, 0xC0000000, "CLK_SOURCE_DISP2"},
|
||||
{0x144, 0xC0000000, "CLK_SOURCE_ISP"},
|
||||
{0x148, 0x80000000, "CLK_SOURCE_VI"},
|
||||
{0x150, 0xC0000000, "CLK_SOURCE_SDMMC1"},
|
||||
{0x154, 0xC0000000, "CLK_SOURCE_SDMMC2"},
|
||||
{0x164, 0xC0000000, "CLK_SOURCE_SDMMC4"},
|
||||
{0x178, 0xC0000002, "CLK_SOURCE_UARTA"},
|
||||
{0x17C, 0xC0000002, "CLK_SOURCE_UARTB"},
|
||||
{0x180, 0x80000000, "CLK_SOURCE_HOST1X"},
|
||||
{0x198, 0xC0000000, "CLK_SOURCE_I2C2"},
|
||||
{0x1A0, 0xC0000002, "CLK_SOURCE_UARTC"},
|
||||
{0x1A8, 0x80000000, "CLK_SOURCE_VI_SENSOR"},
|
||||
{0x1B4, 0xC0000000, "CLK_SOURCE_SPI4"},
|
||||
{0x1B8, 0xC0000000, "CLK_SOURCE_I2C3"},
|
||||
{0x1BC, 0xC0000000, "CLK_SOURCE_SDMMC3"},
|
||||
{0x1C0, 0xC0000002, "CLK_SOURCE_UARTD"},
|
||||
{0x1CC, 0xC0000000, "CLK_SOURCE_OWR"},
|
||||
{0x1D4, 0xC0000000, "CLK_SOURCE_CSITE"},
|
||||
{0x1D8, 0xD0000000, "CLK_SOURCE_I2S1"},
|
||||
{0x1DC, 0x00000000, "CLK_SOURCE_DTV"},
|
||||
{0x1F4, 0xC0000000, "CLK_SOURCE_TSEC"},
|
||||
{0x1F8, 0xC0000000, "CLK_SOURCE_LA"},
|
||||
{0x1FC, 0x00000000, "CLK_SPARE2"},
|
||||
{0x280, 0x23000780, "CLK_OUT_ENB_X"},
|
||||
{0x284, 0x23000780, "CLK_ENB_X_SET"},
|
||||
{0x288, 0x23000780, "CLK_ENB_X_CLR"},
|
||||
{0x28C, 0x01E42049, "RST_DEVICES_X"},
|
||||
{0x290, 0x01E42049, "RST_DEV_X_SET"},
|
||||
{0x294, 0x01E42049, "RST_DEV_X_CLR"},
|
||||
{0x298, 0x00000300, "CLK_OUT_ENB_Y"},
|
||||
{0x29C, 0x00000300, "CLK_ENB_Y_SET"},
|
||||
{0x2A0, 0x00000300, "CLK_ENB_Y_CLR"},
|
||||
{0x2A4, 0x0FE9F0DD, "RST_DEVICES_Y"},
|
||||
{0x2A8, 0x0FE9F0DD, "RST_DEV_Y_SET"},
|
||||
{0x2AC, 0x0FE9F0DD, "RST_DEV_Y_CLR"},
|
||||
{0x2F4, 0x00000001, "DFLL_BASE"},
|
||||
{0x304, 0x1CD3D000, "RST_DEV_L_CLR"},
|
||||
{0x308, 0x87D1F320, "RST_DEV_H_SET"},
|
||||
{0x30C, 0x87D1F327, "RST_DEV_H_CLR"},
|
||||
{0x310, 0x828EC5FA, "RST_DEV_U_SET"},
|
||||
{0x314, 0x828EC5FA, "RST_DEV_U_CLR"},
|
||||
{0x320, 0x80000130, "CLK_ENB_L_SET"},
|
||||
{0x324, 0x80000130, "CLK_ENB_L_CLR"},
|
||||
{0x328, 0x00000080, "CLK_ENB_H_SET"},
|
||||
{0x32C, 0x00000080, "CLK_ENB_H_CLR"},
|
||||
{0x330, 0x01F80200, "CLK_ENB_U_SET"},
|
||||
{0x334, 0x01F00200, "CLK_ENB_U_CLR"},
|
||||
{0x33C, 0x00000000, "CCPLEX_PG_SM_OVRD"},
|
||||
{0x340, 0x2000FEEF, "RST_CPU_CMPLX_SET"},
|
||||
{0x358, 0xFF818080, "RST_DEVICES_V"},
|
||||
{0x35C, 0x190077FF, "RST_DEVICES_W"},
|
||||
{0x360, 0x00400008, "CLK_OUT_ENB_V"},
|
||||
{0x364, 0x402000FC, "CLK_OUT_ENB_W"},
|
||||
{0x368, 0x10000000, "CCLKG_BURST_POLICY"},
|
||||
{0x36C, 0x00000000, "SUPER_CCLKG_DIVIDER"},
|
||||
{0x374, 0x00000000, "SUPER_CCLKLP_DIVIDER"},
|
||||
{0x380, 0x00000010, "CPU_SOFTRST_CTRL"},
|
||||
{0x384, 0x00040004, "CPU_SOFTRST_CTRL1"},
|
||||
{0x388, 0x07000200, "CPU_SOFTRST_CTRL2"},
|
||||
{0x3A0, 0x00000000, "LVL2_CLK_GATE_OVRC"},
|
||||
{0x3A4, 0x00000000, "LVL2_CLK_GATE_OVRD"},
|
||||
{0x3B4, 0xC0000000, "CLK_SOURCE_MSELECT"},
|
||||
{0x3B8, 0xC0000000, "CLK_SOURCE_TSENSOR"},
|
||||
{0x3BC, 0xD0000000, "CLK_SOURCE_I2S4"},
|
||||
{0x3C0, 0xD0000000, "CLK_SOURCE_I2S5"},
|
||||
{0x3C4, 0xC0000000, "CLK_SOURCE_I2C4"},
|
||||
{0x3D0, 0xD0000000, "CLK_SOURCE_AHUB"},
|
||||
{0x3E4, 0xC0000000, "CLK_SOURCE_HDA2CODEC_2X"},
|
||||
{0x3E8, 0xC0000000, "CLK_SOURCE_ACTMON"},
|
||||
{0x3EC, 0x60000000, "CLK_SOURCE_EXTPERIPH1"},
|
||||
{0x3F0, 0x60000000, "CLK_SOURCE_EXTPERIPH2"},
|
||||
{0x3F4, 0x60000000, "CLK_SOURCE_EXTPERIPH3"},
|
||||
{0x3FC, 0xC0000000, "CLK_SOURCE_I2C_SLOW"},
|
||||
{0x410, 0xC0000000, "CLK_SOURCE_SOR1"},
|
||||
{0x414, 0x00000000, "CLK_SOURCE_SOR0"},
|
||||
{0x420, 0xC0000000, "CLK_SOURCE_SATA_OOB"},
|
||||
{0x424, 0xC1000000, "CLK_SOURCE_SATA"},
|
||||
{0x428, 0xC0000000, "CLK_SOURCE_HDA"},
|
||||
{0x430, 0xFF818080, "RST_DEV_V_SET"},
|
||||
{0x434, 0xFF818080, "RST_DEV_V_CLR"},
|
||||
{0x438, 0x190077FF, "RST_DEV_W_SET"},
|
||||
{0x43C, 0x190077FF, "RST_DEV_W_CLR"},
|
||||
{0x440, 0x00400008, "CLK_ENB_V_SET"},
|
||||
{0x444, 0x00400008, "CLK_ENB_V_CLR"},
|
||||
{0x448, 0x402000FC, "CLK_ENB_W_SET"},
|
||||
{0x44C, 0x402000FC, "CLK_ENB_W_CLR"},
|
||||
{0x450, 0x2000FEEF, "RST_CPUG_CMPLX_SET"},
|
||||
{0x454, 0x2000FEEF, "RST_CPUG_CMPLX_CLR"},
|
||||
{0x460, 0x00000000, "CLK_CPUG_CMPLX_SET"},
|
||||
{0x464, 0x00000000, "CLK_CPUG_CMPLX_CLR"},
|
||||
{0x470, 0x00000000, "CPU_CMPLX_STATUS"},
|
||||
{0x47C, 0x00000000, "INTMASK"},
|
||||
{0x488, 0x00318015, "UTMIP_PLL_CFG2"},
|
||||
{0x48C, 0x00002070, "PLLE_AUX"},
|
||||
{0x494, 0x00011400, "SATA_PLL_CFG1"},
|
||||
{0x4A0, 0x00000000, "AUDIO_SYNC_CLK_I2S1"},
|
||||
{0x4A4, 0x00000000, "AUDIO_SYNC_CLK_I2S2"},
|
||||
{0x4A8, 0x00000000, "AUDIO_SYNC_CLK_I2S3"},
|
||||
{0x4AC, 0x00000000, "AUDIO_SYNC_CLK_I2S4"},
|
||||
{0x4B0, 0x00000000, "AUDIO_SYNC_CLK_I2S5"},
|
||||
{0x4B4, 0x00000000, "AUDIO_SYNC_CLK_SPDIF"},
|
||||
{0x4B8, 0x00041401, "PLLD2_BASE"},
|
||||
{0x4BC, 0x40000000, "PLLD2_MISC"},
|
||||
{0x4C0, 0x00000000, "UTMIP_PLL_CFG3"},
|
||||
{0x4C4, 0x00004104, "PLLREFE_BASE"},
|
||||
{0x4C8, 0x00000000, "PLLREFE_MISC"},
|
||||
{0x4CC, 0x00020002, "PLLREFE_OUT"},
|
||||
{0x4D0, 0x00000000, "CPU_FINETRIM_BYP"},
|
||||
{0x4D4, 0x00000000, "CPU_FINETRIM_SELECT"},
|
||||
{0x4D8, 0x00000000, "CPU_FINETRIM_DR"},
|
||||
{0x4DC, 0x00000000, "CPU_FINETRIM_DF"},
|
||||
{0x4E0, 0x00000000, "CPU_FINETRIM_F"},
|
||||
{0x4E4, 0x00000000, "CPU_FINETRIM_R"},
|
||||
{0x4E8, 0x00108002, "PLLC2_BASE"},
|
||||
{0x4EC, 0x40000000, "PLLC2_MISC"},
|
||||
{0x4F0, 0x08000000, "PLLC2_MISC_1"},
|
||||
{0x4F4, 0x1F720005, "PLLC2_MISC_2"},
|
||||
{0x4F8, 0x000000C4, "PLLC2_MISC_3"},
|
||||
{0x4FC, 0x00108002, "PLLC3_BASE"},
|
||||
{0x500, 0x40000000, "PLLC3_MISC"},
|
||||
{0x504, 0x08000000, "PLLC3_MISC_1"},
|
||||
{0x508, 0x1F720005, "PLLC3_MISC_2"},
|
||||
{0x50C, 0x000000C4, "PLLC3_MISC_3"},
|
||||
{0x510, 0x00000000, "PLLX_MISC_1"},
|
||||
{0x514, 0x00000000, "PLLX_MISC_2"},
|
||||
{0x518, 0x00000008, "PLLX_MISC_3"},
|
||||
{0x520, 0x00000014, "XUSBIO_PLL_CFG1"},
|
||||
{0x524, 0x00000105, "PLLE_AUX1"},
|
||||
{0x528, 0x00000053, "PLLP_RESHIFT"},
|
||||
{0x52C, 0x0000000F, "UTMIPLL_HW_PWRDN_CFG0"},
|
||||
{0x530, 0x0000008D, "PLLU_HW_PWRDN_CFG0"},
|
||||
{0x534, 0x8002A80F, "XUSB_PLL_CFG0"},
|
||||
{0x53C, 0x00000002, "CLK_CPU_MISC"},
|
||||
{0x540, 0x00000002, "CLK_CPUG_MISC"},
|
||||
{0x548, 0x00000001, "PLLX_HW_CTRL_CFG"},
|
||||
{0x54C, 0x00000000, "PLLX_SW_RAMP_CFG"},
|
||||
{0x550, 0x00000000, "PLLX_HW_CTRL_STATUS"},
|
||||
{0x554, 0x00000000, "LVL2_CLK_GATE_OVRE"},
|
||||
{0x55C, 0x00000000, "SPARE_REG0"},
|
||||
{0x560, 0x00000000, "AUDIO_SYNC_CLK_DMIC1"},
|
||||
{0x564, 0x00000000, "AUDIO_SYNC_CLK_DMIC2"},
|
||||
{0x570, 0x10000000, "PLLD2_SS_CFG"},
|
||||
{0x574, 0x00000000, "PLLD2_SS_CTRL1"},
|
||||
{0x578, 0x00000000, "PLLD2_SS_CTRL2"},
|
||||
{0x594, 0x40000000, "PLLDP_MISC"},
|
||||
{0x598, 0x10000000, "PLLDP_SS_CFG"},
|
||||
{0x59C, 0x00000000, "PLLDP_SS_CTRL1"},
|
||||
{0x5A0, 0x00000000, "PLLDP_SS_CTRL2"},
|
||||
{0x5A4, 0x000C2302, "PLLC4_BASE"},
|
||||
{0x5A8, 0x40000000, "PLLC4_MISC"},
|
||||
{0x5C4, 0x00000000, "CLK_SPARE0"},
|
||||
{0x5C8, 0x00000000, "CLK_SPARE1"},
|
||||
{0x5CC, 0x00000000, "GPU_ISOB_CTRL"},
|
||||
{0x5D0, 0x1F720005, "PLLC_MISC_2"},
|
||||
{0x5D4, 0x000000C4, "PLLC_MISC_3"},
|
||||
{0x5D8, 0x00000000, "PLLA_MISC2"},
|
||||
{0x5E4, 0x00000002, "PLLC4_OUT"},
|
||||
{0x5E8, 0x00002A02, "PLLMB_BASE"},
|
||||
{0x5EC, 0x00010000, "PLLMB_MISC1"},
|
||||
{0x5F0, 0x00000000, "PLLX_MISC_4"},
|
||||
{0x5F4, 0x00000000, "PLLX_MISC_5"},
|
||||
{0x600, 0x00000000, "CLK_SOURCE_XUSB_CORE_HOST"},
|
||||
{0x604, 0x00000000, "CLK_SOURCE_XUSB_FALCON"},
|
||||
{0x608, 0x00000000, "CLK_SOURCE_XUSB_FS"},
|
||||
{0x60C, 0x00000000, "CLK_SOURCE_XUSB_CORE_DEV"},
|
||||
{0x610, 0x04000000, "CLK_SOURCE_XUSB_SS"},
|
||||
{0x614, 0xC0000000, "CLK_SOURCE_CILAB"},
|
||||
{0x618, 0xC0000000, "CLK_SOURCE_CILCD"},
|
||||
{0x61C, 0xC0000000, "CLK_SOURCE_CILEF"},
|
||||
{0x620, 0xC0000000, "CLK_SOURCE_DSIA_LP"},
|
||||
{0x624, 0xC0000000, "CLK_SOURCE_DSIB_LP"},
|
||||
{0x628, 0x40000000, "CLK_SOURCE_ENTROPY"},
|
||||
{0x62C, 0xD0000000, "CLK_SOURCE_DVFS_REF"},
|
||||
{0x630, 0xD0000000, "CLK_SOURCE_DVFS_SOC"},
|
||||
{0x640, 0x60000000, "CLK_SOURCE_EMC_LATENCY"},
|
||||
{0x644, 0x40000000, "CLK_SOURCE_SOC_THERM"},
|
||||
{0x64C, 0x00000000, "CLK_SOURCE_DMIC1"},
|
||||
{0x650, 0x00000000, "CLK_SOURCE_DMIC2"},
|
||||
{0x658, 0x80000000, "CLK_SOURCE_VI_SENSOR2"},
|
||||
{0x65C, 0xC0000000, "CLK_SOURCE_I2C6"},
|
||||
{0x660, 0xC0000000, "CLK_SOURCE_MIPIBIF"},
|
||||
{0x664, 0x60000000, "CLK_SOURCE_EMC_DLL"},
|
||||
{0x66C, 0x00000000, "CLK_SOURCE_UART_FST_MIPI_CAL"},
|
||||
{0x678, 0xC0000000, "CLK_SOURCE_VIC"},
|
||||
{0x67C, 0x00030000, "PLLP_OUTC"},
|
||||
{0x680, 0x30000000, "PLLP_MISC1"},
|
||||
{0x694, 0x00000000, "CLK_SOURCE_SDMMC_LEGACY_TM"},
|
||||
{0x698, 0xE0000000, "CLK_SOURCE_NVDEC"},
|
||||
{0x69C, 0xE0000000, "CLK_SOURCE_NVJPG"},
|
||||
{0x6A0, 0x80000000, "CLK_SOURCE_NVENC"},
|
||||
{0x6A4, 0x00108002, "PLLA1_BASE"},
|
||||
{0x6A8, 0x40000000, "PLLA1_MISC"},
|
||||
{0x6AC, 0x08000000, "PLLA1_MISC_1"},
|
||||
{0x6B0, 0x1F720005, "PLLA1_MISC_2"},
|
||||
{0x6B4, 0x000000C4, "PLLA1_MISC_3"},
|
||||
{0x6B8, 0x00000000, "AUDIO_SYNC_CLK_DMIC3"},
|
||||
{0x6BC, 0x00000000, "CLK_SOURCE_DMIC3"},
|
||||
{0x6C0, 0xC0000000, "CLK_SOURCE_APE"},
|
||||
{0x6C4, 0xC0000000, "CLK_SOURCE_QSPI"},
|
||||
{0x6C8, 0xC0000000, "CLK_SOURCE_VI_I2C"},
|
||||
{0x6CC, 0x00000000, "CLK_SOURCE_USB2_HSIC_TRK"},
|
||||
{0x6D0, 0x00000000, "CLK_SOURCE_PEX_SATA_USB_RX_BYP"},
|
||||
{0x6D4, 0x40000000, "CLK_SOURCE_MAUD"},
|
||||
{0x6D8, 0xC0000000, "CLK_SOURCE_TSECB"},
|
||||
{0x6DC, 0x00000002, "CLK_CPUG_MISC1"},
|
||||
{0x6E0, 0x10006666, "ACLK_BURST_POLICY"},
|
||||
{0x6E4, 0x00000000, "SUPER_ACLK_DIVIDER"},
|
||||
{0x6E8, 0x80000000, "NVENC_SUPER_CLK_DIVIDER"},
|
||||
{0x6EC, 0x80000000, "VI_SUPER_CLK_DIVIDER"},
|
||||
{0x6F0, 0x80000000, "VIC_SUPER_CLK_DIVIDER"},
|
||||
{0x6F4, 0x80000000, "NVDEC_SUPER_CLK_DIVIDER"},
|
||||
{0x6F8, 0x80000000, "ISP_SUPER_CLK_DIVIDER"},
|
||||
{0x6FC, 0x80000000, "ISPB_SUPER_CLK_DIVIDER"},
|
||||
{0x700, 0x80000000, "NVJPG_SUPER_CLK_DIVIDER"},
|
||||
{0x704, 0x80000000, "SE_SUPER_CLK_DIVIDER"},
|
||||
{0x708, 0x80000000, "TSEC_SUPER_CLK_DIVIDER"},
|
||||
{0x70C, 0x80000000, "TSECB_SUPER_CLK_DIVIDER"},
|
||||
{0x710, 0xC0000002, "CLK_SOURCE_UARTAPE"},
|
||||
{0x714, 0x00000000, "CLK_CPUG_MISC2"},
|
||||
{0x718, 0xC0000000, "CLK_SOURCE_DBGAPB"},
|
||||
{0x71C, 0x001FFFFF, "CLK_CCPLEX_CC4_RET_CLK_ENB"},
|
||||
{0x720, 0x00000400, "ACTMON_CPU_CLK"},
|
||||
{0x724, 0x60000000, "CLK_SOURCE_EMC_SAFE"},
|
||||
{0x728, 0x00000C00, "SDMMC2_PLLC4_OUT0_SHAPER_CTRL"},
|
||||
{0x72C, 0x00000C00, "SDMMC2_PLLC4_OUT1_SHAPER_CTRL"},
|
||||
{0x730, 0x00000C00, "SDMMC2_PLLC4_OUT2_SHAPER_CTRL"},
|
||||
{0x734, 0x00000C00, "SDMMC2_DIV_CLK_SHAPER_CTRL"},
|
||||
};
|
||||
|
||||
#endif
|
||||
+484
-19
@@ -1,7 +1,10 @@
|
||||
#include "ccplex.h"
|
||||
#include "bpmp.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
@@ -12,6 +15,7 @@
|
||||
#include "memory_map.h"
|
||||
#include "mmio.h"
|
||||
#include "pcie.h"
|
||||
#include "gic.h"
|
||||
|
||||
// ==========================================================================
|
||||
// Registers (bdk soc/clock.h, soc/t210.h, soc/pmc.h; Tegra X1 TRM ch.5/16)
|
||||
@@ -138,6 +142,16 @@ struct Model {
|
||||
uint64_t bus_accesses = 0;
|
||||
uint32_t boots = 0;
|
||||
const char *last_refusal = nullptr;
|
||||
|
||||
// Generic timer (ARM ARM D11): the EL1 physical and virtual timers.
|
||||
uint64_t cntp_cval = 0, cntv_cval = 0, cntvoff = 0;
|
||||
uint32_t cntp_ctl = 0, cntv_ctl = 0, cntkctl = 0, cnthctl = 0;
|
||||
|
||||
// An interrupt found pending and unmasked at a block boundary: the run
|
||||
// stops there and ccplex_run() takes it.
|
||||
bool irq_take = false;
|
||||
uint64_t irqs_taken = 0, syncs_taken = 0, smcs = 0;
|
||||
uint32_t xlat_gen = 1; // bumped when TTBR/TCR/SCTLR change
|
||||
};
|
||||
|
||||
Model m;
|
||||
@@ -151,10 +165,201 @@ struct CodePage {
|
||||
};
|
||||
std::vector<std::unique_ptr<CodePage>> code_pages;
|
||||
uint64_t last_code_page = ~0ULL;
|
||||
uc_hook h_block, h_unmapped, h_intr;
|
||||
uc_hook h_block, h_unmapped, h_intr, h_mrs, h_msr;
|
||||
|
||||
bool held() { return (m.rst_cpug & (RST_CPU0_MASK | RST_NONCPU)) != 0; }
|
||||
|
||||
// ---- Generic timer -------------------------------------------------------
|
||||
//
|
||||
// The A57s' system counter runs from the 19.2 MHz TSC on the Tegra X1 (the
|
||||
// device tree's arch timer node says 19200000), so the count is CPU0's own
|
||||
// clock in those units: deterministic, like everything else here. Every
|
||||
// access to the timer registers is answered from this model; QEMU's own
|
||||
// timers never see one.
|
||||
static constexpr uint64_t CNT_HZ = 19200000;
|
||||
|
||||
uint64_t cnt_now() { return m.ns * 12 / 625; } // ns * 19.2e6 / 1e9
|
||||
|
||||
// TMR10..13 (see ccplex.h): PTV bit 31 EN, bit 30 PER, 28:0 the count of
|
||||
// microseconds in the n+1 scheme (TRM 8.7.1); PCR bit 30 clears the
|
||||
// interrupt. Kept with CPU0 because only its clock times them.
|
||||
struct CpuTmr { uint32_t ptv = 0; uint64_t armed_ns = 0; bool pending = false; };
|
||||
CpuTmr cpu_tmr[4];
|
||||
|
||||
uint64_t tmr_deadline(const CpuTmr &t) {
|
||||
return t.armed_ns + ((uint64_t)(t.ptv & 0x1FFFFFFF) + 1) * 1000;
|
||||
}
|
||||
|
||||
void cpu_tmrs_update() {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
CpuTmr &t = cpu_tmr[i];
|
||||
if ((t.ptv & (1u << 31)) && m.ns >= tmr_deadline(t)) {
|
||||
t.pending = true;
|
||||
if (t.ptv & (1u << 30)) {
|
||||
uint64_t per = ((uint64_t)(t.ptv & 0x1FFFFFFF) + 1) * 1000;
|
||||
while (tmr_deadline(t) <= m.ns)
|
||||
t.armed_ns += per;
|
||||
} else {
|
||||
t.ptv &= ~(1u << 31);
|
||||
}
|
||||
}
|
||||
gic_set_spi(176 + i, t.pending);
|
||||
}
|
||||
}
|
||||
uint64_t ns_of_cnt(uint64_t c) {
|
||||
return c > UINT64_MAX / 625 ? UINT64_MAX : (c * 625 + 11) / 12;
|
||||
}
|
||||
|
||||
void timers_update() {
|
||||
cpu_tmrs_update();
|
||||
uint64_t p = cnt_now(), v = p - m.cntvoff;
|
||||
bool pt = (m.cntp_ctl & 1) && p >= m.cntp_cval;
|
||||
bool vt = (m.cntv_ctl & 1) && v >= m.cntv_cval;
|
||||
gic_set_ppi(GIC_PPI_PTIMER, pt && !(m.cntp_ctl & 2));
|
||||
gic_set_ppi(GIC_PPI_VTIMER, vt && !(m.cntv_ctl & 2));
|
||||
}
|
||||
|
||||
// When the next unmasked timer fires, on CPU0's clock; UINT64_MAX for never.
|
||||
uint64_t next_timer_ns() {
|
||||
uint64_t t = UINT64_MAX;
|
||||
for (const CpuTmr &c : cpu_tmr)
|
||||
if (c.ptv & (1u << 31))
|
||||
t = std::min(t, tmr_deadline(c));
|
||||
if ((m.cntp_ctl & 3) == 1)
|
||||
t = std::min(t, ns_of_cnt(m.cntp_cval));
|
||||
if ((m.cntv_ctl & 3) == 1) {
|
||||
uint64_t c = m.cntv_cval + m.cntvoff;
|
||||
if (c >= m.cntv_cval) // no wrap
|
||||
t = std::min(t, ns_of_cnt(c));
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
// ---- EL1&0 stage-1 translation ---------------------------------------------
|
||||
//
|
||||
// Only for the emulator's own peeks (is the last instruction of a block a
|
||||
// WFI?), so 4 KiB granules only, which is what Linux and the stubs use. A
|
||||
// small direct-mapped cache, emptied when the translation registers change.
|
||||
uint64_t cp_read(uc_engine *uc, uint32_t op0, uint32_t op1, uint32_t crn,
|
||||
uint32_t crm, uint32_t op2) {
|
||||
uc_arm64_cp_reg r = {crn, crm, op0, op1, op2, 0};
|
||||
uc_reg_read(uc, UC_ARM64_REG_CP_REG, &r);
|
||||
return r.val;
|
||||
}
|
||||
|
||||
void cp_write(uc_engine *uc, uint32_t op0, uint32_t op1, uint32_t crn,
|
||||
uint32_t crm, uint32_t op2, uint64_t val) {
|
||||
uc_arm64_cp_reg r = {crn, crm, op0, op1, op2, val};
|
||||
uc_reg_write(uc, UC_ARM64_REG_CP_REG, &r);
|
||||
}
|
||||
|
||||
const uint8_t *host_ptr(uint64_t a, uint32_t len);
|
||||
|
||||
bool walk_va(uc_engine *uc, uint64_t va, uint64_t *pa) {
|
||||
uint64_t sctlr = cp_read(uc, 3, 0, 1, 0, 0);
|
||||
if (!(sctlr & 1)) {
|
||||
*pa = va;
|
||||
return true;
|
||||
}
|
||||
uint64_t tcr = cp_read(uc, 3, 0, 2, 0, 2);
|
||||
bool hi = (va >> 55) & 1;
|
||||
uint64_t ttbr = hi ? cp_read(uc, 3, 0, 2, 0, 1) : cp_read(uc, 3, 0, 2, 0, 0);
|
||||
unsigned tsz = hi ? (tcr >> 16) & 0x3F : tcr & 0x3F;
|
||||
unsigned tg = hi ? (tcr >> 30) & 3 : (tcr >> 14) & 3;
|
||||
if ((hi && tg != 2) || (!hi && tg != 0))
|
||||
return false; // not a 4 KiB granule
|
||||
unsigned va_bits = 64 - tsz;
|
||||
if (va_bits < 25 || va_bits > 48)
|
||||
return false;
|
||||
int levels = (int)(va_bits - 12 + 8) / 9;
|
||||
uint64_t table = ttbr & 0x0000FFFFFFFFF000ULL;
|
||||
for (int lvl = 4 - levels; lvl <= 3; lvl++) {
|
||||
unsigned shift = 12 + 9 * (3 - lvl);
|
||||
uint64_t idx = (va >> shift) & 0x1FF;
|
||||
const uint8_t *p = host_ptr(table + idx * 8, 8);
|
||||
if (!p)
|
||||
return false;
|
||||
uint64_t d;
|
||||
memcpy(&d, p, 8);
|
||||
if (!(d & 1))
|
||||
return false;
|
||||
if (lvl == 3 || !(d & 2)) {
|
||||
if (lvl == 3 && !(d & 2))
|
||||
return false;
|
||||
uint64_t mask = (1ULL << shift) - 1;
|
||||
*pa = (d & 0x0000FFFFFFFFF000ULL & ~mask) | (va & mask);
|
||||
return true;
|
||||
}
|
||||
table = d & 0x0000FFFFFFFFF000ULL;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
struct XlatEntry { uint64_t va_page; uint64_t pa_page; uint32_t gen; };
|
||||
XlatEntry xlat_cache[1024];
|
||||
|
||||
bool va_to_pa(uc_engine *uc, uint64_t va, uint64_t *pa) {
|
||||
uint64_t page = va >> 12;
|
||||
XlatEntry &e = xlat_cache[page & 1023];
|
||||
if (e.gen == m.xlat_gen && e.va_page == page) {
|
||||
*pa = (e.pa_page << 12) | (va & 0xFFF);
|
||||
return true;
|
||||
}
|
||||
uint64_t p;
|
||||
if (!walk_va(uc, va & ~0xFFFULL, &p))
|
||||
return false;
|
||||
e = {page, p >> 12, m.xlat_gen};
|
||||
*pa = p | (va & 0xFFF);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read_insn(uc_engine *uc, uint64_t va, uint32_t *insn) {
|
||||
uint64_t pa;
|
||||
if (!va_to_pa(uc, va, &pa))
|
||||
return false;
|
||||
const uint8_t *p = host_ptr(pa, 4);
|
||||
if (!p)
|
||||
return false;
|
||||
memcpy(insn, p, 4);
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---- Exception entry ---------------------------------------------------------
|
||||
//
|
||||
// Unicorn never delivers guest exceptions (see the reset-entry note), so the
|
||||
// model takes them itself: SPSR/ELR/ESR, PSTATE, then the vector. Only to
|
||||
// EL1 from EL1 on SP_EL1, which is every exception a kernel without
|
||||
// userspace or hypervisor takes - and it is the case where the translator's
|
||||
// cached EL stays right, because the EL does not change.
|
||||
enum ExcKind { EXC_SYNC = 0, EXC_IRQ = 1, EXC_FIQ = 2, EXC_SERROR = 3 };
|
||||
|
||||
bool take_exception(uc_engine *uc, uint64_t ret_pc, ExcKind kind,
|
||||
uint32_t esr, uint64_t far) {
|
||||
uint64_t ps = 0, vbar = 0;
|
||||
uc_reg_read(uc, UC_ARM64_REG_PSTATE, &ps);
|
||||
unsigned el = (ps >> 2) & 3;
|
||||
if (el != 1 || !(ps & 1)) {
|
||||
printf("[ccplex] CPU0 exception (kind %d, ESR 0x%08X) at PC=0x%llX from "
|
||||
"EL%u%c: only EL1h->EL1 entry is modelled, CPU0 halted\n", kind,
|
||||
esr, (unsigned long long)ret_pc, el, (ps & 1) ? 'h' : 't');
|
||||
fflush(stdout);
|
||||
return false;
|
||||
}
|
||||
uc_reg_read(uc, UC_ARM64_REG_VBAR_EL1, &vbar);
|
||||
cp_write(uc, 3, 0, 4, 0, 0, ps); // SPSR_EL1
|
||||
uc_reg_write(uc, UC_ARM64_REG_ELR_EL1, &ret_pc);
|
||||
if (kind == EXC_SYNC) {
|
||||
uint64_t e = esr;
|
||||
uc_reg_write(uc, UC_ARM64_REG_ESR_EL1, &e);
|
||||
uc_reg_write(uc, UC_ARM64_REG_FAR_EL1, &far);
|
||||
}
|
||||
uint64_t nps = (ps & 0xF0000000ULL) | 0x3C0 | 0x5; // EL1h, DAIF masked
|
||||
uc_reg_write(uc, UC_ARM64_REG_PSTATE, &nps);
|
||||
uint64_t vec = vbar + 0x200 + 0x80 * (uint64_t)kind;
|
||||
uc_reg_write(uc, UC_ARM64_REG_PC, &vec);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint64_t entry_vector() {
|
||||
return ((uint64_t)m.sb_rst_hi << 32) | (m.sb_rst_lo & ~1u);
|
||||
}
|
||||
@@ -290,28 +495,158 @@ void hook_block(uc_engine *uc, uint64_t address, uint32_t size, void *) {
|
||||
commit_pending();
|
||||
if (m.wfe_next) {
|
||||
// The block that just ran ended in WFE/WFI: the core sleeps until
|
||||
// something wakes it. Nothing in this model sends it an event, so it
|
||||
// sleeps to the end of the slice and resumes - a spurious wake-up, which
|
||||
// the architecture allows, and which a `for(;;) wfe;` park loop turns
|
||||
// straight back into sleep. Without this that loop would spin through
|
||||
// hundreds of millions of instructions per emulated second.
|
||||
// something wakes it - an interrupt, which here means a timer firing or
|
||||
// a device line the GIC already holds. With neither in sight it sleeps
|
||||
// to the end of the slice and resumes, a spurious wake-up the
|
||||
// architecture allows and a `for(;;) wfe;` park loop turns straight back
|
||||
// into sleep. Without this that loop would spin through hundreds of
|
||||
// millions of instructions per emulated second.
|
||||
m.wfe_next = false;
|
||||
if (m.ns < m.target_ns)
|
||||
m.ns = m.target_ns;
|
||||
if (!gic_irq_pending()) {
|
||||
uint64_t wake = std::min(m.target_ns, next_timer_ns());
|
||||
if (wake > m.ns)
|
||||
m.ns = wake;
|
||||
}
|
||||
}
|
||||
timers_update();
|
||||
g_state->emu_usec = m.ns / 1000;
|
||||
if (m.ns >= m.target_ns || m.kill_pending) {
|
||||
uc_emu_stop(uc);
|
||||
return;
|
||||
}
|
||||
if (gic_irq_pending()) {
|
||||
uint64_t ps = 0;
|
||||
uc_reg_read(uc, UC_ARM64_REG_PSTATE, &ps);
|
||||
if (!(ps & (1u << 7))) { // PSTATE.I clear: take it before this block
|
||||
m.irq_take = true;
|
||||
uc_emu_stop(uc);
|
||||
return;
|
||||
}
|
||||
}
|
||||
note_code_page(address);
|
||||
// Every A64 instruction is four bytes.
|
||||
m.pending = size >= 4 ? size / 4 : 1;
|
||||
if (const uint8_t *p = host_ptr(address + size - 4, 4)) {
|
||||
uint32_t last;
|
||||
memcpy(&last, p, 4);
|
||||
uint32_t last;
|
||||
if (read_insn(uc, address + size - 4, &last))
|
||||
m.wfe_next = (last == A64_WFE || last == A64_WFI);
|
||||
}
|
||||
|
||||
// ---- System registers --------------------------------------------------------
|
||||
//
|
||||
// MRS/MSR of the generic timer are answered here (and skipped by Unicorn);
|
||||
// writes to the translation registers also empty the emulator's VA cache.
|
||||
bool is(const uc_arm64_cp_reg *r, uint32_t op0, uint32_t op1, uint32_t crn,
|
||||
uint32_t crm, uint32_t op2) {
|
||||
return r->op0 == op0 && r->op1 == op1 && r->crn == crn && r->crm == crm &&
|
||||
r->op2 == op2;
|
||||
}
|
||||
|
||||
uint32_t hook_mrs(uc_engine *uc, uc_arm64_reg reg, const uc_arm64_cp_reg *r,
|
||||
void *) {
|
||||
if (r->op0 != 3 || r->crn != 14)
|
||||
return 0;
|
||||
uint64_t p = cnt_now(), v = p - m.cntvoff, out;
|
||||
if (is(r, 3, 3, 14, 0, 0)) out = CNT_HZ; // CNTFRQ_EL0
|
||||
else if (is(r, 3, 3, 14, 0, 1)) out = p; // CNTPCT_EL0
|
||||
else if (is(r, 3, 3, 14, 0, 2)) out = v; // CNTVCT_EL0
|
||||
else if (is(r, 3, 3, 14, 2, 0)) out = (uint32_t)(m.cntp_cval - p);
|
||||
else if (is(r, 3, 3, 14, 2, 1)) out = m.cntp_ctl | (((m.cntp_ctl & 1) && p >= m.cntp_cval) ? 4 : 0);
|
||||
else if (is(r, 3, 3, 14, 2, 2)) out = m.cntp_cval;
|
||||
else if (is(r, 3, 3, 14, 3, 0)) out = (uint32_t)(m.cntv_cval - v);
|
||||
else if (is(r, 3, 3, 14, 3, 1)) out = m.cntv_ctl | (((m.cntv_ctl & 1) && v >= m.cntv_cval) ? 4 : 0);
|
||||
else if (is(r, 3, 3, 14, 3, 2)) out = m.cntv_cval;
|
||||
else if (is(r, 3, 0, 14, 1, 0)) out = m.cntkctl; // CNTKCTL_EL1
|
||||
else if (is(r, 3, 4, 14, 0, 3)) out = m.cntvoff; // CNTVOFF_EL2
|
||||
else if (is(r, 3, 4, 14, 1, 0)) out = m.cnthctl; // CNTHCTL_EL2
|
||||
else return 0;
|
||||
if (reg != UC_ARM64_REG_XZR)
|
||||
uc_reg_write(uc, reg, &out);
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint32_t hook_msr(uc_engine *uc, uc_arm64_reg reg, const uc_arm64_cp_reg *r,
|
||||
void *) {
|
||||
(void)reg;
|
||||
(void)uc;
|
||||
if (r->op0 == 3 && r->op1 == 0 && ((r->crn == 2 && r->crm == 0 && r->op2 <= 2) ||
|
||||
(r->crn == 1 && r->crm == 0 && r->op2 == 0))) {
|
||||
m.xlat_gen++; // TTBR0/TTBR1/TCR/SCTLR_EL1
|
||||
return 0;
|
||||
}
|
||||
if (r->op0 != 3 || r->crn != 14)
|
||||
return 0;
|
||||
uint64_t p = cnt_now(), v = p - m.cntvoff, val = r->val;
|
||||
if (is(r, 3, 3, 14, 0, 0)) ; // CNTFRQ: fixed
|
||||
else if (is(r, 3, 3, 14, 2, 0)) m.cntp_cval = p + (uint64_t)(int64_t)(int32_t)val;
|
||||
else if (is(r, 3, 3, 14, 2, 1)) m.cntp_ctl = val & 3;
|
||||
else if (is(r, 3, 3, 14, 2, 2)) m.cntp_cval = val;
|
||||
else if (is(r, 3, 3, 14, 3, 0)) m.cntv_cval = v + (uint64_t)(int64_t)(int32_t)val;
|
||||
else if (is(r, 3, 3, 14, 3, 1)) m.cntv_ctl = val & 3;
|
||||
else if (is(r, 3, 3, 14, 3, 2)) m.cntv_cval = val;
|
||||
else if (is(r, 3, 0, 14, 1, 0)) m.cntkctl = (uint32_t)val;
|
||||
else if (is(r, 3, 4, 14, 0, 3)) m.cntvoff = val;
|
||||
else if (is(r, 3, 4, 14, 1, 0)) m.cnthctl = (uint32_t)val;
|
||||
else return 0;
|
||||
timers_update();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ---- PSCI (ARM DEN 0022), answered on SMC -------------------------------------
|
||||
//
|
||||
// On a Switch the secure monitor (ATF) answers these; here the emulator is
|
||||
// the monitor. One core is modelled, so CPU_ON is refused.
|
||||
void psci_call(uc_engine *uc) {
|
||||
uint64_t x0 = 0, x1 = 0, ret;
|
||||
uc_reg_read(uc, UC_ARM64_REG_X0, &x0);
|
||||
uc_reg_read(uc, UC_ARM64_REG_X1, &x1);
|
||||
uint32_t fid = (uint32_t)x0;
|
||||
m.smcs++;
|
||||
switch (fid) {
|
||||
case 0x84000000: ret = 0x00010000; break; // PSCI_VERSION 1.0
|
||||
case 0x84000006: ret = 2; break; // MIGRATE_INFO_TYPE: none
|
||||
case 0x8400000A: { // PSCI_FEATURES
|
||||
uint32_t q = (uint32_t)x1;
|
||||
ret = (q == 0x84000000 || q == 0x84000001 || q == 0xC4000001 ||
|
||||
q == 0x84000002 || q == 0x84000003 || q == 0xC4000003 ||
|
||||
q == 0x84000004 || q == 0xC4000004 || q == 0x84000006 ||
|
||||
q == 0x84000008 || q == 0x84000009 || q == 0x8400000A) ? 0 : (uint64_t)-1;
|
||||
break;
|
||||
}
|
||||
case 0x84000001: case 0xC4000001: ret = 0; break; // CPU_SUSPEND: a WFI
|
||||
case 0x84000003: case 0xC4000003: ret = (uint64_t)-3; break; // CPU_ON: DENIED
|
||||
case 0x84000004: case 0xC4000004: ret = (x1 & 0xFF) == 0 ? 0 : 1; break; // AFFINITY_INFO
|
||||
case 0x84000002: // CPU_OFF
|
||||
printf("[ccplex] PSCI CPU_OFF from CPU0\n");
|
||||
m.kill_pending = true;
|
||||
m.kill_reason = "PSCI CPU_OFF";
|
||||
uc_emu_stop(uc);
|
||||
ret = 0;
|
||||
break;
|
||||
case 0x84000008: // SYSTEM_OFF
|
||||
case 0x84000009: // SYSTEM_RESET
|
||||
printf("[ccplex] PSCI %s from CPU0\n", fid == 0x84000008 ? "SYSTEM_OFF" : "SYSTEM_RESET");
|
||||
m.kill_pending = true;
|
||||
m.kill_reason = fid == 0x84000008 ? "PSCI SYSTEM_OFF" : "PSCI SYSTEM_RESET";
|
||||
uc_emu_stop(uc);
|
||||
ret = 0;
|
||||
break;
|
||||
default: {
|
||||
static uint32_t seen[32];
|
||||
static int nseen;
|
||||
bool known = false;
|
||||
for (int i = 0; i < nseen; i++)
|
||||
known |= seen[i] == fid;
|
||||
if (!known && nseen < 32) {
|
||||
seen[nseen++] = fid;
|
||||
printf("[ccplex] SMC 0x%08X (x1=0x%llX) not implemented, returns NOT_SUPPORTED\n",
|
||||
fid, (unsigned long long)x1);
|
||||
fflush(stdout);
|
||||
}
|
||||
ret = (uint64_t)-1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
uc_reg_write(uc, UC_ARM64_REG_X0, &ret);
|
||||
}
|
||||
|
||||
bool hook_unmapped(uc_engine *uc, uc_mem_type type, uint64_t address, int size,
|
||||
@@ -320,6 +655,22 @@ bool hook_unmapped(uc_engine *uc, uc_mem_type type, uint64_t address, int size,
|
||||
(void)value;
|
||||
uint64_t pc = 0;
|
||||
uc_reg_read(uc, UC_ARM64_REG_PC, &pc);
|
||||
// The peripheral band decodes everywhere on the APB/AHB: a register the
|
||||
// emulator does not model reads back what was written, as it does for the
|
||||
// BPMP. Undecoded space outside it (the PCIe apertures' holes, nothing at
|
||||
// all) still wedges the core.
|
||||
if (address >= 0x50000000 && address < 0x80000000 &&
|
||||
type != UC_MEM_FETCH_UNMAPPED) {
|
||||
static std::vector<uint64_t> told;
|
||||
uint64_t page = address & ~0xFFFULL;
|
||||
if (std::find(told.begin(), told.end(), page) == told.end() && told.size() < 512) {
|
||||
told.push_back(page);
|
||||
printf("[ccplex] CPU0 first access to unmodelled page 0x%08llX (PC=0x%llX)\n",
|
||||
(unsigned long long)page, (unsigned long long)pc);
|
||||
fflush(stdout);
|
||||
}
|
||||
return mmio_map_page(uc, g_state, page);
|
||||
}
|
||||
const char *kind = type == UC_MEM_FETCH_UNMAPPED ? "fetch"
|
||||
: type == UC_MEM_WRITE_UNMAPPED ? "write"
|
||||
: "read";
|
||||
@@ -330,16 +681,54 @@ bool hook_unmapped(uc_engine *uc, uc_mem_type type, uint64_t address, int size,
|
||||
printf("[ccplex] CPU0 %s of unmapped 0x%llX at PC=0x%llX: nothing answers, "
|
||||
"CPU0 wedged\n", kind, (unsigned long long)address,
|
||||
(unsigned long long)pc);
|
||||
{
|
||||
uint64_t pa = 0;
|
||||
bool ok = walk_va(uc, address, &pa);
|
||||
printf("[ccplex] SCTLR_EL1 %08llX TCR_EL1 %llX TTBR0 %llX TTBR1 %llX; walk %s 0x%llX\n",
|
||||
(unsigned long long)cp_read(uc, 3, 0, 1, 0, 0),
|
||||
(unsigned long long)cp_read(uc, 3, 0, 2, 0, 2),
|
||||
(unsigned long long)cp_read(uc, 3, 0, 2, 0, 0),
|
||||
(unsigned long long)cp_read(uc, 3, 0, 2, 0, 1), ok ? "->" : "failed",
|
||||
(unsigned long long)pa);
|
||||
}
|
||||
fflush(stdout);
|
||||
return false;
|
||||
}
|
||||
|
||||
void hook_intr(uc_engine *uc, uint32_t intno, void *) {
|
||||
// Unicorn does not deliver guest exceptions (see the reset-entry note),
|
||||
// so a fault cannot reach the stub's own vectors. Say what happened and
|
||||
// stop the core rather than resume past the faulting instruction.
|
||||
// Unicorn does not deliver guest exceptions (see the reset-entry note).
|
||||
// SMC is answered here (PSCI) and execution goes on after it; BRK and
|
||||
// undefined instructions are taken to EL1's own vectors, which is how a
|
||||
// kernel's WARN() and BUG() work. Anything else stops the core rather
|
||||
// than resume past the faulting instruction.
|
||||
uint64_t pc = 0;
|
||||
uc_reg_read(uc, UC_ARM64_REG_PC, &pc);
|
||||
uint32_t insn = 0;
|
||||
if (intno == 13 || intno == 12) { // EXCP_SMC (QEMU 5: 13)
|
||||
if (read_insn(uc, pc - 4, &insn) && (insn & 0xFFE0001F) == 0xD4000003) {
|
||||
psci_call(uc);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (intno == 11 && read_insn(uc, pc - 4, &insn) && (insn & 0xFFE0001F) == 0xD4000002) {
|
||||
uint64_t r = (uint64_t)-1; // HVC with no hypervisor
|
||||
uc_reg_write(uc, UC_ARM64_REG_X0, &r);
|
||||
return;
|
||||
}
|
||||
if (intno == 7 && read_insn(uc, pc, &insn) && (insn & 0xFFE0001F) == 0xD4200000) {
|
||||
uint32_t esr = (0x3Cu << 26) | (1u << 25) | ((insn >> 5) & 0xFFFF);
|
||||
m.syncs_taken++;
|
||||
if (take_exception(uc, pc, EXC_SYNC, esr, 0))
|
||||
return;
|
||||
}
|
||||
if (intno == 1) { // EXCP_UDEF
|
||||
m.syncs_taken++;
|
||||
if (read_insn(uc, pc, &insn))
|
||||
printf("[ccplex] CPU0 undefined instruction 0x%08X at PC=0x%llX, taken to EL1\n",
|
||||
insn, (unsigned long long)pc);
|
||||
if (take_exception(uc, pc, EXC_SYNC, (1u << 25), 0))
|
||||
return;
|
||||
}
|
||||
static const char *names[] = {"?", "UDEF", "SWI", "PREFETCH_ABORT",
|
||||
"DATA_ABORT", "IRQ", "FIQ", "BKPT", "EXIT",
|
||||
"KERNEL_TRAP", "HVC", "HYP_TRAP", "SMC"};
|
||||
@@ -362,6 +751,11 @@ bool start_cpu0(EmuState *state) {
|
||||
return false;
|
||||
}
|
||||
uc_ctl_set_cpu_model(uc, UC_CPU_ARM64_A57);
|
||||
#if UC_API_MAJOR > 2 || (UC_API_MAJOR == 2 && UC_API_MINOR >= 1)
|
||||
// Translate through the guest's own page tables (the 2.1 default, set
|
||||
// explicitly): a kernel runs with its MMU on.
|
||||
uc_ctl_tlb_mode(uc, UC_TLB_CPU);
|
||||
#endif
|
||||
|
||||
// IRAM, DRAM and TZRAM are the same host memory the BPMP engine maps, so
|
||||
// a stub the BPMP copies into DRAM and the mailbox both cores poll are one
|
||||
@@ -397,6 +791,15 @@ bool start_cpu0(EmuState *state) {
|
||||
uc_hook_add(uc, &h_unmapped, UC_HOOK_MEM_UNMAPPED, (void *)hook_unmapped,
|
||||
nullptr, 1, 0);
|
||||
uc_hook_add(uc, &h_intr, UC_HOOK_INTR, (void *)hook_intr, nullptr, 1, 0);
|
||||
uc_hook_add(uc, &h_mrs, UC_HOOK_INSN, (void *)hook_mrs, nullptr, 1, 0,
|
||||
UC_ARM64_INS_MRS);
|
||||
if (!getenv("RCM_EMU_NO_MSR_HOOK"))
|
||||
uc_hook_add(uc, &h_msr, UC_HOOK_INSN, (void *)hook_msr, nullptr, 1, 0,
|
||||
UC_ARM64_INS_MSR);
|
||||
m.cntp_cval = m.cntv_cval = m.cntvoff = 0;
|
||||
m.cntp_ctl = m.cntv_ctl = m.cntkctl = m.cnthctl = 0;
|
||||
m.irq_take = false;
|
||||
m.xlat_gen++;
|
||||
|
||||
m.uc = uc;
|
||||
m.pc = RESET_TRAMPOLINE;
|
||||
@@ -572,6 +975,21 @@ void ccplex_bus_tick(EmuState *state) {
|
||||
|
||||
uint64_t ccplex_now_us() { return m.ns / 1000; }
|
||||
|
||||
void ccplex_tmr_write(uint32_t off, uint32_t val) {
|
||||
if (off < 0x90 || off >= 0xB0)
|
||||
return;
|
||||
CpuTmr &t = cpu_tmr[(off - 0x90) / 8];
|
||||
if ((off - 0x90) % 8 == 4) { // PCR
|
||||
if (val & (1u << 30)) {
|
||||
t.pending = false;
|
||||
gic_set_spi(176 + (int)((off - 0x90) / 8), false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
t.ptv = val; // PTV: (re)armed now
|
||||
t.armed_ns = m.ns;
|
||||
}
|
||||
|
||||
bool ccplex_cpu0_running() { return m.cpu0 == Cpu0::Running && m.uc; }
|
||||
|
||||
void ccplex_run(EmuState *state, uint64_t until_us) {
|
||||
@@ -601,18 +1019,65 @@ void ccplex_run(EmuState *state, uint64_t until_us) {
|
||||
m.target_ns = target;
|
||||
m.in_run = true;
|
||||
|
||||
drop_stale_code();
|
||||
// The BPMP can only rewrite CPU0's code while it runs; parked, it cannot,
|
||||
// and comparing hundreds of kernel pages before every slice is the one
|
||||
// thing that would make a booted kernel crawl.
|
||||
if (!state->bpmp_halted)
|
||||
drop_stale_code();
|
||||
|
||||
// `until` is an address an A64 PC can never hold (it is not 4-aligned).
|
||||
// 0 would make a branch to address 0 end every slice before it starts.
|
||||
uc_err err = uc_emu_start(m.uc, m.pc, ~0ULL, 0, 0);
|
||||
|
||||
commit_pending();
|
||||
uc_reg_read(m.uc, UC_ARM64_REG_PC, &m.pc);
|
||||
uc_err err;
|
||||
for (;;) {
|
||||
err = uc_emu_start(m.uc, m.pc, ~0ULL, 0, 0);
|
||||
commit_pending();
|
||||
uc_reg_read(m.uc, UC_ARM64_REG_PC, &m.pc);
|
||||
if (err != UC_ERR_OK || m.kill_pending || m.cpu0 != Cpu0::Running || !m.irq_take)
|
||||
break;
|
||||
// An interrupt was pending and unmasked at the block that starts at PC:
|
||||
// take it there, then carry on with the slice from the vector.
|
||||
m.irq_take = false;
|
||||
m.irqs_taken++;
|
||||
if (!take_exception(m.uc, m.pc, EXC_IRQ, 0, 0)) {
|
||||
m.cpu0 = Cpu0::Hung;
|
||||
break;
|
||||
}
|
||||
uc_reg_read(m.uc, UC_ARM64_REG_PC, &m.pc);
|
||||
if (m.ns >= m.target_ns)
|
||||
break;
|
||||
}
|
||||
m.in_run = false;
|
||||
g_bus_master = prev;
|
||||
state->emu_usec = bpmp_usec;
|
||||
|
||||
// RCM_EMU_CPU0_PROFILE=1: one line every second of real time with where
|
||||
// CPU0 is, how much it ran and how many interrupts it took - enough to
|
||||
// tell a hung kernel from a slow one.
|
||||
{
|
||||
static int prof = -1;
|
||||
static uint64_t last_wall = 0, last_ret = 0;
|
||||
if (prof < 0)
|
||||
prof = getenv("RCM_EMU_CPU0_PROFILE") ? 1 : 0;
|
||||
if (prof) {
|
||||
uint64_t wall = (uint64_t)std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch()).count();
|
||||
if (wall - last_wall >= 1000) {
|
||||
last_wall = wall;
|
||||
printf("[cpu0prof] t=%llu us pc=0x%llX retired=%llu (+%llu) irqs=%llu syncs=%llu smcs=%llu gic=%d\n",
|
||||
(unsigned long long)(m.ns / 1000), (unsigned long long)m.pc,
|
||||
(unsigned long long)m.retired, (unsigned long long)(m.retired - last_ret),
|
||||
(unsigned long long)m.irqs_taken, (unsigned long long)m.syncs_taken,
|
||||
(unsigned long long)m.smcs, gic_highest_pending());
|
||||
last_ret = m.retired;
|
||||
char g27[160];
|
||||
gic_describe(GIC_PPI_VTIMER, g27, sizeof(g27));
|
||||
printf("[cpu0prof] vtimer ctl %u cval %llu cnt %llu; %s\n", m.cntv_ctl,
|
||||
(unsigned long long)m.cntv_cval, (unsigned long long)(cnt_now() - m.cntvoff), g27);
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (m.kill_pending) {
|
||||
m.kill_pending = false;
|
||||
const char *why = m.kill_reason ? m.kill_reason : "stopped";
|
||||
|
||||
@@ -58,6 +58,11 @@ void ccplex_bus_tick(EmuState *state);
|
||||
// True while CPU0 is out of reset and executing (not parked on a hung bus).
|
||||
bool ccplex_cpu0_running();
|
||||
|
||||
// TMR10..TMR13 (TMR base + 0x90..0xAF): the timers Tegra210 kernels use as
|
||||
// per-CPU clock event devices. Their interrupts go to GIC SPI 176..179 on
|
||||
// CPU0's clock.
|
||||
void ccplex_tmr_write(uint32_t off, uint32_t val);
|
||||
|
||||
// Run CPU0 until its clock reaches `until_us` - the BPMP's current time.
|
||||
void ccplex_run(EmuState *state, uint64_t until_us);
|
||||
|
||||
|
||||
+1228
File diff suppressed because it is too large.
Load diff
+72
@@ -0,0 +1,72 @@
|
||||
#ifndef T210_DOCK_H
|
||||
#define T210_DOCK_H
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
struct EmuState;
|
||||
|
||||
/*
|
||||
* The USB-C port, the dock on it and the TV behind the dock.
|
||||
*
|
||||
* BM92T36 (I2C1 0x18): the PD controller's registers and commands as
|
||||
* switchroot's bm92txx.c and HOS's usb sysmodule drive them, with a partner
|
||||
* on the cable that answers the way the measured docks did: a Nintendo dock
|
||||
* (057E:2003, 5 V 0.5 A + 15 V 3 A, Nintendo alt mode, DP pin D), a third-
|
||||
* party adapter (Fresco Logic 1D5C:7102, four PDOs, DisplayPort SVID) or
|
||||
* the Nintendo charger (057E:2004, no video). Its interrupt is GPIO PK4,
|
||||
* active low; its reset is PV5.
|
||||
*
|
||||
* DPAUX1 (0x54040000): the AUX channel to the dock's DisplayPort-to-HDMI
|
||||
* converter (Realtek OUI 00-E0-4C, DPCD 1.2), its DPCD, and the TV's EDID
|
||||
* over I2C-over-AUX. HPD reaches DPAUX1 only while the hdmi_int_dp_hpd pad
|
||||
* is in SFIO mode, as measured.
|
||||
*
|
||||
* SOR1 (0x54580000): link training is judged lane by lane against what the
|
||||
* board does - DP lane 0 leaves the SoC on pad 0 and lane 1 on pad 1, so the
|
||||
* crossbar has to route them there (L4T's nvidia,xbar-ctrl <2 1 0 3 4>), and
|
||||
* a lane only trains with its pad powered, the macro PLL up, PLLDP locked
|
||||
* and SOR1 clocked from the brick at the rate the sink was told.
|
||||
*/
|
||||
|
||||
enum DockKind : uint8_t {
|
||||
DOCK_NONE = 0,
|
||||
DOCK_NINTENDO = 1,
|
||||
DOCK_THIRD_PARTY = 2, // Ugreen-style adapter: Fresco Logic 1D5C:7102
|
||||
DOCK_CHARGER = 3, // Nintendo charger, no DisplayPort
|
||||
};
|
||||
|
||||
void dock_reset(EmuState *state);
|
||||
// Advance the dock's timers to state->emu_usec. Cheap; called often.
|
||||
void dock_tick(EmuState *state);
|
||||
|
||||
// BM92T36. A write carries its register first; a read returns n bytes from
|
||||
// `reg` on (block registers lead with their length byte, as on the chip).
|
||||
bool bm92_present(EmuState *state);
|
||||
void bm92_write(EmuState *state, const uint8_t *buf, uint32_t n);
|
||||
void bm92_read(EmuState *state, uint8_t reg, uint8_t *out, uint32_t n);
|
||||
// PK4 as the SoC sees it: false while an enabled alert is pending.
|
||||
bool bm92_alert_line(EmuState *state);
|
||||
// PV5 driven by the SoC: high holds the chip in reset.
|
||||
void bm92_reset_line(EmuState *state, bool high);
|
||||
|
||||
// HPD from the dock's DisplayPort sink.
|
||||
bool dock_hpd(EmuState *state);
|
||||
|
||||
// DPAUX1 and SOR1 register windows (byte offsets).
|
||||
uint32_t dpaux1_read(EmuState *state, uint32_t off);
|
||||
void dpaux1_write(EmuState *state, uint32_t off, uint32_t val);
|
||||
uint32_t sor1_read(EmuState *state, uint32_t off);
|
||||
void sor1_write(EmuState *state, uint32_t off, uint32_t val);
|
||||
|
||||
// What the TV shows, for its window.
|
||||
struct TvState {
|
||||
bool connected; // a dock with a TV behind it is on the cable
|
||||
bool hpd; // the converter raised HPD to the SoC
|
||||
bool link_trained; // every configured lane has CR, EQ, symbol lock
|
||||
bool video; // SOR1 attached to a running head: frames flow
|
||||
uint32_t lanes, rate_code;
|
||||
char status[160]; // one line for the "no signal" screen
|
||||
};
|
||||
void dock_tv_state(EmuState *state, TvState *out);
|
||||
|
||||
#endif // T210_DOCK_H
|
||||
+307
@@ -0,0 +1,307 @@
|
||||
#include "gic.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
/*
|
||||
* GIC-400 model. See gic.h. Register offsets and reset values: ARM IHI 0048B
|
||||
* (GICv2 architecture) ch.4, GIC-400 TRM (DDI 0471B) table 3-1 / 3-6.
|
||||
*/
|
||||
|
||||
namespace {
|
||||
|
||||
struct Gic {
|
||||
uint32_t dctlr = 0;
|
||||
uint32_t cctlr = 0;
|
||||
uint32_t pmr = 0;
|
||||
uint32_t bpr = 2;
|
||||
uint32_t group[GIC_NR_IRQS / 32] = {};
|
||||
bool enabled[GIC_NR_IRQS] = {};
|
||||
bool latched[GIC_NR_IRQS] = {}; // edge, SGI or software-set pending
|
||||
bool line[GIC_NR_IRQS] = {}; // level input
|
||||
bool active[GIC_NR_IRQS] = {};
|
||||
uint8_t prio[GIC_NR_IRQS] = {};
|
||||
uint8_t target[GIC_NR_IRQS] = {};
|
||||
uint8_t cfg[GIC_NR_IRQS] = {}; // 2 bits each, bit 1 set = edge
|
||||
// Active priorities, most recent last: the running priority is the
|
||||
// highest (numerically lowest) of them.
|
||||
int stack[GIC_NR_IRQS] = {};
|
||||
int depth = 0;
|
||||
};
|
||||
|
||||
Gic g;
|
||||
|
||||
// CPU0 asks "is an interrupt pending?" before every translated block, so the
|
||||
// answer is cached and recomputed only after something changed.
|
||||
bool g_dirty = true;
|
||||
int g_best = 1023;
|
||||
bool g_line_up = false;
|
||||
|
||||
bool is_edge(int id) { return id < 16 || (g.cfg[id] & 2); }
|
||||
|
||||
bool pending(int id) {
|
||||
if (g.latched[id])
|
||||
return true;
|
||||
return !is_edge(id) && g.line[id];
|
||||
}
|
||||
|
||||
int running_prio() {
|
||||
int p = 0x100;
|
||||
for (int i = 0; i < g.depth; i++)
|
||||
if (g.prio[g.stack[i]] < p)
|
||||
p = g.prio[g.stack[i]];
|
||||
return p;
|
||||
}
|
||||
|
||||
int best_scan() {
|
||||
int bestid = 1023, bestp = 0x100;
|
||||
for (int id = 0; id < GIC_NR_IRQS; id++) {
|
||||
if (!g.enabled[id] || g.active[id] || !pending(id))
|
||||
continue;
|
||||
if (g.prio[id] < bestp) {
|
||||
bestp = g.prio[id];
|
||||
bestid = id;
|
||||
}
|
||||
}
|
||||
return bestid;
|
||||
}
|
||||
|
||||
int best() {
|
||||
if (g_dirty) {
|
||||
g_best = best_scan();
|
||||
g_line_up = false;
|
||||
if ((g.dctlr & 3) && (g.cctlr & 3) && g_best != 1023)
|
||||
g_line_up = g.prio[g_best] < g.pmr && g.prio[g_best] < running_prio();
|
||||
g_dirty = false;
|
||||
}
|
||||
return g_best;
|
||||
}
|
||||
|
||||
uint32_t bits_read(const bool *v, uint32_t word) {
|
||||
uint32_t r = 0;
|
||||
for (int b = 0; b < 32; b++)
|
||||
if (v[word * 32 + b])
|
||||
r |= 1u << b;
|
||||
return r;
|
||||
}
|
||||
|
||||
uint32_t pend_read(uint32_t word) {
|
||||
uint32_t r = 0;
|
||||
for (int b = 0; b < 32; b++)
|
||||
if (pending(word * 32 + b))
|
||||
r |= 1u << b;
|
||||
return r;
|
||||
}
|
||||
|
||||
// Byte-array registers (priority, targets) accessed by byte or by word.
|
||||
uint32_t bytes_read(const uint8_t *a, uint32_t off, unsigned size) {
|
||||
uint32_t r = 0;
|
||||
for (unsigned i = 0; i < size && off + i < GIC_NR_IRQS; i++)
|
||||
r |= (uint32_t)a[off + i] << (8 * i);
|
||||
return r;
|
||||
}
|
||||
|
||||
void bytes_write(uint8_t *a, uint32_t off, unsigned size, uint32_t v,
|
||||
uint8_t mask) {
|
||||
for (unsigned i = 0; i < size && off + i < GIC_NR_IRQS; i++)
|
||||
a[off + i] = (uint8_t)((v >> (8 * i)) & mask);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void gic_reset() {
|
||||
g = Gic();
|
||||
g_dirty = true;
|
||||
for (int id = 0; id < 32; id++)
|
||||
g.target[id] = 0x01; // banked: reads this CPU's own bit
|
||||
for (int id = 0; id < 16; id++)
|
||||
g.enabled[id] = true; // SGIs are always enabled on GIC-400
|
||||
for (int id = 16; id < 32; id++)
|
||||
g.cfg[id] = 0; // PPIs level
|
||||
}
|
||||
|
||||
uint32_t gic_dist_read(uint32_t off, unsigned size) {
|
||||
if (off >= 0x400 && off < 0x400 + GIC_NR_IRQS)
|
||||
return bytes_read(g.prio, off - 0x400, size);
|
||||
if (off >= 0x800 && off < 0x800 + GIC_NR_IRQS) {
|
||||
uint32_t i = off - 0x800;
|
||||
if (i < 32)
|
||||
return bytes_read(g.target, i, size);
|
||||
return bytes_read(g.target, i, size);
|
||||
}
|
||||
uint32_t w = (off & 0x7F) >> 2;
|
||||
switch (off & ~0x7Fu) {
|
||||
case 0x080: return w < GIC_NR_IRQS / 32 ? g.group[w] : 0;
|
||||
case 0x100: case 0x180: return w < GIC_NR_IRQS / 32 ? bits_read(g.enabled, w) : 0;
|
||||
case 0x200: case 0x280: return w < GIC_NR_IRQS / 32 ? pend_read(w) : 0;
|
||||
case 0x300: case 0x380: return w < GIC_NR_IRQS / 32 ? bits_read(g.active, w) : 0;
|
||||
default: break;
|
||||
}
|
||||
if (off >= 0xC00 && off < 0xC00 + GIC_NR_IRQS / 4) {
|
||||
uint32_t base = (off - 0xC00) * 4, r = 0;
|
||||
for (int i = 0; i < 16; i++)
|
||||
r |= (uint32_t)(g.cfg[base + i] & 3) << (2 * i);
|
||||
return r;
|
||||
}
|
||||
switch (off) {
|
||||
case 0x000: return g.dctlr;
|
||||
case 0x004: return (1u << 10) | (3u << 5) | (GIC_NR_IRQS / 32 - 1); // SecExt, 4 CPUs
|
||||
case 0x008: return 0x0200143B; // GIC-400, implementer ARM
|
||||
case 0xFE8: return 0x2B; // PIDR2: GICv2
|
||||
case 0xFD0: return 0x04;
|
||||
case 0xFE0: return 0x90;
|
||||
case 0xFE4: return 0xB4;
|
||||
case 0xFEC: return 0x00;
|
||||
case 0xFF0: return 0x0D;
|
||||
case 0xFF4: return 0xF0;
|
||||
case 0xFF8: return 0x05;
|
||||
case 0xFFC: return 0xB1;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void gic_dist_write(uint32_t off, unsigned size, uint32_t val) {
|
||||
g_dirty = true;
|
||||
if (off >= 0x400 && off < 0x400 + GIC_NR_IRQS) {
|
||||
bytes_write(g.prio, off - 0x400, size, val, 0xF8); // 5 priority bits
|
||||
return;
|
||||
}
|
||||
if (off >= 0x800 && off < 0x800 + GIC_NR_IRQS) {
|
||||
uint32_t i = off - 0x800;
|
||||
if (i >= 32)
|
||||
bytes_write(g.target, i, size, val, 0xFF);
|
||||
return;
|
||||
}
|
||||
if (off >= 0xC00 && off < 0xC00 + GIC_NR_IRQS / 4) {
|
||||
uint32_t base = (off - 0xC00) * 4;
|
||||
for (int i = 0; i < 16; i++)
|
||||
if (base + i >= 16) // SGI configuration is read-only
|
||||
g.cfg[base + i] = (uint8_t)((val >> (2 * i)) & 3);
|
||||
return;
|
||||
}
|
||||
uint32_t w = (off & 0x7F) >> 2;
|
||||
bool inrange = w < GIC_NR_IRQS / 32;
|
||||
switch (off & ~0x7Fu) {
|
||||
case 0x080: if (inrange) g.group[w] = val; return;
|
||||
case 0x100:
|
||||
if (inrange) for (int b = 0; b < 32; b++) if (val & (1u << b)) g.enabled[w * 32 + b] = true;
|
||||
return;
|
||||
case 0x180:
|
||||
if (inrange) for (int b = 0; b < 32; b++)
|
||||
if ((val & (1u << b)) && w * 32 + b >= 16) g.enabled[w * 32 + b] = false;
|
||||
return;
|
||||
case 0x200:
|
||||
if (inrange) for (int b = 0; b < 32; b++) if (val & (1u << b)) g.latched[w * 32 + b] = true;
|
||||
return;
|
||||
case 0x280:
|
||||
if (inrange) for (int b = 0; b < 32; b++) if (val & (1u << b)) g.latched[w * 32 + b] = false;
|
||||
return;
|
||||
case 0x300:
|
||||
if (inrange) for (int b = 0; b < 32; b++) if (val & (1u << b)) g.active[w * 32 + b] = true;
|
||||
return;
|
||||
case 0x380:
|
||||
if (inrange) for (int b = 0; b < 32; b++) if (val & (1u << b)) g.active[w * 32 + b] = false;
|
||||
return;
|
||||
default: break;
|
||||
}
|
||||
switch (off) {
|
||||
case 0x000: g.dctlr = val & 3; return;
|
||||
case 0xF00: { // GICD_SGIR
|
||||
int id = val & 0xF;
|
||||
uint32_t filter = (val >> 24) & 3, targets = (val >> 16) & 0xFF;
|
||||
bool to_me = filter == 2 || (filter == 0 && (targets & 1));
|
||||
if (to_me)
|
||||
g.latched[id] = true;
|
||||
return;
|
||||
}
|
||||
default: return;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t gic_cpu_read(uint32_t off, unsigned size) {
|
||||
(void)size;
|
||||
if (off == 0x0C || off == 0x20)
|
||||
g_dirty = true;
|
||||
switch (off) {
|
||||
case 0x00: return g.cctlr;
|
||||
case 0x04: return g.pmr;
|
||||
case 0x08: return g.bpr;
|
||||
case 0x0C: case 0x20: { // IAR / AIAR: acknowledge
|
||||
if (!(g.dctlr & 3) || !(g.cctlr & 3))
|
||||
return 1023;
|
||||
int id = best();
|
||||
if (id == 1023 || g.prio[id] >= g.pmr || g.prio[id] >= running_prio())
|
||||
return 1023;
|
||||
g.active[id] = true;
|
||||
g.latched[id] = false;
|
||||
if (g.depth < GIC_NR_IRQS)
|
||||
g.stack[g.depth++] = id;
|
||||
return (uint32_t)id; // SGI source CPU field: CPU0, i.e. 0
|
||||
}
|
||||
case 0x14: return (uint32_t)(running_prio() & 0xFF);
|
||||
case 0x18: case 0x28: return (uint32_t)best();
|
||||
case 0x1C: return g.bpr;
|
||||
case 0xFC: return 0x0202143B; // GICC_IIDR, GIC-400
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void gic_cpu_write(uint32_t off, unsigned size, uint32_t val) {
|
||||
(void)size;
|
||||
g_dirty = true;
|
||||
switch (off) {
|
||||
case 0x00: g.cctlr = val & 0x3FF; return;
|
||||
case 0x04: g.pmr = val & 0xF8; return;
|
||||
case 0x08: case 0x1C: g.bpr = val & 7; return;
|
||||
case 0x10: case 0x24: case 0x1000: { // EOIR / AEOIR / DIR
|
||||
int id = (int)(val & 0x3FF);
|
||||
if (id >= GIC_NR_IRQS)
|
||||
return;
|
||||
g.active[id] = false;
|
||||
for (int i = g.depth - 1; i >= 0; i--)
|
||||
if (g.stack[i] == id) {
|
||||
memmove(&g.stack[i], &g.stack[i + 1], (g.depth - i - 1) * sizeof(int));
|
||||
g.depth--;
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
default: return;
|
||||
}
|
||||
}
|
||||
|
||||
void gic_set_spi(int spi, bool level) {
|
||||
int id = 32 + spi;
|
||||
if (spi < 0 || id >= GIC_NR_IRQS)
|
||||
return;
|
||||
if (g.line[id] == level)
|
||||
return;
|
||||
if (level && is_edge(id))
|
||||
g.latched[id] = true; // rising edge of an edge-configured line
|
||||
g.line[id] = level;
|
||||
g_dirty = true;
|
||||
}
|
||||
|
||||
void gic_set_ppi(int id, bool level) {
|
||||
if (id < 16 || id >= 32 || g.line[id] == level)
|
||||
return;
|
||||
g.line[id] = level;
|
||||
g_dirty = true;
|
||||
}
|
||||
|
||||
bool gic_irq_pending() {
|
||||
best();
|
||||
return g_line_up;
|
||||
}
|
||||
|
||||
int gic_highest_pending() { return best(); }
|
||||
|
||||
void gic_describe(int id, char *buf, unsigned n) {
|
||||
if (id < 0 || id >= GIC_NR_IRQS) {
|
||||
snprintf(buf, n, "?");
|
||||
return;
|
||||
}
|
||||
snprintf(buf, n, "dctlr %u cctlr %u pmr %02X id%d en %d line %d latch %d act %d prio %02X depth %d",
|
||||
g.dctlr, g.cctlr, g.pmr, id, g.enabled[id], g.line[id], g.latched[id],
|
||||
g.active[id], g.prio[id], g.depth);
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
#ifndef T210_GIC_H
|
||||
#define T210_GIC_H
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
/*
|
||||
* GIC-400 - the interrupt controller in front of the CCPLEX (Tegra X1 TRM
|
||||
* ch.3; ARM IHI 0048B, GICv2). Distributor at 0x50041000, CPU interface at
|
||||
* 0x50042000 (8 KiB: GICC_DIR is at +0x1000).
|
||||
*
|
||||
* One CPU interface is modelled: CPU0 is the only core the emulator runs.
|
||||
* Security is not: both groups behave as one, which is all a kernel booted
|
||||
* non-secure ever sees. Peripherals drive level lines (gic_set_spi); SGIs
|
||||
* and edge-configured interrupts latch.
|
||||
*/
|
||||
|
||||
constexpr uint64_t GICD_BASE = 0x50041000;
|
||||
constexpr uint64_t GICD_SIZE = 0x1000;
|
||||
constexpr uint64_t GICC_BASE = 0x50042000;
|
||||
constexpr uint64_t GICC_SIZE = 0x2000;
|
||||
|
||||
constexpr int GIC_NR_IRQS = 256; // 32 private + 224 SPIs (ITLinesNumber 7)
|
||||
constexpr int GIC_PPI_VTIMER = 27; // EL1 virtual timer
|
||||
constexpr int GIC_PPI_PTIMER = 30; // EL1 physical (non-secure) timer
|
||||
|
||||
void gic_reset();
|
||||
uint32_t gic_dist_read(uint32_t off, unsigned size);
|
||||
void gic_dist_write(uint32_t off, unsigned size, uint32_t val);
|
||||
uint32_t gic_cpu_read(uint32_t off, unsigned size);
|
||||
void gic_cpu_write(uint32_t off, unsigned size, uint32_t val);
|
||||
|
||||
// A peripheral's interrupt line. `spi` is the SPI number as device trees
|
||||
// write it (<GIC_SPI n ...>); the interrupt ID is 32 + n.
|
||||
void gic_set_spi(int spi, bool level);
|
||||
// A private peripheral interrupt line (ID 16..31) of CPU0.
|
||||
void gic_set_ppi(int id, bool level);
|
||||
|
||||
// CPU0's nIRQ: something enabled, pending, not active and above the
|
||||
// priority mask and the running priority.
|
||||
bool gic_irq_pending();
|
||||
|
||||
// For the log: the interrupt ID that IRQ would deliver, or 1023.
|
||||
int gic_highest_pending();
|
||||
|
||||
// One line of the distributor and CPU interface state for interrupt `id`.
|
||||
void gic_describe(int id, char *buf, unsigned n);
|
||||
|
||||
#endif // T210_GIC_H
|
||||
@@ -108,6 +108,7 @@ constexpr uint64_t MC_SIZE = 0x1000;
|
||||
|
||||
// Display Controller (DC/DSI)
|
||||
constexpr uint64_t DISPLAY_A_BASE = 0x54200000;
|
||||
constexpr uint64_t DISPLAY_B_BASE = 0x54240000; // DC1: feeds SOR1 / the dock
|
||||
constexpr uint64_t DISPLAY_SIZE = 0x40000;
|
||||
|
||||
// DSI Controller
|
||||
|
||||
+282
-72
@@ -8,10 +8,13 @@
|
||||
#include "bpmp.h"
|
||||
#include "ccplex.h"
|
||||
#include "pcie.h"
|
||||
#include "gic.h"
|
||||
#include "dock.h"
|
||||
#include "usb.h"
|
||||
#include "regcache.h"
|
||||
#include "se_engine.h"
|
||||
#include "tegra_bl.h"
|
||||
#include "btpeer.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -251,7 +254,7 @@ struct BtChip {
|
||||
BtPhase phase = BT_PHASE_OFF;
|
||||
bool reg_on = false; // last sampled BT_REG_ON level
|
||||
uint64_t por_us = 0; // emu_usec at the rising edge
|
||||
uint8_t cmd[4 + 255]; // H4 command being assembled, host -> chip
|
||||
uint8_t cmd[5 + 1024]; // H4 command or ACL packet, host -> chip
|
||||
uint32_t cmd_n = 0;
|
||||
};
|
||||
static BtChip bt_chip;
|
||||
@@ -281,6 +284,8 @@ static void bt_uart_sync(EmuState *state) {
|
||||
bt_chip_tick(state);
|
||||
UartPort &up = uart_ports[UART_D];
|
||||
bool cts = bt_radio_alive(state) && bt_chip.phase == BT_PHASE_READY;
|
||||
if (cts)
|
||||
btpeer_tick(state); // the fake controllers' traffic, in guest time
|
||||
if (cts != up.cts) {
|
||||
up.cts = cts;
|
||||
up.msr_delta |= 0x01; // DCTS
|
||||
@@ -317,7 +322,8 @@ static void bt_queue(EmuState *state, const uint8_t *ev, size_t n) {
|
||||
// One complete HCI command has arrived. Broadcom parts run their lower-layer
|
||||
// stack out of on-die ROM, so a bare HCI_Reset is answered long before any
|
||||
// firmware download -- which is the whole reason this probe works at all.
|
||||
static void bt_chip_command(EmuState *state, uint16_t opcode) {
|
||||
static void bt_chip_command(EmuState *state, uint16_t opcode,
|
||||
const uint8_t *p, uint32_t n) {
|
||||
switch (opcode) {
|
||||
case 0x0C03: { // HCI_Reset
|
||||
static const uint8_t cc[] = {0x04, 0x0E, 0x04, 0x01, 0x03, 0x0C, 0x00};
|
||||
@@ -341,7 +347,9 @@ static void bt_chip_command(EmuState *state, uint16_t opcode) {
|
||||
bt_queue(state, cc, sizeof(cc));
|
||||
break;
|
||||
}
|
||||
default: { // Unknown Command, so the H4 stream stays framed either way.
|
||||
default: { // A host's command (btpeer.cpp), or Unknown Command.
|
||||
if (btpeer_command(state, opcode, p, n))
|
||||
break;
|
||||
const uint8_t cc[] = {0x04, 0x0E, 0x04, 0x01, (uint8_t)opcode,
|
||||
(uint8_t)(opcode >> 8), 0x01};
|
||||
bt_queue(state, cc, sizeof(cc));
|
||||
@@ -360,17 +368,32 @@ static void bt_chip_rx(EmuState *state, uint8_t b) {
|
||||
bt_chip.cmd_n = 0;
|
||||
return;
|
||||
}
|
||||
if (bt_chip.cmd_n == 0 && b != 0x01)
|
||||
return; // resync on the H4 command indicator
|
||||
if (bt_chip.cmd_n == 0 && b != 0x01 && b != 0x02)
|
||||
return; // resync on the H4 command or ACL indicator
|
||||
if (bt_chip.cmd_n < sizeof(bt_chip.cmd))
|
||||
bt_chip.cmd[bt_chip.cmd_n++] = b;
|
||||
if (bt_chip.cmd[0] == 0x02) {
|
||||
// ACL: 02 <handle/flags 2> <length 2> <data>, for the fake controllers.
|
||||
if (bt_chip.cmd_n < 5)
|
||||
return;
|
||||
uint32_t len = bt_chip.cmd[3] | (uint32_t)bt_chip.cmd[4] << 8;
|
||||
if (5 + len > sizeof(bt_chip.cmd)) {
|
||||
bt_chip.cmd_n = 0;
|
||||
return;
|
||||
}
|
||||
if (bt_chip.cmd_n < 5 + len)
|
||||
return;
|
||||
bt_chip.cmd_n = 0;
|
||||
btpeer_acl(state, bt_chip.cmd + 1, 4 + len);
|
||||
return;
|
||||
}
|
||||
if (bt_chip.cmd_n < 4)
|
||||
return; // 01 <opcode lo> <opcode hi> <plen>
|
||||
if (bt_chip.cmd_n < 4u + bt_chip.cmd[3])
|
||||
return;
|
||||
uint16_t opcode = (uint16_t)bt_chip.cmd[1] | ((uint16_t)bt_chip.cmd[2] << 8);
|
||||
bt_chip.cmd_n = 0;
|
||||
bt_chip_command(state, opcode);
|
||||
bt_chip_command(state, opcode, bt_chip.cmd + 4, bt_chip.cmd[3]);
|
||||
}
|
||||
|
||||
// BT_REG_ON (PH4) moved. The rising edge is the chip's power-on reset; the
|
||||
@@ -389,6 +412,7 @@ static void bt_reg_on_set(EmuState *state, bool level) {
|
||||
}
|
||||
bt_chip.cmd_n = 0;
|
||||
state->uart_rx_fifo[UART_D].clear();
|
||||
btpeer_reset(state);
|
||||
bt_uart_sync(state);
|
||||
printf("[bt] BT_REG_ON %s (%s radio)\n", level ? "HIGH - POR" : "LOW - off",
|
||||
bt_radio_name(state->bt_radio.load()));
|
||||
@@ -540,12 +564,36 @@ uint32_t gpio_read(EmuState *state, uint64_t addr) {
|
||||
// PH5/BT_HOST_WAKE and PH3/BT_DEV_WAKE are read while the Tegra is
|
||||
// deliberately not driving them, and mirroring OUT there answers 0 to every
|
||||
// question regardless of what is on the board.
|
||||
// Port K (bank 2, port 2): PK4 is the PD controller's alert, active low.
|
||||
// Port CC (bank 7, port 0): CC1 is HPD from the dock. Both read the pin
|
||||
// when it is an input, as in GPIO mode they are.
|
||||
if (offset == 0x238 || offset == 0x730) {
|
||||
uint32_t v = mmio_regs.get(addr - 0x10);
|
||||
uint32_t oe = mmio_regs.get(addr - 0x20);
|
||||
uint32_t bit = offset == 0x238 ? (1u << 4) : (1u << 1);
|
||||
bool level = offset == 0x238 ? bm92_alert_line(state) : dock_hpd(state);
|
||||
if (!(oe & bit))
|
||||
v = level ? (v | bit) : (v & ~bit);
|
||||
return v;
|
||||
}
|
||||
|
||||
if (offset == 0x13C) {
|
||||
uint32_t v = gpio_h_in(state);
|
||||
TRACE("[gpio] R: Port H IN = 0x%02X\n", v);
|
||||
return v;
|
||||
}
|
||||
|
||||
// Port E IN (bank 1, port 0). PE6 is the left Joy-Con rail's attach line,
|
||||
// active low, as PH6 is the right one's: nothing is plugged in, so it reads
|
||||
// high while undriven. Mirroring OUT answered 0, a Joy-Con on the left rail,
|
||||
// and bdk's driver took UART-C over for good. The rest mirror OUT.
|
||||
if (offset == 0x130) {
|
||||
uint32_t v = mmio_regs.get(addr - 0x10);
|
||||
if (!(mmio_regs.get(GPIO_BASE + 0x110) & (1u << 6)))
|
||||
v |= 1u << 6;
|
||||
return v;
|
||||
}
|
||||
|
||||
// Tegra X1 GPIO is organized as 8 banks of 256 bytes; within each
|
||||
// bank a port occupies a 4-byte slot at offsets:
|
||||
// CNF +0x00..0x0F (GPIO vs SPIO mode select per pin)
|
||||
@@ -623,6 +671,13 @@ void gpio_write(EmuState *state, uint64_t addr, uint32_t val) {
|
||||
gpio_h_update(state);
|
||||
sd_card_gpio_update();
|
||||
}
|
||||
// Port V (bank 5, port 1): PV5 drives the PD controller's reset, high =
|
||||
// held. Only once it is a GPIO output: as the SFIO it rests low.
|
||||
if (offset >= 0x500 && offset < 0x600) {
|
||||
uint32_t cnf = mmio_regs.get(GPIO_BASE + 0x504), oe = mmio_regs.get(GPIO_BASE + 0x514);
|
||||
uint32_t out = mmio_regs.get(GPIO_BASE + 0x524);
|
||||
bm92_reset_line(state, (cnf & oe & out & (1u << 5)) != 0);
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== I2C ====================
|
||||
@@ -646,6 +701,8 @@ void gpio_write(EmuState *state, uint64_t addr, uint32_t val) {
|
||||
|
||||
static uint8_t i2c_slave_addr = 0;
|
||||
static uint8_t i2c_reg_addr = 0;
|
||||
// What the last normal-mode read of the BM92T36 returned, for CMD_DATA1/2.
|
||||
static uint8_t bm92_normal_rx[8];
|
||||
// Last full CMD_DATA1 word, committed when I2C_CNFG starts the transfer.
|
||||
static uint32_t i2c_cmd_data1 = 0;
|
||||
|
||||
@@ -798,7 +855,8 @@ static bool i2c_slave_fitted(EmuState *state, bool on_i2c5, uint8_t addr) {
|
||||
}
|
||||
}
|
||||
switch (addr) {
|
||||
case 0x18: // BM92T36 USB-PD
|
||||
case 0x18: // BM92T36 USB-PD: NAKs in reset and while it boots
|
||||
return bm92_present(state);
|
||||
case 0x36: // MAX17050 fuel gauge
|
||||
case 0x4C: // TMP451 thermal
|
||||
case 0x6B: // BQ24193 charger
|
||||
@@ -986,49 +1044,22 @@ struct PacketState {
|
||||
uint8_t rx_buf[64] = {0};
|
||||
uint32_t rx_size = 0;
|
||||
uint32_t rx_pos = 0;
|
||||
uint8_t tx_buf[64] = {0}; // a write's payload, for chips that take
|
||||
// whole messages (the BM92T36)
|
||||
};
|
||||
static PacketState pkt_i2c1;
|
||||
static PacketState pkt_i2c5;
|
||||
// I2C_INT_STATUS per controller (I2C1, I2C5): PACKET_XFER_COMPLETE (7) and
|
||||
// ALL_PACKETS_XFER_COMPLETE (6) latch when a packet finishes, NO_ACK (3)
|
||||
// when nobody answered; write 1 to clear. BUS_CLEAR_DONE (11) always reads
|
||||
// set, as before.
|
||||
static uint32_t i2c_int_latch[2];
|
||||
|
||||
#define I2C_PACKET_PROT_I2C (1u << 4)
|
||||
#define I2C_HEADER_READ (1u << 19)
|
||||
|
||||
static void bm92t36_fill_rx(EmuState *state, uint8_t reg, uint8_t *buf, uint32_t size) {
|
||||
// All multi-byte values are little-endian on the wire (Hekate reassembles
|
||||
// with `(buf[1] << 8) | buf[0]`). FW_TYPE is the exception — Hekate uses
|
||||
// `(buf[0] << 4) | buf[1]` and expects 0x36, so buf[0]=3, buf[1]=6.
|
||||
auto put = [&](uint32_t i, uint8_t v) { if (i < size) buf[i] = v; };
|
||||
switch (reg) {
|
||||
case 0x03: // STATUS1: bit 7 = cable inserted
|
||||
put(0, state->usb_pd_inserted.load() ? 0x80 : 0x00);
|
||||
break;
|
||||
case 0x4B: // FW_TYPE_REG -> VER_36 = 0x36
|
||||
put(0, 0x03); put(1, 0x06);
|
||||
break;
|
||||
case 0x4D: // MAN_ID_REG -> MAN_ROHM = 0x04B5
|
||||
put(0, 0xB5); put(1, 0x04);
|
||||
break;
|
||||
case 0x4E: // DEV_ID_REG -> DEV_BM92T = 0x03B0
|
||||
put(0, 0xB0); put(1, 0x03);
|
||||
break;
|
||||
case 0x08: // READ_PDOS_SRC: byte0 = PDO-bytes count, then 4-byte PDOs
|
||||
case 0x28: { // CURRENT_PDO: same layout, 1 PDO
|
||||
// Synthesize a single Fixed-type PDO from the EmuState values.
|
||||
// pd_object_t bitfields (LSB->MSB): amp:10, volt:10, info:10, type:2.
|
||||
uint32_t amp_lsb = (uint32_t)(state->usb_pd_amperage_ma.load() / 10) & 0x3FF;
|
||||
uint32_t volt_lsb = (uint32_t)(state->usb_pd_voltage_mv.load() / 50) & 0x3FF;
|
||||
uint32_t pdo = amp_lsb | (volt_lsb << 10);
|
||||
put(0, 4);
|
||||
put(1, (uint8_t)(pdo >> 0));
|
||||
put(2, (uint8_t)(pdo >> 8));
|
||||
put(3, (uint8_t)(pdo >> 16));
|
||||
put(4, (uint8_t)(pdo >> 24));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
// The BM92T36 itself is t210/dock.cpp; packet and normal-mode accesses
|
||||
// to I2C1 0x18 are routed there.
|
||||
|
||||
// ==================== I2C_2: Rohm BH1730 ambient light sensor ============
|
||||
//
|
||||
@@ -1096,7 +1127,7 @@ static void packet_populate_rx(EmuState *state, bool on_i2c5, PacketState &p) {
|
||||
p.rx_size = std::min((uint32_t)sizeof(p.rx_buf), p.payload_size);
|
||||
memset(p.rx_buf, 0, sizeof(p.rx_buf));
|
||||
if (!on_i2c5 && p.dev_addr == 0x18) {
|
||||
bm92t36_fill_rx(state, p.reg_addr, p.rx_buf, p.rx_size);
|
||||
bm92_read(state, p.reg_addr, p.rx_buf, p.rx_size);
|
||||
return;
|
||||
}
|
||||
// Every other slave: serve packet-mode reads from the same register models
|
||||
@@ -1133,7 +1164,7 @@ uint32_t i2c_read(EmuState *state, uint64_t addr) {
|
||||
case 0x8C: // I2C_CONFIG_LOAD
|
||||
return 0; // MSTR_CONFIG_LOAD (bit 0) cleared = load complete
|
||||
case 0x68: // I2C_INT_STATUS
|
||||
return (1 << 11); // BUS_CLEAR_DONE (bit 11)
|
||||
return i2c_int_latch[on_i2c5 ? 1 : 0] | (1 << 11);
|
||||
case 0x54: { // I2C_RX_FIFO (packet-mode receive)
|
||||
uint32_t word = 0;
|
||||
uint32_t n = std::min((uint32_t)4, pkt.rx_size - pkt.rx_pos);
|
||||
@@ -1155,9 +1186,15 @@ uint32_t i2c_read(EmuState *state, uint64_t addr) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
case 0x10: // I2C_CMD_DATA2 (bytes 4-7) — no slaves currently need a >4 byte response
|
||||
case 0x10: // I2C_CMD_DATA2 (bytes 4-7): only the PD controller fills it
|
||||
if (!on_i2c5 && i2c_slave_addr == 0x18)
|
||||
return bm92_normal_rx[4] | bm92_normal_rx[5] << 8 | bm92_normal_rx[6] << 16 |
|
||||
(uint32_t)bm92_normal_rx[7] << 24;
|
||||
return 0;
|
||||
case 0x0C: // I2C_CMD_DATA1
|
||||
if (!on_i2c5 && i2c_slave_addr == 0x18)
|
||||
return bm92_normal_rx[0] | bm92_normal_rx[1] << 8 | bm92_normal_rx[2] << 16 |
|
||||
(uint32_t)bm92_normal_rx[3] << 24;
|
||||
// TMP451 SoC/PCB thermal sensor (slave 0x4C on I2C_1).
|
||||
// Hekate reads (per bdk/thermal/tmp451.c) the integer °C from
|
||||
// PCB: 0x00, SoC: 0x01
|
||||
@@ -1502,6 +1539,20 @@ void i2c_write(EmuState *state, uint64_t addr, uint32_t val) {
|
||||
i2c_cnfg_reg[on_i2c5 ? 1 : 0] = val;
|
||||
bool is_write = (val & (1u << 6)) == 0;
|
||||
uint32_t size = ((val >> 1) & 7) + 1;
|
||||
// BM92T36: whole messages, normal mode (up to 8 bytes in CMD_DATA1/2).
|
||||
if ((val & (1u << 9)) && !on_i2c5 && i2c_slave_addr == 0x18 &&
|
||||
i2c_slave_present(state, false, 0x18)) {
|
||||
uint32_t d2 = mmio_regs.get(I2C1_BASE + 0x10);
|
||||
uint8_t b[8];
|
||||
for (int k = 0; k < 4; k++) {
|
||||
b[k] = (uint8_t)(i2c_cmd_data1 >> (8 * k));
|
||||
b[4 + k] = (uint8_t)(d2 >> (8 * k));
|
||||
}
|
||||
if (is_write && size >= 2)
|
||||
bm92_write(state, b, size);
|
||||
else if (!is_write)
|
||||
bm92_read(state, i2c_reg_addr, bm92_normal_rx, size);
|
||||
}
|
||||
if ((val & (1u << 9)) && is_write && size >= 2 && on_i2c5 &&
|
||||
i2c_slave_addr == MAX77620_I2C_ADDR) {
|
||||
max77620_regs_init(state);
|
||||
@@ -1597,6 +1648,8 @@ void i2c_write(EmuState *state, uint64_t addr, uint32_t val) {
|
||||
if (pkt.is_read) {
|
||||
packet_populate_rx(state, on_i2c5, pkt);
|
||||
pkt.hdr_idx = 0;
|
||||
i2c_int_latch[on_i2c5 ? 1 : 0] |= (1u << 7) | (1u << 6) |
|
||||
(i2c_slave_present(state, on_i2c5, pkt.dev_addr) ? 0 : (1u << 3));
|
||||
} else {
|
||||
pkt.hdr_idx = 3;
|
||||
}
|
||||
@@ -1604,6 +1657,8 @@ void i2c_write(EmuState *state, uint64_t addr, uint32_t val) {
|
||||
default:
|
||||
for (int k = 0; k < 4 && pkt.tx_seen < pkt.payload_size; k++) {
|
||||
uint8_t b = (uint8_t)(val >> (8 * k));
|
||||
if (pkt.tx_seen < sizeof(pkt.tx_buf))
|
||||
pkt.tx_buf[pkt.tx_seen] = b;
|
||||
if (pkt.tx_seen == 0)
|
||||
pkt.reg_addr = b; // first payload byte: the register address
|
||||
else
|
||||
@@ -1611,12 +1666,22 @@ void i2c_write(EmuState *state, uint64_t addr, uint32_t val) {
|
||||
(uint8_t)(pkt.reg_addr + pkt.tx_seen - 1), b);
|
||||
pkt.tx_seen++;
|
||||
}
|
||||
if (pkt.tx_seen >= pkt.payload_size)
|
||||
if (pkt.tx_seen >= pkt.payload_size) {
|
||||
pkt.hdr_idx = 0;
|
||||
i2c_int_latch[on_i2c5 ? 1 : 0] |= (1u << 7) | (1u << 6) |
|
||||
(i2c_slave_present(state, on_i2c5, pkt.dev_addr) ? 0 : (1u << 3));
|
||||
if (!on_i2c5 && pkt.dev_addr == 0x18 && pkt.payload_size >= 2 &&
|
||||
i2c_slave_present(state, false, 0x18))
|
||||
bm92_write(state, pkt.tx_buf,
|
||||
std::min<uint32_t>(pkt.payload_size, sizeof(pkt.tx_buf)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0x68: // I2C_INT_STATUS: write 1 to clear
|
||||
i2c_int_latch[on_i2c5 ? 1 : 0] &= ~val;
|
||||
break;
|
||||
case 0x5C: // I2C_FIFO_CONTROL — TX/RX flush at the start of each xfer
|
||||
if (val & 0x3) {
|
||||
pkt.hdr_idx = 0;
|
||||
@@ -1652,15 +1717,15 @@ static constexpr uint32_t kDcIntFrameEndVBlank = (1u << 1) | (1u << 2);
|
||||
|
||||
// Resolve a DC word index to the windows it addresses and the register-file
|
||||
// slot. False for anything that is not a window register.
|
||||
static bool dc_window_reg(const EmuState *state, uint32_t index,
|
||||
static bool dc_window_reg(uint32_t header, uint32_t index,
|
||||
uint32_t *mask, uint32_t *slot) {
|
||||
if (index >= 0x700 && index < 0x800) {
|
||||
*mask = (state->dc_window_header >> 4) & 0xF;
|
||||
*mask = (header >> 4) & 0xF;
|
||||
*slot = index - 0x700;
|
||||
return true;
|
||||
}
|
||||
if (index >= 0x800 && index < 0x840) {
|
||||
*mask = (state->dc_window_header >> 4) & 0xF;
|
||||
*mask = (header >> 4) & 0xF;
|
||||
*slot = 0x100 + (index - 0x800);
|
||||
return true;
|
||||
}
|
||||
@@ -1676,28 +1741,101 @@ static bool dc_window_reg(const EmuState *state, uint32_t index,
|
||||
return false;
|
||||
}
|
||||
|
||||
// The two heads share one model; this is a head's slice of EmuState.
|
||||
struct DcHead {
|
||||
uint64_t base;
|
||||
uint32_t (*win)[dcwin::kRegs];
|
||||
uint32_t (*act)[dcwin::kRegs];
|
||||
uint32_t *header;
|
||||
bool *programmed;
|
||||
char name;
|
||||
};
|
||||
|
||||
static DcHead dc_head(EmuState *s, uint64_t addr) {
|
||||
if (addr >= DISPLAY_B_BASE)
|
||||
return {DISPLAY_B_BASE, s->dcb_win, s->dcb_win_active, &s->dcb_window_header,
|
||||
&s->dcb_programmed, 'B'};
|
||||
return {DISPLAY_A_BASE, s->dc_win, s->dc_win_active, &s->dc_window_header,
|
||||
&s->dc_programmed, 'A'};
|
||||
}
|
||||
|
||||
// Head B's interrupts are real: FRAME_END and V_BLANK latch once per frame
|
||||
// while the head runs (DISPLAY_COMMAND C_DISPLAY), clear on a write of 1,
|
||||
// and drive GIC SPI 74 when enabled - which a kernel's vblank waits need.
|
||||
// Head A keeps reading "a frame has just ended" for the payloads that poll.
|
||||
static uint32_t dcb_int_latch = 0;
|
||||
static uint64_t dcb_last_frame = 0;
|
||||
static bool dcb_running(EmuState *s) {
|
||||
return ((mmio_regs.get(DISPLAY_B_BASE + 0x032 * 4) >> 5) & 3) == 1;
|
||||
}
|
||||
static void dcb_frames(EmuState *s) {
|
||||
if (!dcb_running(s))
|
||||
return;
|
||||
uint64_t f = s->emu_usec / 16667;
|
||||
if (f != dcb_last_frame) {
|
||||
dcb_last_frame = f;
|
||||
dcb_int_latch |= (1u << 1) | (1u << 2);
|
||||
}
|
||||
uint32_t en = mmio_regs.get(DISPLAY_B_BASE + 0x039 * 4);
|
||||
gic_set_spi(74, (dcb_int_latch & en) != 0);
|
||||
}
|
||||
|
||||
// DC_CMD_CONT_SYNCPT_VSYNC (0x028): SYNCPT_VSYNC_ENABLE (bit 8) makes the
|
||||
// head increment host1x syncpoint INDX (7:0) once per frame. A frame counter
|
||||
// that needs no interrupt logic: hekate's panel uses 9, hwtest's TV check 11.
|
||||
static void dc_syncpt_tick(EmuState *s, uint64_t base, uint64_t *last) {
|
||||
uint32_t cmd = mmio_regs.get(base + 0x032 * 4);
|
||||
uint32_t sv = mmio_regs.get(base + 0x028 * 4);
|
||||
uint64_t f = s->emu_usec / 16667;
|
||||
if (((cmd >> 5) & 3) != 1 || !(sv & (1u << 8))) {
|
||||
*last = f;
|
||||
return;
|
||||
}
|
||||
if (f == *last)
|
||||
return;
|
||||
uint64_t reg = HOST1X_BASE + 0x2100 + 0xF80 + 4 * (sv & 0xFF);
|
||||
mmio_regs[reg] = mmio_regs.get(reg) + (uint32_t)(f - *last);
|
||||
*last = f;
|
||||
}
|
||||
|
||||
void display_tick(EmuState *s) {
|
||||
static uint64_t last_a = 0, last_b = 0;
|
||||
dcb_frames(s);
|
||||
dc_syncpt_tick(s, DISPLAY_A_BASE, &last_a);
|
||||
dc_syncpt_tick(s, DISPLAY_B_BASE, &last_b);
|
||||
}
|
||||
|
||||
static void dc_reset(EmuState *state) {
|
||||
memset(state->dc_win, 0, sizeof(state->dc_win));
|
||||
memset(state->dc_win_active, 0, sizeof(state->dc_win_active));
|
||||
for (int w = 0; w < 4; w++)
|
||||
memset(state->dcb_win, 0, sizeof(state->dcb_win));
|
||||
memset(state->dcb_win_active, 0, sizeof(state->dcb_win_active));
|
||||
for (int w = 0; w < 4; w++) {
|
||||
state->dc_win[w][dcwin::BLEND_LAYER] =
|
||||
state->dc_win_active[w][dcwin::BLEND_LAYER] = dcwin::BLEND_LAYER_RESET;
|
||||
state->dcb_win[w][dcwin::BLEND_LAYER] =
|
||||
state->dcb_win_active[w][dcwin::BLEND_LAYER] = dcwin::BLEND_LAYER_RESET;
|
||||
}
|
||||
state->dc_window_header = 0;
|
||||
state->dc_programmed = false;
|
||||
state->dcb_window_header = 0;
|
||||
state->dcb_programmed = false;
|
||||
dcb_int_latch = 0;
|
||||
state->vic_out_addr = 0;
|
||||
state->vic_out_xform = 0;
|
||||
}
|
||||
|
||||
// One line per window whose picture changed (not for moves: bdk slides its
|
||||
// log window in 16 pixels at a time).
|
||||
static void dc_log_window(int w, const uint32_t *r) {
|
||||
static void dc_log_window(char head, int w, const uint32_t *r) {
|
||||
const char *hn = head == 'B' ? " (head B)" : "";
|
||||
if (!(r[dcwin::OPTIONS] & dcwin::WIN_ENABLE)) {
|
||||
printf("[display] window %c off\n", 'A' + w);
|
||||
printf("[display] window %c%s off\n", 'A' + w, hn);
|
||||
return;
|
||||
}
|
||||
static const char *const kKind[4] = {"pitch", "tiled", "block", "?"};
|
||||
uint32_t kind = r[dcwin::SURFACE_KIND];
|
||||
printf("[display] window %c: %ux%u @ 0x%08X, %s", 'A' + w,
|
||||
printf("[display] window %c%s: %ux%u @ 0x%08X, %s", 'A' + w, hn,
|
||||
r[dcwin::SIZE] & 0x1FFF, (r[dcwin::SIZE] >> 16) & 0x1FFF,
|
||||
r[dcwin::START_ADDR], kKind[kind & 3]);
|
||||
if ((kind & 3) == 2)
|
||||
@@ -1708,52 +1846,71 @@ static void dc_log_window(int w, const uint32_t *r) {
|
||||
}
|
||||
|
||||
uint32_t display_read(EmuState *state, uint64_t addr) {
|
||||
uint32_t index = (uint32_t)(addr - DISPLAY_A_BASE) / 4;
|
||||
DcHead h = dc_head(state, addr);
|
||||
uint32_t index = (uint32_t)(addr - h.base) / 4;
|
||||
uint32_t mask, slot;
|
||||
if (dc_window_reg(state, index, &mask, &slot)) {
|
||||
if (dc_window_reg(*h.header, index, &mask, &slot)) {
|
||||
for (int w = 0; w < 4; w++)
|
||||
if (mask & (1u << w))
|
||||
return state->dc_win[w][slot];
|
||||
return h.win[w][slot];
|
||||
return 0;
|
||||
}
|
||||
switch (index) {
|
||||
case kDcCmdIntStatus: return kDcIntFrameEndVBlank;
|
||||
case kDcCmdIntStatus:
|
||||
if (h.name == 'B') {
|
||||
dcb_frames(state);
|
||||
return dcb_int_latch;
|
||||
}
|
||||
return kDcIntFrameEndVBlank;
|
||||
case kDcCmdStateControl: return 0;
|
||||
case kDcCmdWindowHeader: return state->dc_window_header;
|
||||
case kDcCmdWindowHeader: return *h.header;
|
||||
default: return mmio_regs.get(addr);
|
||||
}
|
||||
}
|
||||
|
||||
void display_write(EmuState *state, uint64_t addr, uint32_t val) {
|
||||
uint32_t index = (uint32_t)(addr - DISPLAY_A_BASE) / 4;
|
||||
DcHead h = dc_head(state, addr);
|
||||
uint32_t index = (uint32_t)(addr - h.base) / 4;
|
||||
uint32_t mask, slot;
|
||||
if (dc_window_reg(state, index, &mask, &slot)) {
|
||||
if (dc_window_reg(*h.header, index, &mask, &slot)) {
|
||||
for (int w = 0; w < 4; w++)
|
||||
if (mask & (1u << w))
|
||||
state->dc_win[w][slot] = val;
|
||||
h.win[w][slot] = val;
|
||||
return;
|
||||
}
|
||||
if (h.name == 'B' && index == kDcCmdIntStatus) {
|
||||
dcb_int_latch &= ~val; // write 1 to clear
|
||||
dcb_frames(state);
|
||||
return;
|
||||
}
|
||||
if (h.name == 'B' && index == 0x032) {
|
||||
static uint32_t last = 0;
|
||||
if (((val >> 5) & 3) != ((last >> 5) & 3))
|
||||
printf("[display] head B %s\n", ((val >> 5) & 3) == 1 ? "running (C_DISPLAY)" :
|
||||
((val >> 5) & 3) == 0 ? "stopped" : "one-shot");
|
||||
last = val;
|
||||
}
|
||||
if (index == kDcCmdWindowHeader) {
|
||||
state->dc_window_header = val;
|
||||
*h.header = val;
|
||||
} else if (index == kDcCmdStateControl) {
|
||||
uint32_t req = (val >> 1) & 0xF; // WIN_A..D_ACT_REQ
|
||||
for (int w = 0; w < 4; w++) {
|
||||
if (!(req & (1u << w)))
|
||||
continue;
|
||||
uint32_t *act = state->dc_win_active[w];
|
||||
const uint32_t *asm_ = state->dc_win[w];
|
||||
uint32_t *act = h.act[w];
|
||||
const uint32_t *asm_ = h.win[w];
|
||||
static const uint32_t kShown[] = {
|
||||
dcwin::OPTIONS, dcwin::COLOR_DEPTH, dcwin::SIZE, dcwin::LINE_STRIDE,
|
||||
dcwin::START_ADDR, dcwin::SURFACE_KIND};
|
||||
bool changed = !state->dc_programmed;
|
||||
bool changed = !*h.programmed;
|
||||
for (uint32_t r : kShown)
|
||||
changed |= act[r] != asm_[r];
|
||||
memcpy(act, asm_, sizeof(state->dc_win_active[w]));
|
||||
if (changed)
|
||||
dc_log_window(w, act);
|
||||
dc_log_window(h.name, w, act);
|
||||
}
|
||||
if (req) {
|
||||
state->dc_programmed = true;
|
||||
*h.programmed = true;
|
||||
state->display_dirty = true;
|
||||
}
|
||||
}
|
||||
@@ -3198,6 +3355,8 @@ static constexpr uint32_t TMR_EN = 1u << 31, TMR_PER = 1u << 30;
|
||||
|
||||
static void tmr_write(EmuState *state, uint64_t addr, uint32_t val) {
|
||||
uint32_t off = (uint32_t)(addr - TMR_BASE);
|
||||
if (off >= 0x90 && off < 0xB0) // TMR10..13: CPU clock event timers
|
||||
ccplex_tmr_write(off, val);
|
||||
for (int t = 0; t < 10; t++)
|
||||
if (off == kTmrPtv[t] && (val & TMR_EN))
|
||||
tmr_armed_us[t] = state->emu_usec;
|
||||
@@ -3391,6 +3550,19 @@ static const CarBank *car_bank_of(uint32_t off) {
|
||||
// The EMC clock as CAR sets it (EMC section below).
|
||||
static uint32_t emc_rate_khz();
|
||||
|
||||
#include "car_reset.h"
|
||||
static uint32_t car_trm_reset(uint32_t off) {
|
||||
static uint32_t table[0x800 / 4];
|
||||
static bool built = false;
|
||||
if (!built) {
|
||||
for (const CarReset &r : kCarReset)
|
||||
if (r.off < 0x800)
|
||||
table[r.off / 4] = r.val;
|
||||
built = true;
|
||||
}
|
||||
return off < 0x800 ? table[off / 4] : 0;
|
||||
}
|
||||
|
||||
uint32_t clk_rst_read(EmuState *state, uint64_t addr) {
|
||||
uint32_t offset = (uint32_t)(addr - CLK_RST_BASE);
|
||||
if (const CarBank *b = car_bank_of(offset)) {
|
||||
@@ -3582,8 +3754,11 @@ uint32_t clk_rst_read(EmuState *state, uint64_t addr) {
|
||||
return v;
|
||||
}
|
||||
// Everything else is a plain R/W register (the CLK_SOURCE_* dividers,
|
||||
// PLL MISC words, ...): it reads back what was written.
|
||||
return mmio_regs.get(addr);
|
||||
// PLL MISC words, ...): it reads back what was written, and its TRM reset
|
||||
// value until then (car_reset.h). A kernel's clock driver reads hundreds
|
||||
// of these to find each clock's parent; 0 is a reset value the hardware
|
||||
// never has for many (a super clock's burst policy BUG()s in STANDBY).
|
||||
return mmio_regs.get(addr, car_trm_reset(offset));
|
||||
}
|
||||
|
||||
// A CLK_SOURCE_EMC write is the CAR/EMC clock-change handshake (EMC below).
|
||||
@@ -4669,6 +4844,13 @@ static uint32_t i2c_empty_bus_read(uint64_t address) {
|
||||
|
||||
uint32_t mmio_bus_read(EmuState *state, uint64_t address, unsigned size) {
|
||||
(void)size;
|
||||
// host1x syncpoints: the heads' per-frame increments are brought up to
|
||||
// date before a read, so a payload timing frames inside one BPMP batch
|
||||
// (a paced TIMERUS wait) sees them.
|
||||
if (address >= HOST1X_BASE + 0x3080 && address < HOST1X_BASE + 0x3080 + 0x300) {
|
||||
display_tick(state);
|
||||
return mmio_regs.get(address);
|
||||
}
|
||||
if (address >= TMR_BASE && address < TMR_BASE + TMR_SIZE) {
|
||||
// TIMERUS_CNTR_1US. The rest of the timer block reads 0. The BPMP's
|
||||
// reads go through the clock's poll pacing; CPU0 has its own clock.
|
||||
@@ -4702,6 +4884,14 @@ uint32_t mmio_bus_read(EmuState *state, uint64_t address, unsigned size) {
|
||||
return sysreg_read(state, address);
|
||||
if (address >= BPMP_CACHE_BASE && address < BPMP_CACHE_BASE + BPMP_CACHE_SIZE)
|
||||
return bpmp_cache_read(state, address);
|
||||
if (address >= GICD_BASE && address < GICD_BASE + GICD_SIZE)
|
||||
return gic_dist_read((uint32_t)(address - GICD_BASE), size);
|
||||
if (address >= 0x54040000 && address < 0x54041000)
|
||||
return dpaux1_read(state, (uint32_t)(address - 0x54040000));
|
||||
if (address >= 0x54580000 && address < 0x54581000)
|
||||
return sor1_read(state, (uint32_t)(address - 0x54580000));
|
||||
if (address >= GICC_BASE && address < GICC_BASE + GICC_SIZE)
|
||||
return gic_cpu_read((uint32_t)(address - GICC_BASE), size);
|
||||
if (address >= SOC_THERM_BASE && address < SOC_THERM_BASE + SOC_THERM_SIZE)
|
||||
return soc_therm_read(state, address);
|
||||
if (address >= FLOW_CTLR_BASE && address < FLOW_CTLR_BASE + FLOW_CTLR_SIZE)
|
||||
@@ -4728,7 +4918,7 @@ uint32_t mmio_bus_read(EmuState *state, uint64_t address, unsigned size) {
|
||||
if ((address >= I2C4_BASE && address < I2C4_BASE + I2C_CTRL_SIZE) ||
|
||||
(address >= I2C6_BASE && address < I2C6_BASE + I2C_CTRL_SIZE))
|
||||
return i2c_empty_bus_read(address);
|
||||
if (address >= DISPLAY_A_BASE && address < DISPLAY_A_BASE + DISPLAY_SIZE)
|
||||
if (address >= DISPLAY_A_BASE && address < DISPLAY_B_BASE + DISPLAY_SIZE)
|
||||
return display_read(state, address);
|
||||
if (address >= PMC_BASE && address < PMC_BASE + PMC_SIZE)
|
||||
return pmc_read(state, address);
|
||||
@@ -4873,6 +5063,14 @@ void mmio_bus_write(EmuState *state, uint64_t address, unsigned size,
|
||||
} else if (address >= BPMP_CACHE_BASE &&
|
||||
address < BPMP_CACHE_BASE + BPMP_CACHE_SIZE) {
|
||||
bpmp_cache_write(state, address, val);
|
||||
} else if (address >= 0x54040000 && address < 0x54041000) {
|
||||
dpaux1_write(state, (uint32_t)(address - 0x54040000), val);
|
||||
} else if (address >= 0x54580000 && address < 0x54581000) {
|
||||
sor1_write(state, (uint32_t)(address - 0x54580000), val);
|
||||
} else if (address >= GICD_BASE && address < GICD_BASE + GICD_SIZE) {
|
||||
gic_dist_write((uint32_t)(address - GICD_BASE), size, val);
|
||||
} else if (address >= GICC_BASE && address < GICC_BASE + GICC_SIZE) {
|
||||
gic_cpu_write((uint32_t)(address - GICC_BASE), size, val);
|
||||
} else if (address >= SOC_THERM_BASE &&
|
||||
address < SOC_THERM_BASE + SOC_THERM_SIZE) {
|
||||
soc_therm_write(state, address, val);
|
||||
@@ -4893,7 +5091,7 @@ void mmio_bus_write(EmuState *state, uint64_t address, unsigned size,
|
||||
} else if (address >= I2C5_BASE && address < I2C5_BASE + I2C_CTRL_SIZE) {
|
||||
i2c_write(state, address, val);
|
||||
} else if (address >= DISPLAY_A_BASE &&
|
||||
address < DISPLAY_A_BASE + DISPLAY_SIZE) {
|
||||
address < DISPLAY_B_BASE + DISPLAY_SIZE) {
|
||||
display_write(state, address, val);
|
||||
} else if (address >= DSI_BASE && address < DSI_BASE + DSI_SIZE) {
|
||||
dsi_write(state, address, val);
|
||||
@@ -5008,6 +5206,7 @@ const struct {
|
||||
{EMC1_BASE, EMC_SIZE},
|
||||
{MC_BASE, MC_SIZE},
|
||||
{DISPLAY_A_BASE, DISPLAY_SIZE},
|
||||
{DISPLAY_B_BASE, DISPLAY_SIZE},
|
||||
{DSI_BASE, DSI_SIZE},
|
||||
{I2C2_BASE, I2C2_SIZE},
|
||||
{SOC_THERM_BASE, SOC_THERM_SIZE},
|
||||
@@ -5030,6 +5229,11 @@ const struct {
|
||||
{USB_OTG_BASE, USB_OTG_SIZE},
|
||||
{XUSB_DEV_BASE, XUSB_DEV_SIZE},
|
||||
{MSELECT_BASE, MSELECT_SIZE},
|
||||
// GIC-400 distributor, CPU interface and the virtualisation pages.
|
||||
{GICD_BASE, 0x7000},
|
||||
// The DisplayPort path to the dock: DPAUX1, SOR1, display head B.
|
||||
{0x54040000, 0x1000},
|
||||
{0x54580000, 0x1000},
|
||||
// PCIe. T210 puts the root complex at the BOTTOM of the address map,
|
||||
// below every other peripheral.
|
||||
{PCIE_BLOCK_BASE, PCIE_BLOCK_SIZE},
|
||||
@@ -5100,6 +5304,10 @@ void mmio_map_bus(uc_engine *uc, EmuState *state) {
|
||||
uc_mem_map_ptr(uc, TZRAM_BASE, TZRAM_SIZE, UC_PROT_ALL, g_tzram);
|
||||
}
|
||||
|
||||
bool mmio_map_page(uc_engine *uc, EmuState *state, uint64_t page) {
|
||||
return map_window(uc, state, page & ~0xFFFULL, 0x1000) == UC_ERR_OK;
|
||||
}
|
||||
|
||||
void mmio_unmap_bus(uc_engine *uc) {
|
||||
g_windows.erase(std::remove_if(g_windows.begin(), g_windows.end(),
|
||||
[uc](const std::unique_ptr<MmioWindow> &w) {
|
||||
@@ -5114,6 +5322,8 @@ void mmio_init(uc_engine *uc, EmuState *state) {
|
||||
mmio_map_bus(uc, state);
|
||||
pcie_reset(state);
|
||||
ccplex_reset(state);
|
||||
gic_reset();
|
||||
dock_reset(state);
|
||||
sdhci_reset_regs(sdhci_regs[0]);
|
||||
sdhci_reset_regs(sdhci_regs[1]);
|
||||
dc_reset(state);
|
||||
|
||||
@@ -36,6 +36,11 @@ void mmio_init(uc_engine *uc, EmuState *state);
|
||||
void mmio_map_bus(uc_engine *uc, EmuState *state);
|
||||
// Forget the windows mapped into `uc`, before that engine is closed.
|
||||
void mmio_unmap_bus(uc_engine *uc);
|
||||
// Map one 4 KiB page of the peripheral band as a register window (CPU0's
|
||||
// first access to a block the emulator does not model).
|
||||
bool mmio_map_page(uc_engine *uc, EmuState *state, uint64_t page);
|
||||
// Display head B's frame clock (interrupt latch, GIC line); called often.
|
||||
void display_tick(EmuState *state);
|
||||
|
||||
// The shared dispatch both engines' MMIO callbacks funnel into. `size` is the
|
||||
// access width in bytes (1, 2 or 4).
|
||||
|
||||
+108
-7
@@ -22,6 +22,11 @@
|
||||
// (START STOP UNIT, LoEj) and polls TEST UNIT READY until
|
||||
// the gadget lets go, as Linux does
|
||||
// HID the report descriptor, SET_IDLE, then interrupt IN reports
|
||||
// CDC ACM SET_LINE_CODING (115200 8N1) and GET_LINE_CODING,
|
||||
// SET_CONTROL_LINE_STATE (DTR, RTS), then bulk IN read
|
||||
// continuously and printed as [usb-serial] lines, the
|
||||
// notification endpoint polled every 16 ms, and what a
|
||||
// USBSER script event types sent on bulk OUT
|
||||
//
|
||||
// With --usb-host nbd[:port] (or [usb] nbd_port) the mass storage session
|
||||
// instead locks the medium (PREVENT MEDIUM REMOVAL) and serves the disk over
|
||||
@@ -161,6 +166,7 @@ enum class Step {
|
||||
NbdPrevent, NbdRead, NbdWrite, NbdFlush, NbdTur,
|
||||
MscAllow, MscEject, MscAfterEject, MscEjectWait, NbdServe,
|
||||
HidReport, HidIdle, HidPoll,
|
||||
CdcSetLine, CdcGetLine, CdcSetCtrl, CdcPoll,
|
||||
Idle, Failed,
|
||||
};
|
||||
|
||||
@@ -238,6 +244,11 @@ private:
|
||||
uint8_t iface_class_ = 0, iface_sub_ = 0, iface_proto_ = 0;
|
||||
uint16_t hid_report_len_ = 0;
|
||||
Endpoint bulk_in_, bulk_out_, intr_in_;
|
||||
// A serial port: its data interface, the line being received, the next
|
||||
// notification poll.
|
||||
uint8_t data_iface_ = 0;
|
||||
std::string cdc_line_;
|
||||
uint64_t cdc_notif_us_ = 0;
|
||||
std::string vendor_, product_, serial_;
|
||||
|
||||
// Mass storage.
|
||||
@@ -466,8 +477,15 @@ private:
|
||||
const uint8_t *d = &cfg_[i];
|
||||
size_t n = std::min<size_t>(d[0], cfg_.size() - i);
|
||||
if (d[1] == 4 && n >= 9) { // interface
|
||||
if (!first_iface)
|
||||
if (!first_iface) {
|
||||
// A serial port's data interface, with its bulk endpoints,
|
||||
// follows its communications interface.
|
||||
if (iface_class_ == 0x02 && d[5] == 0x0A) {
|
||||
data_iface_ = d[2];
|
||||
continue;
|
||||
}
|
||||
break; // the first interface is enough
|
||||
}
|
||||
first_iface = false;
|
||||
iface_ = d[2];
|
||||
iface_class_ = d[5];
|
||||
@@ -497,6 +515,8 @@ private:
|
||||
return "mass storage, bulk-only";
|
||||
if (iface_class_ == 0x03)
|
||||
return "HID";
|
||||
if (iface_class_ == 0x02 && iface_sub_ == 0x02)
|
||||
return "CDC ACM serial port";
|
||||
return "a class this host does not drive";
|
||||
}
|
||||
|
||||
@@ -643,7 +663,8 @@ private:
|
||||
}
|
||||
|
||||
case Step::HidPoll:
|
||||
finish(); // polls the endpoint itself
|
||||
case Step::CdcPoll:
|
||||
finish(); // polls the endpoints itself
|
||||
return;
|
||||
|
||||
case Step::NbdServe:
|
||||
@@ -742,6 +763,12 @@ private:
|
||||
control(0x81, 6, 0x2200, iface_, hid_report_len_ ? hid_report_len_ : 256);
|
||||
return go(Step::HidReport);
|
||||
}
|
||||
if (iface_class_ == 0x02 && iface_sub_ == 0x02 && bulk_in_.addr &&
|
||||
bulk_out_.addr) {
|
||||
// As a terminal opening the port: the line coding first.
|
||||
control(0x21, 0x20, 0, iface_, 7, {0x00, 0xC2, 0x01, 0x00, 0, 0, 8});
|
||||
return go(Step::CdcSetLine);
|
||||
}
|
||||
printf("[usb-host] configured; nothing more to do\n");
|
||||
go(Step::Idle);
|
||||
}
|
||||
@@ -1064,6 +1091,80 @@ private:
|
||||
deadline_ = now_us() + kTimeoutUs;
|
||||
return go(Step::HidPoll);
|
||||
|
||||
// ---- CDC ACM ----
|
||||
|
||||
case Step::CdcSetLine:
|
||||
printf("[usb-host] SET_LINE_CODING 115200 8N1 %s\n",
|
||||
ctl_.stalled ? "stalled" : "done");
|
||||
control(0xA1, 0x21, 0, iface_, 7); // GET_LINE_CODING
|
||||
return go(Step::CdcGetLine);
|
||||
|
||||
case Step::CdcGetLine: {
|
||||
const std::vector<uint8_t> &d = ctl_.data;
|
||||
if (ctl_.stalled || d.size() < 7)
|
||||
printf("[usb-host] GET_LINE_CODING %s\n",
|
||||
ctl_.stalled ? "stalled" : "came back short");
|
||||
else
|
||||
printf("[usb-host] GET_LINE_CODING: %u baud, %u data bits, stop %u, "
|
||||
"parity %u\n", d[0] | d[1] << 8 | d[2] << 16 | (uint32_t)d[3] << 24,
|
||||
d[6], d[4], d[5]);
|
||||
control(0x21, 0x22, 0x03, iface_, 0); // SET_CONTROL_LINE_STATE
|
||||
return go(Step::CdcSetCtrl);
|
||||
}
|
||||
|
||||
case Step::CdcSetCtrl:
|
||||
printf("[usb-host] SET_CONTROL_LINE_STATE DTR RTS %s; reading EP %02X "
|
||||
"(data interface %u)\n", ctl_.stalled ? "stalled" : "done",
|
||||
bulk_in_.addr, data_iface_);
|
||||
cdc_line_.clear();
|
||||
cdc_notif_us_ = 0;
|
||||
return go(Step::CdcPoll);
|
||||
|
||||
case Step::CdcPoll: {
|
||||
// What the device sends, a line at a time.
|
||||
uint8_t buf[512];
|
||||
bool short_pkt = false;
|
||||
int n = dc_->in(bulk_in_.addr & 0xF, buf,
|
||||
std::min<int>(bulk_in_.max_packet, sizeof(buf)), &short_pkt);
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (buf[i] == '\n') {
|
||||
printf("[usb-serial] %s\n", cdc_line_.c_str());
|
||||
cdc_line_.clear();
|
||||
} else if (buf[i] >= 0x20 && buf[i] < 0x7F) {
|
||||
cdc_line_ += (char)buf[i];
|
||||
}
|
||||
}
|
||||
if (n > 0)
|
||||
progressed_ = true;
|
||||
// What the script types, once the device has a read queued.
|
||||
std::deque<uint8_t> &tx = g_state->usb_serial_rx;
|
||||
if (!tx.empty()) {
|
||||
uint8_t out[512];
|
||||
int len = (int)std::min<size_t>(tx.size(),
|
||||
std::min<size_t>(bulk_out_.max_packet, sizeof(out)));
|
||||
std::copy(tx.begin(), tx.begin() + len, out);
|
||||
int m = dc_->out(bulk_out_.addr & 0xF, out, len);
|
||||
if (m > 0) {
|
||||
tx.erase(tx.begin(), tx.begin() + m);
|
||||
printf("[usb-host] %d byte(s) to EP %02X\n", m, bulk_out_.addr);
|
||||
progressed_ = true;
|
||||
}
|
||||
}
|
||||
// The notification endpoint, polled as a PC polls it. This device
|
||||
// never sends on it, so a NAK is the answer expected.
|
||||
if (intr_in_.addr && now_us() >= cdc_notif_us_) {
|
||||
cdc_notif_us_ = now_us() + 16000;
|
||||
uint8_t nb[16];
|
||||
bool sp = false;
|
||||
int k = dc_->in(intr_in_.addr & 0xF, nb, sizeof(nb), &sp);
|
||||
if (k >= 0)
|
||||
printf("[usb-host] serial state notification, %d byte(s)\n", k);
|
||||
else if (k == XFER_STALL)
|
||||
printf("[usb-host] the notification endpoint stalled\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
case Step::HidPoll: {
|
||||
uint8_t buf[512];
|
||||
bool short_pkt = false;
|
||||
@@ -1419,7 +1520,7 @@ Usb2d g_usb2d;
|
||||
// The device side of an xHCI controller, as bdk's xusbd.c drives it on
|
||||
// Mariko (register layout in usb_t210.h, TRB and context layouts in xusbd.c).
|
||||
// Endpoints are numbered as device context indexes: 0 is the control
|
||||
// endpoint, 2 and 3 bulk OUT and IN. Each has a context (16 words at ECP +
|
||||
// endpoint, 2n and 2n + 1 endpoint n's OUT and IN. Each has a context (16 words at ECP +
|
||||
// 64 * index: word 2 holds its transfer ring's dequeue pointer and DCS) and a
|
||||
// ring of 16-byte TRBs the driver fills - Data and Status TRBs on the control
|
||||
// ring, Normal TRBs on the bulk ones, a Link TRB closing each ring - ringing
|
||||
@@ -1483,7 +1584,7 @@ public:
|
||||
reg(off) &= ~val;
|
||||
return;
|
||||
case X_EP_RELOAD:
|
||||
for (int i = 0; i < 4; i++)
|
||||
for (int i = 0; i < 6; i++)
|
||||
if (val & (1u << i)) {
|
||||
load_ring(i);
|
||||
UTRACE("reload ep %d: ring %08X dcs %u\n", i, rings_[i].deq,
|
||||
@@ -1546,7 +1647,7 @@ public:
|
||||
bool setup_taken() override { return true; }
|
||||
|
||||
int in(int ep, uint8_t *buf, int max, bool *short_pkt) override {
|
||||
int dci = ep ? 3 : 0;
|
||||
int dci = ep ? ep * 2 + 1 : 0;
|
||||
int n = transfer(dci, true, buf, max);
|
||||
if (n >= 0) {
|
||||
// As on the USB2 controller, a TRB that moved less than the host asked
|
||||
@@ -1562,7 +1663,7 @@ public:
|
||||
}
|
||||
|
||||
int out(int ep, const uint8_t *buf, int len) override {
|
||||
return transfer(ep ? 2 : 0, false, const_cast<uint8_t *>(buf), len);
|
||||
return transfer(ep ? ep * 2 : 0, false, const_cast<uint8_t *>(buf), len);
|
||||
}
|
||||
|
||||
uint32_t address() override { return (reg(X_CTRL) >> 24) & 0x7F; }
|
||||
@@ -1579,7 +1680,7 @@ private:
|
||||
uint32_t deq = 0;
|
||||
uint32_t dcs = 0;
|
||||
bool valid = false;
|
||||
} rings_[4];
|
||||
} rings_[6]; // endpoints 0 to 2
|
||||
|
||||
uint32_t ®(uint32_t off) { return regs_[(off % kSize) / 4]; }
|
||||
|
||||
|
||||
Executable
+34
@@ -0,0 +1,34 @@
|
||||
#!/bin/sh
|
||||
# Builds tests/kernelcpu/payload.bin: cpu0.S with an AArch64 assembler,
|
||||
# embedded as a word array into the BPMP payload built with arm-none-eabi.
|
||||
set -e
|
||||
D=$(dirname "$0")
|
||||
# devkitPro when it is installed, else the distribution's cross compilers
|
||||
# (gcc-aarch64-linux-gnu, gcc-arm-none-eabi), as on CI.
|
||||
DKP=${DEVKITPRO:-/opt/devkitpro}
|
||||
if [ -z "$A64" ]; then
|
||||
A64=aarch64-linux-gnu-
|
||||
if [ -x "$DKP/devkitA64/bin/aarch64-none-elf-gcc" ]; then A64=$DKP/devkitA64/bin/aarch64-none-elf-; fi
|
||||
fi
|
||||
if [ -z "$ARM" ]; then
|
||||
ARM=arm-none-eabi-
|
||||
if [ -x "$DKP/devkitARM/bin/arm-none-eabi-gcc" ]; then ARM=$DKP/devkitARM/bin/arm-none-eabi-; fi
|
||||
fi
|
||||
${A64}gcc -c -o "$D/cpu0.o" "$D/cpu0.S"
|
||||
${A64}ld -Ttext=0xA0000000 -o "$D/cpu0.elf" "$D/cpu0.o"
|
||||
${A64}objcopy -O binary "$D/cpu0.elf" "$D/cpu0.bin"
|
||||
python3 - "$D/cpu0.bin" "$D/cpu0_blob.h" <<'PY'
|
||||
import sys, struct
|
||||
b = open(sys.argv[1], "rb").read()
|
||||
b += b"\0" * (-len(b) % 4)
|
||||
w = struct.unpack("<%dI" % (len(b) // 4), b)
|
||||
with open(sys.argv[2], "w") as f:
|
||||
f.write("/* Generated by build.sh from cpu0.S. */\nstatic const u32 cpu0_blob[] = {\n")
|
||||
for i in range(0, len(w), 6):
|
||||
f.write(" " + ", ".join("0x%08x" % x for x in w[i:i + 6]) + ",\n")
|
||||
f.write("};\n")
|
||||
PY
|
||||
${ARM}gcc -march=armv4t -marm -Os -ffreestanding -nostdlib -fno-builtin -I"$D" \
|
||||
-T "$D/link.ld" -o "$D/payload.elf" "$D/payload.c"
|
||||
${ARM}objcopy -O binary "$D/payload.elf" "$D/payload.bin"
|
||||
echo "built $D/payload.bin"
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* CPU0 side of the kernel-CPU test: what a kernel needs from the core
|
||||
* model, exercised the way Linux uses it.
|
||||
*
|
||||
* Entered at EL3, MMU off, at 0xA0000000. Drops to EL1h the way a boot
|
||||
* shim hands over to a kernel, then:
|
||||
* [1] CurrentEL at EL1 (expect 0x4)
|
||||
* [2] PSCI_VERSION over SMC (expect 0x00010000)
|
||||
* [3] ESR_EL1 seen by the BRK handler (expect 0xF2000123)
|
||||
* [4] CNTFRQ_EL0 (expect 19200000)
|
||||
* [5] virtual timer interrupts taken (expect 10)
|
||||
* [6] CNTVCT ticks across them (expect about 10 x 19200)
|
||||
* [7] the last interrupt ID acknowledged (expect 27)
|
||||
* [8] instructions spent waiting: the WFI loop count (expect small)
|
||||
* [0] magic 'KCPU', written last
|
||||
* then parks in WFE.
|
||||
*/
|
||||
.equ MBOX, 0xA0030000
|
||||
.equ GICD, 0x50041000
|
||||
.equ GICC, 0x50042000
|
||||
.equ TICK, 19200 /* 1 ms at 19.2 MHz */
|
||||
|
||||
.text
|
||||
.global _start
|
||||
_start:
|
||||
mov x0, #0x431 /* SCR_EL3: RW, RES1, NS */
|
||||
msr scr_el3, x0
|
||||
mov x0, #(1 << 31) /* HCR_EL2.RW: EL1 is AArch64 */
|
||||
msr hcr_el2, x0
|
||||
mov x0, #3
|
||||
msr cnthctl_el2, x0
|
||||
msr cntvoff_el2, xzr
|
||||
ldr x0, =0x30C50830 /* SCTLR_EL1: RES1, MMU off */
|
||||
msr sctlr_el1, x0
|
||||
mov x0, #0x3C5 /* EL1h, DAIF masked */
|
||||
msr spsr_el3, x0
|
||||
adr x0, el1
|
||||
msr elr_el3, x0
|
||||
eret
|
||||
|
||||
el1:
|
||||
adr x0, vectors
|
||||
msr vbar_el1, x0
|
||||
isb
|
||||
ldr x1, =MBOX
|
||||
mrs x2, currentel
|
||||
str w2, [x1, #4]
|
||||
|
||||
ldr x0, =0x84000000 /* PSCI_VERSION */
|
||||
smc #0
|
||||
str w0, [x1, #8]
|
||||
|
||||
brk #0x123 /* the sync handler steps over it */
|
||||
|
||||
mrs x3, cntfrq_el0
|
||||
str w3, [x1, #16]
|
||||
|
||||
ldr x4, =GICD
|
||||
mov w5, #1
|
||||
str w5, [x4] /* GICD_CTLR */
|
||||
mov w5, #(1 << 27)
|
||||
str w5, [x4, #0x100] /* ISENABLER0: PPI 27 */
|
||||
ldr w5, [x4, #0x418]
|
||||
and w5, w5, #0x00FFFFFF
|
||||
mov w12, #0xA0000000
|
||||
orr w5, w5, w12
|
||||
str w5, [x4, #0x418] /* IPRIORITYR[27] = 0xA0 */
|
||||
ldr x6, =GICC
|
||||
mov w5, #0xF0
|
||||
str w5, [x6, #4] /* GICC_PMR */
|
||||
mov w5, #1
|
||||
str w5, [x6] /* GICC_CTLR */
|
||||
|
||||
mov w7, #TICK
|
||||
msr cntv_tval_el0, x7
|
||||
mov w7, #1
|
||||
msr cntv_ctl_el0, x7
|
||||
mrs x8, cntvct_el0
|
||||
mov w11, #0
|
||||
msr daifclr, #2 /* IRQs on */
|
||||
1: wfi
|
||||
add w11, w11, #1
|
||||
ldr w9, [x1, #20]
|
||||
cmp w9, #10
|
||||
b.lo 1b
|
||||
msr daifset, #2
|
||||
mrs x10, cntvct_el0
|
||||
sub x10, x10, x8
|
||||
str w10, [x1, #24]
|
||||
str w11, [x1, #32]
|
||||
mov w7, #0
|
||||
msr cntv_ctl_el0, x7
|
||||
|
||||
ldr w2, =0x4B435055 /* 'KCPU' */
|
||||
str w2, [x1]
|
||||
2: wfe
|
||||
b 2b
|
||||
|
||||
.ltorg
|
||||
|
||||
.balign 2048
|
||||
vectors:
|
||||
.skip 0x200 /* current EL with SP_EL0: unused */
|
||||
/* 0x200: synchronous, current EL with SP_ELx */
|
||||
mrs x20, esr_el1
|
||||
str w20, [x1, #12]
|
||||
mrs x21, elr_el1
|
||||
add x21, x21, #4
|
||||
msr elr_el1, x21
|
||||
eret
|
||||
.balign 0x80
|
||||
/* 0x280: IRQ, current EL with SP_ELx */
|
||||
ldr w22, [x6, #0x0C] /* GICC_IAR */
|
||||
ldr w23, [x1, #20]
|
||||
add w23, w23, #1
|
||||
str w23, [x1, #20]
|
||||
str w22, [x1, #28]
|
||||
mov w24, #TICK
|
||||
msr cntv_tval_el0, x24
|
||||
str w22, [x6, #0x10] /* GICC_EOIR */
|
||||
eret
|
||||
.balign 0x80
|
||||
.skip 0x80 * 2 /* FIQ, SError */
|
||||
.skip 0x400 /* lower ELs: unused */
|
||||
@@ -0,0 +1,10 @@
|
||||
/* Loaded and entered at 0x40010000, like any RCM payload. */
|
||||
ENTRY(_start)
|
||||
SECTIONS {
|
||||
. = 0x40010000;
|
||||
.text : { KEEP(*(.text.start)) *(.text*) }
|
||||
.rodata : { *(.rodata*) }
|
||||
.data : { *(.data*) }
|
||||
.bss : { *(.bss*) *(COMMON) }
|
||||
/DISCARD/ : { *(.comment) *(.note*) *(.ARM.attributes) *(.ARM.exidx*) }
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Kernel-CPU regression payload: boots CPU0 at cpu0.S and reports its mailbox.
|
||||
*
|
||||
* A freestanding BPMP payload (ARM mode, loaded at 0x40010000 like any RCM
|
||||
* payload) that drives the emulator's CPU0 model through the same register
|
||||
* sequence as bdk's ccplex_boot_cpu0(), and reports what happened on UART-B.
|
||||
* tests/ccplex/run.sh checks the emulator's log for the expected lines.
|
||||
*
|
||||
* 1. Release CPU0 from reset with nothing powered. The core must NOT run,
|
||||
* and the emulator must say which precondition is missing.
|
||||
* 2. Do the real bring-up - CPU rail (MAX77620 GPIO5 + MAX77621 VOUT on
|
||||
* I2C5), CRAIL/C0NC/CE0 partitions, CPUG + MSELECT clocks, MSELECT out
|
||||
* of reset, PLLX, RAM repair, SB_AA64_RESET_LOW - then release it. An
|
||||
* AArch64 blob copied to DRAM records CurrentEL and a TIMERUS delta in
|
||||
* a mailbox, writes a magic word last, and parks in WFE.
|
||||
* Released first with its clock stopped (CLK_CPUG_CMPLX), it must
|
||||
* stay still until the stop is cleared.
|
||||
* 3. Put CPU0 back into reset and power the console off.
|
||||
*
|
||||
* Built by `make test` with arm-none-eabi-gcc; no other toolchain needed, as
|
||||
* the AArch64 side is a handful of pre-assembled words.
|
||||
*/
|
||||
|
||||
typedef unsigned int u32;
|
||||
|
||||
#define REG(a) (*(volatile u32 *)(a))
|
||||
|
||||
#define TIMERUS 0x60005010u
|
||||
#define CAR 0x60006000u
|
||||
#define FLOW 0x60007000u
|
||||
#define SB 0x6000C200u
|
||||
#define UARTB_THR 0x70006040u
|
||||
#define PMC 0x7000E400u
|
||||
#define I2C5 0x7000D000u
|
||||
|
||||
#define CPU_ENTRY 0xA0000000u
|
||||
#define MBOX 0xA0030000u
|
||||
#define KCPU_MAGIC 0x4B435055u
|
||||
|
||||
static void putc_(char c) { REG(UARTB_THR) = (u32)(unsigned char)c; }
|
||||
|
||||
static void puts_(const char *s) {
|
||||
while (*s)
|
||||
putc_(*s++);
|
||||
}
|
||||
|
||||
static void puthex(u32 v) {
|
||||
for (int i = 28; i >= 0; i -= 4)
|
||||
putc_("0123456789ABCDEF"[(v >> i) & 0xF]);
|
||||
}
|
||||
|
||||
static void udelay(u32 us) {
|
||||
u32 start = REG(TIMERUS);
|
||||
while (REG(TIMERUS) - start < us)
|
||||
;
|
||||
}
|
||||
|
||||
/* bdk-style normal-mode I2C write: [reg, val] in CMD_DATA1, 2 bytes, GO. */
|
||||
static void i2c5_write(u32 dev, u32 reg, u32 val) {
|
||||
REG(I2C5 + 0x04) = dev << 1; /* CMD_ADDR0 */
|
||||
REG(I2C5 + 0x0C) = reg | (val << 8); /* CMD_DATA1 */
|
||||
REG(I2C5 + 0x00) = (1u << 1) | (1u << 9); /* CNFG: 2 bytes, write, GO */
|
||||
}
|
||||
|
||||
static void pwrgate_on(u32 part) {
|
||||
if (!(REG(PMC + 0x38) & (1u << part))) /* PWRGATE_STATUS */
|
||||
REG(PMC + 0x30) = part | (1u << 8); /* PWRGATE_TOGGLE */
|
||||
}
|
||||
|
||||
#define RST_CPU0 ((1u << 30) | (1u << 24) | (1u << 16) | (1u << 0))
|
||||
#define RST_NONCPU (1u << 29)
|
||||
|
||||
static void release_cpu0(void) {
|
||||
REG(SB + 0x30) = CPU_ENTRY | 1u; /* SB_AA64_RESET_LOW */
|
||||
REG(SB + 0x34) = 0; /* SB_AA64_RESET_HIGH */
|
||||
REG(CAR + 0x454) = RST_NONCPU; /* RST_CPUG_CMPLX_CLR */
|
||||
REG(CAR + 0x454) = RST_CPU0;
|
||||
}
|
||||
|
||||
static void hold_cpu0(void) {
|
||||
REG(CAR + 0x450) = RST_CPU0; /* RST_CPUG_CMPLX_SET */
|
||||
REG(CAR + 0x450) = RST_NONCPU;
|
||||
}
|
||||
|
||||
|
||||
#include "cpu0_blob.h"
|
||||
|
||||
__attribute__((section(".text.start"), noreturn))
|
||||
void _start(void) {
|
||||
volatile u32 *mb = (volatile u32 *)MBOX;
|
||||
|
||||
puts_("kernelcpu test: start\n");
|
||||
for (u32 i = 0; i < sizeof(cpu0_blob) / sizeof(cpu0_blob[0]); i++)
|
||||
((volatile u32 *)CPU_ENTRY)[i] = cpu0_blob[i];
|
||||
for (u32 i = 0; i < 16; i++)
|
||||
mb[i] = 0;
|
||||
|
||||
/* The bring-up, in ccplex_boot_cpu0() order (Erista). */
|
||||
i2c5_write(0x3C, 0x40, 0x1C); /* MAX77620 AME_GPIO: GPIO5 plain */
|
||||
i2c5_write(0x3C, 0x3B, 0x09); /* GPIO5: push-pull, out high */
|
||||
i2c5_write(0x1B, 0x00, 0x80 | 55); /* MAX77621 VOUT: on, 0.950 V */
|
||||
REG(CAR + 0x0E0) = (1u << 30) | (2u << 20) | (156u << 8) | 2u; /* PLLX */
|
||||
REG(CAR + 0x3B4) = 4u << 1; /* CLK_SOURCE_MSELECT */
|
||||
REG(CAR + 0x440) = 1u << 3; /* CLK_ENB_V_SET: MSELECT */
|
||||
REG(CAR + 0x020) = 0x20008888; /* CCLK_BURST: PLLX_OUT0_LJ */
|
||||
REG(CAR + 0x024) = 1u << 31; /* SUPER_CCLK_DIVIDER */
|
||||
REG(CAR + 0x440) = 1u << 0; /* CLK_ENB_V_SET: CPUG */
|
||||
pwrgate_on(0); /* CRAIL */
|
||||
pwrgate_on(15); /* C0NC */
|
||||
pwrgate_on(14); /* CE0 */
|
||||
REG(FLOW + 0x40) = 1; /* RAM_REPAIR_REQ */
|
||||
while (!(REG(FLOW + 0x40) & 2))
|
||||
;
|
||||
REG(CAR + 0x434) = 1u << 3; /* RST_DEV_V_CLR: MSELECT */
|
||||
release_cpu0();
|
||||
|
||||
u32 start = REG(TIMERUS);
|
||||
while (mb[0] != KCPU_MAGIC && REG(TIMERUS) - start < 200000)
|
||||
udelay(100);
|
||||
static const char *names[9] = { "magic", "el", "psci", "esr", "cntfrq",
|
||||
"irqs", "ticks", "irqid", "wfis" };
|
||||
for (u32 i = 0; i < 9; i++) {
|
||||
puts_(names[i]);
|
||||
putc_('=');
|
||||
puthex(mb[i]);
|
||||
putc_(i == 8 ? '\n' : ' ');
|
||||
}
|
||||
hold_cpu0();
|
||||
puts_("kernelcpu test: done\n");
|
||||
i2c5_write(0x3C, 0x41, 0x02); /* MAX77620 ONOFFCNFG1: PWR_OFF */
|
||||
for (;;)
|
||||
;
|
||||
}
|
||||
Executable
+53
@@ -0,0 +1,53 @@
|
||||
#!/bin/sh
|
||||
# Kernel-CPU regression test: CPU0 dropped to EL1 takes timer interrupts
|
||||
# through the GIC, a BRK into its own vector, and a PSCI call over SMC.
|
||||
# Usage: tests/kernelcpu/run.sh [rcm_emu] [payload]
|
||||
EMU=${1:-./rcm_emu}
|
||||
PAYLOAD=${2:-tests/kernelcpu/payload.bin}
|
||||
LOG=${LOG:-tests/kernelcpu/out.log}
|
||||
|
||||
SDL_VIDEODRIVER=dummy RCM_EMU_NO_AUDIO=1 timeout 120 "$EMU" "$PAYLOAD" --oem erista \
|
||||
> "$LOG" 2>&1
|
||||
status=$?
|
||||
|
||||
fail=0
|
||||
expect() {
|
||||
if grep -q -- "$1" "$LOG"; then
|
||||
echo " ok $2"
|
||||
else
|
||||
echo " FAIL $2 (missing: $1)"
|
||||
fail=1
|
||||
fi
|
||||
}
|
||||
|
||||
echo "kernelcpu test (emulator exit $status):"
|
||||
expect 'magic=4B435055' 'CPU0 ran the whole test and wrote the mailbox'
|
||||
expect 'el=00000004' 'the shim dropped it to EL1'
|
||||
expect 'psci=00010000' 'PSCI_VERSION over SMC answers 1.0'
|
||||
expect 'esr=F2000123' 'BRK is taken to the EL1 vector with its ESR'
|
||||
expect 'cntfrq=0124F800' 'the counter runs at 19.2 MHz'
|
||||
expect 'irqs=0000000A' 'ten virtual timer interrupts arrive through the GIC'
|
||||
expect 'irqid=0000001B' 'the GIC acknowledges them as PPI 27'
|
||||
|
||||
ticks=$(sed -n 's/.* ticks=\([0-9A-F]*\).*/\1/p' "$LOG" | head -1)
|
||||
if [ -n "$ticks" ] && [ $((0x$ticks)) -ge 190000 ] && [ $((0x$ticks)) -le 200000 ]; then
|
||||
echo " ok ten 1 ms ticks take about 192000 counts (0x$ticks)"
|
||||
else
|
||||
echo " FAIL the tick count across ten interrupts is off (0x${ticks:-?})"
|
||||
fail=1
|
||||
fi
|
||||
retired=$(sed -n 's/.*CPU0 stopped (held in reset) after \([0-9]*\) instructions.*/\1/p' "$LOG" | head -1)
|
||||
if [ -n "$retired" ] && [ "$retired" -lt 5000 ]; then
|
||||
echo " ok WFI sleeps until the timer ($retired instructions retired)"
|
||||
else
|
||||
echo " FAIL WFI spun instead of sleeping (retired: ${retired:-?})"
|
||||
fail=1
|
||||
fi
|
||||
if [ "$status" -eq 0 ]; then
|
||||
echo " ok the emulator exited cleanly"
|
||||
else
|
||||
echo " FAIL the emulator exited with status $status (124 = timed out)"
|
||||
fail=1
|
||||
fi
|
||||
[ $fail -eq 0 ] && echo "kernelcpu test: PASS" || echo "kernelcpu test: FAIL"
|
||||
exit $fail
|
||||
Reference in new issue
Block a user