65a14603f5c8b3d78852fd53fd2d4b386fd2f6c2
[coreboot.git] / src / include / cpu / p6 / apic.h
1 #ifndef APIC_H
2 #define APIC_H
3
4 #define APIC_BASE_MSR 0x1B
5 #define APIC_BASE_MSR_BOOTSTRAP_PROCESSOR (1 << 8)
6 #define APIC_BASE_MSR_ENABLE (1 << 11)
7 #define APIC_BASE_MSR_ADDR_MASK 0xFFFFF000
8
9 #define APIC_DEFAULT_BASE 0xfee00000
10
11 #define APIC_ID         0x020
12 #define APIC_LVR        0x030
13 #define APIC_TASKPRI    0x80
14 #define         APIC_TPRI_MASK          0xFF
15 #define APIC_ARBID      0x090
16 #define APIC_RRR        0x0C0
17 #define APIC_SVR        0x0f0
18 #define APIC_SPIV       0x0f0
19 #define         APIC_SPIV_ENABLE  0x100
20 #define APIC_ESR        0x280
21 #define         APIC_ESR_SEND_CS        0x00001
22 #define         APIC_ESR_RECV_CS        0x00002
23 #define         APIC_ESR_SEND_ACC       0x00004
24 #define         APIC_ESR_RECV_ACC       0x00008
25 #define         APIC_ESR_SENDILL        0x00020
26 #define         APIC_ESR_RECVILL        0x00040
27 #define         APIC_ESR_ILLREGA        0x00080
28 #define APIC_ICR        0x300
29 #define         APIC_DEST_SELF          0x40000
30 #define         APIC_DEST_ALLINC        0x80000
31 #define         APIC_DEST_ALLBUT        0xC0000
32 #define         APIC_ICR_RR_MASK        0x30000
33 #define         APIC_ICR_RR_INVALID     0x00000
34 #define         APIC_ICR_RR_INPROG      0x10000
35 #define         APIC_ICR_RR_VALID       0x20000
36 #define         APIC_INT_LEVELTRIG      0x08000
37 #define         APIC_INT_ASSERT         0x04000
38 #define         APIC_ICR_BUSY           0x01000
39 #define         APIC_DEST_LOGICAL       0x00800
40 #define         APIC_DM_FIXED           0x00000
41 #define         APIC_DM_LOWEST          0x00100
42 #define         APIC_DM_SMI             0x00200
43 #define         APIC_DM_REMRD           0x00300
44 #define         APIC_DM_NMI             0x00400
45 #define         APIC_DM_INIT            0x00500
46 #define         APIC_DM_STARTUP         0x00600
47 #define         APIC_DM_EXTINT          0x00700
48 #define         APIC_VECTOR_MASK        0x000FF
49 #define APIC_ICR2       0x310
50 #define         GET_APIC_DEST_FIELD(x)  (((x)>>24)&0xFF)
51 #define         SET_APIC_DEST_FIELD(x)  ((x)<<24)
52 #define APIC_LVTT       0x320
53 #define APIC_LVTPC      0x340
54 #define APIC_LVT0       0x350
55 #define         APIC_LVT_TIMER_BASE_MASK        (0x3<<18)
56 #define         GET_APIC_TIMER_BASE(x)          (((x)>>18)&0x3)
57 #define         SET_APIC_TIMER_BASE(x)          (((x)<<18))
58 #define         APIC_TIMER_BASE_CLKIN           0x0
59 #define         APIC_TIMER_BASE_TMBASE          0x1
60 #define         APIC_TIMER_BASE_DIV             0x2
61 #define         APIC_LVT_TIMER_PERIODIC         (1<<17)
62 #define         APIC_LVT_MASKED                 (1<<16)
63 #define         APIC_LVT_LEVEL_TRIGGER          (1<<15)
64 #define         APIC_LVT_REMOTE_IRR             (1<<14)
65 #define         APIC_INPUT_POLARITY             (1<<13)
66 #define         APIC_SEND_PENDING               (1<<12)
67 #define         APIC_LVT_RESERVED_1             (1<<11)
68 #define         APIC_DELIVERY_MODE_MASK         (7<<8)
69 #define         APIC_DELIVERY_MODE_FIXED        (0<<8)
70 #define         APIC_DELIVERY_MODE_NMI          (4<<8)
71 #define         APIC_DELIVERY_MODE_EXTINT       (7<<8)
72 #define         GET_APIC_DELIVERY_MODE(x)       (((x)>>8)&0x7)
73 #define         SET_APIC_DELIVERY_MODE(x,y)     (((x)&~0x700)|((y)<<8))
74 #define                 APIC_MODE_FIXED         0x0
75 #define                 APIC_MODE_NMI           0x4
76 #define                 APIC_MODE_EXINT         0x7
77 #define APIC_LVT1       0x360
78 #define APIC_LVTERR     0x370
79
80
81 #if !defined(ASSEMBLY)
82
83 #include <console/console.h>
84
85
86 #define xchg(ptr,v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
87
88 struct __xchg_dummy { unsigned long a[100]; };
89 #define __xg(x) ((struct __xchg_dummy *)(x))
90
91 /*
92  * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
93  * Note 2: xchg has side effect, so that attribute volatile is necessary,
94  *        but generally the primitive is invalid, *ptr is output argument. --ANK
95  */
96 static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
97 {
98         switch (size) {
99                 case 1:
100                         __asm__ __volatile__("xchgb %b0,%1"
101                                 :"=q" (x)
102                                 :"m" (*__xg(ptr)), "0" (x)
103                                 :"memory");
104                         break;
105                 case 2:
106                         __asm__ __volatile__("xchgw %w0,%1"
107                                 :"=r" (x)
108                                 :"m" (*__xg(ptr)), "0" (x)
109                                 :"memory");
110                         break;
111                 case 4:
112                         __asm__ __volatile__("xchgl %0,%1"
113                                 :"=r" (x)
114                                 :"m" (*__xg(ptr)), "0" (x)
115                                 :"memory");
116                         break;
117         }
118         return x;
119 }
120
121
122 static inline unsigned long apic_read(unsigned long reg)
123 {
124         return *((volatile unsigned long *)(APIC_DEFAULT_BASE+reg));
125 }
126
127 extern inline void apic_write_atomic(unsigned long reg, unsigned long v)
128 {
129         xchg((volatile unsigned long *)(APIC_DEFAULT_BASE+reg), v);
130 }
131
132 static inline void apic_write(unsigned long reg, unsigned long v)
133 {
134         *((volatile unsigned long *)(APIC_DEFAULT_BASE+reg)) = v;
135 }
136
137 static inline void apic_wait_icr_idle(void)
138 {
139         do { } while ( apic_read( APIC_ICR ) & APIC_ICR_BUSY );
140 }
141
142 #ifdef CONFIG_X86_GOOD_APIC
143 # define FORCE_READ_AROUND_WRITE 0
144 # define apic_read_around(x) apic_read(x)
145 # define apic_write_around(x,y) apic_write((x),(y))
146 #else
147 # define FORCE_READ_AROUND_WRITE 1
148 # define apic_read_around(x) apic_read(x)
149 # define apic_write_around(x,y) apic_write_atomic((x),(y))
150 #endif
151
152 static inline int apic_remote_read(int apicid, int reg, unsigned long *pvalue)
153 {
154         int timeout;
155         unsigned long status;
156         int result;
157         apic_wait_icr_idle();
158         apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
159         apic_write_around(APIC_ICR, APIC_DM_REMRD | (reg >> 4));
160         timeout = 0;
161         do {
162 #if 0
163                 udelay(100);
164 #endif
165                 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
166         } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
167
168         result = -1;
169         if (status == APIC_ICR_RR_VALID) {
170                 *pvalue = apic_read(APIC_RRR);
171                 result = 0;
172         }
173         return result;
174 }
175 #endif /* ASSEMBLY */
176
177 #endif /* APIC_H */