199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 299a2dd95SBruce Richardson * Copyright(c) 2017-2018 Intel Corporation. 399a2dd95SBruce Richardson * All rights reserved. 499a2dd95SBruce Richardson */ 599a2dd95SBruce Richardson 672b452c5SDmitry Kozlyuk #include <ctype.h> 799a2dd95SBruce Richardson #include <string.h> 899a2dd95SBruce Richardson #include <inttypes.h> 999a2dd95SBruce Richardson #include <stdbool.h> 1072b452c5SDmitry Kozlyuk #include <stdlib.h> 1199a2dd95SBruce Richardson 1299a2dd95SBruce Richardson #include <rte_memzone.h> 1399a2dd95SBruce Richardson #include <rte_errno.h> 1499a2dd95SBruce Richardson #include <rte_malloc.h> 1599a2dd95SBruce Richardson #include <rte_mempool.h> 1699a2dd95SBruce Richardson #include <rte_common.h> 1799a2dd95SBruce Richardson #include <rte_timer.h> 1899a2dd95SBruce Richardson #include <rte_service_component.h> 19791dfec2SAnkur Dwivedi #include <rte_telemetry.h> 2099a2dd95SBruce Richardson 2153548ad3SPavan Nikhilesh #include "event_timer_adapter_pmd.h" 2299a2dd95SBruce Richardson #include "eventdev_pmd.h" 2399a2dd95SBruce Richardson #include "rte_event_timer_adapter.h" 2453548ad3SPavan Nikhilesh #include "rte_eventdev.h" 25f26f2ca6SPavan Nikhilesh #include "eventdev_trace.h" 2699a2dd95SBruce Richardson 2799a2dd95SBruce Richardson #define DATA_MZ_NAME_MAX_LEN 64 2899a2dd95SBruce Richardson #define DATA_MZ_NAME_FORMAT "rte_event_timer_adapter_data_%d" 2999a2dd95SBruce Richardson 30eeded204SDavid Marchand RTE_LOG_REGISTER_SUFFIX(evtim_logtype, adapter.timer, NOTICE); 31eeded204SDavid Marchand RTE_LOG_REGISTER_SUFFIX(evtim_buffer_logtype, adapter.timer, NOTICE); 32eeded204SDavid Marchand RTE_LOG_REGISTER_SUFFIX(evtim_svc_logtype, adapter.timer.svc, NOTICE); 3399a2dd95SBruce Richardson 34f3f3a917SPavan Nikhilesh static struct rte_event_timer_adapter *adapters; 3599a2dd95SBruce Richardson 3653548ad3SPavan Nikhilesh static const struct event_timer_adapter_ops swtim_ops; 3799a2dd95SBruce Richardson 3899a2dd95SBruce Richardson #define EVTIM_LOG(level, logtype, ...) \ 3999a2dd95SBruce Richardson rte_log(RTE_LOG_ ## level, logtype, \ 4099a2dd95SBruce Richardson RTE_FMT("EVTIMER: %s() line %u: " RTE_FMT_HEAD(__VA_ARGS__,) \ 4199a2dd95SBruce Richardson "\n", __func__, __LINE__, RTE_FMT_TAIL(__VA_ARGS__,))) 4299a2dd95SBruce Richardson 4399a2dd95SBruce Richardson #define EVTIM_LOG_ERR(...) EVTIM_LOG(ERR, evtim_logtype, __VA_ARGS__) 4499a2dd95SBruce Richardson 4599a2dd95SBruce Richardson #ifdef RTE_LIBRTE_EVENTDEV_DEBUG 4699a2dd95SBruce Richardson #define EVTIM_LOG_DBG(...) \ 4799a2dd95SBruce Richardson EVTIM_LOG(DEBUG, evtim_logtype, __VA_ARGS__) 4899a2dd95SBruce Richardson #define EVTIM_BUF_LOG_DBG(...) \ 4999a2dd95SBruce Richardson EVTIM_LOG(DEBUG, evtim_buffer_logtype, __VA_ARGS__) 5099a2dd95SBruce Richardson #define EVTIM_SVC_LOG_DBG(...) \ 5199a2dd95SBruce Richardson EVTIM_LOG(DEBUG, evtim_svc_logtype, __VA_ARGS__) 5299a2dd95SBruce Richardson #else 5399a2dd95SBruce Richardson #define EVTIM_LOG_DBG(...) (void)0 5499a2dd95SBruce Richardson #define EVTIM_BUF_LOG_DBG(...) (void)0 5599a2dd95SBruce Richardson #define EVTIM_SVC_LOG_DBG(...) (void)0 5699a2dd95SBruce Richardson #endif 5799a2dd95SBruce Richardson 58*3d9d8adfSNaga Harish K S V static inline enum rte_timer_type 59*3d9d8adfSNaga Harish K S V get_timer_type(const struct rte_event_timer_adapter *adapter) 60*3d9d8adfSNaga Harish K S V { 61*3d9d8adfSNaga Harish K S V return (adapter->data->conf.flags & 62*3d9d8adfSNaga Harish K S V RTE_EVENT_TIMER_ADAPTER_F_PERIODIC) ? 63*3d9d8adfSNaga Harish K S V PERIODICAL : SINGLE; 64*3d9d8adfSNaga Harish K S V } 65*3d9d8adfSNaga Harish K S V 6699a2dd95SBruce Richardson static int 6799a2dd95SBruce Richardson default_port_conf_cb(uint16_t id, uint8_t event_dev_id, uint8_t *event_port_id, 6899a2dd95SBruce Richardson void *conf_arg) 6999a2dd95SBruce Richardson { 7099a2dd95SBruce Richardson struct rte_event_timer_adapter *adapter; 7199a2dd95SBruce Richardson struct rte_eventdev *dev; 7299a2dd95SBruce Richardson struct rte_event_dev_config dev_conf; 7399a2dd95SBruce Richardson struct rte_event_port_conf *port_conf, def_port_conf = {0}; 7499a2dd95SBruce Richardson int started; 7599a2dd95SBruce Richardson uint8_t port_id; 7699a2dd95SBruce Richardson uint8_t dev_id; 7799a2dd95SBruce Richardson int ret; 7899a2dd95SBruce Richardson 7999a2dd95SBruce Richardson RTE_SET_USED(event_dev_id); 8099a2dd95SBruce Richardson 8199a2dd95SBruce Richardson adapter = &adapters[id]; 8299a2dd95SBruce Richardson dev = &rte_eventdevs[adapter->data->event_dev_id]; 8399a2dd95SBruce Richardson dev_id = dev->data->dev_id; 8499a2dd95SBruce Richardson dev_conf = dev->data->dev_conf; 8599a2dd95SBruce Richardson 8699a2dd95SBruce Richardson started = dev->data->dev_started; 8799a2dd95SBruce Richardson if (started) 8899a2dd95SBruce Richardson rte_event_dev_stop(dev_id); 8999a2dd95SBruce Richardson 9099a2dd95SBruce Richardson port_id = dev_conf.nb_event_ports; 9199a2dd95SBruce Richardson dev_conf.nb_event_ports += 1; 9299a2dd95SBruce Richardson ret = rte_event_dev_configure(dev_id, &dev_conf); 9399a2dd95SBruce Richardson if (ret < 0) { 9499a2dd95SBruce Richardson EVTIM_LOG_ERR("failed to configure event dev %u\n", dev_id); 9599a2dd95SBruce Richardson if (started) 9699a2dd95SBruce Richardson if (rte_event_dev_start(dev_id)) 9799a2dd95SBruce Richardson return -EIO; 9899a2dd95SBruce Richardson 9999a2dd95SBruce Richardson return ret; 10099a2dd95SBruce Richardson } 10199a2dd95SBruce Richardson 10299a2dd95SBruce Richardson if (conf_arg != NULL) 10399a2dd95SBruce Richardson port_conf = conf_arg; 10499a2dd95SBruce Richardson else { 10599a2dd95SBruce Richardson port_conf = &def_port_conf; 10699a2dd95SBruce Richardson ret = rte_event_port_default_conf_get(dev_id, port_id, 10799a2dd95SBruce Richardson port_conf); 10899a2dd95SBruce Richardson if (ret < 0) 10999a2dd95SBruce Richardson return ret; 11099a2dd95SBruce Richardson } 11199a2dd95SBruce Richardson 11299a2dd95SBruce Richardson ret = rte_event_port_setup(dev_id, port_id, port_conf); 11399a2dd95SBruce Richardson if (ret < 0) { 11499a2dd95SBruce Richardson EVTIM_LOG_ERR("failed to setup event port %u on event dev %u\n", 11599a2dd95SBruce Richardson port_id, dev_id); 11699a2dd95SBruce Richardson return ret; 11799a2dd95SBruce Richardson } 11899a2dd95SBruce Richardson 11999a2dd95SBruce Richardson *event_port_id = port_id; 12099a2dd95SBruce Richardson 12199a2dd95SBruce Richardson if (started) 12299a2dd95SBruce Richardson ret = rte_event_dev_start(dev_id); 12399a2dd95SBruce Richardson 12499a2dd95SBruce Richardson return ret; 12599a2dd95SBruce Richardson } 12699a2dd95SBruce Richardson 12799a2dd95SBruce Richardson struct rte_event_timer_adapter * 12899a2dd95SBruce Richardson rte_event_timer_adapter_create(const struct rte_event_timer_adapter_conf *conf) 12999a2dd95SBruce Richardson { 13099a2dd95SBruce Richardson return rte_event_timer_adapter_create_ext(conf, default_port_conf_cb, 13199a2dd95SBruce Richardson NULL); 13299a2dd95SBruce Richardson } 13399a2dd95SBruce Richardson 13499a2dd95SBruce Richardson struct rte_event_timer_adapter * 13599a2dd95SBruce Richardson rte_event_timer_adapter_create_ext( 13699a2dd95SBruce Richardson const struct rte_event_timer_adapter_conf *conf, 13799a2dd95SBruce Richardson rte_event_timer_adapter_port_conf_cb_t conf_cb, 13899a2dd95SBruce Richardson void *conf_arg) 13999a2dd95SBruce Richardson { 14099a2dd95SBruce Richardson uint16_t adapter_id; 14199a2dd95SBruce Richardson struct rte_event_timer_adapter *adapter; 14299a2dd95SBruce Richardson const struct rte_memzone *mz; 14399a2dd95SBruce Richardson char mz_name[DATA_MZ_NAME_MAX_LEN]; 14499a2dd95SBruce Richardson int n, ret; 14599a2dd95SBruce Richardson struct rte_eventdev *dev; 14699a2dd95SBruce Richardson 147f3f3a917SPavan Nikhilesh if (adapters == NULL) { 148f3f3a917SPavan Nikhilesh adapters = rte_zmalloc("Eventdev", 149f3f3a917SPavan Nikhilesh sizeof(struct rte_event_timer_adapter) * 150f3f3a917SPavan Nikhilesh RTE_EVENT_TIMER_ADAPTER_NUM_MAX, 151f3f3a917SPavan Nikhilesh RTE_CACHE_LINE_SIZE); 152f3f3a917SPavan Nikhilesh if (adapters == NULL) { 153f3f3a917SPavan Nikhilesh rte_errno = ENOMEM; 154f3f3a917SPavan Nikhilesh return NULL; 155f3f3a917SPavan Nikhilesh } 156f3f3a917SPavan Nikhilesh } 157f3f3a917SPavan Nikhilesh 15899a2dd95SBruce Richardson if (conf == NULL) { 15999a2dd95SBruce Richardson rte_errno = EINVAL; 16099a2dd95SBruce Richardson return NULL; 16199a2dd95SBruce Richardson } 16299a2dd95SBruce Richardson 16399a2dd95SBruce Richardson /* Check eventdev ID */ 16499a2dd95SBruce Richardson if (!rte_event_pmd_is_valid_dev(conf->event_dev_id)) { 16599a2dd95SBruce Richardson rte_errno = EINVAL; 16699a2dd95SBruce Richardson return NULL; 16799a2dd95SBruce Richardson } 16899a2dd95SBruce Richardson dev = &rte_eventdevs[conf->event_dev_id]; 16999a2dd95SBruce Richardson 17099a2dd95SBruce Richardson adapter_id = conf->timer_adapter_id; 17199a2dd95SBruce Richardson 17299a2dd95SBruce Richardson /* Check that adapter_id is in range */ 17399a2dd95SBruce Richardson if (adapter_id >= RTE_EVENT_TIMER_ADAPTER_NUM_MAX) { 17499a2dd95SBruce Richardson rte_errno = EINVAL; 17599a2dd95SBruce Richardson return NULL; 17699a2dd95SBruce Richardson } 17799a2dd95SBruce Richardson 17899a2dd95SBruce Richardson /* Check adapter ID not already allocated */ 17999a2dd95SBruce Richardson adapter = &adapters[adapter_id]; 18099a2dd95SBruce Richardson if (adapter->allocated) { 18199a2dd95SBruce Richardson rte_errno = EEXIST; 18299a2dd95SBruce Richardson return NULL; 18399a2dd95SBruce Richardson } 18499a2dd95SBruce Richardson 18599a2dd95SBruce Richardson /* Create shared data area. */ 18699a2dd95SBruce Richardson n = snprintf(mz_name, sizeof(mz_name), DATA_MZ_NAME_FORMAT, adapter_id); 18799a2dd95SBruce Richardson if (n >= (int)sizeof(mz_name)) { 18899a2dd95SBruce Richardson rte_errno = EINVAL; 18999a2dd95SBruce Richardson return NULL; 19099a2dd95SBruce Richardson } 19199a2dd95SBruce Richardson mz = rte_memzone_reserve(mz_name, 19299a2dd95SBruce Richardson sizeof(struct rte_event_timer_adapter_data), 19399a2dd95SBruce Richardson conf->socket_id, 0); 19499a2dd95SBruce Richardson if (mz == NULL) 19599a2dd95SBruce Richardson /* rte_errno set by rte_memzone_reserve */ 19699a2dd95SBruce Richardson return NULL; 19799a2dd95SBruce Richardson 19899a2dd95SBruce Richardson adapter->data = mz->addr; 19999a2dd95SBruce Richardson memset(adapter->data, 0, sizeof(struct rte_event_timer_adapter_data)); 20099a2dd95SBruce Richardson 20199a2dd95SBruce Richardson adapter->data->mz = mz; 20299a2dd95SBruce Richardson adapter->data->event_dev_id = conf->event_dev_id; 20399a2dd95SBruce Richardson adapter->data->id = adapter_id; 20499a2dd95SBruce Richardson adapter->data->socket_id = conf->socket_id; 20599a2dd95SBruce Richardson adapter->data->conf = *conf; /* copy conf structure */ 20699a2dd95SBruce Richardson 20799a2dd95SBruce Richardson /* Query eventdev PMD for timer adapter capabilities and ops */ 208*3d9d8adfSNaga Harish K S V if (dev->dev_ops->timer_adapter_caps_get) { 20999a2dd95SBruce Richardson ret = dev->dev_ops->timer_adapter_caps_get(dev, 21099a2dd95SBruce Richardson adapter->data->conf.flags, 211*3d9d8adfSNaga Harish K S V &adapter->data->caps, &adapter->ops); 21299a2dd95SBruce Richardson if (ret < 0) { 21399a2dd95SBruce Richardson rte_errno = -ret; 21499a2dd95SBruce Richardson goto free_memzone; 21599a2dd95SBruce Richardson } 216*3d9d8adfSNaga Harish K S V } 21799a2dd95SBruce Richardson 21899a2dd95SBruce Richardson if (!(adapter->data->caps & 21999a2dd95SBruce Richardson RTE_EVENT_TIMER_ADAPTER_CAP_INTERNAL_PORT)) { 22099a2dd95SBruce Richardson FUNC_PTR_OR_NULL_RET_WITH_ERRNO(conf_cb, EINVAL); 22199a2dd95SBruce Richardson ret = conf_cb(adapter->data->id, adapter->data->event_dev_id, 22299a2dd95SBruce Richardson &adapter->data->event_port_id, conf_arg); 22399a2dd95SBruce Richardson if (ret < 0) { 22499a2dd95SBruce Richardson rte_errno = -ret; 22599a2dd95SBruce Richardson goto free_memzone; 22699a2dd95SBruce Richardson } 22799a2dd95SBruce Richardson } 22899a2dd95SBruce Richardson 22999a2dd95SBruce Richardson /* If eventdev PMD did not provide ops, use default software 23099a2dd95SBruce Richardson * implementation. 23199a2dd95SBruce Richardson */ 23299a2dd95SBruce Richardson if (adapter->ops == NULL) 23399a2dd95SBruce Richardson adapter->ops = &swtim_ops; 23499a2dd95SBruce Richardson 23599a2dd95SBruce Richardson /* Allow driver to do some setup */ 23699a2dd95SBruce Richardson FUNC_PTR_OR_NULL_RET_WITH_ERRNO(adapter->ops->init, ENOTSUP); 23799a2dd95SBruce Richardson ret = adapter->ops->init(adapter); 23899a2dd95SBruce Richardson if (ret < 0) { 23999a2dd95SBruce Richardson rte_errno = -ret; 24099a2dd95SBruce Richardson goto free_memzone; 24199a2dd95SBruce Richardson } 24299a2dd95SBruce Richardson 24399a2dd95SBruce Richardson /* Set fast-path function pointers */ 24499a2dd95SBruce Richardson adapter->arm_burst = adapter->ops->arm_burst; 24599a2dd95SBruce Richardson adapter->arm_tmo_tick_burst = adapter->ops->arm_tmo_tick_burst; 24699a2dd95SBruce Richardson adapter->cancel_burst = adapter->ops->cancel_burst; 24799a2dd95SBruce Richardson 24899a2dd95SBruce Richardson adapter->allocated = 1; 24999a2dd95SBruce Richardson 25099a2dd95SBruce Richardson rte_eventdev_trace_timer_adapter_create(adapter_id, adapter, conf, 25199a2dd95SBruce Richardson conf_cb); 25299a2dd95SBruce Richardson return adapter; 25399a2dd95SBruce Richardson 25499a2dd95SBruce Richardson free_memzone: 25599a2dd95SBruce Richardson rte_memzone_free(adapter->data->mz); 25699a2dd95SBruce Richardson return NULL; 25799a2dd95SBruce Richardson } 25899a2dd95SBruce Richardson 25999a2dd95SBruce Richardson int 26099a2dd95SBruce Richardson rte_event_timer_adapter_get_info(const struct rte_event_timer_adapter *adapter, 26199a2dd95SBruce Richardson struct rte_event_timer_adapter_info *adapter_info) 26299a2dd95SBruce Richardson { 26399a2dd95SBruce Richardson ADAPTER_VALID_OR_ERR_RET(adapter, -EINVAL); 26499a2dd95SBruce Richardson 26599a2dd95SBruce Richardson if (adapter->ops->get_info) 26699a2dd95SBruce Richardson /* let driver set values it knows */ 26799a2dd95SBruce Richardson adapter->ops->get_info(adapter, adapter_info); 26899a2dd95SBruce Richardson 26999a2dd95SBruce Richardson /* Set common values */ 27099a2dd95SBruce Richardson adapter_info->conf = adapter->data->conf; 27199a2dd95SBruce Richardson adapter_info->event_dev_port_id = adapter->data->event_port_id; 27299a2dd95SBruce Richardson adapter_info->caps = adapter->data->caps; 27399a2dd95SBruce Richardson 27499a2dd95SBruce Richardson return 0; 27599a2dd95SBruce Richardson } 27699a2dd95SBruce Richardson 27799a2dd95SBruce Richardson int 27899a2dd95SBruce Richardson rte_event_timer_adapter_start(const struct rte_event_timer_adapter *adapter) 27999a2dd95SBruce Richardson { 28099a2dd95SBruce Richardson int ret; 28199a2dd95SBruce Richardson 28299a2dd95SBruce Richardson ADAPTER_VALID_OR_ERR_RET(adapter, -EINVAL); 28399a2dd95SBruce Richardson FUNC_PTR_OR_ERR_RET(adapter->ops->start, -EINVAL); 28499a2dd95SBruce Richardson 28599a2dd95SBruce Richardson if (adapter->data->started) { 28699a2dd95SBruce Richardson EVTIM_LOG_ERR("event timer adapter %"PRIu8" already started", 28799a2dd95SBruce Richardson adapter->data->id); 28899a2dd95SBruce Richardson return -EALREADY; 28999a2dd95SBruce Richardson } 29099a2dd95SBruce Richardson 29199a2dd95SBruce Richardson ret = adapter->ops->start(adapter); 29299a2dd95SBruce Richardson if (ret < 0) 29399a2dd95SBruce Richardson return ret; 29499a2dd95SBruce Richardson 29599a2dd95SBruce Richardson adapter->data->started = 1; 29699a2dd95SBruce Richardson rte_eventdev_trace_timer_adapter_start(adapter); 29799a2dd95SBruce Richardson return 0; 29899a2dd95SBruce Richardson } 29999a2dd95SBruce Richardson 30099a2dd95SBruce Richardson int 30199a2dd95SBruce Richardson rte_event_timer_adapter_stop(const struct rte_event_timer_adapter *adapter) 30299a2dd95SBruce Richardson { 30399a2dd95SBruce Richardson int ret; 30499a2dd95SBruce Richardson 30599a2dd95SBruce Richardson ADAPTER_VALID_OR_ERR_RET(adapter, -EINVAL); 30699a2dd95SBruce Richardson FUNC_PTR_OR_ERR_RET(adapter->ops->stop, -EINVAL); 30799a2dd95SBruce Richardson 30899a2dd95SBruce Richardson if (adapter->data->started == 0) { 30999a2dd95SBruce Richardson EVTIM_LOG_ERR("event timer adapter %"PRIu8" already stopped", 31099a2dd95SBruce Richardson adapter->data->id); 31199a2dd95SBruce Richardson return 0; 31299a2dd95SBruce Richardson } 31399a2dd95SBruce Richardson 31499a2dd95SBruce Richardson ret = adapter->ops->stop(adapter); 31599a2dd95SBruce Richardson if (ret < 0) 31699a2dd95SBruce Richardson return ret; 31799a2dd95SBruce Richardson 31899a2dd95SBruce Richardson adapter->data->started = 0; 31999a2dd95SBruce Richardson rte_eventdev_trace_timer_adapter_stop(adapter); 32099a2dd95SBruce Richardson return 0; 32199a2dd95SBruce Richardson } 32299a2dd95SBruce Richardson 32399a2dd95SBruce Richardson struct rte_event_timer_adapter * 32499a2dd95SBruce Richardson rte_event_timer_adapter_lookup(uint16_t adapter_id) 32599a2dd95SBruce Richardson { 32699a2dd95SBruce Richardson char name[DATA_MZ_NAME_MAX_LEN]; 32799a2dd95SBruce Richardson const struct rte_memzone *mz; 32899a2dd95SBruce Richardson struct rte_event_timer_adapter_data *data; 32999a2dd95SBruce Richardson struct rte_event_timer_adapter *adapter; 33099a2dd95SBruce Richardson int ret; 33199a2dd95SBruce Richardson struct rte_eventdev *dev; 33299a2dd95SBruce Richardson 333f3f3a917SPavan Nikhilesh if (adapters == NULL) { 334f3f3a917SPavan Nikhilesh adapters = rte_zmalloc("Eventdev", 335f3f3a917SPavan Nikhilesh sizeof(struct rte_event_timer_adapter) * 336f3f3a917SPavan Nikhilesh RTE_EVENT_TIMER_ADAPTER_NUM_MAX, 337f3f3a917SPavan Nikhilesh RTE_CACHE_LINE_SIZE); 338f3f3a917SPavan Nikhilesh if (adapters == NULL) { 339f3f3a917SPavan Nikhilesh rte_errno = ENOMEM; 340f3f3a917SPavan Nikhilesh return NULL; 341f3f3a917SPavan Nikhilesh } 342f3f3a917SPavan Nikhilesh } 343f3f3a917SPavan Nikhilesh 34499a2dd95SBruce Richardson if (adapters[adapter_id].allocated) 34599a2dd95SBruce Richardson return &adapters[adapter_id]; /* Adapter is already loaded */ 34699a2dd95SBruce Richardson 34799a2dd95SBruce Richardson snprintf(name, DATA_MZ_NAME_MAX_LEN, DATA_MZ_NAME_FORMAT, adapter_id); 34899a2dd95SBruce Richardson mz = rte_memzone_lookup(name); 34999a2dd95SBruce Richardson if (mz == NULL) { 35099a2dd95SBruce Richardson rte_errno = ENOENT; 35199a2dd95SBruce Richardson return NULL; 35299a2dd95SBruce Richardson } 35399a2dd95SBruce Richardson 35499a2dd95SBruce Richardson data = mz->addr; 35599a2dd95SBruce Richardson 35699a2dd95SBruce Richardson adapter = &adapters[data->id]; 35799a2dd95SBruce Richardson adapter->data = data; 35899a2dd95SBruce Richardson 35999a2dd95SBruce Richardson dev = &rte_eventdevs[adapter->data->event_dev_id]; 36099a2dd95SBruce Richardson 36199a2dd95SBruce Richardson /* Query eventdev PMD for timer adapter capabilities and ops */ 362*3d9d8adfSNaga Harish K S V if (dev->dev_ops->timer_adapter_caps_get) { 36399a2dd95SBruce Richardson ret = dev->dev_ops->timer_adapter_caps_get(dev, 36499a2dd95SBruce Richardson adapter->data->conf.flags, 365*3d9d8adfSNaga Harish K S V &adapter->data->caps, &adapter->ops); 36699a2dd95SBruce Richardson if (ret < 0) { 36799a2dd95SBruce Richardson rte_errno = EINVAL; 36899a2dd95SBruce Richardson return NULL; 36999a2dd95SBruce Richardson } 370*3d9d8adfSNaga Harish K S V } 37199a2dd95SBruce Richardson 37299a2dd95SBruce Richardson /* If eventdev PMD did not provide ops, use default software 37399a2dd95SBruce Richardson * implementation. 37499a2dd95SBruce Richardson */ 37599a2dd95SBruce Richardson if (adapter->ops == NULL) 37699a2dd95SBruce Richardson adapter->ops = &swtim_ops; 37799a2dd95SBruce Richardson 37899a2dd95SBruce Richardson /* Set fast-path function pointers */ 37999a2dd95SBruce Richardson adapter->arm_burst = adapter->ops->arm_burst; 38099a2dd95SBruce Richardson adapter->arm_tmo_tick_burst = adapter->ops->arm_tmo_tick_burst; 38199a2dd95SBruce Richardson adapter->cancel_burst = adapter->ops->cancel_burst; 38299a2dd95SBruce Richardson 38399a2dd95SBruce Richardson adapter->allocated = 1; 38499a2dd95SBruce Richardson 38599a2dd95SBruce Richardson return adapter; 38699a2dd95SBruce Richardson } 38799a2dd95SBruce Richardson 38899a2dd95SBruce Richardson int 38999a2dd95SBruce Richardson rte_event_timer_adapter_free(struct rte_event_timer_adapter *adapter) 39099a2dd95SBruce Richardson { 391f3f3a917SPavan Nikhilesh int i, ret; 39299a2dd95SBruce Richardson 39399a2dd95SBruce Richardson ADAPTER_VALID_OR_ERR_RET(adapter, -EINVAL); 39499a2dd95SBruce Richardson FUNC_PTR_OR_ERR_RET(adapter->ops->uninit, -EINVAL); 39599a2dd95SBruce Richardson 39699a2dd95SBruce Richardson if (adapter->data->started == 1) { 39799a2dd95SBruce Richardson EVTIM_LOG_ERR("event timer adapter %"PRIu8" must be stopped " 39899a2dd95SBruce Richardson "before freeing", adapter->data->id); 39999a2dd95SBruce Richardson return -EBUSY; 40099a2dd95SBruce Richardson } 40199a2dd95SBruce Richardson 40299a2dd95SBruce Richardson /* free impl priv data */ 40399a2dd95SBruce Richardson ret = adapter->ops->uninit(adapter); 40499a2dd95SBruce Richardson if (ret < 0) 40599a2dd95SBruce Richardson return ret; 40699a2dd95SBruce Richardson 40799a2dd95SBruce Richardson /* free shared data area */ 40899a2dd95SBruce Richardson ret = rte_memzone_free(adapter->data->mz); 40999a2dd95SBruce Richardson if (ret < 0) 41099a2dd95SBruce Richardson return ret; 41199a2dd95SBruce Richardson 41299a2dd95SBruce Richardson adapter->data = NULL; 41399a2dd95SBruce Richardson adapter->allocated = 0; 41499a2dd95SBruce Richardson 415f3f3a917SPavan Nikhilesh ret = 0; 416f3f3a917SPavan Nikhilesh for (i = 0; i < RTE_EVENT_TIMER_ADAPTER_NUM_MAX; i++) 417f3f3a917SPavan Nikhilesh if (adapters[i].allocated) 418f3f3a917SPavan Nikhilesh ret = adapters[i].allocated; 419f3f3a917SPavan Nikhilesh 420f3f3a917SPavan Nikhilesh if (!ret) { 421f3f3a917SPavan Nikhilesh rte_free(adapters); 422f3f3a917SPavan Nikhilesh adapters = NULL; 423f3f3a917SPavan Nikhilesh } 424f3f3a917SPavan Nikhilesh 42599a2dd95SBruce Richardson rte_eventdev_trace_timer_adapter_free(adapter); 42699a2dd95SBruce Richardson return 0; 42799a2dd95SBruce Richardson } 42899a2dd95SBruce Richardson 42999a2dd95SBruce Richardson int 43099a2dd95SBruce Richardson rte_event_timer_adapter_service_id_get(struct rte_event_timer_adapter *adapter, 43199a2dd95SBruce Richardson uint32_t *service_id) 43299a2dd95SBruce Richardson { 43399a2dd95SBruce Richardson ADAPTER_VALID_OR_ERR_RET(adapter, -EINVAL); 43499a2dd95SBruce Richardson 43599a2dd95SBruce Richardson if (adapter->data->service_inited && service_id != NULL) 43699a2dd95SBruce Richardson *service_id = adapter->data->service_id; 43799a2dd95SBruce Richardson 43899a2dd95SBruce Richardson return adapter->data->service_inited ? 0 : -ESRCH; 43999a2dd95SBruce Richardson } 44099a2dd95SBruce Richardson 44199a2dd95SBruce Richardson int 44299a2dd95SBruce Richardson rte_event_timer_adapter_stats_get(struct rte_event_timer_adapter *adapter, 44399a2dd95SBruce Richardson struct rte_event_timer_adapter_stats *stats) 44499a2dd95SBruce Richardson { 44599a2dd95SBruce Richardson ADAPTER_VALID_OR_ERR_RET(adapter, -EINVAL); 44699a2dd95SBruce Richardson FUNC_PTR_OR_ERR_RET(adapter->ops->stats_get, -EINVAL); 44799a2dd95SBruce Richardson if (stats == NULL) 44899a2dd95SBruce Richardson return -EINVAL; 44999a2dd95SBruce Richardson 45099a2dd95SBruce Richardson return adapter->ops->stats_get(adapter, stats); 45199a2dd95SBruce Richardson } 45299a2dd95SBruce Richardson 45399a2dd95SBruce Richardson int 45499a2dd95SBruce Richardson rte_event_timer_adapter_stats_reset(struct rte_event_timer_adapter *adapter) 45599a2dd95SBruce Richardson { 45699a2dd95SBruce Richardson ADAPTER_VALID_OR_ERR_RET(adapter, -EINVAL); 45799a2dd95SBruce Richardson FUNC_PTR_OR_ERR_RET(adapter->ops->stats_reset, -EINVAL); 45899a2dd95SBruce Richardson return adapter->ops->stats_reset(adapter); 45999a2dd95SBruce Richardson } 46099a2dd95SBruce Richardson 46199a2dd95SBruce Richardson /* 46299a2dd95SBruce Richardson * Software event timer adapter buffer helper functions 46399a2dd95SBruce Richardson */ 46499a2dd95SBruce Richardson 46599a2dd95SBruce Richardson #define NSECPERSEC 1E9 46699a2dd95SBruce Richardson 46799a2dd95SBruce Richardson /* Optimizations used to index into the buffer require that the buffer size 46899a2dd95SBruce Richardson * be a power of 2. 46999a2dd95SBruce Richardson */ 47099a2dd95SBruce Richardson #define EVENT_BUFFER_SZ 4096 47199a2dd95SBruce Richardson #define EVENT_BUFFER_BATCHSZ 32 47299a2dd95SBruce Richardson #define EVENT_BUFFER_MASK (EVENT_BUFFER_SZ - 1) 47399a2dd95SBruce Richardson 47499a2dd95SBruce Richardson #define EXP_TIM_BUF_SZ 128 47599a2dd95SBruce Richardson 47699a2dd95SBruce Richardson struct event_buffer { 47799a2dd95SBruce Richardson size_t head; 47899a2dd95SBruce Richardson size_t tail; 47999a2dd95SBruce Richardson struct rte_event events[EVENT_BUFFER_SZ]; 48099a2dd95SBruce Richardson } __rte_cache_aligned; 48199a2dd95SBruce Richardson 48299a2dd95SBruce Richardson static inline bool 48399a2dd95SBruce Richardson event_buffer_full(struct event_buffer *bufp) 48499a2dd95SBruce Richardson { 48599a2dd95SBruce Richardson return (bufp->head - bufp->tail) == EVENT_BUFFER_SZ; 48699a2dd95SBruce Richardson } 48799a2dd95SBruce Richardson 48899a2dd95SBruce Richardson static inline bool 48999a2dd95SBruce Richardson event_buffer_batch_ready(struct event_buffer *bufp) 49099a2dd95SBruce Richardson { 49199a2dd95SBruce Richardson return (bufp->head - bufp->tail) >= EVENT_BUFFER_BATCHSZ; 49299a2dd95SBruce Richardson } 49399a2dd95SBruce Richardson 49499a2dd95SBruce Richardson static void 49599a2dd95SBruce Richardson event_buffer_init(struct event_buffer *bufp) 49699a2dd95SBruce Richardson { 49799a2dd95SBruce Richardson bufp->head = bufp->tail = 0; 49899a2dd95SBruce Richardson memset(&bufp->events, 0, sizeof(struct rte_event) * EVENT_BUFFER_SZ); 49999a2dd95SBruce Richardson } 50099a2dd95SBruce Richardson 50199a2dd95SBruce Richardson static int 50299a2dd95SBruce Richardson event_buffer_add(struct event_buffer *bufp, struct rte_event *eventp) 50399a2dd95SBruce Richardson { 50499a2dd95SBruce Richardson size_t head_idx; 50599a2dd95SBruce Richardson struct rte_event *buf_eventp; 50699a2dd95SBruce Richardson 50799a2dd95SBruce Richardson if (event_buffer_full(bufp)) 50899a2dd95SBruce Richardson return -1; 50999a2dd95SBruce Richardson 51099a2dd95SBruce Richardson /* Instead of modulus, bitwise AND with mask to get head_idx. */ 51199a2dd95SBruce Richardson head_idx = bufp->head & EVENT_BUFFER_MASK; 51299a2dd95SBruce Richardson buf_eventp = &bufp->events[head_idx]; 51399a2dd95SBruce Richardson rte_memcpy(buf_eventp, eventp, sizeof(struct rte_event)); 51499a2dd95SBruce Richardson 51599a2dd95SBruce Richardson /* Wrap automatically when overflow occurs. */ 51699a2dd95SBruce Richardson bufp->head++; 51799a2dd95SBruce Richardson 51899a2dd95SBruce Richardson return 0; 51999a2dd95SBruce Richardson } 52099a2dd95SBruce Richardson 52199a2dd95SBruce Richardson static void 52299a2dd95SBruce Richardson event_buffer_flush(struct event_buffer *bufp, uint8_t dev_id, uint8_t port_id, 52399a2dd95SBruce Richardson uint16_t *nb_events_flushed, 52499a2dd95SBruce Richardson uint16_t *nb_events_inv) 52599a2dd95SBruce Richardson { 52699a2dd95SBruce Richardson struct rte_event *events = bufp->events; 52799a2dd95SBruce Richardson size_t head_idx, tail_idx; 52899a2dd95SBruce Richardson uint16_t n = 0; 52999a2dd95SBruce Richardson 53099a2dd95SBruce Richardson /* Instead of modulus, bitwise AND with mask to get index. */ 53199a2dd95SBruce Richardson head_idx = bufp->head & EVENT_BUFFER_MASK; 53299a2dd95SBruce Richardson tail_idx = bufp->tail & EVENT_BUFFER_MASK; 53399a2dd95SBruce Richardson 53499a2dd95SBruce Richardson RTE_ASSERT(head_idx < EVENT_BUFFER_SZ && tail_idx < EVENT_BUFFER_SZ); 53599a2dd95SBruce Richardson 5364a6672c2SStephen Hemminger /* Determine the largest contiguous run we can attempt to enqueue to the 53799a2dd95SBruce Richardson * event device. 53899a2dd95SBruce Richardson */ 53999a2dd95SBruce Richardson if (head_idx > tail_idx) 54099a2dd95SBruce Richardson n = head_idx - tail_idx; 54199a2dd95SBruce Richardson else if (head_idx < tail_idx) 54299a2dd95SBruce Richardson n = EVENT_BUFFER_SZ - tail_idx; 54399a2dd95SBruce Richardson else if (event_buffer_full(bufp)) 54499a2dd95SBruce Richardson n = EVENT_BUFFER_SZ - tail_idx; 54599a2dd95SBruce Richardson else { 54699a2dd95SBruce Richardson *nb_events_flushed = 0; 54799a2dd95SBruce Richardson return; 54899a2dd95SBruce Richardson } 54999a2dd95SBruce Richardson 55099a2dd95SBruce Richardson n = RTE_MIN(EVENT_BUFFER_BATCHSZ, n); 55199a2dd95SBruce Richardson *nb_events_inv = 0; 55299a2dd95SBruce Richardson 55399a2dd95SBruce Richardson *nb_events_flushed = rte_event_enqueue_burst(dev_id, port_id, 55499a2dd95SBruce Richardson &events[tail_idx], n); 55599a2dd95SBruce Richardson if (*nb_events_flushed != n) { 55699a2dd95SBruce Richardson if (rte_errno == EINVAL) { 55799a2dd95SBruce Richardson EVTIM_LOG_ERR("failed to enqueue invalid event - " 55899a2dd95SBruce Richardson "dropping it"); 55999a2dd95SBruce Richardson (*nb_events_inv)++; 56099a2dd95SBruce Richardson } else if (rte_errno == ENOSPC) 56199a2dd95SBruce Richardson rte_pause(); 56299a2dd95SBruce Richardson } 56399a2dd95SBruce Richardson 56499a2dd95SBruce Richardson if (*nb_events_flushed > 0) 56599a2dd95SBruce Richardson EVTIM_BUF_LOG_DBG("enqueued %"PRIu16" timer events to event " 56699a2dd95SBruce Richardson "device", *nb_events_flushed); 56799a2dd95SBruce Richardson 56899a2dd95SBruce Richardson bufp->tail = bufp->tail + *nb_events_flushed + *nb_events_inv; 56999a2dd95SBruce Richardson } 57099a2dd95SBruce Richardson 57199a2dd95SBruce Richardson /* 57299a2dd95SBruce Richardson * Software event timer adapter implementation 57399a2dd95SBruce Richardson */ 57499a2dd95SBruce Richardson struct swtim { 57599a2dd95SBruce Richardson /* Identifier of service executing timer management logic. */ 57699a2dd95SBruce Richardson uint32_t service_id; 57799a2dd95SBruce Richardson /* The cycle count at which the adapter should next tick */ 57899a2dd95SBruce Richardson uint64_t next_tick_cycles; 57999a2dd95SBruce Richardson /* The tick resolution used by adapter instance. May have been 58099a2dd95SBruce Richardson * adjusted from what user requested 58199a2dd95SBruce Richardson */ 58299a2dd95SBruce Richardson uint64_t timer_tick_ns; 58399a2dd95SBruce Richardson /* Maximum timeout in nanoseconds allowed by adapter instance. */ 58499a2dd95SBruce Richardson uint64_t max_tmo_ns; 58599a2dd95SBruce Richardson /* Buffered timer expiry events to be enqueued to an event device. */ 58699a2dd95SBruce Richardson struct event_buffer buffer; 58799a2dd95SBruce Richardson /* Statistics */ 58899a2dd95SBruce Richardson struct rte_event_timer_adapter_stats stats; 58999a2dd95SBruce Richardson /* Mempool of timer objects */ 59099a2dd95SBruce Richardson struct rte_mempool *tim_pool; 59199a2dd95SBruce Richardson /* Back pointer for convenience */ 59299a2dd95SBruce Richardson struct rte_event_timer_adapter *adapter; 59399a2dd95SBruce Richardson /* Identifier of timer data instance */ 59499a2dd95SBruce Richardson uint32_t timer_data_id; 59599a2dd95SBruce Richardson /* Track which cores have actually armed a timer */ 59699a2dd95SBruce Richardson struct { 59799a2dd95SBruce Richardson uint16_t v; 59899a2dd95SBruce Richardson } __rte_cache_aligned in_use[RTE_MAX_LCORE]; 59999a2dd95SBruce Richardson /* Track which cores' timer lists should be polled */ 60099a2dd95SBruce Richardson unsigned int poll_lcores[RTE_MAX_LCORE]; 60199a2dd95SBruce Richardson /* The number of lists that should be polled */ 60299a2dd95SBruce Richardson int n_poll_lcores; 60399a2dd95SBruce Richardson /* Timers which have expired and can be returned to a mempool */ 60499a2dd95SBruce Richardson struct rte_timer *expired_timers[EXP_TIM_BUF_SZ]; 60599a2dd95SBruce Richardson /* The number of timers that can be returned to a mempool */ 60699a2dd95SBruce Richardson size_t n_expired_timers; 60799a2dd95SBruce Richardson }; 60899a2dd95SBruce Richardson 60999a2dd95SBruce Richardson static inline struct swtim * 61099a2dd95SBruce Richardson swtim_pmd_priv(const struct rte_event_timer_adapter *adapter) 61199a2dd95SBruce Richardson { 61299a2dd95SBruce Richardson return adapter->data->adapter_priv; 61399a2dd95SBruce Richardson } 61499a2dd95SBruce Richardson 61599a2dd95SBruce Richardson static void 61699a2dd95SBruce Richardson swtim_callback(struct rte_timer *tim) 61799a2dd95SBruce Richardson { 61899a2dd95SBruce Richardson struct rte_event_timer *evtim = tim->arg; 61999a2dd95SBruce Richardson struct rte_event_timer_adapter *adapter; 62099a2dd95SBruce Richardson unsigned int lcore = rte_lcore_id(); 62199a2dd95SBruce Richardson struct swtim *sw; 62299a2dd95SBruce Richardson uint16_t nb_evs_flushed = 0; 62399a2dd95SBruce Richardson uint16_t nb_evs_invalid = 0; 62499a2dd95SBruce Richardson uint64_t opaque; 62599a2dd95SBruce Richardson int ret; 62699a2dd95SBruce Richardson int n_lcores; 627*3d9d8adfSNaga Harish K S V enum rte_timer_type type; 62899a2dd95SBruce Richardson 62999a2dd95SBruce Richardson opaque = evtim->impl_opaque[1]; 63099a2dd95SBruce Richardson adapter = (struct rte_event_timer_adapter *)(uintptr_t)opaque; 63199a2dd95SBruce Richardson sw = swtim_pmd_priv(adapter); 632*3d9d8adfSNaga Harish K S V type = get_timer_type(adapter); 63399a2dd95SBruce Richardson 63499a2dd95SBruce Richardson if (unlikely(sw->in_use[lcore].v == 0)) { 63599a2dd95SBruce Richardson sw->in_use[lcore].v = 1; 63699a2dd95SBruce Richardson n_lcores = __atomic_fetch_add(&sw->n_poll_lcores, 1, 63799a2dd95SBruce Richardson __ATOMIC_RELAXED); 63899a2dd95SBruce Richardson __atomic_store_n(&sw->poll_lcores[n_lcores], lcore, 63999a2dd95SBruce Richardson __ATOMIC_RELAXED); 64099a2dd95SBruce Richardson } 641*3d9d8adfSNaga Harish K S V 642*3d9d8adfSNaga Harish K S V ret = event_buffer_add(&sw->buffer, &evtim->ev); 643*3d9d8adfSNaga Harish K S V if (ret < 0) { 644*3d9d8adfSNaga Harish K S V if (type == SINGLE) { 645*3d9d8adfSNaga Harish K S V /* If event buffer is full, put timer back in list with 646*3d9d8adfSNaga Harish K S V * immediate expiry value, so that we process it again 647*3d9d8adfSNaga Harish K S V * on the next iteration. 648*3d9d8adfSNaga Harish K S V */ 649*3d9d8adfSNaga Harish K S V ret = rte_timer_alt_reset(sw->timer_data_id, tim, 0, 650*3d9d8adfSNaga Harish K S V SINGLE, lcore, NULL, evtim); 651*3d9d8adfSNaga Harish K S V if (ret < 0) { 652*3d9d8adfSNaga Harish K S V EVTIM_LOG_DBG("event buffer full, failed to " 653*3d9d8adfSNaga Harish K S V "reset timer with immediate " 654*3d9d8adfSNaga Harish K S V "expiry value"); 655*3d9d8adfSNaga Harish K S V } else { 656*3d9d8adfSNaga Harish K S V sw->stats.evtim_retry_count++; 657*3d9d8adfSNaga Harish K S V EVTIM_LOG_DBG("event buffer full, resetting " 658*3d9d8adfSNaga Harish K S V "rte_timer with immediate " 659*3d9d8adfSNaga Harish K S V "expiry value"); 660*3d9d8adfSNaga Harish K S V } 661*3d9d8adfSNaga Harish K S V } else { 662*3d9d8adfSNaga Harish K S V sw->stats.evtim_drop_count++; 663*3d9d8adfSNaga Harish K S V } 664*3d9d8adfSNaga Harish K S V 66599a2dd95SBruce Richardson } else { 66699a2dd95SBruce Richardson EVTIM_BUF_LOG_DBG("buffered an event timer expiry event"); 66799a2dd95SBruce Richardson 66899a2dd95SBruce Richardson /* Empty the buffer here, if necessary, to free older expired 66999a2dd95SBruce Richardson * timers only 67099a2dd95SBruce Richardson */ 67199a2dd95SBruce Richardson if (unlikely(sw->n_expired_timers == EXP_TIM_BUF_SZ)) { 67299a2dd95SBruce Richardson rte_mempool_put_bulk(sw->tim_pool, 67399a2dd95SBruce Richardson (void **)sw->expired_timers, 67499a2dd95SBruce Richardson sw->n_expired_timers); 67599a2dd95SBruce Richardson sw->n_expired_timers = 0; 67699a2dd95SBruce Richardson } 67799a2dd95SBruce Richardson 678*3d9d8adfSNaga Harish K S V /* Don't free rte_timer for a periodic event timer until 679*3d9d8adfSNaga Harish K S V * it is cancelled 680*3d9d8adfSNaga Harish K S V */ 681*3d9d8adfSNaga Harish K S V if (type == SINGLE) 68299a2dd95SBruce Richardson sw->expired_timers[sw->n_expired_timers++] = tim; 68399a2dd95SBruce Richardson sw->stats.evtim_exp_count++; 68499a2dd95SBruce Richardson 685*3d9d8adfSNaga Harish K S V if (type == SINGLE) 68699a2dd95SBruce Richardson __atomic_store_n(&evtim->state, RTE_EVENT_TIMER_NOT_ARMED, 68799a2dd95SBruce Richardson __ATOMIC_RELEASE); 68899a2dd95SBruce Richardson } 68999a2dd95SBruce Richardson 69099a2dd95SBruce Richardson if (event_buffer_batch_ready(&sw->buffer)) { 69199a2dd95SBruce Richardson event_buffer_flush(&sw->buffer, 69299a2dd95SBruce Richardson adapter->data->event_dev_id, 69399a2dd95SBruce Richardson adapter->data->event_port_id, 69499a2dd95SBruce Richardson &nb_evs_flushed, 69599a2dd95SBruce Richardson &nb_evs_invalid); 69699a2dd95SBruce Richardson 69799a2dd95SBruce Richardson sw->stats.ev_enq_count += nb_evs_flushed; 69899a2dd95SBruce Richardson sw->stats.ev_inv_count += nb_evs_invalid; 69999a2dd95SBruce Richardson } 70099a2dd95SBruce Richardson } 70199a2dd95SBruce Richardson 70299a2dd95SBruce Richardson static __rte_always_inline uint64_t 70399a2dd95SBruce Richardson get_timeout_cycles(struct rte_event_timer *evtim, 70499a2dd95SBruce Richardson const struct rte_event_timer_adapter *adapter) 70599a2dd95SBruce Richardson { 70699a2dd95SBruce Richardson struct swtim *sw = swtim_pmd_priv(adapter); 70799a2dd95SBruce Richardson uint64_t timeout_ns = evtim->timeout_ticks * sw->timer_tick_ns; 70899a2dd95SBruce Richardson return timeout_ns * rte_get_timer_hz() / NSECPERSEC; 70999a2dd95SBruce Richardson } 71099a2dd95SBruce Richardson 71199a2dd95SBruce Richardson /* This function returns true if one or more (adapter) ticks have occurred since 71299a2dd95SBruce Richardson * the last time it was called. 71399a2dd95SBruce Richardson */ 71499a2dd95SBruce Richardson static inline bool 71599a2dd95SBruce Richardson swtim_did_tick(struct swtim *sw) 71699a2dd95SBruce Richardson { 71799a2dd95SBruce Richardson uint64_t cycles_per_adapter_tick, start_cycles; 71899a2dd95SBruce Richardson uint64_t *next_tick_cyclesp; 71999a2dd95SBruce Richardson 72099a2dd95SBruce Richardson next_tick_cyclesp = &sw->next_tick_cycles; 72199a2dd95SBruce Richardson cycles_per_adapter_tick = sw->timer_tick_ns * 72299a2dd95SBruce Richardson (rte_get_timer_hz() / NSECPERSEC); 72399a2dd95SBruce Richardson start_cycles = rte_get_timer_cycles(); 72499a2dd95SBruce Richardson 72599a2dd95SBruce Richardson /* Note: initially, *next_tick_cyclesp == 0, so the clause below will 72699a2dd95SBruce Richardson * execute, and set things going. 72799a2dd95SBruce Richardson */ 72899a2dd95SBruce Richardson 72999a2dd95SBruce Richardson if (start_cycles >= *next_tick_cyclesp) { 73099a2dd95SBruce Richardson /* Snap the current cycle count to the preceding adapter tick 73199a2dd95SBruce Richardson * boundary. 73299a2dd95SBruce Richardson */ 73399a2dd95SBruce Richardson start_cycles -= start_cycles % cycles_per_adapter_tick; 73499a2dd95SBruce Richardson *next_tick_cyclesp = start_cycles + cycles_per_adapter_tick; 73599a2dd95SBruce Richardson 73699a2dd95SBruce Richardson return true; 73799a2dd95SBruce Richardson } 73899a2dd95SBruce Richardson 73999a2dd95SBruce Richardson return false; 74099a2dd95SBruce Richardson } 74199a2dd95SBruce Richardson 74299a2dd95SBruce Richardson /* Check that event timer timeout value is in range */ 74399a2dd95SBruce Richardson static __rte_always_inline int 74499a2dd95SBruce Richardson check_timeout(struct rte_event_timer *evtim, 74599a2dd95SBruce Richardson const struct rte_event_timer_adapter *adapter) 74699a2dd95SBruce Richardson { 74799a2dd95SBruce Richardson uint64_t tmo_nsec; 74899a2dd95SBruce Richardson struct swtim *sw = swtim_pmd_priv(adapter); 74999a2dd95SBruce Richardson 75099a2dd95SBruce Richardson tmo_nsec = evtim->timeout_ticks * sw->timer_tick_ns; 75199a2dd95SBruce Richardson if (tmo_nsec > sw->max_tmo_ns) 75299a2dd95SBruce Richardson return -1; 75399a2dd95SBruce Richardson if (tmo_nsec < sw->timer_tick_ns) 75499a2dd95SBruce Richardson return -2; 75599a2dd95SBruce Richardson 75699a2dd95SBruce Richardson return 0; 75799a2dd95SBruce Richardson } 75899a2dd95SBruce Richardson 75999a2dd95SBruce Richardson /* Check that event timer event queue sched type matches destination event queue 76099a2dd95SBruce Richardson * sched type 76199a2dd95SBruce Richardson */ 76299a2dd95SBruce Richardson static __rte_always_inline int 76399a2dd95SBruce Richardson check_destination_event_queue(struct rte_event_timer *evtim, 76499a2dd95SBruce Richardson const struct rte_event_timer_adapter *adapter) 76599a2dd95SBruce Richardson { 76699a2dd95SBruce Richardson int ret; 76799a2dd95SBruce Richardson uint32_t sched_type; 76899a2dd95SBruce Richardson 76999a2dd95SBruce Richardson ret = rte_event_queue_attr_get(adapter->data->event_dev_id, 77099a2dd95SBruce Richardson evtim->ev.queue_id, 77199a2dd95SBruce Richardson RTE_EVENT_QUEUE_ATTR_SCHEDULE_TYPE, 77299a2dd95SBruce Richardson &sched_type); 77399a2dd95SBruce Richardson 77499a2dd95SBruce Richardson if ((ret == 0 && evtim->ev.sched_type == sched_type) || 77599a2dd95SBruce Richardson ret == -EOVERFLOW) 77699a2dd95SBruce Richardson return 0; 77799a2dd95SBruce Richardson 77899a2dd95SBruce Richardson return -1; 77999a2dd95SBruce Richardson } 78099a2dd95SBruce Richardson 78199a2dd95SBruce Richardson static int 78299a2dd95SBruce Richardson swtim_service_func(void *arg) 78399a2dd95SBruce Richardson { 78499a2dd95SBruce Richardson struct rte_event_timer_adapter *adapter = arg; 78599a2dd95SBruce Richardson struct swtim *sw = swtim_pmd_priv(adapter); 78699a2dd95SBruce Richardson uint16_t nb_evs_flushed = 0; 78799a2dd95SBruce Richardson uint16_t nb_evs_invalid = 0; 78899a2dd95SBruce Richardson 78999a2dd95SBruce Richardson if (swtim_did_tick(sw)) { 79099a2dd95SBruce Richardson rte_timer_alt_manage(sw->timer_data_id, 79199a2dd95SBruce Richardson sw->poll_lcores, 79299a2dd95SBruce Richardson sw->n_poll_lcores, 79399a2dd95SBruce Richardson swtim_callback); 79499a2dd95SBruce Richardson 79599a2dd95SBruce Richardson /* Return expired timer objects back to mempool */ 79699a2dd95SBruce Richardson rte_mempool_put_bulk(sw->tim_pool, (void **)sw->expired_timers, 79799a2dd95SBruce Richardson sw->n_expired_timers); 79899a2dd95SBruce Richardson sw->n_expired_timers = 0; 79999a2dd95SBruce Richardson 80099a2dd95SBruce Richardson event_buffer_flush(&sw->buffer, 80199a2dd95SBruce Richardson adapter->data->event_dev_id, 80299a2dd95SBruce Richardson adapter->data->event_port_id, 80399a2dd95SBruce Richardson &nb_evs_flushed, 80499a2dd95SBruce Richardson &nb_evs_invalid); 80599a2dd95SBruce Richardson 80699a2dd95SBruce Richardson sw->stats.ev_enq_count += nb_evs_flushed; 80799a2dd95SBruce Richardson sw->stats.ev_inv_count += nb_evs_invalid; 80899a2dd95SBruce Richardson sw->stats.adapter_tick_count++; 80999a2dd95SBruce Richardson } 81099a2dd95SBruce Richardson 811578402f2SMattias Rönnblom rte_event_maintain(adapter->data->event_dev_id, 812578402f2SMattias Rönnblom adapter->data->event_port_id, 0); 813578402f2SMattias Rönnblom 81499a2dd95SBruce Richardson return 0; 81599a2dd95SBruce Richardson } 81699a2dd95SBruce Richardson 81799a2dd95SBruce Richardson /* The adapter initialization function rounds the mempool size up to the next 81899a2dd95SBruce Richardson * power of 2, so we can take the difference between that value and what the 81999a2dd95SBruce Richardson * user requested, and use the space for caches. This avoids a scenario where a 82099a2dd95SBruce Richardson * user can't arm the number of timers the adapter was configured with because 82199a2dd95SBruce Richardson * mempool objects have been lost to caches. 82299a2dd95SBruce Richardson * 82399a2dd95SBruce Richardson * nb_actual should always be a power of 2, so we can iterate over the powers 82499a2dd95SBruce Richardson * of 2 to see what the largest cache size we can use is. 82599a2dd95SBruce Richardson */ 82699a2dd95SBruce Richardson static int 82799a2dd95SBruce Richardson compute_msg_mempool_cache_size(uint64_t nb_requested, uint64_t nb_actual) 82899a2dd95SBruce Richardson { 82999a2dd95SBruce Richardson int i; 83099a2dd95SBruce Richardson int size; 83199a2dd95SBruce Richardson int cache_size = 0; 83299a2dd95SBruce Richardson 83399a2dd95SBruce Richardson for (i = 0;; i++) { 83499a2dd95SBruce Richardson size = 1 << i; 83599a2dd95SBruce Richardson 83699a2dd95SBruce Richardson if (RTE_MAX_LCORE * size < (int)(nb_actual - nb_requested) && 83799a2dd95SBruce Richardson size < RTE_MEMPOOL_CACHE_MAX_SIZE && 83899a2dd95SBruce Richardson size <= nb_actual / 1.5) 83999a2dd95SBruce Richardson cache_size = size; 84099a2dd95SBruce Richardson else 84199a2dd95SBruce Richardson break; 84299a2dd95SBruce Richardson } 84399a2dd95SBruce Richardson 84499a2dd95SBruce Richardson return cache_size; 84599a2dd95SBruce Richardson } 84699a2dd95SBruce Richardson 84799a2dd95SBruce Richardson static int 84899a2dd95SBruce Richardson swtim_init(struct rte_event_timer_adapter *adapter) 84999a2dd95SBruce Richardson { 85099a2dd95SBruce Richardson int i, ret; 85199a2dd95SBruce Richardson struct swtim *sw; 85299a2dd95SBruce Richardson unsigned int flags; 85399a2dd95SBruce Richardson struct rte_service_spec service; 85499a2dd95SBruce Richardson 85599a2dd95SBruce Richardson /* Allocate storage for private data area */ 85699a2dd95SBruce Richardson #define SWTIM_NAMESIZE 32 85799a2dd95SBruce Richardson char swtim_name[SWTIM_NAMESIZE]; 85899a2dd95SBruce Richardson snprintf(swtim_name, SWTIM_NAMESIZE, "swtim_%"PRIu8, 85999a2dd95SBruce Richardson adapter->data->id); 86099a2dd95SBruce Richardson sw = rte_zmalloc_socket(swtim_name, sizeof(*sw), RTE_CACHE_LINE_SIZE, 86199a2dd95SBruce Richardson adapter->data->socket_id); 86299a2dd95SBruce Richardson if (sw == NULL) { 86399a2dd95SBruce Richardson EVTIM_LOG_ERR("failed to allocate space for private data"); 86499a2dd95SBruce Richardson rte_errno = ENOMEM; 86599a2dd95SBruce Richardson return -1; 86699a2dd95SBruce Richardson } 86799a2dd95SBruce Richardson 86899a2dd95SBruce Richardson /* Connect storage to adapter instance */ 86999a2dd95SBruce Richardson adapter->data->adapter_priv = sw; 87099a2dd95SBruce Richardson sw->adapter = adapter; 87199a2dd95SBruce Richardson 87299a2dd95SBruce Richardson sw->timer_tick_ns = adapter->data->conf.timer_tick_ns; 87399a2dd95SBruce Richardson sw->max_tmo_ns = adapter->data->conf.max_tmo_ns; 87499a2dd95SBruce Richardson 87599a2dd95SBruce Richardson /* Create a timer pool */ 87699a2dd95SBruce Richardson char pool_name[SWTIM_NAMESIZE]; 87799a2dd95SBruce Richardson snprintf(pool_name, SWTIM_NAMESIZE, "swtim_pool_%"PRIu8, 87899a2dd95SBruce Richardson adapter->data->id); 87999a2dd95SBruce Richardson /* Optimal mempool size is a power of 2 minus one */ 88099a2dd95SBruce Richardson uint64_t nb_timers = rte_align64pow2(adapter->data->conf.nb_timers); 88199a2dd95SBruce Richardson int pool_size = nb_timers - 1; 88299a2dd95SBruce Richardson int cache_size = compute_msg_mempool_cache_size( 88399a2dd95SBruce Richardson adapter->data->conf.nb_timers, nb_timers); 88499a2dd95SBruce Richardson flags = 0; /* pool is multi-producer, multi-consumer */ 88599a2dd95SBruce Richardson sw->tim_pool = rte_mempool_create(pool_name, pool_size, 88699a2dd95SBruce Richardson sizeof(struct rte_timer), cache_size, 0, NULL, NULL, 88799a2dd95SBruce Richardson NULL, NULL, adapter->data->socket_id, flags); 88899a2dd95SBruce Richardson if (sw->tim_pool == NULL) { 88999a2dd95SBruce Richardson EVTIM_LOG_ERR("failed to create timer object mempool"); 89099a2dd95SBruce Richardson rte_errno = ENOMEM; 89199a2dd95SBruce Richardson goto free_alloc; 89299a2dd95SBruce Richardson } 89399a2dd95SBruce Richardson 89499a2dd95SBruce Richardson /* Initialize the variables that track in-use timer lists */ 89599a2dd95SBruce Richardson for (i = 0; i < RTE_MAX_LCORE; i++) 89699a2dd95SBruce Richardson sw->in_use[i].v = 0; 89799a2dd95SBruce Richardson 89899a2dd95SBruce Richardson /* Initialize the timer subsystem and allocate timer data instance */ 89999a2dd95SBruce Richardson ret = rte_timer_subsystem_init(); 90099a2dd95SBruce Richardson if (ret < 0) { 90199a2dd95SBruce Richardson if (ret != -EALREADY) { 90299a2dd95SBruce Richardson EVTIM_LOG_ERR("failed to initialize timer subsystem"); 90399a2dd95SBruce Richardson rte_errno = -ret; 90499a2dd95SBruce Richardson goto free_mempool; 90599a2dd95SBruce Richardson } 90699a2dd95SBruce Richardson } 90799a2dd95SBruce Richardson 90899a2dd95SBruce Richardson ret = rte_timer_data_alloc(&sw->timer_data_id); 90999a2dd95SBruce Richardson if (ret < 0) { 91099a2dd95SBruce Richardson EVTIM_LOG_ERR("failed to allocate timer data instance"); 91199a2dd95SBruce Richardson rte_errno = -ret; 91299a2dd95SBruce Richardson goto free_mempool; 91399a2dd95SBruce Richardson } 91499a2dd95SBruce Richardson 91599a2dd95SBruce Richardson /* Initialize timer event buffer */ 91699a2dd95SBruce Richardson event_buffer_init(&sw->buffer); 91799a2dd95SBruce Richardson 91899a2dd95SBruce Richardson sw->adapter = adapter; 91999a2dd95SBruce Richardson 92099a2dd95SBruce Richardson /* Register a service component to run adapter logic */ 92199a2dd95SBruce Richardson memset(&service, 0, sizeof(service)); 92299a2dd95SBruce Richardson snprintf(service.name, RTE_SERVICE_NAME_MAX, 92399a2dd95SBruce Richardson "swtim_svc_%"PRIu8, adapter->data->id); 92499a2dd95SBruce Richardson service.socket_id = adapter->data->socket_id; 92599a2dd95SBruce Richardson service.callback = swtim_service_func; 92699a2dd95SBruce Richardson service.callback_userdata = adapter; 92799a2dd95SBruce Richardson service.capabilities &= ~(RTE_SERVICE_CAP_MT_SAFE); 92899a2dd95SBruce Richardson ret = rte_service_component_register(&service, &sw->service_id); 92999a2dd95SBruce Richardson if (ret < 0) { 93099a2dd95SBruce Richardson EVTIM_LOG_ERR("failed to register service %s with id %"PRIu32 93199a2dd95SBruce Richardson ": err = %d", service.name, sw->service_id, 93299a2dd95SBruce Richardson ret); 93399a2dd95SBruce Richardson 93499a2dd95SBruce Richardson rte_errno = ENOSPC; 93599a2dd95SBruce Richardson goto free_mempool; 93699a2dd95SBruce Richardson } 93799a2dd95SBruce Richardson 93899a2dd95SBruce Richardson EVTIM_LOG_DBG("registered service %s with id %"PRIu32, service.name, 93999a2dd95SBruce Richardson sw->service_id); 94099a2dd95SBruce Richardson 94199a2dd95SBruce Richardson adapter->data->service_id = sw->service_id; 94299a2dd95SBruce Richardson adapter->data->service_inited = 1; 94399a2dd95SBruce Richardson 94499a2dd95SBruce Richardson return 0; 94599a2dd95SBruce Richardson free_mempool: 94699a2dd95SBruce Richardson rte_mempool_free(sw->tim_pool); 94799a2dd95SBruce Richardson free_alloc: 94899a2dd95SBruce Richardson rte_free(sw); 94999a2dd95SBruce Richardson return -1; 95099a2dd95SBruce Richardson } 95199a2dd95SBruce Richardson 95299a2dd95SBruce Richardson static void 95399a2dd95SBruce Richardson swtim_free_tim(struct rte_timer *tim, void *arg) 95499a2dd95SBruce Richardson { 95599a2dd95SBruce Richardson struct swtim *sw = arg; 95699a2dd95SBruce Richardson 95799a2dd95SBruce Richardson rte_mempool_put(sw->tim_pool, tim); 95899a2dd95SBruce Richardson } 95999a2dd95SBruce Richardson 96099a2dd95SBruce Richardson /* Traverse the list of outstanding timers and put them back in the mempool 96199a2dd95SBruce Richardson * before freeing the adapter to avoid leaking the memory. 96299a2dd95SBruce Richardson */ 96399a2dd95SBruce Richardson static int 96499a2dd95SBruce Richardson swtim_uninit(struct rte_event_timer_adapter *adapter) 96599a2dd95SBruce Richardson { 96699a2dd95SBruce Richardson int ret; 96799a2dd95SBruce Richardson struct swtim *sw = swtim_pmd_priv(adapter); 96899a2dd95SBruce Richardson 96999a2dd95SBruce Richardson /* Free outstanding timers */ 97099a2dd95SBruce Richardson rte_timer_stop_all(sw->timer_data_id, 97199a2dd95SBruce Richardson sw->poll_lcores, 97299a2dd95SBruce Richardson sw->n_poll_lcores, 97399a2dd95SBruce Richardson swtim_free_tim, 97499a2dd95SBruce Richardson sw); 97599a2dd95SBruce Richardson 976*3d9d8adfSNaga Harish K S V ret = rte_timer_data_dealloc(sw->timer_data_id); 977*3d9d8adfSNaga Harish K S V if (ret < 0) { 978*3d9d8adfSNaga Harish K S V EVTIM_LOG_ERR("failed to deallocate timer data instance"); 979*3d9d8adfSNaga Harish K S V return ret; 980*3d9d8adfSNaga Harish K S V } 981*3d9d8adfSNaga Harish K S V 98299a2dd95SBruce Richardson ret = rte_service_component_unregister(sw->service_id); 98399a2dd95SBruce Richardson if (ret < 0) { 98499a2dd95SBruce Richardson EVTIM_LOG_ERR("failed to unregister service component"); 98599a2dd95SBruce Richardson return ret; 98699a2dd95SBruce Richardson } 98799a2dd95SBruce Richardson 98899a2dd95SBruce Richardson rte_mempool_free(sw->tim_pool); 98999a2dd95SBruce Richardson rte_free(sw); 99099a2dd95SBruce Richardson adapter->data->adapter_priv = NULL; 99199a2dd95SBruce Richardson 99299a2dd95SBruce Richardson return 0; 99399a2dd95SBruce Richardson } 99499a2dd95SBruce Richardson 99599a2dd95SBruce Richardson static inline int32_t 99699a2dd95SBruce Richardson get_mapped_count_for_service(uint32_t service_id) 99799a2dd95SBruce Richardson { 99899a2dd95SBruce Richardson int32_t core_count, i, mapped_count = 0; 99999a2dd95SBruce Richardson uint32_t lcore_arr[RTE_MAX_LCORE]; 100099a2dd95SBruce Richardson 100199a2dd95SBruce Richardson core_count = rte_service_lcore_list(lcore_arr, RTE_MAX_LCORE); 100299a2dd95SBruce Richardson 100399a2dd95SBruce Richardson for (i = 0; i < core_count; i++) 100499a2dd95SBruce Richardson if (rte_service_map_lcore_get(service_id, lcore_arr[i]) == 1) 100599a2dd95SBruce Richardson mapped_count++; 100699a2dd95SBruce Richardson 100799a2dd95SBruce Richardson return mapped_count; 100899a2dd95SBruce Richardson } 100999a2dd95SBruce Richardson 101099a2dd95SBruce Richardson static int 101199a2dd95SBruce Richardson swtim_start(const struct rte_event_timer_adapter *adapter) 101299a2dd95SBruce Richardson { 101399a2dd95SBruce Richardson int mapped_count; 101499a2dd95SBruce Richardson struct swtim *sw = swtim_pmd_priv(adapter); 101599a2dd95SBruce Richardson 101699a2dd95SBruce Richardson /* Mapping the service to more than one service core can introduce 101799a2dd95SBruce Richardson * delays while one thread is waiting to acquire a lock, so only allow 101899a2dd95SBruce Richardson * one core to be mapped to the service. 101999a2dd95SBruce Richardson * 102099a2dd95SBruce Richardson * Note: the service could be modified such that it spreads cores to 102199a2dd95SBruce Richardson * poll over multiple service instances. 102299a2dd95SBruce Richardson */ 102399a2dd95SBruce Richardson mapped_count = get_mapped_count_for_service(sw->service_id); 102499a2dd95SBruce Richardson 102599a2dd95SBruce Richardson if (mapped_count != 1) 102699a2dd95SBruce Richardson return mapped_count < 1 ? -ENOENT : -ENOTSUP; 102799a2dd95SBruce Richardson 102899a2dd95SBruce Richardson return rte_service_component_runstate_set(sw->service_id, 1); 102999a2dd95SBruce Richardson } 103099a2dd95SBruce Richardson 103199a2dd95SBruce Richardson static int 103299a2dd95SBruce Richardson swtim_stop(const struct rte_event_timer_adapter *adapter) 103399a2dd95SBruce Richardson { 103499a2dd95SBruce Richardson int ret; 103599a2dd95SBruce Richardson struct swtim *sw = swtim_pmd_priv(adapter); 103699a2dd95SBruce Richardson 103799a2dd95SBruce Richardson ret = rte_service_component_runstate_set(sw->service_id, 0); 103899a2dd95SBruce Richardson if (ret < 0) 103999a2dd95SBruce Richardson return ret; 104099a2dd95SBruce Richardson 104199a2dd95SBruce Richardson /* Wait for the service to complete its final iteration */ 104299a2dd95SBruce Richardson while (rte_service_may_be_active(sw->service_id)) 104399a2dd95SBruce Richardson rte_pause(); 104499a2dd95SBruce Richardson 104599a2dd95SBruce Richardson return 0; 104699a2dd95SBruce Richardson } 104799a2dd95SBruce Richardson 104899a2dd95SBruce Richardson static void 104999a2dd95SBruce Richardson swtim_get_info(const struct rte_event_timer_adapter *adapter, 105099a2dd95SBruce Richardson struct rte_event_timer_adapter_info *adapter_info) 105199a2dd95SBruce Richardson { 105299a2dd95SBruce Richardson struct swtim *sw = swtim_pmd_priv(adapter); 105399a2dd95SBruce Richardson adapter_info->min_resolution_ns = sw->timer_tick_ns; 105499a2dd95SBruce Richardson adapter_info->max_tmo_ns = sw->max_tmo_ns; 105599a2dd95SBruce Richardson } 105699a2dd95SBruce Richardson 105799a2dd95SBruce Richardson static int 105899a2dd95SBruce Richardson swtim_stats_get(const struct rte_event_timer_adapter *adapter, 105999a2dd95SBruce Richardson struct rte_event_timer_adapter_stats *stats) 106099a2dd95SBruce Richardson { 106199a2dd95SBruce Richardson struct swtim *sw = swtim_pmd_priv(adapter); 106299a2dd95SBruce Richardson *stats = sw->stats; /* structure copy */ 106399a2dd95SBruce Richardson return 0; 106499a2dd95SBruce Richardson } 106599a2dd95SBruce Richardson 106699a2dd95SBruce Richardson static int 106799a2dd95SBruce Richardson swtim_stats_reset(const struct rte_event_timer_adapter *adapter) 106899a2dd95SBruce Richardson { 106999a2dd95SBruce Richardson struct swtim *sw = swtim_pmd_priv(adapter); 107099a2dd95SBruce Richardson memset(&sw->stats, 0, sizeof(sw->stats)); 107199a2dd95SBruce Richardson return 0; 107299a2dd95SBruce Richardson } 107399a2dd95SBruce Richardson 107499a2dd95SBruce Richardson static uint16_t 107599a2dd95SBruce Richardson __swtim_arm_burst(const struct rte_event_timer_adapter *adapter, 107699a2dd95SBruce Richardson struct rte_event_timer **evtims, 107799a2dd95SBruce Richardson uint16_t nb_evtims) 107899a2dd95SBruce Richardson { 107999a2dd95SBruce Richardson int i, ret; 108099a2dd95SBruce Richardson struct swtim *sw = swtim_pmd_priv(adapter); 108199a2dd95SBruce Richardson uint32_t lcore_id = rte_lcore_id(); 108299a2dd95SBruce Richardson struct rte_timer *tim, *tims[nb_evtims]; 108399a2dd95SBruce Richardson uint64_t cycles; 108499a2dd95SBruce Richardson int n_lcores; 108599a2dd95SBruce Richardson /* Timer list for this lcore is not in use. */ 108699a2dd95SBruce Richardson uint16_t exp_state = 0; 108799a2dd95SBruce Richardson enum rte_event_timer_state n_state; 1088*3d9d8adfSNaga Harish K S V enum rte_timer_type type = SINGLE; 108999a2dd95SBruce Richardson 109099a2dd95SBruce Richardson #ifdef RTE_LIBRTE_EVENTDEV_DEBUG 109199a2dd95SBruce Richardson /* Check that the service is running. */ 109299a2dd95SBruce Richardson if (rte_service_runstate_get(adapter->data->service_id) != 1) { 109399a2dd95SBruce Richardson rte_errno = EINVAL; 109499a2dd95SBruce Richardson return 0; 109599a2dd95SBruce Richardson } 109699a2dd95SBruce Richardson #endif 109799a2dd95SBruce Richardson 109899a2dd95SBruce Richardson /* Adjust lcore_id if non-EAL thread. Arbitrarily pick the timer list of 109999a2dd95SBruce Richardson * the highest lcore to insert such timers into 110099a2dd95SBruce Richardson */ 110199a2dd95SBruce Richardson if (lcore_id == LCORE_ID_ANY) 110299a2dd95SBruce Richardson lcore_id = RTE_MAX_LCORE - 1; 110399a2dd95SBruce Richardson 110499a2dd95SBruce Richardson /* If this is the first time we're arming an event timer on this lcore, 110599a2dd95SBruce Richardson * mark this lcore as "in use"; this will cause the service 110699a2dd95SBruce Richardson * function to process the timer list that corresponds to this lcore. 110799a2dd95SBruce Richardson * The atomic compare-and-swap operation can prevent the race condition 110899a2dd95SBruce Richardson * on in_use flag between multiple non-EAL threads. 110999a2dd95SBruce Richardson */ 111099a2dd95SBruce Richardson if (unlikely(__atomic_compare_exchange_n(&sw->in_use[lcore_id].v, 111199a2dd95SBruce Richardson &exp_state, 1, 0, 111299a2dd95SBruce Richardson __ATOMIC_RELAXED, __ATOMIC_RELAXED))) { 111399a2dd95SBruce Richardson EVTIM_LOG_DBG("Adding lcore id = %u to list of lcores to poll", 111499a2dd95SBruce Richardson lcore_id); 111599a2dd95SBruce Richardson n_lcores = __atomic_fetch_add(&sw->n_poll_lcores, 1, 111699a2dd95SBruce Richardson __ATOMIC_RELAXED); 111799a2dd95SBruce Richardson __atomic_store_n(&sw->poll_lcores[n_lcores], lcore_id, 111899a2dd95SBruce Richardson __ATOMIC_RELAXED); 111999a2dd95SBruce Richardson } 112099a2dd95SBruce Richardson 112199a2dd95SBruce Richardson ret = rte_mempool_get_bulk(sw->tim_pool, (void **)tims, 112299a2dd95SBruce Richardson nb_evtims); 112399a2dd95SBruce Richardson if (ret < 0) { 112499a2dd95SBruce Richardson rte_errno = ENOSPC; 112599a2dd95SBruce Richardson return 0; 112699a2dd95SBruce Richardson } 112799a2dd95SBruce Richardson 1128*3d9d8adfSNaga Harish K S V /* update timer type for periodic adapter */ 1129*3d9d8adfSNaga Harish K S V type = get_timer_type(adapter); 1130*3d9d8adfSNaga Harish K S V 113199a2dd95SBruce Richardson for (i = 0; i < nb_evtims; i++) { 113299a2dd95SBruce Richardson n_state = __atomic_load_n(&evtims[i]->state, __ATOMIC_ACQUIRE); 113399a2dd95SBruce Richardson if (n_state == RTE_EVENT_TIMER_ARMED) { 113499a2dd95SBruce Richardson rte_errno = EALREADY; 113599a2dd95SBruce Richardson break; 113699a2dd95SBruce Richardson } else if (!(n_state == RTE_EVENT_TIMER_NOT_ARMED || 113799a2dd95SBruce Richardson n_state == RTE_EVENT_TIMER_CANCELED)) { 113899a2dd95SBruce Richardson rte_errno = EINVAL; 113999a2dd95SBruce Richardson break; 114099a2dd95SBruce Richardson } 114199a2dd95SBruce Richardson 114299a2dd95SBruce Richardson ret = check_timeout(evtims[i], adapter); 114399a2dd95SBruce Richardson if (unlikely(ret == -1)) { 114499a2dd95SBruce Richardson __atomic_store_n(&evtims[i]->state, 114599a2dd95SBruce Richardson RTE_EVENT_TIMER_ERROR_TOOLATE, 114699a2dd95SBruce Richardson __ATOMIC_RELAXED); 114799a2dd95SBruce Richardson rte_errno = EINVAL; 114899a2dd95SBruce Richardson break; 114999a2dd95SBruce Richardson } else if (unlikely(ret == -2)) { 115099a2dd95SBruce Richardson __atomic_store_n(&evtims[i]->state, 115199a2dd95SBruce Richardson RTE_EVENT_TIMER_ERROR_TOOEARLY, 115299a2dd95SBruce Richardson __ATOMIC_RELAXED); 115399a2dd95SBruce Richardson rte_errno = EINVAL; 115499a2dd95SBruce Richardson break; 115599a2dd95SBruce Richardson } 115699a2dd95SBruce Richardson 115799a2dd95SBruce Richardson if (unlikely(check_destination_event_queue(evtims[i], 115899a2dd95SBruce Richardson adapter) < 0)) { 115999a2dd95SBruce Richardson __atomic_store_n(&evtims[i]->state, 116099a2dd95SBruce Richardson RTE_EVENT_TIMER_ERROR, 116199a2dd95SBruce Richardson __ATOMIC_RELAXED); 116299a2dd95SBruce Richardson rte_errno = EINVAL; 116399a2dd95SBruce Richardson break; 116499a2dd95SBruce Richardson } 116599a2dd95SBruce Richardson 116699a2dd95SBruce Richardson tim = tims[i]; 116799a2dd95SBruce Richardson rte_timer_init(tim); 116899a2dd95SBruce Richardson 116999a2dd95SBruce Richardson evtims[i]->impl_opaque[0] = (uintptr_t)tim; 117099a2dd95SBruce Richardson evtims[i]->impl_opaque[1] = (uintptr_t)adapter; 117199a2dd95SBruce Richardson 117299a2dd95SBruce Richardson cycles = get_timeout_cycles(evtims[i], adapter); 117399a2dd95SBruce Richardson ret = rte_timer_alt_reset(sw->timer_data_id, tim, cycles, 1174*3d9d8adfSNaga Harish K S V type, lcore_id, NULL, evtims[i]); 117599a2dd95SBruce Richardson if (ret < 0) { 117699a2dd95SBruce Richardson /* tim was in RUNNING or CONFIG state */ 117799a2dd95SBruce Richardson __atomic_store_n(&evtims[i]->state, 117899a2dd95SBruce Richardson RTE_EVENT_TIMER_ERROR, 117999a2dd95SBruce Richardson __ATOMIC_RELEASE); 118099a2dd95SBruce Richardson break; 118199a2dd95SBruce Richardson } 118299a2dd95SBruce Richardson 118399a2dd95SBruce Richardson EVTIM_LOG_DBG("armed an event timer"); 118499a2dd95SBruce Richardson /* RELEASE ordering guarantees the adapter specific value 118599a2dd95SBruce Richardson * changes observed before the update of state. 118699a2dd95SBruce Richardson */ 118799a2dd95SBruce Richardson __atomic_store_n(&evtims[i]->state, RTE_EVENT_TIMER_ARMED, 118899a2dd95SBruce Richardson __ATOMIC_RELEASE); 118999a2dd95SBruce Richardson } 119099a2dd95SBruce Richardson 119199a2dd95SBruce Richardson if (i < nb_evtims) 119299a2dd95SBruce Richardson rte_mempool_put_bulk(sw->tim_pool, 119399a2dd95SBruce Richardson (void **)&tims[i], nb_evtims - i); 119499a2dd95SBruce Richardson 119599a2dd95SBruce Richardson return i; 119699a2dd95SBruce Richardson } 119799a2dd95SBruce Richardson 119899a2dd95SBruce Richardson static uint16_t 119999a2dd95SBruce Richardson swtim_arm_burst(const struct rte_event_timer_adapter *adapter, 120099a2dd95SBruce Richardson struct rte_event_timer **evtims, 120199a2dd95SBruce Richardson uint16_t nb_evtims) 120299a2dd95SBruce Richardson { 120399a2dd95SBruce Richardson return __swtim_arm_burst(adapter, evtims, nb_evtims); 120499a2dd95SBruce Richardson } 120599a2dd95SBruce Richardson 120699a2dd95SBruce Richardson static uint16_t 120799a2dd95SBruce Richardson swtim_cancel_burst(const struct rte_event_timer_adapter *adapter, 120899a2dd95SBruce Richardson struct rte_event_timer **evtims, 120999a2dd95SBruce Richardson uint16_t nb_evtims) 121099a2dd95SBruce Richardson { 121199a2dd95SBruce Richardson int i, ret; 121299a2dd95SBruce Richardson struct rte_timer *timp; 121399a2dd95SBruce Richardson uint64_t opaque; 121499a2dd95SBruce Richardson struct swtim *sw = swtim_pmd_priv(adapter); 121599a2dd95SBruce Richardson enum rte_event_timer_state n_state; 121699a2dd95SBruce Richardson 121799a2dd95SBruce Richardson #ifdef RTE_LIBRTE_EVENTDEV_DEBUG 121899a2dd95SBruce Richardson /* Check that the service is running. */ 121999a2dd95SBruce Richardson if (rte_service_runstate_get(adapter->data->service_id) != 1) { 122099a2dd95SBruce Richardson rte_errno = EINVAL; 122199a2dd95SBruce Richardson return 0; 122299a2dd95SBruce Richardson } 122399a2dd95SBruce Richardson #endif 122499a2dd95SBruce Richardson 122599a2dd95SBruce Richardson for (i = 0; i < nb_evtims; i++) { 122699a2dd95SBruce Richardson /* Don't modify the event timer state in these cases */ 122799a2dd95SBruce Richardson /* ACQUIRE ordering guarantees the access of implementation 122899a2dd95SBruce Richardson * specific opaque data under the correct state. 122999a2dd95SBruce Richardson */ 123099a2dd95SBruce Richardson n_state = __atomic_load_n(&evtims[i]->state, __ATOMIC_ACQUIRE); 123199a2dd95SBruce Richardson if (n_state == RTE_EVENT_TIMER_CANCELED) { 123299a2dd95SBruce Richardson rte_errno = EALREADY; 123399a2dd95SBruce Richardson break; 123499a2dd95SBruce Richardson } else if (n_state != RTE_EVENT_TIMER_ARMED) { 123599a2dd95SBruce Richardson rte_errno = EINVAL; 123699a2dd95SBruce Richardson break; 123799a2dd95SBruce Richardson } 123899a2dd95SBruce Richardson 123999a2dd95SBruce Richardson opaque = evtims[i]->impl_opaque[0]; 124099a2dd95SBruce Richardson timp = (struct rte_timer *)(uintptr_t)opaque; 124199a2dd95SBruce Richardson RTE_ASSERT(timp != NULL); 124299a2dd95SBruce Richardson 124399a2dd95SBruce Richardson ret = rte_timer_alt_stop(sw->timer_data_id, timp); 124499a2dd95SBruce Richardson if (ret < 0) { 124599a2dd95SBruce Richardson /* Timer is running or being configured */ 124699a2dd95SBruce Richardson rte_errno = EAGAIN; 124799a2dd95SBruce Richardson break; 124899a2dd95SBruce Richardson } 124999a2dd95SBruce Richardson 125099a2dd95SBruce Richardson rte_mempool_put(sw->tim_pool, (void **)timp); 125199a2dd95SBruce Richardson 125299a2dd95SBruce Richardson /* The RELEASE ordering here pairs with atomic ordering 125399a2dd95SBruce Richardson * to make sure the state update data observed between 125499a2dd95SBruce Richardson * threads. 125599a2dd95SBruce Richardson */ 125699a2dd95SBruce Richardson __atomic_store_n(&evtims[i]->state, RTE_EVENT_TIMER_CANCELED, 125799a2dd95SBruce Richardson __ATOMIC_RELEASE); 125899a2dd95SBruce Richardson } 125999a2dd95SBruce Richardson 126099a2dd95SBruce Richardson return i; 126199a2dd95SBruce Richardson } 126299a2dd95SBruce Richardson 126399a2dd95SBruce Richardson static uint16_t 126499a2dd95SBruce Richardson swtim_arm_tmo_tick_burst(const struct rte_event_timer_adapter *adapter, 126599a2dd95SBruce Richardson struct rte_event_timer **evtims, 126699a2dd95SBruce Richardson uint64_t timeout_ticks, 126799a2dd95SBruce Richardson uint16_t nb_evtims) 126899a2dd95SBruce Richardson { 126999a2dd95SBruce Richardson int i; 127099a2dd95SBruce Richardson 127199a2dd95SBruce Richardson for (i = 0; i < nb_evtims; i++) 127299a2dd95SBruce Richardson evtims[i]->timeout_ticks = timeout_ticks; 127399a2dd95SBruce Richardson 127499a2dd95SBruce Richardson return __swtim_arm_burst(adapter, evtims, nb_evtims); 127599a2dd95SBruce Richardson } 127699a2dd95SBruce Richardson 127753548ad3SPavan Nikhilesh static const struct event_timer_adapter_ops swtim_ops = { 127899a2dd95SBruce Richardson .init = swtim_init, 127999a2dd95SBruce Richardson .uninit = swtim_uninit, 128099a2dd95SBruce Richardson .start = swtim_start, 128199a2dd95SBruce Richardson .stop = swtim_stop, 128299a2dd95SBruce Richardson .get_info = swtim_get_info, 128399a2dd95SBruce Richardson .stats_get = swtim_stats_get, 128499a2dd95SBruce Richardson .stats_reset = swtim_stats_reset, 128599a2dd95SBruce Richardson .arm_burst = swtim_arm_burst, 128699a2dd95SBruce Richardson .arm_tmo_tick_burst = swtim_arm_tmo_tick_burst, 128799a2dd95SBruce Richardson .cancel_burst = swtim_cancel_burst, 128899a2dd95SBruce Richardson }; 1289791dfec2SAnkur Dwivedi 1290791dfec2SAnkur Dwivedi static int 1291791dfec2SAnkur Dwivedi handle_ta_info(const char *cmd __rte_unused, const char *params, 1292791dfec2SAnkur Dwivedi struct rte_tel_data *d) 1293791dfec2SAnkur Dwivedi { 1294791dfec2SAnkur Dwivedi struct rte_event_timer_adapter_info adapter_info; 1295791dfec2SAnkur Dwivedi struct rte_event_timer_adapter *adapter; 1296791dfec2SAnkur Dwivedi uint16_t adapter_id; 1297791dfec2SAnkur Dwivedi int ret; 1298791dfec2SAnkur Dwivedi 1299791dfec2SAnkur Dwivedi if (params == NULL || strlen(params) == 0 || !isdigit(*params)) 1300791dfec2SAnkur Dwivedi return -1; 1301791dfec2SAnkur Dwivedi 1302791dfec2SAnkur Dwivedi adapter_id = atoi(params); 1303791dfec2SAnkur Dwivedi 1304791dfec2SAnkur Dwivedi if (adapter_id >= RTE_EVENT_TIMER_ADAPTER_NUM_MAX) { 1305791dfec2SAnkur Dwivedi EVTIM_LOG_ERR("Invalid timer adapter id %u", adapter_id); 1306791dfec2SAnkur Dwivedi return -EINVAL; 1307791dfec2SAnkur Dwivedi } 1308791dfec2SAnkur Dwivedi 1309791dfec2SAnkur Dwivedi adapter = &adapters[adapter_id]; 1310791dfec2SAnkur Dwivedi 1311791dfec2SAnkur Dwivedi ret = rte_event_timer_adapter_get_info(adapter, &adapter_info); 1312791dfec2SAnkur Dwivedi if (ret < 0) { 1313791dfec2SAnkur Dwivedi EVTIM_LOG_ERR("Failed to get info for timer adapter id %u", adapter_id); 1314791dfec2SAnkur Dwivedi return ret; 1315791dfec2SAnkur Dwivedi } 1316791dfec2SAnkur Dwivedi 1317791dfec2SAnkur Dwivedi rte_tel_data_start_dict(d); 1318791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "timer_adapter_id", adapter_id); 1319791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "min_resolution_ns", adapter_info.min_resolution_ns); 1320791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "max_tmo_ns", adapter_info.max_tmo_ns); 1321791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "event_dev_id", adapter_info.conf.event_dev_id); 1322791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "socket_id", adapter_info.conf.socket_id); 1323791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "clk_src", adapter_info.conf.clk_src); 1324791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "timer_tick_ns", adapter_info.conf.timer_tick_ns); 1325791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "nb_timers", adapter_info.conf.nb_timers); 1326791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "flags", adapter_info.conf.flags); 1327791dfec2SAnkur Dwivedi 1328791dfec2SAnkur Dwivedi return 0; 1329791dfec2SAnkur Dwivedi } 1330791dfec2SAnkur Dwivedi 1331791dfec2SAnkur Dwivedi static int 1332791dfec2SAnkur Dwivedi handle_ta_stats(const char *cmd __rte_unused, const char *params, 1333791dfec2SAnkur Dwivedi struct rte_tel_data *d) 1334791dfec2SAnkur Dwivedi { 1335791dfec2SAnkur Dwivedi struct rte_event_timer_adapter_stats stats; 1336791dfec2SAnkur Dwivedi struct rte_event_timer_adapter *adapter; 1337791dfec2SAnkur Dwivedi uint16_t adapter_id; 1338791dfec2SAnkur Dwivedi int ret; 1339791dfec2SAnkur Dwivedi 1340791dfec2SAnkur Dwivedi if (params == NULL || strlen(params) == 0 || !isdigit(*params)) 1341791dfec2SAnkur Dwivedi return -1; 1342791dfec2SAnkur Dwivedi 1343791dfec2SAnkur Dwivedi adapter_id = atoi(params); 1344791dfec2SAnkur Dwivedi 1345791dfec2SAnkur Dwivedi if (adapter_id >= RTE_EVENT_TIMER_ADAPTER_NUM_MAX) { 1346791dfec2SAnkur Dwivedi EVTIM_LOG_ERR("Invalid timer adapter id %u", adapter_id); 1347791dfec2SAnkur Dwivedi return -EINVAL; 1348791dfec2SAnkur Dwivedi } 1349791dfec2SAnkur Dwivedi 1350791dfec2SAnkur Dwivedi adapter = &adapters[adapter_id]; 1351791dfec2SAnkur Dwivedi 1352791dfec2SAnkur Dwivedi ret = rte_event_timer_adapter_stats_get(adapter, &stats); 1353791dfec2SAnkur Dwivedi if (ret < 0) { 1354791dfec2SAnkur Dwivedi EVTIM_LOG_ERR("Failed to get stats for timer adapter id %u", adapter_id); 1355791dfec2SAnkur Dwivedi return ret; 1356791dfec2SAnkur Dwivedi } 1357791dfec2SAnkur Dwivedi 1358791dfec2SAnkur Dwivedi rte_tel_data_start_dict(d); 1359791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "timer_adapter_id", adapter_id); 1360791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "evtim_exp_count", stats.evtim_exp_count); 1361791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "ev_enq_count", stats.ev_enq_count); 1362791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "ev_inv_count", stats.ev_inv_count); 1363791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "evtim_retry_count", stats.evtim_retry_count); 1364791dfec2SAnkur Dwivedi rte_tel_data_add_dict_u64(d, "adapter_tick_count", stats.adapter_tick_count); 1365791dfec2SAnkur Dwivedi 1366791dfec2SAnkur Dwivedi return 0; 1367791dfec2SAnkur Dwivedi } 1368791dfec2SAnkur Dwivedi 1369791dfec2SAnkur Dwivedi RTE_INIT(ta_init_telemetry) 1370791dfec2SAnkur Dwivedi { 1371791dfec2SAnkur Dwivedi rte_telemetry_register_cmd("/eventdev/ta_info", 1372791dfec2SAnkur Dwivedi handle_ta_info, 1373791dfec2SAnkur Dwivedi "Returns Timer adapter info. Parameter: Timer adapter id"); 1374791dfec2SAnkur Dwivedi 1375791dfec2SAnkur Dwivedi rte_telemetry_register_cmd("/eventdev/ta_stats", 1376791dfec2SAnkur Dwivedi handle_ta_stats, 1377791dfec2SAnkur Dwivedi "Returns Timer adapter stats. Parameter: Timer adapter id"); 1378791dfec2SAnkur Dwivedi } 1379