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 unsigned int dpaa_svr_family;
56 RTE_DEFINE_PER_LCORE(bool, _dpaa_io);
59 dpaa_add_to_device_list(struct rte_dpaa_device *dev)
61 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
65 dpaa_remove_from_device_list(struct rte_dpaa_device *dev)
67 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
71 * Reads the SEC device from DTS
72 * Returns -1 if SEC devices not available, 0 otherwise
75 dpaa_sec_available(void)
77 const struct device_node *caam_node;
79 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
86 static void dpaa_clean_device_list(void);
89 dpaa_create_device_list(void)
93 struct rte_dpaa_device *dev;
94 struct fm_eth_port_cfg *cfg;
95 struct fman_if *fman_intf;
97 /* Creating Ethernet Devices */
98 for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
99 dev = calloc(1, sizeof(struct rte_dpaa_device));
101 DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
106 cfg = &dpaa_netcfg->port_cfg[i];
107 fman_intf = cfg->fman_if;
109 /* Device identifiers */
110 dev->id.fman_id = fman_intf->fman_idx + 1;
111 dev->id.mac_id = fman_intf->mac_idx;
112 dev->device_type = FSL_DPAA_ETH;
115 /* Create device name */
116 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
117 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
119 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
120 dev->device.name = dev->name;
122 dpaa_add_to_device_list(dev);
125 rte_dpaa_bus.device_count = i;
127 /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
128 * constantly created only if "sec" property is found in the device
129 * tree. Logically there is no limit for number of devices (QI
130 * interfaces) that can be created.
133 if (dpaa_sec_available()) {
134 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
138 /* Creating SEC Devices */
139 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
140 dev = calloc(1, sizeof(struct rte_dpaa_device));
142 DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
147 dev->device_type = FSL_DPAA_CRYPTO;
148 dev->id.dev_id = rte_dpaa_bus.device_count + i;
150 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
151 * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
152 * allocated for dev->name/
154 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
155 sprintf(dev->name, "dpaa-sec%d", i);
156 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
158 dpaa_add_to_device_list(dev);
161 rte_dpaa_bus.device_count += i;
166 dpaa_clean_device_list();
171 dpaa_clean_device_list(void)
173 struct rte_dpaa_device *dev = NULL;
174 struct rte_dpaa_device *tdev = NULL;
176 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
177 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
183 /** XXX move this function into a separate file */
185 _dpaa_portal_init(void *arg)
189 uint32_t cpu = rte_lcore_id();
191 struct dpaa_portal *dpaa_io_portal;
193 BUS_INIT_FUNC_TRACE();
195 if ((uint64_t)arg == 1 || cpu == LCORE_ID_ANY)
196 cpu = rte_get_master_lcore();
197 /* if the core id is not supported */
199 if (cpu >= RTE_MAX_LCORE)
202 /* Set CPU affinity for this thread */
204 CPU_SET(cpu, &cpuset);
206 ret = pthread_setaffinity_np(id, sizeof(cpu_set_t), &cpuset);
208 DPAA_BUS_LOG(ERR, "pthread_setaffinity_np failed on "
209 "core :%d with ret: %d", cpu, ret);
213 /* Initialise bman thread portals */
214 ret = bman_thread_init();
216 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
217 "core %d with ret: %d", cpu, ret);
221 DPAA_BUS_LOG(DEBUG, "BMAN thread initialized");
223 /* Initialise qman thread portals */
224 ret = qman_thread_init();
226 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
227 "core %d with ret: %d", cpu, ret);
228 bman_thread_finish();
232 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
234 dpaa_io_portal = rte_malloc(NULL, sizeof(struct dpaa_portal),
235 RTE_CACHE_LINE_SIZE);
236 if (!dpaa_io_portal) {
237 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
238 bman_thread_finish();
239 qman_thread_finish();
243 dpaa_io_portal->qman_idx = qman_get_portal_index();
244 dpaa_io_portal->bman_idx = bman_get_portal_index();
245 dpaa_io_portal->tid = syscall(SYS_gettid);
247 ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
249 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on "
250 "core %d with ret: %d", cpu, ret);
251 dpaa_portal_finish(NULL);
256 RTE_PER_LCORE(_dpaa_io) = true;
258 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
264 * rte_dpaa_portal_init - Wrapper over _dpaa_portal_init with thread level check
267 int rte_dpaa_portal_init(void *arg)
269 if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
270 return _dpaa_portal_init(arg);
276 rte_dpaa_portal_fq_init(void *arg, struct qman_fq *fq)
278 /* Affine above created portal with channel*/
280 struct qman_portal *qp;
282 if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
283 _dpaa_portal_init(arg);
285 /* Initialise qman specific portals */
286 qp = fsl_qman_portal_create();
288 DPAA_BUS_LOG(ERR, "Unable to alloc fq portal");
292 sdqcr = QM_SDQCR_CHANNELS_POOL_CONV(fq->ch_id);
293 qman_static_dequeue_add(sdqcr, qp);
298 int rte_dpaa_portal_fq_close(struct qman_fq *fq)
300 return fsl_qman_portal_destroy(fq->qp);
304 dpaa_portal_finish(void *arg)
306 struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
308 if (!dpaa_io_portal) {
309 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
313 bman_thread_finish();
314 qman_thread_finish();
316 pthread_setspecific(dpaa_portal_key, NULL);
318 rte_free(dpaa_io_portal);
319 dpaa_io_portal = NULL;
321 RTE_PER_LCORE(_dpaa_io) = false;
324 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
325 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
328 rte_dpaa_bus_scan(void)
332 BUS_INIT_FUNC_TRACE();
334 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
335 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
336 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
340 /* Load the device-tree driver */
343 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
347 /* Get the interface configurations from device-tree */
348 dpaa_netcfg = netcfg_acquire();
350 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
354 RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
356 if (!dpaa_netcfg->num_ethports) {
357 DPAA_BUS_LOG(INFO, "no network interfaces available");
358 /* This is not an error */
362 DPAA_BUS_LOG(DEBUG, "Bus: Address of netcfg=%p, Ethports=%d",
363 dpaa_netcfg, dpaa_netcfg->num_ethports);
365 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
366 dump_netcfg(dpaa_netcfg);
369 DPAA_BUS_LOG(DEBUG, "Number of devices = %d\n",
370 dpaa_netcfg->num_ethports);
371 ret = dpaa_create_device_list();
373 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
377 /* create the key, supplying a function that'll be invoked
378 * when a portal affined thread will be deleted.
380 ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
382 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
383 dpaa_clean_device_list();
387 DPAA_BUS_LOG(DEBUG, "dpaa_portal_key=%u, ret=%d\n",
388 (unsigned int)dpaa_portal_key, ret);
393 /* register a dpaa bus based dpaa driver */
395 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
399 BUS_INIT_FUNC_TRACE();
401 TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
402 /* Update Bus references */
403 driver->dpaa_bus = &rte_dpaa_bus;
406 /* un-register a dpaa bus based dpaa driver */
408 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
410 struct rte_dpaa_bus *dpaa_bus;
412 BUS_INIT_FUNC_TRACE();
414 dpaa_bus = driver->dpaa_bus;
416 TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
417 /* Update Bus references */
418 driver->dpaa_bus = NULL;
422 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
423 struct rte_dpaa_device *dev)
427 BUS_INIT_FUNC_TRACE();
430 DPAA_BUS_DEBUG("Invalid drv or dev received.");
434 if (drv->drv_type == dev->device_type) {
435 DPAA_BUS_INFO("Device: %s matches for driver: %s",
436 dev->name, drv->driver.name);
437 ret = 0; /* Found a match */
444 rte_dpaa_bus_probe(void)
447 struct rte_dpaa_device *dev;
448 struct rte_dpaa_driver *drv;
449 FILE *svr_file = NULL;
450 unsigned int svr_ver;
452 BUS_INIT_FUNC_TRACE();
454 /* For each registered driver, and device, call the driver->probe */
455 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
456 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
457 ret = rte_dpaa_device_match(drv, dev);
464 ret = drv->probe(drv, dev);
466 DPAA_BUS_ERR("Unable to probe.\n");
471 svr_file = fopen(DPAA_SOC_ID_FILE, "r");
473 if (fscanf(svr_file, "svr:%x", &svr_ver) > 0)
474 dpaa_svr_family = svr_ver & SVR_MASK;
481 static struct rte_device *
482 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
485 struct rte_dpaa_device *dev;
487 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
488 if (start && &dev->device == start) {
489 start = NULL; /* starting point found */
493 if (cmp(&dev->device, data) == 0)
501 * Get iommu class of DPAA2 devices on the bus.
503 static enum rte_iova_mode
504 rte_dpaa_get_iommu_class(void)
509 struct rte_dpaa_bus rte_dpaa_bus = {
511 .scan = rte_dpaa_bus_scan,
512 .probe = rte_dpaa_bus_probe,
513 .find_device = rte_dpaa_find_device,
514 .get_iommu_class = rte_dpaa_get_iommu_class,
516 .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
517 .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
521 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
523 RTE_INIT(dpaa_init_log);
527 dpaa_logtype_bus = rte_log_register("bus.dpaa");
528 if (dpaa_logtype_bus >= 0)
529 rte_log_set_level(dpaa_logtype_bus, RTE_LOG_NOTICE);
531 dpaa_logtype_mempool = rte_log_register("mempool.dpaa");
532 if (dpaa_logtype_mempool >= 0)
533 rte_log_set_level(dpaa_logtype_mempool, RTE_LOG_NOTICE);
535 dpaa_logtype_pmd = rte_log_register("pmd.dpaa");
536 if (dpaa_logtype_pmd >= 0)
537 rte_log_set_level(dpaa_logtype_pmd, RTE_LOG_NOTICE);