Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Apr 14-17, 2025
Register
Loading...
Note: File does not exist in v5.9.
  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	struct svm_range_list		*svms;
 50	uint32_t			evicting;
 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 * @remove_list:link list node used to add to remove list
 79 * @insert_list:link list node used to add to insert list
 80 * @mapping:    bo_va mapping structure to create and update GPU page table
 81 * @npages:     number of pages
 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: the actual location, 0 for CPU, or GPU id
 93 * @granularity:migration granularity, log2 num pages
 94 * @invalid:    not 0 means cpu page table is invalidated
 95 * @validate_timestamp: system timestamp when range is validated
 96 * @notifier:   register mmu interval notifier
 97 * @work_item:  deferred work item information
 98 * @deferred_list: list header used to add range to deferred list
 99 * @child_list: list header for split ranges which are not added to svms yet
100 * @bitmap_access: index bitmap of GPUs which can access the range
101 * @bitmap_aip: index bitmap of GPUs which can access the range in place
102 *
103 * Data structure for virtual memory range shared by CPU and GPUs, it can be
104 * allocated from system memory ram or device vram, and migrate from ram to vram
105 * or from vram to ram.
106 */
107struct svm_range {
108	struct svm_range_list		*svms;
109	struct mutex			migrate_mutex;
110	unsigned long			start;
111	unsigned long			last;
112	struct interval_tree_node	it_node;
113	struct list_head		list;
114	struct list_head		update_list;
115	struct list_head		remove_list;
116	struct list_head		insert_list;
117	uint64_t			npages;
118	dma_addr_t			*dma_addr[MAX_GPU_INSTANCE];
119	struct ttm_resource		*ttm_res;
120	uint64_t			offset;
121	struct svm_range_bo		*svm_bo;
122	struct list_head		svm_bo_list;
123	struct mutex                    lock;
124	unsigned int                    saved_flags;
125	uint32_t			flags;
126	uint32_t			preferred_loc;
127	uint32_t			prefetch_loc;
128	uint32_t			actual_loc;
129	uint8_t				granularity;
130	atomic_t			invalid;
131	uint64_t			validate_timestamp;
132	struct mmu_interval_notifier	notifier;
133	struct svm_work_list_item	work_item;
134	struct list_head		deferred_list;
135	struct list_head		child_list;
136	DECLARE_BITMAP(bitmap_access, MAX_GPU_INSTANCE);
137	DECLARE_BITMAP(bitmap_aip, MAX_GPU_INSTANCE);
138	bool				validated_once;
139};
140
141static inline void svm_range_lock(struct svm_range *prange)
142{
143	mutex_lock(&prange->lock);
144	prange->saved_flags = memalloc_noreclaim_save();
145
146}
147static inline void svm_range_unlock(struct svm_range *prange)
148{
149	memalloc_noreclaim_restore(prange->saved_flags);
150	mutex_unlock(&prange->lock);
151}
152
153static inline struct svm_range_bo *svm_range_bo_ref(struct svm_range_bo *svm_bo)
154{
155	if (svm_bo)
156		kref_get(&svm_bo->kref);
157
158	return svm_bo;
159}
160
161int svm_range_list_init(struct kfd_process *p);
162void svm_range_list_fini(struct kfd_process *p);
163int svm_ioctl(struct kfd_process *p, enum kfd_ioctl_svm_op op, uint64_t start,
164	      uint64_t size, uint32_t nattrs,
165	      struct kfd_ioctl_svm_attribute *attrs);
166struct svm_range *svm_range_from_addr(struct svm_range_list *svms,
167				      unsigned long addr,
168				      struct svm_range **parent);
169struct amdgpu_device *svm_range_get_adev_by_id(struct svm_range *prange,
170					       uint32_t id);
171int svm_range_vram_node_new(struct amdgpu_device *adev,
172			    struct svm_range *prange, bool clear);
173void svm_range_vram_node_free(struct svm_range *prange);
174int svm_range_split_by_granularity(struct kfd_process *p, struct mm_struct *mm,
175			       unsigned long addr, struct svm_range *parent,
176			       struct svm_range *prange);
177int svm_range_restore_pages(struct amdgpu_device *adev,
178			    unsigned int pasid, uint64_t addr);
179int svm_range_schedule_evict_svm_bo(struct amdgpu_amdkfd_fence *fence);
180void svm_range_add_list_work(struct svm_range_list *svms,
181			     struct svm_range *prange, struct mm_struct *mm,
182			     enum svm_work_list_ops op);
183void schedule_deferred_list_work(struct svm_range_list *svms);
184void svm_range_dma_unmap(struct device *dev, dma_addr_t *dma_addr,
185			 unsigned long offset, unsigned long npages);
186void svm_range_free_dma_mappings(struct svm_range *prange);
187void svm_range_prefault(struct svm_range *prange, struct mm_struct *mm,
188			void *owner);
189struct kfd_process_device *
190svm_range_get_pdd_by_adev(struct svm_range *prange, struct amdgpu_device *adev);
191
192/* SVM API and HMM page migration work together, device memory type
193 * is initialized to not 0 when page migration register device memory.
194 */
195#define KFD_IS_SVM_API_SUPPORTED(dev) ((dev)->pgmap.type != 0)
196
197void svm_range_bo_unref(struct svm_range_bo *svm_bo);
198#else
199
200struct kfd_process;
201
202static inline int svm_range_list_init(struct kfd_process *p)
203{
204	return 0;
205}
206static inline void svm_range_list_fini(struct kfd_process *p)
207{
208	/* empty */
209}
210
211static inline int svm_range_restore_pages(struct amdgpu_device *adev,
212					  unsigned int pasid, uint64_t addr)
213{
214	return -EFAULT;
215}
216
217static inline int svm_range_schedule_evict_svm_bo(
218		struct amdgpu_amdkfd_fence *fence)
219{
220	WARN_ONCE(1, "SVM eviction fence triggered, but SVM is disabled");
221	return -EINVAL;
222}
223
224#define KFD_IS_SVM_API_SUPPORTED(dev) false
225
226#endif /* IS_ENABLED(CONFIG_HSA_AMD_SVM) */
227
228#endif /* KFD_SVM_H_ */