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 #include "priv.h"
23 #include "conn.h"
24 #include "head.h"
25 #include "outp.h"
26 
27 #include <nvif/class.h>
28 #include <nvif/if0010.h>
29 
30 static int
31 nvkm_udisp_sclass(struct nvkm_object *object, int index, struct nvkm_oclass *sclass)
32 {
33 	struct nvkm_disp *disp = nvkm_udisp(object);
34 
35 	if (index-- == 0) {
36 		sclass->base = (struct nvkm_sclass) { 0, 0, NVIF_CLASS_CONN };
37 		sclass->ctor = nvkm_uconn_new;
38 		return 0;
39 	}
40 
41 	if (index-- == 0) {
42 		sclass->base = (struct nvkm_sclass) { 0, 0, NVIF_CLASS_OUTP };
43 		sclass->ctor = nvkm_uoutp_new;
44 		return 0;
45 	}
46 
47 	if (index-- == 0) {
48 		sclass->base = (struct nvkm_sclass) { 0, 0, NVIF_CLASS_HEAD };
49 		sclass->ctor = nvkm_uhead_new;
50 		return 0;
51 	}
52 
53 	if (disp->func->user[index].ctor) {
54 		sclass->base = disp->func->user[index].base;
55 		sclass->ctor = disp->func->user[index].ctor;
56 		return 0;
57 	}
58 
59 	return -EINVAL;
60 }
61 
62 static void *
63 nvkm_udisp_dtor(struct nvkm_object *object)
64 {
65 	struct nvkm_disp *disp = nvkm_udisp(object);
66 
67 	spin_lock(&disp->client.lock);
68 	if (object == &disp->client.object)
69 		disp->client.object.func = NULL;
70 	spin_unlock(&disp->client.lock);
71 	return NULL;
72 }
73 
74 static const struct nvkm_object_func
75 nvkm_udisp = {
76 	.dtor = nvkm_udisp_dtor,
77 	.sclass = nvkm_udisp_sclass,
78 };
79 
80 int
81 nvkm_udisp_new(const struct nvkm_oclass *oclass, void *argv, u32 argc, struct nvkm_object **pobject)
82 {
83 	struct nvkm_disp *disp = nvkm_disp(oclass->engine);
84 	struct nvkm_conn *conn;
85 	struct nvkm_outp *outp;
86 	struct nvkm_head *head;
87 	union nvif_disp_args *args = argv;
88 
89 	if (argc != sizeof(args->v0) || args->v0.version != 0)
90 		return -ENOSYS;
91 
92 	spin_lock(&disp->client.lock);
93 	if (disp->client.object.func) {
94 		spin_unlock(&disp->client.lock);
95 		return -EBUSY;
96 	}
97 	nvkm_object_ctor(&nvkm_udisp, oclass, &disp->client.object);
98 	*pobject = &disp->client.object;
99 	spin_unlock(&disp->client.lock);
100 
101 	args->v0.conn_mask = 0;
102 	list_for_each_entry(conn, &disp->conns, head)
103 		args->v0.conn_mask |= BIT(conn->index);
104 
105 	args->v0.outp_mask = 0;
106 	list_for_each_entry(outp, &disp->outps, head)
107 		args->v0.outp_mask |= BIT(outp->index);
108 
109 	args->v0.head_mask = 0;
110 	list_for_each_entry(head, &disp->heads, head)
111 		args->v0.head_mask |= BIT(head->id);
112 
113 	return 0;
114 }
115