1 // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
2 /*
3  *
4  * (C) COPYRIGHT 2014-2021 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, int js, int nr_jobs_to_submit)40 static bool kbase_jm_next_job(struct kbase_device *kbdev, int js,
41 				int nr_jobs_to_submit)
42 {
43 	struct kbase_context *kctx;
44 	int i;
45 
46 	kctx = kbdev->hwaccess.active_kctx[js];
47 	dev_dbg(kbdev->dev,
48 		"Trying to run the next %d jobs in kctx %pK (s:%d)\n",
49 		nr_jobs_to_submit, (void *)kctx, js);
50 
51 	if (!kctx)
52 		return true;
53 
54 	for (i = 0; i < nr_jobs_to_submit; i++) {
55 		struct kbase_jd_atom *katom = kbase_js_pull(kctx, js);
56 
57 		if (!katom)
58 			return true; /* Context has no jobs on this slot */
59 
60 		kbase_backend_run_atom(kbdev, katom);
61 	}
62 
63 	dev_dbg(kbdev->dev, "Slot ringbuffer should now be full (s:%d)\n", js);
64 	return false;
65 }
66 
kbase_jm_kick(struct kbase_device *kbdev, u32 js_mask)67 u32 kbase_jm_kick(struct kbase_device *kbdev, u32 js_mask)
68 {
69 	u32 ret_mask = 0;
70 
71 	lockdep_assert_held(&kbdev->hwaccess_lock);
72 	dev_dbg(kbdev->dev, "JM kick slot mask 0x%x\n", js_mask);
73 
74 	while (js_mask) {
75 		int js = ffs(js_mask) - 1;
76 		int nr_jobs_to_submit = kbase_backend_slot_free(kbdev, js);
77 
78 		if (kbase_jm_next_job(kbdev, js, nr_jobs_to_submit))
79 			ret_mask |= (1 << js);
80 
81 		js_mask &= ~(1 << js);
82 	}
83 
84 	dev_dbg(kbdev->dev, "Can still submit to mask 0x%x\n", ret_mask);
85 	return ret_mask;
86 }
87 
kbase_jm_try_kick(struct kbase_device *kbdev, u32 js_mask)88 void kbase_jm_try_kick(struct kbase_device *kbdev, u32 js_mask)
89 {
90 	struct kbasep_js_device_data *js_devdata = &kbdev->js_data;
91 
92 	lockdep_assert_held(&kbdev->hwaccess_lock);
93 
94 	if (!down_trylock(&js_devdata->schedule_sem)) {
95 		kbase_jm_kick(kbdev, js_mask);
96 		up(&js_devdata->schedule_sem);
97 	}
98 }
99 
kbase_jm_try_kick_all(struct kbase_device *kbdev)100 void kbase_jm_try_kick_all(struct kbase_device *kbdev)
101 {
102 	struct kbasep_js_device_data *js_devdata = &kbdev->js_data;
103 
104 	lockdep_assert_held(&kbdev->hwaccess_lock);
105 
106 	if (!down_trylock(&js_devdata->schedule_sem)) {
107 		kbase_jm_kick_all(kbdev);
108 		up(&js_devdata->schedule_sem);
109 	}
110 }
111 
kbase_jm_idle_ctx(struct kbase_device *kbdev, struct kbase_context *kctx)112 void kbase_jm_idle_ctx(struct kbase_device *kbdev, struct kbase_context *kctx)
113 {
114 	int js;
115 
116 	lockdep_assert_held(&kbdev->hwaccess_lock);
117 
118 	for (js = 0; js < BASE_JM_MAX_NR_SLOTS; js++) {
119 		if (kbdev->hwaccess.active_kctx[js] == kctx) {
120 			dev_dbg(kbdev->dev, "Marking kctx %pK as inactive (s:%d)\n",
121 					(void *)kctx, js);
122 			kbdev->hwaccess.active_kctx[js] = NULL;
123 		}
124 	}
125 }
126 
kbase_jm_return_atom_to_js(struct kbase_device *kbdev, struct kbase_jd_atom *katom)127 struct kbase_jd_atom *kbase_jm_return_atom_to_js(struct kbase_device *kbdev,
128 				struct kbase_jd_atom *katom)
129 {
130 	lockdep_assert_held(&kbdev->hwaccess_lock);
131 
132 	dev_dbg(kbdev->dev, "Atom %pK is returning with event code 0x%x\n",
133 		(void *)katom, katom->event_code);
134 
135 	KBASE_KTRACE_ADD_JM(kbdev, JM_RETURN_ATOM_TO_JS, katom->kctx, katom,
136 			    katom->jc, katom->event_code);
137 
138 	if (katom->event_code != BASE_JD_EVENT_STOPPED &&
139 			katom->event_code != BASE_JD_EVENT_REMOVED_FROM_NEXT) {
140 		return kbase_js_complete_atom(katom, NULL);
141 	} else {
142 		kbase_js_unpull(katom->kctx, katom);
143 		return NULL;
144 	}
145 }
146 
kbase_jm_complete(struct kbase_device *kbdev, struct kbase_jd_atom *katom, ktime_t *end_timestamp)147 struct kbase_jd_atom *kbase_jm_complete(struct kbase_device *kbdev,
148 		struct kbase_jd_atom *katom, ktime_t *end_timestamp)
149 {
150 	lockdep_assert_held(&kbdev->hwaccess_lock);
151 
152 	return kbase_js_complete_atom(katom, end_timestamp);
153 }
154 #endif /* !MALI_USE_CSF */
155