• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
2 /*
3  *
4  * (C) COPYRIGHT 2020-2021 ARM Limited. All rights reserved.
5  *
6  * This program is free software and is provided to you under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation, and any use by you of this program is subject to the terms
9  * of such GNU license.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you can access it online at
18  * http://www.gnu.org/licenses/gpl-2.0.html.
19  *
20  */
21 
22 #include "mali_kbase_ccswe.h"
23 #include "mali_kbase_linux.h"
24 
25 #include <linux/math64.h>
26 #include <linux/time.h>
27 
kbasep_ccswe_cycle_at_no_lock(struct kbase_ccswe * self,u64 timestamp_ns)28 static u64 kbasep_ccswe_cycle_at_no_lock(
29 	struct kbase_ccswe *self, u64 timestamp_ns)
30 {
31 	s64 diff_s, diff_ns;
32 	u32 gpu_freq;
33 
34 	lockdep_assert_held(&self->access);
35 
36 	diff_ns = timestamp_ns - self->timestamp_ns;
37 	gpu_freq = diff_ns > 0 ? self->gpu_freq : self->prev_gpu_freq;
38 
39 	diff_s = div_s64(diff_ns, NSEC_PER_SEC);
40 	diff_ns -= diff_s * NSEC_PER_SEC;
41 
42 	return self->cycles_elapsed + diff_s * gpu_freq
43 		+ div_s64(diff_ns * gpu_freq, NSEC_PER_SEC);
44 }
45 
kbase_ccswe_init(struct kbase_ccswe * self)46 void kbase_ccswe_init(struct kbase_ccswe *self)
47 {
48 	memset(self, 0, sizeof(*self));
49 
50 	spin_lock_init(&self->access);
51 }
52 
kbase_ccswe_cycle_at(struct kbase_ccswe * self,u64 timestamp_ns)53 u64 kbase_ccswe_cycle_at(struct kbase_ccswe *self, u64 timestamp_ns)
54 {
55 	unsigned long flags;
56 	u64 result;
57 
58 	spin_lock_irqsave(&self->access, flags);
59 	result = kbasep_ccswe_cycle_at_no_lock(self, timestamp_ns);
60 	spin_unlock_irqrestore(&self->access, flags);
61 
62 	return result;
63 }
64 
kbase_ccswe_freq_change(struct kbase_ccswe * self,u64 timestamp_ns,u32 gpu_freq)65 void kbase_ccswe_freq_change(
66 	struct kbase_ccswe *self, u64 timestamp_ns, u32 gpu_freq)
67 {
68 	unsigned long flags;
69 
70 	spin_lock_irqsave(&self->access, flags);
71 
72 	/* The time must go only forward. */
73 	if (WARN_ON(timestamp_ns < self->timestamp_ns))
74 		goto exit;
75 
76 	/* If this is the first frequency change, cycles_elapsed is zero. */
77 	if (self->timestamp_ns)
78 		self->cycles_elapsed = kbasep_ccswe_cycle_at_no_lock(
79 			self, timestamp_ns);
80 
81 	self->timestamp_ns = timestamp_ns;
82 	self->prev_gpu_freq = self->gpu_freq;
83 	self->gpu_freq = gpu_freq;
84 exit:
85 	spin_unlock_irqrestore(&self->access, flags);
86 }
87 
kbase_ccswe_reset(struct kbase_ccswe * self)88 void kbase_ccswe_reset(struct kbase_ccswe *self)
89 {
90 	unsigned long flags;
91 
92 	spin_lock_irqsave(&self->access, flags);
93 
94 	self->timestamp_ns = 0;
95 	self->cycles_elapsed = 0;
96 	self->gpu_freq = 0;
97 	self->prev_gpu_freq = 0;
98 
99 	spin_unlock_irqrestore(&self->access, flags);
100 }
101