cpu.c revision 1.15 1 /* $NetBSD: cpu.c,v 1.15 2017/04/08 17:40:50 scole Exp $ */
2
3 /*
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 *
8 * Author:
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.15 2017/04/08 17:40:50 scole Exp $");
34
35 #include <sys/param.h>
36 #include <sys/cpu.h>
37 #include <sys/proc.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/kmem.h>
41
42 #include <dev/acpi/acpica.h>
43 #include <dev/acpi/acpivar.h>
44
45 #define MHz 1000000L
46 #define GHz (1000L * MHz)
47
48 extern int ia64_sync_icache_needed;
49
50 struct cpu_info cpu_info_primary __aligned(CACHE_LINE_SIZE);
51 struct cpu_info *cpu_info_list = &cpu_info_primary;
52
53 struct cpu_softc {
54 device_t sc_dev; /* device tree glue */
55 struct cpu_info *sc_info; /* pointer to CPU info */
56 };
57
58 static int cpu_match(device_t, cfdata_t, void *);
59 static void cpu_attach(device_t, device_t, void *);
60
61 static void identifycpu(struct cpu_softc *);
62
63 CFATTACH_DECL_NEW(cpu, sizeof(struct cpu_softc),
64 cpu_match, cpu_attach, NULL, NULL);
65
66
67 static int
68 cpu_match(device_t parent, cfdata_t match, void *aux)
69 {
70
71 return 1;
72 }
73
74 static void
75 cpu_attach(device_t parent, device_t self, void *aux)
76 {
77 struct cpu_softc *sc = device_private(self);
78 ACPI_MADT_LOCAL_SAPIC *sapic = (ACPI_MADT_LOCAL_SAPIC *)aux;
79 struct cpu_info *ci;
80 uint64_t lid;
81 int id, eid;
82
83 aprint_naive("\n");
84 aprint_normal(": ProcessorID %d, Id %d, Eid %d%s\n",
85 sapic->ProcessorId, sapic->Id, sapic->Eid,
86 sapic->LapicFlags & ACPI_MADT_ENABLED ? "" : " (disabled)");
87
88 /* Get current CPU Id */
89 lid = ia64_get_lid();
90 id = (lid & 0x00000000ff000000) >> 24;
91 eid = (lid & 0x0000000000ff0000) >> 16;
92
93 sc->sc_dev = self;
94 if (id == sapic->Id && eid == sapic->Eid)
95 ci = curcpu();
96 else {
97 ci = (struct cpu_info *)kmem_zalloc(sizeof(*ci), KM_NOSLEEP);
98 if (ci == NULL) {
99 aprint_error_dev(self, "memory alloc failed\n");
100 return;
101 }
102 }
103 sc->sc_info = ci;
104
105 ci->ci_cpuid = sapic->ProcessorId;
106 ci->ci_intrdepth = -1; /* need ? */
107 ci->ci_dev = self;
108
109 identifycpu(sc);
110
111 return;
112 }
113
114
115 static void
116 identifycpu(struct cpu_softc *sc)
117 {
118 uint64_t vendor[3];
119 const char *family_name, *model_name;
120 uint64_t features, tmp;
121 int revision, model, family;
122 char bitbuf[32];
123 extern uint64_t processor_frequency;
124
125 /*
126 * Assumes little-endian.
127 */
128 vendor[0] = ia64_get_cpuid(0);
129 vendor[1] = ia64_get_cpuid(1);
130 vendor[2] = '\0';
131
132 tmp = ia64_get_cpuid(3);
133 /* number = (tmp >> 0) & 0xff; */
134 revision = (tmp >> 8) & 0xff;
135 model = (tmp >> 16) & 0xff;
136 family = (tmp >> 24) & 0xff;
137 /* archrev = (tmp >> 32) & 0xff; */
138
139 family_name = model_name = "unknown";
140 switch (family) {
141 case 0x07:
142 family_name = "Itanium";
143 model_name = "Merced";
144 break;
145 case 0x1f:
146 family_name = "Itanium 2";
147 switch (model) {
148 case 0x00:
149 model_name = "McKinley";
150 break;
151 case 0x01:
152 /*
153 * Deerfield is a low-voltage variant based on the
154 * Madison core. We need circumstantial evidence
155 * (i.e. the clock frequency) to identify those.
156 * Allow for roughly 1% error margin.
157 */
158 tmp = processor_frequency >> 7;
159 if ((processor_frequency - tmp) < 1*GHz &&
160 (processor_frequency + tmp) >= 1*GHz)
161 model_name = "Deerfield";
162 else
163 model_name = "Madison";
164 break;
165 case 0x02:
166 model_name = "Madison II";
167 break;
168 }
169 break;
170 case 0x20:
171 ia64_sync_icache_needed = 1;
172
173 family_name = "Itanium 2";
174 switch (model) {
175 case 0x00:
176 model_name = "Montecito";
177 break;
178 case 0x01:
179 model_name = "Montvale";
180 break;
181 }
182 break;
183 }
184 cpu_setmodel("%s", model_name);
185
186 features = ia64_get_cpuid(4);
187
188 aprint_normal_dev(sc->sc_dev, "%s (", model_name);
189 if (processor_frequency) {
190 aprint_normal("%ld.%02ld-MHz ",
191 (processor_frequency + 4999) / MHz,
192 ((processor_frequency + 4999) / (MHz/100)) % 100);
193 }
194 aprint_normal("%s)\n", family_name);
195 aprint_normal_dev(sc->sc_dev, "Origin \"%s\", Revision %d\n",
196 (char *)vendor, revision);
197
198 #define IA64_FEATURES_BITMASK "\177\020" \
199 "b\0LB\0" /* 'brl' instruction is implemented */ \
200 "b\1SD\0" /* Processor implements sportaneous deferral */ \
201 "b\2AO\0" /* Processor implements 16-byte atomic operations */ \
202 "\0"
203 snprintb(bitbuf, sizeof(bitbuf), IA64_FEATURES_BITMASK, features);
204 aprint_normal_dev(sc->sc_dev, "Features %s\n", bitbuf);
205 }
206