1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES
3 */
4 #ifndef __IOMMUFD_PRIVATE_H
5 #define __IOMMUFD_PRIVATE_H
6
7 #include <linux/iommu.h>
8 #include <linux/iommufd.h>
9 #include <linux/iova_bitmap.h>
10 #include <linux/rwsem.h>
11 #include <linux/uaccess.h>
12 #include <linux/xarray.h>
13 #include <uapi/linux/iommufd.h>
14
15 #include "../iommu-priv.h"
16
17 struct iommu_domain;
18 struct iommu_group;
19 struct iommu_option;
20 struct iommufd_device;
21
22 struct iommufd_sw_msi_map {
23 struct list_head sw_msi_item;
24 phys_addr_t sw_msi_start;
25 phys_addr_t msi_addr;
26 unsigned int pgoff;
27 unsigned int id;
28 };
29
30 /* Bitmap of struct iommufd_sw_msi_map::id */
31 struct iommufd_sw_msi_maps {
32 DECLARE_BITMAP(bitmap, 64);
33 };
34
35 #ifdef CONFIG_IRQ_MSI_IOMMU
36 int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
37 struct iommufd_hwpt_paging *hwpt_paging,
38 struct iommufd_sw_msi_map *msi_map);
39 #endif
40
41 struct iommufd_ctx {
42 struct file *file;
43 struct xarray objects;
44 struct xarray groups;
45 wait_queue_head_t destroy_wait;
46 struct rw_semaphore ioas_creation_lock;
47
48 struct mutex sw_msi_lock;
49 struct list_head sw_msi_list;
50 unsigned int sw_msi_id;
51
52 u8 account_mode;
53 /* Compatibility with VFIO no iommu */
54 u8 no_iommu_mode;
55 struct iommufd_ioas *vfio_ioas;
56 };
57
58 /*
59 * The IOVA to PFN map. The map automatically copies the PFNs into multiple
60 * domains and permits sharing of PFNs between io_pagetable instances. This
61 * supports both a design where IOAS's are 1:1 with a domain (eg because the
62 * domain is HW customized), or where the IOAS is 1:N with multiple generic
63 * domains. The io_pagetable holds an interval tree of iopt_areas which point
64 * to shared iopt_pages which hold the pfns mapped to the page table.
65 *
66 * The locking order is domains_rwsem -> iova_rwsem -> pages::mutex
67 */
68 struct io_pagetable {
69 struct rw_semaphore domains_rwsem;
70 struct xarray domains;
71 struct xarray access_list;
72 unsigned int next_domain_id;
73
74 struct rw_semaphore iova_rwsem;
75 struct rb_root_cached area_itree;
76 /* IOVA that cannot become reserved, struct iopt_allowed */
77 struct rb_root_cached allowed_itree;
78 /* IOVA that cannot be allocated, struct iopt_reserved */
79 struct rb_root_cached reserved_itree;
80 u8 disable_large_pages;
81 unsigned long iova_alignment;
82 };
83
84 void iopt_init_table(struct io_pagetable *iopt);
85 void iopt_destroy_table(struct io_pagetable *iopt);
86 int iopt_get_pages(struct io_pagetable *iopt, unsigned long iova,
87 unsigned long length, struct list_head *pages_list);
88 void iopt_free_pages_list(struct list_head *pages_list);
89 enum {
90 IOPT_ALLOC_IOVA = 1 << 0,
91 };
92 int iopt_map_user_pages(struct iommufd_ctx *ictx, struct io_pagetable *iopt,
93 unsigned long *iova, void __user *uptr,
94 unsigned long length, int iommu_prot,
95 unsigned int flags);
96 int iopt_map_file_pages(struct iommufd_ctx *ictx, struct io_pagetable *iopt,
97 unsigned long *iova, struct file *file,
98 unsigned long start, unsigned long length,
99 int iommu_prot, unsigned int flags);
100 int iopt_map_pages(struct io_pagetable *iopt, struct list_head *pages_list,
101 unsigned long length, unsigned long *dst_iova,
102 int iommu_prot, unsigned int flags);
103 int iopt_unmap_iova(struct io_pagetable *iopt, unsigned long iova,
104 unsigned long length, unsigned long *unmapped);
105 int iopt_unmap_all(struct io_pagetable *iopt, unsigned long *unmapped);
106
107 int iopt_read_and_clear_dirty_data(struct io_pagetable *iopt,
108 struct iommu_domain *domain,
109 unsigned long flags,
110 struct iommu_hwpt_get_dirty_bitmap *bitmap);
111 int iopt_set_dirty_tracking(struct io_pagetable *iopt,
112 struct iommu_domain *domain, bool enable);
113
114 void iommufd_access_notify_unmap(struct io_pagetable *iopt, unsigned long iova,
115 unsigned long length);
116 int iopt_table_add_domain(struct io_pagetable *iopt,
117 struct iommu_domain *domain);
118 void iopt_table_remove_domain(struct io_pagetable *iopt,
119 struct iommu_domain *domain);
120 int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt,
121 struct device *dev,
122 phys_addr_t *sw_msi_start);
123 int iopt_set_allow_iova(struct io_pagetable *iopt,
124 struct rb_root_cached *allowed_iova);
125 int iopt_reserve_iova(struct io_pagetable *iopt, unsigned long start,
126 unsigned long last, void *owner);
127 void iopt_remove_reserved_iova(struct io_pagetable *iopt, void *owner);
128 int iopt_cut_iova(struct io_pagetable *iopt, unsigned long *iovas,
129 size_t num_iovas);
130 void iopt_enable_large_pages(struct io_pagetable *iopt);
131 int iopt_disable_large_pages(struct io_pagetable *iopt);
132
133 struct iommufd_ucmd {
134 struct iommufd_ctx *ictx;
135 void __user *ubuffer;
136 u32 user_size;
137 void *cmd;
138 };
139
140 int iommufd_vfio_ioctl(struct iommufd_ctx *ictx, unsigned int cmd,
141 unsigned long arg);
142
143 /* Copy the response in ucmd->cmd back to userspace. */
iommufd_ucmd_respond(struct iommufd_ucmd * ucmd,size_t cmd_len)144 static inline int iommufd_ucmd_respond(struct iommufd_ucmd *ucmd,
145 size_t cmd_len)
146 {
147 if (copy_to_user(ucmd->ubuffer, ucmd->cmd,
148 min_t(size_t, ucmd->user_size, cmd_len)))
149 return -EFAULT;
150 return 0;
151 }
152
iommufd_lock_obj(struct iommufd_object * obj)153 static inline bool iommufd_lock_obj(struct iommufd_object *obj)
154 {
155 if (!refcount_inc_not_zero(&obj->users))
156 return false;
157 if (!refcount_inc_not_zero(&obj->shortterm_users)) {
158 /*
159 * If the caller doesn't already have a ref on obj this must be
160 * called under the xa_lock. Otherwise the caller is holding a
161 * ref on users. Thus it cannot be one before this decrement.
162 */
163 refcount_dec(&obj->users);
164 return false;
165 }
166 return true;
167 }
168
169 struct iommufd_object *iommufd_get_object(struct iommufd_ctx *ictx, u32 id,
170 enum iommufd_object_type type);
iommufd_put_object(struct iommufd_ctx * ictx,struct iommufd_object * obj)171 static inline void iommufd_put_object(struct iommufd_ctx *ictx,
172 struct iommufd_object *obj)
173 {
174 /*
175 * Users first, then shortterm so that REMOVE_WAIT_SHORTTERM never sees
176 * a spurious !0 users with a 0 shortterm_users.
177 */
178 refcount_dec(&obj->users);
179 if (refcount_dec_and_test(&obj->shortterm_users))
180 wake_up_interruptible_all(&ictx->destroy_wait);
181 }
182
183 void iommufd_object_abort(struct iommufd_ctx *ictx, struct iommufd_object *obj);
184 void iommufd_object_abort_and_destroy(struct iommufd_ctx *ictx,
185 struct iommufd_object *obj);
186 void iommufd_object_finalize(struct iommufd_ctx *ictx,
187 struct iommufd_object *obj);
188
189 enum {
190 REMOVE_WAIT_SHORTTERM = 1,
191 };
192 int iommufd_object_remove(struct iommufd_ctx *ictx,
193 struct iommufd_object *to_destroy, u32 id,
194 unsigned int flags);
195
196 /*
197 * The caller holds a users refcount and wants to destroy the object. At this
198 * point the caller has no shortterm_users reference and at least the xarray
199 * will be holding one.
200 */
iommufd_object_destroy_user(struct iommufd_ctx * ictx,struct iommufd_object * obj)201 static inline void iommufd_object_destroy_user(struct iommufd_ctx *ictx,
202 struct iommufd_object *obj)
203 {
204 int ret;
205
206 ret = iommufd_object_remove(ictx, obj, obj->id, REMOVE_WAIT_SHORTTERM);
207
208 /*
209 * If there is a bug and we couldn't destroy the object then we did put
210 * back the caller's users refcount and will eventually try to free it
211 * again during close.
212 */
213 WARN_ON(ret);
214 }
215
216 /*
217 * The HWPT allocated by autodomains is used in possibly many devices and
218 * is automatically destroyed when its refcount reaches zero.
219 *
220 * If userspace uses the HWPT manually, even for a short term, then it will
221 * disrupt this refcounting and the auto-free in the kernel will not work.
222 * Userspace that tries to use the automatically allocated HWPT must be careful
223 * to ensure that it is consistently destroyed, eg by not racing accesses
224 * and by not attaching an automatic HWPT to a device manually.
225 */
226 static inline void
iommufd_object_put_and_try_destroy(struct iommufd_ctx * ictx,struct iommufd_object * obj)227 iommufd_object_put_and_try_destroy(struct iommufd_ctx *ictx,
228 struct iommufd_object *obj)
229 {
230 iommufd_object_remove(ictx, obj, obj->id, 0);
231 }
232
233 #define __iommufd_object_alloc(ictx, ptr, type, obj) \
234 container_of(_iommufd_object_alloc( \
235 ictx, \
236 sizeof(*(ptr)) + BUILD_BUG_ON_ZERO( \
237 offsetof(typeof(*(ptr)), \
238 obj) != 0), \
239 type), \
240 typeof(*(ptr)), obj)
241
242 #define iommufd_object_alloc(ictx, ptr, type) \
243 __iommufd_object_alloc(ictx, ptr, type, obj)
244
245 /*
246 * The IO Address Space (IOAS) pagetable is a virtual page table backed by the
247 * io_pagetable object. It is a user controlled mapping of IOVA -> PFNs. The
248 * mapping is copied into all of the associated domains and made available to
249 * in-kernel users.
250 *
251 * Every iommu_domain that is created is wrapped in a iommufd_hw_pagetable
252 * object. When we go to attach a device to an IOAS we need to get an
253 * iommu_domain and wrapping iommufd_hw_pagetable for it.
254 *
255 * An iommu_domain & iommfd_hw_pagetable will be automatically selected
256 * for a device based on the hwpt_list. If no suitable iommu_domain
257 * is found a new iommu_domain will be created.
258 */
259 struct iommufd_ioas {
260 struct iommufd_object obj;
261 struct io_pagetable iopt;
262 struct mutex mutex;
263 struct list_head hwpt_list;
264 };
265
iommufd_get_ioas(struct iommufd_ctx * ictx,u32 id)266 static inline struct iommufd_ioas *iommufd_get_ioas(struct iommufd_ctx *ictx,
267 u32 id)
268 {
269 return container_of(iommufd_get_object(ictx, id,
270 IOMMUFD_OBJ_IOAS),
271 struct iommufd_ioas, obj);
272 }
273
274 struct iommufd_ioas *iommufd_ioas_alloc(struct iommufd_ctx *ictx);
275 int iommufd_ioas_alloc_ioctl(struct iommufd_ucmd *ucmd);
276 void iommufd_ioas_destroy(struct iommufd_object *obj);
277 int iommufd_ioas_iova_ranges(struct iommufd_ucmd *ucmd);
278 int iommufd_ioas_allow_iovas(struct iommufd_ucmd *ucmd);
279 int iommufd_ioas_map(struct iommufd_ucmd *ucmd);
280 int iommufd_ioas_map_file(struct iommufd_ucmd *ucmd);
281 int iommufd_ioas_change_process(struct iommufd_ucmd *ucmd);
282 int iommufd_ioas_copy(struct iommufd_ucmd *ucmd);
283 int iommufd_ioas_unmap(struct iommufd_ucmd *ucmd);
284 int iommufd_ioas_option(struct iommufd_ucmd *ucmd);
285 int iommufd_option_rlimit_mode(struct iommu_option *cmd,
286 struct iommufd_ctx *ictx);
287
288 int iommufd_vfio_ioas(struct iommufd_ucmd *ucmd);
289 int iommufd_check_iova_range(struct io_pagetable *iopt,
290 struct iommu_hwpt_get_dirty_bitmap *bitmap);
291
292 /*
293 * A HW pagetable is called an iommu_domain inside the kernel. This user object
294 * allows directly creating and inspecting the domains. Domains that have kernel
295 * owned page tables will be associated with an iommufd_ioas that provides the
296 * IOVA to PFN map.
297 */
298 struct iommufd_hw_pagetable {
299 struct iommufd_object obj;
300 struct iommu_domain *domain;
301 struct iommufd_fault *fault;
302 bool pasid_compat : 1;
303 };
304
305 struct iommufd_hwpt_paging {
306 struct iommufd_hw_pagetable common;
307 struct iommufd_ioas *ioas;
308 bool auto_domain : 1;
309 bool enforce_cache_coherency : 1;
310 bool nest_parent : 1;
311 /* Head at iommufd_ioas::hwpt_list */
312 struct list_head hwpt_item;
313 struct iommufd_sw_msi_maps present_sw_msi;
314 };
315
316 struct iommufd_hwpt_nested {
317 struct iommufd_hw_pagetable common;
318 struct iommufd_hwpt_paging *parent;
319 struct iommufd_viommu *viommu;
320 };
321
hwpt_is_paging(struct iommufd_hw_pagetable * hwpt)322 static inline bool hwpt_is_paging(struct iommufd_hw_pagetable *hwpt)
323 {
324 return hwpt->obj.type == IOMMUFD_OBJ_HWPT_PAGING;
325 }
326
327 static inline struct iommufd_hwpt_paging *
to_hwpt_paging(struct iommufd_hw_pagetable * hwpt)328 to_hwpt_paging(struct iommufd_hw_pagetable *hwpt)
329 {
330 return container_of(hwpt, struct iommufd_hwpt_paging, common);
331 }
332
333 static inline struct iommufd_hwpt_nested *
to_hwpt_nested(struct iommufd_hw_pagetable * hwpt)334 to_hwpt_nested(struct iommufd_hw_pagetable *hwpt)
335 {
336 return container_of(hwpt, struct iommufd_hwpt_nested, common);
337 }
338
339 static inline struct iommufd_hwpt_paging *
find_hwpt_paging(struct iommufd_hw_pagetable * hwpt)340 find_hwpt_paging(struct iommufd_hw_pagetable *hwpt)
341 {
342 switch (hwpt->obj.type) {
343 case IOMMUFD_OBJ_HWPT_PAGING:
344 return to_hwpt_paging(hwpt);
345 case IOMMUFD_OBJ_HWPT_NESTED:
346 return to_hwpt_nested(hwpt)->parent;
347 default:
348 return NULL;
349 }
350 }
351
352 static inline struct iommufd_hwpt_paging *
iommufd_get_hwpt_paging(struct iommufd_ucmd * ucmd,u32 id)353 iommufd_get_hwpt_paging(struct iommufd_ucmd *ucmd, u32 id)
354 {
355 return container_of(iommufd_get_object(ucmd->ictx, id,
356 IOMMUFD_OBJ_HWPT_PAGING),
357 struct iommufd_hwpt_paging, common.obj);
358 }
359
360 static inline struct iommufd_hw_pagetable *
iommufd_get_hwpt_nested(struct iommufd_ucmd * ucmd,u32 id)361 iommufd_get_hwpt_nested(struct iommufd_ucmd *ucmd, u32 id)
362 {
363 return container_of(iommufd_get_object(ucmd->ictx, id,
364 IOMMUFD_OBJ_HWPT_NESTED),
365 struct iommufd_hw_pagetable, obj);
366 }
367
368 int iommufd_hwpt_set_dirty_tracking(struct iommufd_ucmd *ucmd);
369 int iommufd_hwpt_get_dirty_bitmap(struct iommufd_ucmd *ucmd);
370
371 struct iommufd_hwpt_paging *
372 iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
373 struct iommufd_device *idev, ioasid_t pasid,
374 u32 flags, bool immediate_attach,
375 const struct iommu_user_data *user_data);
376 int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
377 struct iommufd_device *idev, ioasid_t pasid);
378 struct iommufd_hw_pagetable *
379 iommufd_hw_pagetable_detach(struct iommufd_device *idev, ioasid_t pasid);
380 void iommufd_hwpt_paging_destroy(struct iommufd_object *obj);
381 void iommufd_hwpt_paging_abort(struct iommufd_object *obj);
382 void iommufd_hwpt_nested_destroy(struct iommufd_object *obj);
383 void iommufd_hwpt_nested_abort(struct iommufd_object *obj);
384 int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd);
385 int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd);
386
iommufd_hw_pagetable_put(struct iommufd_ctx * ictx,struct iommufd_hw_pagetable * hwpt)387 static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx,
388 struct iommufd_hw_pagetable *hwpt)
389 {
390 if (hwpt->obj.type == IOMMUFD_OBJ_HWPT_PAGING) {
391 struct iommufd_hwpt_paging *hwpt_paging = to_hwpt_paging(hwpt);
392
393 lockdep_assert_not_held(&hwpt_paging->ioas->mutex);
394
395 if (hwpt_paging->auto_domain) {
396 iommufd_object_put_and_try_destroy(ictx, &hwpt->obj);
397 return;
398 }
399 }
400 refcount_dec(&hwpt->obj.users);
401 }
402
403 struct iommufd_attach;
404
405 struct iommufd_group {
406 struct kref ref;
407 struct mutex lock;
408 struct iommufd_ctx *ictx;
409 struct iommu_group *group;
410 struct xarray pasid_attach;
411 struct iommufd_sw_msi_maps required_sw_msi;
412 phys_addr_t sw_msi_start;
413 };
414
415 /*
416 * A iommufd_device object represents the binding relationship between a
417 * consuming driver and the iommufd. These objects are created/destroyed by
418 * external drivers, not by userspace.
419 */
420 struct iommufd_device {
421 struct iommufd_object obj;
422 struct iommufd_ctx *ictx;
423 struct iommufd_group *igroup;
424 struct list_head group_item;
425 /* always the physical device */
426 struct device *dev;
427 bool enforce_cache_coherency;
428 /* protect iopf_enabled counter */
429 struct mutex iopf_lock;
430 unsigned int iopf_enabled;
431 };
432
433 static inline struct iommufd_device *
iommufd_get_device(struct iommufd_ucmd * ucmd,u32 id)434 iommufd_get_device(struct iommufd_ucmd *ucmd, u32 id)
435 {
436 return container_of(iommufd_get_object(ucmd->ictx, id,
437 IOMMUFD_OBJ_DEVICE),
438 struct iommufd_device, obj);
439 }
440
441 void iommufd_device_destroy(struct iommufd_object *obj);
442 int iommufd_get_hw_info(struct iommufd_ucmd *ucmd);
443
444 struct iommufd_access {
445 struct iommufd_object obj;
446 struct iommufd_ctx *ictx;
447 struct iommufd_ioas *ioas;
448 struct iommufd_ioas *ioas_unpin;
449 struct mutex ioas_lock;
450 const struct iommufd_access_ops *ops;
451 void *data;
452 unsigned long iova_alignment;
453 u32 iopt_access_list_id;
454 };
455
456 int iopt_add_access(struct io_pagetable *iopt, struct iommufd_access *access);
457 void iopt_remove_access(struct io_pagetable *iopt,
458 struct iommufd_access *access,
459 u32 iopt_access_list_id);
460 void iommufd_access_destroy_object(struct iommufd_object *obj);
461
462 struct iommufd_eventq {
463 struct iommufd_object obj;
464 struct iommufd_ctx *ictx;
465 struct file *filep;
466
467 spinlock_t lock; /* protects the deliver list */
468 struct list_head deliver;
469
470 struct wait_queue_head wait_queue;
471 };
472
473 struct iommufd_attach_handle {
474 struct iommu_attach_handle handle;
475 struct iommufd_device *idev;
476 };
477
478 /* Convert an iommu attach handle to iommufd handle. */
479 #define to_iommufd_handle(hdl) container_of(hdl, struct iommufd_attach_handle, handle)
480
481 /*
482 * An iommufd_fault object represents an interface to deliver I/O page faults
483 * to the user space. These objects are created/destroyed by the user space and
484 * associated with hardware page table objects during page-table allocation.
485 */
486 struct iommufd_fault {
487 struct iommufd_eventq common;
488 struct mutex mutex; /* serializes response flows */
489 struct xarray response;
490 };
491
492 static inline struct iommufd_fault *
eventq_to_fault(struct iommufd_eventq * eventq)493 eventq_to_fault(struct iommufd_eventq *eventq)
494 {
495 return container_of(eventq, struct iommufd_fault, common);
496 }
497
498 static inline struct iommufd_fault *
iommufd_get_fault(struct iommufd_ucmd * ucmd,u32 id)499 iommufd_get_fault(struct iommufd_ucmd *ucmd, u32 id)
500 {
501 return container_of(iommufd_get_object(ucmd->ictx, id,
502 IOMMUFD_OBJ_FAULT),
503 struct iommufd_fault, common.obj);
504 }
505
506 int iommufd_fault_alloc(struct iommufd_ucmd *ucmd);
507 void iommufd_fault_destroy(struct iommufd_object *obj);
508 int iommufd_fault_iopf_handler(struct iopf_group *group);
509
510 int iommufd_fault_iopf_enable(struct iommufd_device *idev);
511 void iommufd_fault_iopf_disable(struct iommufd_device *idev);
512 void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt,
513 struct iommufd_attach_handle *handle);
514
515 /* An iommufd_vevent represents a vIOMMU event in an iommufd_veventq */
516 struct iommufd_vevent {
517 struct iommufd_vevent_header header;
518 struct list_head node; /* for iommufd_eventq::deliver */
519 ssize_t data_len;
520 u64 event_data[] __counted_by(data_len);
521 };
522
523 #define vevent_for_lost_events_header(vevent) \
524 (vevent->header.flags & IOMMU_VEVENTQ_FLAG_LOST_EVENTS)
525
526 /*
527 * An iommufd_veventq object represents an interface to deliver vIOMMU events to
528 * the user space. It is created/destroyed by the user space and associated with
529 * a vIOMMU object during the allocations.
530 */
531 struct iommufd_veventq {
532 struct iommufd_eventq common;
533 struct iommufd_viommu *viommu;
534 struct list_head node; /* for iommufd_viommu::veventqs */
535 struct iommufd_vevent lost_events_header;
536
537 unsigned int type;
538 unsigned int depth;
539
540 /* Use common.lock for protection */
541 u32 num_events;
542 u32 sequence;
543 };
544
545 static inline struct iommufd_veventq *
eventq_to_veventq(struct iommufd_eventq * eventq)546 eventq_to_veventq(struct iommufd_eventq *eventq)
547 {
548 return container_of(eventq, struct iommufd_veventq, common);
549 }
550
551 static inline struct iommufd_veventq *
iommufd_get_veventq(struct iommufd_ucmd * ucmd,u32 id)552 iommufd_get_veventq(struct iommufd_ucmd *ucmd, u32 id)
553 {
554 return container_of(iommufd_get_object(ucmd->ictx, id,
555 IOMMUFD_OBJ_VEVENTQ),
556 struct iommufd_veventq, common.obj);
557 }
558
559 int iommufd_veventq_alloc(struct iommufd_ucmd *ucmd);
560 void iommufd_veventq_destroy(struct iommufd_object *obj);
561 void iommufd_veventq_abort(struct iommufd_object *obj);
562
iommufd_vevent_handler(struct iommufd_veventq * veventq,struct iommufd_vevent * vevent)563 static inline void iommufd_vevent_handler(struct iommufd_veventq *veventq,
564 struct iommufd_vevent *vevent)
565 {
566 struct iommufd_eventq *eventq = &veventq->common;
567
568 lockdep_assert_held(&eventq->lock);
569
570 /*
571 * Remove the lost_events_header and add the new node at the same time.
572 * Note the new node can be lost_events_header, for a sequence update.
573 */
574 if (list_is_last(&veventq->lost_events_header.node, &eventq->deliver))
575 list_del(&veventq->lost_events_header.node);
576 list_add_tail(&vevent->node, &eventq->deliver);
577 vevent->header.sequence = veventq->sequence;
578 veventq->sequence = (veventq->sequence + 1) & INT_MAX;
579
580 wake_up_interruptible(&eventq->wait_queue);
581 }
582
583 static inline struct iommufd_viommu *
iommufd_get_viommu(struct iommufd_ucmd * ucmd,u32 id)584 iommufd_get_viommu(struct iommufd_ucmd *ucmd, u32 id)
585 {
586 return container_of(iommufd_get_object(ucmd->ictx, id,
587 IOMMUFD_OBJ_VIOMMU),
588 struct iommufd_viommu, obj);
589 }
590
591 static inline struct iommufd_veventq *
iommufd_viommu_find_veventq(struct iommufd_viommu * viommu,u32 type)592 iommufd_viommu_find_veventq(struct iommufd_viommu *viommu, u32 type)
593 {
594 struct iommufd_veventq *veventq, *next;
595
596 lockdep_assert_held(&viommu->veventqs_rwsem);
597
598 list_for_each_entry_safe(veventq, next, &viommu->veventqs, node) {
599 if (veventq->type == type)
600 return veventq;
601 }
602 return NULL;
603 }
604
605 int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd);
606 void iommufd_viommu_destroy(struct iommufd_object *obj);
607 int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd);
608 void iommufd_vdevice_destroy(struct iommufd_object *obj);
609
610 struct iommufd_vdevice {
611 struct iommufd_object obj;
612 struct iommufd_ctx *ictx;
613 struct iommufd_viommu *viommu;
614 struct device *dev;
615 u64 id; /* per-vIOMMU virtual ID */
616 };
617
618 #ifdef CONFIG_IOMMUFD_TEST
619 int iommufd_test(struct iommufd_ucmd *ucmd);
620 void iommufd_selftest_destroy(struct iommufd_object *obj);
621 extern size_t iommufd_test_memory_limit;
622 void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
623 unsigned int ioas_id, u64 *iova, u32 *flags);
624 bool iommufd_should_fail(void);
625 int __init iommufd_test_init(void);
626 void iommufd_test_exit(void);
627 bool iommufd_selftest_is_mock_dev(struct device *dev);
628 #else
iommufd_test_syz_conv_iova_id(struct iommufd_ucmd * ucmd,unsigned int ioas_id,u64 * iova,u32 * flags)629 static inline void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
630 unsigned int ioas_id,
631 u64 *iova, u32 *flags)
632 {
633 }
iommufd_should_fail(void)634 static inline bool iommufd_should_fail(void)
635 {
636 return false;
637 }
iommufd_test_init(void)638 static inline int __init iommufd_test_init(void)
639 {
640 return 0;
641 }
iommufd_test_exit(void)642 static inline void iommufd_test_exit(void)
643 {
644 }
iommufd_selftest_is_mock_dev(struct device * dev)645 static inline bool iommufd_selftest_is_mock_dev(struct device *dev)
646 {
647 return false;
648 }
649 #endif
650 #endif
651