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 #include <linux/rwsem.h>
29 #include <linux/list.h>
30 #include <linux/mutex.h>
31 #include <linux/sched/mm.h>
32 #include <linux/hmm.h>
39 struct list_head range_list; /* all svm ranges shared this bo */
41 struct amdgpu_amdkfd_fence *eviction_fence;
42 struct work_struct eviction_work;
43 struct svm_range_list *svms;
47 enum svm_work_list_ops {
50 SVM_OP_UPDATE_RANGE_NOTIFIER,
51 SVM_OP_UPDATE_RANGE_NOTIFIER_AND_MAP,
53 SVM_OP_ADD_RANGE_AND_MAP
56 struct svm_work_list_item {
57 enum svm_work_list_ops op;
62 * struct svm_range - shared virtual memory range
64 * @svms: list of svm ranges, structure defined in kfd_process
65 * @migrate_mutex: to serialize range migration, validation and mapping update
66 * @start: range start address in pages
67 * @last: range last address in pages
68 * @it_node: node [start, last] stored in interval tree, start, last are page
69 * aligned, page size is (last - start + 1)
70 * @list: link list node, used to scan all ranges of svms
71 * @update_list:link list node used to add to update_list
72 * @remove_list:link list node used to add to remove list
73 * @insert_list:link list node used to add to insert list
74 * @mapping: bo_va mapping structure to create and update GPU page table
75 * @npages: number of pages
76 * @dma_addr: dma mapping address on each GPU for system memory physical page
77 * @ttm_res: vram ttm resource map
78 * @offset: range start offset within mm_nodes
79 * @svm_bo: struct to manage splited amdgpu_bo
80 * @svm_bo_list:link list node, to scan all ranges which share same svm_bo
81 * @lock: protect prange start, last, child_list, svm_bo_list
82 * @saved_flags:save/restore current PF_MEMALLOC flags
83 * @flags: flags defined as KFD_IOCTL_SVM_FLAG_*
84 * @perferred_loc: perferred location, 0 for CPU, or GPU id
85 * @perfetch_loc: last prefetch location, 0 for CPU, or GPU id
86 * @actual_loc: the actual location, 0 for CPU, or GPU id
87 * @granularity:migration granularity, log2 num pages
88 * @invalid: not 0 means cpu page table is invalidated
89 * @validate_timestamp: system timestamp when range is validated
90 * @notifier: register mmu interval notifier
91 * @work_item: deferred work item information
92 * @deferred_list: list header used to add range to deferred list
93 * @child_list: list header for split ranges which are not added to svms yet
94 * @bitmap_access: index bitmap of GPUs which can access the range
95 * @bitmap_aip: index bitmap of GPUs which can access the range in place
97 * Data structure for virtual memory range shared by CPU and GPUs, it can be
98 * allocated from system memory ram or device vram, and migrate from ram to vram
99 * or from vram to ram.
102 struct svm_range_list *svms;
103 struct mutex migrate_mutex;
106 struct interval_tree_node it_node;
107 struct list_head list;
108 struct list_head update_list;
109 struct list_head remove_list;
110 struct list_head insert_list;
111 struct amdgpu_bo_va_mapping mapping;
113 dma_addr_t *dma_addr[MAX_GPU_INSTANCE];
114 struct ttm_resource *ttm_res;
116 struct svm_range_bo *svm_bo;
117 struct list_head svm_bo_list;
119 unsigned int saved_flags;
121 uint32_t preferred_loc;
122 uint32_t prefetch_loc;
126 uint64_t validate_timestamp;
127 struct mmu_interval_notifier notifier;
128 struct svm_work_list_item work_item;
129 struct list_head deferred_list;
130 struct list_head child_list;
131 DECLARE_BITMAP(bitmap_access, MAX_GPU_INSTANCE);
132 DECLARE_BITMAP(bitmap_aip, MAX_GPU_INSTANCE);
136 static inline void svm_range_lock(struct svm_range *prange)
138 mutex_lock(&prange->lock);
139 prange->saved_flags = memalloc_noreclaim_save();
142 static inline void svm_range_unlock(struct svm_range *prange)
144 memalloc_noreclaim_restore(prange->saved_flags);
145 mutex_unlock(&prange->lock);
148 int svm_range_list_init(struct kfd_process *p);
149 void svm_range_list_fini(struct kfd_process *p);
150 int svm_ioctl(struct kfd_process *p, enum kfd_ioctl_svm_op op, uint64_t start,
151 uint64_t size, uint32_t nattrs,
152 struct kfd_ioctl_svm_attribute *attrs);
153 struct svm_range *svm_range_from_addr(struct svm_range_list *svms,
155 struct svm_range **parent);
156 struct amdgpu_device *svm_range_get_adev_by_id(struct svm_range *prange,
158 int svm_range_vram_node_new(struct amdgpu_device *adev,
159 struct svm_range *prange, bool clear);
160 void svm_range_vram_node_free(struct svm_range *prange);
161 int svm_range_split_by_granularity(struct kfd_process *p, struct mm_struct *mm,
162 unsigned long addr, struct svm_range *parent,
163 struct svm_range *prange);
164 int svm_range_restore_pages(struct amdgpu_device *adev,
165 unsigned int pasid, uint64_t addr);
166 int svm_range_schedule_evict_svm_bo(struct amdgpu_amdkfd_fence *fence);
167 void svm_range_add_list_work(struct svm_range_list *svms,
168 struct svm_range *prange, struct mm_struct *mm,
169 enum svm_work_list_ops op);
170 void schedule_deferred_list_work(struct svm_range_list *svms);
171 void svm_range_dma_unmap(struct device *dev, dma_addr_t *dma_addr,
172 unsigned long offset, unsigned long npages);
173 void svm_range_free_dma_mappings(struct svm_range *prange);
174 void svm_range_prefault(struct svm_range *prange, struct mm_struct *mm);
176 #endif /* KFD_SVM_H_ */