summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4u/io/ppm_plat.c
blob: fd3f307b83e68570c740c67188e289af9f9be000 (plain)
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Platform Power Management master pseudo driver platform support.
 */

#include <sys/file.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/ppmvar.h>

/*
 * This flag disables vcore/vid feature by default.
 */
uint_t	ppm_do_vcore = 0;

/*
 * PPMDC_CPU_NEXT operation
 */
static int
ppm_cpu_next(ppm_domain_t *domp, int level)
{
#ifdef DEBUG
	char *str = "ppm_cpu_next";
#endif
	ppm_dc_t *dc;
	int index = level - 1;
	int ret = 0;

	dc = ppm_lookup_dc(domp, PPMDC_CPU_NEXT);
	for (; dc && (dc->cmd == PPMDC_CPU_NEXT); dc = dc->next) {
		switch (dc->method) {
		case PPMDC_CPUSPEEDKIO:
			ret = ldi_ioctl(dc->lh, dc->m_un.cpu.iowr,
			    (intptr_t)index, FWRITE | FKIOCTL, kcred, NULL);
			if (ret)
				return (ret);
			break;

		default:
			PPMD(D_CPU, ("%s: unsupported method(0x%x)\n",
			    str, dc->method))
			return (-1);
		}
	}
	return (ret);
}

/*
 * PPMDC_PRE_CHNG operation
 */
static int
ppm_cpu_pre_chng(ppm_domain_t *domp, int oldl, int speedup)
{
#ifdef DEBUG
	char *str = "ppm_cpu_pre_chng";
#endif
	ppm_dc_t *dc;
	int lowest;
	int ret = 0;

	dc = ppm_lookup_dc(domp, PPMDC_PRE_CHNG);
	for (; dc && (dc->cmd == PPMDC_PRE_CHNG); dc = dc->next) {

		switch (dc->method) {
		case PPMDC_VCORE:
			lowest = domp->devlist->lowest;
			if ((oldl != lowest) || (speedup != 1))
				break;

			/* raise core voltage */
			if (ppm_do_vcore > 0) {
				ret = ldi_ioctl(dc->lh,
				    dc->m_un.cpu.iowr,
				    (intptr_t)&dc->m_un.cpu.val,
				    FWRITE | FKIOCTL, kcred, NULL);
				if (ret != 0)
					return (ret);
				if (dc->m_un.cpu.delay > 0)
					drv_usecwait(dc->m_un.cpu.delay);
			}
			break;

		default:
			PPMD(D_CPU, ("%s: unsupported method(0x%x)\n",
			    str, dc->method))
			return (-1);
		}
	}

	return (ret);
}

/*
 * PPMDC_CPU_GO operation
 */
/* ARGSUSED */
static int
ppm_cpu_go(ppm_domain_t *domp, int level)
{
	ppm_dc_t *dc;
	int ret = 0;

	dc = ppm_lookup_dc(domp, PPMDC_CPU_GO);
	if (dc == NULL) {
		return (ret);
	}
	switch (dc->method) {
	case PPMDC_KIO:
		ret = ldi_ioctl(dc->lh, dc->m_un.kio.iowr,
		    (intptr_t)dc->m_un.kio.val, FWRITE | FKIOCTL,
		    kcred, NULL);
		break;
	default:
		return (-1);
	}

	return (ret);
}

/*
 * PPMDC_POST_CHNG operation
 */
static int
ppm_cpu_post_chng(ppm_domain_t *domp, int newl, int speedup)
{
#ifdef DEBUG
	char *str = "ppm_cpu_post_chng";
#endif
	ppm_dc_t *dc;
	int	lowest;
	int ret = 0;

	dc = ppm_lookup_dc(domp, PPMDC_POST_CHNG);
	for (; dc && (dc->cmd == PPMDC_POST_CHNG); dc = dc->next) {

		switch (dc->method) {
		case PPMDC_VCORE:
			lowest = domp->devlist->lowest;
			if ((newl != lowest) || (speedup != 0))
				break;

			/* lower core voltage */
			if (ppm_do_vcore > 0) {
				ret = ldi_ioctl(dc->lh,
				    dc->m_un.cpu.iowr,
				    (intptr_t)&dc->m_un.cpu.val,
				    FWRITE | FKIOCTL, kcred, NULL);
				if (ret != 0)
					return (ret);
				if (dc->m_un.cpu.delay > 0)
					drv_usecwait(dc->m_un.cpu.delay);
			}
			break;

		default:
			PPMD(D_CPU, ("%s: unsupported method(0x%x)\n",
			    str, dc->method))
			return (-1);
		}
	}
	return (ret);
}

/*
 * The effective cpu estar model is: program all cpus to be ready to go
 * the same next(or new) speed level, program all other system bus resident
 * devices to the same next speed level.  At last, pull the trigger to
 * initiate the speed change for all system bus resident devices
 * simultaneously.
 *
 * On Excalibur, the Safari bus resident devices are Cheetah/Cheetah+ and
 * Schizo.  On Enchilada, the JBus resident devides are Jalapeno(s) and
 * Tomatillo(s).
 */
int
ppm_change_cpu_power(ppm_dev_t *ppmd, int newlevel)
{
#ifdef DEBUG
	char *str = "ppm_change_cpu_power";
#endif
	ppm_unit_t *unitp;
	ppm_domain_t *domp;
	ppm_dev_t *cpup;
	dev_info_t *dip;
	int level, oldlevel;
	int speedup, incr, lowest, highest;
	char *chstr;
	int ret;

	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
	ASSERT(unitp);
	domp = ppmd->domp;
	cpup = domp->devlist;
	lowest = cpup->lowest;
	highest = cpup->highest;

	/*
	 * Not all cpus may have transitioned to a known level by this time
	 */
	oldlevel = (cpup->level == PM_LEVEL_UNKNOWN) ? highest : cpup->level;
	dip = cpup->dip;
	ASSERT(dip);

	PPMD(D_CPU, ("%s: old %d, new %d, highest %d, lowest %d\n",
	    str, oldlevel, newlevel, highest, lowest))

	if (newlevel > oldlevel) {
		chstr = "UP";
		speedup = 1;
		incr = 1;
	} else if (newlevel < oldlevel) {
		chstr = "DOWN";
		speedup = 0;
		incr = -1;
	} else
		return (DDI_SUCCESS);

	/*
	 * This loop will execute 1x or 2x depending on
	 * number of times we need to change clock rates
	 */
	for (level = oldlevel+incr; level != newlevel+incr; level += incr) {
		/* bring each cpu to next level */
		for (; cpup; cpup = cpup->next) {
			if (cpup->level == level)
				continue;

			ret = pm_power(cpup->dip, 0, level);
			PPMD(D_CPU, ("%s: \"%s\", %s to level %d, ret %d\n",
			    str, cpup->path, chstr, level, ret))
			if (ret == DDI_SUCCESS) {
				cpup->level = level;
				cpup->rplvl = PM_LEVEL_UNKNOWN;
				continue;
			}

			/*
			 * if the driver was unable to lower cpu speed,
			 * the cpu probably got busy; set the previous
			 * cpus back to the original level
			 */
			if (speedup == 0)
				ret = ppm_revert_cpu_power(cpup, level - incr);
			return (ret);
		}
		cpup = domp->devlist;

		/*
		 * set bus resident devices at next speed level
		 */
		ret = ppm_cpu_next(domp, level);
		if (ret != 0) {
			(void) ppm_revert_cpu_power(cpup, level - incr);
			return (ret);
		}

		/*
		 * platform dependent various operations before
		 * initiating cpu speed change
		 */
		ret = ppm_cpu_pre_chng(domp, level - incr, speedup);
		if (ret != 0) {
			(void) ppm_revert_cpu_power(cpup, level - incr);
			(void) ppm_cpu_next(domp, level - incr);
			return (ret);
		}

		/*
		 * the following 1us delay is actually required for us3i only.
		 * on us3i system, entering estar mode from full requires
		 * to set mcu to single fsm state followed by 1us delay
		 * before trigger actual transition.  The mcu part is
		 * handled in us_drv, the delay is here.
		 */
		if ((oldlevel == highest) && (speedup == 0))
			drv_usecwait(1);

		/*
		 * initiate cpu speed change
		 */
		ret = ppm_cpu_go(domp, level);
		if (ret != 0) {
			(void) ppm_revert_cpu_power(cpup, level - incr);
			(void) ppm_cpu_next(domp, level - incr);
			return (ret);
		}

		/*
		 * platform dependent operations post cpu speed change
		 */
		ret = ppm_cpu_post_chng(domp, level, speedup);
		if (ret != 0)
			return (ret);

	}   /* end of looping each level */

	return (DDI_SUCCESS);
}

/*
 * This handles the power-on case where cpu power level is
 * PM_LEVEL_UNKNOWN.  Per agreement with OBP, cpus always
 * boot up at full speed.  In fact, we must not making calls
 * into tomtppm or schppm to trigger cpu speed change to a
 * different level at early boot time since some cpu may not
 * be ready, causing xc_one() to fail silently.
 *
 * Here we simply call pm_power() to get the power level updated
 * in pm and ppm. Had xc_one() failed silently inside us_power()
 * at this time we're unaffected.
 */
boolean_t
ppm_manage_early_cpus(dev_info_t *dip, int new, int *result)
{
	ppm_dev_t *ppmd = PPM_GET_PRIVATE(dip);
	int ret;

	if (ppmd->level == PM_LEVEL_UNKNOWN && new == ppmd->highest) {
		ret = pm_power(dip, 0, new);
		if (ret != DDI_SUCCESS) {
			PPMD(D_CPU, ("ppm_manage_early_cpus: pm_power() "
			    "failed to change power level to %d", new))
		} else {
			ppmd->level = new;
			ppmd->rplvl = PM_LEVEL_UNKNOWN;
		}
		*result = ret;
		return (B_TRUE);
	}
	return (B_FALSE);
}