2 * Copyright (C) 2012 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__)
32 /* I/O intrin functions. */
33 static __inline__ __attribute__((always_inline)) unsigned char __inbyte(unsigned short Port)
37 __asm__ __volatile__ (
46 static __inline__ __attribute__((always_inline)) unsigned short __inword(unsigned short Port)
50 __asm__ __volatile__ (
59 static __inline__ __attribute__((always_inline)) unsigned long __indword(unsigned short Port)
63 __asm__ __volatile__ (
72 static __inline__ __attribute__((always_inline)) void __outbyte(unsigned short Port,unsigned char Data)
74 __asm__ __volatile__ (
77 : "a" (Data), "d" (Port)
81 static __inline__ __attribute__((always_inline)) void __outword(unsigned short Port,unsigned short Data)
83 __asm__ __volatile__ (
86 : "a" (Data), "d" (Port)
90 static __inline__ __attribute__((always_inline)) void __outdword(unsigned short Port,unsigned long Data)
92 __asm__ __volatile__ (
95 : "a" (Data), "d" (Port)
99 static __inline__ __attribute__((always_inline)) void __inbytestring(unsigned short Port,unsigned char *Buffer,unsigned long Count)
101 __asm__ __volatile__ (
103 : "=D" (Buffer), "=c" (Count)
104 : "d"(Port), "0"(Buffer), "1" (Count)
108 static __inline__ __attribute__((always_inline)) void __inwordstring(unsigned short Port,unsigned short *Buffer,unsigned long Count)
110 __asm__ __volatile__ (
112 : "=D" (Buffer), "=c" (Count)
113 : "d"(Port), "0"(Buffer), "1" (Count)
117 static __inline__ __attribute__((always_inline)) void __indwordstring(unsigned short Port,unsigned long *Buffer,unsigned long Count)
119 __asm__ __volatile__ (
121 : "=D" (Buffer), "=c" (Count)
122 : "d"(Port), "0"(Buffer), "1" (Count)
126 static __inline__ __attribute__((always_inline)) void __outbytestring(unsigned short Port,unsigned char *Buffer,unsigned long Count)
128 __asm__ __volatile__ (
130 : "=S" (Buffer), "=c" (Count)
131 : "d"(Port), "0"(Buffer), "1" (Count)
135 static __inline__ __attribute__((always_inline)) void __outwordstring(unsigned short Port,unsigned short *Buffer,unsigned long Count)
137 __asm__ __volatile__ (
139 : "=S" (Buffer), "=c" (Count)
140 : "d"(Port), "0"(Buffer), "1" (Count)
144 static __inline__ __attribute__((always_inline)) void __outdwordstring(unsigned short Port,unsigned long *Buffer,unsigned long Count)
146 __asm__ __volatile__ (
148 : "=S" (Buffer), "=c" (Count)
149 : "d"(Port), "0"(Buffer), "1" (Count)
153 static __inline__ __attribute__((always_inline)) unsigned long __readdr0(void)
156 __asm__ __volatile__ (
157 "mov %%dr0, %[value]"
158 : [value] "=a" (value)
163 static __inline__ __attribute__((always_inline)) unsigned long __readdr1(void)
166 __asm__ __volatile__ (
167 "mov %%dr1, %[value]"
168 : [value] "=a" (value)
173 static __inline__ __attribute__((always_inline)) unsigned long __readdr2(void)
176 __asm__ __volatile__ (
177 "mov %%dr2, %[value]"
178 : [value] "=a" (value)
183 static __inline__ __attribute__((always_inline)) unsigned long __readdr3(void)
186 __asm__ __volatile__ (
187 "mov %%dr3, %[value]"
188 : [value] "=a" (value)
193 static __inline__ __attribute__((always_inline)) unsigned long __readdr7(void)
196 __asm__ __volatile__ (
197 "mov %%dr7, %[value]"
198 : [value] "=a" (value)
203 static __inline__ __attribute__((always_inline)) unsigned long __readdr(unsigned long reg)
231 static __inline__ __attribute__((always_inline)) void __writedr0(unsigned long Data)
233 __asm__ __volatile__ (
240 static __inline__ __attribute__((always_inline)) void __writedr1(unsigned long Data)
242 __asm__ __volatile__ (
249 static __inline__ __attribute__((always_inline)) void __writedr2(unsigned long Data)
251 __asm__ __volatile__ (
258 static __inline__ __attribute__((always_inline)) void __writedr3(unsigned long Data)
260 __asm__ __volatile__ (
267 static __inline__ __attribute__((always_inline)) void __writedr7(unsigned long Data)
269 __asm__ __volatile__ (
276 static __inline__ __attribute__((always_inline)) void __writedr(unsigned long reg, unsigned long Data)
304 static __inline__ __attribute__((always_inline)) unsigned long __readcr0(void)
307 __asm__ __volatile__ (
308 "mov %%cr0, %[value]"
309 : [value] "=a" (value));
313 static __inline__ __attribute__((always_inline)) unsigned long __readcr2(void)
316 __asm__ __volatile__ (
317 "mov %%cr2, %[value]"
318 : [value] "=a" (value));
322 static __inline__ __attribute__((always_inline)) unsigned long __readcr3(void)
325 __asm__ __volatile__ (
326 "mov %%cr3, %[value]"
327 : [value] "=a" (value));
331 static __inline__ __attribute__((always_inline)) unsigned long __readcr4(void)
334 __asm__ __volatile__ (
335 "mov %%cr4, %[value]"
336 : [value] "=a" (value));
340 static __inline__ __attribute__((always_inline)) unsigned long __readcr8(void)
343 __asm__ __volatile__ (
344 "mov %%cr8, %[value]"
345 : [value] "=a" (value));
349 static __inline__ __attribute__((always_inline)) unsigned long __readcr(unsigned long reg)
377 static __inline__ __attribute__((always_inline)) void __writecr0(unsigned long Data)
379 __asm__ __volatile__ (
386 static __inline__ __attribute__((always_inline)) void __writecr2(unsigned long Data)
388 __asm__ __volatile__ (
395 static __inline__ __attribute__((always_inline)) void __writecr3(unsigned long Data)
397 __asm__ __volatile__ (
404 static __inline__ __attribute__((always_inline)) void __writecr4(unsigned long Data)
406 __asm__ __volatile__ (
413 static __inline__ __attribute__((always_inline)) void __writecr8(unsigned long Data)
415 __asm__ __volatile__ (
422 static __inline__ __attribute__((always_inline)) void __writecr(unsigned long reg, unsigned long Data)
450 static __inline__ __attribute__((always_inline)) UINT64 __readmsr(UINT32 msr)
453 __asm__ __volatile__(
461 static __inline__ __attribute__((always_inline)) void __writemsr (UINT32 msr, UINT64 Value)
463 __asm__ __volatile__ (
466 : "c" (msr), "A" (Value)
470 static __inline__ __attribute__((always_inline)) UINT64 __rdtsc(void)
473 __asm__ __volatile__ (
479 static __inline__ __attribute__((always_inline)) void __cpuid(int CPUInfo[], const int InfoType)
481 __asm__ __volatile__(
483 :"=a" (CPUInfo[0]), "=b" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3])
489 static __inline__ __attribute__((always_inline)) void _disable(void)
491 __asm__ __volatile__ ("cli");
495 static __inline__ __attribute__((always_inline)) void _enable(void)
497 __asm__ __volatile__ ("sti");
501 static __inline__ __attribute__((always_inline)) void __halt(void)
503 __asm__ __volatile__ ("hlt");
507 static __inline__ __attribute__((always_inline)) void __debugbreak(void)
509 __asm__ __volatile__ ("int3");
513 static __inline__ __attribute__((always_inline)) void __wbinvd(void)
515 __asm__ __volatile__ ("wbinvd");
519 static __inline__ __attribute__((always_inline)) void __lidt(void *Source)
521 __asm__ __volatile__("lidt %0" : : "m"(*(short*)Source));
524 static __inline__ __attribute__((always_inline)) void __writefsbyte(const unsigned long Offset, const unsigned char Data)
526 __asm__("movb %b[Data], %%fs:%a[Offset]" : : [Offset] "ir" (Offset), [Data] "iq" (Data));
529 static __inline__ __attribute__((always_inline)) void __writefsword(const unsigned long Offset, const unsigned short Data)
531 __asm__("movw %w[Data], %%fs:%a[Offset]" : : [Offset] "ir" (Offset), [Data] "iq" (Data));
534 static __inline__ __attribute__((always_inline)) void __writefsdword(const unsigned long Offset, const unsigned long Data)
536 __asm__("movl %k[Data], %%fs:%a[Offset]" : : [Offset] "ir" (Offset), [Data] "iq" (Data));
539 static __inline__ __attribute__((always_inline)) unsigned char __readfsbyte(const unsigned long Offset)
542 __asm__("movb %%fs:%a[Offset], %b[value]" : [value] "=q" (value) : [Offset] "irm" (Offset));
546 static __inline__ __attribute__((always_inline)) unsigned short __readfsword(const unsigned long Offset)
548 unsigned short value;
549 __asm__("movw %%fs:%a[Offset], %w[value]" : [value] "=q" (value) : [Offset] "irm" (Offset));
553 static __inline__ __attribute__((always_inline)) unsigned long long __readfsdword(unsigned long long Offset)
555 unsigned long long value;
556 __asm__("movl %%fs:%a[Offset], %k[value]" : [value] "=q" (value) : [Offset] "irm" (Offset));
561 typedef long long __v2di __attribute__ ((__vector_size__ (16)));
562 typedef long long __m128i __attribute__ ((__vector_size__ (16), __may_alias__));
563 static __inline__ __attribute__((always_inline)) void _mm_stream_si128_fs2 (void *__A, __m128i __B)
565 __asm__(".byte 0x64"); // fs prefix
566 __builtin_ia32_movntdq ((__v2di *)__A, (__v2di)__B);
569 static __inline__ __attribute__((always_inline)) void _mm_stream_si128_fs (void *__A, void *__B)
572 data = (__m128i) __builtin_ia32_lddqu ((char const *)__B);
573 _mm_stream_si128_fs2 (__A, data);
577 static __inline__ __attribute__((always_inline)) void _mm_clflush_fs (void *__A)
579 __asm__(".byte 0x64"); // fs prefix
580 __builtin_ia32_clflush (__A);
582 static __inline __attribute__(( __always_inline__)) void _mm_mfence (void)
584 __builtin_ia32_mfence ();
586 static __inline __attribute__(( __always_inline__)) void _mm_sfence (void)
588 __builtin_ia32_sfence ();
591 static __inline__ __attribute__((always_inline)) void __stosb(unsigned char *dest, unsigned char data, size_t count)
593 __asm__ __volatile__ (
595 : "=D" (dest), "=c" (count)
596 : "a"(data), "0"(dest), "1" (count)
600 static __inline__ __attribute__((always_inline)) void __movsb(unsigned char *dest, unsigned char *data, size_t count)
602 __asm__ __volatile__ (
604 : "=D" (dest), "=S"(data), "=c" (count)
605 : "S"(data), "0"(dest), "1" (count)
609 static __inline__ __attribute__((always_inline))
610 void debug_point ( unsigned short Port, unsigned long Data )
612 __outdword (Port, Data);
613 __asm__ __volatile__ (".word 0xfeeb");
617 static __inline__ __attribute__((always_inline))
618 void delay_point ( unsigned short Port, unsigned long Data, unsigned long delayTime )
622 __outdword (Port, Data);
623 while (Index < delayTime * 600000) {
624 __outdword (0xE0, 0);
628 #endif // defined (__GNUC__)