habanalabs: create common folder
[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         /*
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
23          */
24
25         for (i = 0 ; i < hdev->asic_prop.max_pending_cs ; i++)
26                 dma_fence_put(ctx->cs_pending[i]);
27
28         kfree(ctx->cs_pending);
29
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
36                  */
37                 if ((hdev->in_debug) && (hdev->compute_ctx == ctx))
38                         hl_device_set_debug_mode(hdev, false);
39
40                 hl_vm_ctx_fini(ctx);
41                 hl_asid_free(hdev, ctx->asid);
42         } else {
43                 hl_mmu_ctx_fini(ctx);
44         }
45 }
46
47 void hl_ctx_do_release(struct kref *ref)
48 {
49         struct hl_ctx *ctx;
50
51         ctx = container_of(ref, struct hl_ctx, refcount);
52
53         hl_ctx_fini(ctx);
54
55         if (ctx->hpriv)
56                 hl_hpriv_put(ctx->hpriv);
57
58         kfree(ctx);
59 }
60
61 int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv)
62 {
63         struct hl_ctx_mgr *mgr = &hpriv->ctx_mgr;
64         struct hl_ctx *ctx;
65         int rc;
66
67         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
68         if (!ctx) {
69                 rc = -ENOMEM;
70                 goto out_err;
71         }
72
73         mutex_lock(&mgr->ctx_lock);
74         rc = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
75         mutex_unlock(&mgr->ctx_lock);
76
77         if (rc < 0) {
78                 dev_err(hdev->dev, "Failed to allocate IDR for a new CTX\n");
79                 goto free_ctx;
80         }
81
82         ctx->handle = rc;
83
84         rc = hl_ctx_init(hdev, ctx, false);
85         if (rc)
86                 goto remove_from_idr;
87
88         hl_hpriv_get(hpriv);
89         ctx->hpriv = hpriv;
90
91         /* TODO: remove for multiple contexts per process */
92         hpriv->ctx = ctx;
93
94         /* TODO: remove the following line for multiple process support */
95         hdev->compute_ctx = ctx;
96
97         return 0;
98
99 remove_from_idr:
100         mutex_lock(&mgr->ctx_lock);
101         idr_remove(&mgr->ctx_handles, ctx->handle);
102         mutex_unlock(&mgr->ctx_lock);
103 free_ctx:
104         kfree(ctx);
105 out_err:
106         return rc;
107 }
108
109 void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx)
110 {
111         if (kref_put(&ctx->refcount, hl_ctx_do_release) == 1)
112                 return;
113
114         dev_warn(hdev->dev,
115                 "user process released device but its command submissions are still executing\n");
116 }
117
118 int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx)
119 {
120         int rc = 0;
121
122         ctx->hdev = hdev;
123
124         kref_init(&ctx->refcount);
125
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 *),
132                                 GFP_KERNEL);
133         if (!ctx->cs_pending)
134                 return -ENOMEM;
135
136         if (is_kernel_ctx) {
137                 ctx->asid = HL_KERNEL_ASID_ID; /* Kernel driver gets ASID 0 */
138                 rc = hl_mmu_ctx_init(ctx);
139                 if (rc) {
140                         dev_err(hdev->dev, "Failed to init mmu ctx module\n");
141                         goto mem_ctx_err;
142                 }
143         } else {
144                 ctx->asid = hl_asid_alloc(hdev);
145                 if (!ctx->asid) {
146                         dev_err(hdev->dev, "No free ASID, failed to create context\n");
147                         return -ENOMEM;
148                 }
149
150                 rc = hl_vm_ctx_init(ctx);
151                 if (rc) {
152                         dev_err(hdev->dev, "Failed to init mem ctx module\n");
153                         rc = -ENOMEM;
154                         goto mem_ctx_err;
155                 }
156         }
157
158         return 0;
159
160 mem_ctx_err:
161         if (ctx->asid != HL_KERNEL_ASID_ID)
162                 hl_asid_free(hdev, ctx->asid);
163
164         return rc;
165 }
166
167 void hl_ctx_get(struct hl_device *hdev, struct hl_ctx *ctx)
168 {
169         kref_get(&ctx->refcount);
170 }
171
172 int hl_ctx_put(struct hl_ctx *ctx)
173 {
174         return kref_put(&ctx->refcount, hl_ctx_do_release);
175 }
176
177 struct dma_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq)
178 {
179         struct asic_fixed_properties *asic_prop = &ctx->hdev->asic_prop;
180         struct dma_fence *fence;
181
182         spin_lock(&ctx->cs_lock);
183
184         if (seq >= ctx->cs_sequence) {
185                 spin_unlock(&ctx->cs_lock);
186                 return ERR_PTR(-EINVAL);
187         }
188
189         if (seq + asic_prop->max_pending_cs < ctx->cs_sequence) {
190                 spin_unlock(&ctx->cs_lock);
191                 return NULL;
192         }
193
194         fence = dma_fence_get(
195                         ctx->cs_pending[seq & (asic_prop->max_pending_cs - 1)]);
196         spin_unlock(&ctx->cs_lock);
197
198         return fence;
199 }
200
201 /*
202  * hl_ctx_mgr_init - initialize the context manager
203  *
204  * @mgr: pointer to context manager structure
205  *
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.
208  */
209 void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr)
210 {
211         mutex_init(&mgr->ctx_lock);
212         idr_init(&mgr->ctx_handles);
213 }
214
215 /*
216  * hl_ctx_mgr_fini - finalize the context manager
217  *
218  * @hdev: pointer to device structure
219  * @mgr: pointer to context manager structure
220  *
221  * This function goes over all the contexts in the manager and frees them.
222  * It is called when a process closes the FD.
223  */
224 void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr)
225 {
226         struct hl_ctx *ctx;
227         struct idr *idp;
228         u32 id;
229
230         idp = &mgr->ctx_handles;
231
232         idr_for_each_entry(idp, ctx, id)
233                 hl_ctx_free(hdev, ctx);
234
235         idr_destroy(&mgr->ctx_handles);
236         mutex_destroy(&mgr->ctx_lock);
237 }