Home | History | Annotate | Line # | Download | only in amdgpu
      1  1.3  riastrad /*	$NetBSD: amdgpu_rlc.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 2014 Advanced Micro Devices, Inc.
      5  1.1  riastrad  * Copyright 2008 Red Hat Inc.
      6  1.1  riastrad  * Copyright 2009 Jerome Glisse.
      7  1.1  riastrad  *
      8  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      9  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
     10  1.1  riastrad  * to deal in the Software without restriction, including without limitation
     11  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     12  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     13  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     14  1.1  riastrad  *
     15  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     16  1.1  riastrad  * all copies or substantial portions of the Software.
     17  1.1  riastrad  *
     18  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     22  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     23  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     24  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     25  1.1  riastrad  *
     26  1.1  riastrad  */
     27  1.1  riastrad #include <sys/cdefs.h>
     28  1.3  riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpu_rlc.c,v 1.3 2021/12/19 12:21:29 riastradh Exp $");
     29  1.1  riastrad 
     30  1.1  riastrad #include <linux/firmware.h>
     31  1.1  riastrad #include "amdgpu.h"
     32  1.1  riastrad #include "amdgpu_gfx.h"
     33  1.1  riastrad #include "amdgpu_rlc.h"
     34  1.1  riastrad 
     35  1.1  riastrad /**
     36  1.1  riastrad  * amdgpu_gfx_rlc_enter_safe_mode - Set RLC into safe mode
     37  1.1  riastrad  *
     38  1.1  riastrad  * @adev: amdgpu_device pointer
     39  1.1  riastrad  *
     40  1.1  riastrad  * Set RLC enter into safe mode if RLC is enabled and haven't in safe mode.
     41  1.1  riastrad  */
     42  1.1  riastrad void amdgpu_gfx_rlc_enter_safe_mode(struct amdgpu_device *adev)
     43  1.1  riastrad {
     44  1.1  riastrad 	if (adev->gfx.rlc.in_safe_mode)
     45  1.1  riastrad 		return;
     46  1.1  riastrad 
     47  1.1  riastrad 	/* if RLC is not enabled, do nothing */
     48  1.1  riastrad 	if (!adev->gfx.rlc.funcs->is_rlc_enabled(adev))
     49  1.1  riastrad 		return;
     50  1.1  riastrad 
     51  1.1  riastrad 	if (adev->cg_flags &
     52  1.1  riastrad 	    (AMD_CG_SUPPORT_GFX_CGCG | AMD_CG_SUPPORT_GFX_MGCG |
     53  1.1  riastrad 	     AMD_CG_SUPPORT_GFX_3D_CGCG)) {
     54  1.1  riastrad 		adev->gfx.rlc.funcs->set_safe_mode(adev);
     55  1.1  riastrad 		adev->gfx.rlc.in_safe_mode = true;
     56  1.1  riastrad 	}
     57  1.1  riastrad }
     58  1.1  riastrad 
     59  1.1  riastrad /**
     60  1.1  riastrad  * amdgpu_gfx_rlc_exit_safe_mode - Set RLC out of safe mode
     61  1.1  riastrad  *
     62  1.1  riastrad  * @adev: amdgpu_device pointer
     63  1.1  riastrad  *
     64  1.1  riastrad  * Set RLC exit safe mode if RLC is enabled and have entered into safe mode.
     65  1.1  riastrad  */
     66  1.1  riastrad void amdgpu_gfx_rlc_exit_safe_mode(struct amdgpu_device *adev)
     67  1.1  riastrad {
     68  1.1  riastrad 	if (!(adev->gfx.rlc.in_safe_mode))
     69  1.1  riastrad 		return;
     70  1.1  riastrad 
     71  1.1  riastrad 	/* if RLC is not enabled, do nothing */
     72  1.1  riastrad 	if (!adev->gfx.rlc.funcs->is_rlc_enabled(adev))
     73  1.1  riastrad 		return;
     74  1.1  riastrad 
     75  1.1  riastrad 	if (adev->cg_flags &
     76  1.1  riastrad 	    (AMD_CG_SUPPORT_GFX_CGCG | AMD_CG_SUPPORT_GFX_MGCG |
     77  1.1  riastrad 	     AMD_CG_SUPPORT_GFX_3D_CGCG)) {
     78  1.1  riastrad 		adev->gfx.rlc.funcs->unset_safe_mode(adev);
     79  1.1  riastrad 		adev->gfx.rlc.in_safe_mode = false;
     80  1.1  riastrad 	}
     81  1.1  riastrad }
     82  1.1  riastrad 
     83  1.1  riastrad /**
     84  1.1  riastrad  * amdgpu_gfx_rlc_init_sr - Init save restore block
     85  1.1  riastrad  *
     86  1.1  riastrad  * @adev: amdgpu_device pointer
     87  1.1  riastrad  * @dws: the size of save restore block
     88  1.1  riastrad  *
     89  1.1  riastrad  * Allocate and setup value to save restore block of rlc.
     90  1.1  riastrad  * Returns 0 on succeess or negative error code if allocate failed.
     91  1.1  riastrad  */
     92  1.1  riastrad int amdgpu_gfx_rlc_init_sr(struct amdgpu_device *adev, u32 dws)
     93  1.1  riastrad {
     94  1.1  riastrad 	const u32 *src_ptr;
     95  1.1  riastrad 	volatile u32 *dst_ptr;
     96  1.1  riastrad 	u32 i;
     97  1.1  riastrad 	int r;
     98  1.1  riastrad 
     99  1.1  riastrad 	/* allocate save restore block */
    100  1.1  riastrad 	r = amdgpu_bo_create_reserved(adev, dws * 4, PAGE_SIZE,
    101  1.1  riastrad 				      AMDGPU_GEM_DOMAIN_VRAM,
    102  1.1  riastrad 				      &adev->gfx.rlc.save_restore_obj,
    103  1.1  riastrad 				      &adev->gfx.rlc.save_restore_gpu_addr,
    104  1.3  riastrad 				      (void **)__UNVOLATILE(&adev->gfx.rlc.sr_ptr));
    105  1.1  riastrad 	if (r) {
    106  1.1  riastrad 		dev_warn(adev->dev, "(%d) create RLC sr bo failed\n", r);
    107  1.1  riastrad 		amdgpu_gfx_rlc_fini(adev);
    108  1.1  riastrad 		return r;
    109  1.1  riastrad 	}
    110  1.1  riastrad 
    111  1.1  riastrad 	/* write the sr buffer */
    112  1.1  riastrad 	src_ptr = adev->gfx.rlc.reg_list;
    113  1.1  riastrad 	dst_ptr = adev->gfx.rlc.sr_ptr;
    114  1.1  riastrad 	for (i = 0; i < adev->gfx.rlc.reg_list_size; i++)
    115  1.1  riastrad 		dst_ptr[i] = cpu_to_le32(src_ptr[i]);
    116  1.1  riastrad 	amdgpu_bo_kunmap(adev->gfx.rlc.save_restore_obj);
    117  1.1  riastrad 	amdgpu_bo_unreserve(adev->gfx.rlc.save_restore_obj);
    118  1.1  riastrad 
    119  1.1  riastrad 	return 0;
    120  1.1  riastrad }
    121  1.1  riastrad 
    122  1.1  riastrad /**
    123  1.1  riastrad  * amdgpu_gfx_rlc_init_csb - Init clear state block
    124  1.1  riastrad  *
    125  1.1  riastrad  * @adev: amdgpu_device pointer
    126  1.1  riastrad  *
    127  1.1  riastrad  * Allocate and setup value to clear state block of rlc.
    128  1.1  riastrad  * Returns 0 on succeess or negative error code if allocate failed.
    129  1.1  riastrad  */
    130  1.1  riastrad int amdgpu_gfx_rlc_init_csb(struct amdgpu_device *adev)
    131  1.1  riastrad {
    132  1.1  riastrad 	u32 dws;
    133  1.1  riastrad 	int r;
    134  1.1  riastrad 
    135  1.1  riastrad 	/* allocate clear state block */
    136  1.1  riastrad 	adev->gfx.rlc.clear_state_size = dws = adev->gfx.rlc.funcs->get_csb_size(adev);
    137  1.1  riastrad 	r = amdgpu_bo_create_kernel(adev, dws * 4, PAGE_SIZE,
    138  1.1  riastrad 				      AMDGPU_GEM_DOMAIN_VRAM,
    139  1.1  riastrad 				      &adev->gfx.rlc.clear_state_obj,
    140  1.1  riastrad 				      &adev->gfx.rlc.clear_state_gpu_addr,
    141  1.3  riastrad 				      (void **)__UNVOLATILE(&adev->gfx.rlc.cs_ptr));
    142  1.1  riastrad 	if (r) {
    143  1.1  riastrad 		dev_err(adev->dev, "(%d) failed to create rlc csb bo\n", r);
    144  1.1  riastrad 		amdgpu_gfx_rlc_fini(adev);
    145  1.1  riastrad 		return r;
    146  1.1  riastrad 	}
    147  1.1  riastrad 
    148  1.1  riastrad 	return 0;
    149  1.1  riastrad }
    150  1.1  riastrad 
    151  1.1  riastrad /**
    152  1.1  riastrad  * amdgpu_gfx_rlc_init_cpt - Init cp table
    153  1.1  riastrad  *
    154  1.1  riastrad  * @adev: amdgpu_device pointer
    155  1.1  riastrad  *
    156  1.1  riastrad  * Allocate and setup value to cp table of rlc.
    157  1.1  riastrad  * Returns 0 on succeess or negative error code if allocate failed.
    158  1.1  riastrad  */
    159  1.1  riastrad int amdgpu_gfx_rlc_init_cpt(struct amdgpu_device *adev)
    160  1.1  riastrad {
    161  1.1  riastrad 	int r;
    162  1.1  riastrad 
    163  1.1  riastrad 	r = amdgpu_bo_create_reserved(adev, adev->gfx.rlc.cp_table_size,
    164  1.1  riastrad 				      PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
    165  1.1  riastrad 				      &adev->gfx.rlc.cp_table_obj,
    166  1.1  riastrad 				      &adev->gfx.rlc.cp_table_gpu_addr,
    167  1.3  riastrad 				      (void **)__UNVOLATILE(&adev->gfx.rlc.cp_table_ptr));
    168  1.1  riastrad 	if (r) {
    169  1.1  riastrad 		dev_err(adev->dev, "(%d) failed to create cp table bo\n", r);
    170  1.1  riastrad 		amdgpu_gfx_rlc_fini(adev);
    171  1.1  riastrad 		return r;
    172  1.1  riastrad 	}
    173  1.1  riastrad 
    174  1.1  riastrad 	/* set up the cp table */
    175  1.1  riastrad 	amdgpu_gfx_rlc_setup_cp_table(adev);
    176  1.1  riastrad 	amdgpu_bo_kunmap(adev->gfx.rlc.cp_table_obj);
    177  1.1  riastrad 	amdgpu_bo_unreserve(adev->gfx.rlc.cp_table_obj);
    178  1.1  riastrad 
    179  1.1  riastrad 	return 0;
    180  1.1  riastrad }
    181  1.1  riastrad 
    182  1.1  riastrad /**
    183  1.1  riastrad  * amdgpu_gfx_rlc_setup_cp_table - setup cp the buffer of cp table
    184  1.1  riastrad  *
    185  1.1  riastrad  * @adev: amdgpu_device pointer
    186  1.1  riastrad  *
    187  1.1  riastrad  * Write cp firmware data into cp table.
    188  1.1  riastrad  */
    189  1.1  riastrad void amdgpu_gfx_rlc_setup_cp_table(struct amdgpu_device *adev)
    190  1.1  riastrad {
    191  1.1  riastrad 	const __le32 *fw_data;
    192  1.1  riastrad 	volatile u32 *dst_ptr;
    193  1.1  riastrad 	int me, i, max_me;
    194  1.1  riastrad 	u32 bo_offset = 0;
    195  1.1  riastrad 	u32 table_offset, table_size;
    196  1.1  riastrad 
    197  1.1  riastrad 	max_me = adev->gfx.rlc.funcs->get_cp_table_num(adev);
    198  1.1  riastrad 
    199  1.1  riastrad 	/* write the cp table buffer */
    200  1.1  riastrad 	dst_ptr = adev->gfx.rlc.cp_table_ptr;
    201  1.1  riastrad 	for (me = 0; me < max_me; me++) {
    202  1.1  riastrad 		if (me == 0) {
    203  1.1  riastrad 			const struct gfx_firmware_header_v1_0 *hdr =
    204  1.1  riastrad 				(const struct gfx_firmware_header_v1_0 *)adev->gfx.ce_fw->data;
    205  1.1  riastrad 			fw_data = (const __le32 *)
    206  1.1  riastrad 				(adev->gfx.ce_fw->data +
    207  1.1  riastrad 				 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
    208  1.1  riastrad 			table_offset = le32_to_cpu(hdr->jt_offset);
    209  1.1  riastrad 			table_size = le32_to_cpu(hdr->jt_size);
    210  1.1  riastrad 		} else if (me == 1) {
    211  1.1  riastrad 			const struct gfx_firmware_header_v1_0 *hdr =
    212  1.1  riastrad 				(const struct gfx_firmware_header_v1_0 *)adev->gfx.pfp_fw->data;
    213  1.1  riastrad 			fw_data = (const __le32 *)
    214  1.1  riastrad 				(adev->gfx.pfp_fw->data +
    215  1.1  riastrad 				 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
    216  1.1  riastrad 			table_offset = le32_to_cpu(hdr->jt_offset);
    217  1.1  riastrad 			table_size = le32_to_cpu(hdr->jt_size);
    218  1.1  riastrad 		} else if (me == 2) {
    219  1.1  riastrad 			const struct gfx_firmware_header_v1_0 *hdr =
    220  1.1  riastrad 				(const struct gfx_firmware_header_v1_0 *)adev->gfx.me_fw->data;
    221  1.1  riastrad 			fw_data = (const __le32 *)
    222  1.1  riastrad 				(adev->gfx.me_fw->data +
    223  1.1  riastrad 				 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
    224  1.1  riastrad 			table_offset = le32_to_cpu(hdr->jt_offset);
    225  1.1  riastrad 			table_size = le32_to_cpu(hdr->jt_size);
    226  1.1  riastrad 		} else if (me == 3) {
    227  1.1  riastrad 			const struct gfx_firmware_header_v1_0 *hdr =
    228  1.1  riastrad 				(const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
    229  1.1  riastrad 			fw_data = (const __le32 *)
    230  1.1  riastrad 				(adev->gfx.mec_fw->data +
    231  1.1  riastrad 				 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
    232  1.1  riastrad 			table_offset = le32_to_cpu(hdr->jt_offset);
    233  1.1  riastrad 			table_size = le32_to_cpu(hdr->jt_size);
    234  1.1  riastrad 		} else  if (me == 4) {
    235  1.1  riastrad 			const struct gfx_firmware_header_v1_0 *hdr =
    236  1.1  riastrad 				(const struct gfx_firmware_header_v1_0 *)adev->gfx.mec2_fw->data;
    237  1.1  riastrad 			fw_data = (const __le32 *)
    238  1.1  riastrad 				(adev->gfx.mec2_fw->data +
    239  1.1  riastrad 				 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
    240  1.1  riastrad 			table_offset = le32_to_cpu(hdr->jt_offset);
    241  1.1  riastrad 			table_size = le32_to_cpu(hdr->jt_size);
    242  1.1  riastrad 		}
    243  1.1  riastrad 
    244  1.1  riastrad 		for (i = 0; i < table_size; i ++) {
    245  1.1  riastrad 			dst_ptr[bo_offset + i] =
    246  1.1  riastrad 				cpu_to_le32(le32_to_cpu(fw_data[table_offset + i]));
    247  1.1  riastrad 		}
    248  1.1  riastrad 
    249  1.1  riastrad 		bo_offset += table_size;
    250  1.1  riastrad 	}
    251  1.1  riastrad }
    252  1.1  riastrad 
    253  1.1  riastrad /**
    254  1.1  riastrad  * amdgpu_gfx_rlc_fini - Free BO which used for RLC
    255  1.1  riastrad  *
    256  1.1  riastrad  * @adev: amdgpu_device pointer
    257  1.1  riastrad  *
    258  1.1  riastrad  * Free three BO which is used for rlc_save_restore_block, rlc_clear_state_block
    259  1.1  riastrad  * and rlc_jump_table_block.
    260  1.1  riastrad  */
    261  1.1  riastrad void amdgpu_gfx_rlc_fini(struct amdgpu_device *adev)
    262  1.1  riastrad {
    263  1.1  riastrad 	/* save restore block */
    264  1.1  riastrad 	if (adev->gfx.rlc.save_restore_obj) {
    265  1.1  riastrad 		amdgpu_bo_free_kernel(&adev->gfx.rlc.save_restore_obj,
    266  1.1  riastrad 				      &adev->gfx.rlc.save_restore_gpu_addr,
    267  1.3  riastrad 				      (void **)__UNVOLATILE(&adev->gfx.rlc.sr_ptr));
    268  1.1  riastrad 	}
    269  1.1  riastrad 
    270  1.1  riastrad 	/* clear state block */
    271  1.1  riastrad 	amdgpu_bo_free_kernel(&adev->gfx.rlc.clear_state_obj,
    272  1.1  riastrad 			      &adev->gfx.rlc.clear_state_gpu_addr,
    273  1.3  riastrad 			      (void **)__UNVOLATILE(&adev->gfx.rlc.cs_ptr));
    274  1.1  riastrad 
    275  1.1  riastrad 	/* jump table block */
    276  1.1  riastrad 	amdgpu_bo_free_kernel(&adev->gfx.rlc.cp_table_obj,
    277  1.1  riastrad 			      &adev->gfx.rlc.cp_table_gpu_addr,
    278  1.3  riastrad 			      (void **)__UNVOLATILE(&adev->gfx.rlc.cp_table_ptr));
    279  1.1  riastrad }
    280