xref: /openbmc/linux/drivers/gpu/drm/nouveau/nvkm/engine/fifo/uchan.c (revision 6de125383a5cce5f0d9235a6d3a9ae83dc5d299e)
1 /*
2  * Copyright 2021 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 #define nvkm_uchan(p) container_of((p), struct nvkm_uchan, object)
23 #include "cgrp.h"
24 #include "chan.h"
25 
26 #include <core/oproxy.h>
27 
28 #include <nvif/if0020.h>
29 
30 #include "gk104.h"
31 
32 struct nvkm_uchan {
33 	struct nvkm_object object;
34 	struct nvkm_chan *chan;
35 };
36 
37 static int
38 nvkm_uchan_uevent(struct nvkm_object *object, void *argv, u32 argc, struct nvkm_uevent *uevent)
39 {
40 	struct nvkm_chan *chan = nvkm_uchan(object)->chan;
41 	union nvif_chan_event_args *args = argv;
42 
43 	if (!uevent)
44 		return 0;
45 	if (argc != sizeof(args->v0) || args->v0.version != 0)
46 		return -ENOSYS;
47 
48 	switch (args->v0.type) {
49 	case NVIF_CHAN_EVENT_V0_NON_STALL_INTR:
50 	case NVIF_CHAN_EVENT_V0_KILLED:
51 		return chan->object.func->uevent(&chan->object, argv, argc, uevent);
52 	default:
53 		break;
54 	}
55 
56 	return -ENOSYS;
57 }
58 
59 struct nvkm_uobj {
60 	struct nvkm_oproxy oproxy;
61 	struct nvkm_chan *chan;
62 };
63 
64 static const struct nvkm_oproxy_func
65 nvkm_uchan_object = {
66 };
67 
68 static int
69 nvkm_uchan_object_new(const struct nvkm_oclass *oclass, void *argv, u32 argc,
70 		      struct nvkm_object **pobject)
71 {
72 	struct nvkm_chan *chan = nvkm_uchan(oclass->parent)->chan;
73 	struct nvkm_uobj *uobj;
74 	struct nvkm_oclass _oclass;
75 
76 	if (!(uobj = kzalloc(sizeof(*uobj), GFP_KERNEL)))
77 		return -ENOMEM;
78 
79 	nvkm_oproxy_ctor(&nvkm_uchan_object, oclass, &uobj->oproxy);
80 	uobj->chan = chan;
81 	*pobject = &uobj->oproxy.base;
82 
83 	_oclass = *oclass;
84 	_oclass.parent = &chan->object;
85 	return nvkm_fifo_chan_child_new(&_oclass, argv, argc, &uobj->oproxy.object);
86 }
87 
88 static int
89 nvkm_uchan_sclass(struct nvkm_object *object, int index, struct nvkm_oclass *oclass)
90 {
91 	struct nvkm_chan *chan = nvkm_uchan(object)->chan;
92 	int ret;
93 
94 	ret = chan->object.func->sclass(&chan->object, index, oclass);
95 	if (ret)
96 		return ret;
97 
98 	oclass->ctor = nvkm_uchan_object_new;
99 	return 0;
100 }
101 
102 static int
103 nvkm_uchan_map(struct nvkm_object *object, void *argv, u32 argc,
104 	       enum nvkm_object_map *type, u64 *addr, u64 *size)
105 {
106 	struct nvkm_chan *chan = nvkm_uchan(object)->chan;
107 
108 	return chan->object.func->map(&chan->object, argv, argc, type, addr, size);
109 }
110 
111 static int
112 nvkm_uchan_fini(struct nvkm_object *object, bool suspend)
113 {
114 	struct nvkm_chan *chan = nvkm_uchan(object)->chan;
115 	int ret;
116 
117 	ret = chan->object.func->fini(&chan->object, suspend);
118 	if (ret && suspend)
119 		return ret;
120 
121 	return 0;
122 }
123 
124 static int
125 nvkm_uchan_init(struct nvkm_object *object)
126 {
127 	struct nvkm_chan *chan = nvkm_uchan(object)->chan;
128 
129 	return chan->object.func->init(&chan->object);
130 }
131 
132 static void *
133 nvkm_uchan_dtor(struct nvkm_object *object)
134 {
135 	struct nvkm_uchan *uchan = nvkm_uchan(object);
136 
137 	nvkm_chan_del(&uchan->chan);
138 	return uchan;
139 }
140 
141 static const struct nvkm_object_func
142 nvkm_uchan = {
143 	.dtor = nvkm_uchan_dtor,
144 	.init = nvkm_uchan_init,
145 	.fini = nvkm_uchan_fini,
146 	.map = nvkm_uchan_map,
147 	.sclass = nvkm_uchan_sclass,
148 	.uevent = nvkm_uchan_uevent,
149 };
150 
151 int
152 nvkm_uchan_new(struct nvkm_fifo *fifo, struct nvkm_cgrp *cgrp, const struct nvkm_oclass *oclass,
153 	       void *argv, u32 argc, struct nvkm_object **pobject)
154 {
155 	struct nvkm_object *object = NULL;
156 	struct nvkm_uchan *uchan;
157 	int ret;
158 
159 	if (!(uchan = kzalloc(sizeof(*uchan), GFP_KERNEL)))
160 		return -ENOMEM;
161 
162 	nvkm_object_ctor(&nvkm_uchan, oclass, &uchan->object);
163 	*pobject = &uchan->object;
164 
165 	if (fifo->func->chan.ctor)
166 		ret = fifo->func->chan.ctor(gk104_fifo(fifo), oclass, argv, argc, &object);
167 	else
168 		ret = fifo->func->chan.oclass->ctor(fifo, oclass, argv, argc, &object);
169 	if (!object)
170 		return ret;
171 
172 	uchan->chan = container_of(object, typeof(*uchan->chan), object);
173 	return ret;
174 }
175