2 * Copyright (C) 2011 Chia-I Wu <olvaffe@gmail.com>
3 * Copyright (C) 2011 LunarG Inc.
5 * Based on xf86-video-nouveau, which has
7 * Copyright © 2007 Red Hat, Inc.
8 * Copyright © 2008 Maarten Maathuis
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
29 #define LOG_TAG "GRALLOC-NOUVEAU"
31 #include <cutils/log.h>
35 #include <nouveau_drmif.h>
36 #include <nouveau_channel.h>
37 #include <nouveau_bo.h>
39 #include "gralloc_drm.h"
40 #include "gralloc_drm_priv.h"
42 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
44 #define NVC0_TILE_HEIGHT(m) (8 << ((m) >> 4))
52 struct gralloc_drm_drv_t base;
55 struct nouveau_device *dev;
56 struct nouveau_channel *chan;
61 struct nouveau_buffer {
62 struct gralloc_drm_bo_t base;
64 struct nouveau_bo *bo;
67 static struct nouveau_bo *alloc_bo(struct nouveau_info *info,
68 int width, int height, int cpp, int usage, int *pitch)
70 struct nouveau_bo *bo = NULL;
71 int flags, tile_mode, tile_flags;
75 flags = NOUVEAU_BO_MAP | NOUVEAU_BO_VRAM;
79 scanout = !!(usage & GRALLOC_USAGE_HW_FB);
81 tiled = !(usage & (GRALLOC_USAGE_SW_READ_OFTEN |
82 GRALLOC_USAGE_SW_WRITE_OFTEN));
85 else if (scanout && info->tiled_scanout)
88 /* calculate pitch align */
90 if (info->arch >= 0x50) {
91 if (scanout && !info->tiled_scanout)
97 *pitch = ALIGN(width * cpp, align);
100 if (info->arch >= 0xc0) {
103 else if (height > 32)
105 else if (height > 16)
114 align = NVC0_TILE_HEIGHT(tile_mode);
115 height = ALIGN(height, align);
117 else if (info->arch >= 0x50) {
120 else if (height > 16)
129 tile_flags = (scanout && cpp != 2) ? 0x7a00 : 0x7000;
131 align = 1 << (tile_mode + 2);
132 height = ALIGN(height, align);
137 /* round down to the previous power of two */
143 align |= align >> 16;
146 align = MAX((info->dev->chipset >= 0x40) ? 1024 : 256,
150 *pitch = ALIGN(*pitch, align);
157 tile_flags |= NOUVEAU_BO_TILE_32BPP;
159 tile_flags |= NOUVEAU_BO_TILE_16BPP;
162 tile_flags |= NOUVEAU_BO_TILE_SCANOUT;
164 if (nouveau_bo_new_tile(info->dev, flags, 0, *pitch * height,
165 tile_mode, tile_flags, &bo)) {
166 ALOGE("failed to allocate bo (flags 0x%x, size %d, tile_mode 0x%x, tile_flags 0x%x)",
167 flags, *pitch * height, tile_mode, tile_flags);
174 static struct gralloc_drm_bo_t *
175 nouveau_alloc(struct gralloc_drm_drv_t *drv, struct gralloc_drm_handle_t *handle)
177 struct nouveau_info *info = (struct nouveau_info *) drv;
178 struct nouveau_buffer *nb;
181 cpp = gralloc_drm_get_bpp(handle->format);
183 ALOGE("unrecognized format 0x%x", handle->format);
187 nb = calloc(1, sizeof(*nb));
192 if (nouveau_bo_handle_ref(info->dev, handle->name, &nb->bo)) {
193 ALOGE("failed to create nouveau bo from name %u",
200 int width, height, pitch;
202 width = handle->width;
203 height = handle->height;
204 gralloc_drm_align_geometry(handle->format, &width, &height);
206 nb->bo = alloc_bo(info, width, height,
207 cpp, handle->usage, &pitch);
209 ALOGE("failed to allocate nouveau bo %dx%dx%d",
210 handle->width, handle->height, cpp);
215 if (nouveau_bo_handle_get(nb->bo,
216 (uint32_t *) &handle->name)) {
217 ALOGE("failed to flink nouveau bo");
218 nouveau_bo_ref(NULL, &nb->bo);
223 handle->stride = pitch;
226 if (handle->usage & GRALLOC_USAGE_HW_FB)
227 nb->base.fb_handle = nb->bo->handle;
229 nb->base.handle = handle;
234 static void nouveau_free(struct gralloc_drm_drv_t *drv,
235 struct gralloc_drm_bo_t *bo)
237 struct nouveau_buffer *nb = (struct nouveau_buffer *) bo;
238 nouveau_bo_ref(NULL, &nb->bo);
242 static int nouveau_map(struct gralloc_drm_drv_t *drv,
243 struct gralloc_drm_bo_t *bo, int x, int y, int w, int h,
244 int enable_write, void **addr)
246 struct nouveau_buffer *nb = (struct nouveau_buffer *) bo;
250 flags = NOUVEAU_BO_RD;
252 flags |= NOUVEAU_BO_WR;
254 /* TODO if tiled, allocate a linear copy of bo in GART and map it */
255 err = nouveau_bo_map(nb->bo, flags);
262 static void nouveau_unmap(struct gralloc_drm_drv_t *drv,
263 struct gralloc_drm_bo_t *bo)
265 struct nouveau_buffer *nb = (struct nouveau_buffer *) bo;
266 /* TODO if tiled, unmap the linear bo and copy back */
267 nouveau_bo_unmap(nb->bo);
270 static void nouveau_init_kms_features(struct gralloc_drm_drv_t *drv,
271 struct gralloc_drm_t *drm)
273 struct nouveau_info *info = (struct nouveau_info *) drv;
275 switch (drm->primary.fb_format) {
276 case HAL_PIXEL_FORMAT_BGRA_8888:
277 case HAL_PIXEL_FORMAT_RGB_565:
280 drm->primary.fb_format = HAL_PIXEL_FORMAT_BGRA_8888;
284 drm->mode_quirk_vmwgfx = 0;
285 drm->swap_mode = (info->chan) ? DRM_SWAP_FLIP : DRM_SWAP_SETCRTC;
286 drm->mode_sync_flip = 1;
287 drm->swap_interval = 1;
288 drm->vblank_secondary = 0;
291 static void nouveau_destroy(struct gralloc_drm_drv_t *drv)
293 struct nouveau_info *info = (struct nouveau_info *) drv;
296 nouveau_channel_free(&info->chan);
297 nouveau_device_close(&info->dev);
301 static int nouveau_init(struct nouveau_info *info)
305 switch (info->dev->chipset & 0xf0) {
332 ALOGE("unknown nouveau chipset 0x%x", info->dev->chipset);
337 info->tiled_scanout = (info->chan != NULL);
342 struct gralloc_drm_drv_t *gralloc_drm_drv_create_for_nouveau(int fd)
344 struct nouveau_info *info;
347 info = calloc(1, sizeof(*info));
352 err = nouveau_device_open_existing(&info->dev, 0, info->fd, 0);
354 ALOGE("failed to create nouveau device");
359 err = nouveau_channel_alloc(info->dev, NvDmaFB, NvDmaTT,
360 24 * 1024, &info->chan);
362 /* make it non-fatal temporarily as it may require firmwares */
363 ALOGW("failed to create nouveau channel");
367 err = nouveau_init(info);
370 nouveau_channel_free(&info->chan);
371 nouveau_device_close(&info->dev);
376 info->base.destroy = nouveau_destroy;
377 info->base.init_kms_features = nouveau_init_kms_features;
378 info->base.alloc = nouveau_alloc;
379 info->base.free = nouveau_free;
380 info->base.map = nouveau_map;
381 info->base.unmap = nouveau_unmap;