Module: Mesa Branch: main Commit: 29ccd991a84e461840fea1056394627056e565c6 URL: http://cgit.freedesktop.org/mesa/mesa/commit/?id=29ccd991a84e461840fea1056394627056e565c6
Author: Juan A. Suarez Romero <[email protected]> Date: Wed Aug 3 13:07:56 2022 +0200 vc4/simulator: use i915/amd ioctls for BO So far we create dumb buffers to emulate the ioctls in VC4, but these doesn't work when using an Intel or AMD GPUs. As in the case of V3D, let's use specific ioctls for these cases. Signed-off-by: Juan A. Suarez Romero <[email protected]> Tested-by: Eric Engestrom <[email protected]> Reviewed-by: Eric Engestrom <[email protected]> Reviewed-by: Iago Toral Quroga <[email protected]> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17872> --- src/gallium/drivers/vc4/vc4_simulator.c | 149 ++++++++++++++++++++++++-------- 1 file changed, 113 insertions(+), 36 deletions(-) diff --git a/src/gallium/drivers/vc4/vc4_simulator.c b/src/gallium/drivers/vc4/vc4_simulator.c index b3379b16c11..b71fcc1af45 100644 --- a/src/gallium/drivers/vc4/vc4_simulator.c +++ b/src/gallium/drivers/vc4/vc4_simulator.c @@ -61,6 +61,9 @@ #include "vc4_simulator_validate.h" #include "simpenrose/simpenrose.h" +#include "drm-uapi/amdgpu_drm.h" +#include "drm-uapi/i915_drm.h" + /** Global (across GEM fds) state for the simulator */ static struct vc4_simulator_state { mtx_t mutex; @@ -78,6 +81,12 @@ static struct vc4_simulator_state { .mutex = _MTX_INITIALIZER_NP, }; +enum gem_type { + GEM_I915, + GEM_AMDGPU, + GEM_DUMB +}; + /** Per-GEM-fd state for the simulator. */ struct vc4_simulator_file { int fd; @@ -90,6 +99,9 @@ struct vc4_simulator_file { /** Mapping from GEM handle to struct vc4_simulator_bo * */ struct hash_table *bo_map; + + /** for specific gpus, use their create ioctl. Otherwise use dumb bo. */ + enum gem_type gem_type; }; /** Wrapper for drm_vc4_bo tracking the simulator-specific state. */ @@ -129,6 +141,87 @@ vc4_get_simulator_file_for_fd(int fd) #define PAGE_ALIGN2 12 +static int +vc4_gem_mmap(int fd, uint32_t handle, uint64_t *offset) +{ + struct vc4_simulator_file *file = vc4_get_simulator_file_for_fd(fd); + int ret; + + assert(offset); + + switch (file->gem_type) { + case GEM_I915: + { + struct drm_i915_gem_mmap_gtt map = { + .handle = handle, + }; + ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &map); + *offset = map.offset; + break; + } + case GEM_AMDGPU: + { + union drm_amdgpu_gem_mmap map = { 0 }; + map.in.handle = handle; + ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &map); + *offset = map.out.addr_ptr; + break; + } + default: + { + struct drm_mode_map_dumb map = { + .handle = handle, + }; + ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map); + *offset = map.offset; + } + } + + return ret; +} + +static int +vc4_gem_create(int fd, uint64_t size, uint32_t *handle) +{ + struct vc4_simulator_file *file = vc4_get_simulator_file_for_fd(fd); + int ret; + + assert(handle); + + switch (file->gem_type) { + case GEM_I915: + { + struct drm_i915_gem_create create = { + .size = size, + }; + ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create); + *handle = create.handle; + break; + } + case GEM_AMDGPU: + { + union drm_amdgpu_gem_create create = { 0 }; + create.in.bo_size = size; + ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_CREATE, &create); + *handle = create.out.handle; + break; + } + default: + { + struct drm_mode_create_dumb create = { + .width = 128, + .bpp = 8, + .height = (size + 127) / 128, + }; + ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create); + assert(create.size >= size); + *handle = create.handle; + } + } + + return ret; +} + /** * Allocates space in simulator memory and returns a tracking struct for it * that also contains the drm_gem_cma_object struct. @@ -168,10 +261,9 @@ vc4_create_simulator_bo(int fd, int handle, unsigned size) mtx_unlock(&sim_state.mutex); /* Map the GEM buffer for copy in/out to the simulator. */ - struct drm_mode_map_dumb map = { - .handle = handle, - }; - int ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map); + uint64_t mmap_offset; + int ret = vc4_gem_mmap(fd, handle, &mmap_offset); + if (ret) { fprintf(stderr, "Failed to get MMAP offset: %d\n", errno); @@ -179,10 +271,10 @@ vc4_create_simulator_bo(int fd, int handle, unsigned size) } sim_bo->gem_vaddr = mmap(NULL, obj->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, - fd, map.offset); + fd, mmap_offset); if (sim_bo->gem_vaddr == MAP_FAILED) { fprintf(stderr, "mmap of bo %d (offset 0x%016llx, size %d) failed\n", - handle, (long long)map.offset, (int)obj->base.size); + handle, (long long)mmap_offset, (int)obj->base.size); abort(); } } @@ -451,19 +543,9 @@ void vc4_simulator_open_from_handle(int fd, int handle, uint32_t size) static int vc4_simulator_create_bo_ioctl(int fd, struct drm_vc4_create_bo *args) { - int ret; - struct drm_mode_create_dumb create = { - .width = 128, - .bpp = 8, - .height = (args->size + 127) / 128, - }; - - ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create); - assert(create.size >= args->size); - - args->handle = create.handle; + int ret = vc4_gem_create(fd, args->size, &(args->handle)); - vc4_create_simulator_bo(fd, create.handle, args->size); + vc4_create_simulator_bo(fd, args->handle, args->size); return ret; } @@ -478,22 +560,13 @@ static int vc4_simulator_create_shader_bo_ioctl(int fd, struct drm_vc4_create_shader_bo *args) { - int ret; - struct drm_mode_create_dumb create = { - .width = 128, - .bpp = 8, - .height = (args->size + 127) / 128, - }; + int ret = vc4_gem_create(fd, args->size, &(args->handle)); - ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create); if (ret) return ret; - assert(create.size >= args->size); - - args->handle = create.handle; struct vc4_simulator_bo *sim_bo = - vc4_create_simulator_bo(fd, create.handle, args->size); + vc4_create_simulator_bo(fd, args->handle, args->size); struct drm_vc4_bo *drm_bo = &sim_bo->base; struct drm_gem_cma_object *obj = &drm_bo->base; @@ -520,13 +593,9 @@ vc4_simulator_create_shader_bo_ioctl(int fd, static int vc4_simulator_mmap_bo_ioctl(int fd, struct drm_vc4_mmap_bo *args) { - int ret; - struct drm_mode_map_dumb map = { - .handle = args->handle, - }; - - ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map); - args->offset = map.offset; + uint64_t mmap_offset; + int ret = vc4_gem_mmap(fd, args->handle, &mmap_offset); + args->offset = mmap_offset; return ret; } @@ -677,6 +746,14 @@ vc4_simulator_init(struct vc4_screen *screen) _mesa_hash_pointer, _mesa_key_pointer_equal); + drmVersionPtr version = drmGetVersion(screen->fd); + if (version && strncmp(version->name, "i915", version->name_len) == 0) + screen->sim_file->gem_type = GEM_I915; + else if (version && strncmp(version->name, "amdgpu", version->name_len) == 0) + screen->sim_file->gem_type = GEM_AMDGPU; + else + screen->sim_file->gem_type = GEM_DUMB; + drmFreeVersion(version); mtx_lock(&sim_state.mutex); _mesa_hash_table_insert(sim_state.fd_map, int_to_key(screen->fd + 1), screen->sim_file);
