1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (c) 2021 Etnaviv Project
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <limits.h>
25bf215546Sopenharmony_ci#include <stdio.h>
26bf215546Sopenharmony_ci#include <stdlib.h>
27bf215546Sopenharmony_ci#include <sys/ioctl.h>
28bf215546Sopenharmony_ci#include "drm-uapi/vc4_drm.h"
29bf215546Sopenharmony_ci#include "drm-shim/drm_shim.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_cibool drm_shim_driver_prefers_first_render_node = true;
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_cistatic int
34bf215546Sopenharmony_civc4_ioctl_noop(int fd, unsigned long request, void *arg)
35bf215546Sopenharmony_ci{
36bf215546Sopenharmony_ci        return 0;
37bf215546Sopenharmony_ci}
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_cistatic int
40bf215546Sopenharmony_civc4_ioctl_create_bo(int fd, unsigned long request, void *arg)
41bf215546Sopenharmony_ci{
42bf215546Sopenharmony_ci        struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
43bf215546Sopenharmony_ci        struct drm_vc4_create_bo *create = arg;
44bf215546Sopenharmony_ci        struct shim_bo *bo = calloc(1, sizeof(*bo));
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci        drm_shim_bo_init(bo, create->size);
47bf215546Sopenharmony_ci        create->handle = drm_shim_bo_get_handle(shim_fd, bo);
48bf215546Sopenharmony_ci        drm_shim_bo_put(bo);
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci        return 0;
51bf215546Sopenharmony_ci}
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic int
54bf215546Sopenharmony_civc4_ioctl_mmap_bo(int fd, unsigned long request, void *arg)
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci        struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
57bf215546Sopenharmony_ci        struct drm_vc4_mmap_bo *map = arg;
58bf215546Sopenharmony_ci        struct shim_bo *bo = drm_shim_bo_lookup(shim_fd, map->handle);
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci        map->offset = drm_shim_bo_get_mmap_offset(shim_fd, bo);
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci        drm_shim_bo_put(bo);
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci        return 0;
65bf215546Sopenharmony_ci}
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_cistatic int
68bf215546Sopenharmony_civc4_ioctl_get_param(int fd, unsigned long request, void *arg)
69bf215546Sopenharmony_ci{
70bf215546Sopenharmony_ci        struct drm_vc4_get_param *gp = arg;
71bf215546Sopenharmony_ci        static const uint32_t param_map[] = {
72bf215546Sopenharmony_ci                [DRM_VC4_PARAM_V3D_IDENT0] = 0x2000000,
73bf215546Sopenharmony_ci                [DRM_VC4_PARAM_V3D_IDENT1] = 0x0000001,
74bf215546Sopenharmony_ci        };
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci        switch (gp->param) {
77bf215546Sopenharmony_ci        case DRM_VC4_PARAM_SUPPORTS_BRANCHES:
78bf215546Sopenharmony_ci        case DRM_VC4_PARAM_SUPPORTS_ETC1:
79bf215546Sopenharmony_ci        case DRM_VC4_PARAM_SUPPORTS_THREADED_FS:
80bf215546Sopenharmony_ci        case DRM_VC4_PARAM_SUPPORTS_FIXED_RCL_ORDER:
81bf215546Sopenharmony_ci                gp->value = 1;
82bf215546Sopenharmony_ci                return 0;
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci        case DRM_VC4_PARAM_SUPPORTS_MADVISE:
85bf215546Sopenharmony_ci        case DRM_VC4_PARAM_SUPPORTS_PERFMON:
86bf215546Sopenharmony_ci                gp->value = 0;
87bf215546Sopenharmony_ci                return 0;
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci        default:
90bf215546Sopenharmony_ci                break;
91bf215546Sopenharmony_ci        }
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci        if (gp->param < ARRAY_SIZE(param_map) && param_map[gp->param]) {
94bf215546Sopenharmony_ci                gp->value = param_map[gp->param];
95bf215546Sopenharmony_ci                return 0;
96bf215546Sopenharmony_ci        }
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci        fprintf(stderr, "Unknown DRM_IOCTL_VC4_GET_PARAM %d\n", gp->param);
99bf215546Sopenharmony_ci        return -1;
100bf215546Sopenharmony_ci}
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_cistatic ioctl_fn_t driver_ioctls[] = {
103bf215546Sopenharmony_ci        [DRM_VC4_CREATE_BO] = vc4_ioctl_create_bo,
104bf215546Sopenharmony_ci        [DRM_VC4_MMAP_BO] = vc4_ioctl_mmap_bo,
105bf215546Sopenharmony_ci        [DRM_VC4_GET_PARAM] = vc4_ioctl_get_param,
106bf215546Sopenharmony_ci        [DRM_VC4_GET_TILING] = vc4_ioctl_noop,
107bf215546Sopenharmony_ci        [DRM_VC4_LABEL_BO] = vc4_ioctl_noop,
108bf215546Sopenharmony_ci};
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_civoid
111bf215546Sopenharmony_cidrm_shim_driver_init(void)
112bf215546Sopenharmony_ci{
113bf215546Sopenharmony_ci        shim_device.bus_type = DRM_BUS_PLATFORM;
114bf215546Sopenharmony_ci        shim_device.driver_name = "vc4";
115bf215546Sopenharmony_ci        shim_device.driver_ioctls = driver_ioctls;
116bf215546Sopenharmony_ci        shim_device.driver_ioctl_count = ARRAY_SIZE(driver_ioctls);
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci        drm_shim_override_file("OF_FULLNAME=/rdb/vc4\n"
119bf215546Sopenharmony_ci                               "OF_COMPATIBLE_N=1\n"
120bf215546Sopenharmony_ci                               "OF_COMPATIBLE_0=brcm,7278-vc4\n",
121bf215546Sopenharmony_ci                               "/sys/dev/char/%d:%d/device/uevent",
122bf215546Sopenharmony_ci                               DRM_MAJOR, render_node_minor);
123bf215546Sopenharmony_ci}
124