cpu/intel: Add socket types
[coreboot2.git] / src / vendorcode / amd / include / gcc-intrin.h
blob44e91d28b6f93b2b414b6018c944b5679a32cdcf
1 /*
2 * Copyright (c) 2011, Advanced Micro Devices, Inc. All rights reserved.
3 * Copyright (c) 2014, Edward O'Callaghan <eocallaghan@alterapraxis.com>
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 <stdint.h>
31 /* I/O intrin functions. */
32 static __inline__ __attribute__((always_inline)) uint8_t __inbyte(uint16_t Port)
34 uint8_t value;
36 __asm__ __volatile__ (
37 "in %1, %0"
38 : "=a" (value)
39 : "Nd" (Port)
42 return value;
45 static __inline__ __attribute__((always_inline)) uint16_t __inword(uint16_t Port)
47 uint16_t value;
49 __asm__ __volatile__ (
50 "in %1, %0"
51 : "=a" (value)
52 : "Nd" (Port)
55 return value;
58 static __inline__ __attribute__((always_inline)) uint32_t __indword(uint16_t Port)
60 uint32_t value;
62 __asm__ __volatile__ (
63 "in %1, %0"
64 : "=a" (value)
65 : "Nd" (Port)
67 return value;
71 static __inline__ __attribute__((always_inline)) void __outbyte(uint16_t Port,uint8_t Data)
73 __asm__ __volatile__ (
74 "out %0, %1"
76 : "a" (Data), "Nd" (Port)
80 static __inline__ __attribute__((always_inline)) void __outword(uint16_t Port,uint16_t Data)
82 __asm__ __volatile__ (
83 "out %0, %1"
85 : "a" (Data), "Nd" (Port)
89 static __inline__ __attribute__((always_inline)) void __outdword(uint16_t Port,uint32_t Data)
91 __asm__ __volatile__ (
92 "out %0, %1"
94 : "a" (Data), "Nd" (Port)
98 static __inline__ __attribute__((always_inline)) void __inbytestring(uint16_t Port,uint8_t *Buffer,unsigned long Count)
100 __asm__ __volatile__ (
101 "rep ; insb"
102 : "+D" (Buffer), "+c" (Count)
103 : "d"(Port)
107 static __inline__ __attribute__((always_inline)) void __inwordstring(uint16_t Port,uint16_t *Buffer,unsigned long Count)
109 __asm__ __volatile__ (
110 "rep ; insw"
111 : "+D" (Buffer), "+c" (Count)
112 : "d"(Port)
116 static __inline__ __attribute__((always_inline)) void __indwordstring(uint16_t Port,unsigned long *Buffer,unsigned long Count)
118 __asm__ __volatile__ (
119 "rep ; insl"
120 : "+D" (Buffer), "+c" (Count)
121 : "d"(Port)
125 static __inline__ __attribute__((always_inline)) void __outbytestring(uint16_t Port,uint8_t *Buffer,unsigned long Count)
127 __asm__ __volatile__ (
128 "rep ; outsb"
129 : "+S" (Buffer), "+c" (Count)
130 : "d"(Port)
134 static __inline__ __attribute__((always_inline)) void __outwordstring(uint16_t Port,uint16_t *Buffer,unsigned long Count)
136 __asm__ __volatile__ (
137 "rep ; outsw"
138 : "+S" (Buffer), "+c" (Count)
139 : "d"(Port)
143 static __inline__ __attribute__((always_inline)) void __outdwordstring(uint16_t Port,unsigned long *Buffer,unsigned long Count)
145 __asm__ __volatile__ (
146 "rep ; outsl"
147 : "+S" (Buffer), "+c" (Count)
148 : "d"(Port)
152 static __inline__ __attribute__((always_inline)) unsigned long __readdr0(void)
154 unsigned long value;
155 __asm__ __volatile__ (
156 "mov %%dr0, %[value]"
157 : [value] "=r" (value)
159 return value;
162 static __inline__ __attribute__((always_inline)) unsigned long __readdr1(void)
164 unsigned long value;
165 __asm__ __volatile__ (
166 "mov %%dr1, %[value]"
167 : [value] "=r" (value)
169 return value;
172 static __inline__ __attribute__((always_inline)) unsigned long __readdr2(void)
174 unsigned long value;
175 __asm__ __volatile__ (
176 "mov %%dr2, %[value]"
177 : [value] "=r" (value)
179 return value;
182 static __inline__ __attribute__((always_inline)) unsigned long __readdr3(void)
184 unsigned long value;
185 __asm__ __volatile__ (
186 "mov %%dr3, %[value]"
187 : [value] "=r" (value)
189 return value;
192 static __inline__ __attribute__((always_inline)) unsigned long __readdr7(void)
194 unsigned long value;
195 __asm__ __volatile__ (
196 "mov %%dr7, %[value]"
197 : [value] "=r" (value)
199 return value;
202 static __inline__ __attribute__((always_inline)) unsigned long __readdr(unsigned long reg)
204 switch (reg){
205 case 0:
206 return __readdr0 ();
207 break;
209 case 1:
210 return __readdr1 ();
211 break;
213 case 2:
214 return __readdr2 ();
215 break;
217 case 3:
218 return __readdr3 ();
219 break;
221 case 7:
222 return __readdr7 ();
223 break;
225 default:
226 return -1;
230 static __inline__ __attribute__((always_inline)) void __writedr0(unsigned long Data)
232 __asm__ __volatile__ (
233 "mov %0, %%dr0"
235 : "r" (Data)
239 static __inline__ __attribute__((always_inline)) void __writedr1(unsigned long Data)
241 __asm__ __volatile__ (
242 "mov %0, %%dr1"
244 : "r" (Data)
248 static __inline__ __attribute__((always_inline)) void __writedr2(unsigned long Data)
250 __asm__ __volatile__ (
251 "mov %0, %%dr2"
253 : "r" (Data)
257 static __inline__ __attribute__((always_inline)) void __writedr3(unsigned long Data)
259 __asm__ __volatile__ (
260 "mov %0, %%dr3"
262 : "r" (Data)
266 static __inline__ __attribute__((always_inline)) void __writedr7(unsigned long Data)
268 __asm__ __volatile__ (
269 "mov %0, %%dr7"
271 : "r" (Data)
275 static __inline__ __attribute__((always_inline)) void __writedr(unsigned long reg, unsigned long Data)
277 switch (reg){
278 case 0:
279 __writedr0 (Data);
280 break;
282 case 1:
283 __writedr1 (Data);
284 break;
286 case 2:
287 __writedr2 (Data);
288 break;
290 case 3:
291 __writedr3 (Data);
292 break;
294 case 7:
295 __writedr7 (Data);
296 break;
298 default:
303 static __inline__ __attribute__((always_inline)) unsigned long __readcr0(void)
305 unsigned long value;
306 __asm__ __volatile__ (
307 "mov %%cr0, %[value]"
308 : [value] "=r" (value));
309 return value;
312 static __inline__ __attribute__((always_inline)) unsigned long __readcr2(void)
314 unsigned long value;
315 __asm__ __volatile__ (
316 "mov %%cr2, %[value]"
317 : [value] "=r" (value));
318 return value;
321 static __inline__ __attribute__((always_inline)) unsigned long __readcr3(void)
323 unsigned long value;
324 __asm__ __volatile__ (
325 "mov %%cr3, %[value]"
326 : [value] "=r" (value));
327 return value;
330 static __inline__ __attribute__((always_inline)) unsigned long __readcr4(void)
332 unsigned long value;
333 __asm__ __volatile__ (
334 "mov %%cr4, %[value]"
335 : [value] "=r" (value));
336 return value;
339 static __inline__ __attribute__((always_inline)) unsigned long __readcr8(void)
341 unsigned long value;
342 __asm__ __volatile__ (
343 "mov %%cr8, %[value]"
344 : [value] "=r" (value));
345 return value;
348 static __inline__ __attribute__((always_inline)) unsigned long __readcr(unsigned long reg)
350 switch (reg){
351 case 0:
352 return __readcr0 ();
353 break;
355 case 2:
356 return __readcr2 ();
357 break;
359 case 3:
360 return __readcr3 ();
361 break;
363 case 4:
364 return __readcr4 ();
365 break;
367 case 8:
368 return __readcr8 ();
369 break;
371 default:
372 return -1;
376 static __inline__ __attribute__((always_inline)) void __writecr0(unsigned long Data)
378 __asm__ __volatile__ (
379 "mov %0, %%cr0"
381 : "r" (Data)
382 : "memory"
386 static __inline__ __attribute__((always_inline)) void __writecr2(unsigned long Data)
388 __asm__ __volatile__ (
389 "mov %0, %%cr2"
391 : "r" (Data)
395 static __inline__ __attribute__((always_inline)) void __writecr3(unsigned long Data)
397 __asm__ __volatile__ (
398 "mov %0, %%cr3"
400 : "r" (Data)
404 static __inline__ __attribute__((always_inline)) void __writecr4(unsigned long Data)
406 __asm__ __volatile__ (
407 "mov %0, %%cr4"
409 : "r" (Data)
413 static __inline__ __attribute__((always_inline)) void __writecr8(unsigned long Data)
415 __asm__ __volatile__ (
416 "mov %0, %%cr8"
418 : "r" (Data)
422 static __inline__ __attribute__((always_inline)) void __writecr(unsigned long reg, unsigned long Data)
424 switch (reg){
425 case 0:
426 __writecr0 (Data);
427 break;
429 case 2:
430 __writecr2 (Data);
431 break;
433 case 3:
434 __writecr3 (Data);
435 break;
437 case 4:
438 __writecr4 (Data);
439 break;
441 case 8:
442 __writecr8 (Data);
443 break;
445 default:
450 static __inline__ __attribute__((always_inline)) UINT64 __readmsr(UINT32 msr)
452 UINT64 retval;
453 __asm__ __volatile__(
454 "rdmsr"
455 : "=A" (retval)
456 : "c" (msr)
458 return retval;
461 static __inline__ __attribute__((always_inline)) void __writemsr (UINT32 msr, UINT64 Value)
463 __asm__ __volatile__ (
464 "wrmsr"
466 : "c" (msr), "A" (Value)
470 #if !defined(__clang__)
471 static __inline__ __attribute__((always_inline)) UINT64 __rdtsc(void)
473 UINT64 retval;
474 __asm__ __volatile__ (
475 "rdtsc"
476 : "=A" (retval));
477 return retval;
479 #endif
481 static __inline__ __attribute__((always_inline)) void __cpuid(int CPUInfo[], const int InfoType)
483 __asm__ __volatile__(
484 "cpuid"
485 :"=a" (CPUInfo[0]), "=b" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3])
486 : "a" (InfoType)
491 static __inline__ __attribute__((always_inline)) void _disable(void)
493 __asm__ __volatile__ ("cli");
497 static __inline__ __attribute__((always_inline)) void _enable(void)
499 __asm__ __volatile__ ("sti");
503 static __inline__ __attribute__((always_inline)) void __halt(void)
505 __asm__ __volatile__ ("hlt");
509 static __inline__ __attribute__((always_inline)) void __debugbreak(void)
511 __asm__ __volatile__ ("int3");
514 static __inline__ __attribute__((always_inline)) void __invd(void)
516 __asm__ __volatile__ ("invd");
519 static __inline__ __attribute__((always_inline)) void __wbinvd(void)
521 __asm__ __volatile__ ("wbinvd");
524 static __inline__ __attribute__((always_inline)) void __lidt(void *Source)
526 __asm__ __volatile__("lidt %0" : : "m"(*(short*)Source));
529 static __inline__ __attribute__((always_inline)) void
530 __writefsbyte(const unsigned long Offset, const uint8_t Data)
532 __asm__ ("movb %[Data], %%fs:%a[Offset]"
534 : [Offset] "ir" (Offset), [Data] "iq" (Data));
537 static __inline__ __attribute__((always_inline)) void
538 __writefsword(const unsigned long Offset, const uint16_t Data)
540 __asm__ ("movw %[Data], %%fs:%a[Offset]"
542 : [Offset] "ir" (Offset), [Data] "ir" (Data));
545 static __inline__ __attribute__((always_inline)) void
546 __writefsdword(const unsigned long Offset, const uint32_t Data)
548 __asm__ ("movl %[Data], %%fs:%a[Offset]"
550 : [Offset] "ir" (Offset), [Data] "ir" (Data));
553 static __inline__ __attribute__((always_inline)) uint8_t
554 __readfsbyte(const unsigned long Offset)
556 unsigned char value;
557 __asm__ ("movb %%fs:%a[Offset], %[value]"
558 : [value] "=q" (value)
559 : [Offset] "ir" (Offset));
560 return value;
563 static __inline__ __attribute__((always_inline)) uint16_t
564 __readfsword(const unsigned long Offset)
566 unsigned short value;
567 __asm__ ("movw %%fs:%a[Offset], %[value]"
568 : [value] "=q" (value)
569 : [Offset] "ir" (Offset));
570 return value;
573 static __inline__ __attribute__((always_inline)) uint32_t
574 __readfsdword(unsigned long Offset)
576 unsigned long value;
577 __asm__ ("mov %%fs:%a[Offset], %[value]"
578 : [value] "=r" (value)
579 : [Offset] "ir" (Offset));
580 return value;
583 #ifdef __SSE3__
584 typedef long long __v2di __attribute__((__vector_size__ (16)));
585 typedef long long __m128i __attribute__((__vector_size__ (16), __may_alias__));
587 static __inline__ __attribute__((always_inline)) void _mm_stream_si128_fs2 (void *__A, __m128i __B)
589 __asm__(".byte 0x64"); // fs prefix
590 #if defined(__clang__)
591 __builtin_nontemporal_store((__v2di)__B, (__v2di *)__A);
592 #else
593 __builtin_ia32_movntdq ((__v2di *)__A, (__v2di)__B);
594 #endif
597 static __inline__ __attribute__((always_inline)) void _mm_stream_si128_fs (void *__A, void *__B)
599 __m128i data;
600 data = (__m128i) __builtin_ia32_lddqu ((char const *)__B);
601 _mm_stream_si128_fs2 (__A, data);
604 static __inline__ __attribute__((always_inline)) void _mm_clflush_fs (void *__A)
606 __asm__(".byte 0x64"); // fs prefix
607 __builtin_ia32_clflush (__A);
610 #if !defined(__clang__)
611 static __inline __attribute__(( __always_inline__)) void _mm_mfence (void)
613 __builtin_ia32_mfence ();
615 #else
616 void _mm_mfence(void);
617 #endif
619 #if !defined(__clang__)
620 static __inline __attribute__(( __always_inline__)) void _mm_sfence (void)
622 __builtin_ia32_sfence ();
624 #else
625 void _mm_sfence(void);
626 #endif
627 #endif /* __SSE3__ */
629 #endif /* defined (__GNUC__) */