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