• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef __ARM64_KVM_NVHE_IOMMU_H__
3 #define __ARM64_KVM_NVHE_IOMMU_H__
4 
5 #include <linux/types.h>
6 #include <asm/kvm_host.h>
7 
8 #include <nvhe/mem_protect.h>
9 
10 struct pkvm_iommu;
11 
12 struct pkvm_iommu_ops {
13 	/*
14 	 * Global driver initialization called before devices are registered.
15 	 * Driver-specific arguments are passed in a buffer shared by the host.
16 	 * The buffer memory has been pinned in EL2 but host retains R/W access.
17 	 * Extra care must be taken when reading from it to avoid TOCTOU bugs.
18 	 * If the driver maintains its own page tables, it is expected to
19 	 * initialize them to all memory owned by the host.
20 	 * Driver initialization lock held during callback.
21 	 */
22 	int (*init)(void *data, size_t size);
23 
24 	/*
25 	 * Driver-specific validation of a device that is being registered.
26 	 * All fields of the device struct have been populated.
27 	 * Called with the host lock held.
28 	 */
29 	int (*validate)(struct pkvm_iommu *dev);
30 
31 	/*
32 	 * Validation of a new child device that is being register by
33 	 * the parent device the child selected. Called with the host lock held.
34 	 */
35 	int (*validate_child)(struct pkvm_iommu *dev, struct pkvm_iommu *child);
36 
37 	/*
38 	 * Callback to apply a host stage-2 mapping change at driver level.
39 	 * Called before 'host_stage2_idmap_apply' with host lock held.
40 	 */
41 	void (*host_stage2_idmap_prepare)(phys_addr_t start, phys_addr_t end,
42 					  enum kvm_pgtable_prot prot);
43 
44 	/*
45 	 * Callback to apply a host stage-2 mapping change at device level.
46 	 * Called after 'host_stage2_idmap_prepare' with host lock held.
47 	 */
48 	void (*host_stage2_idmap_apply)(struct pkvm_iommu *dev,
49 					phys_addr_t start, phys_addr_t end);
50 
51 	/*
52 	 * Callback to finish a host stage-2 mapping change at device level.
53 	 * Called after 'host_stage2_idmap_apply' with host lock held.
54 	 */
55 	void (*host_stage2_idmap_complete)(struct pkvm_iommu *dev);
56 
57 	/* Power management callbacks. Called with host lock held. */
58 	int (*suspend)(struct pkvm_iommu *dev);
59 	int (*resume)(struct pkvm_iommu *dev);
60 
61 	/*
62 	 * Host data abort handler callback. Called with host lock held.
63 	 * Returns true if the data abort has been handled.
64 	 */
65 	bool (*host_dabt_handler)(struct pkvm_iommu *dev,
66 				  struct kvm_cpu_context *host_ctxt,
67 				  u32 esr, size_t off);
68 
69 	/* Amount of memory allocated per-device for use by the driver. */
70 	size_t data_size;
71 };
72 
73 struct pkvm_iommu {
74 	struct pkvm_iommu *parent;
75 	struct list_head list;
76 	struct list_head siblings;
77 	struct list_head children;
78 	unsigned long id;
79 	const struct pkvm_iommu_ops *ops;
80 	phys_addr_t pa;
81 	void *va;
82 	size_t size;
83 	bool powered;
84 	char data[];
85 };
86 
87 int __pkvm_iommu_driver_init(enum pkvm_iommu_driver_id id, void *data, size_t size);
88 int __pkvm_iommu_register(unsigned long dev_id,
89 			  enum pkvm_iommu_driver_id drv_id,
90 			  phys_addr_t dev_pa, size_t dev_size,
91 			  unsigned long parent_id,
92 			  void *kern_mem_va, size_t mem_size);
93 int __pkvm_iommu_pm_notify(unsigned long dev_id,
94 			   enum pkvm_iommu_pm_event event);
95 int __pkvm_iommu_finalize(void);
96 int pkvm_iommu_host_stage2_adjust_range(phys_addr_t addr, phys_addr_t *start,
97 					phys_addr_t *end);
98 bool pkvm_iommu_host_dabt_handler(struct kvm_cpu_context *host_ctxt, u32 esr,
99 				  phys_addr_t fault_pa);
100 void pkvm_iommu_host_stage2_idmap(phys_addr_t start, phys_addr_t end,
101 				  enum kvm_pgtable_prot prot);
102 
103 extern const struct pkvm_iommu_ops pkvm_s2mpu_ops;
104 extern const struct pkvm_iommu_ops pkvm_sysmmu_sync_ops;
105 
106 #endif	/* __ARM64_KVM_NVHE_IOMMU_H__ */
107