2020-03-26 22:01:24 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
/*
|
2020-12-15 22:42:56 +08:00
|
|
|
* VDPA device simulator core.
|
2020-03-26 22:01:24 +08:00
|
|
|
*
|
|
|
|
* Copyright (c) 2020, Red Hat Inc. All rights reserved.
|
|
|
|
* Author: Jason Wang <jasowang@redhat.com>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/sched.h>
|
2020-09-22 21:31:03 +08:00
|
|
|
#include <linux/dma-map-ops.h>
|
2020-03-26 22:01:24 +08:00
|
|
|
#include <linux/vringh.h>
|
|
|
|
#include <linux/vdpa.h>
|
|
|
|
#include <linux/vhost_iotlb.h>
|
2022-09-27 15:48:09 +08:00
|
|
|
#include <uapi/linux/vdpa.h>
|
2020-12-15 22:42:56 +08:00
|
|
|
|
|
|
|
#include "vdpa_sim.h"
|
2020-03-26 22:01:24 +08:00
|
|
|
|
|
|
|
#define DRV_VERSION "0.1"
|
|
|
|
#define DRV_AUTHOR "Jason Wang <jasowang@redhat.com>"
|
2020-12-15 22:42:56 +08:00
|
|
|
#define DRV_DESC "vDPA Device Simulator core"
|
2020-03-26 22:01:24 +08:00
|
|
|
#define DRV_LICENSE "GPL v2"
|
|
|
|
|
2020-08-05 00:20:41 +08:00
|
|
|
static int batch_mapping = 1;
|
|
|
|
module_param(batch_mapping, int, 0444);
|
|
|
|
MODULE_PARM_DESC(batch_mapping, "Batched mapping 1 -Enable; 0 - Disable");
|
|
|
|
|
2020-12-15 22:42:42 +08:00
|
|
|
static int max_iotlb_entries = 2048;
|
|
|
|
module_param(max_iotlb_entries, int, 0444);
|
|
|
|
MODULE_PARM_DESC(max_iotlb_entries,
|
2022-06-21 23:12:08 +08:00
|
|
|
"Maximum number of iotlb entries for each address space. 0 means unlimited. (default: 2048)");
|
2020-12-15 22:42:42 +08:00
|
|
|
|
2020-03-26 22:01:24 +08:00
|
|
|
#define VDPASIM_QUEUE_ALIGN PAGE_SIZE
|
|
|
|
#define VDPASIM_QUEUE_MAX 256
|
|
|
|
#define VDPASIM_VENDOR_ID 0
|
|
|
|
|
|
|
|
static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
|
|
|
|
{
|
|
|
|
return container_of(vdpa, struct vdpasim, vdpa);
|
|
|
|
}
|
|
|
|
|
2020-12-15 22:42:52 +08:00
|
|
|
static void vdpasim_vq_notify(struct vringh *vring)
|
|
|
|
{
|
|
|
|
struct vdpasim_virtqueue *vq =
|
|
|
|
container_of(vring, struct vdpasim_virtqueue, vring);
|
|
|
|
|
|
|
|
if (!vq->cb)
|
|
|
|
return;
|
|
|
|
|
|
|
|
vq->cb(vq->private);
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:01:24 +08:00
|
|
|
static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx)
|
|
|
|
{
|
|
|
|
struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
|
2023-01-19 00:43:58 +08:00
|
|
|
uint16_t last_avail_idx = vq->vring.last_avail_idx;
|
2020-03-26 22:01:24 +08:00
|
|
|
|
2022-12-21 14:21:46 +08:00
|
|
|
vringh_init_iotlb(&vq->vring, vdpasim->features, vq->num, true,
|
2020-04-10 19:54:22 +08:00
|
|
|
(struct vring_desc *)(uintptr_t)vq->desc_addr,
|
|
|
|
(struct vring_avail *)
|
|
|
|
(uintptr_t)vq->driver_addr,
|
|
|
|
(struct vring_used *)
|
|
|
|
(uintptr_t)vq->device_addr);
|
2020-12-15 22:42:52 +08:00
|
|
|
|
2023-01-19 00:43:58 +08:00
|
|
|
vq->vring.last_avail_idx = last_avail_idx;
|
2020-12-15 22:42:52 +08:00
|
|
|
vq->vring.notify = vdpasim_vq_notify;
|
2020-03-26 22:01:24 +08:00
|
|
|
}
|
|
|
|
|
2020-12-15 22:42:46 +08:00
|
|
|
static void vdpasim_vq_reset(struct vdpasim *vdpasim,
|
|
|
|
struct vdpasim_virtqueue *vq)
|
2020-03-26 22:01:24 +08:00
|
|
|
{
|
2020-05-09 10:20:02 +08:00
|
|
|
vq->ready = false;
|
2020-03-26 22:01:24 +08:00
|
|
|
vq->desc_addr = 0;
|
|
|
|
vq->driver_addr = 0;
|
|
|
|
vq->device_addr = 0;
|
|
|
|
vq->cb = NULL;
|
|
|
|
vq->private = NULL;
|
2020-12-15 22:42:46 +08:00
|
|
|
vringh_init_iotlb(&vq->vring, vdpasim->dev_attr.supported_features,
|
|
|
|
VDPASIM_QUEUE_MAX, false, NULL, NULL, NULL);
|
2020-12-15 22:42:52 +08:00
|
|
|
|
|
|
|
vq->vring.notify = NULL;
|
2020-03-26 22:01:24 +08:00
|
|
|
}
|
|
|
|
|
2021-08-31 18:36:26 +08:00
|
|
|
static void vdpasim_do_reset(struct vdpasim *vdpasim)
|
2020-03-26 22:01:24 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2022-03-31 02:03:59 +08:00
|
|
|
spin_lock(&vdpasim->iommu_lock);
|
|
|
|
|
|
|
|
for (i = 0; i < vdpasim->dev_attr.nvqs; i++) {
|
2020-12-15 22:42:46 +08:00
|
|
|
vdpasim_vq_reset(vdpasim, &vdpasim->vqs[i]);
|
2022-03-31 02:03:59 +08:00
|
|
|
vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
|
|
|
|
&vdpasim->iommu_lock);
|
|
|
|
}
|
|
|
|
|
2022-12-23 14:00:21 +08:00
|
|
|
for (i = 0; i < vdpasim->dev_attr.nas; i++) {
|
2022-03-31 02:03:59 +08:00
|
|
|
vhost_iotlb_reset(&vdpasim->iommu[i]);
|
2022-12-23 14:00:21 +08:00
|
|
|
vhost_iotlb_add_range(&vdpasim->iommu[i], 0, ULONG_MAX,
|
|
|
|
0, VHOST_MAP_RW);
|
|
|
|
vdpasim->iommu_pt[i] = true;
|
|
|
|
}
|
2020-03-26 22:01:24 +08:00
|
|
|
|
2022-08-11 01:15:12 +08:00
|
|
|
vdpasim->running = true;
|
2020-07-31 15:38:22 +08:00
|
|
|
spin_unlock(&vdpasim->iommu_lock);
|
2020-03-26 22:01:24 +08:00
|
|
|
|
|
|
|
vdpasim->features = 0;
|
|
|
|
vdpasim->status = 0;
|
|
|
|
++vdpasim->generation;
|
|
|
|
}
|
|
|
|
|
2020-12-15 22:42:43 +08:00
|
|
|
static const struct vdpa_config_ops vdpasim_config_ops;
|
|
|
|
static const struct vdpa_config_ops vdpasim_batch_config_ops;
|
2020-03-26 22:01:24 +08:00
|
|
|
|
2022-09-27 15:48:09 +08:00
|
|
|
struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,
|
|
|
|
const struct vdpa_dev_set_config *config)
|
2020-03-26 22:01:24 +08:00
|
|
|
{
|
2020-08-05 00:20:41 +08:00
|
|
|
const struct vdpa_config_ops *ops;
|
2022-12-23 13:55:45 +08:00
|
|
|
struct vdpa_device *vdpa;
|
2020-03-26 22:01:24 +08:00
|
|
|
struct vdpasim *vdpasim;
|
|
|
|
struct device *dev;
|
2020-12-15 22:42:41 +08:00
|
|
|
int i, ret = -ENOMEM;
|
2020-03-26 22:01:24 +08:00
|
|
|
|
2022-12-23 13:55:46 +08:00
|
|
|
if (!dev_attr->alloc_size)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2022-09-27 15:48:09 +08:00
|
|
|
if (config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
|
|
|
|
if (config->device_features &
|
|
|
|
~dev_attr->supported_features)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
dev_attr->supported_features =
|
|
|
|
config->device_features;
|
|
|
|
}
|
|
|
|
|
2020-08-05 00:20:41 +08:00
|
|
|
if (batch_mapping)
|
2020-12-15 22:42:43 +08:00
|
|
|
ops = &vdpasim_batch_config_ops;
|
2020-08-05 00:20:41 +08:00
|
|
|
else
|
2020-12-15 22:42:43 +08:00
|
|
|
ops = &vdpasim_config_ops;
|
2020-08-05 00:20:41 +08:00
|
|
|
|
2022-12-23 13:55:45 +08:00
|
|
|
vdpa = __vdpa_alloc_device(NULL, ops,
|
|
|
|
dev_attr->ngroups, dev_attr->nas,
|
2022-12-23 13:55:46 +08:00
|
|
|
dev_attr->alloc_size,
|
2022-12-23 13:55:45 +08:00
|
|
|
dev_attr->name, false);
|
|
|
|
if (IS_ERR(vdpa)) {
|
|
|
|
ret = PTR_ERR(vdpa);
|
2020-03-26 22:01:24 +08:00
|
|
|
goto err_alloc;
|
2021-07-15 16:00:23 +08:00
|
|
|
}
|
2020-03-26 22:01:24 +08:00
|
|
|
|
2022-12-23 13:55:45 +08:00
|
|
|
vdpasim = vdpa_to_sim(vdpa);
|
2020-12-15 22:42:44 +08:00
|
|
|
vdpasim->dev_attr = *dev_attr;
|
2020-12-15 22:42:47 +08:00
|
|
|
INIT_WORK(&vdpasim->work, dev_attr->work_fn);
|
2020-03-26 22:01:24 +08:00
|
|
|
spin_lock_init(&vdpasim->lock);
|
2020-08-10 20:44:43 +08:00
|
|
|
spin_lock_init(&vdpasim->iommu_lock);
|
2020-03-26 22:01:24 +08:00
|
|
|
|
|
|
|
dev = &vdpasim->vdpa.dev;
|
2020-10-28 01:59:14 +08:00
|
|
|
dev->dma_mask = &dev->coherent_dma_mask;
|
|
|
|
if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
|
|
|
|
goto err_iommu;
|
2021-01-05 18:32:03 +08:00
|
|
|
vdpasim->vdpa.mdev = dev_attr->mgmt_dev;
|
2020-03-26 22:01:24 +08:00
|
|
|
|
2020-12-15 22:42:49 +08:00
|
|
|
vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL);
|
|
|
|
if (!vdpasim->config)
|
|
|
|
goto err_iommu;
|
|
|
|
|
2020-12-15 22:42:44 +08:00
|
|
|
vdpasim->vqs = kcalloc(dev_attr->nvqs, sizeof(struct vdpasim_virtqueue),
|
2020-12-15 22:42:41 +08:00
|
|
|
GFP_KERNEL);
|
|
|
|
if (!vdpasim->vqs)
|
|
|
|
goto err_iommu;
|
|
|
|
|
2022-03-31 02:03:59 +08:00
|
|
|
vdpasim->iommu = kmalloc_array(vdpasim->dev_attr.nas,
|
|
|
|
sizeof(*vdpasim->iommu), GFP_KERNEL);
|
2020-03-26 22:01:24 +08:00
|
|
|
if (!vdpasim->iommu)
|
|
|
|
goto err_iommu;
|
|
|
|
|
2022-12-23 14:00:21 +08:00
|
|
|
vdpasim->iommu_pt = kmalloc_array(vdpasim->dev_attr.nas,
|
|
|
|
sizeof(*vdpasim->iommu_pt), GFP_KERNEL);
|
|
|
|
if (!vdpasim->iommu_pt)
|
|
|
|
goto err_iommu;
|
|
|
|
|
2022-03-31 02:03:59 +08:00
|
|
|
for (i = 0; i < vdpasim->dev_attr.nas; i++)
|
2022-06-21 23:12:08 +08:00
|
|
|
vhost_iotlb_init(&vdpasim->iommu[i], max_iotlb_entries, 0);
|
2022-03-31 02:03:59 +08:00
|
|
|
|
2020-12-15 22:42:54 +08:00
|
|
|
vdpasim->buffer = kvmalloc(dev_attr->buffer_size, GFP_KERNEL);
|
2020-03-26 22:01:24 +08:00
|
|
|
if (!vdpasim->buffer)
|
|
|
|
goto err_iommu;
|
|
|
|
|
2020-12-15 22:42:44 +08:00
|
|
|
for (i = 0; i < dev_attr->nvqs; i++)
|
2022-03-31 02:03:59 +08:00
|
|
|
vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
|
2021-03-16 00:34:38 +08:00
|
|
|
&vdpasim->iommu_lock);
|
2020-03-26 22:01:24 +08:00
|
|
|
|
|
|
|
vdpasim->vdpa.dma_dev = dev;
|
|
|
|
|
|
|
|
return vdpasim;
|
|
|
|
|
|
|
|
err_iommu:
|
|
|
|
put_device(dev);
|
|
|
|
err_alloc:
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
2020-12-15 22:42:56 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vdpasim_create);
|
2020-03-26 22:01:24 +08:00
|
|
|
|
|
|
|
static int vdpasim_set_vq_address(struct vdpa_device *vdpa, u16 idx,
|
|
|
|
u64 desc_area, u64 driver_area,
|
|
|
|
u64 device_area)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
|
|
|
|
|
|
|
|
vq->desc_addr = desc_area;
|
|
|
|
vq->driver_addr = driver_area;
|
|
|
|
vq->device_addr = device_area;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vdpasim_set_vq_num(struct vdpa_device *vdpa, u16 idx, u32 num)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
|
|
|
|
|
|
|
|
vq->num = num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
|
|
|
|
|
2023-01-03 18:51:08 +08:00
|
|
|
if (!vdpasim->running &&
|
|
|
|
(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
|
|
|
|
vdpasim->pending_kick = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:01:24 +08:00
|
|
|
if (vq->ready)
|
|
|
|
schedule_work(&vdpasim->work);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vdpasim_set_vq_cb(struct vdpa_device *vdpa, u16 idx,
|
|
|
|
struct vdpa_callback *cb)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
|
|
|
|
|
|
|
|
vq->cb = cb->callback;
|
|
|
|
vq->private = cb->private;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
|
2022-05-19 22:59:19 +08:00
|
|
|
bool old_ready;
|
2020-03-26 22:01:24 +08:00
|
|
|
|
|
|
|
spin_lock(&vdpasim->lock);
|
2022-05-19 22:59:19 +08:00
|
|
|
old_ready = vq->ready;
|
2020-03-26 22:01:24 +08:00
|
|
|
vq->ready = ready;
|
2022-05-19 22:59:19 +08:00
|
|
|
if (vq->ready && !old_ready) {
|
2020-03-26 22:01:24 +08:00
|
|
|
vdpasim_queue_ready(vdpasim, idx);
|
2022-05-19 22:59:19 +08:00
|
|
|
}
|
2020-03-26 22:01:24 +08:00
|
|
|
spin_unlock(&vdpasim->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
|
|
|
|
|
|
|
|
return vq->ready;
|
|
|
|
}
|
|
|
|
|
2020-08-05 00:20:43 +08:00
|
|
|
static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx,
|
|
|
|
const struct vdpa_vq_state *state)
|
2020-03-26 22:01:24 +08:00
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
|
|
|
|
struct vringh *vrh = &vq->vring;
|
|
|
|
|
|
|
|
spin_lock(&vdpasim->lock);
|
2021-06-02 10:15:33 +08:00
|
|
|
vrh->last_avail_idx = state->split.avail_index;
|
2020-03-26 22:01:24 +08:00
|
|
|
spin_unlock(&vdpasim->lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-05 00:20:44 +08:00
|
|
|
static int vdpasim_get_vq_state(struct vdpa_device *vdpa, u16 idx,
|
|
|
|
struct vdpa_vq_state *state)
|
2020-03-26 22:01:24 +08:00
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
|
|
|
|
struct vringh *vrh = &vq->vring;
|
|
|
|
|
2021-06-02 10:15:33 +08:00
|
|
|
state->split.avail_index = vrh->last_avail_idx;
|
2020-08-05 00:20:44 +08:00
|
|
|
return 0;
|
2020-03-26 22:01:24 +08:00
|
|
|
}
|
|
|
|
|
2022-12-23 13:55:47 +08:00
|
|
|
static int vdpasim_get_vq_stats(struct vdpa_device *vdpa, u16 idx,
|
|
|
|
struct sk_buff *msg,
|
|
|
|
struct netlink_ext_ack *extack)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
|
|
|
|
if (vdpasim->dev_attr.get_stats)
|
|
|
|
return vdpasim->dev_attr.get_stats(vdpasim, idx,
|
|
|
|
msg, extack);
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2020-04-10 04:26:21 +08:00
|
|
|
static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
|
2020-03-26 22:01:24 +08:00
|
|
|
{
|
|
|
|
return VDPASIM_QUEUE_ALIGN;
|
|
|
|
}
|
|
|
|
|
2022-03-31 02:03:45 +08:00
|
|
|
static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
|
|
|
|
{
|
2022-03-31 02:03:59 +08:00
|
|
|
/* RX and TX belongs to group 0, CVQ belongs to group 1 */
|
|
|
|
if (idx == 2)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2022-03-31 02:03:45 +08:00
|
|
|
}
|
|
|
|
|
2022-01-05 19:46:33 +08:00
|
|
|
static u64 vdpasim_get_device_features(struct vdpa_device *vdpa)
|
2020-03-26 22:01:24 +08:00
|
|
|
{
|
2020-12-15 22:42:46 +08:00
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
|
|
|
|
return vdpasim->dev_attr.supported_features;
|
2020-03-26 22:01:24 +08:00
|
|
|
}
|
|
|
|
|
2022-01-05 19:46:33 +08:00
|
|
|
static int vdpasim_set_driver_features(struct vdpa_device *vdpa, u64 features)
|
2020-03-26 22:01:24 +08:00
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
|
|
|
|
/* DMA mapping must be done by driver */
|
2020-06-25 06:24:33 +08:00
|
|
|
if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
|
2020-03-26 22:01:24 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2020-12-15 22:42:46 +08:00
|
|
|
vdpasim->features = features & vdpasim->dev_attr.supported_features;
|
2020-03-26 22:01:24 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-05 19:46:33 +08:00
|
|
|
static u64 vdpasim_get_driver_features(struct vdpa_device *vdpa)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
|
|
|
|
return vdpasim->features;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:01:24 +08:00
|
|
|
static void vdpasim_set_config_cb(struct vdpa_device *vdpa,
|
|
|
|
struct vdpa_callback *cb)
|
|
|
|
{
|
|
|
|
/* We don't support config interrupt */
|
|
|
|
}
|
|
|
|
|
|
|
|
static u16 vdpasim_get_vq_num_max(struct vdpa_device *vdpa)
|
|
|
|
{
|
|
|
|
return VDPASIM_QUEUE_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 vdpasim_get_device_id(struct vdpa_device *vdpa)
|
|
|
|
{
|
2020-12-15 22:42:45 +08:00
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
|
|
|
|
return vdpasim->dev_attr.id;
|
2020-03-26 22:01:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static u32 vdpasim_get_vendor_id(struct vdpa_device *vdpa)
|
|
|
|
{
|
|
|
|
return VDPASIM_VENDOR_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u8 vdpasim_get_status(struct vdpa_device *vdpa)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
u8 status;
|
|
|
|
|
|
|
|
spin_lock(&vdpasim->lock);
|
|
|
|
status = vdpasim->status;
|
|
|
|
spin_unlock(&vdpasim->lock);
|
|
|
|
|
2020-04-02 10:49:16 +08:00
|
|
|
return status;
|
2020-03-26 22:01:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
|
|
|
|
spin_lock(&vdpasim->lock);
|
|
|
|
vdpasim->status = status;
|
|
|
|
spin_unlock(&vdpasim->lock);
|
|
|
|
}
|
|
|
|
|
2021-08-31 18:36:26 +08:00
|
|
|
static int vdpasim_reset(struct vdpa_device *vdpa)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
|
|
|
|
spin_lock(&vdpasim->lock);
|
|
|
|
vdpasim->status = 0;
|
|
|
|
vdpasim_do_reset(vdpasim);
|
|
|
|
spin_unlock(&vdpasim->lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-11 01:15:12 +08:00
|
|
|
static int vdpasim_suspend(struct vdpa_device *vdpa)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
|
|
|
|
spin_lock(&vdpasim->lock);
|
|
|
|
vdpasim->running = false;
|
|
|
|
spin_unlock(&vdpasim->lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-03 18:51:08 +08:00
|
|
|
static int vdpasim_resume(struct vdpa_device *vdpa)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
spin_lock(&vdpasim->lock);
|
|
|
|
vdpasim->running = true;
|
|
|
|
|
|
|
|
if (vdpasim->pending_kick) {
|
|
|
|
/* Process pending descriptors */
|
|
|
|
for (i = 0; i < vdpasim->dev_attr.nvqs; ++i)
|
|
|
|
vdpasim_kick_vq(vdpa, i);
|
|
|
|
|
|
|
|
vdpasim->pending_kick = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(&vdpasim->lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-16 00:34:44 +08:00
|
|
|
static size_t vdpasim_get_config_size(struct vdpa_device *vdpa)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
|
|
|
|
return vdpasim->dev_attr.config_size;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:01:24 +08:00
|
|
|
static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
|
|
|
|
void *buf, unsigned int len)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
|
2020-12-15 22:42:50 +08:00
|
|
|
if (offset + len > vdpasim->dev_attr.config_size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (vdpasim->dev_attr.get_config)
|
|
|
|
vdpasim->dev_attr.get_config(vdpasim, vdpasim->config);
|
|
|
|
|
|
|
|
memcpy(buf, vdpasim->config + offset, len);
|
2020-03-26 22:01:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
|
|
|
|
const void *buf, unsigned int len)
|
|
|
|
{
|
2020-12-15 22:42:51 +08:00
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
|
|
|
|
if (offset + len > vdpasim->dev_attr.config_size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(vdpasim->config + offset, buf, len);
|
|
|
|
|
|
|
|
if (vdpasim->dev_attr.set_config)
|
|
|
|
vdpasim->dev_attr.set_config(vdpasim, vdpasim->config);
|
2020-03-26 22:01:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
|
|
|
|
return vdpasim->generation;
|
|
|
|
}
|
|
|
|
|
2020-10-23 17:00:43 +08:00
|
|
|
static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
|
|
|
|
{
|
|
|
|
struct vdpa_iova_range range = {
|
|
|
|
.first = 0ULL,
|
|
|
|
.last = ULLONG_MAX,
|
|
|
|
};
|
|
|
|
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
2022-03-31 02:03:59 +08:00
|
|
|
static int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
|
|
|
|
unsigned int asid)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
struct vhost_iotlb *iommu;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (group > vdpasim->dev_attr.ngroups)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2022-05-23 16:30:57 +08:00
|
|
|
if (asid >= vdpasim->dev_attr.nas)
|
2022-03-31 02:03:59 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
iommu = &vdpasim->iommu[asid];
|
|
|
|
|
|
|
|
spin_lock(&vdpasim->lock);
|
|
|
|
|
|
|
|
for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
|
|
|
|
if (vdpasim_get_vq_group(vdpa, i) == group)
|
|
|
|
vringh_set_iotlb(&vdpasim->vqs[i].vring, iommu,
|
|
|
|
&vdpasim->iommu_lock);
|
|
|
|
|
|
|
|
spin_unlock(&vdpasim->lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-31 02:03:46 +08:00
|
|
|
static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
|
2020-03-26 22:01:24 +08:00
|
|
|
struct vhost_iotlb *iotlb)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
struct vhost_iotlb_map *map;
|
2022-03-31 02:03:59 +08:00
|
|
|
struct vhost_iotlb *iommu;
|
2020-03-26 22:01:24 +08:00
|
|
|
u64 start = 0ULL, last = 0ULL - 1;
|
|
|
|
int ret;
|
|
|
|
|
2022-03-31 02:03:59 +08:00
|
|
|
if (asid >= vdpasim->dev_attr.nas)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-07-31 15:38:22 +08:00
|
|
|
spin_lock(&vdpasim->iommu_lock);
|
2022-03-31 02:03:59 +08:00
|
|
|
|
|
|
|
iommu = &vdpasim->iommu[asid];
|
|
|
|
vhost_iotlb_reset(iommu);
|
2022-12-23 14:00:21 +08:00
|
|
|
vdpasim->iommu_pt[asid] = false;
|
2020-03-26 22:01:24 +08:00
|
|
|
|
|
|
|
for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
|
|
|
|
map = vhost_iotlb_itree_next(map, start, last)) {
|
2022-03-31 02:03:59 +08:00
|
|
|
ret = vhost_iotlb_add_range(iommu, map->start,
|
2020-03-26 22:01:24 +08:00
|
|
|
map->last, map->addr, map->perm);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
}
|
2020-07-31 15:38:22 +08:00
|
|
|
spin_unlock(&vdpasim->iommu_lock);
|
2020-03-26 22:01:24 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
2022-03-31 02:03:59 +08:00
|
|
|
vhost_iotlb_reset(iommu);
|
2020-07-31 15:38:22 +08:00
|
|
|
spin_unlock(&vdpasim->iommu_lock);
|
2020-03-26 22:01:24 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-31 02:03:46 +08:00
|
|
|
static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
|
|
|
|
u64 iova, u64 size,
|
2021-08-31 18:36:29 +08:00
|
|
|
u64 pa, u32 perm, void *opaque)
|
2020-03-26 22:01:24 +08:00
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
2020-07-31 15:38:22 +08:00
|
|
|
int ret;
|
2020-03-26 22:01:24 +08:00
|
|
|
|
2022-03-31 02:03:59 +08:00
|
|
|
if (asid >= vdpasim->dev_attr.nas)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-07-31 15:38:22 +08:00
|
|
|
spin_lock(&vdpasim->iommu_lock);
|
2022-12-23 14:00:21 +08:00
|
|
|
if (vdpasim->iommu_pt[asid]) {
|
|
|
|
vhost_iotlb_reset(&vdpasim->iommu[asid]);
|
|
|
|
vdpasim->iommu_pt[asid] = false;
|
|
|
|
}
|
2022-03-31 02:03:59 +08:00
|
|
|
ret = vhost_iotlb_add_range_ctx(&vdpasim->iommu[asid], iova,
|
|
|
|
iova + size - 1, pa, perm, opaque);
|
2020-07-31 15:38:22 +08:00
|
|
|
spin_unlock(&vdpasim->iommu_lock);
|
|
|
|
|
|
|
|
return ret;
|
2020-03-26 22:01:24 +08:00
|
|
|
}
|
|
|
|
|
2022-03-31 02:03:46 +08:00
|
|
|
static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
|
|
|
|
u64 iova, u64 size)
|
2020-03-26 22:01:24 +08:00
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
|
|
|
|
2022-03-31 02:03:59 +08:00
|
|
|
if (asid >= vdpasim->dev_attr.nas)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2022-12-23 14:00:21 +08:00
|
|
|
if (vdpasim->iommu_pt[asid]) {
|
|
|
|
vhost_iotlb_reset(&vdpasim->iommu[asid]);
|
|
|
|
vdpasim->iommu_pt[asid] = false;
|
|
|
|
}
|
|
|
|
|
2020-07-31 15:38:22 +08:00
|
|
|
spin_lock(&vdpasim->iommu_lock);
|
2022-03-31 02:03:59 +08:00
|
|
|
vhost_iotlb_del_range(&vdpasim->iommu[asid], iova, iova + size - 1);
|
2020-07-31 15:38:22 +08:00
|
|
|
spin_unlock(&vdpasim->iommu_lock);
|
2020-03-26 22:01:24 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vdpasim_free(struct vdpa_device *vdpa)
|
|
|
|
{
|
|
|
|
struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
|
2021-03-16 00:34:43 +08:00
|
|
|
int i;
|
2020-03-26 22:01:24 +08:00
|
|
|
|
|
|
|
cancel_work_sync(&vdpasim->work);
|
2021-03-16 00:34:43 +08:00
|
|
|
|
|
|
|
for (i = 0; i < vdpasim->dev_attr.nvqs; i++) {
|
|
|
|
vringh_kiov_cleanup(&vdpasim->vqs[i].out_iov);
|
|
|
|
vringh_kiov_cleanup(&vdpasim->vqs[i].in_iov);
|
|
|
|
}
|
|
|
|
|
2020-12-15 22:42:53 +08:00
|
|
|
kvfree(vdpasim->buffer);
|
2022-12-13 17:07:17 +08:00
|
|
|
for (i = 0; i < vdpasim->dev_attr.nas; i++)
|
|
|
|
vhost_iotlb_reset(&vdpasim->iommu[i]);
|
|
|
|
kfree(vdpasim->iommu);
|
2022-12-23 14:00:21 +08:00
|
|
|
kfree(vdpasim->iommu_pt);
|
2020-12-15 22:42:41 +08:00
|
|
|
kfree(vdpasim->vqs);
|
2020-12-15 22:42:49 +08:00
|
|
|
kfree(vdpasim->config);
|
2020-03-26 22:01:24 +08:00
|
|
|
}
|
|
|
|
|
2020-12-15 22:42:43 +08:00
|
|
|
static const struct vdpa_config_ops vdpasim_config_ops = {
|
2020-03-26 22:01:24 +08:00
|
|
|
.set_vq_address = vdpasim_set_vq_address,
|
|
|
|
.set_vq_num = vdpasim_set_vq_num,
|
|
|
|
.kick_vq = vdpasim_kick_vq,
|
|
|
|
.set_vq_cb = vdpasim_set_vq_cb,
|
|
|
|
.set_vq_ready = vdpasim_set_vq_ready,
|
|
|
|
.get_vq_ready = vdpasim_get_vq_ready,
|
|
|
|
.set_vq_state = vdpasim_set_vq_state,
|
2022-12-23 13:55:47 +08:00
|
|
|
.get_vendor_vq_stats = vdpasim_get_vq_stats,
|
2020-03-26 22:01:24 +08:00
|
|
|
.get_vq_state = vdpasim_get_vq_state,
|
|
|
|
.get_vq_align = vdpasim_get_vq_align,
|
2022-03-31 02:03:45 +08:00
|
|
|
.get_vq_group = vdpasim_get_vq_group,
|
2022-01-05 19:46:33 +08:00
|
|
|
.get_device_features = vdpasim_get_device_features,
|
|
|
|
.set_driver_features = vdpasim_set_driver_features,
|
|
|
|
.get_driver_features = vdpasim_get_driver_features,
|
2020-03-26 22:01:24 +08:00
|
|
|
.set_config_cb = vdpasim_set_config_cb,
|
|
|
|
.get_vq_num_max = vdpasim_get_vq_num_max,
|
|
|
|
.get_device_id = vdpasim_get_device_id,
|
|
|
|
.get_vendor_id = vdpasim_get_vendor_id,
|
|
|
|
.get_status = vdpasim_get_status,
|
|
|
|
.set_status = vdpasim_set_status,
|
2021-08-31 18:36:26 +08:00
|
|
|
.reset = vdpasim_reset,
|
2022-08-11 01:15:12 +08:00
|
|
|
.suspend = vdpasim_suspend,
|
2023-01-03 18:51:08 +08:00
|
|
|
.resume = vdpasim_resume,
|
2021-03-16 00:34:44 +08:00
|
|
|
.get_config_size = vdpasim_get_config_size,
|
2020-03-26 22:01:24 +08:00
|
|
|
.get_config = vdpasim_get_config,
|
|
|
|
.set_config = vdpasim_set_config,
|
|
|
|
.get_generation = vdpasim_get_generation,
|
2020-10-23 17:00:43 +08:00
|
|
|
.get_iova_range = vdpasim_get_iova_range,
|
2022-03-31 02:03:59 +08:00
|
|
|
.set_group_asid = vdpasim_set_group_asid,
|
2020-03-26 22:01:24 +08:00
|
|
|
.dma_map = vdpasim_dma_map,
|
|
|
|
.dma_unmap = vdpasim_dma_unmap,
|
|
|
|
.free = vdpasim_free,
|
|
|
|
};
|
|
|
|
|
2020-12-15 22:42:43 +08:00
|
|
|
static const struct vdpa_config_ops vdpasim_batch_config_ops = {
|
2020-08-05 00:20:41 +08:00
|
|
|
.set_vq_address = vdpasim_set_vq_address,
|
|
|
|
.set_vq_num = vdpasim_set_vq_num,
|
|
|
|
.kick_vq = vdpasim_kick_vq,
|
|
|
|
.set_vq_cb = vdpasim_set_vq_cb,
|
|
|
|
.set_vq_ready = vdpasim_set_vq_ready,
|
|
|
|
.get_vq_ready = vdpasim_get_vq_ready,
|
|
|
|
.set_vq_state = vdpasim_set_vq_state,
|
2022-12-23 13:55:47 +08:00
|
|
|
.get_vendor_vq_stats = vdpasim_get_vq_stats,
|
2020-08-05 00:20:41 +08:00
|
|
|
.get_vq_state = vdpasim_get_vq_state,
|
|
|
|
.get_vq_align = vdpasim_get_vq_align,
|
2022-03-31 02:03:45 +08:00
|
|
|
.get_vq_group = vdpasim_get_vq_group,
|
2022-01-05 19:46:33 +08:00
|
|
|
.get_device_features = vdpasim_get_device_features,
|
|
|
|
.set_driver_features = vdpasim_set_driver_features,
|
|
|
|
.get_driver_features = vdpasim_get_driver_features,
|
2020-08-05 00:20:41 +08:00
|
|
|
.set_config_cb = vdpasim_set_config_cb,
|
|
|
|
.get_vq_num_max = vdpasim_get_vq_num_max,
|
|
|
|
.get_device_id = vdpasim_get_device_id,
|
|
|
|
.get_vendor_id = vdpasim_get_vendor_id,
|
|
|
|
.get_status = vdpasim_get_status,
|
|
|
|
.set_status = vdpasim_set_status,
|
2021-08-31 18:36:26 +08:00
|
|
|
.reset = vdpasim_reset,
|
2022-08-11 01:15:12 +08:00
|
|
|
.suspend = vdpasim_suspend,
|
2023-01-03 18:51:08 +08:00
|
|
|
.resume = vdpasim_resume,
|
2021-03-16 00:34:44 +08:00
|
|
|
.get_config_size = vdpasim_get_config_size,
|
2020-08-05 00:20:41 +08:00
|
|
|
.get_config = vdpasim_get_config,
|
|
|
|
.set_config = vdpasim_set_config,
|
|
|
|
.get_generation = vdpasim_get_generation,
|
2020-10-23 17:00:43 +08:00
|
|
|
.get_iova_range = vdpasim_get_iova_range,
|
2022-03-31 02:03:59 +08:00
|
|
|
.set_group_asid = vdpasim_set_group_asid,
|
2020-08-05 00:20:41 +08:00
|
|
|
.set_map = vdpasim_set_map,
|
|
|
|
.free = vdpasim_free,
|
|
|
|
};
|
|
|
|
|
2020-03-26 22:01:24 +08:00
|
|
|
MODULE_VERSION(DRV_VERSION);
|
|
|
|
MODULE_LICENSE(DRV_LICENSE);
|
|
|
|
MODULE_AUTHOR(DRV_AUTHOR);
|
|
|
|
MODULE_DESCRIPTION(DRV_DESC);
|