1 /* bufs.c -- IOCTLs to manage buffers -*- linux-c -*-
2  * Created: Tue Feb  2 08:37:54 1999 by faith@precisioninsight.com
3  *
4  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * 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  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26  *
27  * Authors:
28  *    Rickard E. (Rik) Faith <faith@valinux.com>
29  *
30  */
31 
32 #define __NO_VERSION__
33 #include <linux/config.h>
34 #include "drmP.h"
35 #include "linux/un.h"
36 
37 				/* Compute order.  Can be made faster. */
drm_order(unsigned long size)38 int drm_order(unsigned long size)
39 {
40 	int	      order;
41 	unsigned long tmp;
42 
43 	for (order = 0, tmp = size; tmp >>= 1; ++order);
44 	if (size & ~(1 << order)) ++order;
45 	return order;
46 }
47 
drm_addmap(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)48 int drm_addmap(struct inode *inode, struct file *filp, unsigned int cmd,
49 	       unsigned long arg)
50 {
51 	drm_file_t	*priv	= filp->private_data;
52 	drm_device_t	*dev	= priv->dev;
53 	drm_map_t	*map;
54 
55 	if (!(filp->f_mode & 3)) return -EACCES; /* Require read/write */
56 
57 	map	     = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
58 	if (!map) return -ENOMEM;
59 	if (copy_from_user(map, (drm_map_t *)arg, sizeof(*map))) {
60 		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
61 		return -EFAULT;
62 	}
63 
64 	DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
65 		  map->offset, map->size, map->type);
66 	if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
67 		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
68 		return -EINVAL;
69 	}
70 	map->mtrr   = -1;
71 	map->handle = 0;
72 
73 	switch (map->type) {
74 	case _DRM_REGISTERS:
75 	case _DRM_FRAME_BUFFER:
76 #ifndef __sparc__
77 		if (map->offset + map->size < map->offset
78 		    || map->offset < virt_to_phys(high_memory)) {
79 			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
80 			return -EINVAL;
81 		}
82 #endif
83 #ifdef CONFIG_MTRR
84 		if (map->type == _DRM_FRAME_BUFFER
85 		    || (map->flags & _DRM_WRITE_COMBINING)) {
86 			map->mtrr = mtrr_add(map->offset, map->size,
87 					     MTRR_TYPE_WRCOMB, 1);
88 		}
89 #endif
90 		map->handle = drm_ioremap(map->offset, map->size, dev);
91 		break;
92 
93 
94 	case _DRM_SHM:
95 		map->handle = (void *)drm_alloc_pages(drm_order(map->size)
96 						      - PAGE_SHIFT,
97 						      DRM_MEM_SAREA);
98 		DRM_DEBUG("%ld %d %p\n", map->size, drm_order(map->size),
99 			  map->handle);
100 		if (!map->handle) {
101 			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
102 			return -ENOMEM;
103 		}
104 		map->offset = (unsigned long)map->handle;
105 		if (map->flags & _DRM_CONTAINS_LOCK) {
106 			dev->lock.hw_lock = map->handle; /* Pointer to lock */
107 		}
108 		break;
109 #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
110 	case _DRM_AGP:
111 		map->offset = map->offset + dev->agp->base;
112 		break;
113 #endif
114 	default:
115 		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
116 		return -EINVAL;
117 	}
118 
119 	down(&dev->struct_sem);
120 	if (dev->maplist) {
121 		++dev->map_count;
122 		dev->maplist = drm_realloc(dev->maplist,
123 					   (dev->map_count-1)
124 					   * sizeof(*dev->maplist),
125 					   dev->map_count
126 					   * sizeof(*dev->maplist),
127 					   DRM_MEM_MAPS);
128 	} else {
129 		dev->map_count = 1;
130 		dev->maplist = drm_alloc(dev->map_count*sizeof(*dev->maplist),
131 					 DRM_MEM_MAPS);
132 	}
133 	dev->maplist[dev->map_count-1] = map;
134 	up(&dev->struct_sem);
135 
136 	if (copy_to_user((drm_map_t *)arg, map, sizeof(*map)))
137 		return -EFAULT;
138 	if (map->type != _DRM_SHM) {
139 		if (copy_to_user(&((drm_map_t *)arg)->handle,
140 				 &map->offset,
141 				 sizeof(map->offset)))
142 			return -EFAULT;
143 	}
144 	return 0;
145 }
146 
drm_addbufs(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)147 int drm_addbufs(struct inode *inode, struct file *filp, unsigned int cmd,
148 		unsigned long arg)
149 {
150 	drm_file_t	 *priv	 = filp->private_data;
151 	drm_device_t	 *dev	 = priv->dev;
152 	drm_device_dma_t *dma	 = dev->dma;
153 	drm_buf_desc_t	 request;
154 	int		 count;
155 	int		 order;
156 	int		 size;
157 	int		 total;
158 	int		 page_order;
159 	drm_buf_entry_t	 *entry;
160 	unsigned long	 page;
161 	drm_buf_t	 *buf;
162 	int		 alignment;
163 	unsigned long	 offset;
164 	int		 i;
165 	int		 byte_count;
166 	int		 page_count;
167 
168 	if (!dma) return -EINVAL;
169 
170 	if (copy_from_user(&request,
171 			   (drm_buf_desc_t *)arg,
172 			   sizeof(request)))
173 		return -EFAULT;
174 
175 	count	   = request.count;
176 	order	   = drm_order(request.size);
177 	size	   = 1 << order;
178 
179 	DRM_DEBUG("count = %d, size = %d (%d), order = %d, queue_count = %d\n",
180 		  request.count, request.size, size, order, dev->queue_count);
181 
182 	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) return -EINVAL;
183 	if (dev->queue_count) return -EBUSY; /* Not while in use */
184 
185 	alignment  = (request.flags & _DRM_PAGE_ALIGN) ? PAGE_ALIGN(size):size;
186 	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
187 	total	   = PAGE_SIZE << page_order;
188 
189 	spin_lock(&dev->count_lock);
190 	if (dev->buf_use) {
191 		spin_unlock(&dev->count_lock);
192 		return -EBUSY;
193 	}
194 	atomic_inc(&dev->buf_alloc);
195 	spin_unlock(&dev->count_lock);
196 
197 	down(&dev->struct_sem);
198 	entry = &dma->bufs[order];
199 	if (entry->buf_count) {
200 		up(&dev->struct_sem);
201 		atomic_dec(&dev->buf_alloc);
202 		return -ENOMEM;	/* May only call once for each order */
203 	}
204 
205 	if(count < 0 || count > 4096)
206 	{
207 		up(&dev->struct_sem);
208 		return -EINVAL;
209 	}
210 
211 	entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
212 				   DRM_MEM_BUFS);
213 	if (!entry->buflist) {
214 		up(&dev->struct_sem);
215 		atomic_dec(&dev->buf_alloc);
216 		return -ENOMEM;
217 	}
218 	memset(entry->buflist, 0, count * sizeof(*entry->buflist));
219 
220 	entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
221 				   DRM_MEM_SEGS);
222 	if (!entry->seglist) {
223 		drm_free(entry->buflist,
224 			 count * sizeof(*entry->buflist),
225 			 DRM_MEM_BUFS);
226 		up(&dev->struct_sem);
227 		atomic_dec(&dev->buf_alloc);
228 		return -ENOMEM;
229 	}
230 	memset(entry->seglist, 0, count * sizeof(*entry->seglist));
231 
232 	dma->pagelist = drm_realloc(dma->pagelist,
233 				    dma->page_count * sizeof(*dma->pagelist),
234 				    (dma->page_count + (count << page_order))
235 				    * sizeof(*dma->pagelist),
236 				    DRM_MEM_PAGES);
237 	DRM_DEBUG("pagelist: %d entries\n",
238 		  dma->page_count + (count << page_order));
239 
240 
241 	entry->buf_size	  = size;
242 	entry->page_order = page_order;
243 	byte_count	  = 0;
244 	page_count	  = 0;
245 	while (entry->buf_count < count) {
246 		if (!(page = drm_alloc_pages(page_order, DRM_MEM_DMA))) break;
247 		entry->seglist[entry->seg_count++] = page;
248 		for (i = 0; i < (1 << page_order); i++) {
249 			DRM_DEBUG("page %d @ 0x%08lx\n",
250 				  dma->page_count + page_count,
251 				  page + PAGE_SIZE * i);
252 			dma->pagelist[dma->page_count + page_count++]
253 				= page + PAGE_SIZE * i;
254 		}
255 		for (offset = 0;
256 		     offset + size <= total && entry->buf_count < count;
257 		     offset += alignment, ++entry->buf_count) {
258 			buf	     = &entry->buflist[entry->buf_count];
259 			buf->idx     = dma->buf_count + entry->buf_count;
260 			buf->total   = alignment;
261 			buf->order   = order;
262 			buf->used    = 0;
263 			buf->offset  = (dma->byte_count + byte_count + offset);
264 			buf->address = (void *)(page + offset);
265 			buf->next    = NULL;
266 			buf->waiting = 0;
267 			buf->pending = 0;
268 			init_waitqueue_head(&buf->dma_wait);
269 			buf->pid     = 0;
270 #if DRM_DMA_HISTOGRAM
271 			buf->time_queued     = 0;
272 			buf->time_dispatched = 0;
273 			buf->time_completed  = 0;
274 			buf->time_freed	     = 0;
275 #endif
276 			DRM_DEBUG("buffer %d @ %p\n",
277 				  entry->buf_count, buf->address);
278 		}
279 		byte_count += PAGE_SIZE << page_order;
280 	}
281 
282 	dma->buflist = drm_realloc(dma->buflist,
283 				   dma->buf_count * sizeof(*dma->buflist),
284 				   (dma->buf_count + entry->buf_count)
285 				   * sizeof(*dma->buflist),
286 				   DRM_MEM_BUFS);
287 	for (i = dma->buf_count; i < dma->buf_count + entry->buf_count; i++)
288 		dma->buflist[i] = &entry->buflist[i - dma->buf_count];
289 
290 	dma->buf_count	+= entry->buf_count;
291 	dma->seg_count	+= entry->seg_count;
292 	dma->page_count += entry->seg_count << page_order;
293 	dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
294 
295 	drm_freelist_create(&entry->freelist, entry->buf_count);
296 	for (i = 0; i < entry->buf_count; i++) {
297 		drm_freelist_put(dev, &entry->freelist, &entry->buflist[i]);
298 	}
299 
300 	up(&dev->struct_sem);
301 
302 	request.count = entry->buf_count;
303 	request.size  = size;
304 
305 	if (copy_to_user((drm_buf_desc_t *)arg,
306 			 &request,
307 			 sizeof(request)))
308 		return -EFAULT;
309 
310 	atomic_dec(&dev->buf_alloc);
311 	return 0;
312 }
313 
drm_infobufs(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)314 int drm_infobufs(struct inode *inode, struct file *filp, unsigned int cmd,
315 		 unsigned long arg)
316 {
317 	drm_file_t	 *priv	 = filp->private_data;
318 	drm_device_t	 *dev	 = priv->dev;
319 	drm_device_dma_t *dma	 = dev->dma;
320 	drm_buf_info_t	 request;
321 	int		 i;
322 	int		 count;
323 
324 	if (!dma) return -EINVAL;
325 
326 	spin_lock(&dev->count_lock);
327 	if (atomic_read(&dev->buf_alloc)) {
328 		spin_unlock(&dev->count_lock);
329 		return -EBUSY;
330 	}
331 	++dev->buf_use;		/* Can't allocate more after this call */
332 	spin_unlock(&dev->count_lock);
333 
334 	if (copy_from_user(&request,
335 			   (drm_buf_info_t *)arg,
336 			   sizeof(request)))
337 		return -EFAULT;
338 
339 	for (i = 0, count = 0; i < DRM_MAX_ORDER+1; i++) {
340 		if (dma->bufs[i].buf_count) ++count;
341 	}
342 
343 	DRM_DEBUG("count = %d\n", count);
344 
345 	if (request.count >= count) {
346 		for (i = 0, count = 0; i < DRM_MAX_ORDER+1; i++) {
347 			if (dma->bufs[i].buf_count) {
348 				if (copy_to_user(&request.list[count].count,
349 						 &dma->bufs[i].buf_count,
350 						 sizeof(dma->bufs[0]
351 							.buf_count)) ||
352 				    copy_to_user(&request.list[count].size,
353 						 &dma->bufs[i].buf_size,
354 						 sizeof(dma->bufs[0].buf_size)) ||
355 				    copy_to_user(&request.list[count].low_mark,
356 						 &dma->bufs[i]
357 						 .freelist.low_mark,
358 						 sizeof(dma->bufs[0]
359 							.freelist.low_mark)) ||
360 				    copy_to_user(&request.list[count]
361 						 .high_mark,
362 						 &dma->bufs[i]
363 						 .freelist.high_mark,
364 						 sizeof(dma->bufs[0]
365 							.freelist.high_mark)))
366 					return -EFAULT;
367 
368 				DRM_DEBUG("%d %d %d %d %d\n",
369 					  i,
370 					  dma->bufs[i].buf_count,
371 					  dma->bufs[i].buf_size,
372 					  dma->bufs[i].freelist.low_mark,
373 					  dma->bufs[i].freelist.high_mark);
374 				++count;
375 			}
376 		}
377 	}
378 	request.count = count;
379 
380 	if (copy_to_user((drm_buf_info_t *)arg,
381 			 &request,
382 			 sizeof(request)))
383 		return -EFAULT;
384 
385 	return 0;
386 }
387 
drm_markbufs(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)388 int drm_markbufs(struct inode *inode, struct file *filp, unsigned int cmd,
389 		 unsigned long arg)
390 {
391 	drm_file_t	 *priv	 = filp->private_data;
392 	drm_device_t	 *dev	 = priv->dev;
393 	drm_device_dma_t *dma	 = dev->dma;
394 	drm_buf_desc_t	 request;
395 	int		 order;
396 	drm_buf_entry_t	 *entry;
397 
398 	if (!dma) return -EINVAL;
399 
400 	if (copy_from_user(&request,
401 			   (drm_buf_desc_t *)arg,
402 			   sizeof(request)))
403 		return -EFAULT;
404 
405 	DRM_DEBUG("%d, %d, %d\n",
406 		  request.size, request.low_mark, request.high_mark);
407 	order = drm_order(request.size);
408 	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) return -EINVAL;
409 	entry = &dma->bufs[order];
410 
411 	if (request.low_mark < 0 || request.low_mark > entry->buf_count)
412 		return -EINVAL;
413 	if (request.high_mark < 0 || request.high_mark > entry->buf_count)
414 		return -EINVAL;
415 
416 	entry->freelist.low_mark  = request.low_mark;
417 	entry->freelist.high_mark = request.high_mark;
418 
419 	return 0;
420 }
421 
drm_freebufs(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)422 int drm_freebufs(struct inode *inode, struct file *filp, unsigned int cmd,
423 		 unsigned long arg)
424 {
425 	drm_file_t	 *priv	 = filp->private_data;
426 	drm_device_t	 *dev	 = priv->dev;
427 	drm_device_dma_t *dma	 = dev->dma;
428 	drm_buf_free_t	 request;
429 	int		 i;
430 	int		 idx;
431 	drm_buf_t	 *buf;
432 
433 	if (!dma) return -EINVAL;
434 
435 	if (copy_from_user(&request,
436 			   (drm_buf_free_t *)arg,
437 			   sizeof(request)))
438 		return -EFAULT;
439 
440 	DRM_DEBUG("%d\n", request.count);
441 	for (i = 0; i < request.count; i++) {
442 		if (copy_from_user(&idx,
443 				   &request.list[i],
444 				   sizeof(idx)))
445 			return -EFAULT;
446 		if (idx < 0 || idx >= dma->buf_count) {
447 			DRM_ERROR("Index %d (of %d max)\n",
448 				  idx, dma->buf_count - 1);
449 			return -EINVAL;
450 		}
451 		buf = dma->buflist[idx];
452 		if (buf->pid != current->pid) {
453 			DRM_ERROR("Process %d freeing buffer owned by %d\n",
454 				  current->pid, buf->pid);
455 			return -EINVAL;
456 		}
457 		drm_free_buffer(dev, buf);
458 	}
459 
460 	return 0;
461 }
462 
drm_mapbufs(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)463 int drm_mapbufs(struct inode *inode, struct file *filp, unsigned int cmd,
464 		unsigned long arg)
465 {
466 	drm_file_t	 *priv	 = filp->private_data;
467 	drm_device_t	 *dev	 = priv->dev;
468 	drm_device_dma_t *dma	 = dev->dma;
469 	int		 retcode = 0;
470 	const int	 zero	 = 0;
471 	unsigned long	 virtual;
472 	unsigned long	 address;
473 	drm_buf_map_t	 request;
474 	int		 i;
475 
476 	if (!dma) return -EINVAL;
477 
478 	DRM_DEBUG("\n");
479 
480 	spin_lock(&dev->count_lock);
481 	if (atomic_read(&dev->buf_alloc)) {
482 		spin_unlock(&dev->count_lock);
483 		return -EBUSY;
484 	}
485 	++dev->buf_use;		/* Can't allocate more after this call */
486 	spin_unlock(&dev->count_lock);
487 
488 	if (copy_from_user(&request,
489 			   (drm_buf_map_t *)arg,
490 			   sizeof(request)))
491 		return -EFAULT;
492 
493 	if (request.count >= dma->buf_count) {
494 		down_write(&current->mm->mmap_sem);
495 		virtual = do_mmap(filp, 0, dma->byte_count,
496 				  PROT_READ|PROT_WRITE, MAP_SHARED, 0);
497 		up_write(&current->mm->mmap_sem);
498 		if (virtual > -1024UL) {
499 				/* Real error */
500 			retcode = (signed long)virtual;
501 			goto done;
502 		}
503 		request.virtual = (void *)virtual;
504 
505 		for (i = 0; i < dma->buf_count; i++) {
506 			if (copy_to_user(&request.list[i].idx,
507 					 &dma->buflist[i]->idx,
508 					 sizeof(request.list[0].idx))) {
509 				retcode = -EFAULT;
510 				goto done;
511 			}
512 			if (copy_to_user(&request.list[i].total,
513 					 &dma->buflist[i]->total,
514 					 sizeof(request.list[0].total))) {
515 				retcode = -EFAULT;
516 				goto done;
517 			}
518 			if (copy_to_user(&request.list[i].used,
519 					 &zero,
520 					 sizeof(zero))) {
521 				retcode = -EFAULT;
522 				goto done;
523 			}
524 			address = virtual + dma->buflist[i]->offset;
525 			if (copy_to_user(&request.list[i].address,
526 					 &address,
527 					 sizeof(address))) {
528 				retcode = -EFAULT;
529 				goto done;
530 			}
531 		}
532 	}
533 done:
534 	request.count = dma->buf_count;
535 	DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
536 
537 	if (copy_to_user((drm_buf_map_t *)arg,
538 			 &request,
539 			 sizeof(request)))
540 		return -EFAULT;
541 
542 	return retcode;
543 }
544