blob: a8418a3f4e9d3f28d39cff6ece0e9462c1b9845e [file] [log] [blame]
Oded Gabbay130e0372015-06-12 21:35:14 +03001/*
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>
Oded Gabbay7420f482018-03-15 10:08:35 +020029#include <linux/mm.h>
Felix Kuehling70539bd2017-08-15 23:00:17 -040030#include <linux/mmu_context.h>
Felix Kuehling5ae02832018-03-23 15:32:33 -040031#include <linux/workqueue.h>
Oded Gabbay130e0372015-06-12 21:35:14 +030032#include <kgd_kfd_interface.h>
Felix Kuehlinga46a2cd2018-02-06 20:32:38 -050033#include <drm/ttm/ttm_execbuf_util.h>
34#include "amdgpu_sync.h"
35#include "amdgpu_vm.h"
Oded Gabbay130e0372015-06-12 21:35:14 +030036
Felix Kuehlingd8d019c2018-02-06 20:32:35 -050037extern const struct kgd2kfd_calls *kgd2kfd;
38
Oded Gabbay130e0372015-06-12 21:35:14 +030039struct amdgpu_device;
40
Felix Kuehlinga46a2cd2018-02-06 20:32:38 -050041struct 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
Oded Gabbay130e0372015-06-12 21:35:14 +030050struct kgd_mem {
Felix Kuehlinga46a2cd2018-02-06 20:32:38 -050051 struct mutex lock;
Oded Gabbay130e0372015-06-12 21:35:14 +030052 struct amdgpu_bo *bo;
Felix Kuehlinga46a2cd2018-02-06 20:32:38 -050053 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
Felix Kuehling5ae02832018-03-23 15:32:33 -040063 atomic_t invalid;
Felix Kuehlinga46a2cd2018-02-06 20:32:38 -050064 struct amdkfd_process_info *process_info;
Felix Kuehling5ae02832018-03-23 15:32:33 -040065 struct page **user_pages;
Felix Kuehlinga46a2cd2018-02-06 20:32:38 -050066
67 struct amdgpu_sync sync;
68
69 bool aql_queue;
Oded Gabbay130e0372015-06-12 21:35:14 +030070};
71
Felix Kuehlingd8d019c2018-02-06 20:32:35 -050072/* KFD Memory Eviction */
73struct 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
80struct amdgpu_amdkfd_fence *amdgpu_amdkfd_fence_create(u64 context,
81 struct mm_struct *mm);
82bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm);
83struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f);
84
Felix Kuehlinga46a2cd2018-02-06 20:32:38 -050085struct 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;
Felix Kuehling5ae02832018-03-23 15:32:33 -040090 /* List of userptr BOs that are valid or invalid */
91 struct list_head userptr_valid_list;
92 struct list_head userptr_inval_list;
Felix Kuehlinga46a2cd2018-02-06 20:32:38 -050093 /* 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;
Felix Kuehling5ae02832018-03-23 15:32:33 -0400100
101 /* MMU-notifier related fields */
102 atomic_t evicted_bos;
103 struct delayed_work restore_userptr_work;
104 struct pid *pid;
Felix Kuehlinga46a2cd2018-02-06 20:32:38 -0500105};
106
Oded Gabbayefb1c652016-02-09 13:30:12 +0200107int amdgpu_amdkfd_init(void);
Oded Gabbay130e0372015-06-12 21:35:14 +0300108void amdgpu_amdkfd_fini(void);
109
Andres Rodriguezdc102c42017-02-01 17:02:13 -0500110void amdgpu_amdkfd_suspend(struct amdgpu_device *adev);
111int amdgpu_amdkfd_resume(struct amdgpu_device *adev);
112void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev,
Oded Gabbay130e0372015-06-12 21:35:14 +0300113 const void *ih_ring_entry);
Andres Rodriguezdc102c42017-02-01 17:02:13 -0500114void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev);
115void amdgpu_amdkfd_device_init(struct amdgpu_device *adev);
116void amdgpu_amdkfd_device_fini(struct amdgpu_device *adev);
Oded Gabbay130e0372015-06-12 21:35:14 +0300117
Felix Kuehlinge52482d2018-03-23 15:32:28 -0400118int amdgpu_amdkfd_evict_userptr(struct kgd_mem *mem, struct mm_struct *mm);
Felix Kuehling4c660c82018-02-06 20:32:39 -0500119int 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
Oded Gabbay130e0372015-06-12 21:35:14 +0300123struct kfd2kgd_calls *amdgpu_amdkfd_gfx_7_get_functions(void);
Ben Gozff758a12014-10-07 14:43:07 +0300124struct kfd2kgd_calls *amdgpu_amdkfd_gfx_8_0_get_functions(void);
Felix Kuehlingd5a114a2018-04-10 17:33:01 -0400125struct kfd2kgd_calls *amdgpu_amdkfd_gfx_9_0_get_functions(void);
Oded Gabbay130e0372015-06-12 21:35:14 +0300126
Felix Kuehling155494d2018-02-06 20:32:36 -0500127bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid);
128
Oded Gabbay130e0372015-06-12 21:35:14 +0300129/* Shared API */
130int alloc_gtt_mem(struct kgd_dev *kgd, size_t size,
131 void **mem_obj, uint64_t *gpu_addr,
132 void **cpu_ptr);
133void free_gtt_mem(struct kgd_dev *kgd, void *mem_obj);
Harish Kasiviswanathan30f1c042017-12-08 23:08:42 -0500134void get_local_mem_info(struct kgd_dev *kgd,
135 struct kfd_local_mem_info *mem_info);
Oded Gabbay130e0372015-06-12 21:35:14 +0300136uint64_t get_gpu_clock_counter(struct kgd_dev *kgd);
137
138uint32_t get_max_engine_clock_in_mhz(struct kgd_dev *kgd);
Flora Cuiebdebf42017-12-08 23:08:40 -0500139void get_cu_info(struct kgd_dev *kgd, struct kfd_cu_info *cu_info);
Kent Russell9f0a0b42017-12-08 23:09:05 -0500140uint64_t amdgpu_amdkfd_get_vram_usage(struct kgd_dev *kgd);
Oded Gabbay130e0372015-06-12 21:35:14 +0300141
Felix Kuehling70539bd2017-08-15 23:00:17 -0400142#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
Felix Kuehlinga46a2cd2018-02-06 20:32:38 -0500157/* GPUVM API */
158int amdgpu_amdkfd_gpuvm_create_process_vm(struct kgd_dev *kgd, void **vm,
Oded Gabbayfcdfa432018-05-18 22:18:16 +0300159 void **process_info,
160 struct dma_fence **ef);
Felix Kuehlingede0dd82018-03-15 17:27:43 -0400161int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct kgd_dev *kgd,
Oded Gabbayfcdfa432018-05-18 22:18:16 +0300162 struct file *filp,
163 void **vm, void **process_info,
164 struct dma_fence **ef);
Felix Kuehlingede0dd82018-03-15 17:27:43 -0400165void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev,
Oded Gabbayfcdfa432018-05-18 22:18:16 +0300166 struct amdgpu_vm *vm);
Felix Kuehlinga46a2cd2018-02-06 20:32:38 -0500167void amdgpu_amdkfd_gpuvm_destroy_process_vm(struct kgd_dev *kgd, void *vm);
168uint32_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *vm);
169int 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);
173int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
174 struct kgd_dev *kgd, struct kgd_mem *mem);
175int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
176 struct kgd_dev *kgd, struct kgd_mem *mem, void *vm);
177int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
178 struct kgd_dev *kgd, struct kgd_mem *mem, void *vm);
179int amdgpu_amdkfd_gpuvm_sync_memory(
180 struct kgd_dev *kgd, struct kgd_mem *mem, bool intr);
181int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
182 struct kgd_mem *mem, void **kptr, uint64_t *size);
183int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info,
184 struct dma_fence **ef);
185
186void amdgpu_amdkfd_gpuvm_init_mem_limits(void);
187void amdgpu_amdkfd_unreserve_system_memory_limit(struct amdgpu_bo *bo);
188
Oded Gabbay130e0372015-06-12 21:35:14 +0300189#endif /* AMDGPU_AMDKFD_H_INCLUDED */