1 /* SPDX-License-Identifier: BSD-3-Clause
14 #include <sys/types.h>
15 #include <sys/syscall.h>
17 #include <rte_byteorder.h>
18 #include <rte_common.h>
19 #include <rte_interrupts.h>
21 #include <rte_debug.h>
23 #include <rte_atomic.h>
24 #include <rte_branch_prediction.h>
25 #include <rte_memory.h>
26 #include <rte_tailq.h>
28 #include <rte_alarm.h>
29 #include <rte_ether.h>
30 #include <rte_ethdev.h>
31 #include <rte_malloc.h>
35 #include <rte_dpaa_bus.h>
36 #include <rte_dpaa_logs.h>
45 int dpaa_logtype_mempool;
48 struct rte_dpaa_bus rte_dpaa_bus;
49 struct netcfg_info *dpaa_netcfg;
51 /* define a variable to hold the portal_key, once created.*/
52 pthread_key_t dpaa_portal_key;
54 RTE_DEFINE_PER_LCORE(bool, _dpaa_io);
57 dpaa_add_to_device_list(struct rte_dpaa_device *dev)
59 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
63 dpaa_remove_from_device_list(struct rte_dpaa_device *dev)
65 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
69 * Reads the SEC device from DTS
70 * Returns -1 if SEC devices not available, 0 otherwise
73 dpaa_sec_available(void)
75 const struct device_node *caam_node;
77 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
84 static void dpaa_clean_device_list(void);
87 dpaa_create_device_list(void)
91 struct rte_dpaa_device *dev;
92 struct fm_eth_port_cfg *cfg;
93 struct fman_if *fman_intf;
95 /* Creating Ethernet Devices */
96 for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
97 dev = calloc(1, sizeof(struct rte_dpaa_device));
99 DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
104 cfg = &dpaa_netcfg->port_cfg[i];
105 fman_intf = cfg->fman_if;
107 /* Device identifiers */
108 dev->id.fman_id = fman_intf->fman_idx + 1;
109 dev->id.mac_id = fman_intf->mac_idx;
110 dev->device_type = FSL_DPAA_ETH;
113 /* Create device name */
114 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
115 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
117 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
118 dev->device.name = dev->name;
120 dpaa_add_to_device_list(dev);
123 rte_dpaa_bus.device_count = i;
125 /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
126 * constantly created only if "sec" property is found in the device
127 * tree. Logically there is no limit for number of devices (QI
128 * interfaces) that can be created.
131 if (dpaa_sec_available()) {
132 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
136 /* Creating SEC Devices */
137 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
138 dev = calloc(1, sizeof(struct rte_dpaa_device));
140 DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
145 dev->device_type = FSL_DPAA_CRYPTO;
146 dev->id.dev_id = rte_dpaa_bus.device_count + i;
148 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
149 * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
150 * allocated for dev->name/
152 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
153 sprintf(dev->name, "dpaa-sec%d", i);
154 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
156 dpaa_add_to_device_list(dev);
159 rte_dpaa_bus.device_count += i;
164 dpaa_clean_device_list();
169 dpaa_clean_device_list(void)
171 struct rte_dpaa_device *dev = NULL;
172 struct rte_dpaa_device *tdev = NULL;
174 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
175 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
181 /** XXX move this function into a separate file */
183 _dpaa_portal_init(void *arg)
187 uint32_t cpu = rte_lcore_id();
189 struct dpaa_portal *dpaa_io_portal;
191 BUS_INIT_FUNC_TRACE();
193 if ((uint64_t)arg == 1 || cpu == LCORE_ID_ANY)
194 cpu = rte_get_master_lcore();
195 /* if the core id is not supported */
197 if (cpu >= RTE_MAX_LCORE)
200 /* Set CPU affinity for this thread */
202 CPU_SET(cpu, &cpuset);
204 ret = pthread_setaffinity_np(id, sizeof(cpu_set_t), &cpuset);
206 DPAA_BUS_LOG(ERR, "pthread_setaffinity_np failed on "
207 "core :%d with ret: %d", cpu, ret);
211 /* Initialise bman thread portals */
212 ret = bman_thread_init();
214 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
215 "core %d with ret: %d", cpu, ret);
219 DPAA_BUS_LOG(DEBUG, "BMAN thread initialized");
221 /* Initialise qman thread portals */
222 ret = qman_thread_init();
224 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
225 "core %d with ret: %d", cpu, ret);
226 bman_thread_finish();
230 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
232 dpaa_io_portal = rte_malloc(NULL, sizeof(struct dpaa_portal),
233 RTE_CACHE_LINE_SIZE);
234 if (!dpaa_io_portal) {
235 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
236 bman_thread_finish();
237 qman_thread_finish();
241 dpaa_io_portal->qman_idx = qman_get_portal_index();
242 dpaa_io_portal->bman_idx = bman_get_portal_index();
243 dpaa_io_portal->tid = syscall(SYS_gettid);
245 ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
247 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on "
248 "core %d with ret: %d", cpu, ret);
249 dpaa_portal_finish(NULL);
254 RTE_PER_LCORE(_dpaa_io) = true;
256 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
262 * rte_dpaa_portal_init - Wrapper over _dpaa_portal_init with thread level check
266 rte_dpaa_portal_init(void *arg)
268 if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
269 return _dpaa_portal_init(arg);
275 dpaa_portal_finish(void *arg)
277 struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
279 if (!dpaa_io_portal) {
280 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
284 bman_thread_finish();
285 qman_thread_finish();
287 pthread_setspecific(dpaa_portal_key, NULL);
289 rte_free(dpaa_io_portal);
290 dpaa_io_portal = NULL;
292 RTE_PER_LCORE(_dpaa_io) = false;
295 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
296 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
299 rte_dpaa_bus_scan(void)
303 BUS_INIT_FUNC_TRACE();
305 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
306 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
307 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
311 /* Load the device-tree driver */
314 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
318 /* Get the interface configurations from device-tree */
319 dpaa_netcfg = netcfg_acquire();
321 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
325 RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
327 if (!dpaa_netcfg->num_ethports) {
328 DPAA_BUS_LOG(INFO, "no network interfaces available");
329 /* This is not an error */
333 DPAA_BUS_LOG(DEBUG, "Bus: Address of netcfg=%p, Ethports=%d",
334 dpaa_netcfg, dpaa_netcfg->num_ethports);
336 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
337 dump_netcfg(dpaa_netcfg);
340 DPAA_BUS_LOG(DEBUG, "Number of devices = %d\n",
341 dpaa_netcfg->num_ethports);
342 ret = dpaa_create_device_list();
344 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
348 /* create the key, supplying a function that'll be invoked
349 * when a portal affined thread will be deleted.
351 ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
353 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
354 dpaa_clean_device_list();
358 DPAA_BUS_LOG(DEBUG, "dpaa_portal_key=%u, ret=%d\n",
359 (unsigned int)dpaa_portal_key, ret);
364 /* register a dpaa bus based dpaa driver */
366 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
370 BUS_INIT_FUNC_TRACE();
372 TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
373 /* Update Bus references */
374 driver->dpaa_bus = &rte_dpaa_bus;
377 /* un-register a dpaa bus based dpaa driver */
379 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
381 struct rte_dpaa_bus *dpaa_bus;
383 BUS_INIT_FUNC_TRACE();
385 dpaa_bus = driver->dpaa_bus;
387 TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
388 /* Update Bus references */
389 driver->dpaa_bus = NULL;
393 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
394 struct rte_dpaa_device *dev)
398 BUS_INIT_FUNC_TRACE();
401 DPAA_BUS_DEBUG("Invalid drv or dev received.");
405 if (drv->drv_type == dev->device_type) {
406 DPAA_BUS_INFO("Device: %s matches for driver: %s",
407 dev->name, drv->driver.name);
408 ret = 0; /* Found a match */
415 rte_dpaa_bus_probe(void)
418 struct rte_dpaa_device *dev;
419 struct rte_dpaa_driver *drv;
421 BUS_INIT_FUNC_TRACE();
423 /* For each registered driver, and device, call the driver->probe */
424 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
425 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
426 ret = rte_dpaa_device_match(drv, dev);
433 ret = drv->probe(drv, dev);
435 DPAA_BUS_ERR("Unable to probe.\n");
442 static struct rte_device *
443 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
446 struct rte_dpaa_device *dev;
448 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
449 if (start && &dev->device == start) {
450 start = NULL; /* starting point found */
454 if (cmp(&dev->device, data) == 0)
462 * Get iommu class of DPAA2 devices on the bus.
464 static enum rte_iova_mode
465 rte_dpaa_get_iommu_class(void)
470 struct rte_dpaa_bus rte_dpaa_bus = {
472 .scan = rte_dpaa_bus_scan,
473 .probe = rte_dpaa_bus_probe,
474 .find_device = rte_dpaa_find_device,
475 .get_iommu_class = rte_dpaa_get_iommu_class,
477 .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
478 .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
482 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
484 RTE_INIT(dpaa_init_log);
488 dpaa_logtype_bus = rte_log_register("bus.dpaa");
489 if (dpaa_logtype_bus >= 0)
490 rte_log_set_level(dpaa_logtype_bus, RTE_LOG_NOTICE);
492 dpaa_logtype_mempool = rte_log_register("mempool.dpaa");
493 if (dpaa_logtype_mempool >= 0)
494 rte_log_set_level(dpaa_logtype_mempool, RTE_LOG_NOTICE);
496 dpaa_logtype_pmd = rte_log_register("pmd.dpaa");
497 if (dpaa_logtype_pmd >= 0)
498 rte_log_set_level(dpaa_logtype_pmd, RTE_LOG_NOTICE);