1 /******************************************************************************
2  * xc_pm.c - Libxc API for Xen Power Management (Px/Cx/Tx, etc.) statistic
3  *
4  * Copyright (c) 2008, Liu Jinsong <jinsong.liu@intel.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation;
9  * version 2.1 of the License.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #include <stdbool.h>
22 #include "xc_private.h"
23 
24 #include <xen-tools/libs.h>
25 
26 /*
27  * Get PM statistic info
28  */
xc_pm_get_max_px(xc_interface * xch,int cpuid,int * max_px)29 int xc_pm_get_max_px(xc_interface *xch, int cpuid, int *max_px)
30 {
31     DECLARE_SYSCTL;
32     int ret;
33 
34     sysctl.cmd = XEN_SYSCTL_get_pmstat;
35     sysctl.u.get_pmstat.type = PMSTAT_get_max_px;
36     sysctl.u.get_pmstat.cpuid = cpuid;
37     ret = xc_sysctl(xch, &sysctl);
38     if ( ret )
39         return ret;
40 
41     *max_px = sysctl.u.get_pmstat.u.getpx.total;
42     return ret;
43 }
44 
xc_pm_get_pxstat(xc_interface * xch,int cpuid,struct xc_px_stat * pxpt)45 int xc_pm_get_pxstat(xc_interface *xch, int cpuid, struct xc_px_stat *pxpt)
46 {
47     DECLARE_SYSCTL;
48     /* Sizes unknown until xc_pm_get_max_px */
49     DECLARE_NAMED_HYPERCALL_BOUNCE(trans, pxpt->trans_pt, 0, XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
50     DECLARE_NAMED_HYPERCALL_BOUNCE(pt, pxpt->pt, 0, XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
51 
52     int max_px, ret;
53 
54     if ( !pxpt->trans_pt || !pxpt->pt )
55     {
56         errno = EINVAL;
57         return -1;
58     }
59     if ( (ret = xc_pm_get_max_px(xch, cpuid, &max_px)) != 0)
60         return ret;
61 
62     HYPERCALL_BOUNCE_SET_SIZE(trans, max_px * max_px * sizeof(uint64_t));
63     HYPERCALL_BOUNCE_SET_SIZE(pt, max_px * sizeof(struct xc_px_val));
64 
65     if ( xc_hypercall_bounce_pre(xch, trans) )
66         return ret;
67 
68     if ( xc_hypercall_bounce_pre(xch, pt) )
69     {
70         xc_hypercall_bounce_post(xch, trans);
71         return ret;
72     }
73 
74     sysctl.cmd = XEN_SYSCTL_get_pmstat;
75     sysctl.u.get_pmstat.type = PMSTAT_get_pxstat;
76     sysctl.u.get_pmstat.cpuid = cpuid;
77     sysctl.u.get_pmstat.u.getpx.total = max_px;
78     set_xen_guest_handle(sysctl.u.get_pmstat.u.getpx.trans_pt, trans);
79     set_xen_guest_handle(sysctl.u.get_pmstat.u.getpx.pt, pt);
80 
81     ret = xc_sysctl(xch, &sysctl);
82     if ( ret )
83     {
84 	xc_hypercall_bounce_post(xch, trans);
85 	xc_hypercall_bounce_post(xch, pt);
86         return ret;
87     }
88 
89     pxpt->total = sysctl.u.get_pmstat.u.getpx.total;
90     pxpt->usable = sysctl.u.get_pmstat.u.getpx.usable;
91     pxpt->last = sysctl.u.get_pmstat.u.getpx.last;
92     pxpt->cur = sysctl.u.get_pmstat.u.getpx.cur;
93 
94     xc_hypercall_bounce_post(xch, trans);
95     xc_hypercall_bounce_post(xch, pt);
96 
97     return ret;
98 }
99 
xc_pm_reset_pxstat(xc_interface * xch,int cpuid)100 int xc_pm_reset_pxstat(xc_interface *xch, int cpuid)
101 {
102     DECLARE_SYSCTL;
103 
104     sysctl.cmd = XEN_SYSCTL_get_pmstat;
105     sysctl.u.get_pmstat.type = PMSTAT_reset_pxstat;
106     sysctl.u.get_pmstat.cpuid = cpuid;
107 
108     return xc_sysctl(xch, &sysctl);
109 }
110 
xc_pm_get_max_cx(xc_interface * xch,int cpuid,int * max_cx)111 int xc_pm_get_max_cx(xc_interface *xch, int cpuid, int *max_cx)
112 {
113     DECLARE_SYSCTL;
114     int ret = 0;
115 
116     sysctl.cmd = XEN_SYSCTL_get_pmstat;
117     sysctl.u.get_pmstat.type = PMSTAT_get_max_cx;
118     sysctl.u.get_pmstat.cpuid = cpuid;
119     if ( (ret = xc_sysctl(xch, &sysctl)) != 0 )
120         return ret;
121 
122     *max_cx = sysctl.u.get_pmstat.u.getcx.nr;
123     return ret;
124 }
125 
xc_pm_get_cxstat(xc_interface * xch,int cpuid,struct xc_cx_stat * cxpt)126 int xc_pm_get_cxstat(xc_interface *xch, int cpuid, struct xc_cx_stat *cxpt)
127 {
128     DECLARE_SYSCTL;
129     DECLARE_NAMED_HYPERCALL_BOUNCE(triggers, cxpt->triggers,
130                                    cxpt->nr * sizeof(*cxpt->triggers),
131                                    XC_HYPERCALL_BUFFER_BOUNCE_OUT);
132     DECLARE_NAMED_HYPERCALL_BOUNCE(residencies, cxpt->residencies,
133                                    cxpt->nr * sizeof(*cxpt->residencies),
134                                    XC_HYPERCALL_BUFFER_BOUNCE_OUT);
135     DECLARE_NAMED_HYPERCALL_BOUNCE(pc, cxpt->pc,
136                                    cxpt->nr_pc * sizeof(*cxpt->pc),
137                                    XC_HYPERCALL_BUFFER_BOUNCE_OUT);
138     DECLARE_NAMED_HYPERCALL_BOUNCE(cc, cxpt->cc,
139                                    cxpt->nr_cc * sizeof(*cxpt->cc),
140                                    XC_HYPERCALL_BUFFER_BOUNCE_OUT);
141     int ret = -1;
142 
143     if ( xc_hypercall_bounce_pre(xch, triggers) )
144         goto unlock_0;
145     if ( xc_hypercall_bounce_pre(xch, residencies) )
146         goto unlock_1;
147     if ( xc_hypercall_bounce_pre(xch, pc) )
148         goto unlock_2;
149     if ( xc_hypercall_bounce_pre(xch, cc) )
150         goto unlock_3;
151 
152     sysctl.cmd = XEN_SYSCTL_get_pmstat;
153     sysctl.u.get_pmstat.type = PMSTAT_get_cxstat;
154     sysctl.u.get_pmstat.cpuid = cpuid;
155     sysctl.u.get_pmstat.u.getcx.nr = cxpt->nr;
156     sysctl.u.get_pmstat.u.getcx.nr_pc = cxpt->nr_pc;
157     sysctl.u.get_pmstat.u.getcx.nr_cc = cxpt->nr_cc;
158     set_xen_guest_handle(sysctl.u.get_pmstat.u.getcx.triggers, triggers);
159     set_xen_guest_handle(sysctl.u.get_pmstat.u.getcx.residencies, residencies);
160     set_xen_guest_handle(sysctl.u.get_pmstat.u.getcx.pc, pc);
161     set_xen_guest_handle(sysctl.u.get_pmstat.u.getcx.cc, cc);
162 
163     if ( (ret = xc_sysctl(xch, &sysctl)) )
164         goto unlock_4;
165 
166     cxpt->nr = sysctl.u.get_pmstat.u.getcx.nr;
167     cxpt->last = sysctl.u.get_pmstat.u.getcx.last;
168     cxpt->idle_time = sysctl.u.get_pmstat.u.getcx.idle_time;
169     cxpt->nr_pc = sysctl.u.get_pmstat.u.getcx.nr_pc;
170     cxpt->nr_cc = sysctl.u.get_pmstat.u.getcx.nr_cc;
171 
172 unlock_4:
173     xc_hypercall_bounce_post(xch, cc);
174 unlock_3:
175     xc_hypercall_bounce_post(xch, pc);
176 unlock_2:
177     xc_hypercall_bounce_post(xch, residencies);
178 unlock_1:
179     xc_hypercall_bounce_post(xch, triggers);
180 unlock_0:
181     return ret;
182 }
183 
xc_pm_reset_cxstat(xc_interface * xch,int cpuid)184 int xc_pm_reset_cxstat(xc_interface *xch, int cpuid)
185 {
186     DECLARE_SYSCTL;
187 
188     sysctl.cmd = XEN_SYSCTL_get_pmstat;
189     sysctl.u.get_pmstat.type = PMSTAT_reset_cxstat;
190     sysctl.u.get_pmstat.cpuid = cpuid;
191 
192     return xc_sysctl(xch, &sysctl);
193 }
194 
195 
196 /*
197  * 1. Get PM parameter
198  * 2. Provide user PM control
199  */
xc_get_cpufreq_para(xc_interface * xch,int cpuid,struct xc_get_cpufreq_para * user_para)200 int xc_get_cpufreq_para(xc_interface *xch, int cpuid,
201                         struct xc_get_cpufreq_para *user_para)
202 {
203     DECLARE_SYSCTL;
204     int ret = 0;
205     struct xen_get_cpufreq_para *sys_para = &sysctl.u.pm_op.u.get_para;
206     DECLARE_NAMED_HYPERCALL_BOUNCE(affected_cpus,
207 			 user_para->affected_cpus,
208 			 user_para->cpu_num * sizeof(uint32_t), XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
209     DECLARE_NAMED_HYPERCALL_BOUNCE(scaling_available_frequencies,
210 			 user_para->scaling_available_frequencies,
211 			 user_para->freq_num * sizeof(uint32_t), XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
212     DECLARE_NAMED_HYPERCALL_BOUNCE(scaling_available_governors,
213 			 user_para->scaling_available_governors,
214 			 user_para->gov_num * CPUFREQ_NAME_LEN * sizeof(char), XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
215 
216     bool has_num = user_para->cpu_num &&
217                      user_para->freq_num &&
218                      user_para->gov_num;
219 
220     if ( has_num )
221     {
222         if ( (!user_para->affected_cpus)                    ||
223              (!user_para->scaling_available_frequencies)    ||
224              (!user_para->scaling_available_governors) )
225         {
226             errno = EINVAL;
227             return -1;
228         }
229         if ( xc_hypercall_bounce_pre(xch, affected_cpus) )
230             goto unlock_1;
231         if ( xc_hypercall_bounce_pre(xch, scaling_available_frequencies) )
232             goto unlock_2;
233         if ( xc_hypercall_bounce_pre(xch, scaling_available_governors) )
234             goto unlock_3;
235 
236         set_xen_guest_handle(sys_para->affected_cpus, affected_cpus);
237         set_xen_guest_handle(sys_para->scaling_available_frequencies, scaling_available_frequencies);
238         set_xen_guest_handle(sys_para->scaling_available_governors, scaling_available_governors);
239     }
240 
241     sysctl.cmd = XEN_SYSCTL_pm_op;
242     sysctl.u.pm_op.cmd = GET_CPUFREQ_PARA;
243     sysctl.u.pm_op.cpuid = cpuid;
244     sys_para->cpu_num  = user_para->cpu_num;
245     sys_para->freq_num = user_para->freq_num;
246     sys_para->gov_num  = user_para->gov_num;
247 
248     ret = xc_sysctl(xch, &sysctl);
249     if ( ret )
250     {
251         if ( errno == EAGAIN )
252         {
253             user_para->cpu_num  = sys_para->cpu_num;
254             user_para->freq_num = sys_para->freq_num;
255             user_para->gov_num  = sys_para->gov_num;
256             ret = -errno;
257         }
258 
259         if ( has_num )
260             goto unlock_4;
261         goto unlock_1;
262     }
263     else
264     {
265         user_para->cpuinfo_cur_freq = sys_para->cpuinfo_cur_freq;
266         user_para->cpuinfo_max_freq = sys_para->cpuinfo_max_freq;
267         user_para->cpuinfo_min_freq = sys_para->cpuinfo_min_freq;
268         user_para->scaling_cur_freq = sys_para->scaling_cur_freq;
269         user_para->scaling_max_freq = sys_para->scaling_max_freq;
270         user_para->scaling_min_freq = sys_para->scaling_min_freq;
271         user_para->turbo_enabled    = sys_para->turbo_enabled;
272 
273         memcpy(user_para->scaling_driver,
274                 sys_para->scaling_driver, CPUFREQ_NAME_LEN);
275         memcpy(user_para->scaling_governor,
276                 sys_para->scaling_governor, CPUFREQ_NAME_LEN);
277 
278         /* copy to user_para no matter what cpufreq governor */
279         BUILD_BUG_ON(sizeof(((struct xc_get_cpufreq_para *)0)->u) !=
280 		     sizeof(((struct xen_get_cpufreq_para *)0)->u));
281 
282         memcpy(&user_para->u, &sys_para->u, sizeof(sys_para->u));
283     }
284 
285 unlock_4:
286     xc_hypercall_bounce_post(xch, scaling_available_governors);
287 unlock_3:
288     xc_hypercall_bounce_post(xch, scaling_available_frequencies);
289 unlock_2:
290     xc_hypercall_bounce_post(xch, affected_cpus);
291 unlock_1:
292     return ret;
293 }
294 
xc_set_cpufreq_gov(xc_interface * xch,int cpuid,char * govname)295 int xc_set_cpufreq_gov(xc_interface *xch, int cpuid, char *govname)
296 {
297     DECLARE_SYSCTL;
298     char *scaling_governor = sysctl.u.pm_op.u.set_gov.scaling_governor;
299 
300     if ( !xch || !govname )
301     {
302         errno = EINVAL;
303         return -1;
304     }
305     sysctl.cmd = XEN_SYSCTL_pm_op;
306     sysctl.u.pm_op.cmd = SET_CPUFREQ_GOV;
307     sysctl.u.pm_op.cpuid = cpuid;
308     strncpy(scaling_governor, govname, CPUFREQ_NAME_LEN - 1);
309     scaling_governor[CPUFREQ_NAME_LEN - 1] = '\0';
310 
311     return xc_sysctl(xch, &sysctl);
312 }
313 
xc_set_cpufreq_para(xc_interface * xch,int cpuid,int ctrl_type,int ctrl_value)314 int xc_set_cpufreq_para(xc_interface *xch, int cpuid,
315                         int ctrl_type, int ctrl_value)
316 {
317     DECLARE_SYSCTL;
318 
319     if ( !xch )
320     {
321         errno = EINVAL;
322         return -1;
323     }
324     sysctl.cmd = XEN_SYSCTL_pm_op;
325     sysctl.u.pm_op.cmd = SET_CPUFREQ_PARA;
326     sysctl.u.pm_op.cpuid = cpuid;
327     sysctl.u.pm_op.u.set_para.ctrl_type = ctrl_type;
328     sysctl.u.pm_op.u.set_para.ctrl_value = ctrl_value;
329 
330     return xc_sysctl(xch, &sysctl);
331 }
332 
xc_get_cpufreq_avgfreq(xc_interface * xch,int cpuid,int * avg_freq)333 int xc_get_cpufreq_avgfreq(xc_interface *xch, int cpuid, int *avg_freq)
334 {
335     int ret = 0;
336     DECLARE_SYSCTL;
337 
338     if ( !xch || !avg_freq )
339     {
340         errno = EINVAL;
341         return -1;
342     }
343     sysctl.cmd = XEN_SYSCTL_pm_op;
344     sysctl.u.pm_op.cmd = GET_CPUFREQ_AVGFREQ;
345     sysctl.u.pm_op.cpuid = cpuid;
346     ret = xc_sysctl(xch, &sysctl);
347 
348     *avg_freq = sysctl.u.pm_op.u.get_avgfreq;
349 
350     return ret;
351 }
352 
353 /* value:   0 - disable sched_smt_power_savings
354             1 - enable sched_smt_power_savings
355  */
xc_set_sched_opt_smt(xc_interface * xch,uint32_t value)356 int xc_set_sched_opt_smt(xc_interface *xch, uint32_t value)
357 {
358    int rc;
359    DECLARE_SYSCTL;
360 
361    sysctl.cmd = XEN_SYSCTL_pm_op;
362    sysctl.u.pm_op.cmd = XEN_SYSCTL_pm_op_set_sched_opt_smt;
363    sysctl.u.pm_op.cpuid = 0;
364    sysctl.u.pm_op.u.set_sched_opt_smt = value;
365    rc = do_sysctl(xch, &sysctl);
366 
367    return rc;
368 }
369 
get_max_cstate(xc_interface * xch,uint32_t * value,uint32_t type)370 static int get_max_cstate(xc_interface *xch, uint32_t *value, uint32_t type)
371 {
372     int rc;
373     DECLARE_SYSCTL;
374 
375     if ( !xch || !value )
376     {
377         errno = EINVAL;
378         return -1;
379     }
380     sysctl.cmd = XEN_SYSCTL_pm_op;
381     sysctl.u.pm_op.cmd = XEN_SYSCTL_pm_op_get_max_cstate;
382     sysctl.u.pm_op.cpuid = type;
383     sysctl.u.pm_op.u.get_max_cstate = 0;
384     rc = do_sysctl(xch, &sysctl);
385     *value = sysctl.u.pm_op.u.get_max_cstate;
386 
387     return rc;
388 }
389 
xc_get_cpuidle_max_cstate(xc_interface * xch,uint32_t * value)390 int xc_get_cpuidle_max_cstate(xc_interface *xch, uint32_t *value)
391 {
392     return get_max_cstate(xch, value, 0);
393 }
394 
xc_get_cpuidle_max_csubstate(xc_interface * xch,uint32_t * value)395 int xc_get_cpuidle_max_csubstate(xc_interface *xch, uint32_t *value)
396 {
397     return get_max_cstate(xch, value, 1);
398 }
399 
set_max_cstate(xc_interface * xch,uint32_t value,uint32_t type)400 static int set_max_cstate(xc_interface *xch, uint32_t value, uint32_t type)
401 {
402     DECLARE_SYSCTL;
403 
404     if ( !xch )
405     {
406         errno = EINVAL;
407         return -1;
408     }
409     sysctl.cmd = XEN_SYSCTL_pm_op;
410     sysctl.u.pm_op.cmd = XEN_SYSCTL_pm_op_set_max_cstate;
411     sysctl.u.pm_op.cpuid = type;
412     sysctl.u.pm_op.u.set_max_cstate = value;
413 
414     return do_sysctl(xch, &sysctl);
415 }
416 
xc_set_cpuidle_max_cstate(xc_interface * xch,uint32_t value)417 int xc_set_cpuidle_max_cstate(xc_interface *xch, uint32_t value)
418 {
419     return set_max_cstate(xch, value, 0);
420 }
421 
xc_set_cpuidle_max_csubstate(xc_interface * xch,uint32_t value)422 int xc_set_cpuidle_max_csubstate(xc_interface *xch, uint32_t value)
423 {
424     return set_max_cstate(xch, value, 1);
425 }
426 
xc_enable_turbo(xc_interface * xch,int cpuid)427 int xc_enable_turbo(xc_interface *xch, int cpuid)
428 {
429     DECLARE_SYSCTL;
430 
431     if ( !xch )
432     {
433         errno = EINVAL;
434         return -1;
435     }
436     sysctl.cmd = XEN_SYSCTL_pm_op;
437     sysctl.u.pm_op.cmd = XEN_SYSCTL_pm_op_enable_turbo;
438     sysctl.u.pm_op.cpuid = cpuid;
439     return do_sysctl(xch, &sysctl);
440 }
441 
xc_disable_turbo(xc_interface * xch,int cpuid)442 int xc_disable_turbo(xc_interface *xch, int cpuid)
443 {
444     DECLARE_SYSCTL;
445 
446     if ( !xch )
447     {
448         errno = EINVAL;
449         return -1;
450     }
451     sysctl.cmd = XEN_SYSCTL_pm_op;
452     sysctl.u.pm_op.cmd = XEN_SYSCTL_pm_op_disable_turbo;
453     sysctl.u.pm_op.cpuid = cpuid;
454     return do_sysctl(xch, &sysctl);
455 }
456