1 // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
2 /*
3 *
4 * (C) COPYRIGHT 2013-2022 ARM Limited. All rights reserved.
5 *
6 * This program is free software and is provided to you under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation, and any use by you of this program is subject to the terms
9 * of such GNU license.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you can access it online at
18 * http://www.gnu.org/licenses/gpl-2.0.html.
19 *
20 */
21
22 /*
23 * HW access job manager common APIs
24 */
25
26 #include <mali_kbase.h>
27 #include "mali_kbase_hwaccess_jm.h"
28 #include "mali_kbase_jm.h"
29
30 #if !MALI_USE_CSF
31 /**
32 * kbase_jm_next_job() - Attempt to run the next @nr_jobs_to_submit jobs on slot
33 * @js on the active context.
34 * @kbdev: Device pointer
35 * @js: Job slot to run on
36 * @nr_jobs_to_submit: Number of jobs to attempt to submit
37 *
38 * Return: true if slot can still be submitted on, false if slot is now full.
39 */
kbase_jm_next_job(struct kbase_device * kbdev,unsigned int js,int nr_jobs_to_submit)40 static bool kbase_jm_next_job(struct kbase_device *kbdev, unsigned int js, int nr_jobs_to_submit)
41 {
42 struct kbase_context *kctx;
43 int i;
44
45 kctx = kbdev->hwaccess.active_kctx[js];
46 dev_dbg(kbdev->dev, "Trying to run the next %d jobs in kctx %pK (s:%u)\n",
47 nr_jobs_to_submit, (void *)kctx, js);
48
49 if (!kctx)
50 return true;
51
52 for (i = 0; i < nr_jobs_to_submit; i++) {
53 struct kbase_jd_atom *katom = kbase_js_pull(kctx, js);
54
55 if (!katom)
56 return true; /* Context has no jobs on this slot */
57
58 kbase_backend_run_atom(kbdev, katom);
59 }
60
61 dev_dbg(kbdev->dev, "Slot ringbuffer should now be full (s:%u)\n", js);
62 return false;
63 }
64
kbase_jm_kick(struct kbase_device * kbdev,u32 js_mask)65 u32 kbase_jm_kick(struct kbase_device *kbdev, u32 js_mask)
66 {
67 u32 ret_mask = 0;
68
69 lockdep_assert_held(&kbdev->hwaccess_lock);
70 dev_dbg(kbdev->dev, "JM kick slot mask 0x%x\n", js_mask);
71
72 while (js_mask) {
73 unsigned int js = ffs(js_mask) - 1;
74 int nr_jobs_to_submit = kbase_backend_slot_free(kbdev, js);
75
76 if (kbase_jm_next_job(kbdev, js, nr_jobs_to_submit))
77 ret_mask |= (1 << js);
78
79 js_mask &= ~(1 << js);
80 }
81
82 dev_dbg(kbdev->dev, "Can still submit to mask 0x%x\n", ret_mask);
83 return ret_mask;
84 }
85
kbase_jm_try_kick(struct kbase_device * kbdev,u32 js_mask)86 void kbase_jm_try_kick(struct kbase_device *kbdev, u32 js_mask)
87 {
88 struct kbasep_js_device_data *js_devdata = &kbdev->js_data;
89
90 lockdep_assert_held(&kbdev->hwaccess_lock);
91
92 if (!down_trylock(&js_devdata->schedule_sem)) {
93 kbase_jm_kick(kbdev, js_mask);
94 up(&js_devdata->schedule_sem);
95 }
96 }
97
kbase_jm_try_kick_all(struct kbase_device * kbdev)98 void kbase_jm_try_kick_all(struct kbase_device *kbdev)
99 {
100 struct kbasep_js_device_data *js_devdata = &kbdev->js_data;
101
102 lockdep_assert_held(&kbdev->hwaccess_lock);
103
104 if (!down_trylock(&js_devdata->schedule_sem)) {
105 kbase_jm_kick_all(kbdev);
106 up(&js_devdata->schedule_sem);
107 }
108 }
109
kbase_jm_idle_ctx(struct kbase_device * kbdev,struct kbase_context * kctx)110 void kbase_jm_idle_ctx(struct kbase_device *kbdev, struct kbase_context *kctx)
111 {
112 unsigned int js;
113
114 lockdep_assert_held(&kbdev->hwaccess_lock);
115
116 for (js = 0; js < BASE_JM_MAX_NR_SLOTS; js++) {
117 if (kbdev->hwaccess.active_kctx[js] == kctx) {
118 dev_dbg(kbdev->dev, "Marking kctx %pK as inactive (s:%u)\n", (void *)kctx,
119 js);
120 kbdev->hwaccess.active_kctx[js] = NULL;
121 }
122 }
123 }
124
kbase_jm_return_atom_to_js(struct kbase_device * kbdev,struct kbase_jd_atom * katom)125 struct kbase_jd_atom *kbase_jm_return_atom_to_js(struct kbase_device *kbdev,
126 struct kbase_jd_atom *katom)
127 {
128 lockdep_assert_held(&kbdev->hwaccess_lock);
129
130 dev_dbg(kbdev->dev, "Atom %pK is returning with event code 0x%x\n",
131 (void *)katom, katom->event_code);
132
133 KBASE_KTRACE_ADD_JM(kbdev, JM_RETURN_ATOM_TO_JS, katom->kctx, katom,
134 katom->jc, katom->event_code);
135
136 if (katom->event_code != BASE_JD_EVENT_STOPPED &&
137 katom->event_code != BASE_JD_EVENT_REMOVED_FROM_NEXT) {
138 return kbase_js_complete_atom(katom, NULL);
139 }
140
141 kbase_js_unpull(katom->kctx, katom);
142
143 return NULL;
144 }
145
kbase_jm_complete(struct kbase_device * kbdev,struct kbase_jd_atom * katom,ktime_t * end_timestamp)146 struct kbase_jd_atom *kbase_jm_complete(struct kbase_device *kbdev,
147 struct kbase_jd_atom *katom, ktime_t *end_timestamp)
148 {
149 lockdep_assert_held(&kbdev->hwaccess_lock);
150
151 return kbase_js_complete_atom(katom, end_timestamp);
152 }
153 #endif /* !MALI_USE_CSF */
154