1
/* drm_bufs.h -- Generic buffer template -*- linux-c -*-
2
* Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
4
* Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
5
* Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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:
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
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
* VA LINUX SYSTEMS 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
25
* OTHER DEALINGS IN THE SOFTWARE.
28
* Rickard E. (Rik) Faith <faith@valinux.com>
29
* Gareth Hughes <gareth@valinux.com>
32
#define __NO_VERSION__
33
#include <linux/vmalloc.h>
36
#ifndef __HAVE_PCI_DMA
37
#define __HAVE_PCI_DMA 0
44
#ifndef DRIVER_BUF_PRIV_T
45
#define DRIVER_BUF_PRIV_T u32
47
#ifndef DRIVER_AGP_BUFFERS_MAP
48
#if __HAVE_AGP && __HAVE_DMA
49
#error "You must define DRIVER_AGP_BUFFERS_MAP()"
51
#define DRIVER_AGP_BUFFERS_MAP( dev ) NULL
56
* Compute order. Can be made faster.
58
int DRM(order)( unsigned long size )
63
for ( order = 0, tmp = size ; tmp >>= 1 ; ++order );
65
if ( size & ~(1 << order) )
71
int DRM(addmap)( struct inode *inode, struct file *filp,
72
unsigned int cmd, unsigned long arg )
74
drm_file_t *priv = filp->private_data;
75
drm_device_t *dev = priv->dev;
79
if ( !(filp->f_mode & 3) ) return -EACCES; /* Require read/write */
81
map = DRM(alloc)( sizeof(*map), DRM_MEM_MAPS );
85
if ( copy_from_user( map, (drm_map_t *)arg, sizeof(*map) ) ) {
86
DRM(free)( map, sizeof(*map), DRM_MEM_MAPS );
90
/* Only allow shared memory to be removable since we only keep enough
91
* book keeping information about shared memory to allow for removal
92
* when processes fork.
94
if ( (map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM ) {
95
DRM(free)( map, sizeof(*map), DRM_MEM_MAPS );
98
DRM_DEBUG( "offset = 0x%08lx, size = 0x%08lx, type = %d\n",
99
map->offset, map->size, map->type );
100
if ( (map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK)) ) {
101
DRM(free)( map, sizeof(*map), DRM_MEM_MAPS );
107
switch ( map->type ) {
109
case _DRM_FRAME_BUFFER:
110
#if !defined(__sparc__) && !defined(__alpha__)
111
if ( map->offset + map->size < map->offset ||
112
map->offset < virt_to_phys(high_memory) ) {
113
DRM(free)( map, sizeof(*map), DRM_MEM_MAPS );
118
map->offset += dev->hose->mem_space->start;
120
#if __REALLY_HAVE_MTRR
121
if ( map->type == _DRM_FRAME_BUFFER ||
122
(map->flags & _DRM_WRITE_COMBINING) ) {
123
map->mtrr = mtrr_add( map->offset, map->size,
124
MTRR_TYPE_WRCOMB, 1 );
127
map->handle = DRM(ioremap)( map->offset, map->size );
131
map->handle = vmalloc_32(map->size);
132
DRM_DEBUG( "%ld %d %p\n",
133
map->size, DRM(order)( map->size ), map->handle );
134
if ( !map->handle ) {
135
DRM(free)( map, sizeof(*map), DRM_MEM_MAPS );
138
map->offset = (unsigned long)map->handle;
139
if ( map->flags & _DRM_CONTAINS_LOCK ) {
141
dev->lock.hw_lock = map->handle; /* Pointer to lock */
144
#if __REALLY_HAVE_AGP
147
map->offset += dev->hose->mem_space->start;
149
map->offset = map->offset + dev->agp->base;
150
map->mtrr = dev->agp->agp_mtrr; /* for getmap */
153
case _DRM_SCATTER_GATHER:
155
DRM(free)(map, sizeof(*map), DRM_MEM_MAPS);
158
map->offset = map->offset + dev->sg->handle;
162
DRM(free)( map, sizeof(*map), DRM_MEM_MAPS );
166
list = DRM(alloc)(sizeof(*list), DRM_MEM_MAPS);
168
DRM(free)(map, sizeof(*map), DRM_MEM_MAPS);
171
memset(list, 0, sizeof(*list));
174
down(&dev->struct_sem);
175
list_add(&list->head, &dev->maplist->head);
176
up(&dev->struct_sem);
178
if ( copy_to_user( (drm_map_t *)arg, map, sizeof(*map) ) )
180
if ( map->type != _DRM_SHM ) {
181
if ( copy_to_user( &((drm_map_t *)arg)->handle,
183
sizeof(map->offset) ) )
190
/* Remove a map private from list and deallocate resources if the mapping
194
int DRM(rmmap)(struct inode *inode, struct file *filp,
195
unsigned int cmd, unsigned long arg)
197
drm_file_t *priv = filp->private_data;
198
drm_device_t *dev = priv->dev;
199
struct list_head *list;
200
drm_map_list_t *r_list = NULL;
201
drm_vma_entry_t *pt, *prev;
206
if (copy_from_user(&request, (drm_map_t *)arg,
211
down(&dev->struct_sem);
212
list = &dev->maplist->head;
213
list_for_each(list, &dev->maplist->head) {
214
r_list = (drm_map_list_t *) list;
217
r_list->map->handle == request.handle &&
218
r_list->map->flags & _DRM_REMOVABLE) break;
221
/* List has wrapped around to the head pointer, or its empty we didn't
224
if(list == (&dev->maplist->head)) {
225
up(&dev->struct_sem);
230
DRM(free)(list, sizeof(*list), DRM_MEM_MAPS);
232
for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) {
233
if (pt->vma->vm_private_data == map) found_maps++;
239
case _DRM_FRAME_BUFFER:
240
#if __REALLY_HAVE_MTRR
241
if (map->mtrr >= 0) {
243
retcode = mtrr_del(map->mtrr,
246
DRM_DEBUG("mtrr_del = %d\n", retcode);
249
DRM(ioremapfree)(map->handle, map->size);
255
case _DRM_SCATTER_GATHER:
258
DRM(free)(map, sizeof(*map), DRM_MEM_MAPS);
260
up(&dev->struct_sem);
267
static void DRM(cleanup_buf_error)(drm_buf_entry_t *entry)
271
if (entry->seg_count) {
272
for (i = 0; i < entry->seg_count; i++) {
273
DRM(free_pages)(entry->seglist[i],
277
DRM(free)(entry->seglist,
279
sizeof(*entry->seglist),
282
entry->seg_count = 0;
285
if(entry->buf_count) {
286
for(i = 0; i < entry->buf_count; i++) {
287
if(entry->buflist[i].dev_private) {
288
DRM(free)(entry->buflist[i].dev_private,
289
entry->buflist[i].dev_priv_size,
293
DRM(free)(entry->buflist,
295
sizeof(*entry->buflist),
298
#if __HAVE_DMA_FREELIST
299
DRM(freelist_destroy)(&entry->freelist);
302
entry->buf_count = 0;
306
#if __REALLY_HAVE_AGP
307
int DRM(addbufs_agp)( struct inode *inode, struct file *filp,
308
unsigned int cmd, unsigned long arg )
310
drm_file_t *priv = filp->private_data;
311
drm_device_t *dev = priv->dev;
312
drm_device_dma_t *dma = dev->dma;
313
drm_buf_desc_t request;
314
drm_buf_entry_t *entry;
316
unsigned long offset;
317
unsigned long agp_offset;
326
drm_buf_t **temp_buflist;
328
if ( !dma ) return -EINVAL;
330
if ( copy_from_user( &request, (drm_buf_desc_t *)arg,
334
count = request.count;
335
order = DRM(order)( request.size );
338
alignment = (request.flags & _DRM_PAGE_ALIGN)
339
? PAGE_ALIGN(size) : size;
340
page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
341
total = PAGE_SIZE << page_order;
344
agp_offset = dev->agp->base + request.agp_start;
346
DRM_DEBUG( "count: %d\n", count );
347
DRM_DEBUG( "order: %d\n", order );
348
DRM_DEBUG( "size: %d\n", size );
349
DRM_DEBUG( "agp_offset: %ld\n", agp_offset );
350
DRM_DEBUG( "alignment: %d\n", alignment );
351
DRM_DEBUG( "page_order: %d\n", page_order );
352
DRM_DEBUG( "total: %d\n", total );
354
if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
355
if ( dev->queue_count ) return -EBUSY; /* Not while in use */
357
spin_lock( &dev->count_lock );
358
if ( dev->buf_use ) {
359
spin_unlock( &dev->count_lock );
362
atomic_inc( &dev->buf_alloc );
363
spin_unlock( &dev->count_lock );
365
down( &dev->struct_sem );
366
entry = &dma->bufs[order];
367
if ( entry->buf_count ) {
368
up( &dev->struct_sem );
369
atomic_dec( &dev->buf_alloc );
370
return -ENOMEM; /* May only call once for each order */
373
if (count < 0 || count > 4096) {
374
up( &dev->struct_sem );
375
atomic_dec( &dev->buf_alloc );
379
entry->buflist = DRM(alloc)( count * sizeof(*entry->buflist),
381
if ( !entry->buflist ) {
382
up( &dev->struct_sem );
383
atomic_dec( &dev->buf_alloc );
386
memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
388
entry->buf_size = size;
389
entry->page_order = page_order;
393
while ( entry->buf_count < count ) {
394
buf = &entry->buflist[entry->buf_count];
395
buf->idx = dma->buf_count + entry->buf_count;
396
buf->total = alignment;
400
buf->offset = (dma->byte_count + offset);
401
buf->bus_address = agp_offset + offset;
402
buf->address = (void *)(agp_offset + offset);
406
init_waitqueue_head( &buf->dma_wait );
409
buf->dev_priv_size = sizeof(DRIVER_BUF_PRIV_T);
410
buf->dev_private = DRM(alloc)( sizeof(DRIVER_BUF_PRIV_T),
412
if(!buf->dev_private) {
413
/* Set count correctly so we free the proper amount. */
414
entry->buf_count = count;
415
DRM(cleanup_buf_error)(entry);
417
memset( buf->dev_private, 0, buf->dev_priv_size );
419
#if __HAVE_DMA_HISTOGRAM
420
buf->time_queued = 0;
421
buf->time_dispatched = 0;
422
buf->time_completed = 0;
425
DRM_DEBUG( "buffer %d @ %p\n",
426
entry->buf_count, buf->address );
430
byte_count += PAGE_SIZE << page_order;
433
DRM_DEBUG( "byte_count: %d\n", byte_count );
435
temp_buflist = DRM(realloc)( dma->buflist,
436
dma->buf_count * sizeof(*dma->buflist),
437
(dma->buf_count + entry->buf_count)
438
* sizeof(*dma->buflist),
441
/* Free the entry because it isn't valid */
442
DRM(cleanup_buf_error)(entry);
443
up( &dev->struct_sem );
444
atomic_dec( &dev->buf_alloc );
447
dma->buflist = temp_buflist;
449
for ( i = 0 ; i < entry->buf_count ; i++ ) {
450
dma->buflist[i + dma->buf_count] = &entry->buflist[i];
453
dma->buf_count += entry->buf_count;
454
dma->byte_count += byte_count;
456
DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
457
DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
459
#if __HAVE_DMA_FREELIST
460
DRM(freelist_create)( &entry->freelist, entry->buf_count );
461
for ( i = 0 ; i < entry->buf_count ; i++ ) {
462
DRM(freelist_put)( dev, &entry->freelist, &entry->buflist[i] );
465
up( &dev->struct_sem );
467
request.count = entry->buf_count;
470
if ( copy_to_user( (drm_buf_desc_t *)arg, &request, sizeof(request) ) )
473
dma->flags = _DRM_DMA_USE_AGP;
475
atomic_dec( &dev->buf_alloc );
478
#endif /* __REALLY_HAVE_AGP */
481
int DRM(addbufs_pci)( struct inode *inode, struct file *filp,
482
unsigned int cmd, unsigned long arg )
484
drm_file_t *priv = filp->private_data;
485
drm_device_t *dev = priv->dev;
486
drm_device_dma_t *dma = dev->dma;
487
drm_buf_desc_t request;
493
drm_buf_entry_t *entry;
497
unsigned long offset;
501
unsigned long *temp_pagelist;
502
drm_buf_t **temp_buflist;
504
if ( !dma ) return -EINVAL;
506
if ( copy_from_user( &request, (drm_buf_desc_t *)arg,
510
count = request.count;
511
order = DRM(order)( request.size );
514
DRM_DEBUG( "count=%d, size=%d (%d), order=%d, queue_count=%d\n",
515
request.count, request.size, size,
516
order, dev->queue_count );
518
if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
519
if ( dev->queue_count ) return -EBUSY; /* Not while in use */
521
alignment = (request.flags & _DRM_PAGE_ALIGN)
522
? PAGE_ALIGN(size) : size;
523
page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
524
total = PAGE_SIZE << page_order;
526
spin_lock( &dev->count_lock );
527
if ( dev->buf_use ) {
528
spin_unlock( &dev->count_lock );
531
atomic_inc( &dev->buf_alloc );
532
spin_unlock( &dev->count_lock );
534
down( &dev->struct_sem );
535
entry = &dma->bufs[order];
536
if ( entry->buf_count ) {
537
up( &dev->struct_sem );
538
atomic_dec( &dev->buf_alloc );
539
return -ENOMEM; /* May only call once for each order */
542
if (count < 0 || count > 4096) {
543
up( &dev->struct_sem );
544
atomic_dec( &dev->buf_alloc );
548
entry->buflist = DRM(alloc)( count * sizeof(*entry->buflist),
550
if ( !entry->buflist ) {
551
up( &dev->struct_sem );
552
atomic_dec( &dev->buf_alloc );
555
memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
557
entry->seglist = DRM(alloc)( count * sizeof(*entry->seglist),
559
if ( !entry->seglist ) {
560
DRM(free)( entry->buflist,
561
count * sizeof(*entry->buflist),
563
up( &dev->struct_sem );
564
atomic_dec( &dev->buf_alloc );
567
memset( entry->seglist, 0, count * sizeof(*entry->seglist) );
569
temp_pagelist = DRM(realloc)( dma->pagelist,
570
dma->page_count * sizeof(*dma->pagelist),
571
(dma->page_count + (count << page_order))
572
* sizeof(*dma->pagelist),
575
DRM(free)( entry->buflist,
576
count * sizeof(*entry->buflist),
578
DRM(free)( entry->seglist,
579
count * sizeof(*entry->seglist),
581
up( &dev->struct_sem );
582
atomic_dec( &dev->buf_alloc );
586
dma->pagelist = temp_pagelist;
587
DRM_DEBUG( "pagelist: %d entries\n",
588
dma->page_count + (count << page_order) );
590
entry->buf_size = size;
591
entry->page_order = page_order;
595
while ( entry->buf_count < count ) {
596
page = DRM(alloc_pages)( page_order, DRM_MEM_DMA );
598
entry->seglist[entry->seg_count++] = page;
599
for ( i = 0 ; i < (1 << page_order) ; i++ ) {
600
DRM_DEBUG( "page %d @ 0x%08lx\n",
601
dma->page_count + page_count,
602
page + PAGE_SIZE * i );
603
dma->pagelist[dma->page_count + page_count++]
604
= page + PAGE_SIZE * i;
607
offset + size <= total && entry->buf_count < count ;
608
offset += alignment, ++entry->buf_count ) {
609
buf = &entry->buflist[entry->buf_count];
610
buf->idx = dma->buf_count + entry->buf_count;
611
buf->total = alignment;
614
buf->offset = (dma->byte_count + byte_count + offset);
615
buf->address = (void *)(page + offset);
619
init_waitqueue_head( &buf->dma_wait );
621
#if __HAVE_DMA_HISTOGRAM
622
buf->time_queued = 0;
623
buf->time_dispatched = 0;
624
buf->time_completed = 0;
627
DRM_DEBUG( "buffer %d @ %p\n",
628
entry->buf_count, buf->address );
630
byte_count += PAGE_SIZE << page_order;
633
temp_buflist = DRM(realloc)( dma->buflist,
634
dma->buf_count * sizeof(*dma->buflist),
635
(dma->buf_count + entry->buf_count)
636
* sizeof(*dma->buflist),
639
/* Free the entry because it isn't valid */
640
DRM(cleanup_buf_error)(entry);
641
up( &dev->struct_sem );
642
atomic_dec( &dev->buf_alloc );
645
dma->buflist = temp_buflist;
647
for ( i = 0 ; i < entry->buf_count ; i++ ) {
648
dma->buflist[i + dma->buf_count] = &entry->buflist[i];
651
dma->buf_count += entry->buf_count;
652
dma->seg_count += entry->seg_count;
653
dma->page_count += entry->seg_count << page_order;
654
dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
656
#if __HAVE_DMA_FREELIST
657
DRM(freelist_create)( &entry->freelist, entry->buf_count );
658
for ( i = 0 ; i < entry->buf_count ; i++ ) {
659
DRM(freelist_put)( dev, &entry->freelist, &entry->buflist[i] );
662
up( &dev->struct_sem );
664
request.count = entry->buf_count;
667
if ( copy_to_user( (drm_buf_desc_t *)arg, &request, sizeof(request) ) )
670
atomic_dec( &dev->buf_alloc );
674
#endif /* __HAVE_PCI_DMA */
677
int DRM(addbufs_sg)( struct inode *inode, struct file *filp,
678
unsigned int cmd, unsigned long arg )
680
drm_file_t *priv = filp->private_data;
681
drm_device_t *dev = priv->dev;
682
drm_device_dma_t *dma = dev->dma;
683
drm_buf_desc_t request;
684
drm_buf_entry_t *entry;
686
unsigned long offset;
687
unsigned long agp_offset;
696
drm_buf_t **temp_buflist;
698
if ( !dma ) return -EINVAL;
700
if ( copy_from_user( &request, (drm_buf_desc_t *)arg,
704
count = request.count;
705
order = DRM(order)( request.size );
708
alignment = (request.flags & _DRM_PAGE_ALIGN)
709
? PAGE_ALIGN(size) : size;
710
page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
711
total = PAGE_SIZE << page_order;
714
agp_offset = request.agp_start;
716
DRM_DEBUG( "count: %d\n", count );
717
DRM_DEBUG( "order: %d\n", order );
718
DRM_DEBUG( "size: %d\n", size );
719
DRM_DEBUG( "agp_offset: %ld\n", agp_offset );
720
DRM_DEBUG( "alignment: %d\n", alignment );
721
DRM_DEBUG( "page_order: %d\n", page_order );
722
DRM_DEBUG( "total: %d\n", total );
724
if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
725
if ( dev->queue_count ) return -EBUSY; /* Not while in use */
727
spin_lock( &dev->count_lock );
728
if ( dev->buf_use ) {
729
spin_unlock( &dev->count_lock );
732
atomic_inc( &dev->buf_alloc );
733
spin_unlock( &dev->count_lock );
735
down( &dev->struct_sem );
736
entry = &dma->bufs[order];
737
if ( entry->buf_count ) {
738
up( &dev->struct_sem );
739
atomic_dec( &dev->buf_alloc );
740
return -ENOMEM; /* May only call once for each order */
743
if (count < 0 || count > 4096) {
744
up( &dev->struct_sem );
745
atomic_dec( &dev->buf_alloc );
749
entry->buflist = DRM(alloc)( count * sizeof(*entry->buflist),
751
if ( !entry->buflist ) {
752
up( &dev->struct_sem );
753
atomic_dec( &dev->buf_alloc );
756
memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
758
entry->buf_size = size;
759
entry->page_order = page_order;
763
while ( entry->buf_count < count ) {
764
buf = &entry->buflist[entry->buf_count];
765
buf->idx = dma->buf_count + entry->buf_count;
766
buf->total = alignment;
770
buf->offset = (dma->byte_count + offset);
771
buf->bus_address = agp_offset + offset;
772
buf->address = (void *)(agp_offset + offset + dev->sg->handle);
776
init_waitqueue_head( &buf->dma_wait );
779
buf->dev_priv_size = sizeof(DRIVER_BUF_PRIV_T);
780
buf->dev_private = DRM(alloc)( sizeof(DRIVER_BUF_PRIV_T),
782
if(!buf->dev_private) {
783
/* Set count correctly so we free the proper amount. */
784
entry->buf_count = count;
785
DRM(cleanup_buf_error)(entry);
786
up( &dev->struct_sem );
787
atomic_dec( &dev->buf_alloc );
791
memset( buf->dev_private, 0, buf->dev_priv_size );
793
# if __HAVE_DMA_HISTOGRAM
794
buf->time_queued = 0;
795
buf->time_dispatched = 0;
796
buf->time_completed = 0;
799
DRM_DEBUG( "buffer %d @ %p\n",
800
entry->buf_count, buf->address );
804
byte_count += PAGE_SIZE << page_order;
807
DRM_DEBUG( "byte_count: %d\n", byte_count );
809
temp_buflist = DRM(realloc)( dma->buflist,
810
dma->buf_count * sizeof(*dma->buflist),
811
(dma->buf_count + entry->buf_count)
812
* sizeof(*dma->buflist),
815
/* Free the entry because it isn't valid */
816
DRM(cleanup_buf_error)(entry);
817
up( &dev->struct_sem );
818
atomic_dec( &dev->buf_alloc );
821
dma->buflist = temp_buflist;
823
for ( i = 0 ; i < entry->buf_count ; i++ ) {
824
dma->buflist[i + dma->buf_count] = &entry->buflist[i];
827
dma->buf_count += entry->buf_count;
828
dma->byte_count += byte_count;
830
DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
831
DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
833
#if __HAVE_DMA_FREELIST
834
DRM(freelist_create)( &entry->freelist, entry->buf_count );
835
for ( i = 0 ; i < entry->buf_count ; i++ ) {
836
DRM(freelist_put)( dev, &entry->freelist, &entry->buflist[i] );
839
up( &dev->struct_sem );
841
request.count = entry->buf_count;
844
if ( copy_to_user( (drm_buf_desc_t *)arg, &request, sizeof(request) ) )
847
dma->flags = _DRM_DMA_USE_SG;
849
atomic_dec( &dev->buf_alloc );
852
#endif /* __HAVE_SG */
854
int DRM(addbufs)( struct inode *inode, struct file *filp,
855
unsigned int cmd, unsigned long arg )
857
drm_buf_desc_t request;
859
if ( copy_from_user( &request, (drm_buf_desc_t *)arg,
863
#if __REALLY_HAVE_AGP
864
if ( request.flags & _DRM_AGP_BUFFER )
865
return DRM(addbufs_agp)( inode, filp, cmd, arg );
869
if ( request.flags & _DRM_SG_BUFFER )
870
return DRM(addbufs_sg)( inode, filp, cmd, arg );
874
return DRM(addbufs_pci)( inode, filp, cmd, arg );
880
int DRM(infobufs)( struct inode *inode, struct file *filp,
881
unsigned int cmd, unsigned long arg )
883
drm_file_t *priv = filp->private_data;
884
drm_device_t *dev = priv->dev;
885
drm_device_dma_t *dma = dev->dma;
886
drm_buf_info_t request;
890
if ( !dma ) return -EINVAL;
892
spin_lock( &dev->count_lock );
893
if ( atomic_read( &dev->buf_alloc ) ) {
894
spin_unlock( &dev->count_lock );
897
++dev->buf_use; /* Can't allocate more after this call */
898
spin_unlock( &dev->count_lock );
900
if ( copy_from_user( &request,
901
(drm_buf_info_t *)arg,
905
for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
906
if ( dma->bufs[i].buf_count ) ++count;
909
DRM_DEBUG( "count = %d\n", count );
911
if ( request.count >= count ) {
912
for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
913
if ( dma->bufs[i].buf_count ) {
914
drm_buf_desc_t *to = &request.list[count];
915
drm_buf_entry_t *from = &dma->bufs[i];
916
drm_freelist_t *list = &dma->bufs[i].freelist;
917
if ( copy_to_user( &to->count,
919
sizeof(from->buf_count) ) ||
920
copy_to_user( &to->size,
922
sizeof(from->buf_size) ) ||
923
copy_to_user( &to->low_mark,
925
sizeof(list->low_mark) ) ||
926
copy_to_user( &to->high_mark,
928
sizeof(list->high_mark) ) )
931
DRM_DEBUG( "%d %d %d %d %d\n",
933
dma->bufs[i].buf_count,
934
dma->bufs[i].buf_size,
935
dma->bufs[i].freelist.low_mark,
936
dma->bufs[i].freelist.high_mark );
941
request.count = count;
943
if ( copy_to_user( (drm_buf_info_t *)arg,
951
int DRM(markbufs)( struct inode *inode, struct file *filp,
952
unsigned int cmd, unsigned long arg )
954
drm_file_t *priv = filp->private_data;
955
drm_device_t *dev = priv->dev;
956
drm_device_dma_t *dma = dev->dma;
957
drm_buf_desc_t request;
959
drm_buf_entry_t *entry;
961
if ( !dma ) return -EINVAL;
963
if ( copy_from_user( &request,
964
(drm_buf_desc_t *)arg,
968
DRM_DEBUG( "%d, %d, %d\n",
969
request.size, request.low_mark, request.high_mark );
970
order = DRM(order)( request.size );
971
if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
972
entry = &dma->bufs[order];
974
if ( request.low_mark < 0 || request.low_mark > entry->buf_count )
976
if ( request.high_mark < 0 || request.high_mark > entry->buf_count )
979
entry->freelist.low_mark = request.low_mark;
980
entry->freelist.high_mark = request.high_mark;
985
int DRM(freebufs)( struct inode *inode, struct file *filp,
986
unsigned int cmd, unsigned long arg )
988
drm_file_t *priv = filp->private_data;
989
drm_device_t *dev = priv->dev;
990
drm_device_dma_t *dma = dev->dma;
991
drm_buf_free_t request;
996
if ( !dma ) return -EINVAL;
998
if ( copy_from_user( &request,
999
(drm_buf_free_t *)arg,
1003
DRM_DEBUG( "%d\n", request.count );
1004
for ( i = 0 ; i < request.count ; i++ ) {
1005
if ( copy_from_user( &idx,
1009
if ( idx < 0 || idx >= dma->buf_count ) {
1010
DRM_ERROR( "Index %d (of %d max)\n",
1011
idx, dma->buf_count - 1 );
1014
buf = dma->buflist[idx];
1015
if ( buf->pid != current->pid ) {
1016
DRM_ERROR( "Process %d freeing buffer owned by %d\n",
1017
current->pid, buf->pid );
1020
DRM(free_buffer)( dev, buf );
1026
int DRM(mapbufs)( struct inode *inode, struct file *filp,
1027
unsigned int cmd, unsigned long arg )
1029
drm_file_t *priv = filp->private_data;
1030
drm_device_t *dev = priv->dev;
1031
drm_device_dma_t *dma = dev->dma;
1034
unsigned long virtual;
1035
unsigned long address;
1036
drm_buf_map_t request;
1039
if ( !dma ) return -EINVAL;
1041
spin_lock( &dev->count_lock );
1042
if ( atomic_read( &dev->buf_alloc ) ) {
1043
spin_unlock( &dev->count_lock );
1046
dev->buf_use++; /* Can't allocate more after this call */
1047
spin_unlock( &dev->count_lock );
1049
if ( copy_from_user( &request, (drm_buf_map_t *)arg,
1053
if ( request.count >= dma->buf_count ) {
1054
if ( (__HAVE_AGP && (dma->flags & _DRM_DMA_USE_AGP)) ||
1055
(__HAVE_SG && (dma->flags & _DRM_DMA_USE_SG)) ) {
1056
drm_map_t *map = DRIVER_AGP_BUFFERS_MAP( dev );
1063
#if LINUX_VERSION_CODE <= 0x020402
1064
down( ¤t->mm->mmap_sem );
1066
down_write( ¤t->mm->mmap_sem );
1068
virtual = do_mmap( filp, 0, map->size,
1069
PROT_READ | PROT_WRITE,
1071
(unsigned long)map->offset );
1072
#if LINUX_VERSION_CODE <= 0x020402
1073
up( ¤t->mm->mmap_sem );
1075
up_write( ¤t->mm->mmap_sem );
1078
#if LINUX_VERSION_CODE <= 0x020402
1079
down( ¤t->mm->mmap_sem );
1081
down_write( ¤t->mm->mmap_sem );
1083
virtual = do_mmap( filp, 0, dma->byte_count,
1084
PROT_READ | PROT_WRITE,
1086
#if LINUX_VERSION_CODE <= 0x020402
1087
up( ¤t->mm->mmap_sem );
1089
up_write( ¤t->mm->mmap_sem );
1092
if ( virtual > -1024UL ) {
1094
retcode = (signed long)virtual;
1097
request.virtual = (void *)virtual;
1099
for ( i = 0 ; i < dma->buf_count ; i++ ) {
1100
if ( copy_to_user( &request.list[i].idx,
1101
&dma->buflist[i]->idx,
1102
sizeof(request.list[0].idx) ) ) {
1106
if ( copy_to_user( &request.list[i].total,
1107
&dma->buflist[i]->total,
1108
sizeof(request.list[0].total) ) ) {
1112
if ( copy_to_user( &request.list[i].used,
1118
address = virtual + dma->buflist[i]->offset; /* *** */
1119
if ( copy_to_user( &request.list[i].address,
1121
sizeof(address) ) ) {
1128
request.count = dma->buf_count;
1129
DRM_DEBUG( "%d buffers, retcode = %d\n", request.count, retcode );
1131
if ( copy_to_user( (drm_buf_map_t *)arg, &request, sizeof(request) ) )
1137
#endif /* __HAVE_DMA */