Skip to content

Commit 35967bd

Browse files
Lencerfmstsirkin
authored andcommitted
virtio_pmem: support feature SHMEM_REGION
This patch adds the support for feature VIRTIO_PMEM_F_SHMEM_REGION (virtio spec v1.2 section 5.19.5.2 [1]). During feature negotiation, if VIRTIO_PMEM_F_SHMEM_REGION is offered by the device, the driver looks for a shared memory region of id 0. If it is found, this feature is understood. Otherwise, this feature bit is cleared. During probe, if VIRTIO_PMEM_F_SHMEM_REGION has been negotiated, virtio pmem ignores the `start` and `size` fields in device config and uses the physical address range of shared memory region 0. [1] https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-6480002 Signed-off-by: Changyuan Lyu <changyuanl@google.com> Message-Id: <20231220204906.566922-1-changyuanl@google.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Acked-by: Jason Wang <jasowang@redhat.com>
1 parent b12fbc3 commit 35967bd

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

drivers/nvdimm/virtio_pmem.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,27 @@ static int init_vq(struct virtio_pmem *vpmem)
2929
return 0;
3030
};
3131

32+
static int virtio_pmem_validate(struct virtio_device *vdev)
33+
{
34+
struct virtio_shm_region shm_reg;
35+
36+
if (virtio_has_feature(vdev, VIRTIO_PMEM_F_SHMEM_REGION) &&
37+
!virtio_get_shm_region(vdev, &shm_reg, (u8)VIRTIO_PMEM_SHMEM_REGION_ID)
38+
) {
39+
dev_notice(&vdev->dev, "failed to get shared memory region %d\n",
40+
VIRTIO_PMEM_SHMEM_REGION_ID);
41+
__virtio_clear_bit(vdev, VIRTIO_PMEM_F_SHMEM_REGION);
42+
}
43+
return 0;
44+
}
45+
3246
static int virtio_pmem_probe(struct virtio_device *vdev)
3347
{
3448
struct nd_region_desc ndr_desc = {};
3549
struct nd_region *nd_region;
3650
struct virtio_pmem *vpmem;
3751
struct resource res;
52+
struct virtio_shm_region shm_reg;
3853
int err = 0;
3954

4055
if (!vdev->config->get) {
@@ -57,10 +72,16 @@ static int virtio_pmem_probe(struct virtio_device *vdev)
5772
goto out_err;
5873
}
5974

60-
virtio_cread_le(vpmem->vdev, struct virtio_pmem_config,
61-
start, &vpmem->start);
62-
virtio_cread_le(vpmem->vdev, struct virtio_pmem_config,
63-
size, &vpmem->size);
75+
if (virtio_has_feature(vdev, VIRTIO_PMEM_F_SHMEM_REGION)) {
76+
virtio_get_shm_region(vdev, &shm_reg, (u8)VIRTIO_PMEM_SHMEM_REGION_ID);
77+
vpmem->start = shm_reg.addr;
78+
vpmem->size = shm_reg.len;
79+
} else {
80+
virtio_cread_le(vpmem->vdev, struct virtio_pmem_config,
81+
start, &vpmem->start);
82+
virtio_cread_le(vpmem->vdev, struct virtio_pmem_config,
83+
size, &vpmem->size);
84+
}
6485

6586
res.start = vpmem->start;
6687
res.end = vpmem->start + vpmem->size - 1;
@@ -122,10 +143,17 @@ static void virtio_pmem_remove(struct virtio_device *vdev)
122143
virtio_reset_device(vdev);
123144
}
124145

146+
static unsigned int features[] = {
147+
VIRTIO_PMEM_F_SHMEM_REGION,
148+
};
149+
125150
static struct virtio_driver virtio_pmem_driver = {
151+
.feature_table = features,
152+
.feature_table_size = ARRAY_SIZE(features),
126153
.driver.name = KBUILD_MODNAME,
127154
.driver.owner = THIS_MODULE,
128155
.id_table = id_table,
156+
.validate = virtio_pmem_validate,
129157
.probe = virtio_pmem_probe,
130158
.remove = virtio_pmem_remove,
131159
};

include/uapi/linux/virtio_pmem.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
#include <linux/virtio_ids.h>
1515
#include <linux/virtio_config.h>
1616

17+
/* Feature bits */
18+
/* guest physical address range will be indicated as shared memory region 0 */
19+
#define VIRTIO_PMEM_F_SHMEM_REGION 0
20+
21+
/* shmid of the shared memory region corresponding to the pmem */
22+
#define VIRTIO_PMEM_SHMEM_REGION_ID 0
23+
1724
struct virtio_pmem_config {
1825
__le64 start;
1926
__le64 size;

0 commit comments

Comments
 (0)