/* * Copyright 2010 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 * on the rights to use, copy, modify, merge, publish, distribute, sub * license, 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 (including the next * paragraph) 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 NON-INFRINGEMENT. IN NO EVENT SHALL * THE AUTHOR(S) AND/OR THEIR SUPPLIERS 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: * Jerome Glisse */ #include #include #include #include "bof.h" /* * helpers */ static int bof_entry_grow(bof_t *bof) { bof_t **array; if (bof->array_size < bof->nentry) return 0; array = realloc(bof->array, (bof->nentry + 16) * sizeof(void*)); if (array == NULL) return -ENOMEM; bof->array = array; bof->nentry += 16; return 0; } /* * object */ bof_t *bof_object(void) { bof_t *object; object = calloc(1, sizeof(bof_t)); if (object == NULL) return NULL; object->refcount = 1; object->type = BOF_TYPE_OBJECT; object->size = 12; return object; } bof_t *bof_object_get(bof_t *object, const char *keyname) { unsigned i; for (i = 0; i < object->array_size; i += 2) { if (!strcmp(object->array[i]->value, keyname)) { return object->array[i + 1]; } } return NULL; } int bof_object_set(bof_t *object, const char *keyname, bof_t *value) { bof_t *key; int r; if (object->type != BOF_TYPE_OBJECT) return -EINVAL; r = bof_entry_grow(object); if (r) return r; key = bof_string(keyname); if (key == NULL) return -ENOMEM; object->array[object->array_size++] = key; object->array[object->array_size++] = value; object->size += value->size; object->size += key->size; bof_incref(value); return 0; } /* * array */ bof_t *bof_array(void) { bof_t *array = bof_object(); if (array == NULL) return NULL; array->type = BOF_TYPE_ARRAY; array->size = 12; return array; } int bof_array_append(bof_t *array, bof_t *value) { int r; if (array->type != BOF_TYPE_ARRAY) return -EINVAL; r = bof_entry_grow(array); if (r) return r; array->array[array->array_size++] = value; array->size += value->size; bof_incref(value); return 0; } bof_t *bof_array_get(bof_t *bof, unsigned i) { if (!bof_is_array(bof) || i >= bof->array_size) return NULL; return bof->array[i]; } unsigned bof_array_size(bof_t *bof) { if (!bof_is_array(bof)) return 0; return bof->array_size; } /* * blob */ bof_t *bof_blob(unsigned size, void *value) { bof_t *blob = bof_object(); if (blob == NULL) return NULL; blob->type = BOF_TYPE_BLOB; blob->value = calloc(1, size); if (blob->value == NULL) { bof_decref(blob); return NULL; } blob->size = size; memcpy(blob->value, value, size); blob->size += 12; return blob; } unsigned bof_blob_size(bof_t *bof) { if (!bof_is_blob(bof)) return 0; return bof->size - 12; } void *bof_blob_value(bof_t *bof) { if (!bof_is_blob(bof)) return NULL; return bof->value; } /* * string */ bof_t *bof_string(const char *value) { bof_t *string = bof_object(); if (string == NULL) return NULL; string->type = BOF_TYPE_STRING; string->size = strlen(value) + 1; string->value = calloc(1, string->size); if (string->value == NULL) { bof_decref(string); return NULL; } strcpy(string->value, value); string->size += 12; return string; } /* * int32 */ bof_t *bof_int32(int32_t value) { bof_t *int32 = bof_object(); if (int32 == NULL) return NULL; int32->type = BOF_TYPE_INT32; int32->size = 4; int32->value = calloc(1, int32->size); if (int32->value == NULL) { bof_decref(int32); return NULL; } memcpy(int32->value, &value, 4); int32->size += 12; return int32; } int32_t bof_int32_value(bof_t *bof) { return *((uint32_t*)bof->value); } /* * common */ static void bof_indent(int level) { int i; for (i = 0; i < level; i++) fprintf(stderr, " "); } static void bof_print_bof(bof_t *bof, int level, int entry) { bof_indent(level); if (bof == NULL) { fprintf(stderr, "--NULL-- for entry %d\n", entry); return; } switch (bof->type) { case BOF_TYPE_STRING: fprintf(stderr, "%p string [%s %d]\n", bof, (char*)bof->value, bof->size); break; case BOF_TYPE_INT32: fprintf(stderr, "%p int32 [%d %d]\n", bof, *(int*)bof->value, bof->size); break; case BOF_TYPE_BLOB: fprintf(stderr, "%p blob [%d]\n", bof, bof->size); break; case BOF_TYPE_NULL: fprintf(stderr, "%p null [%d]\n", bof, bof->size); break; case BOF_TYPE_OBJECT: fprintf(stderr, "%p object [%d %d]\n", bof, bof->array_size / 2, bof->size); break; case BOF_TYPE_ARRAY: fprintf(stderr, "%p array [%d %d]\n", bof, bof->array_size, bof->size); break; default: fprintf(stderr, "%p unknown [%d]\n", bof, bof->type); return; } } static void bof_print_rec(bof_t *bof, int level, int entry) { unsigned i; bof_print_bof(bof, level, entry); for (i = 0; i < bof->array_size; i++) { bof_print_rec(bof->array[i], level + 2, i); } } void bof_print(bof_t *bof) { bof_print_rec(bof, 0, 0); } static int bof_read(bof_t *root, FILE *file, long end, int level) { bof_t *bof = NULL; int r; if (ftell(file) >= end) { return 0; } r = bof_entry_grow(root); if (r) return r; bof = bof_object(); if (bof == NULL) return -ENOMEM; bof->offset = ftell(file); r = fread(&bof->type, 4, 1, file); if (r != 1) goto out_err; r = fread(&bof->size, 4, 1, file); if (r != 1) goto out_err; r = fread(&bof->array_size, 4, 1, file); if (r != 1) goto out_err; switch (bof->type) { case BOF_TYPE_STRING: case BOF_TYPE_INT32: case BOF_TYPE_BLOB: bof->value = calloc(1, bof->size - 12); if (bof->value == NULL) { goto out_err; } r = fread(bof->value, bof->size - 12, 1, file); if (r != 1) { fprintf(stderr, "error reading %d\n", bof->size - 12); goto out_err; } break; case BOF_TYPE_NULL: return 0; case BOF_TYPE_OBJECT: case BOF_TYPE_ARRAY: r = bof_read(bof, file, bof->offset + bof->size, level + 2); if (r) goto out_err; break; default: fprintf(stderr, "invalid type %d\n", bof->type); goto out_err; } root->array[root->centry++] = bof; return bof_read(root, file, end, level); out_err: bof_decref(bof); return -EINVAL; } bof_t *bof_load_file(const char *filename) { bof_t *root = bof_object(); int r; if (root == NULL) { fprintf(stderr, "%s failed to create root object\n", __func__); return NULL; } root->file = fopen(filename, "r"); if (root->file == NULL) goto out_err; r = fseek(root->file, 0L, SEEK_SET); if (r) { fprintf(stderr, "%s failed to seek into file %s\n", __func__, filename); goto out_err; } root->offset = ftell(root->file); r = fread(&root->type, 4, 1, root->file); if (r != 1) goto out_err; r = fread(&root->size, 4, 1, root->file); if (r != 1) goto out_err; r = fread(&root->array_size, 4, 1, root->file); if (r != 1) goto out_err; r = bof_read(root, root->file, root->offset + root->size, 2); if (r) goto out_err; return root; out_err: bof_decref(root); return NULL; } void bof_incref(bof_t *bof) { bof->refcount++; } void bof_decref(bof_t *bof) { unsigned i; if (bof == NULL) return; if (--bof->refcount > 0) return; for (i = 0; i < bof->array_size; i++) { bof_decref(bof->array[i]); bof->array[i] = NULL; } bof->array_size = 0; if (bof->file) { fclose(bof->file); bof->file = NULL; } free(bof->array); free(bof->value); free(bof); } static int bof_file_write(bof_t *bof, FILE *file) { unsigned i; int r; r = fwrite(&bof->type, 4, 1, file); if (r != 1) return -EINVAL; r = fwrite(&bof->size, 4, 1, file); if (r != 1) return -EINVAL; r = fwrite(&bof->array_size, 4, 1, file); if (r != 1) return -EINVAL; switch (bof->type) { case BOF_TYPE_NULL: if (bof->size) return -EINVAL; break; case BOF_TYPE_STRING: case BOF_TYPE_INT32: case BOF_TYPE_BLOB: r = fwrite(bof->value, bof->size - 12, 1, file); if (r != 1) return -EINVAL; break; case BOF_TYPE_OBJECT: case BOF_TYPE_ARRAY: for (i = 0; i < bof->array_size; i++) { r = bof_file_write(bof->array[i], file); if (r) return r; } break; default: return -EINVAL; } return 0; } int bof_dump_file(bof_t *bof, const char *filename) { unsigned i; int r = 0; if (bof->file) { fclose(bof->file); bof->file = NULL; } bof->file = fopen(filename, "w"); if (bof->file == NULL) { fprintf(stderr, "%s failed to open file %s\n", __func__, filename); r = -EINVAL; goto out_err; } r = fseek(bof->file, 0L, SEEK_SET); if (r) { fprintf(stderr, "%s failed to seek into file %s\n", __func__, filename); goto out_err; } r = fwrite(&bof->type, 4, 1, bof->file); if (r != 1) goto out_err; r = fwrite(&bof->size, 4, 1, bof->file); if (r != 1) goto out_err; r = fwrite(&bof->array_size, 4, 1, bof->file); if (r != 1) goto out_err; for (i = 0; i < bof->array_size; i++) { r = bof_file_write(bof->array[i], bof->file); if (r) return r; } out_err: fclose(bof->file); bof->file = NULL; return r; } 210'>210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
/* bufs.c -- IOCTLs to manage buffers -*- linux-c -*-
 * Created: Tue Feb  2 08:37:54 1999 by faith@precisioninsight.com
 * Revised: Fri Dec  3 12:11:11 1999 by faith@precisioninsight.com
 *
 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 *
 * 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 (including the next
 * paragraph) 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
 * PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.
 * 
 * $PI: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/kernel/bufs.c,v 1.8 1999/08/30 13:05:00 faith Exp $
 * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/kernel/bufs.c,v 1.2 1999/12/14 01:33:55 robin Exp $
 *
 */

#define __NO_VERSION__
#include "drmP.h"
#include "linux/un.h"

				/* Compute order.  Can be made faster. */
int drm_order(unsigned long size)
{
	int	      order;
	unsigned long tmp;

	for (order = 0, tmp = size; tmp >>= 1; ++order);
	if (size & ~(1 << order)) ++order;
	return order;
}

int drm_addmap(struct inode *inode, struct file *filp, unsigned int cmd,
	       unsigned long arg)
{
	drm_file_t	*priv	= filp->private_data;
	drm_device_t	*dev	= priv->dev;
	drm_map_t	*map;
	
	if (!(filp->f_mode & 3)) return -EACCES; /* Require read/write */

	map	     = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
	if (!map) return -ENOMEM;
	if (copy_from_user(map, (drm_map_t *)arg, sizeof(*map))) {
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		return -EFAULT;
	}

	DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
		  map->offset, map->size, map->type);
	if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		return -EINVAL;
	}
	map->mtrr   = -1;
	map->handle = 0;

	switch (map->type) {
	case _DRM_REGISTERS:
	case _DRM_FRAME_BUFFER:	
		if (map->offset + map->size < map->offset
		    || map->offset < virt_to_phys(high_memory)) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -EINVAL;
		}
#ifdef CONFIG_MTRR
		if (map->type == _DRM_FRAME_BUFFER
		    || (map->flags & _DRM_WRITE_COMBINING)) {
			map->mtrr = mtrr_add(map->offset, map->size,
					     MTRR_TYPE_WRCOMB, 1);
		}
#endif
		map->handle = drm_ioremap(map->offset, map->size);
		break;
			

	case _DRM_SHM:
		map->handle = (void *)drm_alloc_pages(drm_order(map->size)
						      - PAGE_SHIFT,
						      DRM_MEM_SAREA);
		DRM_DEBUG("%ld %d %p\n", map->size, drm_order(map->size),
			  map->handle);
		if (!map->handle) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -ENOMEM;
		}
		map->offset = (unsigned long)map->handle;
		if (map->flags & _DRM_CONTAINS_LOCK) {
			dev->lock.hw_lock = map->handle; /* Pointer to lock */
		}
		break;
	default:
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		return -EINVAL;
	}

	down(&dev->struct_sem);
	if (dev->maplist) {
		++dev->map_count;
		dev->maplist = drm_realloc(dev->maplist,
					   (dev->map_count-1)
					   * sizeof(*dev->maplist),
					   dev->map_count
					   * sizeof(*dev->maplist),
					   DRM_MEM_MAPS);
	} else {
		dev->map_count = 1;
		dev->maplist = drm_alloc(dev->map_count*sizeof(*dev->maplist),
					 DRM_MEM_MAPS);
	}
	dev->maplist[dev->map_count-1] = map;
	up(&dev->struct_sem);

	copy_to_user_ret((drm_map_t *)arg, map, sizeof(*map), -EFAULT);
	if (map->type != _DRM_SHM) {
		copy_to_user_ret(&((drm_map_t *)arg)->handle,
				 &map->offset,
				 sizeof(map->offset),
				 -EFAULT);
	}		
	return 0;
}

int drm_addbufs(struct inode *inode, struct file *filp, unsigned int cmd,
		unsigned long arg)
{
	drm_file_t	 *priv	 = filp->private_data;
	drm_device_t	 *dev	 = priv->dev;
	drm_device_dma_t *dma	 = dev->dma;
	drm_buf_desc_t	 request;
	int		 count;
	int		 order;
	int		 size;
	int		 total;
	int		 page_order;
	drm_buf_entry_t	 *entry;
	unsigned long	 page;
	drm_buf_t	 *buf;
	int		 alignment;
	unsigned long	 offset;
	int		 i;
	int		 byte_count;
	int		 page_count;

	if (!dma) return -EINVAL;

	copy_from_user_ret(&request,
			   (drm_buf_desc_t *)arg,
			   sizeof(request),
			   -EFAULT);

	count	   = request.count;
	order	   = drm_order(request.size);
	size	   = 1 << order;
	
	DRM_DEBUG("count = %d, size = %d (%d), order = %d, queue_count = %d\n",
		  request.count, request.size, size, order, dev->queue_count);

	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) return -EINVAL;
	if (dev->queue_count) return -EBUSY; /* Not while in use */

	alignment  = (request.flags & DRM_PAGE_ALIGN) ? PAGE_ALIGN(size) :size;
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total	   = PAGE_SIZE << page_order;

	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
		return -EBUSY;
	}
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
	
	down(&dev->struct_sem);
	entry = &dma->bufs[order];
	if (entry->buf_count) {
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;	/* May only call once for each order */
	}
	
	entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
				   DRM_MEM_BUFS);
	if (!entry->buflist) {
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;
	}
	memset(entry->buflist, 0, count * sizeof(*entry->buflist));

	entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
				   DRM_MEM_SEGS);
	if (!entry->seglist) {
		drm_free(entry->buflist,
			 count * sizeof(*entry->buflist),
			 DRM_MEM_BUFS);
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;
	}
	memset(entry->seglist, 0, count * sizeof(*entry->seglist));

	dma->pagelist = drm_realloc(dma->pagelist,
				    dma->page_count * sizeof(*dma->pagelist),
				    (dma->page_count + (count << page_order))
				    * sizeof(*dma->pagelist),
				    DRM_MEM_PAGES);
	DRM_DEBUG("pagelist: %d entries\n",
		  dma->page_count + (count << page_order));


	entry->buf_size	  = size;
	entry->page_order = page_order;
	byte_count	  = 0;
	page_count	  = 0;
	while (entry->buf_count < count) {
		if (!(page = drm_alloc_pages(page_order, DRM_MEM_DMA))) break;
		entry->seglist[entry->seg_count++] = page;
		for (i = 0; i < (1 << page_order); i++) {
			DRM_DEBUG("page %d @ 0x%08lx\n",
				  dma->page_count + page_count,
				  page + PAGE_SIZE * i);
			dma->pagelist[dma->page_count + page_count++]
				= page + PAGE_SIZE * i;
		}
		for (offset = 0;
		     offset + size <= total && entry->buf_count < count;
		     offset += alignment, ++entry->buf_count) {
			buf	     = &entry->buflist[entry->buf_count];
			buf->idx     = dma->buf_count + entry->buf_count;
			buf->total   = alignment;
			buf->order   = order;
			buf->used    = 0;
			buf->offset  = (dma->byte_count + byte_count + offset);
			buf->address = (void *)(page + offset);
			buf->next    = NULL;
			buf->waiting = 0;
			buf->pending = 0;
			init_waitqueue_head(&buf->dma_wait);
			buf->pid     = 0;
#if DRM_DMA_HISTOGRAM
			buf->time_queued     = 0;
			buf->time_dispatched = 0;
			buf->time_completed  = 0;
			buf->time_freed	     = 0;
#endif
			DRM_DEBUG("buffer %d @ %p\n",
				  entry->buf_count, buf->address);
		}
		byte_count += PAGE_SIZE << page_order;
	}

	dma->buflist = drm_realloc(dma->buflist,
				   dma->buf_count * sizeof(*dma->buflist),
				   (dma->buf_count + entry->buf_count)
				   * sizeof(*dma->buflist),
				   DRM_MEM_BUFS);
	for (i = dma->buf_count; i < dma->buf_count + entry->buf_count; i++)
		dma->buflist[i] = &entry->buflist[i - dma->buf_count];

	dma->buf_count	+= entry->buf_count;
	dma->seg_count	+= entry->seg_count;
	dma->page_count += entry->seg_count << page_order;
	dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
	
	drm_freelist_create(&entry->freelist, entry->buf_count);
	for (i = 0; i < entry->buf_count; i++) {
		drm_freelist_put(dev, &entry->freelist, &entry->buflist[i]);
	}
	
	up(&dev->struct_sem);

	request.count = entry->buf_count;
	request.size  = size;

	copy_to_user_ret((drm_buf_desc_t *)arg,
			 &request,
			 sizeof(request),
			 -EFAULT);
	
	atomic_dec(&dev->buf_alloc);
	return 0;
}

int drm_infobufs(struct inode *inode, struct file *filp, unsigned int cmd,
		 unsigned long arg)
{
	drm_file_t	 *priv	 = filp->private_data;
	drm_device_t	 *dev	 = priv->dev;
	drm_device_dma_t *dma	 = dev->dma;
	drm_buf_info_t	 request;
	int		 i;
	int		 count;

	if (!dma) return -EINVAL;

	spin_lock(&dev->count_lock);
	if (atomic_read(&dev->buf_alloc)) {
		spin_unlock(&dev->count_lock);
		return -EBUSY;
	}
	++dev->buf_use;		/* Can't allocate more after this call */
	spin_unlock(&dev->count_lock);

	copy_from_user_ret(&request,
			   (drm_buf_info_t *)arg,
			   sizeof(request),
			   -EFAULT);

	for (i = 0, count = 0; i < DRM_MAX_ORDER+1; i++) {
		if (dma->bufs[i].buf_count) ++count;
	}
	
	DRM_DEBUG("count = %d\n", count);
	
	if (request.count >= count) {
		for (i = 0, count = 0; i < DRM_MAX_ORDER+1; i++) {
			if (dma->bufs[i].buf_count) {
				copy_to_user_ret(&request.list[count].count,
						 &dma->bufs[i].buf_count,
						 sizeof(dma->bufs[0]
							.buf_count),
						 -EFAULT);
				copy_to_user_ret(&request.list[count].size,
						 &dma->bufs[i].buf_size,
						 sizeof(dma->bufs[0].buf_size),
						 -EFAULT);
				copy_to_user_ret(&request.list[count].low_mark,
						 &dma->bufs[i]
						 .freelist.low_mark,
						 sizeof(dma->bufs[0]
							.freelist.low_mark),
						 -EFAULT);
				copy_to_user_ret(&request.list[count]
						 .high_mark,
						 &dma->bufs[i]
						 .freelist.high_mark,
						 sizeof(dma->bufs[0]
							.freelist.high_mark),
						 -EFAULT);
				DRM_DEBUG("%d %d %d %d %d\n",
					  i,
					  dma->bufs[i].buf_count,
					  dma->bufs[i].buf_size,
					  dma->bufs[i].freelist.low_mark,
					  dma->bufs[i].freelist.high_mark);
				++count;
			}
		}
	}
	request.count = count;

	copy_to_user_ret((drm_buf_info_t *)arg,
			 &request,
			 sizeof(request),
			 -EFAULT);
	
	return 0;
}

int drm_markbufs(struct inode *inode, struct file *filp, unsigned int cmd,
		 unsigned long arg)
{
	drm_file_t	 *priv	 = filp->private_data;
	drm_device_t	 *dev	 = priv->dev;
	drm_device_dma_t *dma	 = dev->dma;
	drm_buf_desc_t	 request;
	int		 order;
	drm_buf_entry_t	 *entry;

	if (!dma) return -EINVAL;

	copy_from_user_ret(&request,
			   (drm_buf_desc_t *)arg,
			   sizeof(request),
			   -EFAULT);

	DRM_DEBUG("%d, %d, %d\n",
		  request.size, request.low_mark, request.high_mark);
	order = drm_order(request.size);
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) return -EINVAL;
	entry = &dma->bufs[order];

	if (request.low_mark < 0 || request.low_mark > entry->buf_count)
		return -EINVAL;
	if (request.high_mark < 0 || request.high_mark > entry->buf_count)
		return -EINVAL;

	entry->freelist.low_mark  = request.low_mark;
	entry->freelist.high_mark = request.high_mark;
	
	return 0;
}

int drm_freebufs(struct inode *inode, struct file *filp, unsigned int cmd,
		 unsigned long arg)
{
	drm_file_t	 *priv	 = filp->private_data;
	drm_device_t	 *dev	 = priv->dev;
	drm_device_dma_t *dma	 = dev->dma;
	drm_buf_free_t	 request;
	int		 i;
	int		 idx;
	drm_buf_t	 *buf;

	if (!dma) return -EINVAL;

	copy_from_user_ret(&request,
			   (drm_buf_free_t *)arg,
			   sizeof(request),
			   -EFAULT);

	DRM_DEBUG("%d\n", request.count);
	for (i = 0; i < request.count; i++) {
		copy_from_user_ret(&idx,
				   &request.list[i],
				   sizeof(idx),
				   -EFAULT);
		if (idx < 0 || idx >= dma->buf_count) {
			DRM_ERROR("Index %d (of %d max)\n",
				  idx, dma->buf_count - 1);
			return -EINVAL;
		}
		buf = dma->buflist[idx];
		if (buf->pid != current->pid) {
			DRM_ERROR("Process %d freeing buffer owned by %d\n",
				  current->pid, buf->pid);
			return -EINVAL;
		}
		drm_free_buffer(dev, buf);
	}
	
	return 0;
}

int drm_mapbufs(struct inode *inode, struct file *filp, unsigned int cmd,
		unsigned long arg)
{
	drm_file_t	 *priv	 = filp->private_data;
	drm_device_t	 *dev	 = priv->dev;
	drm_device_dma_t *dma	 = dev->dma;
	int		 retcode = 0;
	const int	 zero	 = 0;
	unsigned long	 virtual;
	unsigned long	 address;
	drm_buf_map_t	 request;
	int		 i;

	if (!dma) return -EINVAL;
	
	DRM_DEBUG("\n");

	spin_lock(&dev->count_lock);
	if (atomic_read(&dev->buf_alloc)) {
		spin_unlock(&dev->count_lock);
		return -EBUSY;
	}
	++dev->buf_use;		/* Can't allocate more after this call */
	spin_unlock(&dev->count_lock);

	copy_from_user_ret(&request,
			   (drm_buf_map_t *)arg,
			   sizeof(request),
			   -EFAULT);

	if (request.count >= dma->buf_count) {
		virtual = do_mmap(filp, 0, dma->byte_count,
				  PROT_READ|PROT_WRITE, MAP_SHARED, 0);
		if (virtual > -1024UL) {
				/* Real error */
			retcode = (signed long)virtual;
			goto done;
		}
		request.virtual = (void *)virtual;

		for (i = 0; i < dma->buf_count; i++) {
			if (copy_to_user(&request.list[i].idx,
					 &dma->buflist[i]->idx,
					 sizeof(request.list[0].idx))) {
				retcode = -EFAULT;
				goto done;
			}
			if (copy_to_user(&request.list[i].total,
					 &dma->buflist[i]->total,
					 sizeof(request.list[0].total))) {
				retcode = -EFAULT;
				goto done;
			}
			if (copy_to_user(&request.list[i].used,
					 &zero,
					 sizeof(zero))) {
				retcode = -EFAULT;
				goto done;
			}
			address = virtual + dma->buflist[i]->offset;
			if (copy_to_user(&request.list[i].address,
					 &address,
					 sizeof(address))) {
				retcode = -EFAULT;
				goto done;
			}
		}
	}
done:
	request.count = dma->buf_count;
	DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);

	copy_to_user_ret((drm_buf_map_t *)arg,
			 &request,
			 sizeof(request),
			 -EFAULT);

	return retcode;
}