Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2020 Intel Corporation
3 : : */
4 : :
5 : : #include <stdlib.h>
6 : :
7 : : #include <eal_export.h>
8 : : #include <rte_lcore.h>
9 : : #include <rte_lcore_var.h>
10 : : #include <rte_cycles.h>
11 : : #include <rte_cpuflags.h>
12 : : #include <rte_malloc.h>
13 : : #include <rte_ethdev.h>
14 : : #include <rte_power_intrinsics.h>
15 : :
16 : : #include "rte_power_pmd_mgmt.h"
17 : : #include "power_common.h"
18 : :
19 : : unsigned int emptypoll_max;
20 : : unsigned int pause_duration;
21 : : unsigned int scale_freq_min[RTE_MAX_LCORE];
22 : : unsigned int scale_freq_max[RTE_MAX_LCORE];
23 : :
24 : : /* store some internal state */
25 : : static struct pmd_conf_data {
26 : : /** what do we support? */
27 : : struct rte_cpu_intrinsics intrinsics_support;
28 : : /** pre-calculated tsc diff for 1us */
29 : : uint64_t tsc_per_us;
30 : : /** how many rte_pause can we fit in a microsecond? */
31 : : uint64_t pause_per_us;
32 : : } global_data;
33 : :
34 : : /**
35 : : * Possible power management states of an ethdev port.
36 : : */
37 : : enum pmd_mgmt_state {
38 : : /** Device power management is disabled. */
39 : : PMD_MGMT_DISABLED = 0,
40 : : /** Device power management is enabled. */
41 : : PMD_MGMT_ENABLED
42 : : };
43 : :
44 : : union queue {
45 : : uint32_t val;
46 : : struct {
47 : : uint16_t portid;
48 : : uint16_t qid;
49 : : };
50 : : };
51 : :
52 : : struct queue_list_entry {
53 : : TAILQ_ENTRY(queue_list_entry) next;
54 : : union queue queue;
55 : : uint64_t n_empty_polls;
56 : : uint64_t n_sleeps;
57 : : const struct rte_eth_rxtx_callback *cb;
58 : : };
59 : :
60 : : struct pmd_core_cfg {
61 : : TAILQ_HEAD(queue_list_head, queue_list_entry) head;
62 : : /**< List of queues associated with this lcore */
63 : : size_t n_queues;
64 : : /**< How many queues are in the list? */
65 : : volatile enum pmd_mgmt_state pwr_mgmt_state;
66 : : /**< State of power management for this queue */
67 : : enum rte_power_pmd_mgmt_type cb_mode;
68 : : /**< Callback mode for this queue */
69 : : uint64_t n_queues_ready_to_sleep;
70 : : /**< Number of queues ready to enter power optimized state */
71 : : uint64_t sleep_target;
72 : : /**< Prevent a queue from triggering sleep multiple times */
73 : : };
74 : : static RTE_LCORE_VAR_HANDLE(struct pmd_core_cfg, lcore_cfgs);
75 : :
76 : : static void
77 : 0 : init_lcore_cfgs(void)
78 : : {
79 : : struct pmd_core_cfg *lcore_cfg;
80 : : unsigned int lcore_id;
81 : :
82 [ # # ]: 0 : if (lcore_cfgs != NULL)
83 : : return;
84 : :
85 : 0 : RTE_LCORE_VAR_ALLOC(lcore_cfgs);
86 : :
87 : : /* initialize all tailqs */
88 [ # # ]: 0 : RTE_LCORE_VAR_FOREACH(lcore_id, lcore_cfg, lcore_cfgs)
89 : 0 : TAILQ_INIT(&lcore_cfg->head);
90 : : }
91 : :
92 : : static inline bool
93 : : queue_equal(const union queue *l, const union queue *r)
94 : : {
95 : 0 : return l->val == r->val;
96 : : }
97 : :
98 : : static inline void
99 : : queue_copy(union queue *dst, const union queue *src)
100 : : {
101 : 0 : dst->val = src->val;
102 : : }
103 : :
104 : : static struct queue_list_entry *
105 : : queue_list_find(const struct pmd_core_cfg *cfg, const union queue *q)
106 : : {
107 : : struct queue_list_entry *cur;
108 : :
109 [ # # # # ]: 0 : TAILQ_FOREACH(cur, &cfg->head, next) {
110 [ # # # # ]: 0 : if (queue_equal(&cur->queue, q))
111 : : return cur;
112 : : }
113 : : return NULL;
114 : : }
115 : :
116 : : static int
117 : 0 : queue_list_add(struct pmd_core_cfg *cfg, const union queue *q)
118 : : {
119 : : struct queue_list_entry *qle;
120 : :
121 : : /* is it already in the list? */
122 [ # # ]: 0 : if (queue_list_find(cfg, q) != NULL)
123 : : return -EEXIST;
124 : :
125 : 0 : qle = malloc(sizeof(*qle));
126 [ # # ]: 0 : if (qle == NULL)
127 : : return -ENOMEM;
128 : : memset(qle, 0, sizeof(*qle));
129 : :
130 : : queue_copy(&qle->queue, q);
131 : 0 : TAILQ_INSERT_TAIL(&cfg->head, qle, next);
132 : 0 : cfg->n_queues++;
133 : :
134 : 0 : return 0;
135 : : }
136 : :
137 : : static struct queue_list_entry *
138 : 0 : queue_list_take(struct pmd_core_cfg *cfg, const union queue *q)
139 : : {
140 : : struct queue_list_entry *found;
141 : :
142 : : found = queue_list_find(cfg, q);
143 [ # # ]: 0 : if (found == NULL)
144 : : return NULL;
145 : :
146 [ # # ]: 0 : TAILQ_REMOVE(&cfg->head, found, next);
147 : 0 : cfg->n_queues--;
148 : :
149 : : /* freeing is responsibility of the caller */
150 : 0 : return found;
151 : : }
152 : :
153 : : static inline int
154 : 0 : get_monitor_addresses(struct pmd_core_cfg *cfg,
155 : : struct rte_power_monitor_cond *pmc, size_t len)
156 : : {
157 : : const struct queue_list_entry *qle;
158 : : size_t i = 0;
159 : : int ret;
160 : :
161 [ # # ]: 0 : TAILQ_FOREACH(qle, &cfg->head, next) {
162 : : const union queue *q = &qle->queue;
163 : : struct rte_power_monitor_cond *cur;
164 : :
165 : : /* attempted out of bounds access */
166 [ # # ]: 0 : if (i >= len) {
167 : 0 : POWER_LOG(ERR, "Too many queues being monitored");
168 : 0 : return -1;
169 : : }
170 : :
171 : 0 : cur = &pmc[i++];
172 : 0 : ret = rte_eth_get_monitor_addr(q->portid, q->qid, cur);
173 [ # # ]: 0 : if (ret < 0)
174 : 0 : return ret;
175 : : }
176 : : return 0;
177 : : }
178 : :
179 : : static void
180 : 0 : calc_tsc(void)
181 : : {
182 : : const uint64_t hz = rte_get_timer_hz();
183 : 0 : const uint64_t tsc_per_us = hz / US_PER_S; /* 1us */
184 : :
185 : 0 : global_data.tsc_per_us = tsc_per_us;
186 : :
187 : : /* only do this if we don't have tpause */
188 [ # # ]: 0 : if (!global_data.intrinsics_support.power_pause) {
189 : : const uint64_t start = rte_rdtsc_precise();
190 : : const uint32_t n_pauses = 10000;
191 : : double us, us_per_pause;
192 : : uint64_t end;
193 : : unsigned int i;
194 : :
195 : : /* estimate number of rte_pause() calls per us*/
196 [ # # ]: 0 : for (i = 0; i < n_pauses; i++)
197 : : rte_pause();
198 : :
199 : : end = rte_rdtsc_precise();
200 : 0 : us = (end - start) / (double)tsc_per_us;
201 : 0 : us_per_pause = us / n_pauses;
202 : :
203 : 0 : global_data.pause_per_us = (uint64_t)(1.0 / us_per_pause);
204 : : }
205 : 0 : }
206 : :
207 : : static inline void
208 : : queue_reset(struct pmd_core_cfg *cfg, struct queue_list_entry *qcfg)
209 : : {
210 : 0 : const bool is_ready_to_sleep = qcfg->n_sleeps == cfg->sleep_target;
211 : :
212 : : /* reset empty poll counter for this queue */
213 : 0 : qcfg->n_empty_polls = 0;
214 : : /* reset the queue sleep counter as well */
215 : 0 : qcfg->n_sleeps = 0;
216 : : /* remove the queue from list of queues ready to sleep */
217 [ # # # # : 0 : if (is_ready_to_sleep)
# # ]
218 : 0 : cfg->n_queues_ready_to_sleep--;
219 : : /*
220 : : * no need change the lcore sleep target counter because this lcore will
221 : : * reach the n_sleeps anyway, and the other cores are already counted so
222 : : * there's no need to do anything else.
223 : : */
224 : : }
225 : :
226 : : static inline bool
227 : : queue_can_sleep(struct pmd_core_cfg *cfg, struct queue_list_entry *qcfg)
228 : : {
229 : : /* this function is called - that means we have an empty poll */
230 : 0 : qcfg->n_empty_polls++;
231 : :
232 : : /* if we haven't reached threshold for empty polls, we can't sleep */
233 [ # # # # ]: 0 : if (qcfg->n_empty_polls <= emptypoll_max)
234 : : return false;
235 : :
236 : : /*
237 : : * we've reached a point where we are able to sleep, but we still need
238 : : * to check if this queue has already been marked for sleeping.
239 : : */
240 [ # # # # : 0 : if (qcfg->n_sleeps == cfg->sleep_target)
# # ]
241 : : return true;
242 : :
243 : : /* mark this queue as ready for sleep */
244 : 0 : qcfg->n_sleeps = cfg->sleep_target;
245 : 0 : cfg->n_queues_ready_to_sleep++;
246 : :
247 : : return true;
248 : : }
249 : :
250 : : static inline bool
251 : : lcore_can_sleep(struct pmd_core_cfg *cfg)
252 : : {
253 : : /* are all queues ready to sleep? */
254 [ # # # # : 0 : if (cfg->n_queues_ready_to_sleep != cfg->n_queues)
# # ]
255 : : return false;
256 : :
257 : : /* we've reached an iteration where we can sleep, reset sleep counter */
258 : 0 : cfg->n_queues_ready_to_sleep = 0;
259 : 0 : cfg->sleep_target++;
260 : : /*
261 : : * we do not reset any individual queue empty poll counters, because
262 : : * we want to keep sleeping on every poll until we actually get traffic.
263 : : */
264 : :
265 : : return true;
266 : : }
267 : :
268 : : static uint16_t
269 : 0 : clb_multiwait(uint16_t port_id __rte_unused, uint16_t qidx __rte_unused,
270 : : struct rte_mbuf **pkts __rte_unused, uint16_t nb_rx,
271 : : uint16_t max_pkts __rte_unused, void *arg)
272 : : {
273 : : struct queue_list_entry *queue_conf = arg;
274 : : struct pmd_core_cfg *lcore_conf;
275 : : const bool empty = nb_rx == 0;
276 : :
277 [ # # ]: 0 : lcore_conf = RTE_LCORE_VAR(lcore_cfgs);
278 : :
279 : : /* early exit */
280 [ # # ]: 0 : if (likely(!empty))
281 : : /* early exit */
282 : : queue_reset(lcore_conf, queue_conf);
283 : 0 : else {
284 [ # # ]: 0 : struct rte_power_monitor_cond pmc[lcore_conf->n_queues];
285 : : int ret;
286 : :
287 : : /* can this queue sleep? */
288 : : if (!queue_can_sleep(lcore_conf, queue_conf))
289 : 0 : return nb_rx;
290 : :
291 : : /* can this lcore sleep? */
292 : : if (!lcore_can_sleep(lcore_conf))
293 : : return nb_rx;
294 : :
295 : : /* gather all monitoring conditions */
296 : 0 : ret = get_monitor_addresses(lcore_conf, pmc,
297 : : lcore_conf->n_queues);
298 [ # # ]: 0 : if (ret < 0)
299 : : return nb_rx;
300 : :
301 : 0 : rte_power_monitor_multi(pmc, lcore_conf->n_queues, UINT64_MAX);
302 : : }
303 : :
304 : : return nb_rx;
305 : : }
306 : :
307 : : static uint16_t
308 : 0 : clb_umwait(uint16_t port_id, uint16_t qidx, struct rte_mbuf **pkts __rte_unused,
309 : : uint16_t nb_rx, uint16_t max_pkts __rte_unused, void *arg)
310 : : {
311 : : struct queue_list_entry *queue_conf = arg;
312 : :
313 : : /* this callback can't do more than one queue, omit multiqueue logic */
314 [ # # ]: 0 : if (unlikely(nb_rx == 0)) {
315 : 0 : queue_conf->n_empty_polls++;
316 [ # # ]: 0 : if (unlikely(queue_conf->n_empty_polls > emptypoll_max)) {
317 : : struct rte_power_monitor_cond pmc;
318 : : int ret;
319 : :
320 : : /* use monitoring condition to sleep */
321 : 0 : ret = rte_eth_get_monitor_addr(port_id, qidx,
322 : : &pmc);
323 [ # # ]: 0 : if (ret == 0)
324 : 0 : rte_power_monitor(&pmc, UINT64_MAX);
325 : : }
326 : : } else
327 : 0 : queue_conf->n_empty_polls = 0;
328 : :
329 : 0 : return nb_rx;
330 : : }
331 : :
332 : : static uint16_t
333 : 0 : clb_pause(uint16_t port_id __rte_unused, uint16_t qidx __rte_unused,
334 : : struct rte_mbuf **pkts __rte_unused, uint16_t nb_rx,
335 : : uint16_t max_pkts __rte_unused, void *arg)
336 : : {
337 : : struct queue_list_entry *queue_conf = arg;
338 : : struct pmd_core_cfg *lcore_conf;
339 : : const bool empty = nb_rx == 0;
340 : 0 : const uint32_t duration = rte_power_pmd_mgmt_get_pause_duration();
341 : :
342 [ # # ]: 0 : lcore_conf = RTE_LCORE_VAR(lcore_cfgs);
343 : :
344 [ # # ]: 0 : if (likely(!empty))
345 : : /* early exit */
346 : : queue_reset(lcore_conf, queue_conf);
347 : : else {
348 : : /* can this queue sleep? */
349 : : if (!queue_can_sleep(lcore_conf, queue_conf))
350 : : return nb_rx;
351 : :
352 : : /* can this lcore sleep? */
353 : : if (!lcore_can_sleep(lcore_conf))
354 : : return nb_rx;
355 : :
356 : : /* sleep for 1 microsecond, use tpause if we have it */
357 [ # # ]: 0 : if (global_data.intrinsics_support.power_pause) {
358 : : const uint64_t cur = rte_rdtsc();
359 : 0 : const uint64_t wait_tsc =
360 : 0 : cur + global_data.tsc_per_us * duration;
361 : 0 : rte_power_pause(wait_tsc);
362 : : } else {
363 : : uint64_t i;
364 [ # # ]: 0 : for (i = 0; i < global_data.pause_per_us * duration; i++)
365 : : rte_pause();
366 : : }
367 : : }
368 : :
369 : : return nb_rx;
370 : : }
371 : :
372 : : static uint16_t
373 : 0 : clb_scale_freq(uint16_t port_id __rte_unused, uint16_t qidx __rte_unused,
374 : : struct rte_mbuf **pkts __rte_unused, uint16_t nb_rx,
375 : : uint16_t max_pkts __rte_unused, void *arg)
376 : : {
377 : : const bool empty = nb_rx == 0;
378 [ # # ]: 0 : struct pmd_core_cfg *lcore_conf = RTE_LCORE_VAR(lcore_cfgs);
379 : : struct queue_list_entry *queue_conf = arg;
380 : :
381 [ # # ]: 0 : if (likely(!empty)) {
382 : : /* early exit */
383 : : queue_reset(lcore_conf, queue_conf);
384 : :
385 : : /* scale up freq immediately */
386 : 0 : rte_power_freq_max(rte_lcore_id());
387 : : } else {
388 : : /* can this queue sleep? */
389 : : if (!queue_can_sleep(lcore_conf, queue_conf))
390 : : return nb_rx;
391 : :
392 : : /* can this lcore sleep? */
393 : : if (!lcore_can_sleep(lcore_conf))
394 : : return nb_rx;
395 : :
396 : 0 : rte_power_freq_min(rte_lcore_id());
397 : : }
398 : :
399 : : return nb_rx;
400 : : }
401 : :
402 : : static int
403 : : queue_stopped(const uint16_t port_id, const uint16_t queue_id)
404 : : {
405 : : struct rte_eth_rxq_info qinfo;
406 : :
407 : 0 : int ret = rte_eth_rx_queue_info_get(port_id, queue_id, &qinfo);
408 [ # # # # : 0 : if (ret < 0) {
# # ]
409 [ # # # # : 0 : if (ret == -ENOTSUP)
# # ]
410 : : return 1;
411 : : else
412 : : return -1;
413 : : }
414 : :
415 : 0 : return qinfo.queue_state == RTE_ETH_QUEUE_STATE_STOPPED;
416 : : }
417 : :
418 : : static int
419 : 0 : cfg_queues_stopped(struct pmd_core_cfg *queue_cfg)
420 : : {
421 : : const struct queue_list_entry *entry;
422 : :
423 [ # # ]: 0 : TAILQ_FOREACH(entry, &queue_cfg->head, next) {
424 : : const union queue *q = &entry->queue;
425 : 0 : int ret = queue_stopped(q->portid, q->qid);
426 [ # # ]: 0 : if (ret != 1)
427 : 0 : return ret;
428 : : }
429 : : return 1;
430 : : }
431 : :
432 : : static int
433 : 0 : check_scale(unsigned int lcore)
434 : : {
435 : : enum power_management_env env;
436 : :
437 : : /* only PSTATE, AMD-PSTATE, ACPI and CPPC modes are supported */
438 [ # # # # ]: 0 : if (!rte_power_check_env_supported(PM_ENV_ACPI_CPUFREQ) &&
439 [ # # ]: 0 : !rte_power_check_env_supported(PM_ENV_PSTATE_CPUFREQ) &&
440 [ # # ]: 0 : !rte_power_check_env_supported(PM_ENV_AMD_PSTATE_CPUFREQ) &&
441 : 0 : !rte_power_check_env_supported(PM_ENV_CPPC_CPUFREQ)) {
442 : 0 : POWER_LOG(DEBUG, "Only ACPI, PSTATE, AMD-PSTATE, or CPPC modes are supported");
443 : 0 : return -ENOTSUP;
444 : : }
445 : : /* ensure we could initialize the power library */
446 [ # # ]: 0 : if (rte_power_init(lcore))
447 : : return -EINVAL;
448 : :
449 : : /* ensure we initialized the correct env */
450 : 0 : env = rte_power_get_env();
451 [ # # ]: 0 : if (env != PM_ENV_ACPI_CPUFREQ && env != PM_ENV_PSTATE_CPUFREQ &&
452 [ # # ]: 0 : env != PM_ENV_AMD_PSTATE_CPUFREQ && env != PM_ENV_CPPC_CPUFREQ) {
453 : 0 : POWER_LOG(DEBUG, "Unable to initialize ACPI, PSTATE, AMD-PSTATE, or CPPC modes");
454 : 0 : return -ENOTSUP;
455 : : }
456 : :
457 : : /* we're done */
458 : : return 0;
459 : : }
460 : :
461 : : static int
462 : 0 : check_monitor(struct pmd_core_cfg *cfg, const union queue *qdata)
463 : : {
464 : : struct rte_power_monitor_cond dummy;
465 : : bool multimonitor_supported;
466 : :
467 : : /* check if rte_power_monitor is supported */
468 [ # # ]: 0 : if (!global_data.intrinsics_support.power_monitor) {
469 : 0 : POWER_LOG(DEBUG, "Monitoring intrinsics are not supported");
470 : 0 : return -ENOTSUP;
471 : : }
472 : : /* check if multi-monitor is supported */
473 : : multimonitor_supported =
474 : 0 : global_data.intrinsics_support.power_monitor_multi;
475 : :
476 : : /* if we're adding a new queue, do we support multiple queues? */
477 [ # # # # ]: 0 : if (cfg->n_queues > 0 && !multimonitor_supported) {
478 : 0 : POWER_LOG(DEBUG, "Monitoring multiple queues is not supported");
479 : 0 : return -ENOTSUP;
480 : : }
481 : :
482 : : /* check if the device supports the necessary PMD API */
483 [ # # ]: 0 : if (rte_eth_get_monitor_addr(qdata->portid, qdata->qid,
484 : : &dummy) == -ENOTSUP) {
485 : 0 : POWER_LOG(DEBUG, "The device does not support rte_eth_get_monitor_addr");
486 : 0 : return -ENOTSUP;
487 : : }
488 : :
489 : : /* we're done */
490 : : return 0;
491 : : }
492 : :
493 : : static inline rte_rx_callback_fn
494 : : get_monitor_callback(void)
495 : : {
496 : 0 : return global_data.intrinsics_support.power_monitor_multi ?
497 [ # # ]: 0 : clb_multiwait : clb_umwait;
498 : : }
499 : :
500 : : RTE_EXPORT_SYMBOL(rte_power_ethdev_pmgmt_queue_enable)
501 : : int
502 : 0 : rte_power_ethdev_pmgmt_queue_enable(unsigned int lcore_id, uint16_t port_id,
503 : : uint16_t queue_id, enum rte_power_pmd_mgmt_type mode)
504 : : {
505 : 0 : const union queue qdata = {.portid = port_id, .qid = queue_id};
506 : : struct pmd_core_cfg *lcore_cfg;
507 : : struct queue_list_entry *queue_cfg;
508 : : struct rte_eth_dev_info info;
509 : : rte_rx_callback_fn clb;
510 : : int ret;
511 : :
512 [ # # ]: 0 : RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
513 : :
514 [ # # # # ]: 0 : if (queue_id >= RTE_MAX_QUEUES_PER_PORT ||
515 : 0 : !rte_lcore_is_enabled(lcore_id)) {
516 : : ret = -EINVAL;
517 : 0 : goto end;
518 : : }
519 : :
520 [ # # ]: 0 : if (rte_eth_dev_info_get(port_id, &info) < 0) {
521 : : ret = -EINVAL;
522 : 0 : goto end;
523 : : }
524 : :
525 : : /* check if queue id is valid */
526 [ # # ]: 0 : if (queue_id >= info.nb_rx_queues) {
527 : : ret = -EINVAL;
528 : 0 : goto end;
529 : : }
530 : :
531 : : /* check if the queue is stopped */
532 : 0 : ret = queue_stopped(port_id, queue_id);
533 [ # # ]: 0 : if (ret != 1) {
534 : : /* error means invalid queue, 0 means queue wasn't stopped */
535 [ # # ]: 0 : ret = ret < 0 ? -EINVAL : -EBUSY;
536 : 0 : goto end;
537 : : }
538 : :
539 : 0 : init_lcore_cfgs();
540 : 0 : lcore_cfg = RTE_LCORE_VAR_LCORE(lcore_id, lcore_cfgs);
541 : :
542 : : /* check if other queues are stopped as well */
543 : 0 : ret = cfg_queues_stopped(lcore_cfg);
544 [ # # ]: 0 : if (ret != 1) {
545 : : /* error means invalid queue, 0 means queue wasn't stopped */
546 [ # # ]: 0 : ret = ret < 0 ? -EINVAL : -EBUSY;
547 : 0 : goto end;
548 : : }
549 : :
550 : : /* if callback was already enabled, check current callback type */
551 [ # # ]: 0 : if (lcore_cfg->pwr_mgmt_state != PMD_MGMT_DISABLED &&
552 [ # # ]: 0 : lcore_cfg->cb_mode != mode) {
553 : : ret = -EINVAL;
554 : 0 : goto end;
555 : : }
556 : :
557 : : /* we need this in various places */
558 : 0 : rte_cpu_get_intrinsics_support(&global_data.intrinsics_support);
559 : :
560 [ # # # # ]: 0 : switch (mode) {
561 : 0 : case RTE_POWER_MGMT_TYPE_MONITOR:
562 : : /* check if we can add a new queue */
563 : 0 : ret = check_monitor(lcore_cfg, &qdata);
564 [ # # ]: 0 : if (ret < 0)
565 : 0 : goto end;
566 : :
567 : : clb = get_monitor_callback();
568 : : break;
569 : 0 : case RTE_POWER_MGMT_TYPE_SCALE:
570 : : clb = clb_scale_freq;
571 : :
572 : : /* we only have to check this when enabling first queue */
573 [ # # ]: 0 : if (lcore_cfg->pwr_mgmt_state != PMD_MGMT_DISABLED)
574 : : break;
575 : : /* check if we can add a new queue */
576 : 0 : ret = check_scale(lcore_id);
577 [ # # ]: 0 : if (ret < 0)
578 : 0 : goto end;
579 : : break;
580 : 0 : case RTE_POWER_MGMT_TYPE_PAUSE:
581 : : /* figure out various time-to-tsc conversions */
582 [ # # ]: 0 : if (global_data.tsc_per_us == 0)
583 : 0 : calc_tsc();
584 : :
585 : : clb = clb_pause;
586 : : break;
587 : 0 : default:
588 : 0 : POWER_LOG(DEBUG, "Invalid power management type");
589 : : ret = -EINVAL;
590 : 0 : goto end;
591 : : }
592 : : /* add this queue to the list */
593 : 0 : ret = queue_list_add(lcore_cfg, &qdata);
594 [ # # ]: 0 : if (ret < 0) {
595 : 0 : POWER_LOG(DEBUG, "Failed to add queue to list: %s",
596 : : strerror(-ret));
597 : 0 : goto end;
598 : : }
599 : : /* new queue is always added last */
600 : 0 : queue_cfg = TAILQ_LAST(&lcore_cfg->head, queue_list_head);
601 : :
602 : : /* when enabling first queue, ensure sleep target is not 0 */
603 [ # # # # ]: 0 : if (lcore_cfg->n_queues == 1 && lcore_cfg->sleep_target == 0)
604 : 0 : lcore_cfg->sleep_target = 1;
605 : :
606 : : /* initialize data before enabling the callback */
607 [ # # ]: 0 : if (lcore_cfg->n_queues == 1) {
608 : 0 : lcore_cfg->cb_mode = mode;
609 : 0 : lcore_cfg->pwr_mgmt_state = PMD_MGMT_ENABLED;
610 : : }
611 : 0 : queue_cfg->cb = rte_eth_add_rx_callback(port_id, queue_id,
612 : : clb, queue_cfg);
613 : :
614 : : ret = 0;
615 : : end:
616 : : return ret;
617 : : }
618 : :
619 : : RTE_EXPORT_SYMBOL(rte_power_ethdev_pmgmt_queue_disable)
620 : : int
621 : 0 : rte_power_ethdev_pmgmt_queue_disable(unsigned int lcore_id,
622 : : uint16_t port_id, uint16_t queue_id)
623 : : {
624 : 0 : const union queue qdata = {.portid = port_id, .qid = queue_id};
625 : : struct pmd_core_cfg *lcore_cfg;
626 : : struct queue_list_entry *queue_cfg;
627 : : int ret;
628 : :
629 [ # # ]: 0 : RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
630 : :
631 [ # # # # ]: 0 : if (!rte_lcore_is_enabled(lcore_id) || queue_id >= RTE_MAX_QUEUES_PER_PORT)
632 : : return -EINVAL;
633 : :
634 : : /* check if the queue is stopped */
635 : 0 : ret = queue_stopped(port_id, queue_id);
636 [ # # ]: 0 : if (ret != 1) {
637 : : /* error means invalid queue, 0 means queue wasn't stopped */
638 [ # # ]: 0 : return ret < 0 ? -EINVAL : -EBUSY;
639 : : }
640 : :
641 : : /* no need to check queue id as wrong queue id would not be enabled */
642 : :
643 : 0 : init_lcore_cfgs();
644 : 0 : lcore_cfg = RTE_LCORE_VAR_LCORE(lcore_id, lcore_cfgs);
645 : :
646 : : /* check if other queues are stopped as well */
647 : 0 : ret = cfg_queues_stopped(lcore_cfg);
648 [ # # ]: 0 : if (ret != 1) {
649 : : /* error means invalid queue, 0 means queue wasn't stopped */
650 [ # # ]: 0 : return ret < 0 ? -EINVAL : -EBUSY;
651 : : }
652 : :
653 [ # # ]: 0 : if (lcore_cfg->pwr_mgmt_state != PMD_MGMT_ENABLED)
654 : : return -EINVAL;
655 : :
656 : : /*
657 : : * There is no good/easy way to do this without race conditions, so we
658 : : * are just going to throw our hands in the air and hope that the user
659 : : * has read the documentation and has ensured that ports are stopped at
660 : : * the time we enter the API functions.
661 : : */
662 : 0 : queue_cfg = queue_list_take(lcore_cfg, &qdata);
663 [ # # ]: 0 : if (queue_cfg == NULL)
664 : : return -ENOENT;
665 : :
666 : : /* if we've removed all queues from the lists, set state to disabled */
667 [ # # ]: 0 : if (lcore_cfg->n_queues == 0)
668 : 0 : lcore_cfg->pwr_mgmt_state = PMD_MGMT_DISABLED;
669 : :
670 [ # # # ]: 0 : switch (lcore_cfg->cb_mode) {
671 : 0 : case RTE_POWER_MGMT_TYPE_MONITOR: /* fall-through */
672 : : case RTE_POWER_MGMT_TYPE_PAUSE:
673 : 0 : rte_eth_remove_rx_callback(port_id, queue_id, queue_cfg->cb);
674 : 0 : break;
675 : 0 : case RTE_POWER_MGMT_TYPE_SCALE:
676 : 0 : rte_eth_remove_rx_callback(port_id, queue_id, queue_cfg->cb);
677 : : /* disable power library on this lcore if this was last queue */
678 [ # # ]: 0 : if (lcore_cfg->pwr_mgmt_state == PMD_MGMT_DISABLED) {
679 : 0 : rte_power_freq_max(lcore_id);
680 : 0 : rte_power_exit(lcore_id);
681 : : }
682 : : break;
683 : : }
684 : : /*
685 : : * the API doc mandates that the user stops all processing on affected
686 : : * ports before calling any of these API's, so we can assume that the
687 : : * callbacks can be freed. we're intentionally casting away const-ness.
688 : : */
689 : 0 : rte_free((void *)(uintptr_t)queue_cfg->cb);
690 : 0 : free(queue_cfg);
691 : :
692 : 0 : return 0;
693 : : }
694 : :
695 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_set_emptypoll_max)
696 : : void
697 : 0 : rte_power_pmd_mgmt_set_emptypoll_max(unsigned int max)
698 : : {
699 : 0 : emptypoll_max = max;
700 : 0 : }
701 : :
702 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_get_emptypoll_max)
703 : : unsigned int
704 : 0 : rte_power_pmd_mgmt_get_emptypoll_max(void)
705 : : {
706 : 0 : return emptypoll_max;
707 : : }
708 : :
709 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_set_pause_duration)
710 : : int
711 : 0 : rte_power_pmd_mgmt_set_pause_duration(unsigned int duration)
712 : : {
713 [ # # ]: 0 : if (duration == 0) {
714 : 0 : POWER_LOG(ERR, "Pause duration must be greater than 0, value unchanged");
715 : 0 : return -EINVAL;
716 : : }
717 : 0 : pause_duration = duration;
718 : :
719 : 0 : return 0;
720 : : }
721 : :
722 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_get_pause_duration)
723 : : unsigned int
724 : 0 : rte_power_pmd_mgmt_get_pause_duration(void)
725 : : {
726 : 0 : return pause_duration;
727 : : }
728 : :
729 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_set_scaling_freq_min)
730 : : int
731 : 0 : rte_power_pmd_mgmt_set_scaling_freq_min(unsigned int lcore, unsigned int min)
732 : : {
733 [ # # ]: 0 : if (!rte_lcore_is_enabled(lcore)) {
734 : 0 : POWER_LOG(ERR, "lcore id %u is not enabled", lcore);
735 : 0 : return -EINVAL;
736 : : }
737 : :
738 [ # # ]: 0 : if (min > scale_freq_max[lcore]) {
739 : 0 : POWER_LOG(ERR, "Invalid min frequency: Cannot be greater than max frequency");
740 : 0 : return -EINVAL;
741 : : }
742 : 0 : scale_freq_min[lcore] = min;
743 : :
744 : 0 : return 0;
745 : : }
746 : :
747 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_set_scaling_freq_max)
748 : : int
749 : 0 : rte_power_pmd_mgmt_set_scaling_freq_max(unsigned int lcore, unsigned int max)
750 : : {
751 [ # # ]: 0 : if (!rte_lcore_is_enabled(lcore)) {
752 : 0 : POWER_LOG(ERR, "lcore id %u is not enabled", lcore);
753 : 0 : return -EINVAL;
754 : : }
755 : :
756 : : /* Zero means 'not set'. Use UINT32_MAX to enable RTE_MIN/MAX macro use when scaling. */
757 [ # # ]: 0 : if (max == 0)
758 : : max = UINT32_MAX;
759 [ # # ]: 0 : if (max < scale_freq_min[lcore]) {
760 : 0 : POWER_LOG(ERR, "Invalid max frequency: Cannot be less than min frequency");
761 : 0 : return -EINVAL;
762 : : }
763 : :
764 : 0 : scale_freq_max[lcore] = max;
765 : :
766 : 0 : return 0;
767 : : }
768 : :
769 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_get_scaling_freq_min)
770 : : int
771 : 0 : rte_power_pmd_mgmt_get_scaling_freq_min(unsigned int lcore)
772 : : {
773 [ # # ]: 0 : if (!rte_lcore_is_enabled(lcore)) {
774 : 0 : POWER_LOG(ERR, "lcore id %u is not enabled", lcore);
775 : 0 : return -EINVAL;
776 : : }
777 : :
778 [ # # ]: 0 : if (scale_freq_max[lcore] == 0)
779 : 0 : POWER_LOG(DEBUG, "Scaling freq min config not set. Using sysfs min freq.");
780 : :
781 : 0 : return scale_freq_min[lcore];
782 : : }
783 : :
784 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_get_scaling_freq_max)
785 : : int
786 : 0 : rte_power_pmd_mgmt_get_scaling_freq_max(unsigned int lcore)
787 : : {
788 [ # # ]: 0 : if (!rte_lcore_is_enabled(lcore)) {
789 : 0 : POWER_LOG(ERR, "lcore id %u is not enabled", lcore);
790 : 0 : return -EINVAL;
791 : : }
792 : :
793 [ # # ]: 0 : if (scale_freq_max[lcore] == UINT32_MAX) {
794 : 0 : POWER_LOG(DEBUG, "Scaling freq max config not set. Using sysfs max freq.");
795 : 0 : return 0;
796 : : }
797 : :
798 : 0 : return scale_freq_max[lcore];
799 : : }
800 : :
801 : 303 : RTE_INIT(rte_power_ethdev_pmgmt_init) {
802 : : int i;
803 : :
804 : : /* initialize config defaults */
805 : 303 : emptypoll_max = 512;
806 : 303 : pause_duration = 1;
807 : : /* scaling defaults out of range to ensure not used unless set by user or app */
808 [ + + ]: 39087 : for (i = 0; i < RTE_MAX_LCORE; i++) {
809 : 38784 : scale_freq_min[i] = 0;
810 : 38784 : scale_freq_max[i] = UINT32_MAX;
811 : : }
812 : 303 : }
|