drmfb.c revision 1.15 1 /* $NetBSD: drmfb.c,v 1.15 2022/09/01 12:01:36 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * drmfb: wsdisplay support, via genfb, for any drm device. Use this
34 * if you're too lazy to write a hardware-accelerated framebuffer using
35 * wsdisplay directly.
36 *
37 * This doesn't actually do anything interesting -- just hooks up
38 * drmkms crap and genfb crap.
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: drmfb.c,v 1.15 2022/09/01 12:01:36 riastradh Exp $");
43
44 #ifdef _KERNEL_OPT
45 #include "vga.h"
46 #endif
47
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/bus.h>
51 #include <sys/device.h>
52 #include <sys/kauth.h>
53
54 #if NVGA > 0
55 /*
56 * XXX All we really need is vga_is_console from vgavar.h, but the
57 * header files are missing their own dependencies, so we need to
58 * explicitly drag in the other crap.
59 */
60 #include <dev/ic/mc6845reg.h>
61 #include <dev/ic/pcdisplayvar.h>
62 #include <dev/ic/vgareg.h>
63 #include <dev/ic/vgavar.h>
64 #endif
65
66 #include <dev/wsfb/genfbvar.h>
67
68 #include <drm/drm_device.h>
69 #include <drm/drm_fb_helper.h>
70 #include <drm/drmfb.h>
71
72 static int drmfb_genfb_ioctl(void *, void *, unsigned long, void *, int,
73 struct lwp *);
74 static paddr_t drmfb_genfb_mmap(void *, void *, off_t, int);
75 static int drmfb_genfb_enable_polling(void *);
76 static int drmfb_genfb_disable_polling(void *);
77 static bool drmfb_genfb_setmode(struct genfb_softc *, int);
78
79 static const struct genfb_mode_callback drmfb_genfb_mode_callback = {
80 .gmc_setmode = drmfb_genfb_setmode,
81 };
82
83 int
84 drmfb_attach(struct drmfb_softc *sc, const struct drmfb_attach_args *da)
85 {
86 const struct drm_fb_helper_surface_size *const sizes = da->da_fb_sizes;
87 struct drm_connector_list_iter conn_iter;
88 struct drm_connector *connector;
89 const prop_dictionary_t dict = device_properties(da->da_dev);
90 const device_t parent = device_parent(da->da_dev);
91 const prop_dictionary_t pdict = device_properties(parent);
92 #if NVGA > 0
93 struct drm_device *const dev = da->da_fb_helper->dev;
94 #endif
95 static const struct genfb_ops zero_genfb_ops;
96 struct genfb_ops genfb_ops = zero_genfb_ops;
97 enum { CONS_VGA, CONS_GENFB, CONS_NONE } what_was_cons;
98 bool is_console;
99 int error;
100
101 /* genfb requires this. */
102 KASSERTMSG((void *)&sc->sc_genfb == device_private(da->da_dev),
103 "drmfb_softc must be first member of device softc");
104
105 sc->sc_da = *da;
106
107 prop_dictionary_set_uint32(dict, "width", sizes->surface_width);
108 prop_dictionary_set_uint32(dict, "height", sizes->surface_height);
109 prop_dictionary_set_uint8(dict, "depth", sizes->surface_bpp);
110 prop_dictionary_set_uint16(dict, "linebytes", da->da_fb_linebytes);
111 prop_dictionary_set_uint32(dict, "address", 0); /* XXX >32-bit */
112 CTASSERT(sizeof(uintptr_t) <= sizeof(uint64_t));
113 prop_dictionary_set_uint64(dict, "virtual_address",
114 (uint64_t)(uintptr_t)da->da_fb_vaddr);
115
116 prop_dictionary_set_uint64(dict, "mode_callback",
117 (uint64_t)(uintptr_t)&drmfb_genfb_mode_callback);
118
119 if (prop_dictionary_get_bool(pdict, "is_console", &is_console)) {
120 what_was_cons = CONS_NONE;
121 prop_dictionary_set_bool(dict, "is_console", is_console);
122 } else {
123 /* XXX Whattakludge! */
124 #if NVGA > 0
125 if ((da->da_params->dp_is_vga_console != NULL) &&
126 (*da->da_params->dp_is_vga_console)(dev)) {
127 what_was_cons = CONS_VGA;
128 prop_dictionary_set_bool(dict, "is_console", true);
129 vga_cndetach();
130 if (da->da_params->dp_disable_vga)
131 (*da->da_params->dp_disable_vga)(dev);
132 } else
133 #endif
134 if (genfb_is_console() && genfb_is_enabled()) {
135 what_was_cons = CONS_GENFB;
136 prop_dictionary_set_bool(dict, "is_console", true);
137 } else {
138 what_was_cons = CONS_NONE;
139 prop_dictionary_set_bool(dict, "is_console", false);
140 }
141 }
142
143 /* Make the first EDID we find available to wsfb */
144 drm_connector_list_iter_begin(da->da_fb_helper->dev, &conn_iter);
145 drm_client_for_each_connector_iter(connector, &conn_iter) {
146 struct drm_property_blob *edid = connector->edid_blob_ptr;
147 if (edid && edid->length) {
148 prop_dictionary_set_data(dict, "EDID", edid->data,
149 edid->length);
150 break;
151 }
152 }
153 drm_connector_list_iter_end(&conn_iter);
154
155 sc->sc_genfb.sc_dev = sc->sc_da.da_dev;
156 genfb_init(&sc->sc_genfb);
157 genfb_ops.genfb_ioctl = drmfb_genfb_ioctl;
158 genfb_ops.genfb_mmap = drmfb_genfb_mmap;
159 genfb_ops.genfb_enable_polling = drmfb_genfb_enable_polling;
160 genfb_ops.genfb_disable_polling = drmfb_genfb_disable_polling;
161
162 KERNEL_LOCK(1, NULL);
163 error = genfb_attach(&sc->sc_genfb, &genfb_ops);
164 KERNEL_UNLOCK_ONE(NULL);
165 if (error) {
166 aprint_error_dev(sc->sc_da.da_dev,
167 "failed to attach genfb: %d\n", error);
168 goto fail0;
169 }
170
171 /* Success! */
172 return 0;
173
174 fail0: KASSERT(error);
175 /* XXX Restore console... */
176 switch (what_was_cons) {
177 case CONS_VGA:
178 break;
179 case CONS_GENFB:
180 break;
181 case CONS_NONE:
182 break;
183 default:
184 break;
185 }
186 return error;
187 }
188
189 int
190 drmfb_detach(struct drmfb_softc *sc, int flags)
191 {
192
193 /* XXX genfb detach? */
194 return 0;
195 }
196
197 static int
198 drmfb_genfb_ioctl(void *v, void *vs, unsigned long cmd, void *data, int flag,
199 struct lwp *l)
200 {
201 struct genfb_softc *const genfb = v;
202 struct drmfb_softc *const sc = container_of(genfb, struct drmfb_softc,
203 sc_genfb);
204 int error;
205
206 if (sc->sc_da.da_params->dp_ioctl) {
207 error = (*sc->sc_da.da_params->dp_ioctl)(sc, cmd, data, flag,
208 l);
209 if (error != EPASSTHROUGH)
210 return error;
211 }
212
213 switch (cmd) {
214 /*
215 * Screen blanking ioctls. Not to be confused with backlight
216 * (can be disabled while stuff is still drawn on the screen),
217 * brightness, or contrast (which we don't support). Backlight
218 * and brightness are done through WSDISPLAYIO_{GET,SET}PARAM.
219 * This toggles between DPMS ON and DPMS OFF; backlight toggles
220 * between DPMS ON and DPMS SUSPEND.
221 */
222 case WSDISPLAYIO_GVIDEO: {
223 int *onp = (int *)data;
224
225 /* XXX Can't really determine a single answer here. */
226 *onp = 1;
227 return 0;
228 }
229 case WSDISPLAYIO_SVIDEO: {
230 const int on = *(const int *)data;
231 const int dpms_mode = on? DRM_MODE_DPMS_ON : DRM_MODE_DPMS_OFF;
232 struct drm_fb_helper *const fb_helper = sc->sc_da.da_fb_helper;
233
234 mutex_lock(&fb_helper->lock);
235 drm_client_modeset_dpms(&fb_helper->client, dpms_mode);
236 mutex_unlock(&fb_helper->lock);
237
238 return 0;
239 }
240 default:
241 return EPASSTHROUGH;
242 }
243 }
244
245 static paddr_t
246 drmfb_genfb_mmap(void *v, void *vs, off_t offset, int prot)
247 {
248 struct genfb_softc *const genfb = v;
249 struct drmfb_softc *const sc = container_of(genfb, struct drmfb_softc,
250 sc_genfb);
251
252 KASSERT(0 <= offset);
253
254 if (offset < genfb->sc_fbsize) {
255 if (sc->sc_da.da_params->dp_mmapfb == NULL)
256 return -1;
257 return (*sc->sc_da.da_params->dp_mmapfb)(sc, offset, prot);
258 } else {
259 if (kauth_authorize_machdep(kauth_cred_get(),
260 KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL)
261 != 0)
262 return -1;
263 if (sc->sc_da.da_params->dp_mmap == NULL)
264 return -1;
265 return (*sc->sc_da.da_params->dp_mmap)(sc, offset, prot);
266 }
267 }
268
269 static int
270 drmfb_genfb_enable_polling(void *cookie)
271 {
272 struct genfb_softc *const genfb = cookie;
273 struct drmfb_softc *const sc = container_of(genfb, struct drmfb_softc,
274 sc_genfb);
275
276 return drm_fb_helper_debug_enter_fb(sc->sc_da.da_fb_helper);
277 }
278
279 static int
280 drmfb_genfb_disable_polling(void *cookie)
281 {
282 struct genfb_softc *const genfb = cookie;
283 struct drmfb_softc *const sc = container_of(genfb, struct drmfb_softc,
284 sc_genfb);
285
286 return drm_fb_helper_debug_leave_fb(sc->sc_da.da_fb_helper);
287 }
288
289 static bool
290 drmfb_genfb_setmode(struct genfb_softc *genfb, int mode)
291 {
292 struct drmfb_softc *sc = container_of(genfb, struct drmfb_softc,
293 sc_genfb);
294 struct drm_fb_helper *fb_helper = sc->sc_da.da_fb_helper;
295
296 if (mode == WSDISPLAYIO_MODE_EMUL)
297 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
298
299 return true;
300 }
301
302 bool
303 drmfb_shutdown(struct drmfb_softc *sc, int flags __unused)
304 {
305
306 genfb_enable_polling(sc->sc_da.da_dev);
307 return true;
308 }
309