5c75c64f227097d5f03c848a2022675783aa40b3
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / nvkm / core / parent.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 #include <core/parent.h>
25 #include <core/client.h>
26 #include <core/engine.h>
27
28 int
29 nvkm_parent_sclass(struct nvkm_object *parent, u16 handle,
30                    struct nvkm_object **pengine,
31                    struct nvkm_oclass **poclass)
32 {
33         struct nvkm_oclass *sclass, *oclass;
34         struct nvkm_engine *engine;
35         u64 mask;
36         int i;
37
38         sclass = nv_parent(parent)->sclass;
39         while ((oclass = sclass++) && oclass->ofuncs) {
40                 if (oclass->handle == handle) {
41                         *pengine = &parent->engine->subdev.object;
42                         *poclass = oclass;
43                         return 0;
44                 }
45         }
46
47         mask = nv_parent(parent)->engine;
48         while (i = __ffs64(mask), mask) {
49                 engine = nvkm_engine(parent, i);
50                 if (engine) {
51                         oclass = engine->sclass;
52                         while (oclass->ofuncs) {
53                                 if (oclass->handle == handle) {
54                                         *pengine = nv_object(engine);
55                                         *poclass = oclass;
56                                         return 0;
57                                 }
58                                 oclass++;
59                         }
60                 }
61
62                 mask &= ~(1ULL << i);
63         }
64
65         return -EINVAL;
66 }
67
68 int
69 nvkm_parent_lclass(struct nvkm_object *parent, u32 *lclass, int size)
70 {
71         struct nvkm_oclass *sclass, *oclass;
72         struct nvkm_engine *engine;
73         int nr = -1, i;
74         u64 mask;
75
76         sclass = nv_parent(parent)->sclass;
77         while ((oclass = sclass++) && oclass->ofuncs) {
78                 if (++nr < size)
79                         lclass[nr] = oclass->handle;
80         }
81
82         mask = nv_parent(parent)->engine;
83         while (i = __ffs64(mask), mask) {
84                 engine = nvkm_engine(parent, i);
85                 if (engine && (oclass = engine->sclass)) {
86                         while (oclass->ofuncs) {
87                                 if (++nr < size)
88                                         lclass[nr] = oclass->handle;
89                                 oclass++;
90                         }
91                 }
92
93                 mask &= ~(1ULL << i);
94         }
95
96         return nr + 1;
97 }
98
99 int
100 nvkm_parent_create_(struct nvkm_object *parent, struct nvkm_object *engine,
101                     struct nvkm_oclass *oclass, u32 pclass,
102                     struct nvkm_oclass *sclass, u64 engcls,
103                     int size, void **pobject)
104 {
105         struct nvkm_parent *object;
106         int ret;
107
108         ret = nvkm_object_create_(parent, engine, oclass, pclass |
109                                   NV_PARENT_CLASS, size, pobject);
110         object = *pobject;
111         if (ret)
112                 return ret;
113
114         object->sclass = sclass;
115         object->engine = engcls;
116         return 0;
117 }
118
119 void
120 nvkm_parent_destroy(struct nvkm_parent *parent)
121 {
122         nvkm_object_destroy(&parent->object);
123 }
124
125
126 void
127 _nvkm_parent_dtor(struct nvkm_object *object)
128 {
129         nvkm_parent_destroy(nv_parent(object));
130 }