Merge tag 'soc-ep93xx-dt-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-block.git] / include / linux / iommufd.h
CommitLineData
2ff4bed7
JG
1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (C) 2021 Intel Corporation
4 * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES
5 */
6#ifndef __LINUX_IOMMUFD_H
7#define __LINUX_IOMMUFD_H
8
2ff4bed7 9#include <linux/err.h>
1d4684fb
NC
10#include <linux/errno.h>
11#include <linux/types.h>
2ff4bed7 12
8d40205f 13struct device;
2ff4bed7 14struct file;
86b0a96c 15struct iommu_group;
3e6a7e3c
NC
16struct iommufd_access;
17struct iommufd_ctx;
18struct iommufd_device;
19struct page;
2ff4bed7 20
e8d57210
JG
21struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
22 struct device *dev, u32 *id);
23void iommufd_device_unbind(struct iommufd_device *idev);
24
25int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id);
fa1ffdb9 26int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id);
e8d57210
JG
27void iommufd_device_detach(struct iommufd_device *idev);
28
78d3df45
YL
29struct iommufd_ctx *iommufd_device_to_ictx(struct iommufd_device *idev);
30u32 iommufd_device_to_id(struct iommufd_device *idev);
31
8d40205f
JG
32struct iommufd_access_ops {
33 u8 needs_pin_pages : 1;
34 void (*unmap)(void *data, unsigned long iova, unsigned long length);
35};
36
f394576e
JG
37enum {
38 IOMMUFD_ACCESS_RW_READ = 0,
39 IOMMUFD_ACCESS_RW_WRITE = 1 << 0,
40 /* Set if the caller is in a kthread then rw will use kthread_use_mm() */
41 IOMMUFD_ACCESS_RW_KTHREAD = 1 << 1,
f4b20bb3
JG
42
43 /* Only for use by selftest */
44 __IOMMUFD_ACCESS_RW_SLOW_PATH = 1 << 2,
f394576e
JG
45};
46
8d40205f 47struct iommufd_access *
54b47585 48iommufd_access_create(struct iommufd_ctx *ictx,
632fda7f 49 const struct iommufd_access_ops *ops, void *data, u32 *id);
8d40205f 50void iommufd_access_destroy(struct iommufd_access *access);
54b47585 51int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id);
70c16123 52int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id);
e23a6217 53void iommufd_access_detach(struct iommufd_access *access);
8d40205f 54
2ff4bed7
JG
55void iommufd_ctx_get(struct iommufd_ctx *ictx);
56
57#if IS_ENABLED(CONFIG_IOMMUFD)
58struct iommufd_ctx *iommufd_ctx_from_file(struct file *file);
1c9dc074 59struct iommufd_ctx *iommufd_ctx_from_fd(int fd);
2ff4bed7 60void iommufd_ctx_put(struct iommufd_ctx *ictx);
86b0a96c 61bool iommufd_ctx_has_group(struct iommufd_ctx *ictx, struct iommu_group *group);
8d40205f
JG
62
63int iommufd_access_pin_pages(struct iommufd_access *access, unsigned long iova,
64 unsigned long length, struct page **out_pages,
65 unsigned int flags);
66void iommufd_access_unpin_pages(struct iommufd_access *access,
67 unsigned long iova, unsigned long length);
68int iommufd_access_rw(struct iommufd_access *access, unsigned long iova,
69 void *data, size_t len, unsigned int flags);
c9a397ce
JG
70int iommufd_vfio_compat_ioas_get_id(struct iommufd_ctx *ictx, u32 *out_ioas_id);
71int iommufd_vfio_compat_ioas_create(struct iommufd_ctx *ictx);
72int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx);
2ff4bed7
JG
73#else /* !CONFIG_IOMMUFD */
74static inline struct iommufd_ctx *iommufd_ctx_from_file(struct file *file)
75{
76 return ERR_PTR(-EOPNOTSUPP);
77}
78
79static inline void iommufd_ctx_put(struct iommufd_ctx *ictx)
80{
81}
8d40205f
JG
82
83static inline int iommufd_access_pin_pages(struct iommufd_access *access,
84 unsigned long iova,
85 unsigned long length,
86 struct page **out_pages,
87 unsigned int flags)
88{
89 return -EOPNOTSUPP;
90}
91
92static inline void iommufd_access_unpin_pages(struct iommufd_access *access,
93 unsigned long iova,
94 unsigned long length)
95{
96}
97
98static inline int iommufd_access_rw(struct iommufd_access *access, unsigned long iova,
99 void *data, size_t len, unsigned int flags)
100{
101 return -EOPNOTSUPP;
102}
d624d665 103
c9a397ce
JG
104static inline int iommufd_vfio_compat_ioas_create(struct iommufd_ctx *ictx)
105{
106 return -EOPNOTSUPP;
107}
108
109static inline int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx)
d624d665
JG
110{
111 return -EOPNOTSUPP;
112}
2ff4bed7
JG
113#endif /* CONFIG_IOMMUFD */
114#endif