1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 #include <errno.h>
5 #include <string.h>
6 
7 #include <rte_log.h>
8 
9 #include "guest_channel.h"
10 #include "channel_commands.h"
11 #include "power_kvm_vm.h"
12 #include "power_common.h"
13 
14 #define FD_PATH "/dev/virtio-ports/virtio.serial.port.poweragent"
15 
16 static struct channel_packet pkt[CHANNEL_CMDS_MAX_VM_CHANNELS];
17 
18 
19 int
20 power_kvm_vm_init(unsigned int lcore_id)
21 {
22 	if (lcore_id >= CHANNEL_CMDS_MAX_VM_CHANNELS) {
23 		RTE_LOG(ERR, POWER, "Core(%u) is out of range 0...%d\n",
24 				lcore_id, CHANNEL_CMDS_MAX_VM_CHANNELS-1);
25 		return -1;
26 	}
27 	pkt[lcore_id].command = CPU_POWER;
28 	pkt[lcore_id].resource_id = lcore_id;
29 	return guest_channel_host_connect(FD_PATH, lcore_id);
30 }
31 
32 int
33 power_kvm_vm_exit(unsigned int lcore_id)
34 {
35 	guest_channel_host_disconnect(lcore_id);
36 	return 0;
37 }
38 
39 uint32_t
40 power_kvm_vm_freqs(__attribute__((unused)) unsigned int lcore_id,
41 		__attribute__((unused)) uint32_t *freqs,
42 		__attribute__((unused)) uint32_t num)
43 {
44 	RTE_LOG(ERR, POWER, "rte_power_freqs is not implemented "
45 			"for Virtual Machine Power Management\n");
46 	return -ENOTSUP;
47 }
48 
49 uint32_t
50 power_kvm_vm_get_freq(__attribute__((unused)) unsigned int lcore_id)
51 {
52 	RTE_LOG(ERR, POWER, "rte_power_get_freq is not implemented "
53 			"for Virtual Machine Power Management\n");
54 	return -ENOTSUP;
55 }
56 
57 int
58 power_kvm_vm_set_freq(__attribute__((unused)) unsigned int lcore_id,
59 		__attribute__((unused)) uint32_t index)
60 {
61 	RTE_LOG(ERR, POWER, "rte_power_set_freq is not implemented "
62 			"for Virtual Machine Power Management\n");
63 	return -ENOTSUP;
64 }
65 
66 static inline int
67 send_msg(unsigned int lcore_id, uint32_t scale_direction)
68 {
69 	int ret;
70 
71 	if (lcore_id >= CHANNEL_CMDS_MAX_VM_CHANNELS) {
72 		RTE_LOG(ERR, POWER, "Core(%u) is out of range 0...%d\n",
73 				lcore_id, CHANNEL_CMDS_MAX_VM_CHANNELS-1);
74 		return -1;
75 	}
76 	pkt[lcore_id].unit = scale_direction;
77 	ret = guest_channel_send_msg(&pkt[lcore_id], lcore_id);
78 	if (ret == 0)
79 		return 1;
80 	RTE_LOG(DEBUG, POWER, "Error sending message: %s\n",
81 			ret > 0 ? strerror(ret) : "channel not connected");
82 	return -1;
83 }
84 
85 int
86 power_kvm_vm_freq_up(unsigned int lcore_id)
87 {
88 	return send_msg(lcore_id, CPU_POWER_SCALE_UP);
89 }
90 
91 int
92 power_kvm_vm_freq_down(unsigned int lcore_id)
93 {
94 	return send_msg(lcore_id, CPU_POWER_SCALE_DOWN);
95 }
96 
97 int
98 power_kvm_vm_freq_max(unsigned int lcore_id)
99 {
100 	return send_msg(lcore_id, CPU_POWER_SCALE_MAX);
101 }
102 
103 int
104 power_kvm_vm_freq_min(unsigned int lcore_id)
105 {
106 	return send_msg(lcore_id, CPU_POWER_SCALE_MIN);
107 }
108 
109 int
110 power_kvm_vm_turbo_status(__attribute__((unused)) unsigned int lcore_id)
111 {
112 	RTE_LOG(ERR, POWER, "rte_power_turbo_status is not implemented for Virtual Machine Power Management\n");
113 	return -ENOTSUP;
114 }
115 
116 int
117 power_kvm_vm_enable_turbo(unsigned int lcore_id)
118 {
119 	return send_msg(lcore_id, CPU_POWER_ENABLE_TURBO);
120 }
121 
122 int
123 power_kvm_vm_disable_turbo(unsigned int lcore_id)
124 {
125 	return send_msg(lcore_id, CPU_POWER_DISABLE_TURBO);
126 }
127 
128 struct rte_power_core_capabilities;
129 int power_kvm_vm_get_capabilities(__rte_unused unsigned int lcore_id,
130 		__rte_unused struct rte_power_core_capabilities *caps)
131 {
132 	RTE_LOG(ERR, POWER, "rte_power_get_capabilities is not implemented for Virtual Machine Power Management\n");
133 	return -ENOTSUP;
134 }
135