radv_amdgpu_winsys.c 6.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * Copyright © 2016 Red Hat.
 * Copyright © 2016 Bas Nieuwenhuizen
 * based on amdgpu winsys.
 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
 * Copyright © 2015 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */
#include "radv_amdgpu_winsys.h"
#include "radv_amdgpu_winsys_public.h"
#include "radv_amdgpu_surface.h"
30
#include "radv_debug.h"
Nicolai Hähnle's avatar
Nicolai Hähnle committed
31
#include "ac_surface.h"
32
33
34
35
36
37
38
39
40
#include "xf86drm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <amdgpu_drm.h>
#include <assert.h>
#include "radv_amdgpu_cs.h"
#include "radv_amdgpu_bo.h"
#include "radv_amdgpu_surface.h"
41

42
43
44
static bool
do_winsys_init(struct radv_amdgpu_winsys *ws, int fd)
{
Nicolai Hähnle's avatar
Nicolai Hähnle committed
45
	if (!ac_query_gpu_info(fd, ws->dev, &ws->info, &ws->amdinfo))
Nicolai Hähnle's avatar
Nicolai Hähnle committed
46
		return false;
47

Nicolai Hähnle's avatar
Nicolai Hähnle committed
48
49
50
	if (ws->info.chip_class > GFX9)
		return false;

51
52
53
54
	/* temporary */
	ws->info.use_display_dcc_unaligned = false;
	ws->info.use_display_dcc_with_retile_blit = false;

55
	ws->addrlib = amdgpu_addr_create(&ws->info, &ws->amdinfo, &ws->info.max_alignment);
56
57
	if (!ws->addrlib) {
		fprintf(stderr, "amdgpu: Cannot create addrlib.\n");
Nicolai Hähnle's avatar
Nicolai Hähnle committed
58
		return false;
59
	}
60

Nicolai Hähnle's avatar
Nicolai Hähnle committed
61
62
	ws->info.num_sdma_rings = MIN2(ws->info.num_sdma_rings, MAX_RINGS_PER_TYPE);
	ws->info.num_compute_rings = MIN2(ws->info.num_compute_rings, MAX_RINGS_PER_TYPE);
63

64
	ws->use_ib_bos = ws->info.chip_class >= GFX7;
65
66
67
68
69
70
71
72
73
	return true;
}

static void radv_amdgpu_winsys_query_info(struct radeon_winsys *rws,
                                     struct radeon_info *info)
{
	*info = ((struct radv_amdgpu_winsys *)rws)->info;
}

74
75
76
77
78
79
80
81
static uint64_t radv_amdgpu_winsys_query_value(struct radeon_winsys *rws,
					       enum radeon_value_id value)
{
	struct radv_amdgpu_winsys *ws = (struct radv_amdgpu_winsys *)rws;
	struct amdgpu_heap_info heap;
	uint64_t retval = 0;

	switch (value) {
82
83
84
85
86
87
	case RADEON_ALLOCATED_VRAM:
		return ws->allocated_vram;
	case RADEON_ALLOCATED_VRAM_VIS:
		return ws->allocated_vram_vis;
	case RADEON_ALLOCATED_GTT:
		return ws->allocated_gtt;
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
	case RADEON_TIMESTAMP:
		amdgpu_query_info(ws->dev, AMDGPU_INFO_TIMESTAMP, 8, &retval);
		return retval;
	case RADEON_NUM_BYTES_MOVED:
		amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_BYTES_MOVED,
				  8, &retval);
		return retval;
	case RADEON_NUM_EVICTIONS:
		amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_EVICTIONS,
				  8, &retval);
		return retval;
	case RADEON_NUM_VRAM_CPU_PAGE_FAULTS:
		amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS,
				  8, &retval);
		return retval;
	case RADEON_VRAM_USAGE:
		amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_VRAM,
				       0, &heap);
		return heap.heap_usage;
	case RADEON_VRAM_VIS_USAGE:
		amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_VRAM,
				       AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
				       &heap);
		return heap.heap_usage;
	case RADEON_GTT_USAGE:
		amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_GTT,
				       0, &heap);
		return heap.heap_usage;
	case RADEON_GPU_TEMPERATURE:
		amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GPU_TEMP,
					 4, &retval);
		return retval;
	case RADEON_CURRENT_SCLK:
		amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GFX_SCLK,
					 4, &retval);
		return retval;
	case RADEON_CURRENT_MCLK:
		amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GFX_MCLK,
					 4, &retval);
		return retval;
	default:
		unreachable("invalid query value");
	}

	return 0;
}

135
136
137
138
139
140
141
142
143
144
static bool radv_amdgpu_winsys_read_registers(struct radeon_winsys *rws,
					      unsigned reg_offset,
					      unsigned num_registers, uint32_t *out)
{
	struct radv_amdgpu_winsys *ws = (struct radv_amdgpu_winsys*)rws;

	return amdgpu_read_mm_registers(ws->dev, reg_offset / 4, num_registers,
					0xffffffff, 0, out) == 0;
}

145
146
147
148
149
150
151
static const char *radv_amdgpu_winsys_get_chip_name(struct radeon_winsys *rws)
{
	amdgpu_device_handle dev = ((struct radv_amdgpu_winsys *)rws)->dev;

	return amdgpu_get_marketing_name(dev);
}

152
153
154
155
156
157
158
159
160
161
static void radv_amdgpu_winsys_destroy(struct radeon_winsys *rws)
{
	struct radv_amdgpu_winsys *ws = (struct radv_amdgpu_winsys*)rws;

	AddrDestroy(ws->addrlib);
	amdgpu_device_deinitialize(ws->dev);
	FREE(rws);
}

struct radeon_winsys *
162
radv_amdgpu_winsys_create(int fd, uint64_t debug_flags, uint64_t perftest_flags)
163
164
165
166
167
168
169
170
171
172
173
{
	uint32_t drm_major, drm_minor, r;
	amdgpu_device_handle dev;
	struct radv_amdgpu_winsys *ws;

	r = amdgpu_device_initialize(fd, &drm_major, &drm_minor, &dev);
	if (r)
		return NULL;

	ws = calloc(1, sizeof(struct radv_amdgpu_winsys));
	if (!ws)
174
		goto fail;
175
176
177
178
179

	ws->dev = dev;
	ws->info.drm_major = drm_major;
	ws->info.drm_minor = drm_minor;
	if (!do_winsys_init(ws, fd))
180
		goto winsys_fail;
181

182
183
184
185
	ws->debug_all_bos = !!(debug_flags & RADV_DEBUG_ALL_BOS);
	if (debug_flags & RADV_DEBUG_NO_IBS)
		ws->use_ib_bos = false;

186
	ws->use_local_bos = perftest_flags & RADV_PERFTEST_LOCAL_BOS;
187
	ws->zero_all_vram_allocs = debug_flags & RADV_DEBUG_ZERO_VRAM;
188
	ws->batchchain = !(perftest_flags & RADV_PERFTEST_NO_BATCHCHAIN);
189
190
191
	LIST_INITHEAD(&ws->global_bo_list);
	pthread_mutex_init(&ws->global_bo_list_lock, NULL);
	ws->base.query_info = radv_amdgpu_winsys_query_info;
192
	ws->base.query_value = radv_amdgpu_winsys_query_value;
193
	ws->base.read_registers = radv_amdgpu_winsys_read_registers;
194
	ws->base.get_chip_name = radv_amdgpu_winsys_get_chip_name;
195
196
197
198
	ws->base.destroy = radv_amdgpu_winsys_destroy;
	radv_amdgpu_bo_init_functions(ws);
	radv_amdgpu_cs_init_functions(ws);
	radv_amdgpu_surface_init_functions(ws);
199

200
	return &ws->base;
201
202
203

winsys_fail:
	free(ws);
204
fail:
205
	amdgpu_device_deinitialize(dev);
206
207
	return NULL;
}