Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/amdgpu: simplify allocation of scratch regs

The scratch regs are sequential so there's no need to keep
them in an array, we can just return the index of the first
free register + the base register. Also change the array
of bools for keeping track of the free regs to a bitfield.

Reviewed-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Nils Wallménius <nils.wallmenius@gmail.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>

authored by

Nils Wallménius and committed by
Alex Deucher
50261151 cb341a31

+11 -34
+1 -2
drivers/gpu/drm/amd/amdgpu/amdgpu.h
··· 794 794 struct amdgpu_scratch { 795 795 unsigned num_reg; 796 796 uint32_t reg_base; 797 - bool free[32]; 798 - uint32_t reg[32]; 797 + uint32_t free_mask; 799 798 }; 800 799 801 800 /*
+7 -14
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
··· 42 42 { 43 43 int i; 44 44 45 - for (i = 0; i < adev->gfx.scratch.num_reg; i++) { 46 - if (adev->gfx.scratch.free[i]) { 47 - adev->gfx.scratch.free[i] = false; 48 - *reg = adev->gfx.scratch.reg[i]; 49 - return 0; 50 - } 45 + i = ffs(adev->gfx.scratch.free_mask); 46 + if (i != 0 && i <= adev->gfx.scratch.num_reg) { 47 + i--; 48 + adev->gfx.scratch.free_mask &= ~(1u << i); 49 + *reg = adev->gfx.scratch.reg_base + i; 50 + return 0; 51 51 } 52 52 return -EINVAL; 53 53 } ··· 62 62 */ 63 63 void amdgpu_gfx_scratch_free(struct amdgpu_device *adev, uint32_t reg) 64 64 { 65 - int i; 66 - 67 - for (i = 0; i < adev->gfx.scratch.num_reg; i++) { 68 - if (adev->gfx.scratch.reg[i] == reg) { 69 - adev->gfx.scratch.free[i] = true; 70 - return; 71 - } 72 - } 65 + adev->gfx.scratch.free_mask |= 1u << (reg - adev->gfx.scratch.reg_base); 73 66 } 74 67 75 68 /**
+1 -6
drivers/gpu/drm/amd/amdgpu/gfx_v6_0.c
··· 1794 1794 1795 1795 static void gfx_v6_0_scratch_init(struct amdgpu_device *adev) 1796 1796 { 1797 - int i; 1798 - 1799 1797 adev->gfx.scratch.num_reg = 7; 1800 1798 adev->gfx.scratch.reg_base = mmSCRATCH_REG0; 1801 - for (i = 0; i < adev->gfx.scratch.num_reg; i++) { 1802 - adev->gfx.scratch.free[i] = true; 1803 - adev->gfx.scratch.reg[i] = adev->gfx.scratch.reg_base + i; 1804 - } 1799 + adev->gfx.scratch.free_mask = (1u << adev->gfx.scratch.num_reg) - 1; 1805 1800 } 1806 1801 1807 1802 static int gfx_v6_0_ring_test_ring(struct amdgpu_ring *ring)
+1 -6
drivers/gpu/drm/amd/amdgpu/gfx_v7_0.c
··· 2003 2003 */ 2004 2004 static void gfx_v7_0_scratch_init(struct amdgpu_device *adev) 2005 2005 { 2006 - int i; 2007 - 2008 2006 adev->gfx.scratch.num_reg = 7; 2009 2007 adev->gfx.scratch.reg_base = mmSCRATCH_REG0; 2010 - for (i = 0; i < adev->gfx.scratch.num_reg; i++) { 2011 - adev->gfx.scratch.free[i] = true; 2012 - adev->gfx.scratch.reg[i] = adev->gfx.scratch.reg_base + i; 2013 - } 2008 + adev->gfx.scratch.free_mask = (1u << adev->gfx.scratch.num_reg) - 1; 2014 2009 } 2015 2010 2016 2011 /**
+1 -6
drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c
··· 749 749 750 750 static void gfx_v8_0_scratch_init(struct amdgpu_device *adev) 751 751 { 752 - int i; 753 - 754 752 adev->gfx.scratch.num_reg = 7; 755 753 adev->gfx.scratch.reg_base = mmSCRATCH_REG0; 756 - for (i = 0; i < adev->gfx.scratch.num_reg; i++) { 757 - adev->gfx.scratch.free[i] = true; 758 - adev->gfx.scratch.reg[i] = adev->gfx.scratch.reg_base + i; 759 - } 754 + adev->gfx.scratch.free_mask = (1u << adev->gfx.scratch.num_reg) - 1; 760 755 } 761 756 762 757 static int gfx_v8_0_ring_test_ring(struct amdgpu_ring *ring)