omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / omega_gpu_mem_cached.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "omega_gpu_mem_cached.h" |
| 18 | #include <string.h> |
| 19 | #include "omega_gpu_platform.h" |
| 20 | #include "omega_gpu_fmt.h" |
| 21 | |
| 22 | int omega_gpu_session_alloc_cached(size_t bytes, OmegaGpuMem *out) { |
| 23 | if (!out || bytes == 0) { omega_gpu_session_set_error("cached alloc: arguments"); return -1; } |
| 24 | memset(out, 0, sizeof *out); |
| 25 | OmegaGpuSession *s = omega_gpu_session_open(); |
| 26 | const OmegaGpuPlatform *p = omega_gpu_session_platform(); |
| 27 | if (!s || !p) return -1; |
| 28 | const size_t want = (bytes + 0xfffu) & ~(size_t)0xfffu; |
| 29 | 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 */ |
| 30 | .gpu_cache = OMEGA_GPU_CACHE_GPU_CACHED, .align = 0x1000u, .bytes = want }; |
| 31 | OmegaCpuView cpu; OmegaGpuVa va; OmegaGpuBusAddr bus; |
| 32 | int prc = p->dma_alloc(p->ctx, s->dev, &d, &out->handle, &cpu, &va, &bus); |
| 33 | if (prc != OMEGA_GPU_PLAT_OK || !cpu.ptr || cpu.len < want) { |
| 34 | if (prc == OMEGA_GPU_PLAT_OK) (void)p->dma_free(p->ctx, out->handle); |
| 35 | char e[96]; omega_gpu_fmt(e, sizeof e, "cached alloc of %zu bytes refused (%d)", want, prc); |
| 36 | omega_gpu_session_set_error(e); memset(out, 0, sizeof *out); return -1; |
| 37 | } |
| 38 | out->kind = OMEGA_GPU_HANDLE_BUF_DATA; out->cpu = cpu.ptr; out->va = va.va; out->size = cpu.len; |
| 39 | return 0; |
| 40 | } |