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