3 * (C) COPYRIGHT 2010-2016 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.
19 * Base kernel job manager APIs
22 #include <mali_kbase.h>
23 #include <mali_kbase_config.h>
24 #include <mali_midg_regmap.h>
25 #if defined(CONFIG_MALI_GATOR_SUPPORT)
26 #include <mali_kbase_gator.h>
28 #include <mali_kbase_tlstream.h>
29 #include <mali_kbase_vinstr.h>
30 #include <mali_kbase_hw.h>
31 #include <mali_kbase_hwaccess_jm.h>
32 #include <backend/gpu/mali_kbase_device_internal.h>
33 #include <backend/gpu/mali_kbase_irq_internal.h>
34 #include <backend/gpu/mali_kbase_js_affinity.h>
35 #include <backend/gpu/mali_kbase_jm_internal.h>
37 #define beenthere(kctx, f, a...) \
38 dev_dbg(kctx->kbdev->dev, "%s:" f, __func__, ##a)
40 #if KBASE_GPU_RESET_EN
41 static void kbasep_try_reset_gpu_early(struct kbase_device *kbdev);
42 static void kbasep_reset_timeout_worker(struct work_struct *data);
43 static enum hrtimer_restart kbasep_reset_timer_callback(struct hrtimer *timer);
44 #endif /* KBASE_GPU_RESET_EN */
46 static inline int kbasep_jm_is_js_free(struct kbase_device *kbdev, int js,
47 struct kbase_context *kctx)
49 return !kbase_reg_read(kbdev, JOB_SLOT_REG(js, JS_COMMAND_NEXT), kctx);
52 void kbase_job_hw_submit(struct kbase_device *kbdev,
53 struct kbase_jd_atom *katom,
56 struct kbase_context *kctx;
58 u64 jc_head = katom->jc;
60 KBASE_DEBUG_ASSERT(kbdev);
61 KBASE_DEBUG_ASSERT(katom);
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,
72 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JS_HEAD_NEXT_LO),
73 jc_head & 0xFFFFFFFF, kctx);
74 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JS_HEAD_NEXT_HI),
77 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JS_AFFINITY_NEXT_LO),
78 katom->affinity & 0xFFFFFFFF, kctx);
79 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JS_AFFINITY_NEXT_HI),
80 katom->affinity >> 32, kctx);
82 /* start MMU, medium priority, cache clean/flush on end, clean/flush on
86 if (kbase_hw_has_feature(kbdev, BASE_HW_FEATURE_FLUSH_REDUCTION))
87 cfg |= JS_CONFIG_ENABLE_FLUSH_REDUCTION;
89 #ifndef CONFIG_MALI_COH_GPU
90 if (0 != (katom->core_req & BASE_JD_REQ_SKIP_CACHE_START))
91 cfg |= JS_CONFIG_START_FLUSH_NO_ACTION;
93 cfg |= JS_CONFIG_START_FLUSH_CLEAN_INVALIDATE;
95 if (0 != (katom->core_req & BASE_JD_REQ_SKIP_CACHE_END))
96 cfg |= JS_CONFIG_END_FLUSH_NO_ACTION;
98 cfg |= JS_CONFIG_END_FLUSH_CLEAN_INVALIDATE;
99 #endif /* CONFIG_MALI_COH_GPU */
101 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_10649) ||
102 !kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_T76X_3982))
103 cfg |= JS_CONFIG_START_MMU;
105 cfg |= JS_CONFIG_THREAD_PRI(8);
107 if (kbase_hw_has_feature(kbdev, BASE_HW_FEATURE_PROTECTED_MODE) &&
108 (katom->atom_flags & KBASE_KATOM_FLAG_PROTECTED))
109 cfg |= JS_CONFIG_DISABLE_DESCRIPTOR_WR_BK;
111 if (kbase_hw_has_feature(kbdev,
112 BASE_HW_FEATURE_JOBCHAIN_DISAMBIGUATION)) {
113 if (!kbdev->hwaccess.backend.slot_rb[js].job_chain_flag) {
114 cfg |= JS_CONFIG_JOB_CHAIN_FLAG;
115 katom->atom_flags |= KBASE_KATOM_FLAGS_JOBCHAIN;
116 kbdev->hwaccess.backend.slot_rb[js].job_chain_flag =
119 katom->atom_flags &= ~KBASE_KATOM_FLAGS_JOBCHAIN;
120 kbdev->hwaccess.backend.slot_rb[js].job_chain_flag =
125 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JS_CONFIG_NEXT), cfg, kctx);
127 if (kbase_hw_has_feature(kbdev, BASE_HW_FEATURE_FLUSH_REDUCTION))
128 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JS_FLUSH_ID_NEXT),
129 katom->flush_id, kctx);
131 /* Write an approximate start timestamp.
132 * It's approximate because there might be a job in the HEAD register.
133 * In such cases, we'll try to make a better approximation in the IRQ
134 * handler (up to the KBASE_JS_IRQ_THROTTLE_TIME_US). */
135 katom->start_timestamp = ktime_get();
138 dev_dbg(kbdev->dev, "JS: Submitting atom %p from ctx %p to js[%d] with head=0x%llx, affinity=0x%llx",
139 katom, kctx, js, jc_head, katom->affinity);
141 KBASE_TRACE_ADD_SLOT_INFO(kbdev, JM_SUBMIT, kctx, katom, jc_head, js,
142 (u32) katom->affinity);
144 #if defined(CONFIG_MALI_GATOR_SUPPORT)
145 kbase_trace_mali_job_slots_event(
146 GATOR_MAKE_EVENT(GATOR_JOB_SLOT_START, js),
147 kctx, kbase_jd_atom_id(kctx, katom));
149 kbase_tlstream_tl_attrib_atom_config(katom, jc_head,
150 katom->affinity, cfg);
151 kbase_tlstream_tl_ret_ctx_lpu(
153 &kbdev->gpu_props.props.raw_props.js_features[
155 kbase_tlstream_tl_ret_atom_as(katom, &kbdev->as[kctx->as_nr]);
156 kbase_tlstream_tl_ret_atom_lpu(
158 &kbdev->gpu_props.props.raw_props.js_features[js],
160 #ifdef CONFIG_GPU_TRACEPOINTS
161 if (!kbase_backend_nr_atoms_submitted(kbdev, js)) {
162 /* If this is the only job on the slot, trace it as starting */
165 trace_gpu_sched_switch(
166 kbasep_make_job_slot_string(js, js_string),
167 ktime_to_ns(katom->start_timestamp),
168 (u32)katom->kctx->id, 0, katom->work_id);
169 kbdev->hwaccess.backend.slot_rb[js].last_context = katom->kctx;
172 kbase_timeline_job_slot_submit(kbdev, kctx, katom, js);
174 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JS_COMMAND_NEXT),
175 JS_COMMAND_START, katom->kctx);
179 * kbasep_job_slot_update_head_start_timestamp - Update timestamp
180 * @kbdev: kbase device
182 * @end_timestamp: timestamp
184 * Update the start_timestamp of the job currently in the HEAD, based on the
185 * fact that we got an IRQ for the previous set of completed jobs.
187 * The estimate also takes into account the %KBASE_JS_IRQ_THROTTLE_TIME_US and
188 * the time the job was submitted, to work out the best estimate (which might
189 * still result in an over-estimate to the calculated time spent)
191 static void kbasep_job_slot_update_head_start_timestamp(
192 struct kbase_device *kbdev,
194 ktime_t end_timestamp)
196 if (kbase_backend_nr_atoms_on_slot(kbdev, js) > 0) {
197 struct kbase_jd_atom *katom;
198 ktime_t new_timestamp;
199 ktime_t timestamp_diff;
200 /* The atom in the HEAD */
201 katom = kbase_gpu_inspect(kbdev, js, 0);
203 KBASE_DEBUG_ASSERT(katom != NULL);
205 /* Account for any IRQ Throttle time - makes an overestimate of
206 * the time spent by the job */
207 new_timestamp = ktime_sub_ns(end_timestamp,
208 KBASE_JS_IRQ_THROTTLE_TIME_US * 1000);
209 timestamp_diff = ktime_sub(new_timestamp,
210 katom->start_timestamp);
211 if (ktime_to_ns(timestamp_diff) >= 0) {
212 /* Only update the timestamp if it's a better estimate
213 * than what's currently stored. This is because our
214 * estimate that accounts for the throttle time may be
215 * too much of an overestimate */
216 katom->start_timestamp = new_timestamp;
222 * kbasep_trace_tl_nret_atom_lpu - Call nret_atom_lpu timeline tracepoint
223 * @kbdev: kbase device
226 * Get kbase atom by calling kbase_gpu_inspect for given job slot.
227 * Then use obtained katom and name of slot associated with the given
228 * job slot number in tracepoint call to the instrumentation module
229 * informing that given atom is no longer executed on given lpu (job slot).
231 static void kbasep_trace_tl_nret_atom_lpu(struct kbase_device *kbdev, int i)
233 struct kbase_jd_atom *katom = kbase_gpu_inspect(kbdev, i, 0);
235 kbase_tlstream_tl_nret_atom_lpu(katom,
236 &kbdev->gpu_props.props.raw_props.js_features[i]);
239 void kbase_job_done(struct kbase_device *kbdev, u32 done)
244 ktime_t end_timestamp = ktime_get();
245 struct kbasep_js_device_data *js_devdata;
247 KBASE_DEBUG_ASSERT(kbdev);
248 js_devdata = &kbdev->js_data;
250 KBASE_TRACE_ADD(kbdev, JM_IRQ, NULL, NULL, 0, done);
252 memset(&kbdev->slot_submit_count_irq[0], 0,
253 sizeof(kbdev->slot_submit_count_irq));
255 /* write irq throttle register, this will prevent irqs from occurring
256 * until the given number of gpu clock cycles have passed */
258 int irq_throttle_cycles =
259 atomic_read(&kbdev->irq_throttle_cycles);
261 kbase_reg_write(kbdev, JOB_CONTROL_REG(JOB_IRQ_THROTTLE),
262 irq_throttle_cycles, NULL);
265 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
268 u32 failed = done >> 16;
270 /* treat failed slots as finished slots */
271 u32 finished = (done & 0xFFFF) | failed;
273 /* Note: This is inherently unfair, as we always check
274 * for lower numbered interrupts before the higher
276 i = ffs(finished) - 1;
277 KBASE_DEBUG_ASSERT(i >= 0);
282 u32 completion_code = BASE_JD_EVENT_DONE;/* assume OK */
285 if (failed & (1u << i)) {
286 /* read out the job slot status code if the job
287 * slot reported failure */
288 completion_code = kbase_reg_read(kbdev,
289 JOB_SLOT_REG(i, JS_STATUS), NULL);
291 switch (completion_code) {
292 case BASE_JD_EVENT_STOPPED:
293 #if defined(CONFIG_MALI_GATOR_SUPPORT)
294 kbase_trace_mali_job_slots_event(
296 GATOR_JOB_SLOT_SOFT_STOPPED, i),
300 kbase_tlstream_aux_job_softstop(i);
302 kbasep_trace_tl_nret_atom_lpu(
305 /* Soft-stopped job - read the value of
306 * JS<n>_TAIL so that the job chain can
308 job_tail = (u64)kbase_reg_read(kbdev,
309 JOB_SLOT_REG(i, JS_TAIL_LO),
311 ((u64)kbase_reg_read(kbdev,
312 JOB_SLOT_REG(i, JS_TAIL_HI),
315 case BASE_JD_EVENT_NOT_STARTED:
316 /* PRLAM-10673 can cause a TERMINATED
317 * job to come back as NOT_STARTED, but
318 * the error interrupt helps us detect
321 BASE_JD_EVENT_TERMINATED;
324 dev_warn(kbdev->dev, "error detected from slot %d, job status 0x%08x (%s)",
331 kbase_gpu_irq_evict(kbdev, i);
334 kbase_reg_write(kbdev, JOB_CONTROL_REG(JOB_IRQ_CLEAR),
335 done & ((1 << i) | (1 << (i + 16))),
337 active = kbase_reg_read(kbdev,
338 JOB_CONTROL_REG(JOB_IRQ_JS_STATE),
341 if (((active >> i) & 1) == 0 &&
342 (((done >> (i + 16)) & 1) == 0)) {
343 /* There is a potential race we must work
346 * 1. A job slot has a job in both current and
348 * 2. The job in current completes
349 * successfully, the IRQ handler reads
350 * RAWSTAT and calls this function with the
351 * relevant bit set in "done"
352 * 3. The job in the next registers becomes the
353 * current job on the GPU
354 * 4. Sometime before the JOB_IRQ_CLEAR line
355 * above the job on the GPU _fails_
356 * 5. The IRQ_CLEAR clears the done bit but not
357 * the failed bit. This atomically sets
358 * JOB_IRQ_JS_STATE. However since both jobs
359 * have now completed the relevant bits for
360 * the slot are set to 0.
362 * If we now did nothing then we'd incorrectly
363 * assume that _both_ jobs had completed
364 * successfully (since we haven't yet observed
365 * the fail bit being set in RAWSTAT).
367 * So at this point if there are no active jobs
368 * left we check to see if RAWSTAT has a failure
369 * bit set for the job slot. If it does we know
370 * that there has been a new failure that we
371 * didn't previously know about, so we make sure
372 * that we record this in active (but we wait
373 * for the next loop to deal with it).
375 * If we were handling a job failure (i.e. done
376 * has the relevant high bit set) then we know
377 * that the value read back from
378 * JOB_IRQ_JS_STATE is the correct number of
379 * remaining jobs because the failed job will
380 * have prevented any futher jobs from starting
383 u32 rawstat = kbase_reg_read(kbdev,
384 JOB_CONTROL_REG(JOB_IRQ_RAWSTAT), NULL);
386 if ((rawstat >> (i + 16)) & 1) {
387 /* There is a failed job that we've
388 * missed - add it back to active */
393 dev_dbg(kbdev->dev, "Job ended with status 0x%08X\n",
396 nr_done = kbase_backend_nr_atoms_submitted(kbdev, i);
397 nr_done -= (active >> i) & 1;
398 nr_done -= (active >> (i + 16)) & 1;
401 dev_warn(kbdev->dev, "Spurious interrupt on slot %d",
411 kbase_gpu_complete_hw(kbdev, i,
415 kbase_jm_try_kick_all(kbdev);
417 /* More than one job has completed.
418 * Since this is not the last job being
419 * reported this time it must have
420 * passed. This is because the hardware
421 * will not allow further jobs in a job
422 * slot to complete until the failed job
423 * is cleared from the IRQ status.
425 kbase_gpu_complete_hw(kbdev, i,
433 done = kbase_reg_read(kbdev,
434 JOB_CONTROL_REG(JOB_IRQ_RAWSTAT), NULL);
436 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_10883)) {
437 /* Workaround for missing interrupt caused by
439 if (((active >> i) & 1) && (0 ==
440 kbase_reg_read(kbdev,
442 JS_STATUS), NULL))) {
443 /* Force job slot to be processed again
450 finished = (done & 0xFFFF) | failed;
452 end_timestamp = ktime_get();
453 } while (finished & (1 << i));
455 kbasep_job_slot_update_head_start_timestamp(kbdev, i,
459 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
460 #if KBASE_GPU_RESET_EN
461 if (atomic_read(&kbdev->hwaccess.backend.reset_gpu) ==
462 KBASE_RESET_GPU_COMMITTED) {
463 /* If we're trying to reset the GPU then we might be able to do
464 * it early (without waiting for a timeout) because some jobs
467 kbasep_try_reset_gpu_early(kbdev);
469 #endif /* KBASE_GPU_RESET_EN */
470 KBASE_TRACE_ADD(kbdev, JM_IRQ_END, NULL, NULL, 0, count);
472 KBASE_EXPORT_TEST_API(kbase_job_done);
474 static bool kbasep_soft_stop_allowed(struct kbase_device *kbdev,
475 struct kbase_jd_atom *katom)
477 bool soft_stops_allowed = true;
479 if (kbase_jd_katom_is_protected(katom)) {
480 soft_stops_allowed = false;
481 } else if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8408)) {
482 if ((katom->core_req & BASE_JD_REQ_T) != 0)
483 soft_stops_allowed = false;
485 return soft_stops_allowed;
488 static bool kbasep_hard_stop_allowed(struct kbase_device *kbdev,
489 base_jd_core_req core_reqs)
491 bool hard_stops_allowed = true;
493 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8394)) {
494 if ((core_reqs & BASE_JD_REQ_T) != 0)
495 hard_stops_allowed = false;
497 return hard_stops_allowed;
500 void kbasep_job_slot_soft_or_hard_stop_do_action(struct kbase_device *kbdev,
503 base_jd_core_req core_reqs,
504 struct kbase_jd_atom *target_katom)
506 struct kbase_context *kctx = target_katom->kctx;
507 #if KBASE_TRACE_ENABLE
508 u32 status_reg_before;
509 u64 job_in_head_before;
510 u32 status_reg_after;
512 KBASE_DEBUG_ASSERT(!(action & (~JS_COMMAND_MASK)));
514 /* Check the head pointer */
515 job_in_head_before = ((u64) kbase_reg_read(kbdev,
516 JOB_SLOT_REG(js, JS_HEAD_LO), NULL))
517 | (((u64) kbase_reg_read(kbdev,
518 JOB_SLOT_REG(js, JS_HEAD_HI), NULL))
520 status_reg_before = kbase_reg_read(kbdev, JOB_SLOT_REG(js, JS_STATUS),
524 if (action == JS_COMMAND_SOFT_STOP) {
525 bool soft_stop_allowed = kbasep_soft_stop_allowed(kbdev,
528 if (!soft_stop_allowed) {
529 #ifdef CONFIG_MALI_DEBUG
531 "Attempt made to soft-stop a job that cannot be soft-stopped. core_reqs = 0x%X",
532 (unsigned int)core_reqs);
533 #endif /* CONFIG_MALI_DEBUG */
537 /* We are about to issue a soft stop, so mark the atom as having
538 * been soft stopped */
539 target_katom->atom_flags |= KBASE_KATOM_FLAG_BEEN_SOFT_STOPPPED;
541 /* Mark the point where we issue the soft-stop command */
542 kbase_tlstream_aux_issue_job_softstop(target_katom);
544 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8316)) {
548 i < kbase_backend_nr_atoms_submitted(kbdev, js);
550 struct kbase_jd_atom *katom;
552 katom = kbase_gpu_inspect(kbdev, js, i);
554 KBASE_DEBUG_ASSERT(katom);
556 /* For HW_ISSUE_8316, only 'bad' jobs attacking
557 * the system can cause this issue: normally,
558 * all memory should be allocated in multiples
559 * of 4 pages, and growable memory should be
560 * changed size in multiples of 4 pages.
562 * Whilst such 'bad' jobs can be cleared by a
563 * GPU reset, the locking up of a uTLB entry
564 * caused by the bad job could also stall other
565 * ASs, meaning that other ASs' jobs don't
566 * complete in the 'grace' period before the
567 * reset. We don't want to lose other ASs' jobs
568 * when they would normally complete fine, so we
569 * must 'poke' the MMU regularly to help other
571 kbase_as_poking_timer_retain_atom(
572 kbdev, katom->kctx, katom);
576 if (kbase_hw_has_feature(
578 BASE_HW_FEATURE_JOBCHAIN_DISAMBIGUATION)) {
579 action = (target_katom->atom_flags &
580 KBASE_KATOM_FLAGS_JOBCHAIN) ?
581 JS_COMMAND_SOFT_STOP_1 :
582 JS_COMMAND_SOFT_STOP_0;
584 } else if (action == JS_COMMAND_HARD_STOP) {
585 bool hard_stop_allowed = kbasep_hard_stop_allowed(kbdev,
588 if (!hard_stop_allowed) {
589 /* Jobs can be hard-stopped for the following reasons:
590 * * CFS decides the job has been running too long (and
591 * soft-stop has not occurred). In this case the GPU
592 * will be reset by CFS if the job remains on the
595 * * The context is destroyed, kbase_jd_zap_context
596 * will attempt to hard-stop the job. However it also
597 * has a watchdog which will cause the GPU to be
598 * reset if the job remains on the GPU.
600 * * An (unhandled) MMU fault occurred. As long as
601 * BASE_HW_ISSUE_8245 is defined then the GPU will be
604 * All three cases result in the GPU being reset if the
605 * hard-stop fails, so it is safe to just return and
606 * ignore the hard-stop request.
609 "Attempt made to hard-stop a job that cannot be hard-stopped. core_reqs = 0x%X",
610 (unsigned int)core_reqs);
613 target_katom->atom_flags |= KBASE_KATOM_FLAG_BEEN_HARD_STOPPED;
615 if (kbase_hw_has_feature(
617 BASE_HW_FEATURE_JOBCHAIN_DISAMBIGUATION)) {
618 action = (target_katom->atom_flags &
619 KBASE_KATOM_FLAGS_JOBCHAIN) ?
620 JS_COMMAND_HARD_STOP_1 :
621 JS_COMMAND_HARD_STOP_0;
625 kbase_reg_write(kbdev, JOB_SLOT_REG(js, JS_COMMAND), action, kctx);
627 #if KBASE_TRACE_ENABLE
628 status_reg_after = kbase_reg_read(kbdev, JOB_SLOT_REG(js, JS_STATUS),
630 if (status_reg_after == BASE_JD_EVENT_ACTIVE) {
631 struct kbase_jd_atom *head;
632 struct kbase_context *head_kctx;
634 head = kbase_gpu_inspect(kbdev, js, 0);
635 head_kctx = head->kctx;
637 if (status_reg_before == BASE_JD_EVENT_ACTIVE)
638 KBASE_TRACE_ADD_SLOT(kbdev, JM_CHECK_HEAD, head_kctx,
639 head, job_in_head_before, js);
641 KBASE_TRACE_ADD_SLOT(kbdev, JM_CHECK_HEAD, NULL, NULL,
645 case JS_COMMAND_SOFT_STOP:
646 KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP, head_kctx,
649 case JS_COMMAND_SOFT_STOP_0:
650 KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP_0, head_kctx,
653 case JS_COMMAND_SOFT_STOP_1:
654 KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP_1, head_kctx,
657 case JS_COMMAND_HARD_STOP:
658 KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP, head_kctx,
661 case JS_COMMAND_HARD_STOP_0:
662 KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP_0, head_kctx,
665 case JS_COMMAND_HARD_STOP_1:
666 KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP_1, head_kctx,
674 if (status_reg_before == BASE_JD_EVENT_ACTIVE)
675 KBASE_TRACE_ADD_SLOT(kbdev, JM_CHECK_HEAD, NULL, NULL,
676 job_in_head_before, js);
678 KBASE_TRACE_ADD_SLOT(kbdev, JM_CHECK_HEAD, NULL, NULL,
682 case JS_COMMAND_SOFT_STOP:
683 KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP, NULL, NULL, 0,
686 case JS_COMMAND_SOFT_STOP_0:
687 KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP_0, NULL, NULL,
690 case JS_COMMAND_SOFT_STOP_1:
691 KBASE_TRACE_ADD_SLOT(kbdev, JM_SOFTSTOP_1, NULL, NULL,
694 case JS_COMMAND_HARD_STOP:
695 KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP, NULL, NULL, 0,
698 case JS_COMMAND_HARD_STOP_0:
699 KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP_0, NULL, NULL,
702 case JS_COMMAND_HARD_STOP_1:
703 KBASE_TRACE_ADD_SLOT(kbdev, JM_HARDSTOP_1, NULL, NULL,
714 void kbase_backend_jm_kill_jobs_from_kctx(struct kbase_context *kctx)
717 struct kbase_device *kbdev;
718 struct kbasep_js_device_data *js_devdata;
721 KBASE_DEBUG_ASSERT(kctx != NULL);
723 KBASE_DEBUG_ASSERT(kbdev != NULL);
724 js_devdata = &kbdev->js_data;
726 /* Cancel any remaining running jobs for this kctx */
727 mutex_lock(&kctx->jctx.lock);
728 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
730 /* Invalidate all jobs in context, to prevent re-submitting */
731 for (i = 0; i < BASE_JD_ATOM_COUNT; i++) {
732 if (!work_pending(&kctx->jctx.atoms[i].work))
733 kctx->jctx.atoms[i].event_code =
734 BASE_JD_EVENT_JOB_CANCELLED;
737 for (i = 0; i < kbdev->gpu_props.num_job_slots; i++)
738 kbase_job_slot_hardstop(kctx, i, NULL);
740 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
741 mutex_unlock(&kctx->jctx.lock);
744 void kbase_job_slot_ctx_priority_check_locked(struct kbase_context *kctx,
745 struct kbase_jd_atom *target_katom)
747 struct kbase_device *kbdev;
748 int js = target_katom->slot_nr;
749 int priority = target_katom->sched_priority;
752 KBASE_DEBUG_ASSERT(kctx != NULL);
754 KBASE_DEBUG_ASSERT(kbdev != NULL);
756 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
758 for (i = 0; i < kbase_backend_nr_atoms_on_slot(kbdev, js); i++) {
759 struct kbase_jd_atom *katom;
761 katom = kbase_gpu_inspect(kbdev, js, i);
765 if (katom->kctx != kctx)
768 if (katom->sched_priority > priority)
769 kbase_job_slot_softstop(kbdev, js, katom);
773 struct zap_reset_data {
775 * 1. The timer has never been called
776 * 2. The zap has timed out, all slots are soft-stopped - the GPU reset
777 * will happen. The GPU has been reset when
778 * kbdev->hwaccess.backend.reset_waitq is signalled
780 * (-1 - The timer has been cancelled)
783 struct kbase_device *kbdev;
784 struct hrtimer timer;
785 spinlock_t lock; /* protects updates to stage member */
788 static enum hrtimer_restart zap_timeout_callback(struct hrtimer *timer)
790 struct zap_reset_data *reset_data = container_of(timer,
791 struct zap_reset_data, timer);
792 struct kbase_device *kbdev = reset_data->kbdev;
795 spin_lock_irqsave(&reset_data->lock, flags);
797 if (reset_data->stage == -1)
800 #if KBASE_GPU_RESET_EN
801 if (kbase_prepare_to_reset_gpu(kbdev)) {
802 dev_err(kbdev->dev, "Issueing GPU soft-reset because jobs failed to be killed (within %d ms) as part of context termination (e.g. process exit)\n",
804 kbase_reset_gpu(kbdev);
806 #endif /* KBASE_GPU_RESET_EN */
807 reset_data->stage = 2;
810 spin_unlock_irqrestore(&reset_data->lock, flags);
812 return HRTIMER_NORESTART;
815 void kbase_jm_wait_for_zero_jobs(struct kbase_context *kctx)
817 struct kbase_device *kbdev = kctx->kbdev;
818 struct zap_reset_data reset_data;
821 hrtimer_init_on_stack(&reset_data.timer, CLOCK_MONOTONIC,
823 reset_data.timer.function = zap_timeout_callback;
825 spin_lock_init(&reset_data.lock);
827 reset_data.kbdev = kbdev;
828 reset_data.stage = 1;
830 hrtimer_start(&reset_data.timer, HR_TIMER_DELAY_MSEC(ZAP_TIMEOUT),
833 /* Wait for all jobs to finish, and for the context to be not-scheduled
834 * (due to kbase_job_zap_context(), we also guarentee it's not in the JS
835 * policy queue either */
836 wait_event(kctx->jctx.zero_jobs_wait, kctx->jctx.job_nr == 0);
837 wait_event(kctx->jctx.sched_info.ctx.is_scheduled_wait,
838 kctx->jctx.sched_info.ctx.is_scheduled == false);
840 spin_lock_irqsave(&reset_data.lock, flags);
841 if (reset_data.stage == 1) {
842 /* The timer hasn't run yet - so cancel it */
843 reset_data.stage = -1;
845 spin_unlock_irqrestore(&reset_data.lock, flags);
847 hrtimer_cancel(&reset_data.timer);
849 if (reset_data.stage == 2) {
850 /* The reset has already started.
851 * Wait for the reset to complete
853 wait_event(kbdev->hwaccess.backend.reset_wait,
854 atomic_read(&kbdev->hwaccess.backend.reset_gpu)
855 == KBASE_RESET_GPU_NOT_PENDING);
857 destroy_hrtimer_on_stack(&reset_data.timer);
859 dev_dbg(kbdev->dev, "Zap: Finished Context %p", kctx);
861 /* Ensure that the signallers of the waitqs have finished */
862 mutex_lock(&kctx->jctx.lock);
863 mutex_lock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
864 mutex_unlock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
865 mutex_unlock(&kctx->jctx.lock);
868 u32 kbase_backend_get_current_flush_id(struct kbase_device *kbdev)
872 if (kbase_hw_has_feature(kbdev, BASE_HW_FEATURE_FLUSH_REDUCTION)) {
873 mutex_lock(&kbdev->pm.lock);
874 if (kbdev->pm.backend.gpu_powered)
875 flush_id = kbase_reg_read(kbdev,
876 GPU_CONTROL_REG(LATEST_FLUSH), NULL);
877 mutex_unlock(&kbdev->pm.lock);
883 int kbase_job_slot_init(struct kbase_device *kbdev)
885 #if KBASE_GPU_RESET_EN
886 kbdev->hwaccess.backend.reset_workq = alloc_workqueue(
887 "Mali reset workqueue", 0, 1);
888 if (NULL == kbdev->hwaccess.backend.reset_workq)
891 KBASE_DEBUG_ASSERT(0 ==
892 object_is_on_stack(&kbdev->hwaccess.backend.reset_work));
893 INIT_WORK(&kbdev->hwaccess.backend.reset_work,
894 kbasep_reset_timeout_worker);
896 hrtimer_init(&kbdev->hwaccess.backend.reset_timer, CLOCK_MONOTONIC,
898 kbdev->hwaccess.backend.reset_timer.function =
899 kbasep_reset_timer_callback;
904 KBASE_EXPORT_TEST_API(kbase_job_slot_init);
906 void kbase_job_slot_halt(struct kbase_device *kbdev)
911 void kbase_job_slot_term(struct kbase_device *kbdev)
913 #if KBASE_GPU_RESET_EN
914 destroy_workqueue(kbdev->hwaccess.backend.reset_workq);
917 KBASE_EXPORT_TEST_API(kbase_job_slot_term);
919 #if KBASE_GPU_RESET_EN
921 * kbasep_check_for_afbc_on_slot() - Check whether AFBC is in use on this slot
922 * @kbdev: kbase device pointer
923 * @kctx: context to check against
925 * @target_katom: An atom to check, or NULL if all atoms from @kctx on
926 * slot @js should be checked
928 * This checks are based upon parameters that would normally be passed to
929 * kbase_job_slot_hardstop().
931 * In the event of @target_katom being NULL, this will check the last jobs that
932 * are likely to be running on the slot to see if a) they belong to kctx, and
933 * so would be stopped, and b) whether they have AFBC
935 * In that case, It's guaranteed that a job currently executing on the HW with
936 * AFBC will be detected. However, this is a conservative check because it also
937 * detects jobs that have just completed too.
939 * Return: true when hard-stop _might_ stop an afbc atom, else false.
941 static bool kbasep_check_for_afbc_on_slot(struct kbase_device *kbdev,
942 struct kbase_context *kctx, int js,
943 struct kbase_jd_atom *target_katom)
948 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
950 /* When we have an atom the decision can be made straight away. */
952 return !!(target_katom->core_req & BASE_JD_REQ_FS_AFBC);
954 /* Otherwise, we must chweck the hardware to see if it has atoms from
955 * this context with AFBC. */
956 for (i = 0; i < kbase_backend_nr_atoms_on_slot(kbdev, js); i++) {
957 struct kbase_jd_atom *katom;
959 katom = kbase_gpu_inspect(kbdev, js, i);
963 /* Ignore atoms from other contexts, they won't be stopped when
964 * we use this for checking if we should hard-stop them */
965 if (katom->kctx != kctx)
968 /* An atom on this slot and this context: check for AFBC */
969 if (katom->core_req & BASE_JD_REQ_FS_AFBC) {
977 #endif /* KBASE_GPU_RESET_EN */
980 * kbase_job_slot_softstop_swflags - Soft-stop a job with flags
981 * @kbdev: The kbase device
982 * @js: The job slot to soft-stop
983 * @target_katom: The job that should be soft-stopped (or NULL for any job)
984 * @sw_flags: Flags to pass in about the soft-stop
987 * The job slot lock must be held when calling this function.
988 * The job slot must not already be in the process of being soft-stopped.
990 * Soft-stop the specified job slot, with extra information about the stop
992 * Where possible any job in the next register is evicted before the soft-stop.
994 void kbase_job_slot_softstop_swflags(struct kbase_device *kbdev, int js,
995 struct kbase_jd_atom *target_katom, u32 sw_flags)
997 KBASE_DEBUG_ASSERT(!(sw_flags & JS_COMMAND_MASK));
998 kbase_backend_soft_hard_stop_slot(kbdev, NULL, js, target_katom,
999 JS_COMMAND_SOFT_STOP | sw_flags);
1003 * kbase_job_slot_softstop - Soft-stop the specified job slot
1004 * @kbdev: The kbase device
1005 * @js: The job slot to soft-stop
1006 * @target_katom: The job that should be soft-stopped (or NULL for any job)
1008 * The job slot lock must be held when calling this function.
1009 * The job slot must not already be in the process of being soft-stopped.
1011 * Where possible any job in the next register is evicted before the soft-stop.
1013 void kbase_job_slot_softstop(struct kbase_device *kbdev, int js,
1014 struct kbase_jd_atom *target_katom)
1016 kbase_job_slot_softstop_swflags(kbdev, js, target_katom, 0u);
1020 * kbase_job_slot_hardstop - Hard-stop the specified job slot
1021 * @kctx: The kbase context that contains the job(s) that should
1023 * @js: The job slot to hard-stop
1024 * @target_katom: The job that should be hard-stopped (or NULL for all
1025 * jobs from the context)
1027 * The job slot lock must be held when calling this function.
1029 void kbase_job_slot_hardstop(struct kbase_context *kctx, int js,
1030 struct kbase_jd_atom *target_katom)
1032 struct kbase_device *kbdev = kctx->kbdev;
1034 #if KBASE_GPU_RESET_EN
1035 /* We make the check for AFBC before evicting/stopping atoms. Note
1036 * that no other thread can modify the slots whilst we have the
1037 * runpool_irq lock. */
1038 int needs_workaround_for_afbc =
1039 kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_T76X_3542)
1040 && kbasep_check_for_afbc_on_slot(kbdev, kctx, js,
1044 stopped = kbase_backend_soft_hard_stop_slot(kbdev, kctx, js,
1046 JS_COMMAND_HARD_STOP);
1047 #if KBASE_GPU_RESET_EN
1048 if (stopped && (kbase_hw_has_issue(kctx->kbdev, BASE_HW_ISSUE_8401) ||
1049 kbase_hw_has_issue(kctx->kbdev, BASE_HW_ISSUE_9510) ||
1050 needs_workaround_for_afbc)) {
1051 /* MIDBASE-2916 if a fragment job with AFBC encoding is
1052 * hardstopped, ensure to do a soft reset also in order to
1053 * clear the GPU status.
1054 * Workaround for HW issue 8401 has an issue,so after
1055 * hard-stopping just reset the GPU. This will ensure that the
1056 * jobs leave the GPU.*/
1057 if (kbase_prepare_to_reset_gpu_locked(kbdev)) {
1058 dev_err(kbdev->dev, "Issueing GPU soft-reset after hard stopping due to hardware issue");
1059 kbase_reset_gpu_locked(kbdev);
1066 * kbase_job_check_enter_disjoint - potentiall enter disjoint mode
1067 * @kbdev: kbase device
1068 * @action: the event which has occurred
1069 * @core_reqs: core requirements of the atom
1070 * @target_katom: the atom which is being affected
1072 * For a certain soft/hard-stop action, work out whether to enter disjoint
1075 * This does not register multiple disjoint events if the atom has already
1076 * started a disjoint period
1078 * @core_reqs can be supplied as 0 if the atom had not started on the hardware
1079 * (and so a 'real' soft/hard-stop was not required, but it still interrupted
1080 * flow, perhaps on another context)
1082 * kbase_job_check_leave_disjoint() should be used to end the disjoint
1083 * state when the soft/hard-stop action is complete
1085 void kbase_job_check_enter_disjoint(struct kbase_device *kbdev, u32 action,
1086 base_jd_core_req core_reqs, struct kbase_jd_atom *target_katom)
1088 u32 hw_action = action & JS_COMMAND_MASK;
1090 /* For hard-stop, don't enter if hard-stop not allowed */
1091 if (hw_action == JS_COMMAND_HARD_STOP &&
1092 !kbasep_hard_stop_allowed(kbdev, core_reqs))
1095 /* For soft-stop, don't enter if soft-stop not allowed, or isn't
1096 * causing disjoint */
1097 if (hw_action == JS_COMMAND_SOFT_STOP &&
1098 !(kbasep_soft_stop_allowed(kbdev, target_katom) &&
1099 (action & JS_COMMAND_SW_CAUSES_DISJOINT)))
1102 /* Nothing to do if already logged disjoint state on this atom */
1103 if (target_katom->atom_flags & KBASE_KATOM_FLAG_IN_DISJOINT)
1106 target_katom->atom_flags |= KBASE_KATOM_FLAG_IN_DISJOINT;
1107 kbase_disjoint_state_up(kbdev);
1111 * kbase_job_check_enter_disjoint - potentially leave disjoint state
1112 * @kbdev: kbase device
1113 * @target_katom: atom which is finishing
1115 * Work out whether to leave disjoint state when finishing an atom that was
1116 * originated by kbase_job_check_enter_disjoint().
1118 void kbase_job_check_leave_disjoint(struct kbase_device *kbdev,
1119 struct kbase_jd_atom *target_katom)
1121 if (target_katom->atom_flags & KBASE_KATOM_FLAG_IN_DISJOINT) {
1122 target_katom->atom_flags &= ~KBASE_KATOM_FLAG_IN_DISJOINT;
1123 kbase_disjoint_state_down(kbdev);
1128 #if KBASE_GPU_RESET_EN
1129 static void kbase_debug_dump_registers(struct kbase_device *kbdev)
1133 dev_err(kbdev->dev, "Register state:");
1134 dev_err(kbdev->dev, " GPU_IRQ_RAWSTAT=0x%08x GPU_STATUS=0x%08x",
1135 kbase_reg_read(kbdev, GPU_CONTROL_REG(GPU_IRQ_RAWSTAT), NULL),
1136 kbase_reg_read(kbdev, GPU_CONTROL_REG(GPU_STATUS), NULL));
1137 dev_err(kbdev->dev, " JOB_IRQ_RAWSTAT=0x%08x JOB_IRQ_JS_STATE=0x%08x JOB_IRQ_THROTTLE=0x%08x",
1138 kbase_reg_read(kbdev, JOB_CONTROL_REG(JOB_IRQ_RAWSTAT), NULL),
1139 kbase_reg_read(kbdev, JOB_CONTROL_REG(JOB_IRQ_JS_STATE), NULL),
1140 kbase_reg_read(kbdev, JOB_CONTROL_REG(JOB_IRQ_THROTTLE), NULL));
1141 for (i = 0; i < 3; i++) {
1142 dev_err(kbdev->dev, " JS%d_STATUS=0x%08x JS%d_HEAD_LO=0x%08x",
1143 i, kbase_reg_read(kbdev, JOB_SLOT_REG(i, JS_STATUS),
1145 i, kbase_reg_read(kbdev, JOB_SLOT_REG(i, JS_HEAD_LO),
1148 dev_err(kbdev->dev, " MMU_IRQ_RAWSTAT=0x%08x GPU_FAULTSTATUS=0x%08x",
1149 kbase_reg_read(kbdev, MMU_REG(MMU_IRQ_RAWSTAT), NULL),
1150 kbase_reg_read(kbdev, GPU_CONTROL_REG(GPU_FAULTSTATUS), NULL));
1151 dev_err(kbdev->dev, " GPU_IRQ_MASK=0x%08x JOB_IRQ_MASK=0x%08x MMU_IRQ_MASK=0x%08x",
1152 kbase_reg_read(kbdev, GPU_CONTROL_REG(GPU_IRQ_MASK), NULL),
1153 kbase_reg_read(kbdev, JOB_CONTROL_REG(JOB_IRQ_MASK), NULL),
1154 kbase_reg_read(kbdev, MMU_REG(MMU_IRQ_MASK), NULL));
1155 dev_err(kbdev->dev, " PWR_OVERRIDE0=0x%08x PWR_OVERRIDE1=0x%08x",
1156 kbase_reg_read(kbdev, GPU_CONTROL_REG(PWR_OVERRIDE0), NULL),
1157 kbase_reg_read(kbdev, GPU_CONTROL_REG(PWR_OVERRIDE1), NULL));
1158 dev_err(kbdev->dev, " SHADER_CONFIG=0x%08x L2_MMU_CONFIG=0x%08x",
1159 kbase_reg_read(kbdev, GPU_CONTROL_REG(SHADER_CONFIG), NULL),
1160 kbase_reg_read(kbdev, GPU_CONTROL_REG(L2_MMU_CONFIG), NULL));
1163 static void kbasep_reset_timeout_worker(struct work_struct *data)
1165 unsigned long flags, mmu_flags;
1166 struct kbase_device *kbdev;
1168 ktime_t end_timestamp = ktime_get();
1169 struct kbasep_js_device_data *js_devdata;
1170 bool try_schedule = false;
1171 bool silent = false;
1173 KBASE_DEBUG_ASSERT(data);
1175 kbdev = container_of(data, struct kbase_device,
1176 hwaccess.backend.reset_work);
1178 KBASE_DEBUG_ASSERT(kbdev);
1179 js_devdata = &kbdev->js_data;
1181 if (atomic_read(&kbdev->hwaccess.backend.reset_gpu) ==
1182 KBASE_RESET_GPU_SILENT)
1185 KBASE_TRACE_ADD(kbdev, JM_BEGIN_RESET_WORKER, NULL, NULL, 0u, 0);
1188 * This call will block until vinstr is suspended. */
1189 kbase_vinstr_suspend(kbdev->vinstr_ctx);
1191 /* Make sure the timer has completed - this cannot be done from
1192 * interrupt context, so this cannot be done within
1193 * kbasep_try_reset_gpu_early. */
1194 hrtimer_cancel(&kbdev->hwaccess.backend.reset_timer);
1196 if (kbase_pm_context_active_handle_suspend(kbdev,
1197 KBASE_PM_SUSPEND_HANDLER_DONT_REACTIVATE)) {
1198 /* This would re-activate the GPU. Since it's already idle,
1199 * there's no need to reset it */
1200 atomic_set(&kbdev->hwaccess.backend.reset_gpu,
1201 KBASE_RESET_GPU_NOT_PENDING);
1202 kbase_disjoint_state_down(kbdev);
1203 wake_up(&kbdev->hwaccess.backend.reset_wait);
1207 KBASE_DEBUG_ASSERT(kbdev->irq_reset_flush == false);
1209 spin_lock_irqsave(&kbdev->mmu_mask_change, mmu_flags);
1210 /* We're about to flush out the IRQs and their bottom half's */
1211 kbdev->irq_reset_flush = true;
1213 /* Disable IRQ to avoid IRQ handlers to kick in after releasing the
1214 * spinlock; this also clears any outstanding interrupts */
1215 spin_lock_irqsave(&kbdev->hwcnt.lock, flags);
1216 kbase_pm_disable_interrupts(kbdev);
1217 spin_unlock_irqrestore(&kbdev->hwcnt.lock, flags);
1219 spin_unlock_irqrestore(&kbdev->mmu_mask_change, mmu_flags);
1221 /* Ensure that any IRQ handlers have finished
1222 * Must be done without any locks IRQ handlers will take */
1223 kbase_synchronize_irqs(kbdev);
1225 /* Flush out any in-flight work items */
1226 kbase_flush_mmu_wqs(kbdev);
1228 /* The flush has completed so reset the active indicator */
1229 kbdev->irq_reset_flush = false;
1231 mutex_lock(&kbdev->pm.lock);
1232 /* We hold the pm lock, so there ought to be a current policy */
1233 KBASE_DEBUG_ASSERT(kbdev->pm.backend.pm_current_policy);
1235 /* All slot have been soft-stopped and we've waited
1236 * SOFT_STOP_RESET_TIMEOUT for the slots to clear, at this point we
1237 * assume that anything that is still left on the GPU is stuck there and
1238 * we'll kill it when we reset the GPU */
1241 dev_err(kbdev->dev, "Resetting GPU (allowing up to %d ms)",
1244 /* Output the state of some interesting registers to help in the
1245 * debugging of GPU resets */
1247 kbase_debug_dump_registers(kbdev);
1250 kbase_pm_init_hw(kbdev, 0);
1252 /* Complete any jobs that were still on the GPU */
1253 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1254 kbase_backend_reset(kbdev, &end_timestamp);
1255 kbase_pm_metrics_update(kbdev, NULL);
1256 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1258 mutex_unlock(&kbdev->pm.lock);
1260 mutex_lock(&js_devdata->runpool_mutex);
1262 /* Reprogram the GPU's MMU */
1263 for (i = 0; i < kbdev->nr_hw_address_spaces; i++) {
1264 struct kbase_as *as = &kbdev->as[i];
1266 mutex_lock(&as->transaction_mutex);
1267 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1269 if (js_devdata->runpool_irq.per_as_data[i].kctx)
1271 js_devdata->runpool_irq.per_as_data[i].kctx);
1273 kbase_mmu_disable_as(kbdev, i);
1275 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1276 mutex_unlock(&as->transaction_mutex);
1279 kbase_pm_enable_interrupts(kbdev);
1281 atomic_set(&kbdev->hwaccess.backend.reset_gpu,
1282 KBASE_RESET_GPU_NOT_PENDING);
1284 kbase_disjoint_state_down(kbdev);
1286 wake_up(&kbdev->hwaccess.backend.reset_wait);
1288 dev_err(kbdev->dev, "Reset complete");
1290 if (js_devdata->nr_contexts_pullable > 0 && !kbdev->poweroff_pending)
1291 try_schedule = true;
1293 mutex_unlock(&js_devdata->runpool_mutex);
1295 mutex_lock(&kbdev->pm.lock);
1297 /* Find out what cores are required now */
1298 kbase_pm_update_cores_state(kbdev);
1300 /* Synchronously request and wait for those cores, because if
1301 * instrumentation is enabled it would need them immediately. */
1302 kbase_pm_check_transitions_sync(kbdev);
1304 mutex_unlock(&kbdev->pm.lock);
1306 /* Try submitting some jobs to restart processing */
1308 KBASE_TRACE_ADD(kbdev, JM_SUBMIT_AFTER_RESET, NULL, NULL, 0u,
1310 kbase_js_sched_all(kbdev);
1313 kbase_pm_context_idle(kbdev);
1315 /* Release vinstr */
1316 kbase_vinstr_resume(kbdev->vinstr_ctx);
1318 KBASE_TRACE_ADD(kbdev, JM_END_RESET_WORKER, NULL, NULL, 0u, 0);
1321 static enum hrtimer_restart kbasep_reset_timer_callback(struct hrtimer *timer)
1323 struct kbase_device *kbdev = container_of(timer, struct kbase_device,
1324 hwaccess.backend.reset_timer);
1326 KBASE_DEBUG_ASSERT(kbdev);
1328 /* Reset still pending? */
1329 if (atomic_cmpxchg(&kbdev->hwaccess.backend.reset_gpu,
1330 KBASE_RESET_GPU_COMMITTED, KBASE_RESET_GPU_HAPPENING) ==
1331 KBASE_RESET_GPU_COMMITTED)
1332 queue_work(kbdev->hwaccess.backend.reset_workq,
1333 &kbdev->hwaccess.backend.reset_work);
1335 return HRTIMER_NORESTART;
1339 * If all jobs are evicted from the GPU then we can reset the GPU
1340 * immediately instead of waiting for the timeout to elapse
1343 static void kbasep_try_reset_gpu_early_locked(struct kbase_device *kbdev)
1346 int pending_jobs = 0;
1348 KBASE_DEBUG_ASSERT(kbdev);
1350 /* Count the number of jobs */
1351 for (i = 0; i < kbdev->gpu_props.num_job_slots; i++)
1352 pending_jobs += kbase_backend_nr_atoms_submitted(kbdev, i);
1354 if (pending_jobs > 0) {
1355 /* There are still jobs on the GPU - wait */
1359 /* To prevent getting incorrect registers when dumping failed job,
1362 if (kbdev->job_fault_debug != false)
1365 /* Check that the reset has been committed to (i.e. kbase_reset_gpu has
1366 * been called), and that no other thread beat this thread to starting
1368 if (atomic_cmpxchg(&kbdev->hwaccess.backend.reset_gpu,
1369 KBASE_RESET_GPU_COMMITTED, KBASE_RESET_GPU_HAPPENING) !=
1370 KBASE_RESET_GPU_COMMITTED) {
1371 /* Reset has already occurred */
1375 queue_work(kbdev->hwaccess.backend.reset_workq,
1376 &kbdev->hwaccess.backend.reset_work);
1379 static void kbasep_try_reset_gpu_early(struct kbase_device *kbdev)
1381 unsigned long flags;
1382 struct kbasep_js_device_data *js_devdata;
1384 js_devdata = &kbdev->js_data;
1385 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1386 kbasep_try_reset_gpu_early_locked(kbdev);
1387 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1391 * kbase_prepare_to_reset_gpu_locked - Prepare for resetting the GPU
1392 * @kbdev: kbase device
1394 * This function just soft-stops all the slots to ensure that as many jobs as
1395 * possible are saved.
1398 * The function returns a boolean which should be interpreted as follows:
1399 * true - Prepared for reset, kbase_reset_gpu_locked should be called.
1400 * false - Another thread is performing a reset, kbase_reset_gpu should
1403 bool kbase_prepare_to_reset_gpu_locked(struct kbase_device *kbdev)
1407 KBASE_DEBUG_ASSERT(kbdev);
1409 if (atomic_cmpxchg(&kbdev->hwaccess.backend.reset_gpu,
1410 KBASE_RESET_GPU_NOT_PENDING,
1411 KBASE_RESET_GPU_PREPARED) !=
1412 KBASE_RESET_GPU_NOT_PENDING) {
1413 /* Some other thread is already resetting the GPU */
1417 kbase_disjoint_state_up(kbdev);
1419 for (i = 0; i < kbdev->gpu_props.num_job_slots; i++)
1420 kbase_job_slot_softstop(kbdev, i, NULL);
1425 bool kbase_prepare_to_reset_gpu(struct kbase_device *kbdev)
1427 unsigned long flags;
1429 struct kbasep_js_device_data *js_devdata;
1431 js_devdata = &kbdev->js_data;
1432 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1433 ret = kbase_prepare_to_reset_gpu_locked(kbdev);
1434 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1438 KBASE_EXPORT_TEST_API(kbase_prepare_to_reset_gpu);
1441 * This function should be called after kbase_prepare_to_reset_gpu if it
1442 * returns true. It should never be called without a corresponding call to
1443 * kbase_prepare_to_reset_gpu.
1445 * After this function is called (or not called if kbase_prepare_to_reset_gpu
1446 * returned false), the caller should wait for
1447 * kbdev->hwaccess.backend.reset_waitq to be signalled to know when the reset
1450 void kbase_reset_gpu(struct kbase_device *kbdev)
1452 KBASE_DEBUG_ASSERT(kbdev);
1454 /* Note this is an assert/atomic_set because it is a software issue for
1455 * a race to be occuring here */
1456 KBASE_DEBUG_ASSERT(atomic_read(&kbdev->hwaccess.backend.reset_gpu) ==
1457 KBASE_RESET_GPU_PREPARED);
1458 atomic_set(&kbdev->hwaccess.backend.reset_gpu,
1459 KBASE_RESET_GPU_COMMITTED);
1461 dev_err(kbdev->dev, "Preparing to soft-reset GPU: Waiting (upto %d ms) for all jobs to complete soft-stop\n",
1462 kbdev->reset_timeout_ms);
1464 hrtimer_start(&kbdev->hwaccess.backend.reset_timer,
1465 HR_TIMER_DELAY_MSEC(kbdev->reset_timeout_ms),
1468 /* Try resetting early */
1469 kbasep_try_reset_gpu_early(kbdev);
1471 KBASE_EXPORT_TEST_API(kbase_reset_gpu);
1473 void kbase_reset_gpu_locked(struct kbase_device *kbdev)
1475 KBASE_DEBUG_ASSERT(kbdev);
1477 /* Note this is an assert/atomic_set because it is a software issue for
1478 * a race to be occuring here */
1479 KBASE_DEBUG_ASSERT(atomic_read(&kbdev->hwaccess.backend.reset_gpu) ==
1480 KBASE_RESET_GPU_PREPARED);
1481 atomic_set(&kbdev->hwaccess.backend.reset_gpu,
1482 KBASE_RESET_GPU_COMMITTED);
1484 dev_err(kbdev->dev, "Preparing to soft-reset GPU: Waiting (upto %d ms) for all jobs to complete soft-stop\n",
1485 kbdev->reset_timeout_ms);
1486 hrtimer_start(&kbdev->hwaccess.backend.reset_timer,
1487 HR_TIMER_DELAY_MSEC(kbdev->reset_timeout_ms),
1490 /* Try resetting early */
1491 kbasep_try_reset_gpu_early_locked(kbdev);
1494 void kbase_reset_gpu_silent(struct kbase_device *kbdev)
1496 if (atomic_cmpxchg(&kbdev->hwaccess.backend.reset_gpu,
1497 KBASE_RESET_GPU_NOT_PENDING,
1498 KBASE_RESET_GPU_SILENT) !=
1499 KBASE_RESET_GPU_NOT_PENDING) {
1500 /* Some other thread is already resetting the GPU */
1504 kbase_disjoint_state_up(kbdev);
1506 queue_work(kbdev->hwaccess.backend.reset_workq,
1507 &kbdev->hwaccess.backend.reset_work);
1510 bool kbase_reset_gpu_active(struct kbase_device *kbdev)
1512 if (atomic_read(&kbdev->hwaccess.backend.reset_gpu) ==
1513 KBASE_RESET_GPU_NOT_PENDING)
1518 #endif /* KBASE_GPU_RESET_EN */