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 : uint32_t pause_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 * pause_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 * pause_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 || lcore_id >= RTE_MAX_LCORE) {
515 : : ret = -EINVAL;
516 : 0 : goto end;
517 : : }
518 : :
519 [ # # ]: 0 : if (rte_eth_dev_info_get(port_id, &info) < 0) {
520 : : ret = -EINVAL;
521 : 0 : goto end;
522 : : }
523 : :
524 : : /* check if queue id is valid */
525 [ # # ]: 0 : if (queue_id >= info.nb_rx_queues) {
526 : : ret = -EINVAL;
527 : 0 : goto end;
528 : : }
529 : :
530 : : /* check if the queue is stopped */
531 : 0 : ret = queue_stopped(port_id, queue_id);
532 [ # # ]: 0 : if (ret != 1) {
533 : : /* error means invalid queue, 0 means queue wasn't stopped */
534 [ # # ]: 0 : ret = ret < 0 ? -EINVAL : -EBUSY;
535 : 0 : goto end;
536 : : }
537 : :
538 : 0 : init_lcore_cfgs();
539 : 0 : lcore_cfg = RTE_LCORE_VAR_LCORE(lcore_id, lcore_cfgs);
540 : :
541 : : /* check if other queues are stopped as well */
542 : 0 : ret = cfg_queues_stopped(lcore_cfg);
543 [ # # ]: 0 : if (ret != 1) {
544 : : /* error means invalid queue, 0 means queue wasn't stopped */
545 [ # # ]: 0 : ret = ret < 0 ? -EINVAL : -EBUSY;
546 : 0 : goto end;
547 : : }
548 : :
549 : : /* if callback was already enabled, check current callback type */
550 [ # # ]: 0 : if (lcore_cfg->pwr_mgmt_state != PMD_MGMT_DISABLED &&
551 [ # # ]: 0 : lcore_cfg->cb_mode != mode) {
552 : : ret = -EINVAL;
553 : 0 : goto end;
554 : : }
555 : :
556 : : /* we need this in various places */
557 : 0 : rte_cpu_get_intrinsics_support(&global_data.intrinsics_support);
558 : :
559 [ # # # # ]: 0 : switch (mode) {
560 : 0 : case RTE_POWER_MGMT_TYPE_MONITOR:
561 : : /* check if we can add a new queue */
562 : 0 : ret = check_monitor(lcore_cfg, &qdata);
563 [ # # ]: 0 : if (ret < 0)
564 : 0 : goto end;
565 : :
566 : : clb = get_monitor_callback();
567 : : break;
568 : 0 : case RTE_POWER_MGMT_TYPE_SCALE:
569 : : clb = clb_scale_freq;
570 : :
571 : : /* we only have to check this when enabling first queue */
572 [ # # ]: 0 : if (lcore_cfg->pwr_mgmt_state != PMD_MGMT_DISABLED)
573 : : break;
574 : : /* check if we can add a new queue */
575 : 0 : ret = check_scale(lcore_id);
576 [ # # ]: 0 : if (ret < 0)
577 : 0 : goto end;
578 : : break;
579 : 0 : case RTE_POWER_MGMT_TYPE_PAUSE:
580 : : /* figure out various time-to-tsc conversions */
581 [ # # ]: 0 : if (global_data.tsc_per_us == 0)
582 : 0 : calc_tsc();
583 : :
584 : : clb = clb_pause;
585 : : break;
586 : 0 : default:
587 : 0 : POWER_LOG(DEBUG, "Invalid power management type");
588 : : ret = -EINVAL;
589 : 0 : goto end;
590 : : }
591 : : /* add this queue to the list */
592 : 0 : ret = queue_list_add(lcore_cfg, &qdata);
593 [ # # ]: 0 : if (ret < 0) {
594 : 0 : POWER_LOG(DEBUG, "Failed to add queue to list: %s",
595 : : strerror(-ret));
596 : 0 : goto end;
597 : : }
598 : : /* new queue is always added last */
599 : 0 : queue_cfg = TAILQ_LAST(&lcore_cfg->head, queue_list_head);
600 : :
601 : : /* when enabling first queue, ensure sleep target is not 0 */
602 [ # # # # ]: 0 : if (lcore_cfg->n_queues == 1 && lcore_cfg->sleep_target == 0)
603 : 0 : lcore_cfg->sleep_target = 1;
604 : :
605 : : /* initialize data before enabling the callback */
606 [ # # ]: 0 : if (lcore_cfg->n_queues == 1) {
607 : 0 : lcore_cfg->cb_mode = mode;
608 : 0 : lcore_cfg->pwr_mgmt_state = PMD_MGMT_ENABLED;
609 : : }
610 : 0 : queue_cfg->cb = rte_eth_add_rx_callback(port_id, queue_id,
611 : : clb, queue_cfg);
612 : :
613 : : ret = 0;
614 : : end:
615 : : return ret;
616 : : }
617 : :
618 : : RTE_EXPORT_SYMBOL(rte_power_ethdev_pmgmt_queue_disable)
619 : : int
620 : 0 : rte_power_ethdev_pmgmt_queue_disable(unsigned int lcore_id,
621 : : uint16_t port_id, uint16_t queue_id)
622 : : {
623 : 0 : const union queue qdata = {.portid = port_id, .qid = queue_id};
624 : : struct pmd_core_cfg *lcore_cfg;
625 : : struct queue_list_entry *queue_cfg;
626 : : int ret;
627 : :
628 [ # # ]: 0 : RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
629 : :
630 [ # # ]: 0 : if (lcore_id >= RTE_MAX_LCORE || queue_id >= RTE_MAX_QUEUES_PER_PORT)
631 : : return -EINVAL;
632 : :
633 : : /* check if the queue is stopped */
634 : 0 : ret = queue_stopped(port_id, queue_id);
635 [ # # ]: 0 : if (ret != 1) {
636 : : /* error means invalid queue, 0 means queue wasn't stopped */
637 [ # # ]: 0 : return ret < 0 ? -EINVAL : -EBUSY;
638 : : }
639 : :
640 : : /* no need to check queue id as wrong queue id would not be enabled */
641 : :
642 : 0 : init_lcore_cfgs();
643 : 0 : lcore_cfg = RTE_LCORE_VAR_LCORE(lcore_id, lcore_cfgs);
644 : :
645 : : /* check if other queues are stopped as well */
646 : 0 : ret = cfg_queues_stopped(lcore_cfg);
647 [ # # ]: 0 : if (ret != 1) {
648 : : /* error means invalid queue, 0 means queue wasn't stopped */
649 [ # # ]: 0 : return ret < 0 ? -EINVAL : -EBUSY;
650 : : }
651 : :
652 [ # # ]: 0 : if (lcore_cfg->pwr_mgmt_state != PMD_MGMT_ENABLED)
653 : : return -EINVAL;
654 : :
655 : : /*
656 : : * There is no good/easy way to do this without race conditions, so we
657 : : * are just going to throw our hands in the air and hope that the user
658 : : * has read the documentation and has ensured that ports are stopped at
659 : : * the time we enter the API functions.
660 : : */
661 : 0 : queue_cfg = queue_list_take(lcore_cfg, &qdata);
662 [ # # ]: 0 : if (queue_cfg == NULL)
663 : : return -ENOENT;
664 : :
665 : : /* if we've removed all queues from the lists, set state to disabled */
666 [ # # ]: 0 : if (lcore_cfg->n_queues == 0)
667 : 0 : lcore_cfg->pwr_mgmt_state = PMD_MGMT_DISABLED;
668 : :
669 [ # # # ]: 0 : switch (lcore_cfg->cb_mode) {
670 : 0 : case RTE_POWER_MGMT_TYPE_MONITOR: /* fall-through */
671 : : case RTE_POWER_MGMT_TYPE_PAUSE:
672 : 0 : rte_eth_remove_rx_callback(port_id, queue_id, queue_cfg->cb);
673 : 0 : break;
674 : 0 : case RTE_POWER_MGMT_TYPE_SCALE:
675 : 0 : rte_eth_remove_rx_callback(port_id, queue_id, queue_cfg->cb);
676 : : /* disable power library on this lcore if this was last queue */
677 [ # # ]: 0 : if (lcore_cfg->pwr_mgmt_state == PMD_MGMT_DISABLED) {
678 : 0 : rte_power_freq_max(lcore_id);
679 : 0 : rte_power_exit(lcore_id);
680 : : }
681 : : break;
682 : : }
683 : : /*
684 : : * the API doc mandates that the user stops all processing on affected
685 : : * ports before calling any of these API's, so we can assume that the
686 : : * callbacks can be freed. we're intentionally casting away const-ness.
687 : : */
688 : 0 : rte_free((void *)(uintptr_t)queue_cfg->cb);
689 : 0 : free(queue_cfg);
690 : :
691 : 0 : return 0;
692 : : }
693 : :
694 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_set_emptypoll_max)
695 : : void
696 : 0 : rte_power_pmd_mgmt_set_emptypoll_max(unsigned int max)
697 : : {
698 : 0 : emptypoll_max = max;
699 : 0 : }
700 : :
701 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_get_emptypoll_max)
702 : : unsigned int
703 : 0 : rte_power_pmd_mgmt_get_emptypoll_max(void)
704 : : {
705 : 0 : return emptypoll_max;
706 : : }
707 : :
708 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_set_pause_duration)
709 : : int
710 : 0 : rte_power_pmd_mgmt_set_pause_duration(unsigned int duration)
711 : : {
712 [ # # ]: 0 : if (duration == 0) {
713 : 0 : POWER_LOG(ERR, "Pause duration must be greater than 0, value unchanged");
714 : 0 : return -EINVAL;
715 : : }
716 : 0 : pause_duration = duration;
717 : :
718 : 0 : return 0;
719 : : }
720 : :
721 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_get_pause_duration)
722 : : unsigned int
723 : 0 : rte_power_pmd_mgmt_get_pause_duration(void)
724 : : {
725 : 0 : return pause_duration;
726 : : }
727 : :
728 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_set_scaling_freq_min)
729 : : int
730 : 0 : rte_power_pmd_mgmt_set_scaling_freq_min(unsigned int lcore, unsigned int min)
731 : : {
732 [ # # ]: 0 : if (lcore >= RTE_MAX_LCORE) {
733 : 0 : POWER_LOG(ERR, "Invalid lcore ID: %u", lcore);
734 : 0 : return -EINVAL;
735 : : }
736 : :
737 [ # # ]: 0 : if (min > scale_freq_max[lcore]) {
738 : 0 : POWER_LOG(ERR, "Invalid min frequency: Cannot be greater than max frequency");
739 : 0 : return -EINVAL;
740 : : }
741 : 0 : scale_freq_min[lcore] = min;
742 : :
743 : 0 : return 0;
744 : : }
745 : :
746 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_set_scaling_freq_max)
747 : : int
748 : 0 : rte_power_pmd_mgmt_set_scaling_freq_max(unsigned int lcore, unsigned int max)
749 : : {
750 [ # # ]: 0 : if (lcore >= RTE_MAX_LCORE) {
751 : 0 : POWER_LOG(ERR, "Invalid lcore ID: %u", lcore);
752 : 0 : return -EINVAL;
753 : : }
754 : :
755 : : /* Zero means 'not set'. Use UINT32_MAX to enable RTE_MIN/MAX macro use when scaling. */
756 [ # # ]: 0 : if (max == 0)
757 : : max = UINT32_MAX;
758 [ # # ]: 0 : if (max < scale_freq_min[lcore]) {
759 : 0 : POWER_LOG(ERR, "Invalid max frequency: Cannot be less than min frequency");
760 : 0 : return -EINVAL;
761 : : }
762 : :
763 : 0 : scale_freq_max[lcore] = max;
764 : :
765 : 0 : return 0;
766 : : }
767 : :
768 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_get_scaling_freq_min)
769 : : int
770 : 0 : rte_power_pmd_mgmt_get_scaling_freq_min(unsigned int lcore)
771 : : {
772 [ # # ]: 0 : if (lcore >= RTE_MAX_LCORE) {
773 : 0 : POWER_LOG(ERR, "Invalid lcore ID: %u", lcore);
774 : 0 : return -EINVAL;
775 : : }
776 : :
777 [ # # ]: 0 : if (scale_freq_max[lcore] == 0)
778 : 0 : POWER_LOG(DEBUG, "Scaling freq min config not set. Using sysfs min freq.");
779 : :
780 : 0 : return scale_freq_min[lcore];
781 : : }
782 : :
783 : : RTE_EXPORT_SYMBOL(rte_power_pmd_mgmt_get_scaling_freq_max)
784 : : int
785 : 0 : rte_power_pmd_mgmt_get_scaling_freq_max(unsigned int lcore)
786 : : {
787 [ # # ]: 0 : if (lcore >= RTE_MAX_LCORE) {
788 : 0 : POWER_LOG(ERR, "Invalid lcore ID: %u", lcore);
789 : 0 : return -EINVAL;
790 : : }
791 : :
792 [ # # ]: 0 : if (scale_freq_max[lcore] == UINT32_MAX) {
793 : 0 : POWER_LOG(DEBUG, "Scaling freq max config not set. Using sysfs max freq.");
794 : 0 : return 0;
795 : : }
796 : :
797 : 0 : return scale_freq_max[lcore];
798 : : }
799 : :
800 : 252 : RTE_INIT(rte_power_ethdev_pmgmt_init) {
801 : : int i;
802 : :
803 : : /* initialize config defaults */
804 : 252 : emptypoll_max = 512;
805 : 252 : pause_duration = 1;
806 : : /* scaling defaults out of range to ensure not used unless set by user or app */
807 [ + + ]: 32508 : for (i = 0; i < RTE_MAX_LCORE; i++) {
808 : 32256 : scale_freq_min[i] = 0;
809 : 32256 : scale_freq_max[i] = UINT32_MAX;
810 : : }
811 : 252 : }
|