2 * Copyright 2016 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
25 #define MAX_KIQ_REG_WAIT 5000 /* in usecs, 5ms */
26 #define MAX_KIQ_REG_BAILOUT_INTERVAL 5 /* in msecs, 5ms */
27 #define MAX_KIQ_REG_TRY 20
29 uint64_t amdgpu_csa_vaddr(struct amdgpu_device
*adev
)
31 uint64_t addr
= adev
->vm_manager
.max_pfn
<< AMDGPU_GPU_PAGE_SHIFT
;
33 addr
-= AMDGPU_VA_RESERVED_SIZE
;
35 if (addr
>= AMDGPU_VA_HOLE_START
)
36 addr
|= AMDGPU_VA_HOLE_END
;
41 bool amdgpu_virt_mmio_blocked(struct amdgpu_device
*adev
)
43 /* By now all MMIO pages except mailbox are blocked */
44 /* if blocking is enabled in hypervisor. Choose the */
45 /* SCRATCH_REG0 to test. */
46 return RREG32_NO_KIQ(0xc040) == 0xffffffff;
49 int amdgpu_allocate_static_csa(struct amdgpu_device
*adev
)
54 r
= amdgpu_bo_create_kernel(adev
, AMDGPU_CSA_SIZE
, PAGE_SIZE
,
55 AMDGPU_GEM_DOMAIN_VRAM
, &adev
->virt
.csa_obj
,
56 &adev
->virt
.csa_vmid0_addr
, &ptr
);
60 memset(ptr
, 0, AMDGPU_CSA_SIZE
);
64 void amdgpu_free_static_csa(struct amdgpu_device
*adev
) {
65 amdgpu_bo_free_kernel(&adev
->virt
.csa_obj
,
66 &adev
->virt
.csa_vmid0_addr
,
71 * amdgpu_map_static_csa should be called during amdgpu_vm_init
72 * it maps virtual address amdgpu_csa_vaddr() to this VM, and each command
73 * submission of GFX should use this virtual address within META_DATA init
74 * package to support SRIOV gfx preemption.
76 int amdgpu_map_static_csa(struct amdgpu_device
*adev
, struct amdgpu_vm
*vm
,
77 struct amdgpu_bo_va
**bo_va
)
79 uint64_t csa_addr
= amdgpu_csa_vaddr(adev
) & AMDGPU_VA_HOLE_MASK
;
80 struct ww_acquire_ctx ticket
;
81 struct list_head list
;
82 struct amdgpu_bo_list_entry pd
;
83 struct ttm_validate_buffer csa_tv
;
86 INIT_LIST_HEAD(&list
);
87 INIT_LIST_HEAD(&csa_tv
.head
);
88 csa_tv
.bo
= &adev
->virt
.csa_obj
->tbo
;
91 list_add(&csa_tv
.head
, &list
);
92 amdgpu_vm_get_pd_bo(vm
, &list
, &pd
);
94 r
= ttm_eu_reserve_buffers(&ticket
, &list
, true, NULL
);
96 DRM_ERROR("failed to reserve CSA,PD BOs: err=%d\n", r
);
100 *bo_va
= amdgpu_vm_bo_add(adev
, vm
, adev
->virt
.csa_obj
);
102 ttm_eu_backoff_reservation(&ticket
, &list
);
103 DRM_ERROR("failed to create bo_va for static CSA\n");
107 r
= amdgpu_vm_alloc_pts(adev
, (*bo_va
)->base
.vm
, csa_addr
,
110 DRM_ERROR("failed to allocate pts for static CSA, err=%d\n", r
);
111 amdgpu_vm_bo_rmv(adev
, *bo_va
);
112 ttm_eu_backoff_reservation(&ticket
, &list
);
116 r
= amdgpu_vm_bo_map(adev
, *bo_va
, csa_addr
, 0, AMDGPU_CSA_SIZE
,
117 AMDGPU_PTE_READABLE
| AMDGPU_PTE_WRITEABLE
|
118 AMDGPU_PTE_EXECUTABLE
);
121 DRM_ERROR("failed to do bo_map on static CSA, err=%d\n", r
);
122 amdgpu_vm_bo_rmv(adev
, *bo_va
);
123 ttm_eu_backoff_reservation(&ticket
, &list
);
127 ttm_eu_backoff_reservation(&ticket
, &list
);
131 void amdgpu_virt_init_setting(struct amdgpu_device
*adev
)
133 /* enable virtual display */
134 adev
->mode_info
.num_crtc
= 1;
135 adev
->enable_virtual_display
= true;
140 uint32_t amdgpu_virt_kiq_rreg(struct amdgpu_device
*adev
, uint32_t reg
)
142 signed long r
, cnt
= 0;
145 struct amdgpu_kiq
*kiq
= &adev
->gfx
.kiq
;
146 struct amdgpu_ring
*ring
= &kiq
->ring
;
148 BUG_ON(!ring
->funcs
->emit_rreg
);
150 spin_lock_irqsave(&kiq
->ring_lock
, flags
);
151 amdgpu_ring_alloc(ring
, 32);
152 amdgpu_ring_emit_rreg(ring
, reg
);
153 amdgpu_fence_emit_polling(ring
, &seq
);
154 amdgpu_ring_commit(ring
);
155 spin_unlock_irqrestore(&kiq
->ring_lock
, flags
);
157 r
= amdgpu_fence_wait_polling(ring
, seq
, MAX_KIQ_REG_WAIT
);
159 /* don't wait anymore for gpu reset case because this way may
160 * block gpu_recover() routine forever, e.g. this virt_kiq_rreg
161 * is triggered in TTM and ttm_bo_lock_delayed_workqueue() will
162 * never return if we keep waiting in virt_kiq_rreg, which cause
163 * gpu_recover() hang there.
165 * also don't wait anymore for IRQ context
167 if (r
< 1 && (adev
->in_gpu_reset
|| in_interrupt()))
168 goto failed_kiq_read
;
173 while (r
< 1 && cnt
++ < MAX_KIQ_REG_TRY
) {
174 msleep(MAX_KIQ_REG_BAILOUT_INTERVAL
);
175 r
= amdgpu_fence_wait_polling(ring
, seq
, MAX_KIQ_REG_WAIT
);
178 if (cnt
> MAX_KIQ_REG_TRY
)
179 goto failed_kiq_read
;
181 return adev
->wb
.wb
[adev
->virt
.reg_val_offs
];
184 pr_err("failed to read reg:%x\n", reg
);
188 void amdgpu_virt_kiq_wreg(struct amdgpu_device
*adev
, uint32_t reg
, uint32_t v
)
190 signed long r
, cnt
= 0;
193 struct amdgpu_kiq
*kiq
= &adev
->gfx
.kiq
;
194 struct amdgpu_ring
*ring
= &kiq
->ring
;
196 BUG_ON(!ring
->funcs
->emit_wreg
);
198 spin_lock_irqsave(&kiq
->ring_lock
, flags
);
199 amdgpu_ring_alloc(ring
, 32);
200 amdgpu_ring_emit_wreg(ring
, reg
, v
);
201 amdgpu_fence_emit_polling(ring
, &seq
);
202 amdgpu_ring_commit(ring
);
203 spin_unlock_irqrestore(&kiq
->ring_lock
, flags
);
205 r
= amdgpu_fence_wait_polling(ring
, seq
, MAX_KIQ_REG_WAIT
);
207 /* don't wait anymore for gpu reset case because this way may
208 * block gpu_recover() routine forever, e.g. this virt_kiq_rreg
209 * is triggered in TTM and ttm_bo_lock_delayed_workqueue() will
210 * never return if we keep waiting in virt_kiq_rreg, which cause
211 * gpu_recover() hang there.
213 * also don't wait anymore for IRQ context
215 if (r
< 1 && (adev
->in_gpu_reset
|| in_interrupt()))
216 goto failed_kiq_write
;
221 while (r
< 1 && cnt
++ < MAX_KIQ_REG_TRY
) {
223 msleep(MAX_KIQ_REG_BAILOUT_INTERVAL
);
224 r
= amdgpu_fence_wait_polling(ring
, seq
, MAX_KIQ_REG_WAIT
);
227 if (cnt
> MAX_KIQ_REG_TRY
)
228 goto failed_kiq_write
;
233 pr_err("failed to write reg:%x\n", reg
);
237 * amdgpu_virt_request_full_gpu() - request full gpu access
238 * @amdgpu: amdgpu device.
239 * @init: is driver init time.
240 * When start to init/fini driver, first need to request full gpu access.
241 * Return: Zero if request success, otherwise will return error.
243 int amdgpu_virt_request_full_gpu(struct amdgpu_device
*adev
, bool init
)
245 struct amdgpu_virt
*virt
= &adev
->virt
;
248 if (virt
->ops
&& virt
->ops
->req_full_gpu
) {
249 r
= virt
->ops
->req_full_gpu(adev
, init
);
253 adev
->virt
.caps
&= ~AMDGPU_SRIOV_CAPS_RUNTIME
;
260 * amdgpu_virt_release_full_gpu() - release full gpu access
261 * @amdgpu: amdgpu device.
262 * @init: is driver init time.
263 * When finishing driver init/fini, need to release full gpu access.
264 * Return: Zero if release success, otherwise will returen error.
266 int amdgpu_virt_release_full_gpu(struct amdgpu_device
*adev
, bool init
)
268 struct amdgpu_virt
*virt
= &adev
->virt
;
271 if (virt
->ops
&& virt
->ops
->rel_full_gpu
) {
272 r
= virt
->ops
->rel_full_gpu(adev
, init
);
276 adev
->virt
.caps
|= AMDGPU_SRIOV_CAPS_RUNTIME
;
282 * amdgpu_virt_reset_gpu() - reset gpu
283 * @amdgpu: amdgpu device.
284 * Send reset command to GPU hypervisor to reset GPU that VM is using
285 * Return: Zero if reset success, otherwise will return error.
287 int amdgpu_virt_reset_gpu(struct amdgpu_device
*adev
)
289 struct amdgpu_virt
*virt
= &adev
->virt
;
292 if (virt
->ops
&& virt
->ops
->reset_gpu
) {
293 r
= virt
->ops
->reset_gpu(adev
);
297 adev
->virt
.caps
&= ~AMDGPU_SRIOV_CAPS_RUNTIME
;
304 * amdgpu_virt_wait_reset() - wait for reset gpu completed
305 * @amdgpu: amdgpu device.
306 * Wait for GPU reset completed.
307 * Return: Zero if reset success, otherwise will return error.
309 int amdgpu_virt_wait_reset(struct amdgpu_device
*adev
)
311 struct amdgpu_virt
*virt
= &adev
->virt
;
313 if (!virt
->ops
|| !virt
->ops
->wait_reset
)
316 return virt
->ops
->wait_reset(adev
);
320 * amdgpu_virt_alloc_mm_table() - alloc memory for mm table
321 * @amdgpu: amdgpu device.
322 * MM table is used by UVD and VCE for its initialization
323 * Return: Zero if allocate success.
325 int amdgpu_virt_alloc_mm_table(struct amdgpu_device
*adev
)
329 if (!amdgpu_sriov_vf(adev
) || adev
->virt
.mm_table
.gpu_addr
)
332 r
= amdgpu_bo_create_kernel(adev
, PAGE_SIZE
, PAGE_SIZE
,
333 AMDGPU_GEM_DOMAIN_VRAM
,
334 &adev
->virt
.mm_table
.bo
,
335 &adev
->virt
.mm_table
.gpu_addr
,
336 (void *)&adev
->virt
.mm_table
.cpu_addr
);
338 DRM_ERROR("failed to alloc mm table and error = %d.\n", r
);
342 memset((void *)adev
->virt
.mm_table
.cpu_addr
, 0, PAGE_SIZE
);
343 DRM_INFO("MM table gpu addr = 0x%llx, cpu addr = %p.\n",
344 adev
->virt
.mm_table
.gpu_addr
,
345 adev
->virt
.mm_table
.cpu_addr
);
350 * amdgpu_virt_free_mm_table() - free mm table memory
351 * @amdgpu: amdgpu device.
352 * Free MM table memory
354 void amdgpu_virt_free_mm_table(struct amdgpu_device
*adev
)
356 if (!amdgpu_sriov_vf(adev
) || !adev
->virt
.mm_table
.gpu_addr
)
359 amdgpu_bo_free_kernel(&adev
->virt
.mm_table
.bo
,
360 &adev
->virt
.mm_table
.gpu_addr
,
361 (void *)&adev
->virt
.mm_table
.cpu_addr
);
362 adev
->virt
.mm_table
.gpu_addr
= 0;
366 int amdgpu_virt_fw_reserve_get_checksum(void *obj
,
367 unsigned long obj_size
,
371 unsigned int ret
= key
;
376 /* calculate checksum */
377 for (i
= 0; i
< obj_size
; ++i
)
379 /* minus the chksum itself */
380 pos
= (char *)&chksum
;
381 for (i
= 0; i
< sizeof(chksum
); ++i
)
386 void amdgpu_virt_init_data_exchange(struct amdgpu_device
*adev
)
388 uint32_t pf2vf_size
= 0;
389 uint32_t checksum
= 0;
393 adev
->virt
.fw_reserve
.p_pf2vf
= NULL
;
394 adev
->virt
.fw_reserve
.p_vf2pf
= NULL
;
396 if (adev
->fw_vram_usage
.va
!= NULL
) {
397 adev
->virt
.fw_reserve
.p_pf2vf
=
398 (struct amdgim_pf2vf_info_header
*)(
399 adev
->fw_vram_usage
.va
+ AMDGIM_DATAEXCHANGE_OFFSET
);
400 AMDGPU_FW_VRAM_PF2VF_READ(adev
, header
.size
, &pf2vf_size
);
401 AMDGPU_FW_VRAM_PF2VF_READ(adev
, checksum
, &checksum
);
402 AMDGPU_FW_VRAM_PF2VF_READ(adev
, feature_flags
, &adev
->virt
.gim_feature
);
404 /* pf2vf message must be in 4K */
405 if (pf2vf_size
> 0 && pf2vf_size
< 4096) {
406 checkval
= amdgpu_virt_fw_reserve_get_checksum(
407 adev
->virt
.fw_reserve
.p_pf2vf
, pf2vf_size
,
408 adev
->virt
.fw_reserve
.checksum_key
, checksum
);
409 if (checkval
== checksum
) {
410 adev
->virt
.fw_reserve
.p_vf2pf
=
411 ((void *)adev
->virt
.fw_reserve
.p_pf2vf
+
413 memset((void *)adev
->virt
.fw_reserve
.p_vf2pf
, 0,
414 sizeof(amdgim_vf2pf_info
));
415 AMDGPU_FW_VRAM_VF2PF_WRITE(adev
, header
.version
,
416 AMDGPU_FW_VRAM_VF2PF_VER
);
417 AMDGPU_FW_VRAM_VF2PF_WRITE(adev
, header
.size
,
418 sizeof(amdgim_vf2pf_info
));
419 AMDGPU_FW_VRAM_VF2PF_READ(adev
, driver_version
,
422 if (THIS_MODULE
->version
!= NULL
)
423 strcpy(str
, THIS_MODULE
->version
);
427 AMDGPU_FW_VRAM_VF2PF_WRITE(adev
, driver_cert
,
429 AMDGPU_FW_VRAM_VF2PF_WRITE(adev
, checksum
,
430 amdgpu_virt_fw_reserve_get_checksum(
431 adev
->virt
.fw_reserve
.p_vf2pf
,
433 adev
->virt
.fw_reserve
.checksum_key
, 0));