1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
3 * Copyright 2020-2021 Advanced Micro Devices, Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
28 #if IS_ENABLED(CONFIG_HSA_AMD_SVM)
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>
38 #define SVM_RANGE_VRAM_DOMAIN (1UL << 0)
39 #define SVM_ADEV_PGMAP_OWNER(adev)\
40 ((adev)->hive ? (void *)(adev)->hive : (void *)(adev))
45 struct list_head range_list
; /* all svm ranges shared this bo */
47 struct amdgpu_amdkfd_fence
*eviction_fence
;
48 struct work_struct eviction_work
;
50 struct work_struct release_work
;
53 enum svm_work_list_ops
{
56 SVM_OP_UPDATE_RANGE_NOTIFIER
,
57 SVM_OP_UPDATE_RANGE_NOTIFIER_AND_MAP
,
59 SVM_OP_ADD_RANGE_AND_MAP
62 struct svm_work_list_item
{
63 enum svm_work_list_ops op
;
68 * struct svm_range - shared virtual memory range
70 * @svms: list of svm ranges, structure defined in kfd_process
71 * @migrate_mutex: to serialize range migration, validation and mapping update
72 * @start: range start address in pages
73 * @last: range last address in pages
74 * @it_node: node [start, last] stored in interval tree, start, last are page
75 * aligned, page size is (last - start + 1)
76 * @list: link list node, used to scan all ranges of svms
77 * @update_list:link list node used to add to update_list
78 * @mapping: bo_va mapping structure to create and update GPU page table
79 * @npages: number of pages
80 * @dma_addr: dma mapping address on each GPU for system memory physical page
81 * @ttm_res: vram ttm resource map
82 * @offset: range start offset within mm_nodes
83 * @svm_bo: struct to manage splited amdgpu_bo
84 * @svm_bo_list:link list node, to scan all ranges which share same svm_bo
85 * @lock: protect prange start, last, child_list, svm_bo_list
86 * @saved_flags:save/restore current PF_MEMALLOC flags
87 * @flags: flags defined as KFD_IOCTL_SVM_FLAG_*
88 * @perferred_loc: perferred location, 0 for CPU, or GPU id
89 * @perfetch_loc: last prefetch location, 0 for CPU, or GPU id
90 * @actual_loc: the actual location, 0 for CPU, or GPU id
91 * @granularity:migration granularity, log2 num pages
92 * @invalid: not 0 means cpu page table is invalidated
93 * @validate_timestamp: system timestamp when range is validated
94 * @notifier: register mmu interval notifier
95 * @work_item: deferred work item information
96 * @deferred_list: list header used to add range to deferred list
97 * @child_list: list header for split ranges which are not added to svms yet
98 * @bitmap_access: index bitmap of GPUs which can access the range
99 * @bitmap_aip: index bitmap of GPUs which can access the range in place
101 * Data structure for virtual memory range shared by CPU and GPUs, it can be
102 * allocated from system memory ram or device vram, and migrate from ram to vram
103 * or from vram to ram.
106 struct svm_range_list
*svms
;
107 struct mutex migrate_mutex
;
110 struct interval_tree_node it_node
;
111 struct list_head list
;
112 struct list_head update_list
;
114 dma_addr_t
*dma_addr
[MAX_GPU_INSTANCE
];
115 struct ttm_resource
*ttm_res
;
117 struct svm_range_bo
*svm_bo
;
118 struct list_head svm_bo_list
;
120 unsigned int saved_flags
;
122 uint32_t preferred_loc
;
123 uint32_t prefetch_loc
;
127 ktime_t validate_timestamp
;
128 struct mmu_interval_notifier notifier
;
129 struct svm_work_list_item work_item
;
130 struct list_head deferred_list
;
131 struct list_head child_list
;
132 DECLARE_BITMAP(bitmap_access
, MAX_GPU_INSTANCE
);
133 DECLARE_BITMAP(bitmap_aip
, MAX_GPU_INSTANCE
);
138 static inline void svm_range_lock(struct svm_range
*prange
)
140 mutex_lock(&prange
->lock
);
141 prange
->saved_flags
= memalloc_noreclaim_save();
144 static inline void svm_range_unlock(struct svm_range
*prange
)
146 memalloc_noreclaim_restore(prange
->saved_flags
);
147 mutex_unlock(&prange
->lock
);
150 static inline struct svm_range_bo
*svm_range_bo_ref(struct svm_range_bo
*svm_bo
)
153 kref_get(&svm_bo
->kref
);
158 int svm_range_list_init(struct kfd_process
*p
);
159 void svm_range_list_fini(struct kfd_process
*p
);
160 int svm_ioctl(struct kfd_process
*p
, enum kfd_ioctl_svm_op op
, uint64_t start
,
161 uint64_t size
, uint32_t nattrs
,
162 struct kfd_ioctl_svm_attribute
*attrs
);
163 struct svm_range
*svm_range_from_addr(struct svm_range_list
*svms
,
165 struct svm_range
**parent
);
166 struct amdgpu_device
*svm_range_get_adev_by_id(struct svm_range
*prange
,
168 int svm_range_vram_node_new(struct amdgpu_device
*adev
,
169 struct svm_range
*prange
, bool clear
);
170 void svm_range_vram_node_free(struct svm_range
*prange
);
171 int svm_range_split_by_granularity(struct kfd_process
*p
, struct mm_struct
*mm
,
172 unsigned long addr
, struct svm_range
*parent
,
173 struct svm_range
*prange
);
174 int svm_range_restore_pages(struct amdgpu_device
*adev
,
175 unsigned int pasid
, uint64_t addr
, bool write_fault
);
176 int svm_range_schedule_evict_svm_bo(struct amdgpu_amdkfd_fence
*fence
);
177 void svm_range_add_list_work(struct svm_range_list
*svms
,
178 struct svm_range
*prange
, struct mm_struct
*mm
,
179 enum svm_work_list_ops op
);
180 void schedule_deferred_list_work(struct svm_range_list
*svms
);
181 void svm_range_dma_unmap(struct device
*dev
, dma_addr_t
*dma_addr
,
182 unsigned long offset
, unsigned long npages
);
183 void svm_range_free_dma_mappings(struct svm_range
*prange
);
184 void svm_range_prefault(struct svm_range
*prange
, struct mm_struct
*mm
,
186 int svm_range_get_info(struct kfd_process
*p
, uint32_t *num_svm_ranges
,
187 uint64_t *svm_priv_data_size
);
188 int kfd_criu_checkpoint_svm(struct kfd_process
*p
,
189 uint8_t __user
*user_priv_data
,
190 uint64_t *priv_offset
);
191 int kfd_criu_restore_svm(struct kfd_process
*p
,
192 uint8_t __user
*user_priv_ptr
,
193 uint64_t *priv_data_offset
,
194 uint64_t max_priv_data_size
);
195 int kfd_criu_resume_svm(struct kfd_process
*p
);
196 struct kfd_process_device
*
197 svm_range_get_pdd_by_adev(struct svm_range
*prange
, struct amdgpu_device
*adev
);
198 void svm_range_list_lock_and_flush_work(struct svm_range_list
*svms
, struct mm_struct
*mm
);
200 /* SVM API and HMM page migration work together, device memory type
201 * is initialized to not 0 when page migration register device memory.
203 #define KFD_IS_SVM_API_SUPPORTED(dev) ((dev)->pgmap.type != 0)
205 void svm_range_bo_unref_async(struct svm_range_bo
*svm_bo
);
207 void svm_range_set_max_pages(struct amdgpu_device
*adev
);
213 static inline int svm_range_list_init(struct kfd_process
*p
)
217 static inline void svm_range_list_fini(struct kfd_process
*p
)
222 static inline int svm_range_restore_pages(struct amdgpu_device
*adev
,
223 unsigned int pasid
, uint64_t addr
,
229 static inline int svm_range_schedule_evict_svm_bo(
230 struct amdgpu_amdkfd_fence
*fence
)
232 WARN_ONCE(1, "SVM eviction fence triggered, but SVM is disabled");
236 static inline int svm_range_get_info(struct kfd_process
*p
,
237 uint32_t *num_svm_ranges
,
238 uint64_t *svm_priv_data_size
)
241 *svm_priv_data_size
= 0;
245 static inline int kfd_criu_checkpoint_svm(struct kfd_process
*p
,
246 uint8_t __user
*user_priv_data
,
247 uint64_t *priv_offset
)
252 static inline int kfd_criu_restore_svm(struct kfd_process
*p
,
253 uint8_t __user
*user_priv_ptr
,
254 uint64_t *priv_data_offset
,
255 uint64_t max_priv_data_size
)
260 static inline int kfd_criu_resume_svm(struct kfd_process
*p
)
265 #define KFD_IS_SVM_API_SUPPORTED(dev) false
267 #endif /* IS_ENABLED(CONFIG_HSA_AMD_SVM) */
269 #endif /* KFD_SVM_H_ */