1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2013 - Virtual Open Systems
4  * Author: Antonios Motakis <a.motakis@virtualopensystems.com>
5  */
6 
7 #ifndef VFIO_PLATFORM_PRIVATE_H
8 #define VFIO_PLATFORM_PRIVATE_H
9 
10 #include <linux/types.h>
11 #include <linux/interrupt.h>
12 #include <linux/vfio.h>
13 
14 #define VFIO_PLATFORM_OFFSET_SHIFT   40
15 #define VFIO_PLATFORM_OFFSET_MASK (((u64)(1) << VFIO_PLATFORM_OFFSET_SHIFT) - 1)
16 
17 #define VFIO_PLATFORM_OFFSET_TO_INDEX(off)	\
18 	(off >> VFIO_PLATFORM_OFFSET_SHIFT)
19 
20 #define VFIO_PLATFORM_INDEX_TO_OFFSET(index)	\
21 	((u64)(index) << VFIO_PLATFORM_OFFSET_SHIFT)
22 
23 struct vfio_platform_irq {
24 	u32			flags;
25 	u32			count;
26 	int			hwirq;
27 	char			*name;
28 	struct eventfd_ctx	*trigger;
29 	bool			masked;
30 	spinlock_t		lock;
31 	struct virqfd		*unmask;
32 	struct virqfd		*mask;
33 };
34 
35 struct vfio_platform_region {
36 	u64			addr;
37 	resource_size_t		size;
38 	u32			flags;
39 	u32			type;
40 #define VFIO_PLATFORM_REGION_TYPE_MMIO	1
41 #define VFIO_PLATFORM_REGION_TYPE_PIO	2
42 	void __iomem		*ioaddr;
43 };
44 
45 struct vfio_platform_device {
46 	struct vfio_device		vdev;
47 	struct vfio_platform_region	*regions;
48 	u32				num_regions;
49 	struct vfio_platform_irq	*irqs;
50 	u32				num_irqs;
51 	int				refcnt;
52 	struct mutex			igate;
53 	struct module			*parent_module;
54 	const char			*compat;
55 	const char			*acpihid;
56 	struct module			*reset_module;
57 	struct device			*device;
58 
59 	/*
60 	 * These fields should be filled by the bus specific binder
61 	 */
62 	void		*opaque;
63 	const char	*name;
64 	uint32_t	flags;
65 	/* callbacks to discover device resources */
66 	struct resource*
67 		(*get_resource)(struct vfio_platform_device *vdev, int i);
68 	int	(*get_irq)(struct vfio_platform_device *vdev, int i);
69 	int	(*of_reset)(struct vfio_platform_device *vdev);
70 
71 	bool				reset_required;
72 };
73 
74 typedef int (*vfio_platform_reset_fn_t)(struct vfio_platform_device *vdev);
75 
76 struct vfio_platform_reset_node {
77 	struct list_head link;
78 	char *compat;
79 	struct module *owner;
80 	vfio_platform_reset_fn_t of_reset;
81 };
82 
83 extern int vfio_platform_probe_common(struct vfio_platform_device *vdev,
84 				      struct device *dev);
85 void vfio_platform_remove_common(struct vfio_platform_device *vdev);
86 
87 extern int vfio_platform_irq_init(struct vfio_platform_device *vdev);
88 extern void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev);
89 
90 extern int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev,
91 					uint32_t flags, unsigned index,
92 					unsigned start, unsigned count,
93 					void *data);
94 
95 extern void __vfio_platform_register_reset(struct vfio_platform_reset_node *n);
96 extern void vfio_platform_unregister_reset(const char *compat,
97 					   vfio_platform_reset_fn_t fn);
98 #define vfio_platform_register_reset(__compat, __reset)		\
99 static struct vfio_platform_reset_node __reset ## _node = {	\
100 	.owner = THIS_MODULE,					\
101 	.compat = __compat,					\
102 	.of_reset = __reset,					\
103 };								\
104 __vfio_platform_register_reset(&__reset ## _node)
105 
106 #define module_vfio_reset_handler(compat, reset)		\
107 MODULE_ALIAS("vfio-reset:" compat);				\
108 static int __init reset ## _module_init(void)			\
109 {								\
110 	vfio_platform_register_reset(compat, reset);		\
111 	return 0;						\
112 };								\
113 static void __exit reset ## _module_exit(void)			\
114 {								\
115 	vfio_platform_unregister_reset(compat, reset);		\
116 };								\
117 module_init(reset ## _module_init);				\
118 module_exit(reset ## _module_exit)
119 
120 #endif /* VFIO_PLATFORM_PRIVATE_H */
121