2091 lines · 61.8 KB · C
RawDownload
/* * Copyright 2016 Advanced Micro Devices, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * */ #include <linux/module.h> #ifdef CONFIG_X86#include <asm/hypervisor.h>#endif #include <drm/drm_drv.h>#include <xen/xen.h> #include "amdgpu.h"#include "amdgpu_ras.h"#include "amdgpu_reset.h"#include "amdgpu_dpm.h"#include "vi.h"#include "soc15.h"#include "nv.h"#include "amdgpu_virt_ras_cmd.h" #define POPULATE_UCODE_INFO(vf2pf_info, ucode, ver) \	do { \		vf2pf_info->ucode_info[ucode].id = ucode; \		vf2pf_info->ucode_info[ucode].version = ver; \	} while (0) #define mmRCC_CONFIG_MEMSIZE    0xde3 const char *amdgpu_virt_dynamic_crit_table_name[] = {	"IP DISCOVERY",	"VBIOS IMG",	"RAS TELEMETRY",	"DATA EXCHANGE",	"BAD PAGE INFO",	"INIT HEADER",	"LAST",}; bool amdgpu_virt_mmio_blocked(struct amdgpu_device *adev){	/* By now all MMIO pages except mailbox are blocked */	/* if blocking is enabled in hypervisor. Choose the */	/* SCRATCH_REG0 to test. */	return RREG32_NO_KIQ(0xc040) == 0xffffffff;} void amdgpu_virt_init_setting(struct amdgpu_device *adev){	struct drm_device *ddev = adev_to_drm(adev); 	/* enable virtual display */	if (adev->asic_type != CHIP_ALDEBARAN &&	    adev->asic_type != CHIP_ARCTURUS &&	    ((adev->pdev->class >> 8) != PCI_CLASS_ACCELERATOR_PROCESSING)) {		if (adev->mode_info.num_crtc == 0)			adev->mode_info.num_crtc = 1;		adev->enable_virtual_display = true;	}	ddev->driver_features &= ~DRIVER_ATOMIC;	adev->cg_flags = 0;	adev->pg_flags = 0; 	/* Reduce kcq number to 2 to reduce latency */	if (amdgpu_num_kcq == -1)		amdgpu_num_kcq = 2;} /** * amdgpu_virt_request_full_gpu() - request full gpu access * @adev:	amdgpu device. * @init:	is driver init time. * When start to init/fini driver, first need to request full gpu access. * Return: Zero if request success, otherwise will return error. */int amdgpu_virt_request_full_gpu(struct amdgpu_device *adev, bool init){	struct amdgpu_virt *virt = &adev->virt;	int r; 	if (virt->ops && virt->ops->req_full_gpu) {		r = virt->ops->req_full_gpu(adev, init);		if (r) {			adev->no_hw_access = true;			return r;		} 		adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;	} 	return 0;} /** * amdgpu_virt_release_full_gpu() - release full gpu access * @adev:	amdgpu device. * @init:	is driver init time. * When finishing driver init/fini, need to release full gpu access. * Return: Zero if release success, otherwise will returen error. */int amdgpu_virt_release_full_gpu(struct amdgpu_device *adev, bool init){	struct amdgpu_virt *virt = &adev->virt;	int r; 	if (virt->ops && virt->ops->rel_full_gpu) {		r = virt->ops->rel_full_gpu(adev, init);		if (r)			return r; 		adev->virt.caps |= AMDGPU_SRIOV_CAPS_RUNTIME;	}	return 0;} /** * amdgpu_virt_reset_gpu() - reset gpu * @adev:	amdgpu device. * Send reset command to GPU hypervisor to reset GPU that VM is using * Return: Zero if reset success, otherwise will return error. */int amdgpu_virt_reset_gpu(struct amdgpu_device *adev){	struct amdgpu_virt *virt = &adev->virt;	int r; 	if (virt->ops && virt->ops->reset_gpu) {		r = virt->ops->reset_gpu(adev);		if (r)			return r; 		adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;	} 	return 0;} void amdgpu_virt_request_init_data(struct amdgpu_device *adev){	struct amdgpu_virt *virt = &adev->virt; 	if (virt->ops && virt->ops->req_init_data)		virt->ops->req_init_data(adev); 	if (adev->virt.req_init_data_ver > 0)		dev_info(adev->dev, "host supports REQ_INIT_DATA handshake of critical_region_version %d\n",				 adev->virt.req_init_data_ver);	else		dev_warn(adev->dev, "host doesn't support REQ_INIT_DATA handshake\n");} /** * amdgpu_virt_ready_to_reset() - send ready to reset to host * @adev:	amdgpu device. * Send ready to reset message to GPU hypervisor to signal we have stopped GPU * activity and is ready for host FLR */void amdgpu_virt_ready_to_reset(struct amdgpu_device *adev){	struct amdgpu_virt *virt = &adev->virt; 	if (virt->ops && virt->ops->reset_gpu)		virt->ops->ready_to_reset(adev);} /** * amdgpu_virt_wait_reset() - wait for reset gpu completed * @adev:	amdgpu device. * Wait for GPU reset completed. * Return: Zero if reset success, otherwise will return error. */int amdgpu_virt_wait_reset(struct amdgpu_device *adev){	struct amdgpu_virt *virt = &adev->virt; 	if (!virt->ops || !virt->ops->wait_reset)		return -EINVAL; 	return virt->ops->wait_reset(adev);} /** * amdgpu_virt_alloc_mm_table() - alloc memory for mm table * @adev:	amdgpu device. * MM table is used by UVD and VCE for its initialization * Return: Zero if allocate success. */int amdgpu_virt_alloc_mm_table(struct amdgpu_device *adev){	int r; 	if (!amdgpu_sriov_vf(adev) || adev->virt.mm_table.gpu_addr)		return 0; 	r = amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,				    AMDGPU_GEM_DOMAIN_VRAM |				    AMDGPU_GEM_DOMAIN_GTT,				    &adev->virt.mm_table.bo,				    &adev->virt.mm_table.gpu_addr,				    (void *)&adev->virt.mm_table.cpu_addr);	if (r) {		dev_err(adev->dev, "failed to alloc mm table and error = %d.\n", r);		return r;	} 	memset((void *)adev->virt.mm_table.cpu_addr, 0, PAGE_SIZE);	dev_info(adev->dev, "MM table gpu addr = 0x%llx, cpu addr = %p.\n",		 adev->virt.mm_table.gpu_addr,		 adev->virt.mm_table.cpu_addr);	return 0;} /** * amdgpu_virt_free_mm_table() - free mm table memory * @adev:	amdgpu device. * Free MM table memory */void amdgpu_virt_free_mm_table(struct amdgpu_device *adev){	if (!amdgpu_sriov_vf(adev) || !adev->virt.mm_table.gpu_addr)		return; 	amdgpu_bo_free_kernel(&adev->virt.mm_table.bo,			      &adev->virt.mm_table.gpu_addr,			      (void *)&adev->virt.mm_table.cpu_addr);	adev->virt.mm_table.gpu_addr = 0;} /** * amdgpu_virt_rcvd_ras_interrupt() - receive ras interrupt * @adev:	amdgpu device. * Check whether host sent RAS error message * Return: true if found, otherwise false */bool amdgpu_virt_rcvd_ras_interrupt(struct amdgpu_device *adev){	struct amdgpu_virt *virt = &adev->virt; 	if (!virt->ops || !virt->ops->rcvd_ras_intr)		return false; 	return virt->ops->rcvd_ras_intr(adev);}  unsigned int amd_sriov_msg_checksum(void *obj,				unsigned long obj_size,				unsigned int key,				unsigned int checksum){	unsigned int ret = key;	unsigned long i = 0;	unsigned char *pos; 	pos = (char *)obj;	/* calculate checksum */	for (i = 0; i < obj_size; ++i)		ret += *(pos + i);	/* minus the checksum itself */	pos = (char *)&checksum;	for (i = 0; i < sizeof(checksum); ++i)		ret -= *(pos + i);	return ret;} #define AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY	512/* Max bad page slots allowed for SRIOV*/#define AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY	10665U /** * amdgpu_virt_ras_realloc_eh_data_space - alloc/realloc VF bad-page @data->bps and @data->bps_bo * @adev: amdgpu device * @data: VF RAS error-handler data * @pages: minimum number of new slots to add beyond @data->capacity * * Return: 0 on success, %-ENOMEM on failure. */static int amdgpu_virt_ras_realloc_eh_data_space(struct amdgpu_device *adev,		struct amdgpu_virt_ras_err_handler_data *data,		int pages){	struct eeprom_table_record *new_bps;	struct amdgpu_bo **new_bo;	unsigned int old_space;	unsigned int new_space;	unsigned int align_space; 	old_space = (unsigned int)data->capacity;	new_space = old_space + max_t(unsigned int, (unsigned int)pages,				      (unsigned int)AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY);	if (new_space < old_space || new_space > AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY)		return -ENOMEM; 	align_space = ALIGN(new_space, AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY);	if (align_space > AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY)		return -ENOMEM; 	new_bps = kmalloc_array(align_space, sizeof(*data->bps), GFP_KERNEL);	new_bo = kcalloc(align_space, sizeof(*data->bps_bo), GFP_KERNEL);	if (!new_bps || !new_bo) {		kfree(new_bps);		kfree(new_bo);		dev_warn_ratelimited(adev->dev,				     "RAS WARN: failed to grow bad page table to %u slots\n",				     align_space);		return -ENOMEM;	} 	memcpy(new_bps, data->bps, data->count * sizeof(*data->bps));	memcpy(new_bo, data->bps_bo, data->count * sizeof(*data->bps_bo)); 	kfree(data->bps);	kfree(data->bps_bo);	data->bps = new_bps;	data->bps_bo = new_bo;	data->capacity = (int)align_space; 	return 0;} static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev){	struct amdgpu_virt *virt = &adev->virt;	struct amdgpu_virt_ras_err_handler_data **data = &virt->virt_eh_data;	unsigned int align_space = AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY;	void *bps = NULL;	struct amdgpu_bo **bps_bo = NULL; 	*data = kmalloc_obj(struct amdgpu_virt_ras_err_handler_data);	if (!*data)		goto data_failure; 	bps = kmalloc_objs(*(*data)->bps, align_space);	if (!bps)		goto bps_failure; 	bps_bo = kcalloc(align_space, sizeof(*(*data)->bps_bo), GFP_KERNEL);	if (!bps_bo)		goto bps_bo_failure; 	(*data)->bps = bps;	(*data)->bps_bo = bps_bo;	(*data)->capacity = align_space;	(*data)->count = 0;	(*data)->last_reserved = 0; 	virt->ras_init_done = true; 	return 0; bps_bo_failure:	kfree(bps);bps_failure:	kfree(*data);data_failure:	return -ENOMEM;} static void amdgpu_virt_ras_release_bp(struct amdgpu_device *adev){	struct amdgpu_virt *virt = &adev->virt;	struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;	struct amdgpu_bo *bo;	int i; 	if (!data)		return; 	for (i = data->last_reserved - 1; i >= 0; i--) {		bo = data->bps_bo[i];		if (bo) {			amdgpu_bo_free_kernel(&bo, NULL, NULL);			data->bps_bo[i] = bo;		}		data->last_reserved = i;	}} void amdgpu_virt_release_ras_err_handler_data(struct amdgpu_device *adev){	struct amdgpu_virt *virt = &adev->virt;	struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data; 	virt->ras_init_done = false; 	if (!data)		return; 	amdgpu_virt_ras_release_bp(adev); 	kfree(data->bps);	kfree(data->bps_bo);	kfree(data);	virt->virt_eh_data = NULL;} static bool amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,		const struct eeprom_table_record *bps, int pages){	struct amdgpu_virt *virt = &adev->virt;	struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;	int need; 	if (!data || pages <= 0)		return false; 	if (pages > AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY - data->count) {		dev_warn_ratelimited(adev->dev,				     "RAS WARN: bad page table at capacity (count=%d pages=%d max=%u)\n",				     data->count, pages,				     AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY);		return false;	} 	need = data->count + pages;	if (need > data->capacity &&	    amdgpu_virt_ras_realloc_eh_data_space(adev, data, need - data->capacity))		return false; 	memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));	data->count += pages; 	return true;} static void amdgpu_virt_ras_reserve_bps(struct amdgpu_device *adev){	struct amdgpu_virt *virt = &adev->virt;	struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;	struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr;	struct ttm_resource_manager *man = &mgr->manager;	struct amdgpu_bo *bo = NULL;	uint64_t bp;	int i; 	if (!data)		return; 	for (i = data->last_reserved; i < data->count; i++) {		bp = data->bps[i].retired_page; 		/* There are two cases of reserve error should be ignored:		 * 1) a ras bad page has been allocated (used by someone);		 * 2) a ras bad page has been reserved (duplicate error injection		 *    for one page);		 */		if  (ttm_resource_manager_used(man)) {			amdgpu_vram_mgr_reserve_range(&adev->mman.vram_mgr,				bp << AMDGPU_GPU_PAGE_SHIFT,				AMDGPU_GPU_PAGE_SIZE);			data->bps_bo[i] = NULL;		} else {			if (amdgpu_bo_create_kernel_at(adev, bp << AMDGPU_GPU_PAGE_SHIFT,							AMDGPU_GPU_PAGE_SIZE,							&bo, NULL))				dev_dbg(adev->dev,						"RAS WARN: reserve vram for retired page %llx fail\n",						bp);			data->bps_bo[i] = bo;		}		data->last_reserved = i + 1;		bo = NULL;	}} static bool amdgpu_virt_ras_check_bad_page(struct amdgpu_device *adev,		uint64_t retired_page){	struct amdgpu_virt *virt = &adev->virt;	struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;	int i; 	if (!data)		return true; 	for (i = 0; i < data->count; i++)		if (retired_page == data->bps[i].retired_page)			return true; 	return false;

Lines 1–500 of 2091.