4 * Copyright (c) 2015-2016 Freescale Semiconductor, Inc. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Freescale Semiconductor, Inc nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/types.h>
41 #include <sys/ioctl.h>
47 #include <sys/eventfd.h>
49 #include <eal_filesystem.h>
51 #include <rte_ethdev.h>
52 #include <rte_malloc.h>
53 #include <rte_memcpy.h>
54 #include <rte_string_fns.h>
55 #include <rte_cycles.h>
56 #include <rte_kvargs.h>
60 #include "rte_fslmc.h"
61 #include "fslmc_vfio.h"
62 #include <mc/fsl_dpmng.h>
64 #include "portal/dpaa2_hw_pvt.h"
65 #include "portal/dpaa2_hw_dpio.h"
67 #define FSLMC_VFIO_LOG(level, fmt, args...) \
68 RTE_LOG(level, EAL, fmt "\n", ##args)
70 /** Pathname of FSL-MC devices directory. */
71 #define SYSFS_FSL_MC_DEVICES "/sys/bus/fsl-mc/devices"
73 #define FSLMC_CONTAINER_MAX_LEN 8 /**< Of the format dprc.XX */
75 /* Number of VFIO containers & groups with in */
76 static struct fslmc_vfio_group vfio_group;
77 static struct fslmc_vfio_container vfio_container;
78 static int container_device_fd;
79 static char *g_container;
80 static uint32_t *msi_intr_vaddr;
81 void *(*rte_mcp_ptr_list);
82 static int is_dma_done;
84 static struct rte_dpaa2_object_list dpaa2_obj_list =
85 TAILQ_HEAD_INITIALIZER(dpaa2_obj_list);
87 /*register a fslmc bus based dpaa2 driver */
89 rte_fslmc_object_register(struct rte_dpaa2_object *object)
93 TAILQ_INSERT_TAIL(&dpaa2_obj_list, object, next);
97 fslmc_get_container_group(int *groupid)
103 container = getenv("DPRC");
104 if (container == NULL) {
105 RTE_LOG(WARNING, EAL, "DPAA2: DPRC not available\n");
109 if (strlen(container) >= FSLMC_CONTAINER_MAX_LEN) {
110 FSLMC_VFIO_LOG(ERR, "Invalid container name: %s\n",
115 g_container = strdup(container);
117 FSLMC_VFIO_LOG(ERR, "Out of memory.");
122 /* get group number */
123 ret = vfio_get_group_no(SYSFS_FSL_MC_DEVICES, g_container, groupid);
125 FSLMC_VFIO_LOG(ERR, "Unable to find %s IOMMU group",
130 FSLMC_VFIO_LOG(DEBUG, "Container: %s has VFIO iommu group id = %d",
131 g_container, *groupid);
137 vfio_connect_container(void)
141 if (vfio_container.used) {
142 FSLMC_VFIO_LOG(DEBUG, "No container available.");
146 /* Try connecting to vfio container if already created */
147 if (!ioctl(vfio_group.fd, VFIO_GROUP_SET_CONTAINER,
148 &vfio_container.fd)) {
150 "Container pre-exists with FD[0x%x] for this group",
152 vfio_group.container = &vfio_container;
156 /* Opens main vfio file descriptor which represents the "container" */
157 fd = vfio_get_container_fd();
159 FSLMC_VFIO_LOG(ERR, "Failed to open VFIO container");
163 /* Check whether support for SMMU type IOMMU present or not */
164 if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
165 /* Connect group to container */
166 ret = ioctl(vfio_group.fd, VFIO_GROUP_SET_CONTAINER, &fd);
168 FSLMC_VFIO_LOG(ERR, "Failed to setup group container");
173 ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
175 FSLMC_VFIO_LOG(ERR, "Failed to setup VFIO iommu");
180 FSLMC_VFIO_LOG(ERR, "No supported IOMMU available");
185 vfio_container.used = 1;
186 vfio_container.fd = fd;
187 vfio_container.group = &vfio_group;
188 vfio_group.container = &vfio_container;
193 static int vfio_map_irq_region(struct fslmc_vfio_group *group)
196 unsigned long *vaddr = NULL;
197 struct vfio_iommu_type1_dma_map map = {
198 .argsz = sizeof(map),
199 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
205 vaddr = (unsigned long *)mmap(NULL, 0x1000, PROT_WRITE |
206 PROT_READ, MAP_SHARED, container_device_fd, 0x6030000);
207 if (vaddr == MAP_FAILED) {
208 FSLMC_VFIO_LOG(ERR, "Unable to map region (errno = %d)", errno);
212 msi_intr_vaddr = (uint32_t *)((char *)(vaddr) + 64);
213 map.vaddr = (unsigned long)vaddr;
214 ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA, &map);
218 FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA fails (errno = %d)", errno);
222 int rte_fslmc_vfio_dmamap(void)
225 struct fslmc_vfio_group *group;
226 struct vfio_iommu_type1_dma_map dma_map = {
227 .argsz = sizeof(struct vfio_iommu_type1_dma_map),
228 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
232 const struct rte_memseg *memseg;
237 memseg = rte_eal_get_physmem_layout();
238 if (memseg == NULL) {
239 FSLMC_VFIO_LOG(ERR, "Cannot get physical layout.");
243 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
244 if (memseg[i].addr == NULL && memseg[i].len == 0) {
245 FSLMC_VFIO_LOG(DEBUG, "Total %d segments found.", i);
249 dma_map.size = memseg[i].len;
250 dma_map.vaddr = memseg[i].addr_64;
251 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
252 dma_map.iova = memseg[i].phys_addr;
254 dma_map.iova = dma_map.vaddr;
257 /* SET DMA MAP for IOMMU */
260 if (!group->container) {
261 FSLMC_VFIO_LOG(ERR, "Container is not connected ");
265 FSLMC_VFIO_LOG(DEBUG, "-->Initial SHM Virtual ADDR %llX",
267 FSLMC_VFIO_LOG(DEBUG, "-----> DMA size 0x%llX", dma_map.size);
268 ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA,
271 FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA API(errno = %d)",
277 /* Verifying that at least single segment is available */
279 FSLMC_VFIO_LOG(ERR, "No Segments found for VFIO Mapping");
283 /* TODO - This is a W.A. as VFIO currently does not add the mapping of
284 * the interrupt region to SMMU. This should be removed once the
285 * support is added in the Kernel.
287 vfio_map_irq_region(group);
294 static int64_t vfio_map_mcp_obj(struct fslmc_vfio_group *group, char *mcp_obj)
296 int64_t v_addr = (int64_t)MAP_FAILED;
299 struct vfio_device_info d_info = { .argsz = sizeof(d_info) };
300 struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
302 /* getting the mcp object's fd*/
303 mc_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, mcp_obj);
305 FSLMC_VFIO_LOG(ERR, "error in VFIO get dev %s fd from group %d",
310 /* getting device info*/
311 ret = ioctl(mc_fd, VFIO_DEVICE_GET_INFO, &d_info);
313 FSLMC_VFIO_LOG(ERR, "error in VFIO getting DEVICE_INFO");
317 /* getting device region info*/
318 ret = ioctl(mc_fd, VFIO_DEVICE_GET_REGION_INFO, ®_info);
320 FSLMC_VFIO_LOG(ERR, "error in VFIO getting REGION_INFO");
324 FSLMC_VFIO_LOG(DEBUG, "region offset = %llx , region size = %llx",
325 reg_info.offset, reg_info.size);
327 v_addr = (uint64_t)mmap(NULL, reg_info.size,
328 PROT_WRITE | PROT_READ, MAP_SHARED,
329 mc_fd, reg_info.offset);
337 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
339 int rte_dpaa2_intr_enable(struct rte_intr_handle *intr_handle, int index)
342 char irq_set_buf[IRQ_SET_BUF_LEN];
343 struct vfio_irq_set *irq_set;
346 len = sizeof(irq_set_buf);
348 irq_set = (struct vfio_irq_set *)irq_set_buf;
349 irq_set->argsz = len;
352 VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
353 irq_set->index = index;
355 fd_ptr = (int *)&irq_set->data;
356 *fd_ptr = intr_handle->fd;
358 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
360 RTE_LOG(ERR, EAL, "Error:dpaa2 SET IRQs fd=%d, err = %d(%s)\n",
361 intr_handle->fd, errno, strerror(errno));
368 int rte_dpaa2_intr_disable(struct rte_intr_handle *intr_handle, int index)
370 struct vfio_irq_set *irq_set;
371 char irq_set_buf[IRQ_SET_BUF_LEN];
374 len = sizeof(struct vfio_irq_set);
376 irq_set = (struct vfio_irq_set *)irq_set_buf;
377 irq_set->argsz = len;
378 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
379 irq_set->index = index;
383 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
386 "Error disabling dpaa2 interrupts for fd %d\n",
392 /* set up interrupt support (but not enable interrupts) */
394 rte_dpaa2_vfio_setup_intr(struct rte_intr_handle *intr_handle,
400 /* start from MSI-X interrupt type */
401 for (i = 0; i < num_irqs; i++) {
402 struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info) };
407 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info);
410 "cannot get IRQ(%d) info, error %i (%s)",
411 i, errno, strerror(errno));
415 /* if this vector cannot be used with eventfd,
416 * fail if we explicitly
417 * specified interrupt type, otherwise continue
419 if ((irq_info.flags & VFIO_IRQ_INFO_EVENTFD) == 0)
422 /* set up an eventfd for interrupts */
423 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
426 "cannot set up eventfd, error %i (%s)\n",
427 errno, strerror(errno));
431 intr_handle->fd = fd;
432 intr_handle->type = RTE_INTR_HANDLE_VFIO_MSI;
433 intr_handle->vfio_dev_fd = vfio_dev_fd;
438 /* if we're here, we haven't found a suitable interrupt vector */
443 * fslmc_process_iodevices for processing only IO (ETH, CRYPTO, and possibly
447 fslmc_process_iodevices(struct rte_dpaa2_device *dev)
450 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
451 struct rte_dpaa2_object *object = NULL;
453 dev_fd = ioctl(vfio_group.fd, VFIO_GROUP_GET_DEVICE_FD,
456 FSLMC_VFIO_LOG(ERR, "Unable to obtain device FD for device:%s",
461 if (ioctl(dev_fd, VFIO_DEVICE_GET_INFO, &device_info)) {
462 FSLMC_VFIO_LOG(ERR, "DPAA2 VFIO_DEVICE_GET_INFO fail");
466 switch (dev->dev_type) {
468 rte_dpaa2_vfio_setup_intr(&dev->intr_handle, dev_fd,
469 device_info.num_irqs);
475 TAILQ_FOREACH(object, &dpaa2_obj_list, next) {
476 if (dev->dev_type == object->dev_type)
477 object->create(dev_fd, &device_info,
487 FSLMC_VFIO_LOG(DEBUG, "Device (%s) abstracted from VFIO",
493 fslmc_process_mcp(struct rte_dpaa2_device *dev)
497 struct fsl_mc_io dpmng = {0};
498 struct mc_version mc_ver_info = {0};
500 rte_mcp_ptr_list = malloc(sizeof(void *) * 1);
501 if (!rte_mcp_ptr_list) {
502 FSLMC_VFIO_LOG(ERR, "Out of memory");
506 dev_name = strdup(dev->device.name);
508 FSLMC_VFIO_LOG(ERR, "Out of memory.");
509 free(rte_mcp_ptr_list);
510 rte_mcp_ptr_list = NULL;
514 v_addr = vfio_map_mcp_obj(&vfio_group, dev_name);
515 if (v_addr == (int64_t)MAP_FAILED) {
516 FSLMC_VFIO_LOG(ERR, "Error mapping region (errno = %d)",
518 free(rte_mcp_ptr_list);
519 rte_mcp_ptr_list = NULL;
523 /* check the MC version compatibility */
524 dpmng.regs = (void *)v_addr;
525 if (mc_get_version(&dpmng, CMD_PRI_LOW, &mc_ver_info))
526 RTE_LOG(WARNING, PMD, "\tmc_get_version failed\n");
528 if ((mc_ver_info.major != MC_VER_MAJOR) ||
529 (mc_ver_info.minor < MC_VER_MINOR)) {
530 RTE_LOG(ERR, PMD, "DPAA2 MC version not compatible!"
531 " Expected %d.%d.x, Detected %d.%d.%d\n",
532 MC_VER_MAJOR, MC_VER_MINOR,
533 mc_ver_info.major, mc_ver_info.minor,
534 mc_ver_info.revision);
535 free(rte_mcp_ptr_list);
536 rte_mcp_ptr_list = NULL;
539 rte_mcp_ptr_list[0] = (void *)v_addr;
545 fslmc_vfio_process_group(void)
548 int found_mportal = 0;
549 struct rte_dpaa2_device *dev, *dev_temp;
551 /* Search the MCP as that should be initialized first. */
552 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, dev_temp) {
553 if (dev->dev_type == DPAA2_MPORTAL) {
554 ret = fslmc_process_mcp(dev);
556 FSLMC_VFIO_LOG(DEBUG, "Unable to map Portal.");
562 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
565 /* Ideally there is only a single dpmcp, but in case
566 * multiple exists, looping on remaining devices.
571 /* Cannot continue if there is not even a single mportal */
572 if (!found_mportal) {
573 FSLMC_VFIO_LOG(DEBUG,
574 "No MC Portal device found. Not continuing.");
578 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, dev_temp) {
582 switch (dev->dev_type) {
585 ret = fslmc_process_iodevices(dev);
587 FSLMC_VFIO_LOG(DEBUG,
588 "Dev (%s) init failed.",
597 /* Call the object creation routine and remove the
598 * device entry from device list
600 ret = fslmc_process_iodevices(dev);
602 FSLMC_VFIO_LOG(DEBUG,
603 "Dev (%s) init failed.",
608 /* This device is not required to be in the DPDK
609 * exposed device list.
611 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
617 /* Unknown - ignore */
618 FSLMC_VFIO_LOG(DEBUG, "Found unknown device (%s).",
620 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
630 fslmc_vfio_setup_group(void)
634 struct vfio_group_status status = { .argsz = sizeof(status) };
636 /* if already done once */
637 if (container_device_fd)
640 ret = fslmc_get_container_group(&groupid);
644 /* In case this group was already opened, continue without any
647 if (vfio_group.groupid == groupid) {
648 FSLMC_VFIO_LOG(ERR, "groupid already exists %d", groupid);
652 /* Get the actual group fd */
653 ret = vfio_get_group_fd(groupid);
658 /* Check group viability */
659 ret = ioctl(vfio_group.fd, VFIO_GROUP_GET_STATUS, &status);
661 FSLMC_VFIO_LOG(ERR, "VFIO error getting group status");
662 close(vfio_group.fd);
666 if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
667 FSLMC_VFIO_LOG(ERR, "VFIO group not viable");
668 close(vfio_group.fd);
671 /* Since Group is VIABLE, Store the groupid */
672 vfio_group.groupid = groupid;
674 /* check if group does not have a container yet */
675 if (!(status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
676 /* Now connect this IOMMU group to given container */
677 ret = vfio_connect_container();
680 "Error connecting container with groupid %d",
682 close(vfio_group.fd);
687 /* Get Device information */
688 ret = ioctl(vfio_group.fd, VFIO_GROUP_GET_DEVICE_FD, g_container);
690 FSLMC_VFIO_LOG(ERR, "Error getting device %s fd from group %d",
691 g_container, vfio_group.groupid);
692 close(vfio_group.fd);
695 container_device_fd = ret;
696 FSLMC_VFIO_LOG(DEBUG, "VFIO Container FD is [0x%X]",
697 container_device_fd);