1 /*
2 * Copyright 2013 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 * Authors: Ben Skeggs
23 */
24
25 #include <core/os.h>
26 #include <nvif/unpack.h>
27 #include <nvif/class.h>
28 #include <nvif/event.h>
29
30 #include "priv.h"
31 #include "outp.h"
32 #include "conn.h"
33
34 int
nouveau_disp_vblank_ctor(struct nouveau_object * object,void * data,u32 size,struct nvkm_notify * notify)35 nouveau_disp_vblank_ctor(struct nouveau_object *object, void *data, u32 size,
36 struct nvkm_notify *notify)
37 {
38 struct nouveau_disp *disp =
39 container_of(notify->event, typeof(*disp), vblank);
40 union {
41 struct nvif_notify_head_req_v0 v0;
42 } *req = data;
43 int ret;
44
45 if (nvif_unpack(req->v0, 0, 0, false)) {
46 notify->size = sizeof(struct nvif_notify_head_rep_v0);
47 if (ret = -ENXIO, req->v0.head <= disp->vblank.index_nr) {
48 notify->types = 1;
49 notify->index = req->v0.head;
50 return 0;
51 }
52 }
53
54 return ret;
55 }
56
57 void
nouveau_disp_vblank(struct nouveau_disp * disp,int head)58 nouveau_disp_vblank(struct nouveau_disp *disp, int head)
59 {
60 struct nvif_notify_head_rep_v0 rep = {};
61 nvkm_event_send(&disp->vblank, 1, head, &rep, sizeof(rep));
62 }
63
64 static int
nouveau_disp_hpd_ctor(struct nouveau_object * object,void * data,u32 size,struct nvkm_notify * notify)65 nouveau_disp_hpd_ctor(struct nouveau_object *object, void *data, u32 size,
66 struct nvkm_notify *notify)
67 {
68 struct nouveau_disp *disp =
69 container_of(notify->event, typeof(*disp), hpd);
70 union {
71 struct nvif_notify_conn_req_v0 v0;
72 } *req = data;
73 struct nvkm_output *outp;
74 int ret;
75
76 if (nvif_unpack(req->v0, 0, 0, false)) {
77 notify->size = sizeof(struct nvif_notify_conn_rep_v0);
78 list_for_each_entry(outp, &disp->outp, head) {
79 if (ret = -ENXIO, outp->conn->index == req->v0.conn) {
80 if (ret = -ENODEV, outp->conn->hpd.event) {
81 notify->types = req->v0.mask;
82 notify->index = req->v0.conn;
83 ret = 0;
84 }
85 break;
86 }
87 }
88 }
89
90 return ret;
91 }
92
93 static const struct nvkm_event_func
94 nouveau_disp_hpd_func = {
95 .ctor = nouveau_disp_hpd_ctor
96 };
97
98 int
nouveau_disp_ntfy(struct nouveau_object * object,u32 type,struct nvkm_event ** event)99 nouveau_disp_ntfy(struct nouveau_object *object, u32 type,
100 struct nvkm_event **event)
101 {
102 struct nouveau_disp *disp = (void *)object->engine;
103 switch (type) {
104 case NV04_DISP_NTFY_VBLANK:
105 *event = &disp->vblank;
106 return 0;
107 case NV04_DISP_NTFY_CONN:
108 *event = &disp->hpd;
109 return 0;
110 default:
111 break;
112 }
113 return -EINVAL;
114 }
115
116 int
_nouveau_disp_fini(struct nouveau_object * object,bool suspend)117 _nouveau_disp_fini(struct nouveau_object *object, bool suspend)
118 {
119 struct nouveau_disp *disp = (void *)object;
120 struct nvkm_output *outp;
121 int ret;
122
123 list_for_each_entry(outp, &disp->outp, head) {
124 ret = nv_ofuncs(outp)->fini(nv_object(outp), suspend);
125 if (ret && suspend)
126 goto fail_outp;
127 }
128
129 return nouveau_engine_fini(&disp->base, suspend);
130
131 fail_outp:
132 list_for_each_entry_continue_reverse(outp, &disp->outp, head) {
133 nv_ofuncs(outp)->init(nv_object(outp));
134 }
135
136 return ret;
137 }
138
139 int
_nouveau_disp_init(struct nouveau_object * object)140 _nouveau_disp_init(struct nouveau_object *object)
141 {
142 struct nouveau_disp *disp = (void *)object;
143 struct nvkm_output *outp;
144 int ret;
145
146 ret = nouveau_engine_init(&disp->base);
147 if (ret)
148 return ret;
149
150 list_for_each_entry(outp, &disp->outp, head) {
151 ret = nv_ofuncs(outp)->init(nv_object(outp));
152 if (ret)
153 goto fail_outp;
154 }
155
156 return ret;
157
158 fail_outp:
159 list_for_each_entry_continue_reverse(outp, &disp->outp, head) {
160 nv_ofuncs(outp)->fini(nv_object(outp), false);
161 }
162
163 return ret;
164 }
165
166 void
_nouveau_disp_dtor(struct nouveau_object * object)167 _nouveau_disp_dtor(struct nouveau_object *object)
168 {
169 struct nouveau_disp *disp = (void *)object;
170 struct nvkm_output *outp, *outt;
171
172 nvkm_event_fini(&disp->vblank);
173 nvkm_event_fini(&disp->hpd);
174
175 if (disp->outp.next) {
176 list_for_each_entry_safe(outp, outt, &disp->outp, head) {
177 nouveau_object_ref(NULL, (struct nouveau_object **)&outp);
178 }
179 }
180
181 nouveau_engine_destroy(&disp->base);
182 }
183
184 int
nouveau_disp_create_(struct nouveau_object * parent,struct nouveau_object * engine,struct nouveau_oclass * oclass,int heads,const char * intname,const char * extname,int length,void ** pobject)185 nouveau_disp_create_(struct nouveau_object *parent,
186 struct nouveau_object *engine,
187 struct nouveau_oclass *oclass, int heads,
188 const char *intname, const char *extname,
189 int length, void **pobject)
190 {
191 struct nouveau_disp_impl *impl = (void *)oclass;
192 struct nouveau_bios *bios = nouveau_bios(parent);
193 struct nouveau_disp *disp;
194 struct nouveau_oclass **sclass;
195 struct nouveau_object *object;
196 struct dcb_output dcbE;
197 u8 hpd = 0, ver, hdr;
198 u32 data;
199 int ret, i;
200
201 ret = nouveau_engine_create_(parent, engine, oclass, true,
202 intname, extname, length, pobject);
203 disp = *pobject;
204 if (ret)
205 return ret;
206
207 INIT_LIST_HEAD(&disp->outp);
208
209 /* create output objects for each display path in the vbios */
210 i = -1;
211 while ((data = dcb_outp_parse(bios, ++i, &ver, &hdr, &dcbE))) {
212 if (dcbE.type == DCB_OUTPUT_UNUSED)
213 continue;
214 if (dcbE.type == DCB_OUTPUT_EOL)
215 break;
216 data = dcbE.location << 4 | dcbE.type;
217
218 oclass = nvkm_output_oclass;
219 sclass = impl->outp;
220 while (sclass && sclass[0]) {
221 if (sclass[0]->handle == data) {
222 oclass = sclass[0];
223 break;
224 }
225 sclass++;
226 }
227
228 nouveau_object_ctor(*pobject, *pobject, oclass,
229 &dcbE, i, &object);
230 hpd = max(hpd, (u8)(dcbE.connector + 1));
231 }
232
233 ret = nvkm_event_init(&nouveau_disp_hpd_func, 3, hpd, &disp->hpd);
234 if (ret)
235 return ret;
236
237 ret = nvkm_event_init(impl->vblank, 1, heads, &disp->vblank);
238 if (ret)
239 return ret;
240
241 return 0;
242 }
243