amdgpu_gpu_info.c revision d8807b2f
1/*
2 * Copyright © 2014 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
29#include <errno.h>
30#include <string.h>
31
32#include "amdgpu.h"
33#include "amdgpu_drm.h"
34#include "amdgpu_internal.h"
35#include "xf86drm.h"
36
37int amdgpu_query_info(amdgpu_device_handle dev, unsigned info_id,
38		      unsigned size, void *value)
39{
40	struct drm_amdgpu_info request;
41
42	memset(&request, 0, sizeof(request));
43	request.return_pointer = (uintptr_t)value;
44	request.return_size = size;
45	request.query = info_id;
46
47	return drmCommandWrite(dev->fd, DRM_AMDGPU_INFO, &request,
48			       sizeof(struct drm_amdgpu_info));
49}
50
51int amdgpu_query_crtc_from_id(amdgpu_device_handle dev, unsigned id,
52			      int32_t *result)
53{
54	struct drm_amdgpu_info request;
55
56	memset(&request, 0, sizeof(request));
57	request.return_pointer = (uintptr_t)result;
58	request.return_size = sizeof(*result);
59	request.query = AMDGPU_INFO_CRTC_FROM_ID;
60	request.mode_crtc.id = id;
61
62	return drmCommandWrite(dev->fd, DRM_AMDGPU_INFO, &request,
63			       sizeof(struct drm_amdgpu_info));
64}
65
66int amdgpu_read_mm_registers(amdgpu_device_handle dev, unsigned dword_offset,
67			     unsigned count, uint32_t instance, uint32_t flags,
68			     uint32_t *values)
69{
70	struct drm_amdgpu_info request;
71
72	memset(&request, 0, sizeof(request));
73	request.return_pointer = (uintptr_t)values;
74	request.return_size = count * sizeof(uint32_t);
75	request.query = AMDGPU_INFO_READ_MMR_REG;
76	request.read_mmr_reg.dword_offset = dword_offset;
77	request.read_mmr_reg.count = count;
78	request.read_mmr_reg.instance = instance;
79	request.read_mmr_reg.flags = flags;
80
81	return drmCommandWrite(dev->fd, DRM_AMDGPU_INFO, &request,
82			       sizeof(struct drm_amdgpu_info));
83}
84
85int amdgpu_query_hw_ip_count(amdgpu_device_handle dev, unsigned type,
86			     uint32_t *count)
87{
88	struct drm_amdgpu_info request;
89
90	memset(&request, 0, sizeof(request));
91	request.return_pointer = (uintptr_t)count;
92	request.return_size = sizeof(*count);
93	request.query = AMDGPU_INFO_HW_IP_COUNT;
94	request.query_hw_ip.type = type;
95
96	return drmCommandWrite(dev->fd, DRM_AMDGPU_INFO, &request,
97			       sizeof(struct drm_amdgpu_info));
98}
99
100int amdgpu_query_hw_ip_info(amdgpu_device_handle dev, unsigned type,
101			    unsigned ip_instance,
102			    struct drm_amdgpu_info_hw_ip *info)
103{
104	struct drm_amdgpu_info request;
105
106	memset(&request, 0, sizeof(request));
107	request.return_pointer = (uintptr_t)info;
108	request.return_size = sizeof(*info);
109	request.query = AMDGPU_INFO_HW_IP_INFO;
110	request.query_hw_ip.type = type;
111	request.query_hw_ip.ip_instance = ip_instance;
112
113	return drmCommandWrite(dev->fd, DRM_AMDGPU_INFO, &request,
114			       sizeof(struct drm_amdgpu_info));
115}
116
117int amdgpu_query_firmware_version(amdgpu_device_handle dev, unsigned fw_type,
118				  unsigned ip_instance, unsigned index,
119				  uint32_t *version, uint32_t *feature)
120{
121	struct drm_amdgpu_info request;
122	struct drm_amdgpu_info_firmware firmware = {};
123	int r;
124
125	memset(&request, 0, sizeof(request));
126	request.return_pointer = (uintptr_t)&firmware;
127	request.return_size = sizeof(firmware);
128	request.query = AMDGPU_INFO_FW_VERSION;
129	request.query_fw.fw_type = fw_type;
130	request.query_fw.ip_instance = ip_instance;
131	request.query_fw.index = index;
132
133	r = drmCommandWrite(dev->fd, DRM_AMDGPU_INFO, &request,
134			    sizeof(struct drm_amdgpu_info));
135	if (r)
136		return r;
137
138	*version = firmware.ver;
139	*feature = firmware.feature;
140	return 0;
141}
142
143drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev)
144{
145	int r, i;
146
147	r = amdgpu_query_info(dev, AMDGPU_INFO_DEV_INFO, sizeof(dev->dev_info),
148			      &dev->dev_info);
149	if (r)
150		return r;
151
152	dev->info.asic_id = dev->dev_info.device_id;
153	dev->info.chip_rev = dev->dev_info.chip_rev;
154	dev->info.chip_external_rev = dev->dev_info.external_rev;
155	dev->info.family_id = dev->dev_info.family;
156	dev->info.max_engine_clk = dev->dev_info.max_engine_clock;
157	dev->info.max_memory_clk = dev->dev_info.max_memory_clock;
158	dev->info.gpu_counter_freq = dev->dev_info.gpu_counter_freq;
159	dev->info.enabled_rb_pipes_mask = dev->dev_info.enabled_rb_pipes_mask;
160	dev->info.rb_pipes = dev->dev_info.num_rb_pipes;
161	dev->info.ids_flags = dev->dev_info.ids_flags;
162	dev->info.num_hw_gfx_contexts = dev->dev_info.num_hw_gfx_contexts;
163	dev->info.num_shader_engines = dev->dev_info.num_shader_engines;
164	dev->info.num_shader_arrays_per_engine =
165		dev->dev_info.num_shader_arrays_per_engine;
166	dev->info.vram_type = dev->dev_info.vram_type;
167	dev->info.vram_bit_width = dev->dev_info.vram_bit_width;
168	dev->info.ce_ram_size = dev->dev_info.ce_ram_size;
169	dev->info.vce_harvest_config = dev->dev_info.vce_harvest_config;
170	dev->info.pci_rev_id = dev->dev_info.pci_rev;
171
172	if (dev->info.family_id < AMDGPU_FAMILY_AI) {
173		for (i = 0; i < (int)dev->info.num_shader_engines; i++) {
174			unsigned instance = (i << AMDGPU_INFO_MMR_SE_INDEX_SHIFT) |
175					    (AMDGPU_INFO_MMR_SH_INDEX_MASK <<
176					     AMDGPU_INFO_MMR_SH_INDEX_SHIFT);
177
178			r = amdgpu_read_mm_registers(dev, 0x263d, 1, instance, 0,
179						     &dev->info.backend_disable[i]);
180			if (r)
181				return r;
182			/* extract bitfield CC_RB_BACKEND_DISABLE.BACKEND_DISABLE */
183			dev->info.backend_disable[i] =
184				(dev->info.backend_disable[i] >> 16) & 0xff;
185
186			r = amdgpu_read_mm_registers(dev, 0xa0d4, 1, instance, 0,
187						     &dev->info.pa_sc_raster_cfg[i]);
188			if (r)
189				return r;
190
191			if (dev->info.family_id >= AMDGPU_FAMILY_CI) {
192				r = amdgpu_read_mm_registers(dev, 0xa0d5, 1, instance, 0,
193						     &dev->info.pa_sc_raster_cfg1[i]);
194				if (r)
195					return r;
196			}
197		}
198	}
199
200	r = amdgpu_read_mm_registers(dev, 0x263e, 1, 0xffffffff, 0,
201					     &dev->info.gb_addr_cfg);
202	if (r)
203		return r;
204
205	if (dev->info.family_id < AMDGPU_FAMILY_AI) {
206		r = amdgpu_read_mm_registers(dev, 0x2644, 32, 0xffffffff, 0,
207					     dev->info.gb_tile_mode);
208		if (r)
209			return r;
210
211		if (dev->info.family_id >= AMDGPU_FAMILY_CI) {
212			r = amdgpu_read_mm_registers(dev, 0x2664, 16, 0xffffffff, 0,
213						     dev->info.gb_macro_tile_mode);
214			if (r)
215				return r;
216		}
217
218		r = amdgpu_read_mm_registers(dev, 0x9d8, 1, 0xffffffff, 0,
219					     &dev->info.mc_arb_ramcfg);
220		if (r)
221			return r;
222	}
223
224	dev->info.cu_active_number = dev->dev_info.cu_active_number;
225	dev->info.cu_ao_mask = dev->dev_info.cu_ao_mask;
226	memcpy(&dev->info.cu_bitmap[0][0], &dev->dev_info.cu_bitmap[0][0], sizeof(dev->info.cu_bitmap));
227
228	/* TODO: info->max_quad_shader_pipes is not set */
229	/* TODO: info->avail_quad_shader_pipes is not set */
230	/* TODO: info->cache_entries_per_quad_pipe is not set */
231	return 0;
232}
233
234int amdgpu_query_gpu_info(amdgpu_device_handle dev,
235			struct amdgpu_gpu_info *info)
236{
237	if (!dev || !info)
238		return -EINVAL;
239
240	/* Get ASIC info*/
241	*info = dev->info;
242
243	return 0;
244}
245
246int amdgpu_query_heap_info(amdgpu_device_handle dev,
247			uint32_t heap,
248			uint32_t flags,
249			struct amdgpu_heap_info *info)
250{
251	struct drm_amdgpu_info_vram_gtt vram_gtt_info = {};
252	int r;
253
254	r = amdgpu_query_info(dev, AMDGPU_INFO_VRAM_GTT,
255			      sizeof(vram_gtt_info), &vram_gtt_info);
256	if (r)
257		return r;
258
259	/* Get heap information */
260	switch (heap) {
261	case AMDGPU_GEM_DOMAIN_VRAM:
262		/* query visible only vram heap */
263		if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
264			info->heap_size = vram_gtt_info.vram_cpu_accessible_size;
265		else /* query total vram heap */
266			info->heap_size = vram_gtt_info.vram_size;
267
268		info->max_allocation = vram_gtt_info.vram_cpu_accessible_size;
269
270		if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
271			r = amdgpu_query_info(dev, AMDGPU_INFO_VIS_VRAM_USAGE,
272					      sizeof(info->heap_usage),
273					      &info->heap_usage);
274		else
275			r = amdgpu_query_info(dev, AMDGPU_INFO_VRAM_USAGE,
276					      sizeof(info->heap_usage),
277					      &info->heap_usage);
278		if (r)
279			return r;
280		break;
281	case AMDGPU_GEM_DOMAIN_GTT:
282		info->heap_size = vram_gtt_info.gtt_size;
283		info->max_allocation = vram_gtt_info.vram_cpu_accessible_size;
284
285		r = amdgpu_query_info(dev, AMDGPU_INFO_GTT_USAGE,
286				      sizeof(info->heap_usage),
287				      &info->heap_usage);
288		if (r)
289			return r;
290		break;
291	default:
292		return -EINVAL;
293	}
294
295	return 0;
296}
297
298int amdgpu_query_gds_info(amdgpu_device_handle dev,
299			struct amdgpu_gds_resource_info *gds_info)
300{
301	struct drm_amdgpu_info_gds gds_config = {};
302        int r;
303
304	if (!gds_info)
305		return -EINVAL;
306
307        r = amdgpu_query_info(dev, AMDGPU_INFO_GDS_CONFIG,
308                              sizeof(gds_config), &gds_config);
309        if (r)
310                return r;
311
312	gds_info->gds_gfx_partition_size = gds_config.gds_gfx_partition_size;
313	gds_info->compute_partition_size = gds_config.compute_partition_size;
314	gds_info->gds_total_size = gds_config.gds_total_size;
315	gds_info->gws_per_gfx_partition = gds_config.gws_per_gfx_partition;
316	gds_info->gws_per_compute_partition = gds_config.gws_per_compute_partition;
317	gds_info->oa_per_gfx_partition = gds_config.oa_per_gfx_partition;
318	gds_info->oa_per_compute_partition = gds_config.oa_per_compute_partition;
319
320	return 0;
321}
322
323int amdgpu_query_sensor_info(amdgpu_device_handle dev, unsigned sensor_type,
324			     unsigned size, void *value)
325{
326	struct drm_amdgpu_info request;
327
328	memset(&request, 0, sizeof(request));
329	request.return_pointer = (uintptr_t)value;
330	request.return_size = size;
331	request.query = AMDGPU_INFO_SENSOR;
332	request.sensor_info.type = sensor_type;
333
334	return drmCommandWrite(dev->fd, DRM_AMDGPU_INFO, &request,
335			       sizeof(struct drm_amdgpu_info));
336}
337