blob: 1fc48e1690ffcd95d31883339ee7073181c41c16 [file] [log] [blame]
Monk Liu4e4bbe72017-01-09 15:21:13 +08001/*
2 * Copyright 2016 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
24#include "amdgpu.h"
25
26int amdgpu_allocate_static_csa(struct amdgpu_device *adev)
27{
28 int r;
29 void *ptr;
30
31 r = amdgpu_bo_create_kernel(adev, AMDGPU_CSA_SIZE, PAGE_SIZE,
32 AMDGPU_GEM_DOMAIN_VRAM, &adev->virt.csa_obj,
33 &adev->virt.csa_vmid0_addr, &ptr);
34 if (r)
35 return r;
36
37 memset(ptr, 0, AMDGPU_CSA_SIZE);
38 return 0;
39}
40
41/*
42 * amdgpu_map_static_csa should be called during amdgpu_vm_init
43 * it maps virtual address "AMDGPU_VA_RESERVED_SIZE - AMDGPU_CSA_SIZE"
44 * to this VM, and each command submission of GFX should use this virtual
45 * address within META_DATA init package to support SRIOV gfx preemption.
46 */
47
48int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm)
49{
50 int r;
51 struct amdgpu_bo_va *bo_va;
52 struct ww_acquire_ctx ticket;
53 struct list_head list;
54 struct amdgpu_bo_list_entry pd;
55 struct ttm_validate_buffer csa_tv;
56
57 INIT_LIST_HEAD(&list);
58 INIT_LIST_HEAD(&csa_tv.head);
59 csa_tv.bo = &adev->virt.csa_obj->tbo;
60 csa_tv.shared = true;
61
62 list_add(&csa_tv.head, &list);
63 amdgpu_vm_get_pd_bo(vm, &list, &pd);
64
65 r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
66 if (r) {
67 DRM_ERROR("failed to reserve CSA,PD BOs: err=%d\n", r);
68 return r;
69 }
70
71 bo_va = amdgpu_vm_bo_add(adev, vm, adev->virt.csa_obj);
72 if (!bo_va) {
73 ttm_eu_backoff_reservation(&ticket, &list);
74 DRM_ERROR("failed to create bo_va for static CSA\n");
75 return -ENOMEM;
76 }
77
78 r = amdgpu_vm_bo_map(adev, bo_va, AMDGPU_CSA_VADDR, 0,AMDGPU_CSA_SIZE,
79 AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE |
80 AMDGPU_PTE_EXECUTABLE);
81
82 if (r) {
83 DRM_ERROR("failed to do bo_map on static CSA, err=%d\n", r);
84 amdgpu_vm_bo_rmv(adev, bo_va);
85 ttm_eu_backoff_reservation(&ticket, &list);
Monk Liu4e4bbe72017-01-09 15:21:13 +080086 return r;
87 }
88
89 vm->csa_bo_va = bo_va;
90 ttm_eu_backoff_reservation(&ticket, &list);
91 return 0;
92}
Xiangliang Yubc992ba2017-01-12 14:29:34 +080093
94void amdgpu_virt_init_setting(struct amdgpu_device *adev)
95{
Xiangliang Yu06465d82017-01-11 17:18:40 +080096 /* enable virtual display */
97 adev->mode_info.num_crtc = 1;
98 adev->enable_virtual_display = true;
99
Monk Liued17c71b32017-01-25 15:33:56 +0800100 mutex_init(&adev->virt.lock_kiq);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800101}
102
103uint32_t amdgpu_virt_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)
104{
105 signed long r;
106 uint32_t val;
107 struct dma_fence *f;
108 struct amdgpu_kiq *kiq = &adev->gfx.kiq;
109 struct amdgpu_ring *ring = &kiq->ring;
110
111 BUG_ON(!ring->funcs->emit_rreg);
112
Monk Liued17c71b32017-01-25 15:33:56 +0800113 mutex_lock(&adev->virt.lock_kiq);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800114 amdgpu_ring_alloc(ring, 32);
115 amdgpu_ring_emit_hdp_flush(ring);
116 amdgpu_ring_emit_rreg(ring, reg);
117 amdgpu_ring_emit_hdp_invalidate(ring);
118 amdgpu_fence_emit(ring, &f);
119 amdgpu_ring_commit(ring);
Monk Liued17c71b32017-01-25 15:33:56 +0800120 mutex_unlock(&adev->virt.lock_kiq);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800121
122 r = dma_fence_wait(f, false);
123 if (r)
124 DRM_ERROR("wait for kiq fence error: %ld.\n", r);
125 dma_fence_put(f);
126
127 val = adev->wb.wb[adev->virt.reg_val_offs];
128
129 return val;
130}
131
132void amdgpu_virt_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
133{
134 signed long r;
135 struct dma_fence *f;
136 struct amdgpu_kiq *kiq = &adev->gfx.kiq;
137 struct amdgpu_ring *ring = &kiq->ring;
138
139 BUG_ON(!ring->funcs->emit_wreg);
140
Monk Liued17c71b32017-01-25 15:33:56 +0800141 mutex_lock(&adev->virt.lock_kiq);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800142 amdgpu_ring_alloc(ring, 32);
143 amdgpu_ring_emit_hdp_flush(ring);
144 amdgpu_ring_emit_wreg(ring, reg, v);
145 amdgpu_ring_emit_hdp_invalidate(ring);
146 amdgpu_fence_emit(ring, &f);
147 amdgpu_ring_commit(ring);
Monk Liued17c71b32017-01-25 15:33:56 +0800148 mutex_unlock(&adev->virt.lock_kiq);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800149
150 r = dma_fence_wait(f, false);
151 if (r)
152 DRM_ERROR("wait for kiq fence error: %ld.\n", r);
153 dma_fence_put(f);
154}
Xiangliang Yu1e9f1392017-01-12 14:53:08 +0800155
156/**
157 * amdgpu_virt_request_full_gpu() - request full gpu access
158 * @amdgpu: amdgpu device.
159 * @init: is driver init time.
160 * When start to init/fini driver, first need to request full gpu access.
161 * Return: Zero if request success, otherwise will return error.
162 */
163int amdgpu_virt_request_full_gpu(struct amdgpu_device *adev, bool init)
164{
165 struct amdgpu_virt *virt = &adev->virt;
166 int r;
167
168 if (virt->ops && virt->ops->req_full_gpu) {
169 r = virt->ops->req_full_gpu(adev, init);
170 if (r)
171 return r;
172
173 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
174 }
175
176 return 0;
177}
178
179/**
180 * amdgpu_virt_release_full_gpu() - release full gpu access
181 * @amdgpu: amdgpu device.
182 * @init: is driver init time.
183 * When finishing driver init/fini, need to release full gpu access.
184 * Return: Zero if release success, otherwise will returen error.
185 */
186int amdgpu_virt_release_full_gpu(struct amdgpu_device *adev, bool init)
187{
188 struct amdgpu_virt *virt = &adev->virt;
189 int r;
190
191 if (virt->ops && virt->ops->rel_full_gpu) {
192 r = virt->ops->rel_full_gpu(adev, init);
193 if (r)
194 return r;
195
196 adev->virt.caps |= AMDGPU_SRIOV_CAPS_RUNTIME;
197 }
198 return 0;
199}
200
201/**
202 * amdgpu_virt_reset_gpu() - reset gpu
203 * @amdgpu: amdgpu device.
204 * Send reset command to GPU hypervisor to reset GPU that VM is using
205 * Return: Zero if reset success, otherwise will return error.
206 */
207int amdgpu_virt_reset_gpu(struct amdgpu_device *adev)
208{
209 struct amdgpu_virt *virt = &adev->virt;
210 int r;
211
212 if (virt->ops && virt->ops->reset_gpu) {
213 r = virt->ops->reset_gpu(adev);
214 if (r)
215 return r;
216
217 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
218 }
219
220 return 0;
221}