Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright 2016-2019 HabanaLabs, Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include "habanalabs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) static void hl_ctx_fini(struct hl_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	struct hl_device *hdev = ctx->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	u64 idle_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	 * If we arrived here, there are no jobs waiting for this context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	 * on its queues so we can safely remove it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	 * This is because for each CS, we increment the ref count and for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	 * every CS that was finished we decrement it and we won't arrive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	 * to this function unless the ref count is 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	for (i = 0 ; i < hdev->asic_prop.max_pending_cs ; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		hl_fence_put(ctx->cs_pending[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	kfree(ctx->cs_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	if (ctx->asid != HL_KERNEL_ASID_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		dev_dbg(hdev->dev, "closing user context %d\n", ctx->asid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		/* The engines are stopped as there is no executing CS, but the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		 * Coresight might be still working by accessing addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		 * related to the stopped engines. Hence stop it explicitly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		 * Stop only if this is the compute context, as there can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		 * only one compute context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		if ((hdev->in_debug) && (hdev->compute_ctx == ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 			hl_device_set_debug_mode(hdev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		hl_cb_va_pool_fini(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		hl_vm_ctx_fini(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		hl_asid_free(hdev, ctx->asid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		if ((!hdev->pldm) && (hdev->pdev) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 				(!hdev->asic_funcs->is_device_idle(hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 							&idle_mask, NULL)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			dev_notice(hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 				"device not idle after user context is closed (0x%llx)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 				idle_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		dev_dbg(hdev->dev, "closing kernel context\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		hl_mmu_ctx_fini(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) void hl_ctx_do_release(struct kref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct hl_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	ctx = container_of(ref, struct hl_ctx, refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	hl_ctx_fini(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (ctx->hpriv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		hl_hpriv_put(ctx->hpriv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	kfree(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct hl_ctx_mgr *mgr = &hpriv->ctx_mgr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct hl_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (!ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	mutex_lock(&mgr->ctx_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	rc = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	mutex_unlock(&mgr->ctx_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		dev_err(hdev->dev, "Failed to allocate IDR for a new CTX\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		goto free_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	ctx->handle = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	rc = hl_ctx_init(hdev, ctx, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		goto remove_from_idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	hl_hpriv_get(hpriv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	ctx->hpriv = hpriv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	/* TODO: remove for multiple contexts per process */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	hpriv->ctx = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	/* TODO: remove the following line for multiple process support */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	hdev->compute_ctx = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) remove_from_idr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	mutex_lock(&mgr->ctx_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	idr_remove(&mgr->ctx_handles, ctx->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	mutex_unlock(&mgr->ctx_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) free_ctx:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	kfree(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (kref_put(&ctx->refcount, hl_ctx_do_release) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	dev_warn(hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		"user process released device but its command submissions are still executing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	ctx->hdev = hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	kref_init(&ctx->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	ctx->cs_sequence = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	spin_lock_init(&ctx->cs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	atomic_set(&ctx->thread_ctx_switch_token, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	ctx->thread_ctx_switch_wait_token = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	ctx->cs_pending = kcalloc(hdev->asic_prop.max_pending_cs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 				sizeof(struct hl_fence *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 				GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (!ctx->cs_pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (is_kernel_ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		ctx->asid = HL_KERNEL_ASID_ID; /* Kernel driver gets ASID 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		rc = hl_mmu_ctx_init(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			dev_err(hdev->dev, "Failed to init mmu ctx module\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			goto err_free_cs_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		ctx->asid = hl_asid_alloc(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		if (!ctx->asid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			dev_err(hdev->dev, "No free ASID, failed to create context\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			goto err_free_cs_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		rc = hl_vm_ctx_init(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			dev_err(hdev->dev, "Failed to init mem ctx module\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			goto err_asid_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		rc = hl_cb_va_pool_init(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			dev_err(hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 				"Failed to init VA pool for mapped CB\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			goto err_vm_ctx_fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		rc = hdev->asic_funcs->ctx_init(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			dev_err(hdev->dev, "ctx_init failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			goto err_cb_va_pool_fini;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		dev_dbg(hdev->dev, "create user context %d\n", ctx->asid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) err_cb_va_pool_fini:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	hl_cb_va_pool_fini(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) err_vm_ctx_fini:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	hl_vm_ctx_fini(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) err_asid_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	hl_asid_free(hdev, ctx->asid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) err_free_cs_pending:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	kfree(ctx->cs_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) void hl_ctx_get(struct hl_device *hdev, struct hl_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	kref_get(&ctx->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) int hl_ctx_put(struct hl_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	return kref_put(&ctx->refcount, hl_ctx_do_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct hl_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	struct asic_fixed_properties *asic_prop = &ctx->hdev->asic_prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	struct hl_fence *fence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	spin_lock(&ctx->cs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	if (seq >= ctx->cs_sequence) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		spin_unlock(&ctx->cs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (seq + asic_prop->max_pending_cs < ctx->cs_sequence) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		spin_unlock(&ctx->cs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	fence = ctx->cs_pending[seq & (asic_prop->max_pending_cs - 1)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	hl_fence_get(fence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	spin_unlock(&ctx->cs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	return fence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  * hl_ctx_mgr_init - initialize the context manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  * @mgr: pointer to context manager structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  * This manager is an object inside the hpriv object of the user process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  * The function is called when a user process opens the FD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	mutex_init(&mgr->ctx_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	idr_init(&mgr->ctx_handles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)  * hl_ctx_mgr_fini - finalize the context manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  * @hdev: pointer to device structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  * @mgr: pointer to context manager structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  * This function goes over all the contexts in the manager and frees them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  * It is called when a process closes the FD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	struct hl_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	struct idr *idp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	u32 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	idp = &mgr->ctx_handles;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	idr_for_each_entry(idp, ctx, id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		hl_ctx_free(hdev, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	idr_destroy(&mgr->ctx_handles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	mutex_destroy(&mgr->ctx_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }