1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2015-2016 Freescale Semiconductor, Inc. All rights reserved.
10 #include <sys/types.h>
15 #include <sys/ioctl.h>
21 #include <sys/eventfd.h>
23 #include <eal_filesystem.h>
25 #include <rte_ethdev_driver.h>
26 #include <rte_malloc.h>
27 #include <rte_memcpy.h>
28 #include <rte_string_fns.h>
29 #include <rte_cycles.h>
30 #include <rte_kvargs.h>
34 #include "rte_fslmc.h"
35 #include "fslmc_vfio.h"
36 #include "fslmc_logs.h"
37 #include <mc/fsl_dpmng.h>
39 #include "portal/dpaa2_hw_pvt.h"
40 #include "portal/dpaa2_hw_dpio.h"
42 /** Pathname of FSL-MC devices directory. */
43 #define SYSFS_FSL_MC_DEVICES "/sys/bus/fsl-mc/devices"
45 #define FSLMC_CONTAINER_MAX_LEN 8 /**< Of the format dprc.XX */
47 /* Number of VFIO containers & groups with in */
48 static struct fslmc_vfio_group vfio_group;
49 static struct fslmc_vfio_container vfio_container;
50 static int container_device_fd;
51 static char *g_container;
52 static uint32_t *msi_intr_vaddr;
53 void *(*rte_mcp_ptr_list);
55 static struct rte_dpaa2_object_list dpaa2_obj_list =
56 TAILQ_HEAD_INITIALIZER(dpaa2_obj_list);
58 /*register a fslmc bus based dpaa2 driver */
60 rte_fslmc_object_register(struct rte_dpaa2_object *object)
64 TAILQ_INSERT_TAIL(&dpaa2_obj_list, object, next);
68 fslmc_get_container_group(int *groupid)
74 container = getenv("DPRC");
75 if (container == NULL) {
76 DPAA2_BUS_INFO("DPAA2: DPRC not available");
80 if (strlen(container) >= FSLMC_CONTAINER_MAX_LEN) {
81 DPAA2_BUS_ERR("Invalid container name: %s", container);
85 g_container = strdup(container);
87 DPAA2_BUS_ERR("Mem alloc failure; Container name");
92 /* get group number */
93 ret = vfio_get_group_no(SYSFS_FSL_MC_DEVICES, g_container, groupid);
95 DPAA2_BUS_ERR("Unable to find %s IOMMU group", g_container);
99 DPAA2_BUS_DEBUG("Container: %s has VFIO iommu group id = %d",
100 g_container, *groupid);
106 vfio_connect_container(void)
110 if (vfio_container.used) {
111 DPAA2_BUS_DEBUG("No container available");
115 /* Try connecting to vfio container if already created */
116 if (!ioctl(vfio_group.fd, VFIO_GROUP_SET_CONTAINER,
117 &vfio_container.fd)) {
119 "Container pre-exists with FD[0x%x] for this group",
121 vfio_group.container = &vfio_container;
125 /* Opens main vfio file descriptor which represents the "container" */
126 fd = vfio_get_container_fd();
128 DPAA2_BUS_ERR("Failed to open VFIO container");
132 /* Check whether support for SMMU type IOMMU present or not */
133 if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
134 /* Connect group to container */
135 ret = ioctl(vfio_group.fd, VFIO_GROUP_SET_CONTAINER, &fd);
137 DPAA2_BUS_ERR("Failed to setup group container");
142 ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
144 DPAA2_BUS_ERR("Failed to setup VFIO iommu");
149 DPAA2_BUS_ERR("No supported IOMMU available");
154 vfio_container.used = 1;
155 vfio_container.fd = fd;
156 vfio_container.group = &vfio_group;
157 vfio_group.container = &vfio_container;
162 static int vfio_map_irq_region(struct fslmc_vfio_group *group)
165 unsigned long *vaddr = NULL;
166 struct vfio_iommu_type1_dma_map map = {
167 .argsz = sizeof(map),
168 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
174 vaddr = (unsigned long *)mmap(NULL, 0x1000, PROT_WRITE |
175 PROT_READ, MAP_SHARED, container_device_fd, 0x6030000);
176 if (vaddr == MAP_FAILED) {
177 DPAA2_BUS_ERR("Unable to map region (errno = %d)", errno);
181 msi_intr_vaddr = (uint32_t *)((char *)(vaddr) + 64);
182 map.vaddr = (unsigned long)vaddr;
183 ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA, &map);
187 DPAA2_BUS_ERR("Unable to map DMA address (errno = %d)", errno);
192 fslmc_vfio_map(const struct rte_memseg_list *msl __rte_unused,
193 const struct rte_memseg *ms, void *arg)
196 struct fslmc_vfio_group *group;
197 struct vfio_iommu_type1_dma_map dma_map = {
198 .argsz = sizeof(struct vfio_iommu_type1_dma_map),
199 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
203 dma_map.size = ms->len;
204 dma_map.vaddr = ms->addr_64;
205 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
206 dma_map.iova = ms->iova;
208 dma_map.iova = dma_map.vaddr;
211 /* SET DMA MAP for IOMMU */
214 if (!group->container) {
215 DPAA2_BUS_ERR("Container is not connected ");
219 DPAA2_BUS_DEBUG("-->Initial SHM Virtual ADDR %llX",
221 DPAA2_BUS_DEBUG("-----> DMA size 0x%llX", dma_map.size);
222 ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA,
225 DPAA2_BUS_ERR("VFIO_IOMMU_MAP_DMA API(errno = %d)",
233 int rte_fslmc_vfio_dmamap(void)
237 if (rte_memseg_walk(fslmc_vfio_map, &i) < 0)
240 /* Verifying that at least single segment is available */
242 DPAA2_BUS_ERR("No Segments found for VFIO Mapping");
245 DPAA2_BUS_DEBUG("Total %d segments found.", i);
247 /* TODO - This is a W.A. as VFIO currently does not add the mapping of
248 * the interrupt region to SMMU. This should be removed once the
249 * support is added in the Kernel.
251 vfio_map_irq_region(&vfio_group);
256 static int64_t vfio_map_mcp_obj(struct fslmc_vfio_group *group, char *mcp_obj)
258 intptr_t v_addr = (intptr_t)MAP_FAILED;
261 struct vfio_device_info d_info = { .argsz = sizeof(d_info) };
262 struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
264 /* getting the mcp object's fd*/
265 mc_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, mcp_obj);
267 DPAA2_BUS_ERR("Error in VFIO get dev %s fd from group %d",
272 /* getting device info*/
273 ret = ioctl(mc_fd, VFIO_DEVICE_GET_INFO, &d_info);
275 DPAA2_BUS_ERR("Error in VFIO getting DEVICE_INFO");
279 /* getting device region info*/
280 ret = ioctl(mc_fd, VFIO_DEVICE_GET_REGION_INFO, ®_info);
282 DPAA2_BUS_ERR("Error in VFIO getting REGION_INFO");
286 DPAA2_BUS_DEBUG("Region offset = %llx , region size = %llx",
287 reg_info.offset, reg_info.size);
289 v_addr = (size_t)mmap(NULL, reg_info.size,
290 PROT_WRITE | PROT_READ, MAP_SHARED,
291 mc_fd, reg_info.offset);
299 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
301 int rte_dpaa2_intr_enable(struct rte_intr_handle *intr_handle, int index)
304 char irq_set_buf[IRQ_SET_BUF_LEN];
305 struct vfio_irq_set *irq_set;
308 len = sizeof(irq_set_buf);
310 irq_set = (struct vfio_irq_set *)irq_set_buf;
311 irq_set->argsz = len;
314 VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
315 irq_set->index = index;
317 fd_ptr = (int *)&irq_set->data;
318 *fd_ptr = intr_handle->fd;
320 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
322 DPAA2_BUS_ERR("Error:dpaa2 SET IRQs fd=%d, err = %d(%s)",
323 intr_handle->fd, errno, strerror(errno));
330 int rte_dpaa2_intr_disable(struct rte_intr_handle *intr_handle, int index)
332 struct vfio_irq_set *irq_set;
333 char irq_set_buf[IRQ_SET_BUF_LEN];
336 len = sizeof(struct vfio_irq_set);
338 irq_set = (struct vfio_irq_set *)irq_set_buf;
339 irq_set->argsz = len;
340 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
341 irq_set->index = index;
345 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
348 "Error disabling dpaa2 interrupts for fd %d",
354 /* set up interrupt support (but not enable interrupts) */
356 rte_dpaa2_vfio_setup_intr(struct rte_intr_handle *intr_handle,
362 /* start from MSI-X interrupt type */
363 for (i = 0; i < num_irqs; i++) {
364 struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info) };
369 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info);
371 DPAA2_BUS_ERR("Cannot get IRQ(%d) info, error %i (%s)",
372 i, errno, strerror(errno));
376 /* if this vector cannot be used with eventfd,
377 * fail if we explicitly
378 * specified interrupt type, otherwise continue
380 if ((irq_info.flags & VFIO_IRQ_INFO_EVENTFD) == 0)
383 /* set up an eventfd for interrupts */
384 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
386 DPAA2_BUS_ERR("Cannot set up eventfd, error %i (%s)",
387 errno, strerror(errno));
391 intr_handle->fd = fd;
392 intr_handle->type = RTE_INTR_HANDLE_VFIO_MSI;
393 intr_handle->vfio_dev_fd = vfio_dev_fd;
398 /* if we're here, we haven't found a suitable interrupt vector */
403 * fslmc_process_iodevices for processing only IO (ETH, CRYPTO, and possibly
407 fslmc_process_iodevices(struct rte_dpaa2_device *dev)
410 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
411 struct rte_dpaa2_object *object = NULL;
413 dev_fd = ioctl(vfio_group.fd, VFIO_GROUP_GET_DEVICE_FD,
416 DPAA2_BUS_ERR("Unable to obtain device FD for device:%s",
421 if (ioctl(dev_fd, VFIO_DEVICE_GET_INFO, &device_info)) {
422 DPAA2_BUS_ERR("Unable to obtain information for device:%s",
427 switch (dev->dev_type) {
429 rte_dpaa2_vfio_setup_intr(&dev->intr_handle, dev_fd,
430 device_info.num_irqs);
436 TAILQ_FOREACH(object, &dpaa2_obj_list, next) {
437 if (dev->dev_type == object->dev_type)
438 object->create(dev_fd, &device_info,
448 DPAA2_BUS_DEBUG("Device (%s) abstracted from VFIO",
454 fslmc_process_mcp(struct rte_dpaa2_device *dev)
458 struct fsl_mc_io dpmng = {0};
459 struct mc_version mc_ver_info = {0};
461 rte_mcp_ptr_list = malloc(sizeof(void *) * 1);
462 if (!rte_mcp_ptr_list) {
463 DPAA2_BUS_ERR("Unable to allocate MC portal memory");
467 dev_name = strdup(dev->device.name);
469 DPAA2_BUS_ERR("Unable to allocate MC device name memory");
470 free(rte_mcp_ptr_list);
471 rte_mcp_ptr_list = NULL;
475 v_addr = vfio_map_mcp_obj(&vfio_group, dev_name);
476 if (v_addr == (intptr_t)MAP_FAILED) {
477 DPAA2_BUS_ERR("Error mapping region (errno = %d)", errno);
478 free(rte_mcp_ptr_list);
479 rte_mcp_ptr_list = NULL;
483 /* check the MC version compatibility */
484 dpmng.regs = (void *)v_addr;
485 if (mc_get_version(&dpmng, CMD_PRI_LOW, &mc_ver_info)) {
486 DPAA2_BUS_ERR("Unable to obtain MC version");
490 if ((mc_ver_info.major != MC_VER_MAJOR) ||
491 (mc_ver_info.minor < MC_VER_MINOR)) {
492 DPAA2_BUS_ERR("DPAA2 MC version not compatible!"
493 " Expected %d.%d.x, Detected %d.%d.%d",
494 MC_VER_MAJOR, MC_VER_MINOR,
495 mc_ver_info.major, mc_ver_info.minor,
496 mc_ver_info.revision);
497 free(rte_mcp_ptr_list);
498 rte_mcp_ptr_list = NULL;
501 rte_mcp_ptr_list[0] = (void *)v_addr;
507 fslmc_vfio_process_group(void)
510 int found_mportal = 0;
511 struct rte_dpaa2_device *dev, *dev_temp;
513 /* Search the MCP as that should be initialized first. */
514 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, dev_temp) {
515 if (dev->dev_type == DPAA2_MPORTAL) {
516 ret = fslmc_process_mcp(dev);
518 DPAA2_BUS_ERR("Unable to map MC Portal");
524 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
527 /* Ideally there is only a single dpmcp, but in case
528 * multiple exists, looping on remaining devices.
533 /* Cannot continue if there is not even a single mportal */
534 if (!found_mportal) {
535 DPAA2_BUS_ERR("No MC Portal device found. Not continuing");
539 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, dev_temp) {
543 switch (dev->dev_type) {
546 ret = fslmc_process_iodevices(dev);
548 DPAA2_BUS_DEBUG("Dev (%s) init failed",
557 /* Call the object creation routine and remove the
558 * device entry from device list
560 ret = fslmc_process_iodevices(dev);
562 DPAA2_BUS_DEBUG("Dev (%s) init failed",
567 /* This device is not required to be in the DPDK
568 * exposed device list.
570 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
576 /* Unknown - ignore */
577 DPAA2_BUS_DEBUG("Found unknown device (%s)",
579 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
589 fslmc_vfio_setup_group(void)
593 struct vfio_group_status status = { .argsz = sizeof(status) };
595 /* if already done once */
596 if (container_device_fd)
599 ret = fslmc_get_container_group(&groupid);
603 /* In case this group was already opened, continue without any
606 if (vfio_group.groupid == groupid) {
607 DPAA2_BUS_ERR("groupid already exists %d", groupid);
611 /* Get the actual group fd */
612 ret = vfio_get_group_fd(groupid);
617 /* Check group viability */
618 ret = ioctl(vfio_group.fd, VFIO_GROUP_GET_STATUS, &status);
620 DPAA2_BUS_ERR("VFIO error getting group status");
621 close(vfio_group.fd);
622 rte_vfio_clear_group(vfio_group.fd);
626 if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
627 DPAA2_BUS_ERR("VFIO group not viable");
628 close(vfio_group.fd);
629 rte_vfio_clear_group(vfio_group.fd);
632 /* Since Group is VIABLE, Store the groupid */
633 vfio_group.groupid = groupid;
635 /* check if group does not have a container yet */
636 if (!(status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
637 /* Now connect this IOMMU group to given container */
638 ret = vfio_connect_container();
641 "Error connecting container with groupid %d",
643 close(vfio_group.fd);
644 rte_vfio_clear_group(vfio_group.fd);
649 /* Get Device information */
650 ret = ioctl(vfio_group.fd, VFIO_GROUP_GET_DEVICE_FD, g_container);
652 DPAA2_BUS_ERR("Error getting device %s fd from group %d",
653 g_container, vfio_group.groupid);
654 close(vfio_group.fd);
655 rte_vfio_clear_group(vfio_group.fd);
658 container_device_fd = ret;
659 DPAA2_BUS_DEBUG("VFIO Container FD is [0x%X]",
660 container_device_fd);