1 /*
2  * Copyright © 2014 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including
13  * the next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include "libhsakmt.h"
27 #include "linux/kfd_ioctl.h"
28 
hsaKmtGetClockCounters(HSAuint32 NodeId,HsaClockCounters * Counters)29 HSAKMT_STATUS HSAKMTAPI hsaKmtGetClockCounters(HSAuint32 NodeId,
30 					       HsaClockCounters *Counters)
31 {
32 	HSAKMT_STATUS result;
33 	uint32_t gpu_id;
34 	struct kfd_ioctl_get_clock_counters_args args = {0};
35 	int err;
36 
37 	CHECK_KFD_OPEN();
38 
39 	result = validate_nodeid(NodeId, &gpu_id);
40 	if (result != HSAKMT_STATUS_SUCCESS)
41 		return result;
42 
43 	args.gpu_id = gpu_id;
44 
45 	err = kmtIoctl(kfd_fd, AMDKFD_IOC_GET_CLOCK_COUNTERS, &args);
46 	if (err < 0) {
47 		result = HSAKMT_STATUS_ERROR;
48 	} else {
49 		/* At this point the result is already HSAKMT_STATUS_SUCCESS */
50 		Counters->GPUClockCounter = args.gpu_clock_counter;
51 		Counters->CPUClockCounter = args.cpu_clock_counter;
52 		Counters->SystemClockCounter = args.system_clock_counter;
53 		Counters->SystemClockFrequencyHz = args.system_clock_freq;
54 	}
55 
56 	return result;
57 }
58