radeon_gem.c 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Copyright 2008 Advanced Micro Devices, Inc.
 * Copyright 2008 Red Hat Inc.
 * Copyright 2009 Jerome Glisse.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Dave Airlie
 *          Alex Deucher
 *          Jerome Glisse
 */
#include "drmP.h"
#include "drm.h"
#include "radeon_drm.h"
#include "radeon.h"

int radeon_gem_object_init(struct drm_gem_object *obj)
{
35 36
	BUG();

37 38 39 40 41
	return 0;
}

void radeon_gem_object_free(struct drm_gem_object *gobj)
{
42
	struct radeon_bo *robj = gem_to_radeon_bo(gobj);
43 44

	if (robj) {
45
		radeon_bo_unref(&robj);
46 47 48 49
	}
}

int radeon_gem_object_create(struct radeon_device *rdev, int size,
50 51 52
				int alignment, int initial_domain,
				bool discardable, bool kernel,
				struct drm_gem_object **obj)
53
{
54
	struct radeon_bo *robj;
55 56 57 58 59 60 61
	int r;

	*obj = NULL;
	/* At least align on page size */
	if (alignment < PAGE_SIZE) {
		alignment = PAGE_SIZE;
	}
62
	r = radeon_bo_create(rdev, size, alignment, kernel, initial_domain, &robj);
63
	if (r) {
64 65 66
		if (r != -ERESTARTSYS)
			DRM_ERROR("Failed to allocate GEM object (%d, %d, %u, %d)\n",
				  size, initial_domain, alignment, r);
67 68
		return r;
	}
69 70 71 72 73 74
	*obj = &robj->gem_base;

	mutex_lock(&rdev->gem.mutex);
	list_add_tail(&robj->list, &rdev->gem.objects);
	mutex_unlock(&rdev->gem.mutex);

75 76 77 78 79 80
	return 0;
}

int radeon_gem_object_pin(struct drm_gem_object *obj, uint32_t pin_domain,
			  uint64_t *gpu_addr)
{
81
	struct radeon_bo *robj = gem_to_radeon_bo(obj);
82
	int r;
83

84 85 86 87 88 89
	r = radeon_bo_reserve(robj, false);
	if (unlikely(r != 0))
		return r;
	r = radeon_bo_pin(robj, pin_domain, gpu_addr);
	radeon_bo_unreserve(robj);
	return r;
90 91 92 93
}

void radeon_gem_object_unpin(struct drm_gem_object *obj)
{
94
	struct radeon_bo *robj = gem_to_radeon_bo(obj);
95 96 97 98 99 100 101
	int r;

	r = radeon_bo_reserve(robj, false);
	if (likely(r == 0)) {
		radeon_bo_unpin(robj);
		radeon_bo_unreserve(robj);
	}
102 103 104 105 106
}

int radeon_gem_set_domain(struct drm_gem_object *gobj,
			  uint32_t rdomain, uint32_t wdomain)
{
107
	struct radeon_bo *robj;
108 109 110 111
	uint32_t domain;
	int r;

	/* FIXME: reeimplement */
112
	robj = gem_to_radeon_bo(gobj);
113 114 115 116 117 118 119 120 121 122 123 124
	/* work out where to validate the buffer to */
	domain = wdomain;
	if (!domain) {
		domain = rdomain;
	}
	if (!domain) {
		/* Do nothings */
		printk(KERN_WARNING "Set domain withou domain !\n");
		return 0;
	}
	if (domain == RADEON_GEM_DOMAIN_CPU) {
		/* Asking for cpu access wait for object idle */
125
		r = radeon_bo_wait(robj, NULL, false);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
		if (r) {
			printk(KERN_ERR "Failed to wait for object !\n");
			return r;
		}
	}
	return 0;
}

int radeon_gem_init(struct radeon_device *rdev)
{
	INIT_LIST_HEAD(&rdev->gem.objects);
	return 0;
}

void radeon_gem_fini(struct radeon_device *rdev)
{
142
	radeon_bo_force_delete(rdev);
143 144 145 146 147 148 149 150 151 152 153
}


/*
 * GEM ioctls.
 */
int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
			  struct drm_file *filp)
{
	struct radeon_device *rdev = dev->dev_private;
	struct drm_radeon_gem_info *args = data;
154
	struct ttm_mem_type_manager *man;
155
	unsigned i;
156 157

	man = &rdev->mman.bdev.man[TTM_PL_VRAM];
158

159
	args->vram_size = rdev->mc.real_vram_size;
160
	args->vram_visible = (u64)man->size << PAGE_SHIFT;
161
	if (rdev->stollen_vga_memory)
162
		args->vram_visible -= radeon_bo_size(rdev->stollen_vga_memory);
163
	args->vram_visible -= radeon_fbdev_total_size(rdev);
164
	args->gart_size = rdev->mc.gtt_size - 4096 - RADEON_IB_POOL_SIZE*64*1024;
165 166
	for(i = 0; i < RADEON_NUM_RINGS; ++i)
		args->gart_size -= rdev->cp[i].ring_size;
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	return 0;
}

int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
			   struct drm_file *filp)
{
	/* TODO: implement */
	DRM_ERROR("unimplemented %s\n", __func__);
	return -ENOSYS;
}

int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
			    struct drm_file *filp)
{
	/* TODO: implement */
	DRM_ERROR("unimplemented %s\n", __func__);
	return -ENOSYS;
}

int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
			    struct drm_file *filp)
{
	struct radeon_device *rdev = dev->dev_private;
	struct drm_radeon_gem_create *args = data;
	struct drm_gem_object *gobj;
	uint32_t handle;
	int r;

	/* create a gem object to contain this object in */
	args->size = roundup(args->size, PAGE_SIZE);
	r = radeon_gem_object_create(rdev, args->size, args->alignment,
198 199
					args->initial_domain, false,
					false, &gobj);
200 201 202 203
	if (r) {
		return r;
	}
	r = drm_gem_handle_create(filp, gobj, &handle);
204 205
	/* drop reference from allocate - handle holds it now */
	drm_gem_object_unreference_unlocked(gobj);
206 207 208 209 210 211 212 213 214 215 216 217 218 219
	if (r) {
		return r;
	}
	args->handle = handle;
	return 0;
}

int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
				struct drm_file *filp)
{
	/* transition the BO to a domain -
	 * just validate the BO into a certain domain */
	struct drm_radeon_gem_set_domain *args = data;
	struct drm_gem_object *gobj;
220
	struct radeon_bo *robj;
221 222 223 224 225 226 227 228
	int r;

	/* for now if someone requests domain CPU -
	 * just make sure the buffer is finished with */

	/* just do a BO wait for now */
	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL) {
229
		return -ENOENT;
230
	}
231
	robj = gem_to_radeon_bo(gobj);
232 233 234

	r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain);

235
	drm_gem_object_unreference_unlocked(gobj);
236 237 238
	return r;
}

239 240 241
int radeon_mode_dumb_mmap(struct drm_file *filp,
			  struct drm_device *dev,
			  uint32_t handle, uint64_t *offset_p)
242 243
{
	struct drm_gem_object *gobj;
244
	struct radeon_bo *robj;
245

246
	gobj = drm_gem_object_lookup(dev, filp, handle);
247
	if (gobj == NULL) {
248
		return -ENOENT;
249
	}
250
	robj = gem_to_radeon_bo(gobj);
251
	*offset_p = radeon_bo_mmap_offset(robj);
252
	drm_gem_object_unreference_unlocked(gobj);
253
	return 0;
254 255
}

256 257 258 259 260 261 262 263
int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
			  struct drm_file *filp)
{
	struct drm_radeon_gem_mmap *args = data;

	return radeon_mode_dumb_mmap(filp, dev, args->handle, &args->addr_ptr);
}

264 265 266
int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
			  struct drm_file *filp)
{
267 268
	struct drm_radeon_gem_busy *args = data;
	struct drm_gem_object *gobj;
269
	struct radeon_bo *robj;
270
	int r;
271
	uint32_t cur_placement = 0;
272 273 274

	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL) {
275
		return -ENOENT;
276
	}
277
	robj = gem_to_radeon_bo(gobj);
278
	r = radeon_bo_wait(robj, &cur_placement, true);
279 280
	switch (cur_placement) {
	case TTM_PL_VRAM:
281
		args->domain = RADEON_GEM_DOMAIN_VRAM;
282 283
		break;
	case TTM_PL_TT:
284
		args->domain = RADEON_GEM_DOMAIN_GTT;
285 286
		break;
	case TTM_PL_SYSTEM:
287
		args->domain = RADEON_GEM_DOMAIN_CPU;
288 289 290
	default:
		break;
	}
291
	drm_gem_object_unreference_unlocked(gobj);
292
	return r;
293 294 295 296 297 298 299
}

int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
			      struct drm_file *filp)
{
	struct drm_radeon_gem_wait_idle *args = data;
	struct drm_gem_object *gobj;
300
	struct radeon_bo *robj;
301 302 303 304
	int r;

	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL) {
305
		return -ENOENT;
306
	}
307
	robj = gem_to_radeon_bo(gobj);
308
	r = radeon_bo_wait(robj, NULL, false);
309 310 311
	/* callback hw specific functions if any */
	if (robj->rdev->asic->ioctl_wait_idle)
		robj->rdev->asic->ioctl_wait_idle(robj->rdev, robj);
312
	drm_gem_object_unreference_unlocked(gobj);
313 314
	return r;
}
315 316 317 318 319 320

int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
				struct drm_file *filp)
{
	struct drm_radeon_gem_set_tiling *args = data;
	struct drm_gem_object *gobj;
321
	struct radeon_bo *robj;
322 323 324 325 326
	int r = 0;

	DRM_DEBUG("%d \n", args->handle);
	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL)
327
		return -ENOENT;
328
	robj = gem_to_radeon_bo(gobj);
329
	r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch);
330
	drm_gem_object_unreference_unlocked(gobj);
331 332 333 334 335 336 337 338
	return r;
}

int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
				struct drm_file *filp)
{
	struct drm_radeon_gem_get_tiling *args = data;
	struct drm_gem_object *gobj;
339
	struct radeon_bo *rbo;
340 341 342 343 344
	int r = 0;

	DRM_DEBUG("\n");
	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL)
345
		return -ENOENT;
346
	rbo = gem_to_radeon_bo(gobj);
347 348
	r = radeon_bo_reserve(rbo, false);
	if (unlikely(r != 0))
349
		goto out;
350 351
	radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch);
	radeon_bo_unreserve(rbo);
352
out:
353
	drm_gem_object_unreference_unlocked(gobj);
354 355
	return r;
}
356 357 358 359 360 361 362

int radeon_mode_dumb_create(struct drm_file *file_priv,
			    struct drm_device *dev,
			    struct drm_mode_create_dumb *args)
{
	struct radeon_device *rdev = dev->dev_private;
	struct drm_gem_object *gobj;
363
	uint32_t handle;
364 365 366 367 368 369 370 371 372 373 374 375 376
	int r;

	args->pitch = radeon_align_pitch(rdev, args->width, args->bpp, 0) * ((args->bpp + 1) / 8);
	args->size = args->pitch * args->height;
	args->size = ALIGN(args->size, PAGE_SIZE);

	r = radeon_gem_object_create(rdev, args->size, 0,
				     RADEON_GEM_DOMAIN_VRAM,
				     false, ttm_bo_type_device,
				     &gobj);
	if (r)
		return -ENOMEM;

377 378 379
	r = drm_gem_handle_create(file_priv, gobj, &handle);
	/* drop reference from allocate - handle holds it now */
	drm_gem_object_unreference_unlocked(gobj);
380 381 382
	if (r) {
		return r;
	}
383
	args->handle = handle;
384 385 386 387 388 389 390 391 392
	return 0;
}

int radeon_mode_dumb_destroy(struct drm_file *file_priv,
			     struct drm_device *dev,
			     uint32_t handle)
{
	return drm_gem_handle_delete(file_priv, handle);
}