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, fmt "\n", ##args)
68 /** Pathname of FSL-MC devices directory. */
69 #define SYSFS_FSL_MC_DEVICES "/sys/bus/fsl-mc/devices"
71 #define FSLMC_CONTAINER_MAX_LEN 8 /**< Of the format dprc.XX */
73 /* Number of VFIO containers & groups with in */
74 static struct fslmc_vfio_group vfio_group;
75 static struct fslmc_vfio_container vfio_container;
76 static int container_device_fd;
77 static char *g_container;
78 static uint32_t *msi_intr_vaddr;
79 void *(*rte_mcp_ptr_list);
80 static int is_dma_done;
82 static struct rte_dpaa2_object_list dpaa2_obj_list =
83 TAILQ_HEAD_INITIALIZER(dpaa2_obj_list);
85 /*register a fslmc bus based dpaa2 driver */
87 rte_fslmc_object_register(struct rte_dpaa2_object *object)
91 TAILQ_INSERT_TAIL(&dpaa2_obj_list, object, next);
95 fslmc_get_container_group(int *groupid)
101 container = getenv("DPRC");
102 if (container == NULL) {
103 RTE_LOG(WARNING, EAL, "DPAA2: DPRC not available\n");
107 if (strlen(container) >= FSLMC_CONTAINER_MAX_LEN) {
108 FSLMC_VFIO_LOG(ERR, "Invalid container name: %s\n",
113 g_container = strdup(container);
115 FSLMC_VFIO_LOG(ERR, "Out of memory.");
120 /* get group number */
121 ret = vfio_get_group_no(SYSFS_FSL_MC_DEVICES, g_container, groupid);
123 FSLMC_VFIO_LOG(ERR, "Unable to find %s IOMMU group",
128 FSLMC_VFIO_LOG(DEBUG, "Container: %s has VFIO iommu group id = %d",
129 g_container, *groupid);
135 vfio_connect_container(void)
139 if (vfio_container.used) {
140 FSLMC_VFIO_LOG(DEBUG, "No container available.");
144 /* Try connecting to vfio container if already created */
145 if (!ioctl(vfio_group.fd, VFIO_GROUP_SET_CONTAINER,
146 &vfio_container.fd)) {
148 "Container pre-exists with FD[0x%x] for this group",
150 vfio_group.container = &vfio_container;
154 /* Opens main vfio file descriptor which represents the "container" */
155 fd = vfio_get_container_fd();
157 FSLMC_VFIO_LOG(ERR, "Failed to open VFIO container");
161 /* Check whether support for SMMU type IOMMU present or not */
162 if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
163 /* Connect group to container */
164 ret = ioctl(vfio_group.fd, VFIO_GROUP_SET_CONTAINER, &fd);
166 FSLMC_VFIO_LOG(ERR, "Failed to setup group container");
171 ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
173 FSLMC_VFIO_LOG(ERR, "Failed to setup VFIO iommu");
178 FSLMC_VFIO_LOG(ERR, "No supported IOMMU available");
183 vfio_container.used = 1;
184 vfio_container.fd = fd;
185 vfio_container.group = &vfio_group;
186 vfio_group.container = &vfio_container;
191 static int vfio_map_irq_region(struct fslmc_vfio_group *group)
194 unsigned long *vaddr = NULL;
195 struct vfio_iommu_type1_dma_map map = {
196 .argsz = sizeof(map),
197 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
203 vaddr = (unsigned long *)mmap(NULL, 0x1000, PROT_WRITE |
204 PROT_READ, MAP_SHARED, container_device_fd, 0x6030000);
205 if (vaddr == MAP_FAILED) {
206 FSLMC_VFIO_LOG(ERR, "Unable to map region (errno = %d)", errno);
210 msi_intr_vaddr = (uint32_t *)((char *)(vaddr) + 64);
211 map.vaddr = (unsigned long)vaddr;
212 ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA, &map);
216 FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA fails (errno = %d)", errno);
220 int rte_fslmc_vfio_dmamap(void)
223 struct fslmc_vfio_group *group;
224 struct vfio_iommu_type1_dma_map dma_map = {
225 .argsz = sizeof(struct vfio_iommu_type1_dma_map),
226 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
230 const struct rte_memseg *memseg;
235 memseg = rte_eal_get_physmem_layout();
236 if (memseg == NULL) {
237 FSLMC_VFIO_LOG(ERR, "Cannot get physical layout.");
241 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
242 if (memseg[i].addr == NULL && memseg[i].len == 0) {
243 FSLMC_VFIO_LOG(DEBUG, "Total %d segments found.", i);
247 dma_map.size = memseg[i].len;
248 dma_map.vaddr = memseg[i].addr_64;
249 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
250 dma_map.iova = memseg[i].phys_addr;
252 dma_map.iova = dma_map.vaddr;
255 /* SET DMA MAP for IOMMU */
258 if (!group->container) {
259 FSLMC_VFIO_LOG(ERR, "Container is not connected ");
263 FSLMC_VFIO_LOG(DEBUG, "-->Initial SHM Virtual ADDR %llX",
265 FSLMC_VFIO_LOG(DEBUG, "-----> DMA size 0x%llX", dma_map.size);
266 ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA,
269 FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA API(errno = %d)",
275 /* Verifying that at least single segment is available */
277 FSLMC_VFIO_LOG(ERR, "No Segments found for VFIO Mapping");
281 /* TODO - This is a W.A. as VFIO currently does not add the mapping of
282 * the interrupt region to SMMU. This should be removed once the
283 * support is added in the Kernel.
285 vfio_map_irq_region(group);
292 static int64_t vfio_map_mcp_obj(struct fslmc_vfio_group *group, char *mcp_obj)
294 int64_t v_addr = (int64_t)MAP_FAILED;
297 struct vfio_device_info d_info = { .argsz = sizeof(d_info) };
298 struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
300 /* getting the mcp object's fd*/
301 mc_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, mcp_obj);
303 FSLMC_VFIO_LOG(ERR, "error in VFIO get dev %s fd from group %d",
308 /* getting device info*/
309 ret = ioctl(mc_fd, VFIO_DEVICE_GET_INFO, &d_info);
311 FSLMC_VFIO_LOG(ERR, "error in VFIO getting DEVICE_INFO");
315 /* getting device region info*/
316 ret = ioctl(mc_fd, VFIO_DEVICE_GET_REGION_INFO, ®_info);
318 FSLMC_VFIO_LOG(ERR, "error in VFIO getting REGION_INFO");
322 FSLMC_VFIO_LOG(DEBUG, "region offset = %llx , region size = %llx",
323 reg_info.offset, reg_info.size);
325 v_addr = (uint64_t)mmap(NULL, reg_info.size,
326 PROT_WRITE | PROT_READ, MAP_SHARED,
327 mc_fd, reg_info.offset);
335 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
337 int rte_dpaa2_intr_enable(struct rte_intr_handle *intr_handle,
340 struct vfio_irq_set *irq_set;
341 char irq_set_buf[IRQ_SET_BUF_LEN];
342 int *fd_ptr, fd, ret;
344 /* Prepare vfio_irq_set structure and SET the IRQ in VFIO */
345 /* Give the eventfd to VFIO */
347 irq_set = (struct vfio_irq_set *)irq_set_buf;
348 irq_set->argsz = sizeof(irq_set_buf);
350 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
351 VFIO_IRQ_SET_ACTION_TRIGGER;
352 irq_set->index = index;
354 fd_ptr = (int *)&irq_set->data;
357 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
359 FSLMC_VFIO_LOG(ERR, "Unable to set IRQ in VFIO, ret: %d\n",
364 /* Set the FD and update the flags */
365 intr_handle->fd = fd;
370 * fslmc_process_iodevices for processing only IO (ETH, CRYPTO, and possibly
374 fslmc_process_iodevices(struct rte_dpaa2_device *dev)
377 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
378 struct rte_dpaa2_object *object = NULL;
380 dev_fd = ioctl(vfio_group.fd, VFIO_GROUP_GET_DEVICE_FD,
383 FSLMC_VFIO_LOG(ERR, "Unable to obtain device FD for device:%s",
388 if (ioctl(dev_fd, VFIO_DEVICE_GET_INFO, &device_info)) {
389 FSLMC_VFIO_LOG(ERR, "DPAA2 VFIO_DEVICE_GET_INFO fail");
393 switch (dev->dev_type) {
398 TAILQ_FOREACH(object, &dpaa2_obj_list, next) {
399 if (dev->dev_type == object->dev_type)
400 object->create(dev_fd, &device_info,
410 FSLMC_VFIO_LOG(DEBUG, "Device (%s) abstracted from VFIO",
416 fslmc_process_mcp(struct rte_dpaa2_device *dev)
421 rte_mcp_ptr_list = malloc(sizeof(void *) * 1);
422 if (!rte_mcp_ptr_list) {
423 FSLMC_VFIO_LOG(ERR, "Out of memory");
427 dev_name = strdup(dev->device.name);
429 FSLMC_VFIO_LOG(ERR, "Out of memory.");
430 free(rte_mcp_ptr_list);
431 rte_mcp_ptr_list = NULL;
435 v_addr = vfio_map_mcp_obj(&vfio_group, dev_name);
436 if (v_addr == (int64_t)MAP_FAILED) {
437 FSLMC_VFIO_LOG(ERR, "Error mapping region (errno = %d)",
439 free(rte_mcp_ptr_list);
440 rte_mcp_ptr_list = NULL;
444 rte_mcp_ptr_list[0] = (void *)v_addr;
450 fslmc_vfio_process_group(void)
453 int found_mportal = 0;
454 struct rte_dpaa2_device *dev, *dev_temp;
456 /* Search the MCP as that should be initialized first. */
457 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, dev_temp) {
458 if (dev->dev_type == DPAA2_MPORTAL) {
459 ret = fslmc_process_mcp(dev);
461 FSLMC_VFIO_LOG(DEBUG, "Unable to map Portal.");
467 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
470 /* Ideally there is only a single dpmcp, but in case
471 * multiple exists, looping on remaining devices.
476 /* Cannot continue if there is not even a single mportal */
477 if (!found_mportal) {
478 FSLMC_VFIO_LOG(DEBUG,
479 "No MC Portal device found. Not continuing.");
483 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, dev_temp) {
487 switch (dev->dev_type) {
490 ret = fslmc_process_iodevices(dev);
492 FSLMC_VFIO_LOG(DEBUG,
493 "Dev (%s) init failed.",
502 /* Call the object creation routine and remove the
503 * device entry from device list
505 ret = fslmc_process_iodevices(dev);
507 FSLMC_VFIO_LOG(DEBUG,
508 "Dev (%s) init failed.",
513 /* This device is not required to be in the DPDK
514 * exposed device list.
516 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
522 /* Unknown - ignore */
523 FSLMC_VFIO_LOG(DEBUG, "Found unknown device (%s).",
525 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
535 fslmc_vfio_setup_group(void)
539 struct vfio_group_status status = { .argsz = sizeof(status) };
541 /* if already done once */
542 if (container_device_fd)
545 ret = fslmc_get_container_group(&groupid);
549 /* In case this group was already opened, continue without any
552 if (vfio_group.groupid == groupid) {
553 FSLMC_VFIO_LOG(ERR, "groupid already exists %d", groupid);
557 /* Get the actual group fd */
558 ret = vfio_get_group_fd(groupid);
563 /* Check group viability */
564 ret = ioctl(vfio_group.fd, VFIO_GROUP_GET_STATUS, &status);
566 FSLMC_VFIO_LOG(ERR, "VFIO error getting group status");
567 close(vfio_group.fd);
571 if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
572 FSLMC_VFIO_LOG(ERR, "VFIO group not viable");
573 close(vfio_group.fd);
576 /* Since Group is VIABLE, Store the groupid */
577 vfio_group.groupid = groupid;
579 /* check if group does not have a container yet */
580 if (!(status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
581 /* Now connect this IOMMU group to given container */
582 ret = vfio_connect_container();
585 "Error connecting container with groupid %d",
587 close(vfio_group.fd);
592 /* Get Device information */
593 ret = ioctl(vfio_group.fd, VFIO_GROUP_GET_DEVICE_FD, g_container);
595 FSLMC_VFIO_LOG(ERR, "Error getting device %s fd from group %d",
596 g_container, vfio_group.groupid);
597 close(vfio_group.fd);
600 container_device_fd = ret;
601 FSLMC_VFIO_LOG(DEBUG, "VFIO Container FD is [0x%X]",
602 container_device_fd);