1 /*
2  * Copyright 2014 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /* amdgpu_amdkfd.h defines the private interface between amdgpu and amdkfd. */
24 
25 #ifndef AMDGPU_AMDKFD_H_INCLUDED
26 #define AMDGPU_AMDKFD_H_INCLUDED
27 
28 #include <linux/types.h>
29 #include <linux/mm.h>
30 #include <linux/mmu_context.h>
31 #include <linux/workqueue.h>
32 #include <kgd_kfd_interface.h>
33 #include <drm/ttm/ttm_execbuf_util.h>
34 #include "amdgpu_sync.h"
35 #include "amdgpu_vm.h"
36 
37 extern const struct kgd2kfd_calls *kgd2kfd;
38 
39 struct amdgpu_device;
40 
41 struct kfd_bo_va_list {
42 	struct list_head bo_list;
43 	struct amdgpu_bo_va *bo_va;
44 	void *kgd_dev;
45 	bool is_mapped;
46 	uint64_t va;
47 	uint64_t pte_flags;
48 };
49 
50 struct kgd_mem {
51 	struct mutex lock;
52 	struct amdgpu_bo *bo;
53 	struct list_head bo_va_list;
54 	/* protected by amdkfd_process_info.lock */
55 	struct ttm_validate_buffer validate_list;
56 	struct ttm_validate_buffer resv_list;
57 	uint32_t domain;
58 	unsigned int mapped_to_gpu_memory;
59 	uint64_t va;
60 
61 	uint32_t mapping_flags;
62 
63 	atomic_t invalid;
64 	struct amdkfd_process_info *process_info;
65 	struct page **user_pages;
66 
67 	struct amdgpu_sync sync;
68 
69 	bool aql_queue;
70 };
71 
72 /* KFD Memory Eviction */
73 struct amdgpu_amdkfd_fence {
74 	struct dma_fence base;
75 	struct mm_struct *mm;
76 	spinlock_t lock;
77 	char timeline_name[TASK_COMM_LEN];
78 };
79 
80 struct amdgpu_amdkfd_fence *amdgpu_amdkfd_fence_create(u64 context,
81 						       struct mm_struct *mm);
82 bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm);
83 struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f);
84 
85 struct amdkfd_process_info {
86 	/* List head of all VMs that belong to a KFD process */
87 	struct list_head vm_list_head;
88 	/* List head for all KFD BOs that belong to a KFD process. */
89 	struct list_head kfd_bo_list;
90 	/* List of userptr BOs that are valid or invalid */
91 	struct list_head userptr_valid_list;
92 	struct list_head userptr_inval_list;
93 	/* Lock to protect kfd_bo_list */
94 	struct mutex lock;
95 
96 	/* Number of VMs */
97 	unsigned int n_vms;
98 	/* Eviction Fence */
99 	struct amdgpu_amdkfd_fence *eviction_fence;
100 
101 	/* MMU-notifier related fields */
102 	atomic_t evicted_bos;
103 	struct delayed_work restore_userptr_work;
104 	struct pid *pid;
105 };
106 
107 int amdgpu_amdkfd_init(void);
108 void amdgpu_amdkfd_fini(void);
109 
110 void amdgpu_amdkfd_suspend(struct amdgpu_device *adev);
111 int amdgpu_amdkfd_resume(struct amdgpu_device *adev);
112 void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev,
113 			const void *ih_ring_entry);
114 void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev);
115 void amdgpu_amdkfd_device_init(struct amdgpu_device *adev);
116 void amdgpu_amdkfd_device_fini(struct amdgpu_device *adev);
117 
118 int amdgpu_amdkfd_evict_userptr(struct kgd_mem *mem, struct mm_struct *mm);
119 int amdgpu_amdkfd_submit_ib(struct kgd_dev *kgd, enum kgd_engine_type engine,
120 				uint32_t vmid, uint64_t gpu_addr,
121 				uint32_t *ib_cmd, uint32_t ib_len);
122 
123 struct kfd2kgd_calls *amdgpu_amdkfd_gfx_7_get_functions(void);
124 struct kfd2kgd_calls *amdgpu_amdkfd_gfx_8_0_get_functions(void);
125 struct kfd2kgd_calls *amdgpu_amdkfd_gfx_9_0_get_functions(void);
126 
127 bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid);
128 
129 /* Shared API */
130 int alloc_gtt_mem(struct kgd_dev *kgd, size_t size,
131 			void **mem_obj, uint64_t *gpu_addr,
132 			void **cpu_ptr);
133 void free_gtt_mem(struct kgd_dev *kgd, void *mem_obj);
134 void get_local_mem_info(struct kgd_dev *kgd,
135 			struct kfd_local_mem_info *mem_info);
136 uint64_t get_gpu_clock_counter(struct kgd_dev *kgd);
137 
138 uint32_t get_max_engine_clock_in_mhz(struct kgd_dev *kgd);
139 void get_cu_info(struct kgd_dev *kgd, struct kfd_cu_info *cu_info);
140 uint64_t amdgpu_amdkfd_get_vram_usage(struct kgd_dev *kgd);
141 
142 #define read_user_wptr(mmptr, wptr, dst)				\
143 	({								\
144 		bool valid = false;					\
145 		if ((mmptr) && (wptr)) {				\
146 			if ((mmptr) == current->mm) {			\
147 				valid = !get_user((dst), (wptr));	\
148 			} else if (current->mm == NULL) {		\
149 				use_mm(mmptr);				\
150 				valid = !get_user((dst), (wptr));	\
151 				unuse_mm(mmptr);			\
152 			}						\
153 		}							\
154 		valid;							\
155 	})
156 
157 /* GPUVM API */
158 int amdgpu_amdkfd_gpuvm_create_process_vm(struct kgd_dev *kgd, void **vm,
159 					void **process_info,
160 					struct dma_fence **ef);
161 int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct kgd_dev *kgd,
162 					struct file *filp,
163 					void **vm, void **process_info,
164 					struct dma_fence **ef);
165 void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev,
166 				struct amdgpu_vm *vm);
167 void amdgpu_amdkfd_gpuvm_destroy_process_vm(struct kgd_dev *kgd, void *vm);
168 uint32_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *vm);
169 int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
170 		struct kgd_dev *kgd, uint64_t va, uint64_t size,
171 		void *vm, struct kgd_mem **mem,
172 		uint64_t *offset, uint32_t flags);
173 int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
174 		struct kgd_dev *kgd, struct kgd_mem *mem);
175 int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
176 		struct kgd_dev *kgd, struct kgd_mem *mem, void *vm);
177 int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
178 		struct kgd_dev *kgd, struct kgd_mem *mem, void *vm);
179 int amdgpu_amdkfd_gpuvm_sync_memory(
180 		struct kgd_dev *kgd, struct kgd_mem *mem, bool intr);
181 int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
182 		struct kgd_mem *mem, void **kptr, uint64_t *size);
183 int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info,
184 					    struct dma_fence **ef);
185 
186 void amdgpu_amdkfd_gpuvm_init_mem_limits(void);
187 void amdgpu_amdkfd_unreserve_system_memory_limit(struct amdgpu_bo *bo);
188 
189 #endif /* AMDGPU_AMDKFD_H_INCLUDED */
190