aboutsummaryrefslogtreecommitdiffhomepage
path: root/drivers/tdfx_vid.c
blob: 501f5fc60339c8a7f78dd2b4301b0219d476e7fe (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
198
199
200
201
202
203
204
205
206
207
208
209
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
/*
 * Copyright (C) 2003 Alban Bedel
 *
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <linux/config.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/errno.h>

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,10)
#include <linux/malloc.h>
#else
#include <linux/slab.h>
#endif

#include <linux/pci.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/agp_backend.h>

#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm/io.h>

#include "tdfx_vid.h"
#include "3dfx.h"


#define TDFX_VID_MAJOR 178

MODULE_AUTHOR("Albeu");
MODULE_DESCRIPTION("A driver for Banshee targeted for video app");

#ifdef MODULE_LICENSE
MODULE_LICENSE("GPL");
#endif

#ifndef min
#define min(x,y) (((x)<(y))?(x):(y))
#endif

static struct pci_dev *pci_dev;

static uint8_t *tdfx_mmio_base = 0;
static uint32_t tdfx_mem_base = 0;
static uint32_t tdfx_io_base = 0;

static int tdfx_ram_size = 0;

static int tdfx_vid_in_use = 0;

static drm_agp_t *drm_agp = NULL;
static agp_kern_info agp_info;
static agp_memory *agp_mem = NULL;

static __initdata int tdfx_map_io = 1;
static __initdata unsigned long map_start = 0; //0x7300000;
static __initdata unsigned long map_max = (10*1024*1024);

MODULE_PARM(tdfx_map_io,"i");
MODULE_PARM_DESC(tdfx_map_io, "Set to 0 to use the page fault handler (you need to patch agpgart_be.c to allow the mapping in user space)\n");
MODULE_PARM(map_start,"l");
MODULE_PARM_DESC(map_start,"Use a block of physical mem instead of the agp arerture.");
MODULE_PARM(map_max,"l");
MODULE_PARM_DESC(map_max, "Maximum amout of physical memory (in bytes) that can be used\n");

static inline u32 tdfx_inl(unsigned int reg) {
  return readl(tdfx_mmio_base + reg);
}

static inline void tdfx_outl(unsigned int reg, u32 val) {
  writel(val,tdfx_mmio_base  + reg);
}

static inline void banshee_make_room(int size) {
  while((tdfx_inl(STATUS) & 0x1f) < size);
}

static inline void banshee_wait_idle(void) {
  int i = 0;

  banshee_make_room(1);
  tdfx_outl(COMMAND_3D, COMMAND_3D_NOP);

  while(1) {
    i = (tdfx_inl(STATUS) & STATUS_BUSY) ? 0 : i + 1;
    if(i == 3) break;
  }
}

static unsigned long get_lfb_size(void) {
  u32 draminit0 = 0;
  u32 draminit1 = 0;
  //  u32 miscinit1 = 0;
  u32 lfbsize   = 0;
  int sgram_p     = 0;

  draminit0 = tdfx_inl(DRAMINIT0);
  draminit1 = tdfx_inl(DRAMINIT1);

  if ((pci_dev->device == PCI_DEVICE_ID_3DFX_BANSHEE) ||
      (pci_dev->device == PCI_DEVICE_ID_3DFX_VOODOO3)) {
    sgram_p = (draminit1 & DRAMINIT1_MEM_SDRAM) ? 0 : 1;

    lfbsize = sgram_p ?
      (((draminit0 & DRAMINIT0_SGRAM_NUM)  ? 2 : 1) *
       ((draminit0 & DRAMINIT0_SGRAM_TYPE) ? 8 : 4) * 1024 * 1024) :
      16 * 1024 * 1024;
  } else {
    /* Voodoo4/5 */
    u32 chips, psize, banks;

    chips = ((draminit0 & (1 << 26)) == 0) ? 4 : 8;
    psize = 1 << ((draminit0 & 0x38000000) >> 28);
    banks = ((draminit0 & (1 << 30)) == 0) ? 2 : 4;
    lfbsize = chips * psize * banks;
    lfbsize <<= 20;
  }

#if 0
  /* disable block writes for SDRAM (why?) */
  miscinit1 = tdfx_inl(MISCINIT1);
  miscinit1 |= sgram_p ? 0 : MISCINIT1_2DBLOCK_DIS;
  miscinit1 |= MISCINIT1_CLUT_INV;

  banshee_make_room(1);
  tdfx_outl(MISCINIT1, miscinit1);
#endif

  return lfbsize;
}

static int tdfx_vid_find_card(void)
{
  struct pci_dev *dev = NULL;
  //  unsigned int card_option;

  if((dev = pci_find_device(PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_BANSHEE, NULL)))
    printk(KERN_INFO "tdfx_vid: Found VOODOO BANSHEE\n");
  else if((dev = pci_find_device(PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO3, NULL)))
    printk(KERN_INFO "tdfx_vid: Found VOODOO 3 \n");
  else
    return 0;


  pci_dev = dev;

#if LINUX_VERSION_CODE >= 0x020300
  tdfx_mmio_base = ioremap_nocache(dev->resource[0].start,1 << 24);
  tdfx_mem_base =  dev->resource[1].start;
  tdfx_io_base = dev->resource[2].start;
#else
  tdfx_mmio_base = ioremap_nocache(dev->base_address[1] & PCI_BASE_ADDRESS_MEM_MASK,0x4000);
  tdfx_mem_base =  dev->base_address[1] & PCI_BASE_ADDRESS_MEM_MASK;
  tdfx_io_base = dev->base_address[2] & PCI_BASE_ADDRESS_MEM_MASK;
#endif
  printk(KERN_INFO "tdfx_vid: MMIO at 0x%p\n", tdfx_mmio_base);
  tdfx_ram_size = get_lfb_size();

  printk(KERN_INFO "tdfx_vid: Found %d MB (%d bytes) of memory\n",
	 tdfx_ram_size / 1024 / 1024,tdfx_ram_size);


#if 0
  {
    int temp;
    printk("List resources -----------\n");
    for(temp=0;temp<DEVICE_COUNT_RESOURCE;temp++){
      struct resource *res=&pci_dev->resource[temp];
      if(res->flags){
	int size=(1+res->end-res->start)>>20;
	printk(KERN_DEBUG "res %d:  start: 0x%X   end: 0x%X  (%d MB) flags=0x%X\n",temp,res->start,res->end,size,res->flags);
	if(res->flags&(IORESOURCE_MEM|IORESOURCE_PREFETCH)){
	  if(size>tdfx_ram_size && size<=64) tdfx_ram_size=size;
	}
      }
    }
  }
#endif


  return 1;
}

static int agp_init(void) {

  drm_agp = (drm_agp_t*)inter_module_get("drm_agp");

  if(!drm_agp) {
    printk(KERN_ERR "tdfx_vid: Unable to get drm_agp pointer\n");
    return 0;
  }

  if(drm_agp->acquire()) {
    printk(KERN_ERR "tdfx_vid: Unable to acquire the agp backend\n");
    drm_agp = NULL;
    return 0;
  }

  drm_agp->copy_info(&agp_info);
#if 0
  printk(KERN_DEBUG "AGP Version : %d %d\n"
	 "AGP Mode: %#X\nAperture Base: %p\nAperture Size: %d\n"
	 "Max memory = %d\nCurrent mem = %d\nCan use perture : %s\n"
	 "Page mask = %#X\n",
	 agp_info.version.major,agp_info.version.minor,
	 agp_info.mode,agp_info.aper_base,agp_info.aper_size,
	 agp_info.max_memory,agp_info.current_memory,
	 agp_info.cant_use_aperture ? "no" : "yes",
	 agp_info.page_mask);
#endif
  drm_agp->enable(agp_info.mode);


  printk(KERN_INFO "AGP Enabled\n");

  return 1;
}

static void agp_close(void) {

  if(!drm_agp) return;

  if(agp_mem) {
    drm_agp->unbind_memory(agp_mem);
    drm_agp->free_memory(agp_mem);
    agp_mem = NULL;
  }


  drm_agp->release();
  inter_module_put("drm_agp");
}

static int agp_move(tdfx_vid_agp_move_t* m) {
  u32 src = 0;
  u32 src_h,src_l;

  if(!(agp_mem||map_start))
    return -EAGAIN;

  if(m->move2 > 3) {
    printk(KERN_DEBUG "tdfx_vid: AGP move invalid destination %d\n",
	   m->move2);
    return -EAGAIN;
  }

  if(map_start)
    src =  map_start + m->src;
  else
    src =  agp_info.aper_base +  m->src;

  src_l = (u32)src;
  src_h = (m->width | (m->src_stride << 14)) & 0x0FFFFFFF;

  //  banshee_wait_idle();
  banshee_make_room(6);
  tdfx_outl(AGPHOSTADDRESSHIGH,src_h);
  tdfx_outl(AGPHOSTADDRESSLOW,src_l);

  tdfx_outl(AGPGRAPHICSADDRESS, m->dst);
  tdfx_outl(AGPGRAPHICSSTRIDE, m->dst_stride);
  tdfx_outl(AGPREQSIZE,m->src_stride*m->height);

  tdfx_outl(AGPMOVECMD,m->move2 << 3);
  banshee_wait_idle();

  return 0;
}

#if 0
static void setup_fifo(u32 offset,ssize_t pages) {
  long addr = agp_info.aper_base + offset;
  u32 size = pages | 0x700; // fifo on, in agp mem, disable hole cnt

  banshee_wait_idle();

  tdfx_outl(CMDBASEADDR0,addr >> 4);
  tdfx_outl(CMDRDPTRL0, addr << 4);
  tdfx_outl(CMDRDPTRH0, addr >> 28);
  tdfx_outl(CMDAMIN0, (addr - 4) & 0xFFFFFF);
  tdfx_outl(CMDAMAX0, (addr - 4) & 0xFFFFFF);
  tdfx_outl(CMDFIFODEPTH0, 0);
  tdfx_outl(CMDHOLECNT0, 0);
  tdfx_outl(CMDBASESIZE0,size);

  banshee_wait_idle();

}
#endif

static int bump_fifo(u16 size) {

  banshee_wait_idle();
  tdfx_outl(CMDBUMP0 , size);
  banshee_wait_idle();

  return 0;
}

static void tdfx_vid_get_config(tdfx_vid_config_t* cfg) {
  u32 in;

  cfg->version = TDFX_VID_VERSION;
  cfg->ram_size = tdfx_ram_size;

  in = tdfx_inl(VIDSCREENSIZE);
  cfg->screen_width = in & 0xFFF;
  cfg->screen_height = (in >> 12) & 0xFFF;
  in = (tdfx_inl(VIDPROCCFG)>> 18)& 0x7;
  switch(in) {
  case 0:
    cfg->screen_format = TDFX_VID_FORMAT_BGR8;
    break;
  case 1:
    cfg->screen_format = TDFX_VID_FORMAT_BGR16;
    break;
  case 2:
    cfg->screen_format = TDFX_VID_FORMAT_BGR24;
    break;
  case 3:
    cfg->screen_format = TDFX_VID_FORMAT_BGR32;
    break;
  default:
    printk(KERN_INFO "tdfx_vid: unknown screen format %d\n",in);
    cfg->screen_format = 0;
    break;
  }
  cfg->screen_stride = tdfx_inl(VIDDESKSTRIDE) & 0x7FFF;
  cfg->screen_start = tdfx_inl(VIDDESKSTART);
}

inline static u32 tdfx_vid_make_format(int src,u16 stride,u32 fmt) {
  u32 r = stride & 0xFFF3;
  u32 tdfx_fmt = 0;

  // src and dest formats
  switch(fmt) {
  case TDFX_VID_FORMAT_BGR8:
    tdfx_fmt = 1;
    break;
  case TDFX_VID_FORMAT_BGR16:
    tdfx_fmt = 3;
    break;
  case TDFX_VID_FORMAT_BGR24:
    tdfx_fmt = 4;
    break;
  case TDFX_VID_FORMAT_BGR32:
    tdfx_fmt = 5;
    break;
  }

  if(!src && !tdfx_fmt) {
    printk(KERN_INFO "tdfx_vid: Invalid destination format %#X\n",fmt);
    return 0;
  }

  if(src && !tdfx_fmt) {
    // src only format
    switch(fmt){
    case TDFX_VID_FORMAT_BGR1:
      tdfx_fmt = 0;
      break;
    case TDFX_VID_FORMAT_BGR15: // To check
      tdfx_fmt = 2;
      break;
    case TDFX_VID_FORMAT_YUY2:
      tdfx_fmt = 8;
      break;
    case TDFX_VID_FORMAT_UYVY:
      tdfx_fmt = 9;
      break;
    default:
      printk(KERN_INFO "tdfx_vid: Invalid source format %#X\n",fmt);
      return 0;
    }
  }

  r |= tdfx_fmt << 16;

  return r;
}

static int tdfx_vid_blit(tdfx_vid_blit_t* blit) {
  u32 src_fmt,dst_fmt,cmd = 2;
  u32 cmin,cmax,srcbase,srcxy,srcfmt,srcsize;
  u32 dstbase,dstxy,dstfmt,dstsize = 0;
  u32 cmd_extra = 0,src_ck[2],dst_ck[2],rop123=0;

  //printk(KERN_INFO "tdfx_vid: Make src fmt 0x%x\n",blit->src_format);
  src_fmt = tdfx_vid_make_format(1,blit->src_stride,blit->src_format);
  if(!src_fmt)
    return 0;
  //printk(KERN_INFO "tdfx_vid: Make dst fmt 0x%x\n", blit->dst_format);
  dst_fmt = tdfx_vid_make_format(0,blit->dst_stride,blit->dst_format);
  if(!dst_fmt)
    return 0;
  blit->colorkey &= 0x3;
  // Be nice if user just want a simple blit
  if((!blit->colorkey) && (!blit->rop[0]))
    blit->rop[0] = TDFX_VID_ROP_COPY;
  // No stretch : fix me the cmd should be 1 but it
  // doesn't work. Maybe some other regs need to be set
  // as non-stretch blit have more options
  if(((!blit->dst_w) && (!blit->dst_h)) ||
     ((blit->dst_w == blit->src_w) && (blit->dst_h == blit->src_h)))
    cmd = 2;

  // Save the regs otherwise fb get crazy
  // we can perhaps avoid some ...
  banshee_wait_idle();
  cmin = tdfx_inl(CLIP0MIN);
  cmax = tdfx_inl(CLIP0MAX);
  srcbase = tdfx_inl(SRCBASE);
  srcxy = tdfx_inl(SRCXY);
  srcfmt = tdfx_inl(SRCFORMAT);
  srcsize = tdfx_inl(SRCSIZE);
  dstbase = tdfx_inl(DSTBASE);
  dstxy = tdfx_inl(DSTXY);
  dstfmt = tdfx_inl(DSTFORMAT);
  if(cmd == 2)
    dstsize = tdfx_inl(DSTSIZE);
  if(blit->colorkey & TDFX_VID_SRC_COLORKEY) {
    src_ck[0] = tdfx_inl(SRCCOLORKEYMIN);
    src_ck[1] = tdfx_inl(SRCCOLORKEYMAX);
    tdfx_outl(SRCCOLORKEYMIN,blit->src_colorkey[0]);
    tdfx_outl(SRCCOLORKEYMAX,blit->src_colorkey[1]);
  }
  if(blit->colorkey & TDFX_VID_DST_COLORKEY) {
    dst_ck[0] = tdfx_inl(DSTCOLORKEYMIN);
    dst_ck[1] = tdfx_inl(DSTCOLORKEYMAX);
    tdfx_outl(SRCCOLORKEYMIN,blit->dst_colorkey[0]);
    tdfx_outl(SRCCOLORKEYMAX,blit->dst_colorkey[1]);
  }
  if(blit->colorkey) {
    cmd_extra = tdfx_inl(COMMANDEXTRA_2D);
    rop123 = tdfx_inl(ROP123);
    tdfx_outl(COMMANDEXTRA_2D, blit->colorkey);
    tdfx_outl(ROP123,(blit->rop[1] | (blit->rop[2] << 8) | blit->rop[3] << 16));

  }
  // Get rid of the clipping at the moment
  tdfx_outl(CLIP0MIN,0);
  tdfx_outl(CLIP0MAX,0x0fff0fff);

  // Setup the src
  tdfx_outl(SRCBASE,blit->src & 0x00FFFFFF);
  tdfx_outl(SRCXY,XYREG(blit->src_x,blit->src_y));
  tdfx_outl(SRCFORMAT,src_fmt);
  tdfx_outl(SRCSIZE,XYREG(blit->src_w,blit->src_h));

  // Setup the dst
  tdfx_outl(DSTBASE,blit->dst & 0x00FFFFFF);
  tdfx_outl(DSTXY,XYREG(blit->dst_x,blit->dst_y));
  tdfx_outl(DSTFORMAT,dst_fmt);
  if(cmd == 2)
    tdfx_outl(DSTSIZE,XYREG(blit->dst_w,blit->dst_h));

  // Send the command
  tdfx_outl(COMMAND_2D,cmd | 0x100 | (blit->rop[0] << 24));
  banshee_wait_idle();

  // Now restore the regs to make fb happy
  tdfx_outl(CLIP0MIN, cmin);
  tdfx_outl(CLIP0MAX, cmax);
  tdfx_outl(SRCBASE, srcbase);
  tdfx_outl(SRCXY, srcxy);
  tdfx_outl(SRCFORMAT, srcfmt);
  tdfx_outl(SRCSIZE, srcsize);
  tdfx_outl(DSTBASE, dstbase);
  tdfx_outl(DSTXY, dstxy);
  tdfx_outl(DSTFORMAT, dstfmt);
  if(cmd == 2)
    tdfx_outl(DSTSIZE, dstsize);
  if(blit->colorkey & TDFX_VID_SRC_COLORKEY) {
    tdfx_outl(SRCCOLORKEYMIN,src_ck[0]);
    tdfx_outl(SRCCOLORKEYMAX,src_ck[1]);
  }
  if(blit->colorkey & TDFX_VID_DST_COLORKEY) {
    tdfx_outl(SRCCOLORKEYMIN,dst_ck[0]);
    tdfx_outl(SRCCOLORKEYMAX,dst_ck[1]);
  }
  if(blit->colorkey) {
    tdfx_outl(COMMANDEXTRA_2D,cmd_extra);
    tdfx_outl(ROP123,rop123);
  }
  return 1;
}

static int tdfx_vid_set_yuv(unsigned long arg) {
  tdfx_vid_yuv_t yuv;

  if(copy_from_user(&yuv,(tdfx_vid_yuv_t*)arg,sizeof(tdfx_vid_yuv_t))) {
    printk(KERN_DEBUG "tdfx_vid:failed copy from userspace\n");
    return -EFAULT;
  }
  banshee_make_room(2);
  tdfx_outl(YUVBASEADDRESS,yuv.base & 0x01FFFFFF);
  tdfx_outl(YUVSTRIDE, yuv.stride & 0x3FFF);

  banshee_wait_idle();

  return 0;
}

static int tdfx_vid_get_yuv(unsigned long arg) {
  tdfx_vid_yuv_t yuv;

  yuv.base = tdfx_inl(YUVBASEADDRESS) & 0x01FFFFFF;
  yuv.stride = tdfx_inl(YUVSTRIDE) & 0x3FFF;

  if(copy_to_user((tdfx_vid_yuv_t*)arg,&yuv,sizeof(tdfx_vid_yuv_t))) {
      printk(KERN_INFO "tdfx_vid:failed copy to userspace\n");
      return -EFAULT;
  }

  return 0;
}

static int tdfx_vid_set_overlay(unsigned long arg) {
  tdfx_vid_overlay_t ov;
  uint32_t screen_w,screen_h;
  uint32_t vidcfg,stride,vidbuf;
  int disp_w,disp_h;

  if(copy_from_user(&ov,(tdfx_vid_overlay_t*)arg,sizeof(tdfx_vid_overlay_t))) {
    printk(KERN_DEBUG "tdfx_vid:failed copy from userspace\n");
    return -EFAULT;
  }

  if(ov.dst_y < 0) {
    int shift;
    if(-ov.dst_y >= ov.src_height) {
      printk(KERN_DEBUG "tdfx_vid: Overlay outside of the screen ????\n");
      return -EFAULT;
    }
    shift = (-ov.dst_y)/(double)ov.dst_height*ov.src_height;
    ov.src[0] += shift*ov.src_stride;
    ov.src_height -= shift;
    ov.dst_height += ov.dst_y;
    ov.dst_y = 0;
  }

  if(ov.dst_x < 0) {
    int shift;
    if(-ov.dst_x >= ov.src_width) {
      printk(KERN_DEBUG "tdfx_vid: Overlay outside of the screen ????\n");
      return -EFAULT;
    }
    shift = (-ov.dst_x)/(double)ov.dst_width*ov.src_width;
    shift = ((shift+3)/2)*2;
    ov.src[0] += shift*2;
    ov.src_width -= shift;
    ov.dst_width += ov.dst_x;
    ov.dst_x = 0;
  }

  vidcfg = tdfx_inl(VIDPROCCFG);
  // clear the overlay fmt
  vidcfg &= ~(7 << 21);
  switch(ov.format) {
  case TDFX_VID_FORMAT_BGR15:
    vidcfg |= (1 << 21);
    break;
  case TDFX_VID_FORMAT_BGR16:
    vidcfg |= (7 << 21);
    break;
  case TDFX_VID_FORMAT_YUY2:
    vidcfg |= (5 << 21);
    break;
  case TDFX_VID_FORMAT_UYVY:
    vidcfg |= (6 << 21);
    break;
  default:
    printk(KERN_DEBUG "tdfx_vid: Invalid overlay fmt 0x%x\n",ov.format);
    return -EFAULT;
  }

  // YUV422 need 4 bytes aligned stride and address
  if((ov.format == TDFX_VID_FORMAT_YUY2 ||
      ov.format == TDFX_VID_FORMAT_UYVY)) {
    if((ov.src_stride & ~0x3) != ov.src_stride) {
      printk(KERN_DEBUG "tdfx_vid: YUV need a 4 bytes aligned stride %d\n",ov.src_stride);
      return -EFAULT;
    }
    if((ov.src[0] & ~0x3) != ov.src[0] || (ov.src[1] & ~0x3) != ov.src[1]){
      printk(KERN_DEBUG "tdfx_vid: YUV need a 4 bytes aligned address 0x%x 0x%x\n",ov.src[0],ov.src[1]);
      return -EFAULT;
    }
  }

  // Now we have a good input format
  // but first get the screen size to check a bit
  // if the size/position is valid
  screen_w = tdfx_inl(VIDSCREENSIZE);
  screen_h = (screen_w >> 12) & 0xFFF;
  screen_w &= 0xFFF;
  disp_w =  ov.dst_x + ov.dst_width >= screen_w ?
    screen_w - ov.dst_x : ov.dst_width;
  disp_h =  ov.dst_y + ov.dst_height >= screen_h ?
    screen_h - ov.dst_y : ov.dst_height;

  if(ov.dst_x >= screen_w || ov.dst_y >= screen_h ||
     disp_h <= 0 || disp_h > screen_h || disp_w <= 0 || disp_w > screen_w) {
    printk(KERN_DEBUG "tdfx_vid: Invalid overlay dimension and/or position\n");
    return -EFAULT;
  }
  // Setup the vidproc
  // H scaling
  if(ov.src_width < ov.dst_width)
    vidcfg |= (1<<14);
  else
    vidcfg &= ~(1<<14);
  // V scaling
  if(ov.src_height < ov.dst_height)
    vidcfg |= (1<<15);
  else
    vidcfg &= ~(1<<15);
  // Filtering can only be used in 1x mode
  if(!(vidcfg | (1<<26)))
    vidcfg |= (3<<16);
  else
    vidcfg &= ~(3<<16);
  // disable overlay stereo mode
  vidcfg &= ~(1<<2);
  // Colorkey on/off
  if(ov.use_colorkey) {
    // Colorkey inversion
    if(ov.invert_colorkey)
      vidcfg |= (1<<6);
    else
      vidcfg &= ~(1<<6);
    vidcfg |= (1<<5);
  } else
    vidcfg &= ~(1<<5);
  // Overlay isn't VidIn
  vidcfg &= ~(1<<9);
  // vidcfg |= (1<<8);
  tdfx_outl(VIDPROCCFG,vidcfg);

  // Start coord
  //printk(KERN_DEBUG "tdfx_vid: start %dx%d\n",ov.dst_x & 0xFFF,ov.dst_y & 0xFFF);
  tdfx_outl(VIDOVRSTARTCRD,(ov.dst_x & 0xFFF)|((ov.dst_y & 0xFFF)<<12));
  // End coord
  tdfx_outl(VIDOVRENDCRD, ((ov.dst_x + disp_w-1) & 0xFFF)|
	    (((ov.dst_y + disp_h-1) & 0xFFF)<<12));
  // H Scaling
  tdfx_outl(VIDOVRDUDX,( ((u32)ov.src_width) << 20) / ov.dst_width);
  // Src offset and width (in bytes)
  tdfx_outl(VIDOVRDUDXOFF,((ov.src_width<<1) & 0xFFF) << 19);
  // V Scaling
  tdfx_outl(VIDOVRDVDY, ( ((u32)ov.src_height) << 20) / ov.dst_height);
  //else
  //  tdfx_outl(VIDOVRDVDY,0);
  // V Offset
  tdfx_outl(VIDOVRDVDYOFF,0);
  // Overlay stride
  stride = tdfx_inl(VIDDESKSTRIDE) & 0xFFFF;
  tdfx_outl(VIDDESKSTRIDE,stride | (((u32)ov.src_stride) << 16));
  // Buffers address
  tdfx_outl(LEFTOVBUF, ov.src[0]);
  tdfx_outl(RIGHTOVBUF, ov.src[1]);

  // Send a swap buffer cmd if we are not on one of the 2 buffers
  vidbuf = tdfx_inl(VIDCUROVRSTART);
  if(vidbuf != ov.src[0] && vidbuf != ov.src[1]) {
    tdfx_outl(SWAPPENDING,0);
    tdfx_outl(SWAPBUFCMD, 1);
  }
  //printk(KERN_DEBUG "tdfx_vid: Buf0=0x%x Buf1=0x%x Current=0x%x\n",
  //	 ov.src[0],ov.src[1],tdfx_inl(VIDCUROVRSTART));
  // Colorkey
  if(ov.use_colorkey) {
    tdfx_outl(VIDCHRMIN,ov.colorkey[0]);
    tdfx_outl(VIDCHRMAX,ov.colorkey[1]);
  }

  return 0;
}

static int tdfx_vid_overlay_on(void) {
  uint32_t vidcfg = tdfx_inl(VIDPROCCFG);
  //return 0;
  if(vidcfg & (1<<8)) { // Overlay is already on
    //printk(KERN_DEBUG "tdfx_vid: Overlay is already on.\n");
    return -EFAULT;
  }
  vidcfg |= (1<<8);
  tdfx_outl(VIDPROCCFG,vidcfg);
  return 0;
}

static int tdfx_vid_overlay_off(void) {
  uint32_t vidcfg = tdfx_inl(VIDPROCCFG);

  if(vidcfg & (1<<8)) {
    vidcfg &= ~(1<<8);
    tdfx_outl(VIDPROCCFG,vidcfg);
    return 0;
  }

  printk(KERN_DEBUG "tdfx_vid: Overlay is already off.\n");
  return -EFAULT;
}


static int tdfx_vid_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
  tdfx_vid_agp_move_t move;
  tdfx_vid_config_t cfg;
  tdfx_vid_blit_t blit;
  u16 int16;

  switch(cmd) {
  case TDFX_VID_AGP_MOVE:
    if(copy_from_user(&move,(tdfx_vid_agp_move_t*)arg,sizeof(tdfx_vid_agp_move_t))) {
      printk(KERN_INFO "tdfx_vid:failed copy from userspace\n");
      return -EFAULT;
    }
    return agp_move(&move);
  case TDFX_VID_BUMP0:
    if(copy_from_user(&int16,(u16*)arg,sizeof(u16))) {
      printk(KERN_INFO "tdfx_vid:failed copy from userspace\n");
      return -EFAULT;
    }
    return bump_fifo(int16);
  case TDFX_VID_BLIT:
    if(copy_from_user(&blit,(tdfx_vid_blit_t*)arg,sizeof(tdfx_vid_blit_t))) {
      printk(KERN_INFO "tdfx_vid:failed copy from userspace\n");
      return -EFAULT;
    }
    if(!tdfx_vid_blit(&blit)) {
      printk(KERN_INFO "tdfx_vid: Blit failed\n");
      return -EFAULT;
    }
    return 0;
  case TDFX_VID_GET_CONFIG:
    if(copy_from_user(&cfg,(tdfx_vid_config_t*)arg,sizeof(tdfx_vid_config_t))) {
      printk(KERN_INFO "tdfx_vid:failed copy from userspace\n");
      return -EFAULT;
    }
    tdfx_vid_get_config(&cfg);
    if(copy_to_user((tdfx_vid_config_t*)arg,&cfg,sizeof(tdfx_vid_config_t))) {
      printk(KERN_INFO "tdfx_vid:failed copy to userspace\n");
      return -EFAULT;
    }
    return 0;
  case TDFX_VID_SET_YUV:
    return tdfx_vid_set_yuv(arg);
  case TDFX_VID_GET_YUV:
    return tdfx_vid_get_yuv(arg);
  case TDFX_VID_SET_OVERLAY:
    return tdfx_vid_set_overlay(arg);
  case TDFX_VID_OVERLAY_ON:
    return tdfx_vid_overlay_on();
  case TDFX_VID_OVERLAY_OFF:
    return tdfx_vid_overlay_off();
  default:
    printk(KERN_ERR "tdfx_vid: Invalid ioctl %d\n",cmd);
    return -EINVAL;
  }
  return 0;
}



static ssize_t tdfx_vid_read(struct file *file, char *buf, size_t count, loff_t *ppos)
{
	return 0;
}

static ssize_t tdfx_vid_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
{

	return 0;
}

static void tdfx_vid_mopen(struct vm_area_struct *vma) {
  int i;
  struct page *page;
  unsigned long phys;

  printk(KERN_DEBUG "tdfx_vid: mopen\n");

  for(i = 0 ; i < agp_mem->page_count ; i++) {
    phys = agp_mem->memory[i] & ~(0x00000fff);
    page = virt_to_page(phys_to_virt(phys));
    if(!page) {
      printk(KERN_DEBUG "tdfx_vid: Can't get the page %d\%d\n",i,agp_mem->page_count);
      return;
    }
    get_page(page);
  }
  MOD_INC_USE_COUNT;
}

static void tdfx_vid_mclose(struct vm_area_struct *vma)  {
  int i;
  struct page *page;
  unsigned long phys;

  printk(KERN_DEBUG "tdfx_vid: mclose\n");

  for(i = 0 ; i < agp_mem->page_count ; i++) {
    phys = agp_mem->memory[i] & ~(0x00000fff);
    page = virt_to_page(phys_to_virt(phys));
    if(!page) {
      printk(KERN_DEBUG "tdfx_vid: Can't get the page %d\%d\n",i,agp_mem->page_count);
      return;
    }
    put_page(page);
  }

  MOD_DEC_USE_COUNT;
}

static struct page *tdfx_vid_nopage(struct vm_area_struct *vma,
				    unsigned long address,
				    int write_access) {
  unsigned long off;
  uint32_t n;
  struct page *page;
  unsigned long phys;

  off = address - vma->vm_start + (vma->vm_pgoff<<PAGE_SHIFT);
  n = off / PAGE_SIZE;

  if(n >= agp_mem->page_count) {
    printk(KERN_DEBUG "tdfx_vid: Too far away\n");
    return (struct page *)0UL;
  }
  phys = agp_mem->memory[n] & ~(0x00000fff);
  page = virt_to_page(phys_to_virt(phys));
  if(!page) {
    printk(KERN_DEBUG "tdfx_vid: Can't get the page\n");
    return (struct page *)0UL;
  }
  return page;
}

/* memory handler functions */
static struct vm_operations_struct tdfx_vid_vm_ops = {
  open:   tdfx_vid_mopen, /* mmap-open */
  close:  tdfx_vid_mclose,/* mmap-close */
  nopage: tdfx_vid_nopage, /* no-page fault handler */
};


static int tdfx_vid_mmap(struct file *file, struct vm_area_struct *vma)
{
  size_t size;
#ifdef MP_DEBUG
  printk(KERN_DEBUG "tdfx_vid: mapping agp memory into userspace\n");
#endif

  size = (vma->vm_end-vma->vm_start + PAGE_SIZE - 1) / PAGE_SIZE;

  if(map_start) { // Ok we map directly in the physcal ram
    if(size*PAGE_SIZE > map_max) {
      printk(KERN_ERR "tdfx_vid: Not enouth mem\n");
      return -EAGAIN;
    }
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,3)
    if(remap_page_range(vma, vma->vm_start,map_start,
			vma->vm_end - vma->vm_start, vma->vm_page_prot))
#else
    if(remap_page_range(vma->vm_start, (unsigned long)map_start,
			vma->vm_end - vma->vm_start, vma->vm_page_prot))
#endif
      {
	printk(KERN_ERR "tdfx_vid: error mapping video memory\n");
	return -EAGAIN;
      }
    printk(KERN_INFO "Physical mem 0x%lx mapped in userspace\n",map_start);
    return 0;
  }

  if(agp_mem)
    return -EAGAIN;

  agp_mem = drm_agp->allocate_memory(size,AGP_NORMAL_MEMORY);
  if(!agp_mem) {
    printk(KERN_ERR "Failed to allocate AGP memory\n");
    return -ENOMEM;
  }

  if(drm_agp->bind_memory(agp_mem,0)) {
    printk(KERN_ERR "Failed to bind the AGP memory\n");
    drm_agp->free_memory(agp_mem);
    agp_mem = NULL;
    return -ENOMEM;
  }

  printk(KERN_INFO "%d pages of AGP mem allocated (%ld/%ld bytes) :)))\n",
	 size,vma->vm_end-vma->vm_start,size*PAGE_SIZE);


  if(tdfx_map_io) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,3)
    if(remap_page_range(vma, vma->vm_start,agp_info.aper_base,
			vma->vm_end - vma->vm_start, vma->vm_page_prot))
#else
    if(remap_page_range(vma->vm_start, (unsigned long)agp_info.aper_base,
			vma->vm_end - vma->vm_start, vma->vm_page_prot))
#endif
      {
	printk(KERN_ERR "tdfx_vid: error mapping video memory\n");
	return -EAGAIN;
      }
  } else {
    // Never swap it out
    vma->vm_flags |= VM_LOCKED | VM_IO;
    vma->vm_ops = &tdfx_vid_vm_ops;
    vma->vm_ops->open(vma);
    printk(KERN_INFO "Page fault handler ready !!!!!\n");
  }

  return 0;
}


static int tdfx_vid_release(struct inode *inode, struct file *file)
{
#ifdef MP_DEBUG
  printk(KERN_DEBUG "tdfx_vid: Video OFF (release)\n");
#endif

  // Release the agp mem
  if(agp_mem) {
    drm_agp->unbind_memory(agp_mem);
    drm_agp->free_memory(agp_mem);
    agp_mem = NULL;
  }

  tdfx_vid_in_use = 0;

  MOD_DEC_USE_COUNT;
  return 0;
}

static long long tdfx_vid_lseek(struct file *file, long long offset, int origin)
{
	return -ESPIPE;
}

static int tdfx_vid_open(struct inode *inode, struct file *file)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,2)
	int minor = MINOR(inode->i_rdev.value);
#else
	int minor = MINOR(inode->i_rdev);
#endif

	if(minor != 0)
	 return -ENXIO;

	if(tdfx_vid_in_use == 1)
		return -EBUSY;

	tdfx_vid_in_use = 1;
	MOD_INC_USE_COUNT;
	return 0;
}

#if LINUX_VERSION_CODE >= 0x020400
static struct file_operations tdfx_vid_fops =
{
  llseek:  tdfx_vid_lseek,
  read:	   tdfx_vid_read,
  write:   tdfx_vid_write,
  ioctl:   tdfx_vid_ioctl,
  mmap:	   tdfx_vid_mmap,
  open:    tdfx_vid_open,
  release: tdfx_vid_release
};
#else
static struct file_operations tdfx_vid_fops =
{
  tdfx_vid_lseek,
  tdfx_vid_read,
  tdfx_vid_write,
  NULL,
  NULL,
  tdfx_vid_ioctl,
  tdfx_vid_mmap,
  tdfx_vid_open,
  NULL,
  tdfx_vid_release
};
#endif


int init_module(void)
{
  tdfx_vid_in_use = 0;

  if(register_chrdev(TDFX_VID_MAJOR, "tdfx_vid", &tdfx_vid_fops)) {
    printk(KERN_ERR "tdfx_vid: unable to get major: %d\n", TDFX_VID_MAJOR);
    return -EIO;
  }

  if(!agp_init()) {
    printk(KERN_ERR "tdfx_vid: AGP init failed\n");
    unregister_chrdev(TDFX_VID_MAJOR, "tdfx_vid");
    return -EINVAL;
  }

  if (!tdfx_vid_find_card()) {
    printk(KERN_ERR "tdfx_vid: no supported devices found\n");
    agp_close();
    unregister_chrdev(TDFX_VID_MAJOR, "tdfx_vid");
    return -EINVAL;
  }



  return 0;

}

void cleanup_module(void)
{
  if(tdfx_mmio_base)
    iounmap(tdfx_mmio_base);
  agp_close();
  printk(KERN_INFO "tdfx_vid: Cleaning up module\n");
  unregister_chrdev(TDFX_VID_MAJOR, "tdfx_vid");
}