1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include "habanalabs.h"
10 #include <linux/slab.h>
12 static void hl_ctx_fini(struct hl_ctx *ctx)
14 struct hl_device *hdev = ctx->hdev;
18 * If we arrived here, there are no jobs waiting for this context
19 * on its queues so we can safely remove it.
20 * This is because for each CS, we increment the ref count and for
21 * every CS that was finished we decrement it and we won't arrive
22 * to this function unless the ref count is 0
25 for (i = 0 ; i < hdev->asic_prop.max_pending_cs ; i++)
26 dma_fence_put(ctx->cs_pending[i]);
28 kfree(ctx->cs_pending);
30 if (ctx->asid != HL_KERNEL_ASID_ID) {
31 /* The engines are stopped as there is no executing CS, but the
32 * Coresight might be still working by accessing addresses
33 * related to the stopped engines. Hence stop it explicitly.
34 * Stop only if this is the compute context, as there can be
35 * only one compute context
37 if ((hdev->in_debug) && (hdev->compute_ctx == ctx))
38 hl_device_set_debug_mode(hdev, false);
41 hl_asid_free(hdev, ctx->asid);
47 void hl_ctx_do_release(struct kref *ref)
51 ctx = container_of(ref, struct hl_ctx, refcount);
56 hl_hpriv_put(ctx->hpriv);
61 int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv)
63 struct hl_ctx_mgr *mgr = &hpriv->ctx_mgr;
67 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
73 mutex_lock(&mgr->ctx_lock);
74 rc = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
75 mutex_unlock(&mgr->ctx_lock);
78 dev_err(hdev->dev, "Failed to allocate IDR for a new CTX\n");
84 rc = hl_ctx_init(hdev, ctx, false);
91 /* TODO: remove for multiple contexts per process */
94 /* TODO: remove the following line for multiple process support */
95 hdev->compute_ctx = ctx;
100 mutex_lock(&mgr->ctx_lock);
101 idr_remove(&mgr->ctx_handles, ctx->handle);
102 mutex_unlock(&mgr->ctx_lock);
109 void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx)
111 if (kref_put(&ctx->refcount, hl_ctx_do_release) == 1)
115 "user process released device but its command submissions are still executing\n");
118 int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx)
124 kref_init(&ctx->refcount);
126 ctx->cs_sequence = 1;
127 spin_lock_init(&ctx->cs_lock);
128 atomic_set(&ctx->thread_ctx_switch_token, 1);
129 ctx->thread_ctx_switch_wait_token = 0;
130 ctx->cs_pending = kcalloc(hdev->asic_prop.max_pending_cs,
131 sizeof(struct dma_fence *),
133 if (!ctx->cs_pending)
137 ctx->asid = HL_KERNEL_ASID_ID; /* Kernel driver gets ASID 0 */
138 rc = hl_mmu_ctx_init(ctx);
140 dev_err(hdev->dev, "Failed to init mmu ctx module\n");
144 ctx->asid = hl_asid_alloc(hdev);
146 dev_err(hdev->dev, "No free ASID, failed to create context\n");
150 rc = hl_vm_ctx_init(ctx);
152 dev_err(hdev->dev, "Failed to init mem ctx module\n");
161 if (ctx->asid != HL_KERNEL_ASID_ID)
162 hl_asid_free(hdev, ctx->asid);
167 void hl_ctx_get(struct hl_device *hdev, struct hl_ctx *ctx)
169 kref_get(&ctx->refcount);
172 int hl_ctx_put(struct hl_ctx *ctx)
174 return kref_put(&ctx->refcount, hl_ctx_do_release);
177 struct dma_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq)
179 struct asic_fixed_properties *asic_prop = &ctx->hdev->asic_prop;
180 struct dma_fence *fence;
182 spin_lock(&ctx->cs_lock);
184 if (seq >= ctx->cs_sequence) {
185 spin_unlock(&ctx->cs_lock);
186 return ERR_PTR(-EINVAL);
189 if (seq + asic_prop->max_pending_cs < ctx->cs_sequence) {
190 spin_unlock(&ctx->cs_lock);
194 fence = dma_fence_get(
195 ctx->cs_pending[seq & (asic_prop->max_pending_cs - 1)]);
196 spin_unlock(&ctx->cs_lock);
202 * hl_ctx_mgr_init - initialize the context manager
204 * @mgr: pointer to context manager structure
206 * This manager is an object inside the hpriv object of the user process.
207 * The function is called when a user process opens the FD.
209 void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr)
211 mutex_init(&mgr->ctx_lock);
212 idr_init(&mgr->ctx_handles);
216 * hl_ctx_mgr_fini - finalize the context manager
218 * @hdev: pointer to device structure
219 * @mgr: pointer to context manager structure
221 * This function goes over all the contexts in the manager and frees them.
222 * It is called when a process closes the FD.
224 void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr)
230 idp = &mgr->ctx_handles;
232 idr_for_each_entry(idp, ctx, id)
233 hl_ctx_free(hdev, ctx);
235 idr_destroy(&mgr->ctx_handles);
236 mutex_destroy(&mgr->ctx_lock);