blob: 30d23130eba3fe0034d44b7843d1f79cda2325de [file] [log] [blame]
Simon Glass0c9075e2014-11-24 21:18:15 -07001/*
2 * From coreboot file of same name
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 * Copyright (C) 2014 Google, Inc
6 *
7 * SPDX-License-Identifier: GPL-2.0
8 */
9
10#include <common.h>
Bin Menga2d73fd2015-06-23 12:18:50 +080011#include <asm/io.h>
Simon Glass0c9075e2014-11-24 21:18:15 -070012#include <asm/lapic.h>
Bin Menga2d73fd2015-06-23 12:18:50 +080013#include <asm/msr.h>
14#include <asm/msr-index.h>
Simon Glass0c9075e2014-11-24 21:18:15 -070015#include <asm/post.h>
16
Bin Menga2d73fd2015-06-23 12:18:50 +080017unsigned long lapic_read(unsigned long reg)
18{
19 return readl(LAPIC_DEFAULT_BASE + reg);
20}
21
22#define xchg(ptr, v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v), (ptr), \
23 sizeof(*(ptr))))
24
25struct __xchg_dummy { unsigned long a[100]; };
26#define __xg(x) ((struct __xchg_dummy *)(x))
27
28/*
29 * Note: no "lock" prefix even on SMP. xchg always implies lock anyway.
30 *
31 * Note 2: xchg has side effect, so that attribute volatile is necessary,
32 * but generally the primitive is invalid, *ptr is output argument.
33 */
34static inline unsigned long __xchg(unsigned long x, volatile void *ptr,
35 int size)
36{
37 switch (size) {
38 case 1:
39 __asm__ __volatile__("xchgb %b0,%1"
40 : "=q" (x)
41 : "m" (*__xg(ptr)), "0" (x)
42 : "memory");
43 break;
44 case 2:
45 __asm__ __volatile__("xchgw %w0,%1"
46 : "=r" (x)
47 : "m" (*__xg(ptr)), "0" (x)
48 : "memory");
49 break;
50 case 4:
51 __asm__ __volatile__("xchgl %0,%1"
52 : "=r" (x)
53 : "m" (*__xg(ptr)), "0" (x)
54 : "memory");
55 break;
56 }
57
58 return x;
59}
60
61void lapic_write(unsigned long reg, unsigned long v)
62{
63 (void)xchg((volatile unsigned long *)(LAPIC_DEFAULT_BASE + reg), v);
64}
65
66void enable_lapic(void)
67{
68 msr_t msr;
69
70 msr = msr_read(MSR_IA32_APICBASE);
71 msr.hi &= 0xffffff00;
72 msr.lo |= MSR_IA32_APICBASE_ENABLE;
73 msr.lo &= ~MSR_IA32_APICBASE_BASE;
74 msr.lo |= LAPIC_DEFAULT_BASE;
75 msr_write(MSR_IA32_APICBASE, msr);
76}
77
78void disable_lapic(void)
79{
80 msr_t msr;
81
82 msr = msr_read(MSR_IA32_APICBASE);
83 msr.lo &= ~MSR_IA32_APICBASE_ENABLE;
84 msr_write(MSR_IA32_APICBASE, msr);
85}
86
87unsigned long lapicid(void)
88{
89 return lapic_read(LAPIC_ID) >> 24;
90}
91
92static void lapic_wait_icr_idle(void)
93{
94 do { } while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY);
95}
96
97int lapic_remote_read(int apicid, int reg, unsigned long *pvalue)
98{
99 int timeout;
100 unsigned long status;
101 int result;
102
103 lapic_wait_icr_idle();
104 lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
105 lapic_write(LAPIC_ICR, LAPIC_DM_REMRD | (reg >> 4));
106
107 timeout = 0;
108 do {
109 status = lapic_read(LAPIC_ICR) & LAPIC_ICR_RR_MASK;
110 } while (status == LAPIC_ICR_RR_INPROG && timeout++ < 1000);
111
112 result = -1;
113 if (status == LAPIC_ICR_RR_VALID) {
114 *pvalue = lapic_read(LAPIC_RRR);
115 result = 0;
116 }
117
118 return result;
119}
120
Simon Glass0c9075e2014-11-24 21:18:15 -0700121void lapic_setup(void)
122{
Bin Meng63d54a62015-06-17 11:15:38 +0800123#ifdef CONFIG_SMP
Simon Glass0c9075e2014-11-24 21:18:15 -0700124 /* Only Pentium Pro and later have those MSR stuff */
125 debug("Setting up local apic: ");
126
127 /* Enable the local apic */
128 enable_lapic();
129
Bin Meng63d54a62015-06-17 11:15:38 +0800130 /* Set Task Priority to 'accept all' */
Bin Menga2d73fd2015-06-23 12:18:50 +0800131 lapic_write(LAPIC_TASKPRI,
132 lapic_read(LAPIC_TASKPRI) & ~LAPIC_TPRI_MASK);
Simon Glass0c9075e2014-11-24 21:18:15 -0700133
134 /* Put the local apic in virtual wire mode */
Bin Menga2d73fd2015-06-23 12:18:50 +0800135 lapic_write(LAPIC_SPIV, (lapic_read(LAPIC_SPIV) &
136 ~(LAPIC_VECTOR_MASK)) | LAPIC_SPIV_ENABLE);
137 lapic_write(LAPIC_LVT0, (lapic_read(LAPIC_LVT0) &
138 ~(LAPIC_LVT_MASKED | LAPIC_LVT_LEVEL_TRIGGER |
139 LAPIC_LVT_REMOTE_IRR | LAPIC_INPUT_POLARITY |
140 LAPIC_SEND_PENDING | LAPIC_LVT_RESERVED_1 |
141 LAPIC_DELIVERY_MODE_MASK)) |
142 (LAPIC_LVT_REMOTE_IRR | LAPIC_SEND_PENDING |
143 LAPIC_DELIVERY_MODE_EXTINT));
144 lapic_write(LAPIC_LVT1, (lapic_read(LAPIC_LVT1) &
145 ~(LAPIC_LVT_MASKED | LAPIC_LVT_LEVEL_TRIGGER |
146 LAPIC_LVT_REMOTE_IRR | LAPIC_INPUT_POLARITY |
147 LAPIC_SEND_PENDING | LAPIC_LVT_RESERVED_1 |
148 LAPIC_DELIVERY_MODE_MASK)) |
149 (LAPIC_LVT_REMOTE_IRR | LAPIC_SEND_PENDING |
150 LAPIC_DELIVERY_MODE_NMI));
Simon Glass0c9075e2014-11-24 21:18:15 -0700151
152 debug("apic_id: 0x%02lx, ", lapicid());
Bin Meng63d54a62015-06-17 11:15:38 +0800153#else /* !CONFIG_SMP */
Simon Glass0c9075e2014-11-24 21:18:15 -0700154 /* Only Pentium Pro and later have those MSR stuff */
155 debug("Disabling local apic: ");
156 disable_lapic();
Bin Meng63d54a62015-06-17 11:15:38 +0800157#endif /* CONFIG_SMP */
Simon Glass0c9075e2014-11-24 21:18:15 -0700158 debug("done.\n");
159 post_code(POST_LAPIC);
160}