blob: e09b16a55f7aab63bcf2656f77c57a632feca620 [file] [log] [blame]
Chia-I Wu2ec32d42011-06-12 16:21:30 +08001/*
2 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
3 * Copyright (C) 2010-2011 LunarG Inc.
4 *
5 * Based on xf86-video-ati, which has
6 *
7 * Copyright © 2009 Red Hat, Inc.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 */
27
28/* XXX This driver assumes evergreen. */
29
30#define LOG_TAG "GRALLOC-RADEON"
31
32#include <cutils/log.h>
33#include <stdlib.h>
34#include <errno.h>
35#include <drm.h>
36#include <radeon_drm.h>
37#include <radeon_bo_gem.h>
38#include <radeon_bo.h>
39
40#include "gralloc_drm.h"
41#include "gralloc_drm_priv.h"
42
43#define RADEON_GPU_PAGE_SIZE 4096
44
45#define MAX(a, b) (((a) > (b)) ? (a) : (b))
46#define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
47
Chia-I Wu896fbd12011-07-14 14:42:30 +080048enum {
49 CHIP_FAMILY_R600,
50 CHIP_FAMILY_CEDAR,
51 CHIP_FAMILY_PALM,
52 CHIP_FAMILY_LAST
53};
54
Chia-I Wu2ec32d42011-06-12 16:21:30 +080055struct radeon_info {
56 struct gralloc_drm_drv_t base;
57
58 int fd;
59 struct radeon_bo_manager *bufmgr;
60
Chia-I Wu896fbd12011-07-14 14:42:30 +080061 int chipset;
62 int chip_family;
63
Chia-I Wu2ec32d42011-06-12 16:21:30 +080064 uint32_t tile_config;
65 int num_channels;
66 int num_banks;
67 int group_bytes;
68 /* r6xx+ tile config */
69 int have_tiling_info;
Chia-I Wu896fbd12011-07-14 14:42:30 +080070
Chia-I Wu2ec32d42011-06-12 16:21:30 +080071 int allow_color_tiling;
Chia-I Wu896fbd12011-07-14 14:42:30 +080072
73 int vram_size;
74 int gart_size;
Chia-I Wu2ec32d42011-06-12 16:21:30 +080075};
76
77struct radeon_buffer {
78 struct gralloc_drm_bo_t base;
79
80 struct radeon_bo *rbo;
81};
82
Chia-I Wu2ec32d42011-06-12 16:21:30 +080083/* returns pitch alignment in pixels */
Chia-I Wu896fbd12011-07-14 14:42:30 +080084static int radeon_get_pitch_align(struct radeon_info *info, int bpe, uint32_t tiling)
Chia-I Wu2ec32d42011-06-12 16:21:30 +080085{
86 int pitch_align = 1;
87
Chia-I Wu896fbd12011-07-14 14:42:30 +080088 if (info->chip_family >= CHIP_FAMILY_R600) {
89 if (tiling & RADEON_TILING_MACRO) {
90 /* general surface requirements */
91 pitch_align = (((info->group_bytes / 8) / bpe) *
92 info->num_banks) * 8;
93 /* further restrictions for scanout */
94 pitch_align = MAX(info->num_banks * 8, pitch_align);
95 } else if (tiling & RADEON_TILING_MICRO) {
96 /* general surface requirements */
97 pitch_align = MAX(8, (info->group_bytes / (8 * bpe)));
98 /* further restrictions for scanout */
99 pitch_align = MAX(info->group_bytes / bpe, pitch_align);
100 } else {
101 if (info->have_tiling_info)
102 /* linear aligned requirements */
103 pitch_align = MAX(64, info->group_bytes / bpe);
104 else
105 /* default to 512 elements if we don't know the real
106 * group size otherwise the kernel may reject the CS
107 * if the group sizes don't match as the pitch won't
108 * be aligned properly.
109 */
110 pitch_align = 512;
111 }
112 }
113 else {
114 /* general surface requirements */
115 if (tiling)
116 pitch_align = 256 / bpe;
117 else
118 pitch_align = 64;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800119 }
120
121 return pitch_align;
122}
123
124/* returns height alignment in pixels */
Chia-I Wu896fbd12011-07-14 14:42:30 +0800125static int radeon_get_height_align(struct radeon_info *info, uint32_t tiling)
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800126{
127 int height_align = 1;
128
Chia-I Wu896fbd12011-07-14 14:42:30 +0800129 if (info->chip_family >= CHIP_FAMILY_R600) {
130 if (tiling & RADEON_TILING_MACRO)
131 height_align = info->num_channels * 8;
132 else if (tiling & RADEON_TILING_MICRO)
133 height_align = 8;
134 else
135 height_align = 8;
136 }
137 else {
138 if (tiling)
139 height_align = 16;
140 else
141 height_align = 1;
142 }
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800143
144 return height_align;
145}
146
147/* returns base alignment in bytes */
Chia-I Wu896fbd12011-07-14 14:42:30 +0800148static int radeon_get_base_align(struct radeon_info *info,
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800149 int bpe, uint32_t tiling)
150{
Chia-I Wu896fbd12011-07-14 14:42:30 +0800151 int pixel_align = radeon_get_pitch_align(info, bpe, tiling);
152 int height_align = radeon_get_height_align(info, tiling);
153 int base_align = RADEON_GPU_PAGE_SIZE;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800154
Chia-I Wu896fbd12011-07-14 14:42:30 +0800155 if (info->chip_family >= CHIP_FAMILY_R600) {
156 if (tiling & RADEON_TILING_MACRO)
157 base_align = MAX(info->num_banks * info->num_channels * 8 * 8 * bpe,
158 pixel_align * bpe * height_align);
159 else {
160 if (info->have_tiling_info)
161 base_align = info->group_bytes;
162 else
163 /* default to 512 if we don't know the real
164 * group size otherwise the kernel may reject the CS
165 * if the group sizes don't match as the base won't
166 * be aligned properly.
167 */
168 base_align = 512;
169 }
170 }
171 return base_align;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800172}
173
Chia-I Wucbd62792011-07-15 10:18:58 +0800174static uint32_t radeon_get_tiling(struct radeon_info *info,
175 const struct gralloc_drm_handle_t *handle)
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800176{
Chia-I Wucbd62792011-07-15 10:18:58 +0800177 int sw = (GRALLOC_USAGE_SW_WRITE_MASK | GRALLOC_USAGE_SW_READ_MASK);
178
179 if ((handle->usage & sw) && !info->allow_color_tiling)
180 return 0;
181
182 if (info->chip_family >= CHIP_FAMILY_R600)
183 return RADEON_TILING_MICRO;
184 else
185 return RADEON_TILING_MACRO;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800186}
187
188static struct gralloc_drm_bo_t *
189drm_gem_radeon_alloc(struct gralloc_drm_drv_t *drv, struct gralloc_drm_handle_t *handle)
190{
191 struct radeon_info *info = (struct radeon_info *) drv;
192 struct radeon_buffer *rbuf;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800193 int cpp;
194
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800195 cpp = gralloc_drm_get_bpp(handle->format);
196 if (!cpp) {
197 LOGE("unrecognized format 0x%x", handle->format);
198 return NULL;
199 }
200
201 rbuf = calloc(1, sizeof(*rbuf));
202 if (!rbuf)
203 return NULL;
204
205
206 if (handle->name) {
Chia-I Wucbd62792011-07-15 10:18:58 +0800207 rbuf->rbo = radeon_bo_open(info->bufmgr,
208 handle->name, 0, 0, 0, 0);
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800209 if (!rbuf->rbo) {
210 LOGE("failed to create rbo from name %u",
211 handle->name);
212 free(rbuf);
213 return NULL;
214 }
215 }
216 else {
217 int aligned_width, aligned_height;
218 int pitch, size, base_align;
Chia-I Wucbd62792011-07-15 10:18:58 +0800219 uint32_t tiling, domain;
220
221 tiling = radeon_get_tiling(info, handle);
222 domain = RADEON_GEM_DOMAIN_VRAM;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800223
224 if (handle->usage & (GRALLOC_USAGE_HW_FB |
225 GRALLOC_USAGE_HW_TEXTURE)) {
226 aligned_width = ALIGN(handle->width,
Chia-I Wu896fbd12011-07-14 14:42:30 +0800227 radeon_get_pitch_align(info, cpp, tiling));
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800228 aligned_height = ALIGN(handle->height,
Chia-I Wu896fbd12011-07-14 14:42:30 +0800229 radeon_get_height_align(info, tiling));
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800230 }
231 else {
232 aligned_width = handle->width;
233 aligned_height = handle->height;
234 }
235
236 if (!(handle->usage & (GRALLOC_USAGE_HW_FB |
237 GRALLOC_USAGE_HW_RENDER)) &&
238 (handle->usage & GRALLOC_USAGE_SW_READ_OFTEN))
239 domain = RADEON_GEM_DOMAIN_GTT;
240
241 pitch = aligned_width * cpp;
242 size = ALIGN(aligned_height * pitch, RADEON_GPU_PAGE_SIZE);
Chia-I Wu896fbd12011-07-14 14:42:30 +0800243 base_align = radeon_get_base_align(info, cpp, tiling);
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800244
245 rbuf->rbo = radeon_bo_open(info->bufmgr, 0,
246 size, base_align, domain, 0);
247 if (!rbuf->rbo) {
248 LOGE("failed to allocate rbo %dx%dx%d",
249 handle->width, handle->height, cpp);
250 free(rbuf);
251 return NULL;
252 }
253
254 if (tiling)
255 radeon_bo_set_tiling(rbuf->rbo, tiling, pitch);
256
257 if (radeon_gem_get_kernel_name(rbuf->rbo,
258 (uint32_t *) &handle->name)) {
259 LOGE("failed to flink rbo");
260 radeon_bo_unref(rbuf->rbo);
261 free(rbuf);
262 return NULL;
263 }
264
265 handle->stride = pitch;
266 }
267
268 if (handle->usage & GRALLOC_USAGE_HW_FB)
269 rbuf->base.fb_handle = rbuf->rbo->handle;
270
271 rbuf->base.handle = handle;
272
273 return &rbuf->base;
274}
275
276static void drm_gem_radeon_free(struct gralloc_drm_drv_t *drv,
277 struct gralloc_drm_bo_t *bo)
278{
279 struct radeon_buffer *rbuf = (struct radeon_buffer *) bo;
280 radeon_bo_unref(rbuf->rbo);
281}
282
283static int drm_gem_radeon_map(struct gralloc_drm_drv_t *drv,
284 struct gralloc_drm_bo_t *bo, int x, int y, int w, int h,
285 int enable_write, void **addr)
286{
287 struct radeon_buffer *rbuf = (struct radeon_buffer *) bo;
288 int err;
289
290 err = radeon_bo_map(rbuf->rbo, enable_write);
291 if (!err)
292 *addr = rbuf->rbo->ptr;
293
294 return err;
295}
296
297static void drm_gem_radeon_unmap(struct gralloc_drm_drv_t *drv,
298 struct gralloc_drm_bo_t *bo)
299{
300 struct radeon_buffer *rbuf = (struct radeon_buffer *) bo;
301 radeon_bo_unmap(rbuf->rbo);
302}
303
304static void drm_gem_radeon_init_kms_features(struct gralloc_drm_drv_t *drv,
305 struct gralloc_drm_t *drm)
306{
Chia-I Wue59db8f2011-07-10 23:12:26 +0800307 drm->fb_format = HAL_PIXEL_FORMAT_BGRA_8888;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800308 drm->mode_dirty_fb = 0;
309 drm->swap_mode = DRM_SWAP_FLIP;
310 drm->mode_sync_flip = 1;
311 drm->swap_interval = 1;
312 drm->vblank_secondary = 0;
313}
314
315static void drm_gem_radeon_destroy(struct gralloc_drm_drv_t *drv)
316{
317 struct radeon_info *info = (struct radeon_info *) drv;
318
319 radeon_bo_manager_gem_dtor(info->bufmgr);
320 free(info);
321}
322
Chia-I Wu896fbd12011-07-14 14:42:30 +0800323static int radeon_init_tile_config(struct radeon_info *info)
324{
325 struct drm_radeon_info ginfo;
326 uint32_t val;
327 int ret;
328
329 memset(&ginfo, 0, sizeof(ginfo));
330 ginfo.request = RADEON_INFO_TILING_CONFIG;
331 ginfo.value = (long) &val;
332 ret = drmCommandWriteRead(info->fd, DRM_RADEON_INFO,
333 &ginfo, sizeof(ginfo));
334 if (ret)
335 return ret;
336
337 info->tile_config = val;
338
339 if (info->chip_family >= CHIP_FAMILY_CEDAR) {
340 switch (info->tile_config & 0xf) {
341 case 0:
342 info->num_channels = 1;
343 break;
344 case 1:
345 info->num_channels = 2;
346 break;
347 case 2:
348 info->num_channels = 4;
349 break;
350 case 3:
351 info->num_channels = 8;
352 break;
353 default:
354 return -EINVAL;
355 break;
356 }
357
358 switch ((info->tile_config & 0xf0) >> 4) {
359 case 0:
360 info->num_banks = 4;
361 break;
362 case 1:
363 info->num_banks = 8;
364 break;
365 case 2:
366 info->num_banks = 16;
367 break;
368 default:
369 return -EINVAL;
370 break;
371 }
372
373 switch ((info->tile_config & 0xf00) >> 8) {
374 case 0:
375 info->group_bytes = 256;
376 break;
377 case 1:
378 info->group_bytes = 512;
379 break;
380 default:
381 return -EINVAL;
382 break;
383 }
384 }
385 else {
386 switch ((info->tile_config & 0xe) >> 1) {
387 case 0:
388 info->num_channels = 1;
389 break;
390 case 1:
391 info->num_channels = 2;
392 break;
393 case 2:
394 info->num_channels = 4;
395 break;
396 case 3:
397 info->num_channels = 8;
398 break;
399 default:
400 return -EINVAL;
401 break;
402 }
403
404 switch ((info->tile_config & 0x30) >> 4) {
405 case 0:
406 info->num_banks = 4;
407 break;
408 case 1:
409 info->num_banks = 8;
410 break;
411 default:
412 return -EINVAL;
413 break;
414 }
415
416 switch ((info->tile_config & 0xc0) >> 6) {
417 case 0:
418 info->group_bytes = 256;
419 break;
420 case 1:
421 info->group_bytes = 512;
422 break;
423 default:
424 return -EINVAL;
425 break;
426 }
427 }
428
429 info->have_tiling_info = 1;
430
431 return 0;
432}
433
434static int radeon_probe(struct radeon_info *info)
435{
436 struct drm_radeon_info kinfo;
437 struct drm_radeon_gem_info mminfo;
438 int err;
439
440 memset(&kinfo, 0, sizeof(kinfo));
441 kinfo.request = RADEON_INFO_DEVICE_ID;
442 kinfo.value = (long) &info->chipset;
443 err = drmCommandWriteRead(info->fd, DRM_RADEON_INFO, &kinfo, sizeof(kinfo));
Chia-I Wua4426742011-07-18 13:24:05 +0800444 if (err) {
445 LOGE("failed to get device id");
Chia-I Wu896fbd12011-07-14 14:42:30 +0800446 return err;
Chia-I Wua4426742011-07-18 13:24:05 +0800447 }
Chia-I Wu896fbd12011-07-14 14:42:30 +0800448
449 /* XXX this is wrong and a table should be used */
Chia-I Wua4426742011-07-18 13:24:05 +0800450 if (info->chipset >= 0x68e4 && info->chipset <= 0x68fe) {
Chia-I Wu896fbd12011-07-14 14:42:30 +0800451 info->chip_family = CHIP_FAMILY_CEDAR;
Chia-I Wua4426742011-07-18 13:24:05 +0800452 }
453 else if (info->chipset >= 0x9802 && info->chipset <= 0x9807) {
Chia-I Wu896fbd12011-07-14 14:42:30 +0800454 info->chip_family = CHIP_FAMILY_PALM;
Chia-I Wua4426742011-07-18 13:24:05 +0800455 }
456 else {
457 LOGE("unknown device id 0x%04x", info->chipset);
Chia-I Wu896fbd12011-07-14 14:42:30 +0800458 return -EINVAL;
Chia-I Wua4426742011-07-18 13:24:05 +0800459 }
Chia-I Wu896fbd12011-07-14 14:42:30 +0800460
461 err = radeon_init_tile_config(info);
Chia-I Wua4426742011-07-18 13:24:05 +0800462 if (err) {
463 LOGE("failed to get tiling config");
Chia-I Wu896fbd12011-07-14 14:42:30 +0800464 return err;
Chia-I Wua4426742011-07-18 13:24:05 +0800465 }
Chia-I Wu896fbd12011-07-14 14:42:30 +0800466
Chia-I Wucbd62792011-07-15 10:18:58 +0800467 /* CPU cannot handle tiled buffers (need scratch buffers) */
468 info->allow_color_tiling = 0;
Chia-I Wu896fbd12011-07-14 14:42:30 +0800469
470 memset(&mminfo, 0, sizeof(mminfo));
471 err = drmCommandWriteRead(info->fd, DRM_RADEON_GEM_INFO, &mminfo, sizeof(mminfo));
Chia-I Wua4426742011-07-18 13:24:05 +0800472 if (err) {
473 LOGE("failed to get gem info");
Chia-I Wu896fbd12011-07-14 14:42:30 +0800474 return err;
Chia-I Wua4426742011-07-18 13:24:05 +0800475 }
Chia-I Wu896fbd12011-07-14 14:42:30 +0800476
477 info->vram_size = mminfo.vram_visible;
478 info->gart_size = mminfo.gart_size;
479
480 LOGI("detected chip family %s (vram size %dMiB, gart size %dMiB)",
481 (info->chip_family == CHIP_FAMILY_CEDAR) ?
482 "CEDAR" : "PALM",
483 info->vram_size / 1024 / 1024,
484 info->gart_size / 1024 / 1024);
485
486 return 0;
487}
488
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800489struct gralloc_drm_drv_t *gralloc_drm_drv_create_for_radeon(int fd)
490{
491 struct radeon_info *info;
492
493 info = calloc(1, sizeof(*info));
494 if (!info)
495 return NULL;
496
497 info->fd = fd;
Chia-I Wu896fbd12011-07-14 14:42:30 +0800498 if (radeon_probe(info)) {
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800499 free(info);
500 return NULL;
501 }
502
Chia-I Wu896fbd12011-07-14 14:42:30 +0800503 info->bufmgr = radeon_bo_manager_gem_ctor(info->fd);
504 if (!info->bufmgr) {
505 LOGE("failed to create buffer manager");
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800506 free(info);
507 return NULL;
508 }
509
510 info->base.destroy = drm_gem_radeon_destroy;
511 info->base.init_kms_features = drm_gem_radeon_init_kms_features;
512 info->base.alloc = drm_gem_radeon_alloc;
513 info->base.free = drm_gem_radeon_free;
514 info->base.map = drm_gem_radeon_map;
515 info->base.unmap = drm_gem_radeon_unmap;
516
517 return &info->base;
518}