Skip to content

Commit

Permalink
lib/vfio: rename vfio platform
Browse files Browse the repository at this point in the history
Patch renames vfio_platform into vfio to make
it use with both platform and pcie devices.

Signed-off-by: Vamsi Attunuru <[email protected]>
Change-Id: I233cdc4ab6e9d37600497058e1326015493052c0
Reviewed-on: https://sj1git1.cavium.com/c/IP/SW/dataplane/dpu-offload/+/143441
Tested-by: sa_ip-toolkits-Jenkins <[email protected]>
Reviewed-by: Jerin Jacob <[email protected]>
  • Loading branch information
Vamsi Attunuru authored and jerinjacobk committed Jan 27, 2025
1 parent 44eae61 commit dd6fb1f
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 149 deletions.
2 changes: 1 addition & 1 deletion doc/api/doxy-api-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The public API headers are grouped by topics
- [pem] (@ref dao_pem.h)

- **vfio**
- [vfio_platform] (@ref dao_vfio_platform.h)
- [vfio] (@ref dao_vfio.h)

- **virtio**
- [virtio] (@ref dao_virtio.h)
Expand Down
69 changes: 45 additions & 24 deletions doc/guides/prog_guide/vfio_lib.rst
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
.. SPDX-License-Identifier: Marvell-MIT
Copyright (c) 2024 Marvell.
*********************
VFIO Platform Library
*********************
************
VFIO Library
************
Platform devices in Linux refer to System-on-Chip (SoC) components that aren't situated on standard
buses such as PCI or USB. You can see them in Linux at the path /sys/bus/platform/devices/. To
interact with platform devices from user space, the vfio-platform driver provides a framework. This
library provides DAO APIs built upon this framework, enabling access to the device resources.

Prerequisites:
~~~~~~~~~~~~~~
Also this library can be used to access the standard PCIe devices present at /sys/bus/pci/devices/
from the user space.

Prerequisites for Platform Devices:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To make use of VFIO platform framework, the ``vfio-platform`` module must be loaded first:

.. code-block:: console
Expand All @@ -37,47 +40,65 @@ Next ``DEV`` device must be bound to ``vfio-platform`` driver:
echo DEV | sudo tee /sys/bus/platform/drivers/vfio-platform/bind
Prerequisites for PCIe Devices:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To make use of VFIO PCIe framework, the ``vfio-pci`` module must be loaded first:

.. code-block:: console
sudo modprobe vfio-pci
The PCIe device needs to be bound to vfio-pci, following a standard two-step procedure. Initially,
the driver_override, located within the pci device directory, must be configured to vfio-pci:

.. code-block:: console
Platform device initialization
echo vfio-pci | sudo tee /sys/bus/pci/devices/<BDF>/driver_override
Next ``BDF`` of the device must be bound to ``vfio-pci`` driver:

.. code-block:: console
echo <BDF> | sudo tee /sys/bus/pci/drivers/vfio-pci/bind
DAO VFIO device initialization
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Invoking the `dao_vfio_platform_init()` API creates a VFIO container by opening the /dev/vfio/vfio
character device and initializes the memory used for storing the details of platform devices. This
API should be invoked only once to initiate the library.
Invoking the `dao_vfio_init()` API creates a VFIO container by opening the /dev/vfio/vfio character
device and initializes the memory used for storing the details of the devices. This API should be
invoked only once to initiate the library.

.. code-block:: c
int dao_vfio_platform_init(void);
int dao_vfio_init(void);
After initializing the library, the `dao_vfio_platform_device_setup()` API can be used to initialize
a platform device. The function takes the memory for storing platform device details, specified by
the `struct dao_vfio_platform_device` argument. Upon successful execution, the resources of the
platform devices are mapped, and the device structure is populated.
After initializing the library, the `dao_vfio_device_setup()` API can be used to initialize the
device. The function takes the memory for storing the device details, specified by the
`struct dao_vfio_device` argument. Upon successful execution, the resources of the devices are
mapped, and the device structure is populated.

.. code-block:: c
int dao_vfio_platform_device_setup(const char *dev_name, struct dao_vfio_platform_device *pdev);
int dao_vfio_device_setup(const char *dev_name, struct dao_vfio_device *pdev);
.. literalinclude:: ../../../lib/vfio/dao_vfio_platform.h
.. literalinclude:: ../../../lib/vfio/dao_vfio.h
:language: c
:start-at: struct dao_vfio_mem_resouce
:end-before: End of structure dao_vfio_platform_device.

:end-before: End of structure dao_vfio_device.

Platform device cleanup
DAO VFIO device cleanup
~~~~~~~~~~~~~~~~~~~~~~~

`dao_vfio_platform_device_free()` releases the VFIO platform device and frees the associated
memory.
`dao_vfio_device_free()` releases the VFIO device and frees the associated memory.

.. code-block:: c
void dao_vfio_platform_device_free(struct dao_vfio_platform_device *pdev);
void dao_vfio_device_free(struct dao_vfio_device *pdev);
Upon closing all open devices, the container can be shut down by calling `dao_vfio_platform_fini()`.
Upon closing all open devices, the container can be shut down by calling `dao_vfio_fini()`.

.. code-block:: c
void dao_vfio_platform_fini(void);
void dao_vfio_fini(void);
28 changes: 14 additions & 14 deletions lib/pem/pem.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <dirent.h>

#include "dao_pem.h"
#include "dao_vfio_platform.h"
#include "dao_vfio.h"
#include "pem.h"
#include "sdp.h"

Expand Down Expand Up @@ -135,11 +135,11 @@ pem_update_bar4_info(struct pem *pem)
}

static void
release_vfio_platform_devices(struct pem *pem)
release_vfio_devices(struct pem *pem)
{
sdp_fini(&pem->sdp_pdev);
dao_vfio_platform_device_free(&pem->bar4_pdev);
dao_vfio_platform_fini();
dao_vfio_device_free(&pem->bar4_pdev);
dao_vfio_fini();
}

static int
Expand Down Expand Up @@ -175,32 +175,32 @@ pem_bar4_pdev_name_get(struct pem *pem, char *pdev_name)
}

static int
setup_vfio_platform_devices(struct pem *pem)
setup_vfio_devices(struct pem *pem)
{
char bar4_pdev_name[VFIO_DEV_NAME_MAX_LEN];
int rc;

rc = dao_vfio_platform_init();
rc = dao_vfio_init();
if (rc < 0) {
dao_err("Failed to initialize VFIO platform");
dao_err("Failed to initialize DAO VFIO");
return -1;
}

rc = sdp_init(&pem->sdp_pdev);
if (rc < 0) {
dao_err("Failed to initialize SDP platform device");
dao_err("Failed to initialize SDP device");
return -1;
}

rc = pem_bar4_pdev_name_get(pem, bar4_pdev_name);
if (rc < 0) {
dao_err("Failed to get PEM platform device name");
dao_err("Failed to get PEM device name");
return -1;
}

rc = dao_vfio_platform_device_setup(bar4_pdev_name, &pem->bar4_pdev);
rc = dao_vfio_device_setup(bar4_pdev_name, &pem->bar4_pdev);
if (rc < 0) {
dao_err("Failed to initialize PEM BAR4 platform device");
dao_err("Failed to initialize PEM BAR4 device");
return -1;
}

Expand All @@ -222,7 +222,7 @@ dao_pem_dev_init(uint16_t pem_devid, struct dao_pem_dev_conf *conf)
return -1;
}

rc = setup_vfio_platform_devices(pem);
rc = setup_vfio_devices(pem);
if (rc < 0)
return -1;

Expand Down Expand Up @@ -267,7 +267,7 @@ dao_pem_dev_init(uint16_t pem_devid, struct dao_pem_dev_conf *conf)

return 0;
err:
release_vfio_platform_devices(pem);
release_vfio_devices(pem);
return -EFAULT;
}

Expand All @@ -287,7 +287,7 @@ dao_pem_dev_fini(uint16_t pem_devid)
pem->regions[i] = NULL;
}

release_vfio_platform_devices(pem);
release_vfio_devices(pem);
return 0;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/pem/pem.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <dao_log.h>
#include <dao_util.h>
#include <dao_vfio_platform.h>
#include <dao_vfio.h>

#define PEM_BAR4_NUM_INDEX 16
#define PEM_BAR4_INDEX_START 0
Expand Down Expand Up @@ -36,8 +36,8 @@ struct pem {
bool ctrl_done;
struct pem_region *regions[DAO_PEM_CTRL_REGION_MAX];
uint64_t region_mask[DAO_PEM_CTRL_REGION_MASK_MAX];
struct dao_vfio_platform_device bar4_pdev;
struct dao_vfio_platform_device sdp_pdev;
struct dao_vfio_device bar4_pdev;
struct dao_vfio_device sdp_pdev;
};

#endif /* __INCLUDE_PEM_H__ */
18 changes: 9 additions & 9 deletions lib/pem/sdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#include <dao_log.h>
#include <dao_util.h>

#define SDP_PLAT_DEV_NAME "86e000000000.dpi_sdp_regs"
#define SDP_PLAT_DEV_NAME "86e000000000.dpi_sdp_regs"

int
sdp_reg_write(struct dao_vfio_platform_device *sdp_pdev, uint64_t offset, uint64_t val)
sdp_reg_write(struct dao_vfio_device *sdp_pdev, uint64_t offset, uint64_t val)
{
if (offset > sdp_pdev->mem[0].len)
return -ENOMEM;
Expand All @@ -27,7 +27,7 @@ sdp_reg_write(struct dao_vfio_platform_device *sdp_pdev, uint64_t offset, uint64
}

uint64_t
sdp_reg_read(struct dao_vfio_platform_device *sdp_pdev, uint64_t offset)
sdp_reg_read(struct dao_vfio_device *sdp_pdev, uint64_t offset)
{
if (offset > sdp_pdev->mem[0].len)
return -ENOMEM;
Expand All @@ -36,7 +36,7 @@ sdp_reg_read(struct dao_vfio_platform_device *sdp_pdev, uint64_t offset)
}

uint64_t *
sdp_reg_addr(struct dao_vfio_platform_device *sdp_pdev, uint64_t offset)
sdp_reg_addr(struct dao_vfio_device *sdp_pdev, uint64_t offset)
{
if (offset > sdp_pdev->mem[0].len)
return NULL;
Expand All @@ -45,14 +45,14 @@ sdp_reg_addr(struct dao_vfio_platform_device *sdp_pdev, uint64_t offset)
}

int
sdp_init(struct dao_vfio_platform_device *sdp_pdev)
sdp_init(struct dao_vfio_device *sdp_pdev)
{
uint64_t reg_val;
int rc;

rc = dao_vfio_platform_device_setup(SDP_PLAT_DEV_NAME, sdp_pdev);
rc = dao_vfio_device_setup(SDP_PLAT_DEV_NAME, sdp_pdev);
if (rc < 0) {
dao_err("Filed to setup VFIO platform device %s", SDP_PLAT_DEV_NAME);
dao_err("Filed to setup DAO VFIO device %s", SDP_PLAT_DEV_NAME);
return errno;
}

Expand All @@ -69,7 +69,7 @@ sdp_init(struct dao_vfio_platform_device *sdp_pdev)
}

void
sdp_fini(struct dao_vfio_platform_device *sdp_pdev)
sdp_fini(struct dao_vfio_device *sdp_pdev)
{
dao_vfio_platform_device_free(sdp_pdev);
dao_vfio_device_free(sdp_pdev);
}
12 changes: 6 additions & 6 deletions lib/pem/sdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <stdint.h>

#include <dao_vfio_platform.h>
#include <dao_vfio.h>

#define SDP_RX_OUT_ENABLE(x) (0x80010170 | (x) << 17)
#define SDP_RX_OUT_CNTS(x) (0x80010100 | (x) << 17)
Expand All @@ -19,10 +19,10 @@
#define SDP_EPFX_RINFO_RPVF_SHIFT 32
#define SDP_EPFX_RINFO_SRN_MASK DAO_GENMASK_ULL(6, 0)

int sdp_init(struct dao_vfio_platform_device *sdp_pdev);
uint64_t sdp_reg_read(struct dao_vfio_platform_device *sdp_pdev, uint64_t offset);
int sdp_reg_write(struct dao_vfio_platform_device *sdp_pdev, uint64_t offset, uint64_t val);
uint64_t *sdp_reg_addr(struct dao_vfio_platform_device *sdp_pdev, uint64_t offset);
void sdp_fini(struct dao_vfio_platform_device *sdp_pdev);
int sdp_init(struct dao_vfio_device *sdp_pdev);
uint64_t sdp_reg_read(struct dao_vfio_device *sdp_pdev, uint64_t offset);
int sdp_reg_write(struct dao_vfio_device *sdp_pdev, uint64_t offset, uint64_t val);
uint64_t *sdp_reg_addr(struct dao_vfio_device *sdp_pdev, uint64_t offset);
void sdp_fini(struct dao_vfio_device *sdp_pdev);

#endif /* __INCLUDE_SDP_H__ */
20 changes: 10 additions & 10 deletions lib/vfio/dao_vfio_platform.c → lib/vfio/dao_vfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <unistd.h>

#include <dao_log.h>
#include <dao_vfio_platform.h>
#include <dao_vfio.h>

#define VFIO_MAX_GROUPS 8
#define VFIO_GROUP_FMT "/dev/vfio/%u"
Expand All @@ -35,12 +35,12 @@ struct vfio_config {
static struct vfio_config vfio_cfg = {.container_fd = -1};

int
dao_vfio_platform_init(void)
dao_vfio_init(void)
{
int i;

if (vfio_cfg.container_fd != -1) {
dao_dbg("VFIO platform has already been initialized.");
dao_dbg("DAO VFIO has already been initialized.");
return 0;
}

Expand Down Expand Up @@ -136,7 +136,7 @@ vfio_get_group_fd(const char *dev_name)
}

static void
vfio_platform_device_mem_free(struct dao_vfio_platform_device *pdev)
vfio_device_mem_free(struct dao_vfio_device *pdev)
{
unsigned int i;

Expand Down Expand Up @@ -165,7 +165,7 @@ vfio_clear_group(int group_fd)
}

int
dao_vfio_platform_device_setup(const char *dev_name, struct dao_vfio_platform_device *pdev)
dao_vfio_device_setup(const char *dev_name, struct dao_vfio_device *pdev)
{
struct vfio_group_status group_status = {.argsz = sizeof(group_status)};
struct vfio_device_info device_info = {.argsz = sizeof(device_info)};
Expand Down Expand Up @@ -242,11 +242,11 @@ dao_vfio_platform_device_setup(const char *dev_name, struct dao_vfio_platform_de
pdev->mem[i].len = reg.size;
}

dao_dbg("%s: enabled VFIO platform device", dev_name);
dao_dbg("%s: enabled VFIO device", dev_name);
return 0;

device_mem_free:
vfio_platform_device_mem_free(pdev);
vfio_device_mem_free(pdev);
close_device_fd:
close(device_fd);
clear_group:
Expand All @@ -255,15 +255,15 @@ dao_vfio_platform_device_setup(const char *dev_name, struct dao_vfio_platform_de
}

void
dao_vfio_platform_device_free(struct dao_vfio_platform_device *pdev)
dao_vfio_device_free(struct dao_vfio_device *pdev)
{
vfio_platform_device_mem_free(pdev);
vfio_device_mem_free(pdev);
vfio_clear_group(pdev->group_fd);
close(pdev->device_fd);
}

void
dao_vfio_platform_fini(void)
dao_vfio_fini(void)
{
int i;

Expand Down
Loading

0 comments on commit dd6fb1f

Please sign in to comment.