xref: /openbmc/linux/drivers/gpu/drm/amd/amdkfd/kfd_svm.h (revision 71501859)
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /*
3  * Copyright 2020-2021 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #ifndef KFD_SVM_H_
26 #define KFD_SVM_H_
27 
28 #if IS_ENABLED(CONFIG_HSA_AMD_SVM)
29 
30 #include <linux/rwsem.h>
31 #include <linux/list.h>
32 #include <linux/mutex.h>
33 #include <linux/sched/mm.h>
34 #include <linux/hmm.h>
35 #include "amdgpu.h"
36 #include "kfd_priv.h"
37 
38 struct svm_range_bo {
39 	struct amdgpu_bo		*bo;
40 	struct kref			kref;
41 	struct list_head		range_list; /* all svm ranges shared this bo */
42 	spinlock_t			list_lock;
43 	struct amdgpu_amdkfd_fence	*eviction_fence;
44 	struct work_struct		eviction_work;
45 	struct svm_range_list		*svms;
46 	uint32_t			evicting;
47 };
48 
49 enum svm_work_list_ops {
50 	SVM_OP_NULL,
51 	SVM_OP_UNMAP_RANGE,
52 	SVM_OP_UPDATE_RANGE_NOTIFIER,
53 	SVM_OP_UPDATE_RANGE_NOTIFIER_AND_MAP,
54 	SVM_OP_ADD_RANGE,
55 	SVM_OP_ADD_RANGE_AND_MAP
56 };
57 
58 struct svm_work_list_item {
59 	enum svm_work_list_ops op;
60 	struct mm_struct *mm;
61 };
62 
63 /**
64  * struct svm_range - shared virtual memory range
65  *
66  * @svms:       list of svm ranges, structure defined in kfd_process
67  * @migrate_mutex: to serialize range migration, validation and mapping update
68  * @start:      range start address in pages
69  * @last:       range last address in pages
70  * @it_node:    node [start, last] stored in interval tree, start, last are page
71  *              aligned, page size is (last - start + 1)
72  * @list:       link list node, used to scan all ranges of svms
73  * @update_list:link list node used to add to update_list
74  * @remove_list:link list node used to add to remove list
75  * @insert_list:link list node used to add to insert list
76  * @mapping:    bo_va mapping structure to create and update GPU page table
77  * @npages:     number of pages
78  * @dma_addr:   dma mapping address on each GPU for system memory physical page
79  * @ttm_res:    vram ttm resource map
80  * @offset:     range start offset within mm_nodes
81  * @svm_bo:     struct to manage splited amdgpu_bo
82  * @svm_bo_list:link list node, to scan all ranges which share same svm_bo
83  * @lock:       protect prange start, last, child_list, svm_bo_list
84  * @saved_flags:save/restore current PF_MEMALLOC flags
85  * @flags:      flags defined as KFD_IOCTL_SVM_FLAG_*
86  * @perferred_loc: perferred location, 0 for CPU, or GPU id
87  * @perfetch_loc: last prefetch location, 0 for CPU, or GPU id
88  * @actual_loc: the actual location, 0 for CPU, or GPU id
89  * @granularity:migration granularity, log2 num pages
90  * @invalid:    not 0 means cpu page table is invalidated
91  * @validate_timestamp: system timestamp when range is validated
92  * @notifier:   register mmu interval notifier
93  * @work_item:  deferred work item information
94  * @deferred_list: list header used to add range to deferred list
95  * @child_list: list header for split ranges which are not added to svms yet
96  * @bitmap_access: index bitmap of GPUs which can access the range
97  * @bitmap_aip: index bitmap of GPUs which can access the range in place
98  *
99  * Data structure for virtual memory range shared by CPU and GPUs, it can be
100  * allocated from system memory ram or device vram, and migrate from ram to vram
101  * or from vram to ram.
102  */
103 struct svm_range {
104 	struct svm_range_list		*svms;
105 	struct mutex			migrate_mutex;
106 	unsigned long			start;
107 	unsigned long			last;
108 	struct interval_tree_node	it_node;
109 	struct list_head		list;
110 	struct list_head		update_list;
111 	struct list_head		remove_list;
112 	struct list_head		insert_list;
113 	struct amdgpu_bo_va_mapping	mapping;
114 	uint64_t			npages;
115 	dma_addr_t			*dma_addr[MAX_GPU_INSTANCE];
116 	struct ttm_resource		*ttm_res;
117 	uint64_t			offset;
118 	struct svm_range_bo		*svm_bo;
119 	struct list_head		svm_bo_list;
120 	struct mutex                    lock;
121 	unsigned int                    saved_flags;
122 	uint32_t			flags;
123 	uint32_t			preferred_loc;
124 	uint32_t			prefetch_loc;
125 	uint32_t			actual_loc;
126 	uint8_t				granularity;
127 	atomic_t			invalid;
128 	uint64_t			validate_timestamp;
129 	struct mmu_interval_notifier	notifier;
130 	struct svm_work_list_item	work_item;
131 	struct list_head		deferred_list;
132 	struct list_head		child_list;
133 	DECLARE_BITMAP(bitmap_access, MAX_GPU_INSTANCE);
134 	DECLARE_BITMAP(bitmap_aip, MAX_GPU_INSTANCE);
135 	bool				validated_once;
136 };
137 
138 static inline void svm_range_lock(struct svm_range *prange)
139 {
140 	mutex_lock(&prange->lock);
141 	prange->saved_flags = memalloc_noreclaim_save();
142 
143 }
144 static inline void svm_range_unlock(struct svm_range *prange)
145 {
146 	memalloc_noreclaim_restore(prange->saved_flags);
147 	mutex_unlock(&prange->lock);
148 }
149 
150 int svm_range_list_init(struct kfd_process *p);
151 void svm_range_list_fini(struct kfd_process *p);
152 int svm_ioctl(struct kfd_process *p, enum kfd_ioctl_svm_op op, uint64_t start,
153 	      uint64_t size, uint32_t nattrs,
154 	      struct kfd_ioctl_svm_attribute *attrs);
155 struct svm_range *svm_range_from_addr(struct svm_range_list *svms,
156 				      unsigned long addr,
157 				      struct svm_range **parent);
158 struct amdgpu_device *svm_range_get_adev_by_id(struct svm_range *prange,
159 					       uint32_t id);
160 int svm_range_vram_node_new(struct amdgpu_device *adev,
161 			    struct svm_range *prange, bool clear);
162 void svm_range_vram_node_free(struct svm_range *prange);
163 int svm_range_split_by_granularity(struct kfd_process *p, struct mm_struct *mm,
164 			       unsigned long addr, struct svm_range *parent,
165 			       struct svm_range *prange);
166 int svm_range_restore_pages(struct amdgpu_device *adev,
167 			    unsigned int pasid, uint64_t addr);
168 int svm_range_schedule_evict_svm_bo(struct amdgpu_amdkfd_fence *fence);
169 void svm_range_add_list_work(struct svm_range_list *svms,
170 			     struct svm_range *prange, struct mm_struct *mm,
171 			     enum svm_work_list_ops op);
172 void schedule_deferred_list_work(struct svm_range_list *svms);
173 void svm_range_dma_unmap(struct device *dev, dma_addr_t *dma_addr,
174 			 unsigned long offset, unsigned long npages);
175 void svm_range_free_dma_mappings(struct svm_range *prange);
176 void svm_range_prefault(struct svm_range *prange, struct mm_struct *mm);
177 
178 #else
179 
180 struct kfd_process;
181 
182 static inline int svm_range_list_init(struct kfd_process *p)
183 {
184 	return 0;
185 }
186 static inline void svm_range_list_fini(struct kfd_process *p)
187 {
188 	/* empty */
189 }
190 
191 static inline int svm_range_restore_pages(struct amdgpu_device *adev,
192 					  unsigned int pasid, uint64_t addr)
193 {
194 	return -EFAULT;
195 }
196 
197 static inline int svm_range_schedule_evict_svm_bo(
198 		struct amdgpu_amdkfd_fence *fence)
199 {
200 	WARN_ONCE(1, "SVM eviction fence triggered, but SVM is disabled");
201 	return -EINVAL;
202 }
203 
204 #endif /* IS_ENABLED(CONFIG_HSA_AMD_SVM) */
205 
206 #endif /* KFD_SVM_H_ */
207