vdpa_sim.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * VDPA device simulator core.
  4. *
  5. * Copyright (c) 2020, Red Hat Inc. All rights reserved.
  6. * Author: Jason Wang <jasowang@redhat.com>
  7. *
  8. */
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. #include <linux/kernel.h>
  13. #include <linux/kthread.h>
  14. #include <linux/slab.h>
  15. #include <linux/dma-map-ops.h>
  16. #include <linux/vringh.h>
  17. #include <linux/vdpa.h>
  18. #include <linux/vhost_iotlb.h>
  19. #include <uapi/linux/vdpa.h>
  20. #include <uapi/linux/vhost_types.h>
  21. #include "vdpa_sim.h"
  22. #define DRV_VERSION "0.1"
  23. #define DRV_AUTHOR "Jason Wang <jasowang@redhat.com>"
  24. #define DRV_DESC "vDPA Device Simulator core"
  25. #define DRV_LICENSE "GPL v2"
  26. static int batch_mapping = 1;
  27. module_param(batch_mapping, int, 0444);
  28. MODULE_PARM_DESC(batch_mapping, "Batched mapping 1 -Enable; 0 - Disable");
  29. static int max_iotlb_entries = 2048;
  30. module_param(max_iotlb_entries, int, 0444);
  31. MODULE_PARM_DESC(max_iotlb_entries,
  32. "Maximum number of iotlb entries for each address space. 0 means unlimited. (default: 2048)");
  33. static bool use_va = true;
  34. module_param(use_va, bool, 0444);
  35. MODULE_PARM_DESC(use_va, "Enable/disable the device's ability to use VA");
  36. #define VDPASIM_QUEUE_ALIGN PAGE_SIZE
  37. #define VDPASIM_QUEUE_MAX 256
  38. #define VDPASIM_VENDOR_ID 0
  39. struct vdpasim_mm_work {
  40. struct kthread_work work;
  41. struct vdpasim *vdpasim;
  42. struct mm_struct *mm_to_bind;
  43. int ret;
  44. };
  45. static void vdpasim_mm_work_fn(struct kthread_work *work)
  46. {
  47. struct vdpasim_mm_work *mm_work =
  48. container_of(work, struct vdpasim_mm_work, work);
  49. struct vdpasim *vdpasim = mm_work->vdpasim;
  50. mm_work->ret = 0;
  51. //TODO: should we attach the cgroup of the mm owner?
  52. vdpasim->mm_bound = mm_work->mm_to_bind;
  53. }
  54. static void vdpasim_worker_change_mm_sync(struct vdpasim *vdpasim,
  55. struct vdpasim_mm_work *mm_work)
  56. {
  57. struct kthread_work *work = &mm_work->work;
  58. kthread_init_work(work, vdpasim_mm_work_fn);
  59. kthread_queue_work(vdpasim->worker, work);
  60. kthread_flush_work(work);
  61. }
  62. static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
  63. {
  64. return container_of(vdpa, struct vdpasim, vdpa);
  65. }
  66. static void vdpasim_vq_notify(struct vringh *vring)
  67. {
  68. struct vdpasim_virtqueue *vq =
  69. container_of(vring, struct vdpasim_virtqueue, vring);
  70. if (!vq->cb)
  71. return;
  72. vq->cb(vq->private);
  73. }
  74. static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx)
  75. {
  76. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  77. uint16_t last_avail_idx = vq->vring.last_avail_idx;
  78. struct vring_desc *desc = (struct vring_desc *)
  79. (uintptr_t)vq->desc_addr;
  80. struct vring_avail *avail = (struct vring_avail *)
  81. (uintptr_t)vq->driver_addr;
  82. struct vring_used *used = (struct vring_used *)
  83. (uintptr_t)vq->device_addr;
  84. if (use_va && vdpasim->mm_bound) {
  85. vringh_init_iotlb_va(&vq->vring, vdpasim->features, vq->num,
  86. true, desc, avail, used);
  87. } else {
  88. vringh_init_iotlb(&vq->vring, vdpasim->features, vq->num,
  89. true, desc, avail, used);
  90. }
  91. vq->vring.last_avail_idx = last_avail_idx;
  92. /*
  93. * Since vdpa_sim does not support receive inflight descriptors as a
  94. * destination of a migration, let's set both avail_idx and used_idx
  95. * the same at vq start. This is how vhost-user works in a
  96. * VHOST_SET_VRING_BASE call.
  97. *
  98. * Although the simple fix is to set last_used_idx at
  99. * vdpasim_set_vq_state, it would be reset at vdpasim_queue_ready.
  100. */
  101. vq->vring.last_used_idx = last_avail_idx;
  102. vq->vring.notify = vdpasim_vq_notify;
  103. }
  104. static void vdpasim_vq_reset(struct vdpasim *vdpasim,
  105. struct vdpasim_virtqueue *vq)
  106. {
  107. vq->ready = false;
  108. vq->desc_addr = 0;
  109. vq->driver_addr = 0;
  110. vq->device_addr = 0;
  111. vq->cb = NULL;
  112. vq->private = NULL;
  113. vringh_init_iotlb(&vq->vring, vdpasim->dev_attr.supported_features,
  114. VDPASIM_QUEUE_MAX, false, NULL, NULL, NULL);
  115. vq->vring.notify = NULL;
  116. }
  117. static void vdpasim_do_reset(struct vdpasim *vdpasim, u32 flags)
  118. {
  119. int i;
  120. spin_lock(&vdpasim->iommu_lock);
  121. for (i = 0; i < vdpasim->dev_attr.nvqs; i++) {
  122. vdpasim_vq_reset(vdpasim, &vdpasim->vqs[i]);
  123. vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
  124. &vdpasim->iommu_lock);
  125. }
  126. if (flags & VDPA_RESET_F_CLEAN_MAP) {
  127. for (i = 0; i < vdpasim->dev_attr.nas; i++) {
  128. vhost_iotlb_reset(&vdpasim->iommu[i]);
  129. vhost_iotlb_add_range(&vdpasim->iommu[i], 0, ULONG_MAX,
  130. 0, VHOST_MAP_RW);
  131. vdpasim->iommu_pt[i] = true;
  132. }
  133. }
  134. vdpasim->running = false;
  135. spin_unlock(&vdpasim->iommu_lock);
  136. vdpasim->features = 0;
  137. vdpasim->status = 0;
  138. ++vdpasim->generation;
  139. }
  140. static const struct vdpa_config_ops vdpasim_config_ops;
  141. static const struct vdpa_config_ops vdpasim_batch_config_ops;
  142. static void vdpasim_work_fn(struct kthread_work *work)
  143. {
  144. struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
  145. struct mm_struct *mm = vdpasim->mm_bound;
  146. if (use_va && mm) {
  147. if (!mmget_not_zero(mm))
  148. return;
  149. kthread_use_mm(mm);
  150. }
  151. vdpasim->dev_attr.work_fn(vdpasim);
  152. if (use_va && mm) {
  153. kthread_unuse_mm(mm);
  154. mmput(mm);
  155. }
  156. }
  157. struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,
  158. const struct vdpa_dev_set_config *config)
  159. {
  160. const struct vdpa_config_ops *ops;
  161. struct vdpa_device *vdpa;
  162. struct vdpasim *vdpasim;
  163. struct device *dev;
  164. int i, ret = -ENOMEM;
  165. if (!dev_attr->alloc_size)
  166. return ERR_PTR(-EINVAL);
  167. if (config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
  168. if (config->device_features &
  169. ~dev_attr->supported_features)
  170. return ERR_PTR(-EINVAL);
  171. dev_attr->supported_features =
  172. config->device_features;
  173. }
  174. if (batch_mapping)
  175. ops = &vdpasim_batch_config_ops;
  176. else
  177. ops = &vdpasim_config_ops;
  178. vdpa = __vdpa_alloc_device(NULL, ops, NULL,
  179. dev_attr->ngroups, dev_attr->nas,
  180. dev_attr->alloc_size,
  181. dev_attr->name, use_va);
  182. if (IS_ERR(vdpa)) {
  183. ret = PTR_ERR(vdpa);
  184. goto err_alloc;
  185. }
  186. vdpasim = vdpa_to_sim(vdpa);
  187. vdpasim->dev_attr = *dev_attr;
  188. dev = &vdpasim->vdpa.dev;
  189. kthread_init_work(&vdpasim->work, vdpasim_work_fn);
  190. vdpasim->worker = kthread_run_worker(0, "vDPA sim worker: %s",
  191. dev_attr->name);
  192. if (IS_ERR(vdpasim->worker))
  193. goto err_iommu;
  194. mutex_init(&vdpasim->mutex);
  195. spin_lock_init(&vdpasim->iommu_lock);
  196. dev->dma_mask = &dev->coherent_dma_mask;
  197. if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
  198. goto err_iommu;
  199. vdpasim->vdpa.mdev = dev_attr->mgmt_dev;
  200. vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL);
  201. if (!vdpasim->config)
  202. goto err_iommu;
  203. vdpasim->vqs = kzalloc_objs(struct vdpasim_virtqueue, dev_attr->nvqs);
  204. if (!vdpasim->vqs)
  205. goto err_iommu;
  206. vdpasim->iommu = kmalloc_objs(*vdpasim->iommu, vdpasim->dev_attr.nas);
  207. if (!vdpasim->iommu)
  208. goto err_iommu;
  209. vdpasim->iommu_pt = kmalloc_objs(*vdpasim->iommu_pt,
  210. vdpasim->dev_attr.nas);
  211. if (!vdpasim->iommu_pt)
  212. goto err_iommu;
  213. for (i = 0; i < vdpasim->dev_attr.nas; i++) {
  214. vhost_iotlb_init(&vdpasim->iommu[i], max_iotlb_entries, 0);
  215. vhost_iotlb_add_range(&vdpasim->iommu[i], 0, ULONG_MAX, 0,
  216. VHOST_MAP_RW);
  217. vdpasim->iommu_pt[i] = true;
  218. }
  219. for (i = 0; i < dev_attr->nvqs; i++)
  220. vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
  221. &vdpasim->iommu_lock);
  222. vdpasim->vdpa.vmap.dma_dev = dev;
  223. return vdpasim;
  224. err_iommu:
  225. put_device(dev);
  226. err_alloc:
  227. return ERR_PTR(ret);
  228. }
  229. EXPORT_SYMBOL_GPL(vdpasim_create);
  230. void vdpasim_schedule_work(struct vdpasim *vdpasim)
  231. {
  232. kthread_queue_work(vdpasim->worker, &vdpasim->work);
  233. }
  234. EXPORT_SYMBOL_GPL(vdpasim_schedule_work);
  235. static int vdpasim_set_vq_address(struct vdpa_device *vdpa, u16 idx,
  236. u64 desc_area, u64 driver_area,
  237. u64 device_area)
  238. {
  239. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  240. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  241. vq->desc_addr = desc_area;
  242. vq->driver_addr = driver_area;
  243. vq->device_addr = device_area;
  244. return 0;
  245. }
  246. static void vdpasim_set_vq_num(struct vdpa_device *vdpa, u16 idx, u32 num)
  247. {
  248. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  249. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  250. vq->num = num;
  251. }
  252. static u16 vdpasim_get_vq_size(struct vdpa_device *vdpa, u16 idx)
  253. {
  254. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  255. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  256. if (vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK)
  257. return vq->num;
  258. else
  259. return VDPASIM_QUEUE_MAX;
  260. }
  261. static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
  262. {
  263. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  264. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  265. if (!vdpasim->running &&
  266. (vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  267. vdpasim->pending_kick = true;
  268. return;
  269. }
  270. if (vq->ready)
  271. vdpasim_schedule_work(vdpasim);
  272. }
  273. static void vdpasim_set_vq_cb(struct vdpa_device *vdpa, u16 idx,
  274. struct vdpa_callback *cb)
  275. {
  276. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  277. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  278. vq->cb = cb->callback;
  279. vq->private = cb->private;
  280. }
  281. static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready)
  282. {
  283. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  284. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  285. bool old_ready;
  286. mutex_lock(&vdpasim->mutex);
  287. old_ready = vq->ready;
  288. vq->ready = ready;
  289. if (vq->ready && !old_ready) {
  290. vdpasim_queue_ready(vdpasim, idx);
  291. }
  292. mutex_unlock(&vdpasim->mutex);
  293. }
  294. static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx)
  295. {
  296. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  297. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  298. return vq->ready;
  299. }
  300. static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx,
  301. const struct vdpa_vq_state *state)
  302. {
  303. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  304. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  305. struct vringh *vrh = &vq->vring;
  306. mutex_lock(&vdpasim->mutex);
  307. vrh->last_avail_idx = state->split.avail_index;
  308. mutex_unlock(&vdpasim->mutex);
  309. return 0;
  310. }
  311. static int vdpasim_get_vq_state(struct vdpa_device *vdpa, u16 idx,
  312. struct vdpa_vq_state *state)
  313. {
  314. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  315. struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
  316. struct vringh *vrh = &vq->vring;
  317. state->split.avail_index = vrh->last_avail_idx;
  318. return 0;
  319. }
  320. static int vdpasim_get_vq_stats(struct vdpa_device *vdpa, u16 idx,
  321. struct sk_buff *msg,
  322. struct netlink_ext_ack *extack)
  323. {
  324. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  325. if (vdpasim->dev_attr.get_stats)
  326. return vdpasim->dev_attr.get_stats(vdpasim, idx,
  327. msg, extack);
  328. return -EOPNOTSUPP;
  329. }
  330. static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
  331. {
  332. return VDPASIM_QUEUE_ALIGN;
  333. }
  334. static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
  335. {
  336. /* RX and TX belongs to group 0, CVQ belongs to group 1 */
  337. if (idx == 2)
  338. return 1;
  339. else
  340. return 0;
  341. }
  342. static u64 vdpasim_get_device_features(struct vdpa_device *vdpa)
  343. {
  344. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  345. return vdpasim->dev_attr.supported_features;
  346. }
  347. static u64 vdpasim_get_backend_features(const struct vdpa_device *vdpa)
  348. {
  349. return BIT_ULL(VHOST_BACKEND_F_ENABLE_AFTER_DRIVER_OK);
  350. }
  351. static int vdpasim_set_driver_features(struct vdpa_device *vdpa, u64 features)
  352. {
  353. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  354. /* DMA mapping must be done by driver */
  355. if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
  356. return -EINVAL;
  357. vdpasim->features = features & vdpasim->dev_attr.supported_features;
  358. return 0;
  359. }
  360. static u64 vdpasim_get_driver_features(struct vdpa_device *vdpa)
  361. {
  362. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  363. return vdpasim->features;
  364. }
  365. static void vdpasim_set_config_cb(struct vdpa_device *vdpa,
  366. struct vdpa_callback *cb)
  367. {
  368. /* We don't support config interrupt */
  369. }
  370. static u16 vdpasim_get_vq_num_max(struct vdpa_device *vdpa)
  371. {
  372. return VDPASIM_QUEUE_MAX;
  373. }
  374. static u32 vdpasim_get_device_id(struct vdpa_device *vdpa)
  375. {
  376. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  377. return vdpasim->dev_attr.id;
  378. }
  379. static u32 vdpasim_get_vendor_id(struct vdpa_device *vdpa)
  380. {
  381. return VDPASIM_VENDOR_ID;
  382. }
  383. static u8 vdpasim_get_status(struct vdpa_device *vdpa)
  384. {
  385. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  386. u8 status;
  387. mutex_lock(&vdpasim->mutex);
  388. status = vdpasim->status;
  389. mutex_unlock(&vdpasim->mutex);
  390. return status;
  391. }
  392. static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status)
  393. {
  394. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  395. mutex_lock(&vdpasim->mutex);
  396. vdpasim->status = status;
  397. vdpasim->running = (status & VIRTIO_CONFIG_S_DRIVER_OK) != 0;
  398. mutex_unlock(&vdpasim->mutex);
  399. }
  400. static int vdpasim_compat_reset(struct vdpa_device *vdpa, u32 flags)
  401. {
  402. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  403. mutex_lock(&vdpasim->mutex);
  404. vdpasim->status = 0;
  405. vdpasim_do_reset(vdpasim, flags);
  406. mutex_unlock(&vdpasim->mutex);
  407. return 0;
  408. }
  409. static int vdpasim_reset(struct vdpa_device *vdpa)
  410. {
  411. return vdpasim_compat_reset(vdpa, 0);
  412. }
  413. static int vdpasim_suspend(struct vdpa_device *vdpa)
  414. {
  415. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  416. mutex_lock(&vdpasim->mutex);
  417. vdpasim->running = false;
  418. mutex_unlock(&vdpasim->mutex);
  419. return 0;
  420. }
  421. static int vdpasim_resume(struct vdpa_device *vdpa)
  422. {
  423. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  424. int i;
  425. mutex_lock(&vdpasim->mutex);
  426. vdpasim->running = true;
  427. if (vdpasim->pending_kick) {
  428. /* Process pending descriptors */
  429. for (i = 0; i < vdpasim->dev_attr.nvqs; ++i)
  430. vdpasim_kick_vq(vdpa, i);
  431. vdpasim->pending_kick = false;
  432. }
  433. mutex_unlock(&vdpasim->mutex);
  434. return 0;
  435. }
  436. static size_t vdpasim_get_config_size(struct vdpa_device *vdpa)
  437. {
  438. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  439. return vdpasim->dev_attr.config_size;
  440. }
  441. static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
  442. void *buf, unsigned int len)
  443. {
  444. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  445. if (offset + len > vdpasim->dev_attr.config_size)
  446. return;
  447. if (vdpasim->dev_attr.get_config)
  448. vdpasim->dev_attr.get_config(vdpasim, vdpasim->config);
  449. memcpy(buf, vdpasim->config + offset, len);
  450. }
  451. static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
  452. const void *buf, unsigned int len)
  453. {
  454. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  455. if (offset + len > vdpasim->dev_attr.config_size)
  456. return;
  457. memcpy(vdpasim->config + offset, buf, len);
  458. if (vdpasim->dev_attr.set_config)
  459. vdpasim->dev_attr.set_config(vdpasim, vdpasim->config);
  460. }
  461. static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
  462. {
  463. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  464. return vdpasim->generation;
  465. }
  466. static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
  467. {
  468. struct vdpa_iova_range range = {
  469. .first = 0ULL,
  470. .last = ULLONG_MAX,
  471. };
  472. return range;
  473. }
  474. static int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
  475. unsigned int asid)
  476. {
  477. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  478. struct vhost_iotlb *iommu;
  479. int i;
  480. iommu = &vdpasim->iommu[asid];
  481. mutex_lock(&vdpasim->mutex);
  482. for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
  483. if (vdpasim_get_vq_group(vdpa, i) == group)
  484. vringh_set_iotlb(&vdpasim->vqs[i].vring, iommu,
  485. &vdpasim->iommu_lock);
  486. mutex_unlock(&vdpasim->mutex);
  487. return 0;
  488. }
  489. static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
  490. struct vhost_iotlb *iotlb)
  491. {
  492. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  493. struct vhost_iotlb_map *map;
  494. struct vhost_iotlb *iommu;
  495. u64 start = 0ULL, last = 0ULL - 1;
  496. int ret;
  497. if (asid >= vdpasim->dev_attr.nas)
  498. return -EINVAL;
  499. spin_lock(&vdpasim->iommu_lock);
  500. iommu = &vdpasim->iommu[asid];
  501. vhost_iotlb_reset(iommu);
  502. vdpasim->iommu_pt[asid] = false;
  503. for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
  504. map = vhost_iotlb_itree_next(map, start, last)) {
  505. ret = vhost_iotlb_add_range(iommu, map->start,
  506. map->last, map->addr, map->perm);
  507. if (ret)
  508. goto err;
  509. }
  510. spin_unlock(&vdpasim->iommu_lock);
  511. return 0;
  512. err:
  513. vhost_iotlb_reset(iommu);
  514. spin_unlock(&vdpasim->iommu_lock);
  515. return ret;
  516. }
  517. static int vdpasim_reset_map(struct vdpa_device *vdpa, unsigned int asid)
  518. {
  519. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  520. if (asid >= vdpasim->dev_attr.nas)
  521. return -EINVAL;
  522. spin_lock(&vdpasim->iommu_lock);
  523. if (vdpasim->iommu_pt[asid])
  524. goto out;
  525. vhost_iotlb_reset(&vdpasim->iommu[asid]);
  526. vhost_iotlb_add_range(&vdpasim->iommu[asid], 0, ULONG_MAX,
  527. 0, VHOST_MAP_RW);
  528. vdpasim->iommu_pt[asid] = true;
  529. out:
  530. spin_unlock(&vdpasim->iommu_lock);
  531. return 0;
  532. }
  533. static int vdpasim_bind_mm(struct vdpa_device *vdpa, struct mm_struct *mm)
  534. {
  535. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  536. struct vdpasim_mm_work mm_work;
  537. mm_work.vdpasim = vdpasim;
  538. mm_work.mm_to_bind = mm;
  539. vdpasim_worker_change_mm_sync(vdpasim, &mm_work);
  540. return mm_work.ret;
  541. }
  542. static void vdpasim_unbind_mm(struct vdpa_device *vdpa)
  543. {
  544. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  545. struct vdpasim_mm_work mm_work;
  546. mm_work.vdpasim = vdpasim;
  547. mm_work.mm_to_bind = NULL;
  548. vdpasim_worker_change_mm_sync(vdpasim, &mm_work);
  549. }
  550. static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
  551. u64 iova, u64 size,
  552. u64 pa, u32 perm, void *opaque)
  553. {
  554. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  555. int ret;
  556. if (asid >= vdpasim->dev_attr.nas)
  557. return -EINVAL;
  558. spin_lock(&vdpasim->iommu_lock);
  559. if (vdpasim->iommu_pt[asid]) {
  560. vhost_iotlb_reset(&vdpasim->iommu[asid]);
  561. vdpasim->iommu_pt[asid] = false;
  562. }
  563. ret = vhost_iotlb_add_range_ctx(&vdpasim->iommu[asid], iova,
  564. iova + size - 1, pa, perm, opaque);
  565. spin_unlock(&vdpasim->iommu_lock);
  566. return ret;
  567. }
  568. static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
  569. u64 iova, u64 size)
  570. {
  571. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  572. if (asid >= vdpasim->dev_attr.nas)
  573. return -EINVAL;
  574. if (vdpasim->iommu_pt[asid]) {
  575. vhost_iotlb_reset(&vdpasim->iommu[asid]);
  576. vdpasim->iommu_pt[asid] = false;
  577. }
  578. spin_lock(&vdpasim->iommu_lock);
  579. vhost_iotlb_del_range(&vdpasim->iommu[asid], iova, iova + size - 1);
  580. spin_unlock(&vdpasim->iommu_lock);
  581. return 0;
  582. }
  583. static void vdpasim_free(struct vdpa_device *vdpa)
  584. {
  585. struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
  586. int i;
  587. kthread_cancel_work_sync(&vdpasim->work);
  588. kthread_destroy_worker(vdpasim->worker);
  589. for (i = 0; i < vdpasim->dev_attr.nvqs; i++) {
  590. vringh_kiov_cleanup(&vdpasim->vqs[i].out_iov);
  591. vringh_kiov_cleanup(&vdpasim->vqs[i].in_iov);
  592. }
  593. vdpasim->dev_attr.free(vdpasim);
  594. for (i = 0; i < vdpasim->dev_attr.nas; i++)
  595. vhost_iotlb_reset(&vdpasim->iommu[i]);
  596. kfree(vdpasim->iommu);
  597. kfree(vdpasim->iommu_pt);
  598. kfree(vdpasim->vqs);
  599. kfree(vdpasim->config);
  600. }
  601. static const struct vdpa_config_ops vdpasim_config_ops = {
  602. .set_vq_address = vdpasim_set_vq_address,
  603. .set_vq_num = vdpasim_set_vq_num,
  604. .kick_vq = vdpasim_kick_vq,
  605. .set_vq_cb = vdpasim_set_vq_cb,
  606. .set_vq_ready = vdpasim_set_vq_ready,
  607. .get_vq_ready = vdpasim_get_vq_ready,
  608. .set_vq_state = vdpasim_set_vq_state,
  609. .get_vendor_vq_stats = vdpasim_get_vq_stats,
  610. .get_vq_state = vdpasim_get_vq_state,
  611. .get_vq_align = vdpasim_get_vq_align,
  612. .get_vq_group = vdpasim_get_vq_group,
  613. .get_device_features = vdpasim_get_device_features,
  614. .get_backend_features = vdpasim_get_backend_features,
  615. .set_driver_features = vdpasim_set_driver_features,
  616. .get_driver_features = vdpasim_get_driver_features,
  617. .set_config_cb = vdpasim_set_config_cb,
  618. .get_vq_num_max = vdpasim_get_vq_num_max,
  619. .get_vq_size = vdpasim_get_vq_size,
  620. .get_device_id = vdpasim_get_device_id,
  621. .get_vendor_id = vdpasim_get_vendor_id,
  622. .get_status = vdpasim_get_status,
  623. .set_status = vdpasim_set_status,
  624. .reset = vdpasim_reset,
  625. .compat_reset = vdpasim_compat_reset,
  626. .suspend = vdpasim_suspend,
  627. .resume = vdpasim_resume,
  628. .get_config_size = vdpasim_get_config_size,
  629. .get_config = vdpasim_get_config,
  630. .set_config = vdpasim_set_config,
  631. .get_generation = vdpasim_get_generation,
  632. .get_iova_range = vdpasim_get_iova_range,
  633. .set_group_asid = vdpasim_set_group_asid,
  634. .dma_map = vdpasim_dma_map,
  635. .dma_unmap = vdpasim_dma_unmap,
  636. .reset_map = vdpasim_reset_map,
  637. .bind_mm = vdpasim_bind_mm,
  638. .unbind_mm = vdpasim_unbind_mm,
  639. .free = vdpasim_free,
  640. };
  641. static const struct vdpa_config_ops vdpasim_batch_config_ops = {
  642. .set_vq_address = vdpasim_set_vq_address,
  643. .set_vq_num = vdpasim_set_vq_num,
  644. .kick_vq = vdpasim_kick_vq,
  645. .set_vq_cb = vdpasim_set_vq_cb,
  646. .set_vq_ready = vdpasim_set_vq_ready,
  647. .get_vq_ready = vdpasim_get_vq_ready,
  648. .set_vq_state = vdpasim_set_vq_state,
  649. .get_vendor_vq_stats = vdpasim_get_vq_stats,
  650. .get_vq_state = vdpasim_get_vq_state,
  651. .get_vq_align = vdpasim_get_vq_align,
  652. .get_vq_group = vdpasim_get_vq_group,
  653. .get_device_features = vdpasim_get_device_features,
  654. .get_backend_features = vdpasim_get_backend_features,
  655. .set_driver_features = vdpasim_set_driver_features,
  656. .get_driver_features = vdpasim_get_driver_features,
  657. .set_config_cb = vdpasim_set_config_cb,
  658. .get_vq_num_max = vdpasim_get_vq_num_max,
  659. .get_device_id = vdpasim_get_device_id,
  660. .get_vendor_id = vdpasim_get_vendor_id,
  661. .get_status = vdpasim_get_status,
  662. .set_status = vdpasim_set_status,
  663. .reset = vdpasim_reset,
  664. .compat_reset = vdpasim_compat_reset,
  665. .suspend = vdpasim_suspend,
  666. .resume = vdpasim_resume,
  667. .get_config_size = vdpasim_get_config_size,
  668. .get_config = vdpasim_get_config,
  669. .set_config = vdpasim_set_config,
  670. .get_generation = vdpasim_get_generation,
  671. .get_iova_range = vdpasim_get_iova_range,
  672. .set_group_asid = vdpasim_set_group_asid,
  673. .set_map = vdpasim_set_map,
  674. .reset_map = vdpasim_reset_map,
  675. .bind_mm = vdpasim_bind_mm,
  676. .unbind_mm = vdpasim_unbind_mm,
  677. .free = vdpasim_free,
  678. };
  679. MODULE_VERSION(DRV_VERSION);
  680. MODULE_LICENSE(DRV_LICENSE);
  681. MODULE_AUTHOR(DRV_AUTHOR);
  682. MODULE_DESCRIPTION(DRV_DESC);