xref: /spdk/module/scheduler/dpdk_governor/dpdk_governor.c (revision 6e5d6032a09ca918509e7c6f28d6d2e20b8dc832)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
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 
34 #include "spdk/stdinc.h"
35 #include "spdk/log.h"
36 #include "spdk/env.h"
37 #include "spdk/event.h"
38 #include "spdk/scheduler.h"
39 
40 #include "spdk_internal/event.h"
41 
42 #include <rte_power.h>
43 
44 static uint32_t
45 _get_core_curr_freq(uint32_t lcore_id)
46 {
47 	const uint32_t MAX_CORE_FREQ_NUM = 64;
48 	uint32_t freqs[MAX_CORE_FREQ_NUM];
49 	uint32_t freq_index;
50 	int rc;
51 
52 	rc = rte_power_freqs(lcore_id, freqs, MAX_CORE_FREQ_NUM);
53 	if (!rc) {
54 		SPDK_ERRLOG("Unable to get current core frequency array for core %d\n.", lcore_id);
55 
56 		return 0;
57 	}
58 	freq_index = rte_power_get_freq(lcore_id);
59 	if (freq_index >= MAX_CORE_FREQ_NUM) {
60 		SPDK_ERRLOG("Unable to get current core frequency for core %d\n.", lcore_id);
61 
62 		return 0;
63 	}
64 
65 	return freqs[freq_index];
66 }
67 
68 static int
69 _core_freq_up(uint32_t lcore_id)
70 {
71 	return rte_power_freq_up(lcore_id);
72 }
73 
74 static int
75 _core_freq_down(uint32_t lcore_id)
76 {
77 	return rte_power_freq_down(lcore_id);
78 }
79 
80 static int
81 _set_core_freq_max(uint32_t lcore_id)
82 {
83 	return rte_power_freq_max(lcore_id);
84 }
85 
86 static int
87 _set_core_freq_min(uint32_t lcore_id)
88 {
89 	return rte_power_freq_min(lcore_id);
90 }
91 
92 static int
93 _get_core_capabilities(uint32_t lcore_id, struct spdk_governor_capabilities *capabilities)
94 {
95 	struct rte_power_core_capabilities caps;
96 	int rc;
97 
98 	rc = rte_power_get_capabilities(lcore_id, &caps);
99 	if (rc != 0) {
100 		return rc;
101 	}
102 
103 	capabilities->priority = caps.priority == 0 ? false : true;
104 
105 	return 0;
106 }
107 
108 static int
109 _init_core(uint32_t lcore_id)
110 {
111 	struct rte_power_core_capabilities caps;
112 	int rc;
113 
114 	rc = rte_power_init(lcore_id);
115 	if (rc != 0) {
116 		SPDK_ERRLOG("Failed to initialize on core%d\n", lcore_id);
117 		return rc;
118 	}
119 
120 	rc = rte_power_get_capabilities(lcore_id, &caps);
121 	if (rc != 0) {
122 		SPDK_ERRLOG("Failed retrievie capabilities of core%d\n", lcore_id);
123 		return rc;
124 	}
125 
126 	if (caps.turbo) {
127 		rc = rte_power_freq_enable_turbo(lcore_id);
128 		if (rc != 0) {
129 			SPDK_ERRLOG("Failed to set turbo on core%d\n", lcore_id);
130 			return rc;
131 		}
132 	}
133 
134 	return rc;
135 }
136 
137 static int
138 _init(void)
139 {
140 	uint32_t i, j;
141 	int rc = 0;
142 
143 	SPDK_ENV_FOREACH_CORE(i) {
144 		rc = _init_core(i);
145 		if (rc != 0) {
146 			SPDK_ERRLOG("Failed to initialize on core%d\n", i);
147 			break;
148 		}
149 	}
150 
151 	if (rc == 0) {
152 		return rc;
153 	}
154 
155 	/* When initalization of a core failed, deinitalize prior cores. */
156 	SPDK_ENV_FOREACH_CORE(j) {
157 		if (j >= i) {
158 			break;
159 		}
160 		if (rte_power_exit(j) != 0) {
161 			SPDK_ERRLOG("Failed to deinitialize on core%d\n", j);
162 		}
163 	}
164 	return rc;
165 }
166 
167 static void
168 _deinit(void)
169 {
170 	uint32_t i;
171 
172 	SPDK_ENV_FOREACH_CORE(i) {
173 		if (rte_power_exit(i) != 0) {
174 			SPDK_ERRLOG("Failed to deinitialize on core%d\n", i);
175 		}
176 	}
177 }
178 
179 static struct spdk_governor dpdk_governor = {
180 	.name = "dpdk_governor",
181 	.get_core_curr_freq = _get_core_curr_freq,
182 	.core_freq_up = _core_freq_up,
183 	.core_freq_down = _core_freq_down,
184 	.set_core_freq_max = _set_core_freq_max,
185 	.set_core_freq_min = _set_core_freq_min,
186 	.get_core_capabilities = _get_core_capabilities,
187 	.init = _init,
188 	.deinit = _deinit,
189 };
190 
191 SPDK_GOVERNOR_REGISTER(dpdk_governor);
192