Home | History | Annotate | Line # | Download | only in amdgpu
      1  1.3  riastrad /*	$NetBSD: amdgpu_psp.c,v 1.3 2021/12/19 12:21:29 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2016 Advanced Micro Devices, Inc.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     14  1.1  riastrad  * all copies or substantial portions of the Software.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     23  1.1  riastrad  *
     24  1.1  riastrad  * Author: Huang Rui
     25  1.1  riastrad  *
     26  1.1  riastrad  */
     27  1.1  riastrad 
     28  1.1  riastrad #include <sys/cdefs.h>
     29  1.3  riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpu_psp.c,v 1.3 2021/12/19 12:21:29 riastradh Exp $");
     30  1.1  riastrad 
     31  1.1  riastrad #include <linux/firmware.h>
     32  1.1  riastrad 
     33  1.1  riastrad #include "amdgpu.h"
     34  1.1  riastrad #include "amdgpu_psp.h"
     35  1.1  riastrad #include "amdgpu_ucode.h"
     36  1.1  riastrad #include "soc15_common.h"
     37  1.1  riastrad #include "psp_v3_1.h"
     38  1.1  riastrad #include "psp_v10_0.h"
     39  1.1  riastrad #include "psp_v11_0.h"
     40  1.1  riastrad #include "psp_v12_0.h"
     41  1.1  riastrad 
     42  1.1  riastrad #include "amdgpu_ras.h"
     43  1.1  riastrad 
     44  1.1  riastrad static void psp_set_funcs(struct amdgpu_device *adev);
     45  1.1  riastrad 
     46  1.1  riastrad static int psp_early_init(void *handle)
     47  1.1  riastrad {
     48  1.1  riastrad 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
     49  1.1  riastrad 	struct psp_context *psp = &adev->psp;
     50  1.1  riastrad 
     51  1.1  riastrad 	psp_set_funcs(adev);
     52  1.1  riastrad 
     53  1.1  riastrad 	switch (adev->asic_type) {
     54  1.1  riastrad 	case CHIP_VEGA10:
     55  1.1  riastrad 	case CHIP_VEGA12:
     56  1.1  riastrad 		psp_v3_1_set_psp_funcs(psp);
     57  1.1  riastrad 		psp->autoload_supported = false;
     58  1.1  riastrad 		break;
     59  1.1  riastrad 	case CHIP_RAVEN:
     60  1.1  riastrad 		psp_v10_0_set_psp_funcs(psp);
     61  1.1  riastrad 		psp->autoload_supported = false;
     62  1.1  riastrad 		break;
     63  1.1  riastrad 	case CHIP_VEGA20:
     64  1.1  riastrad 	case CHIP_ARCTURUS:
     65  1.1  riastrad 		psp_v11_0_set_psp_funcs(psp);
     66  1.1  riastrad 		psp->autoload_supported = false;
     67  1.1  riastrad 		break;
     68  1.1  riastrad 	case CHIP_NAVI10:
     69  1.1  riastrad 	case CHIP_NAVI14:
     70  1.1  riastrad 	case CHIP_NAVI12:
     71  1.1  riastrad 		psp_v11_0_set_psp_funcs(psp);
     72  1.1  riastrad 		psp->autoload_supported = true;
     73  1.1  riastrad 		break;
     74  1.1  riastrad 	case CHIP_RENOIR:
     75  1.1  riastrad 		psp_v12_0_set_psp_funcs(psp);
     76  1.1  riastrad 		break;
     77  1.1  riastrad 	default:
     78  1.1  riastrad 		return -EINVAL;
     79  1.1  riastrad 	}
     80  1.1  riastrad 
     81  1.1  riastrad 	psp->adev = adev;
     82  1.1  riastrad 
     83  1.1  riastrad 	return 0;
     84  1.1  riastrad }
     85  1.1  riastrad 
     86  1.1  riastrad static int psp_sw_init(void *handle)
     87  1.1  riastrad {
     88  1.1  riastrad 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
     89  1.1  riastrad 	struct psp_context *psp = &adev->psp;
     90  1.1  riastrad 	int ret;
     91  1.1  riastrad 
     92  1.1  riastrad 	ret = psp_init_microcode(psp);
     93  1.1  riastrad 	if (ret) {
     94  1.1  riastrad 		DRM_ERROR("Failed to load psp firmware!\n");
     95  1.1  riastrad 		return ret;
     96  1.1  riastrad 	}
     97  1.1  riastrad 
     98  1.1  riastrad 	ret = psp_mem_training_init(psp);
     99  1.1  riastrad 	if (ret) {
    100  1.1  riastrad 		DRM_ERROR("Failed to initialize memory training!\n");
    101  1.1  riastrad 		return ret;
    102  1.1  riastrad 	}
    103  1.1  riastrad 	ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
    104  1.1  riastrad 	if (ret) {
    105  1.1  riastrad 		DRM_ERROR("Failed to process memory training!\n");
    106  1.1  riastrad 		return ret;
    107  1.1  riastrad 	}
    108  1.1  riastrad 
    109  1.1  riastrad 	return 0;
    110  1.1  riastrad }
    111  1.1  riastrad 
    112  1.1  riastrad static int psp_sw_fini(void *handle)
    113  1.1  riastrad {
    114  1.1  riastrad 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
    115  1.1  riastrad 
    116  1.1  riastrad 	psp_mem_training_fini(&adev->psp);
    117  1.1  riastrad 	release_firmware(adev->psp.sos_fw);
    118  1.1  riastrad 	adev->psp.sos_fw = NULL;
    119  1.1  riastrad 	release_firmware(adev->psp.asd_fw);
    120  1.1  riastrad 	adev->psp.asd_fw = NULL;
    121  1.1  riastrad 	if (adev->psp.ta_fw) {
    122  1.1  riastrad 		release_firmware(adev->psp.ta_fw);
    123  1.1  riastrad 		adev->psp.ta_fw = NULL;
    124  1.1  riastrad 	}
    125  1.1  riastrad 	return 0;
    126  1.1  riastrad }
    127  1.1  riastrad 
    128  1.1  riastrad int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
    129  1.1  riastrad 		 uint32_t reg_val, uint32_t mask, bool check_changed)
    130  1.1  riastrad {
    131  1.1  riastrad 	uint32_t val;
    132  1.1  riastrad 	int i;
    133  1.1  riastrad 	struct amdgpu_device *adev = psp->adev;
    134  1.1  riastrad 
    135  1.1  riastrad 	for (i = 0; i < adev->usec_timeout; i++) {
    136  1.1  riastrad 		val = RREG32(reg_index);
    137  1.1  riastrad 		if (check_changed) {
    138  1.1  riastrad 			if (val != reg_val)
    139  1.1  riastrad 				return 0;
    140  1.1  riastrad 		} else {
    141  1.1  riastrad 			if ((val & mask) == reg_val)
    142  1.1  riastrad 				return 0;
    143  1.1  riastrad 		}
    144  1.1  riastrad 		udelay(1);
    145  1.1  riastrad 	}
    146  1.1  riastrad 
    147  1.1  riastrad 	return -ETIME;
    148  1.1  riastrad }
    149  1.1  riastrad 
    150  1.1  riastrad static int
    151  1.1  riastrad psp_cmd_submit_buf(struct psp_context *psp,
    152  1.1  riastrad 		   struct amdgpu_firmware_info *ucode,
    153  1.1  riastrad 		   struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
    154  1.1  riastrad {
    155  1.1  riastrad 	int ret;
    156  1.1  riastrad 	int index;
    157  1.1  riastrad 	int timeout = 2000;
    158  1.1  riastrad 
    159  1.1  riastrad 	mutex_lock(&psp->mutex);
    160  1.1  riastrad 
    161  1.1  riastrad 	memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
    162  1.1  riastrad 
    163  1.1  riastrad 	memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
    164  1.1  riastrad 
    165  1.1  riastrad 	index = atomic_inc_return(&psp->fence_value);
    166  1.1  riastrad 	ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
    167  1.1  riastrad 	if (ret) {
    168  1.1  riastrad 		atomic_dec(&psp->fence_value);
    169  1.1  riastrad 		mutex_unlock(&psp->mutex);
    170  1.1  riastrad 		return ret;
    171  1.1  riastrad 	}
    172  1.1  riastrad 
    173  1.1  riastrad 	amdgpu_asic_invalidate_hdp(psp->adev, NULL);
    174  1.1  riastrad 	while (*((unsigned int *)psp->fence_buf) != index) {
    175  1.1  riastrad 		if (--timeout == 0)
    176  1.1  riastrad 			break;
    177  1.1  riastrad 		/*
    178  1.1  riastrad 		 * Shouldn't wait for timeout when err_event_athub occurs,
    179  1.1  riastrad 		 * because gpu reset thread triggered and lock resource should
    180  1.1  riastrad 		 * be released for psp resume sequence.
    181  1.1  riastrad 		 */
    182  1.1  riastrad 		if (amdgpu_ras_intr_triggered())
    183  1.1  riastrad 			break;
    184  1.1  riastrad 		msleep(1);
    185  1.1  riastrad 		amdgpu_asic_invalidate_hdp(psp->adev, NULL);
    186  1.1  riastrad 	}
    187  1.1  riastrad 
    188  1.1  riastrad 	/* In some cases, psp response status is not 0 even there is no
    189  1.1  riastrad 	 * problem while the command is submitted. Some version of PSP FW
    190  1.1  riastrad 	 * doesn't write 0 to that field.
    191  1.1  riastrad 	 * So here we would like to only print a warning instead of an error
    192  1.1  riastrad 	 * during psp initialization to avoid breaking hw_init and it doesn't
    193  1.1  riastrad 	 * return -EINVAL.
    194  1.1  riastrad 	 */
    195  1.1  riastrad 	if (psp->cmd_buf_mem->resp.status || !timeout) {
    196  1.1  riastrad 		if (ucode)
    197  1.1  riastrad 			DRM_WARN("failed to load ucode id (%d) ",
    198  1.1  riastrad 				  ucode->ucode_id);
    199  1.1  riastrad 		DRM_WARN("psp command (0x%X) failed and response status is (0x%X)\n",
    200  1.1  riastrad 			 psp->cmd_buf_mem->cmd_id,
    201  1.1  riastrad 			 psp->cmd_buf_mem->resp.status);
    202  1.1  riastrad 		if (!timeout) {
    203  1.1  riastrad 			mutex_unlock(&psp->mutex);
    204  1.1  riastrad 			return -EINVAL;
    205  1.1  riastrad 		}
    206  1.1  riastrad 	}
    207  1.1  riastrad 
    208  1.1  riastrad 	/* get xGMI session id from response buffer */
    209  1.1  riastrad 	cmd->resp.session_id = psp->cmd_buf_mem->resp.session_id;
    210  1.1  riastrad 
    211  1.1  riastrad 	if (ucode) {
    212  1.1  riastrad 		ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
    213  1.1  riastrad 		ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
    214  1.1  riastrad 	}
    215  1.1  riastrad 	mutex_unlock(&psp->mutex);
    216  1.1  riastrad 
    217  1.1  riastrad 	return ret;
    218  1.1  riastrad }
    219  1.1  riastrad 
    220  1.1  riastrad static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
    221  1.1  riastrad 				 struct psp_gfx_cmd_resp *cmd,
    222  1.1  riastrad 				 uint64_t tmr_mc, uint32_t size)
    223  1.1  riastrad {
    224  1.1  riastrad 	if (psp_support_vmr_ring(psp))
    225  1.1  riastrad 		cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
    226  1.1  riastrad 	else
    227  1.1  riastrad 		cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
    228  1.1  riastrad 	cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
    229  1.1  riastrad 	cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
    230  1.1  riastrad 	cmd->cmd.cmd_setup_tmr.buf_size = size;
    231  1.1  riastrad }
    232  1.1  riastrad 
    233  1.1  riastrad static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
    234  1.1  riastrad 				      uint64_t pri_buf_mc, uint32_t size)
    235  1.1  riastrad {
    236  1.1  riastrad 	cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
    237  1.1  riastrad 	cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
    238  1.1  riastrad 	cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
    239  1.1  riastrad 	cmd->cmd.cmd_load_toc.toc_size = size;
    240  1.1  riastrad }
    241  1.1  riastrad 
    242  1.1  riastrad /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
    243  1.1  riastrad static int psp_load_toc(struct psp_context *psp,
    244  1.1  riastrad 			uint32_t *tmr_size)
    245  1.1  riastrad {
    246  1.1  riastrad 	int ret;
    247  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd;
    248  1.1  riastrad 
    249  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
    250  1.1  riastrad 	if (!cmd)
    251  1.1  riastrad 		return -ENOMEM;
    252  1.1  riastrad 	/* Copy toc to psp firmware private buffer */
    253  1.1  riastrad 	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
    254  1.1  riastrad 	memcpy(psp->fw_pri_buf, psp->toc_start_addr, psp->toc_bin_size);
    255  1.1  riastrad 
    256  1.1  riastrad 	psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc_bin_size);
    257  1.1  riastrad 
    258  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd,
    259  1.1  riastrad 				 psp->fence_buf_mc_addr);
    260  1.1  riastrad 	if (!ret)
    261  1.1  riastrad 		*tmr_size = psp->cmd_buf_mem->resp.tmr_size;
    262  1.1  riastrad 	kfree(cmd);
    263  1.1  riastrad 	return ret;
    264  1.1  riastrad }
    265  1.1  riastrad 
    266  1.1  riastrad /* Set up Trusted Memory Region */
    267  1.1  riastrad static int psp_tmr_init(struct psp_context *psp)
    268  1.1  riastrad {
    269  1.1  riastrad 	int ret;
    270  1.1  riastrad 	int tmr_size;
    271  1.1  riastrad 	void *tmr_buf;
    272  1.1  riastrad 	void **pptr;
    273  1.1  riastrad 
    274  1.1  riastrad 	/*
    275  1.1  riastrad 	 * According to HW engineer, they prefer the TMR address be "naturally
    276  1.1  riastrad 	 * aligned" , e.g. the start address be an integer divide of TMR size.
    277  1.1  riastrad 	 *
    278  1.1  riastrad 	 * Note: this memory need be reserved till the driver
    279  1.1  riastrad 	 * uninitializes.
    280  1.1  riastrad 	 */
    281  1.1  riastrad 	tmr_size = PSP_TMR_SIZE;
    282  1.1  riastrad 
    283  1.1  riastrad 	/* For ASICs support RLC autoload, psp will parse the toc
    284  1.1  riastrad 	 * and calculate the total size of TMR needed */
    285  1.1  riastrad 	if (!amdgpu_sriov_vf(psp->adev) &&
    286  1.1  riastrad 	    psp->toc_start_addr &&
    287  1.1  riastrad 	    psp->toc_bin_size &&
    288  1.1  riastrad 	    psp->fw_pri_buf) {
    289  1.1  riastrad 		ret = psp_load_toc(psp, &tmr_size);
    290  1.1  riastrad 		if (ret) {
    291  1.1  riastrad 			DRM_ERROR("Failed to load toc\n");
    292  1.1  riastrad 			return ret;
    293  1.1  riastrad 		}
    294  1.1  riastrad 	}
    295  1.1  riastrad 
    296  1.1  riastrad 	pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
    297  1.1  riastrad 	ret = amdgpu_bo_create_kernel(psp->adev, tmr_size, PSP_TMR_SIZE,
    298  1.1  riastrad 				      AMDGPU_GEM_DOMAIN_VRAM,
    299  1.1  riastrad 				      &psp->tmr_bo, &psp->tmr_mc_addr, pptr);
    300  1.1  riastrad 
    301  1.1  riastrad 	return ret;
    302  1.1  riastrad }
    303  1.1  riastrad 
    304  1.1  riastrad static int psp_tmr_load(struct psp_context *psp)
    305  1.1  riastrad {
    306  1.1  riastrad 	int ret;
    307  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd;
    308  1.1  riastrad 
    309  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
    310  1.1  riastrad 	if (!cmd)
    311  1.1  riastrad 		return -ENOMEM;
    312  1.1  riastrad 
    313  1.1  riastrad 	psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr,
    314  1.1  riastrad 			     amdgpu_bo_size(psp->tmr_bo));
    315  1.3  riastrad 	DRM_INFO("reserve 0x%lx from 0x%"PRIx64" for PSP TMR\n",
    316  1.1  riastrad 		 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
    317  1.1  riastrad 
    318  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd,
    319  1.1  riastrad 				 psp->fence_buf_mc_addr);
    320  1.1  riastrad 
    321  1.1  riastrad 	kfree(cmd);
    322  1.1  riastrad 
    323  1.1  riastrad 	return ret;
    324  1.1  riastrad }
    325  1.1  riastrad 
    326  1.1  riastrad static void psp_prep_asd_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
    327  1.1  riastrad 				uint64_t asd_mc, uint32_t size)
    328  1.1  riastrad {
    329  1.1  riastrad 	cmd->cmd_id = GFX_CMD_ID_LOAD_ASD;
    330  1.1  riastrad 	cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(asd_mc);
    331  1.1  riastrad 	cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(asd_mc);
    332  1.1  riastrad 	cmd->cmd.cmd_load_ta.app_len = size;
    333  1.1  riastrad 
    334  1.1  riastrad 	cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = 0;
    335  1.1  riastrad 	cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = 0;
    336  1.1  riastrad 	cmd->cmd.cmd_load_ta.cmd_buf_len = 0;
    337  1.1  riastrad }
    338  1.1  riastrad 
    339  1.1  riastrad static int psp_asd_load(struct psp_context *psp)
    340  1.1  riastrad {
    341  1.1  riastrad 	int ret;
    342  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd;
    343  1.1  riastrad 
    344  1.1  riastrad 	/* If PSP version doesn't match ASD version, asd loading will be failed.
    345  1.1  riastrad 	 * add workaround to bypass it for sriov now.
    346  1.1  riastrad 	 * TODO: add version check to make it common
    347  1.1  riastrad 	 */
    348  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
    349  1.1  riastrad 		return 0;
    350  1.1  riastrad 
    351  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
    352  1.1  riastrad 	if (!cmd)
    353  1.1  riastrad 		return -ENOMEM;
    354  1.1  riastrad 
    355  1.1  riastrad 	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
    356  1.1  riastrad 	memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
    357  1.1  riastrad 
    358  1.1  riastrad 	psp_prep_asd_load_cmd_buf(cmd, psp->fw_pri_mc_addr,
    359  1.1  riastrad 				  psp->asd_ucode_size);
    360  1.1  riastrad 
    361  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd,
    362  1.1  riastrad 				 psp->fence_buf_mc_addr);
    363  1.1  riastrad 	if (!ret) {
    364  1.1  riastrad 		psp->asd_context.asd_initialized = true;
    365  1.1  riastrad 		psp->asd_context.session_id = cmd->resp.session_id;
    366  1.1  riastrad 	}
    367  1.1  riastrad 
    368  1.1  riastrad 	kfree(cmd);
    369  1.1  riastrad 
    370  1.1  riastrad 	return ret;
    371  1.1  riastrad }
    372  1.1  riastrad 
    373  1.1  riastrad static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
    374  1.1  riastrad 				       uint32_t session_id)
    375  1.1  riastrad {
    376  1.1  riastrad 	cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
    377  1.1  riastrad 	cmd->cmd.cmd_unload_ta.session_id = session_id;
    378  1.1  riastrad }
    379  1.1  riastrad 
    380  1.1  riastrad static int psp_asd_unload(struct psp_context *psp)
    381  1.1  riastrad {
    382  1.1  riastrad 	int ret;
    383  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd;
    384  1.1  riastrad 
    385  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
    386  1.1  riastrad 		return 0;
    387  1.1  riastrad 
    388  1.1  riastrad 	if (!psp->asd_context.asd_initialized)
    389  1.1  riastrad 		return 0;
    390  1.1  riastrad 
    391  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
    392  1.1  riastrad 	if (!cmd)
    393  1.1  riastrad 		return -ENOMEM;
    394  1.1  riastrad 
    395  1.1  riastrad 	psp_prep_ta_unload_cmd_buf(cmd, psp->asd_context.session_id);
    396  1.1  riastrad 
    397  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd,
    398  1.1  riastrad 				 psp->fence_buf_mc_addr);
    399  1.1  riastrad 	if (!ret)
    400  1.1  riastrad 		psp->asd_context.asd_initialized = false;
    401  1.1  riastrad 
    402  1.1  riastrad 	kfree(cmd);
    403  1.1  riastrad 
    404  1.1  riastrad 	return ret;
    405  1.1  riastrad }
    406  1.1  riastrad 
    407  1.1  riastrad static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
    408  1.1  riastrad 		uint32_t id, uint32_t value)
    409  1.1  riastrad {
    410  1.1  riastrad 	cmd->cmd_id = GFX_CMD_ID_PROG_REG;
    411  1.1  riastrad 	cmd->cmd.cmd_setup_reg_prog.reg_value = value;
    412  1.1  riastrad 	cmd->cmd.cmd_setup_reg_prog.reg_id = id;
    413  1.1  riastrad }
    414  1.1  riastrad 
    415  1.1  riastrad int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
    416  1.1  riastrad 		uint32_t value)
    417  1.1  riastrad {
    418  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd = NULL;
    419  1.1  riastrad 	int ret = 0;
    420  1.1  riastrad 
    421  1.1  riastrad 	if (reg >= PSP_REG_LAST)
    422  1.1  riastrad 		return -EINVAL;
    423  1.1  riastrad 
    424  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
    425  1.1  riastrad 	if (!cmd)
    426  1.1  riastrad 		return -ENOMEM;
    427  1.1  riastrad 
    428  1.1  riastrad 	psp_prep_reg_prog_cmd_buf(cmd, reg, value);
    429  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
    430  1.1  riastrad 
    431  1.1  riastrad 	kfree(cmd);
    432  1.1  riastrad 	return ret;
    433  1.1  riastrad }
    434  1.1  riastrad 
    435  1.1  riastrad static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
    436  1.1  riastrad 				     uint64_t ta_bin_mc,
    437  1.1  riastrad 				     uint32_t ta_bin_size,
    438  1.1  riastrad 				     uint64_t ta_shared_mc,
    439  1.1  riastrad 				     uint32_t ta_shared_size)
    440  1.1  riastrad {
    441  1.1  riastrad 	cmd->cmd_id 				= GFX_CMD_ID_LOAD_TA;
    442  1.1  riastrad 	cmd->cmd.cmd_load_ta.app_phy_addr_lo 	= lower_32_bits(ta_bin_mc);
    443  1.1  riastrad 	cmd->cmd.cmd_load_ta.app_phy_addr_hi 	= upper_32_bits(ta_bin_mc);
    444  1.1  riastrad 	cmd->cmd.cmd_load_ta.app_len 		= ta_bin_size;
    445  1.1  riastrad 
    446  1.1  riastrad 	cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = lower_32_bits(ta_shared_mc);
    447  1.1  riastrad 	cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = upper_32_bits(ta_shared_mc);
    448  1.1  riastrad 	cmd->cmd.cmd_load_ta.cmd_buf_len 	 = ta_shared_size;
    449  1.1  riastrad }
    450  1.1  riastrad 
    451  1.1  riastrad static int psp_xgmi_init_shared_buf(struct psp_context *psp)
    452  1.1  riastrad {
    453  1.1  riastrad 	int ret;
    454  1.1  riastrad 
    455  1.1  riastrad 	/*
    456  1.1  riastrad 	 * Allocate 16k memory aligned to 4k from Frame Buffer (local
    457  1.1  riastrad 	 * physical) for xgmi ta <-> Driver
    458  1.1  riastrad 	 */
    459  1.1  riastrad 	ret = amdgpu_bo_create_kernel(psp->adev, PSP_XGMI_SHARED_MEM_SIZE,
    460  1.1  riastrad 				      PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
    461  1.1  riastrad 				      &psp->xgmi_context.xgmi_shared_bo,
    462  1.1  riastrad 				      &psp->xgmi_context.xgmi_shared_mc_addr,
    463  1.1  riastrad 				      &psp->xgmi_context.xgmi_shared_buf);
    464  1.1  riastrad 
    465  1.1  riastrad 	return ret;
    466  1.1  riastrad }
    467  1.1  riastrad 
    468  1.1  riastrad static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
    469  1.1  riastrad 				       uint32_t ta_cmd_id,
    470  1.1  riastrad 				       uint32_t session_id)
    471  1.1  riastrad {
    472  1.1  riastrad 	cmd->cmd_id 				= GFX_CMD_ID_INVOKE_CMD;
    473  1.1  riastrad 	cmd->cmd.cmd_invoke_cmd.session_id 	= session_id;
    474  1.1  riastrad 	cmd->cmd.cmd_invoke_cmd.ta_cmd_id 	= ta_cmd_id;
    475  1.1  riastrad }
    476  1.1  riastrad 
    477  1.1  riastrad int psp_ta_invoke(struct psp_context *psp,
    478  1.1  riastrad 		  uint32_t ta_cmd_id,
    479  1.1  riastrad 		  uint32_t session_id)
    480  1.1  riastrad {
    481  1.1  riastrad 	int ret;
    482  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd;
    483  1.1  riastrad 
    484  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
    485  1.1  riastrad 	if (!cmd)
    486  1.1  riastrad 		return -ENOMEM;
    487  1.1  riastrad 
    488  1.1  riastrad 	psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, session_id);
    489  1.1  riastrad 
    490  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd,
    491  1.1  riastrad 				 psp->fence_buf_mc_addr);
    492  1.1  riastrad 
    493  1.1  riastrad 	kfree(cmd);
    494  1.1  riastrad 
    495  1.1  riastrad 	return ret;
    496  1.1  riastrad }
    497  1.1  riastrad 
    498  1.1  riastrad static int psp_xgmi_load(struct psp_context *psp)
    499  1.1  riastrad {
    500  1.1  riastrad 	int ret;
    501  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd;
    502  1.1  riastrad 
    503  1.1  riastrad 	/*
    504  1.1  riastrad 	 * TODO: bypass the loading in sriov for now
    505  1.1  riastrad 	 */
    506  1.1  riastrad 
    507  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
    508  1.1  riastrad 	if (!cmd)
    509  1.1  riastrad 		return -ENOMEM;
    510  1.1  riastrad 
    511  1.1  riastrad 	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
    512  1.1  riastrad 	memcpy(psp->fw_pri_buf, psp->ta_xgmi_start_addr, psp->ta_xgmi_ucode_size);
    513  1.1  riastrad 
    514  1.1  riastrad 	psp_prep_ta_load_cmd_buf(cmd,
    515  1.1  riastrad 				 psp->fw_pri_mc_addr,
    516  1.1  riastrad 				 psp->ta_xgmi_ucode_size,
    517  1.1  riastrad 				 psp->xgmi_context.xgmi_shared_mc_addr,
    518  1.1  riastrad 				 PSP_XGMI_SHARED_MEM_SIZE);
    519  1.1  riastrad 
    520  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd,
    521  1.1  riastrad 				 psp->fence_buf_mc_addr);
    522  1.1  riastrad 
    523  1.1  riastrad 	if (!ret) {
    524  1.1  riastrad 		psp->xgmi_context.initialized = 1;
    525  1.1  riastrad 		psp->xgmi_context.session_id = cmd->resp.session_id;
    526  1.1  riastrad 	}
    527  1.1  riastrad 
    528  1.1  riastrad 	kfree(cmd);
    529  1.1  riastrad 
    530  1.1  riastrad 	return ret;
    531  1.1  riastrad }
    532  1.1  riastrad 
    533  1.1  riastrad static int psp_xgmi_unload(struct psp_context *psp)
    534  1.1  riastrad {
    535  1.1  riastrad 	int ret;
    536  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd;
    537  1.1  riastrad 	struct amdgpu_device *adev = psp->adev;
    538  1.1  riastrad 
    539  1.1  riastrad 	/* XGMI TA unload currently is not supported on Arcturus */
    540  1.1  riastrad 	if (adev->asic_type == CHIP_ARCTURUS)
    541  1.1  riastrad 		return 0;
    542  1.1  riastrad 
    543  1.1  riastrad 	/*
    544  1.1  riastrad 	 * TODO: bypass the unloading in sriov for now
    545  1.1  riastrad 	 */
    546  1.1  riastrad 
    547  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
    548  1.1  riastrad 	if (!cmd)
    549  1.1  riastrad 		return -ENOMEM;
    550  1.1  riastrad 
    551  1.1  riastrad 	psp_prep_ta_unload_cmd_buf(cmd, psp->xgmi_context.session_id);
    552  1.1  riastrad 
    553  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd,
    554  1.1  riastrad 				 psp->fence_buf_mc_addr);
    555  1.1  riastrad 
    556  1.1  riastrad 	kfree(cmd);
    557  1.1  riastrad 
    558  1.1  riastrad 	return ret;
    559  1.1  riastrad }
    560  1.1  riastrad 
    561  1.1  riastrad int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
    562  1.1  riastrad {
    563  1.1  riastrad 	return psp_ta_invoke(psp, ta_cmd_id, psp->xgmi_context.session_id);
    564  1.1  riastrad }
    565  1.1  riastrad 
    566  1.1  riastrad static int psp_xgmi_terminate(struct psp_context *psp)
    567  1.1  riastrad {
    568  1.1  riastrad 	int ret;
    569  1.1  riastrad 
    570  1.1  riastrad 	if (!psp->xgmi_context.initialized)
    571  1.1  riastrad 		return 0;
    572  1.1  riastrad 
    573  1.1  riastrad 	ret = psp_xgmi_unload(psp);
    574  1.1  riastrad 	if (ret)
    575  1.1  riastrad 		return ret;
    576  1.1  riastrad 
    577  1.1  riastrad 	psp->xgmi_context.initialized = 0;
    578  1.1  riastrad 
    579  1.1  riastrad 	/* free xgmi shared memory */
    580  1.1  riastrad 	amdgpu_bo_free_kernel(&psp->xgmi_context.xgmi_shared_bo,
    581  1.1  riastrad 			&psp->xgmi_context.xgmi_shared_mc_addr,
    582  1.1  riastrad 			&psp->xgmi_context.xgmi_shared_buf);
    583  1.1  riastrad 
    584  1.1  riastrad 	return 0;
    585  1.1  riastrad }
    586  1.1  riastrad 
    587  1.1  riastrad static int psp_xgmi_initialize(struct psp_context *psp)
    588  1.1  riastrad {
    589  1.1  riastrad 	struct ta_xgmi_shared_memory *xgmi_cmd;
    590  1.1  riastrad 	int ret;
    591  1.1  riastrad 
    592  1.1  riastrad 	if (!psp->adev->psp.ta_fw ||
    593  1.1  riastrad 	    !psp->adev->psp.ta_xgmi_ucode_size ||
    594  1.1  riastrad 	    !psp->adev->psp.ta_xgmi_start_addr)
    595  1.1  riastrad 		return -ENOENT;
    596  1.1  riastrad 
    597  1.1  riastrad 	if (!psp->xgmi_context.initialized) {
    598  1.1  riastrad 		ret = psp_xgmi_init_shared_buf(psp);
    599  1.1  riastrad 		if (ret)
    600  1.1  riastrad 			return ret;
    601  1.1  riastrad 	}
    602  1.1  riastrad 
    603  1.1  riastrad 	/* Load XGMI TA */
    604  1.1  riastrad 	ret = psp_xgmi_load(psp);
    605  1.1  riastrad 	if (ret)
    606  1.1  riastrad 		return ret;
    607  1.1  riastrad 
    608  1.1  riastrad 	/* Initialize XGMI session */
    609  1.1  riastrad 	xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.xgmi_shared_buf);
    610  1.1  riastrad 	memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
    611  1.1  riastrad 	xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
    612  1.1  riastrad 
    613  1.1  riastrad 	ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
    614  1.1  riastrad 
    615  1.1  riastrad 	return ret;
    616  1.1  riastrad }
    617  1.1  riastrad 
    618  1.1  riastrad // ras begin
    619  1.1  riastrad static int psp_ras_init_shared_buf(struct psp_context *psp)
    620  1.1  riastrad {
    621  1.1  riastrad 	int ret;
    622  1.1  riastrad 
    623  1.1  riastrad 	/*
    624  1.1  riastrad 	 * Allocate 16k memory aligned to 4k from Frame Buffer (local
    625  1.1  riastrad 	 * physical) for ras ta <-> Driver
    626  1.1  riastrad 	 */
    627  1.1  riastrad 	ret = amdgpu_bo_create_kernel(psp->adev, PSP_RAS_SHARED_MEM_SIZE,
    628  1.1  riastrad 			PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
    629  1.1  riastrad 			&psp->ras.ras_shared_bo,
    630  1.1  riastrad 			&psp->ras.ras_shared_mc_addr,
    631  1.1  riastrad 			&psp->ras.ras_shared_buf);
    632  1.1  riastrad 
    633  1.1  riastrad 	return ret;
    634  1.1  riastrad }
    635  1.1  riastrad 
    636  1.1  riastrad static int psp_ras_load(struct psp_context *psp)
    637  1.1  riastrad {
    638  1.1  riastrad 	int ret;
    639  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd;
    640  1.1  riastrad 
    641  1.1  riastrad 	/*
    642  1.1  riastrad 	 * TODO: bypass the loading in sriov for now
    643  1.1  riastrad 	 */
    644  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
    645  1.1  riastrad 		return 0;
    646  1.1  riastrad 
    647  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
    648  1.1  riastrad 	if (!cmd)
    649  1.1  riastrad 		return -ENOMEM;
    650  1.1  riastrad 
    651  1.1  riastrad 	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
    652  1.1  riastrad 	memcpy(psp->fw_pri_buf, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
    653  1.1  riastrad 
    654  1.1  riastrad 	psp_prep_ta_load_cmd_buf(cmd,
    655  1.1  riastrad 				 psp->fw_pri_mc_addr,
    656  1.1  riastrad 				 psp->ta_ras_ucode_size,
    657  1.1  riastrad 				 psp->ras.ras_shared_mc_addr,
    658  1.1  riastrad 				 PSP_RAS_SHARED_MEM_SIZE);
    659  1.1  riastrad 
    660  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd,
    661  1.1  riastrad 			psp->fence_buf_mc_addr);
    662  1.1  riastrad 
    663  1.1  riastrad 	if (!ret) {
    664  1.1  riastrad 		psp->ras.ras_initialized = true;
    665  1.1  riastrad 		psp->ras.session_id = cmd->resp.session_id;
    666  1.1  riastrad 	}
    667  1.1  riastrad 
    668  1.1  riastrad 	kfree(cmd);
    669  1.1  riastrad 
    670  1.1  riastrad 	return ret;
    671  1.1  riastrad }
    672  1.1  riastrad 
    673  1.1  riastrad static int psp_ras_unload(struct psp_context *psp)
    674  1.1  riastrad {
    675  1.1  riastrad 	int ret;
    676  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd;
    677  1.1  riastrad 
    678  1.1  riastrad 	/*
    679  1.1  riastrad 	 * TODO: bypass the unloading in sriov for now
    680  1.1  riastrad 	 */
    681  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
    682  1.1  riastrad 		return 0;
    683  1.1  riastrad 
    684  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
    685  1.1  riastrad 	if (!cmd)
    686  1.1  riastrad 		return -ENOMEM;
    687  1.1  riastrad 
    688  1.1  riastrad 	psp_prep_ta_unload_cmd_buf(cmd, psp->ras.session_id);
    689  1.1  riastrad 
    690  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd,
    691  1.1  riastrad 			psp->fence_buf_mc_addr);
    692  1.1  riastrad 
    693  1.1  riastrad 	kfree(cmd);
    694  1.1  riastrad 
    695  1.1  riastrad 	return ret;
    696  1.1  riastrad }
    697  1.1  riastrad 
    698  1.1  riastrad int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
    699  1.1  riastrad {
    700  1.1  riastrad 	/*
    701  1.1  riastrad 	 * TODO: bypass the loading in sriov for now
    702  1.1  riastrad 	 */
    703  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
    704  1.1  riastrad 		return 0;
    705  1.1  riastrad 
    706  1.1  riastrad 	return psp_ta_invoke(psp, ta_cmd_id, psp->ras.session_id);
    707  1.1  riastrad }
    708  1.1  riastrad 
    709  1.1  riastrad int psp_ras_enable_features(struct psp_context *psp,
    710  1.1  riastrad 		union ta_ras_cmd_input *info, bool enable)
    711  1.1  riastrad {
    712  1.1  riastrad 	struct ta_ras_shared_memory *ras_cmd;
    713  1.1  riastrad 	int ret;
    714  1.1  riastrad 
    715  1.1  riastrad 	if (!psp->ras.ras_initialized)
    716  1.1  riastrad 		return -EINVAL;
    717  1.1  riastrad 
    718  1.1  riastrad 	ras_cmd = (struct ta_ras_shared_memory *)psp->ras.ras_shared_buf;
    719  1.1  riastrad 	memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
    720  1.1  riastrad 
    721  1.1  riastrad 	if (enable)
    722  1.1  riastrad 		ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
    723  1.1  riastrad 	else
    724  1.1  riastrad 		ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
    725  1.1  riastrad 
    726  1.1  riastrad 	ras_cmd->ras_in_message = *info;
    727  1.1  riastrad 
    728  1.1  riastrad 	ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
    729  1.1  riastrad 	if (ret)
    730  1.1  riastrad 		return -EINVAL;
    731  1.1  riastrad 
    732  1.1  riastrad 	return ras_cmd->ras_status;
    733  1.1  riastrad }
    734  1.1  riastrad 
    735  1.1  riastrad static int psp_ras_terminate(struct psp_context *psp)
    736  1.1  riastrad {
    737  1.1  riastrad 	int ret;
    738  1.1  riastrad 
    739  1.1  riastrad 	/*
    740  1.1  riastrad 	 * TODO: bypass the terminate in sriov for now
    741  1.1  riastrad 	 */
    742  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
    743  1.1  riastrad 		return 0;
    744  1.1  riastrad 
    745  1.1  riastrad 	if (!psp->ras.ras_initialized)
    746  1.1  riastrad 		return 0;
    747  1.1  riastrad 
    748  1.1  riastrad 	ret = psp_ras_unload(psp);
    749  1.1  riastrad 	if (ret)
    750  1.1  riastrad 		return ret;
    751  1.1  riastrad 
    752  1.1  riastrad 	psp->ras.ras_initialized = false;
    753  1.1  riastrad 
    754  1.1  riastrad 	/* free ras shared memory */
    755  1.1  riastrad 	amdgpu_bo_free_kernel(&psp->ras.ras_shared_bo,
    756  1.1  riastrad 			&psp->ras.ras_shared_mc_addr,
    757  1.1  riastrad 			&psp->ras.ras_shared_buf);
    758  1.1  riastrad 
    759  1.1  riastrad 	return 0;
    760  1.1  riastrad }
    761  1.1  riastrad 
    762  1.1  riastrad static int psp_ras_initialize(struct psp_context *psp)
    763  1.1  riastrad {
    764  1.1  riastrad 	int ret;
    765  1.1  riastrad 
    766  1.1  riastrad 	/*
    767  1.1  riastrad 	 * TODO: bypass the initialize in sriov for now
    768  1.1  riastrad 	 */
    769  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
    770  1.1  riastrad 		return 0;
    771  1.1  riastrad 
    772  1.1  riastrad 	if (!psp->adev->psp.ta_ras_ucode_size ||
    773  1.1  riastrad 	    !psp->adev->psp.ta_ras_start_addr) {
    774  1.1  riastrad 		dev_warn(psp->adev->dev, "RAS: ras ta ucode is not available\n");
    775  1.1  riastrad 		return 0;
    776  1.1  riastrad 	}
    777  1.1  riastrad 
    778  1.1  riastrad 	if (!psp->ras.ras_initialized) {
    779  1.1  riastrad 		ret = psp_ras_init_shared_buf(psp);
    780  1.1  riastrad 		if (ret)
    781  1.1  riastrad 			return ret;
    782  1.1  riastrad 	}
    783  1.1  riastrad 
    784  1.1  riastrad 	ret = psp_ras_load(psp);
    785  1.1  riastrad 	if (ret)
    786  1.1  riastrad 		return ret;
    787  1.1  riastrad 
    788  1.1  riastrad 	return 0;
    789  1.1  riastrad }
    790  1.1  riastrad // ras end
    791  1.1  riastrad 
    792  1.1  riastrad // HDCP start
    793  1.1  riastrad static int psp_hdcp_init_shared_buf(struct psp_context *psp)
    794  1.1  riastrad {
    795  1.1  riastrad 	int ret;
    796  1.1  riastrad 
    797  1.1  riastrad 	/*
    798  1.1  riastrad 	 * Allocate 16k memory aligned to 4k from Frame Buffer (local
    799  1.1  riastrad 	 * physical) for hdcp ta <-> Driver
    800  1.1  riastrad 	 */
    801  1.1  riastrad 	ret = amdgpu_bo_create_kernel(psp->adev, PSP_HDCP_SHARED_MEM_SIZE,
    802  1.1  riastrad 				      PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
    803  1.1  riastrad 				      &psp->hdcp_context.hdcp_shared_bo,
    804  1.1  riastrad 				      &psp->hdcp_context.hdcp_shared_mc_addr,
    805  1.1  riastrad 				      &psp->hdcp_context.hdcp_shared_buf);
    806  1.1  riastrad 
    807  1.1  riastrad 	return ret;
    808  1.1  riastrad }
    809  1.1  riastrad 
    810  1.1  riastrad static int psp_hdcp_load(struct psp_context *psp)
    811  1.1  riastrad {
    812  1.1  riastrad 	int ret;
    813  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd;
    814  1.1  riastrad 
    815  1.1  riastrad 	/*
    816  1.1  riastrad 	 * TODO: bypass the loading in sriov for now
    817  1.1  riastrad 	 */
    818  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
    819  1.1  riastrad 		return 0;
    820  1.1  riastrad 
    821  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
    822  1.1  riastrad 	if (!cmd)
    823  1.1  riastrad 		return -ENOMEM;
    824  1.1  riastrad 
    825  1.1  riastrad 	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
    826  1.1  riastrad 	memcpy(psp->fw_pri_buf, psp->ta_hdcp_start_addr,
    827  1.1  riastrad 	       psp->ta_hdcp_ucode_size);
    828  1.1  riastrad 
    829  1.1  riastrad 	psp_prep_ta_load_cmd_buf(cmd,
    830  1.1  riastrad 				 psp->fw_pri_mc_addr,
    831  1.1  riastrad 				 psp->ta_hdcp_ucode_size,
    832  1.1  riastrad 				 psp->hdcp_context.hdcp_shared_mc_addr,
    833  1.1  riastrad 				 PSP_HDCP_SHARED_MEM_SIZE);
    834  1.1  riastrad 
    835  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
    836  1.1  riastrad 
    837  1.1  riastrad 	if (!ret) {
    838  1.1  riastrad 		psp->hdcp_context.hdcp_initialized = true;
    839  1.1  riastrad 		psp->hdcp_context.session_id = cmd->resp.session_id;
    840  1.1  riastrad 	}
    841  1.1  riastrad 
    842  1.1  riastrad 	kfree(cmd);
    843  1.1  riastrad 
    844  1.1  riastrad 	return ret;
    845  1.1  riastrad }
    846  1.1  riastrad static int psp_hdcp_initialize(struct psp_context *psp)
    847  1.1  riastrad {
    848  1.1  riastrad 	int ret;
    849  1.1  riastrad 
    850  1.1  riastrad 	/*
    851  1.1  riastrad 	 * TODO: bypass the initialize in sriov for now
    852  1.1  riastrad 	 */
    853  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
    854  1.1  riastrad 		return 0;
    855  1.1  riastrad 
    856  1.1  riastrad 	if (!psp->adev->psp.ta_hdcp_ucode_size ||
    857  1.1  riastrad 	    !psp->adev->psp.ta_hdcp_start_addr) {
    858  1.1  riastrad 		dev_warn(psp->adev->dev, "HDCP: hdcp ta ucode is not available\n");
    859  1.1  riastrad 		return 0;
    860  1.1  riastrad 	}
    861  1.1  riastrad 
    862  1.1  riastrad 	if (!psp->hdcp_context.hdcp_initialized) {
    863  1.1  riastrad 		ret = psp_hdcp_init_shared_buf(psp);
    864  1.1  riastrad 		if (ret)
    865  1.1  riastrad 			return ret;
    866  1.1  riastrad 	}
    867  1.1  riastrad 
    868  1.1  riastrad 	ret = psp_hdcp_load(psp);
    869  1.1  riastrad 	if (ret)
    870  1.1  riastrad 		return ret;
    871  1.1  riastrad 
    872  1.1  riastrad 	return 0;
    873  1.1  riastrad }
    874  1.1  riastrad 
    875  1.1  riastrad static int psp_hdcp_unload(struct psp_context *psp)
    876  1.1  riastrad {
    877  1.1  riastrad 	int ret;
    878  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd;
    879  1.1  riastrad 
    880  1.1  riastrad 	/*
    881  1.1  riastrad 	 * TODO: bypass the unloading in sriov for now
    882  1.1  riastrad 	 */
    883  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
    884  1.1  riastrad 		return 0;
    885  1.1  riastrad 
    886  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
    887  1.1  riastrad 	if (!cmd)
    888  1.1  riastrad 		return -ENOMEM;
    889  1.1  riastrad 
    890  1.1  riastrad 	psp_prep_ta_unload_cmd_buf(cmd, psp->hdcp_context.session_id);
    891  1.1  riastrad 
    892  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
    893  1.1  riastrad 
    894  1.1  riastrad 	kfree(cmd);
    895  1.1  riastrad 
    896  1.1  riastrad 	return ret;
    897  1.1  riastrad }
    898  1.1  riastrad 
    899  1.1  riastrad int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
    900  1.1  riastrad {
    901  1.1  riastrad 	/*
    902  1.1  riastrad 	 * TODO: bypass the loading in sriov for now
    903  1.1  riastrad 	 */
    904  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
    905  1.1  riastrad 		return 0;
    906  1.1  riastrad 
    907  1.1  riastrad 	return psp_ta_invoke(psp, ta_cmd_id, psp->hdcp_context.session_id);
    908  1.1  riastrad }
    909  1.1  riastrad 
    910  1.1  riastrad static int psp_hdcp_terminate(struct psp_context *psp)
    911  1.1  riastrad {
    912  1.1  riastrad 	int ret;
    913  1.1  riastrad 
    914  1.1  riastrad 	/*
    915  1.1  riastrad 	 * TODO: bypass the terminate in sriov for now
    916  1.1  riastrad 	 */
    917  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
    918  1.1  riastrad 		return 0;
    919  1.1  riastrad 
    920  1.1  riastrad 	if (!psp->hdcp_context.hdcp_initialized)
    921  1.1  riastrad 		return 0;
    922  1.1  riastrad 
    923  1.1  riastrad 	ret = psp_hdcp_unload(psp);
    924  1.1  riastrad 	if (ret)
    925  1.1  riastrad 		return ret;
    926  1.1  riastrad 
    927  1.1  riastrad 	psp->hdcp_context.hdcp_initialized = false;
    928  1.1  riastrad 
    929  1.1  riastrad 	/* free hdcp shared memory */
    930  1.1  riastrad 	amdgpu_bo_free_kernel(&psp->hdcp_context.hdcp_shared_bo,
    931  1.1  riastrad 			      &psp->hdcp_context.hdcp_shared_mc_addr,
    932  1.1  riastrad 			      &psp->hdcp_context.hdcp_shared_buf);
    933  1.1  riastrad 
    934  1.1  riastrad 	return 0;
    935  1.1  riastrad }
    936  1.1  riastrad // HDCP end
    937  1.1  riastrad 
    938  1.1  riastrad // DTM start
    939  1.1  riastrad static int psp_dtm_init_shared_buf(struct psp_context *psp)
    940  1.1  riastrad {
    941  1.1  riastrad 	int ret;
    942  1.1  riastrad 
    943  1.1  riastrad 	/*
    944  1.1  riastrad 	 * Allocate 16k memory aligned to 4k from Frame Buffer (local
    945  1.1  riastrad 	 * physical) for dtm ta <-> Driver
    946  1.1  riastrad 	 */
    947  1.1  riastrad 	ret = amdgpu_bo_create_kernel(psp->adev, PSP_DTM_SHARED_MEM_SIZE,
    948  1.1  riastrad 				      PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
    949  1.1  riastrad 				      &psp->dtm_context.dtm_shared_bo,
    950  1.1  riastrad 				      &psp->dtm_context.dtm_shared_mc_addr,
    951  1.1  riastrad 				      &psp->dtm_context.dtm_shared_buf);
    952  1.1  riastrad 
    953  1.1  riastrad 	return ret;
    954  1.1  riastrad }
    955  1.1  riastrad 
    956  1.1  riastrad static int psp_dtm_load(struct psp_context *psp)
    957  1.1  riastrad {
    958  1.1  riastrad 	int ret;
    959  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd;
    960  1.1  riastrad 
    961  1.1  riastrad 	/*
    962  1.1  riastrad 	 * TODO: bypass the loading in sriov for now
    963  1.1  riastrad 	 */
    964  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
    965  1.1  riastrad 		return 0;
    966  1.1  riastrad 
    967  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
    968  1.1  riastrad 	if (!cmd)
    969  1.1  riastrad 		return -ENOMEM;
    970  1.1  riastrad 
    971  1.1  riastrad 	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
    972  1.1  riastrad 	memcpy(psp->fw_pri_buf, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
    973  1.1  riastrad 
    974  1.1  riastrad 	psp_prep_ta_load_cmd_buf(cmd,
    975  1.1  riastrad 				 psp->fw_pri_mc_addr,
    976  1.1  riastrad 				 psp->ta_dtm_ucode_size,
    977  1.1  riastrad 				 psp->dtm_context.dtm_shared_mc_addr,
    978  1.1  riastrad 				 PSP_DTM_SHARED_MEM_SIZE);
    979  1.1  riastrad 
    980  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
    981  1.1  riastrad 
    982  1.1  riastrad 	if (!ret) {
    983  1.1  riastrad 		psp->dtm_context.dtm_initialized = true;
    984  1.1  riastrad 		psp->dtm_context.session_id = cmd->resp.session_id;
    985  1.1  riastrad 	}
    986  1.1  riastrad 
    987  1.1  riastrad 	kfree(cmd);
    988  1.1  riastrad 
    989  1.1  riastrad 	return ret;
    990  1.1  riastrad }
    991  1.1  riastrad 
    992  1.1  riastrad static int psp_dtm_initialize(struct psp_context *psp)
    993  1.1  riastrad {
    994  1.1  riastrad 	int ret;
    995  1.1  riastrad 
    996  1.1  riastrad 	/*
    997  1.1  riastrad 	 * TODO: bypass the initialize in sriov for now
    998  1.1  riastrad 	 */
    999  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
   1000  1.1  riastrad 		return 0;
   1001  1.1  riastrad 
   1002  1.1  riastrad 	if (!psp->adev->psp.ta_dtm_ucode_size ||
   1003  1.1  riastrad 	    !psp->adev->psp.ta_dtm_start_addr) {
   1004  1.1  riastrad 		dev_warn(psp->adev->dev, "DTM: dtm ta ucode is not available\n");
   1005  1.1  riastrad 		return 0;
   1006  1.1  riastrad 	}
   1007  1.1  riastrad 
   1008  1.1  riastrad 	if (!psp->dtm_context.dtm_initialized) {
   1009  1.1  riastrad 		ret = psp_dtm_init_shared_buf(psp);
   1010  1.1  riastrad 		if (ret)
   1011  1.1  riastrad 			return ret;
   1012  1.1  riastrad 	}
   1013  1.1  riastrad 
   1014  1.1  riastrad 	ret = psp_dtm_load(psp);
   1015  1.1  riastrad 	if (ret)
   1016  1.1  riastrad 		return ret;
   1017  1.1  riastrad 
   1018  1.1  riastrad 	return 0;
   1019  1.1  riastrad }
   1020  1.1  riastrad 
   1021  1.1  riastrad static int psp_dtm_unload(struct psp_context *psp)
   1022  1.1  riastrad {
   1023  1.1  riastrad 	int ret;
   1024  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd;
   1025  1.1  riastrad 
   1026  1.1  riastrad 	/*
   1027  1.1  riastrad 	 * TODO: bypass the unloading in sriov for now
   1028  1.1  riastrad 	 */
   1029  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
   1030  1.1  riastrad 		return 0;
   1031  1.1  riastrad 
   1032  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
   1033  1.1  riastrad 	if (!cmd)
   1034  1.1  riastrad 		return -ENOMEM;
   1035  1.1  riastrad 
   1036  1.1  riastrad 	psp_prep_ta_unload_cmd_buf(cmd, psp->dtm_context.session_id);
   1037  1.1  riastrad 
   1038  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
   1039  1.1  riastrad 
   1040  1.1  riastrad 	kfree(cmd);
   1041  1.1  riastrad 
   1042  1.1  riastrad 	return ret;
   1043  1.1  riastrad }
   1044  1.1  riastrad 
   1045  1.1  riastrad int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
   1046  1.1  riastrad {
   1047  1.1  riastrad 	/*
   1048  1.1  riastrad 	 * TODO: bypass the loading in sriov for now
   1049  1.1  riastrad 	 */
   1050  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
   1051  1.1  riastrad 		return 0;
   1052  1.1  riastrad 
   1053  1.1  riastrad 	return psp_ta_invoke(psp, ta_cmd_id, psp->dtm_context.session_id);
   1054  1.1  riastrad }
   1055  1.1  riastrad 
   1056  1.1  riastrad static int psp_dtm_terminate(struct psp_context *psp)
   1057  1.1  riastrad {
   1058  1.1  riastrad 	int ret;
   1059  1.1  riastrad 
   1060  1.1  riastrad 	/*
   1061  1.1  riastrad 	 * TODO: bypass the terminate in sriov for now
   1062  1.1  riastrad 	 */
   1063  1.1  riastrad 	if (amdgpu_sriov_vf(psp->adev))
   1064  1.1  riastrad 		return 0;
   1065  1.1  riastrad 
   1066  1.1  riastrad 	if (!psp->dtm_context.dtm_initialized)
   1067  1.1  riastrad 		return 0;
   1068  1.1  riastrad 
   1069  1.1  riastrad 	ret = psp_dtm_unload(psp);
   1070  1.1  riastrad 	if (ret)
   1071  1.1  riastrad 		return ret;
   1072  1.1  riastrad 
   1073  1.1  riastrad 	psp->dtm_context.dtm_initialized = false;
   1074  1.1  riastrad 
   1075  1.1  riastrad 	/* free hdcp shared memory */
   1076  1.1  riastrad 	amdgpu_bo_free_kernel(&psp->dtm_context.dtm_shared_bo,
   1077  1.1  riastrad 			      &psp->dtm_context.dtm_shared_mc_addr,
   1078  1.1  riastrad 			      &psp->dtm_context.dtm_shared_buf);
   1079  1.1  riastrad 
   1080  1.1  riastrad 	return 0;
   1081  1.1  riastrad }
   1082  1.1  riastrad // DTM end
   1083  1.1  riastrad 
   1084  1.1  riastrad static int psp_hw_start(struct psp_context *psp)
   1085  1.1  riastrad {
   1086  1.1  riastrad 	struct amdgpu_device *adev = psp->adev;
   1087  1.1  riastrad 	int ret;
   1088  1.1  riastrad 
   1089  1.1  riastrad 	if (!amdgpu_sriov_vf(adev) || !adev->in_gpu_reset) {
   1090  1.1  riastrad 		if (psp->kdb_bin_size &&
   1091  1.1  riastrad 		    (psp->funcs->bootloader_load_kdb != NULL)) {
   1092  1.1  riastrad 			ret = psp_bootloader_load_kdb(psp);
   1093  1.1  riastrad 			if (ret) {
   1094  1.1  riastrad 				DRM_ERROR("PSP load kdb failed!\n");
   1095  1.1  riastrad 				return ret;
   1096  1.1  riastrad 			}
   1097  1.1  riastrad 		}
   1098  1.1  riastrad 
   1099  1.1  riastrad 		ret = psp_bootloader_load_sysdrv(psp);
   1100  1.1  riastrad 		if (ret) {
   1101  1.1  riastrad 			DRM_ERROR("PSP load sysdrv failed!\n");
   1102  1.1  riastrad 			return ret;
   1103  1.1  riastrad 		}
   1104  1.1  riastrad 
   1105  1.1  riastrad 		ret = psp_bootloader_load_sos(psp);
   1106  1.1  riastrad 		if (ret) {
   1107  1.1  riastrad 			DRM_ERROR("PSP load sos failed!\n");
   1108  1.1  riastrad 			return ret;
   1109  1.1  riastrad 		}
   1110  1.1  riastrad 	}
   1111  1.1  riastrad 
   1112  1.1  riastrad 	ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
   1113  1.1  riastrad 	if (ret) {
   1114  1.1  riastrad 		DRM_ERROR("PSP create ring failed!\n");
   1115  1.1  riastrad 		return ret;
   1116  1.1  riastrad 	}
   1117  1.1  riastrad 
   1118  1.1  riastrad 	ret = psp_tmr_init(psp);
   1119  1.1  riastrad 	if (ret) {
   1120  1.1  riastrad 		DRM_ERROR("PSP tmr init failed!\n");
   1121  1.1  riastrad 		return ret;
   1122  1.1  riastrad 	}
   1123  1.1  riastrad 
   1124  1.1  riastrad 	ret = psp_tmr_load(psp);
   1125  1.1  riastrad 	if (ret) {
   1126  1.1  riastrad 		DRM_ERROR("PSP load tmr failed!\n");
   1127  1.1  riastrad 		return ret;
   1128  1.1  riastrad 	}
   1129  1.1  riastrad 
   1130  1.1  riastrad 	return 0;
   1131  1.1  riastrad }
   1132  1.1  riastrad 
   1133  1.1  riastrad static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
   1134  1.1  riastrad 			   enum psp_gfx_fw_type *type)
   1135  1.1  riastrad {
   1136  1.1  riastrad 	switch (ucode->ucode_id) {
   1137  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA0:
   1138  1.1  riastrad 		*type = GFX_FW_TYPE_SDMA0;
   1139  1.1  riastrad 		break;
   1140  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA1:
   1141  1.1  riastrad 		*type = GFX_FW_TYPE_SDMA1;
   1142  1.1  riastrad 		break;
   1143  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA2:
   1144  1.1  riastrad 		*type = GFX_FW_TYPE_SDMA2;
   1145  1.1  riastrad 		break;
   1146  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA3:
   1147  1.1  riastrad 		*type = GFX_FW_TYPE_SDMA3;
   1148  1.1  riastrad 		break;
   1149  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA4:
   1150  1.1  riastrad 		*type = GFX_FW_TYPE_SDMA4;
   1151  1.1  riastrad 		break;
   1152  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA5:
   1153  1.1  riastrad 		*type = GFX_FW_TYPE_SDMA5;
   1154  1.1  riastrad 		break;
   1155  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA6:
   1156  1.1  riastrad 		*type = GFX_FW_TYPE_SDMA6;
   1157  1.1  riastrad 		break;
   1158  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA7:
   1159  1.1  riastrad 		*type = GFX_FW_TYPE_SDMA7;
   1160  1.1  riastrad 		break;
   1161  1.1  riastrad 	case AMDGPU_UCODE_ID_CP_CE:
   1162  1.1  riastrad 		*type = GFX_FW_TYPE_CP_CE;
   1163  1.1  riastrad 		break;
   1164  1.1  riastrad 	case AMDGPU_UCODE_ID_CP_PFP:
   1165  1.1  riastrad 		*type = GFX_FW_TYPE_CP_PFP;
   1166  1.1  riastrad 		break;
   1167  1.1  riastrad 	case AMDGPU_UCODE_ID_CP_ME:
   1168  1.1  riastrad 		*type = GFX_FW_TYPE_CP_ME;
   1169  1.1  riastrad 		break;
   1170  1.1  riastrad 	case AMDGPU_UCODE_ID_CP_MEC1:
   1171  1.1  riastrad 		*type = GFX_FW_TYPE_CP_MEC;
   1172  1.1  riastrad 		break;
   1173  1.1  riastrad 	case AMDGPU_UCODE_ID_CP_MEC1_JT:
   1174  1.1  riastrad 		*type = GFX_FW_TYPE_CP_MEC_ME1;
   1175  1.1  riastrad 		break;
   1176  1.1  riastrad 	case AMDGPU_UCODE_ID_CP_MEC2:
   1177  1.1  riastrad 		*type = GFX_FW_TYPE_CP_MEC;
   1178  1.1  riastrad 		break;
   1179  1.1  riastrad 	case AMDGPU_UCODE_ID_CP_MEC2_JT:
   1180  1.1  riastrad 		*type = GFX_FW_TYPE_CP_MEC_ME2;
   1181  1.1  riastrad 		break;
   1182  1.1  riastrad 	case AMDGPU_UCODE_ID_RLC_G:
   1183  1.1  riastrad 		*type = GFX_FW_TYPE_RLC_G;
   1184  1.1  riastrad 		break;
   1185  1.1  riastrad 	case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
   1186  1.1  riastrad 		*type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
   1187  1.1  riastrad 		break;
   1188  1.1  riastrad 	case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
   1189  1.1  riastrad 		*type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
   1190  1.1  riastrad 		break;
   1191  1.1  riastrad 	case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
   1192  1.1  riastrad 		*type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
   1193  1.1  riastrad 		break;
   1194  1.1  riastrad 	case AMDGPU_UCODE_ID_SMC:
   1195  1.1  riastrad 		*type = GFX_FW_TYPE_SMU;
   1196  1.1  riastrad 		break;
   1197  1.1  riastrad 	case AMDGPU_UCODE_ID_UVD:
   1198  1.1  riastrad 		*type = GFX_FW_TYPE_UVD;
   1199  1.1  riastrad 		break;
   1200  1.1  riastrad 	case AMDGPU_UCODE_ID_UVD1:
   1201  1.1  riastrad 		*type = GFX_FW_TYPE_UVD1;
   1202  1.1  riastrad 		break;
   1203  1.1  riastrad 	case AMDGPU_UCODE_ID_VCE:
   1204  1.1  riastrad 		*type = GFX_FW_TYPE_VCE;
   1205  1.1  riastrad 		break;
   1206  1.1  riastrad 	case AMDGPU_UCODE_ID_VCN:
   1207  1.1  riastrad 		*type = GFX_FW_TYPE_VCN;
   1208  1.1  riastrad 		break;
   1209  1.1  riastrad 	case AMDGPU_UCODE_ID_VCN1:
   1210  1.1  riastrad 		*type = GFX_FW_TYPE_VCN1;
   1211  1.1  riastrad 		break;
   1212  1.1  riastrad 	case AMDGPU_UCODE_ID_DMCU_ERAM:
   1213  1.1  riastrad 		*type = GFX_FW_TYPE_DMCU_ERAM;
   1214  1.1  riastrad 		break;
   1215  1.1  riastrad 	case AMDGPU_UCODE_ID_DMCU_INTV:
   1216  1.1  riastrad 		*type = GFX_FW_TYPE_DMCU_ISR;
   1217  1.1  riastrad 		break;
   1218  1.1  riastrad 	case AMDGPU_UCODE_ID_VCN0_RAM:
   1219  1.1  riastrad 		*type = GFX_FW_TYPE_VCN0_RAM;
   1220  1.1  riastrad 		break;
   1221  1.1  riastrad 	case AMDGPU_UCODE_ID_VCN1_RAM:
   1222  1.1  riastrad 		*type = GFX_FW_TYPE_VCN1_RAM;
   1223  1.1  riastrad 		break;
   1224  1.1  riastrad 	case AMDGPU_UCODE_ID_DMCUB:
   1225  1.1  riastrad 		*type = GFX_FW_TYPE_DMUB;
   1226  1.1  riastrad 		break;
   1227  1.1  riastrad 	case AMDGPU_UCODE_ID_MAXIMUM:
   1228  1.1  riastrad 	default:
   1229  1.1  riastrad 		return -EINVAL;
   1230  1.1  riastrad 	}
   1231  1.1  riastrad 
   1232  1.1  riastrad 	return 0;
   1233  1.1  riastrad }
   1234  1.1  riastrad 
   1235  1.1  riastrad static void psp_print_fw_hdr(struct psp_context *psp,
   1236  1.1  riastrad 			     struct amdgpu_firmware_info *ucode)
   1237  1.1  riastrad {
   1238  1.1  riastrad 	struct amdgpu_device *adev = psp->adev;
   1239  1.1  riastrad 	struct common_firmware_header *hdr;
   1240  1.1  riastrad 
   1241  1.1  riastrad 	switch (ucode->ucode_id) {
   1242  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA0:
   1243  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA1:
   1244  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA2:
   1245  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA3:
   1246  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA4:
   1247  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA5:
   1248  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA6:
   1249  1.1  riastrad 	case AMDGPU_UCODE_ID_SDMA7:
   1250  1.1  riastrad 		hdr = (struct common_firmware_header *)
   1251  1.1  riastrad 			adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
   1252  1.1  riastrad 		amdgpu_ucode_print_sdma_hdr(hdr);
   1253  1.1  riastrad 		break;
   1254  1.1  riastrad 	case AMDGPU_UCODE_ID_CP_CE:
   1255  1.1  riastrad 		hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
   1256  1.1  riastrad 		amdgpu_ucode_print_gfx_hdr(hdr);
   1257  1.1  riastrad 		break;
   1258  1.1  riastrad 	case AMDGPU_UCODE_ID_CP_PFP:
   1259  1.1  riastrad 		hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
   1260  1.1  riastrad 		amdgpu_ucode_print_gfx_hdr(hdr);
   1261  1.1  riastrad 		break;
   1262  1.1  riastrad 	case AMDGPU_UCODE_ID_CP_ME:
   1263  1.1  riastrad 		hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
   1264  1.1  riastrad 		amdgpu_ucode_print_gfx_hdr(hdr);
   1265  1.1  riastrad 		break;
   1266  1.1  riastrad 	case AMDGPU_UCODE_ID_CP_MEC1:
   1267  1.1  riastrad 		hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
   1268  1.1  riastrad 		amdgpu_ucode_print_gfx_hdr(hdr);
   1269  1.1  riastrad 		break;
   1270  1.1  riastrad 	case AMDGPU_UCODE_ID_RLC_G:
   1271  1.1  riastrad 		hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
   1272  1.1  riastrad 		amdgpu_ucode_print_rlc_hdr(hdr);
   1273  1.1  riastrad 		break;
   1274  1.1  riastrad 	case AMDGPU_UCODE_ID_SMC:
   1275  1.1  riastrad 		hdr = (struct common_firmware_header *)adev->pm.fw->data;
   1276  1.1  riastrad 		amdgpu_ucode_print_smc_hdr(hdr);
   1277  1.1  riastrad 		break;
   1278  1.1  riastrad 	default:
   1279  1.1  riastrad 		break;
   1280  1.1  riastrad 	}
   1281  1.1  riastrad }
   1282  1.1  riastrad 
   1283  1.1  riastrad static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
   1284  1.1  riastrad 				       struct psp_gfx_cmd_resp *cmd)
   1285  1.1  riastrad {
   1286  1.1  riastrad 	int ret;
   1287  1.1  riastrad 	uint64_t fw_mem_mc_addr = ucode->mc_addr;
   1288  1.1  riastrad 
   1289  1.1  riastrad 	memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
   1290  1.1  riastrad 
   1291  1.1  riastrad 	cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
   1292  1.1  riastrad 	cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
   1293  1.1  riastrad 	cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
   1294  1.1  riastrad 	cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
   1295  1.1  riastrad 
   1296  1.1  riastrad 	ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
   1297  1.1  riastrad 	if (ret)
   1298  1.1  riastrad 		DRM_ERROR("Unknown firmware type\n");
   1299  1.1  riastrad 
   1300  1.1  riastrad 	return ret;
   1301  1.1  riastrad }
   1302  1.1  riastrad 
   1303  1.1  riastrad static int psp_execute_np_fw_load(struct psp_context *psp,
   1304  1.1  riastrad 			       struct amdgpu_firmware_info *ucode)
   1305  1.1  riastrad {
   1306  1.1  riastrad 	int ret = 0;
   1307  1.1  riastrad 
   1308  1.1  riastrad 	ret = psp_prep_load_ip_fw_cmd_buf(ucode, psp->cmd);
   1309  1.1  riastrad 	if (ret)
   1310  1.1  riastrad 		return ret;
   1311  1.1  riastrad 
   1312  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, ucode, psp->cmd,
   1313  1.1  riastrad 				 psp->fence_buf_mc_addr);
   1314  1.1  riastrad 
   1315  1.1  riastrad 	return ret;
   1316  1.1  riastrad }
   1317  1.1  riastrad 
   1318  1.1  riastrad static int psp_np_fw_load(struct psp_context *psp)
   1319  1.1  riastrad {
   1320  1.1  riastrad 	int i, ret;
   1321  1.1  riastrad 	struct amdgpu_firmware_info *ucode;
   1322  1.1  riastrad 	struct amdgpu_device* adev = psp->adev;
   1323  1.1  riastrad 
   1324  1.1  riastrad 	if (psp->autoload_supported) {
   1325  1.1  riastrad 		ucode = &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
   1326  1.1  riastrad 		if (!ucode->fw)
   1327  1.1  riastrad 			goto out;
   1328  1.1  riastrad 
   1329  1.1  riastrad 		ret = psp_execute_np_fw_load(psp, ucode);
   1330  1.1  riastrad 		if (ret)
   1331  1.1  riastrad 			return ret;
   1332  1.1  riastrad 	}
   1333  1.1  riastrad 
   1334  1.1  riastrad out:
   1335  1.1  riastrad 	for (i = 0; i < adev->firmware.max_ucodes; i++) {
   1336  1.1  riastrad 		ucode = &adev->firmware.ucode[i];
   1337  1.1  riastrad 		if (!ucode->fw)
   1338  1.1  riastrad 			continue;
   1339  1.1  riastrad 
   1340  1.1  riastrad 		if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
   1341  1.1  riastrad 		    (psp_smu_reload_quirk(psp) || psp->autoload_supported))
   1342  1.1  riastrad 			continue;
   1343  1.1  riastrad 
   1344  1.1  riastrad 		if (amdgpu_sriov_vf(adev) &&
   1345  1.1  riastrad 		   (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
   1346  1.1  riastrad 		    || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
   1347  1.1  riastrad 		    || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2
   1348  1.1  riastrad 		    || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3
   1349  1.1  riastrad 		    || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA4
   1350  1.1  riastrad 		    || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA5
   1351  1.1  riastrad 		    || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA6
   1352  1.1  riastrad 		    || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA7
   1353  1.1  riastrad                     || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G
   1354  1.1  riastrad 	            || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL
   1355  1.1  riastrad 	            || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM
   1356  1.1  riastrad 	            || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM
   1357  1.1  riastrad 	            || ucode->ucode_id == AMDGPU_UCODE_ID_SMC))
   1358  1.1  riastrad 			/*skip ucode loading in SRIOV VF */
   1359  1.1  riastrad 			continue;
   1360  1.1  riastrad 
   1361  1.1  riastrad 		if (psp->autoload_supported &&
   1362  1.1  riastrad 		    (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
   1363  1.1  riastrad 		     ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
   1364  1.1  riastrad 			/* skip mec JT when autoload is enabled */
   1365  1.1  riastrad 			continue;
   1366  1.1  riastrad 
   1367  1.1  riastrad 		psp_print_fw_hdr(psp, ucode);
   1368  1.1  riastrad 
   1369  1.1  riastrad 		ret = psp_execute_np_fw_load(psp, ucode);
   1370  1.1  riastrad 		if (ret)
   1371  1.1  riastrad 			return ret;
   1372  1.1  riastrad 
   1373  1.1  riastrad 		/* Start rlc autoload after psp recieved all the gfx firmware */
   1374  1.1  riastrad 		if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
   1375  1.1  riastrad 		    AMDGPU_UCODE_ID_CP_MEC2 : AMDGPU_UCODE_ID_RLC_G)) {
   1376  1.1  riastrad 			ret = psp_rlc_autoload(psp);
   1377  1.1  riastrad 			if (ret) {
   1378  1.1  riastrad 				DRM_ERROR("Failed to start rlc autoload\n");
   1379  1.1  riastrad 				return ret;
   1380  1.1  riastrad 			}
   1381  1.1  riastrad 		}
   1382  1.1  riastrad #if 0
   1383  1.1  riastrad 		/* check if firmware loaded sucessfully */
   1384  1.1  riastrad 		if (!amdgpu_psp_check_fw_loading_status(adev, i))
   1385  1.1  riastrad 			return -EINVAL;
   1386  1.1  riastrad #endif
   1387  1.1  riastrad 	}
   1388  1.1  riastrad 
   1389  1.1  riastrad 	return 0;
   1390  1.1  riastrad }
   1391  1.1  riastrad 
   1392  1.1  riastrad static int psp_load_fw(struct amdgpu_device *adev)
   1393  1.1  riastrad {
   1394  1.1  riastrad 	int ret;
   1395  1.1  riastrad 	struct psp_context *psp = &adev->psp;
   1396  1.1  riastrad 
   1397  1.1  riastrad 	if (amdgpu_sriov_vf(adev) && adev->in_gpu_reset) {
   1398  1.1  riastrad 		psp_ring_stop(psp, PSP_RING_TYPE__KM); /* should not destroy ring, only stop */
   1399  1.1  riastrad 		goto skip_memalloc;
   1400  1.1  riastrad 	}
   1401  1.1  riastrad 
   1402  1.1  riastrad 	psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
   1403  1.1  riastrad 	if (!psp->cmd)
   1404  1.1  riastrad 		return -ENOMEM;
   1405  1.1  riastrad 
   1406  1.1  riastrad 	ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
   1407  1.1  riastrad 					AMDGPU_GEM_DOMAIN_GTT,
   1408  1.1  riastrad 					&psp->fw_pri_bo,
   1409  1.1  riastrad 					&psp->fw_pri_mc_addr,
   1410  1.1  riastrad 					&psp->fw_pri_buf);
   1411  1.1  riastrad 	if (ret)
   1412  1.1  riastrad 		goto failed;
   1413  1.1  riastrad 
   1414  1.1  riastrad 	ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
   1415  1.1  riastrad 					AMDGPU_GEM_DOMAIN_VRAM,
   1416  1.1  riastrad 					&psp->fence_buf_bo,
   1417  1.1  riastrad 					&psp->fence_buf_mc_addr,
   1418  1.1  riastrad 					&psp->fence_buf);
   1419  1.1  riastrad 	if (ret)
   1420  1.1  riastrad 		goto failed;
   1421  1.1  riastrad 
   1422  1.1  riastrad 	ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
   1423  1.1  riastrad 				      AMDGPU_GEM_DOMAIN_VRAM,
   1424  1.1  riastrad 				      &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
   1425  1.1  riastrad 				      (void **)&psp->cmd_buf_mem);
   1426  1.1  riastrad 	if (ret)
   1427  1.1  riastrad 		goto failed;
   1428  1.1  riastrad 
   1429  1.1  riastrad 	memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
   1430  1.1  riastrad 
   1431  1.1  riastrad 	ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
   1432  1.1  riastrad 	if (ret) {
   1433  1.1  riastrad 		DRM_ERROR("PSP ring init failed!\n");
   1434  1.1  riastrad 		goto failed;
   1435  1.1  riastrad 	}
   1436  1.1  riastrad 
   1437  1.1  riastrad skip_memalloc:
   1438  1.1  riastrad 	ret = psp_hw_start(psp);
   1439  1.1  riastrad 	if (ret)
   1440  1.1  riastrad 		goto failed;
   1441  1.1  riastrad 
   1442  1.1  riastrad 	ret = psp_np_fw_load(psp);
   1443  1.1  riastrad 	if (ret)
   1444  1.1  riastrad 		goto failed;
   1445  1.1  riastrad 
   1446  1.1  riastrad 	ret = psp_asd_load(psp);
   1447  1.1  riastrad 	if (ret) {
   1448  1.1  riastrad 		DRM_ERROR("PSP load asd failed!\n");
   1449  1.1  riastrad 		return ret;
   1450  1.1  riastrad 	}
   1451  1.1  riastrad 
   1452  1.1  riastrad 	if (adev->gmc.xgmi.num_physical_nodes > 1) {
   1453  1.1  riastrad 		ret = psp_xgmi_initialize(psp);
   1454  1.1  riastrad 		/* Warning the XGMI seesion initialize failure
   1455  1.1  riastrad 		 * Instead of stop driver initialization
   1456  1.1  riastrad 		 */
   1457  1.1  riastrad 		if (ret)
   1458  1.1  riastrad 			dev_err(psp->adev->dev,
   1459  1.1  riastrad 				"XGMI: Failed to initialize XGMI session\n");
   1460  1.1  riastrad 	}
   1461  1.1  riastrad 
   1462  1.1  riastrad 	if (psp->adev->psp.ta_fw) {
   1463  1.1  riastrad 		ret = psp_ras_initialize(psp);
   1464  1.1  riastrad 		if (ret)
   1465  1.1  riastrad 			dev_err(psp->adev->dev,
   1466  1.1  riastrad 					"RAS: Failed to initialize RAS\n");
   1467  1.1  riastrad 
   1468  1.1  riastrad 		ret = psp_hdcp_initialize(psp);
   1469  1.1  riastrad 		if (ret)
   1470  1.1  riastrad 			dev_err(psp->adev->dev,
   1471  1.1  riastrad 				"HDCP: Failed to initialize HDCP\n");
   1472  1.1  riastrad 
   1473  1.1  riastrad 		ret = psp_dtm_initialize(psp);
   1474  1.1  riastrad 		if (ret)
   1475  1.1  riastrad 			dev_err(psp->adev->dev,
   1476  1.1  riastrad 				"DTM: Failed to initialize DTM\n");
   1477  1.1  riastrad 	}
   1478  1.1  riastrad 
   1479  1.1  riastrad 	return 0;
   1480  1.1  riastrad 
   1481  1.1  riastrad failed:
   1482  1.1  riastrad 	/*
   1483  1.1  riastrad 	 * all cleanup jobs (xgmi terminate, ras terminate,
   1484  1.1  riastrad 	 * ring destroy, cmd/fence/fw buffers destory,
   1485  1.1  riastrad 	 * psp->cmd destory) are delayed to psp_hw_fini
   1486  1.1  riastrad 	 */
   1487  1.1  riastrad 	return ret;
   1488  1.1  riastrad }
   1489  1.1  riastrad 
   1490  1.1  riastrad static int psp_hw_init(void *handle)
   1491  1.1  riastrad {
   1492  1.1  riastrad 	int ret;
   1493  1.1  riastrad 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
   1494  1.1  riastrad 
   1495  1.1  riastrad 	mutex_lock(&adev->firmware.mutex);
   1496  1.1  riastrad 	/*
   1497  1.1  riastrad 	 * This sequence is just used on hw_init only once, no need on
   1498  1.1  riastrad 	 * resume.
   1499  1.1  riastrad 	 */
   1500  1.1  riastrad 	ret = amdgpu_ucode_init_bo(adev);
   1501  1.1  riastrad 	if (ret)
   1502  1.1  riastrad 		goto failed;
   1503  1.1  riastrad 
   1504  1.1  riastrad 	ret = psp_load_fw(adev);
   1505  1.1  riastrad 	if (ret) {
   1506  1.1  riastrad 		DRM_ERROR("PSP firmware loading failed\n");
   1507  1.1  riastrad 		goto failed;
   1508  1.1  riastrad 	}
   1509  1.1  riastrad 
   1510  1.1  riastrad 	mutex_unlock(&adev->firmware.mutex);
   1511  1.1  riastrad 	return 0;
   1512  1.1  riastrad 
   1513  1.1  riastrad failed:
   1514  1.1  riastrad 	adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
   1515  1.1  riastrad 	mutex_unlock(&adev->firmware.mutex);
   1516  1.1  riastrad 	return -EINVAL;
   1517  1.1  riastrad }
   1518  1.1  riastrad 
   1519  1.1  riastrad static int psp_hw_fini(void *handle)
   1520  1.1  riastrad {
   1521  1.1  riastrad 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
   1522  1.1  riastrad 	struct psp_context *psp = &adev->psp;
   1523  1.1  riastrad 	void *tmr_buf;
   1524  1.1  riastrad 	void **pptr;
   1525  1.1  riastrad 
   1526  1.1  riastrad 	if (adev->gmc.xgmi.num_physical_nodes > 1 &&
   1527  1.1  riastrad 	    psp->xgmi_context.initialized == 1)
   1528  1.1  riastrad                 psp_xgmi_terminate(psp);
   1529  1.1  riastrad 
   1530  1.1  riastrad 	if (psp->adev->psp.ta_fw) {
   1531  1.1  riastrad 		psp_ras_terminate(psp);
   1532  1.1  riastrad 		psp_dtm_terminate(psp);
   1533  1.1  riastrad 		psp_hdcp_terminate(psp);
   1534  1.1  riastrad 	}
   1535  1.1  riastrad 
   1536  1.1  riastrad 	psp_asd_unload(psp);
   1537  1.1  riastrad 
   1538  1.1  riastrad 	psp_ring_destroy(psp, PSP_RING_TYPE__KM);
   1539  1.1  riastrad 
   1540  1.1  riastrad 	pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
   1541  1.1  riastrad 	amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
   1542  1.1  riastrad 	amdgpu_bo_free_kernel(&psp->fw_pri_bo,
   1543  1.1  riastrad 			      &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
   1544  1.1  riastrad 	amdgpu_bo_free_kernel(&psp->fence_buf_bo,
   1545  1.1  riastrad 			      &psp->fence_buf_mc_addr, &psp->fence_buf);
   1546  1.1  riastrad 	amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
   1547  1.1  riastrad 			      (void **)&psp->cmd_buf_mem);
   1548  1.1  riastrad 
   1549  1.1  riastrad 	kfree(psp->cmd);
   1550  1.1  riastrad 	psp->cmd = NULL;
   1551  1.1  riastrad 
   1552  1.1  riastrad 	return 0;
   1553  1.1  riastrad }
   1554  1.1  riastrad 
   1555  1.1  riastrad static int psp_suspend(void *handle)
   1556  1.1  riastrad {
   1557  1.1  riastrad 	int ret;
   1558  1.1  riastrad 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
   1559  1.1  riastrad 	struct psp_context *psp = &adev->psp;
   1560  1.1  riastrad 
   1561  1.1  riastrad 	if (adev->gmc.xgmi.num_physical_nodes > 1 &&
   1562  1.1  riastrad 	    psp->xgmi_context.initialized == 1) {
   1563  1.1  riastrad 		ret = psp_xgmi_terminate(psp);
   1564  1.1  riastrad 		if (ret) {
   1565  1.1  riastrad 			DRM_ERROR("Failed to terminate xgmi ta\n");
   1566  1.1  riastrad 			return ret;
   1567  1.1  riastrad 		}
   1568  1.1  riastrad 	}
   1569  1.1  riastrad 
   1570  1.1  riastrad 	if (psp->adev->psp.ta_fw) {
   1571  1.1  riastrad 		ret = psp_ras_terminate(psp);
   1572  1.1  riastrad 		if (ret) {
   1573  1.1  riastrad 			DRM_ERROR("Failed to terminate ras ta\n");
   1574  1.1  riastrad 			return ret;
   1575  1.1  riastrad 		}
   1576  1.1  riastrad 		ret = psp_hdcp_terminate(psp);
   1577  1.1  riastrad 		if (ret) {
   1578  1.1  riastrad 			DRM_ERROR("Failed to terminate hdcp ta\n");
   1579  1.1  riastrad 			return ret;
   1580  1.1  riastrad 		}
   1581  1.1  riastrad 		ret = psp_dtm_terminate(psp);
   1582  1.1  riastrad 		if (ret) {
   1583  1.1  riastrad 			DRM_ERROR("Failed to terminate dtm ta\n");
   1584  1.1  riastrad 			return ret;
   1585  1.1  riastrad 		}
   1586  1.1  riastrad 	}
   1587  1.1  riastrad 
   1588  1.1  riastrad 	ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
   1589  1.1  riastrad 	if (ret) {
   1590  1.1  riastrad 		DRM_ERROR("PSP ring stop failed\n");
   1591  1.1  riastrad 		return ret;
   1592  1.1  riastrad 	}
   1593  1.1  riastrad 
   1594  1.1  riastrad 	return 0;
   1595  1.1  riastrad }
   1596  1.1  riastrad 
   1597  1.1  riastrad static int psp_resume(void *handle)
   1598  1.1  riastrad {
   1599  1.1  riastrad 	int ret;
   1600  1.1  riastrad 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
   1601  1.1  riastrad 	struct psp_context *psp = &adev->psp;
   1602  1.1  riastrad 
   1603  1.1  riastrad 	DRM_INFO("PSP is resuming...\n");
   1604  1.1  riastrad 
   1605  1.1  riastrad 	ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
   1606  1.1  riastrad 	if (ret) {
   1607  1.1  riastrad 		DRM_ERROR("Failed to process memory training!\n");
   1608  1.1  riastrad 		return ret;
   1609  1.1  riastrad 	}
   1610  1.1  riastrad 
   1611  1.1  riastrad 	mutex_lock(&adev->firmware.mutex);
   1612  1.1  riastrad 
   1613  1.1  riastrad 	ret = psp_hw_start(psp);
   1614  1.1  riastrad 	if (ret)
   1615  1.1  riastrad 		goto failed;
   1616  1.1  riastrad 
   1617  1.1  riastrad 	ret = psp_np_fw_load(psp);
   1618  1.1  riastrad 	if (ret)
   1619  1.1  riastrad 		goto failed;
   1620  1.1  riastrad 
   1621  1.1  riastrad 	ret = psp_asd_load(psp);
   1622  1.1  riastrad 	if (ret) {
   1623  1.1  riastrad 		DRM_ERROR("PSP load asd failed!\n");
   1624  1.1  riastrad 		goto failed;
   1625  1.1  riastrad 	}
   1626  1.1  riastrad 
   1627  1.1  riastrad 	if (adev->gmc.xgmi.num_physical_nodes > 1) {
   1628  1.1  riastrad 		ret = psp_xgmi_initialize(psp);
   1629  1.1  riastrad 		/* Warning the XGMI seesion initialize failure
   1630  1.1  riastrad 		 * Instead of stop driver initialization
   1631  1.1  riastrad 		 */
   1632  1.1  riastrad 		if (ret)
   1633  1.1  riastrad 			dev_err(psp->adev->dev,
   1634  1.1  riastrad 				"XGMI: Failed to initialize XGMI session\n");
   1635  1.1  riastrad 	}
   1636  1.1  riastrad 
   1637  1.1  riastrad 	if (psp->adev->psp.ta_fw) {
   1638  1.1  riastrad 		ret = psp_ras_initialize(psp);
   1639  1.1  riastrad 		if (ret)
   1640  1.1  riastrad 			dev_err(psp->adev->dev,
   1641  1.1  riastrad 					"RAS: Failed to initialize RAS\n");
   1642  1.1  riastrad 
   1643  1.1  riastrad 		ret = psp_hdcp_initialize(psp);
   1644  1.1  riastrad 		if (ret)
   1645  1.1  riastrad 			dev_err(psp->adev->dev,
   1646  1.1  riastrad 				"HDCP: Failed to initialize HDCP\n");
   1647  1.1  riastrad 
   1648  1.1  riastrad 		ret = psp_dtm_initialize(psp);
   1649  1.1  riastrad 		if (ret)
   1650  1.1  riastrad 			dev_err(psp->adev->dev,
   1651  1.1  riastrad 				"DTM: Failed to initialize DTM\n");
   1652  1.1  riastrad 	}
   1653  1.1  riastrad 
   1654  1.1  riastrad 	mutex_unlock(&adev->firmware.mutex);
   1655  1.1  riastrad 
   1656  1.1  riastrad 	return 0;
   1657  1.1  riastrad 
   1658  1.1  riastrad failed:
   1659  1.1  riastrad 	DRM_ERROR("PSP resume failed\n");
   1660  1.1  riastrad 	mutex_unlock(&adev->firmware.mutex);
   1661  1.1  riastrad 	return ret;
   1662  1.1  riastrad }
   1663  1.1  riastrad 
   1664  1.1  riastrad int psp_gpu_reset(struct amdgpu_device *adev)
   1665  1.1  riastrad {
   1666  1.1  riastrad 	int ret;
   1667  1.1  riastrad 
   1668  1.1  riastrad 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
   1669  1.1  riastrad 		return 0;
   1670  1.1  riastrad 
   1671  1.1  riastrad 	mutex_lock(&adev->psp.mutex);
   1672  1.1  riastrad 	ret = psp_mode1_reset(&adev->psp);
   1673  1.1  riastrad 	mutex_unlock(&adev->psp.mutex);
   1674  1.1  riastrad 
   1675  1.1  riastrad 	return ret;
   1676  1.1  riastrad }
   1677  1.1  riastrad 
   1678  1.1  riastrad int psp_rlc_autoload_start(struct psp_context *psp)
   1679  1.1  riastrad {
   1680  1.1  riastrad 	int ret;
   1681  1.1  riastrad 	struct psp_gfx_cmd_resp *cmd;
   1682  1.1  riastrad 
   1683  1.1  riastrad 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
   1684  1.1  riastrad 	if (!cmd)
   1685  1.1  riastrad 		return -ENOMEM;
   1686  1.1  riastrad 
   1687  1.1  riastrad 	cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
   1688  1.1  riastrad 
   1689  1.1  riastrad 	ret = psp_cmd_submit_buf(psp, NULL, cmd,
   1690  1.1  riastrad 				 psp->fence_buf_mc_addr);
   1691  1.1  riastrad 	kfree(cmd);
   1692  1.1  riastrad 	return ret;
   1693  1.1  riastrad }
   1694  1.1  riastrad 
   1695  1.1  riastrad int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
   1696  1.1  riastrad 			uint64_t cmd_gpu_addr, int cmd_size)
   1697  1.1  riastrad {
   1698  1.1  riastrad 	struct amdgpu_firmware_info ucode = {0};
   1699  1.1  riastrad 
   1700  1.1  riastrad 	ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
   1701  1.1  riastrad 		AMDGPU_UCODE_ID_VCN0_RAM;
   1702  1.1  riastrad 	ucode.mc_addr = cmd_gpu_addr;
   1703  1.1  riastrad 	ucode.ucode_size = cmd_size;
   1704  1.1  riastrad 
   1705  1.1  riastrad 	return psp_execute_np_fw_load(&adev->psp, &ucode);
   1706  1.1  riastrad }
   1707  1.1  riastrad 
   1708  1.1  riastrad int psp_ring_cmd_submit(struct psp_context *psp,
   1709  1.1  riastrad 			uint64_t cmd_buf_mc_addr,
   1710  1.1  riastrad 			uint64_t fence_mc_addr,
   1711  1.1  riastrad 			int index)
   1712  1.1  riastrad {
   1713  1.1  riastrad 	unsigned int psp_write_ptr_reg = 0;
   1714  1.1  riastrad 	struct psp_gfx_rb_frame *write_frame;
   1715  1.1  riastrad 	struct psp_ring *ring = &psp->km_ring;
   1716  1.1  riastrad 	struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
   1717  1.1  riastrad 	struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
   1718  1.1  riastrad 		ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
   1719  1.1  riastrad 	struct amdgpu_device *adev = psp->adev;
   1720  1.1  riastrad 	uint32_t ring_size_dw = ring->ring_size / 4;
   1721  1.1  riastrad 	uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
   1722  1.1  riastrad 
   1723  1.1  riastrad 	/* KM (GPCOM) prepare write pointer */
   1724  1.1  riastrad 	psp_write_ptr_reg = psp_ring_get_wptr(psp);
   1725  1.1  riastrad 
   1726  1.1  riastrad 	/* Update KM RB frame pointer to new frame */
   1727  1.1  riastrad 	/* write_frame ptr increments by size of rb_frame in bytes */
   1728  1.1  riastrad 	/* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
   1729  1.1  riastrad 	if ((psp_write_ptr_reg % ring_size_dw) == 0)
   1730  1.1  riastrad 		write_frame = ring_buffer_start;
   1731  1.1  riastrad 	else
   1732  1.1  riastrad 		write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
   1733  1.1  riastrad 	/* Check invalid write_frame ptr address */
   1734  1.1  riastrad 	if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
   1735  1.1  riastrad 		DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
   1736  1.1  riastrad 			  ring_buffer_start, ring_buffer_end, write_frame);
   1737  1.1  riastrad 		DRM_ERROR("write_frame is pointing to address out of bounds\n");
   1738  1.1  riastrad 		return -EINVAL;
   1739  1.1  riastrad 	}
   1740  1.1  riastrad 
   1741  1.1  riastrad 	/* Initialize KM RB frame */
   1742  1.1  riastrad 	memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
   1743  1.1  riastrad 
   1744  1.1  riastrad 	/* Update KM RB frame */
   1745  1.1  riastrad 	write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
   1746  1.1  riastrad 	write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
   1747  1.1  riastrad 	write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
   1748  1.1  riastrad 	write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
   1749  1.1  riastrad 	write_frame->fence_value = index;
   1750  1.1  riastrad 	amdgpu_asic_flush_hdp(adev, NULL);
   1751  1.1  riastrad 
   1752  1.1  riastrad 	/* Update the write Pointer in DWORDs */
   1753  1.1  riastrad 	psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
   1754  1.1  riastrad 	psp_ring_set_wptr(psp, psp_write_ptr_reg);
   1755  1.1  riastrad 	return 0;
   1756  1.1  riastrad }
   1757  1.1  riastrad 
   1758  1.1  riastrad static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
   1759  1.1  riastrad 					enum AMDGPU_UCODE_ID ucode_type)
   1760  1.1  riastrad {
   1761  1.1  riastrad 	struct amdgpu_firmware_info *ucode = NULL;
   1762  1.1  riastrad 
   1763  1.1  riastrad 	if (!adev->firmware.fw_size)
   1764  1.1  riastrad 		return false;
   1765  1.1  riastrad 
   1766  1.1  riastrad 	ucode = &adev->firmware.ucode[ucode_type];
   1767  1.1  riastrad 	if (!ucode->fw || !ucode->ucode_size)
   1768  1.1  riastrad 		return false;
   1769  1.1  riastrad 
   1770  1.1  riastrad 	return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
   1771  1.1  riastrad }
   1772  1.1  riastrad 
   1773  1.1  riastrad static int psp_set_clockgating_state(void *handle,
   1774  1.1  riastrad 				     enum amd_clockgating_state state)
   1775  1.1  riastrad {
   1776  1.1  riastrad 	return 0;
   1777  1.1  riastrad }
   1778  1.1  riastrad 
   1779  1.1  riastrad static int psp_set_powergating_state(void *handle,
   1780  1.1  riastrad 				     enum amd_powergating_state state)
   1781  1.1  riastrad {
   1782  1.1  riastrad 	return 0;
   1783  1.1  riastrad }
   1784  1.1  riastrad 
   1785  1.1  riastrad const struct amd_ip_funcs psp_ip_funcs = {
   1786  1.1  riastrad 	.name = "psp",
   1787  1.1  riastrad 	.early_init = psp_early_init,
   1788  1.1  riastrad 	.late_init = NULL,
   1789  1.1  riastrad 	.sw_init = psp_sw_init,
   1790  1.1  riastrad 	.sw_fini = psp_sw_fini,
   1791  1.1  riastrad 	.hw_init = psp_hw_init,
   1792  1.1  riastrad 	.hw_fini = psp_hw_fini,
   1793  1.1  riastrad 	.suspend = psp_suspend,
   1794  1.1  riastrad 	.resume = psp_resume,
   1795  1.1  riastrad 	.is_idle = NULL,
   1796  1.1  riastrad 	.check_soft_reset = NULL,
   1797  1.1  riastrad 	.wait_for_idle = NULL,
   1798  1.1  riastrad 	.soft_reset = NULL,
   1799  1.1  riastrad 	.set_clockgating_state = psp_set_clockgating_state,
   1800  1.1  riastrad 	.set_powergating_state = psp_set_powergating_state,
   1801  1.1  riastrad };
   1802  1.1  riastrad 
   1803  1.1  riastrad static const struct amdgpu_psp_funcs psp_funcs = {
   1804  1.1  riastrad 	.check_fw_loading_status = psp_check_fw_loading_status,
   1805  1.1  riastrad };
   1806  1.1  riastrad 
   1807  1.1  riastrad static void psp_set_funcs(struct amdgpu_device *adev)
   1808  1.1  riastrad {
   1809  1.1  riastrad 	if (NULL == adev->firmware.funcs)
   1810  1.1  riastrad 		adev->firmware.funcs = &psp_funcs;
   1811  1.1  riastrad }
   1812  1.1  riastrad 
   1813  1.1  riastrad const struct amdgpu_ip_block_version psp_v3_1_ip_block =
   1814  1.1  riastrad {
   1815  1.1  riastrad 	.type = AMD_IP_BLOCK_TYPE_PSP,
   1816  1.1  riastrad 	.major = 3,
   1817  1.1  riastrad 	.minor = 1,
   1818  1.1  riastrad 	.rev = 0,
   1819  1.1  riastrad 	.funcs = &psp_ip_funcs,
   1820  1.1  riastrad };
   1821  1.1  riastrad 
   1822  1.1  riastrad const struct amdgpu_ip_block_version psp_v10_0_ip_block =
   1823  1.1  riastrad {
   1824  1.1  riastrad 	.type = AMD_IP_BLOCK_TYPE_PSP,
   1825  1.1  riastrad 	.major = 10,
   1826  1.1  riastrad 	.minor = 0,
   1827  1.1  riastrad 	.rev = 0,
   1828  1.1  riastrad 	.funcs = &psp_ip_funcs,
   1829  1.1  riastrad };
   1830  1.1  riastrad 
   1831  1.1  riastrad const struct amdgpu_ip_block_version psp_v11_0_ip_block =
   1832  1.1  riastrad {
   1833  1.1  riastrad 	.type = AMD_IP_BLOCK_TYPE_PSP,
   1834  1.1  riastrad 	.major = 11,
   1835  1.1  riastrad 	.minor = 0,
   1836  1.1  riastrad 	.rev = 0,
   1837  1.1  riastrad 	.funcs = &psp_ip_funcs,
   1838  1.1  riastrad };
   1839  1.1  riastrad 
   1840  1.1  riastrad const struct amdgpu_ip_block_version psp_v12_0_ip_block =
   1841  1.1  riastrad {
   1842  1.1  riastrad 	.type = AMD_IP_BLOCK_TYPE_PSP,
   1843  1.1  riastrad 	.major = 12,
   1844  1.1  riastrad 	.minor = 0,
   1845  1.1  riastrad 	.rev = 0,
   1846  1.1  riastrad 	.funcs = &psp_ip_funcs,
   1847  1.1  riastrad };
   1848