xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_gfx.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: amdgpu_gfx.c,v 1.3 2018/08/27 14:04:50 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2014 Advanced Micro Devices, Inc.
5  * Copyright 2008 Red Hat Inc.
6  * Copyright 2009 Jerome Glisse.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  * OTHER DEALINGS IN THE SOFTWARE.
25  *
26  */
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: amdgpu_gfx.c,v 1.3 2018/08/27 14:04:50 riastradh Exp $");
29 
30 #include <drm/drmP.h>
31 #include "amdgpu.h"
32 #include "amdgpu_gfx.h"
33 
34 /*
35  * GPU scratch registers helpers function.
36  */
37 /**
38  * amdgpu_gfx_scratch_get - Allocate a scratch register
39  *
40  * @adev: amdgpu_device pointer
41  * @reg: scratch register mmio offset
42  *
43  * Allocate a CP scratch register for use by the driver (all asics).
44  * Returns 0 on success or -EINVAL on failure.
45  */
46 int amdgpu_gfx_scratch_get(struct amdgpu_device *adev, uint32_t *reg)
47 {
48 	int i;
49 
50 	for (i = 0; i < adev->gfx.scratch.num_reg; i++) {
51 		if (adev->gfx.scratch.free[i]) {
52 			adev->gfx.scratch.free[i] = false;
53 			*reg = adev->gfx.scratch.reg[i];
54 			return 0;
55 		}
56 	}
57 	return -EINVAL;
58 }
59 
60 /**
61  * amdgpu_gfx_scratch_free - Free a scratch register
62  *
63  * @adev: amdgpu_device pointer
64  * @reg: scratch register mmio offset
65  *
66  * Free a CP scratch register allocated for use by the driver (all asics)
67  */
68 void amdgpu_gfx_scratch_free(struct amdgpu_device *adev, uint32_t reg)
69 {
70 	int i;
71 
72 	for (i = 0; i < adev->gfx.scratch.num_reg; i++) {
73 		if (adev->gfx.scratch.reg[i] == reg) {
74 			adev->gfx.scratch.free[i] = true;
75 			return;
76 		}
77 	}
78 }
79