diff --git a/autogen_stubs.sh b/autogen_stubs.sh index 6798b1d896..94f419d115 100755 --- a/autogen_stubs.sh +++ b/autogen_stubs.sh @@ -126,6 +126,11 @@ generate_comgr() { generate_kfd() { clang2py /usr/include/linux/kfd_ioctl.h -o $BASE/kfd.py -k cdefstum + awk '/^#define AMDKFD_IOC_/ { if ($0 ~ /\\$/) { getline nextline; $0 = $0 nextline } + if (match($0, /AMDKFD_IOC_([A-Z_]+).*AMDKFD_(IOW?R?)\(0x([0-9A-F]+),.*struct ([a-z_]+)/, arr)) { + print "AMDKFD_IOC_" arr[1] " = (\"" arr[2] "\", 0x" arr[3] ", struct_" arr[4] ")" + } + }' /usr/include/linux/kfd_ioctl.h >> $BASE/kfd.py fixup $BASE/kfd.py sed -i "s\import ctypes\import ctypes, os\g" $BASE/kfd.py python3 -c "import tinygrad.runtime.autogen.kfd" diff --git a/tinygrad/runtime/autogen/kfd.py b/tinygrad/runtime/autogen/kfd.py index 8772dc0751..5d5b29fe5e 100644 --- a/tinygrad/runtime/autogen/kfd.py +++ b/tinygrad/runtime/autogen/kfd.py @@ -810,3 +810,35 @@ __all__ = \ 'struct_kfd_ioctl_wait_events_args', 'struct_kfd_memory_exception_failure', 'struct_kfd_process_device_apertures', 'union_kfd_event_data_0'] +AMDKFD_IOC_GET_VERSION = ("IOR", 0x01, struct_kfd_ioctl_get_version_args) +AMDKFD_IOC_CREATE_QUEUE = ("IOWR", 0x02, struct_kfd_ioctl_create_queue_args) +AMDKFD_IOC_DESTROY_QUEUE = ("IOWR", 0x03, struct_kfd_ioctl_destroy_queue_args) +AMDKFD_IOC_SET_MEMORY_POLICY = ("IOW", 0x04, struct_kfd_ioctl_set_memory_policy_args) +AMDKFD_IOC_GET_CLOCK_COUNTERS = ("IOWR", 0x05, struct_kfd_ioctl_get_clock_counters_args) +AMDKFD_IOC_GET_PROCESS_APERTURES = ("IOR", 0x06, struct_kfd_ioctl_get_process_apertures_args) +AMDKFD_IOC_UPDATE_QUEUE = ("IOW", 0x07, struct_kfd_ioctl_update_queue_args) +AMDKFD_IOC_CREATE_EVENT = ("IOWR", 0x08, struct_kfd_ioctl_create_event_args) +AMDKFD_IOC_DESTROY_EVENT = ("IOW", 0x09, struct_kfd_ioctl_destroy_event_args) +AMDKFD_IOC_SET_EVENT = ("IOW", 0x0A, struct_kfd_ioctl_set_event_args) +AMDKFD_IOC_RESET_EVENT = ("IOW", 0x0B, struct_kfd_ioctl_reset_event_args) +AMDKFD_IOC_WAIT_EVENTS = ("IOWR", 0x0C, struct_kfd_ioctl_wait_events_args) +AMDKFD_IOC_DBG_REGISTER = ("IOW", 0x0D, struct_kfd_ioctl_dbg_register_args) +AMDKFD_IOC_DBG_UNREGISTER = ("IOW", 0x0E, struct_kfd_ioctl_dbg_unregister_args) +AMDKFD_IOC_DBG_ADDRESS_WATCH = ("IOW", 0x0F, struct_kfd_ioctl_dbg_address_watch_args) +AMDKFD_IOC_DBG_WAVE_CONTROL = ("IOW", 0x10, struct_kfd_ioctl_dbg_wave_control_args) +AMDKFD_IOC_SET_SCRATCH_BACKING_VA = ("IOWR", 0x11, struct_kfd_ioctl_set_scratch_backing_va_args) +AMDKFD_IOC_GET_TILE_CONFIG = ("IOWR", 0x12, struct_kfd_ioctl_get_tile_config_args) +AMDKFD_IOC_SET_TRAP_HANDLER = ("IOW", 0x13, struct_kfd_ioctl_set_trap_handler_args) +AMDKFD_IOC_ACQUIRE_VM = ("IOW", 0x15, struct_kfd_ioctl_acquire_vm_args) +AMDKFD_IOC_ALLOC_MEMORY_OF_GPU = ("IOWR", 0x16, struct_kfd_ioctl_alloc_memory_of_gpu_args) +AMDKFD_IOC_FREE_MEMORY_OF_GPU = ("IOW", 0x17, struct_kfd_ioctl_free_memory_of_gpu_args) +AMDKFD_IOC_MAP_MEMORY_TO_GPU = ("IOWR", 0x18, struct_kfd_ioctl_map_memory_to_gpu_args) +AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU = ("IOWR", 0x19, struct_kfd_ioctl_unmap_memory_from_gpu_args) +AMDKFD_IOC_SET_CU_MASK = ("IOW", 0x1A, struct_kfd_ioctl_set_cu_mask_args) +AMDKFD_IOC_GET_QUEUE_WAVE_STATE = ("IOWR", 0x1B, struct_kfd_ioctl_get_queue_wave_state_args) +AMDKFD_IOC_GET_DMABUF_INFO = ("IOWR", 0x1C, struct_kfd_ioctl_get_dmabuf_info_args) +AMDKFD_IOC_IMPORT_DMABUF = ("IOWR", 0x1D, struct_kfd_ioctl_import_dmabuf_args) +AMDKFD_IOC_ALLOC_QUEUE_GWS = ("IOWR", 0x1E, struct_kfd_ioctl_alloc_queue_gws_args) +AMDKFD_IOC_SMI_EVENTS = ("IOWR", 0x1F, struct_kfd_ioctl_smi_events_args) +AMDKFD_IOC_SVM = ("IOWR", 0x20, struct_kfd_ioctl_svm_args) +AMDKFD_IOC_SET_XNACK_MODE = ("IOWR", 0x21, struct_kfd_ioctl_set_xnack_mode_args) diff --git a/tinygrad/runtime/ops_amd.py b/tinygrad/runtime/ops_amd.py index 38388bc8a4..e7d6f4944e 100644 --- a/tinygrad/runtime/ops_amd.py +++ b/tinygrad/runtime/ops_amd.py @@ -1,6 +1,6 @@ from __future__ import annotations from typing import Tuple, List, Any, cast -import os, fcntl, ctypes, ctypes.util, functools, re, pathlib, mmap, errno, time, array +import os, fcntl, ctypes, ctypes.util, functools, pathlib, mmap, errno, time, array, contextlib from dataclasses import dataclass from tinygrad.device import HCQCompiled, HCQAllocator, HCQBuffer, HWComputeQueue, HWCopyQueue, \ HCQSignal, HCQProgram, BufferOptions @@ -13,31 +13,16 @@ if getenv("IOCTL"): import extra.hip_gpu_driver.hip_ioctl # noqa: F401 # pylint if getenv("MOCKGPU"): import extra.mockgpu.mockgpu # noqa: F401 # pylint: disable=unused-import def is_usable_gpu(gpu_id): - try: - with gpu_id.open() as f: - return int(f.read()) != 0 - except OSError: - return False + with contextlib.suppress(OSError): return int(pathlib.Path(gpu_id).read_text()) != 0 + return False def kfd_ioctl(idir, nr, user_struct, fd, **kwargs): ret = fcntl.ioctl(fd, (idir<<30) | (ctypes.sizeof(made := user_struct(**kwargs))<<16) | (ord('K')<<8) | nr, made) if ret != 0: raise RuntimeError(f"ioctl returned {ret}") return made -def ioctls_from_header(): - #hdr = pathlib.Path("/usr/include/linux/kfd_ioctl.h").read_text().replace("\\\n", "") - #pattern = r'#define\s+(AMDKFD_IOC_[A-Z0-9_]+)\s+AMDKFD_(IOW?R?)\((0x[0-9a-fA-F]+),\s+struct\s([A-Za-z0-9_]+)\)' - #matches = re.findall(pattern, hdr, re.MULTILINE) - # get this from python instead - hdrpy = (pathlib.Path(__file__).parent / "autogen" / "kfd.py").read_text() - pattern = r'# (AMDKFD_IOC_[A-Z0-9_]+)\s=\s_(IOW?R?).*\(( 0x[0-9a-fA-F]+) ,\s+struct\s([A-Za-z0-9_]+)\s+\)' - matches = re.findall(pattern, hdrpy, re.MULTILINE) - idirs = {"IOW": 1, "IOR": 2, "IOWR": 3} - fxns = {name.replace("AMDKFD_IOC_", "").lower(): - functools.partial(kfd_ioctl, idirs[idir], int(nr, 0x10), getattr(kfd, "struct_"+sname)) - for name, idir, nr, sname in matches} - return type("KIO", (object, ), fxns) -kio = ioctls_from_header() +kio:Any = type("KIO", (object,), {name[11:].lower(): functools.partial(kfd_ioctl, {"IOW": 1, "IOR": 2, "IOWR": 3}[p[0]], p[1], p[2]) + for name,p in kfd.__dict__.items() if name.startswith("AMDKFD_IOC_")}) regBIF_BX_PF1_GPU_HDP_FLUSH_REQ, regBIF_BX_PF1_GPU_HDP_FLUSH_DONE = 0x0106, 0x0107