1/*
2 * Copyright © 2018 Broadcom
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include <limits.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/ioctl.h>
28#include "drm-uapi/v3d_drm.h"
29#include "drm-shim/drm_shim.h"
30
31bool drm_shim_driver_prefers_first_render_node = true;
32
33struct v3d_bo {
34        struct shim_bo base;
35        uint32_t offset;
36};
37
38static struct v3d_bo *
39v3d_bo(struct shim_bo *bo)
40{
41        return (struct v3d_bo *)bo;
42}
43
44struct v3d_device {
45        uint32_t next_offset;
46};
47
48static struct v3d_device v3d = {
49        .next_offset = 0x1000,
50};
51
52static int
53v3d_ioctl_noop(int fd, unsigned long request, void *arg)
54{
55        return 0;
56}
57
58static int
59v3d_ioctl_create_bo(int fd, unsigned long request, void *arg)
60{
61        struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
62        struct drm_v3d_create_bo *create = arg;
63        struct v3d_bo *bo = calloc(1, sizeof(*bo));
64
65        drm_shim_bo_init(&bo->base, create->size);
66
67        assert(UINT_MAX - v3d.next_offset > create->size);
68        bo->offset = v3d.next_offset;
69        v3d.next_offset += create->size;
70
71        create->offset = bo->offset;
72        create->handle = drm_shim_bo_get_handle(shim_fd, &bo->base);
73
74        drm_shim_bo_put(&bo->base);
75
76        return 0;
77}
78
79static int
80v3d_ioctl_get_bo_offset(int fd, unsigned long request, void *arg)
81{
82        struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
83        struct drm_v3d_get_bo_offset *args = arg;
84        struct shim_bo *bo = drm_shim_bo_lookup(shim_fd, args->handle);
85
86        args->offset = v3d_bo(bo)->offset;
87
88        drm_shim_bo_put(bo);
89
90        return 0;
91}
92
93static int
94v3d_ioctl_mmap_bo(int fd, unsigned long request, void *arg)
95{
96        struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
97        struct drm_v3d_mmap_bo *map = arg;
98        struct shim_bo *bo = drm_shim_bo_lookup(shim_fd, map->handle);
99
100        map->offset = drm_shim_bo_get_mmap_offset(shim_fd, bo);
101
102        drm_shim_bo_put(bo);
103
104        return 0;
105}
106
107static int
108v3d_ioctl_get_param(int fd, unsigned long request, void *arg)
109{
110        struct drm_v3d_get_param *gp = arg;
111        static const uint32_t v3d42_reg_map[] = {
112                [DRM_V3D_PARAM_V3D_UIFCFG] = 0x00000045,
113                [DRM_V3D_PARAM_V3D_HUB_IDENT1] = 0x000e1124,
114                [DRM_V3D_PARAM_V3D_HUB_IDENT2] = 0x00000100,
115                [DRM_V3D_PARAM_V3D_HUB_IDENT3] = 0x00000e00,
116                [DRM_V3D_PARAM_V3D_CORE0_IDENT0] = 0x04443356,
117                [DRM_V3D_PARAM_V3D_CORE0_IDENT1] = 0x81001422,
118                [DRM_V3D_PARAM_V3D_CORE0_IDENT2] = 0x40078121,
119        };
120
121        switch (gp->param) {
122        case DRM_V3D_PARAM_SUPPORTS_TFU:
123                gp->value = 1;
124                return 0;
125        default:
126                break;
127        }
128
129        if (gp->param < ARRAY_SIZE(v3d42_reg_map) && v3d42_reg_map[gp->param]) {
130                gp->value = v3d42_reg_map[gp->param];
131                return 0;
132        }
133
134        fprintf(stderr, "Unknown DRM_IOCTL_V3D_GET_PARAM %d\n", gp->param);
135        return -1;
136}
137
138static ioctl_fn_t driver_ioctls[] = {
139        [DRM_V3D_SUBMIT_CL] = v3d_ioctl_noop,
140        [DRM_V3D_SUBMIT_TFU] = v3d_ioctl_noop,
141        [DRM_V3D_WAIT_BO] = v3d_ioctl_noop,
142        [DRM_V3D_CREATE_BO] = v3d_ioctl_create_bo,
143        [DRM_V3D_GET_PARAM] = v3d_ioctl_get_param,
144        [DRM_V3D_GET_BO_OFFSET] = v3d_ioctl_get_bo_offset,
145        [DRM_V3D_MMAP_BO] = v3d_ioctl_mmap_bo,
146};
147
148void
149drm_shim_driver_init(void)
150{
151        shim_device.bus_type = DRM_BUS_PLATFORM;
152        shim_device.driver_name = "v3d";
153        shim_device.driver_ioctls = driver_ioctls;
154        shim_device.driver_ioctl_count = ARRAY_SIZE(driver_ioctls);
155
156        drm_shim_override_file("OF_FULLNAME=/rdb/v3d\n"
157                               "OF_COMPATIBLE_N=1\n"
158                               "OF_COMPATIBLE_0=brcm,7278-v3d\n",
159                               "/sys/dev/char/%d:%d/device/uevent",
160                               DRM_MAJOR, render_node_minor);
161}
162