Place the host window before InitDevice in the host app and the self-test

MacLinuxGPUHost init and drm-selftest.py --init called InitDevice
without placing the host window (selector 54), so compute start failed
with gart status -19. Query the aperture size, reserve it aligned, set
the base, then probe, as the HSA runtime does.
This commit is contained in:
geramyloveless committed 2026-10-03 15:16:21 -07:00
1 parent 8ac5223b6b
commit 84b40028d2
2 files changed
+52 -1

No files matched your search

+28
View File
@@ -408,6 +408,7 @@ private let kSelAllocateDMABuffer: UInt32 = 6
private let kSelFreeDMABuffer: UInt32 = 7
private let kSelResetDevice: UInt32 = 8
private let kSelInitDevice: UInt32 = 9
private let kSelHostWindow: UInt32 = 54
private let kSelLoadFirmware: UInt32 = 10
private let kSelSetIPBase: UInt32 = 11
private let kSelGetIPBase: UInt32 = 12
@@ -763,7 +764,34 @@ final class MacLinuxGPUHost {
if started != 0 {
append("initDevice: firmware servicer unavailable (%d); embedded fallback only", started)
}
// Place the host window before the probe, as the HSA runtime's
// initializeDevice does: query the GART aperture size (selector 54
// with 0), reserve that much of this process's address space aligned
// to its size, and hand the base to the driver. It is only a
// placement hint, released once InitDevice returns.
var reservation: (UnsafeMutableRawPointer, Int)?
let (queryKR, query) = callScalar(kSelHostWindow, inScalars: [0], outScalars: 3)
if queryKR == kIOReturnSuccess, query.count == 3 {
let bytes = query[1]
if bytes >= 16384, bytes & (bytes - 1) == 0, bytes <= 1 << 45,
let raw = mmap(nil, Int(bytes) * 2, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0),
raw != MAP_FAILED {
reservation = (raw, Int(bytes) * 2)
let base = (UInt64(UInt(bitPattern: raw)) + bytes - 1) & ~(bytes - 1)
let (setKR, set) = callScalar(kSelHostWindow, inScalars: [base], outScalars: 3)
if setKR == kIOReturnSuccess, set.count == 3, set[0] == base, set[1] == bytes {
append(String(format: "initDevice: host window at %#llx, %llu MiB", base, bytes >> 20))
} else {
append(String(format: "initDevice: host window not placed (kr=%#x)", setKR))
}
} else {
append(String(format: "initDevice: cannot reserve a %#llx-byte host window", bytes))
}
} else {
append(String(format: "initDevice: host window query failed (kr=%#x)", queryKR))
}
let (kr, _) = callScalar(kSelInitDevice, inScalars: [], outScalars: 0)
if let (raw, length) = reservation { munmap(raw, length) }
if let service {
append("initDevice: firmware servicer served %llu file(s), %llu not found",
mlg_fw_service_served(service), mlg_fw_service_missing(service))
+24 -1
View File
@@ -32,7 +32,7 @@ import struct
import sys
SESSION_CLIENT, OBSERVER_CLIENT = 0, 1
RUNTIME_BUILD, INIT_DEVICE = 43, 9
RUNTIME_BUILD, INIT_DEVICE, HOST_WINDOW = 43, 9, 54
DRM_SELFTEST = 82 # dext/sources/session_state.h MLG_SELECTOR_DRM_SELFTEST
CONFIRM = 0x43535354 # MLG_DRM_SELFTEST_CONFIRM ("CSST")
RESULT_MAX = 512 # MLG_DRM_SELFTEST_RESULT_MAX
@@ -164,9 +164,32 @@ def connect(init=False):
build = (c.c_uint64 * 3)()
count = c.c_uint(3)
result = io.IOConnectCallScalarMethod(session, RUNTIME_BUILD, None, 0, build, c.byref(count))
reservation = None
if not result:
# Place the host window before the probe, as the HSA runtime does:
# query the GART aperture size, reserve that much address space
# aligned to its size, and hand the base to the driver.
window = (c.c_uint64 * 3)()
count = c.c_uint(3)
query = (c.c_uint64 * 1)(0)
result = io.IOConnectCallScalarMethod(session, HOST_WINDOW, query, 1, window, c.byref(count))
size = window[1]
if not result and size and not size & (size - 1):
system.mmap.argtypes = [c.c_void_p, c.c_size_t, c.c_int, c.c_int, c.c_int, c.c_long]
system.mmap.restype = c.c_void_p
system.munmap.argtypes = [c.c_void_p, c.c_size_t]
raw = system.mmap(None, size * 2, 0, 0x0002 | 0x1000, -1, 0) # PROT_NONE, MAP_PRIVATE|MAP_ANON
if raw and raw != c.c_void_p(-1).value:
reservation = (raw, size * 2)
base = (raw + size - 1) & ~(size - 1)
count = c.c_uint(3)
request = (c.c_uint64 * 1)(base)
result = io.IOConnectCallScalarMethod(session, HOST_WINDOW, request, 1, window, c.byref(count))
if not result:
count = c.c_uint(0)
result = io.IOConnectCallScalarMethod(session, INIT_DEVICE, None, 0, None, c.byref(count))
if reservation:
system.munmap(reservation[0], reservation[1])
if result:
io.IOServiceClose(session)
io.IOServiceClose(port)