hc
2023-12-06 d38611ca164021d018c1b23eee65bbebc09c63e0
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
/*
 *
 * (C) COPYRIGHT 2014-2023 ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can access it online at
 * http://www.gnu.org/licenses/gpl-2.0.html.
 *
 */
 
#include <mali_kbase.h>
#include <mali_kbase_hwaccess_time.h>
#if MALI_USE_CSF
#include <asm/arch_timer.h>
#include <linux/gcd.h>
#include <csf/mali_kbase_csf_timeout.h>
#endif
#include <device/mali_kbase_device.h>
#include <backend/gpu/mali_kbase_pm_internal.h>
#include <mali_kbase_config_defaults.h>
 
void kbase_backend_get_gpu_time_norequest(struct kbase_device *kbdev,
                     u64 *cycle_counter,
                     u64 *system_time,
                     struct timespec64 *ts)
{
   u32 hi1, hi2;
 
   if (cycle_counter)
       *cycle_counter = kbase_backend_get_cycle_cnt(kbdev);
 
   if (system_time) {
       /* Read hi, lo, hi to ensure a coherent u64 */
       do {
           hi1 = kbase_reg_read(kbdev,
                        GPU_CONTROL_REG(TIMESTAMP_HI));
           *system_time = kbase_reg_read(kbdev,
                        GPU_CONTROL_REG(TIMESTAMP_LO));
           hi2 = kbase_reg_read(kbdev,
                        GPU_CONTROL_REG(TIMESTAMP_HI));
       } while (hi1 != hi2);
       *system_time |= (((u64) hi1) << 32);
   }
 
   /* Record the CPU's idea of current time */
   if (ts != NULL)
#if (KERNEL_VERSION(4, 17, 0) > LINUX_VERSION_CODE)
       *ts = ktime_to_timespec64(ktime_get_raw());
#else
       ktime_get_raw_ts64(ts);
#endif
}
 
#if !MALI_USE_CSF
/**
 * timedwait_cycle_count_active() - Timed wait till CYCLE_COUNT_ACTIVE is active
 *
 * @kbdev: Kbase device
 *
 * Return: true if CYCLE_COUNT_ACTIVE is active within the timeout.
 */
static bool timedwait_cycle_count_active(struct kbase_device *kbdev)
{
#if IS_ENABLED(CONFIG_MALI_BIFROST_NO_MALI)
   return true;
#else
   bool success = false;
   const unsigned int timeout = 100;
   const unsigned long remaining = jiffies + msecs_to_jiffies(timeout);
 
   while (time_is_after_jiffies(remaining)) {
       if ((kbase_reg_read(kbdev, GPU_CONTROL_REG(GPU_STATUS)) &
            GPU_STATUS_CYCLE_COUNT_ACTIVE)) {
           success = true;
           break;
       }
   }
   return success;
#endif
}
#endif
 
void kbase_backend_get_gpu_time(struct kbase_device *kbdev, u64 *cycle_counter,
               u64 *system_time, struct timespec64 *ts)
{
#if !MALI_USE_CSF
   kbase_pm_request_gpu_cycle_counter(kbdev);
   WARN_ONCE(kbdev->pm.backend.l2_state != KBASE_L2_ON,
         "L2 not powered up");
   WARN_ONCE((!timedwait_cycle_count_active(kbdev)),
         "Timed out on CYCLE_COUNT_ACTIVE");
#endif
   kbase_backend_get_gpu_time_norequest(kbdev, cycle_counter, system_time,
                        ts);
#if !MALI_USE_CSF
   kbase_pm_release_gpu_cycle_counter(kbdev);
#endif
}
 
unsigned int kbase_get_timeout_ms(struct kbase_device *kbdev,
                 enum kbase_timeout_selector selector)
{
   /* Timeout calculation:
    * dividing number of cycles by freq in KHz automatically gives value
    * in milliseconds. nr_cycles will have to be multiplied by 1e3 to
    * get result in microseconds, and 1e6 to get result in nanoseconds.
    */
 
   u64 timeout, nr_cycles = 0;
   u64 freq_khz;
 
   /* Only for debug messages, safe default in case it's mis-maintained */
   const char *selector_str = "(unknown)";
 
   if (!kbdev->lowest_gpu_freq_khz) {
       dev_dbg(kbdev->dev,
           "Lowest frequency uninitialized! Using reference frequency for scaling");
       freq_khz = DEFAULT_REF_TIMEOUT_FREQ_KHZ;
   } else {
       freq_khz = kbdev->lowest_gpu_freq_khz;
   }
 
   switch (selector) {
   case MMU_AS_INACTIVE_WAIT_TIMEOUT:
       selector_str = "MMU_AS_INACTIVE_WAIT_TIMEOUT";
       nr_cycles = MMU_AS_INACTIVE_WAIT_TIMEOUT_CYCLES;
       break;
   case KBASE_TIMEOUT_SELECTOR_COUNT:
   default:
#if !MALI_USE_CSF
       WARN(1, "Invalid timeout selector used! Using default value");
       nr_cycles = JM_DEFAULT_TIMEOUT_CYCLES;
       break;
   case JM_DEFAULT_JS_FREE_TIMEOUT:
       selector_str = "JM_DEFAULT_JS_FREE_TIMEOUT";
       nr_cycles = JM_DEFAULT_JS_FREE_TIMEOUT_CYCLES;
       break;
#else
       /* Use Firmware timeout if invalid selection */
       WARN(1,
            "Invalid timeout selector used! Using CSF Firmware timeout");
       fallthrough;
   case CSF_FIRMWARE_TIMEOUT:
       selector_str = "CSF_FIRMWARE_TIMEOUT";
       /* Any FW timeout cannot be longer than the FW ping interval, after which
        * the firmware_aliveness_monitor will be triggered and may restart
        * the GPU if the FW is unresponsive.
        */
       nr_cycles = min(CSF_FIRMWARE_PING_TIMEOUT_CYCLES, CSF_FIRMWARE_TIMEOUT_CYCLES);
 
       if (nr_cycles == CSF_FIRMWARE_PING_TIMEOUT_CYCLES)
           dev_warn(kbdev->dev, "Capping %s to CSF_FIRMWARE_PING_TIMEOUT\n",
                selector_str);
       break;
   case CSF_PM_TIMEOUT:
       selector_str = "CSF_PM_TIMEOUT";
       nr_cycles = CSF_PM_TIMEOUT_CYCLES;
       break;
   case CSF_GPU_RESET_TIMEOUT:
       selector_str = "CSF_GPU_RESET_TIMEOUT";
       nr_cycles = CSF_GPU_RESET_TIMEOUT_CYCLES;
       break;
   case CSF_CSG_SUSPEND_TIMEOUT:
       selector_str = "CSF_CSG_SUSPEND_TIMEOUT";
       nr_cycles = CSF_CSG_SUSPEND_TIMEOUT_CYCLES;
       break;
   case CSF_FIRMWARE_BOOT_TIMEOUT:
       selector_str = "CSF_FIRMWARE_BOOT_TIMEOUT";
       nr_cycles = CSF_FIRMWARE_BOOT_TIMEOUT_CYCLES;
       break;
   case CSF_FIRMWARE_PING_TIMEOUT:
       selector_str = "CSF_FIRMWARE_PING_TIMEOUT";
       nr_cycles = CSF_FIRMWARE_PING_TIMEOUT_CYCLES;
       break;
   case CSF_SCHED_PROTM_PROGRESS_TIMEOUT:
       selector_str = "CSF_SCHED_PROTM_PROGRESS_TIMEOUT";
       nr_cycles = kbase_csf_timeout_get(kbdev);
       break;
#endif
   }
 
   timeout = div_u64(nr_cycles, freq_khz);
   if (WARN(timeout > UINT_MAX,
        "Capping excessive timeout %llums for %s at freq %llukHz to UINT_MAX ms",
        (unsigned long long)timeout, selector_str, (unsigned long long)freq_khz))
       timeout = UINT_MAX;
   return (unsigned int)timeout;
}
KBASE_EXPORT_TEST_API(kbase_get_timeout_ms);
 
u64 kbase_backend_get_cycle_cnt(struct kbase_device *kbdev)
{
   u32 hi1, hi2, lo;
 
   /* Read hi, lo, hi to ensure a coherent u64 */
   do {
       hi1 = kbase_reg_read(kbdev,
                   GPU_CONTROL_REG(CYCLE_COUNT_HI));
       lo = kbase_reg_read(kbdev,
                   GPU_CONTROL_REG(CYCLE_COUNT_LO));
       hi2 = kbase_reg_read(kbdev,
                   GPU_CONTROL_REG(CYCLE_COUNT_HI));
   } while (hi1 != hi2);
 
   return lo | (((u64) hi1) << 32);
}
 
#if MALI_USE_CSF
u64 __maybe_unused kbase_backend_time_convert_gpu_to_cpu(struct kbase_device *kbdev, u64 gpu_ts)
{
   if (WARN_ON(!kbdev))
       return 0;
 
   return div64_u64(gpu_ts * kbdev->backend_time.multiplier, kbdev->backend_time.divisor) +
          kbdev->backend_time.offset;
}
 
/**
 * get_cpu_gpu_time() - Get current CPU and GPU timestamps.
 *
 * @kbdev:    Kbase device.
 * @cpu_ts:    Output CPU timestamp.
 * @gpu_ts:    Output GPU timestamp.
 * @gpu_cycle:  Output GPU cycle counts.
 */
static void get_cpu_gpu_time(struct kbase_device *kbdev, u64 *cpu_ts, u64 *gpu_ts, u64 *gpu_cycle)
{
   struct timespec64 ts;
 
   kbase_backend_get_gpu_time(kbdev, gpu_cycle, gpu_ts, &ts);
 
   if (cpu_ts)
       *cpu_ts = ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
}
#endif
 
int kbase_backend_time_init(struct kbase_device *kbdev)
{
#if MALI_USE_CSF
   u64 cpu_ts = 0;
   u64 gpu_ts = 0;
   u64 freq;
   u64 common_factor;
 
   get_cpu_gpu_time(kbdev, &cpu_ts, &gpu_ts, NULL);
   freq = arch_timer_get_cntfrq();
 
   if (!freq) {
       dev_warn(kbdev->dev, "arch_timer_get_rate() is zero!");
       return -EINVAL;
   }
 
   common_factor = gcd(NSEC_PER_SEC, freq);
 
   kbdev->backend_time.multiplier = div64_u64(NSEC_PER_SEC, common_factor);
   kbdev->backend_time.divisor = div64_u64(freq, common_factor);
 
   if (!kbdev->backend_time.divisor) {
       dev_warn(kbdev->dev, "CPU to GPU divisor is zero!");
       return -EINVAL;
   }
 
   kbdev->backend_time.offset = cpu_ts - div64_u64(gpu_ts * kbdev->backend_time.multiplier,
                           kbdev->backend_time.divisor);
#endif
 
   return 0;
}