1 // SPDX-License-Identifier: GPL-2.0
3 * virtio_pmem.c: Virtio pmem Driver
5 * Discovers persistent memory range information
6 * from host and registers the virtual pmem device
9 #include "virtio_pmem.h"
12 static struct virtio_device_id id_table
[] = {
13 { VIRTIO_ID_PMEM
, VIRTIO_DEV_ANY_ID
},
17 /* Initialize virt queue */
18 static int init_vq(struct virtio_pmem
*vpmem
)
21 vpmem
->req_vq
= virtio_find_single_vq(vpmem
->vdev
,
22 virtio_pmem_host_ack
, "flush_queue");
23 if (IS_ERR(vpmem
->req_vq
))
24 return PTR_ERR(vpmem
->req_vq
);
26 spin_lock_init(&vpmem
->pmem_lock
);
27 INIT_LIST_HEAD(&vpmem
->req_list
);
32 static int virtio_pmem_validate(struct virtio_device
*vdev
)
34 struct virtio_shm_region shm_reg
;
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
)
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
);
46 static int virtio_pmem_probe(struct virtio_device
*vdev
)
48 struct nd_region_desc ndr_desc
= {};
49 struct nd_region
*nd_region
;
50 struct virtio_pmem
*vpmem
;
52 struct virtio_shm_region shm_reg
;
55 if (!vdev
->config
->get
) {
56 dev_err(&vdev
->dev
, "%s failure: config access disabled\n",
61 vpmem
= devm_kzalloc(&vdev
->dev
, sizeof(*vpmem
), GFP_KERNEL
);
71 dev_err(&vdev
->dev
, "failed to initialize virtio pmem vq's\n");
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
;
80 virtio_cread_le(vpmem
->vdev
, struct virtio_pmem_config
,
81 start
, &vpmem
->start
);
82 virtio_cread_le(vpmem
->vdev
, struct virtio_pmem_config
,
86 res
.start
= vpmem
->start
;
87 res
.end
= vpmem
->start
+ vpmem
->size
- 1;
88 vpmem
->nd_desc
.provider_name
= "virtio-pmem";
89 vpmem
->nd_desc
.module
= THIS_MODULE
;
91 vpmem
->nvdimm_bus
= nvdimm_bus_register(&vdev
->dev
,
93 if (!vpmem
->nvdimm_bus
) {
94 dev_err(&vdev
->dev
, "failed to register device with nvdimm_bus\n");
99 dev_set_drvdata(&vdev
->dev
, vpmem
->nvdimm_bus
);
103 ndr_desc
.numa_node
= memory_add_physaddr_to_nid(res
.start
);
104 ndr_desc
.target_node
= phys_to_target_node(res
.start
);
105 if (ndr_desc
.target_node
== NUMA_NO_NODE
) {
106 ndr_desc
.target_node
= ndr_desc
.numa_node
;
107 dev_dbg(&vdev
->dev
, "changing target node from %d to %d",
108 NUMA_NO_NODE
, ndr_desc
.target_node
);
111 ndr_desc
.flush
= async_pmem_flush
;
112 ndr_desc
.provider_data
= vdev
;
113 set_bit(ND_REGION_PAGEMAP
, &ndr_desc
.flags
);
114 set_bit(ND_REGION_ASYNC
, &ndr_desc
.flags
);
116 * The NVDIMM region could be available before the
117 * virtio_device_ready() that is called by
118 * virtio_dev_probe(), so we set device ready here.
120 virtio_device_ready(vdev
);
121 nd_region
= nvdimm_pmem_region_create(vpmem
->nvdimm_bus
, &ndr_desc
);
123 dev_err(&vdev
->dev
, "failed to create nvdimm region\n");
129 virtio_reset_device(vdev
);
130 nvdimm_bus_unregister(vpmem
->nvdimm_bus
);
132 vdev
->config
->del_vqs(vdev
);
137 static void virtio_pmem_remove(struct virtio_device
*vdev
)
139 struct nvdimm_bus
*nvdimm_bus
= dev_get_drvdata(&vdev
->dev
);
141 nvdimm_bus_unregister(nvdimm_bus
);
142 vdev
->config
->del_vqs(vdev
);
143 virtio_reset_device(vdev
);
146 static int virtio_pmem_freeze(struct virtio_device
*vdev
)
148 vdev
->config
->del_vqs(vdev
);
149 virtio_reset_device(vdev
);
154 static int virtio_pmem_restore(struct virtio_device
*vdev
)
158 ret
= init_vq(vdev
->priv
);
160 dev_err(&vdev
->dev
, "failed to initialize virtio pmem's vq\n");
163 virtio_device_ready(vdev
);
168 static unsigned int features
[] = {
169 VIRTIO_PMEM_F_SHMEM_REGION
,
172 static struct virtio_driver virtio_pmem_driver
= {
173 .feature_table
= features
,
174 .feature_table_size
= ARRAY_SIZE(features
),
175 .driver
.name
= KBUILD_MODNAME
,
176 .id_table
= id_table
,
177 .validate
= virtio_pmem_validate
,
178 .probe
= virtio_pmem_probe
,
179 .remove
= virtio_pmem_remove
,
180 .freeze
= virtio_pmem_freeze
,
181 .restore
= virtio_pmem_restore
,
184 module_virtio_driver(virtio_pmem_driver
);
185 MODULE_DEVICE_TABLE(virtio
, id_table
);
186 MODULE_DESCRIPTION("Virtio pmem driver");
187 MODULE_LICENSE("GPL");