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