Skip to content

Instantly share code, notes, and snippets.

@makslevental
Created September 19, 2024 19:19
Show Gist options
  • Save makslevental/87701e2abd770a4afe0004871a711d66 to your computer and use it in GitHub Desktop.
Save makslevental/87701e2abd770a4afe0004871a711d66 to your computer and use it in GitHub Desktop.
// Copyright 2024 The IREE Authors
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <cstdint>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include "amdxdna_accel.h"
void *addr_align(void *p, size_t align) {
return (void *)(((uintptr_t)p + align) & ~(align - 1));
}
void *alloc_heap(int fd, __u32 size, __u32 *handle, void *heap_buf) {
amdxdna_drm_create_bo create_bo_params = {
.type = AMDXDNA_BO_DEV_HEAP,
.vaddr = reinterpret_cast<uintptr_t>(heap_buf),
.size = size,
};
ioctl(fd, DRM_IOCTL_AMDXDNA_CREATE_BO, &create_bo_params);
amdxdna_drm_get_bo_info get_bo_info = {.handle = create_bo_params.handle};
ioctl(fd, DRM_IOCTL_AMDXDNA_GET_BO_INFO, &get_bo_info);
void *m_parent = nullptr;
const size_t align = 64 * 1024 * 1024;
auto aligned = addr_align(m_parent, align);
void *m_aligned = mmap(aligned, align, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_FIXED, fd, get_bo_info.map_offset);
return m_aligned;
}
int main(int argc, char **argv) {
int drv_fd;
int ret;
const char drv_path[] = "/dev/accel/accel0";
uint32_t heap_handle;
drv_fd = open(drv_path, O_RDWR);
void *heap_buf = nullptr;
void *aligned = alloc_heap(drv_fd, 64 * 1024 * 1024, &heap_handle, heap_buf);
munmap(aligned, 64 * 1024 * 1024);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment