Skip to content

soc: apple: sep: unmap the firmware IOVA, not the physical address - #592

Open
brentkearney wants to merge 39 commits into
AsahiLinux:asahi-wipfrom
brentkearney:sep-unmap-iova
Open

soc: apple: sep: unmap the firmware IOVA, not the physical address#592
brentkearney wants to merge 39 commits into
AsahiLinux:asahi-wipfrom
brentkearney:sep-unmap-iova

Conversation

@brentkearney

@brentkearney brentkearney commented Sep 2, 2026

Copy link
Copy Markdown

Unmap the IOVA that dma_map_resource() returned instead of the region's physical address, so unbinding apple_sep stops warning twice in the IOMMU and stops leaking the SEP firmware mapping. Closes #591.

Changes

  • Add fw_iova: Atomic<u64> to SepData, storing the return value of dma_map_resource() next to the existing fw_mapped flag.
  • Pass fw_iova instead of region_params.addr to dma_unmap_resource() in the unbind path.
  • Leave the bind and boot paths unchanged.

Testing

Compile-tested against asahi-7.1.6-1 with rustc 1.93.1, and runtime-tested on a MacBook Pro 16-inch 2021 (apple,j316s / apple,t6000) with the sep node enabled:

  • drivers/soc/apple/sep.o builds before and after, CLIPPY=1 introduces no new warnings, and rustfmt --check passes.
  • On the stock kernel, echo 396400000.sep > /sys/bus/platform/drivers/apple_sep/unbind produces the two WARNs in apple_sep: unbinding warns twice and leaks the firmware IOMMU mapping #591 and sets the W taint bit.
  • On the patched kernel, the same unbind logs nothing: dmesg | grep -c WARNING and dmesg | grep -c dart_unmap both return 0, and the W taint bit (512) stays clear.
  • On the patched kernel, the device's driver link is gone afterwards, so remove() ran to completion.

marcan and others added 30 commits August 30, 2026 10:36
Apple M2 devices expose the multi-touch device over the HID over
DockChannel transport, which we represent as the HOST bus type. The
report format is the same, except the legacy mouse header is gone and
there is no enable request needed.

Signed-off-by: Hector Martin <marcan@marcan.st>
The trackpad has to request multi touch reports during resume.

Signed-off-by: Janne Grunau <j@jannau.net>
On at least some SPI devices (e.g. recent Apple Silicon machines), the
Broadcom touch controller is prone to crashing. When this happens, the
STM eventually notices and resets it. It then notifies the driver via
HID report 0x60, and the driver needs to re-enable MT mode to make
things work again.

This poses an additional issue: the hidinput core will close the
low-level transport while the device is closed, which can cause us to
miss a reset notification. To fix this, override the input open/close
callbacks and send the MT enable every time the HID device is opened,
instead of only once on probe. This should increase general robustness,
even if the reset mechanism doesn't work for some reason, so it's worth
doing it for USB devices too. MTP devices are exempt since they do not
require the MT enable at all.

Signed-off-by: Hector Martin <marcan@marcan.st>
For SPI/MTP trackpads, query the dimensions via HID report instead of
hardcoding values.

TODO: Does this work for the USB/BT devices? Maybe we can get rid of the
hardcoded sizes everywhere?

Signed-off-by: Hector Martin <marcan@marcan.st>
Keyboard and trackpad of Apple Sillicon SoCs (M1, M1 Pro/Max) laptops
are are HID devices connected via SPI.

This is the same protocol as implemented by applespi.c. It was not
noticed that protocol is a transport for HID. Adding support for ACPI
based Intel MacBooks will be done in a separate commit.

How HID is mapped in this protocol is not yet fully understood.

Microsoft has a specification for HID over SPI [1] incompatible with the
transport protocol used by Apple.

[1] https://docs.microsoft.com/en-us/windows-hardware/drivers/hid/hid-over-spi

Contains "HID: transport: spi: apple: Increase receive buffer size"

The SPI receive buffer is passed directly to hid_input_report() if it
contains a complete report. It is then passed to hid_report_raw_event()
which computes the expected report size and memsets the "missing
trailing data up to HID_MAX_BUFFER_SIZE (16K) or
hid_ll_driver.max_buffer_size (if set) to zero.

Co-developed-by: Hector Martin <marcan@marcan.st>
Signed-off-by: Hector Martin <marcan@marcan.st>
Signed-off-by: Janne Grunau <j@jannau.net>
DockChannel is a simple FIFO interface used to communicate between SoC
blocks. Add a driver that represents the shared interrupt controller for
the DockChannel block, and then exposes probe and data transfer
functions that child device drivers can use to instantiate individual
FIFOs.

Signed-off-by: Hector Martin <marcan@marcan.st>
Apple M2 devices have an MTP coprocessor embedded in the SoC that
handles HID for the integrated touchpad/keyboard, and communicates
over the DockChannel interface. This driver implements this new
interface.

Signed-off-by: Hector Martin <marcan@marcan.st>
This driver can be used for coprocessors that do some background task or
communicate out-of-band, and do not do any mailbox I/O beyond the
standard RTKit initialization.

Signed-off-by: Hector Martin <marcan@marcan.st>
Certain Broadcom bluetooth chips (bcm4377/bcm4378/bcm438) need ACL
streams carrying audio to be set as "high priority" using a vendor
specific command to prevent 10-ish second-long dropouts whenever
something does a device scan. This patch sends the command when the
socket priority is set to TC_PRIO_INTERACTIVE, as BlueZ does for audio.

Signed-off-by: Sasha Finkelstein <fnkl.kernel@gmail.com>
The current approach of silently disabling all rust drivers if the
toolchain is missing results in users that try to compile their own
kernels getting a "successful" build and then being confused about where
did their drivers go. In comparison, missing openssl results in a build
failure, not a disappearance of everything that depends on it.

This also means that allyesconfig will depend on rust, but since the
rust experiment concluded with "rust is here to stay", i believe that
allyesconfig should be building rust drivers too.

Signed-off-by: Sasha Finkelstein <k@chaosmail.tech>
Apple M3 Pro and Max devices are using 'gp00' keys for GPIO in addition
to 'gP00' keys. Add a second compatible to handle this keys with an
additional macsmc-gpio instance.

Signed-off-by: Janne Grunau <j@jannau.net>
Add support for SMC GPIO keys with a lower letter 'p' via the
"apple,smc-low-gpio" compatible. This adds support for a second
macsmc-gpio controller using 'gp00' keys.
These keys are used on Apple M3 Pro and Max MacBooks in the controller
for keyboard and trackpad and for the built-in DisplayPort to HDMI
converter.

Signed-off-by: Janne Grunau <j@jannau.net>
Apple M3 Pro and Max devices are using 'gp00' keys for GPIO in addition
to 'gP00' keys. These keys are handled by an additional macsmc-gpio
instance using the "apple,smc-low-gpio" compatible.

Signed-off-by: Janne Grunau <j@jannau.net>
Signed-off-by: Janne Grunau <j@jannau.net>
@brentkearney

brentkearney commented Sep 2, 2026

Copy link
Copy Markdown
Author

Runtime results from the patched kernel on the j316s (MacBook Pro 16-inch, 2021, apple,t6000):

  • apple_sep binds at boot, SEPOS completes the boot handshake and advertises its endpoints as before.
  • echo 396400000.sep > /sys/bus/platform/drivers/apple_sep/unbind completes with no kernel output at all. dmesg | grep -c WARNING → 0, dmesg | grep -c dart_unmap → 0. /proc/sys/kernel/tainted keeps the W bit (512) clear; on the stock kernel this same unbind emits the two WARNs in apple_sep: unbinding warns twice and leaks the firmware IOMMU mapping #591 and taints the kernel.
  • /sys/bus/platform/devices/396400000.sep/driver is gone after the unbind, so remove() ran to completion.

load_fw_and_shmem() maps the SEP firmware region with dma_map_resource(),
which returns an IOVA, but keeps only the shifted copy needed for the
MSG_BOOT_IMG4 message and discards the IOVA itself. remove() then calls
dma_unmap_resource() with region_params.addr, the physical address of the
reserved region.

The two are unrelated, so the unmap tears down page table entries that were
never present, and the real mapping is leaked. On t6000 the region sits at
0x10006340000, far outside the device's DART address space, and unbinding
the driver warns twice:

  WARNING: drivers/iommu/io-pgtable-dart.c:308 at dart_unmap_pages+0x11c/0x140
   dart_unmap_pages+0x11c/0x140
   apple_dart_unmap_pages+0x24/0x30
   __iommu_dma_unmap+0x94/0x140
   iommu_dma_unmap_phys+0xf8/0x118
   ...3sep9SepDriverEE20post_unbind_callback+0x34/0x70

  WARNING: drivers/iommu/dma-iommu.c:844 at __iommu_dma_unmap+0x128/0x140
   __iommu_dma_unmap+0x128/0x140
   iommu_dma_unmap_phys+0xf8/0x118
   ...3sep9SepDriverEE20post_unbind_callback+0x34/0x70

Keep the IOVA alongside the existing fw_mapped flag and unmap that instead.

Reproduced on an Apple MacBook Pro (16-inch, 2021), t6000/j316s, by
enabling the sep node and unbinding the driver.
ElectricS01 pushed a commit to ElectricS01/linux that referenced this pull request Sep 7, 2026
Extend the verifier_direct_packet_access BPF selftests to exercise the
verifier code paths which ensure that the pkt range is cleared after
add/sub alu with a known scalar. The tests reject the invalid access.

  # LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t verifier_direct
  [...]
  AsahiLinux#592/35  verifier_direct_packet_access/direct packet access: pkt_range cleared after sub with known scalar:OK
  AsahiLinux#592/36  verifier_direct_packet_access/direct packet access: pkt_range cleared after add with known scalar:OK
  AsahiLinux#592/37  verifier_direct_packet_access/direct packet access: test3:OK
  AsahiLinux#592/38  verifier_direct_packet_access/direct packet access: test3 @unpriv:OK
  AsahiLinux#592/39  verifier_direct_packet_access/direct packet access: test34 (non-linear, cgroup_skb/ingress, too short eth):OK
  AsahiLinux#592/40  verifier_direct_packet_access/direct packet access: test35 (non-linear, cgroup_skb/ingress, too short 1):OK
  AsahiLinux#592/41  verifier_direct_packet_access/direct packet access: test36 (non-linear, cgroup_skb/ingress, long enough):OK
  AsahiLinux#592     verifier_direct_packet_access:OK
  [...]
  Summary: 2/47 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/r/20260409155016.536608-2-daniel@iogearbox.net
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants