drm/nouveau/core: have fifo store a unique context identifier at attach time
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / gpu / drm / nouveau / core / core / engctx.c
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24
25 #include <core/object.h>
26 #include <core/namedb.h>
27 #include <core/handle.h>
28 #include <core/client.h>
29 #include <core/engctx.h>
30
31 #include <subdev/vm.h>
32
33 static inline int
34 nouveau_engctx_exists(struct nouveau_object *parent,
35                       struct nouveau_engine *engine, void **pobject)
36 {
37         struct nouveau_engctx *engctx;
38         struct nouveau_object *parctx;
39
40         list_for_each_entry(engctx, &engine->contexts, head) {
41                 parctx = nv_pclass(nv_object(engctx), NV_PARENT_CLASS);
42                 if (parctx == parent) {
43                         atomic_inc(&nv_object(engctx)->refcount);
44                         *pobject = engctx;
45                         return 1;
46                 }
47         }
48
49         return 0;
50 }
51
52 int
53 nouveau_engctx_create_(struct nouveau_object *parent,
54                        struct nouveau_object *engobj,
55                        struct nouveau_oclass *oclass,
56                        struct nouveau_object *pargpu,
57                        u32 size, u32 align, u32 flags,
58                        int length, void **pobject)
59 {
60         struct nouveau_client *client = nouveau_client(parent);
61         struct nouveau_engine *engine = nv_engine(engobj);
62         struct nouveau_subdev *subdev = nv_subdev(engine);
63         struct nouveau_object *engctx;
64         unsigned long save;
65         int ret;
66
67         /* check if this engine already has a context for the parent object,
68          * and reference it instead of creating a new one
69          */
70         spin_lock_irqsave(&engine->lock, save);
71         ret = nouveau_engctx_exists(parent, engine, pobject);
72         spin_unlock_irqrestore(&engine->lock, save);
73         if (ret)
74                 return ret;
75
76         /* create the new context, supports creating both raw objects and
77          * objects backed by instance memory
78          */
79         if (size) {
80                 ret = nouveau_gpuobj_create_(parent, engobj, oclass,
81                                              NV_ENGCTX_CLASS,
82                                              pargpu, size, align, flags,
83                                              length, pobject);
84         } else {
85                 ret = nouveau_object_create_(parent, engobj, oclass,
86                                              NV_ENGCTX_CLASS, length, pobject);
87         }
88
89         engctx = *pobject;
90         if (ret)
91                 return ret;
92
93         /* must take the lock again and re-check a context doesn't already
94          * exist (in case of a race) - the lock had to be dropped before as
95          * it's not possible to allocate the object with it held.
96          */
97         spin_lock_irqsave(&engine->lock, save);
98         ret = nouveau_engctx_exists(parent, engine, pobject);
99         if (ret) {
100                 spin_unlock_irqrestore(&engine->lock, save);
101                 nouveau_object_ref(NULL, &engctx);
102                 return ret;
103         }
104
105         if (client->vm)
106                 atomic_inc(&client->vm->engref[nv_engidx(engobj)]);
107         list_add(&nv_engctx(engctx)->head, &engine->contexts);
108         nv_engctx(engctx)->addr = ~0ULL;
109         spin_unlock_irqrestore(&engine->lock, save);
110         return 0;
111 }
112
113 void
114 nouveau_engctx_destroy(struct nouveau_engctx *engctx)
115 {
116         struct nouveau_object *engobj = nv_object(engctx)->engine;
117         struct nouveau_engine *engine = nv_engine(engobj);
118         struct nouveau_client *client = nouveau_client(engctx);
119         unsigned long save;
120
121         nouveau_gpuobj_unmap(&engctx->vma);
122         spin_lock_irqsave(&engine->lock, save);
123         list_del(&engctx->head);
124         spin_unlock_irqrestore(&engine->lock, save);
125
126         if (client->vm)
127                 atomic_dec(&client->vm->engref[nv_engidx(engobj)]);
128
129         if (engctx->base.size)
130                 nouveau_gpuobj_destroy(&engctx->base);
131         else
132                 nouveau_object_destroy(&engctx->base.base);
133 }
134
135 int
136 nouveau_engctx_init(struct nouveau_engctx *engctx)
137 {
138         struct nouveau_object *object = nv_object(engctx);
139         struct nouveau_subdev *subdev = nv_subdev(object->engine);
140         struct nouveau_object *parent;
141         struct nouveau_subdev *pardev;
142         int ret;
143
144         ret = nouveau_gpuobj_init(&engctx->base);
145         if (ret)
146                 return ret;
147
148         parent = nv_pclass(object->parent, NV_PARENT_CLASS);
149         pardev = nv_subdev(parent->engine);
150         if (nv_parent(parent)->context_attach) {
151                 mutex_lock(&pardev->mutex);
152                 ret = nv_parent(parent)->context_attach(parent, object);
153                 mutex_unlock(&pardev->mutex);
154         }
155
156         if (ret) {
157                 nv_error(parent, "failed to attach %s context, %d\n",
158                          subdev->name, ret);
159                 return ret;
160         }
161
162         nv_debug(parent, "attached %s context\n", subdev->name);
163         return 0;
164 }
165
166 int
167 nouveau_engctx_fini(struct nouveau_engctx *engctx, bool suspend)
168 {
169         struct nouveau_object *object = nv_object(engctx);
170         struct nouveau_subdev *subdev = nv_subdev(object->engine);
171         struct nouveau_object *parent;
172         struct nouveau_subdev *pardev;
173         int ret = 0;
174
175         parent = nv_pclass(object->parent, NV_PARENT_CLASS);
176         pardev = nv_subdev(parent->engine);
177         if (nv_parent(parent)->context_detach) {
178                 mutex_lock(&pardev->mutex);
179                 ret = nv_parent(parent)->context_detach(parent, suspend, object);
180                 mutex_unlock(&pardev->mutex);
181         }
182
183         if (ret) {
184                 nv_error(parent, "failed to detach %s context, %d\n",
185                          subdev->name, ret);
186                 return ret;
187         }
188
189         nv_debug(parent, "detached %s context\n", subdev->name);
190         return nouveau_gpuobj_fini(&engctx->base, suspend);
191 }
192
193 void
194 _nouveau_engctx_dtor(struct nouveau_object *object)
195 {
196         nouveau_engctx_destroy(nv_engctx(object));
197 }
198
199 int
200 _nouveau_engctx_init(struct nouveau_object *object)
201 {
202         return nouveau_engctx_init(nv_engctx(object));
203 }
204
205
206 int
207 _nouveau_engctx_fini(struct nouveau_object *object, bool suspend)
208 {
209         return nouveau_engctx_fini(nv_engctx(object), suspend);
210 }
211
212 struct nouveau_object *
213 nouveau_engctx_lookup(struct nouveau_engine *engine, u64 addr)
214 {
215         struct nouveau_engctx *engctx;
216
217         list_for_each_entry(engctx, &engine->contexts, head) {
218                 if (engctx->base.size &&
219                     nv_gpuobj(engctx)->addr == addr)
220                         return nv_object(engctx);
221         }
222
223         return NULL;
224 }
225
226 struct nouveau_handle *
227 nouveau_engctx_lookup_class(struct nouveau_engine *engine, u64 addr, u16 oclass)
228 {
229         struct nouveau_object *engctx = nouveau_engctx_lookup(engine, addr);
230         struct nouveau_namedb *namedb;
231
232         if (engctx && (namedb = (void *)nv_pclass(engctx, NV_NAMEDB_CLASS)))
233                 return nouveau_namedb_get_class(namedb, oclass);
234
235         return NULL;
236 }
237
238 struct nouveau_handle *
239 nouveau_engctx_lookup_vinst(struct nouveau_engine *engine, u64 addr, u64 vinst)
240 {
241         struct nouveau_object *engctx = nouveau_engctx_lookup(engine, addr);
242         struct nouveau_namedb *namedb;
243
244         if (engctx && (namedb = (void *)nv_pclass(engctx, NV_NAMEDB_CLASS)))
245                 return nouveau_namedb_get_vinst(namedb, vinst);
246
247         return NULL;
248 }
249
250 struct nouveau_handle *
251 nouveau_engctx_lookup_cinst(struct nouveau_engine *engine, u64 addr, u32 cinst)
252 {
253         struct nouveau_object *engctx = nouveau_engctx_lookup(engine, addr);
254         struct nouveau_namedb *namedb;
255
256         if (engctx && (namedb = (void *)nv_pclass(engctx, NV_NAMEDB_CLASS)))
257                 return nouveau_namedb_get_cinst(namedb, cinst);
258
259         return NULL;
260 }
261
262 void
263 nouveau_engctx_handle_put(struct nouveau_handle *handle)
264 {
265         if (handle)
266                 nouveau_namedb_put(handle);
267 }