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_ucgrp(p) container_of((p), struct nvkm_ucgrp, object)
23 #include "priv.h"
24 #include "cgrp.h"
25 #include "runl.h"
26 
27 #include <subdev/mmu.h>
28 
29 #include <nvif/if0021.h>
30 
31 struct nvkm_ucgrp {
32 	struct nvkm_object object;
33 	struct nvkm_cgrp *cgrp;
34 };
35 
36 static int
37 nvkm_ucgrp_chan_new(const struct nvkm_oclass *oclass, void *argv, u32 argc,
38 		    struct nvkm_object **pobject)
39 {
40 	struct nvkm_cgrp *cgrp = nvkm_ucgrp(oclass->parent)->cgrp;
41 
42 	return nvkm_uchan_new(cgrp->runl->fifo, cgrp, oclass, argv, argc, pobject);
43 }
44 
45 static int
46 nvkm_ucgrp_sclass(struct nvkm_object *object, int index, struct nvkm_oclass *oclass)
47 {
48 	struct nvkm_cgrp *cgrp = nvkm_ucgrp(object)->cgrp;
49 	struct nvkm_fifo *fifo = cgrp->runl->fifo;
50 	const struct nvkm_fifo_func_chan *chan = &fifo->func->chan;
51 	int c = 0;
52 
53 	/* *_CHANNEL_GPFIFO_* */
54 	if (chan->user.oclass) {
55 		if (c++ == index) {
56 			oclass->base = chan->user;
57 			oclass->ctor = nvkm_ucgrp_chan_new;
58 			return 0;
59 		}
60 	}
61 
62 	return -EINVAL;
63 }
64 
65 static void *
66 nvkm_ucgrp_dtor(struct nvkm_object *object)
67 {
68 	struct nvkm_ucgrp *ucgrp = nvkm_ucgrp(object);
69 
70 	nvkm_cgrp_unref(&ucgrp->cgrp);
71 	return ucgrp;
72 }
73 
74 static const struct nvkm_object_func
75 nvkm_ucgrp = {
76 	.dtor = nvkm_ucgrp_dtor,
77 	.sclass = nvkm_ucgrp_sclass,
78 };
79 
80 int
81 nvkm_ucgrp_new(struct nvkm_fifo *fifo, const struct nvkm_oclass *oclass, void *argv, u32 argc,
82 	       struct nvkm_object **pobject)
83 {
84 	union nvif_cgrp_args *args = argv;
85 	struct nvkm_runl *runl;
86 	struct nvkm_vmm *vmm;
87 	struct nvkm_ucgrp *ucgrp;
88 	int ret;
89 
90 	if (argc < sizeof(args->v0) || args->v0.version != 0)
91 		return -ENOSYS;
92 	argc -= sizeof(args->v0);
93 
94 	if (args->v0.namelen != argc)
95 		return -EINVAL;
96 
97 	/* Lookup objects referenced in args. */
98 	runl = nvkm_runl_get(fifo, args->v0.runlist, 0);
99 	if (!runl)
100 		return -EINVAL;
101 
102 	vmm = nvkm_uvmm_search(oclass->client, args->v0.vmm);
103 	if (IS_ERR(vmm))
104 		return PTR_ERR(vmm);
105 
106 	/* Allocate channel group. */
107 	if (!(ucgrp = kzalloc(sizeof(*ucgrp), GFP_KERNEL))) {
108 		ret = -ENOMEM;
109 		goto done;
110 	}
111 
112 	nvkm_object_ctor(&nvkm_ucgrp, oclass, &ucgrp->object);
113 	*pobject = &ucgrp->object;
114 
115 	ret = nvkm_cgrp_new(runl, args->v0.name, vmm, true, &ucgrp->cgrp);
116 	if (ret)
117 		goto done;
118 
119 	/* Return channel group info to caller. */
120 	args->v0.cgid = ucgrp->cgrp->id;
121 
122 done:
123 	nvkm_vmm_unref(&vmm);
124 	return ret;
125 }
126