2 * Copyright (c) 2011, Advanced Micro Devices, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Advanced Micro Devices, Inc. nor the names of
13 * its contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #if defined (__GNUC__)
30 #include <pmmintrin.h>
33 /* I/O intrin functions. */
34 static __inline__ __attribute__((always_inline)) unsigned char __inbyte(unsigned short Port)
38 __asm__ __volatile__ (
47 static __inline__ __attribute__((always_inline)) unsigned short __inword(unsigned short Port)
51 __asm__ __volatile__ (
60 static __inline__ __attribute__((always_inline)) unsigned long __indword(unsigned short Port)
64 __asm__ __volatile__ (
73 static __inline__ __attribute__((always_inline)) void __outbyte(unsigned short Port,unsigned char Data)
75 __asm__ __volatile__ (
78 : "a" (Data), "d" (Port)
82 static __inline__ __attribute__((always_inline)) void __outword(unsigned short Port,unsigned short Data)
84 __asm__ __volatile__ (
87 : "a" (Data), "d" (Port)
91 static __inline__ __attribute__((always_inline)) void __outdword(unsigned short Port,unsigned long Data)
93 __asm__ __volatile__ (
96 : "a" (Data), "d" (Port)
100 static __inline__ __attribute__((always_inline)) void __inbytestring(unsigned short Port,unsigned char *Buffer,unsigned long Count)
102 __asm__ __volatile__ (
104 : "=D" (Buffer), "=c" (Count)
105 : "d"(Port), "0"(Buffer), "1" (Count)
109 static __inline__ __attribute__((always_inline)) void __inwordstring(unsigned short Port,unsigned short *Buffer,unsigned long Count)
111 __asm__ __volatile__ (
113 : "=D" (Buffer), "=c" (Count)
114 : "d"(Port), "0"(Buffer), "1" (Count)
118 static __inline__ __attribute__((always_inline)) void __indwordstring(unsigned short Port,unsigned long *Buffer,unsigned long Count)
120 __asm__ __volatile__ (
122 : "=D" (Buffer), "=c" (Count)
123 : "d"(Port), "0"(Buffer), "1" (Count)
127 static __inline__ __attribute__((always_inline)) void __outbytestring(unsigned short Port,unsigned char *Buffer,unsigned long Count)
129 __asm__ __volatile__ (
131 : "=S" (Buffer), "=c" (Count)
132 : "d"(Port), "0"(Buffer), "1" (Count)
136 static __inline__ __attribute__((always_inline)) void __outwordstring(unsigned short Port,unsigned short *Buffer,unsigned long Count)
138 __asm__ __volatile__ (
140 : "=S" (Buffer), "=c" (Count)
141 : "d"(Port), "0"(Buffer), "1" (Count)
145 static __inline__ __attribute__((always_inline)) void __outdwordstring(unsigned short Port,unsigned long *Buffer,unsigned long Count)
147 __asm__ __volatile__ (
149 : "=S" (Buffer), "=c" (Count)
150 : "d"(Port), "0"(Buffer), "1" (Count)
154 static __inline__ __attribute__((always_inline)) unsigned long __readdr0(void)
157 __asm__ __volatile__ (
158 "mov %%dr0, %[value]"
159 : [value] "=a" (value)
164 static __inline__ __attribute__((always_inline)) unsigned long __readdr1(void)
167 __asm__ __volatile__ (
168 "mov %%dr1, %[value]"
169 : [value] "=a" (value)
174 static __inline__ __attribute__((always_inline)) unsigned long __readdr2(void)
177 __asm__ __volatile__ (
178 "mov %%dr2, %[value]"
179 : [value] "=a" (value)
184 static __inline__ __attribute__((always_inline)) unsigned long __readdr3(void)
187 __asm__ __volatile__ (
188 "mov %%dr3, %[value]"
189 : [value] "=a" (value)
194 static __inline__ __attribute__((always_inline)) unsigned long __readdr7(void)
197 __asm__ __volatile__ (
198 "mov %%dr7, %[value]"
199 : [value] "=a" (value)
204 static __inline__ __attribute__((always_inline)) unsigned long __readdr(unsigned long reg)
232 static __inline__ __attribute__((always_inline)) void __writedr0(unsigned long Data)
234 __asm__ __volatile__ (
241 static __inline__ __attribute__((always_inline)) void __writedr1(unsigned long Data)
243 __asm__ __volatile__ (
250 static __inline__ __attribute__((always_inline)) void __writedr2(unsigned long Data)
252 __asm__ __volatile__ (
259 static __inline__ __attribute__((always_inline)) void __writedr3(unsigned long Data)
261 __asm__ __volatile__ (
268 static __inline__ __attribute__((always_inline)) void __writedr7(unsigned long Data)
270 __asm__ __volatile__ (
277 static __inline__ __attribute__((always_inline)) void __writedr(unsigned long reg, unsigned long Data)
305 static __inline__ __attribute__((always_inline)) unsigned long __readcr0(void)
308 __asm__ __volatile__ (
309 "mov %%cr0, %[value]"
310 : [value] "=a" (value));
314 static __inline__ __attribute__((always_inline)) unsigned long __readcr2(void)
317 __asm__ __volatile__ (
318 "mov %%cr2, %[value]"
319 : [value] "=a" (value));
323 static __inline__ __attribute__((always_inline)) unsigned long __readcr3(void)
326 __asm__ __volatile__ (
327 "mov %%cr3, %[value]"
328 : [value] "=a" (value));
332 static __inline__ __attribute__((always_inline)) unsigned long __readcr4(void)
335 __asm__ __volatile__ (
336 "mov %%cr4, %[value]"
337 : [value] "=a" (value));
341 static __inline__ __attribute__((always_inline)) unsigned long __readcr8(void)
344 __asm__ __volatile__ (
345 "mov %%cr8, %[value]"
346 : [value] "=a" (value));
350 static __inline__ __attribute__((always_inline)) unsigned long __readcr(unsigned long reg)
378 static __inline__ __attribute__((always_inline)) void __writecr0(unsigned long Data)
380 __asm__ __volatile__ (
387 static __inline__ __attribute__((always_inline)) void __writecr2(unsigned long Data)
389 __asm__ __volatile__ (
396 static __inline__ __attribute__((always_inline)) void __writecr3(unsigned long Data)
398 __asm__ __volatile__ (
405 static __inline__ __attribute__((always_inline)) void __writecr4(unsigned long Data)
407 __asm__ __volatile__ (
414 static __inline__ __attribute__((always_inline)) void __writecr8(unsigned long Data)
416 __asm__ __volatile__ (
423 static __inline__ __attribute__((always_inline)) void __writecr(unsigned long reg, unsigned long Data)
451 static __inline__ __attribute__((always_inline)) UINT64 __readmsr(UINT32 msr)
454 __asm__ __volatile__(
462 static __inline__ __attribute__((always_inline)) void __writemsr (UINT32 msr, UINT64 Value)
464 __asm__ __volatile__ (
467 : "c" (msr), "A" (Value)
471 static __inline__ __attribute__((always_inline)) UINT64 __rdtsc(void)
474 __asm__ __volatile__ (
480 static __inline__ __attribute__((always_inline)) void __cpuid(int CPUInfo[], const int InfoType)
482 __asm__ __volatile__(
484 :"=a" (CPUInfo[0]), "=b" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3])
490 static __inline__ __attribute__((always_inline)) void _disable(void)
492 __asm__ __volatile__ ("cli");
496 static __inline__ __attribute__((always_inline)) void _enable(void)
498 __asm__ __volatile__ ("sti");
502 static __inline__ __attribute__((always_inline)) void __halt(void)
504 __asm__ __volatile__ ("hlt");
508 static __inline__ __attribute__((always_inline)) void __debugbreak(void)
510 __asm__ __volatile__ ("int3");
514 static __inline__ __attribute__((always_inline)) void __wbinvd(void)
516 __asm__ __volatile__ ("wbinvd");
520 static __inline__ __attribute__((always_inline)) void __lidt(void *Source)
522 __asm__ __volatile__("lidt %0" : : "m"(*(short*)Source));
525 static __inline__ __attribute__((always_inline)) void __writefsbyte(const unsigned long Offset, const unsigned char Data)
527 __asm__("movb %b[Data], %%fs:%a[Offset]" : : [Offset] "ir" (Offset), [Data] "iq" (Data));
530 static __inline__ __attribute__((always_inline)) void __writefsword(const unsigned long Offset, const unsigned short Data)
532 __asm__("movw %w[Data], %%fs:%a[Offset]" : : [Offset] "ir" (Offset), [Data] "iq" (Data));
535 static __inline__ __attribute__((always_inline)) void __writefsdword(const unsigned long Offset, const unsigned long Data)
537 __asm__("movl %k[Data], %%fs:%a[Offset]" : : [Offset] "ir" (Offset), [Data] "iq" (Data));
540 static __inline__ __attribute__((always_inline)) unsigned char __readfsbyte(const unsigned long Offset)
543 __asm__("movb %%fs:%a[Offset], %b[value]" : [value] "=q" (value) : [Offset] "irm" (Offset));
547 static __inline__ __attribute__((always_inline)) unsigned short __readfsword(const unsigned long Offset)
549 unsigned short value;
550 __asm__("movw %%fs:%a[Offset], %w[value]" : [value] "=q" (value) : [Offset] "irm" (Offset));
554 static __inline__ __attribute__((always_inline)) unsigned long long __readfsdword(unsigned long long Offset)
556 unsigned long long value;
557 __asm__("movl %%fs:%a[Offset], %k[value]" : [value] "=q" (value) : [Offset] "irm" (Offset));
561 static __inline__ __attribute__((always_inline)) void _mm_stream_si128_fs2 (void *__A, __m128i __B)
563 __asm__(".byte 0x64"); // fs prefix
564 __builtin_ia32_movntdq ((__v2di *)__A, (__v2di)__B);
567 static __inline__ __attribute__((always_inline)) void _mm_stream_si128_fs (void *__A, void *__B)
570 data = _mm_lddqu_si128 (__B);
571 _mm_stream_si128_fs2 (__A, data);
574 static __inline__ __attribute__((always_inline)) void _mm_clflush_fs (void *__A)
576 __asm__(".byte 0x64"); // fs prefix
577 __builtin_ia32_clflush (__A);
580 static __inline__ __attribute__((always_inline)) void __stosb(unsigned char *dest, unsigned char data, size_t count)
582 __asm__ __volatile__ (
584 : "=D" (dest), "=c" (count)
585 : "a"(data), "0"(dest), "1" (count)
589 static __inline__ __attribute__((always_inline)) void __movsb(unsigned char *dest, unsigned char *data, size_t count)
591 __asm__ __volatile__ (
593 : "=D" (dest), "=S"(data), "=c" (count)
594 : "S"(data), "0"(dest), "1" (count)
598 static __inline__ __attribute__((always_inline))
599 void debug_point ( unsigned short Port, unsigned long Data )
601 __outdword (Port, Data);
602 __asm__ __volatile__ (".word 0xfeeb");
606 static __inline__ __attribute__((always_inline))
607 void delay_point ( unsigned short Port, unsigned long Data, unsigned long delayTime )
611 __outdword (Port, Data);
612 while (Index < delayTime * 600000) {
613 __outdword (0xE0, 0);
617 #endif // defined (__GNUC__)