New upstream version 17.11
[deb_dpdk.git] / lib / librte_power / rte_power_kvm_vm.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <errno.h>
34 #include <string.h>
35
36 #include <rte_log.h>
37
38 #include "guest_channel.h"
39 #include "channel_commands.h"
40 #include "rte_power_kvm_vm.h"
41 #include "rte_power_common.h"
42
43 #define FD_PATH "/dev/virtio-ports/virtio.serial.port.poweragent"
44
45 static struct channel_packet pkt[CHANNEL_CMDS_MAX_VM_CHANNELS];
46
47
48 int
49 rte_power_kvm_vm_init(unsigned lcore_id)
50 {
51         if (lcore_id >= CHANNEL_CMDS_MAX_VM_CHANNELS) {
52                 RTE_LOG(ERR, POWER, "Core(%u) is out of range 0...%d\n",
53                                 lcore_id, CHANNEL_CMDS_MAX_VM_CHANNELS-1);
54                 return -1;
55         }
56         pkt[lcore_id].command = CPU_POWER;
57         pkt[lcore_id].resource_id = lcore_id;
58         return guest_channel_host_connect(FD_PATH, lcore_id);
59 }
60
61 int
62 rte_power_kvm_vm_exit(unsigned lcore_id)
63 {
64         guest_channel_host_disconnect(lcore_id);
65         return 0;
66 }
67
68 uint32_t
69 rte_power_kvm_vm_freqs(__attribute__((unused)) unsigned lcore_id,
70                 __attribute__((unused)) uint32_t *freqs,
71                 __attribute__((unused)) uint32_t num)
72 {
73         RTE_LOG(ERR, POWER, "rte_power_freqs is not implemented "
74                         "for Virtual Machine Power Management\n");
75         return -ENOTSUP;
76 }
77
78 uint32_t
79 rte_power_kvm_vm_get_freq(__attribute__((unused)) unsigned lcore_id)
80 {
81         RTE_LOG(ERR, POWER, "rte_power_get_freq is not implemented "
82                         "for Virtual Machine Power Management\n");
83         return -ENOTSUP;
84 }
85
86 int
87 rte_power_kvm_vm_set_freq(__attribute__((unused)) unsigned lcore_id,
88                 __attribute__((unused)) uint32_t index)
89 {
90         RTE_LOG(ERR, POWER, "rte_power_set_freq is not implemented "
91                         "for Virtual Machine Power Management\n");
92         return -ENOTSUP;
93 }
94
95 static inline int
96 send_msg(unsigned lcore_id, uint32_t scale_direction)
97 {
98         int ret;
99
100         if (lcore_id >= CHANNEL_CMDS_MAX_VM_CHANNELS) {
101                 RTE_LOG(ERR, POWER, "Core(%u) is out of range 0...%d\n",
102                                 lcore_id, CHANNEL_CMDS_MAX_VM_CHANNELS-1);
103                 return -1;
104         }
105         pkt[lcore_id].unit = scale_direction;
106         ret = guest_channel_send_msg(&pkt[lcore_id], lcore_id);
107         if (ret == 0)
108                 return 1;
109         RTE_LOG(DEBUG, POWER, "Error sending message: %s\n",
110                         ret > 0 ? strerror(ret) : "channel not connected");
111         return -1;
112 }
113
114 int
115 rte_power_kvm_vm_freq_up(unsigned lcore_id)
116 {
117         return send_msg(lcore_id, CPU_POWER_SCALE_UP);
118 }
119
120 int
121 rte_power_kvm_vm_freq_down(unsigned lcore_id)
122 {
123         return send_msg(lcore_id, CPU_POWER_SCALE_DOWN);
124 }
125
126 int
127 rte_power_kvm_vm_freq_max(unsigned lcore_id)
128 {
129         return send_msg(lcore_id, CPU_POWER_SCALE_MAX);
130 }
131
132 int
133 rte_power_kvm_vm_freq_min(unsigned lcore_id)
134 {
135         return send_msg(lcore_id, CPU_POWER_SCALE_MIN);
136 }
137
138 int
139 rte_power_kvm_vm_turbo_status(__attribute__((unused)) unsigned int lcore_id)
140 {
141         RTE_LOG(ERR, POWER, "rte_power_turbo_status is not implemented for Virtual Machine Power Management\n");
142         return -ENOTSUP;
143 }
144
145 int
146 rte_power_kvm_vm_enable_turbo(unsigned int lcore_id)
147 {
148         return send_msg(lcore_id, CPU_POWER_ENABLE_TURBO);
149 }
150
151 int
152 rte_power_kvm_vm_disable_turbo(unsigned int lcore_id)
153 {
154         return send_msg(lcore_id, CPU_POWER_DISABLE_TURBO);
155 }