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/panfrost_drm.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "util/u_math.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci/* Default GPU ID if PAN_GPU_ID is not set. This defaults to Mali-G52. */
34bf215546Sopenharmony_ci#define PAN_GPU_ID_DEFAULT (0x7212)
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_cibool drm_shim_driver_prefers_first_render_node = true;
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistatic int
39bf215546Sopenharmony_cipan_ioctl_noop(int fd, unsigned long request, void *arg)
40bf215546Sopenharmony_ci{
41bf215546Sopenharmony_ci   return 0;
42bf215546Sopenharmony_ci}
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_cistatic int
45bf215546Sopenharmony_cipan_ioctl_get_param(int fd, unsigned long request, void *arg)
46bf215546Sopenharmony_ci{
47bf215546Sopenharmony_ci   struct drm_panfrost_get_param *gp = arg;
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   switch (gp->param) {
50bf215546Sopenharmony_ci   case DRM_PANFROST_PARAM_GPU_PROD_ID:
51bf215546Sopenharmony_ci   {
52bf215546Sopenharmony_ci      char *override_version = getenv("PAN_GPU_ID");
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci      if (override_version)
55bf215546Sopenharmony_ci         gp->value = strtol(override_version, NULL, 16);
56bf215546Sopenharmony_ci      else
57bf215546Sopenharmony_ci         gp->value = PAN_GPU_ID_DEFAULT;
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci      return 0;
60bf215546Sopenharmony_ci   }
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   case DRM_PANFROST_PARAM_SHADER_PRESENT:
63bf215546Sopenharmony_ci      /* Assume an MP4 GPU */
64bf215546Sopenharmony_ci      gp->value = 0xF;
65bf215546Sopenharmony_ci      return 0;
66bf215546Sopenharmony_ci   case DRM_PANFROST_PARAM_TILER_FEATURES:
67bf215546Sopenharmony_ci      gp->value = 0x809;
68bf215546Sopenharmony_ci      return 0;
69bf215546Sopenharmony_ci   case DRM_PANFROST_PARAM_TEXTURE_FEATURES0:
70bf215546Sopenharmony_ci   case DRM_PANFROST_PARAM_TEXTURE_FEATURES1:
71bf215546Sopenharmony_ci      /* Allow all compressed textures */
72bf215546Sopenharmony_ci      gp->value = ~0;
73bf215546Sopenharmony_ci      return 0;
74bf215546Sopenharmony_ci   case DRM_PANFROST_PARAM_GPU_REVISION:
75bf215546Sopenharmony_ci   case DRM_PANFROST_PARAM_THREAD_TLS_ALLOC:
76bf215546Sopenharmony_ci   case DRM_PANFROST_PARAM_AFBC_FEATURES:
77bf215546Sopenharmony_ci      gp->value = 0;
78bf215546Sopenharmony_ci      return 0;
79bf215546Sopenharmony_ci   default:
80bf215546Sopenharmony_ci      fprintf(stderr, "Unknown DRM_IOCTL_PANFROST_GET_PARAM %d\n", gp->param);
81bf215546Sopenharmony_ci      return -1;
82bf215546Sopenharmony_ci   }
83bf215546Sopenharmony_ci}
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_cistatic int
86bf215546Sopenharmony_cipan_ioctl_create_bo(int fd, unsigned long request, void *arg)
87bf215546Sopenharmony_ci{
88bf215546Sopenharmony_ci   struct drm_panfrost_create_bo *create = arg;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
91bf215546Sopenharmony_ci   struct shim_bo *bo = calloc(1, sizeof(*bo));
92bf215546Sopenharmony_ci   size_t size = ALIGN(create->size, 4096);
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   drm_shim_bo_init(bo, size);
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   create->handle = drm_shim_bo_get_handle(shim_fd, bo);
97bf215546Sopenharmony_ci   create->offset = bo->mem_addr;
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   drm_shim_bo_put(bo);
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   return 0;
102bf215546Sopenharmony_ci}
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_cistatic int
105bf215546Sopenharmony_cipan_ioctl_mmap_bo(int fd, unsigned long request, void *arg)
106bf215546Sopenharmony_ci{
107bf215546Sopenharmony_ci   struct drm_panfrost_mmap_bo *mmap_bo = arg;
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
110bf215546Sopenharmony_ci   struct shim_bo *bo = drm_shim_bo_lookup(shim_fd, mmap_bo->handle);
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci   mmap_bo->offset = drm_shim_bo_get_mmap_offset(shim_fd, bo);
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   return 0;
115bf215546Sopenharmony_ci}
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_cistatic int
118bf215546Sopenharmony_cipan_ioctl_madvise(int fd, unsigned long request, void *arg)
119bf215546Sopenharmony_ci{
120bf215546Sopenharmony_ci   struct drm_panfrost_madvise *madvise = arg;
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci   madvise->retained = 1;
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   return 0;
125bf215546Sopenharmony_ci}
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_cistatic ioctl_fn_t driver_ioctls[] = {
128bf215546Sopenharmony_ci   [DRM_PANFROST_SUBMIT] = pan_ioctl_noop,
129bf215546Sopenharmony_ci   [DRM_PANFROST_WAIT_BO] = pan_ioctl_noop,
130bf215546Sopenharmony_ci   [DRM_PANFROST_CREATE_BO] = pan_ioctl_create_bo,
131bf215546Sopenharmony_ci   [DRM_PANFROST_MMAP_BO] = pan_ioctl_mmap_bo,
132bf215546Sopenharmony_ci   [DRM_PANFROST_GET_PARAM] = pan_ioctl_get_param,
133bf215546Sopenharmony_ci   [DRM_PANFROST_GET_BO_OFFSET] = pan_ioctl_noop,
134bf215546Sopenharmony_ci   [DRM_PANFROST_PERFCNT_ENABLE] = pan_ioctl_noop,
135bf215546Sopenharmony_ci   [DRM_PANFROST_PERFCNT_DUMP] = pan_ioctl_noop,
136bf215546Sopenharmony_ci   [DRM_PANFROST_MADVISE] = pan_ioctl_madvise,
137bf215546Sopenharmony_ci};
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_civoid
140bf215546Sopenharmony_cidrm_shim_driver_init(void)
141bf215546Sopenharmony_ci{
142bf215546Sopenharmony_ci   shim_device.bus_type = DRM_BUS_PLATFORM;
143bf215546Sopenharmony_ci   shim_device.driver_name = "panfrost";
144bf215546Sopenharmony_ci   shim_device.driver_ioctls = driver_ioctls;
145bf215546Sopenharmony_ci   shim_device.driver_ioctl_count = ARRAY_SIZE(driver_ioctls);
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   /* panfrost uses the DRM version to expose features, instead of getparam. */
148bf215546Sopenharmony_ci   shim_device.version_major = 1;
149bf215546Sopenharmony_ci   shim_device.version_minor = 1;
150bf215546Sopenharmony_ci   shim_device.version_patchlevel = 0;
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci   drm_shim_override_file("DRIVER=panfrost\n"
153bf215546Sopenharmony_ci                          "OF_FULLNAME=/soc/mali\n"
154bf215546Sopenharmony_ci                          "OF_COMPATIBLE_0=arm,mali-t860\n"
155bf215546Sopenharmony_ci                          "OF_COMPATIBLE_N=1\n",
156bf215546Sopenharmony_ci                          "/sys/dev/char/%d:%d/device/uevent", DRM_MAJOR,
157bf215546Sopenharmony_ci                          render_node_minor);
158bf215546Sopenharmony_ci}
159