]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/misc/habanalabs/context.c
Merge tag 'usb-5.2-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
[linux.git] / drivers / misc / habanalabs / 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 < HL_MAX_PENDING_CS ; i++)
26                 dma_fence_put(ctx->cs_pending[i]);
27
28         if (ctx->asid != HL_KERNEL_ASID_ID) {
29                 /*
30                  * The engines are stopped as there is no executing CS, but the
31                  * Coresight might be still working by accessing addresses
32                  * related to the stopped engines. Hence stop it explicitly.
33                  */
34                 hdev->asic_funcs->halt_coresight(hdev);
35                 hl_vm_ctx_fini(ctx);
36                 hl_asid_free(hdev, ctx->asid);
37         }
38 }
39
40 void hl_ctx_do_release(struct kref *ref)
41 {
42         struct hl_ctx *ctx;
43
44         ctx = container_of(ref, struct hl_ctx, refcount);
45
46         hl_ctx_fini(ctx);
47
48         if (ctx->hpriv)
49                 hl_hpriv_put(ctx->hpriv);
50
51         kfree(ctx);
52 }
53
54 int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv)
55 {
56         struct hl_ctx_mgr *mgr = &hpriv->ctx_mgr;
57         struct hl_ctx *ctx;
58         int rc;
59
60         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
61         if (!ctx) {
62                 rc = -ENOMEM;
63                 goto out_err;
64         }
65
66         rc = hl_ctx_init(hdev, ctx, false);
67         if (rc)
68                 goto free_ctx;
69
70         hl_hpriv_get(hpriv);
71         ctx->hpriv = hpriv;
72
73         /* TODO: remove for multiple contexts */
74         hpriv->ctx = ctx;
75         hdev->user_ctx = ctx;
76
77         mutex_lock(&mgr->ctx_lock);
78         rc = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
79         mutex_unlock(&mgr->ctx_lock);
80
81         if (rc < 0) {
82                 dev_err(hdev->dev, "Failed to allocate IDR for a new CTX\n");
83                 hl_ctx_free(hdev, ctx);
84                 goto out_err;
85         }
86
87         return 0;
88
89 free_ctx:
90         kfree(ctx);
91 out_err:
92         return rc;
93 }
94
95 void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx)
96 {
97         if (kref_put(&ctx->refcount, hl_ctx_do_release) == 1)
98                 return;
99
100         dev_warn(hdev->dev,
101                 "Context %d closed or terminated but its CS are executing\n",
102                 ctx->asid);
103 }
104
105 int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx)
106 {
107         int rc = 0;
108
109         ctx->hdev = hdev;
110
111         kref_init(&ctx->refcount);
112
113         ctx->cs_sequence = 1;
114         spin_lock_init(&ctx->cs_lock);
115         atomic_set(&ctx->thread_ctx_switch_token, 1);
116         ctx->thread_ctx_switch_wait_token = 0;
117
118         if (is_kernel_ctx) {
119                 ctx->asid = HL_KERNEL_ASID_ID; /* KMD gets ASID 0 */
120         } else {
121                 ctx->asid = hl_asid_alloc(hdev);
122                 if (!ctx->asid) {
123                         dev_err(hdev->dev, "No free ASID, failed to create context\n");
124                         return -ENOMEM;
125                 }
126
127                 rc = hl_vm_ctx_init(ctx);
128                 if (rc) {
129                         dev_err(hdev->dev, "Failed to init mem ctx module\n");
130                         rc = -ENOMEM;
131                         goto mem_ctx_err;
132                 }
133         }
134
135         return 0;
136
137 mem_ctx_err:
138         if (ctx->asid != HL_KERNEL_ASID_ID)
139                 hl_asid_free(hdev, ctx->asid);
140
141         return rc;
142 }
143
144 void hl_ctx_get(struct hl_device *hdev, struct hl_ctx *ctx)
145 {
146         kref_get(&ctx->refcount);
147 }
148
149 int hl_ctx_put(struct hl_ctx *ctx)
150 {
151         return kref_put(&ctx->refcount, hl_ctx_do_release);
152 }
153
154 struct dma_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq)
155 {
156         struct hl_device *hdev = ctx->hdev;
157         struct dma_fence *fence;
158
159         spin_lock(&ctx->cs_lock);
160
161         if (seq >= ctx->cs_sequence) {
162                 dev_notice(hdev->dev,
163                         "Can't wait on seq %llu because current CS is at seq %llu\n",
164                         seq, ctx->cs_sequence);
165                 spin_unlock(&ctx->cs_lock);
166                 return ERR_PTR(-EINVAL);
167         }
168
169
170         if (seq + HL_MAX_PENDING_CS < ctx->cs_sequence) {
171                 dev_dbg(hdev->dev,
172                         "Can't wait on seq %llu because current CS is at seq %llu (Fence is gone)\n",
173                         seq, ctx->cs_sequence);
174                 spin_unlock(&ctx->cs_lock);
175                 return NULL;
176         }
177
178         fence = dma_fence_get(
179                         ctx->cs_pending[seq & (HL_MAX_PENDING_CS - 1)]);
180         spin_unlock(&ctx->cs_lock);
181
182         return fence;
183 }
184
185 /*
186  * hl_ctx_mgr_init - initialize the context manager
187  *
188  * @mgr: pointer to context manager structure
189  *
190  * This manager is an object inside the hpriv object of the user process.
191  * The function is called when a user process opens the FD.
192  */
193 void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr)
194 {
195         mutex_init(&mgr->ctx_lock);
196         idr_init(&mgr->ctx_handles);
197 }
198
199 /*
200  * hl_ctx_mgr_fini - finalize the context manager
201  *
202  * @hdev: pointer to device structure
203  * @mgr: pointer to context manager structure
204  *
205  * This function goes over all the contexts in the manager and frees them.
206  * It is called when a process closes the FD.
207  */
208 void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr)
209 {
210         struct hl_ctx *ctx;
211         struct idr *idp;
212         u32 id;
213
214         idp = &mgr->ctx_handles;
215
216         idr_for_each_entry(idp, ctx, id)
217                 hl_ctx_free(hdev, ctx);
218
219         idr_destroy(&mgr->ctx_handles);
220         mutex_destroy(&mgr->ctx_lock);
221 }