Lines Matching refs:device
60 int vfio_assign_device_set(struct vfio_device *device, void *set_id) in vfio_assign_device_set() argument
103 device->dev_set = dev_set; in vfio_assign_device_set()
104 list_add_tail(&device->dev_set_list, &dev_set->device_list); in vfio_assign_device_set()
110 static void vfio_release_device_set(struct vfio_device *device) in vfio_release_device_set() argument
112 struct vfio_device_set *dev_set = device->dev_set; in vfio_release_device_set()
118 list_del(&device->dev_set_list); in vfio_release_device_set()
146 struct device *dev) in vfio_find_device_in_devset()
163 void vfio_device_put_registration(struct vfio_device *device) in vfio_device_put_registration() argument
165 if (refcount_dec_and_test(&device->refcount)) in vfio_device_put_registration()
166 complete(&device->comp); in vfio_device_put_registration()
169 bool vfio_device_try_get_registration(struct vfio_device *device) in vfio_device_try_get_registration() argument
171 return refcount_inc_not_zero(&device->refcount); in vfio_device_try_get_registration()
178 static void vfio_device_release(struct device *dev) in vfio_device_release()
180 struct vfio_device *device = in vfio_device_release() local
181 container_of(dev, struct vfio_device, device); in vfio_device_release()
183 vfio_release_device_set(device); in vfio_device_release()
184 ida_free(&vfio.device_ida, device->index); in vfio_device_release()
186 if (device->ops->release) in vfio_device_release()
187 device->ops->release(device); in vfio_device_release()
189 kvfree(device); in vfio_device_release()
192 static int vfio_init_device(struct vfio_device *device, struct device *dev,
207 struct vfio_device *_vfio_alloc_device(size_t size, struct device *dev, in _vfio_alloc_device()
210 struct vfio_device *device; in _vfio_alloc_device() local
216 device = kvzalloc(size, GFP_KERNEL); in _vfio_alloc_device()
217 if (!device) in _vfio_alloc_device()
220 ret = vfio_init_device(device, dev, ops); in _vfio_alloc_device()
223 return device; in _vfio_alloc_device()
226 kvfree(device); in _vfio_alloc_device()
234 static int vfio_init_device(struct vfio_device *device, struct device *dev, in vfio_init_device() argument
245 device->index = ret; in vfio_init_device()
246 init_completion(&device->comp); in vfio_init_device()
247 device->dev = dev; in vfio_init_device()
248 device->ops = ops; in vfio_init_device()
251 ret = ops->init(device); in vfio_init_device()
256 device_initialize(&device->device); in vfio_init_device()
257 device->device.release = vfio_device_release; in vfio_init_device()
258 device->device.class = vfio.device_class; in vfio_init_device()
259 device->device.parent = device->dev; in vfio_init_device()
263 vfio_release_device_set(device); in vfio_init_device()
264 ida_free(&vfio.device_ida, device->index); in vfio_init_device()
268 static int __vfio_register_dev(struct vfio_device *device, in __vfio_register_dev() argument
274 (!device->ops->bind_iommufd || in __vfio_register_dev()
275 !device->ops->unbind_iommufd || in __vfio_register_dev()
276 !device->ops->attach_ioas || in __vfio_register_dev()
277 !device->ops->detach_ioas))) in __vfio_register_dev()
284 if (!device->dev_set) in __vfio_register_dev()
285 vfio_assign_device_set(device, device); in __vfio_register_dev()
287 ret = dev_set_name(&device->device, "vfio%d", device->index); in __vfio_register_dev()
291 ret = vfio_device_set_group(device, type); in __vfio_register_dev()
300 if (type == VFIO_IOMMU && !vfio_device_is_noiommu(device) && in __vfio_register_dev()
301 !device_iommu_capable(device->dev, IOMMU_CAP_CACHE_COHERENCY)) { in __vfio_register_dev()
306 ret = vfio_device_add(device); in __vfio_register_dev()
311 refcount_set(&device->refcount, 1); in __vfio_register_dev()
313 vfio_device_group_register(device); in __vfio_register_dev()
317 vfio_device_remove_group(device); in __vfio_register_dev()
321 int vfio_register_group_dev(struct vfio_device *device) in vfio_register_group_dev() argument
323 return __vfio_register_dev(device, VFIO_IOMMU); in vfio_register_group_dev()
331 int vfio_register_emulated_iommu_dev(struct vfio_device *device) in vfio_register_emulated_iommu_dev() argument
333 return __vfio_register_dev(device, VFIO_EMULATED_IOMMU); in vfio_register_emulated_iommu_dev()
340 void vfio_unregister_group_dev(struct vfio_device *device) in vfio_unregister_group_dev() argument
350 vfio_device_group_unregister(device); in vfio_unregister_group_dev()
356 vfio_device_del(device); in vfio_unregister_group_dev()
358 vfio_device_put_registration(device); in vfio_unregister_group_dev()
359 rc = try_wait_for_completion(&device->comp); in vfio_unregister_group_dev()
361 if (device->ops->request) in vfio_unregister_group_dev()
362 device->ops->request(device, i++); in vfio_unregister_group_dev()
365 rc = wait_for_completion_timeout(&device->comp, in vfio_unregister_group_dev()
369 &device->comp, HZ * 10); in vfio_unregister_group_dev()
372 dev_warn(device->dev, in vfio_unregister_group_dev()
382 vfio_device_remove_group(device); in vfio_unregister_group_dev()
387 void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm) in vfio_device_get_kvm_safe() argument
393 lockdep_assert_held(&device->dev_set->lock); in vfio_device_get_kvm_safe()
415 device->put_kvm = pfn; in vfio_device_get_kvm_safe()
416 device->kvm = kvm; in vfio_device_get_kvm_safe()
419 void vfio_device_put_kvm(struct vfio_device *device) in vfio_device_put_kvm() argument
421 lockdep_assert_held(&device->dev_set->lock); in vfio_device_put_kvm()
423 if (!device->kvm) in vfio_device_put_kvm()
426 if (WARN_ON(!device->put_kvm)) in vfio_device_put_kvm()
429 device->put_kvm(device->kvm); in vfio_device_put_kvm()
430 device->put_kvm = NULL; in vfio_device_put_kvm()
434 device->kvm = NULL; in vfio_device_put_kvm()
439 static bool vfio_assert_device_open(struct vfio_device *device) in vfio_assert_device_open() argument
441 return !WARN_ON_ONCE(!READ_ONCE(device->open_count)); in vfio_assert_device_open()
445 vfio_allocate_device_file(struct vfio_device *device) in vfio_allocate_device_file() argument
453 df->device = device; in vfio_allocate_device_file()
461 struct vfio_device *device = df->device; in vfio_df_device_first_open() local
465 lockdep_assert_held(&device->dev_set->lock); in vfio_df_device_first_open()
467 if (!try_module_get(device->dev->driver->owner)) in vfio_df_device_first_open()
473 ret = vfio_device_group_use_iommu(device); in vfio_df_device_first_open()
477 if (device->ops->open_device) { in vfio_df_device_first_open()
478 ret = device->ops->open_device(device); in vfio_df_device_first_open()
488 vfio_device_group_unuse_iommu(device); in vfio_df_device_first_open()
490 module_put(device->dev->driver->owner); in vfio_df_device_first_open()
496 struct vfio_device *device = df->device; in vfio_df_device_last_close() local
499 lockdep_assert_held(&device->dev_set->lock); in vfio_df_device_last_close()
501 if (device->ops->close_device) in vfio_df_device_last_close()
502 device->ops->close_device(device); in vfio_df_device_last_close()
506 vfio_device_group_unuse_iommu(device); in vfio_df_device_last_close()
507 module_put(device->dev->driver->owner); in vfio_df_device_last_close()
512 struct vfio_device *device = df->device; in vfio_df_open() local
515 lockdep_assert_held(&device->dev_set->lock); in vfio_df_open()
521 if (device->open_count != 0 && !df->group) in vfio_df_open()
524 device->open_count++; in vfio_df_open()
525 if (device->open_count == 1) { in vfio_df_open()
528 device->open_count--; in vfio_df_open()
536 struct vfio_device *device = df->device; in vfio_df_close() local
538 lockdep_assert_held(&device->dev_set->lock); in vfio_df_close()
540 vfio_assert_device_open(device); in vfio_df_close()
541 if (device->open_count == 1) in vfio_df_close()
543 device->open_count--; in vfio_df_close()
550 static inline int vfio_device_pm_runtime_get(struct vfio_device *device) in vfio_device_pm_runtime_get() argument
552 struct device *dev = device->dev; in vfio_device_pm_runtime_get()
571 static inline void vfio_device_pm_runtime_put(struct vfio_device *device) in vfio_device_pm_runtime_put() argument
573 struct device *dev = device->dev; in vfio_device_pm_runtime_put()
585 struct vfio_device *device = df->device; in vfio_device_fops_release() local
592 vfio_device_put_registration(device); in vfio_device_fops_release()
614 int vfio_mig_get_next_state(struct vfio_device *device, in vfio_mig_get_next_state() argument
786 (state_flags_table[cur_fsm] & device->migration_flags) != in vfio_mig_get_next_state()
791 (state_flags_table[new_fsm] & device->migration_flags) != in vfio_mig_get_next_state()
801 while ((state_flags_table[*next_fsm] & device->migration_flags) != in vfio_mig_get_next_state()
840 vfio_ioctl_device_feature_mig_device_state(struct vfio_device *device, in vfio_ioctl_device_feature_mig_device_state() argument
850 if (!device->mig_ops) in vfio_ioctl_device_feature_mig_device_state()
866 ret = device->mig_ops->migration_get_state(device, in vfio_ioctl_device_feature_mig_device_state()
875 filp = device->mig_ops->migration_set_state(device, mig.device_state); in vfio_ioctl_device_feature_mig_device_state()
890 vfio_ioctl_device_feature_migration_data_size(struct vfio_device *device, in vfio_ioctl_device_feature_migration_data_size() argument
898 if (!device->mig_ops) in vfio_ioctl_device_feature_migration_data_size()
906 ret = device->mig_ops->migration_get_data_size(device, &stop_copy_length); in vfio_ioctl_device_feature_migration_data_size()
917 static int vfio_ioctl_device_feature_migration(struct vfio_device *device, in vfio_ioctl_device_feature_migration() argument
922 .flags = device->migration_flags, in vfio_ioctl_device_feature_migration()
926 if (!device->mig_ops) in vfio_ioctl_device_feature_migration()
990 vfio_ioctl_device_feature_logging_start(struct vfio_device *device, in vfio_ioctl_device_feature_logging_start() argument
1006 if (!device->log_ops) in vfio_ioctl_device_feature_logging_start()
1059 ret = device->log_ops->log_start(device, &root, nnodes, in vfio_ioctl_device_feature_logging_start()
1066 device->log_ops->log_stop(device); in vfio_ioctl_device_feature_logging_start()
1075 vfio_ioctl_device_feature_logging_stop(struct vfio_device *device, in vfio_ioctl_device_feature_logging_stop() argument
1081 if (!device->log_ops) in vfio_ioctl_device_feature_logging_stop()
1089 return device->log_ops->log_stop(device); in vfio_ioctl_device_feature_logging_stop()
1096 struct vfio_device *device = opaque; in vfio_device_log_read_and_clear() local
1098 return device->log_ops->log_read_and_clear(device, iova, length, iter); in vfio_device_log_read_and_clear()
1102 vfio_ioctl_device_feature_logging_report(struct vfio_device *device, in vfio_ioctl_device_feature_logging_report() argument
1114 if (!device->log_ops) in vfio_ioctl_device_feature_logging_report()
1139 ret = iova_bitmap_for_each(iter, device, in vfio_ioctl_device_feature_logging_report()
1146 static int vfio_ioctl_device_feature(struct vfio_device *device, in vfio_ioctl_device_feature() argument
1173 device, feature.flags, arg->data, in vfio_ioctl_device_feature()
1177 device, feature.flags, arg->data, in vfio_ioctl_device_feature()
1181 device, feature.flags, arg->data, in vfio_ioctl_device_feature()
1185 device, feature.flags, arg->data, in vfio_ioctl_device_feature()
1189 device, feature.flags, arg->data, in vfio_ioctl_device_feature()
1193 device, feature.flags, arg->data, in vfio_ioctl_device_feature()
1196 if (unlikely(!device->ops->device_feature)) in vfio_ioctl_device_feature()
1198 return device->ops->device_feature(device, feature.flags, in vfio_ioctl_device_feature()
1208 struct vfio_device *device = df->device; in vfio_device_fops_unl_ioctl() local
1219 ret = vfio_device_pm_runtime_get(device); in vfio_device_fops_unl_ioctl()
1238 ret = vfio_ioctl_device_feature(device, uptr); in vfio_device_fops_unl_ioctl()
1242 if (unlikely(!device->ops->ioctl)) in vfio_device_fops_unl_ioctl()
1245 ret = device->ops->ioctl(device, cmd, arg); in vfio_device_fops_unl_ioctl()
1249 vfio_device_pm_runtime_put(device); in vfio_device_fops_unl_ioctl()
1257 struct vfio_device *device = df->device; in vfio_device_fops_read() local
1263 if (unlikely(!device->ops->read)) in vfio_device_fops_read()
1266 return device->ops->read(device, buf, count, ppos); in vfio_device_fops_read()
1274 struct vfio_device *device = df->device; in vfio_device_fops_write() local
1280 if (unlikely(!device->ops->write)) in vfio_device_fops_write()
1283 return device->ops->write(device, buf, count, ppos); in vfio_device_fops_write()
1289 struct vfio_device *device = df->device; in vfio_device_fops_mmap() local
1295 if (unlikely(!device->ops->mmap)) in vfio_device_fops_mmap()
1298 return device->ops->mmap(device, vma); in vfio_device_fops_mmap()
1318 return df->device; in vfio_device_from_file()
1343 struct vfio_device *device; in vfio_file_enforced_coherent() local
1350 device = vfio_device_from_file(file); in vfio_file_enforced_coherent()
1351 if (device) in vfio_file_enforced_coherent()
1352 return device_iommu_capable(device->dev, in vfio_file_enforced_coherent()
1533 int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, in vfio_pin_pages() argument
1537 if (!pages || !npage || WARN_ON(!vfio_assert_device_open(device))) in vfio_pin_pages()
1539 if (!device->ops->dma_unmap) in vfio_pin_pages()
1541 if (vfio_device_has_container(device)) in vfio_pin_pages()
1542 return vfio_device_container_pin_pages(device, iova, in vfio_pin_pages()
1544 if (device->iommufd_access) { in vfio_pin_pages()
1556 device->iommufd_access, ALIGN_DOWN(iova, PAGE_SIZE), in vfio_pin_pages()
1574 void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage) in vfio_unpin_pages() argument
1576 if (WARN_ON(!vfio_assert_device_open(device))) in vfio_unpin_pages()
1578 if (WARN_ON(!device->ops->dma_unmap)) in vfio_unpin_pages()
1581 if (vfio_device_has_container(device)) { in vfio_unpin_pages()
1582 vfio_device_container_unpin_pages(device, iova, npage); in vfio_unpin_pages()
1585 if (device->iommufd_access) { in vfio_unpin_pages()
1588 iommufd_access_unpin_pages(device->iommufd_access, in vfio_unpin_pages()
1613 int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, void *data, in vfio_dma_rw() argument
1616 if (!data || len <= 0 || !vfio_assert_device_open(device)) in vfio_dma_rw()
1619 if (vfio_device_has_container(device)) in vfio_dma_rw()
1620 return vfio_device_container_dma_rw(device, iova, in vfio_dma_rw()
1623 if (device->iommufd_access) { in vfio_dma_rw()
1634 return iommufd_access_rw(device->iommufd_access, iova, data, in vfio_dma_rw()