summaryrefslogtreecommitdiff
path: root/usr/src/uts/i86pc/io/ppm_plat.c
blob: 9cb2d73c79ae1fbbe3b0957d2e32b03178c1ba9d (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
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
/*
 * Copyright (c) 2009, Intel Corporation.
 * All rights reserved.
 */

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

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

#define	PPM_CPU_PSTATE_DOMAIN_FLG	0x100

/*
 * Used by ppm_redefine_topspeed() to set the highest power level of all CPUs
 * in a domain.
 */
void
ppm_set_topspeed(ppm_dev_t *cpup, int speed)
{
	for (cpup = cpup->domp->devlist; cpup != NULL; cpup = cpup->next)
		(*cpupm_set_topspeed_callb)(cpup->dip, speed);
}

/*
 * Redefine the highest power level for all CPUs in a domain. This
 * functionality is necessary because ACPI uses the _PPC to define
 * a CPU's highest power level *and* allows the _PPC to be redefined
 * dynamically. _PPC changes are communicated through _PPC change
 * notifications caught by the CPU device driver.
 */
void
ppm_redefine_topspeed(void *ctx)
{
	char *str = "ppm_redefine_topspeed";
	ppm_dev_t *cpup;
	ppm_dev_t *ncpup;
	int topspeed;
	int newspeed = -1;

	cpup = PPM_GET_PRIVATE((dev_info_t *)ctx);

	if (cpupm_get_topspeed_callb == NULL ||
	    cpupm_set_topspeed_callb == NULL) {
		cmn_err(CE_WARN, "%s: Cannot process request for instance %d "
		    "since cpupm interfaces are not initialized", str,
		    ddi_get_instance(cpup->dip));
		return;
	}

	if (!(cpup->domp->dflags & PPMD_CPU_READY)) {
		PPMD(D_CPU, ("%s: instance %d received _PPC change "
		    "notification before PPMD_CPU_READY", str,
		    ddi_get_instance(cpup->dip)));
		return;
	}

	/*
	 * Process each CPU in the domain.
	 */
	for (ncpup = cpup->domp->devlist; ncpup != NULL; ncpup = ncpup->next) {
		topspeed = (*cpupm_get_topspeed_callb)(ncpup->dip);
		if (newspeed == -1 || topspeed < newspeed)
			newspeed = topspeed;
	}

	ppm_set_topspeed(cpup, newspeed);
}

/*
 * For x86 platforms CPU domains must be built dynamically at bootime.
 * Until the domains have been built, refuse all power transition
 * requests.
 */
/* ARGSUSED */
boolean_t
ppm_manage_early_cpus(dev_info_t *dip, int new, int *result)
{
	ppm_dev_t *ppmd = PPM_GET_PRIVATE(dip);

	if (!(ppmd->domp->dflags & PPMD_CPU_READY)) {
		PPMD(D_CPU, ("ppm_manage_early_cpus: attempt to manage CPU "
		    "before it was ready dip(0x%p)", (void *)dip));
		return (B_TRUE);
	}
	*result = DDI_FAILURE;
	return (B_FALSE);
}

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 oldlevel;
	int ret;

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

	dip = cpup->dip;
	ASSERT(dip);

	oldlevel = cpup->level;

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

	if (newlevel == oldlevel)
		return (DDI_SUCCESS);

	/* bring each cpu to next level */
	for (; cpup; cpup = cpup->next) {
		ret = pm_power(cpup->dip, 0, newlevel);
		PPMD(D_CPU, ("%s: \"%s\", changed to level %d, ret %d\n",
		    str, cpup->path, newlevel, ret))
		if (ret == DDI_SUCCESS) {
			cpup->level = newlevel;
			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 (newlevel < oldlevel)
			ret = ppm_revert_cpu_power(cpup, oldlevel);

		return (ret);
	}

	return (DDI_SUCCESS);
}

/*
 * allocate ppm CPU pstate domain if non-existence,
 * otherwise, add the CPU to the corresponding ppm
 * CPU pstate domain.
 */
void
ppm_alloc_pstate_domains(cpu_t *cp)
{
	cpupm_mach_state_t	*mach_state;
	uint32_t		pm_domain;
	int			sub_domain;
	ppm_domain_t		*domp;
	dev_info_t		*cpu_dip;
	ppm_db_t		*dbp;
	char			path[MAXNAMELEN];

	mach_state = (cpupm_mach_state_t *)(cp->cpu_m.mcpu_pm_mach_state);
	ASSERT(mach_state);
	pm_domain = mach_state->ms_pstate.cma_domain->pm_domain;

	/*
	 * There are two purposes of sub_domain:
	 * 1. skip the orignal ppm CPU domain generated by ppm.conf
	 * 2. A CPU ppm domain could have several pstate domains indeed.
	 */
	sub_domain = pm_domain | PPM_CPU_PSTATE_DOMAIN_FLG;

	/*
	 * Find ppm CPU pstate domain
	 */
	for (domp = ppm_domain_p; domp; domp = domp->next) {
		if ((domp->model == PPMD_CPU) &&
		    (domp->sub_domain == sub_domain)) {
			break;
		}
	}

	/*
	 * Create one ppm CPU pstate domain if no found
	 */
	if (domp == NULL) {
		domp = kmem_zalloc(sizeof (*domp), KM_SLEEP);
		mutex_init(&domp->lock, NULL, MUTEX_DRIVER, NULL);
		mutex_enter(&domp->lock);
		domp->name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
		(void) snprintf(domp->name, MAXNAMELEN, "cpu_pstate_domain_%d",
		    pm_domain);
		domp->sub_domain = sub_domain;
		domp->dflags = PPMD_LOCK_ALL | PPMD_CPU_READY;
		domp->pwr_cnt = 0;
		domp->pwr_cnt++;
		domp->propname = NULL;
		domp->model = PPMD_CPU;
		domp->status = PPMD_ON;
		cpu_dip = mach_state->ms_dip;
		(void) ddi_pathname(cpu_dip, path);
		dbp = kmem_zalloc(sizeof (struct ppm_db), KM_SLEEP);
		dbp->name = kmem_zalloc((strlen(path) + 1),
		    KM_SLEEP);
		(void) strcpy(dbp->name, path);
		dbp->next = domp->conflist;
		domp->conflist = dbp;
		domp->next = ppm_domain_p;
		ppm_domain_p = domp;
		mutex_exit(&domp->lock);
	}
	/*
	 * We found one matched ppm CPU pstate domain,
	 * add cpu to this domain
	 */
	else {
		mutex_enter(&domp->lock);
		cpu_dip = mach_state->ms_dip;
		(void) ddi_pathname(cpu_dip, path);
		dbp = kmem_zalloc(sizeof (struct ppm_db), KM_SLEEP);
		dbp->name = kmem_zalloc((strlen(path) + 1),
		    KM_SLEEP);
		(void) strcpy(dbp->name, path);
		dbp->next = domp->conflist;
		domp->conflist = dbp;
		domp->pwr_cnt++;
		mutex_exit(&domp->lock);
	}
}

/*
 * remove CPU from the corresponding ppm CPU pstate
 * domain. We only remove CPU from conflist here.
 */
void
ppm_free_pstate_domains(cpu_t *cp)
{
	cpupm_mach_state_t	*mach_state;
	ppm_domain_t		*domp;
	ppm_dev_t		*devp;
	dev_info_t		*cpu_dip;
	ppm_db_t		**dbpp, *pconf;
	char			path[MAXNAMELEN];

	mach_state = (cpupm_mach_state_t *)(cp->cpu_m.mcpu_pm_mach_state);
	ASSERT(mach_state);
	cpu_dip = mach_state->ms_dip;
	(void) ddi_pathname(cpu_dip, path);

	/*
	 * get ppm CPU pstate domain
	 */
	devp = PPM_GET_PRIVATE(cpu_dip);
	ASSERT(devp);
	domp = devp->domp;
	ASSERT(domp);

	/*
	 * remove CPU from conflist
	 */
	mutex_enter(&domp->lock);
	for (dbpp = &domp->conflist; (pconf = *dbpp) != NULL; ) {
		if (strcmp(pconf->name, path) != 0) {
			dbpp = &pconf->next;
			continue;
		}
		*dbpp = pconf->next;
		kmem_free(pconf->name, strlen(pconf->name) + 1);
		kmem_free(pconf, sizeof (*pconf));
	}
	mutex_exit(&domp->lock);
}