xref: /linux/drivers/accel/habanalabs/common/context.c (revision 1fd1dc41724319406b0aff221a352a400b0ddfc5)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /*
4  * Copyright 2016-2021 HabanaLabs, Ltd.
5  * All Rights Reserved.
6  */
7 
8 #include "habanalabs.h"
9 
10 #include <linux/slab.h>
11 
12 static void encaps_handle_do_release(struct hl_cs_encaps_sig_handle *handle, bool put_hw_sob,
13 					bool put_ctx)
14 {
15 	struct hl_encaps_signals_mgr *mgr = &handle->ctx->sig_mgr;
16 
17 	if (put_hw_sob)
18 		hw_sob_put(handle->hw_sob);
19 
20 	spin_lock(&mgr->lock);
21 	idr_remove(&mgr->handles, handle->id);
22 	spin_unlock(&mgr->lock);
23 
24 	if (put_ctx)
25 		hl_ctx_put(handle->ctx);
26 
27 	kfree(handle);
28 }
29 
30 void hl_encaps_release_handle_and_put_ctx(struct kref *ref)
31 {
32 	struct hl_cs_encaps_sig_handle *handle =
33 			container_of(ref, struct hl_cs_encaps_sig_handle, refcount);
34 
35 	encaps_handle_do_release(handle, false, true);
36 }
37 
38 static void hl_encaps_release_handle_and_put_sob(struct kref *ref)
39 {
40 	struct hl_cs_encaps_sig_handle *handle =
41 			container_of(ref, struct hl_cs_encaps_sig_handle, refcount);
42 
43 	encaps_handle_do_release(handle, true, false);
44 }
45 
46 void hl_encaps_release_handle_and_put_sob_ctx(struct kref *ref)
47 {
48 	struct hl_cs_encaps_sig_handle *handle =
49 			container_of(ref, struct hl_cs_encaps_sig_handle, refcount);
50 
51 	encaps_handle_do_release(handle, true, true);
52 }
53 
54 static void hl_encaps_sig_mgr_init(struct hl_encaps_signals_mgr *mgr)
55 {
56 	spin_lock_init(&mgr->lock);
57 	idr_init(&mgr->handles);
58 }
59 
60 static void hl_encaps_sig_mgr_fini(struct hl_device *hdev, struct hl_encaps_signals_mgr *mgr)
61 {
62 	struct hl_cs_encaps_sig_handle *handle;
63 	struct idr *idp;
64 	u32 id;
65 
66 	idp = &mgr->handles;
67 
68 	/* The IDR is expected to be empty at this stage, because any left signal should have been
69 	 * released as part of CS roll-back.
70 	 */
71 	if (!idr_is_empty(idp)) {
72 		dev_warn(hdev->dev,
73 			"device released while some encaps signals handles are still allocated\n");
74 		idr_for_each_entry(idp, handle, id)
75 			kref_put(&handle->refcount, hl_encaps_release_handle_and_put_sob);
76 	}
77 
78 	idr_destroy(&mgr->handles);
79 }
80 
81 static void hl_ctx_fini(struct hl_ctx *ctx)
82 {
83 	struct hl_device *hdev = ctx->hdev;
84 	int i;
85 
86 	/* Release all allocated HW block mapped list entries and destroy
87 	 * the mutex.
88 	 */
89 	hl_hw_block_mem_fini(ctx);
90 
91 	/*
92 	 * If we arrived here, there are no jobs waiting for this context
93 	 * on its queues so we can safely remove it.
94 	 * This is because for each CS, we increment the ref count and for
95 	 * every CS that was finished we decrement it and we won't arrive
96 	 * to this function unless the ref count is 0
97 	 */
98 
99 	for (i = 0 ; i < hdev->asic_prop.max_pending_cs ; i++)
100 		hl_fence_put(ctx->cs_pending[i]);
101 
102 	kfree(ctx->cs_pending);
103 
104 	if (ctx->asid != HL_KERNEL_ASID_ID) {
105 		dev_dbg(hdev->dev, "closing user context, asid=%u\n", ctx->asid);
106 
107 		/* The engines are stopped as there is no executing CS, but the
108 		 * Coresight might be still working by accessing addresses
109 		 * related to the stopped engines. Hence stop it explicitly.
110 		 */
111 		if (hdev->in_debug)
112 			hl_device_set_debug_mode(hdev, ctx, false);
113 
114 		hdev->asic_funcs->ctx_fini(ctx);
115 
116 		hl_dec_ctx_fini(ctx);
117 
118 		hl_cb_va_pool_fini(ctx);
119 		hl_vm_ctx_fini(ctx);
120 		hl_asid_free(hdev, ctx->asid);
121 		hl_encaps_sig_mgr_fini(hdev, &ctx->sig_mgr);
122 		mutex_destroy(&ctx->ts_reg_lock);
123 	} else {
124 		dev_dbg(hdev->dev, "closing kernel context\n");
125 		hdev->asic_funcs->ctx_fini(ctx);
126 		hl_vm_ctx_fini(ctx);
127 		hl_mmu_ctx_fini(ctx);
128 	}
129 }
130 
131 void hl_ctx_do_release(struct kref *ref)
132 {
133 	struct hl_ctx *ctx;
134 
135 	ctx = container_of(ref, struct hl_ctx, refcount);
136 
137 	hl_ctx_fini(ctx);
138 
139 	if (ctx->hpriv) {
140 		struct hl_fpriv *hpriv = ctx->hpriv;
141 
142 		mutex_lock(&hpriv->ctx_lock);
143 		hpriv->ctx = NULL;
144 		mutex_unlock(&hpriv->ctx_lock);
145 
146 		hl_hpriv_put(hpriv);
147 	}
148 
149 	kfree(ctx);
150 }
151 
152 int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv)
153 {
154 	struct hl_ctx_mgr *ctx_mgr = &hpriv->ctx_mgr;
155 	struct hl_ctx *ctx;
156 	int rc;
157 
158 	ctx = kzalloc_obj(*ctx);
159 	if (!ctx) {
160 		rc = -ENOMEM;
161 		goto out_err;
162 	}
163 
164 	mutex_lock(&ctx_mgr->lock);
165 	rc = idr_alloc(&ctx_mgr->handles, ctx, 1, 0, GFP_KERNEL);
166 	mutex_unlock(&ctx_mgr->lock);
167 
168 	if (rc < 0) {
169 		dev_err(hdev->dev, "Failed to allocate IDR for a new CTX\n");
170 		goto free_ctx;
171 	}
172 
173 	ctx->handle = rc;
174 
175 	rc = hl_ctx_init(hdev, ctx, false);
176 	if (rc)
177 		goto remove_from_idr;
178 
179 	hl_hpriv_get(hpriv);
180 	ctx->hpriv = hpriv;
181 
182 	/* TODO: remove for multiple contexts per process */
183 	hpriv->ctx = ctx;
184 
185 	/* TODO: remove the following line for multiple process support */
186 	hdev->is_compute_ctx_active = true;
187 
188 	return 0;
189 
190 remove_from_idr:
191 	mutex_lock(&ctx_mgr->lock);
192 	idr_remove(&ctx_mgr->handles, ctx->handle);
193 	mutex_unlock(&ctx_mgr->lock);
194 free_ctx:
195 	kfree(ctx);
196 out_err:
197 	return rc;
198 }
199 
200 int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx)
201 {
202 	int rc = 0, i;
203 
204 	ctx->hdev = hdev;
205 
206 	kref_init(&ctx->refcount);
207 
208 	ctx->cs_sequence = 1;
209 	spin_lock_init(&ctx->cs_lock);
210 	atomic_set(&ctx->thread_ctx_switch_token, 1);
211 	ctx->thread_ctx_switch_wait_token = 0;
212 	ctx->cs_pending = kzalloc_objs(struct hl_fence *,
213 				       hdev->asic_prop.max_pending_cs);
214 	if (!ctx->cs_pending)
215 		return -ENOMEM;
216 
217 	INIT_LIST_HEAD(&ctx->outcome_store.used_list);
218 	INIT_LIST_HEAD(&ctx->outcome_store.free_list);
219 	hash_init(ctx->outcome_store.outcome_map);
220 	for (i = 0; i < ARRAY_SIZE(ctx->outcome_store.nodes_pool); ++i)
221 		list_add(&ctx->outcome_store.nodes_pool[i].list_link,
222 			 &ctx->outcome_store.free_list);
223 
224 	hl_hw_block_mem_init(ctx);
225 
226 	if (is_kernel_ctx) {
227 		ctx->asid = HL_KERNEL_ASID_ID; /* Kernel driver gets ASID 0 */
228 		rc = hl_vm_ctx_init(ctx);
229 		if (rc) {
230 			dev_err(hdev->dev, "Failed to init mem ctx module\n");
231 			rc = -ENOMEM;
232 			goto err_hw_block_mem_fini;
233 		}
234 
235 		rc = hdev->asic_funcs->ctx_init(ctx);
236 		if (rc) {
237 			dev_err(hdev->dev, "ctx_init failed\n");
238 			goto err_vm_ctx_fini;
239 		}
240 	} else {
241 		ctx->asid = hl_asid_alloc(hdev);
242 		if (!ctx->asid) {
243 			dev_err(hdev->dev, "No free ASID, failed to create context\n");
244 			rc = -ENOMEM;
245 			goto err_hw_block_mem_fini;
246 		}
247 
248 		rc = hl_vm_ctx_init(ctx);
249 		if (rc) {
250 			dev_err(hdev->dev, "Failed to init mem ctx module\n");
251 			rc = -ENOMEM;
252 			goto err_asid_free;
253 		}
254 
255 		rc = hl_cb_va_pool_init(ctx);
256 		if (rc) {
257 			dev_err(hdev->dev,
258 				"Failed to init VA pool for mapped CB\n");
259 			goto err_vm_ctx_fini;
260 		}
261 
262 		rc = hdev->asic_funcs->ctx_init(ctx);
263 		if (rc) {
264 			dev_err(hdev->dev, "ctx_init failed\n");
265 			goto err_cb_va_pool_fini;
266 		}
267 
268 		hl_encaps_sig_mgr_init(&ctx->sig_mgr);
269 
270 		mutex_init(&ctx->ts_reg_lock);
271 
272 		dev_dbg(hdev->dev, "create user context, comm=\"%s\", asid=%u\n",
273 			current->comm, ctx->asid);
274 	}
275 
276 	return 0;
277 
278 err_cb_va_pool_fini:
279 	hl_cb_va_pool_fini(ctx);
280 err_vm_ctx_fini:
281 	hl_vm_ctx_fini(ctx);
282 err_asid_free:
283 	if (ctx->asid != HL_KERNEL_ASID_ID)
284 		hl_asid_free(hdev, ctx->asid);
285 err_hw_block_mem_fini:
286 	hl_hw_block_mem_fini(ctx);
287 	kfree(ctx->cs_pending);
288 
289 	return rc;
290 }
291 
292 static int hl_ctx_get_unless_zero(struct hl_ctx *ctx)
293 {
294 	return kref_get_unless_zero(&ctx->refcount);
295 }
296 
297 void hl_ctx_get(struct hl_ctx *ctx)
298 {
299 	kref_get(&ctx->refcount);
300 }
301 
302 int hl_ctx_put(struct hl_ctx *ctx)
303 {
304 	return kref_put(&ctx->refcount, hl_ctx_do_release);
305 }
306 
307 struct hl_ctx *hl_get_compute_ctx(struct hl_device *hdev)
308 {
309 	struct hl_ctx *ctx = NULL;
310 	struct hl_fpriv *hpriv;
311 
312 	mutex_lock(&hdev->fpriv_list_lock);
313 
314 	list_for_each_entry(hpriv, &hdev->fpriv_list, dev_node) {
315 		mutex_lock(&hpriv->ctx_lock);
316 		ctx = hpriv->ctx;
317 		if (ctx && !hl_ctx_get_unless_zero(ctx))
318 			ctx = NULL;
319 		mutex_unlock(&hpriv->ctx_lock);
320 
321 		/* There can only be a single user which has opened the compute device, so exit
322 		 * immediately once we find its context or if we see that it has been released
323 		 */
324 		break;
325 	}
326 
327 	mutex_unlock(&hdev->fpriv_list_lock);
328 
329 	return ctx;
330 }
331 
332 /*
333  * hl_ctx_get_fence_locked - get CS fence under CS lock
334  *
335  * @ctx: pointer to the context structure.
336  * @seq: CS sequences number
337  *
338  * @return valid fence pointer on success, NULL if fence is gone, otherwise
339  *         error pointer.
340  *
341  * NOTE: this function shall be called with cs_lock locked
342  */
343 static struct hl_fence *hl_ctx_get_fence_locked(struct hl_ctx *ctx, u64 seq)
344 {
345 	struct asic_fixed_properties *asic_prop = &ctx->hdev->asic_prop;
346 	struct hl_fence *fence;
347 
348 	if (seq >= ctx->cs_sequence)
349 		return ERR_PTR(-EINVAL);
350 
351 	if (seq + asic_prop->max_pending_cs < ctx->cs_sequence)
352 		return NULL;
353 
354 	fence = ctx->cs_pending[seq & (asic_prop->max_pending_cs - 1)];
355 	hl_fence_get(fence);
356 	return fence;
357 }
358 
359 struct hl_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq)
360 {
361 	struct hl_fence *fence;
362 
363 	spin_lock(&ctx->cs_lock);
364 
365 	fence = hl_ctx_get_fence_locked(ctx, seq);
366 
367 	spin_unlock(&ctx->cs_lock);
368 
369 	return fence;
370 }
371 
372 /*
373  * hl_ctx_get_fences - get multiple CS fences under the same CS lock
374  *
375  * @ctx: pointer to the context structure.
376  * @seq_arr: array of CS sequences to wait for
377  * @fence: fence array to store the CS fences
378  * @arr_len: length of seq_arr and fence_arr
379  *
380  * @return 0 on success, otherwise non 0 error code
381  */
382 int hl_ctx_get_fences(struct hl_ctx *ctx, u64 *seq_arr,
383 				struct hl_fence **fence, u32 arr_len)
384 {
385 	struct hl_fence **fence_arr_base = fence;
386 	int i, rc = 0;
387 
388 	spin_lock(&ctx->cs_lock);
389 
390 	for (i = 0; i < arr_len; i++, fence++) {
391 		u64 seq = seq_arr[i];
392 
393 		*fence = hl_ctx_get_fence_locked(ctx, seq);
394 
395 		if (IS_ERR(*fence)) {
396 			dev_err(ctx->hdev->dev,
397 				"Failed to get fence for CS with seq 0x%llx\n",
398 					seq);
399 			rc = PTR_ERR(*fence);
400 			break;
401 		}
402 	}
403 
404 	spin_unlock(&ctx->cs_lock);
405 
406 	if (rc)
407 		hl_fences_put(fence_arr_base, i);
408 
409 	return rc;
410 }
411 
412 /*
413  * hl_ctx_mgr_init - initialize the context manager
414  *
415  * @ctx_mgr: pointer to context manager structure
416  *
417  * This manager is an object inside the hpriv object of the user process.
418  * The function is called when a user process opens the FD.
419  */
420 void hl_ctx_mgr_init(struct hl_ctx_mgr *ctx_mgr)
421 {
422 	mutex_init(&ctx_mgr->lock);
423 	idr_init(&ctx_mgr->handles);
424 }
425 
426 /*
427  * hl_ctx_mgr_fini - finalize the context manager
428  *
429  * @hdev: pointer to device structure
430  * @ctx_mgr: pointer to context manager structure
431  *
432  * This function goes over all the contexts in the manager and frees them.
433  * It is called when a process closes the FD.
434  */
435 void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *ctx_mgr)
436 {
437 	struct hl_ctx *ctx;
438 	struct idr *idp;
439 	u32 id;
440 
441 	idp = &ctx_mgr->handles;
442 
443 	idr_for_each_entry(idp, ctx, id)
444 		kref_put(&ctx->refcount, hl_ctx_do_release);
445 
446 	idr_destroy(&ctx_mgr->handles);
447 	mutex_destroy(&ctx_mgr->lock);
448 }
449