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_driver.h>
31 #include <rte_malloc.h>
34 #include <rte_mbuf_pool_ops.h>
36 #include <rte_dpaa_bus.h>
37 #include <rte_dpaa_logs.h>
46 int dpaa_logtype_mempool;
48 int dpaa_logtype_eventdev;
50 struct rte_dpaa_bus rte_dpaa_bus;
51 struct netcfg_info *dpaa_netcfg;
53 /* define a variable to hold the portal_key, once created.*/
54 pthread_key_t dpaa_portal_key;
56 unsigned int dpaa_svr_family;
58 RTE_DEFINE_PER_LCORE(bool, _dpaa_io);
59 RTE_DEFINE_PER_LCORE(struct dpaa_portal_dqrr, held_bufs);
62 dpaa_add_to_device_list(struct rte_dpaa_device *dev)
64 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
68 dpaa_remove_from_device_list(struct rte_dpaa_device *dev)
70 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
74 * Reads the SEC device from DTS
75 * Returns -1 if SEC devices not available, 0 otherwise
78 dpaa_sec_available(void)
80 const struct device_node *caam_node;
82 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
89 static void dpaa_clean_device_list(void);
92 dpaa_create_device_list(void)
96 struct rte_dpaa_device *dev;
97 struct fm_eth_port_cfg *cfg;
98 struct fman_if *fman_intf;
100 /* Creating Ethernet Devices */
101 for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
102 dev = calloc(1, sizeof(struct rte_dpaa_device));
104 DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
109 cfg = &dpaa_netcfg->port_cfg[i];
110 fman_intf = cfg->fman_if;
112 /* Device identifiers */
113 dev->id.fman_id = fman_intf->fman_idx + 1;
114 dev->id.mac_id = fman_intf->mac_idx;
115 dev->device_type = FSL_DPAA_ETH;
118 /* Create device name */
119 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
120 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
122 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
123 dev->device.name = dev->name;
125 dpaa_add_to_device_list(dev);
128 rte_dpaa_bus.device_count = i;
130 /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
131 * constantly created only if "sec" property is found in the device
132 * tree. Logically there is no limit for number of devices (QI
133 * interfaces) that can be created.
136 if (dpaa_sec_available()) {
137 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
141 /* Creating SEC Devices */
142 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
143 dev = calloc(1, sizeof(struct rte_dpaa_device));
145 DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
150 dev->device_type = FSL_DPAA_CRYPTO;
151 dev->id.dev_id = rte_dpaa_bus.device_count + i;
153 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
154 * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
155 * allocated for dev->name/
157 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
158 sprintf(dev->name, "dpaa-sec%d", i);
159 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
161 dpaa_add_to_device_list(dev);
164 rte_dpaa_bus.device_count += i;
169 dpaa_clean_device_list();
174 dpaa_clean_device_list(void)
176 struct rte_dpaa_device *dev = NULL;
177 struct rte_dpaa_device *tdev = NULL;
179 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
180 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
186 /** XXX move this function into a separate file */
188 _dpaa_portal_init(void *arg)
192 uint32_t cpu = rte_lcore_id();
194 struct dpaa_portal *dpaa_io_portal;
196 BUS_INIT_FUNC_TRACE();
198 if ((uint64_t)arg == 1 || cpu == LCORE_ID_ANY)
199 cpu = rte_get_master_lcore();
200 /* if the core id is not supported */
202 if (cpu >= RTE_MAX_LCORE)
205 /* Set CPU affinity for this thread */
207 CPU_SET(cpu, &cpuset);
209 ret = pthread_setaffinity_np(id, sizeof(cpu_set_t), &cpuset);
211 DPAA_BUS_LOG(ERR, "pthread_setaffinity_np failed on "
212 "core :%d with ret: %d", cpu, ret);
216 /* Initialise bman thread portals */
217 ret = bman_thread_init();
219 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
220 "core %d with ret: %d", cpu, ret);
224 DPAA_BUS_LOG(DEBUG, "BMAN thread initialized");
226 /* Initialise qman thread portals */
227 ret = qman_thread_init();
229 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
230 "core %d with ret: %d", cpu, ret);
231 bman_thread_finish();
235 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
237 dpaa_io_portal = rte_malloc(NULL, sizeof(struct dpaa_portal),
238 RTE_CACHE_LINE_SIZE);
239 if (!dpaa_io_portal) {
240 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
241 bman_thread_finish();
242 qman_thread_finish();
246 dpaa_io_portal->qman_idx = qman_get_portal_index();
247 dpaa_io_portal->bman_idx = bman_get_portal_index();
248 dpaa_io_portal->tid = syscall(SYS_gettid);
250 ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
252 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on "
253 "core %d with ret: %d", cpu, ret);
254 dpaa_portal_finish(NULL);
259 RTE_PER_LCORE(_dpaa_io) = true;
261 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
267 * rte_dpaa_portal_init - Wrapper over _dpaa_portal_init with thread level check
270 int rte_dpaa_portal_init(void *arg)
272 if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
273 return _dpaa_portal_init(arg);
279 rte_dpaa_portal_fq_init(void *arg, struct qman_fq *fq)
281 /* Affine above created portal with channel*/
283 struct qman_portal *qp;
285 if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
286 _dpaa_portal_init(arg);
288 /* Initialise qman specific portals */
289 qp = fsl_qman_portal_create();
291 DPAA_BUS_LOG(ERR, "Unable to alloc fq portal");
295 sdqcr = QM_SDQCR_CHANNELS_POOL_CONV(fq->ch_id);
296 qman_static_dequeue_add(sdqcr, qp);
301 int rte_dpaa_portal_fq_close(struct qman_fq *fq)
303 return fsl_qman_portal_destroy(fq->qp);
307 dpaa_portal_finish(void *arg)
309 struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
311 if (!dpaa_io_portal) {
312 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
316 bman_thread_finish();
317 qman_thread_finish();
319 pthread_setspecific(dpaa_portal_key, NULL);
321 rte_free(dpaa_io_portal);
322 dpaa_io_portal = NULL;
324 RTE_PER_LCORE(_dpaa_io) = false;
327 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
328 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
331 rte_dpaa_bus_scan(void)
335 BUS_INIT_FUNC_TRACE();
337 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
338 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
339 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
343 /* Load the device-tree driver */
346 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
350 /* Get the interface configurations from device-tree */
351 dpaa_netcfg = netcfg_acquire();
353 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
357 RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
359 if (!dpaa_netcfg->num_ethports) {
360 DPAA_BUS_LOG(INFO, "no network interfaces available");
361 /* This is not an error */
365 DPAA_BUS_LOG(DEBUG, "Bus: Address of netcfg=%p, Ethports=%d",
366 dpaa_netcfg, dpaa_netcfg->num_ethports);
368 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
369 dump_netcfg(dpaa_netcfg);
372 DPAA_BUS_LOG(DEBUG, "Number of devices = %d\n",
373 dpaa_netcfg->num_ethports);
374 ret = dpaa_create_device_list();
376 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
380 /* create the key, supplying a function that'll be invoked
381 * when a portal affined thread will be deleted.
383 ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
385 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
386 dpaa_clean_device_list();
390 DPAA_BUS_LOG(DEBUG, "dpaa_portal_key=%u, ret=%d\n",
391 (unsigned int)dpaa_portal_key, ret);
396 /* register a dpaa bus based dpaa driver */
398 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
402 BUS_INIT_FUNC_TRACE();
404 TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
405 /* Update Bus references */
406 driver->dpaa_bus = &rte_dpaa_bus;
409 /* un-register a dpaa bus based dpaa driver */
411 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
413 struct rte_dpaa_bus *dpaa_bus;
415 BUS_INIT_FUNC_TRACE();
417 dpaa_bus = driver->dpaa_bus;
419 TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
420 /* Update Bus references */
421 driver->dpaa_bus = NULL;
425 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
426 struct rte_dpaa_device *dev)
430 BUS_INIT_FUNC_TRACE();
433 DPAA_BUS_DEBUG("Invalid drv or dev received.");
437 if (drv->drv_type == dev->device_type) {
438 DPAA_BUS_INFO("Device: %s matches for driver: %s",
439 dev->name, drv->driver.name);
440 ret = 0; /* Found a match */
447 rte_dpaa_bus_probe(void)
450 struct rte_dpaa_device *dev;
451 struct rte_dpaa_driver *drv;
452 FILE *svr_file = NULL;
453 unsigned int svr_ver;
455 BUS_INIT_FUNC_TRACE();
457 /* For each registered driver, and device, call the driver->probe */
458 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
459 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
460 ret = rte_dpaa_device_match(drv, dev);
467 ret = drv->probe(drv, dev);
469 DPAA_BUS_ERR("Unable to probe.\n");
473 rte_mbuf_set_platform_mempool_ops(DPAA_MEMPOOL_OPS_NAME);
475 svr_file = fopen(DPAA_SOC_ID_FILE, "r");
477 if (fscanf(svr_file, "svr:%x", &svr_ver) > 0)
478 dpaa_svr_family = svr_ver & SVR_MASK;
485 static struct rte_device *
486 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
489 struct rte_dpaa_device *dev;
491 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
492 if (start && &dev->device == start) {
493 start = NULL; /* starting point found */
497 if (cmp(&dev->device, data) == 0)
505 * Get iommu class of DPAA2 devices on the bus.
507 static enum rte_iova_mode
508 rte_dpaa_get_iommu_class(void)
513 struct rte_dpaa_bus rte_dpaa_bus = {
515 .scan = rte_dpaa_bus_scan,
516 .probe = rte_dpaa_bus_probe,
517 .find_device = rte_dpaa_find_device,
518 .get_iommu_class = rte_dpaa_get_iommu_class,
520 .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
521 .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
525 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
527 RTE_INIT(dpaa_init_log);
531 dpaa_logtype_bus = rte_log_register("bus.dpaa");
532 if (dpaa_logtype_bus >= 0)
533 rte_log_set_level(dpaa_logtype_bus, RTE_LOG_NOTICE);
535 dpaa_logtype_mempool = rte_log_register("mempool.dpaa");
536 if (dpaa_logtype_mempool >= 0)
537 rte_log_set_level(dpaa_logtype_mempool, RTE_LOG_NOTICE);
539 dpaa_logtype_pmd = rte_log_register("pmd.dpaa");
540 if (dpaa_logtype_pmd >= 0)
541 rte_log_set_level(dpaa_logtype_pmd, RTE_LOG_NOTICE);
543 dpaa_logtype_eventdev = rte_log_register("eventdev.dpaa");
544 if (dpaa_logtype_eventdev >= 0)
545 rte_log_set_level(dpaa_logtype_eventdev, RTE_LOG_NOTICE);