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 };
429
430 static inline struct iommufd_device *
iommufd_get_device(struct iommufd_ucmd * ucmd,u32 id)431 iommufd_get_device(struct iommufd_ucmd *ucmd, u32 id)
432 {
433 return container_of(iommufd_get_object(ucmd->ictx, id,
434 IOMMUFD_OBJ_DEVICE),
435 struct iommufd_device, obj);
436 }
437
438 void iommufd_device_destroy(struct iommufd_object *obj);
439 int iommufd_get_hw_info(struct iommufd_ucmd *ucmd);
440
441 struct iommufd_access {
442 struct iommufd_object obj;
443 struct iommufd_ctx *ictx;
444 struct iommufd_ioas *ioas;
445 struct iommufd_ioas *ioas_unpin;
446 struct mutex ioas_lock;
447 const struct iommufd_access_ops *ops;
448 void *data;
449 unsigned long iova_alignment;
450 u32 iopt_access_list_id;
451 };
452
453 int iopt_add_access(struct io_pagetable *iopt, struct iommufd_access *access);
454 void iopt_remove_access(struct io_pagetable *iopt,
455 struct iommufd_access *access,
456 u32 iopt_access_list_id);
457 void iommufd_access_destroy_object(struct iommufd_object *obj);
458
459 struct iommufd_eventq {
460 struct iommufd_object obj;
461 struct iommufd_ctx *ictx;
462 struct file *filep;
463
464 spinlock_t lock; /* protects the deliver list */
465 struct list_head deliver;
466
467 struct wait_queue_head wait_queue;
468 };
469
470 struct iommufd_attach_handle {
471 struct iommu_attach_handle handle;
472 struct iommufd_device *idev;
473 };
474
475 /* Convert an iommu attach handle to iommufd handle. */
476 #define to_iommufd_handle(hdl) container_of(hdl, struct iommufd_attach_handle, handle)
477
478 /*
479 * An iommufd_fault object represents an interface to deliver I/O page faults
480 * to the user space. These objects are created/destroyed by the user space and
481 * associated with hardware page table objects during page-table allocation.
482 */
483 struct iommufd_fault {
484 struct iommufd_eventq common;
485 struct mutex mutex; /* serializes response flows */
486 struct xarray response;
487 };
488
489 static inline struct iommufd_fault *
eventq_to_fault(struct iommufd_eventq * eventq)490 eventq_to_fault(struct iommufd_eventq *eventq)
491 {
492 return container_of(eventq, struct iommufd_fault, common);
493 }
494
495 static inline struct iommufd_fault *
iommufd_get_fault(struct iommufd_ucmd * ucmd,u32 id)496 iommufd_get_fault(struct iommufd_ucmd *ucmd, u32 id)
497 {
498 return container_of(iommufd_get_object(ucmd->ictx, id,
499 IOMMUFD_OBJ_FAULT),
500 struct iommufd_fault, common.obj);
501 }
502
503 int iommufd_fault_alloc(struct iommufd_ucmd *ucmd);
504 void iommufd_fault_destroy(struct iommufd_object *obj);
505 int iommufd_fault_iopf_handler(struct iopf_group *group);
506 void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt,
507 struct iommufd_attach_handle *handle);
508
509 /* An iommufd_vevent represents a vIOMMU event in an iommufd_veventq */
510 struct iommufd_vevent {
511 struct iommufd_vevent_header header;
512 struct list_head node; /* for iommufd_eventq::deliver */
513 ssize_t data_len;
514 u64 event_data[] __counted_by(data_len);
515 };
516
517 #define vevent_for_lost_events_header(vevent) \
518 (vevent->header.flags & IOMMU_VEVENTQ_FLAG_LOST_EVENTS)
519
520 /*
521 * An iommufd_veventq object represents an interface to deliver vIOMMU events to
522 * the user space. It is created/destroyed by the user space and associated with
523 * a vIOMMU object during the allocations.
524 */
525 struct iommufd_veventq {
526 struct iommufd_eventq common;
527 struct iommufd_viommu *viommu;
528 struct list_head node; /* for iommufd_viommu::veventqs */
529 struct iommufd_vevent lost_events_header;
530
531 unsigned int type;
532 unsigned int depth;
533
534 /* Use common.lock for protection */
535 u32 num_events;
536 u32 sequence;
537 };
538
539 static inline struct iommufd_veventq *
eventq_to_veventq(struct iommufd_eventq * eventq)540 eventq_to_veventq(struct iommufd_eventq *eventq)
541 {
542 return container_of(eventq, struct iommufd_veventq, common);
543 }
544
545 static inline struct iommufd_veventq *
iommufd_get_veventq(struct iommufd_ucmd * ucmd,u32 id)546 iommufd_get_veventq(struct iommufd_ucmd *ucmd, u32 id)
547 {
548 return container_of(iommufd_get_object(ucmd->ictx, id,
549 IOMMUFD_OBJ_VEVENTQ),
550 struct iommufd_veventq, common.obj);
551 }
552
553 int iommufd_veventq_alloc(struct iommufd_ucmd *ucmd);
554 void iommufd_veventq_destroy(struct iommufd_object *obj);
555 void iommufd_veventq_abort(struct iommufd_object *obj);
556
iommufd_vevent_handler(struct iommufd_veventq * veventq,struct iommufd_vevent * vevent)557 static inline void iommufd_vevent_handler(struct iommufd_veventq *veventq,
558 struct iommufd_vevent *vevent)
559 {
560 struct iommufd_eventq *eventq = &veventq->common;
561
562 lockdep_assert_held(&eventq->lock);
563
564 /*
565 * Remove the lost_events_header and add the new node at the same time.
566 * Note the new node can be lost_events_header, for a sequence update.
567 */
568 if (list_is_last(&veventq->lost_events_header.node, &eventq->deliver))
569 list_del(&veventq->lost_events_header.node);
570 list_add_tail(&vevent->node, &eventq->deliver);
571 vevent->header.sequence = veventq->sequence;
572 veventq->sequence = (veventq->sequence + 1) & INT_MAX;
573
574 wake_up_interruptible(&eventq->wait_queue);
575 }
576
577 static inline struct iommufd_viommu *
iommufd_get_viommu(struct iommufd_ucmd * ucmd,u32 id)578 iommufd_get_viommu(struct iommufd_ucmd *ucmd, u32 id)
579 {
580 return container_of(iommufd_get_object(ucmd->ictx, id,
581 IOMMUFD_OBJ_VIOMMU),
582 struct iommufd_viommu, obj);
583 }
584
585 static inline struct iommufd_veventq *
iommufd_viommu_find_veventq(struct iommufd_viommu * viommu,u32 type)586 iommufd_viommu_find_veventq(struct iommufd_viommu *viommu, u32 type)
587 {
588 struct iommufd_veventq *veventq, *next;
589
590 lockdep_assert_held(&viommu->veventqs_rwsem);
591
592 list_for_each_entry_safe(veventq, next, &viommu->veventqs, node) {
593 if (veventq->type == type)
594 return veventq;
595 }
596 return NULL;
597 }
598
599 int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd);
600 void iommufd_viommu_destroy(struct iommufd_object *obj);
601 int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd);
602 void iommufd_vdevice_destroy(struct iommufd_object *obj);
603
604 struct iommufd_vdevice {
605 struct iommufd_object obj;
606 struct iommufd_ctx *ictx;
607 struct iommufd_viommu *viommu;
608 struct device *dev;
609 u64 id; /* per-vIOMMU virtual ID */
610 };
611
612 #ifdef CONFIG_IOMMUFD_TEST
613 int iommufd_test(struct iommufd_ucmd *ucmd);
614 void iommufd_selftest_destroy(struct iommufd_object *obj);
615 extern size_t iommufd_test_memory_limit;
616 void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
617 unsigned int ioas_id, u64 *iova, u32 *flags);
618 bool iommufd_should_fail(void);
619 int __init iommufd_test_init(void);
620 void iommufd_test_exit(void);
621 bool iommufd_selftest_is_mock_dev(struct device *dev);
622 #else
iommufd_test_syz_conv_iova_id(struct iommufd_ucmd * ucmd,unsigned int ioas_id,u64 * iova,u32 * flags)623 static inline void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
624 unsigned int ioas_id,
625 u64 *iova, u32 *flags)
626 {
627 }
iommufd_should_fail(void)628 static inline bool iommufd_should_fail(void)
629 {
630 return false;
631 }
iommufd_test_init(void)632 static inline int __init iommufd_test_init(void)
633 {
634 return 0;
635 }
iommufd_test_exit(void)636 static inline void iommufd_test_exit(void)
637 {
638 }
iommufd_selftest_is_mock_dev(struct device * dev)639 static inline bool iommufd_selftest_is_mock_dev(struct device *dev)
640 {
641 return false;
642 }
643 #endif
644 #endif
645