CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / src / omega_gpu_mem_cached.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include "omega_gpu_mem_cached.h" |
| 3 | #include <string.h> |
| 4 | #include "omega_gpu_platform.h" |
| 5 | #include "omega_gpu_fmt.h" |
| 6 | #if defined(CUDAPP_MIGRATION_PENDING_C_MEMCACHE) |
| 7 | #define ENG_C_PART 1 |
| 8 | #endif |
| 9 | #include "omega_eng_dec.h" |
| 10 | |
| 11 | int omega_gpu_session_alloc_cached(size_t bytes, OmegaGpuMem *out) { |
| 12 | size_t want; |
| 13 | if (eng_on()) { |
| 14 | uint64_t in[2], w[16]; |
| 15 | in[0] = (uint64_t)bytes; |
| 16 | in[1] = out ? 1u : 0u; |
| 17 | if (eng_ask(ENG_OP_CACHE, in, 2, w) != 0 || w[0] != 0) { |
| 18 | omega_gpu_session_set_error("cached alloc: arguments"); |
| 19 | return -1; |
| 20 | } |
| 21 | want = (size_t)w[1]; |
| 22 | } else { |
| 23 | if (!out || bytes == 0) { omega_gpu_session_set_error("cached alloc: arguments"); return -1; } |
| 24 | want = (bytes + 0xfffu) & ~(size_t)0xfffu; |
| 25 | } |
| 26 | memset(out, 0, sizeof *out); |
| 27 | OmegaGpuSession *s = omega_gpu_session_open(); |
| 28 | const OmegaGpuPlatform *p = omega_gpu_session_platform(); |
| 29 | if (!s || !p) return -1; |
| 30 | OmegaGpuBufferDesc d = { .kind = OMEGA_GPU_HANDLE_BUF_DATA, .rights = OMEGA_GPU_RIGHT_CPU_WRITE | OMEGA_GPU_RIGHT_GPU_READ, /* no CPU_READ: the platform refuses a CPU-readable GPU-cached buffer */ |
| 31 | .gpu_cache = OMEGA_GPU_CACHE_GPU_CACHED, .align = 0x1000u, .bytes = want }; |
| 32 | OmegaCpuView cpu; OmegaGpuVa va; OmegaGpuBusAddr bus; |
| 33 | int prc = p->dma_alloc(p->ctx, s->dev, &d, &out->handle, &cpu, &va, &bus); |
| 34 | if (prc != OMEGA_GPU_PLAT_OK || !cpu.ptr || cpu.len < want) { |
| 35 | if (prc == OMEGA_GPU_PLAT_OK) (void)p->dma_free(p->ctx, out->handle); |
| 36 | char e[96]; omega_gpu_fmt(e, sizeof e, "cached alloc of %zu bytes refused (%d)", want, prc); |
| 37 | omega_gpu_session_set_error(e); memset(out, 0, sizeof *out); return -1; |
| 38 | } |
| 39 | out->kind = OMEGA_GPU_HANDLE_BUF_DATA; out->cpu = cpu.ptr; out->va = va.va; out->size = cpu.len; |
| 40 | return 0; |
| 41 | } |