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