Commit | Line | Data |
---|---|---|
4c8cf318 TB |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Copyright (C) 2018-2020 Intel Corporation. | |
4 | * Copyright (C) 2020 Red Hat, Inc. | |
5 | * | |
6 | * Author: Tiwei Bie <tiwei.bie@intel.com> | |
7 | * Jason Wang <jasowang@redhat.com> | |
8 | * | |
9 | * Thanks Michael S. Tsirkin for the valuable comments and | |
10 | * suggestions. And thanks to Cunming Liang and Zhihong Wang for all | |
11 | * their supports. | |
12 | */ | |
13 | ||
14 | #include <linux/kernel.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/cdev.h> | |
17 | #include <linux/device.h> | |
ddd89d0a | 18 | #include <linux/mm.h> |
9d6d97bf | 19 | #include <linux/slab.h> |
4c8cf318 TB |
20 | #include <linux/iommu.h> |
21 | #include <linux/uuid.h> | |
22 | #include <linux/vdpa.h> | |
23 | #include <linux/nospec.h> | |
24 | #include <linux/vhost.h> | |
4c8cf318 TB |
25 | |
26 | #include "vhost.h" | |
27 | ||
653055b9 | 28 | enum { |
25abc060 JW |
29 | VHOST_VDPA_BACKEND_FEATURES = |
30 | (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) | | |
aaca8373 GD |
31 | (1ULL << VHOST_BACKEND_F_IOTLB_BATCH) | |
32 | (1ULL << VHOST_BACKEND_F_IOTLB_ASID), | |
653055b9 JW |
33 | }; |
34 | ||
4c8cf318 TB |
35 | #define VHOST_VDPA_DEV_MAX (1U << MINORBITS) |
36 | ||
3d569879 GD |
37 | #define VHOST_VDPA_IOTLB_BUCKETS 16 |
38 | ||
39 | struct vhost_vdpa_as { | |
40 | struct hlist_node hash_link; | |
41 | struct vhost_iotlb iotlb; | |
42 | u32 id; | |
43 | }; | |
44 | ||
4c8cf318 TB |
45 | struct vhost_vdpa { |
46 | struct vhost_dev vdev; | |
47 | struct iommu_domain *domain; | |
48 | struct vhost_virtqueue *vqs; | |
49 | struct completion completion; | |
50 | struct vdpa_device *vdpa; | |
3d569879 | 51 | struct hlist_head as[VHOST_VDPA_IOTLB_BUCKETS]; |
4c8cf318 TB |
52 | struct device dev; |
53 | struct cdev cdev; | |
54 | atomic_t opened; | |
81d46d69 | 55 | u32 nvqs; |
4c8cf318 TB |
56 | int virtio_id; |
57 | int minor; | |
776f3950 | 58 | struct eventfd_ctx *config_ctx; |
25abc060 | 59 | int in_batch; |
1b48dc03 | 60 | struct vdpa_iova_range range; |
aaca8373 | 61 | u32 batch_asid; |
4c8cf318 TB |
62 | }; |
63 | ||
64 | static DEFINE_IDA(vhost_vdpa_ida); | |
65 | ||
66 | static dev_t vhost_vdpa_major; | |
67 | ||
c070c191 | 68 | static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, |
e794070a CL |
69 | struct vhost_iotlb *iotlb, u64 start, |
70 | u64 last, u32 asid); | |
c070c191 | 71 | |
aaca8373 GD |
72 | static inline u32 iotlb_to_asid(struct vhost_iotlb *iotlb) |
73 | { | |
74 | struct vhost_vdpa_as *as = container_of(iotlb, struct | |
75 | vhost_vdpa_as, iotlb); | |
76 | return as->id; | |
77 | } | |
78 | ||
3d569879 GD |
79 | static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid) |
80 | { | |
81 | struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS]; | |
82 | struct vhost_vdpa_as *as; | |
83 | ||
84 | hlist_for_each_entry(as, head, hash_link) | |
85 | if (as->id == asid) | |
86 | return as; | |
87 | ||
88 | return NULL; | |
89 | } | |
90 | ||
aaca8373 GD |
91 | static struct vhost_iotlb *asid_to_iotlb(struct vhost_vdpa *v, u32 asid) |
92 | { | |
93 | struct vhost_vdpa_as *as = asid_to_as(v, asid); | |
94 | ||
95 | if (!as) | |
96 | return NULL; | |
97 | ||
98 | return &as->iotlb; | |
99 | } | |
100 | ||
3d569879 GD |
101 | static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid) |
102 | { | |
103 | struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS]; | |
104 | struct vhost_vdpa_as *as; | |
105 | ||
106 | if (asid_to_as(v, asid)) | |
107 | return NULL; | |
108 | ||
aaca8373 GD |
109 | if (asid >= v->vdpa->nas) |
110 | return NULL; | |
111 | ||
3d569879 GD |
112 | as = kmalloc(sizeof(*as), GFP_KERNEL); |
113 | if (!as) | |
114 | return NULL; | |
115 | ||
116 | vhost_iotlb_init(&as->iotlb, 0, 0); | |
117 | as->id = asid; | |
118 | hlist_add_head(&as->hash_link, head); | |
119 | ||
120 | return as; | |
121 | } | |
122 | ||
aaca8373 GD |
123 | static struct vhost_vdpa_as *vhost_vdpa_find_alloc_as(struct vhost_vdpa *v, |
124 | u32 asid) | |
125 | { | |
126 | struct vhost_vdpa_as *as = asid_to_as(v, asid); | |
127 | ||
128 | if (as) | |
129 | return as; | |
130 | ||
131 | return vhost_vdpa_alloc_as(v, asid); | |
132 | } | |
133 | ||
3d569879 GD |
134 | static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid) |
135 | { | |
136 | struct vhost_vdpa_as *as = asid_to_as(v, asid); | |
137 | ||
138 | if (!as) | |
139 | return -EINVAL; | |
140 | ||
141 | hlist_del(&as->hash_link); | |
e794070a | 142 | vhost_vdpa_iotlb_unmap(v, &as->iotlb, 0ULL, 0ULL - 1, asid); |
3d569879 GD |
143 | kfree(as); |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
4c8cf318 TB |
148 | static void handle_vq_kick(struct vhost_work *work) |
149 | { | |
150 | struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, | |
151 | poll.work); | |
152 | struct vhost_vdpa *v = container_of(vq->dev, struct vhost_vdpa, vdev); | |
153 | const struct vdpa_config_ops *ops = v->vdpa->config; | |
154 | ||
155 | ops->kick_vq(v->vdpa, vq - v->vqs); | |
156 | } | |
157 | ||
158 | static irqreturn_t vhost_vdpa_virtqueue_cb(void *private) | |
159 | { | |
160 | struct vhost_virtqueue *vq = private; | |
265a0ad8 | 161 | struct eventfd_ctx *call_ctx = vq->call_ctx.ctx; |
4c8cf318 TB |
162 | |
163 | if (call_ctx) | |
164 | eventfd_signal(call_ctx, 1); | |
165 | ||
166 | return IRQ_HANDLED; | |
167 | } | |
168 | ||
776f3950 ZL |
169 | static irqreturn_t vhost_vdpa_config_cb(void *private) |
170 | { | |
171 | struct vhost_vdpa *v = private; | |
172 | struct eventfd_ctx *config_ctx = v->config_ctx; | |
173 | ||
174 | if (config_ctx) | |
175 | eventfd_signal(config_ctx, 1); | |
176 | ||
177 | return IRQ_HANDLED; | |
178 | } | |
179 | ||
2cf1ba9a ZL |
180 | static void vhost_vdpa_setup_vq_irq(struct vhost_vdpa *v, u16 qid) |
181 | { | |
182 | struct vhost_virtqueue *vq = &v->vqs[qid]; | |
183 | const struct vdpa_config_ops *ops = v->vdpa->config; | |
184 | struct vdpa_device *vdpa = v->vdpa; | |
185 | int ret, irq; | |
186 | ||
187 | if (!ops->get_vq_irq) | |
188 | return; | |
189 | ||
190 | irq = ops->get_vq_irq(vdpa, qid); | |
cce0ab2b ZL |
191 | if (irq < 0) |
192 | return; | |
193 | ||
2cf1ba9a | 194 | irq_bypass_unregister_producer(&vq->call_ctx.producer); |
cce0ab2b | 195 | if (!vq->call_ctx.ctx) |
2cf1ba9a | 196 | return; |
2cf1ba9a ZL |
197 | |
198 | vq->call_ctx.producer.token = vq->call_ctx.ctx; | |
199 | vq->call_ctx.producer.irq = irq; | |
200 | ret = irq_bypass_register_producer(&vq->call_ctx.producer); | |
e01afe36 ZL |
201 | if (unlikely(ret)) |
202 | dev_info(&v->dev, "vq %u, irq bypass producer (token %p) registration fails, ret = %d\n", | |
203 | qid, vq->call_ctx.producer.token, ret); | |
2cf1ba9a ZL |
204 | } |
205 | ||
206 | static void vhost_vdpa_unsetup_vq_irq(struct vhost_vdpa *v, u16 qid) | |
207 | { | |
208 | struct vhost_virtqueue *vq = &v->vqs[qid]; | |
209 | ||
2cf1ba9a | 210 | irq_bypass_unregister_producer(&vq->call_ctx.producer); |
2cf1ba9a ZL |
211 | } |
212 | ||
7f05630d | 213 | static int vhost_vdpa_reset(struct vhost_vdpa *v) |
4c8cf318 TB |
214 | { |
215 | struct vdpa_device *vdpa = v->vdpa; | |
4c8cf318 | 216 | |
25abc060 | 217 | v->in_batch = 0; |
7f05630d XY |
218 | |
219 | return vdpa_reset(vdpa); | |
4c8cf318 TB |
220 | } |
221 | ||
222 | static long vhost_vdpa_get_device_id(struct vhost_vdpa *v, u8 __user *argp) | |
223 | { | |
224 | struct vdpa_device *vdpa = v->vdpa; | |
225 | const struct vdpa_config_ops *ops = vdpa->config; | |
226 | u32 device_id; | |
227 | ||
228 | device_id = ops->get_device_id(vdpa); | |
229 | ||
230 | if (copy_to_user(argp, &device_id, sizeof(device_id))) | |
231 | return -EFAULT; | |
232 | ||
233 | return 0; | |
234 | } | |
235 | ||
236 | static long vhost_vdpa_get_status(struct vhost_vdpa *v, u8 __user *statusp) | |
237 | { | |
238 | struct vdpa_device *vdpa = v->vdpa; | |
f6d955d8 | 239 | const struct vdpa_config_ops *ops = vdpa->config; |
4c8cf318 TB |
240 | u8 status; |
241 | ||
f6d955d8 | 242 | status = ops->get_status(vdpa); |
4c8cf318 TB |
243 | |
244 | if (copy_to_user(statusp, &status, sizeof(status))) | |
245 | return -EFAULT; | |
246 | ||
247 | return 0; | |
248 | } | |
249 | ||
250 | static long vhost_vdpa_set_status(struct vhost_vdpa *v, u8 __user *statusp) | |
251 | { | |
252 | struct vdpa_device *vdpa = v->vdpa; | |
253 | const struct vdpa_config_ops *ops = vdpa->config; | |
2cf1ba9a | 254 | u8 status, status_old; |
81d46d69 L |
255 | u32 nvqs = v->nvqs; |
256 | int ret; | |
2cf1ba9a | 257 | u16 i; |
4c8cf318 TB |
258 | |
259 | if (copy_from_user(&status, statusp, sizeof(status))) | |
260 | return -EFAULT; | |
261 | ||
f6d955d8 | 262 | status_old = ops->get_status(vdpa); |
2cf1ba9a | 263 | |
4c8cf318 TB |
264 | /* |
265 | * Userspace shouldn't remove status bits unless reset the | |
266 | * status to 0. | |
267 | */ | |
23118b09 | 268 | if (status != 0 && (status_old & ~status) != 0) |
4c8cf318 TB |
269 | return -EINVAL; |
270 | ||
97f854be WZ |
271 | if ((status_old & VIRTIO_CONFIG_S_DRIVER_OK) && !(status & VIRTIO_CONFIG_S_DRIVER_OK)) |
272 | for (i = 0; i < nvqs; i++) | |
273 | vhost_vdpa_unsetup_vq_irq(v, i); | |
274 | ||
0686082d | 275 | if (status == 0) { |
680ab9d6 | 276 | ret = vdpa_reset(vdpa); |
0686082d XY |
277 | if (ret) |
278 | return ret; | |
279 | } else | |
73bc0dbb | 280 | vdpa_set_status(vdpa, status); |
4c8cf318 | 281 | |
2cf1ba9a ZL |
282 | if ((status & VIRTIO_CONFIG_S_DRIVER_OK) && !(status_old & VIRTIO_CONFIG_S_DRIVER_OK)) |
283 | for (i = 0; i < nvqs; i++) | |
284 | vhost_vdpa_setup_vq_irq(v, i); | |
285 | ||
4c8cf318 TB |
286 | return 0; |
287 | } | |
288 | ||
289 | static int vhost_vdpa_config_validate(struct vhost_vdpa *v, | |
290 | struct vhost_vdpa_config *c) | |
291 | { | |
d6d8bb92 | 292 | struct vdpa_device *vdpa = v->vdpa; |
870aaff9 | 293 | size_t size = vdpa->config->get_config_size(vdpa); |
4c8cf318 | 294 | |
3ed21c14 | 295 | if (c->len == 0 || c->off > size) |
4c8cf318 TB |
296 | return -EINVAL; |
297 | ||
298 | if (c->len > size - c->off) | |
299 | return -E2BIG; | |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
304 | static long vhost_vdpa_get_config(struct vhost_vdpa *v, | |
305 | struct vhost_vdpa_config __user *c) | |
306 | { | |
307 | struct vdpa_device *vdpa = v->vdpa; | |
4c8cf318 TB |
308 | struct vhost_vdpa_config config; |
309 | unsigned long size = offsetof(struct vhost_vdpa_config, buf); | |
310 | u8 *buf; | |
311 | ||
312 | if (copy_from_user(&config, c, size)) | |
313 | return -EFAULT; | |
314 | if (vhost_vdpa_config_validate(v, &config)) | |
315 | return -EINVAL; | |
316 | buf = kvzalloc(config.len, GFP_KERNEL); | |
317 | if (!buf) | |
318 | return -ENOMEM; | |
319 | ||
0d234007 | 320 | vdpa_get_config(vdpa, config.off, buf, config.len); |
4c8cf318 TB |
321 | |
322 | if (copy_to_user(c->buf, buf, config.len)) { | |
323 | kvfree(buf); | |
324 | return -EFAULT; | |
325 | } | |
326 | ||
327 | kvfree(buf); | |
328 | return 0; | |
329 | } | |
330 | ||
331 | static long vhost_vdpa_set_config(struct vhost_vdpa *v, | |
332 | struct vhost_vdpa_config __user *c) | |
333 | { | |
334 | struct vdpa_device *vdpa = v->vdpa; | |
4c8cf318 TB |
335 | struct vhost_vdpa_config config; |
336 | unsigned long size = offsetof(struct vhost_vdpa_config, buf); | |
337 | u8 *buf; | |
338 | ||
339 | if (copy_from_user(&config, c, size)) | |
340 | return -EFAULT; | |
341 | if (vhost_vdpa_config_validate(v, &config)) | |
342 | return -EINVAL; | |
4c8cf318 | 343 | |
0ab4b890 TT |
344 | buf = vmemdup_user(c->buf, config.len); |
345 | if (IS_ERR(buf)) | |
346 | return PTR_ERR(buf); | |
4c8cf318 | 347 | |
6dbb1f16 | 348 | vdpa_set_config(vdpa, config.off, buf, config.len); |
4c8cf318 TB |
349 | |
350 | kvfree(buf); | |
351 | return 0; | |
352 | } | |
353 | ||
0723f1df EP |
354 | static bool vhost_vdpa_can_suspend(const struct vhost_vdpa *v) |
355 | { | |
356 | struct vdpa_device *vdpa = v->vdpa; | |
357 | const struct vdpa_config_ops *ops = vdpa->config; | |
358 | ||
359 | return ops->suspend; | |
360 | } | |
361 | ||
69106b6f SB |
362 | static bool vhost_vdpa_can_resume(const struct vhost_vdpa *v) |
363 | { | |
364 | struct vdpa_device *vdpa = v->vdpa; | |
365 | const struct vdpa_config_ops *ops = vdpa->config; | |
366 | ||
367 | return ops->resume; | |
368 | } | |
369 | ||
4c8cf318 TB |
370 | static long vhost_vdpa_get_features(struct vhost_vdpa *v, u64 __user *featurep) |
371 | { | |
372 | struct vdpa_device *vdpa = v->vdpa; | |
373 | const struct vdpa_config_ops *ops = vdpa->config; | |
374 | u64 features; | |
375 | ||
a64917bc | 376 | features = ops->get_device_features(vdpa); |
4c8cf318 TB |
377 | |
378 | if (copy_to_user(featurep, &features, sizeof(features))) | |
379 | return -EFAULT; | |
380 | ||
381 | return 0; | |
382 | } | |
383 | ||
384 | static long vhost_vdpa_set_features(struct vhost_vdpa *v, u64 __user *featurep) | |
385 | { | |
386 | struct vdpa_device *vdpa = v->vdpa; | |
387 | const struct vdpa_config_ops *ops = vdpa->config; | |
388 | u64 features; | |
389 | ||
390 | /* | |
391 | * It's not allowed to change the features after they have | |
392 | * been negotiated. | |
393 | */ | |
394 | if (ops->get_status(vdpa) & VIRTIO_CONFIG_S_FEATURES_OK) | |
395 | return -EBUSY; | |
396 | ||
397 | if (copy_from_user(&features, featurep, sizeof(features))) | |
398 | return -EFAULT; | |
399 | ||
e0077cc1 | 400 | if (vdpa_set_features(vdpa, features)) |
4c8cf318 TB |
401 | return -EINVAL; |
402 | ||
403 | return 0; | |
404 | } | |
405 | ||
406 | static long vhost_vdpa_get_vring_num(struct vhost_vdpa *v, u16 __user *argp) | |
407 | { | |
408 | struct vdpa_device *vdpa = v->vdpa; | |
409 | const struct vdpa_config_ops *ops = vdpa->config; | |
410 | u16 num; | |
411 | ||
412 | num = ops->get_vq_num_max(vdpa); | |
413 | ||
414 | if (copy_to_user(argp, &num, sizeof(num))) | |
415 | return -EFAULT; | |
416 | ||
417 | return 0; | |
418 | } | |
419 | ||
776f3950 ZL |
420 | static void vhost_vdpa_config_put(struct vhost_vdpa *v) |
421 | { | |
f6bbf001 | 422 | if (v->config_ctx) { |
776f3950 | 423 | eventfd_ctx_put(v->config_ctx); |
f6bbf001 SG |
424 | v->config_ctx = NULL; |
425 | } | |
776f3950 ZL |
426 | } |
427 | ||
428 | static long vhost_vdpa_set_config_call(struct vhost_vdpa *v, u32 __user *argp) | |
429 | { | |
430 | struct vdpa_callback cb; | |
431 | int fd; | |
432 | struct eventfd_ctx *ctx; | |
433 | ||
434 | cb.callback = vhost_vdpa_config_cb; | |
bcef9356 | 435 | cb.private = v; |
776f3950 ZL |
436 | if (copy_from_user(&fd, argp, sizeof(fd))) |
437 | return -EFAULT; | |
438 | ||
439 | ctx = fd == VHOST_FILE_UNBIND ? NULL : eventfd_ctx_fdget(fd); | |
440 | swap(ctx, v->config_ctx); | |
441 | ||
442 | if (!IS_ERR_OR_NULL(ctx)) | |
443 | eventfd_ctx_put(ctx); | |
444 | ||
0bde59c1 SG |
445 | if (IS_ERR(v->config_ctx)) { |
446 | long ret = PTR_ERR(v->config_ctx); | |
447 | ||
448 | v->config_ctx = NULL; | |
449 | return ret; | |
450 | } | |
776f3950 ZL |
451 | |
452 | v->vdpa->config->set_config_cb(v->vdpa, &cb); | |
453 | ||
454 | return 0; | |
455 | } | |
2cf1ba9a | 456 | |
1b48dc03 JW |
457 | static long vhost_vdpa_get_iova_range(struct vhost_vdpa *v, u32 __user *argp) |
458 | { | |
459 | struct vhost_vdpa_iova_range range = { | |
460 | .first = v->range.first, | |
461 | .last = v->range.last, | |
462 | }; | |
463 | ||
2c602741 DC |
464 | if (copy_to_user(argp, &range, sizeof(range))) |
465 | return -EFAULT; | |
466 | return 0; | |
1b48dc03 JW |
467 | } |
468 | ||
a61280dd L |
469 | static long vhost_vdpa_get_config_size(struct vhost_vdpa *v, u32 __user *argp) |
470 | { | |
471 | struct vdpa_device *vdpa = v->vdpa; | |
472 | const struct vdpa_config_ops *ops = vdpa->config; | |
473 | u32 size; | |
474 | ||
475 | size = ops->get_config_size(vdpa); | |
476 | ||
477 | if (copy_to_user(argp, &size, sizeof(size))) | |
478 | return -EFAULT; | |
479 | ||
480 | return 0; | |
481 | } | |
482 | ||
b04d910a L |
483 | static long vhost_vdpa_get_vqs_count(struct vhost_vdpa *v, u32 __user *argp) |
484 | { | |
485 | struct vdpa_device *vdpa = v->vdpa; | |
486 | ||
487 | if (copy_to_user(argp, &vdpa->nvqs, sizeof(vdpa->nvqs))) | |
488 | return -EFAULT; | |
489 | ||
490 | return 0; | |
491 | } | |
492 | ||
f345a014 EP |
493 | /* After a successful return of ioctl the device must not process more |
494 | * virtqueue descriptors. The device can answer to read or writes of config | |
495 | * fields as if it were not suspended. In particular, writing to "queue_enable" | |
496 | * with a value of 1 will not make the device start processing buffers. | |
497 | */ | |
498 | static long vhost_vdpa_suspend(struct vhost_vdpa *v) | |
499 | { | |
500 | struct vdpa_device *vdpa = v->vdpa; | |
501 | const struct vdpa_config_ops *ops = vdpa->config; | |
502 | ||
503 | if (!ops->suspend) | |
504 | return -EOPNOTSUPP; | |
505 | ||
506 | return ops->suspend(vdpa); | |
507 | } | |
508 | ||
3b688d7a SB |
509 | /* After a successful return of this ioctl the device resumes processing |
510 | * virtqueue descriptors. The device becomes fully operational the same way it | |
511 | * was before it was suspended. | |
512 | */ | |
513 | static long vhost_vdpa_resume(struct vhost_vdpa *v) | |
514 | { | |
515 | struct vdpa_device *vdpa = v->vdpa; | |
516 | const struct vdpa_config_ops *ops = vdpa->config; | |
517 | ||
518 | if (!ops->resume) | |
519 | return -EOPNOTSUPP; | |
520 | ||
521 | return ops->resume(vdpa); | |
522 | } | |
523 | ||
4c8cf318 TB |
524 | static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd, |
525 | void __user *argp) | |
526 | { | |
527 | struct vdpa_device *vdpa = v->vdpa; | |
528 | const struct vdpa_config_ops *ops = vdpa->config; | |
aac50c0b | 529 | struct vdpa_vq_state vq_state; |
4c8cf318 TB |
530 | struct vdpa_callback cb; |
531 | struct vhost_virtqueue *vq; | |
532 | struct vhost_vring_state s; | |
4c8cf318 TB |
533 | u32 idx; |
534 | long r; | |
535 | ||
536 | r = get_user(idx, (u32 __user *)argp); | |
537 | if (r < 0) | |
538 | return r; | |
539 | ||
540 | if (idx >= v->nvqs) | |
541 | return -ENOBUFS; | |
542 | ||
543 | idx = array_index_nospec(idx, v->nvqs); | |
544 | vq = &v->vqs[idx]; | |
545 | ||
b0bd82bf JW |
546 | switch (cmd) { |
547 | case VHOST_VDPA_SET_VRING_ENABLE: | |
4c8cf318 TB |
548 | if (copy_from_user(&s, argp, sizeof(s))) |
549 | return -EFAULT; | |
550 | ops->set_vq_ready(vdpa, idx, s.num); | |
551 | return 0; | |
2d1fcb77 | 552 | case VHOST_VDPA_GET_VRING_GROUP: |
00d1f546 JW |
553 | if (!ops->get_vq_group) |
554 | return -EOPNOTSUPP; | |
2d1fcb77 GD |
555 | s.index = idx; |
556 | s.num = ops->get_vq_group(vdpa, idx); | |
557 | if (s.num >= vdpa->ngroups) | |
558 | return -EIO; | |
559 | else if (copy_to_user(argp, &s, sizeof(s))) | |
560 | return -EFAULT; | |
561 | return 0; | |
84d7c8fd GD |
562 | case VHOST_VDPA_SET_GROUP_ASID: |
563 | if (copy_from_user(&s, argp, sizeof(s))) | |
564 | return -EFAULT; | |
565 | if (s.num >= vdpa->nas) | |
566 | return -EINVAL; | |
567 | if (!ops->set_group_asid) | |
568 | return -EOPNOTSUPP; | |
569 | return ops->set_group_asid(vdpa, idx, s.num); | |
b0bd82bf | 570 | case VHOST_GET_VRING_BASE: |
23750e39 EC |
571 | r = ops->get_vq_state(v->vdpa, idx, &vq_state); |
572 | if (r) | |
573 | return r; | |
574 | ||
530a5678 | 575 | vq->last_avail_idx = vq_state.split.avail_index; |
b0bd82bf JW |
576 | break; |
577 | } | |
4c8cf318 TB |
578 | |
579 | r = vhost_vring_ioctl(&v->vdev, cmd, argp); | |
580 | if (r) | |
581 | return r; | |
582 | ||
583 | switch (cmd) { | |
584 | case VHOST_SET_VRING_ADDR: | |
585 | if (ops->set_vq_address(vdpa, idx, | |
586 | (u64)(uintptr_t)vq->desc, | |
587 | (u64)(uintptr_t)vq->avail, | |
588 | (u64)(uintptr_t)vq->used)) | |
589 | r = -EINVAL; | |
590 | break; | |
591 | ||
592 | case VHOST_SET_VRING_BASE: | |
530a5678 | 593 | vq_state.split.avail_index = vq->last_avail_idx; |
aac50c0b | 594 | if (ops->set_vq_state(vdpa, idx, &vq_state)) |
4c8cf318 TB |
595 | r = -EINVAL; |
596 | break; | |
597 | ||
598 | case VHOST_SET_VRING_CALL: | |
265a0ad8 | 599 | if (vq->call_ctx.ctx) { |
4c8cf318 TB |
600 | cb.callback = vhost_vdpa_virtqueue_cb; |
601 | cb.private = vq; | |
602 | } else { | |
603 | cb.callback = NULL; | |
604 | cb.private = NULL; | |
605 | } | |
606 | ops->set_vq_cb(vdpa, idx, &cb); | |
2cf1ba9a | 607 | vhost_vdpa_setup_vq_irq(v, idx); |
4c8cf318 TB |
608 | break; |
609 | ||
610 | case VHOST_SET_VRING_NUM: | |
611 | ops->set_vq_num(vdpa, idx, vq->num); | |
612 | break; | |
613 | } | |
614 | ||
615 | return r; | |
616 | } | |
617 | ||
618 | static long vhost_vdpa_unlocked_ioctl(struct file *filep, | |
619 | unsigned int cmd, unsigned long arg) | |
620 | { | |
621 | struct vhost_vdpa *v = filep->private_data; | |
622 | struct vhost_dev *d = &v->vdev; | |
623 | void __user *argp = (void __user *)arg; | |
a127c5bb JW |
624 | u64 __user *featurep = argp; |
625 | u64 features; | |
7922460e | 626 | long r = 0; |
4c8cf318 | 627 | |
a127c5bb | 628 | if (cmd == VHOST_SET_BACKEND_FEATURES) { |
7922460e DC |
629 | if (copy_from_user(&features, featurep, sizeof(features))) |
630 | return -EFAULT; | |
0723f1df | 631 | if (features & ~(VHOST_VDPA_BACKEND_FEATURES | |
69106b6f SB |
632 | BIT_ULL(VHOST_BACKEND_F_SUSPEND) | |
633 | BIT_ULL(VHOST_BACKEND_F_RESUME))) | |
0723f1df EP |
634 | return -EOPNOTSUPP; |
635 | if ((features & BIT_ULL(VHOST_BACKEND_F_SUSPEND)) && | |
636 | !vhost_vdpa_can_suspend(v)) | |
a127c5bb | 637 | return -EOPNOTSUPP; |
69106b6f SB |
638 | if ((features & BIT_ULL(VHOST_BACKEND_F_RESUME)) && |
639 | !vhost_vdpa_can_resume(v)) | |
640 | return -EOPNOTSUPP; | |
a127c5bb JW |
641 | vhost_set_backend_features(&v->vdev, features); |
642 | return 0; | |
643 | } | |
644 | ||
4c8cf318 TB |
645 | mutex_lock(&d->mutex); |
646 | ||
647 | switch (cmd) { | |
648 | case VHOST_VDPA_GET_DEVICE_ID: | |
649 | r = vhost_vdpa_get_device_id(v, argp); | |
650 | break; | |
651 | case VHOST_VDPA_GET_STATUS: | |
652 | r = vhost_vdpa_get_status(v, argp); | |
653 | break; | |
654 | case VHOST_VDPA_SET_STATUS: | |
655 | r = vhost_vdpa_set_status(v, argp); | |
656 | break; | |
657 | case VHOST_VDPA_GET_CONFIG: | |
658 | r = vhost_vdpa_get_config(v, argp); | |
659 | break; | |
660 | case VHOST_VDPA_SET_CONFIG: | |
661 | r = vhost_vdpa_set_config(v, argp); | |
662 | break; | |
663 | case VHOST_GET_FEATURES: | |
664 | r = vhost_vdpa_get_features(v, argp); | |
665 | break; | |
666 | case VHOST_SET_FEATURES: | |
667 | r = vhost_vdpa_set_features(v, argp); | |
668 | break; | |
669 | case VHOST_VDPA_GET_VRING_NUM: | |
670 | r = vhost_vdpa_get_vring_num(v, argp); | |
671 | break; | |
3ace88bd | 672 | case VHOST_VDPA_GET_GROUP_NUM: |
f4a8686e DC |
673 | if (copy_to_user(argp, &v->vdpa->ngroups, |
674 | sizeof(v->vdpa->ngroups))) | |
675 | r = -EFAULT; | |
3ace88bd | 676 | break; |
a0c95f20 | 677 | case VHOST_VDPA_GET_AS_NUM: |
f4a8686e DC |
678 | if (copy_to_user(argp, &v->vdpa->nas, sizeof(v->vdpa->nas))) |
679 | r = -EFAULT; | |
a0c95f20 | 680 | break; |
4c8cf318 TB |
681 | case VHOST_SET_LOG_BASE: |
682 | case VHOST_SET_LOG_FD: | |
683 | r = -ENOIOCTLCMD; | |
684 | break; | |
776f3950 ZL |
685 | case VHOST_VDPA_SET_CONFIG_CALL: |
686 | r = vhost_vdpa_set_config_call(v, argp); | |
687 | break; | |
a127c5bb JW |
688 | case VHOST_GET_BACKEND_FEATURES: |
689 | features = VHOST_VDPA_BACKEND_FEATURES; | |
0723f1df EP |
690 | if (vhost_vdpa_can_suspend(v)) |
691 | features |= BIT_ULL(VHOST_BACKEND_F_SUSPEND); | |
69106b6f SB |
692 | if (vhost_vdpa_can_resume(v)) |
693 | features |= BIT_ULL(VHOST_BACKEND_F_RESUME); | |
7922460e DC |
694 | if (copy_to_user(featurep, &features, sizeof(features))) |
695 | r = -EFAULT; | |
a127c5bb | 696 | break; |
1b48dc03 JW |
697 | case VHOST_VDPA_GET_IOVA_RANGE: |
698 | r = vhost_vdpa_get_iova_range(v, argp); | |
699 | break; | |
a61280dd L |
700 | case VHOST_VDPA_GET_CONFIG_SIZE: |
701 | r = vhost_vdpa_get_config_size(v, argp); | |
702 | break; | |
b04d910a L |
703 | case VHOST_VDPA_GET_VQS_COUNT: |
704 | r = vhost_vdpa_get_vqs_count(v, argp); | |
705 | break; | |
f345a014 EP |
706 | case VHOST_VDPA_SUSPEND: |
707 | r = vhost_vdpa_suspend(v); | |
708 | break; | |
3b688d7a SB |
709 | case VHOST_VDPA_RESUME: |
710 | r = vhost_vdpa_resume(v); | |
711 | break; | |
4c8cf318 TB |
712 | default: |
713 | r = vhost_dev_ioctl(&v->vdev, cmd, argp); | |
714 | if (r == -ENOIOCTLCMD) | |
715 | r = vhost_vdpa_vring_ioctl(v, cmd, argp); | |
716 | break; | |
717 | } | |
718 | ||
719 | mutex_unlock(&d->mutex); | |
720 | return r; | |
721 | } | |
e794070a CL |
722 | static void vhost_vdpa_general_unmap(struct vhost_vdpa *v, |
723 | struct vhost_iotlb_map *map, u32 asid) | |
724 | { | |
725 | struct vdpa_device *vdpa = v->vdpa; | |
726 | const struct vdpa_config_ops *ops = vdpa->config; | |
727 | if (ops->dma_map) { | |
728 | ops->dma_unmap(vdpa, asid, map->start, map->size); | |
729 | } else if (ops->set_map == NULL) { | |
730 | iommu_unmap(v->domain, map->start, map->size); | |
731 | } | |
732 | } | |
4c8cf318 | 733 | |
e794070a CL |
734 | static void vhost_vdpa_pa_unmap(struct vhost_vdpa *v, struct vhost_iotlb *iotlb, |
735 | u64 start, u64 last, u32 asid) | |
4c8cf318 TB |
736 | { |
737 | struct vhost_dev *dev = &v->vdev; | |
4c8cf318 TB |
738 | struct vhost_iotlb_map *map; |
739 | struct page *page; | |
740 | unsigned long pfn, pinned; | |
741 | ||
742 | while ((map = vhost_iotlb_itree_first(iotlb, start, last)) != NULL) { | |
729ce5a5 CH |
743 | pinned = PFN_DOWN(map->size); |
744 | for (pfn = PFN_DOWN(map->addr); | |
4c8cf318 TB |
745 | pinned > 0; pfn++, pinned--) { |
746 | page = pfn_to_page(pfn); | |
747 | if (map->perm & VHOST_ACCESS_WO) | |
748 | set_page_dirty_lock(page); | |
749 | unpin_user_page(page); | |
750 | } | |
729ce5a5 | 751 | atomic64_sub(PFN_DOWN(map->size), &dev->mm->pinned_vm); |
e794070a | 752 | vhost_vdpa_general_unmap(v, map, asid); |
4c8cf318 TB |
753 | vhost_iotlb_map_free(iotlb, map); |
754 | } | |
755 | } | |
756 | ||
e794070a CL |
757 | static void vhost_vdpa_va_unmap(struct vhost_vdpa *v, struct vhost_iotlb *iotlb, |
758 | u64 start, u64 last, u32 asid) | |
d8945ec4 | 759 | { |
d8945ec4 XY |
760 | struct vhost_iotlb_map *map; |
761 | struct vdpa_map_file *map_file; | |
762 | ||
763 | while ((map = vhost_iotlb_itree_first(iotlb, start, last)) != NULL) { | |
764 | map_file = (struct vdpa_map_file *)map->opaque; | |
765 | fput(map_file->file); | |
766 | kfree(map_file); | |
e794070a | 767 | vhost_vdpa_general_unmap(v, map, asid); |
d8945ec4 XY |
768 | vhost_iotlb_map_free(iotlb, map); |
769 | } | |
770 | } | |
771 | ||
ae967246 | 772 | static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, |
e794070a CL |
773 | struct vhost_iotlb *iotlb, u64 start, |
774 | u64 last, u32 asid) | |
22af48cf | 775 | { |
d8945ec4 XY |
776 | struct vdpa_device *vdpa = v->vdpa; |
777 | ||
778 | if (vdpa->use_va) | |
e794070a | 779 | return vhost_vdpa_va_unmap(v, iotlb, start, last, asid); |
d8945ec4 | 780 | |
e794070a | 781 | return vhost_vdpa_pa_unmap(v, iotlb, start, last, asid); |
22af48cf XY |
782 | } |
783 | ||
4c8cf318 TB |
784 | static int perm_to_iommu_flags(u32 perm) |
785 | { | |
786 | int flags = 0; | |
787 | ||
788 | switch (perm) { | |
789 | case VHOST_ACCESS_WO: | |
790 | flags |= IOMMU_WRITE; | |
791 | break; | |
792 | case VHOST_ACCESS_RO: | |
793 | flags |= IOMMU_READ; | |
794 | break; | |
795 | case VHOST_ACCESS_RW: | |
796 | flags |= (IOMMU_WRITE | IOMMU_READ); | |
797 | break; | |
798 | default: | |
799 | WARN(1, "invalidate vhost IOTLB permission\n"); | |
800 | break; | |
801 | } | |
802 | ||
803 | return flags | IOMMU_CACHE; | |
804 | } | |
805 | ||
ae967246 GD |
806 | static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb, |
807 | u64 iova, u64 size, u64 pa, u32 perm, void *opaque) | |
4c8cf318 TB |
808 | { |
809 | struct vhost_dev *dev = &v->vdev; | |
810 | struct vdpa_device *vdpa = v->vdpa; | |
811 | const struct vdpa_config_ops *ops = vdpa->config; | |
aaca8373 | 812 | u32 asid = iotlb_to_asid(iotlb); |
4c8cf318 TB |
813 | int r = 0; |
814 | ||
ae967246 | 815 | r = vhost_iotlb_add_range_ctx(iotlb, iova, iova + size - 1, |
d8945ec4 | 816 | pa, perm, opaque); |
4c8cf318 TB |
817 | if (r) |
818 | return r; | |
819 | ||
25abc060 | 820 | if (ops->dma_map) { |
aaca8373 | 821 | r = ops->dma_map(vdpa, asid, iova, size, pa, perm, opaque); |
25abc060 JW |
822 | } else if (ops->set_map) { |
823 | if (!v->in_batch) | |
aaca8373 | 824 | r = ops->set_map(vdpa, asid, iotlb); |
25abc060 | 825 | } else { |
4c8cf318 | 826 | r = iommu_map(v->domain, iova, pa, size, |
1369459b | 827 | perm_to_iommu_flags(perm), GFP_KERNEL); |
25abc060 | 828 | } |
d8945ec4 | 829 | if (r) { |
ae967246 | 830 | vhost_iotlb_del_range(iotlb, iova, iova + size - 1); |
d8945ec4 XY |
831 | return r; |
832 | } | |
833 | ||
834 | if (!vdpa->use_va) | |
729ce5a5 | 835 | atomic64_add(PFN_DOWN(size), &dev->mm->pinned_vm); |
1477c8ae | 836 | |
d8945ec4 | 837 | return 0; |
4c8cf318 TB |
838 | } |
839 | ||
ae967246 GD |
840 | static void vhost_vdpa_unmap(struct vhost_vdpa *v, |
841 | struct vhost_iotlb *iotlb, | |
842 | u64 iova, u64 size) | |
4c8cf318 | 843 | { |
4c8cf318 TB |
844 | struct vdpa_device *vdpa = v->vdpa; |
845 | const struct vdpa_config_ops *ops = vdpa->config; | |
aaca8373 | 846 | u32 asid = iotlb_to_asid(iotlb); |
4c8cf318 | 847 | |
e794070a | 848 | vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1, asid); |
4c8cf318 | 849 | |
e794070a | 850 | if (ops->set_map) { |
25abc060 | 851 | if (!v->in_batch) |
aaca8373 | 852 | ops->set_map(vdpa, asid, iotlb); |
25abc060 | 853 | } |
aaca8373 GD |
854 | /* If we are in the middle of batch processing, delay the free |
855 | * of AS until BATCH_END. | |
856 | */ | |
857 | if (!v->in_batch && !iotlb->nmaps) | |
858 | vhost_vdpa_remove_as(v, asid); | |
4c8cf318 TB |
859 | } |
860 | ||
d8945ec4 | 861 | static int vhost_vdpa_va_map(struct vhost_vdpa *v, |
ae967246 | 862 | struct vhost_iotlb *iotlb, |
d8945ec4 XY |
863 | u64 iova, u64 size, u64 uaddr, u32 perm) |
864 | { | |
865 | struct vhost_dev *dev = &v->vdev; | |
866 | u64 offset, map_size, map_iova = iova; | |
867 | struct vdpa_map_file *map_file; | |
868 | struct vm_area_struct *vma; | |
be9c6bad | 869 | int ret = 0; |
d8945ec4 XY |
870 | |
871 | mmap_read_lock(dev->mm); | |
872 | ||
873 | while (size) { | |
874 | vma = find_vma(dev->mm, uaddr); | |
875 | if (!vma) { | |
876 | ret = -EINVAL; | |
877 | break; | |
878 | } | |
879 | map_size = min(size, vma->vm_end - uaddr); | |
880 | if (!(vma->vm_file && (vma->vm_flags & VM_SHARED) && | |
881 | !(vma->vm_flags & (VM_IO | VM_PFNMAP)))) | |
882 | goto next; | |
883 | ||
884 | map_file = kzalloc(sizeof(*map_file), GFP_KERNEL); | |
885 | if (!map_file) { | |
886 | ret = -ENOMEM; | |
887 | break; | |
888 | } | |
889 | offset = (vma->vm_pgoff << PAGE_SHIFT) + uaddr - vma->vm_start; | |
890 | map_file->offset = offset; | |
891 | map_file->file = get_file(vma->vm_file); | |
ae967246 | 892 | ret = vhost_vdpa_map(v, iotlb, map_iova, map_size, uaddr, |
d8945ec4 XY |
893 | perm, map_file); |
894 | if (ret) { | |
895 | fput(map_file->file); | |
896 | kfree(map_file); | |
897 | break; | |
898 | } | |
899 | next: | |
900 | size -= map_size; | |
901 | uaddr += map_size; | |
902 | map_iova += map_size; | |
903 | } | |
904 | if (ret) | |
ae967246 | 905 | vhost_vdpa_unmap(v, iotlb, iova, map_iova - iova); |
d8945ec4 XY |
906 | |
907 | mmap_read_unlock(dev->mm); | |
908 | ||
909 | return ret; | |
910 | } | |
911 | ||
22af48cf | 912 | static int vhost_vdpa_pa_map(struct vhost_vdpa *v, |
ae967246 | 913 | struct vhost_iotlb *iotlb, |
22af48cf | 914 | u64 iova, u64 size, u64 uaddr, u32 perm) |
4c8cf318 TB |
915 | { |
916 | struct vhost_dev *dev = &v->vdev; | |
4c8cf318 | 917 | struct page **page_list; |
5e1a3149 | 918 | unsigned long list_size = PAGE_SIZE / sizeof(struct page *); |
4c8cf318 | 919 | unsigned int gup_flags = FOLL_LONGTERM; |
5e1a3149 | 920 | unsigned long npages, cur_base, map_pfn, last_pfn = 0; |
ad89653f | 921 | unsigned long lock_limit, sz2pin, nchunks, i; |
22af48cf | 922 | u64 start = iova; |
ad89653f | 923 | long pinned; |
4c8cf318 TB |
924 | int ret = 0; |
925 | ||
ad89653f | 926 | /* Limit the use of memory for bookkeeping */ |
5e1a3149 MT |
927 | page_list = (struct page **) __get_free_page(GFP_KERNEL); |
928 | if (!page_list) | |
929 | return -ENOMEM; | |
930 | ||
22af48cf | 931 | if (perm & VHOST_ACCESS_WO) |
4c8cf318 TB |
932 | gup_flags |= FOLL_WRITE; |
933 | ||
22af48cf | 934 | npages = PFN_UP(size + (iova & ~PAGE_MASK)); |
ad89653f SWL |
935 | if (!npages) { |
936 | ret = -EINVAL; | |
937 | goto free; | |
938 | } | |
4c8cf318 | 939 | |
d8ed45c5 | 940 | mmap_read_lock(dev->mm); |
4c8cf318 | 941 | |
729ce5a5 | 942 | lock_limit = PFN_DOWN(rlimit(RLIMIT_MEMLOCK)); |
ad89653f | 943 | if (npages + atomic64_read(&dev->mm->pinned_vm) > lock_limit) { |
5e1a3149 | 944 | ret = -ENOMEM; |
ad89653f | 945 | goto unlock; |
7ed9e3d9 | 946 | } |
4c8cf318 | 947 | |
22af48cf | 948 | cur_base = uaddr & PAGE_MASK; |
7ed9e3d9 | 949 | iova &= PAGE_MASK; |
ad89653f | 950 | nchunks = 0; |
5e1a3149 MT |
951 | |
952 | while (npages) { | |
ad89653f SWL |
953 | sz2pin = min_t(unsigned long, npages, list_size); |
954 | pinned = pin_user_pages(cur_base, sz2pin, | |
955 | gup_flags, page_list, NULL); | |
956 | if (sz2pin != pinned) { | |
957 | if (pinned < 0) { | |
958 | ret = pinned; | |
959 | } else { | |
960 | unpin_user_pages(page_list, pinned); | |
961 | ret = -ENOMEM; | |
962 | } | |
5e1a3149 | 963 | goto out; |
ad89653f SWL |
964 | } |
965 | nchunks++; | |
5e1a3149 MT |
966 | |
967 | if (!last_pfn) | |
968 | map_pfn = page_to_pfn(page_list[0]); | |
969 | ||
ad89653f | 970 | for (i = 0; i < pinned; i++) { |
5e1a3149 MT |
971 | unsigned long this_pfn = page_to_pfn(page_list[i]); |
972 | u64 csize; | |
973 | ||
974 | if (last_pfn && (this_pfn != last_pfn + 1)) { | |
975 | /* Pin a contiguous chunk of memory */ | |
729ce5a5 | 976 | csize = PFN_PHYS(last_pfn - map_pfn + 1); |
ae967246 | 977 | ret = vhost_vdpa_map(v, iotlb, iova, csize, |
729ce5a5 | 978 | PFN_PHYS(map_pfn), |
d8945ec4 | 979 | perm, NULL); |
ad89653f SWL |
980 | if (ret) { |
981 | /* | |
982 | * Unpin the pages that are left unmapped | |
983 | * from this point on in the current | |
984 | * page_list. The remaining outstanding | |
985 | * ones which may stride across several | |
986 | * chunks will be covered in the common | |
987 | * error path subsequently. | |
988 | */ | |
989 | unpin_user_pages(&page_list[i], | |
990 | pinned - i); | |
5e1a3149 | 991 | goto out; |
ad89653f SWL |
992 | } |
993 | ||
5e1a3149 MT |
994 | map_pfn = this_pfn; |
995 | iova += csize; | |
ad89653f | 996 | nchunks = 0; |
4c8cf318 | 997 | } |
5e1a3149 MT |
998 | |
999 | last_pfn = this_pfn; | |
4c8cf318 | 1000 | } |
5e1a3149 | 1001 | |
729ce5a5 | 1002 | cur_base += PFN_PHYS(pinned); |
ad89653f | 1003 | npages -= pinned; |
4c8cf318 TB |
1004 | } |
1005 | ||
5e1a3149 | 1006 | /* Pin the rest chunk */ |
ae967246 | 1007 | ret = vhost_vdpa_map(v, iotlb, iova, PFN_PHYS(last_pfn - map_pfn + 1), |
d8945ec4 | 1008 | PFN_PHYS(map_pfn), perm, NULL); |
4c8cf318 | 1009 | out: |
5e1a3149 | 1010 | if (ret) { |
ad89653f SWL |
1011 | if (nchunks) { |
1012 | unsigned long pfn; | |
1013 | ||
1014 | /* | |
1015 | * Unpin the outstanding pages which are yet to be | |
1016 | * mapped but haven't due to vdpa_map() or | |
1017 | * pin_user_pages() failure. | |
1018 | * | |
1019 | * Mapped pages are accounted in vdpa_map(), hence | |
1020 | * the corresponding unpinning will be handled by | |
1021 | * vdpa_unmap(). | |
1022 | */ | |
1023 | WARN_ON(!last_pfn); | |
1024 | for (pfn = map_pfn; pfn <= last_pfn; pfn++) | |
1025 | unpin_user_page(pfn_to_page(pfn)); | |
1026 | } | |
ae967246 | 1027 | vhost_vdpa_unmap(v, iotlb, start, size); |
5e1a3149 | 1028 | } |
ad89653f | 1029 | unlock: |
d8ed45c5 | 1030 | mmap_read_unlock(dev->mm); |
ad89653f | 1031 | free: |
5e1a3149 | 1032 | free_page((unsigned long)page_list); |
4c8cf318 | 1033 | return ret; |
22af48cf XY |
1034 | |
1035 | } | |
1036 | ||
1037 | static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, | |
ae967246 | 1038 | struct vhost_iotlb *iotlb, |
22af48cf XY |
1039 | struct vhost_iotlb_msg *msg) |
1040 | { | |
d8945ec4 | 1041 | struct vdpa_device *vdpa = v->vdpa; |
22af48cf XY |
1042 | |
1043 | if (msg->iova < v->range.first || !msg->size || | |
1044 | msg->iova > U64_MAX - msg->size + 1 || | |
1045 | msg->iova + msg->size - 1 > v->range.last) | |
1046 | return -EINVAL; | |
1047 | ||
1048 | if (vhost_iotlb_itree_first(iotlb, msg->iova, | |
1049 | msg->iova + msg->size - 1)) | |
1050 | return -EEXIST; | |
1051 | ||
d8945ec4 | 1052 | if (vdpa->use_va) |
ae967246 | 1053 | return vhost_vdpa_va_map(v, iotlb, msg->iova, msg->size, |
d8945ec4 XY |
1054 | msg->uaddr, msg->perm); |
1055 | ||
ae967246 | 1056 | return vhost_vdpa_pa_map(v, iotlb, msg->iova, msg->size, msg->uaddr, |
22af48cf | 1057 | msg->perm); |
4c8cf318 TB |
1058 | } |
1059 | ||
91233ad7 | 1060 | static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid, |
4c8cf318 TB |
1061 | struct vhost_iotlb_msg *msg) |
1062 | { | |
1063 | struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev); | |
25abc060 JW |
1064 | struct vdpa_device *vdpa = v->vdpa; |
1065 | const struct vdpa_config_ops *ops = vdpa->config; | |
aaca8373 GD |
1066 | struct vhost_iotlb *iotlb = NULL; |
1067 | struct vhost_vdpa_as *as = NULL; | |
4c8cf318 TB |
1068 | int r = 0; |
1069 | ||
a9d06452 XY |
1070 | mutex_lock(&dev->mutex); |
1071 | ||
4c8cf318 TB |
1072 | r = vhost_dev_check_owner(dev); |
1073 | if (r) | |
a9d06452 | 1074 | goto unlock; |
4c8cf318 | 1075 | |
aaca8373 GD |
1076 | if (msg->type == VHOST_IOTLB_UPDATE || |
1077 | msg->type == VHOST_IOTLB_BATCH_BEGIN) { | |
1078 | as = vhost_vdpa_find_alloc_as(v, asid); | |
1079 | if (!as) { | |
1080 | dev_err(&v->dev, "can't find and alloc asid %d\n", | |
1081 | asid); | |
1082 | r = -EINVAL; | |
1083 | goto unlock; | |
1084 | } | |
1085 | iotlb = &as->iotlb; | |
1086 | } else | |
1087 | iotlb = asid_to_iotlb(v, asid); | |
1088 | ||
1089 | if ((v->in_batch && v->batch_asid != asid) || !iotlb) { | |
1090 | if (v->in_batch && v->batch_asid != asid) { | |
1091 | dev_info(&v->dev, "batch id %d asid %d\n", | |
1092 | v->batch_asid, asid); | |
1093 | } | |
1094 | if (!iotlb) | |
1095 | dev_err(&v->dev, "no iotlb for asid %d\n", asid); | |
1096 | r = -EINVAL; | |
1097 | goto unlock; | |
1098 | } | |
1099 | ||
4c8cf318 TB |
1100 | switch (msg->type) { |
1101 | case VHOST_IOTLB_UPDATE: | |
ae967246 | 1102 | r = vhost_vdpa_process_iotlb_update(v, iotlb, msg); |
4c8cf318 TB |
1103 | break; |
1104 | case VHOST_IOTLB_INVALIDATE: | |
ae967246 | 1105 | vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size); |
4c8cf318 | 1106 | break; |
25abc060 | 1107 | case VHOST_IOTLB_BATCH_BEGIN: |
aaca8373 | 1108 | v->batch_asid = asid; |
25abc060 JW |
1109 | v->in_batch = true; |
1110 | break; | |
1111 | case VHOST_IOTLB_BATCH_END: | |
1112 | if (v->in_batch && ops->set_map) | |
aaca8373 | 1113 | ops->set_map(vdpa, asid, iotlb); |
25abc060 | 1114 | v->in_batch = false; |
aaca8373 GD |
1115 | if (!iotlb->nmaps) |
1116 | vhost_vdpa_remove_as(v, asid); | |
25abc060 | 1117 | break; |
4c8cf318 TB |
1118 | default: |
1119 | r = -EINVAL; | |
1120 | break; | |
1121 | } | |
a9d06452 XY |
1122 | unlock: |
1123 | mutex_unlock(&dev->mutex); | |
4c8cf318 TB |
1124 | |
1125 | return r; | |
1126 | } | |
1127 | ||
1128 | static ssize_t vhost_vdpa_chr_write_iter(struct kiocb *iocb, | |
1129 | struct iov_iter *from) | |
1130 | { | |
1131 | struct file *file = iocb->ki_filp; | |
1132 | struct vhost_vdpa *v = file->private_data; | |
1133 | struct vhost_dev *dev = &v->vdev; | |
1134 | ||
1135 | return vhost_chr_write_iter(dev, from); | |
1136 | } | |
1137 | ||
1138 | static int vhost_vdpa_alloc_domain(struct vhost_vdpa *v) | |
1139 | { | |
1140 | struct vdpa_device *vdpa = v->vdpa; | |
1141 | const struct vdpa_config_ops *ops = vdpa->config; | |
1142 | struct device *dma_dev = vdpa_get_dma_dev(vdpa); | |
1143 | struct bus_type *bus; | |
1144 | int ret; | |
1145 | ||
1146 | /* Device want to do DMA by itself */ | |
1147 | if (ops->set_map || ops->dma_map) | |
1148 | return 0; | |
1149 | ||
1150 | bus = dma_dev->bus; | |
1151 | if (!bus) | |
1152 | return -EFAULT; | |
1153 | ||
6830a6ab AK |
1154 | if (!device_iommu_capable(dma_dev, IOMMU_CAP_CACHE_COHERENCY)) { |
1155 | dev_warn_once(&v->dev, | |
1156 | "Failed to allocate domain, device is not IOMMU cache coherent capable\n"); | |
4c8cf318 | 1157 | return -ENOTSUPP; |
6830a6ab | 1158 | } |
4c8cf318 TB |
1159 | |
1160 | v->domain = iommu_domain_alloc(bus); | |
1161 | if (!v->domain) | |
1162 | return -EIO; | |
1163 | ||
1164 | ret = iommu_attach_device(v->domain, dma_dev); | |
1165 | if (ret) | |
1166 | goto err_attach; | |
1167 | ||
1168 | return 0; | |
1169 | ||
1170 | err_attach: | |
1171 | iommu_domain_free(v->domain); | |
5a522150 | 1172 | v->domain = NULL; |
4c8cf318 TB |
1173 | return ret; |
1174 | } | |
1175 | ||
1176 | static void vhost_vdpa_free_domain(struct vhost_vdpa *v) | |
1177 | { | |
1178 | struct vdpa_device *vdpa = v->vdpa; | |
1179 | struct device *dma_dev = vdpa_get_dma_dev(vdpa); | |
1180 | ||
1181 | if (v->domain) { | |
1182 | iommu_detach_device(v->domain, dma_dev); | |
1183 | iommu_domain_free(v->domain); | |
1184 | } | |
1185 | ||
1186 | v->domain = NULL; | |
1187 | } | |
1188 | ||
1b48dc03 JW |
1189 | static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v) |
1190 | { | |
1191 | struct vdpa_iova_range *range = &v->range; | |
1b48dc03 JW |
1192 | struct vdpa_device *vdpa = v->vdpa; |
1193 | const struct vdpa_config_ops *ops = vdpa->config; | |
1194 | ||
1195 | if (ops->get_iova_range) { | |
1196 | *range = ops->get_iova_range(vdpa); | |
bc9a05ee CH |
1197 | } else if (v->domain && v->domain->geometry.force_aperture) { |
1198 | range->first = v->domain->geometry.aperture_start; | |
1199 | range->last = v->domain->geometry.aperture_end; | |
1b48dc03 JW |
1200 | } else { |
1201 | range->first = 0; | |
1202 | range->last = ULLONG_MAX; | |
1203 | } | |
1204 | } | |
1205 | ||
3d569879 GD |
1206 | static void vhost_vdpa_cleanup(struct vhost_vdpa *v) |
1207 | { | |
aaca8373 GD |
1208 | struct vhost_vdpa_as *as; |
1209 | u32 asid; | |
1210 | ||
aaca8373 GD |
1211 | for (asid = 0; asid < v->vdpa->nas; asid++) { |
1212 | as = asid_to_as(v, asid); | |
1213 | if (as) | |
1214 | vhost_vdpa_remove_as(v, asid); | |
1215 | } | |
c070c191 | 1216 | |
5a522150 | 1217 | vhost_vdpa_free_domain(v); |
c070c191 SG |
1218 | vhost_dev_cleanup(&v->vdev); |
1219 | kfree(v->vdev.vqs); | |
3d569879 GD |
1220 | } |
1221 | ||
4c8cf318 TB |
1222 | static int vhost_vdpa_open(struct inode *inode, struct file *filep) |
1223 | { | |
1224 | struct vhost_vdpa *v; | |
1225 | struct vhost_dev *dev; | |
1226 | struct vhost_virtqueue **vqs; | |
81d46d69 L |
1227 | int r, opened; |
1228 | u32 i, nvqs; | |
4c8cf318 TB |
1229 | |
1230 | v = container_of(inode->i_cdev, struct vhost_vdpa, cdev); | |
4c8cf318 TB |
1231 | |
1232 | opened = atomic_cmpxchg(&v->opened, 0, 1); | |
1233 | if (opened) | |
1234 | return -EBUSY; | |
1235 | ||
1236 | nvqs = v->nvqs; | |
7f05630d XY |
1237 | r = vhost_vdpa_reset(v); |
1238 | if (r) | |
1239 | goto err; | |
4c8cf318 TB |
1240 | |
1241 | vqs = kmalloc_array(nvqs, sizeof(*vqs), GFP_KERNEL); | |
1242 | if (!vqs) { | |
1243 | r = -ENOMEM; | |
1244 | goto err; | |
1245 | } | |
1246 | ||
1247 | dev = &v->vdev; | |
1248 | for (i = 0; i < nvqs; i++) { | |
1249 | vqs[i] = &v->vqs[i]; | |
1250 | vqs[i]->handle_kick = handle_vq_kick; | |
1251 | } | |
01fcb1cb | 1252 | vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false, |
4c8cf318 TB |
1253 | vhost_vdpa_process_iotlb_msg); |
1254 | ||
4c8cf318 TB |
1255 | r = vhost_vdpa_alloc_domain(v); |
1256 | if (r) | |
aaca8373 | 1257 | goto err_alloc_domain; |
4c8cf318 | 1258 | |
1b48dc03 JW |
1259 | vhost_vdpa_set_iova_range(v); |
1260 | ||
4c8cf318 TB |
1261 | filep->private_data = v; |
1262 | ||
1263 | return 0; | |
1264 | ||
aaca8373 | 1265 | err_alloc_domain: |
3d569879 | 1266 | vhost_vdpa_cleanup(v); |
4c8cf318 TB |
1267 | err: |
1268 | atomic_dec(&v->opened); | |
1269 | return r; | |
1270 | } | |
1271 | ||
2cf1ba9a ZL |
1272 | static void vhost_vdpa_clean_irq(struct vhost_vdpa *v) |
1273 | { | |
81d46d69 | 1274 | u32 i; |
2cf1ba9a | 1275 | |
4c050286 GD |
1276 | for (i = 0; i < v->nvqs; i++) |
1277 | vhost_vdpa_unsetup_vq_irq(v, i); | |
2cf1ba9a ZL |
1278 | } |
1279 | ||
4c8cf318 TB |
1280 | static int vhost_vdpa_release(struct inode *inode, struct file *filep) |
1281 | { | |
1282 | struct vhost_vdpa *v = filep->private_data; | |
1283 | struct vhost_dev *d = &v->vdev; | |
1284 | ||
1285 | mutex_lock(&d->mutex); | |
1286 | filep->private_data = NULL; | |
ea8f17e4 | 1287 | vhost_vdpa_clean_irq(v); |
4c8cf318 TB |
1288 | vhost_vdpa_reset(v); |
1289 | vhost_dev_stop(&v->vdev); | |
776f3950 | 1290 | vhost_vdpa_config_put(v); |
037d4305 | 1291 | vhost_vdpa_cleanup(v); |
4c8cf318 TB |
1292 | mutex_unlock(&d->mutex); |
1293 | ||
1294 | atomic_dec(&v->opened); | |
1295 | complete(&v->completion); | |
1296 | ||
1297 | return 0; | |
1298 | } | |
1299 | ||
4b4e4867 | 1300 | #ifdef CONFIG_MMU |
ddd89d0a JW |
1301 | static vm_fault_t vhost_vdpa_fault(struct vm_fault *vmf) |
1302 | { | |
1303 | struct vhost_vdpa *v = vmf->vma->vm_file->private_data; | |
1304 | struct vdpa_device *vdpa = v->vdpa; | |
1305 | const struct vdpa_config_ops *ops = vdpa->config; | |
1306 | struct vdpa_notification_area notify; | |
1307 | struct vm_area_struct *vma = vmf->vma; | |
1308 | u16 index = vma->vm_pgoff; | |
1309 | ||
1310 | notify = ops->get_vq_notification(vdpa, index); | |
1311 | ||
1312 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); | |
1313 | if (remap_pfn_range(vma, vmf->address & PAGE_MASK, | |
729ce5a5 | 1314 | PFN_DOWN(notify.addr), PAGE_SIZE, |
ddd89d0a JW |
1315 | vma->vm_page_prot)) |
1316 | return VM_FAULT_SIGBUS; | |
1317 | ||
1318 | return VM_FAULT_NOPAGE; | |
1319 | } | |
1320 | ||
1321 | static const struct vm_operations_struct vhost_vdpa_vm_ops = { | |
1322 | .fault = vhost_vdpa_fault, | |
1323 | }; | |
1324 | ||
1325 | static int vhost_vdpa_mmap(struct file *file, struct vm_area_struct *vma) | |
1326 | { | |
1327 | struct vhost_vdpa *v = vma->vm_file->private_data; | |
1328 | struct vdpa_device *vdpa = v->vdpa; | |
1329 | const struct vdpa_config_ops *ops = vdpa->config; | |
1330 | struct vdpa_notification_area notify; | |
c09cc2c3 | 1331 | unsigned long index = vma->vm_pgoff; |
ddd89d0a JW |
1332 | |
1333 | if (vma->vm_end - vma->vm_start != PAGE_SIZE) | |
1334 | return -EINVAL; | |
1335 | if ((vma->vm_flags & VM_SHARED) == 0) | |
1336 | return -EINVAL; | |
1337 | if (vma->vm_flags & VM_READ) | |
1338 | return -EINVAL; | |
1339 | if (index > 65535) | |
1340 | return -EINVAL; | |
1341 | if (!ops->get_vq_notification) | |
1342 | return -ENOTSUPP; | |
1343 | ||
1344 | /* To be safe and easily modelled by userspace, We only | |
1345 | * support the doorbell which sits on the page boundary and | |
1346 | * does not share the page with other registers. | |
1347 | */ | |
1348 | notify = ops->get_vq_notification(vdpa, index); | |
1349 | if (notify.addr & (PAGE_SIZE - 1)) | |
1350 | return -EINVAL; | |
1351 | if (vma->vm_end - vma->vm_start != notify.size) | |
1352 | return -ENOTSUPP; | |
1353 | ||
1c71222e | 1354 | vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); |
ddd89d0a JW |
1355 | vma->vm_ops = &vhost_vdpa_vm_ops; |
1356 | return 0; | |
1357 | } | |
4b4e4867 | 1358 | #endif /* CONFIG_MMU */ |
ddd89d0a | 1359 | |
4c8cf318 TB |
1360 | static const struct file_operations vhost_vdpa_fops = { |
1361 | .owner = THIS_MODULE, | |
1362 | .open = vhost_vdpa_open, | |
1363 | .release = vhost_vdpa_release, | |
1364 | .write_iter = vhost_vdpa_chr_write_iter, | |
1365 | .unlocked_ioctl = vhost_vdpa_unlocked_ioctl, | |
4b4e4867 | 1366 | #ifdef CONFIG_MMU |
ddd89d0a | 1367 | .mmap = vhost_vdpa_mmap, |
4b4e4867 | 1368 | #endif /* CONFIG_MMU */ |
4c8cf318 TB |
1369 | .compat_ioctl = compat_ptr_ioctl, |
1370 | }; | |
1371 | ||
1372 | static void vhost_vdpa_release_dev(struct device *device) | |
1373 | { | |
1374 | struct vhost_vdpa *v = | |
1375 | container_of(device, struct vhost_vdpa, dev); | |
1376 | ||
1377 | ida_simple_remove(&vhost_vdpa_ida, v->minor); | |
1378 | kfree(v->vqs); | |
1379 | kfree(v); | |
1380 | } | |
1381 | ||
1382 | static int vhost_vdpa_probe(struct vdpa_device *vdpa) | |
1383 | { | |
1384 | const struct vdpa_config_ops *ops = vdpa->config; | |
1385 | struct vhost_vdpa *v; | |
a9974489 | 1386 | int minor; |
3d569879 | 1387 | int i, r; |
4c8cf318 | 1388 | |
aaca8373 GD |
1389 | /* We can't support platform IOMMU device with more than 1 |
1390 | * group or as | |
1391 | */ | |
1392 | if (!ops->set_map && !ops->dma_map && | |
1393 | (vdpa->ngroups > 1 || vdpa->nas > 1)) | |
db9adcbf GD |
1394 | return -EOPNOTSUPP; |
1395 | ||
4c8cf318 TB |
1396 | v = kzalloc(sizeof(*v), GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
1397 | if (!v) | |
1398 | return -ENOMEM; | |
1399 | ||
1400 | minor = ida_simple_get(&vhost_vdpa_ida, 0, | |
1401 | VHOST_VDPA_DEV_MAX, GFP_KERNEL); | |
1402 | if (minor < 0) { | |
1403 | kfree(v); | |
1404 | return minor; | |
1405 | } | |
1406 | ||
1407 | atomic_set(&v->opened, 0); | |
1408 | v->minor = minor; | |
1409 | v->vdpa = vdpa; | |
a9974489 | 1410 | v->nvqs = vdpa->nvqs; |
4c8cf318 TB |
1411 | v->virtio_id = ops->get_device_id(vdpa); |
1412 | ||
1413 | device_initialize(&v->dev); | |
1414 | v->dev.release = vhost_vdpa_release_dev; | |
1415 | v->dev.parent = &vdpa->dev; | |
1416 | v->dev.devt = MKDEV(MAJOR(vhost_vdpa_major), minor); | |
a9974489 | 1417 | v->vqs = kmalloc_array(v->nvqs, sizeof(struct vhost_virtqueue), |
4c8cf318 TB |
1418 | GFP_KERNEL); |
1419 | if (!v->vqs) { | |
1420 | r = -ENOMEM; | |
1421 | goto err; | |
1422 | } | |
1423 | ||
1424 | r = dev_set_name(&v->dev, "vhost-vdpa-%u", minor); | |
1425 | if (r) | |
1426 | goto err; | |
1427 | ||
1428 | cdev_init(&v->cdev, &vhost_vdpa_fops); | |
1429 | v->cdev.owner = THIS_MODULE; | |
1430 | ||
1431 | r = cdev_device_add(&v->cdev, &v->dev); | |
1432 | if (r) | |
1433 | goto err; | |
1434 | ||
1435 | init_completion(&v->completion); | |
1436 | vdpa_set_drvdata(vdpa, v); | |
1437 | ||
3d569879 GD |
1438 | for (i = 0; i < VHOST_VDPA_IOTLB_BUCKETS; i++) |
1439 | INIT_HLIST_HEAD(&v->as[i]); | |
1440 | ||
4c8cf318 TB |
1441 | return 0; |
1442 | ||
1443 | err: | |
1444 | put_device(&v->dev); | |
ebe6a354 | 1445 | ida_simple_remove(&vhost_vdpa_ida, v->minor); |
4c8cf318 TB |
1446 | return r; |
1447 | } | |
1448 | ||
1449 | static void vhost_vdpa_remove(struct vdpa_device *vdpa) | |
1450 | { | |
1451 | struct vhost_vdpa *v = vdpa_get_drvdata(vdpa); | |
1452 | int opened; | |
1453 | ||
1454 | cdev_device_del(&v->cdev, &v->dev); | |
1455 | ||
1456 | do { | |
1457 | opened = atomic_cmpxchg(&v->opened, 0, 1); | |
1458 | if (!opened) | |
1459 | break; | |
1460 | wait_for_completion(&v->completion); | |
1461 | } while (1); | |
1462 | ||
1463 | put_device(&v->dev); | |
1464 | } | |
1465 | ||
1466 | static struct vdpa_driver vhost_vdpa_driver = { | |
1467 | .driver = { | |
1468 | .name = "vhost_vdpa", | |
1469 | }, | |
1470 | .probe = vhost_vdpa_probe, | |
1471 | .remove = vhost_vdpa_remove, | |
1472 | }; | |
1473 | ||
1474 | static int __init vhost_vdpa_init(void) | |
1475 | { | |
1476 | int r; | |
1477 | ||
1478 | r = alloc_chrdev_region(&vhost_vdpa_major, 0, VHOST_VDPA_DEV_MAX, | |
1479 | "vhost-vdpa"); | |
1480 | if (r) | |
1481 | goto err_alloc_chrdev; | |
1482 | ||
1483 | r = vdpa_register_driver(&vhost_vdpa_driver); | |
1484 | if (r) | |
1485 | goto err_vdpa_register_driver; | |
1486 | ||
1487 | return 0; | |
1488 | ||
1489 | err_vdpa_register_driver: | |
1490 | unregister_chrdev_region(vhost_vdpa_major, VHOST_VDPA_DEV_MAX); | |
1491 | err_alloc_chrdev: | |
1492 | return r; | |
1493 | } | |
1494 | module_init(vhost_vdpa_init); | |
1495 | ||
1496 | static void __exit vhost_vdpa_exit(void) | |
1497 | { | |
1498 | vdpa_unregister_driver(&vhost_vdpa_driver); | |
1499 | unregister_chrdev_region(vhost_vdpa_major, VHOST_VDPA_DEV_MAX); | |
1500 | } | |
1501 | module_exit(vhost_vdpa_exit); | |
1502 | ||
1503 | MODULE_VERSION("0.0.1"); | |
1504 | MODULE_LICENSE("GPL v2"); | |
1505 | MODULE_AUTHOR("Intel Corporation"); | |
1506 | MODULE_DESCRIPTION("vDPA-based vhost backend for virtio"); |