1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/* $OpenBSD: piixpcib.c,v 1.2 2007/05/29 02:40:24 tom Exp $ */
/*
* Copyright (c) 2007 Stefan Sperling <stsp@stsp.in-berlin.de>
* Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*-
* Copyright (c) 2004, 2006 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Minoura Makoto, Matthew R. Green, and Jared D. McNeill.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Special driver for the Intel PIIX4 bridges that attaches
* instead of pcib(4). In addition to the core pcib(4) functionality this
* driver provides support for Intel SpeedStep technology present in
* some Pentium III CPUs.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/sysctl.h>
#include <machine/bus.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>
#include <machine/cpu.h>
#include <machine/cpufunc.h>
#include <machine/kvm86.h>
/* 0x47534943 == "ISGE" ('Intel Speedstep Gate E') */
#define PIIXPCIB_ISGE 0x47534943
#define PIIXPCIB_IST_CALL 0x0000e980
#define PIIXPCIB_GSIC_CMD 0x82
#define PIIXPCIB_DEFAULT_COMMAND \
((PIIXPCIB_ISGE & 0xffffff00) | (PIIXPCIB_GSIC_CMD & 0xff))
#define PIIXPCIB_DEFAULT_SMI_PORT 0xb2
#define PIIXPCIB_DEFAULT_SMI_DATA 0xb3
#define PIIXPCIB_GETSTATE 1
#define PIIXPCIB_SETSTATE 2
#define PIIXPCIB_SPEEDSTEP_HIGH 0
#define PIIXPCIB_SPEEDSTEP_LOW 1
struct piixpcib_softc {
struct device sc_dev;
int sc_sig;
int sc_smi_port;
int sc_smi_data;
int sc_command;
int sc_flags;
int state;
};
int piixpcib_match(struct device *, void *, void *);
void piixpcib_attach(struct device *, struct device *, void *);
void piixpcib_setperf(int);
int piixpcib_cpuspeed(int *);
int piixpcib_set_ownership(struct piixpcib_softc *);
int piixpcib_configure_speedstep(struct piixpcib_softc *);
int piixpcib_getset_state(struct piixpcib_softc *, int *, int);
void piixpcib_int15_gsic_call(struct piixpcib_softc *);
/* arch/i386/pci/pcib.c */
extern void pcibattach(struct device *, struct device *, void *);
/* arch/i386/i386/machdep.c */
#if !defined(SMALL_KERNEL) && defined(I686_CPU)
extern void p3_update_cpuspeed(void);
#endif
struct cfattach piixpcib_ca = {
sizeof(struct piixpcib_softc),
piixpcib_match,
piixpcib_attach
};
struct cfdriver piixpcib_cd = {
NULL, "piixpcib", DV_DULL
};
struct piixpcib_softc *piixpcib_sc;
extern int setperf_prio;
const struct pci_matchid piixpcib_devices[] = {
{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82371AB_ISA}, /* PIIX4 */
{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82440MX_PM}, /* PIIX4 in MX440 */
};
void
piixpcib_int15_gsic_call(struct piixpcib_softc *sc)
{
struct kvm86regs regs;
int cmd;
memset(®s, 0, sizeof(struct kvm86regs));
regs.eax = PIIXPCIB_IST_CALL;
regs.edx = PIIXPCIB_ISGE;
kvm86_simplecall(0x15, ®s);
if (regs.eax == PIIXPCIB_ISGE) {
sc->sc_sig = regs.eax;
sc->sc_smi_port = regs.ebx & 0xff;
cmd = (regs.ebx >> 16) & 0xff;
/* GSIC may return cmd 0x80, should be PIIXPCIB_GSIC_CMD */
if (cmd == 0x80)
cmd = PIIXPCIB_GSIC_CMD;
sc->sc_command = (sc->sc_sig & 0xffffff00) | (cmd & 0xff);
sc->sc_smi_data = regs.ecx;
sc->sc_flags = regs.edx;
}
}
int
piixpcib_set_ownership(struct piixpcib_softc *sc)
{
int rv;
paddr_t pmagic;
char magic[] = "Copyright (c) 1999 Intel Corporation";
pmap_extract(pmap_kernel(), (vaddr_t)magic, &pmagic);
__asm __volatile(
"movl $0, %%edi\n\t"
"out %%al, (%%dx)\n"
: "=D" (rv)
: "a" (sc->sc_command),
"b" (0),
"c" (0),
"d" (sc->sc_smi_port),
"S" (pmagic)
);
return (rv ? ENXIO : 0);
}
int
piixpcib_configure_speedstep(struct piixpcib_softc *sc)
{
int rv;
sc->sc_sig = -1;
/* setup some defaults */
sc->sc_smi_port = PIIXPCIB_DEFAULT_SMI_PORT;
sc->sc_smi_data = PIIXPCIB_DEFAULT_SMI_DATA;
sc->sc_command = PIIXPCIB_DEFAULT_COMMAND;
sc->sc_flags = 0;
piixpcib_int15_gsic_call(sc);
/* If signature doesn't match, bail out */
if (sc->sc_sig != PIIXPCIB_ISGE)
return ENODEV;
if (piixpcib_set_ownership(sc) != 0) {
printf(": unable to claim ownership from BIOS, "
"SpeedStep disabled");
return ENXIO;
}
rv = piixpcib_getset_state(sc, &sc->state, PIIXPCIB_GETSTATE);
if (rv != 0) {
printf(": cannot determine CPU power state, "
"SpeedStep disabled");
return ENXIO;
}
/* save the sc for IO tag/handle */
piixpcib_sc = sc;
return 0;
}
int
piixpcib_match(struct device *parent, void *match, void *aux)
{
if (pci_matchbyid((struct pci_attach_args *)aux, piixpcib_devices,
sizeof(piixpcib_devices) / sizeof(piixpcib_devices[0])))
return (2); /* supersede pcib(4) */
return (0);
}
void
piixpcib_attach(struct device *parent, struct device *self, void *aux)
{
struct piixpcib_softc *sc = (struct piixpcib_softc *)self;
if (setperf_prio < 2) {
/* Set up SpeedStep. */
if (piixpcib_configure_speedstep(sc) == 0) {
printf(": SpeedStep");
/* Hook into hw.setperf sysctl */
cpu_setperf = piixpcib_setperf;
setperf_prio = 2;
}
}
/* Provide core pcib(4) functionality */
pcibattach(parent, self, aux);
}
int
piixpcib_getset_state(struct piixpcib_softc *sc, int *state, int function)
{
int new_state;
int rv;
int eax;
#ifdef DIAGNOSTIC
if (function != PIIXPCIB_GETSTATE &&
function != PIIXPCIB_SETSTATE) {
printf("%s: %s called with invalid function %d\n",
sc->sc_dev.dv_xname, __func__, function);
return EINVAL;
}
#endif
__asm __volatile(
"movl $0, %%edi\n\t"
"out %%al, (%%dx)\n"
: "=a" (eax),
"=b" (new_state),
"=D" (rv)
: "a" (sc->sc_command),
"b" (function),
"c" (*state),
"d" (sc->sc_smi_port),
"S" (0)
);
*state = new_state & 1;
switch (function) {
case PIIXPCIB_GETSTATE:
if (eax)
return ENXIO;
break;
case PIIXPCIB_SETSTATE:
if (rv)
return ENXIO;
break;
}
return 0;
}
void
piixpcib_setperf(int level)
{
struct piixpcib_softc *sc;
int new_state;
int tries, rv, s;
sc = piixpcib_sc;
#ifdef DIAGNOSTIC
if (sc == NULL) {
printf("%s: no cookie", __func__);
return;
}
#endif
/* Only two states are available */
if (level <= 50)
new_state = PIIXPCIB_SPEEDSTEP_LOW;
else
new_state = PIIXPCIB_SPEEDSTEP_HIGH;
if (sc->state == new_state)
return;
tries = 5;
s = splhigh();
do {
rv = piixpcib_getset_state(sc, &new_state,
PIIXPCIB_SETSTATE);
if (rv)
delay(200);
} while (rv && --tries);
splx(s);
#ifdef DIAGNOSTIC
if (rv)
printf("%s: setting CPU power state failed",
sc->sc_dev.dv_xname);
#endif
sc->state = new_state;
/* Force update of hw.cpuspeed.
*
* XXX: First generation SpeedStep is only present in some
* Pentium III CPUs and we are lacking a reliable method to
* determine CPU freqs corresponding to low and high power state.
*
* And yes, I've tried it the way the Linux speedstep-smi
* driver does it, thank you very much. It doesn't work
* half the time (my machine has more than 4Mhz ;-) and
* even crashes some machines without specific workarounds.
*
* So call p3_update_cpuspeed() from arch/i386/i386/machdep.c
* instead. Works fine on my Thinkpad X21.
*
* BUT: Apparently, if the bus is busy, the transition may be
* delayed and retried under control of evil SMIs we cannot
* control. So busy-wait a short while before updating hw.cpuspeed
* to decrease chances of picking up the old CPU speed.
* There seems to be no reliable fix for this.
*/
delay(200);
#if !defined(SMALL_KERNEL) && defined(I686_CPU)
p3_update_cpuspeed();
#endif
}
|