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;
80 static struct rte_dpaa2_object_list dpaa2_obj_list =
81 TAILQ_HEAD_INITIALIZER(dpaa2_obj_list);
83 /*register a fslmc bus based dpaa2 driver */
85 rte_fslmc_object_register(struct rte_dpaa2_object *object)
89 TAILQ_INSERT_TAIL(&dpaa2_obj_list, object, next);
92 static int vfio_connect_container(void)
96 /* Try connecting to vfio container if already created */
97 if (!ioctl(vfio_group.fd, VFIO_GROUP_SET_CONTAINER,
98 &vfio_container.fd)) {
100 "Container pre-exists with FD[0x%x] for this group",
102 vfio_group.container = &vfio_container;
106 /* Opens main vfio file descriptor which represents the "container" */
107 fd = vfio_get_container_fd();
109 FSLMC_VFIO_LOG(ERR, "Failed to open VFIO container");
113 /* Check whether support for SMMU type IOMMU present or not */
114 if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
115 /* Connect group to container */
116 ret = ioctl(vfio_group.fd, VFIO_GROUP_SET_CONTAINER, &fd);
118 FSLMC_VFIO_LOG(ERR, "Failed to setup group container");
123 ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
125 FSLMC_VFIO_LOG(ERR, "Failed to setup VFIO iommu");
130 FSLMC_VFIO_LOG(ERR, "No supported IOMMU available");
135 vfio_container.used = 1;
136 vfio_container.fd = fd;
137 vfio_container.group = &vfio_group;
138 vfio_group.container = &vfio_container;
143 static int vfio_map_irq_region(struct fslmc_vfio_group *group)
146 unsigned long *vaddr = NULL;
147 struct vfio_iommu_type1_dma_map map = {
148 .argsz = sizeof(map),
149 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
155 vaddr = (unsigned long *)mmap(NULL, 0x1000, PROT_WRITE |
156 PROT_READ, MAP_SHARED, container_device_fd, 0x6030000);
157 if (vaddr == MAP_FAILED) {
158 FSLMC_VFIO_LOG(ERR, "Unable to map region (errno = %d)", errno);
162 msi_intr_vaddr = (uint32_t *)((char *)(vaddr) + 64);
163 map.vaddr = (unsigned long)vaddr;
164 ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA, &map);
168 FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA fails (errno = %d)", errno);
172 int rte_fslmc_vfio_dmamap(void)
175 struct fslmc_vfio_group *group;
176 struct vfio_iommu_type1_dma_map dma_map = {
177 .argsz = sizeof(struct vfio_iommu_type1_dma_map),
178 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
182 const struct rte_memseg *memseg;
187 memseg = rte_eal_get_physmem_layout();
188 if (memseg == NULL) {
189 FSLMC_VFIO_LOG(ERR, "Cannot get physical layout.");
193 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
194 if (memseg[i].addr == NULL && memseg[i].len == 0) {
195 FSLMC_VFIO_LOG(DEBUG, "Total %d segments found.", i);
199 dma_map.size = memseg[i].len;
200 dma_map.vaddr = memseg[i].addr_64;
201 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
202 dma_map.iova = memseg[i].phys_addr;
204 dma_map.iova = dma_map.vaddr;
207 /* SET DMA MAP for IOMMU */
210 if (!group->container) {
211 FSLMC_VFIO_LOG(ERR, "Container is not connected ");
215 FSLMC_VFIO_LOG(DEBUG, "-->Initial SHM Virtual ADDR %llX",
217 FSLMC_VFIO_LOG(DEBUG, "-----> DMA size 0x%llX", dma_map.size);
218 ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA,
221 FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA API(errno = %d)",
227 /* Verifying that at least single segment is available */
229 FSLMC_VFIO_LOG(ERR, "No Segments found for VFIO Mapping");
233 /* TODO - This is a W.A. as VFIO currently does not add the mapping of
234 * the interrupt region to SMMU. This should be removed once the
235 * support is added in the Kernel.
237 vfio_map_irq_region(group);
244 static int64_t vfio_map_mcp_obj(struct fslmc_vfio_group *group, char *mcp_obj)
246 int64_t v_addr = (int64_t)MAP_FAILED;
249 struct vfio_device_info d_info = { .argsz = sizeof(d_info) };
250 struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
252 /* getting the mcp object's fd*/
253 mc_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, mcp_obj);
255 FSLMC_VFIO_LOG(ERR, "error in VFIO get dev %s fd from group %d",
260 /* getting device info*/
261 ret = ioctl(mc_fd, VFIO_DEVICE_GET_INFO, &d_info);
263 FSLMC_VFIO_LOG(ERR, "error in VFIO getting DEVICE_INFO");
267 /* getting device region info*/
268 ret = ioctl(mc_fd, VFIO_DEVICE_GET_REGION_INFO, ®_info);
270 FSLMC_VFIO_LOG(ERR, "error in VFIO getting REGION_INFO");
274 FSLMC_VFIO_LOG(DEBUG, "region offset = %llx , region size = %llx",
275 reg_info.offset, reg_info.size);
277 v_addr = (uint64_t)mmap(NULL, reg_info.size,
278 PROT_WRITE | PROT_READ, MAP_SHARED,
279 mc_fd, reg_info.offset);
288 dpaa2_compare_dpaa2_dev(const struct rte_dpaa2_device *dev,
289 const struct rte_dpaa2_device *dev2)
291 /*not the same family device */
292 if (dev->dev_type != DPAA2_ETH ||
293 dev->dev_type != DPAA2_CRYPTO)
296 if (dev->object_id == dev2->object_id)
303 fslmc_bus_add_device(struct rte_dpaa2_device *dev)
305 struct rte_fslmc_device_list *dev_l;
307 dev_l = &rte_fslmc_bus.device_list;
309 /* device is valid, add in list (sorted) */
310 if (TAILQ_EMPTY(dev_l)) {
311 TAILQ_INSERT_TAIL(dev_l, dev, next);
313 struct rte_dpaa2_device *dev2;
316 TAILQ_FOREACH(dev2, dev_l, next) {
317 ret = dpaa2_compare_dpaa2_dev(dev, dev2);
321 TAILQ_INSERT_BEFORE(dev2, dev, next);
325 TAILQ_INSERT_TAIL(dev_l, dev, next);
329 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
331 int rte_dpaa2_intr_enable(struct rte_intr_handle *intr_handle,
334 struct vfio_irq_set *irq_set;
335 char irq_set_buf[IRQ_SET_BUF_LEN];
336 int *fd_ptr, fd, ret;
338 /* Prepare vfio_irq_set structure and SET the IRQ in VFIO */
339 /* Give the eventfd to VFIO */
341 irq_set = (struct vfio_irq_set *)irq_set_buf;
342 irq_set->argsz = sizeof(irq_set_buf);
344 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
345 VFIO_IRQ_SET_ACTION_TRIGGER;
346 irq_set->index = index;
348 fd_ptr = (int *)&irq_set->data;
351 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
353 FSLMC_VFIO_LOG(ERR, "Unable to set IRQ in VFIO, ret: %d\n",
358 /* Set the FD and update the flags */
359 intr_handle->fd = fd;
363 /* Following function shall fetch total available list of MC devices
364 * from VFIO container & populate private list of devices and other
367 int fslmc_vfio_process_group(void)
369 struct fslmc_vfio_device *vdev;
370 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
371 char *temp_obj, *object_type, *mcp_obj, *dev_name;
372 int32_t object_id, i, dev_fd, ret;
378 struct fslmc_vfio_group *group = &vfio_group;
379 static int process_once;
381 /* if already done once */
383 FSLMC_VFIO_LOG(DEBUG,
384 "Already scanned once - re-scan not supported");
389 sprintf(path, "/sys/kernel/iommu_groups/%d/devices", group->groupid);
393 FSLMC_VFIO_LOG(ERR, "Unable to open directory %s", path);
397 /*Counting the number of devices in a group and getting the mcp ID*/
400 while ((dir = readdir(d)) != NULL) {
401 if (dir->d_type == DT_LNK) {
403 if (!strncmp("dpmcp", dir->d_name, 5)) {
406 mcp_obj = malloc(sizeof(dir->d_name));
409 "mcp obj:alloc failed");
413 strcpy(mcp_obj, dir->d_name);
414 temp_obj = strtok(dir->d_name, ".");
415 temp_obj = strtok(NULL, ".");
416 sscanf(temp_obj, "%d", &mcp_id);
423 FSLMC_VFIO_LOG(ERR, "DPAA2 MCP Object not Found");
426 RTE_LOG(INFO, EAL, "fslmc: DPRC contains = %d devices\n", ndev_count);
428 /* Allocate the memory depends upon number of objects in a group*/
429 group->vfio_device = (struct fslmc_vfio_device *)malloc(ndev_count *
430 sizeof(struct fslmc_vfio_device));
431 if (!(group->vfio_device)) {
432 FSLMC_VFIO_LOG(ERR, "vfio device: Unable to allocate memory\n");
437 /* Allocate memory for MC Portal list */
438 rte_mcp_ptr_list = malloc(sizeof(void *) * 1);
439 if (!rte_mcp_ptr_list) {
440 FSLMC_VFIO_LOG(ERR, "portal list: Unable to allocate memory!");
445 v_addr = vfio_map_mcp_obj(group, mcp_obj);
447 if (v_addr == (int64_t)MAP_FAILED) {
448 FSLMC_VFIO_LOG(ERR, "Error mapping region (errno = %d)", errno);
452 rte_mcp_ptr_list[0] = (void *)v_addr;
456 FSLMC_VFIO_LOG(ERR, "Unable to open %s Directory", path);
461 /* Parsing each object and initiating them*/
462 while ((dir = readdir(d)) != NULL) {
463 if (dir->d_type != DT_LNK)
465 if (!strncmp("dprc", dir->d_name, 4) ||
466 !strncmp("dpmcp", dir->d_name, 5))
468 dev_name = malloc(sizeof(dir->d_name));
470 FSLMC_VFIO_LOG(ERR, "name: Unable to allocate memory");
473 strcpy(dev_name, dir->d_name);
474 object_type = strtok(dir->d_name, ".");
475 temp_obj = strtok(NULL, ".");
476 sscanf(temp_obj, "%d", &object_id);
478 /* getting the device fd*/
479 dev_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, dev_name);
482 "GET_DEVICE_FD error fd: %s, Group: %d",
483 dev_name, group->fd);
489 vdev = &group->vfio_device[group->object_index++];
493 /* Get Device inofrmation */
494 if (ioctl(vdev->fd, VFIO_DEVICE_GET_INFO, &device_info)) {
495 FSLMC_VFIO_LOG(ERR, "DPAA2 VFIO_DEVICE_GET_INFO fail");
498 if (!strcmp(object_type, "dpni") ||
499 !strcmp(object_type, "dpseci")) {
500 struct rte_dpaa2_device *dev;
502 dev = malloc(sizeof(struct rte_dpaa2_device));
506 memset(dev, 0, sizeof(*dev));
507 /* store hw_id of dpni/dpseci device */
508 dev->object_id = object_id;
509 dev->dev_type = (strcmp(object_type, "dpseci")) ?
510 DPAA2_ETH : DPAA2_CRYPTO;
512 sprintf(dev->name, "%s.%d", object_type, object_id);
513 dev->device.name = dev->name;
515 fslmc_bus_add_device(dev);
516 FSLMC_VFIO_LOG(DEBUG, "DPAA2: Added %s", dev->name);
518 /* Parse all other objects */
519 struct rte_dpaa2_object *object;
521 TAILQ_FOREACH(object, &dpaa2_obj_list, next) {
522 if (!strcmp(object_type, object->name))
523 object->create(dev_fd, &device_info,
532 ret = dpaa2_affine_qbman_swp();
534 FSLMC_VFIO_LOG(DEBUG, "Error in affining qbman swp %d", ret);
541 if (rte_mcp_ptr_list) {
542 free(rte_mcp_ptr_list);
543 rte_mcp_ptr_list = NULL;
546 free(group->vfio_device);
547 group->vfio_device = NULL;
551 int fslmc_vfio_setup_group(void)
553 struct fslmc_vfio_group *group = NULL;
557 struct vfio_group_status status = { .argsz = sizeof(status) };
559 /* if already done once */
560 if (container_device_fd)
563 container = getenv("DPRC");
565 if (container == NULL) {
566 FSLMC_VFIO_LOG(ERR, "VFIO container not set in env DPRC");
570 /* get group number */
571 ret = vfio_get_group_no(SYSFS_FSL_MC_DEVICES, container, &groupid);
573 RTE_LOG(WARNING, EAL, "%s not managed by VFIO, skipping\n",
578 /* if negative, something failed */
582 FSLMC_VFIO_LOG(DEBUG, "VFIO iommu group id = %d", groupid);
584 /* Check if group already exists */
586 if (group->groupid == groupid) {
587 FSLMC_VFIO_LOG(ERR, "groupid already exists %d",
592 /* get the actual group fd */
593 ret = vfio_get_group_fd(groupid);
599 * at this point, we know that this group is viable (meaning,
600 * all devices are either bound to VFIO or not bound to anything)
603 ret = ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status);
605 FSLMC_VFIO_LOG(ERR, " VFIO error getting group status");
610 if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
611 FSLMC_VFIO_LOG(ERR, "VFIO group not viable");
615 /* Since Group is VIABLE, Store the groupid */
616 group->groupid = groupid;
618 /* check if group does not have a container yet */
619 if (!(status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
620 /* Now connect this IOMMU group to given container */
621 ret = vfio_connect_container();
623 FSLMC_VFIO_LOG(ERR, "VFIO error connecting container"
624 " with groupid %d", groupid);
630 /* Get Device information */
631 ret = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, container);
633 FSLMC_VFIO_LOG(ERR, "VFIO error getting device %s fd from"
634 " group %d", container, group->groupid);
637 container_device_fd = ret;
638 FSLMC_VFIO_LOG(DEBUG, "VFIO Container FD is [0x%X]",
639 container_device_fd);