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_uoutp(p) container_of((p), struct nvkm_outp, object)
23 #include "outp.h"
24 #include "ior.h"
25 
26 #include <nvif/if0012.h>
27 
28 static int
29 nvkm_uoutp_mthd_load_detect(struct nvkm_outp *outp, void *argv, u32 argc)
30 {
31 	union nvif_outp_load_detect_args *args = argv;
32 	int ret;
33 
34 	if (argc != sizeof(args->v0) || args->v0.version != 0)
35 		return -ENOSYS;
36 
37 	ret = nvkm_outp_acquire(outp, NVKM_OUTP_PRIV, false);
38 	if (ret == 0) {
39 		if (outp->ior->func->sense) {
40 			ret = outp->ior->func->sense(outp->ior, args->v0.data);
41 			args->v0.load = ret < 0 ? 0 : ret;
42 		} else {
43 			ret = -EINVAL;
44 		}
45 		nvkm_outp_release(outp, NVKM_OUTP_PRIV);
46 	}
47 
48 	return ret;
49 }
50 
51 static int
52 nvkm_uoutp_mthd_noacquire(struct nvkm_outp *outp, u32 mthd, void *argv, u32 argc)
53 {
54 	switch (mthd) {
55 	case NVIF_OUTP_V0_LOAD_DETECT: return nvkm_uoutp_mthd_load_detect(outp, argv, argc);
56 	default:
57 		break;
58 	}
59 
60 	return 1;
61 }
62 
63 static int
64 nvkm_uoutp_mthd(struct nvkm_object *object, u32 mthd, void *argv, u32 argc)
65 {
66 	struct nvkm_outp *outp = nvkm_uoutp(object);
67 	struct nvkm_disp *disp = outp->disp;
68 	int ret;
69 
70 	mutex_lock(&disp->super.mutex);
71 
72 	ret = nvkm_uoutp_mthd_noacquire(outp, mthd, argv, argc);
73 	if (ret <= 0)
74 		goto done;
75 
76 done:
77 	mutex_unlock(&disp->super.mutex);
78 	return ret;
79 }
80 
81 static void *
82 nvkm_uoutp_dtor(struct nvkm_object *object)
83 {
84 	struct nvkm_outp *outp = nvkm_uoutp(object);
85 	struct nvkm_disp *disp = outp->disp;
86 
87 	spin_lock(&disp->client.lock);
88 	outp->object.func = NULL;
89 	spin_unlock(&disp->client.lock);
90 	return NULL;
91 }
92 
93 static const struct nvkm_object_func
94 nvkm_uoutp = {
95 	.dtor = nvkm_uoutp_dtor,
96 	.mthd = nvkm_uoutp_mthd,
97 };
98 
99 int
100 nvkm_uoutp_new(const struct nvkm_oclass *oclass, void *argv, u32 argc, struct nvkm_object **pobject)
101 {
102 	struct nvkm_disp *disp = nvkm_udisp(oclass->parent);
103 	struct nvkm_outp *outt, *outp = NULL;
104 	union nvif_outp_args *args = argv;
105 	int ret;
106 
107 	if (argc != sizeof(args->v0) || args->v0.version != 0)
108 		return -ENOSYS;
109 
110 	list_for_each_entry(outt, &disp->outps, head) {
111 		if (outt->index == args->v0.id) {
112 			outp = outt;
113 			break;
114 		}
115 	}
116 
117 	if (!outp)
118 		return -EINVAL;
119 
120 	ret = -EBUSY;
121 	spin_lock(&disp->client.lock);
122 	if (!outp->object.func) {
123 		nvkm_object_ctor(&nvkm_uoutp, oclass, &outp->object);
124 		*pobject = &outp->object;
125 		ret = 0;
126 	}
127 	spin_unlock(&disp->client.lock);
128 	return ret;
129 }
130