1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2021 Icecream95
3bf215546Sopenharmony_ci * Copyright (C) 2019 Google LLC
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include <limits.h>
26bf215546Sopenharmony_ci#include <stdio.h>
27bf215546Sopenharmony_ci#include <stdlib.h>
28bf215546Sopenharmony_ci#include "drm-shim/drm_shim.h"
29bf215546Sopenharmony_ci#include "drm-uapi/lima_drm.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "util/u_math.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_cibool drm_shim_driver_prefers_first_render_node = true;
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cistatic int
36bf215546Sopenharmony_cilima_ioctl_noop(int fd, unsigned long request, void *arg)
37bf215546Sopenharmony_ci{
38bf215546Sopenharmony_ci   return 0;
39bf215546Sopenharmony_ci}
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_cistatic int
42bf215546Sopenharmony_cilima_ioctl_get_param(int fd, unsigned long request, void *arg)
43bf215546Sopenharmony_ci{
44bf215546Sopenharmony_ci   struct drm_lima_get_param *gp = arg;
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci   switch (gp->param) {
47bf215546Sopenharmony_ci   case DRM_LIMA_PARAM_GPU_ID:
48bf215546Sopenharmony_ci      gp->value = DRM_LIMA_PARAM_GPU_ID_MALI450;
49bf215546Sopenharmony_ci      return 0;
50bf215546Sopenharmony_ci   case DRM_LIMA_PARAM_NUM_PP:
51bf215546Sopenharmony_ci      gp->value = 6;
52bf215546Sopenharmony_ci      return 0;
53bf215546Sopenharmony_ci   default:
54bf215546Sopenharmony_ci      fprintf(stderr, "Unknown DRM_IOCTL_LIMA_GET_PARAM %d\n", gp->param);
55bf215546Sopenharmony_ci      return -1;
56bf215546Sopenharmony_ci   }
57bf215546Sopenharmony_ci}
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_cistatic int
60bf215546Sopenharmony_cilima_ioctl_gem_create(int fd, unsigned long request, void *arg)
61bf215546Sopenharmony_ci{
62bf215546Sopenharmony_ci   struct drm_lima_gem_create *create = arg;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
65bf215546Sopenharmony_ci   struct shim_bo *bo = calloc(1, sizeof(*bo));
66bf215546Sopenharmony_ci   size_t size = ALIGN(create->size, 4096);
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   drm_shim_bo_init(bo, size);
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   create->handle = drm_shim_bo_get_handle(shim_fd, bo);
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   drm_shim_bo_put(bo);
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   return 0;
75bf215546Sopenharmony_ci}
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_cistatic int
78bf215546Sopenharmony_cilima_ioctl_gem_info(int fd, unsigned long request, void *arg)
79bf215546Sopenharmony_ci{
80bf215546Sopenharmony_ci   struct drm_lima_gem_info *gem_info = arg;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
83bf215546Sopenharmony_ci   struct shim_bo *bo = drm_shim_bo_lookup(shim_fd, gem_info->handle);
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   gem_info->va = bo->mem_addr;
86bf215546Sopenharmony_ci   gem_info->offset = drm_shim_bo_get_mmap_offset(shim_fd, bo);
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   return 0;
89bf215546Sopenharmony_ci}
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_cistatic ioctl_fn_t driver_ioctls[] = {
92bf215546Sopenharmony_ci   [DRM_LIMA_GET_PARAM] = lima_ioctl_get_param,
93bf215546Sopenharmony_ci   [DRM_LIMA_GEM_CREATE] = lima_ioctl_gem_create,
94bf215546Sopenharmony_ci   [DRM_LIMA_GEM_INFO] = lima_ioctl_gem_info,
95bf215546Sopenharmony_ci   [DRM_LIMA_GEM_SUBMIT] = lima_ioctl_noop,
96bf215546Sopenharmony_ci   [DRM_LIMA_GEM_WAIT] = lima_ioctl_noop,
97bf215546Sopenharmony_ci   [DRM_LIMA_CTX_CREATE] = lima_ioctl_noop,
98bf215546Sopenharmony_ci   [DRM_LIMA_CTX_FREE] = lima_ioctl_noop,
99bf215546Sopenharmony_ci};
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_civoid
102bf215546Sopenharmony_cidrm_shim_driver_init(void)
103bf215546Sopenharmony_ci{
104bf215546Sopenharmony_ci   shim_device.bus_type = DRM_BUS_PLATFORM;
105bf215546Sopenharmony_ci   shim_device.driver_name = "lima";
106bf215546Sopenharmony_ci   shim_device.driver_ioctls = driver_ioctls;
107bf215546Sopenharmony_ci   shim_device.driver_ioctl_count = ARRAY_SIZE(driver_ioctls);
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   /* lima uses the DRM version to expose features, instead of getparam. */
110bf215546Sopenharmony_ci   shim_device.version_major = 1;
111bf215546Sopenharmony_ci   shim_device.version_minor = 1;
112bf215546Sopenharmony_ci   shim_device.version_patchlevel = 0;
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   drm_shim_override_file("DRIVER=lima\n"
115bf215546Sopenharmony_ci                          "OF_FULLNAME=/soc/mali\n"
116bf215546Sopenharmony_ci                          "OF_COMPATIBLE_0=arm,mali-450\n"
117bf215546Sopenharmony_ci                          "OF_COMPATIBLE_N=1\n",
118bf215546Sopenharmony_ci                          "/sys/dev/char/%d:%d/device/uevent", DRM_MAJOR,
119bf215546Sopenharmony_ci                          render_node_minor);
120bf215546Sopenharmony_ci}
121