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 
699a2dd95SBruce Richardson #include <string.h>
799a2dd95SBruce Richardson #include <inttypes.h>
899a2dd95SBruce Richardson #include <stdbool.h>
999a2dd95SBruce Richardson 
1099a2dd95SBruce Richardson #include <rte_memzone.h>
1199a2dd95SBruce Richardson #include <rte_errno.h>
1299a2dd95SBruce Richardson #include <rte_malloc.h>
1399a2dd95SBruce Richardson #include <rte_mempool.h>
1499a2dd95SBruce Richardson #include <rte_common.h>
1599a2dd95SBruce Richardson #include <rte_timer.h>
1699a2dd95SBruce Richardson #include <rte_service_component.h>
17*791dfec2SAnkur Dwivedi #include <rte_telemetry.h>
1899a2dd95SBruce Richardson 
1953548ad3SPavan Nikhilesh #include "event_timer_adapter_pmd.h"
2099a2dd95SBruce Richardson #include "eventdev_pmd.h"
2199a2dd95SBruce Richardson #include "rte_event_timer_adapter.h"
2253548ad3SPavan Nikhilesh #include "rte_eventdev.h"
23f26f2ca6SPavan Nikhilesh #include "eventdev_trace.h"
2499a2dd95SBruce Richardson 
2599a2dd95SBruce Richardson #define DATA_MZ_NAME_MAX_LEN 64
2699a2dd95SBruce Richardson #define DATA_MZ_NAME_FORMAT "rte_event_timer_adapter_data_%d"
2799a2dd95SBruce Richardson 
28eeded204SDavid Marchand RTE_LOG_REGISTER_SUFFIX(evtim_logtype, adapter.timer, NOTICE);
29eeded204SDavid Marchand RTE_LOG_REGISTER_SUFFIX(evtim_buffer_logtype, adapter.timer, NOTICE);
30eeded204SDavid Marchand RTE_LOG_REGISTER_SUFFIX(evtim_svc_logtype, adapter.timer.svc, NOTICE);
3199a2dd95SBruce Richardson 
32f3f3a917SPavan Nikhilesh static struct rte_event_timer_adapter *adapters;
3399a2dd95SBruce Richardson 
3453548ad3SPavan Nikhilesh static const struct event_timer_adapter_ops swtim_ops;
3599a2dd95SBruce Richardson 
3699a2dd95SBruce Richardson #define EVTIM_LOG(level, logtype, ...) \
3799a2dd95SBruce Richardson 	rte_log(RTE_LOG_ ## level, logtype, \
3899a2dd95SBruce Richardson 		RTE_FMT("EVTIMER: %s() line %u: " RTE_FMT_HEAD(__VA_ARGS__,) \
3999a2dd95SBruce Richardson 			"\n", __func__, __LINE__, RTE_FMT_TAIL(__VA_ARGS__,)))
4099a2dd95SBruce Richardson 
4199a2dd95SBruce Richardson #define EVTIM_LOG_ERR(...) EVTIM_LOG(ERR, evtim_logtype, __VA_ARGS__)
4299a2dd95SBruce Richardson 
4399a2dd95SBruce Richardson #ifdef RTE_LIBRTE_EVENTDEV_DEBUG
4499a2dd95SBruce Richardson #define EVTIM_LOG_DBG(...) \
4599a2dd95SBruce Richardson 	EVTIM_LOG(DEBUG, evtim_logtype, __VA_ARGS__)
4699a2dd95SBruce Richardson #define EVTIM_BUF_LOG_DBG(...) \
4799a2dd95SBruce Richardson 	EVTIM_LOG(DEBUG, evtim_buffer_logtype, __VA_ARGS__)
4899a2dd95SBruce Richardson #define EVTIM_SVC_LOG_DBG(...) \
4999a2dd95SBruce Richardson 	EVTIM_LOG(DEBUG, evtim_svc_logtype, __VA_ARGS__)
5099a2dd95SBruce Richardson #else
5199a2dd95SBruce Richardson #define EVTIM_LOG_DBG(...) (void)0
5299a2dd95SBruce Richardson #define EVTIM_BUF_LOG_DBG(...) (void)0
5399a2dd95SBruce Richardson #define EVTIM_SVC_LOG_DBG(...) (void)0
5499a2dd95SBruce Richardson #endif
5599a2dd95SBruce Richardson 
5699a2dd95SBruce Richardson static int
default_port_conf_cb(uint16_t id,uint8_t event_dev_id,uint8_t * event_port_id,void * conf_arg)5799a2dd95SBruce Richardson default_port_conf_cb(uint16_t id, uint8_t event_dev_id, uint8_t *event_port_id,
5899a2dd95SBruce Richardson 		     void *conf_arg)
5999a2dd95SBruce Richardson {
6099a2dd95SBruce Richardson 	struct rte_event_timer_adapter *adapter;
6199a2dd95SBruce Richardson 	struct rte_eventdev *dev;
6299a2dd95SBruce Richardson 	struct rte_event_dev_config dev_conf;
6399a2dd95SBruce Richardson 	struct rte_event_port_conf *port_conf, def_port_conf = {0};
6499a2dd95SBruce Richardson 	int started;
6599a2dd95SBruce Richardson 	uint8_t port_id;
6699a2dd95SBruce Richardson 	uint8_t dev_id;
6799a2dd95SBruce Richardson 	int ret;
6899a2dd95SBruce Richardson 
6999a2dd95SBruce Richardson 	RTE_SET_USED(event_dev_id);
7099a2dd95SBruce Richardson 
7199a2dd95SBruce Richardson 	adapter = &adapters[id];
7299a2dd95SBruce Richardson 	dev = &rte_eventdevs[adapter->data->event_dev_id];
7399a2dd95SBruce Richardson 	dev_id = dev->data->dev_id;
7499a2dd95SBruce Richardson 	dev_conf = dev->data->dev_conf;
7599a2dd95SBruce Richardson 
7699a2dd95SBruce Richardson 	started = dev->data->dev_started;
7799a2dd95SBruce Richardson 	if (started)
7899a2dd95SBruce Richardson 		rte_event_dev_stop(dev_id);
7999a2dd95SBruce Richardson 
8099a2dd95SBruce Richardson 	port_id = dev_conf.nb_event_ports;
8199a2dd95SBruce Richardson 	dev_conf.nb_event_ports += 1;
8299a2dd95SBruce Richardson 	ret = rte_event_dev_configure(dev_id, &dev_conf);
8399a2dd95SBruce Richardson 	if (ret < 0) {
8499a2dd95SBruce Richardson 		EVTIM_LOG_ERR("failed to configure event dev %u\n", dev_id);
8599a2dd95SBruce Richardson 		if (started)
8699a2dd95SBruce Richardson 			if (rte_event_dev_start(dev_id))
8799a2dd95SBruce Richardson 				return -EIO;
8899a2dd95SBruce Richardson 
8999a2dd95SBruce Richardson 		return ret;
9099a2dd95SBruce Richardson 	}
9199a2dd95SBruce Richardson 
9299a2dd95SBruce Richardson 	if (conf_arg != NULL)
9399a2dd95SBruce Richardson 		port_conf = conf_arg;
9499a2dd95SBruce Richardson 	else {
9599a2dd95SBruce Richardson 		port_conf = &def_port_conf;
9699a2dd95SBruce Richardson 		ret = rte_event_port_default_conf_get(dev_id, port_id,
9799a2dd95SBruce Richardson 						      port_conf);
9899a2dd95SBruce Richardson 		if (ret < 0)
9999a2dd95SBruce Richardson 			return ret;
10099a2dd95SBruce Richardson 	}
10199a2dd95SBruce Richardson 
10299a2dd95SBruce Richardson 	ret = rte_event_port_setup(dev_id, port_id, port_conf);
10399a2dd95SBruce Richardson 	if (ret < 0) {
10499a2dd95SBruce Richardson 		EVTIM_LOG_ERR("failed to setup event port %u on event dev %u\n",
10599a2dd95SBruce Richardson 			      port_id, dev_id);
10699a2dd95SBruce Richardson 		return ret;
10799a2dd95SBruce Richardson 	}
10899a2dd95SBruce Richardson 
10999a2dd95SBruce Richardson 	*event_port_id = port_id;
11099a2dd95SBruce Richardson 
11199a2dd95SBruce Richardson 	if (started)
11299a2dd95SBruce Richardson 		ret = rte_event_dev_start(dev_id);
11399a2dd95SBruce Richardson 
11499a2dd95SBruce Richardson 	return ret;
11599a2dd95SBruce Richardson }
11699a2dd95SBruce Richardson 
11799a2dd95SBruce Richardson struct rte_event_timer_adapter *
rte_event_timer_adapter_create(const struct rte_event_timer_adapter_conf * conf)11899a2dd95SBruce Richardson rte_event_timer_adapter_create(const struct rte_event_timer_adapter_conf *conf)
11999a2dd95SBruce Richardson {
12099a2dd95SBruce Richardson 	return rte_event_timer_adapter_create_ext(conf, default_port_conf_cb,
12199a2dd95SBruce Richardson 						  NULL);
12299a2dd95SBruce Richardson }
12399a2dd95SBruce Richardson 
12499a2dd95SBruce Richardson struct rte_event_timer_adapter *
rte_event_timer_adapter_create_ext(const struct rte_event_timer_adapter_conf * conf,rte_event_timer_adapter_port_conf_cb_t conf_cb,void * conf_arg)12599a2dd95SBruce Richardson rte_event_timer_adapter_create_ext(
12699a2dd95SBruce Richardson 		const struct rte_event_timer_adapter_conf *conf,
12799a2dd95SBruce Richardson 		rte_event_timer_adapter_port_conf_cb_t conf_cb,
12899a2dd95SBruce Richardson 		void *conf_arg)
12999a2dd95SBruce Richardson {
13099a2dd95SBruce Richardson 	uint16_t adapter_id;
13199a2dd95SBruce Richardson 	struct rte_event_timer_adapter *adapter;
13299a2dd95SBruce Richardson 	const struct rte_memzone *mz;
13399a2dd95SBruce Richardson 	char mz_name[DATA_MZ_NAME_MAX_LEN];
13499a2dd95SBruce Richardson 	int n, ret;
13599a2dd95SBruce Richardson 	struct rte_eventdev *dev;
13699a2dd95SBruce Richardson 
137f3f3a917SPavan Nikhilesh 	if (adapters == NULL) {
138f3f3a917SPavan Nikhilesh 		adapters = rte_zmalloc("Eventdev",
139f3f3a917SPavan Nikhilesh 				       sizeof(struct rte_event_timer_adapter) *
140f3f3a917SPavan Nikhilesh 					       RTE_EVENT_TIMER_ADAPTER_NUM_MAX,
141f3f3a917SPavan Nikhilesh 				       RTE_CACHE_LINE_SIZE);
142f3f3a917SPavan Nikhilesh 		if (adapters == NULL) {
143f3f3a917SPavan Nikhilesh 			rte_errno = ENOMEM;
144f3f3a917SPavan Nikhilesh 			return NULL;
145f3f3a917SPavan Nikhilesh 		}
146f3f3a917SPavan Nikhilesh 	}
147f3f3a917SPavan Nikhilesh 
14899a2dd95SBruce Richardson 	if (conf == NULL) {
14999a2dd95SBruce Richardson 		rte_errno = EINVAL;
15099a2dd95SBruce Richardson 		return NULL;
15199a2dd95SBruce Richardson 	}
15299a2dd95SBruce Richardson 
15399a2dd95SBruce Richardson 	/* Check eventdev ID */
15499a2dd95SBruce Richardson 	if (!rte_event_pmd_is_valid_dev(conf->event_dev_id)) {
15599a2dd95SBruce Richardson 		rte_errno = EINVAL;
15699a2dd95SBruce Richardson 		return NULL;
15799a2dd95SBruce Richardson 	}
15899a2dd95SBruce Richardson 	dev = &rte_eventdevs[conf->event_dev_id];
15999a2dd95SBruce Richardson 
16099a2dd95SBruce Richardson 	adapter_id = conf->timer_adapter_id;
16199a2dd95SBruce Richardson 
16299a2dd95SBruce Richardson 	/* Check that adapter_id is in range */
16399a2dd95SBruce Richardson 	if (adapter_id >= RTE_EVENT_TIMER_ADAPTER_NUM_MAX) {
16499a2dd95SBruce Richardson 		rte_errno = EINVAL;
16599a2dd95SBruce Richardson 		return NULL;
16699a2dd95SBruce Richardson 	}
16799a2dd95SBruce Richardson 
16899a2dd95SBruce Richardson 	/* Check adapter ID not already allocated */
16999a2dd95SBruce Richardson 	adapter = &adapters[adapter_id];
17099a2dd95SBruce Richardson 	if (adapter->allocated) {
17199a2dd95SBruce Richardson 		rte_errno = EEXIST;
17299a2dd95SBruce Richardson 		return NULL;
17399a2dd95SBruce Richardson 	}
17499a2dd95SBruce Richardson 
17599a2dd95SBruce Richardson 	/* Create shared data area. */
17699a2dd95SBruce Richardson 	n = snprintf(mz_name, sizeof(mz_name), DATA_MZ_NAME_FORMAT, adapter_id);
17799a2dd95SBruce Richardson 	if (n >= (int)sizeof(mz_name)) {
17899a2dd95SBruce Richardson 		rte_errno = EINVAL;
17999a2dd95SBruce Richardson 		return NULL;
18099a2dd95SBruce Richardson 	}
18199a2dd95SBruce Richardson 	mz = rte_memzone_reserve(mz_name,
18299a2dd95SBruce Richardson 				 sizeof(struct rte_event_timer_adapter_data),
18399a2dd95SBruce Richardson 				 conf->socket_id, 0);
18499a2dd95SBruce Richardson 	if (mz == NULL)
18599a2dd95SBruce Richardson 		/* rte_errno set by rte_memzone_reserve */
18699a2dd95SBruce Richardson 		return NULL;
18799a2dd95SBruce Richardson 
18899a2dd95SBruce Richardson 	adapter->data = mz->addr;
18999a2dd95SBruce Richardson 	memset(adapter->data, 0, sizeof(struct rte_event_timer_adapter_data));
19099a2dd95SBruce Richardson 
19199a2dd95SBruce Richardson 	adapter->data->mz = mz;
19299a2dd95SBruce Richardson 	adapter->data->event_dev_id = conf->event_dev_id;
19399a2dd95SBruce Richardson 	adapter->data->id = adapter_id;
19499a2dd95SBruce Richardson 	adapter->data->socket_id = conf->socket_id;
19599a2dd95SBruce Richardson 	adapter->data->conf = *conf;  /* copy conf structure */
19699a2dd95SBruce Richardson 
19799a2dd95SBruce Richardson 	/* Query eventdev PMD for timer adapter capabilities and ops */
19899a2dd95SBruce Richardson 	ret = dev->dev_ops->timer_adapter_caps_get(dev,
19999a2dd95SBruce Richardson 						   adapter->data->conf.flags,
20099a2dd95SBruce Richardson 						   &adapter->data->caps,
20199a2dd95SBruce Richardson 						   &adapter->ops);
20299a2dd95SBruce Richardson 	if (ret < 0) {
20399a2dd95SBruce Richardson 		rte_errno = -ret;
20499a2dd95SBruce Richardson 		goto free_memzone;
20599a2dd95SBruce Richardson 	}
20699a2dd95SBruce Richardson 
20799a2dd95SBruce Richardson 	if (!(adapter->data->caps &
20899a2dd95SBruce Richardson 	      RTE_EVENT_TIMER_ADAPTER_CAP_INTERNAL_PORT)) {
20999a2dd95SBruce Richardson 		FUNC_PTR_OR_NULL_RET_WITH_ERRNO(conf_cb, EINVAL);
21099a2dd95SBruce Richardson 		ret = conf_cb(adapter->data->id, adapter->data->event_dev_id,
21199a2dd95SBruce Richardson 			      &adapter->data->event_port_id, conf_arg);
21299a2dd95SBruce Richardson 		if (ret < 0) {
21399a2dd95SBruce Richardson 			rte_errno = -ret;
21499a2dd95SBruce Richardson 			goto free_memzone;
21599a2dd95SBruce Richardson 		}
21699a2dd95SBruce Richardson 	}
21799a2dd95SBruce Richardson 
21899a2dd95SBruce Richardson 	/* If eventdev PMD did not provide ops, use default software
21999a2dd95SBruce Richardson 	 * implementation.
22099a2dd95SBruce Richardson 	 */
22199a2dd95SBruce Richardson 	if (adapter->ops == NULL)
22299a2dd95SBruce Richardson 		adapter->ops = &swtim_ops;
22399a2dd95SBruce Richardson 
22499a2dd95SBruce Richardson 	/* Allow driver to do some setup */
22599a2dd95SBruce Richardson 	FUNC_PTR_OR_NULL_RET_WITH_ERRNO(adapter->ops->init, ENOTSUP);
22699a2dd95SBruce Richardson 	ret = adapter->ops->init(adapter);
22799a2dd95SBruce Richardson 	if (ret < 0) {
22899a2dd95SBruce Richardson 		rte_errno = -ret;
22999a2dd95SBruce Richardson 		goto free_memzone;
23099a2dd95SBruce Richardson 	}
23199a2dd95SBruce Richardson 
23299a2dd95SBruce Richardson 	/* Set fast-path function pointers */
23399a2dd95SBruce Richardson 	adapter->arm_burst = adapter->ops->arm_burst;
23499a2dd95SBruce Richardson 	adapter->arm_tmo_tick_burst = adapter->ops->arm_tmo_tick_burst;
23599a2dd95SBruce Richardson 	adapter->cancel_burst = adapter->ops->cancel_burst;
23699a2dd95SBruce Richardson 
23799a2dd95SBruce Richardson 	adapter->allocated = 1;
23899a2dd95SBruce Richardson 
23999a2dd95SBruce Richardson 	rte_eventdev_trace_timer_adapter_create(adapter_id, adapter, conf,
24099a2dd95SBruce Richardson 		conf_cb);
24199a2dd95SBruce Richardson 	return adapter;
24299a2dd95SBruce Richardson 
24399a2dd95SBruce Richardson free_memzone:
24499a2dd95SBruce Richardson 	rte_memzone_free(adapter->data->mz);
24599a2dd95SBruce Richardson 	return NULL;
24699a2dd95SBruce Richardson }
24799a2dd95SBruce Richardson 
24899a2dd95SBruce Richardson int
rte_event_timer_adapter_get_info(const struct rte_event_timer_adapter * adapter,struct rte_event_timer_adapter_info * adapter_info)24999a2dd95SBruce Richardson rte_event_timer_adapter_get_info(const struct rte_event_timer_adapter *adapter,
25099a2dd95SBruce Richardson 		struct rte_event_timer_adapter_info *adapter_info)
25199a2dd95SBruce Richardson {
25299a2dd95SBruce Richardson 	ADAPTER_VALID_OR_ERR_RET(adapter, -EINVAL);
25399a2dd95SBruce Richardson 
25499a2dd95SBruce Richardson 	if (adapter->ops->get_info)
25599a2dd95SBruce Richardson 		/* let driver set values it knows */
25699a2dd95SBruce Richardson 		adapter->ops->get_info(adapter, adapter_info);
25799a2dd95SBruce Richardson 
25899a2dd95SBruce Richardson 	/* Set common values */
25999a2dd95SBruce Richardson 	adapter_info->conf = adapter->data->conf;
26099a2dd95SBruce Richardson 	adapter_info->event_dev_port_id = adapter->data->event_port_id;
26199a2dd95SBruce Richardson 	adapter_info->caps = adapter->data->caps;
26299a2dd95SBruce Richardson 
26399a2dd95SBruce Richardson 	return 0;
26499a2dd95SBruce Richardson }
26599a2dd95SBruce Richardson 
26699a2dd95SBruce Richardson int
rte_event_timer_adapter_start(const struct rte_event_timer_adapter * adapter)26799a2dd95SBruce Richardson rte_event_timer_adapter_start(const struct rte_event_timer_adapter *adapter)
26899a2dd95SBruce Richardson {
26999a2dd95SBruce Richardson 	int ret;
27099a2dd95SBruce Richardson 
27199a2dd95SBruce Richardson 	ADAPTER_VALID_OR_ERR_RET(adapter, -EINVAL);
27299a2dd95SBruce Richardson 	FUNC_PTR_OR_ERR_RET(adapter->ops->start, -EINVAL);
27399a2dd95SBruce Richardson 
27499a2dd95SBruce Richardson 	if (adapter->data->started) {
27599a2dd95SBruce Richardson 		EVTIM_LOG_ERR("event timer adapter %"PRIu8" already started",
27699a2dd95SBruce Richardson 			      adapter->data->id);
27799a2dd95SBruce Richardson 		return -EALREADY;
27899a2dd95SBruce Richardson 	}
27999a2dd95SBruce Richardson 
28099a2dd95SBruce Richardson 	ret = adapter->ops->start(adapter);
28199a2dd95SBruce Richardson 	if (ret < 0)
28299a2dd95SBruce Richardson 		return ret;
28399a2dd95SBruce Richardson 
28499a2dd95SBruce Richardson 	adapter->data->started = 1;
28599a2dd95SBruce Richardson 	rte_eventdev_trace_timer_adapter_start(adapter);
28699a2dd95SBruce Richardson 	return 0;
28799a2dd95SBruce Richardson }
28899a2dd95SBruce Richardson 
28999a2dd95SBruce Richardson int
rte_event_timer_adapter_stop(const struct rte_event_timer_adapter * adapter)29099a2dd95SBruce Richardson rte_event_timer_adapter_stop(const struct rte_event_timer_adapter *adapter)
29199a2dd95SBruce Richardson {
29299a2dd95SBruce Richardson 	int ret;
29399a2dd95SBruce Richardson 
29499a2dd95SBruce Richardson 	ADAPTER_VALID_OR_ERR_RET(adapter, -EINVAL);
29599a2dd95SBruce Richardson 	FUNC_PTR_OR_ERR_RET(adapter->ops->stop, -EINVAL);
29699a2dd95SBruce Richardson 
29799a2dd95SBruce Richardson 	if (adapter->data->started == 0) {
29899a2dd95SBruce Richardson 		EVTIM_LOG_ERR("event timer adapter %"PRIu8" already stopped",
29999a2dd95SBruce Richardson 			      adapter->data->id);
30099a2dd95SBruce Richardson 		return 0;
30199a2dd95SBruce Richardson 	}
30299a2dd95SBruce Richardson 
30399a2dd95SBruce Richardson 	ret = adapter->ops->stop(adapter);
30499a2dd95SBruce Richardson 	if (ret < 0)
30599a2dd95SBruce Richardson 		return ret;
30699a2dd95SBruce Richardson 
30799a2dd95SBruce Richardson 	adapter->data->started = 0;
30899a2dd95SBruce Richardson 	rte_eventdev_trace_timer_adapter_stop(adapter);
30999a2dd95SBruce Richardson 	return 0;
31099a2dd95SBruce Richardson }
31199a2dd95SBruce Richardson 
31299a2dd95SBruce Richardson struct rte_event_timer_adapter *
rte_event_timer_adapter_lookup(uint16_t adapter_id)31399a2dd95SBruce Richardson rte_event_timer_adapter_lookup(uint16_t adapter_id)
31499a2dd95SBruce Richardson {
31599a2dd95SBruce Richardson 	char name[DATA_MZ_NAME_MAX_LEN];
31699a2dd95SBruce Richardson 	const struct rte_memzone *mz;
31799a2dd95SBruce Richardson 	struct rte_event_timer_adapter_data *data;
31899a2dd95SBruce Richardson 	struct rte_event_timer_adapter *adapter;
31999a2dd95SBruce Richardson 	int ret;
32099a2dd95SBruce Richardson 	struct rte_eventdev *dev;
32199a2dd95SBruce Richardson 
322f3f3a917SPavan Nikhilesh 	if (adapters == NULL) {
323f3f3a917SPavan Nikhilesh 		adapters = rte_zmalloc("Eventdev",
324f3f3a917SPavan Nikhilesh 				       sizeof(struct rte_event_timer_adapter) *
325f3f3a917SPavan Nikhilesh 					       RTE_EVENT_TIMER_ADAPTER_NUM_MAX,
326f3f3a917SPavan Nikhilesh 				       RTE_CACHE_LINE_SIZE);
327f3f3a917SPavan Nikhilesh 		if (adapters == NULL) {
328f3f3a917SPavan Nikhilesh 			rte_errno = ENOMEM;
329f3f3a917SPavan Nikhilesh 			return NULL;
330f3f3a917SPavan Nikhilesh 		}
331f3f3a917SPavan Nikhilesh 	}
332f3f3a917SPavan Nikhilesh 
33399a2dd95SBruce Richardson 	if (adapters[adapter_id].allocated)
33499a2dd95SBruce Richardson 		return &adapters[adapter_id]; /* Adapter is already loaded */
33599a2dd95SBruce Richardson 
33699a2dd95SBruce Richardson 	snprintf(name, DATA_MZ_NAME_MAX_LEN, DATA_MZ_NAME_FORMAT, adapter_id);
33799a2dd95SBruce Richardson 	mz = rte_memzone_lookup(name);
33899a2dd95SBruce Richardson 	if (mz == NULL) {
33999a2dd95SBruce Richardson 		rte_errno = ENOENT;
34099a2dd95SBruce Richardson 		return NULL;
34199a2dd95SBruce Richardson 	}
34299a2dd95SBruce Richardson 
34399a2dd95SBruce Richardson 	data = mz->addr;
34499a2dd95SBruce Richardson 
34599a2dd95SBruce Richardson 	adapter = &adapters[data->id];
34699a2dd95SBruce Richardson 	adapter->data = data;
34799a2dd95SBruce Richardson 
34899a2dd95SBruce Richardson 	dev = &rte_eventdevs[adapter->data->event_dev_id];
34999a2dd95SBruce Richardson 
35099a2dd95SBruce Richardson 	/* Query eventdev PMD for timer adapter capabilities and ops */
35199a2dd95SBruce Richardson 	ret = dev->dev_ops->timer_adapter_caps_get(dev,
35299a2dd95SBruce Richardson 						   adapter->data->conf.flags,
35399a2dd95SBruce Richardson 						   &adapter->data->caps,
35499a2dd95SBruce Richardson 						   &adapter->ops);
35599a2dd95SBruce Richardson 	if (ret < 0) {
35699a2dd95SBruce Richardson 		rte_errno = EINVAL;
35799a2dd95SBruce Richardson 		return NULL;
35899a2dd95SBruce Richardson 	}
35999a2dd95SBruce Richardson 
36099a2dd95SBruce Richardson 	/* If eventdev PMD did not provide ops, use default software
36199a2dd95SBruce Richardson 	 * implementation.
36299a2dd95SBruce Richardson 	 */
36399a2dd95SBruce Richardson 	if (adapter->ops == NULL)
36499a2dd95SBruce Richardson 		adapter->ops = &swtim_ops;
36599a2dd95SBruce Richardson 
36699a2dd95SBruce Richardson 	/* Set fast-path function pointers */
36799a2dd95SBruce Richardson 	adapter->arm_burst = adapter->ops->arm_burst;
36899a2dd95SBruce Richardson 	adapter->arm_tmo_tick_burst = adapter->ops->arm_tmo_tick_burst;
36999a2dd95SBruce Richardson 	adapter->cancel_burst = adapter->ops->cancel_burst;
37099a2dd95SBruce Richardson 
37199a2dd95SBruce Richardson 	adapter->allocated = 1;
37299a2dd95SBruce Richardson 
37399a2dd95SBruce Richardson 	return adapter;
37499a2dd95SBruce Richardson }
37599a2dd95SBruce Richardson 
37699a2dd95SBruce Richardson int
rte_event_timer_adapter_free(struct rte_event_timer_adapter * adapter)37799a2dd95SBruce Richardson rte_event_timer_adapter_free(struct rte_event_timer_adapter *adapter)
37899a2dd95SBruce Richardson {
379f3f3a917SPavan Nikhilesh 	int i, ret;
38099a2dd95SBruce Richardson 
38199a2dd95SBruce Richardson 	ADAPTER_VALID_OR_ERR_RET(adapter, -EINVAL);
38299a2dd95SBruce Richardson 	FUNC_PTR_OR_ERR_RET(adapter->ops->uninit, -EINVAL);
38399a2dd95SBruce Richardson 
38499a2dd95SBruce Richardson 	if (adapter->data->started == 1) {
38599a2dd95SBruce Richardson 		EVTIM_LOG_ERR("event timer adapter %"PRIu8" must be stopped "
38699a2dd95SBruce Richardson 			      "before freeing", adapter->data->id);
38799a2dd95SBruce Richardson 		return -EBUSY;
38899a2dd95SBruce Richardson 	}
38999a2dd95SBruce Richardson 
39099a2dd95SBruce Richardson 	/* free impl priv data */
39199a2dd95SBruce Richardson 	ret = adapter->ops->uninit(adapter);
39299a2dd95SBruce Richardson 	if (ret < 0)
39399a2dd95SBruce Richardson 		return ret;
39499a2dd95SBruce Richardson 
39599a2dd95SBruce Richardson 	/* free shared data area */
39699a2dd95SBruce Richardson 	ret = rte_memzone_free(adapter->data->mz);
39799a2dd95SBruce Richardson 	if (ret < 0)
39899a2dd95SBruce Richardson 		return ret;
39999a2dd95SBruce Richardson 
40099a2dd95SBruce Richardson 	adapter->data = NULL;
40199a2dd95SBruce Richardson 	adapter->allocated = 0;
40299a2dd95SBruce Richardson 
403f3f3a917SPavan Nikhilesh 	ret = 0;
404f3f3a917SPavan Nikhilesh 	for (i = 0; i < RTE_EVENT_TIMER_ADAPTER_NUM_MAX; i++)
405f3f3a917SPavan Nikhilesh 		if (adapters[i].allocated)
406f3f3a917SPavan Nikhilesh 			ret = adapters[i].allocated;
407f3f3a917SPavan Nikhilesh 
408f3f3a917SPavan Nikhilesh 	if (!ret) {
409f3f3a917SPavan Nikhilesh 		rte_free(adapters);
410f3f3a917SPavan Nikhilesh 		adapters = NULL;
411f3f3a917SPavan Nikhilesh 	}
412f3f3a917SPavan Nikhilesh 
41399a2dd95SBruce Richardson 	rte_eventdev_trace_timer_adapter_free(adapter);
41499a2dd95SBruce Richardson 	return 0;
41599a2dd95SBruce Richardson }
41699a2dd95SBruce Richardson 
41799a2dd95SBruce Richardson int
rte_event_timer_adapter_service_id_get(struct rte_event_timer_adapter * adapter,uint32_t * service_id)41899a2dd95SBruce Richardson rte_event_timer_adapter_service_id_get(struct rte_event_timer_adapter *adapter,
41999a2dd95SBruce Richardson 				       uint32_t *service_id)
42099a2dd95SBruce Richardson {
42199a2dd95SBruce Richardson 	ADAPTER_VALID_OR_ERR_RET(adapter, -EINVAL);
42299a2dd95SBruce Richardson 
42399a2dd95SBruce Richardson 	if (adapter->data->service_inited && service_id != NULL)
42499a2dd95SBruce Richardson 		*service_id = adapter->data->service_id;
42599a2dd95SBruce Richardson 
42699a2dd95SBruce Richardson 	return adapter->data->service_inited ? 0 : -ESRCH;
42799a2dd95SBruce Richardson }
42899a2dd95SBruce Richardson 
42999a2dd95SBruce Richardson int
rte_event_timer_adapter_stats_get(struct rte_event_timer_adapter * adapter,struct rte_event_timer_adapter_stats * stats)43099a2dd95SBruce Richardson rte_event_timer_adapter_stats_get(struct rte_event_timer_adapter *adapter,
43199a2dd95SBruce Richardson 				  struct rte_event_timer_adapter_stats *stats)
43299a2dd95SBruce Richardson {
43399a2dd95SBruce Richardson 	ADAPTER_VALID_OR_ERR_RET(adapter, -EINVAL);
43499a2dd95SBruce Richardson 	FUNC_PTR_OR_ERR_RET(adapter->ops->stats_get, -EINVAL);
43599a2dd95SBruce Richardson 	if (stats == NULL)
43699a2dd95SBruce Richardson 		return -EINVAL;
43799a2dd95SBruce Richardson 
43899a2dd95SBruce Richardson 	return adapter->ops->stats_get(adapter, stats);
43999a2dd95SBruce Richardson }
44099a2dd95SBruce Richardson 
44199a2dd95SBruce Richardson int
rte_event_timer_adapter_stats_reset(struct rte_event_timer_adapter * adapter)44299a2dd95SBruce Richardson rte_event_timer_adapter_stats_reset(struct rte_event_timer_adapter *adapter)
44399a2dd95SBruce Richardson {
44499a2dd95SBruce Richardson 	ADAPTER_VALID_OR_ERR_RET(adapter, -EINVAL);
44599a2dd95SBruce Richardson 	FUNC_PTR_OR_ERR_RET(adapter->ops->stats_reset, -EINVAL);
44699a2dd95SBruce Richardson 	return adapter->ops->stats_reset(adapter);
44799a2dd95SBruce Richardson }
44899a2dd95SBruce Richardson 
44999a2dd95SBruce Richardson /*
45099a2dd95SBruce Richardson  * Software event timer adapter buffer helper functions
45199a2dd95SBruce Richardson  */
45299a2dd95SBruce Richardson 
45399a2dd95SBruce Richardson #define NSECPERSEC 1E9
45499a2dd95SBruce Richardson 
45599a2dd95SBruce Richardson /* Optimizations used to index into the buffer require that the buffer size
45699a2dd95SBruce Richardson  * be a power of 2.
45799a2dd95SBruce Richardson  */
45899a2dd95SBruce Richardson #define EVENT_BUFFER_SZ 4096
45999a2dd95SBruce Richardson #define EVENT_BUFFER_BATCHSZ 32
46099a2dd95SBruce Richardson #define EVENT_BUFFER_MASK (EVENT_BUFFER_SZ - 1)
46199a2dd95SBruce Richardson 
46299a2dd95SBruce Richardson #define EXP_TIM_BUF_SZ 128
46399a2dd95SBruce Richardson 
46499a2dd95SBruce Richardson struct event_buffer {
46599a2dd95SBruce Richardson 	size_t head;
46699a2dd95SBruce Richardson 	size_t tail;
46799a2dd95SBruce Richardson 	struct rte_event events[EVENT_BUFFER_SZ];
46899a2dd95SBruce Richardson } __rte_cache_aligned;
46999a2dd95SBruce Richardson 
47099a2dd95SBruce Richardson static inline bool
event_buffer_full(struct event_buffer * bufp)47199a2dd95SBruce Richardson event_buffer_full(struct event_buffer *bufp)
47299a2dd95SBruce Richardson {
47399a2dd95SBruce Richardson 	return (bufp->head - bufp->tail) == EVENT_BUFFER_SZ;
47499a2dd95SBruce Richardson }
47599a2dd95SBruce Richardson 
47699a2dd95SBruce Richardson static inline bool
event_buffer_batch_ready(struct event_buffer * bufp)47799a2dd95SBruce Richardson event_buffer_batch_ready(struct event_buffer *bufp)
47899a2dd95SBruce Richardson {
47999a2dd95SBruce Richardson 	return (bufp->head - bufp->tail) >= EVENT_BUFFER_BATCHSZ;
48099a2dd95SBruce Richardson }
48199a2dd95SBruce Richardson 
48299a2dd95SBruce Richardson static void
event_buffer_init(struct event_buffer * bufp)48399a2dd95SBruce Richardson event_buffer_init(struct event_buffer *bufp)
48499a2dd95SBruce Richardson {
48599a2dd95SBruce Richardson 	bufp->head = bufp->tail = 0;
48699a2dd95SBruce Richardson 	memset(&bufp->events, 0, sizeof(struct rte_event) * EVENT_BUFFER_SZ);
48799a2dd95SBruce Richardson }
48899a2dd95SBruce Richardson 
48999a2dd95SBruce Richardson static int
event_buffer_add(struct event_buffer * bufp,struct rte_event * eventp)49099a2dd95SBruce Richardson event_buffer_add(struct event_buffer *bufp, struct rte_event *eventp)
49199a2dd95SBruce Richardson {
49299a2dd95SBruce Richardson 	size_t head_idx;
49399a2dd95SBruce Richardson 	struct rte_event *buf_eventp;
49499a2dd95SBruce Richardson 
49599a2dd95SBruce Richardson 	if (event_buffer_full(bufp))
49699a2dd95SBruce Richardson 		return -1;
49799a2dd95SBruce Richardson 
49899a2dd95SBruce Richardson 	/* Instead of modulus, bitwise AND with mask to get head_idx. */
49999a2dd95SBruce Richardson 	head_idx = bufp->head & EVENT_BUFFER_MASK;
50099a2dd95SBruce Richardson 	buf_eventp = &bufp->events[head_idx];
50199a2dd95SBruce Richardson 	rte_memcpy(buf_eventp, eventp, sizeof(struct rte_event));
50299a2dd95SBruce Richardson 
50399a2dd95SBruce Richardson 	/* Wrap automatically when overflow occurs. */
50499a2dd95SBruce Richardson 	bufp->head++;
50599a2dd95SBruce Richardson 
50699a2dd95SBruce Richardson 	return 0;
50799a2dd95SBruce Richardson }
50899a2dd95SBruce Richardson 
50999a2dd95SBruce Richardson static void
event_buffer_flush(struct event_buffer * bufp,uint8_t dev_id,uint8_t port_id,uint16_t * nb_events_flushed,uint16_t * nb_events_inv)51099a2dd95SBruce Richardson event_buffer_flush(struct event_buffer *bufp, uint8_t dev_id, uint8_t port_id,
51199a2dd95SBruce Richardson 		   uint16_t *nb_events_flushed,
51299a2dd95SBruce Richardson 		   uint16_t *nb_events_inv)
51399a2dd95SBruce Richardson {
51499a2dd95SBruce Richardson 	struct rte_event *events = bufp->events;
51599a2dd95SBruce Richardson 	size_t head_idx, tail_idx;
51699a2dd95SBruce Richardson 	uint16_t n = 0;
51799a2dd95SBruce Richardson 
51899a2dd95SBruce Richardson 	/* Instead of modulus, bitwise AND with mask to get index. */
51999a2dd95SBruce Richardson 	head_idx = bufp->head & EVENT_BUFFER_MASK;
52099a2dd95SBruce Richardson 	tail_idx = bufp->tail & EVENT_BUFFER_MASK;
52199a2dd95SBruce Richardson 
52299a2dd95SBruce Richardson 	RTE_ASSERT(head_idx < EVENT_BUFFER_SZ && tail_idx < EVENT_BUFFER_SZ);
52399a2dd95SBruce Richardson 
5244a6672c2SStephen Hemminger 	/* Determine the largest contiguous run we can attempt to enqueue to the
52599a2dd95SBruce Richardson 	 * event device.
52699a2dd95SBruce Richardson 	 */
52799a2dd95SBruce Richardson 	if (head_idx > tail_idx)
52899a2dd95SBruce Richardson 		n = head_idx - tail_idx;
52999a2dd95SBruce Richardson 	else if (head_idx < tail_idx)
53099a2dd95SBruce Richardson 		n = EVENT_BUFFER_SZ - tail_idx;
53199a2dd95SBruce Richardson 	else if (event_buffer_full(bufp))
53299a2dd95SBruce Richardson 		n = EVENT_BUFFER_SZ - tail_idx;
53399a2dd95SBruce Richardson 	else {
53499a2dd95SBruce Richardson 		*nb_events_flushed = 0;
53599a2dd95SBruce Richardson 		return;
53699a2dd95SBruce Richardson 	}
53799a2dd95SBruce Richardson 
53899a2dd95SBruce Richardson 	n = RTE_MIN(EVENT_BUFFER_BATCHSZ, n);
53999a2dd95SBruce Richardson 	*nb_events_inv = 0;
54099a2dd95SBruce Richardson 
54199a2dd95SBruce Richardson 	*nb_events_flushed = rte_event_enqueue_burst(dev_id, port_id,
54299a2dd95SBruce Richardson 						     &events[tail_idx], n);
54399a2dd95SBruce Richardson 	if (*nb_events_flushed != n) {
54499a2dd95SBruce Richardson 		if (rte_errno == EINVAL) {
54599a2dd95SBruce Richardson 			EVTIM_LOG_ERR("failed to enqueue invalid event - "
54699a2dd95SBruce Richardson 				      "dropping it");
54799a2dd95SBruce Richardson 			(*nb_events_inv)++;
54899a2dd95SBruce Richardson 		} else if (rte_errno == ENOSPC)
54999a2dd95SBruce Richardson 			rte_pause();
55099a2dd95SBruce Richardson 	}
55199a2dd95SBruce Richardson 
55299a2dd95SBruce Richardson 	if (*nb_events_flushed > 0)
55399a2dd95SBruce Richardson 		EVTIM_BUF_LOG_DBG("enqueued %"PRIu16" timer events to event "
55499a2dd95SBruce Richardson 				  "device", *nb_events_flushed);
55599a2dd95SBruce Richardson 
55699a2dd95SBruce Richardson 	bufp->tail = bufp->tail + *nb_events_flushed + *nb_events_inv;
55799a2dd95SBruce Richardson }
55899a2dd95SBruce Richardson 
55999a2dd95SBruce Richardson /*
56099a2dd95SBruce Richardson  * Software event timer adapter implementation
56199a2dd95SBruce Richardson  */
56299a2dd95SBruce Richardson struct swtim {
56399a2dd95SBruce Richardson 	/* Identifier of service executing timer management logic. */
56499a2dd95SBruce Richardson 	uint32_t service_id;
56599a2dd95SBruce Richardson 	/* The cycle count at which the adapter should next tick */
56699a2dd95SBruce Richardson 	uint64_t next_tick_cycles;
56799a2dd95SBruce Richardson 	/* The tick resolution used by adapter instance. May have been
56899a2dd95SBruce Richardson 	 * adjusted from what user requested
56999a2dd95SBruce Richardson 	 */
57099a2dd95SBruce Richardson 	uint64_t timer_tick_ns;
57199a2dd95SBruce Richardson 	/* Maximum timeout in nanoseconds allowed by adapter instance. */
57299a2dd95SBruce Richardson 	uint64_t max_tmo_ns;
57399a2dd95SBruce Richardson 	/* Buffered timer expiry events to be enqueued to an event device. */
57499a2dd95SBruce Richardson 	struct event_buffer buffer;
57599a2dd95SBruce Richardson 	/* Statistics */
57699a2dd95SBruce Richardson 	struct rte_event_timer_adapter_stats stats;
57799a2dd95SBruce Richardson 	/* Mempool of timer objects */
57899a2dd95SBruce Richardson 	struct rte_mempool *tim_pool;
57999a2dd95SBruce Richardson 	/* Back pointer for convenience */
58099a2dd95SBruce Richardson 	struct rte_event_timer_adapter *adapter;
58199a2dd95SBruce Richardson 	/* Identifier of timer data instance */
58299a2dd95SBruce Richardson 	uint32_t timer_data_id;
58399a2dd95SBruce Richardson 	/* Track which cores have actually armed a timer */
58499a2dd95SBruce Richardson 	struct {
58599a2dd95SBruce Richardson 		uint16_t v;
58699a2dd95SBruce Richardson 	} __rte_cache_aligned in_use[RTE_MAX_LCORE];
58799a2dd95SBruce Richardson 	/* Track which cores' timer lists should be polled */
58899a2dd95SBruce Richardson 	unsigned int poll_lcores[RTE_MAX_LCORE];
58999a2dd95SBruce Richardson 	/* The number of lists that should be polled */
59099a2dd95SBruce Richardson 	int n_poll_lcores;
59199a2dd95SBruce Richardson 	/* Timers which have expired and can be returned to a mempool */
59299a2dd95SBruce Richardson 	struct rte_timer *expired_timers[EXP_TIM_BUF_SZ];
59399a2dd95SBruce Richardson 	/* The number of timers that can be returned to a mempool */
59499a2dd95SBruce Richardson 	size_t n_expired_timers;
59599a2dd95SBruce Richardson };
59699a2dd95SBruce Richardson 
59799a2dd95SBruce Richardson static inline struct swtim *
swtim_pmd_priv(const struct rte_event_timer_adapter * adapter)59899a2dd95SBruce Richardson swtim_pmd_priv(const struct rte_event_timer_adapter *adapter)
59999a2dd95SBruce Richardson {
60099a2dd95SBruce Richardson 	return adapter->data->adapter_priv;
60199a2dd95SBruce Richardson }
60299a2dd95SBruce Richardson 
60399a2dd95SBruce Richardson static void
swtim_callback(struct rte_timer * tim)60499a2dd95SBruce Richardson swtim_callback(struct rte_timer *tim)
60599a2dd95SBruce Richardson {
60699a2dd95SBruce Richardson 	struct rte_event_timer *evtim = tim->arg;
60799a2dd95SBruce Richardson 	struct rte_event_timer_adapter *adapter;
60899a2dd95SBruce Richardson 	unsigned int lcore = rte_lcore_id();
60999a2dd95SBruce Richardson 	struct swtim *sw;
61099a2dd95SBruce Richardson 	uint16_t nb_evs_flushed = 0;
61199a2dd95SBruce Richardson 	uint16_t nb_evs_invalid = 0;
61299a2dd95SBruce Richardson 	uint64_t opaque;
61399a2dd95SBruce Richardson 	int ret;
61499a2dd95SBruce Richardson 	int n_lcores;
61599a2dd95SBruce Richardson 
61699a2dd95SBruce Richardson 	opaque = evtim->impl_opaque[1];
61799a2dd95SBruce Richardson 	adapter = (struct rte_event_timer_adapter *)(uintptr_t)opaque;
61899a2dd95SBruce Richardson 	sw = swtim_pmd_priv(adapter);
61999a2dd95SBruce Richardson 
62099a2dd95SBruce Richardson 	ret = event_buffer_add(&sw->buffer, &evtim->ev);
62199a2dd95SBruce Richardson 	if (ret < 0) {
62299a2dd95SBruce Richardson 		/* If event buffer is full, put timer back in list with
62399a2dd95SBruce Richardson 		 * immediate expiry value, so that we process it again on the
62499a2dd95SBruce Richardson 		 * next iteration.
62599a2dd95SBruce Richardson 		 */
62699a2dd95SBruce Richardson 		ret = rte_timer_alt_reset(sw->timer_data_id, tim, 0, SINGLE,
62799a2dd95SBruce Richardson 					  lcore, NULL, evtim);
62899a2dd95SBruce Richardson 		if (ret < 0) {
62999a2dd95SBruce Richardson 			EVTIM_LOG_DBG("event buffer full, failed to reset "
63099a2dd95SBruce Richardson 				      "timer with immediate expiry value");
63199a2dd95SBruce Richardson 		} else {
63299a2dd95SBruce Richardson 			sw->stats.evtim_retry_count++;
63399a2dd95SBruce Richardson 			EVTIM_LOG_DBG("event buffer full, resetting rte_timer "
63499a2dd95SBruce Richardson 				      "with immediate expiry value");
63599a2dd95SBruce Richardson 		}
63699a2dd95SBruce Richardson 
63799a2dd95SBruce Richardson 		if (unlikely(sw->in_use[lcore].v == 0)) {
63899a2dd95SBruce Richardson 			sw->in_use[lcore].v = 1;
63999a2dd95SBruce Richardson 			n_lcores = __atomic_fetch_add(&sw->n_poll_lcores, 1,
64099a2dd95SBruce Richardson 						     __ATOMIC_RELAXED);
64199a2dd95SBruce Richardson 			__atomic_store_n(&sw->poll_lcores[n_lcores], lcore,
64299a2dd95SBruce Richardson 					__ATOMIC_RELAXED);
64399a2dd95SBruce Richardson 		}
64499a2dd95SBruce Richardson 	} else {
64599a2dd95SBruce Richardson 		EVTIM_BUF_LOG_DBG("buffered an event timer expiry event");
64699a2dd95SBruce Richardson 
64799a2dd95SBruce Richardson 		/* Empty the buffer here, if necessary, to free older expired
64899a2dd95SBruce Richardson 		 * timers only
64999a2dd95SBruce Richardson 		 */
65099a2dd95SBruce Richardson 		if (unlikely(sw->n_expired_timers == EXP_TIM_BUF_SZ)) {
65199a2dd95SBruce Richardson 			rte_mempool_put_bulk(sw->tim_pool,
65299a2dd95SBruce Richardson 					     (void **)sw->expired_timers,
65399a2dd95SBruce Richardson 					     sw->n_expired_timers);
65499a2dd95SBruce Richardson 			sw->n_expired_timers = 0;
65599a2dd95SBruce Richardson 		}
65699a2dd95SBruce Richardson 
65799a2dd95SBruce Richardson 		sw->expired_timers[sw->n_expired_timers++] = tim;
65899a2dd95SBruce Richardson 		sw->stats.evtim_exp_count++;
65999a2dd95SBruce Richardson 
66099a2dd95SBruce Richardson 		__atomic_store_n(&evtim->state, RTE_EVENT_TIMER_NOT_ARMED,
66199a2dd95SBruce Richardson 				__ATOMIC_RELEASE);
66299a2dd95SBruce Richardson 	}
66399a2dd95SBruce Richardson 
66499a2dd95SBruce Richardson 	if (event_buffer_batch_ready(&sw->buffer)) {
66599a2dd95SBruce Richardson 		event_buffer_flush(&sw->buffer,
66699a2dd95SBruce Richardson 				   adapter->data->event_dev_id,
66799a2dd95SBruce Richardson 				   adapter->data->event_port_id,
66899a2dd95SBruce Richardson 				   &nb_evs_flushed,
66999a2dd95SBruce Richardson 				   &nb_evs_invalid);
67099a2dd95SBruce Richardson 
67199a2dd95SBruce Richardson 		sw->stats.ev_enq_count += nb_evs_flushed;
67299a2dd95SBruce Richardson 		sw->stats.ev_inv_count += nb_evs_invalid;
67399a2dd95SBruce Richardson 	}
67499a2dd95SBruce Richardson }
67599a2dd95SBruce Richardson 
67699a2dd95SBruce Richardson static __rte_always_inline uint64_t
get_timeout_cycles(struct rte_event_timer * evtim,const struct rte_event_timer_adapter * adapter)67799a2dd95SBruce Richardson get_timeout_cycles(struct rte_event_timer *evtim,
67899a2dd95SBruce Richardson 		   const struct rte_event_timer_adapter *adapter)
67999a2dd95SBruce Richardson {
68099a2dd95SBruce Richardson 	struct swtim *sw = swtim_pmd_priv(adapter);
68199a2dd95SBruce Richardson 	uint64_t timeout_ns = evtim->timeout_ticks * sw->timer_tick_ns;
68299a2dd95SBruce Richardson 	return timeout_ns * rte_get_timer_hz() / NSECPERSEC;
68399a2dd95SBruce Richardson }
68499a2dd95SBruce Richardson 
68599a2dd95SBruce Richardson /* This function returns true if one or more (adapter) ticks have occurred since
68699a2dd95SBruce Richardson  * the last time it was called.
68799a2dd95SBruce Richardson  */
68899a2dd95SBruce Richardson static inline bool
swtim_did_tick(struct swtim * sw)68999a2dd95SBruce Richardson swtim_did_tick(struct swtim *sw)
69099a2dd95SBruce Richardson {
69199a2dd95SBruce Richardson 	uint64_t cycles_per_adapter_tick, start_cycles;
69299a2dd95SBruce Richardson 	uint64_t *next_tick_cyclesp;
69399a2dd95SBruce Richardson 
69499a2dd95SBruce Richardson 	next_tick_cyclesp = &sw->next_tick_cycles;
69599a2dd95SBruce Richardson 	cycles_per_adapter_tick = sw->timer_tick_ns *
69699a2dd95SBruce Richardson 			(rte_get_timer_hz() / NSECPERSEC);
69799a2dd95SBruce Richardson 	start_cycles = rte_get_timer_cycles();
69899a2dd95SBruce Richardson 
69999a2dd95SBruce Richardson 	/* Note: initially, *next_tick_cyclesp == 0, so the clause below will
70099a2dd95SBruce Richardson 	 * execute, and set things going.
70199a2dd95SBruce Richardson 	 */
70299a2dd95SBruce Richardson 
70399a2dd95SBruce Richardson 	if (start_cycles >= *next_tick_cyclesp) {
70499a2dd95SBruce Richardson 		/* Snap the current cycle count to the preceding adapter tick
70599a2dd95SBruce Richardson 		 * boundary.
70699a2dd95SBruce Richardson 		 */
70799a2dd95SBruce Richardson 		start_cycles -= start_cycles % cycles_per_adapter_tick;
70899a2dd95SBruce Richardson 		*next_tick_cyclesp = start_cycles + cycles_per_adapter_tick;
70999a2dd95SBruce Richardson 
71099a2dd95SBruce Richardson 		return true;
71199a2dd95SBruce Richardson 	}
71299a2dd95SBruce Richardson 
71399a2dd95SBruce Richardson 	return false;
71499a2dd95SBruce Richardson }
71599a2dd95SBruce Richardson 
71699a2dd95SBruce Richardson /* Check that event timer timeout value is in range */
71799a2dd95SBruce Richardson static __rte_always_inline int
check_timeout(struct rte_event_timer * evtim,const struct rte_event_timer_adapter * adapter)71899a2dd95SBruce Richardson check_timeout(struct rte_event_timer *evtim,
71999a2dd95SBruce Richardson 	      const struct rte_event_timer_adapter *adapter)
72099a2dd95SBruce Richardson {
72199a2dd95SBruce Richardson 	uint64_t tmo_nsec;
72299a2dd95SBruce Richardson 	struct swtim *sw = swtim_pmd_priv(adapter);
72399a2dd95SBruce Richardson 
72499a2dd95SBruce Richardson 	tmo_nsec = evtim->timeout_ticks * sw->timer_tick_ns;
72599a2dd95SBruce Richardson 	if (tmo_nsec > sw->max_tmo_ns)
72699a2dd95SBruce Richardson 		return -1;
72799a2dd95SBruce Richardson 	if (tmo_nsec < sw->timer_tick_ns)
72899a2dd95SBruce Richardson 		return -2;
72999a2dd95SBruce Richardson 
73099a2dd95SBruce Richardson 	return 0;
73199a2dd95SBruce Richardson }
73299a2dd95SBruce Richardson 
73399a2dd95SBruce Richardson /* Check that event timer event queue sched type matches destination event queue
73499a2dd95SBruce Richardson  * sched type
73599a2dd95SBruce Richardson  */
73699a2dd95SBruce Richardson static __rte_always_inline int
check_destination_event_queue(struct rte_event_timer * evtim,const struct rte_event_timer_adapter * adapter)73799a2dd95SBruce Richardson check_destination_event_queue(struct rte_event_timer *evtim,
73899a2dd95SBruce Richardson 			      const struct rte_event_timer_adapter *adapter)
73999a2dd95SBruce Richardson {
74099a2dd95SBruce Richardson 	int ret;
74199a2dd95SBruce Richardson 	uint32_t sched_type;
74299a2dd95SBruce Richardson 
74399a2dd95SBruce Richardson 	ret = rte_event_queue_attr_get(adapter->data->event_dev_id,
74499a2dd95SBruce Richardson 				       evtim->ev.queue_id,
74599a2dd95SBruce Richardson 				       RTE_EVENT_QUEUE_ATTR_SCHEDULE_TYPE,
74699a2dd95SBruce Richardson 				       &sched_type);
74799a2dd95SBruce Richardson 
74899a2dd95SBruce Richardson 	if ((ret == 0 && evtim->ev.sched_type == sched_type) ||
74999a2dd95SBruce Richardson 	    ret == -EOVERFLOW)
75099a2dd95SBruce Richardson 		return 0;
75199a2dd95SBruce Richardson 
75299a2dd95SBruce Richardson 	return -1;
75399a2dd95SBruce Richardson }
75499a2dd95SBruce Richardson 
75599a2dd95SBruce Richardson static int
swtim_service_func(void * arg)75699a2dd95SBruce Richardson swtim_service_func(void *arg)
75799a2dd95SBruce Richardson {
75899a2dd95SBruce Richardson 	struct rte_event_timer_adapter *adapter = arg;
75999a2dd95SBruce Richardson 	struct swtim *sw = swtim_pmd_priv(adapter);
76099a2dd95SBruce Richardson 	uint16_t nb_evs_flushed = 0;
76199a2dd95SBruce Richardson 	uint16_t nb_evs_invalid = 0;
76299a2dd95SBruce Richardson 
76399a2dd95SBruce Richardson 	if (swtim_did_tick(sw)) {
76499a2dd95SBruce Richardson 		rte_timer_alt_manage(sw->timer_data_id,
76599a2dd95SBruce Richardson 				     sw->poll_lcores,
76699a2dd95SBruce Richardson 				     sw->n_poll_lcores,
76799a2dd95SBruce Richardson 				     swtim_callback);
76899a2dd95SBruce Richardson 
76999a2dd95SBruce Richardson 		/* Return expired timer objects back to mempool */
77099a2dd95SBruce Richardson 		rte_mempool_put_bulk(sw->tim_pool, (void **)sw->expired_timers,
77199a2dd95SBruce Richardson 				     sw->n_expired_timers);
77299a2dd95SBruce Richardson 		sw->n_expired_timers = 0;
77399a2dd95SBruce Richardson 
77499a2dd95SBruce Richardson 		event_buffer_flush(&sw->buffer,
77599a2dd95SBruce Richardson 				   adapter->data->event_dev_id,
77699a2dd95SBruce Richardson 				   adapter->data->event_port_id,
77799a2dd95SBruce Richardson 				   &nb_evs_flushed,
77899a2dd95SBruce Richardson 				   &nb_evs_invalid);
77999a2dd95SBruce Richardson 
78099a2dd95SBruce Richardson 		sw->stats.ev_enq_count += nb_evs_flushed;
78199a2dd95SBruce Richardson 		sw->stats.ev_inv_count += nb_evs_invalid;
78299a2dd95SBruce Richardson 		sw->stats.adapter_tick_count++;
78399a2dd95SBruce Richardson 	}
78499a2dd95SBruce Richardson 
785578402f2SMattias Rönnblom 	rte_event_maintain(adapter->data->event_dev_id,
786578402f2SMattias Rönnblom 			   adapter->data->event_port_id, 0);
787578402f2SMattias Rönnblom 
78899a2dd95SBruce Richardson 	return 0;
78999a2dd95SBruce Richardson }
79099a2dd95SBruce Richardson 
79199a2dd95SBruce Richardson /* The adapter initialization function rounds the mempool size up to the next
79299a2dd95SBruce Richardson  * power of 2, so we can take the difference between that value and what the
79399a2dd95SBruce Richardson  * user requested, and use the space for caches.  This avoids a scenario where a
79499a2dd95SBruce Richardson  * user can't arm the number of timers the adapter was configured with because
79599a2dd95SBruce Richardson  * mempool objects have been lost to caches.
79699a2dd95SBruce Richardson  *
79799a2dd95SBruce Richardson  * nb_actual should always be a power of 2, so we can iterate over the powers
79899a2dd95SBruce Richardson  * of 2 to see what the largest cache size we can use is.
79999a2dd95SBruce Richardson  */
80099a2dd95SBruce Richardson static int
compute_msg_mempool_cache_size(uint64_t nb_requested,uint64_t nb_actual)80199a2dd95SBruce Richardson compute_msg_mempool_cache_size(uint64_t nb_requested, uint64_t nb_actual)
80299a2dd95SBruce Richardson {
80399a2dd95SBruce Richardson 	int i;
80499a2dd95SBruce Richardson 	int size;
80599a2dd95SBruce Richardson 	int cache_size = 0;
80699a2dd95SBruce Richardson 
80799a2dd95SBruce Richardson 	for (i = 0;; i++) {
80899a2dd95SBruce Richardson 		size = 1 << i;
80999a2dd95SBruce Richardson 
81099a2dd95SBruce Richardson 		if (RTE_MAX_LCORE * size < (int)(nb_actual - nb_requested) &&
81199a2dd95SBruce Richardson 		    size < RTE_MEMPOOL_CACHE_MAX_SIZE &&
81299a2dd95SBruce Richardson 		    size <= nb_actual / 1.5)
81399a2dd95SBruce Richardson 			cache_size = size;
81499a2dd95SBruce Richardson 		else
81599a2dd95SBruce Richardson 			break;
81699a2dd95SBruce Richardson 	}
81799a2dd95SBruce Richardson 
81899a2dd95SBruce Richardson 	return cache_size;
81999a2dd95SBruce Richardson }
82099a2dd95SBruce Richardson 
82199a2dd95SBruce Richardson static int
swtim_init(struct rte_event_timer_adapter * adapter)82299a2dd95SBruce Richardson swtim_init(struct rte_event_timer_adapter *adapter)
82399a2dd95SBruce Richardson {
82499a2dd95SBruce Richardson 	int i, ret;
82599a2dd95SBruce Richardson 	struct swtim *sw;
82699a2dd95SBruce Richardson 	unsigned int flags;
82799a2dd95SBruce Richardson 	struct rte_service_spec service;
82899a2dd95SBruce Richardson 
82999a2dd95SBruce Richardson 	/* Allocate storage for private data area */
83099a2dd95SBruce Richardson #define SWTIM_NAMESIZE 32
83199a2dd95SBruce Richardson 	char swtim_name[SWTIM_NAMESIZE];
83299a2dd95SBruce Richardson 	snprintf(swtim_name, SWTIM_NAMESIZE, "swtim_%"PRIu8,
83399a2dd95SBruce Richardson 			adapter->data->id);
83499a2dd95SBruce Richardson 	sw = rte_zmalloc_socket(swtim_name, sizeof(*sw), RTE_CACHE_LINE_SIZE,
83599a2dd95SBruce Richardson 			adapter->data->socket_id);
83699a2dd95SBruce Richardson 	if (sw == NULL) {
83799a2dd95SBruce Richardson 		EVTIM_LOG_ERR("failed to allocate space for private data");
83899a2dd95SBruce Richardson 		rte_errno = ENOMEM;
83999a2dd95SBruce Richardson 		return -1;
84099a2dd95SBruce Richardson 	}
84199a2dd95SBruce Richardson 
84299a2dd95SBruce Richardson 	/* Connect storage to adapter instance */
84399a2dd95SBruce Richardson 	adapter->data->adapter_priv = sw;
84499a2dd95SBruce Richardson 	sw->adapter = adapter;
84599a2dd95SBruce Richardson 
84699a2dd95SBruce Richardson 	sw->timer_tick_ns = adapter->data->conf.timer_tick_ns;
84799a2dd95SBruce Richardson 	sw->max_tmo_ns = adapter->data->conf.max_tmo_ns;
84899a2dd95SBruce Richardson 
84999a2dd95SBruce Richardson 	/* Create a timer pool */
85099a2dd95SBruce Richardson 	char pool_name[SWTIM_NAMESIZE];
85199a2dd95SBruce Richardson 	snprintf(pool_name, SWTIM_NAMESIZE, "swtim_pool_%"PRIu8,
85299a2dd95SBruce Richardson 		 adapter->data->id);
85399a2dd95SBruce Richardson 	/* Optimal mempool size is a power of 2 minus one */
85499a2dd95SBruce Richardson 	uint64_t nb_timers = rte_align64pow2(adapter->data->conf.nb_timers);
85599a2dd95SBruce Richardson 	int pool_size = nb_timers - 1;
85699a2dd95SBruce Richardson 	int cache_size = compute_msg_mempool_cache_size(
85799a2dd95SBruce Richardson 				adapter->data->conf.nb_timers, nb_timers);
85899a2dd95SBruce Richardson 	flags = 0; /* pool is multi-producer, multi-consumer */
85999a2dd95SBruce Richardson 	sw->tim_pool = rte_mempool_create(pool_name, pool_size,
86099a2dd95SBruce Richardson 			sizeof(struct rte_timer), cache_size, 0, NULL, NULL,
86199a2dd95SBruce Richardson 			NULL, NULL, adapter->data->socket_id, flags);
86299a2dd95SBruce Richardson 	if (sw->tim_pool == NULL) {
86399a2dd95SBruce Richardson 		EVTIM_LOG_ERR("failed to create timer object mempool");
86499a2dd95SBruce Richardson 		rte_errno = ENOMEM;
86599a2dd95SBruce Richardson 		goto free_alloc;
86699a2dd95SBruce Richardson 	}
86799a2dd95SBruce Richardson 
86899a2dd95SBruce Richardson 	/* Initialize the variables that track in-use timer lists */
86999a2dd95SBruce Richardson 	for (i = 0; i < RTE_MAX_LCORE; i++)
87099a2dd95SBruce Richardson 		sw->in_use[i].v = 0;
87199a2dd95SBruce Richardson 
87299a2dd95SBruce Richardson 	/* Initialize the timer subsystem and allocate timer data instance */
87399a2dd95SBruce Richardson 	ret = rte_timer_subsystem_init();
87499a2dd95SBruce Richardson 	if (ret < 0) {
87599a2dd95SBruce Richardson 		if (ret != -EALREADY) {
87699a2dd95SBruce Richardson 			EVTIM_LOG_ERR("failed to initialize timer subsystem");
87799a2dd95SBruce Richardson 			rte_errno = -ret;
87899a2dd95SBruce Richardson 			goto free_mempool;
87999a2dd95SBruce Richardson 		}
88099a2dd95SBruce Richardson 	}
88199a2dd95SBruce Richardson 
88299a2dd95SBruce Richardson 	ret = rte_timer_data_alloc(&sw->timer_data_id);
88399a2dd95SBruce Richardson 	if (ret < 0) {
88499a2dd95SBruce Richardson 		EVTIM_LOG_ERR("failed to allocate timer data instance");
88599a2dd95SBruce Richardson 		rte_errno = -ret;
88699a2dd95SBruce Richardson 		goto free_mempool;
88799a2dd95SBruce Richardson 	}
88899a2dd95SBruce Richardson 
88999a2dd95SBruce Richardson 	/* Initialize timer event buffer */
89099a2dd95SBruce Richardson 	event_buffer_init(&sw->buffer);
89199a2dd95SBruce Richardson 
89299a2dd95SBruce Richardson 	sw->adapter = adapter;
89399a2dd95SBruce Richardson 
89499a2dd95SBruce Richardson 	/* Register a service component to run adapter logic */
89599a2dd95SBruce Richardson 	memset(&service, 0, sizeof(service));
89699a2dd95SBruce Richardson 	snprintf(service.name, RTE_SERVICE_NAME_MAX,
89799a2dd95SBruce Richardson 		 "swtim_svc_%"PRIu8, adapter->data->id);
89899a2dd95SBruce Richardson 	service.socket_id = adapter->data->socket_id;
89999a2dd95SBruce Richardson 	service.callback = swtim_service_func;
90099a2dd95SBruce Richardson 	service.callback_userdata = adapter;
90199a2dd95SBruce Richardson 	service.capabilities &= ~(RTE_SERVICE_CAP_MT_SAFE);
90299a2dd95SBruce Richardson 	ret = rte_service_component_register(&service, &sw->service_id);
90399a2dd95SBruce Richardson 	if (ret < 0) {
90499a2dd95SBruce Richardson 		EVTIM_LOG_ERR("failed to register service %s with id %"PRIu32
90599a2dd95SBruce Richardson 			      ": err = %d", service.name, sw->service_id,
90699a2dd95SBruce Richardson 			      ret);
90799a2dd95SBruce Richardson 
90899a2dd95SBruce Richardson 		rte_errno = ENOSPC;
90999a2dd95SBruce Richardson 		goto free_mempool;
91099a2dd95SBruce Richardson 	}
91199a2dd95SBruce Richardson 
91299a2dd95SBruce Richardson 	EVTIM_LOG_DBG("registered service %s with id %"PRIu32, service.name,
91399a2dd95SBruce Richardson 		      sw->service_id);
91499a2dd95SBruce Richardson 
91599a2dd95SBruce Richardson 	adapter->data->service_id = sw->service_id;
91699a2dd95SBruce Richardson 	adapter->data->service_inited = 1;
91799a2dd95SBruce Richardson 
91899a2dd95SBruce Richardson 	return 0;
91999a2dd95SBruce Richardson free_mempool:
92099a2dd95SBruce Richardson 	rte_mempool_free(sw->tim_pool);
92199a2dd95SBruce Richardson free_alloc:
92299a2dd95SBruce Richardson 	rte_free(sw);
92399a2dd95SBruce Richardson 	return -1;
92499a2dd95SBruce Richardson }
92599a2dd95SBruce Richardson 
92699a2dd95SBruce Richardson static void
swtim_free_tim(struct rte_timer * tim,void * arg)92799a2dd95SBruce Richardson swtim_free_tim(struct rte_timer *tim, void *arg)
92899a2dd95SBruce Richardson {
92999a2dd95SBruce Richardson 	struct swtim *sw = arg;
93099a2dd95SBruce Richardson 
93199a2dd95SBruce Richardson 	rte_mempool_put(sw->tim_pool, tim);
93299a2dd95SBruce Richardson }
93399a2dd95SBruce Richardson 
93499a2dd95SBruce Richardson /* Traverse the list of outstanding timers and put them back in the mempool
93599a2dd95SBruce Richardson  * before freeing the adapter to avoid leaking the memory.
93699a2dd95SBruce Richardson  */
93799a2dd95SBruce Richardson static int
swtim_uninit(struct rte_event_timer_adapter * adapter)93899a2dd95SBruce Richardson swtim_uninit(struct rte_event_timer_adapter *adapter)
93999a2dd95SBruce Richardson {
94099a2dd95SBruce Richardson 	int ret;
94199a2dd95SBruce Richardson 	struct swtim *sw = swtim_pmd_priv(adapter);
94299a2dd95SBruce Richardson 
94399a2dd95SBruce Richardson 	/* Free outstanding timers */
94499a2dd95SBruce Richardson 	rte_timer_stop_all(sw->timer_data_id,
94599a2dd95SBruce Richardson 			   sw->poll_lcores,
94699a2dd95SBruce Richardson 			   sw->n_poll_lcores,
94799a2dd95SBruce Richardson 			   swtim_free_tim,
94899a2dd95SBruce Richardson 			   sw);
94999a2dd95SBruce Richardson 
95099a2dd95SBruce Richardson 	ret = rte_service_component_unregister(sw->service_id);
95199a2dd95SBruce Richardson 	if (ret < 0) {
95299a2dd95SBruce Richardson 		EVTIM_LOG_ERR("failed to unregister service component");
95399a2dd95SBruce Richardson 		return ret;
95499a2dd95SBruce Richardson 	}
95599a2dd95SBruce Richardson 
95699a2dd95SBruce Richardson 	rte_mempool_free(sw->tim_pool);
95799a2dd95SBruce Richardson 	rte_free(sw);
95899a2dd95SBruce Richardson 	adapter->data->adapter_priv = NULL;
95999a2dd95SBruce Richardson 
96099a2dd95SBruce Richardson 	return 0;
96199a2dd95SBruce Richardson }
96299a2dd95SBruce Richardson 
96399a2dd95SBruce Richardson static inline int32_t
get_mapped_count_for_service(uint32_t service_id)96499a2dd95SBruce Richardson get_mapped_count_for_service(uint32_t service_id)
96599a2dd95SBruce Richardson {
96699a2dd95SBruce Richardson 	int32_t core_count, i, mapped_count = 0;
96799a2dd95SBruce Richardson 	uint32_t lcore_arr[RTE_MAX_LCORE];
96899a2dd95SBruce Richardson 
96999a2dd95SBruce Richardson 	core_count = rte_service_lcore_list(lcore_arr, RTE_MAX_LCORE);
97099a2dd95SBruce Richardson 
97199a2dd95SBruce Richardson 	for (i = 0; i < core_count; i++)
97299a2dd95SBruce Richardson 		if (rte_service_map_lcore_get(service_id, lcore_arr[i]) == 1)
97399a2dd95SBruce Richardson 			mapped_count++;
97499a2dd95SBruce Richardson 
97599a2dd95SBruce Richardson 	return mapped_count;
97699a2dd95SBruce Richardson }
97799a2dd95SBruce Richardson 
97899a2dd95SBruce Richardson static int
swtim_start(const struct rte_event_timer_adapter * adapter)97999a2dd95SBruce Richardson swtim_start(const struct rte_event_timer_adapter *adapter)
98099a2dd95SBruce Richardson {
98199a2dd95SBruce Richardson 	int mapped_count;
98299a2dd95SBruce Richardson 	struct swtim *sw = swtim_pmd_priv(adapter);
98399a2dd95SBruce Richardson 
98499a2dd95SBruce Richardson 	/* Mapping the service to more than one service core can introduce
98599a2dd95SBruce Richardson 	 * delays while one thread is waiting to acquire a lock, so only allow
98699a2dd95SBruce Richardson 	 * one core to be mapped to the service.
98799a2dd95SBruce Richardson 	 *
98899a2dd95SBruce Richardson 	 * Note: the service could be modified such that it spreads cores to
98999a2dd95SBruce Richardson 	 * poll over multiple service instances.
99099a2dd95SBruce Richardson 	 */
99199a2dd95SBruce Richardson 	mapped_count = get_mapped_count_for_service(sw->service_id);
99299a2dd95SBruce Richardson 
99399a2dd95SBruce Richardson 	if (mapped_count != 1)
99499a2dd95SBruce Richardson 		return mapped_count < 1 ? -ENOENT : -ENOTSUP;
99599a2dd95SBruce Richardson 
99699a2dd95SBruce Richardson 	return rte_service_component_runstate_set(sw->service_id, 1);
99799a2dd95SBruce Richardson }
99899a2dd95SBruce Richardson 
99999a2dd95SBruce Richardson static int
swtim_stop(const struct rte_event_timer_adapter * adapter)100099a2dd95SBruce Richardson swtim_stop(const struct rte_event_timer_adapter *adapter)
100199a2dd95SBruce Richardson {
100299a2dd95SBruce Richardson 	int ret;
100399a2dd95SBruce Richardson 	struct swtim *sw = swtim_pmd_priv(adapter);
100499a2dd95SBruce Richardson 
100599a2dd95SBruce Richardson 	ret = rte_service_component_runstate_set(sw->service_id, 0);
100699a2dd95SBruce Richardson 	if (ret < 0)
100799a2dd95SBruce Richardson 		return ret;
100899a2dd95SBruce Richardson 
100999a2dd95SBruce Richardson 	/* Wait for the service to complete its final iteration */
101099a2dd95SBruce Richardson 	while (rte_service_may_be_active(sw->service_id))
101199a2dd95SBruce Richardson 		rte_pause();
101299a2dd95SBruce Richardson 
101399a2dd95SBruce Richardson 	return 0;
101499a2dd95SBruce Richardson }
101599a2dd95SBruce Richardson 
101699a2dd95SBruce Richardson static void
swtim_get_info(const struct rte_event_timer_adapter * adapter,struct rte_event_timer_adapter_info * adapter_info)101799a2dd95SBruce Richardson swtim_get_info(const struct rte_event_timer_adapter *adapter,
101899a2dd95SBruce Richardson 		struct rte_event_timer_adapter_info *adapter_info)
101999a2dd95SBruce Richardson {
102099a2dd95SBruce Richardson 	struct swtim *sw = swtim_pmd_priv(adapter);
102199a2dd95SBruce Richardson 	adapter_info->min_resolution_ns = sw->timer_tick_ns;
102299a2dd95SBruce Richardson 	adapter_info->max_tmo_ns = sw->max_tmo_ns;
102399a2dd95SBruce Richardson }
102499a2dd95SBruce Richardson 
102599a2dd95SBruce Richardson static int
swtim_stats_get(const struct rte_event_timer_adapter * adapter,struct rte_event_timer_adapter_stats * stats)102699a2dd95SBruce Richardson swtim_stats_get(const struct rte_event_timer_adapter *adapter,
102799a2dd95SBruce Richardson 		struct rte_event_timer_adapter_stats *stats)
102899a2dd95SBruce Richardson {
102999a2dd95SBruce Richardson 	struct swtim *sw = swtim_pmd_priv(adapter);
103099a2dd95SBruce Richardson 	*stats = sw->stats; /* structure copy */
103199a2dd95SBruce Richardson 	return 0;
103299a2dd95SBruce Richardson }
103399a2dd95SBruce Richardson 
103499a2dd95SBruce Richardson static int
swtim_stats_reset(const struct rte_event_timer_adapter * adapter)103599a2dd95SBruce Richardson swtim_stats_reset(const struct rte_event_timer_adapter *adapter)
103699a2dd95SBruce Richardson {
103799a2dd95SBruce Richardson 	struct swtim *sw = swtim_pmd_priv(adapter);
103899a2dd95SBruce Richardson 	memset(&sw->stats, 0, sizeof(sw->stats));
103999a2dd95SBruce Richardson 	return 0;
104099a2dd95SBruce Richardson }
104199a2dd95SBruce Richardson 
104299a2dd95SBruce Richardson static uint16_t
__swtim_arm_burst(const struct rte_event_timer_adapter * adapter,struct rte_event_timer ** evtims,uint16_t nb_evtims)104399a2dd95SBruce Richardson __swtim_arm_burst(const struct rte_event_timer_adapter *adapter,
104499a2dd95SBruce Richardson 		struct rte_event_timer **evtims,
104599a2dd95SBruce Richardson 		uint16_t nb_evtims)
104699a2dd95SBruce Richardson {
104799a2dd95SBruce Richardson 	int i, ret;
104899a2dd95SBruce Richardson 	struct swtim *sw = swtim_pmd_priv(adapter);
104999a2dd95SBruce Richardson 	uint32_t lcore_id = rte_lcore_id();
105099a2dd95SBruce Richardson 	struct rte_timer *tim, *tims[nb_evtims];
105199a2dd95SBruce Richardson 	uint64_t cycles;
105299a2dd95SBruce Richardson 	int n_lcores;
105399a2dd95SBruce Richardson 	/* Timer list for this lcore is not in use. */
105499a2dd95SBruce Richardson 	uint16_t exp_state = 0;
105599a2dd95SBruce Richardson 	enum rte_event_timer_state n_state;
105699a2dd95SBruce Richardson 
105799a2dd95SBruce Richardson #ifdef RTE_LIBRTE_EVENTDEV_DEBUG
105899a2dd95SBruce Richardson 	/* Check that the service is running. */
105999a2dd95SBruce Richardson 	if (rte_service_runstate_get(adapter->data->service_id) != 1) {
106099a2dd95SBruce Richardson 		rte_errno = EINVAL;
106199a2dd95SBruce Richardson 		return 0;
106299a2dd95SBruce Richardson 	}
106399a2dd95SBruce Richardson #endif
106499a2dd95SBruce Richardson 
106599a2dd95SBruce Richardson 	/* Adjust lcore_id if non-EAL thread. Arbitrarily pick the timer list of
106699a2dd95SBruce Richardson 	 * the highest lcore to insert such timers into
106799a2dd95SBruce Richardson 	 */
106899a2dd95SBruce Richardson 	if (lcore_id == LCORE_ID_ANY)
106999a2dd95SBruce Richardson 		lcore_id = RTE_MAX_LCORE - 1;
107099a2dd95SBruce Richardson 
107199a2dd95SBruce Richardson 	/* If this is the first time we're arming an event timer on this lcore,
107299a2dd95SBruce Richardson 	 * mark this lcore as "in use"; this will cause the service
107399a2dd95SBruce Richardson 	 * function to process the timer list that corresponds to this lcore.
107499a2dd95SBruce Richardson 	 * The atomic compare-and-swap operation can prevent the race condition
107599a2dd95SBruce Richardson 	 * on in_use flag between multiple non-EAL threads.
107699a2dd95SBruce Richardson 	 */
107799a2dd95SBruce Richardson 	if (unlikely(__atomic_compare_exchange_n(&sw->in_use[lcore_id].v,
107899a2dd95SBruce Richardson 			&exp_state, 1, 0,
107999a2dd95SBruce Richardson 			__ATOMIC_RELAXED, __ATOMIC_RELAXED))) {
108099a2dd95SBruce Richardson 		EVTIM_LOG_DBG("Adding lcore id = %u to list of lcores to poll",
108199a2dd95SBruce Richardson 			      lcore_id);
108299a2dd95SBruce Richardson 		n_lcores = __atomic_fetch_add(&sw->n_poll_lcores, 1,
108399a2dd95SBruce Richardson 					     __ATOMIC_RELAXED);
108499a2dd95SBruce Richardson 		__atomic_store_n(&sw->poll_lcores[n_lcores], lcore_id,
108599a2dd95SBruce Richardson 				__ATOMIC_RELAXED);
108699a2dd95SBruce Richardson 	}
108799a2dd95SBruce Richardson 
108899a2dd95SBruce Richardson 	ret = rte_mempool_get_bulk(sw->tim_pool, (void **)tims,
108999a2dd95SBruce Richardson 				   nb_evtims);
109099a2dd95SBruce Richardson 	if (ret < 0) {
109199a2dd95SBruce Richardson 		rte_errno = ENOSPC;
109299a2dd95SBruce Richardson 		return 0;
109399a2dd95SBruce Richardson 	}
109499a2dd95SBruce Richardson 
109599a2dd95SBruce Richardson 	for (i = 0; i < nb_evtims; i++) {
109699a2dd95SBruce Richardson 		n_state = __atomic_load_n(&evtims[i]->state, __ATOMIC_ACQUIRE);
109799a2dd95SBruce Richardson 		if (n_state == RTE_EVENT_TIMER_ARMED) {
109899a2dd95SBruce Richardson 			rte_errno = EALREADY;
109999a2dd95SBruce Richardson 			break;
110099a2dd95SBruce Richardson 		} else if (!(n_state == RTE_EVENT_TIMER_NOT_ARMED ||
110199a2dd95SBruce Richardson 			     n_state == RTE_EVENT_TIMER_CANCELED)) {
110299a2dd95SBruce Richardson 			rte_errno = EINVAL;
110399a2dd95SBruce Richardson 			break;
110499a2dd95SBruce Richardson 		}
110599a2dd95SBruce Richardson 
110699a2dd95SBruce Richardson 		ret = check_timeout(evtims[i], adapter);
110799a2dd95SBruce Richardson 		if (unlikely(ret == -1)) {
110899a2dd95SBruce Richardson 			__atomic_store_n(&evtims[i]->state,
110999a2dd95SBruce Richardson 					RTE_EVENT_TIMER_ERROR_TOOLATE,
111099a2dd95SBruce Richardson 					__ATOMIC_RELAXED);
111199a2dd95SBruce Richardson 			rte_errno = EINVAL;
111299a2dd95SBruce Richardson 			break;
111399a2dd95SBruce Richardson 		} else if (unlikely(ret == -2)) {
111499a2dd95SBruce Richardson 			__atomic_store_n(&evtims[i]->state,
111599a2dd95SBruce Richardson 					RTE_EVENT_TIMER_ERROR_TOOEARLY,
111699a2dd95SBruce Richardson 					__ATOMIC_RELAXED);
111799a2dd95SBruce Richardson 			rte_errno = EINVAL;
111899a2dd95SBruce Richardson 			break;
111999a2dd95SBruce Richardson 		}
112099a2dd95SBruce Richardson 
112199a2dd95SBruce Richardson 		if (unlikely(check_destination_event_queue(evtims[i],
112299a2dd95SBruce Richardson 							   adapter) < 0)) {
112399a2dd95SBruce Richardson 			__atomic_store_n(&evtims[i]->state,
112499a2dd95SBruce Richardson 					RTE_EVENT_TIMER_ERROR,
112599a2dd95SBruce Richardson 					__ATOMIC_RELAXED);
112699a2dd95SBruce Richardson 			rte_errno = EINVAL;
112799a2dd95SBruce Richardson 			break;
112899a2dd95SBruce Richardson 		}
112999a2dd95SBruce Richardson 
113099a2dd95SBruce Richardson 		tim = tims[i];
113199a2dd95SBruce Richardson 		rte_timer_init(tim);
113299a2dd95SBruce Richardson 
113399a2dd95SBruce Richardson 		evtims[i]->impl_opaque[0] = (uintptr_t)tim;
113499a2dd95SBruce Richardson 		evtims[i]->impl_opaque[1] = (uintptr_t)adapter;
113599a2dd95SBruce Richardson 
113699a2dd95SBruce Richardson 		cycles = get_timeout_cycles(evtims[i], adapter);
113799a2dd95SBruce Richardson 		ret = rte_timer_alt_reset(sw->timer_data_id, tim, cycles,
113899a2dd95SBruce Richardson 					  SINGLE, lcore_id, NULL, evtims[i]);
113999a2dd95SBruce Richardson 		if (ret < 0) {
114099a2dd95SBruce Richardson 			/* tim was in RUNNING or CONFIG state */
114199a2dd95SBruce Richardson 			__atomic_store_n(&evtims[i]->state,
114299a2dd95SBruce Richardson 					RTE_EVENT_TIMER_ERROR,
114399a2dd95SBruce Richardson 					__ATOMIC_RELEASE);
114499a2dd95SBruce Richardson 			break;
114599a2dd95SBruce Richardson 		}
114699a2dd95SBruce Richardson 
114799a2dd95SBruce Richardson 		EVTIM_LOG_DBG("armed an event timer");
114899a2dd95SBruce Richardson 		/* RELEASE ordering guarantees the adapter specific value
114999a2dd95SBruce Richardson 		 * changes observed before the update of state.
115099a2dd95SBruce Richardson 		 */
115199a2dd95SBruce Richardson 		__atomic_store_n(&evtims[i]->state, RTE_EVENT_TIMER_ARMED,
115299a2dd95SBruce Richardson 				__ATOMIC_RELEASE);
115399a2dd95SBruce Richardson 	}
115499a2dd95SBruce Richardson 
115599a2dd95SBruce Richardson 	if (i < nb_evtims)
115699a2dd95SBruce Richardson 		rte_mempool_put_bulk(sw->tim_pool,
115799a2dd95SBruce Richardson 				     (void **)&tims[i], nb_evtims - i);
115899a2dd95SBruce Richardson 
115999a2dd95SBruce Richardson 	return i;
116099a2dd95SBruce Richardson }
116199a2dd95SBruce Richardson 
116299a2dd95SBruce Richardson static uint16_t
swtim_arm_burst(const struct rte_event_timer_adapter * adapter,struct rte_event_timer ** evtims,uint16_t nb_evtims)116399a2dd95SBruce Richardson swtim_arm_burst(const struct rte_event_timer_adapter *adapter,
116499a2dd95SBruce Richardson 		struct rte_event_timer **evtims,
116599a2dd95SBruce Richardson 		uint16_t nb_evtims)
116699a2dd95SBruce Richardson {
116799a2dd95SBruce Richardson 	return __swtim_arm_burst(adapter, evtims, nb_evtims);
116899a2dd95SBruce Richardson }
116999a2dd95SBruce Richardson 
117099a2dd95SBruce Richardson static uint16_t
swtim_cancel_burst(const struct rte_event_timer_adapter * adapter,struct rte_event_timer ** evtims,uint16_t nb_evtims)117199a2dd95SBruce Richardson swtim_cancel_burst(const struct rte_event_timer_adapter *adapter,
117299a2dd95SBruce Richardson 		   struct rte_event_timer **evtims,
117399a2dd95SBruce Richardson 		   uint16_t nb_evtims)
117499a2dd95SBruce Richardson {
117599a2dd95SBruce Richardson 	int i, ret;
117699a2dd95SBruce Richardson 	struct rte_timer *timp;
117799a2dd95SBruce Richardson 	uint64_t opaque;
117899a2dd95SBruce Richardson 	struct swtim *sw = swtim_pmd_priv(adapter);
117999a2dd95SBruce Richardson 	enum rte_event_timer_state n_state;
118099a2dd95SBruce Richardson 
118199a2dd95SBruce Richardson #ifdef RTE_LIBRTE_EVENTDEV_DEBUG
118299a2dd95SBruce Richardson 	/* Check that the service is running. */
118399a2dd95SBruce Richardson 	if (rte_service_runstate_get(adapter->data->service_id) != 1) {
118499a2dd95SBruce Richardson 		rte_errno = EINVAL;
118599a2dd95SBruce Richardson 		return 0;
118699a2dd95SBruce Richardson 	}
118799a2dd95SBruce Richardson #endif
118899a2dd95SBruce Richardson 
118999a2dd95SBruce Richardson 	for (i = 0; i < nb_evtims; i++) {
119099a2dd95SBruce Richardson 		/* Don't modify the event timer state in these cases */
119199a2dd95SBruce Richardson 		/* ACQUIRE ordering guarantees the access of implementation
119299a2dd95SBruce Richardson 		 * specific opaque data under the correct state.
119399a2dd95SBruce Richardson 		 */
119499a2dd95SBruce Richardson 		n_state = __atomic_load_n(&evtims[i]->state, __ATOMIC_ACQUIRE);
119599a2dd95SBruce Richardson 		if (n_state == RTE_EVENT_TIMER_CANCELED) {
119699a2dd95SBruce Richardson 			rte_errno = EALREADY;
119799a2dd95SBruce Richardson 			break;
119899a2dd95SBruce Richardson 		} else if (n_state != RTE_EVENT_TIMER_ARMED) {
119999a2dd95SBruce Richardson 			rte_errno = EINVAL;
120099a2dd95SBruce Richardson 			break;
120199a2dd95SBruce Richardson 		}
120299a2dd95SBruce Richardson 
120399a2dd95SBruce Richardson 		opaque = evtims[i]->impl_opaque[0];
120499a2dd95SBruce Richardson 		timp = (struct rte_timer *)(uintptr_t)opaque;
120599a2dd95SBruce Richardson 		RTE_ASSERT(timp != NULL);
120699a2dd95SBruce Richardson 
120799a2dd95SBruce Richardson 		ret = rte_timer_alt_stop(sw->timer_data_id, timp);
120899a2dd95SBruce Richardson 		if (ret < 0) {
120999a2dd95SBruce Richardson 			/* Timer is running or being configured */
121099a2dd95SBruce Richardson 			rte_errno = EAGAIN;
121199a2dd95SBruce Richardson 			break;
121299a2dd95SBruce Richardson 		}
121399a2dd95SBruce Richardson 
121499a2dd95SBruce Richardson 		rte_mempool_put(sw->tim_pool, (void **)timp);
121599a2dd95SBruce Richardson 
121699a2dd95SBruce Richardson 		/* The RELEASE ordering here pairs with atomic ordering
121799a2dd95SBruce Richardson 		 * to make sure the state update data observed between
121899a2dd95SBruce Richardson 		 * threads.
121999a2dd95SBruce Richardson 		 */
122099a2dd95SBruce Richardson 		__atomic_store_n(&evtims[i]->state, RTE_EVENT_TIMER_CANCELED,
122199a2dd95SBruce Richardson 				__ATOMIC_RELEASE);
122299a2dd95SBruce Richardson 	}
122399a2dd95SBruce Richardson 
122499a2dd95SBruce Richardson 	return i;
122599a2dd95SBruce Richardson }
122699a2dd95SBruce Richardson 
122799a2dd95SBruce Richardson static uint16_t
swtim_arm_tmo_tick_burst(const struct rte_event_timer_adapter * adapter,struct rte_event_timer ** evtims,uint64_t timeout_ticks,uint16_t nb_evtims)122899a2dd95SBruce Richardson swtim_arm_tmo_tick_burst(const struct rte_event_timer_adapter *adapter,
122999a2dd95SBruce Richardson 			 struct rte_event_timer **evtims,
123099a2dd95SBruce Richardson 			 uint64_t timeout_ticks,
123199a2dd95SBruce Richardson 			 uint16_t nb_evtims)
123299a2dd95SBruce Richardson {
123399a2dd95SBruce Richardson 	int i;
123499a2dd95SBruce Richardson 
123599a2dd95SBruce Richardson 	for (i = 0; i < nb_evtims; i++)
123699a2dd95SBruce Richardson 		evtims[i]->timeout_ticks = timeout_ticks;
123799a2dd95SBruce Richardson 
123899a2dd95SBruce Richardson 	return __swtim_arm_burst(adapter, evtims, nb_evtims);
123999a2dd95SBruce Richardson }
124099a2dd95SBruce Richardson 
124153548ad3SPavan Nikhilesh static const struct event_timer_adapter_ops swtim_ops = {
124299a2dd95SBruce Richardson 	.init = swtim_init,
124399a2dd95SBruce Richardson 	.uninit = swtim_uninit,
124499a2dd95SBruce Richardson 	.start = swtim_start,
124599a2dd95SBruce Richardson 	.stop = swtim_stop,
124699a2dd95SBruce Richardson 	.get_info = swtim_get_info,
124799a2dd95SBruce Richardson 	.stats_get = swtim_stats_get,
124899a2dd95SBruce Richardson 	.stats_reset = swtim_stats_reset,
124999a2dd95SBruce Richardson 	.arm_burst = swtim_arm_burst,
125099a2dd95SBruce Richardson 	.arm_tmo_tick_burst = swtim_arm_tmo_tick_burst,
125199a2dd95SBruce Richardson 	.cancel_burst = swtim_cancel_burst,
125299a2dd95SBruce Richardson };
1253*791dfec2SAnkur Dwivedi 
1254*791dfec2SAnkur Dwivedi static int
handle_ta_info(const char * cmd __rte_unused,const char * params,struct rte_tel_data * d)1255*791dfec2SAnkur Dwivedi handle_ta_info(const char *cmd __rte_unused, const char *params,
1256*791dfec2SAnkur Dwivedi 		struct rte_tel_data *d)
1257*791dfec2SAnkur Dwivedi {
1258*791dfec2SAnkur Dwivedi 	struct rte_event_timer_adapter_info adapter_info;
1259*791dfec2SAnkur Dwivedi 	struct rte_event_timer_adapter *adapter;
1260*791dfec2SAnkur Dwivedi 	uint16_t adapter_id;
1261*791dfec2SAnkur Dwivedi 	int ret;
1262*791dfec2SAnkur Dwivedi 
1263*791dfec2SAnkur Dwivedi 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
1264*791dfec2SAnkur Dwivedi 		return -1;
1265*791dfec2SAnkur Dwivedi 
1266*791dfec2SAnkur Dwivedi 	adapter_id = atoi(params);
1267*791dfec2SAnkur Dwivedi 
1268*791dfec2SAnkur Dwivedi 	if (adapter_id >= RTE_EVENT_TIMER_ADAPTER_NUM_MAX) {
1269*791dfec2SAnkur Dwivedi 		EVTIM_LOG_ERR("Invalid timer adapter id %u", adapter_id);
1270*791dfec2SAnkur Dwivedi 		return -EINVAL;
1271*791dfec2SAnkur Dwivedi 	}
1272*791dfec2SAnkur Dwivedi 
1273*791dfec2SAnkur Dwivedi 	adapter = &adapters[adapter_id];
1274*791dfec2SAnkur Dwivedi 
1275*791dfec2SAnkur Dwivedi 	ret = rte_event_timer_adapter_get_info(adapter, &adapter_info);
1276*791dfec2SAnkur Dwivedi 	if (ret < 0) {
1277*791dfec2SAnkur Dwivedi 		EVTIM_LOG_ERR("Failed to get info for timer adapter id %u", adapter_id);
1278*791dfec2SAnkur Dwivedi 		return ret;
1279*791dfec2SAnkur Dwivedi 	}
1280*791dfec2SAnkur Dwivedi 
1281*791dfec2SAnkur Dwivedi 	rte_tel_data_start_dict(d);
1282*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "timer_adapter_id", adapter_id);
1283*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "min_resolution_ns", adapter_info.min_resolution_ns);
1284*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "max_tmo_ns", adapter_info.max_tmo_ns);
1285*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "event_dev_id", adapter_info.conf.event_dev_id);
1286*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "socket_id", adapter_info.conf.socket_id);
1287*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "clk_src", adapter_info.conf.clk_src);
1288*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "timer_tick_ns", adapter_info.conf.timer_tick_ns);
1289*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "nb_timers", adapter_info.conf.nb_timers);
1290*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "flags", adapter_info.conf.flags);
1291*791dfec2SAnkur Dwivedi 
1292*791dfec2SAnkur Dwivedi 	return 0;
1293*791dfec2SAnkur Dwivedi }
1294*791dfec2SAnkur Dwivedi 
1295*791dfec2SAnkur Dwivedi static int
handle_ta_stats(const char * cmd __rte_unused,const char * params,struct rte_tel_data * d)1296*791dfec2SAnkur Dwivedi handle_ta_stats(const char *cmd __rte_unused, const char *params,
1297*791dfec2SAnkur Dwivedi 		struct rte_tel_data *d)
1298*791dfec2SAnkur Dwivedi {
1299*791dfec2SAnkur Dwivedi 	struct rte_event_timer_adapter_stats stats;
1300*791dfec2SAnkur Dwivedi 	struct rte_event_timer_adapter *adapter;
1301*791dfec2SAnkur Dwivedi 	uint16_t adapter_id;
1302*791dfec2SAnkur Dwivedi 	int ret;
1303*791dfec2SAnkur Dwivedi 
1304*791dfec2SAnkur Dwivedi 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
1305*791dfec2SAnkur Dwivedi 		return -1;
1306*791dfec2SAnkur Dwivedi 
1307*791dfec2SAnkur Dwivedi 	adapter_id = atoi(params);
1308*791dfec2SAnkur Dwivedi 
1309*791dfec2SAnkur Dwivedi 	if (adapter_id >= RTE_EVENT_TIMER_ADAPTER_NUM_MAX) {
1310*791dfec2SAnkur Dwivedi 		EVTIM_LOG_ERR("Invalid timer adapter id %u", adapter_id);
1311*791dfec2SAnkur Dwivedi 		return -EINVAL;
1312*791dfec2SAnkur Dwivedi 	}
1313*791dfec2SAnkur Dwivedi 
1314*791dfec2SAnkur Dwivedi 	adapter = &adapters[adapter_id];
1315*791dfec2SAnkur Dwivedi 
1316*791dfec2SAnkur Dwivedi 	ret = rte_event_timer_adapter_stats_get(adapter, &stats);
1317*791dfec2SAnkur Dwivedi 	if (ret < 0) {
1318*791dfec2SAnkur Dwivedi 		EVTIM_LOG_ERR("Failed to get stats for timer adapter id %u", adapter_id);
1319*791dfec2SAnkur Dwivedi 		return ret;
1320*791dfec2SAnkur Dwivedi 	}
1321*791dfec2SAnkur Dwivedi 
1322*791dfec2SAnkur Dwivedi 	rte_tel_data_start_dict(d);
1323*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "timer_adapter_id", adapter_id);
1324*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "evtim_exp_count", stats.evtim_exp_count);
1325*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "ev_enq_count", stats.ev_enq_count);
1326*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "ev_inv_count", stats.ev_inv_count);
1327*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "evtim_retry_count", stats.evtim_retry_count);
1328*791dfec2SAnkur Dwivedi 	rte_tel_data_add_dict_u64(d, "adapter_tick_count", stats.adapter_tick_count);
1329*791dfec2SAnkur Dwivedi 
1330*791dfec2SAnkur Dwivedi 	return 0;
1331*791dfec2SAnkur Dwivedi }
1332*791dfec2SAnkur Dwivedi 
RTE_INIT(ta_init_telemetry)1333*791dfec2SAnkur Dwivedi RTE_INIT(ta_init_telemetry)
1334*791dfec2SAnkur Dwivedi {
1335*791dfec2SAnkur Dwivedi 	rte_telemetry_register_cmd("/eventdev/ta_info",
1336*791dfec2SAnkur Dwivedi 		handle_ta_info,
1337*791dfec2SAnkur Dwivedi 		"Returns Timer adapter info. Parameter: Timer adapter id");
1338*791dfec2SAnkur Dwivedi 
1339*791dfec2SAnkur Dwivedi 	rte_telemetry_register_cmd("/eventdev/ta_stats",
1340*791dfec2SAnkur Dwivedi 		handle_ta_stats,
1341*791dfec2SAnkur Dwivedi 		"Returns Timer adapter stats. Parameter: Timer adapter id");
1342*791dfec2SAnkur Dwivedi }
1343