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