1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2018 Broadcom
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/v3d_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_cistruct v3d_bo {
34bf215546Sopenharmony_ci        struct shim_bo base;
35bf215546Sopenharmony_ci        uint32_t offset;
36bf215546Sopenharmony_ci};
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistatic struct v3d_bo *
39bf215546Sopenharmony_civ3d_bo(struct shim_bo *bo)
40bf215546Sopenharmony_ci{
41bf215546Sopenharmony_ci        return (struct v3d_bo *)bo;
42bf215546Sopenharmony_ci}
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_cistruct v3d_device {
45bf215546Sopenharmony_ci        uint32_t next_offset;
46bf215546Sopenharmony_ci};
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_cistatic struct v3d_device v3d = {
49bf215546Sopenharmony_ci        .next_offset = 0x1000,
50bf215546Sopenharmony_ci};
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_cistatic int
53bf215546Sopenharmony_civ3d_ioctl_noop(int fd, unsigned long request, void *arg)
54bf215546Sopenharmony_ci{
55bf215546Sopenharmony_ci        return 0;
56bf215546Sopenharmony_ci}
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_cistatic int
59bf215546Sopenharmony_civ3d_ioctl_create_bo(int fd, unsigned long request, void *arg)
60bf215546Sopenharmony_ci{
61bf215546Sopenharmony_ci        struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
62bf215546Sopenharmony_ci        struct drm_v3d_create_bo *create = arg;
63bf215546Sopenharmony_ci        struct v3d_bo *bo = calloc(1, sizeof(*bo));
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci        drm_shim_bo_init(&bo->base, create->size);
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci        assert(UINT_MAX - v3d.next_offset > create->size);
68bf215546Sopenharmony_ci        bo->offset = v3d.next_offset;
69bf215546Sopenharmony_ci        v3d.next_offset += create->size;
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci        create->offset = bo->offset;
72bf215546Sopenharmony_ci        create->handle = drm_shim_bo_get_handle(shim_fd, &bo->base);
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci        drm_shim_bo_put(&bo->base);
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci        return 0;
77bf215546Sopenharmony_ci}
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_cistatic int
80bf215546Sopenharmony_civ3d_ioctl_get_bo_offset(int fd, unsigned long request, void *arg)
81bf215546Sopenharmony_ci{
82bf215546Sopenharmony_ci        struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
83bf215546Sopenharmony_ci        struct drm_v3d_get_bo_offset *args = arg;
84bf215546Sopenharmony_ci        struct shim_bo *bo = drm_shim_bo_lookup(shim_fd, args->handle);
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci        args->offset = v3d_bo(bo)->offset;
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci        drm_shim_bo_put(bo);
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci        return 0;
91bf215546Sopenharmony_ci}
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_cistatic int
94bf215546Sopenharmony_civ3d_ioctl_mmap_bo(int fd, unsigned long request, void *arg)
95bf215546Sopenharmony_ci{
96bf215546Sopenharmony_ci        struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
97bf215546Sopenharmony_ci        struct drm_v3d_mmap_bo *map = arg;
98bf215546Sopenharmony_ci        struct shim_bo *bo = drm_shim_bo_lookup(shim_fd, map->handle);
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci        map->offset = drm_shim_bo_get_mmap_offset(shim_fd, bo);
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci        drm_shim_bo_put(bo);
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci        return 0;
105bf215546Sopenharmony_ci}
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_cistatic int
108bf215546Sopenharmony_civ3d_ioctl_get_param(int fd, unsigned long request, void *arg)
109bf215546Sopenharmony_ci{
110bf215546Sopenharmony_ci        struct drm_v3d_get_param *gp = arg;
111bf215546Sopenharmony_ci        static const uint32_t v3d42_reg_map[] = {
112bf215546Sopenharmony_ci                [DRM_V3D_PARAM_V3D_UIFCFG] = 0x00000045,
113bf215546Sopenharmony_ci                [DRM_V3D_PARAM_V3D_HUB_IDENT1] = 0x000e1124,
114bf215546Sopenharmony_ci                [DRM_V3D_PARAM_V3D_HUB_IDENT2] = 0x00000100,
115bf215546Sopenharmony_ci                [DRM_V3D_PARAM_V3D_HUB_IDENT3] = 0x00000e00,
116bf215546Sopenharmony_ci                [DRM_V3D_PARAM_V3D_CORE0_IDENT0] = 0x04443356,
117bf215546Sopenharmony_ci                [DRM_V3D_PARAM_V3D_CORE0_IDENT1] = 0x81001422,
118bf215546Sopenharmony_ci                [DRM_V3D_PARAM_V3D_CORE0_IDENT2] = 0x40078121,
119bf215546Sopenharmony_ci        };
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci        switch (gp->param) {
122bf215546Sopenharmony_ci        case DRM_V3D_PARAM_SUPPORTS_TFU:
123bf215546Sopenharmony_ci                gp->value = 1;
124bf215546Sopenharmony_ci                return 0;
125bf215546Sopenharmony_ci        default:
126bf215546Sopenharmony_ci                break;
127bf215546Sopenharmony_ci        }
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci        if (gp->param < ARRAY_SIZE(v3d42_reg_map) && v3d42_reg_map[gp->param]) {
130bf215546Sopenharmony_ci                gp->value = v3d42_reg_map[gp->param];
131bf215546Sopenharmony_ci                return 0;
132bf215546Sopenharmony_ci        }
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci        fprintf(stderr, "Unknown DRM_IOCTL_V3D_GET_PARAM %d\n", gp->param);
135bf215546Sopenharmony_ci        return -1;
136bf215546Sopenharmony_ci}
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_cistatic ioctl_fn_t driver_ioctls[] = {
139bf215546Sopenharmony_ci        [DRM_V3D_SUBMIT_CL] = v3d_ioctl_noop,
140bf215546Sopenharmony_ci        [DRM_V3D_SUBMIT_TFU] = v3d_ioctl_noop,
141bf215546Sopenharmony_ci        [DRM_V3D_WAIT_BO] = v3d_ioctl_noop,
142bf215546Sopenharmony_ci        [DRM_V3D_CREATE_BO] = v3d_ioctl_create_bo,
143bf215546Sopenharmony_ci        [DRM_V3D_GET_PARAM] = v3d_ioctl_get_param,
144bf215546Sopenharmony_ci        [DRM_V3D_GET_BO_OFFSET] = v3d_ioctl_get_bo_offset,
145bf215546Sopenharmony_ci        [DRM_V3D_MMAP_BO] = v3d_ioctl_mmap_bo,
146bf215546Sopenharmony_ci};
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_civoid
149bf215546Sopenharmony_cidrm_shim_driver_init(void)
150bf215546Sopenharmony_ci{
151bf215546Sopenharmony_ci        shim_device.bus_type = DRM_BUS_PLATFORM;
152bf215546Sopenharmony_ci        shim_device.driver_name = "v3d";
153bf215546Sopenharmony_ci        shim_device.driver_ioctls = driver_ioctls;
154bf215546Sopenharmony_ci        shim_device.driver_ioctl_count = ARRAY_SIZE(driver_ioctls);
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci        drm_shim_override_file("OF_FULLNAME=/rdb/v3d\n"
157bf215546Sopenharmony_ci                               "OF_COMPATIBLE_N=1\n"
158bf215546Sopenharmony_ci                               "OF_COMPATIBLE_0=brcm,7278-v3d\n",
159bf215546Sopenharmony_ci                               "/sys/dev/char/%d:%d/device/uevent",
160bf215546Sopenharmony_ci                               DRM_MAJOR, render_node_minor);
161bf215546Sopenharmony_ci}
162