2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
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
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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Corbin Simpson <MostAwesomeDude@gmail.com>
26 * Joakim Sindholt <opensource@zhasha.com>
30 #include <sys/ioctl.h>
31 #include "util/u_inlines.h"
32 #include "util/u_debug.h"
33 #include "util/u_hash_table.h"
34 #include <pipebuffer/pb_bufmgr.h>
36 #include "r600_priv.h"
37 #include "r600_drm_public.h"
39 #include "radeon_drm.h"
41 #ifndef RADEON_INFO_TILING_CONFIG
42 #define RADEON_INFO_TILING_CONFIG 0x6
45 #ifndef RADEON_INFO_CLOCK_CRYSTAL_FREQ
46 #define RADEON_INFO_CLOCK_CRYSTAL_FREQ 0x9
49 #ifndef RADEON_INFO_NUM_BACKENDS
50 #define RADEON_INFO_NUM_BACKENDS 0xa
53 #ifndef RADEON_INFO_NUM_TILE_PIPES
54 #define RADEON_INFO_NUM_TILE_PIPES 0xb
57 #ifndef RADEON_INFO_BACKEND_MAP
58 #define RADEON_INFO_BACKEND_MAP 0xd
61 enum radeon_family r600_get_family(struct radeon *r600)
66 enum chip_class r600_get_family_class(struct radeon *radeon)
68 return radeon->chip_class;
71 struct r600_tiling_info *r600_get_tiling_info(struct radeon *radeon)
73 return &radeon->tiling_info;
76 unsigned r600_get_clock_crystal_freq(struct radeon *radeon)
78 return radeon->clock_crystal_freq;
81 unsigned r600_get_num_backends(struct radeon *radeon)
83 return radeon->num_backends;
86 unsigned r600_get_num_tile_pipes(struct radeon *radeon)
88 return radeon->num_tile_pipes;
91 unsigned r600_get_backend_map(struct radeon *radeon)
93 return radeon->backend_map;
96 unsigned r600_get_minor_version(struct radeon *radeon)
98 return radeon->minor_version;
102 static int radeon_get_device(struct radeon *radeon)
104 struct drm_radeon_info info = {};
108 info.request = RADEON_INFO_DEVICE_ID;
109 info.value = (uintptr_t)&radeon->device;
110 r = drmCommandWriteRead(radeon->fd, DRM_RADEON_INFO, &info,
111 sizeof(struct drm_radeon_info));
115 static int r600_interpret_tiling(struct radeon *radeon, uint32_t tiling_config)
117 switch ((tiling_config & 0xe) >> 1) {
119 radeon->tiling_info.num_channels = 1;
122 radeon->tiling_info.num_channels = 2;
125 radeon->tiling_info.num_channels = 4;
128 radeon->tiling_info.num_channels = 8;
134 switch ((tiling_config & 0x30) >> 4) {
136 radeon->tiling_info.num_banks = 4;
139 radeon->tiling_info.num_banks = 8;
145 switch ((tiling_config & 0xc0) >> 6) {
147 radeon->tiling_info.group_bytes = 256;
150 radeon->tiling_info.group_bytes = 512;
158 static int eg_interpret_tiling(struct radeon *radeon, uint32_t tiling_config)
160 switch (tiling_config & 0xf) {
162 radeon->tiling_info.num_channels = 1;
165 radeon->tiling_info.num_channels = 2;
168 radeon->tiling_info.num_channels = 4;
171 radeon->tiling_info.num_channels = 8;
177 switch ((tiling_config & 0xf0) >> 4) {
179 radeon->tiling_info.num_banks = 4;
182 radeon->tiling_info.num_banks = 8;
185 radeon->tiling_info.num_banks = 16;
192 switch ((tiling_config & 0xf00) >> 8) {
194 radeon->tiling_info.group_bytes = 256;
197 radeon->tiling_info.group_bytes = 512;
205 static int radeon_drm_get_tiling(struct radeon *radeon)
207 struct drm_radeon_info info = {};
209 uint32_t tiling_config = 0;
211 info.request = RADEON_INFO_TILING_CONFIG;
212 info.value = (uintptr_t)&tiling_config;
213 r = drmCommandWriteRead(radeon->fd, DRM_RADEON_INFO, &info,
214 sizeof(struct drm_radeon_info));
219 if (radeon->chip_class == R600 || radeon->chip_class == R700) {
220 r = r600_interpret_tiling(radeon, tiling_config);
222 r = eg_interpret_tiling(radeon, tiling_config);
227 static int radeon_get_clock_crystal_freq(struct radeon *radeon)
229 struct drm_radeon_info info = {};
230 uint32_t clock_crystal_freq = 0;
233 info.request = RADEON_INFO_CLOCK_CRYSTAL_FREQ;
234 info.value = (uintptr_t)&clock_crystal_freq;
235 r = drmCommandWriteRead(radeon->fd, DRM_RADEON_INFO, &info,
236 sizeof(struct drm_radeon_info));
240 radeon->clock_crystal_freq = clock_crystal_freq;
245 static int radeon_get_num_backends(struct radeon *radeon)
247 struct drm_radeon_info info = {};
248 uint32_t num_backends = 0;
251 info.request = RADEON_INFO_NUM_BACKENDS;
252 info.value = (uintptr_t)&num_backends;
253 r = drmCommandWriteRead(radeon->fd, DRM_RADEON_INFO, &info,
254 sizeof(struct drm_radeon_info));
258 radeon->num_backends = num_backends;
262 static int radeon_get_num_tile_pipes(struct radeon *radeon)
264 struct drm_radeon_info info = {};
265 uint32_t num_tile_pipes = 0;
268 info.request = RADEON_INFO_NUM_TILE_PIPES;
269 info.value = (uintptr_t)&num_tile_pipes;
270 r = drmCommandWriteRead(radeon->fd, DRM_RADEON_INFO, &info,
271 sizeof(struct drm_radeon_info));
275 radeon->num_tile_pipes = num_tile_pipes;
279 static int radeon_get_backend_map(struct radeon *radeon)
281 struct drm_radeon_info info = {};
282 uint32_t backend_map = 0;
285 info.request = RADEON_INFO_BACKEND_MAP;
286 info.value = (uintptr_t)&backend_map;
287 r = drmCommandWriteRead(radeon->fd, DRM_RADEON_INFO, &info,
288 sizeof(struct drm_radeon_info));
292 radeon->backend_map = backend_map;
293 radeon->backend_map_valid = TRUE;
299 static int radeon_init_fence(struct radeon *radeon)
302 radeon->fence_bo = r600_bo(radeon, 4096, 0, 0, 0);
303 if (radeon->fence_bo == NULL) {
306 radeon->cfence = r600_bo_map(radeon, radeon->fence_bo, PB_USAGE_UNSYNCHRONIZED, NULL);
311 #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
313 static unsigned handle_hash(void *key)
315 return PTR_TO_UINT(key);
318 static int handle_compare(void *key1, void *key2)
320 return PTR_TO_UINT(key1) != PTR_TO_UINT(key2);
323 static struct radeon *radeon_new(int fd, unsigned device)
325 struct radeon *radeon;
327 drmVersionPtr version;
329 radeon = calloc(1, sizeof(*radeon));
330 if (radeon == NULL) {
334 radeon->device = device;
335 radeon->refcount = 1;
337 version = drmGetVersion(radeon->fd);
338 if (version->version_major != 2) {
339 fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
340 "only compatible with 2.x.x\n", __FUNCTION__,
341 version->version_major, version->version_minor,
342 version->version_patchlevel);
343 drmFreeVersion(version);
347 radeon->minor_version = version->version_minor;
349 drmFreeVersion(version);
351 r = radeon_get_device(radeon);
353 fprintf(stderr, "Failed to get device id\n");
354 return radeon_decref(radeon);
357 radeon->family = radeon_family_from_device(radeon->device);
358 if (radeon->family == CHIP_UNKNOWN) {
359 fprintf(stderr, "Unknown chipset 0x%04X\n", radeon->device);
360 return radeon_decref(radeon);
363 switch (radeon->family) {
372 radeon->chip_class = R600;
373 /* set default group bytes, overridden by tiling info ioctl */
374 radeon->tiling_info.group_bytes = 256;
380 radeon->chip_class = R700;
381 /* set default group bytes, overridden by tiling info ioctl */
382 radeon->tiling_info.group_bytes = 256;
395 radeon->chip_class = EVERGREEN;
396 /* set default group bytes, overridden by tiling info ioctl */
397 radeon->tiling_info.group_bytes = 512;
400 radeon->chip_class = CAYMAN;
401 /* set default group bytes, overridden by tiling info ioctl */
402 radeon->tiling_info.group_bytes = 512;
405 fprintf(stderr, "%s unknown or unsupported chipset 0x%04X\n",
406 __func__, radeon->device);
410 if (radeon_drm_get_tiling(radeon))
413 /* get the GPU counter frequency, failure is non fatal */
414 radeon_get_clock_crystal_freq(radeon);
416 if (radeon->minor_version >= 9)
417 radeon_get_num_backends(radeon);
419 if (radeon->minor_version >= 11) {
420 radeon_get_num_tile_pipes(radeon);
421 radeon_get_backend_map(radeon);
424 radeon->bomgr = r600_bomgr_create(radeon, 1000000);
425 if (radeon->bomgr == NULL) {
428 r = radeon_init_fence(radeon);
430 radeon_decref(radeon);
434 radeon->bo_handles = util_hash_table_create(handle_hash, handle_compare);
435 pipe_mutex_init(radeon->bo_handles_mutex);
439 struct radeon *r600_drm_winsys_create(int drmfd)
441 return radeon_new(drmfd, 0);
444 struct radeon *radeon_decref(struct radeon *radeon)
448 if (--radeon->refcount > 0) {
452 util_hash_table_destroy(radeon->bo_handles);
453 pipe_mutex_destroy(radeon->bo_handles_mutex);
454 if (radeon->fence_bo) {
455 r600_bo_reference(radeon, &radeon->fence_bo, NULL);
459 r600_bomgr_destroy(radeon->bomgr);