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>
33 #include <rte_eal_memconfig.h>
35 #include "rte_fslmc.h"
36 #include "fslmc_vfio.h"
37 #include "fslmc_logs.h"
38 #include <mc/fsl_dpmng.h>
40 #include "portal/dpaa2_hw_pvt.h"
41 #include "portal/dpaa2_hw_dpio.h"
43 /** Pathname of FSL-MC devices directory. */
44 #define SYSFS_FSL_MC_DEVICES "/sys/bus/fsl-mc/devices"
46 #define FSLMC_CONTAINER_MAX_LEN 8 /**< Of the format dprc.XX */
48 /* Number of VFIO containers & groups with in */
49 static struct fslmc_vfio_group vfio_group
;
50 static struct fslmc_vfio_container vfio_container
;
51 static int container_device_fd
;
52 static char *g_container
;
53 static uint32_t *msi_intr_vaddr
;
54 void *(*rte_mcp_ptr_list
);
56 static struct rte_dpaa2_object_list dpaa2_obj_list
=
57 TAILQ_HEAD_INITIALIZER(dpaa2_obj_list
);
59 /*register a fslmc bus based dpaa2 driver */
61 rte_fslmc_object_register(struct rte_dpaa2_object
*object
)
65 TAILQ_INSERT_TAIL(&dpaa2_obj_list
, object
, next
);
69 fslmc_get_container_group(int *groupid
)
75 container
= getenv("DPRC");
76 if (container
== NULL
) {
77 DPAA2_BUS_DEBUG("DPAA2: DPRC not available");
81 if (strlen(container
) >= FSLMC_CONTAINER_MAX_LEN
) {
82 DPAA2_BUS_ERR("Invalid container name: %s", container
);
86 g_container
= strdup(container
);
88 DPAA2_BUS_ERR("Mem alloc failure; Container name");
93 /* get group number */
94 ret
= rte_vfio_get_group_num(SYSFS_FSL_MC_DEVICES
,
95 g_container
, groupid
);
97 DPAA2_BUS_ERR("Unable to find %s IOMMU group", g_container
);
101 DPAA2_BUS_DEBUG("Container: %s has VFIO iommu group id = %d",
102 g_container
, *groupid
);
108 vfio_connect_container(void)
112 if (vfio_container
.used
) {
113 DPAA2_BUS_DEBUG("No container available");
117 /* Try connecting to vfio container if already created */
118 if (!ioctl(vfio_group
.fd
, VFIO_GROUP_SET_CONTAINER
,
119 &vfio_container
.fd
)) {
121 "Container pre-exists with FD[0x%x] for this group",
123 vfio_group
.container
= &vfio_container
;
127 /* Opens main vfio file descriptor which represents the "container" */
128 fd
= rte_vfio_get_container_fd();
130 DPAA2_BUS_ERR("Failed to open VFIO container");
134 /* Check whether support for SMMU type IOMMU present or not */
135 if (ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_TYPE1_IOMMU
)) {
136 /* Connect group to container */
137 ret
= ioctl(vfio_group
.fd
, VFIO_GROUP_SET_CONTAINER
, &fd
);
139 DPAA2_BUS_ERR("Failed to setup group container");
144 ret
= ioctl(fd
, VFIO_SET_IOMMU
, VFIO_TYPE1_IOMMU
);
146 DPAA2_BUS_ERR("Failed to setup VFIO iommu");
151 DPAA2_BUS_ERR("No supported IOMMU available");
156 vfio_container
.used
= 1;
157 vfio_container
.fd
= fd
;
158 vfio_container
.group
= &vfio_group
;
159 vfio_group
.container
= &vfio_container
;
164 static int vfio_map_irq_region(struct fslmc_vfio_group
*group
)
167 unsigned long *vaddr
= NULL
;
168 struct vfio_iommu_type1_dma_map map
= {
169 .argsz
= sizeof(map
),
170 .flags
= VFIO_DMA_MAP_FLAG_READ
| VFIO_DMA_MAP_FLAG_WRITE
,
176 vaddr
= (unsigned long *)mmap(NULL
, 0x1000, PROT_WRITE
|
177 PROT_READ
, MAP_SHARED
, container_device_fd
, 0x6030000);
178 if (vaddr
== MAP_FAILED
) {
179 DPAA2_BUS_ERR("Unable to map region (errno = %d)", errno
);
183 msi_intr_vaddr
= (uint32_t *)((char *)(vaddr
) + 64);
184 map
.vaddr
= (unsigned long)vaddr
;
185 ret
= ioctl(group
->container
->fd
, VFIO_IOMMU_MAP_DMA
, &map
);
189 DPAA2_BUS_ERR("Unable to map DMA address (errno = %d)", errno
);
193 static int fslmc_map_dma(uint64_t vaddr
, rte_iova_t iovaddr
, size_t len
);
194 static int fslmc_unmap_dma(uint64_t vaddr
, rte_iova_t iovaddr
, size_t len
);
197 fslmc_memevent_cb(enum rte_mem_event type
, const void *addr
, size_t len
,
198 void *arg __rte_unused
)
200 struct rte_memseg_list
*msl
;
201 struct rte_memseg
*ms
;
202 size_t cur_len
= 0, map_len
= 0;
204 rte_iova_t iova_addr
;
207 msl
= rte_mem_virt2memseg_list(addr
);
209 while (cur_len
< len
) {
210 const void *va
= RTE_PTR_ADD(addr
, cur_len
);
212 ms
= rte_mem_virt2memseg(va
, msl
);
213 iova_addr
= ms
->iova
;
214 virt_addr
= ms
->addr_64
;
217 DPAA2_BUS_DEBUG("Request for %s, va=%p, "
218 "virt_addr=0x%" PRIx64
", "
219 "iova=0x%" PRIx64
", map_len=%zu",
220 type
== RTE_MEM_EVENT_ALLOC
?
222 va
, virt_addr
, iova_addr
, map_len
);
224 if (type
== RTE_MEM_EVENT_ALLOC
)
225 ret
= fslmc_map_dma(virt_addr
, iova_addr
, map_len
);
227 ret
= fslmc_unmap_dma(virt_addr
, iova_addr
, map_len
);
230 DPAA2_BUS_ERR("DMA Mapping/Unmapping failed. "
231 "Map=%d, addr=%p, len=%zu, err:(%d)",
232 type
, va
, map_len
, ret
);
239 if (type
== RTE_MEM_EVENT_ALLOC
)
240 DPAA2_BUS_DEBUG("Total Mapped: addr=%p, len=%zu",
243 DPAA2_BUS_DEBUG("Total Unmapped: addr=%p, len=%zu",
248 fslmc_map_dma(uint64_t vaddr
, rte_iova_t iovaddr __rte_unused
, size_t len
)
250 struct fslmc_vfio_group
*group
;
251 struct vfio_iommu_type1_dma_map dma_map
= {
252 .argsz
= sizeof(struct vfio_iommu_type1_dma_map
),
253 .flags
= VFIO_DMA_MAP_FLAG_READ
| VFIO_DMA_MAP_FLAG_WRITE
,
258 dma_map
.vaddr
= vaddr
;
260 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
261 dma_map
.iova
= iovaddr
;
263 dma_map
.iova
= dma_map
.vaddr
;
266 /* SET DMA MAP for IOMMU */
269 if (!group
->container
) {
270 DPAA2_BUS_ERR("Container is not connected ");
274 DPAA2_BUS_DEBUG("--> Map address: 0x%"PRIx64
", size: %"PRIu64
"",
275 (uint64_t)dma_map
.vaddr
, (uint64_t)dma_map
.size
);
276 ret
= ioctl(group
->container
->fd
, VFIO_IOMMU_MAP_DMA
, &dma_map
);
278 DPAA2_BUS_ERR("VFIO_IOMMU_MAP_DMA API(errno = %d)",
287 fslmc_unmap_dma(uint64_t vaddr
, uint64_t iovaddr __rte_unused
, size_t len
)
289 struct fslmc_vfio_group
*group
;
290 struct vfio_iommu_type1_dma_unmap dma_unmap
= {
291 .argsz
= sizeof(struct vfio_iommu_type1_dma_unmap
),
296 dma_unmap
.size
= len
;
297 dma_unmap
.iova
= vaddr
;
299 /* SET DMA MAP for IOMMU */
302 if (!group
->container
) {
303 DPAA2_BUS_ERR("Container is not connected ");
307 DPAA2_BUS_DEBUG("--> Unmap address: 0x%"PRIx64
", size: %"PRIu64
"",
308 (uint64_t)dma_unmap
.iova
, (uint64_t)dma_unmap
.size
);
309 ret
= ioctl(group
->container
->fd
, VFIO_IOMMU_UNMAP_DMA
, &dma_unmap
);
311 DPAA2_BUS_ERR("VFIO_IOMMU_UNMAP_DMA API(errno = %d)",
320 fslmc_dmamap_seg(const struct rte_memseg_list
*msl __rte_unused
,
321 const struct rte_memseg
*ms
, void *arg
)
326 ret
= fslmc_map_dma(ms
->addr_64
, ms
->iova
, ms
->len
);
328 DPAA2_BUS_ERR("Unable to VFIO map (addr=%p, len=%zu)",
336 int rte_fslmc_vfio_dmamap(void)
339 struct rte_mem_config
*mcfg
= rte_eal_get_configuration()->mem_config
;
340 rte_rwlock_t
*mem_lock
= &mcfg
->memory_hotplug_lock
;
342 /* Lock before parsing and registering callback to memory subsystem */
343 rte_rwlock_read_lock(mem_lock
);
345 if (rte_memseg_walk(fslmc_dmamap_seg
, &i
) < 0) {
346 rte_rwlock_read_unlock(mem_lock
);
350 ret
= rte_mem_event_callback_register("fslmc_memevent_clb",
351 fslmc_memevent_cb
, NULL
);
352 if (ret
&& rte_errno
== ENOTSUP
)
353 DPAA2_BUS_DEBUG("Memory event callbacks not supported");
355 DPAA2_BUS_DEBUG("Unable to install memory handler");
357 DPAA2_BUS_DEBUG("Installed memory callback handler");
359 DPAA2_BUS_DEBUG("Total %d segments found.", i
);
361 /* TODO - This is a W.A. as VFIO currently does not add the mapping of
362 * the interrupt region to SMMU. This should be removed once the
363 * support is added in the Kernel.
365 vfio_map_irq_region(&vfio_group
);
367 /* Existing segments have been mapped and memory callback for hotplug
368 * has been installed.
370 rte_rwlock_read_unlock(mem_lock
);
375 static int64_t vfio_map_mcp_obj(struct fslmc_vfio_group
*group
, char *mcp_obj
)
377 intptr_t v_addr
= (intptr_t)MAP_FAILED
;
380 struct vfio_device_info d_info
= { .argsz
= sizeof(d_info
) };
381 struct vfio_region_info reg_info
= { .argsz
= sizeof(reg_info
) };
383 /* getting the mcp object's fd*/
384 mc_fd
= ioctl(group
->fd
, VFIO_GROUP_GET_DEVICE_FD
, mcp_obj
);
386 DPAA2_BUS_ERR("Error in VFIO get dev %s fd from group %d",
391 /* getting device info*/
392 ret
= ioctl(mc_fd
, VFIO_DEVICE_GET_INFO
, &d_info
);
394 DPAA2_BUS_ERR("Error in VFIO getting DEVICE_INFO");
398 /* getting device region info*/
399 ret
= ioctl(mc_fd
, VFIO_DEVICE_GET_REGION_INFO
, ®_info
);
401 DPAA2_BUS_ERR("Error in VFIO getting REGION_INFO");
405 v_addr
= (size_t)mmap(NULL
, reg_info
.size
,
406 PROT_WRITE
| PROT_READ
, MAP_SHARED
,
407 mc_fd
, reg_info
.offset
);
415 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
417 int rte_dpaa2_intr_enable(struct rte_intr_handle
*intr_handle
, int index
)
420 char irq_set_buf
[IRQ_SET_BUF_LEN
];
421 struct vfio_irq_set
*irq_set
;
424 len
= sizeof(irq_set_buf
);
426 irq_set
= (struct vfio_irq_set
*)irq_set_buf
;
427 irq_set
->argsz
= len
;
430 VFIO_IRQ_SET_DATA_EVENTFD
| VFIO_IRQ_SET_ACTION_TRIGGER
;
431 irq_set
->index
= index
;
433 fd_ptr
= (int *)&irq_set
->data
;
434 *fd_ptr
= intr_handle
->fd
;
436 ret
= ioctl(intr_handle
->vfio_dev_fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
438 DPAA2_BUS_ERR("Error:dpaa2 SET IRQs fd=%d, err = %d(%s)",
439 intr_handle
->fd
, errno
, strerror(errno
));
446 int rte_dpaa2_intr_disable(struct rte_intr_handle
*intr_handle
, int index
)
448 struct vfio_irq_set
*irq_set
;
449 char irq_set_buf
[IRQ_SET_BUF_LEN
];
452 len
= sizeof(struct vfio_irq_set
);
454 irq_set
= (struct vfio_irq_set
*)irq_set_buf
;
455 irq_set
->argsz
= len
;
456 irq_set
->flags
= VFIO_IRQ_SET_DATA_NONE
| VFIO_IRQ_SET_ACTION_TRIGGER
;
457 irq_set
->index
= index
;
461 ret
= ioctl(intr_handle
->vfio_dev_fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
464 "Error disabling dpaa2 interrupts for fd %d",
470 /* set up interrupt support (but not enable interrupts) */
472 rte_dpaa2_vfio_setup_intr(struct rte_intr_handle
*intr_handle
,
478 /* start from MSI-X interrupt type */
479 for (i
= 0; i
< num_irqs
; i
++) {
480 struct vfio_irq_info irq_info
= { .argsz
= sizeof(irq_info
) };
485 ret
= ioctl(vfio_dev_fd
, VFIO_DEVICE_GET_IRQ_INFO
, &irq_info
);
487 DPAA2_BUS_ERR("Cannot get IRQ(%d) info, error %i (%s)",
488 i
, errno
, strerror(errno
));
492 /* if this vector cannot be used with eventfd,
493 * fail if we explicitly
494 * specified interrupt type, otherwise continue
496 if ((irq_info
.flags
& VFIO_IRQ_INFO_EVENTFD
) == 0)
499 /* set up an eventfd for interrupts */
500 fd
= eventfd(0, EFD_NONBLOCK
| EFD_CLOEXEC
);
502 DPAA2_BUS_ERR("Cannot set up eventfd, error %i (%s)",
503 errno
, strerror(errno
));
507 intr_handle
->fd
= fd
;
508 intr_handle
->type
= RTE_INTR_HANDLE_VFIO_MSI
;
509 intr_handle
->vfio_dev_fd
= vfio_dev_fd
;
514 /* if we're here, we haven't found a suitable interrupt vector */
519 * fslmc_process_iodevices for processing only IO (ETH, CRYPTO, and possibly
523 fslmc_process_iodevices(struct rte_dpaa2_device
*dev
)
526 struct vfio_device_info device_info
= { .argsz
= sizeof(device_info
) };
527 struct rte_dpaa2_object
*object
= NULL
;
529 dev_fd
= ioctl(vfio_group
.fd
, VFIO_GROUP_GET_DEVICE_FD
,
532 DPAA2_BUS_ERR("Unable to obtain device FD for device:%s",
537 if (ioctl(dev_fd
, VFIO_DEVICE_GET_INFO
, &device_info
)) {
538 DPAA2_BUS_ERR("Unable to obtain information for device:%s",
543 switch (dev
->dev_type
) {
545 rte_dpaa2_vfio_setup_intr(&dev
->intr_handle
, dev_fd
,
546 device_info
.num_irqs
);
552 TAILQ_FOREACH(object
, &dpaa2_obj_list
, next
) {
553 if (dev
->dev_type
== object
->dev_type
)
554 object
->create(dev_fd
, &device_info
,
564 DPAA2_BUS_LOG(DEBUG
, "Device (%s) abstracted from VFIO",
570 fslmc_process_mcp(struct rte_dpaa2_device
*dev
)
574 char *dev_name
= NULL
;
575 struct fsl_mc_io dpmng
= {0};
576 struct mc_version mc_ver_info
= {0};
578 rte_mcp_ptr_list
= malloc(sizeof(void *) * 1);
579 if (!rte_mcp_ptr_list
) {
580 DPAA2_BUS_ERR("Unable to allocate MC portal memory");
585 dev_name
= strdup(dev
->device
.name
);
587 DPAA2_BUS_ERR("Unable to allocate MC device name memory");
592 v_addr
= vfio_map_mcp_obj(&vfio_group
, dev_name
);
593 if (v_addr
== (intptr_t)MAP_FAILED
) {
594 DPAA2_BUS_ERR("Error mapping region (errno = %d)", errno
);
599 /* check the MC version compatibility */
600 dpmng
.regs
= (void *)v_addr
;
601 if (mc_get_version(&dpmng
, CMD_PRI_LOW
, &mc_ver_info
)) {
602 DPAA2_BUS_ERR("Unable to obtain MC version");
607 if ((mc_ver_info
.major
!= MC_VER_MAJOR
) ||
608 (mc_ver_info
.minor
< MC_VER_MINOR
)) {
609 DPAA2_BUS_ERR("DPAA2 MC version not compatible!"
610 " Expected %d.%d.x, Detected %d.%d.%d",
611 MC_VER_MAJOR
, MC_VER_MINOR
,
612 mc_ver_info
.major
, mc_ver_info
.minor
,
613 mc_ver_info
.revision
);
617 rte_mcp_ptr_list
[0] = (void *)v_addr
;
626 if (rte_mcp_ptr_list
) {
627 free(rte_mcp_ptr_list
);
628 rte_mcp_ptr_list
= NULL
;
635 fslmc_vfio_process_group(void)
638 int found_mportal
= 0;
639 struct rte_dpaa2_device
*dev
, *dev_temp
;
641 /* Search the MCP as that should be initialized first. */
642 TAILQ_FOREACH_SAFE(dev
, &rte_fslmc_bus
.device_list
, next
, dev_temp
) {
643 if (dev
->dev_type
== DPAA2_MPORTAL
) {
644 ret
= fslmc_process_mcp(dev
);
646 DPAA2_BUS_ERR("Unable to map MC Portal");
652 TAILQ_REMOVE(&rte_fslmc_bus
.device_list
, dev
, next
);
655 /* Ideally there is only a single dpmcp, but in case
656 * multiple exists, looping on remaining devices.
661 /* Cannot continue if there is not even a single mportal */
662 if (!found_mportal
) {
663 DPAA2_BUS_ERR("No MC Portal device found. Not continuing");
667 TAILQ_FOREACH_SAFE(dev
, &rte_fslmc_bus
.device_list
, next
, dev_temp
) {
668 switch (dev
->dev_type
) {
672 ret
= fslmc_process_iodevices(dev
);
674 DPAA2_BUS_DEBUG("Dev (%s) init failed",
683 /* Call the object creation routine and remove the
684 * device entry from device list
686 ret
= fslmc_process_iodevices(dev
);
688 DPAA2_BUS_DEBUG("Dev (%s) init failed",
693 /* This device is not required to be in the DPDK
694 * exposed device list.
696 TAILQ_REMOVE(&rte_fslmc_bus
.device_list
, dev
, next
);
702 /* Unknown - ignore */
703 DPAA2_BUS_DEBUG("Found unknown device (%s)",
705 TAILQ_REMOVE(&rte_fslmc_bus
.device_list
, dev
, next
);
715 fslmc_vfio_setup_group(void)
719 struct vfio_group_status status
= { .argsz
= sizeof(status
) };
721 /* if already done once */
722 if (container_device_fd
)
725 ret
= fslmc_get_container_group(&groupid
);
729 /* In case this group was already opened, continue without any
732 if (vfio_group
.groupid
== groupid
) {
733 DPAA2_BUS_ERR("groupid already exists %d", groupid
);
737 /* Get the actual group fd */
738 ret
= rte_vfio_get_group_fd(groupid
);
743 /* Check group viability */
744 ret
= ioctl(vfio_group
.fd
, VFIO_GROUP_GET_STATUS
, &status
);
746 DPAA2_BUS_ERR("VFIO error getting group status");
747 close(vfio_group
.fd
);
748 rte_vfio_clear_group(vfio_group
.fd
);
752 if (!(status
.flags
& VFIO_GROUP_FLAGS_VIABLE
)) {
753 DPAA2_BUS_ERR("VFIO group not viable");
754 close(vfio_group
.fd
);
755 rte_vfio_clear_group(vfio_group
.fd
);
758 /* Since Group is VIABLE, Store the groupid */
759 vfio_group
.groupid
= groupid
;
761 /* check if group does not have a container yet */
762 if (!(status
.flags
& VFIO_GROUP_FLAGS_CONTAINER_SET
)) {
763 /* Now connect this IOMMU group to given container */
764 ret
= vfio_connect_container();
767 "Error connecting container with groupid %d",
769 close(vfio_group
.fd
);
770 rte_vfio_clear_group(vfio_group
.fd
);
775 /* Get Device information */
776 ret
= ioctl(vfio_group
.fd
, VFIO_GROUP_GET_DEVICE_FD
, g_container
);
778 DPAA2_BUS_ERR("Error getting device %s fd from group %d",
779 g_container
, vfio_group
.groupid
);
780 close(vfio_group
.fd
);
781 rte_vfio_clear_group(vfio_group
.fd
);
784 container_device_fd
= ret
;
785 DPAA2_BUS_DEBUG("VFIO Container FD is [0x%X]",
786 container_device_fd
);