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>
50 #include <rte_ethdev.h>
51 #include <rte_malloc.h>
52 #include <rte_memcpy.h>
53 #include <rte_string_fns.h>
54 #include <rte_cycles.h>
55 #include <rte_kvargs.h>
59 #include "rte_fslmc.h"
60 #include "fslmc_vfio.h"
62 #include "portal/dpaa2_hw_pvt.h"
63 #include "portal/dpaa2_hw_dpio.h"
65 #define FSLMC_VFIO_LOG(level, fmt, args...) \
66 RTE_LOG(level, EAL, "%s(): " fmt "\n", __func__, ##args)
68 /** Pathname of FSL-MC devices directory. */
69 #define SYSFS_FSL_MC_DEVICES "/sys/bus/fsl-mc/devices"
71 /* Number of VFIO containers & groups with in */
72 static struct fslmc_vfio_group vfio_group;
73 static struct fslmc_vfio_container vfio_container;
74 static int container_device_fd;
75 static uint32_t *msi_intr_vaddr;
76 void *(*rte_mcp_ptr_list);
77 static uint32_t mcp_id;
78 static int is_dma_done;
79 static struct rte_fslmc_object_list fslmc_obj_list =
80 TAILQ_HEAD_INITIALIZER(fslmc_obj_list);
82 /*register a fslmc bus based dpaa2 driver */
84 rte_fslmc_object_register(struct rte_dpaa2_object *object)
88 TAILQ_INSERT_TAIL(&fslmc_obj_list, object, next);
91 static int vfio_connect_container(void)
95 /* Try connecting to vfio container if already created */
96 if (!ioctl(vfio_group.fd, VFIO_GROUP_SET_CONTAINER,
97 &vfio_container.fd)) {
99 "Container pre-exists with FD[0x%x] for this group",
101 vfio_group.container = &vfio_container;
105 /* Opens main vfio file descriptor which represents the "container" */
106 fd = vfio_get_container_fd();
108 FSLMC_VFIO_LOG(ERR, "Failed to open VFIO container");
112 /* Check whether support for SMMU type IOMMU present or not */
113 if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
114 /* Connect group to container */
115 ret = ioctl(vfio_group.fd, VFIO_GROUP_SET_CONTAINER, &fd);
117 FSLMC_VFIO_LOG(ERR, "Failed to setup group container");
122 ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
124 FSLMC_VFIO_LOG(ERR, "Failed to setup VFIO iommu");
129 FSLMC_VFIO_LOG(ERR, "No supported IOMMU available");
134 vfio_container.used = 1;
135 vfio_container.fd = fd;
136 vfio_container.group = &vfio_group;
137 vfio_group.container = &vfio_container;
142 static int vfio_map_irq_region(struct fslmc_vfio_group *group)
145 unsigned long *vaddr = NULL;
146 struct vfio_iommu_type1_dma_map map = {
147 .argsz = sizeof(map),
148 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
154 vaddr = (unsigned long *)mmap(NULL, 0x1000, PROT_WRITE |
155 PROT_READ, MAP_SHARED, container_device_fd, 0x6030000);
156 if (vaddr == MAP_FAILED) {
157 FSLMC_VFIO_LOG(ERR, "Unable to map region (errno = %d)", errno);
161 msi_intr_vaddr = (uint32_t *)((char *)(vaddr) + 64);
162 map.vaddr = (unsigned long)vaddr;
163 ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA, &map);
167 FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA fails (errno = %d)", errno);
171 int rte_fslmc_vfio_dmamap(void)
174 struct fslmc_vfio_group *group;
175 struct vfio_iommu_type1_dma_map dma_map = {
176 .argsz = sizeof(struct vfio_iommu_type1_dma_map),
177 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
181 const struct rte_memseg *memseg;
186 memseg = rte_eal_get_physmem_layout();
187 if (memseg == NULL) {
188 FSLMC_VFIO_LOG(ERR, "Cannot get physical layout.");
192 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
193 if (memseg[i].addr == NULL && memseg[i].len == 0) {
194 FSLMC_VFIO_LOG(DEBUG, "Total %d segments found.", i);
198 dma_map.size = memseg[i].len;
199 dma_map.vaddr = memseg[i].addr_64;
200 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
201 dma_map.iova = memseg[i].phys_addr;
203 dma_map.iova = dma_map.vaddr;
206 /* SET DMA MAP for IOMMU */
209 if (!group->container) {
210 FSLMC_VFIO_LOG(ERR, "Container is not connected ");
214 FSLMC_VFIO_LOG(DEBUG, "-->Initial SHM Virtual ADDR %llX",
216 FSLMC_VFIO_LOG(DEBUG, "-----> DMA size 0x%llX", dma_map.size);
217 ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA,
220 FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA API(errno = %d)",
226 /* Verifying that at least single segment is available */
228 FSLMC_VFIO_LOG(ERR, "No Segments found for VFIO Mapping");
232 /* TODO - This is a W.A. as VFIO currently does not add the mapping of
233 * the interrupt region to SMMU. This should be removed once the
234 * support is added in the Kernel.
236 vfio_map_irq_region(group);
243 static int64_t vfio_map_mcp_obj(struct fslmc_vfio_group *group, char *mcp_obj)
245 int64_t v_addr = (int64_t)MAP_FAILED;
248 struct vfio_device_info d_info = { .argsz = sizeof(d_info) };
249 struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
251 /* getting the mcp object's fd*/
252 mc_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, mcp_obj);
254 FSLMC_VFIO_LOG(ERR, "error in VFIO get dev %s fd from group %d",
259 /* getting device info*/
260 ret = ioctl(mc_fd, VFIO_DEVICE_GET_INFO, &d_info);
262 FSLMC_VFIO_LOG(ERR, "error in VFIO getting DEVICE_INFO");
266 /* getting device region info*/
267 ret = ioctl(mc_fd, VFIO_DEVICE_GET_REGION_INFO, ®_info);
269 FSLMC_VFIO_LOG(ERR, "error in VFIO getting REGION_INFO");
273 FSLMC_VFIO_LOG(DEBUG, "region offset = %llx , region size = %llx",
274 reg_info.offset, reg_info.size);
276 v_addr = (uint64_t)mmap(NULL, reg_info.size,
277 PROT_WRITE | PROT_READ, MAP_SHARED,
278 mc_fd, reg_info.offset);
287 dpaa2_compare_dpaa2_dev(const struct rte_dpaa2_device *dev,
288 const struct rte_dpaa2_device *dev2)
290 /*not the same family device */
291 if (dev->dev_type != DPAA2_MC_DPNI_DEVID ||
292 dev->dev_type != DPAA2_MC_DPSECI_DEVID)
295 if (dev->object_id == dev2->object_id)
302 fslmc_bus_add_device(struct rte_dpaa2_device *dev)
304 struct rte_fslmc_device_list *dev_l;
306 dev_l = &rte_fslmc_bus.device_list;
308 /* device is valid, add in list (sorted) */
309 if (TAILQ_EMPTY(dev_l)) {
310 TAILQ_INSERT_TAIL(dev_l, dev, next);
312 struct rte_dpaa2_device *dev2;
315 TAILQ_FOREACH(dev2, dev_l, next) {
316 ret = dpaa2_compare_dpaa2_dev(dev, dev2);
320 TAILQ_INSERT_BEFORE(dev2, dev, next);
324 TAILQ_INSERT_TAIL(dev_l, dev, next);
328 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
330 int rte_dpaa2_intr_enable(struct rte_intr_handle *intr_handle,
333 struct vfio_irq_set *irq_set;
334 char irq_set_buf[IRQ_SET_BUF_LEN];
335 int *fd_ptr, fd, ret;
337 /* Prepare vfio_irq_set structure and SET the IRQ in VFIO */
338 /* Give the eventfd to VFIO */
340 irq_set = (struct vfio_irq_set *)irq_set_buf;
341 irq_set->argsz = sizeof(irq_set_buf);
343 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
344 VFIO_IRQ_SET_ACTION_TRIGGER;
345 irq_set->index = index;
347 fd_ptr = (int *)&irq_set->data;
350 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
352 FSLMC_VFIO_LOG(ERR, "Unable to set IRQ in VFIO, ret: %d\n",
357 /* Set the FD and update the flags */
358 intr_handle->fd = fd;
362 /* Following function shall fetch total available list of MC devices
363 * from VFIO container & populate private list of devices and other
366 int fslmc_vfio_process_group(void)
368 struct fslmc_vfio_device *vdev;
369 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
370 char *temp_obj, *object_type, *mcp_obj, *dev_name;
371 int32_t object_id, i, dev_fd, ret;
377 struct fslmc_vfio_group *group = &vfio_group;
378 static int process_once;
380 /* if already done once */
382 FSLMC_VFIO_LOG(DEBUG,
383 "Already scanned once - re-scan not supported");
388 sprintf(path, "/sys/kernel/iommu_groups/%d/devices", group->groupid);
392 FSLMC_VFIO_LOG(ERR, "Unable to open directory %s", path);
396 /*Counting the number of devices in a group and getting the mcp ID*/
399 while ((dir = readdir(d)) != NULL) {
400 if (dir->d_type == DT_LNK) {
402 if (!strncmp("dpmcp", dir->d_name, 5)) {
405 mcp_obj = malloc(sizeof(dir->d_name));
408 "mcp obj:alloc failed");
412 strcpy(mcp_obj, dir->d_name);
413 temp_obj = strtok(dir->d_name, ".");
414 temp_obj = strtok(NULL, ".");
415 sscanf(temp_obj, "%d", &mcp_id);
422 FSLMC_VFIO_LOG(ERR, "DPAA2 MCP Object not Found");
425 RTE_LOG(INFO, EAL, "fslmc: DPRC contains = %d devices\n", ndev_count);
427 /* Allocate the memory depends upon number of objects in a group*/
428 group->vfio_device = (struct fslmc_vfio_device *)malloc(ndev_count *
429 sizeof(struct fslmc_vfio_device));
430 if (!(group->vfio_device)) {
431 FSLMC_VFIO_LOG(ERR, "vfio device: Unable to allocate memory\n");
436 /* Allocate memory for MC Portal list */
437 rte_mcp_ptr_list = malloc(sizeof(void *) * 1);
438 if (!rte_mcp_ptr_list) {
439 FSLMC_VFIO_LOG(ERR, "portal list: Unable to allocate memory!");
444 v_addr = vfio_map_mcp_obj(group, mcp_obj);
446 if (v_addr == (int64_t)MAP_FAILED) {
447 FSLMC_VFIO_LOG(ERR, "Error mapping region (errno = %d)", errno);
451 rte_mcp_ptr_list[0] = (void *)v_addr;
455 FSLMC_VFIO_LOG(ERR, "Unable to open %s Directory", path);
460 /* Parsing each object and initiating them*/
461 while ((dir = readdir(d)) != NULL) {
462 if (dir->d_type != DT_LNK)
464 if (!strncmp("dprc", dir->d_name, 4) ||
465 !strncmp("dpmcp", dir->d_name, 5))
467 dev_name = malloc(sizeof(dir->d_name));
469 FSLMC_VFIO_LOG(ERR, "name: Unable to allocate memory");
472 strcpy(dev_name, dir->d_name);
473 object_type = strtok(dir->d_name, ".");
474 temp_obj = strtok(NULL, ".");
475 sscanf(temp_obj, "%d", &object_id);
477 /* getting the device fd*/
478 dev_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, dev_name);
481 "GET_DEVICE_FD error fd: %s, Group: %d",
482 dev_name, group->fd);
488 vdev = &group->vfio_device[group->object_index++];
492 /* Get Device inofrmation */
493 if (ioctl(vdev->fd, VFIO_DEVICE_GET_INFO, &device_info)) {
494 FSLMC_VFIO_LOG(ERR, "DPAA2 VFIO_DEVICE_GET_INFO fail");
497 if (!strcmp(object_type, "dpni") ||
498 !strcmp(object_type, "dpseci")) {
499 struct rte_dpaa2_device *dev;
501 dev = malloc(sizeof(struct rte_dpaa2_device));
505 memset(dev, 0, sizeof(*dev));
506 /* store hw_id of dpni/dpseci device */
507 dev->object_id = object_id;
508 dev->dev_type = (strcmp(object_type, "dpseci")) ?
509 DPAA2_MC_DPNI_DEVID : DPAA2_MC_DPSECI_DEVID;
511 sprintf(dev->name, "%s.%d", object_type, object_id);
512 dev->device.name = dev->name;
514 fslmc_bus_add_device(dev);
515 FSLMC_VFIO_LOG(DEBUG, "DPAA2: Added %s", dev->name);
517 /* Parse all other objects */
518 struct rte_dpaa2_object *object;
520 TAILQ_FOREACH(object, &fslmc_obj_list, next) {
521 if (!strcmp(object_type, object->name))
522 object->create(vdev, &device_info,
531 ret = dpaa2_affine_qbman_swp();
533 FSLMC_VFIO_LOG(DEBUG, "Error in affining qbman swp %d", ret);
540 if (rte_mcp_ptr_list) {
541 free(rte_mcp_ptr_list);
542 rte_mcp_ptr_list = NULL;
545 free(group->vfio_device);
546 group->vfio_device = NULL;
550 int fslmc_vfio_setup_group(void)
552 struct fslmc_vfio_group *group = NULL;
556 struct vfio_group_status status = { .argsz = sizeof(status) };
558 /* if already done once */
559 if (container_device_fd)
562 container = getenv("DPRC");
564 if (container == NULL) {
565 FSLMC_VFIO_LOG(ERR, "VFIO container not set in env DPRC");
569 /* get group number */
570 ret = vfio_get_group_no(SYSFS_FSL_MC_DEVICES, container, &groupid);
572 RTE_LOG(WARNING, EAL, "%s not managed by VFIO, skipping\n",
577 /* if negative, something failed */
581 FSLMC_VFIO_LOG(DEBUG, "VFIO iommu group id = %d", groupid);
583 /* Check if group already exists */
585 if (group->groupid == groupid) {
586 FSLMC_VFIO_LOG(ERR, "groupid already exists %d",
591 /* get the actual group fd */
592 ret = vfio_get_group_fd(groupid);
598 * at this point, we know that this group is viable (meaning,
599 * all devices are either bound to VFIO or not bound to anything)
602 ret = ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status);
604 FSLMC_VFIO_LOG(ERR, " VFIO error getting group status");
609 if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
610 FSLMC_VFIO_LOG(ERR, "VFIO group not viable");
614 /* Since Group is VIABLE, Store the groupid */
615 group->groupid = groupid;
617 /* check if group does not have a container yet */
618 if (!(status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
619 /* Now connect this IOMMU group to given container */
620 ret = vfio_connect_container();
622 FSLMC_VFIO_LOG(ERR, "VFIO error connecting container"
623 " with groupid %d", groupid);
629 /* Get Device information */
630 ret = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, container);
632 FSLMC_VFIO_LOG(ERR, "VFIO error getting device %s fd from"
633 " group %d", container, group->groupid);
636 container_device_fd = ret;
637 FSLMC_VFIO_LOG(DEBUG, "VFIO Container FD is [0x%X]",
638 container_device_fd);