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