drm/amdkfd: add svm range validate timestamp
[linux-2.6-block.git] / drivers / gpu / drm / amd / amdkfd / 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 #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>
33 #include "amdgpu.h"
34 #include "kfd_priv.h"
35
36 struct svm_range_bo {
37         struct amdgpu_bo                *bo;
38         struct kref                     kref;
39         struct list_head                range_list; /* all svm ranges shared this bo */
40         spinlock_t                      list_lock;
41         struct amdgpu_amdkfd_fence      *eviction_fence;
42         struct work_struct              eviction_work;
43         struct svm_range_list           *svms;
44         uint32_t                        evicting;
45 };
46
47 enum svm_work_list_ops {
48         SVM_OP_NULL,
49         SVM_OP_UNMAP_RANGE,
50         SVM_OP_UPDATE_RANGE_NOTIFIER,
51         SVM_OP_UPDATE_RANGE_NOTIFIER_AND_MAP,
52         SVM_OP_ADD_RANGE,
53         SVM_OP_ADD_RANGE_AND_MAP
54 };
55
56 struct svm_work_list_item {
57         enum svm_work_list_ops op;
58         struct mm_struct *mm;
59 };
60
61 /**
62  * struct svm_range - shared virtual memory range
63  *
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
96  *
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.
100  */
101 struct svm_range {
102         struct svm_range_list           *svms;
103         struct mutex                    migrate_mutex;
104         unsigned long                   start;
105         unsigned long                   last;
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;
112         uint64_t                        npages;
113         dma_addr_t                      *dma_addr[MAX_GPU_INSTANCE];
114         struct ttm_resource             *ttm_res;
115         uint64_t                        offset;
116         struct svm_range_bo             *svm_bo;
117         struct list_head                svm_bo_list;
118         struct mutex                    lock;
119         unsigned int                    saved_flags;
120         uint32_t                        flags;
121         uint32_t                        preferred_loc;
122         uint32_t                        prefetch_loc;
123         uint32_t                        actual_loc;
124         uint8_t                         granularity;
125         atomic_t                        invalid;
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);
133         bool                            validated_once;
134 };
135
136 static inline void svm_range_lock(struct svm_range *prange)
137 {
138         mutex_lock(&prange->lock);
139         prange->saved_flags = memalloc_noreclaim_save();
140
141 }
142 static inline void svm_range_unlock(struct svm_range *prange)
143 {
144         memalloc_noreclaim_restore(prange->saved_flags);
145         mutex_unlock(&prange->lock);
146 }
147
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,
154                                       unsigned long addr,
155                                       struct svm_range **parent);
156 struct amdgpu_device *svm_range_get_adev_by_id(struct svm_range *prange,
157                                                uint32_t id);
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);
175
176 #endif /* KFD_SVM_H_ */