rockchip: gpu: add /sys/rk_gpu/error_count
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase_jm.c
1 /*
2  *
3  * (C) COPYRIGHT ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * A copy of the licence is included with the program, and can also be obtained
11  * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12  * Boston, MA  02110-1301, USA.
13  *
14  */
15
16
17
18
19
20 /**
21  * @file mali_kbase_jm.c
22  * Base kernel job manager APIs
23  */
24
25 #include <mali_kbase.h>
26 #include <mali_midg_regmap.h>
27 #include <mali_kbase_gator.h>
28 #include <mali_kbase_js_affinity.h>
29 #include <mali_kbase_hw.h>
30
31 #include "mali_kbase_jm.h"
32
33 #define beenthere(kctx, f, a...)  dev_dbg(kctx->kbdev->dev, "%s:" f, __func__, ##a)
34
35 #ifdef CONFIG_MALI_DEBUG_SHADER_SPLIT_FS
36 u64 mali_js0_affinity_mask = 0xFFFFFFFFFFFFFFFFULL;
37 u64 mali_js1_affinity_mask = 0xFFFFFFFFFFFFFFFFULL;
38 u64 mali_js2_affinity_mask = 0xFFFFFFFFFFFFFFFFULL;
39 #endif
40
41
42 static void kbasep_try_reset_gpu_early(kbase_device *kbdev);
43
44 #ifdef CONFIG_GPU_TRACEPOINTS
45 static char *kbasep_make_job_slot_string(int js, char *js_string)
46 {
47         sprintf(js_string, "job_slot_%i", js);
48         return js_string;
49 }
50 #endif
51
52 static void kbase_job_hw_submit(kbase_device *kbdev, kbase_jd_atom *katom, int js)
53 {
54         kbase_context *kctx;
55         u32 cfg;
56         u64 jc_head = katom->jc;
57
58         KBASE_DEBUG_ASSERT(kbdev);
59         KBASE_DEBUG_ASSERT(katom);
60
61         kctx = katom->kctx;
62
63         /* Command register must be available */
64         KBASE_DEBUG_ASSERT(kbasep_jm_is_js_free(kbdev, js, kctx));
65         /* Affinity is not violating */
66         kbase_js_debug_log_current_affinities(kbdev);
67         KBASE_DEBUG_ASSERT(!kbase_js_affinity_would_violate(kbdev, js, katom->affinity));
68
69         kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_HEAD_NEXT_LO), jc_head & 0xFFFFFFFF, kctx);
70         kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_HEAD_NEXT_HI), jc_head >> 32, kctx);
71
72 #ifdef CONFIG_MALI_DEBUG_SHADER_SPLIT_FS
73         {
74                 u64 mask;
75                 u32 value;
76
77                 if( 0 == js )
78                 {
79                         mask = mali_js0_affinity_mask;
80                 }
81                 else if( 1 == js )
82                 {
83                         mask = mali_js1_affinity_mask;
84                 }
85                 else
86                 {
87                         mask = mali_js2_affinity_mask;
88                 }
89
90                 value = katom->affinity & (mask & 0xFFFFFFFF);
91
92                 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_AFFINITY_NEXT_LO), value, kctx);
93
94                 value = (katom->affinity >> 32) & ((mask>>32) & 0xFFFFFFFF);
95                 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_AFFINITY_NEXT_HI), value, kctx);
96         }
97 #else
98         kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_AFFINITY_NEXT_LO), katom->affinity & 0xFFFFFFFF, kctx);
99         kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_AFFINITY_NEXT_HI), katom->affinity >> 32, kctx);
100 #endif
101
102         /* start MMU, medium priority, cache clean/flush on end, clean/flush on start */
103         cfg = kctx->as_nr | JSn_CONFIG_END_FLUSH_CLEAN_INVALIDATE | JSn_CONFIG_START_MMU | JSn_CONFIG_START_FLUSH_CLEAN_INVALIDATE | JSn_CONFIG_THREAD_PRI(8);
104
105         if (kbase_hw_has_feature(kbdev, BASE_HW_FEATURE_JOBCHAIN_DISAMBIGUATION)) {
106                 if (!kbdev->jm_slots[js].job_chain_flag) {
107                         cfg |= JSn_CONFIG_JOB_CHAIN_FLAG;
108                         katom->atom_flags |= KBASE_KATOM_FLAGS_JOBCHAIN;
109                         kbdev->jm_slots[js].job_chain_flag = MALI_TRUE;
110                 } else {
111                         katom->atom_flags &= ~KBASE_KATOM_FLAGS_JOBCHAIN;
112                         kbdev->jm_slots[js].job_chain_flag = MALI_FALSE;
113                 }
114         }
115
116         kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_CONFIG_NEXT), cfg, kctx);
117
118         /* Write an approximate start timestamp.
119          * It's approximate because there might be a job in the HEAD register. In
120          * such cases, we'll try to make a better approximation in the IRQ handler
121          * (up to the KBASE_JS_IRQ_THROTTLE_TIME_US). */
122         katom->start_timestamp = ktime_get();
123
124         /* GO ! */
125         dev_dbg(kbdev->dev, "JS: Submitting atom %p from ctx %p to js[%d] with head=0x%llx, affinity=0x%llx", katom, kctx, js, jc_head, katom->affinity);
126
127         KBASE_TRACE_ADD_SLOT_INFO(kbdev, JM_SUBMIT, kctx, katom, jc_head, js, (u32) katom->affinity);
128
129 #ifdef CONFIG_MALI_GATOR_SUPPORT
130         kbase_trace_mali_job_slots_event(GATOR_MAKE_EVENT(GATOR_JOB_SLOT_START, js), kctx, kbase_jd_atom_id(kctx, katom)); 
131 #endif                          /* CONFIG_MALI_GATOR_SUPPORT */
132 #ifdef CONFIG_GPU_TRACEPOINTS
133         if (kbasep_jm_nr_jobs_submitted(&kbdev->jm_slots[js]) == 1)
134         {
135                 /* If this is the only job on the slot, trace it as starting */
136                 char js_string[16];
137                 trace_gpu_sched_switch(kbasep_make_job_slot_string(js, js_string), ktime_to_ns(katom->start_timestamp), (u32)katom->kctx, 0, katom->work_id);
138                 kbdev->jm_slots[js].last_context = katom->kctx;
139         }
140 #endif
141         kbase_timeline_job_slot_submit(kbdev, kctx, katom, js);
142
143         kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_COMMAND_NEXT), JSn_COMMAND_START, katom->kctx);
144 }
145
146 void kbase_job_submit_nolock(kbase_device *kbdev, kbase_jd_atom *katom, int js)
147 {
148         kbase_jm_slot *jm_slots;
149         base_jd_core_req core_req;
150
151         KBASE_DEBUG_ASSERT(kbdev);
152         KBASE_DEBUG_ASSERT(katom);
153
154         jm_slots = kbdev->jm_slots;
155
156         core_req = katom->core_req;
157         if (core_req & BASE_JD_REQ_ONLY_COMPUTE) {
158                 unsigned long flags;
159                 int device_nr = (core_req & BASE_JD_REQ_SPECIFIC_COHERENT_GROUP) ? katom->device_nr : 0;
160                 KBASE_DEBUG_ASSERT(device_nr < 2);
161                 spin_lock_irqsave(&kbdev->pm.metrics.lock, flags);
162                 kbasep_pm_record_job_status(kbdev);
163                 kbdev->pm.metrics.active_cl_ctx[device_nr]++;
164                 spin_unlock_irqrestore(&kbdev->pm.metrics.lock, flags);
165         } else {
166                 unsigned long flags;
167
168                 spin_lock_irqsave(&kbdev->pm.metrics.lock, flags);
169                 kbasep_pm_record_job_status(kbdev);
170                 kbdev->pm.metrics.active_gl_ctx++;
171                 spin_unlock_irqrestore(&kbdev->pm.metrics.lock, flags);
172         }
173
174         /*
175          * We can have:
176          * - one job already done (pending interrupt),
177          * - one running,
178          * - one ready to be run.
179          * Hence a maximum of 3 inflight jobs. We have a 4 job
180          * queue, which I hope will be enough...
181          */
182         kbasep_jm_enqueue_submit_slot(&jm_slots[js], katom);
183         kbase_job_hw_submit(kbdev, katom, js);
184 }
185
186 void kbase_job_done_slot(kbase_device *kbdev, int s, u32 completion_code, u64 job_tail, ktime_t *end_timestamp)
187 {
188         kbase_jm_slot *slot;
189         kbase_jd_atom *katom;
190         mali_addr64 jc_head;
191         kbase_context *kctx;
192
193         KBASE_DEBUG_ASSERT(kbdev);
194
195         if (completion_code != BASE_JD_EVENT_DONE && completion_code != BASE_JD_EVENT_STOPPED)
196                 dev_err(kbdev->dev, "t6xx: GPU fault 0x%02lx from job slot %d\n", (unsigned long)completion_code, s);
197
198         /* IMPORTANT: this function must only contain work necessary to complete a
199          * job from a Real IRQ (and not 'fake' completion, e.g. from
200          * Soft-stop). For general work that must happen no matter how the job was
201          * removed from the hardware, place it in kbase_jd_done() */
202
203         slot = &kbdev->jm_slots[s];
204         katom = kbasep_jm_dequeue_submit_slot(slot);
205
206         /* If the katom completed is because it's a dummy job for HW workarounds, then take no further action */
207         if (kbasep_jm_is_dummy_workaround_job(kbdev, katom)) {
208                 KBASE_TRACE_ADD_SLOT_INFO(kbdev, JM_JOB_DONE, NULL, NULL, 0, s, completion_code);
209                 return;
210         }
211
212         jc_head = katom->jc;
213         kctx = katom->kctx;
214
215         KBASE_TRACE_ADD_SLOT_INFO(kbdev, JM_JOB_DONE, kctx, katom, jc_head, s, completion_code);
216
217         if (completion_code != BASE_JD_EVENT_DONE && completion_code != BASE_JD_EVENT_STOPPED) {
218
219 #if KBASE_TRACE_DUMP_ON_JOB_SLOT_ERROR != 0
220                 KBASE_TRACE_DUMP(kbdev);
221 #endif
222         }
223         if (job_tail != 0) {
224                 mali_bool was_updated = (job_tail != jc_head);
225                 /* Some of the job has been executed, so we update the job chain address to where we should resume from */
226                 katom->jc = job_tail;
227                 if (was_updated)
228                         KBASE_TRACE_ADD_SLOT(kbdev, JM_UPDATE_HEAD, kctx, katom, job_tail, s);
229         }
230
231         /* Only update the event code for jobs that weren't cancelled */
232         if (katom->event_code != BASE_JD_EVENT_JOB_CANCELLED)
233                 katom->event_code = (base_jd_event_code) completion_code;
234
235         kbase_device_trace_register_access(kctx, REG_WRITE, JOB_CONTROL_REG(JOB_IRQ_CLEAR), 1 << s);
236
237         /* Complete the job, and start new ones
238          *
239          * Also defer remaining work onto the workqueue:
240          * - Re-queue Soft-stopped jobs
241          * - For any other jobs, queue the job back into the dependency system
242          * - Schedule out the parent context if necessary, and schedule a new one in.
243          */
244 #ifdef CONFIG_GPU_TRACEPOINTS
245         if (kbasep_jm_nr_jobs_submitted(slot) != 0) {
246                 kbase_jd_atom *katom;
247                 char js_string[16];
248                 katom = kbasep_jm_peek_idx_submit_slot(slot, 0);        /* The atom in the HEAD */
249                 trace_gpu_sched_switch(kbasep_make_job_slot_string(s, js_string), ktime_to_ns(*end_timestamp), (u32)katom->kctx, 0, katom->work_id);
250                 slot->last_context = katom->kctx;
251         } else {
252                 char js_string[16];
253                 trace_gpu_sched_switch(kbasep_make_job_slot_string(s, js_string), ktime_to_ns(ktime_get()), 0, 0, 0);
254                 slot->last_context = 0;
255         }
256 #endif
257         kbase_jd_done(katom, s, end_timestamp, KBASE_JS_ATOM_DONE_START_NEW_ATOMS);
258 }
259
260 /**
261  * Update the start_timestamp of the job currently in the HEAD, based on the
262  * fact that we got an IRQ for the previous set of completed jobs.
263  *
264  * The estimate also takes into account the KBASE_JS_IRQ_THROTTLE_TIME_US and
265  * the time the job was submitted, to work out the best estimate (which might
266  * still result in an over-estimate to the calculated time spent)
267  */
268 STATIC void kbasep_job_slot_update_head_start_timestamp(kbase_device *kbdev, kbase_jm_slot *slot, ktime_t end_timestamp)
269 {
270         KBASE_DEBUG_ASSERT(slot);
271
272         if (kbasep_jm_nr_jobs_submitted(slot) > 0) {
273                 kbase_jd_atom *katom;
274                 ktime_t new_timestamp;
275                 ktime_t timestamp_diff;
276                 katom = kbasep_jm_peek_idx_submit_slot(slot, 0);        /* The atom in the HEAD */
277
278                 KBASE_DEBUG_ASSERT(katom != NULL);
279
280                 if (kbasep_jm_is_dummy_workaround_job(kbdev, katom) != MALI_FALSE) {
281                         /* Don't access the members of HW workaround 'dummy' jobs */
282                         return;
283                 }
284
285                 /* Account for any IRQ Throttle time - makes an overestimate of the time spent by the job */
286                 new_timestamp = ktime_sub_ns(end_timestamp, KBASE_JS_IRQ_THROTTLE_TIME_US * 1000);
287                 timestamp_diff = ktime_sub(new_timestamp, katom->start_timestamp);
288                 if (ktime_to_ns(timestamp_diff) >= 0) {
289                         /* Only update the timestamp if it's a better estimate than what's currently stored.
290                          * This is because our estimate that accounts for the throttle time may be too much
291                          * of an overestimate */
292                         katom->start_timestamp = new_timestamp;
293                 }
294         }
295 }
296
297 void kbase_job_done(kbase_device *kbdev, u32 done)
298 {
299         unsigned long flags;
300         int i;
301         u32 count = 0;
302         ktime_t end_timestamp = ktime_get();
303         kbasep_js_device_data *js_devdata;
304
305         KBASE_DEBUG_ASSERT(kbdev);
306         js_devdata = &kbdev->js_data;
307
308         KBASE_TRACE_ADD(kbdev, JM_IRQ, NULL, NULL, 0, done);
309
310         memset(&kbdev->slot_submit_count_irq[0], 0, sizeof(kbdev->slot_submit_count_irq));
311
312         /* write irq throttle register, this will prevent irqs from occurring until
313          * the given number of gpu clock cycles have passed */
314         {
315                 int irq_throttle_cycles = atomic_read(&kbdev->irq_throttle_cycles);
316                 kbase_reg_write(kbdev, JOB_CONTROL_REG(JOB_IRQ_THROTTLE), irq_throttle_cycles, NULL);
317         }
318
319         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
320
321         while (done) {
322                 kbase_jm_slot *slot;
323                 u32 failed = done >> 16;
324
325                 /* treat failed slots as finished slots */
326                 u32 finished = (done & 0xFFFF) | failed;
327
328                 /* Note: This is inherently unfair, as we always check
329                  * for lower numbered interrupts before the higher
330                  * numbered ones.*/
331                 i = ffs(finished) - 1;
332                 KBASE_DEBUG_ASSERT(i >= 0);
333
334                 slot = &kbdev->jm_slots[i];
335
336                 do {
337                         int nr_done;
338                         u32 active;
339                         u32 completion_code = BASE_JD_EVENT_DONE;       /* assume OK */
340                         u64 job_tail = 0;
341
342                         if (failed & (1u << i)) {
343                                 /* read out the job slot status code if the job slot reported failure */
344                                 completion_code = kbase_reg_read(kbdev, JOB_SLOT_REG(i, JSn_STATUS), NULL);
345
346                                 switch (completion_code) {
347                                 case BASE_JD_EVENT_STOPPED:
348 #ifdef CONFIG_MALI_GATOR_SUPPORT
349                                         kbase_trace_mali_job_slots_event(GATOR_MAKE_EVENT(GATOR_JOB_SLOT_SOFT_STOPPED, i), NULL, 0);
350 #endif                          /* CONFIG_MALI_GATOR_SUPPORT */
351                                         /* Soft-stopped job - read the value of JS<n>_TAIL so that the job chain can be resumed */
352                                         job_tail = (u64) kbase_reg_read(kbdev, JOB_SLOT_REG(i, JSn_TAIL_LO), NULL) | ((u64) kbase_reg_read(kbdev, JOB_SLOT_REG(i, JSn_TAIL_HI), NULL) << 32);
353                                         break;
354                                 case BASE_JD_EVENT_NOT_STARTED:
355                                         /* PRLAM-10673 can cause a TERMINATED job to come back as NOT_STARTED, but the error interrupt helps us detect it */
356                                         completion_code = BASE_JD_EVENT_TERMINATED;
357                                         /* fall throught */
358                                 default:
359                                         dev_warn(kbdev->dev, "error detected from slot %d, job status 0x%08x (%s)", i, completion_code, kbase_exception_name(completion_code));
360                                 kbdev->kbase_group_error++;
361                                 }
362                         }
363
364                         kbase_reg_write(kbdev, JOB_CONTROL_REG(JOB_IRQ_CLEAR), done & ((1 << i) | (1 << (i + 16))), NULL);
365                         active = kbase_reg_read(kbdev, JOB_CONTROL_REG(JOB_IRQ_JS_STATE), NULL);
366
367                         if (((active >> i) & 1) == 0 && (((done >> (i + 16)) & 1) == 0)) {
368                                 /* There is a potential race we must work around:
369                                  *
370                                  *  1. A job slot has a job in both current and next registers
371                                  *  2. The job in current completes successfully, the IRQ handler reads RAWSTAT
372                                  *     and calls this function with the relevant bit set in "done"
373                                  *  3. The job in the next registers becomes the current job on the GPU
374                                  *  4. Sometime before the JOB_IRQ_CLEAR line above the job on the GPU _fails_
375                                  *  5. The IRQ_CLEAR clears the done bit but not the failed bit. This atomically sets
376                                  *     JOB_IRQ_JS_STATE. However since both jobs have now completed the relevant bits
377                                  *     for the slot are set to 0.
378                                  *
379                                  * If we now did nothing then we'd incorrectly assume that _both_ jobs had completed
380                                  * successfully (since we haven't yet observed the fail bit being set in RAWSTAT).
381                                  *
382                                  * So at this point if there are no active jobs left we check to see if RAWSTAT has a failure
383                                  * bit set for the job slot. If it does we know that there has been a new failure that we
384                                  * didn't previously know about, so we make sure that we record this in active (but we wait
385                                  * for the next loop to deal with it).
386                                  *
387                                  * If we were handling a job failure (i.e. done has the relevant high bit set) then we know that
388                                  * the value read back from JOB_IRQ_JS_STATE is the correct number of remaining jobs because
389                                  * the failed job will have prevented any futher jobs from starting execution.
390                                  */
391                                 u32 rawstat = kbase_reg_read(kbdev, JOB_CONTROL_REG(JOB_IRQ_RAWSTAT), NULL);
392
393                                 if ((rawstat >> (i + 16)) & 1) {
394                                         /* There is a failed job that we've missed - add it back to active */
395                                         active |= (1u << i);
396                                 }
397                         }
398
399                         dev_dbg(kbdev->dev, "Job ended with status 0x%08X\n", completion_code);
400
401                         nr_done = kbasep_jm_nr_jobs_submitted(slot);
402                         nr_done -= (active >> i) & 1;
403                         nr_done -= (active >> (i + 16)) & 1;
404
405                         if (nr_done <= 0) {
406                                 dev_warn(kbdev->dev, "Spurious interrupt on slot %d", i);
407                                 goto spurious;
408                         }
409
410                         count += nr_done;
411
412                         while (nr_done) {
413                                 if (nr_done == 1) {
414                                         kbase_job_done_slot(kbdev, i, completion_code, job_tail, &end_timestamp);
415                                 } else {
416                                         /* More than one job has completed. Since this is not the last job being reported this time it
417                                          * must have passed. This is because the hardware will not allow further jobs in a job slot to
418                                          * complete until the faile job is cleared from the IRQ status.
419                                          */
420                                         kbase_job_done_slot(kbdev, i, BASE_JD_EVENT_DONE, 0, &end_timestamp);
421                                 }
422                                 nr_done--;
423                         }
424
425  spurious:
426                         done = kbase_reg_read(kbdev, JOB_CONTROL_REG(JOB_IRQ_RAWSTAT), NULL);
427
428                         if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_10883)) {
429                                 /* Workaround for missing interrupt caused by PRLAM-10883 */
430                                 if (((active >> i) & 1) && (0 == kbase_reg_read(kbdev, JOB_SLOT_REG(i, JSn_STATUS), NULL))) {
431                                         /* Force job slot to be processed again */
432                                         done |= (1u << i);
433                                 }
434                         }
435
436                         failed = done >> 16;
437                         finished = (done & 0xFFFF) | failed;
438                 } while (finished & (1 << i));
439
440                 kbasep_job_slot_update_head_start_timestamp(kbdev, slot, end_timestamp);
441         }
442         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
443
444         if (atomic_read(&kbdev->reset_gpu) == KBASE_RESET_GPU_COMMITTED) {
445                 /* If we're trying to reset the GPU then we might be able to do it early
446                  * (without waiting for a timeout) because some jobs have completed
447                  */
448                 kbasep_try_reset_gpu_early(kbdev);
449         }
450
451         KBASE_TRACE_ADD(kbdev, JM_IRQ_END, NULL, NULL, 0, count);
452 }
453 KBASE_EXPORT_TEST_API(kbase_job_done)
454
455 static mali_bool kbasep_soft_stop_allowed(kbase_device *kbdev, u16 core_reqs)
456 {
457         mali_bool soft_stops_allowed = MALI_TRUE;
458
459         if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8408)) {
460                 if ((core_reqs & BASE_JD_REQ_T) != 0)
461                         soft_stops_allowed = MALI_FALSE;
462         }
463         return soft_stops_allowed;
464 }
465
466 static mali_bool kbasep_hard_stop_allowed(kbase_device *kbdev, u16 core_reqs)
467 {
468         mali_bool hard_stops_allowed = MALI_TRUE;
469
470         if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8394)) {
471                 if ((core_reqs & BASE_JD_REQ_T) != 0)
472                         hard_stops_allowed = MALI_FALSE;
473         }
474         return hard_stops_allowed;
475 }
476
477 static void kbasep_job_slot_soft_or_hard_stop_do_action(kbase_device *kbdev, int js, u32 action, u16 core_reqs, kbase_jd_atom * target_katom )
478 {
479         kbase_context *kctx = target_katom->kctx;
480 #if KBASE_TRACE_ENABLE
481         u32 status_reg_before;
482         u64 job_in_head_before;
483         u32 status_reg_after;
484
485         /* Check the head pointer */
486         job_in_head_before = ((u64) kbase_reg_read(kbdev, JOB_SLOT_REG(js, JSn_HEAD_LO), NULL))
487             | (((u64) kbase_reg_read(kbdev, JOB_SLOT_REG(js, JSn_HEAD_HI), NULL)) << 32);
488         status_reg_before = kbase_reg_read(kbdev, JOB_SLOT_REG(js, JSn_STATUS), NULL);
489 #endif
490
491         if (action == JSn_COMMAND_SOFT_STOP) {
492                 mali_bool soft_stop_allowed = kbasep_soft_stop_allowed(kbdev, core_reqs);
493                 if (!soft_stop_allowed) {
494 #ifdef CONFIG_MALI_DEBUG
495                         dev_dbg(kbdev->dev, "Attempt made to soft-stop a job that cannot be soft-stopped. core_reqs = 0x%X", (unsigned int)core_reqs);
496 #endif                          /* CONFIG_MALI_DEBUG */
497                         return;
498                 }
499
500                 /* We are about to issue a soft stop, so mark the atom as having been soft stopped */
501                 target_katom->atom_flags |= KBASE_KATOM_FLAG_BEEN_SOFT_STOPPPED;
502         }
503
504         if (action == JSn_COMMAND_HARD_STOP) {
505                 mali_bool hard_stop_allowed = kbasep_hard_stop_allowed(kbdev, core_reqs);
506                 if (!hard_stop_allowed) {
507                         /* Jobs can be hard-stopped for the following reasons:
508                          *  * CFS decides the job has been running too long (and soft-stop has not occurred).
509                          *    In this case the GPU will be reset by CFS if the job remains on the GPU.
510                          *
511                          *  * The context is destroyed, kbase_jd_zap_context will attempt to hard-stop the job. However
512                          *    it also has a watchdog which will cause the GPU to be reset if the job remains on the GPU.
513                          *
514                          *  * An (unhandled) MMU fault occurred. As long as BASE_HW_ISSUE_8245 is defined then
515                          *    the GPU will be reset.
516                          *
517                          * All three cases result in the GPU being reset if the hard-stop fails,
518                          * so it is safe to just return and ignore the hard-stop request.
519                          */
520                         dev_warn(kbdev->dev, "Attempt made to hard-stop a job that cannot be hard-stopped. core_reqs = 0x%X", (unsigned int)core_reqs);
521                         return;
522                 }
523         }
524
525         if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8316) && action == JSn_COMMAND_SOFT_STOP) {
526                 int i;
527                 kbase_jm_slot *slot;
528                 slot = &kbdev->jm_slots[js];
529
530                 for (i = 0; i < kbasep_jm_nr_jobs_submitted(slot); i++) {
531                         kbase_jd_atom *katom;
532
533                         katom = kbasep_jm_peek_idx_submit_slot(slot, i);
534
535                         KBASE_DEBUG_ASSERT(katom);
536
537                         if (kbasep_jm_is_dummy_workaround_job(kbdev, katom) != MALI_FALSE) {
538                                 /* Don't access the members of HW workaround 'dummy' jobs
539                                  *
540                                  * This assumes that such jobs can't cause HW_ISSUE_8316, and could only be blocked
541                                  * by other jobs causing HW_ISSUE_8316 (which will get poked/or eventually get killed) */
542                                 continue;
543                         }
544
545                         /* For HW_ISSUE_8316, only 'bad' jobs attacking the system can
546                          * cause this issue: normally, all memory should be allocated in
547                          * multiples of 4 pages, and growable memory should be changed size
548                          * in multiples of 4 pages.
549                          *
550                          * Whilst such 'bad' jobs can be cleared by a GPU reset, the
551                          * locking up of a uTLB entry caused by the bad job could also
552                          * stall other ASs, meaning that other ASs' jobs don't complete in
553                          * the 'grace' period before the reset. We don't want to lose other
554                          * ASs' jobs when they would normally complete fine, so we must
555                          * 'poke' the MMU regularly to help other ASs complete */
556                         kbase_as_poking_timer_retain_atom(kbdev, katom->kctx, katom);
557                 }
558         }
559
560         if (kbase_hw_has_feature(kbdev, BASE_HW_FEATURE_JOBCHAIN_DISAMBIGUATION)) {
561                 if (action == JSn_COMMAND_SOFT_STOP)
562                         action = (target_katom->atom_flags & KBASE_KATOM_FLAGS_JOBCHAIN) ? 
563                                  JSn_COMMAND_SOFT_STOP_1:
564                          JSn_COMMAND_SOFT_STOP_0;
565                 else
566                         action = (target_katom->atom_flags & KBASE_KATOM_FLAGS_JOBCHAIN) ? 
567                                  JSn_COMMAND_HARD_STOP_1:
568                          JSn_COMMAND_HARD_STOP_0;
569         }
570
571         kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_COMMAND), action, kctx);
572
573 #if KBASE_TRACE_ENABLE
574         status_reg_after = kbase_reg_read(kbdev, JOB_SLOT_REG(js, JSn_STATUS), NULL);
575         if (status_reg_after == BASE_JD_EVENT_ACTIVE) {
576                 kbase_jm_slot *slot;
577                 kbase_jd_atom *head;
578                 kbase_context *head_kctx;
579
580                 slot = &kbdev->jm_slots[js];
581                 head = kbasep_jm_peek_idx_submit_slot(slot, slot->submitted_nr - 1);
582                 head_kctx = head->kctx;
583
584                 /* We don't need to check kbasep_jm_is_dummy_workaround_job( head ) here:
585                  * - Members are not indirected through
586                  * - The members will all be zero anyway
587                  */
588                 if (status_reg_before == BASE_JD_EVENT_ACTIVE)
589                         KBASE_TRACE_ADD_SLOT(kbdev, JM_CHECK_HEAD, head_kctx, head, job_in_head_before, js);
590                 else
591                         KBASE_TRACE_ADD_SLOT(kbdev, JM_CHECK_HEAD, NULL, NULL, 0, js);
592
593                 switch(action) {
594                 case JSn_COMMAND_SOFT_STOP:
595                         KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP, head_kctx, head, head->jc, js);
596                         break;
597                 case JSn_COMMAND_SOFT_STOP_0:
598                         KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP_0, head_kctx, head, head->jc, js);
599                         break;
600                 case JSn_COMMAND_SOFT_STOP_1:
601                         KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP_1, head_kctx, head, head->jc, js);
602                         break;
603                 case JSn_COMMAND_HARD_STOP:
604                         KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP, head_kctx, head, head->jc, js);
605                         break;
606                 case JSn_COMMAND_HARD_STOP_0:
607                         KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP_0, head_kctx, head, head->jc, js);
608                         break;
609                 case JSn_COMMAND_HARD_STOP_1:
610                         KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP_1, head_kctx, head, head->jc, js);
611                         break;
612                 default:
613                         BUG();
614                         break;
615                 }
616         } else {
617                 if (status_reg_before == BASE_JD_EVENT_ACTIVE)
618                         KBASE_TRACE_ADD_SLOT(kbdev, JM_CHECK_HEAD, NULL, NULL, job_in_head_before, js);
619                 else
620                         KBASE_TRACE_ADD_SLOT(kbdev, JM_CHECK_HEAD, NULL, NULL, 0, js);
621
622                 switch(action) {
623                 case JSn_COMMAND_SOFT_STOP:
624                         KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP, NULL, NULL, 0, js);
625                         break;
626                 case JSn_COMMAND_SOFT_STOP_0:
627                         KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP_0, NULL, NULL, 0, js);
628                         break;
629                 case JSn_COMMAND_SOFT_STOP_1:
630                         KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP_1, NULL, NULL, 0, js);
631                         break;
632                 case JSn_COMMAND_HARD_STOP:
633                         KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP, NULL, NULL, 0, js);
634                         break;
635                 case JSn_COMMAND_HARD_STOP_0:
636                         KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP_0, NULL, NULL, 0, js);
637                         break;
638                 case JSn_COMMAND_HARD_STOP_1:
639                         KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP_1, NULL, NULL, 0, js);
640                         break;
641                 default:
642                         BUG();
643                         break;
644                 }
645         }
646 #endif
647 }
648
649 /* Helper macros used by kbasep_job_slot_soft_or_hard_stop */
650 #define JM_SLOT_MAX_JOB_SUBMIT_REGS    2
651 #define JM_JOB_IS_CURRENT_JOB_INDEX(n) (1 == n) /* Index of the last job to process */
652 #define JM_JOB_IS_NEXT_JOB_INDEX(n)    (2 == n) /* Index of the prior to last job to process */
653
654 /** Soft or hard-stop a slot
655  *
656  * This function safely ensures that the correct job is either hard or soft-stopped.
657  * It deals with evicting jobs from the next registers where appropriate.
658  *
659  * This does not attempt to stop or evict jobs that are 'dummy' jobs for HW workarounds.
660  *
661  * @param kbdev         The kbase device
662  * @param kctx          The context to soft/hard-stop job(s) from (or NULL is all jobs should be targeted)
663  * @param js            The slot that the job(s) are on
664  * @param target_katom  The atom that should be targeted (or NULL if all jobs from the context should be targeted)
665  * @param action        The action to perform, either JSn_COMMAND_HARD_STOP or JSn_COMMAND_SOFT_STOP
666  */
667 static void kbasep_job_slot_soft_or_hard_stop(kbase_device *kbdev, kbase_context *kctx, int js, kbase_jd_atom *target_katom, u32 action)
668 {
669         kbase_jd_atom *katom;
670         u8 i;
671         u8 jobs_submitted;
672         kbase_jm_slot *slot;
673         u16 core_reqs;
674         kbasep_js_device_data *js_devdata;
675         mali_bool can_safely_stop = kbase_hw_has_feature(kbdev, BASE_HW_FEATURE_JOBCHAIN_DISAMBIGUATION);
676
677         KBASE_DEBUG_ASSERT(action == JSn_COMMAND_HARD_STOP || action == JSn_COMMAND_SOFT_STOP);
678         KBASE_DEBUG_ASSERT(kbdev);
679         js_devdata = &kbdev->js_data;
680
681         slot = &kbdev->jm_slots[js];
682         KBASE_DEBUG_ASSERT(slot);
683         lockdep_assert_held(&js_devdata->runpool_irq.lock);
684
685         jobs_submitted = kbasep_jm_nr_jobs_submitted(slot);
686
687         KBASE_TIMELINE_TRY_SOFT_STOP(kctx, js, 1);
688         KBASE_TRACE_ADD_SLOT_INFO(kbdev, JM_SLOT_SOFT_OR_HARD_STOP, kctx, NULL, 0u, js, jobs_submitted);
689
690         if (jobs_submitted > JM_SLOT_MAX_JOB_SUBMIT_REGS)
691                 i = jobs_submitted - JM_SLOT_MAX_JOB_SUBMIT_REGS;
692         else
693                 i = 0;
694
695         /* Loop through all jobs that have been submitted to the slot and haven't completed */
696         for (; i < jobs_submitted; i++) {
697                 katom = kbasep_jm_peek_idx_submit_slot(slot, i);
698
699                 if (kctx && katom->kctx != kctx)
700                         continue;
701
702                 if (target_katom && katom != target_katom)
703                         continue;
704
705                 if (kbasep_jm_is_dummy_workaround_job(kbdev, katom))
706                         continue;
707
708                 core_reqs = katom->core_req;
709         
710                 if (JM_JOB_IS_CURRENT_JOB_INDEX(jobs_submitted - i)) {
711                         /* The last job in the slot, check if there is a job in the next register */
712                         if (kbase_reg_read(kbdev, JOB_SLOT_REG(js, JSn_COMMAND_NEXT), NULL) == 0)
713                                 kbasep_job_slot_soft_or_hard_stop_do_action(kbdev, js, action, core_reqs, katom);
714                         else {
715                                 /* The job is in the next registers */
716                                 beenthere(kctx, "clearing job from next registers on slot %d", js);
717                                 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_COMMAND_NEXT), JSn_COMMAND_NOP, NULL);
718                                 /* Check to see if we did remove a job from the next registers */
719                                 if (kbase_reg_read(kbdev, JOB_SLOT_REG(js, JSn_HEAD_NEXT_LO), NULL) != 0 || kbase_reg_read(kbdev, JOB_SLOT_REG(js, JSn_HEAD_NEXT_HI), NULL) != 0) {
720                                         /* The job was successfully cleared from the next registers, requeue it */
721                                         kbase_jd_atom *dequeued_katom = kbasep_jm_dequeue_tail_submit_slot(slot);
722                                         KBASE_DEBUG_ASSERT(dequeued_katom == katom);
723                                         jobs_submitted--;
724
725                                         /* Set the next registers to NULL */
726                                         kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_HEAD_NEXT_LO), 0, NULL);
727                                         kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_HEAD_NEXT_HI), 0, NULL);
728
729                                         /* As the job is removed from the next registers we undo the associated
730                                          * update to the job_chain_flag for the job slot. */
731                                         if (can_safely_stop)
732                                                 slot->job_chain_flag = !slot->job_chain_flag;
733
734                                         KBASE_TRACE_ADD_SLOT(kbdev, JM_SLOT_EVICT, dequeued_katom->kctx, dequeued_katom, dequeued_katom->jc, js);
735
736                                         /* Complete the job, indicate it took no time, but don't submit any more at this point */
737                                         kbase_jd_done(dequeued_katom, js, NULL, KBASE_JS_ATOM_DONE_EVICTED_FROM_NEXT);
738                                 } else {
739                                         /* The job transitioned into the current registers before we managed to evict it,
740                                          * in this case we fall back to soft/hard-stopping the job */
741                                         beenthere(kctx, "missed job in next register, soft/hard-stopping slot %d", js);
742                                         kbasep_job_slot_soft_or_hard_stop_do_action(kbdev, js, action, core_reqs, katom);
743                                 }
744                         }
745                 } else if (JM_JOB_IS_NEXT_JOB_INDEX(jobs_submitted - i)) {
746                         /* There's a job after this one, check to see if that job is in the next registers.
747              * If so, we need to pay attention to not accidently stop that one when issueing
748              * the command to stop the one pointed to by the head registers (as the one in the head
749              * may finish in the mean time and the one in the next moves to the head). Either the hardware
750                          * has support for this using job chain disambiguation or we need to evict the job
751                          * from the next registers first to ensure we can safely stop the one pointed to by
752                          * the head registers. */
753                         if (kbase_reg_read(kbdev, JOB_SLOT_REG(js, JSn_COMMAND_NEXT), NULL) != 0) {
754                                 kbase_jd_atom *check_next_atom;
755                                 /* It is - we should remove that job and soft/hard-stop the slot */
756
757                                 /* Only proceed when the next job isn't a HW workaround 'dummy' job
758                                  *
759                                  * This can't be an ASSERT due to MMU fault code:
760                                  * - This first hard-stops the job that caused the fault
761                                  * - Under HW Issue 8245, it will then reset the GPU
762                                  *  - This causes a Soft-stop to occur on all slots
763                                  * - By the time of the soft-stop, we may (depending on timing) still have:
764                                  *  - The original job in HEAD, if it's not finished the hard-stop
765                                  *  - The dummy workaround job in NEXT
766                                  *
767                                  * Other cases could be coded in future that cause back-to-back Soft/Hard
768                                  * stops with dummy workaround jobs in place, e.g. MMU handler code and Job
769                                  * Scheduler watchdog timer running in parallel.
770                                  *
771                                  * Note, the index i+1 is valid to peek from: i == jobs_submitted-2, therefore
772                                  * i+1 == jobs_submitted-1 */
773                                 check_next_atom = kbasep_jm_peek_idx_submit_slot(slot, i + 1);
774                                 if (kbasep_jm_is_dummy_workaround_job(kbdev, check_next_atom) != MALI_FALSE)
775                                         continue;
776
777                                 if (!can_safely_stop) {
778                                         beenthere(kctx, "clearing job from next registers on slot %d", js);
779                                         kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_COMMAND_NEXT), JSn_COMMAND_NOP, NULL);
780
781                                         /* Check to see if we did remove a job from the next registers */
782                                         if (kbase_reg_read(kbdev, JOB_SLOT_REG(js, JSn_HEAD_NEXT_LO), NULL) != 0 || kbase_reg_read(kbdev, JOB_SLOT_REG(js, JSn_HEAD_NEXT_HI), NULL) != 0) {
783                                                 /* We did remove a job from the next registers, requeue it */
784                                                 kbase_jd_atom *dequeued_katom = kbasep_jm_dequeue_tail_submit_slot(slot);
785                                                 KBASE_DEBUG_ASSERT(dequeued_katom != NULL);
786                                                 jobs_submitted--;
787
788                                                 /* Set the next registers to NULL */
789                                                 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_HEAD_NEXT_LO), 0, NULL);
790                                                 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_HEAD_NEXT_HI), 0, NULL);
791
792                                                 KBASE_TRACE_ADD_SLOT(kbdev, JM_SLOT_EVICT, dequeued_katom->kctx, dequeued_katom, dequeued_katom->jc, js);
793
794                                                 /* Complete the job, indicate it took no time, but don't submit any more at this point */
795                                                 kbase_jd_done(dequeued_katom, js, NULL, KBASE_JS_ATOM_DONE_EVICTED_FROM_NEXT);
796                                         } else {
797                                                 /* We missed the job, that means the job we're interested in left the hardware before
798                                                  * we managed to do anything, so we can proceed to the next job */
799                                                 continue;
800                                         }
801                                 }
802
803                                 /* Next is now free, so we can soft/hard-stop the slot */
804                                 beenthere(kctx, "soft/hard-stopped slot %d (there was a job in next which was successfully cleared)\n", js);
805                                 kbasep_job_slot_soft_or_hard_stop_do_action(kbdev, js, action, core_reqs, katom);
806                         }
807                         /* If there was no job in the next registers, then the job we were
808                          * interested in has finished, so we need not take any action
809                          */
810                 }
811         }
812
813         KBASE_TIMELINE_TRY_SOFT_STOP(kctx, js, 0);
814 }
815
816 void kbase_job_kill_jobs_from_context(kbase_context *kctx)
817 {
818         unsigned long flags;
819         kbase_device *kbdev;
820         kbasep_js_device_data *js_devdata;
821         int i;
822
823         KBASE_DEBUG_ASSERT(kctx != NULL);
824         kbdev = kctx->kbdev;
825         KBASE_DEBUG_ASSERT(kbdev != NULL);
826         js_devdata = &kbdev->js_data;
827
828         /* Cancel any remaining running jobs for this kctx  */
829         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
830
831         /* Invalidate all jobs in context, to prevent re-submitting */
832         for (i = 0; i < BASE_JD_ATOM_COUNT; i++)
833                 kctx->jctx.atoms[i].event_code = BASE_JD_EVENT_JOB_CANCELLED;
834
835         for (i = 0; i < kbdev->gpu_props.num_job_slots; i++)
836                 kbase_job_slot_hardstop(kctx, i, NULL);
837
838         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
839 }
840
841 void kbase_job_zap_context(kbase_context *kctx)
842 {
843         kbase_device *kbdev;
844         kbasep_js_device_data *js_devdata;
845         kbasep_js_kctx_info *js_kctx_info;
846         int i;
847         mali_bool evict_success;
848
849         KBASE_DEBUG_ASSERT(kctx != NULL);
850         kbdev = kctx->kbdev;
851         KBASE_DEBUG_ASSERT(kbdev != NULL);
852         js_devdata = &kbdev->js_data;
853         js_kctx_info = &kctx->jctx.sched_info;
854
855         /*
856          * Critical assumption: No more submission is possible outside of the
857          * workqueue. This is because the OS *must* prevent U/K calls (IOCTLs)
858          * whilst the kbase_context is terminating.
859          */
860
861         /* First, atomically do the following:
862          * - mark the context as dying
863          * - try to evict it from the policy queue */
864
865         mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
866         js_kctx_info->ctx.is_dying = MALI_TRUE;
867
868         dev_dbg(kbdev->dev, "Zap: Try Evict Ctx %p", kctx);
869         mutex_lock(&js_devdata->queue_mutex);
870         evict_success = kbasep_js_policy_try_evict_ctx(&js_devdata->policy, kctx);
871         mutex_unlock(&js_devdata->queue_mutex);
872
873         /*
874          * At this point we know:
875          * - If eviction succeeded, it was in the policy queue, but now no longer is
876          *  - We must cancel the jobs here. No Power Manager active reference to
877          * release.
878          *  - This happens asynchronously - kbase_jd_zap_context() will wait for
879          * those jobs to be killed.
880          * - If eviction failed, then it wasn't in the policy queue. It is one of
881          * the following:
882          *  - a. it didn't have any jobs, and so is not in the Policy Queue or the
883          * Run Pool (not scheduled)
884          *   - Hence, no more work required to cancel jobs. No Power Manager active
885          * reference to release.
886          *  - b. it was in the middle of a scheduling transaction (and thus must
887          * have at least 1 job). This can happen from a syscall or a kernel thread.
888          * We still hold the jsctx_mutex, and so the thread must be waiting inside
889          * kbasep_js_try_schedule_head_ctx(), before checking whether the runpool
890          * is full. That thread will continue after we drop the mutex, and will
891          * notice the context is dying. It will rollback the transaction, killing
892          * all jobs at the same time. kbase_jd_zap_context() will wait for those
893          * jobs to be killed.
894          *   - Hence, no more work required to cancel jobs, or to release the Power
895          * Manager active reference.
896          *  - c. it is scheduled, and may or may not be running jobs
897          * - We must cause it to leave the runpool by stopping it from submitting
898          * any more jobs. When it finally does leave,
899          * kbasep_js_runpool_requeue_or_kill_ctx() will kill all remaining jobs
900          * (because it is dying), release the Power Manager active reference, and
901          * will not requeue the context in the policy queue. kbase_jd_zap_context()
902          * will wait for those jobs to be killed.
903          *  - Hence, work required just to make it leave the runpool. Cancelling
904          * jobs and releasing the Power manager active reference will be handled
905          * when it leaves the runpool.
906          */
907
908         if (evict_success != MALI_FALSE || js_kctx_info->ctx.is_scheduled == MALI_FALSE) {
909                 /* The following events require us to kill off remaining jobs and
910                  * update PM book-keeping:
911                  * - we evicted it correctly (it must have jobs to be in the Policy Queue)
912                  *
913                  * These events need no action, but take this path anyway:
914                  * - Case a: it didn't have any jobs, and was never in the Queue
915                  * - Case b: scheduling transaction will be partially rolled-back (this
916                  * already cancels the jobs)
917                  */
918
919                 KBASE_TRACE_ADD(kbdev, JM_ZAP_NON_SCHEDULED, kctx, NULL, 0u, js_kctx_info->ctx.is_scheduled);
920
921                 dev_dbg(kbdev->dev, "Zap: Ctx %p evict_success=%d, scheduled=%d", kctx, evict_success, js_kctx_info->ctx.is_scheduled);
922
923                 if (evict_success != MALI_FALSE) {
924                         /* Only cancel jobs when we evicted from the policy queue. No Power
925                          * Manager active reference was held.
926                          *
927                          * Having is_dying set ensures that this kills, and doesn't requeue */
928                         kbasep_js_runpool_requeue_or_kill_ctx(kbdev, kctx, MALI_FALSE);
929                 }
930                 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
931         } else {
932                 unsigned long flags;
933                 mali_bool was_retained;
934                 /* Case c: didn't evict, but it is scheduled - it's in the Run Pool */
935                 KBASE_TRACE_ADD(kbdev, JM_ZAP_SCHEDULED, kctx, NULL, 0u, js_kctx_info->ctx.is_scheduled);
936                 dev_dbg(kbdev->dev, "Zap: Ctx %p is in RunPool", kctx);
937
938                 /* Disable the ctx from submitting any more jobs */
939                 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
940                 kbasep_js_clear_submit_allowed(js_devdata, kctx);
941
942                 /* Retain and (later) release the context whilst it is is now disallowed from submitting
943                  * jobs - ensures that someone somewhere will be removing the context later on */
944                 was_retained = kbasep_js_runpool_retain_ctx_nolock(kbdev, kctx);
945
946                 /* Since it's scheduled and we have the jsctx_mutex, it must be retained successfully */
947                 KBASE_DEBUG_ASSERT(was_retained != MALI_FALSE);
948
949                 dev_dbg(kbdev->dev, "Zap: Ctx %p Kill Any Running jobs", kctx);
950                 /* Cancel any remaining running jobs for this kctx - if any. Submit is disallowed
951                  * which takes effect immediately, so no more new jobs will appear after we do this.  */
952                 for (i = 0; i < kbdev->gpu_props.num_job_slots; i++)
953                         kbase_job_slot_hardstop(kctx, i, NULL);
954
955                 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
956                 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
957
958                 dev_dbg(kbdev->dev, "Zap: Ctx %p Release (may or may not schedule out immediately)", kctx);
959                 kbasep_js_runpool_release_ctx(kbdev, kctx);
960         }
961         KBASE_TRACE_ADD(kbdev, JM_ZAP_DONE, kctx, NULL, 0u, 0u);
962
963         /* After this, you must wait on both the kbase_jd_context::zero_jobs_wait
964          * and the kbasep_js_kctx_info::ctx::is_scheduled_waitq - to wait for the
965          * jobs to be destroyed, and the context to be de-scheduled (if it was on
966          * the runpool).
967          *
968          * kbase_jd_zap_context() will do this. */
969 }
970 KBASE_EXPORT_TEST_API(kbase_job_zap_context)
971
972 mali_error kbase_job_slot_init(kbase_device *kbdev)
973 {
974         int i;
975         KBASE_DEBUG_ASSERT(kbdev);
976
977         for (i = 0; i < kbdev->gpu_props.num_job_slots; i++)
978                 kbasep_jm_init_submit_slot(&kbdev->jm_slots[i]);
979
980         return MALI_ERROR_NONE;
981 }
982 KBASE_EXPORT_TEST_API(kbase_job_slot_init)
983
984 void kbase_job_slot_halt(kbase_device *kbdev)
985 {
986         CSTD_UNUSED(kbdev);
987 }
988
989 void kbase_job_slot_term(kbase_device *kbdev)
990 {
991         KBASE_DEBUG_ASSERT(kbdev);
992 }
993 KBASE_EXPORT_TEST_API(kbase_job_slot_term)
994
995 /**
996  * Soft-stop the specified job slot
997  *
998  * The job slot lock must be held when calling this function.
999  * The job slot must not already be in the process of being soft-stopped.
1000  *
1001  * Where possible any job in the next register is evicted before the soft-stop.
1002  *
1003  * @param kbdev         The kbase device
1004  * @param js            The job slot to soft-stop
1005  * @param target_katom  The job that should be soft-stopped (or NULL for any job)
1006  */
1007 void kbase_job_slot_softstop(kbase_device *kbdev, int js, kbase_jd_atom *target_katom)
1008 {
1009         kbasep_job_slot_soft_or_hard_stop(kbdev, NULL, js, target_katom, JSn_COMMAND_SOFT_STOP);
1010 }
1011
1012 /**
1013  * Hard-stop the specified job slot
1014  *
1015  * The job slot lock must be held when calling this function.
1016  *
1017  * @param kctx          The kbase context that contains the job(s) that should
1018  *                      be hard-stopped
1019  * @param js            The job slot to hard-stop
1020  * @param target_katom  The job that should be hard-stopped (or NULL for all
1021  *                      jobs from the context)
1022  */
1023 void kbase_job_slot_hardstop(kbase_context *kctx, int js,
1024                                 kbase_jd_atom *target_katom)
1025 {
1026         kbase_device *kbdev = kctx->kbdev;
1027
1028         kbasep_job_slot_soft_or_hard_stop(kbdev, kctx, js, target_katom,
1029                                                 JSn_COMMAND_HARD_STOP);
1030         if (kbase_hw_has_issue(kctx->kbdev, BASE_HW_ISSUE_8401) ||
1031                 kbase_hw_has_issue(kctx->kbdev, BASE_HW_ISSUE_9510) ||
1032                 (kbase_hw_has_issue(kctx->kbdev, BASE_HW_ISSUE_T76X_3542) &&
1033                 (target_katom == NULL || target_katom->core_req & BASE_JD_REQ_FS_AFBC))) {
1034                 /* MIDBASE-2916 if a fragment job with AFBC encoding is
1035                  * hardstopped, ensure to do a soft reset also in order to
1036                  * clear the GPU status.
1037                  * Workaround for HW issue 8401 has an issue,so after
1038                  * hard-stopping just reset the GPU. This will ensure that the
1039                  * jobs leave the GPU.*/
1040                 if (kbase_prepare_to_reset_gpu_locked(kbdev)) {
1041                         dev_err(kbdev->dev, "Issueing GPU\
1042                         soft-reset after hard stopping due to hardware issue");
1043                         kbase_reset_gpu_locked(kbdev);
1044                 }
1045         }
1046 }
1047
1048
1049 void kbase_debug_dump_registers(kbase_device *kbdev)
1050 {
1051         int i;
1052         dev_err(kbdev->dev, "Register state:");
1053         dev_err(kbdev->dev, "  GPU_IRQ_RAWSTAT=0x%08x GPU_STATUS=0x%08x",
1054                 kbase_reg_read(kbdev, GPU_CONTROL_REG(GPU_IRQ_RAWSTAT), NULL),
1055                 kbase_reg_read(kbdev, GPU_CONTROL_REG(GPU_STATUS), NULL));
1056         dev_err(kbdev->dev, "  JOB_IRQ_RAWSTAT=0x%08x JOB_IRQ_JS_STATE=0x%08x JOB_IRQ_THROTTLE=0x%08x",
1057                 kbase_reg_read(kbdev, JOB_CONTROL_REG(JOB_IRQ_RAWSTAT), NULL),
1058                 kbase_reg_read(kbdev, JOB_CONTROL_REG(JOB_IRQ_JS_STATE), NULL),
1059                 kbase_reg_read(kbdev, JOB_CONTROL_REG(JOB_IRQ_THROTTLE), NULL));
1060         for (i = 0; i < 3; i++) {
1061                 dev_err(kbdev->dev, "  JS%d_STATUS=0x%08x      JS%d_HEAD_LO=0x%08x",
1062                         i, kbase_reg_read(kbdev, JOB_SLOT_REG(i, JSn_STATUS),
1063                                         NULL),
1064                         i, kbase_reg_read(kbdev, JOB_SLOT_REG(i, JSn_HEAD_LO),
1065                                         NULL));
1066         }
1067         dev_err(kbdev->dev, "  MMU_IRQ_RAWSTAT=0x%08x GPU_FAULTSTATUS=0x%08x",
1068                 kbase_reg_read(kbdev, MMU_REG(MMU_IRQ_RAWSTAT), NULL),
1069                 kbase_reg_read(kbdev, GPU_CONTROL_REG(GPU_FAULTSTATUS), NULL));
1070         dev_err(kbdev->dev, "  GPU_IRQ_MASK=0x%08x    JOB_IRQ_MASK=0x%08x     MMU_IRQ_MASK=0x%08x",
1071                 kbase_reg_read(kbdev, GPU_CONTROL_REG(GPU_IRQ_MASK), NULL),
1072                 kbase_reg_read(kbdev, JOB_CONTROL_REG(JOB_IRQ_MASK), NULL),
1073                 kbase_reg_read(kbdev, MMU_REG(MMU_IRQ_MASK), NULL));
1074         dev_err(kbdev->dev, "  PWR_OVERRIDE0=0x%08x   PWR_OVERRIDE1=0x%08x",
1075                 kbase_reg_read(kbdev, GPU_CONTROL_REG(PWR_OVERRIDE0), NULL),
1076                 kbase_reg_read(kbdev, GPU_CONTROL_REG(PWR_OVERRIDE1), NULL));
1077         dev_err(kbdev->dev, "  SHADER_CONFIG=0x%08x   L2_MMU_CONFIG=0x%08x",
1078                 kbase_reg_read(kbdev, GPU_CONTROL_REG(SHADER_CONFIG), NULL),
1079                 kbase_reg_read(kbdev, GPU_CONTROL_REG(L2_MMU_CONFIG), NULL));
1080 }
1081
1082 void kbasep_reset_timeout_worker(struct work_struct *data)
1083 {
1084         unsigned long flags;
1085         kbase_device *kbdev;
1086         int i;
1087         ktime_t end_timestamp = ktime_get();
1088         kbasep_js_device_data *js_devdata;
1089         kbase_uk_hwcnt_setup hwcnt_setup = { {0} };
1090         kbase_instr_state bckp_state;
1091
1092         KBASE_DEBUG_ASSERT(data);
1093
1094         kbdev = container_of(data, kbase_device, reset_work);
1095
1096         KBASE_DEBUG_ASSERT(kbdev);
1097         js_devdata = &kbdev->js_data;
1098
1099         KBASE_TRACE_ADD(kbdev, JM_BEGIN_RESET_WORKER, NULL, NULL, 0u, 0);
1100
1101         /* Make sure the timer has completed - this cannot be done from interrupt context,
1102          * so this cannot be done within kbasep_try_reset_gpu_early. */
1103         hrtimer_cancel(&kbdev->reset_timer);
1104
1105         if (kbase_pm_context_active_handle_suspend(kbdev, KBASE_PM_SUSPEND_HANDLER_DONT_REACTIVATE)) {
1106                 /* This would re-activate the GPU. Since it's already idle, there's no
1107                  * need to reset it */
1108                 atomic_set(&kbdev->reset_gpu, KBASE_RESET_GPU_NOT_PENDING);
1109                 wake_up(&kbdev->reset_wait);
1110                 return;
1111         }
1112
1113         mutex_lock(&kbdev->pm.lock);
1114         /* We hold the pm lock, so there ought to be a current policy */
1115         KBASE_DEBUG_ASSERT(kbdev->pm.pm_current_policy);
1116
1117         /* All slot have been soft-stopped and we've waited SOFT_STOP_RESET_TIMEOUT for the slots to clear, at this point
1118          * we assume that anything that is still left on the GPU is stuck there and we'll kill it when we reset the GPU */
1119
1120         dev_err(kbdev->dev, "Resetting GPU (allowing up to %d ms)", RESET_TIMEOUT);
1121
1122         spin_lock_irqsave(&kbdev->hwcnt.lock, flags);
1123
1124         if (kbdev->hwcnt.state == KBASE_INSTR_STATE_RESETTING) {        /*the same interrupt handler preempted itself */
1125                 /* GPU is being reset */
1126                 spin_unlock_irqrestore(&kbdev->hwcnt.lock, flags);
1127                 wait_event(kbdev->hwcnt.wait, kbdev->hwcnt.triggered != 0);
1128                 spin_lock_irqsave(&kbdev->hwcnt.lock, flags);
1129         }
1130         /* Save the HW counters setup */
1131         if (kbdev->hwcnt.kctx != NULL) {
1132                 kbase_context *kctx = kbdev->hwcnt.kctx;
1133                 hwcnt_setup.dump_buffer = kbase_reg_read(kbdev, GPU_CONTROL_REG(PRFCNT_BASE_LO), kctx) & 0xffffffff;
1134                 hwcnt_setup.dump_buffer |= (mali_addr64) kbase_reg_read(kbdev, GPU_CONTROL_REG(PRFCNT_BASE_HI), kctx) << 32;
1135                 hwcnt_setup.jm_bm = kbase_reg_read(kbdev, GPU_CONTROL_REG(PRFCNT_JM_EN), kctx);
1136                 hwcnt_setup.shader_bm = kbase_reg_read(kbdev, GPU_CONTROL_REG(PRFCNT_SHADER_EN), kctx);
1137                 hwcnt_setup.tiler_bm = kbase_reg_read(kbdev, GPU_CONTROL_REG(PRFCNT_TILER_EN), kctx);
1138                 hwcnt_setup.l3_cache_bm = kbase_reg_read(kbdev, GPU_CONTROL_REG(PRFCNT_L3_CACHE_EN), kctx);
1139                 hwcnt_setup.mmu_l2_bm = kbase_reg_read(kbdev, GPU_CONTROL_REG(PRFCNT_MMU_L2_EN), kctx);
1140         }
1141
1142         /* Output the state of some interesting registers to help in the
1143          * debugging of GPU resets */
1144         kbase_debug_dump_registers(kbdev);
1145
1146         bckp_state = kbdev->hwcnt.state;
1147         kbdev->hwcnt.state = KBASE_INSTR_STATE_RESETTING;
1148         kbdev->hwcnt.triggered = 0;
1149         /* Disable IRQ to avoid IRQ handlers to kick in after releaseing the spinlock;
1150          * this also clears any outstanding interrupts */
1151         kbase_pm_disable_interrupts(kbdev);
1152         /* Ensure that any IRQ handlers have finished */
1153         kbase_synchronize_irqs(kbdev);
1154         spin_unlock_irqrestore(&kbdev->hwcnt.lock, flags);
1155
1156         /* Reset the GPU */
1157         kbase_pm_init_hw(kbdev, MALI_TRUE);
1158         /* IRQs were re-enabled by kbase_pm_init_hw, and GPU is still powered */
1159
1160         spin_lock_irqsave(&kbdev->hwcnt.lock, flags);
1161         /* Restore the HW counters setup */
1162         if (kbdev->hwcnt.kctx != NULL) {
1163                 kbase_context *kctx = kbdev->hwcnt.kctx;
1164                 kbase_reg_write(kbdev, GPU_CONTROL_REG(PRFCNT_CONFIG), (kctx->as_nr << PRFCNT_CONFIG_AS_SHIFT) | PRFCNT_CONFIG_MODE_OFF, kctx);
1165                 kbase_reg_write(kbdev, GPU_CONTROL_REG(PRFCNT_BASE_LO),     hwcnt_setup.dump_buffer & 0xFFFFFFFF, kctx);
1166                 kbase_reg_write(kbdev, GPU_CONTROL_REG(PRFCNT_BASE_HI),     hwcnt_setup.dump_buffer >> 32,        kctx);
1167                 kbase_reg_write(kbdev, GPU_CONTROL_REG(PRFCNT_JM_EN),       hwcnt_setup.jm_bm,                    kctx);
1168                 kbase_reg_write(kbdev, GPU_CONTROL_REG(PRFCNT_SHADER_EN),   hwcnt_setup.shader_bm,                kctx);
1169                 kbase_reg_write(kbdev, GPU_CONTROL_REG(PRFCNT_L3_CACHE_EN), hwcnt_setup.l3_cache_bm,              kctx);
1170                 kbase_reg_write(kbdev, GPU_CONTROL_REG(PRFCNT_MMU_L2_EN),   hwcnt_setup.mmu_l2_bm,                kctx);
1171
1172                 /* Due to PRLAM-8186 we need to disable the Tiler before we enable the HW counter dump. */
1173                 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8186))
1174                         kbase_reg_write(kbdev, GPU_CONTROL_REG(PRFCNT_TILER_EN), 0, kctx);
1175                 else
1176                         kbase_reg_write(kbdev, GPU_CONTROL_REG(PRFCNT_TILER_EN), hwcnt_setup.tiler_bm, kctx);
1177
1178                 kbase_reg_write(kbdev, GPU_CONTROL_REG(PRFCNT_CONFIG), (kctx->as_nr << PRFCNT_CONFIG_AS_SHIFT) | PRFCNT_CONFIG_MODE_MANUAL, kctx);
1179
1180                 /* If HW has PRLAM-8186 we can now re-enable the tiler HW counters dump */
1181                 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8186))
1182                         kbase_reg_write(kbdev, GPU_CONTROL_REG(PRFCNT_TILER_EN), hwcnt_setup.tiler_bm, kctx);
1183         }
1184         kbdev->hwcnt.state = bckp_state;
1185         switch(kbdev->hwcnt.state) {
1186         /* Cases for waking kbasep_cache_clean_worker worker */
1187         case KBASE_INSTR_STATE_CLEANED:
1188                 /* Cache-clean IRQ occurred, but we reset:
1189                  * Wakeup incase the waiter saw RESETTING */
1190         case KBASE_INSTR_STATE_REQUEST_CLEAN:
1191                 /* After a clean was requested, but before the regs were written:
1192                  * Wakeup incase the waiter saw RESETTING */
1193                 wake_up(&kbdev->hwcnt.cache_clean_wait);
1194                 break;
1195         case KBASE_INSTR_STATE_CLEANING:
1196                 /* Either:
1197                  * 1) We've not got the Cache-clean IRQ yet: it was lost, or:
1198                  * 2) We got it whilst resetting: it was voluntarily lost
1199                  *
1200                  * So, move to the next state and wakeup: */
1201                 kbdev->hwcnt.state = KBASE_INSTR_STATE_CLEANED;
1202                 wake_up(&kbdev->hwcnt.cache_clean_wait);
1203                 break;
1204
1205         /* Cases for waking anyone else */
1206         case KBASE_INSTR_STATE_DUMPING:
1207                 /* If dumping, abort the dump, because we may've lost the IRQ */
1208                 kbdev->hwcnt.state = KBASE_INSTR_STATE_IDLE;
1209                 kbdev->hwcnt.triggered = 1;
1210                 wake_up(&kbdev->hwcnt.wait);
1211                 break;
1212         case KBASE_INSTR_STATE_DISABLED:
1213         case KBASE_INSTR_STATE_IDLE:
1214         case KBASE_INSTR_STATE_FAULT:
1215                 /* Every other reason: wakeup in that state */
1216                 kbdev->hwcnt.triggered = 1;
1217                 wake_up(&kbdev->hwcnt.wait);
1218                 break;
1219
1220         /* Unhandled cases */
1221         case KBASE_INSTR_STATE_RESETTING:
1222         default:
1223                 BUG();
1224                 break;
1225         }
1226         spin_unlock_irqrestore(&kbdev->hwcnt.lock, flags);
1227
1228         /* Complete any jobs that were still on the GPU */
1229         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1230         for (i = 0; i < kbdev->gpu_props.num_job_slots; i++) {
1231                 int nr_done;
1232                 kbase_jm_slot *slot = &kbdev->jm_slots[i];
1233
1234                 nr_done = kbasep_jm_nr_jobs_submitted(slot);
1235                 while (nr_done) {
1236                         dev_err(kbdev->dev, "Job stuck in slot %d on the GPU was cancelled", i);
1237                         kbase_job_done_slot(kbdev, i, BASE_JD_EVENT_JOB_CANCELLED, 0, &end_timestamp);
1238                         nr_done--;
1239                 }
1240         }
1241         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1242
1243         mutex_lock(&js_devdata->runpool_mutex);
1244
1245         /* Reprogram the GPU's MMU */
1246         for (i = 0; i < BASE_MAX_NR_AS; i++) {
1247                 if (js_devdata->runpool_irq.per_as_data[i].kctx) {
1248                         kbase_as *as = &kbdev->as[i];
1249                         mutex_lock(&as->transaction_mutex);
1250                         kbase_mmu_update(js_devdata->runpool_irq.per_as_data[i].kctx);
1251                         mutex_unlock(&as->transaction_mutex);
1252                 }
1253         }
1254
1255         atomic_set(&kbdev->reset_gpu, KBASE_RESET_GPU_NOT_PENDING);
1256         wake_up(&kbdev->reset_wait);
1257         dev_err(kbdev->dev, "Reset complete");
1258
1259         /* Find out what cores are required now */
1260         kbase_pm_update_cores_state(kbdev);
1261
1262         /* Synchronously request and wait for those cores, because if
1263          * instrumentation is enabled it would need them immediately. */
1264         kbase_pm_check_transitions_sync(kbdev);
1265
1266         /* Try submitting some jobs to restart processing */
1267         if (js_devdata->nr_user_contexts_running > 0) {
1268                 KBASE_TRACE_ADD(kbdev, JM_SUBMIT_AFTER_RESET, NULL, NULL, 0u, 0);
1269
1270                 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1271                 kbasep_js_try_run_next_job_nolock(kbdev);
1272                 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1273         }
1274         mutex_unlock(&js_devdata->runpool_mutex);
1275         mutex_unlock(&kbdev->pm.lock);
1276
1277         kbase_pm_context_idle(kbdev);
1278         KBASE_TRACE_ADD(kbdev, JM_END_RESET_WORKER, NULL, NULL, 0u, 0);
1279 }
1280
1281 enum hrtimer_restart kbasep_reset_timer_callback(struct hrtimer *timer)
1282 {
1283         kbase_device *kbdev = container_of(timer, kbase_device, reset_timer);
1284
1285         KBASE_DEBUG_ASSERT(kbdev);
1286
1287         /* Reset still pending? */
1288         if (atomic_cmpxchg(&kbdev->reset_gpu, KBASE_RESET_GPU_COMMITTED, KBASE_RESET_GPU_HAPPENING) == KBASE_RESET_GPU_COMMITTED)
1289                 queue_work(kbdev->reset_workq, &kbdev->reset_work);
1290
1291         return HRTIMER_NORESTART;
1292 }
1293
1294 /*
1295  * If all jobs are evicted from the GPU then we can reset the GPU
1296  * immediately instead of waiting for the timeout to elapse
1297  */
1298
1299 static void kbasep_try_reset_gpu_early_locked(kbase_device *kbdev)
1300 {
1301         int i;
1302         int pending_jobs = 0;
1303
1304         KBASE_DEBUG_ASSERT(kbdev);
1305
1306         /* Count the number of jobs */
1307         for (i = 0; i < kbdev->gpu_props.num_job_slots; i++) {
1308                 kbase_jm_slot *slot = &kbdev->jm_slots[i];
1309                 pending_jobs += kbasep_jm_nr_jobs_submitted(slot);
1310         }
1311
1312         if (pending_jobs > 0) {
1313                 /* There are still jobs on the GPU - wait */
1314                 return;
1315         }
1316
1317         /* Check that the reset has been committed to (i.e. kbase_reset_gpu has been called), and that no other
1318          * thread beat this thread to starting the reset */
1319         if (atomic_cmpxchg(&kbdev->reset_gpu, KBASE_RESET_GPU_COMMITTED, KBASE_RESET_GPU_HAPPENING) != KBASE_RESET_GPU_COMMITTED) {
1320                 /* Reset has already occurred */
1321                 return;
1322         }
1323         queue_work(kbdev->reset_workq, &kbdev->reset_work);
1324 }
1325
1326 static void kbasep_try_reset_gpu_early(kbase_device *kbdev)
1327 {
1328         unsigned long flags;
1329         kbasep_js_device_data *js_devdata;
1330
1331         js_devdata = &kbdev->js_data;
1332         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1333         kbasep_try_reset_gpu_early_locked(kbdev);
1334         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1335 }
1336
1337 /*
1338  * Prepare for resetting the GPU.
1339  * This function just soft-stops all the slots to ensure that as many jobs as possible are saved.
1340  *
1341  * The function returns a boolean which should be interpreted as follows:
1342  * - MALI_TRUE - Prepared for reset, kbase_reset_gpu should be called.
1343  * - MALI_FALSE - Another thread is performing a reset, kbase_reset_gpu should not be called.
1344  *
1345  * @return See description
1346  */
1347 mali_bool kbase_prepare_to_reset_gpu_locked(kbase_device *kbdev)
1348 {
1349         int i;
1350
1351         KBASE_DEBUG_ASSERT(kbdev);
1352
1353         if (atomic_cmpxchg(&kbdev->reset_gpu, KBASE_RESET_GPU_NOT_PENDING, KBASE_RESET_GPU_PREPARED) != KBASE_RESET_GPU_NOT_PENDING) {
1354                 /* Some other thread is already resetting the GPU */
1355                 return MALI_FALSE;
1356         }
1357
1358         for (i = 0; i < kbdev->gpu_props.num_job_slots; i++)
1359                 kbase_job_slot_softstop(kbdev, i, NULL);
1360
1361         return MALI_TRUE;
1362 }
1363
1364 mali_bool kbase_prepare_to_reset_gpu(kbase_device *kbdev)
1365 {
1366         unsigned long flags;
1367         mali_bool ret;
1368         kbasep_js_device_data *js_devdata;
1369
1370         js_devdata = &kbdev->js_data;
1371         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1372         ret = kbase_prepare_to_reset_gpu_locked(kbdev);
1373         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1374
1375         return ret;
1376 }
1377 KBASE_EXPORT_TEST_API(kbase_prepare_to_reset_gpu)
1378
1379 /*
1380  * This function should be called after kbase_prepare_to_reset_gpu iff it returns MALI_TRUE.
1381  * It should never be called without a corresponding call to kbase_prepare_to_reset_gpu.
1382  *
1383  * After this function is called (or not called if kbase_prepare_to_reset_gpu returned MALI_FALSE),
1384  * the caller should wait for kbdev->reset_waitq to be signalled to know when the reset has completed.
1385  */
1386 void kbase_reset_gpu(kbase_device *kbdev)
1387 {
1388         u32 timeout_ms;
1389
1390         KBASE_DEBUG_ASSERT(kbdev);
1391
1392         /* Note this is an assert/atomic_set because it is a software issue for a race to be occuring here */
1393         KBASE_DEBUG_ASSERT(atomic_read(&kbdev->reset_gpu) == KBASE_RESET_GPU_PREPARED);
1394         atomic_set(&kbdev->reset_gpu, KBASE_RESET_GPU_COMMITTED);
1395
1396         timeout_ms = kbasep_get_config_value(kbdev, kbdev->config_attributes, KBASE_CONFIG_ATTR_JS_RESET_TIMEOUT_MS);
1397         dev_err(kbdev->dev, "Preparing to soft-reset GPU: Waiting (upto %d ms) for all jobs to complete soft-stop\n", timeout_ms);
1398         hrtimer_start(&kbdev->reset_timer, HR_TIMER_DELAY_MSEC(timeout_ms), HRTIMER_MODE_REL);
1399
1400         /* Try resetting early */
1401         kbasep_try_reset_gpu_early(kbdev);
1402 }
1403 KBASE_EXPORT_TEST_API(kbase_reset_gpu)
1404
1405 void kbase_reset_gpu_locked(kbase_device *kbdev)
1406 {
1407         u32 timeout_ms;
1408
1409         KBASE_DEBUG_ASSERT(kbdev);
1410
1411         /* Note this is an assert/atomic_set because it is a software issue for a race to be occuring here */
1412         KBASE_DEBUG_ASSERT(atomic_read(&kbdev->reset_gpu) == KBASE_RESET_GPU_PREPARED);
1413         atomic_set(&kbdev->reset_gpu, KBASE_RESET_GPU_COMMITTED);
1414
1415         timeout_ms = kbasep_get_config_value(kbdev, kbdev->config_attributes, KBASE_CONFIG_ATTR_JS_RESET_TIMEOUT_MS);
1416         dev_err(kbdev->dev, "Preparing to soft-reset GPU: Waiting (upto %d ms) for all jobs to complete soft-stop\n", timeout_ms);
1417         hrtimer_start(&kbdev->reset_timer, HR_TIMER_DELAY_MSEC(timeout_ms), HRTIMER_MODE_REL);
1418
1419         /* Try resetting early */
1420         kbasep_try_reset_gpu_early_locked(kbdev);
1421 }