1bf215546Sopenharmony_ci#include <limits.h>
2bf215546Sopenharmony_ci#include <stdio.h>
3bf215546Sopenharmony_ci#include <stdlib.h>
4bf215546Sopenharmony_ci#include "common/amd_family.h"
5bf215546Sopenharmony_ci#include "drm-shim/drm_shim.h"
6bf215546Sopenharmony_ci#include "util/log.h"
7bf215546Sopenharmony_ci#include <util/u_math.h>
8bf215546Sopenharmony_ci#include <radeon_drm.h>
9bf215546Sopenharmony_ci
10bf215546Sopenharmony_cibool drm_shim_driver_prefers_first_render_node = true;
11bf215546Sopenharmony_ci
12bf215546Sopenharmony_cistatic enum radeon_family radeon_family = CHIP_RV515;
13bf215546Sopenharmony_cistatic uint16_t device_id = 0x7140;
14bf215546Sopenharmony_ci
15bf215546Sopenharmony_cistatic int
16bf215546Sopenharmony_ciradeon_ioctl_noop(int fd, unsigned long request, void *arg)
17bf215546Sopenharmony_ci{
18bf215546Sopenharmony_ci   return 0;
19bf215546Sopenharmony_ci}
20bf215546Sopenharmony_ci
21bf215546Sopenharmony_cistatic int
22bf215546Sopenharmony_ciradeon_ioctl_info(int fd, unsigned long request, void *arg)
23bf215546Sopenharmony_ci{
24bf215546Sopenharmony_ci   struct drm_radeon_info *info = arg;
25bf215546Sopenharmony_ci   uint32_t *value = (uint32_t *)(intptr_t)info->value;
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci   switch (info->request) {
28bf215546Sopenharmony_ci   case RADEON_INFO_DEVICE_ID:
29bf215546Sopenharmony_ci      *value = device_id;
30bf215546Sopenharmony_ci      return 0;
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci   case RADEON_INFO_RING_WORKING:
33bf215546Sopenharmony_ci   case RADEON_INFO_ACCEL_WORKING2:
34bf215546Sopenharmony_ci   case RADEON_INFO_VA_UNMAP_WORKING:
35bf215546Sopenharmony_ci      *value = true;
36bf215546Sopenharmony_ci      return 0;
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   case RADEON_INFO_GPU_RESET_COUNTER:
39bf215546Sopenharmony_ci      *value = 0;
40bf215546Sopenharmony_ci      return 0;
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci   case RADEON_INFO_IB_VM_MAX_SIZE:
43bf215546Sopenharmony_ci      if (radeon_family < CHIP_CAYMAN)
44bf215546Sopenharmony_ci         return -EINVAL;
45bf215546Sopenharmony_ci      *value = 64 << 10;
46bf215546Sopenharmony_ci      return 0;
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   case RADEON_INFO_VCE_FW_VERSION:
49bf215546Sopenharmony_ci   case RADEON_INFO_TILING_CONFIG:
50bf215546Sopenharmony_ci   case RADEON_INFO_BACKEND_MAP:
51bf215546Sopenharmony_ci      *value = 0; /* dummy */
52bf215546Sopenharmony_ci      return 0;
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci   case RADEON_INFO_VA_START:
55bf215546Sopenharmony_ci      return 4096;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   case RADEON_INFO_MAX_SCLK:
58bf215546Sopenharmony_ci   case RADEON_INFO_CLOCK_CRYSTAL_FREQ:
59bf215546Sopenharmony_ci   case RADEON_INFO_NUM_GB_PIPES:
60bf215546Sopenharmony_ci   case RADEON_INFO_NUM_Z_PIPES:
61bf215546Sopenharmony_ci   case RADEON_INFO_MAX_PIPES:
62bf215546Sopenharmony_ci   case RADEON_INFO_MAX_SE:
63bf215546Sopenharmony_ci   case RADEON_INFO_MAX_SH_PER_SE:
64bf215546Sopenharmony_ci   case RADEON_INFO_ACTIVE_CU_COUNT:
65bf215546Sopenharmony_ci   case RADEON_INFO_NUM_BACKENDS:
66bf215546Sopenharmony_ci   case RADEON_INFO_NUM_TILE_PIPES:
67bf215546Sopenharmony_ci      *value = 1; /* dummy */
68bf215546Sopenharmony_ci      return 0;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   default:
71bf215546Sopenharmony_ci      fprintf(stderr, "Unknown DRM_IOCTL_RADEON_INFO request 0x%02X\n", info->request);
72bf215546Sopenharmony_ci      return -1;
73bf215546Sopenharmony_ci   }
74bf215546Sopenharmony_ci}
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_cistatic int
77bf215546Sopenharmony_ciradeon_ioctl_gem_info(int fd, unsigned long request, void *arg)
78bf215546Sopenharmony_ci{
79bf215546Sopenharmony_ci   struct drm_radeon_gem_info *info = arg;
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   /* Dummy values. */
82bf215546Sopenharmony_ci   info->vram_size = 256 * 1024 * 1024;
83bf215546Sopenharmony_ci   info->vram_visible = info->vram_size;
84bf215546Sopenharmony_ci   info->gart_size = 512 * 1024 * 1024;
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   return 0;
87bf215546Sopenharmony_ci}
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_cistatic int
90bf215546Sopenharmony_ciradeon_ioctl_gem_create(int fd, unsigned long request, void *arg)
91bf215546Sopenharmony_ci{
92bf215546Sopenharmony_ci   struct drm_radeon_gem_create *create = arg;
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
95bf215546Sopenharmony_ci   struct shim_bo *bo = calloc(1, sizeof(*bo));
96bf215546Sopenharmony_ci   size_t size = ALIGN(create->size, 4096);
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci   drm_shim_bo_init(bo, size);
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci   create->handle = drm_shim_bo_get_handle(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_ciradeon_ioctl_gem_mmap(int fd, unsigned long request, void *arg)
109bf215546Sopenharmony_ci{
110bf215546Sopenharmony_ci   struct drm_radeon_gem_mmap *mmap_bo = arg;
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci   struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
113bf215546Sopenharmony_ci   struct shim_bo *bo = drm_shim_bo_lookup(shim_fd, mmap_bo->handle);
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci   mmap_bo->addr_ptr = drm_shim_bo_get_mmap_offset(shim_fd, bo);
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci   return 0;
118bf215546Sopenharmony_ci}
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_cistatic int
121bf215546Sopenharmony_ciradeon_ioctl_gem_userptr(int fd, unsigned long request, void *arg)
122bf215546Sopenharmony_ci{
123bf215546Sopenharmony_ci   /* probed at winsys init, just return no support. */
124bf215546Sopenharmony_ci   return -EINVAL;
125bf215546Sopenharmony_ci}
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_cistatic ioctl_fn_t driver_ioctls[] = {
128bf215546Sopenharmony_ci   [DRM_RADEON_CS] = radeon_ioctl_noop,
129bf215546Sopenharmony_ci   [DRM_RADEON_INFO] = radeon_ioctl_info,
130bf215546Sopenharmony_ci   [DRM_RADEON_GEM_SET_DOMAIN] = radeon_ioctl_noop,
131bf215546Sopenharmony_ci   [DRM_RADEON_GEM_SET_TILING] = radeon_ioctl_noop,
132bf215546Sopenharmony_ci   [DRM_RADEON_GEM_WAIT_IDLE] = radeon_ioctl_noop,
133bf215546Sopenharmony_ci   [DRM_RADEON_GEM_INFO] = radeon_ioctl_gem_info,
134bf215546Sopenharmony_ci   [DRM_RADEON_GEM_CREATE] = radeon_ioctl_gem_create,
135bf215546Sopenharmony_ci   [DRM_RADEON_GEM_MMAP] = radeon_ioctl_gem_mmap,
136bf215546Sopenharmony_ci   [DRM_RADEON_GEM_USERPTR] = radeon_ioctl_gem_userptr,
137bf215546Sopenharmony_ci};
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_cistruct radeon_pci_id {
140bf215546Sopenharmony_ci   uint16_t device_id;
141bf215546Sopenharmony_ci   const char *name;
142bf215546Sopenharmony_ci   enum radeon_family family;
143bf215546Sopenharmony_ci   const char *family_name;
144bf215546Sopenharmony_ci};
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci#define CHIPSET(d, n, f) {.device_id = (d), .name = #n, .family = CHIP_##f, .family_name = #f},
147bf215546Sopenharmony_cistatic const struct radeon_pci_id radeon_pci_ids[] = {
148bf215546Sopenharmony_ci#include "pci_ids/r300_pci_ids.h"
149bf215546Sopenharmony_ci#include "pci_ids/r600_pci_ids.h"
150bf215546Sopenharmony_ci};
151bf215546Sopenharmony_ci#undef CHIPSET
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_cistatic void
154bf215546Sopenharmony_ciradeon_get_device_id()
155bf215546Sopenharmony_ci{
156bf215546Sopenharmony_ci   const char *gpu_id = getenv("RADEON_GPU_ID");
157bf215546Sopenharmony_ci   if (!gpu_id)
158bf215546Sopenharmony_ci      return;
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci   if (strncmp(gpu_id, "0x", 2) == 0) {
161bf215546Sopenharmony_ci      device_id = strtoll(gpu_id + 2, NULL, 16);
162bf215546Sopenharmony_ci      return;
163bf215546Sopenharmony_ci   }
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   for (int i = 0; i < ARRAY_SIZE(radeon_pci_ids); i++) {
166bf215546Sopenharmony_ci      if (strcasecmp(gpu_id, radeon_pci_ids[i].name) == 0 ||
167bf215546Sopenharmony_ci          strcasecmp(gpu_id, radeon_pci_ids[i].family_name) == 0) {
168bf215546Sopenharmony_ci         device_id = radeon_pci_ids[i].device_id;
169bf215546Sopenharmony_ci         return;
170bf215546Sopenharmony_ci      }
171bf215546Sopenharmony_ci   }
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci   mesa_loge("Failed to find radeon GPU named \"%s\"\n", gpu_id);
174bf215546Sopenharmony_ci   abort();
175bf215546Sopenharmony_ci}
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_civoid
178bf215546Sopenharmony_cidrm_shim_driver_init(void)
179bf215546Sopenharmony_ci{
180bf215546Sopenharmony_ci   radeon_get_device_id();
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci   shim_device.bus_type = DRM_BUS_PCI;
183bf215546Sopenharmony_ci   shim_device.driver_name = "radeon";
184bf215546Sopenharmony_ci   shim_device.driver_ioctls = driver_ioctls;
185bf215546Sopenharmony_ci   shim_device.driver_ioctl_count = ARRAY_SIZE(driver_ioctls);
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci   shim_device.version_major = 2;
188bf215546Sopenharmony_ci   shim_device.version_minor = 50;
189bf215546Sopenharmony_ci   shim_device.version_patchlevel = 0;
190bf215546Sopenharmony_ci}
191