38 #include <linux/slab.h>
41 #define I810_BUF_FREE 2
42 #define I810_BUF_CLIENT 1
43 #define I810_BUF_HARDWARE 0
45 #define I810_BUF_UNMAPPED 0
46 #define I810_BUF_MAPPED 1
48 static struct drm_buf *i810_freelist_get(
struct drm_device *
dev)
50 struct drm_device_dma *
dma = dev->dma;
56 for (i = 0; i < dma->buf_count; i++) {
57 struct drm_buf *
buf = dma->buflist[
i];
80 DRM_ERROR(
"Freeing buffer thats not in use : %d\n", buf->idx);
95 dev = priv->minor->dev;
96 dev_priv = dev->dev_private;
98 buf_priv = buf->dev_private;
100 vma->
vm_flags |= (VM_IO | VM_DONTCOPY);
115 .mmap = i810_mmap_buffers,
123 static int i810_map_buffer(
struct drm_buf *buf,
struct drm_file *file_priv)
125 struct drm_device *dev = file_priv->minor->dev;
135 old_fops = file_priv->filp->f_op;
136 file_priv->filp->f_op = &i810_buffer_fops;
138 buf_priv->
virtual = (
void *)
vm_mmap(file_priv->filp, 0, buf->total,
142 file_priv->filp->f_op = old_fops;
143 if (IS_ERR(buf_priv->
virtual)) {
145 DRM_ERROR(
"mmap error\n");
146 retcode = PTR_ERR(buf_priv->
virtual);
153 static int i810_unmap_buffer(
struct drm_buf *buf)
162 (
size_t) buf->total);
171 struct drm_file *file_priv)
177 buf = i810_freelist_get(dev);
180 DRM_DEBUG(
"retcode=%d\n", retcode);
184 retcode = i810_map_buffer(buf, file_priv);
186 i810_freelist_put(dev, buf);
187 DRM_ERROR(
"mapbuf failed, retcode %d\n", retcode);
190 buf->file_priv = file_priv;
191 buf_priv = buf->dev_private;
200 static int i810_dma_cleanup(
struct drm_device *dev)
202 struct drm_device_dma *
dma = dev->dma;
208 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
211 if (dev->dev_private) {
223 kfree(dev->dev_private);
224 dev->dev_private =
NULL;
226 for (i = 0; i < dma->buf_count; i++) {
227 struct drm_buf *buf = dma->buflist[
i];
237 static int i810_wait_ring(
struct drm_device *dev,
int n)
246 while (ring->
space < n) {
252 if (ring->
head != last_head) {
254 last_head = ring->
head;
259 DRM_ERROR(
"space: %d wanted %d\n", ring->
space, n);
260 DRM_ERROR(
"lockup\n");
270 static void i810_kernel_lost_context(
struct drm_device *dev)
284 struct drm_device_dma *dma = dev->dma;
289 if (dma->buf_count > 1019) {
294 for (i = 0; i < dma->buf_count; i++) {
295 struct drm_buf *buf = dma->buflist[
i];
298 buf_priv->
in_use = hw_status++;
304 buf_priv->
map.offset = buf->bus_address;
305 buf_priv->
map.size = buf->total;
307 buf_priv->
map.flags = 0;
308 buf_priv->
map.mtrr = 0;
317 static int i810_dma_initialize(
struct drm_device *dev,
321 struct drm_map_list *r_list;
333 dev->dev_private = (
void *)dev_priv;
334 i810_dma_cleanup(dev);
335 DRM_ERROR(
"can not find sarea!\n");
340 dev->dev_private = (
void *)dev_priv;
341 i810_dma_cleanup(dev);
342 DRM_ERROR(
"can not find mmio map!\n");
346 dev->agp_buffer_map = drm_core_findmap(dev, init->
buffers_offset);
347 if (!dev->agp_buffer_map) {
348 dev->dev_private = (
void *)dev_priv;
349 i810_dma_cleanup(dev);
350 DRM_ERROR(
"can not find dma buffer map!\n");
370 dev->dev_private = (
void *)dev_priv;
371 i810_dma_cleanup(dev);
372 DRM_ERROR(
"can not ioremap virtual address for"
381 dev_priv->
w = init->
w;
382 dev_priv->
h = init->
h;
400 dev->dev_private = (
void *)dev_priv;
401 i810_dma_cleanup(dev);
402 DRM_ERROR(
"Can not allocate hardware status page\n");
409 DRM_DEBUG(
"Enabled hardware status page\n");
412 if (i810_freelist_init(dev, dev_priv) != 0) {
413 dev->dev_private = (
void *)dev_priv;
414 i810_dma_cleanup(dev);
415 DRM_ERROR(
"Not enough space in the status page for"
419 dev->dev_private = (
void *)dev_priv;
425 struct drm_file *file_priv)
431 switch (init->
func) {
433 DRM_INFO(
"Using v1.4 init.\n");
435 if (dev_priv ==
NULL)
437 retcode = i810_dma_initialize(dev, dev_priv, init);
441 DRM_INFO(
"DMA Cleanup\n");
442 retcode = i810_dma_cleanup(dev);
457 static void i810EmitContextVerified(
struct drm_device *dev,
458 volatile unsigned int *
code)
476 if ((tmp & (7 << 29)) == (3 << 29) &&
477 (tmp & (0x1f << 24)) < (0x1d << 24)) {
481 printk(
"constext state dropped!!!\n");
490 static void i810EmitTexVerified(
struct drm_device *dev,
volatile unsigned int *code)
507 if ((tmp & (7 << 29)) == (3 << 29) &&
508 (tmp & (0x1f << 24)) < (0x1d << 24)) {
512 printk(
"texture state dropped!!!\n");
523 static void i810EmitDestVerified(
struct drm_device *dev,
524 volatile unsigned int *code)
537 DRM_DEBUG(
"bad di1 %x (allow %x or %x)\n",
558 static void i810EmitState(
struct drm_device *dev)
564 DRM_DEBUG(
"%x\n", dirty);
567 i810EmitDestVerified(dev, sarea_priv->
BufferState);
568 sarea_priv->
dirty &= ~I810_UPLOAD_BUFFERS;
573 sarea_priv->
dirty &= ~I810_UPLOAD_CTX;
577 i810EmitTexVerified(dev, sarea_priv->
TexState[0]);
578 sarea_priv->
dirty &= ~I810_UPLOAD_TEX0;
582 i810EmitTexVerified(dev, sarea_priv->
TexState[1]);
583 sarea_priv->
dirty &= ~I810_UPLOAD_TEX1;
590 unsigned int clear_color,
591 unsigned int clear_zval)
595 int nbox = sarea_priv->
nbox;
597 int pitch = dev_priv->
pitch;
603 unsigned int tmp =
flags;
612 i810_kernel_lost_context(dev);
617 for (i = 0; i < nbox; i++, pbox++) {
618 unsigned int x = pbox->
x1;
619 unsigned int y = pbox->
y1;
620 unsigned int width = (pbox->
x2 -
x) * cpp;
622 unsigned int start = y * pitch + x *
cpp;
624 if (pbox->
x1 > pbox->
x2 ||
625 pbox->
y1 > pbox->
y2 ||
626 pbox->
x2 > dev_priv->
w || pbox->
y2 > dev_priv->
h)
629 if (flags & I810_FRONT) {
664 static void i810_dma_dispatch_swap(
struct drm_device *dev)
668 int nbox = sarea_priv->
nbox;
670 int pitch = dev_priv->
pitch;
675 DRM_DEBUG(
"swapbuffers\n");
677 i810_kernel_lost_context(dev);
682 for (i = 0; i < nbox; i++, pbox++) {
683 unsigned int w = pbox->
x2 - pbox->
x1;
684 unsigned int h = pbox->
y2 - pbox->
y1;
685 unsigned int dst = pbox->
x1 * cpp + pbox->
y1 * pitch;
686 unsigned int start =
dst;
688 if (pbox->
x1 > pbox->
x2 ||
689 pbox->
y1 > pbox->
y2 ||
690 pbox->
x2 > dev_priv->
w || pbox->
y2 > dev_priv->
h)
710 static void i810_dma_dispatch_vertex(
struct drm_device *dev,
711 struct drm_buf *buf,
int discard,
int used)
717 int nbox = sarea_priv->
nbox;
718 unsigned long address = (
unsigned long)buf->bus_address;
719 unsigned long start = address - dev->agp->base;
723 i810_kernel_lost_context(dev);
731 if (sarea_priv->
dirty)
745 i810_unmap_buffer(buf);
757 1) | ((box[i].
y2 - 1) << 16));
768 }
while (++i < nbox);
790 static void i810_dma_dispatch_flip(
struct drm_device *dev)
793 int pitch = dev_priv->
pitch;
796 DRM_DEBUG(
"page=%d pfCurrentPage=%d\n",
800 i810_kernel_lost_context(dev);
836 static void i810_dma_quiescent(
struct drm_device *dev)
841 i810_kernel_lost_context(dev);
850 i810_wait_ring(dev, dev_priv->
ring.
Size - 8);
853 static int i810_flush_queue(
struct drm_device *dev)
856 struct drm_device_dma *dma = dev->dma;
860 i810_kernel_lost_context(dev);
867 i810_wait_ring(dev, dev_priv->
ring.
Size - 8);
869 for (i = 0; i < dma->buf_count; i++) {
870 struct drm_buf *buf = dma->buflist[
i];
877 DRM_DEBUG(
"reclaimed from HARDWARE\n");
879 DRM_DEBUG(
"still on client\n");
887 struct drm_file *file_priv)
889 struct drm_device_dma *dma = dev->dma;
894 if (!dev->dev_private)
899 i810_flush_queue(dev);
901 for (i = 0; i < dma->buf_count; i++) {
902 struct drm_buf *buf = dma->buflist[
i];
905 if (buf->file_priv == file_priv && buf_priv) {
910 DRM_DEBUG(
"reclaimed from client\n");
917 static int i810_flush_ioctl(
struct drm_device *dev,
void *data,
918 struct drm_file *file_priv)
920 LOCK_TEST_WITH_RETURN(dev, file_priv);
922 i810_flush_queue(dev);
926 static int i810_dma_vertex(
struct drm_device *dev,
void *data,
927 struct drm_file *file_priv)
929 struct drm_device_dma *dma = dev->dma;
936 LOCK_TEST_WITH_RETURN(dev, file_priv);
938 DRM_DEBUG(
"idx %d used %d discard %d\n",
941 if (vertex->
idx < 0 || vertex->
idx > dma->buf_count)
944 i810_dma_dispatch_vertex(dev,
945 dma->buflist[vertex->
idx],
956 static int i810_clear_bufs(
struct drm_device *dev,
void *data,
957 struct drm_file *file_priv)
961 LOCK_TEST_WITH_RETURN(dev, file_priv);
964 if (!dev->dev_private)
967 i810_dma_dispatch_clear(dev, clear->
flags,
972 static int i810_swap_bufs(
struct drm_device *dev,
void *data,
973 struct drm_file *file_priv)
977 LOCK_TEST_WITH_RETURN(dev, file_priv);
979 i810_dma_dispatch_swap(dev);
983 static int i810_getage(
struct drm_device *dev,
void *data,
984 struct drm_file *file_priv)
995 static int i810_getbuf(
struct drm_device *dev,
void *data,
996 struct drm_file *file_priv)
1005 LOCK_TEST_WITH_RETURN(dev, file_priv);
1009 retcode = i810_dma_get_buffer(dev, d, file_priv);
1011 DRM_DEBUG(
"i810_dma: %d returning %d, granted = %d\n",
1019 static int i810_copybuf(
struct drm_device *dev,
void *data,
1020 struct drm_file *file_priv)
1026 static int i810_docopy(
struct drm_device *dev,
void *data,
1027 struct drm_file *file_priv)
1033 static void i810_dma_dispatch_mc(
struct drm_device *dev,
struct drm_buf *buf,
int used,
1034 unsigned int last_render)
1039 unsigned long address = (
unsigned long)buf->bus_address;
1040 unsigned long start = address - dev->agp->base;
1044 i810_kernel_lost_context(dev);
1048 DRM_DEBUG(
"MC found buffer that isn't mine!\n");
1050 if (used > 4 * 1024)
1053 sarea_priv->
dirty = 0x7f;
1055 DRM_DEBUG(
"addr 0x%lx, used 0x%x\n", address, used);
1058 DRM_DEBUG(
"dispatch counter : %ld\n", dev_priv->
counter);
1059 DRM_DEBUG(
"start : %lx\n", start);
1060 DRM_DEBUG(
"used : %d\n", used);
1061 DRM_DEBUG(
"start + used - 4 : %ld\n", start + used - 4);
1069 i810_unmap_buffer(buf);
1091 static int i810_dma_mc(
struct drm_device *dev,
void *data,
1092 struct drm_file *file_priv)
1094 struct drm_device_dma *dma = dev->dma;
1101 LOCK_TEST_WITH_RETURN(dev, file_priv);
1103 if (mc->
idx >= dma->buf_count || mc->
idx < 0)
1106 i810_dma_dispatch_mc(dev, dma->buflist[mc->
idx], mc->
used,
1117 static int i810_rstatus(
struct drm_device *dev,
void *data,
1118 struct drm_file *file_priv)
1125 static int i810_ov0_info(
struct drm_device *dev,
void *data,
1126 struct drm_file *file_priv)
1137 static int i810_fstatus(
struct drm_device *dev,
void *data,
1138 struct drm_file *file_priv)
1142 LOCK_TEST_WITH_RETURN(dev, file_priv);
1146 static int i810_ov0_flip(
struct drm_device *dev,
void *data,
1147 struct drm_file *file_priv)
1151 LOCK_TEST_WITH_RETURN(dev, file_priv);
1161 static void i810_do_init_pageflip(
struct drm_device *dev)
1171 static int i810_do_cleanup_pageflip(
struct drm_device *dev)
1177 i810_dma_dispatch_flip(dev);
1183 static int i810_flip_bufs(
struct drm_device *dev,
void *data,
1184 struct drm_file *file_priv)
1190 LOCK_TEST_WITH_RETURN(dev, file_priv);
1193 i810_do_init_pageflip(dev);
1195 i810_dma_dispatch_flip(dev);
1215 i810_dma_cleanup(dev);
1220 if (dev->dev_private) {
1223 i810_do_cleanup_pageflip(dev);
1226 if (file_priv->master && file_priv->master->lock.hw_lock) {
1240 i810_dma_quiescent(dev);
1245 DRM_IOCTL_DEF_DRV(I810_INIT, i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1246 DRM_IOCTL_DEF_DRV(I810_VERTEX, i810_dma_vertex, DRM_AUTH|DRM_UNLOCKED),
1247 DRM_IOCTL_DEF_DRV(I810_CLEAR, i810_clear_bufs, DRM_AUTH|DRM_UNLOCKED),
1248 DRM_IOCTL_DEF_DRV(I810_FLUSH, i810_flush_ioctl, DRM_AUTH|DRM_UNLOCKED),
1249 DRM_IOCTL_DEF_DRV(I810_GETAGE, i810_getage, DRM_AUTH|DRM_UNLOCKED),
1250 DRM_IOCTL_DEF_DRV(I810_GETBUF, i810_getbuf, DRM_AUTH|DRM_UNLOCKED),
1251 DRM_IOCTL_DEF_DRV(I810_SWAP, i810_swap_bufs, DRM_AUTH|DRM_UNLOCKED),
1252 DRM_IOCTL_DEF_DRV(I810_COPY, i810_copybuf, DRM_AUTH|DRM_UNLOCKED),
1253 DRM_IOCTL_DEF_DRV(I810_DOCOPY, i810_docopy, DRM_AUTH|DRM_UNLOCKED),
1254 DRM_IOCTL_DEF_DRV(I810_OV0INFO, i810_ov0_info, DRM_AUTH|DRM_UNLOCKED),
1255 DRM_IOCTL_DEF_DRV(I810_FSTATUS, i810_fstatus, DRM_AUTH|DRM_UNLOCKED),
1256 DRM_IOCTL_DEF_DRV(I810_OV0FLIP, i810_ov0_flip, DRM_AUTH|DRM_UNLOCKED),
1257 DRM_IOCTL_DEF_DRV(I810_MC, i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1258 DRM_IOCTL_DEF_DRV(I810_RSTATUS, i810_rstatus, DRM_AUTH|DRM_UNLOCKED),
1259 DRM_IOCTL_DEF_DRV(I810_FLIP, i810_flip_bufs, DRM_AUTH|DRM_UNLOCKED),