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
|
/*
* Copyright (C) 2004 Thomas Hellstrom, 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, 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 AUTHORS OR COPYRIGHT HOLDERS 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "via_driver.h"
#include "via_memcpy.h"
#include "compiler.h"
#define BSIZ 2048 /* Size of /proc/cpuinfo buffer */
#define BSIZW 720 /* Typical Copy Width (YUV420) Copy. */
#define BSIZA 736 /* Multiple of 32 bytes */
#define BSIZH 576 /* Typical Copy Hight */
#define SSE_PREFETCH " prefetchnta "
#define FENCE __asm__ __volatile__ ("sfence":::"memory");
#define FENCEMMS __asm__ __volatile__ ("\t" \
"sfence\n\t" \
"emms\n\t" \
:::"memory");
#define FEMMS __asm__ __volatile__("femms":::"memory");
#define EMMS __asm__ __volatile__("emms":::"memory");
#define NOW_PREFETCH " prefetch "
#define PREFETCH1(arch_prefetch,from) \
__asm__ __volatile__ ( \
"1: " arch_prefetch "(%0)\n" \
arch_prefetch "32(%0)\n" \
arch_prefetch "64(%0)\n" \
arch_prefetch "96(%0)\n" \
arch_prefetch "128(%0)\n" \
arch_prefetch "160(%0)\n" \
arch_prefetch "192(%0)\n" \
arch_prefetch "256(%0)\n" \
arch_prefetch "288(%0)\n" \
"2:\n" \
: : "r" (from) );
#define PREFETCH2(arch_prefetch,from) \
__asm__ __volatile__ ( \
arch_prefetch "320(%0)\n" \
: : "r" (from) );
#define PREFETCH3(arch_prefetch,from) \
__asm__ __volatile__ ( \
arch_prefetch "288(%0)\n" \
: : "r" (from) );
#define small_memcpy(to,from,n) \
{ \
__asm__ __volatile__( \
"movl %2,%%ecx\n\t" \
"sarl $2,%%ecx\n\t" \
"rep ; movsl\n\t" \
"testb $2,%b2\n\t" \
"je 1f\n\t" \
"movsw\n" \
"1:\ttestb $1,%b2\n\t" \
"je 2f\n\t" \
"movsb\n" \
"2:" \
:"=&D" (to), "=&S" (from) \
:"q" (n),"0" ((long) to),"1" ((long) from) \
: "%ecx","memory"); \
}
#define SSE_CPY(prefetch,from,to,dummy,lcnt) \
if ((unsigned long) from & 15) { \
__asm__ __volatile__ ( \
"1:\n" \
prefetch "320(%1)\n" \
" movups (%1), %%xmm0\n" \
" movups 16(%1), %%xmm1\n" \
" movntps %%xmm0, (%0)\n" \
" movntps %%xmm1, 16(%0)\n" \
prefetch "352(%1)\n" \
" movups 32(%1), %%xmm2\n" \
" movups 48(%1), %%xmm3\n" \
" movntps %%xmm2, 32(%0)\n" \
" movntps %%xmm3, 48(%0)\n" \
" addl $64,%0\n" \
" addl $64,%1\n" \
" decl %2\n" \
" jne 1b\n" \
:"=&D"(to), "=&S"(from), "=&r"(dummy) \
:"0" (to), "1" (from), "2" (lcnt): "memory"); \
} else { \
__asm__ __volatile__ ( \
"2:\n" \
prefetch "320(%1)\n" \
" movaps (%1), %%xmm0\n" \
" movaps 16(%1), %%xmm1\n" \
" movntps %%xmm0, (%0)\n" \
" movntps %%xmm1, 16(%0)\n" \
prefetch "352(%1)\n" \
" movaps 32(%1), %%xmm2\n" \
" movaps 48(%1), %%xmm3\n" \
" movntps %%xmm2, 32(%0)\n" \
" movntps %%xmm3, 48(%0)\n" \
" addl $64,%0\n" \
" addl $64,%1\n" \
" decl %2\n" \
" jne 2b\n" \
:"=&D"(to), "=&S"(from), "=&r"(dummy) \
:"0" (to), "1" (from), "2" (lcnt): "memory"); \
}
#define MMX_CPY(prefetch,from,to,dummy,lcnt) \
__asm__ __volatile__ ( \
"1:\n" \
prefetch "320(%1)\n" \
"2: movq (%1), %%mm0\n" \
" movq 8(%1), %%mm1\n" \
" movq 16(%1), %%mm2\n" \
" movq 24(%1), %%mm3\n" \
" movq %%mm0, (%0)\n" \
" movq %%mm1, 8(%0)\n" \
" movq %%mm2, 16(%0)\n" \
" movq %%mm3, 24(%0)\n" \
prefetch "352(%1)\n" \
" movq 32(%1), %%mm0\n" \
" movq 40(%1), %%mm1\n" \
" movq 48(%1), %%mm2\n" \
" movq 56(%1), %%mm3\n" \
" movq %%mm0, 32(%0)\n" \
" movq %%mm1, 40(%0)\n" \
" movq %%mm2, 48(%0)\n" \
" movq %%mm3, 56(%0)\n" \
" addl $64,%0\n" \
" addl $64,%1\n" \
" decl %2\n" \
" jne 1b\n" \
:"=&D"(to), "=&S"(from), "=&r"(dummy) \
:"0" (to), "1" (from), "2" (lcnt) : "memory");
#define MMXEXT_CPY(prefetch,from,to,dummy,lcnt) \
__asm__ __volatile__ ( \
".p2align 4,,7\n" \
"1:\n" \
prefetch "320(%1)\n" \
" movq (%1), %%mm0\n" \
" movq 8(%1), %%mm1\n" \
" movq 16(%1), %%mm2\n" \
" movq 24(%1), %%mm3\n" \
" movntq %%mm0, (%0)\n" \
" movntq %%mm1, 8(%0)\n" \
" movntq %%mm2, 16(%0)\n" \
" movntq %%mm3, 24(%0)\n" \
prefetch "352(%1)\n" \
" movq 32(%1), %%mm0\n" \
" movq 40(%1), %%mm1\n" \
" movq 48(%1), %%mm2\n" \
" movq 56(%1), %%mm3\n" \
" movntq %%mm0, 32(%0)\n" \
" movntq %%mm1, 40(%0)\n" \
" movntq %%mm2, 48(%0)\n" \
" movntq %%mm3, 56(%0)\n" \
" addl $64,%0\n" \
" addl $64,%1\n" \
" decl %2\n" \
" jne 1b\n" \
:"=&D"(to), "=&S"(from), "=&r"(dummy) \
:"0" (to), "1" (from), "2" (lcnt) : "memory");
#define PREFETCH_FUNC(prefix,itype,ptype,begin,fence) \
\
static void prefix##_YUV42X(unsigned char *to, \
const unsigned char *from, \
int dstPitch, \
int w, \
int h, \
int yuv422) \
\
{ \
int \
dadd,rest,count,hc,lcnt; \
register int dummy; \
PREFETCH1(ptype##_PREFETCH,from); \
begin; \
count = 2; \
\
/* \
* If destination pitch and width ar equal, do it all in one go. \
*/ \
\
if ( yuv422 ) { \
w <<= 1; \
if (w == dstPitch) { \
w *= h; \
h = 1; \
dstPitch = w; \
count = 0; \
} else { \
h -= 1; \
count = 1; \
} \
} else if (w == dstPitch) { \
w = h*(w + (w >> 1)); \
count = 0; \
h = 1; \
dstPitch = w; \
} \
\
lcnt = w >> 6; \
rest = w & 63; \
while(count--) { \
hc = h; \
lcnt = w >> 6; \
rest = w & 63; \
dadd = dstPitch - w; \
while(hc--) { \
if (lcnt) { \
itype##_CPY(ptype##_PREFETCH,from,to,dummy, \
lcnt); \
} \
if (rest) { \
PREFETCH2(ptype##_PREFETCH,from); \
small_memcpy(to, from, rest); \
PREFETCH3(ptype##_PREFETCH,from); \
} \
to += dadd; \
} \
w >>= 1; \
dstPitch >>= 1; \
h -= 1; \
} \
if (lcnt > 5) { \
lcnt -= 5; \
itype##_CPY(ptype##_PREFETCH,from,to,dummy,lcnt); \
lcnt = 5; \
} \
if (lcnt) { \
itype##_CPY("#",from,to,dummy,lcnt); \
} \
if (rest) small_memcpy(to, from, rest); \
fence; \
}
#define NOPREFETCH_FUNC(prefix,itype,begin,fence) \
static void prefix##_YUV42X(unsigned char *to, \
const unsigned char *from, \
int dstPitch, \
int w, \
int h, \
int yuv422) \
\
{ \
int \
dadd,rest,count,hc,lcnt; \
register int dummy; \
begin; \
count = 2; \
\
/* \
* If destination pitch and width ar equal, do it all in one go. \
*/ \
\
if ( yuv422 ) { \
w <<= 1; \
count = 1; \
if (w == dstPitch) { \
w *= h; \
h = 1; \
dstPitch = w; \
} \
} else if (w == dstPitch) { \
w = h*(w + (w >> 1)); \
count = 1; \
h = 1; \
dstPitch = w; \
} \
\
lcnt = w >> 6; \
rest = w & 63; \
while(count--) { \
hc = h; \
dadd = dstPitch - w; \
lcnt = w >> 6; \
rest = w & 63; \
while(hc--) { \
if (lcnt) { \
itype##_CPY("#",from,to,dummy,lcnt); \
} \
if (rest) small_memcpy(to, from, rest); \
to += dadd; \
} \
w >>= 1; \
dstPitch >>= 1; \
} \
fence; \
} \
#if !defined(__i386__) || (defined(linux) && defined(__i386__))
static void libc_YUV42X(unsigned char *dst,
const unsigned char *src,
int dstPitch,
int w,
int h, int yuv422) {
if ( yuv422 ) w <<= 1;
if (dstPitch == w) {
int size = h*((yuv422) ? w : (w + (w >> 1)));
xf86memcpy(dst, src, size);
return;
} else {
int count;
/* copy Y component to video memory */
count = h;
while(count--) {
xf86memcpy(dst, src, w);
src += w;
dst += dstPitch;
}
/* UV component is 1/2 of Y */
if (! yuv422 ) {
w >>= 1;
dstPitch >>= 1;
/* copy V(Cr),U(Cb) components to video memory */
count = h;
while(count--) {
xf86memcpy(dst, src, w);
src += w;
dst += dstPitch;
}
}
}
}
#endif
#ifdef __i386__
/* linux kernel __memcpy */
static __inline void * __memcpy(void * to, const void * from, size_t n)
{
int d1,d2,d3;
__asm__ __volatile__(
"rep ; movsl\n\t"
"testb $2,%b4\n\t"
"je 1f\n\t"
"movsw\n"
"1:\ttestb $1,%b4\n\t"
"je 2f\n\t"
"movsb\n"
"2:"
: "=&c" (d1), "=&D" (d2), "=&S" (d3)
:"0" (n >> 2), "q" (n),"1" ((long) to),"2" ((long) from)
: "memory");
return (to);
}
static void kernel_YUV42X(unsigned char *dst,
const unsigned char *src,
int dstPitch,
int w,
int h, int yuv422) {
if ( yuv422 ) w <<= 1;
if (dstPitch == w) {
int size = h*((yuv422) ? w : (w + (w >> 1)));
__memcpy(dst, src, size);
return;
} else {
int count;
/* copy Y component to video memory */
count = h;
while(count--) {
__memcpy(dst, src, w);
src += w;
dst += dstPitch;
}
/* UV component is 1/2 of Y */
if (! yuv422 ) {
w >>= 1;
dstPitch >>= 1;
/* copy V(Cr),U(Cb) components to video memory */
count = h;
while(count--) {
__memcpy(dst, src, w);
src += w;
dst += dstPitch;
}
}
}
}
#ifdef linux
PREFETCH_FUNC(sse,SSE,SSE,,FENCE)
PREFETCH_FUNC(mmxext,MMXEXT,SSE,EMMS,FENCEMMS)
PREFETCH_FUNC(now,MMX,NOW,FEMMS,FEMMS)
NOPREFETCH_FUNC(mmx,MMX,EMMS,EMMS)
static void * kernel_memcpy(void * to, const void * from, size_t len) {
return __memcpy(to, from, len);
}
static unsigned fastrdtsc(void)
{
unsigned eax;
__asm__ volatile ("\t"
"pushl %%ebx\n\t"
"cpuid\n\t"
".byte 0x0f, 0x31\n\t"
"popl %%ebx\n"
: "=a" (eax)
: "0"(0)
: "ecx","edx","cc");
return eax;
}
static unsigned time_function(vidCopyFunc mf,unsigned char *buf1,
unsigned char *buf2) {
unsigned t,t2;
t = fastrdtsc();
(*mf)(buf1,buf2,BSIZA,BSIZW,BSIZH,0);
t2 = fastrdtsc();
return ((t < t2) ? t2 - t : 0xFFFFFFFFU - (t - t2 -1));
}
enum {libc=0,kernel,sse,mmx,now,mmxext,totNum};
typedef struct {
vidCopyFunc mFunc;
char *mName,**cpuFlag;
} McFuncData;
static char *libc_cpuflags[] = {" ",0};
static char *kernel_cpuflags[] = {" ",0};
static char *sse_cpuflags[] = {" sse ",0};
static char *mmx_cpuflags[] = {" mmx ",0};
static char *now_cpuflags[] = {" 3dnow ",0};
static char *mmx2_cpuflags[] = {" mmxext ", " sse ",0};
static McFuncData mcFunctions[totNum] =
{{libc_YUV42X,"libc",libc_cpuflags},
{kernel_YUV42X,"kernel",kernel_cpuflags},
{sse_YUV42X,"SSE",sse_cpuflags},
{mmx_YUV42X,"MMX",mmx_cpuflags},
{now_YUV42X,"3DNow!",now_cpuflags},
{mmxext_YUV42X,"MMX2",mmx2_cpuflags}};
static int flagValid(const char *cpuinfo, char *flag) {
const char *flagLoc,*nextProc;
int located = 0;
while ((cpuinfo = strstr(cpuinfo, "processor\t:"))) {
located = 1;
cpuinfo += 11;
if ((flagLoc = strstr(cpuinfo,flag))) {
if ((nextProc = strstr(cpuinfo,"processor\t:"))) {
if (nextProc < flagLoc) return 0;
}
} else {
return 0;
}
}
return located;
}
static int cpuValid(const char *cpuinfo, char **flags) {
for(; *flags != 0; flags++) {
if (flagValid(cpuinfo, *flags)) return 1;
}
return 0;
}
#endif
vidCopyFunc viaVidCopyInit( char *copyType,
ScreenPtr pScreen ) {
/*
* Benchmark the video copy routines using a relevant benchmark
* and choose the fastest.
*/
ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
#ifdef linux
char buf[BSIZ];
unsigned char *buf1,*buf2,*buf3;
char *tmpBuf,*endBuf;
int count,j,bestSoFar;
unsigned best,tmp,testSize,alignSize,tmp2;
VIAMem tmpFbBuffer;
McFuncData *curData;
FILE *cpuInfoFile;
double cpuFreq;
VIAPtr pVia = VIAPTR(pScrn);
if (NULL == (cpuInfoFile = fopen("/proc/cpuinfo","r"))) {
return libc_YUV42X;
}
count = fread(buf,1,BSIZ,cpuInfoFile);
if (ferror(cpuInfoFile)) {
fclose(cpuInfoFile);
return libc_YUV42X;
}
fclose(cpuInfoFile);
if (BSIZ == count) {
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"\"/proc/cpuinfo\" file too long. "
"Using Linux kernel memcpy.\n");
return libc_YUV42X;
}
buf[count] = 0;
while(count--)
if ('\n' == buf[count]) buf[count] = ' ';
/*
* Extract the cpu frequency.
*/
cpuFreq = 0.;
if (NULL != (tmpBuf = strstr(buf,"cpu MHz"))) {
if (NULL != (tmpBuf = strstr(tmpBuf,":") + 1)) {
cpuFreq = strtod(tmpBuf,&endBuf);
if (endBuf == tmpBuf) tmpBuf = NULL;
}
}
alignSize = BSIZH*(BSIZA + (BSIZA >> 1));
testSize = BSIZH*(BSIZW + (BSIZW >> 1));
tmpFbBuffer.pool = 0;
/*
* Allocate an area of offscreen FB memory, (buf1), a simulated video
* player buffer (buf2) and a pool of uninitialized "video" data (buf3).
*/
if (VIAAllocLinear(&tmpFbBuffer, pScrn, alignSize + 31))
return libc_YUV42X;
if (NULL == (buf2 = (unsigned char *)xalloc(testSize))) {
VIAFreeLinear(&tmpFbBuffer);
return libc_YUV42X;
}
if (NULL == (buf3 = (unsigned char *)xalloc(testSize))) {
xfree(buf2);
VIAFreeLinear(&tmpFbBuffer);
return libc_YUV42X;
}
buf1 = (unsigned char *)pVia->FBBase + tmpFbBuffer.base;
/*
* Align the frame buffer destination memory to a 32 byte boundary.
*/
if ((unsigned long)buf1 & 31)
buf1 += (32 - ((unsigned long)buf1 & 31));
bestSoFar = 0;
best = 0xFFFFFFFFU;
/*
* Make probable buf1 and buf2 are not paged out by
* referencing them.
*/
libc_YUV42X(buf1,buf2,BSIZA,BSIZW,BSIZH,0);
xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Benchmarking %s copy. Less is better.\n",copyType);
for (j=0; j<totNum; ++j) {
curData = mcFunctions + j;
if (cpuValid(buf,curData->cpuFlag)) {
/*
* Simulate setup of the video buffer.
*/
kernel_memcpy(buf2,buf3,testSize);
/*
* Copy the video buffer to frame-buffer memory.
*/
tmp = time_function(curData->mFunc,buf1,buf2);
/*
* Do it again to avoid context switch effects.
*/
kernel_memcpy(buf2,buf3,testSize);
tmp2 = time_function(curData->mFunc,buf1,buf2);
tmp = (tmp2 < tmp) ? tmp2 : tmp;
if (NULL == tmpBuf) {
xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
"Timed %6s YUV420 copy... %u.\n",curData->mName,tmp);
} else {
xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
"Timed %6s YUV420 copy... %u. "
"Throughput: %.1f MiB/s.\n",curData->mName,tmp,
cpuFreq * 1.e6 * (double)testSize /
((double)(tmp) * (double)(0x100000)));
}
if (tmp < best) {
best = tmp;
bestSoFar = j;
}
} else {
xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
"Ditch %6s YUV420 copy... Not supported by CPU.\n",
curData->mName);
}
}
xfree(buf3);
xfree(buf2);
VIAFreeLinear(&tmpFbBuffer);
xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
"Using %s YUV42X copy for %s.\n",mcFunctions[bestSoFar].mName,
copyType);
return mcFunctions[bestSoFar].mFunc;
#endif
xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Using copy of Linux kernel memcpy for video.\n");
return kernel_YUV42X;
}
#else
vidCopyFunc viaVidCopyInit( char *copyType,
ScreenPtr pScreen ) {
ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Using default xfree86 memcpy for video.\n");
return libc_YUV42X;
}
#endif
|