2 * Copyright © 2008 Dave Airlie
3 * Copyright © 2008 Jérôme Glisse
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
18 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
30 * Jérôme Glisse <glisse@freedesktop.org>
37 #include <sys/ioctl.h>
40 #include "radeon_drm.h"
41 #include "radeon_bo.h"
42 #include "radeon_bo_gem.h"
44 struct radeon_bo_gem {
45 struct radeon_bo base;
50 struct bo_manager_gem {
51 struct radeon_bo_manager base;
54 static struct radeon_bo *bo_open(struct radeon_bo_manager *bom,
61 struct radeon_bo_gem *bo;
64 bo = (struct radeon_bo_gem*)calloc(1, sizeof(struct radeon_bo_gem));
72 bo->base.alignment = alignment;
73 bo->base.domains = domains;
74 bo->base.flags = flags;
78 struct drm_gem_open open_arg;
80 memset(&open_arg, 0, sizeof(open_arg));
81 open_arg.name = handle;
82 r = ioctl(bom->fd, DRM_IOCTL_GEM_OPEN, &open_arg);
84 fprintf(stderr, "GEM open failed: %d (%s)\n",r,strerror(r));
88 bo->base.handle = open_arg.handle;
89 bo->base.size = open_arg.size;
92 struct drm_radeon_gem_create args;
95 args.alignment = alignment;
96 args.initial_domain = bo->base.domains;
97 args.no_backing_store = 0;
98 r = drmCommandWriteRead(bom->fd, DRM_RADEON_GEM_CREATE,
100 bo->base.handle = args.handle;
102 fprintf(stderr, "Failed to allocate :\n");
103 fprintf(stderr, " size : %d bytes\n", size);
104 fprintf(stderr, " alignment : %d bytes\n", alignment);
105 fprintf(stderr, " domains : %d\n", bo->base.domains);
110 radeon_bo_ref((struct radeon_bo*)bo);
111 return (struct radeon_bo*)bo;
114 static void bo_ref(struct radeon_bo *bo)
118 static void bo_unref(struct radeon_bo *bo)
120 struct radeon_bo_gem *bo_gem = (struct radeon_bo_gem*)bo;
121 struct drm_gem_close args;
129 if (bo_gem->map_count) {
130 munmap(bo->ptr, bo->size);
134 args.handle = bo->handle;
135 ioctl(bo->bom->fd, DRM_IOCTL_GEM_CLOSE, &args);
139 static int bo_map(struct radeon_bo *bo, int write)
141 struct radeon_bo_gem *bo_gem = (struct radeon_bo_gem*)bo;
142 struct drm_radeon_gem_mmap args;
145 if (bo_gem->map_count++ != 0) {
149 args.handle = bo->handle;
151 args.size = (uint64_t)bo->size;
152 r = drmCommandWriteRead(bo->bom->fd,
157 bo->ptr = (void *)(unsigned long)args.addr_ptr;
159 fprintf(stderr, "error mapping %p 0x%08X (error = %d)\n",
165 static int bo_unmap(struct radeon_bo *bo)
167 struct radeon_bo_gem *bo_gem = (struct radeon_bo_gem*)bo;
169 if (--bo_gem->map_count > 0) {
172 munmap(bo->ptr, bo->size);
177 static struct radeon_bo_funcs bo_gem_funcs = {
185 struct radeon_bo_manager *radeon_bo_manager_gem(int fd)
187 struct bo_manager_gem *bomg;
189 bomg = (struct bo_manager_gem*)calloc(1, sizeof(struct bo_manager_gem));
193 bomg->base.funcs = &bo_gem_funcs;
195 return (struct radeon_bo_manager*)bomg;
198 void radeon_bo_manager_gem_shutdown(struct radeon_bo_manager *bom)
200 struct bo_manager_gem *bomg = (struct bo_manager_gem*)bom;