cpu.c revision 1.10 1 /* $NetBSD: cpu.c,v 1.10 2011/10/01 15:59:27 chs 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.10 2011/10/01 15:59:27 chs Exp $");
34
35 #include <sys/param.h>
36 #include <sys/proc.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/kmem.h>
40
41 #include <dev/acpi/acpica.h>
42 #include <dev/acpi/acpivar.h>
43
44 #define MHz 1000000L
45 #define GHz (1000L * MHz)
46
47 struct cpu_info cpu_info_primary __aligned(CACHE_LINE_SIZE);
48 struct cpu_info *cpu_info_list = &cpu_info_primary;
49
50 struct cpu_softc {
51 device_t sc_dev; /* device tree glue */
52 struct cpu_info *sc_info; /* pointer to CPU info */
53 };
54
55 char cpu_model[64];
56
57 static int cpu_match(device_t, cfdata_t, void *);
58 static void cpu_attach(device_t, device_t, void *);
59
60 static void identifycpu(struct cpu_softc *);
61
62 CFATTACH_DECL_NEW(cpu, sizeof(struct cpu_softc),
63 cpu_match, cpu_attach, NULL, NULL);
64
65
66 static int
67 cpu_match(device_t parent, cfdata_t match, void *aux)
68 {
69
70 return 1;
71 }
72
73 static void
74 cpu_attach(device_t parent, device_t self, void *aux)
75 {
76 struct cpu_softc *sc = device_private(self);
77 ACPI_MADT_LOCAL_SAPIC *sapic = (ACPI_MADT_LOCAL_SAPIC *)aux;
78 struct cpu_info *ci;
79 uint64_t lid;
80 int id, eid;
81
82 aprint_naive("\n");
83 aprint_normal(": ProcessorID %d, Id %d, Eid %d%s\n",
84 sapic->ProcessorId, sapic->Id, sapic->Eid,
85 sapic->LapicFlags & ACPI_MADT_ENABLED ? "" : " (disabled)");
86
87 /* Get current CPU Id */
88 lid = ia64_get_lid();
89 id = (lid & 0x00000000ff000000) >> 24;
90 eid = (lid & 0x0000000000ff0000) >> 16;
91
92 sc->sc_dev = self;
93 if (id == sapic->Id && eid == sapic->Eid)
94 ci = curcpu();
95 else {
96 ci = (struct cpu_info *)kmem_zalloc(sizeof(*ci), KM_NOSLEEP);
97 if (ci == NULL) {
98 aprint_error_dev(self, "memory alloc failed\n");
99 return;
100 }
101 }
102 sc->sc_info = ci;
103
104 ci->ci_cpuid = sapic->ProcessorId;
105 ci->ci_intrdepth = -1; /* need ? */
106 ci->ci_dev = self;
107
108 identifycpu(sc);
109
110 return;
111 }
112
113
114 static void
115 identifycpu(struct cpu_softc *sc)
116 {
117 uint64_t vendor[3];
118 const char *family_name, *model_name;
119 uint64_t features, tmp;
120 int number, revision, model, family, archrev;
121 extern uint64_t processor_frequency;
122
123 /*
124 * Assumes little-endian.
125 */
126 vendor[0] = ia64_get_cpuid(0);
127 vendor[1] = ia64_get_cpuid(1);
128 vendor[2] = '\0';
129
130 tmp = ia64_get_cpuid(3);
131 number = (tmp >> 0) & 0xff;
132 revision = (tmp >> 8) & 0xff;
133 model = (tmp >> 16) & 0xff;
134 family = (tmp >> 24) & 0xff;
135 archrev = (tmp >> 32) & 0xff;
136
137 family_name = model_name = "unknown";
138 switch (family) {
139 case 0x07:
140 family_name = "Itanium";
141 model_name = "Merced";
142 break;
143 case 0x1f:
144 family_name = "Itanium 2";
145 switch (model) {
146 case 0x00:
147 model_name = "McKinley";
148 break;
149 case 0x01:
150 /*
151 * Deerfield is a low-voltage variant based on the
152 * Madison core. We need circumstantial evidence
153 * (i.e. the clock frequency) to identify those.
154 * Allow for roughly 1% error margin.
155 */
156 tmp = processor_frequency >> 7;
157 if ((processor_frequency - tmp) < 1*GHz &&
158 (processor_frequency + tmp) >= 1*GHz)
159 model_name = "Deerfield";
160 else
161 model_name = "Madison";
162 break;
163 case 0x02:
164 model_name = "Madison II";
165 break;
166 }
167 break;
168 }
169 snprintf(cpu_model, sizeof(cpu_model), "%s", model_name);
170
171 features = ia64_get_cpuid(4);
172
173 aprint_normal_dev(sc->sc_dev, "%s (", model_name);
174 if (processor_frequency) {
175 aprint_normal("%ld.%02ld-MHz ",
176 (processor_frequency + 4999) / MHz,
177 ((processor_frequency + 4999) / (MHz/100)) % 100);
178 }
179 aprint_normal("%s)\n", family_name);
180 aprint_normal_dev(sc->sc_dev, "Origin \"%s\", Revision %d\n",
181 (char *)vendor, revision);
182 aprint_normal_dev(sc->sc_dev, "Features 0x%x\n", (uint32_t)features);
183 }
184