Loading...
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#define SVM_RANGE_VRAM_DOMAIN (1UL << 0)
39#define SVM_ADEV_PGMAP_OWNER(adev)\
40 ((adev)->hive ? (void *)(adev)->hive : (void *)(adev))
41
42struct svm_range_bo {
43 struct amdgpu_bo *bo;
44 struct kref kref;
45 struct list_head range_list; /* all svm ranges shared this bo */
46 spinlock_t list_lock;
47 struct amdgpu_amdkfd_fence *eviction_fence;
48 struct work_struct eviction_work;
49 uint32_t evicting;
50 struct work_struct release_work;
51 struct kfd_node *node;
52};
53
54enum svm_work_list_ops {
55 SVM_OP_NULL,
56 SVM_OP_UNMAP_RANGE,
57 SVM_OP_UPDATE_RANGE_NOTIFIER,
58 SVM_OP_UPDATE_RANGE_NOTIFIER_AND_MAP,
59 SVM_OP_ADD_RANGE,
60 SVM_OP_ADD_RANGE_AND_MAP
61};
62
63struct svm_work_list_item {
64 enum svm_work_list_ops op;
65 struct mm_struct *mm;
66};
67
68/**
69 * struct svm_range - shared virtual memory range
70 *
71 * @svms: list of svm ranges, structure defined in kfd_process
72 * @migrate_mutex: to serialize range migration, validation and mapping update
73 * @start: range start address in pages
74 * @last: range last address in pages
75 * @it_node: node [start, last] stored in interval tree, start, last are page
76 * aligned, page size is (last - start + 1)
77 * @list: link list node, used to scan all ranges of svms
78 * @update_list:link list node used to add to update_list
79 * @mapping: bo_va mapping structure to create and update GPU page table
80 * @npages: number of pages
81 * @vram_pages: vram pages number in this svm_range
82 * @dma_addr: dma mapping address on each GPU for system memory physical page
83 * @ttm_res: vram ttm resource map
84 * @offset: range start offset within mm_nodes
85 * @svm_bo: struct to manage splited amdgpu_bo
86 * @svm_bo_list:link list node, to scan all ranges which share same svm_bo
87 * @lock: protect prange start, last, child_list, svm_bo_list
88 * @saved_flags:save/restore current PF_MEMALLOC flags
89 * @flags: flags defined as KFD_IOCTL_SVM_FLAG_*
90 * @perferred_loc: perferred location, 0 for CPU, or GPU id
91 * @perfetch_loc: last prefetch location, 0 for CPU, or GPU id
92 * @actual_loc: this svm_range location. 0: all pages are from sys ram;
93 * GPU id: this svm_range may include vram pages from GPU with
94 * id actual_loc.
95 * @granularity:migration granularity, log2 num pages
96 * @invalid: not 0 means cpu page table is invalidated
97 * @validate_timestamp: system timestamp when range is validated
98 * @notifier: register mmu interval notifier
99 * @work_item: deferred work item information
100 * @deferred_list: list header used to add range to deferred list
101 * @child_list: list header for split ranges which are not added to svms yet
102 * @bitmap_access: index bitmap of GPUs which can access the range
103 * @bitmap_aip: index bitmap of GPUs which can access the range in place
104 *
105 * Data structure for virtual memory range shared by CPU and GPUs, it can be
106 * allocated from system memory ram or device vram, and migrate from ram to vram
107 * or from vram to ram.
108 */
109struct svm_range {
110 struct svm_range_list *svms;
111 struct mutex migrate_mutex;
112 unsigned long start;
113 unsigned long last;
114 struct interval_tree_node it_node;
115 struct list_head list;
116 struct list_head update_list;
117 uint64_t npages;
118 uint64_t vram_pages;
119 dma_addr_t *dma_addr[MAX_GPU_INSTANCE];
120 struct ttm_resource *ttm_res;
121 uint64_t offset;
122 struct svm_range_bo *svm_bo;
123 struct list_head svm_bo_list;
124 struct mutex lock;
125 unsigned int saved_flags;
126 uint32_t flags;
127 uint32_t preferred_loc;
128 uint32_t prefetch_loc;
129 uint32_t actual_loc;
130 uint8_t granularity;
131 atomic_t invalid;
132 ktime_t validate_timestamp;
133 struct mmu_interval_notifier notifier;
134 struct svm_work_list_item work_item;
135 struct list_head deferred_list;
136 struct list_head child_list;
137 DECLARE_BITMAP(bitmap_access, MAX_GPU_INSTANCE);
138 DECLARE_BITMAP(bitmap_aip, MAX_GPU_INSTANCE);
139 bool mapped_to_gpu;
140};
141
142static inline void svm_range_lock(struct svm_range *prange)
143{
144 mutex_lock(&prange->lock);
145 prange->saved_flags = memalloc_noreclaim_save();
146
147}
148static inline void svm_range_unlock(struct svm_range *prange)
149{
150 memalloc_noreclaim_restore(prange->saved_flags);
151 mutex_unlock(&prange->lock);
152}
153
154static inline struct svm_range_bo *svm_range_bo_ref(struct svm_range_bo *svm_bo)
155{
156 if (svm_bo)
157 kref_get(&svm_bo->kref);
158
159 return svm_bo;
160}
161
162int svm_range_list_init(struct kfd_process *p);
163void svm_range_list_fini(struct kfd_process *p);
164int svm_ioctl(struct kfd_process *p, enum kfd_ioctl_svm_op op, uint64_t start,
165 uint64_t size, uint32_t nattrs,
166 struct kfd_ioctl_svm_attribute *attrs);
167struct svm_range *svm_range_from_addr(struct svm_range_list *svms,
168 unsigned long addr,
169 struct svm_range **parent);
170struct kfd_node *svm_range_get_node_by_id(struct svm_range *prange,
171 uint32_t gpu_id);
172int svm_range_vram_node_new(struct kfd_node *node, struct svm_range *prange,
173 bool clear);
174void svm_range_vram_node_free(struct svm_range *prange);
175int svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
176 uint32_t vmid, uint32_t node_id, uint64_t addr,
177 bool write_fault);
178int svm_range_schedule_evict_svm_bo(struct amdgpu_amdkfd_fence *fence);
179void svm_range_add_list_work(struct svm_range_list *svms,
180 struct svm_range *prange, struct mm_struct *mm,
181 enum svm_work_list_ops op);
182void schedule_deferred_list_work(struct svm_range_list *svms);
183void svm_range_dma_unmap_dev(struct device *dev, dma_addr_t *dma_addr,
184 unsigned long offset, unsigned long npages);
185void svm_range_dma_unmap(struct svm_range *prange);
186int svm_range_get_info(struct kfd_process *p, uint32_t *num_svm_ranges,
187 uint64_t *svm_priv_data_size);
188int kfd_criu_checkpoint_svm(struct kfd_process *p,
189 uint8_t __user *user_priv_data,
190 uint64_t *priv_offset);
191int 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);
195int kfd_criu_resume_svm(struct kfd_process *p);
196struct kfd_process_device *
197svm_range_get_pdd_by_node(struct svm_range *prange, struct kfd_node *node);
198void svm_range_list_lock_and_flush_work(struct svm_range_list *svms, struct mm_struct *mm);
199
200/* SVM API and HMM page migration work together, device memory type
201 * is initialized to not 0 when page migration register device memory.
202 */
203#define KFD_IS_SVM_API_SUPPORTED(adev) ((adev)->kfd.pgmap.type != 0 ||\
204 (adev)->gmc.is_app_apu)
205
206void svm_range_bo_unref_async(struct svm_range_bo *svm_bo);
207
208void svm_range_set_max_pages(struct amdgpu_device *adev);
209int svm_range_switch_xnack_reserve_mem(struct kfd_process *p, bool xnack_enabled);
210
211#else
212
213struct kfd_process;
214
215static inline int svm_range_list_init(struct kfd_process *p)
216{
217 return 0;
218}
219static inline void svm_range_list_fini(struct kfd_process *p)
220{
221 /* empty */
222}
223
224static inline int svm_range_restore_pages(struct amdgpu_device *adev,
225 unsigned int pasid,
226 uint32_t client_id, uint32_t node_id,
227 uint64_t addr, bool write_fault)
228{
229 return -EFAULT;
230}
231
232static inline int svm_range_schedule_evict_svm_bo(
233 struct amdgpu_amdkfd_fence *fence)
234{
235 WARN_ONCE(1, "SVM eviction fence triggered, but SVM is disabled");
236 return -EINVAL;
237}
238
239static inline int svm_range_get_info(struct kfd_process *p,
240 uint32_t *num_svm_ranges,
241 uint64_t *svm_priv_data_size)
242{
243 *num_svm_ranges = 0;
244 *svm_priv_data_size = 0;
245 return 0;
246}
247
248static inline int kfd_criu_checkpoint_svm(struct kfd_process *p,
249 uint8_t __user *user_priv_data,
250 uint64_t *priv_offset)
251{
252 return 0;
253}
254
255static inline int kfd_criu_restore_svm(struct kfd_process *p,
256 uint8_t __user *user_priv_ptr,
257 uint64_t *priv_data_offset,
258 uint64_t max_priv_data_size)
259{
260 return -EINVAL;
261}
262
263static inline int kfd_criu_resume_svm(struct kfd_process *p)
264{
265 return 0;
266}
267
268static inline void svm_range_set_max_pages(struct amdgpu_device *adev)
269{
270}
271
272#define KFD_IS_SVM_API_SUPPORTED(dev) false
273
274#endif /* IS_ENABLED(CONFIG_HSA_AMD_SVM) */
275
276#endif /* KFD_SVM_H_ */
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#define SVM_RANGE_VRAM_DOMAIN (1UL << 0)
39#define SVM_ADEV_PGMAP_OWNER(adev)\
40 ((adev)->hive ? (void *)(adev)->hive : (void *)(adev))
41
42struct svm_range_bo {
43 struct amdgpu_bo *bo;
44 struct kref kref;
45 struct list_head range_list; /* all svm ranges shared this bo */
46 spinlock_t list_lock;
47 struct amdgpu_amdkfd_fence *eviction_fence;
48 struct work_struct eviction_work;
49 uint32_t evicting;
50 struct work_struct release_work;
51};
52
53enum svm_work_list_ops {
54 SVM_OP_NULL,
55 SVM_OP_UNMAP_RANGE,
56 SVM_OP_UPDATE_RANGE_NOTIFIER,
57 SVM_OP_UPDATE_RANGE_NOTIFIER_AND_MAP,
58 SVM_OP_ADD_RANGE,
59 SVM_OP_ADD_RANGE_AND_MAP
60};
61
62struct svm_work_list_item {
63 enum svm_work_list_ops op;
64 struct mm_struct *mm;
65};
66
67/**
68 * struct svm_range - shared virtual memory range
69 *
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
100 *
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.
104 */
105struct svm_range {
106 struct svm_range_list *svms;
107 struct mutex migrate_mutex;
108 unsigned long start;
109 unsigned long last;
110 struct interval_tree_node it_node;
111 struct list_head list;
112 struct list_head update_list;
113 uint64_t npages;
114 dma_addr_t *dma_addr[MAX_GPU_INSTANCE];
115 struct ttm_resource *ttm_res;
116 uint64_t offset;
117 struct svm_range_bo *svm_bo;
118 struct list_head svm_bo_list;
119 struct mutex lock;
120 unsigned int saved_flags;
121 uint32_t flags;
122 uint32_t preferred_loc;
123 uint32_t prefetch_loc;
124 uint32_t actual_loc;
125 uint8_t granularity;
126 atomic_t invalid;
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);
134 bool validated_once;
135 bool mapped_to_gpu;
136};
137
138static inline void svm_range_lock(struct svm_range *prange)
139{
140 mutex_lock(&prange->lock);
141 prange->saved_flags = memalloc_noreclaim_save();
142
143}
144static inline void svm_range_unlock(struct svm_range *prange)
145{
146 memalloc_noreclaim_restore(prange->saved_flags);
147 mutex_unlock(&prange->lock);
148}
149
150static inline struct svm_range_bo *svm_range_bo_ref(struct svm_range_bo *svm_bo)
151{
152 if (svm_bo)
153 kref_get(&svm_bo->kref);
154
155 return svm_bo;
156}
157
158int svm_range_list_init(struct kfd_process *p);
159void svm_range_list_fini(struct kfd_process *p);
160int 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);
163struct svm_range *svm_range_from_addr(struct svm_range_list *svms,
164 unsigned long addr,
165 struct svm_range **parent);
166struct amdgpu_device *svm_range_get_adev_by_id(struct svm_range *prange,
167 uint32_t id);
168int svm_range_vram_node_new(struct amdgpu_device *adev,
169 struct svm_range *prange, bool clear);
170void svm_range_vram_node_free(struct svm_range *prange);
171int 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);
174int svm_range_restore_pages(struct amdgpu_device *adev,
175 unsigned int pasid, uint64_t addr, bool write_fault);
176int svm_range_schedule_evict_svm_bo(struct amdgpu_amdkfd_fence *fence);
177void 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);
180void schedule_deferred_list_work(struct svm_range_list *svms);
181void svm_range_dma_unmap(struct device *dev, dma_addr_t *dma_addr,
182 unsigned long offset, unsigned long npages);
183void svm_range_free_dma_mappings(struct svm_range *prange);
184int svm_range_get_info(struct kfd_process *p, uint32_t *num_svm_ranges,
185 uint64_t *svm_priv_data_size);
186int kfd_criu_checkpoint_svm(struct kfd_process *p,
187 uint8_t __user *user_priv_data,
188 uint64_t *priv_offset);
189int kfd_criu_restore_svm(struct kfd_process *p,
190 uint8_t __user *user_priv_ptr,
191 uint64_t *priv_data_offset,
192 uint64_t max_priv_data_size);
193int kfd_criu_resume_svm(struct kfd_process *p);
194struct kfd_process_device *
195svm_range_get_pdd_by_adev(struct svm_range *prange, struct amdgpu_device *adev);
196void svm_range_list_lock_and_flush_work(struct svm_range_list *svms, struct mm_struct *mm);
197
198/* SVM API and HMM page migration work together, device memory type
199 * is initialized to not 0 when page migration register device memory.
200 */
201#define KFD_IS_SVM_API_SUPPORTED(dev) ((dev)->pgmap.type != 0)
202
203void svm_range_bo_unref_async(struct svm_range_bo *svm_bo);
204
205void svm_range_set_max_pages(struct amdgpu_device *adev);
206int svm_range_switch_xnack_reserve_mem(struct kfd_process *p, bool xnack_enabled);
207
208#else
209
210struct kfd_process;
211
212static inline int svm_range_list_init(struct kfd_process *p)
213{
214 return 0;
215}
216static inline void svm_range_list_fini(struct kfd_process *p)
217{
218 /* empty */
219}
220
221static inline int svm_range_restore_pages(struct amdgpu_device *adev,
222 unsigned int pasid, uint64_t addr,
223 bool write_fault)
224{
225 return -EFAULT;
226}
227
228static inline int svm_range_schedule_evict_svm_bo(
229 struct amdgpu_amdkfd_fence *fence)
230{
231 WARN_ONCE(1, "SVM eviction fence triggered, but SVM is disabled");
232 return -EINVAL;
233}
234
235static inline int svm_range_get_info(struct kfd_process *p,
236 uint32_t *num_svm_ranges,
237 uint64_t *svm_priv_data_size)
238{
239 *num_svm_ranges = 0;
240 *svm_priv_data_size = 0;
241 return 0;
242}
243
244static inline int kfd_criu_checkpoint_svm(struct kfd_process *p,
245 uint8_t __user *user_priv_data,
246 uint64_t *priv_offset)
247{
248 return 0;
249}
250
251static inline int kfd_criu_restore_svm(struct kfd_process *p,
252 uint8_t __user *user_priv_ptr,
253 uint64_t *priv_data_offset,
254 uint64_t max_priv_data_size)
255{
256 return -EINVAL;
257}
258
259static inline int kfd_criu_resume_svm(struct kfd_process *p)
260{
261 return 0;
262}
263
264#define KFD_IS_SVM_API_SUPPORTED(dev) false
265
266#endif /* IS_ENABLED(CONFIG_HSA_AMD_SVM) */
267
268#endif /* KFD_SVM_H_ */