3 * (C) COPYRIGHT ARM Limited. All rights reserved.
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
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.
21 * @file mali_kbase_jm.c
22 * Base kernel job manager APIs
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>
31 #include "mali_kbase_jm.h"
33 #define beenthere(kctx, f, a...) dev_dbg(kctx->kbdev->dev, "%s:" f, __func__, ##a)
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;
42 static void kbasep_try_reset_gpu_early(kbase_device *kbdev);
44 #ifdef CONFIG_GPU_TRACEPOINTS
45 static char *kbasep_make_job_slot_string(int js, char *js_string)
47 sprintf(js_string, "job_slot_%i", js);
52 static void kbase_job_hw_submit(kbase_device *kbdev, kbase_jd_atom *katom, int js)
56 u64 jc_head = katom->jc;
58 KBASE_DEBUG_ASSERT(kbdev);
59 KBASE_DEBUG_ASSERT(katom);
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));
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);
72 #ifdef CONFIG_MALI_DEBUG_SHADER_SPLIT_FS
79 mask = mali_js0_affinity_mask;
83 mask = mali_js1_affinity_mask;
87 mask = mali_js2_affinity_mask;
90 value = katom->affinity & (mask & 0xFFFFFFFF);
92 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_AFFINITY_NEXT_LO), value, kctx);
94 value = (katom->affinity >> 32) & ((mask>>32) & 0xFFFFFFFF);
95 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_AFFINITY_NEXT_HI), value, kctx);
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);
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);
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;
111 katom->atom_flags &= ~KBASE_KATOM_FLAGS_JOBCHAIN;
112 kbdev->jm_slots[js].job_chain_flag = MALI_FALSE;
116 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_CONFIG_NEXT), cfg, kctx);
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();
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);
127 KBASE_TRACE_ADD_SLOT_INFO(kbdev, JM_SUBMIT, kctx, katom, jc_head, js, (u32) katom->affinity);
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)
135 /* If this is the only job on the slot, trace it as starting */
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;
141 kbase_timeline_job_slot_submit(kbdev, kctx, katom, js);
143 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_COMMAND_NEXT), JSn_COMMAND_START, katom->kctx);
146 void kbase_job_submit_nolock(kbase_device *kbdev, kbase_jd_atom *katom, int js)
148 kbase_jm_slot *jm_slots;
149 base_jd_core_req core_req;
151 KBASE_DEBUG_ASSERT(kbdev);
152 KBASE_DEBUG_ASSERT(katom);
154 jm_slots = kbdev->jm_slots;
156 core_req = katom->core_req;
157 if (core_req & BASE_JD_REQ_ONLY_COMPUTE) {
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);
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);
176 * - one job already done (pending interrupt),
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...
182 kbasep_jm_enqueue_submit_slot(&jm_slots[js], katom);
183 kbase_job_hw_submit(kbdev, katom, js);
186 void kbase_job_done_slot(kbase_device *kbdev, int s, u32 completion_code, u64 job_tail, ktime_t *end_timestamp)
189 kbase_jd_atom *katom;
193 KBASE_DEBUG_ASSERT(kbdev);
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);
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() */
203 slot = &kbdev->jm_slots[s];
204 katom = kbasep_jm_dequeue_submit_slot(slot);
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);
215 KBASE_TRACE_ADD_SLOT_INFO(kbdev, JM_JOB_DONE, kctx, katom, jc_head, s, completion_code);
217 if (completion_code != BASE_JD_EVENT_DONE && completion_code != BASE_JD_EVENT_STOPPED) {
219 #if KBASE_TRACE_DUMP_ON_JOB_SLOT_ERROR != 0
220 KBASE_TRACE_DUMP(kbdev);
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;
228 KBASE_TRACE_ADD_SLOT(kbdev, JM_UPDATE_HEAD, kctx, katom, job_tail, s);
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;
235 kbase_device_trace_register_access(kctx, REG_WRITE, JOB_CONTROL_REG(JOB_IRQ_CLEAR), 1 << s);
237 /* Complete the job, and start new ones
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.
244 #ifdef CONFIG_GPU_TRACEPOINTS
245 if (kbasep_jm_nr_jobs_submitted(slot) != 0) {
246 kbase_jd_atom *katom;
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;
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;
257 kbase_jd_done(katom, s, end_timestamp, KBASE_JS_ATOM_DONE_START_NEW_ATOMS);
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.
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)
268 STATIC void kbasep_job_slot_update_head_start_timestamp(kbase_device *kbdev, kbase_jm_slot *slot, ktime_t end_timestamp)
270 KBASE_DEBUG_ASSERT(slot);
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 */
278 KBASE_DEBUG_ASSERT(katom != NULL);
280 if (kbasep_jm_is_dummy_workaround_job(kbdev, katom) != MALI_FALSE) {
281 /* Don't access the members of HW workaround 'dummy' jobs */
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;
297 void kbase_job_done(kbase_device *kbdev, u32 done)
302 ktime_t end_timestamp = ktime_get();
303 kbasep_js_device_data *js_devdata;
305 KBASE_DEBUG_ASSERT(kbdev);
306 js_devdata = &kbdev->js_data;
308 KBASE_TRACE_ADD(kbdev, JM_IRQ, NULL, NULL, 0, done);
310 memset(&kbdev->slot_submit_count_irq[0], 0, sizeof(kbdev->slot_submit_count_irq));
312 /* write irq throttle register, this will prevent irqs from occurring until
313 * the given number of gpu clock cycles have passed */
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);
319 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
323 u32 failed = done >> 16;
325 /* treat failed slots as finished slots */
326 u32 finished = (done & 0xFFFF) | failed;
328 /* Note: This is inherently unfair, as we always check
329 * for lower numbered interrupts before the higher
331 i = ffs(finished) - 1;
332 KBASE_DEBUG_ASSERT(i >= 0);
334 slot = &kbdev->jm_slots[i];
339 u32 completion_code = BASE_JD_EVENT_DONE; /* assume OK */
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);
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);
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;
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++;
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);
367 if (((active >> i) & 1) == 0 && (((done >> (i + 16)) & 1) == 0)) {
368 /* There is a potential race we must work around:
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.
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).
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).
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.
391 u32 rawstat = kbase_reg_read(kbdev, JOB_CONTROL_REG(JOB_IRQ_RAWSTAT), NULL);
393 if ((rawstat >> (i + 16)) & 1) {
394 /* There is a failed job that we've missed - add it back to active */
399 dev_dbg(kbdev->dev, "Job ended with status 0x%08X\n", completion_code);
401 nr_done = kbasep_jm_nr_jobs_submitted(slot);
402 nr_done -= (active >> i) & 1;
403 nr_done -= (active >> (i + 16)) & 1;
406 dev_warn(kbdev->dev, "Spurious interrupt on slot %d", i);
414 kbase_job_done_slot(kbdev, i, completion_code, job_tail, &end_timestamp);
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.
420 kbase_job_done_slot(kbdev, i, BASE_JD_EVENT_DONE, 0, &end_timestamp);
426 done = kbase_reg_read(kbdev, JOB_CONTROL_REG(JOB_IRQ_RAWSTAT), NULL);
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 */
437 finished = (done & 0xFFFF) | failed;
438 } while (finished & (1 << i));
440 kbasep_job_slot_update_head_start_timestamp(kbdev, slot, end_timestamp);
442 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
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
448 kbasep_try_reset_gpu_early(kbdev);
451 KBASE_TRACE_ADD(kbdev, JM_IRQ_END, NULL, NULL, 0, count);
453 KBASE_EXPORT_TEST_API(kbase_job_done)
455 static mali_bool kbasep_soft_stop_allowed(kbase_device *kbdev, u16 core_reqs)
457 mali_bool soft_stops_allowed = MALI_TRUE;
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;
463 return soft_stops_allowed;
466 static mali_bool kbasep_hard_stop_allowed(kbase_device *kbdev, u16 core_reqs)
468 mali_bool hard_stops_allowed = MALI_TRUE;
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;
474 return hard_stops_allowed;
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 )
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;
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);
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 */
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;
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.
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.
514 * * An (unhandled) MMU fault occurred. As long as BASE_HW_ISSUE_8245 is defined then
515 * the GPU will be reset.
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.
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);
525 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8316) && action == JSn_COMMAND_SOFT_STOP) {
528 slot = &kbdev->jm_slots[js];
530 for (i = 0; i < kbasep_jm_nr_jobs_submitted(slot); i++) {
531 kbase_jd_atom *katom;
533 katom = kbasep_jm_peek_idx_submit_slot(slot, i);
535 KBASE_DEBUG_ASSERT(katom);
537 if (kbasep_jm_is_dummy_workaround_job(kbdev, katom) != MALI_FALSE) {
538 /* Don't access the members of HW workaround 'dummy' jobs
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) */
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.
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);
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;
566 action = (target_katom->atom_flags & KBASE_KATOM_FLAGS_JOBCHAIN) ?
567 JSn_COMMAND_HARD_STOP_1:
568 JSn_COMMAND_HARD_STOP_0;
571 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JSn_COMMAND), action, kctx);
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) {
578 kbase_context *head_kctx;
580 slot = &kbdev->jm_slots[js];
581 head = kbasep_jm_peek_idx_submit_slot(slot, slot->submitted_nr - 1);
582 head_kctx = head->kctx;
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
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);
591 KBASE_TRACE_ADD_SLOT(kbdev, JM_CHECK_HEAD, NULL, NULL, 0, js);
594 case JSn_COMMAND_SOFT_STOP:
595 KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP, head_kctx, head, head->jc, js);
597 case JSn_COMMAND_SOFT_STOP_0:
598 KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP_0, head_kctx, head, head->jc, js);
600 case JSn_COMMAND_SOFT_STOP_1:
601 KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP_1, head_kctx, head, head->jc, js);
603 case JSn_COMMAND_HARD_STOP:
604 KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP, head_kctx, head, head->jc, js);
606 case JSn_COMMAND_HARD_STOP_0:
607 KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP_0, head_kctx, head, head->jc, js);
609 case JSn_COMMAND_HARD_STOP_1:
610 KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP_1, head_kctx, head, head->jc, js);
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);
620 KBASE_TRACE_ADD_SLOT(kbdev, JM_CHECK_HEAD, NULL, NULL, 0, js);
623 case JSn_COMMAND_SOFT_STOP:
624 KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP, NULL, NULL, 0, js);
626 case JSn_COMMAND_SOFT_STOP_0:
627 KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP_0, NULL, NULL, 0, js);
629 case JSn_COMMAND_SOFT_STOP_1:
630 KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP_1, NULL, NULL, 0, js);
632 case JSn_COMMAND_HARD_STOP:
633 KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP, NULL, NULL, 0, js);
635 case JSn_COMMAND_HARD_STOP_0:
636 KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP_0, NULL, NULL, 0, js);
638 case JSn_COMMAND_HARD_STOP_1:
639 KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP_1, NULL, NULL, 0, js);
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 */
654 /** Soft or hard-stop a slot
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.
659 * This does not attempt to stop or evict jobs that are 'dummy' jobs for HW workarounds.
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
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)
669 kbase_jd_atom *katom;
674 kbasep_js_device_data *js_devdata;
675 mali_bool can_safely_stop = kbase_hw_has_feature(kbdev, BASE_HW_FEATURE_JOBCHAIN_DISAMBIGUATION);
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;
681 slot = &kbdev->jm_slots[js];
682 KBASE_DEBUG_ASSERT(slot);
683 lockdep_assert_held(&js_devdata->runpool_irq.lock);
685 jobs_submitted = kbasep_jm_nr_jobs_submitted(slot);
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);
690 if (jobs_submitted > JM_SLOT_MAX_JOB_SUBMIT_REGS)
691 i = jobs_submitted - JM_SLOT_MAX_JOB_SUBMIT_REGS;
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);
699 if (kctx && katom->kctx != kctx)
702 if (target_katom && katom != target_katom)
705 if (kbasep_jm_is_dummy_workaround_job(kbdev, katom))
708 core_reqs = katom->core_req;
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);
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);
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);
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. */
732 slot->job_chain_flag = !slot->job_chain_flag;
734 KBASE_TRACE_ADD_SLOT(kbdev, JM_SLOT_EVICT, dequeued_katom->kctx, dequeued_katom, dequeued_katom->jc, js);
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);
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);
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 */
757 /* Only proceed when the next job isn't a HW workaround 'dummy' job
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
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.
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)
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);
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);
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);
792 KBASE_TRACE_ADD_SLOT(kbdev, JM_SLOT_EVICT, dequeued_katom->kctx, dequeued_katom, dequeued_katom->jc, js);
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);
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 */
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);
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
813 KBASE_TIMELINE_TRY_SOFT_STOP(kctx, js, 0);
816 void kbase_job_kill_jobs_from_context(kbase_context *kctx)
820 kbasep_js_device_data *js_devdata;
823 KBASE_DEBUG_ASSERT(kctx != NULL);
825 KBASE_DEBUG_ASSERT(kbdev != NULL);
826 js_devdata = &kbdev->js_data;
828 /* Cancel any remaining running jobs for this kctx */
829 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
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;
835 for (i = 0; i < kbdev->gpu_props.num_job_slots; i++)
836 kbase_job_slot_hardstop(kctx, i, NULL);
838 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
841 void kbase_job_zap_context(kbase_context *kctx)
844 kbasep_js_device_data *js_devdata;
845 kbasep_js_kctx_info *js_kctx_info;
847 mali_bool evict_success;
849 KBASE_DEBUG_ASSERT(kctx != NULL);
851 KBASE_DEBUG_ASSERT(kbdev != NULL);
852 js_devdata = &kbdev->js_data;
853 js_kctx_info = &kctx->jctx.sched_info;
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.
861 /* First, atomically do the following:
862 * - mark the context as dying
863 * - try to evict it from the policy queue */
865 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
866 js_kctx_info->ctx.is_dying = MALI_TRUE;
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);
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
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
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
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.
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)
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)
919 KBASE_TRACE_ADD(kbdev, JM_ZAP_NON_SCHEDULED, kctx, NULL, 0u, js_kctx_info->ctx.is_scheduled);
921 dev_dbg(kbdev->dev, "Zap: Ctx %p evict_success=%d, scheduled=%d", kctx, evict_success, js_kctx_info->ctx.is_scheduled);
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.
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);
930 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
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);
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);
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);
946 /* Since it's scheduled and we have the jsctx_mutex, it must be retained successfully */
947 KBASE_DEBUG_ASSERT(was_retained != MALI_FALSE);
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);
955 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
956 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
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);
961 KBASE_TRACE_ADD(kbdev, JM_ZAP_DONE, kctx, NULL, 0u, 0u);
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
968 * kbase_jd_zap_context() will do this. */
970 KBASE_EXPORT_TEST_API(kbase_job_zap_context)
972 mali_error kbase_job_slot_init(kbase_device *kbdev)
975 KBASE_DEBUG_ASSERT(kbdev);
977 for (i = 0; i < kbdev->gpu_props.num_job_slots; i++)
978 kbasep_jm_init_submit_slot(&kbdev->jm_slots[i]);
980 return MALI_ERROR_NONE;
982 KBASE_EXPORT_TEST_API(kbase_job_slot_init)
984 void kbase_job_slot_halt(kbase_device *kbdev)
989 void kbase_job_slot_term(kbase_device *kbdev)
991 KBASE_DEBUG_ASSERT(kbdev);
993 KBASE_EXPORT_TEST_API(kbase_job_slot_term)
996 * Soft-stop the specified job slot
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.
1001 * Where possible any job in the next register is evicted before the soft-stop.
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)
1007 void kbase_job_slot_softstop(kbase_device *kbdev, int js, kbase_jd_atom *target_katom)
1009 kbasep_job_slot_soft_or_hard_stop(kbdev, NULL, js, target_katom, JSn_COMMAND_SOFT_STOP);
1013 * Hard-stop the specified job slot
1015 * The job slot lock must be held when calling this function.
1017 * @param kctx The kbase context that contains the job(s) that should
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)
1023 void kbase_job_slot_hardstop(kbase_context *kctx, int js,
1024 kbase_jd_atom *target_katom)
1026 kbase_device *kbdev = kctx->kbdev;
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);
1049 void kbase_debug_dump_registers(kbase_device *kbdev)
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),
1064 i, kbase_reg_read(kbdev, JOB_SLOT_REG(i, JSn_HEAD_LO),
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));
1082 void kbasep_reset_timeout_worker(struct work_struct *data)
1084 unsigned long flags;
1085 kbase_device *kbdev;
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;
1092 KBASE_DEBUG_ASSERT(data);
1094 kbdev = container_of(data, kbase_device, reset_work);
1096 KBASE_DEBUG_ASSERT(kbdev);
1097 js_devdata = &kbdev->js_data;
1099 KBASE_TRACE_ADD(kbdev, JM_BEGIN_RESET_WORKER, NULL, NULL, 0u, 0);
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);
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);
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);
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 */
1120 dev_err(kbdev->dev, "Resetting GPU (allowing up to %d ms)", RESET_TIMEOUT);
1122 spin_lock_irqsave(&kbdev->hwcnt.lock, flags);
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);
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);
1142 /* Output the state of some interesting registers to help in the
1143 * debugging of GPU resets */
1144 kbase_debug_dump_registers(kbdev);
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);
1157 kbase_pm_init_hw(kbdev, MALI_TRUE);
1158 /* IRQs were re-enabled by kbase_pm_init_hw, and GPU is still powered */
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);
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);
1176 kbase_reg_write(kbdev, GPU_CONTROL_REG(PRFCNT_TILER_EN), hwcnt_setup.tiler_bm, kctx);
1178 kbase_reg_write(kbdev, GPU_CONTROL_REG(PRFCNT_CONFIG), (kctx->as_nr << PRFCNT_CONFIG_AS_SHIFT) | PRFCNT_CONFIG_MODE_MANUAL, kctx);
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);
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);
1195 case KBASE_INSTR_STATE_CLEANING:
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
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);
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);
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);
1220 /* Unhandled cases */
1221 case KBASE_INSTR_STATE_RESETTING:
1226 spin_unlock_irqrestore(&kbdev->hwcnt.lock, flags);
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++) {
1232 kbase_jm_slot *slot = &kbdev->jm_slots[i];
1234 nr_done = kbasep_jm_nr_jobs_submitted(slot);
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);
1241 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1243 mutex_lock(&js_devdata->runpool_mutex);
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);
1255 atomic_set(&kbdev->reset_gpu, KBASE_RESET_GPU_NOT_PENDING);
1256 wake_up(&kbdev->reset_wait);
1257 dev_err(kbdev->dev, "Reset complete");
1259 /* Find out what cores are required now */
1260 kbase_pm_update_cores_state(kbdev);
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);
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);
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);
1274 mutex_unlock(&js_devdata->runpool_mutex);
1275 mutex_unlock(&kbdev->pm.lock);
1277 kbase_pm_context_idle(kbdev);
1278 KBASE_TRACE_ADD(kbdev, JM_END_RESET_WORKER, NULL, NULL, 0u, 0);
1281 enum hrtimer_restart kbasep_reset_timer_callback(struct hrtimer *timer)
1283 kbase_device *kbdev = container_of(timer, kbase_device, reset_timer);
1285 KBASE_DEBUG_ASSERT(kbdev);
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);
1291 return HRTIMER_NORESTART;
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
1299 static void kbasep_try_reset_gpu_early_locked(kbase_device *kbdev)
1302 int pending_jobs = 0;
1304 KBASE_DEBUG_ASSERT(kbdev);
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);
1312 if (pending_jobs > 0) {
1313 /* There are still jobs on the GPU - wait */
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 */
1323 queue_work(kbdev->reset_workq, &kbdev->reset_work);
1326 static void kbasep_try_reset_gpu_early(kbase_device *kbdev)
1328 unsigned long flags;
1329 kbasep_js_device_data *js_devdata;
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);
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.
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.
1345 * @return See description
1347 mali_bool kbase_prepare_to_reset_gpu_locked(kbase_device *kbdev)
1351 KBASE_DEBUG_ASSERT(kbdev);
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 */
1358 for (i = 0; i < kbdev->gpu_props.num_job_slots; i++)
1359 kbase_job_slot_softstop(kbdev, i, NULL);
1364 mali_bool kbase_prepare_to_reset_gpu(kbase_device *kbdev)
1366 unsigned long flags;
1368 kbasep_js_device_data *js_devdata;
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);
1377 KBASE_EXPORT_TEST_API(kbase_prepare_to_reset_gpu)
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.
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.
1386 void kbase_reset_gpu(kbase_device *kbdev)
1390 KBASE_DEBUG_ASSERT(kbdev);
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);
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);
1400 /* Try resetting early */
1401 kbasep_try_reset_gpu_early(kbdev);
1403 KBASE_EXPORT_TEST_API(kbase_reset_gpu)
1405 void kbase_reset_gpu_locked(kbase_device *kbdev)
1409 KBASE_DEBUG_ASSERT(kbdev);
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);
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);
1419 /* Try resetting early */
1420 kbasep_try_reset_gpu_early_locked(kbdev);