genfb.c revision 1.81 1 1.81 macallan /* $NetBSD: genfb.c,v 1.81 2021/01/27 22:42:53 macallan Exp $ */
2 1.1 macallan
3 1.1 macallan /*-
4 1.1 macallan * Copyright (c) 2007 Michael Lorenz
5 1.1 macallan * All rights reserved.
6 1.1 macallan *
7 1.1 macallan * Redistribution and use in source and binary forms, with or without
8 1.1 macallan * modification, are permitted provided that the following conditions
9 1.1 macallan * are met:
10 1.1 macallan * 1. Redistributions of source code must retain the above copyright
11 1.1 macallan * notice, this list of conditions and the following disclaimer.
12 1.1 macallan * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 macallan * notice, this list of conditions and the following disclaimer in the
14 1.1 macallan * documentation and/or other materials provided with the distribution.
15 1.1 macallan *
16 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 macallan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 macallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 macallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 macallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 macallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 macallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 macallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 macallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 macallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 macallan * POSSIBILITY OF SUCH DAMAGE.
27 1.1 macallan */
28 1.1 macallan
29 1.1 macallan #include <sys/cdefs.h>
30 1.81 macallan __KERNEL_RCSID(0, "$NetBSD: genfb.c,v 1.81 2021/01/27 22:42:53 macallan Exp $");
31 1.1 macallan
32 1.1 macallan #include <sys/param.h>
33 1.1 macallan #include <sys/systm.h>
34 1.1 macallan #include <sys/kernel.h>
35 1.1 macallan #include <sys/device.h>
36 1.1 macallan #include <sys/proc.h>
37 1.1 macallan #include <sys/mutex.h>
38 1.1 macallan #include <sys/ioctl.h>
39 1.1 macallan #include <sys/kernel.h>
40 1.1 macallan #include <sys/systm.h>
41 1.23 jmcneill #include <sys/kmem.h>
42 1.71 martin #include <sys/reboot.h>
43 1.1 macallan
44 1.79 jmcneill #include <uvm/uvm_extern.h>
45 1.79 jmcneill
46 1.1 macallan #include <dev/wscons/wsconsio.h>
47 1.1 macallan #include <dev/wscons/wsdisplayvar.h>
48 1.1 macallan #include <dev/rasops/rasops.h>
49 1.1 macallan #include <dev/wsfont/wsfont.h>
50 1.1 macallan
51 1.1 macallan #include <dev/wscons/wsdisplay_vconsvar.h>
52 1.1 macallan
53 1.1 macallan #include <dev/wsfb/genfbvar.h>
54 1.1 macallan
55 1.65 jmcneill #include <dev/videomode/videomode.h>
56 1.65 jmcneill #include <dev/videomode/edidvar.h>
57 1.65 jmcneill
58 1.29 ahoka #ifdef GENFB_DISABLE_TEXT
59 1.29 ahoka #define DISABLESPLASH (boothowto & (RB_SINGLE | RB_USERCONF | RB_ASKNAME | \
60 1.29 ahoka AB_VERBOSE | AB_DEBUG) )
61 1.29 ahoka #endif
62 1.29 ahoka
63 1.53 riastrad #ifdef _KERNEL_OPT
64 1.1 macallan #include "opt_genfb.h"
65 1.1 macallan #include "opt_wsfb.h"
66 1.65 jmcneill #include "opt_rasops.h"
67 1.53 riastrad #endif
68 1.1 macallan
69 1.11 macallan #ifdef GENFB_DEBUG
70 1.11 macallan #define GPRINTF panic
71 1.11 macallan #else
72 1.49 jmcneill #define GPRINTF aprint_debug
73 1.11 macallan #endif
74 1.11 macallan
75 1.42 macallan #define GENFB_BRIGHTNESS_STEP 15
76 1.65 jmcneill #define GENFB_CHAR_WIDTH_MM 3
77 1.42 macallan
78 1.1 macallan static int genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
79 1.1 macallan static paddr_t genfb_mmap(void *, void *, off_t, int);
80 1.52 skrll static void genfb_pollc(void *, int);
81 1.52 skrll
82 1.1 macallan static void genfb_init_screen(void *, struct vcons_screen *, int, long *);
83 1.65 jmcneill static int genfb_calc_hsize(struct genfb_softc *);
84 1.65 jmcneill static int genfb_calc_cols(struct genfb_softc *);
85 1.1 macallan
86 1.1 macallan static int genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
87 1.1 macallan static int genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
88 1.1 macallan static int genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
89 1.1 macallan uint8_t, uint8_t);
90 1.45 macallan static void genfb_init_palette(struct genfb_softc *);
91 1.1 macallan
92 1.31 macallan static void genfb_brightness_up(device_t);
93 1.31 macallan static void genfb_brightness_down(device_t);
94 1.31 macallan
95 1.81 macallan #if GENFB_GLYPHCACHE > 0
96 1.81 macallan static int genfb_setup_glyphcache(struct genfb_softc *, long);
97 1.81 macallan static void genfb_putchar(void *, int, int, u_int, long);
98 1.81 macallan #endif
99 1.81 macallan
100 1.1 macallan extern const u_char rasops_cmap[768];
101 1.1 macallan
102 1.18 jmcneill static int genfb_cnattach_called = 0;
103 1.20 jmcneill static int genfb_enabled = 1;
104 1.18 jmcneill
105 1.19 jmcneill static struct genfb_softc *genfb_softc = NULL;
106 1.19 jmcneill
107 1.2 macallan void
108 1.2 macallan genfb_init(struct genfb_softc *sc)
109 1.2 macallan {
110 1.2 macallan prop_dictionary_t dict;
111 1.42 macallan uint64_t cmap_cb, pmf_cb, mode_cb, bl_cb, br_cb, fbaddr;
112 1.72 msaitoh uint64_t fboffset;
113 1.31 macallan bool console;
114 1.2 macallan
115 1.32 macallan dict = device_properties(sc->sc_dev);
116 1.2 macallan #ifdef GENFB_DEBUG
117 1.54 riastrad printf("%s", prop_dictionary_externalize(dict));
118 1.2 macallan #endif
119 1.31 macallan prop_dictionary_get_bool(dict, "is_console", &console);
120 1.31 macallan
121 1.11 macallan if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
122 1.12 macallan GPRINTF("no width property\n");
123 1.11 macallan return;
124 1.11 macallan }
125 1.11 macallan if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
126 1.12 macallan GPRINTF("no height property\n");
127 1.11 macallan return;
128 1.11 macallan }
129 1.11 macallan if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
130 1.12 macallan GPRINTF("no depth property\n");
131 1.11 macallan return;
132 1.11 macallan }
133 1.2 macallan
134 1.72 msaitoh if (!prop_dictionary_get_uint64(dict, "address", &fboffset)) {
135 1.12 macallan GPRINTF("no address property\n");
136 1.11 macallan return;
137 1.11 macallan }
138 1.11 macallan
139 1.72 msaitoh sc->sc_fboffset = (bus_addr_t)fboffset;
140 1.2 macallan
141 1.39 macallan sc->sc_fbaddr = NULL;
142 1.39 macallan if (prop_dictionary_get_uint64(dict, "virtual_address", &fbaddr)) {
143 1.40 macallan sc->sc_fbaddr = (void *)(uintptr_t)fbaddr;
144 1.39 macallan }
145 1.39 macallan
146 1.59 nonaka sc->sc_shadowfb = NULL;
147 1.59 nonaka if (!prop_dictionary_get_bool(dict, "enable_shadowfb",
148 1.59 nonaka &sc->sc_enable_shadowfb))
149 1.59 nonaka #ifdef GENFB_SHADOWFB
150 1.59 nonaka sc->sc_enable_shadowfb = true;
151 1.59 nonaka #else
152 1.59 nonaka sc->sc_enable_shadowfb = false;
153 1.59 nonaka #endif
154 1.59 nonaka
155 1.2 macallan if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
156 1.2 macallan sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
157 1.13 macallan
158 1.13 macallan /*
159 1.13 macallan * deal with a bug in the Raptor firmware which always sets
160 1.13 macallan * stride = width even when depth != 8
161 1.13 macallan */
162 1.13 macallan if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3))
163 1.13 macallan sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
164 1.13 macallan
165 1.6 macallan sc->sc_fbsize = sc->sc_height * sc->sc_stride;
166 1.4 macallan
167 1.4 macallan /* optional colour map callback */
168 1.4 macallan sc->sc_cmcb = NULL;
169 1.4 macallan if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
170 1.4 macallan if (cmap_cb != 0)
171 1.4 macallan sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
172 1.4 macallan }
173 1.31 macallan
174 1.28 jmcneill /* optional pmf callback */
175 1.28 jmcneill sc->sc_pmfcb = NULL;
176 1.28 jmcneill if (prop_dictionary_get_uint64(dict, "pmf_callback", &pmf_cb)) {
177 1.28 jmcneill if (pmf_cb != 0)
178 1.28 jmcneill sc->sc_pmfcb = (void *)(vaddr_t)pmf_cb;
179 1.28 jmcneill }
180 1.31 macallan
181 1.34 jmcneill /* optional mode callback */
182 1.34 jmcneill sc->sc_modecb = NULL;
183 1.34 jmcneill if (prop_dictionary_get_uint64(dict, "mode_callback", &mode_cb)) {
184 1.34 jmcneill if (mode_cb != 0)
185 1.34 jmcneill sc->sc_modecb = (void *)(vaddr_t)mode_cb;
186 1.34 jmcneill }
187 1.34 jmcneill
188 1.31 macallan /* optional backlight control callback */
189 1.31 macallan sc->sc_backlight = NULL;
190 1.31 macallan if (prop_dictionary_get_uint64(dict, "backlight_callback", &bl_cb)) {
191 1.31 macallan if (bl_cb != 0) {
192 1.31 macallan sc->sc_backlight = (void *)(vaddr_t)bl_cb;
193 1.42 macallan aprint_naive_dev(sc->sc_dev,
194 1.31 macallan "enabling backlight control\n");
195 1.42 macallan }
196 1.42 macallan }
197 1.42 macallan
198 1.42 macallan /* optional brightness control callback */
199 1.42 macallan sc->sc_brightness = NULL;
200 1.42 macallan if (prop_dictionary_get_uint64(dict, "brightness_callback", &br_cb)) {
201 1.42 macallan if (br_cb != 0) {
202 1.42 macallan sc->sc_brightness = (void *)(vaddr_t)br_cb;
203 1.42 macallan aprint_naive_dev(sc->sc_dev,
204 1.42 macallan "enabling brightness control\n");
205 1.42 macallan if (console &&
206 1.42 macallan sc->sc_brightness->gpc_upd_parameter != NULL) {
207 1.42 macallan pmf_event_register(sc->sc_dev,
208 1.31 macallan PMFE_DISPLAY_BRIGHTNESS_UP,
209 1.31 macallan genfb_brightness_up, TRUE);
210 1.42 macallan pmf_event_register(sc->sc_dev,
211 1.31 macallan PMFE_DISPLAY_BRIGHTNESS_DOWN,
212 1.31 macallan genfb_brightness_down, TRUE);
213 1.31 macallan }
214 1.31 macallan }
215 1.31 macallan }
216 1.2 macallan }
217 1.2 macallan
218 1.2 macallan int
219 1.2 macallan genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
220 1.1 macallan {
221 1.1 macallan struct wsemuldisplaydev_attach_args aa;
222 1.1 macallan prop_dictionary_t dict;
223 1.1 macallan struct rasops_info *ri;
224 1.79 jmcneill paddr_t fb_phys;
225 1.21 jmcneill uint16_t crow;
226 1.1 macallan long defattr;
227 1.7 macallan bool console;
228 1.33 jmcneill #ifdef SPLASHSCREEN
229 1.45 macallan int i, j;
230 1.33 jmcneill int error = ENXIO;
231 1.33 jmcneill #endif
232 1.9 jmmv
233 1.32 macallan dict = device_properties(sc->sc_dev);
234 1.14 phx prop_dictionary_get_bool(dict, "is_console", &console);
235 1.14 phx
236 1.21 jmcneill if (prop_dictionary_get_uint16(dict, "cursor-row", &crow) == false)
237 1.21 jmcneill crow = 0;
238 1.21 jmcneill if (prop_dictionary_get_bool(dict, "clear-screen", &sc->sc_want_clear)
239 1.21 jmcneill == false)
240 1.21 jmcneill sc->sc_want_clear = true;
241 1.21 jmcneill
242 1.79 jmcneill fb_phys = (paddr_t)sc->sc_fboffset;
243 1.79 jmcneill if (fb_phys == 0) {
244 1.79 jmcneill KASSERT(sc->sc_fbaddr != NULL);
245 1.79 jmcneill (void)pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_fbaddr,
246 1.79 jmcneill &fb_phys);
247 1.79 jmcneill }
248 1.79 jmcneill
249 1.42 macallan aprint_verbose_dev(sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
250 1.42 macallan "stride %d\n",
251 1.79 jmcneill fb_phys ? (void *)(intptr_t)fb_phys : sc->sc_fbaddr,
252 1.9 jmmv sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
253 1.9 jmmv
254 1.1 macallan sc->sc_defaultscreen_descr = (struct wsscreen_descr){
255 1.1 macallan "default",
256 1.1 macallan 0, 0,
257 1.1 macallan NULL,
258 1.1 macallan 8, 16,
259 1.60 macallan WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
260 1.60 macallan WSSCREEN_RESIZE,
261 1.3 macallan NULL
262 1.1 macallan };
263 1.1 macallan sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
264 1.1 macallan sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
265 1.1 macallan memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
266 1.1 macallan sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
267 1.36 phx if (sc->sc_modecb != NULL)
268 1.34 jmcneill sc->sc_modecb->gmc_setmode(sc, sc->sc_mode);
269 1.1 macallan
270 1.41 macallan sc->sc_accessops.ioctl = genfb_ioctl;
271 1.41 macallan sc->sc_accessops.mmap = genfb_mmap;
272 1.52 skrll sc->sc_accessops.pollc = genfb_pollc;
273 1.41 macallan
274 1.59 nonaka if (sc->sc_enable_shadowfb) {
275 1.59 nonaka sc->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP);
276 1.61 chs if (sc->sc_want_clear == false)
277 1.59 nonaka memcpy(sc->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize);
278 1.68 rin aprint_verbose_dev(sc->sc_dev,
279 1.68 rin "shadow framebuffer enabled, size %zu KB\n",
280 1.68 rin sc->sc_fbsize >> 10);
281 1.59 nonaka }
282 1.5 macallan
283 1.1 macallan vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
284 1.41 macallan &sc->sc_accessops);
285 1.1 macallan sc->vd.init_screen = genfb_init_screen;
286 1.1 macallan
287 1.10 jmmv /* Do not print anything between this point and the screen
288 1.10 jmmv * clear operation below. Otherwise it will be lost. */
289 1.10 jmmv
290 1.1 macallan ri = &sc->sc_console_screen.scr_ri;
291 1.1 macallan
292 1.14 phx vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
293 1.14 phx &defattr);
294 1.14 phx sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
295 1.14 phx
296 1.81 macallan #if GENFB_GLYPHCACHE > 0
297 1.81 macallan genfb_setup_glyphcache(sc, defattr);
298 1.81 macallan #endif
299 1.81 macallan
300 1.29 ahoka #ifdef SPLASHSCREEN
301 1.29 ahoka /*
302 1.29 ahoka * If system isn't going to go multiuser, or user has requested to see
303 1.29 ahoka * boot text, don't render splash screen immediately
304 1.29 ahoka */
305 1.29 ahoka if (DISABLESPLASH)
306 1.29 ahoka #endif
307 1.29 ahoka vcons_redraw_screen(&sc->sc_console_screen);
308 1.29 ahoka
309 1.14 phx sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
310 1.14 phx sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
311 1.14 phx sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
312 1.14 phx sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
313 1.41 macallan
314 1.44 macallan if (crow >= ri->ri_rows) {
315 1.44 macallan crow = 0;
316 1.44 macallan sc->sc_want_clear = 1;
317 1.44 macallan }
318 1.44 macallan
319 1.41 macallan if (console)
320 1.41 macallan wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, crow,
321 1.41 macallan defattr);
322 1.1 macallan
323 1.10 jmmv /* Clear the whole screen to bring it to a known state. */
324 1.21 jmcneill if (sc->sc_want_clear)
325 1.21 jmcneill (*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
326 1.10 jmmv
327 1.45 macallan #ifdef SPLASHSCREEN
328 1.1 macallan j = 0;
329 1.64 riastrad for (i = 0; i < uimin(1 << sc->sc_depth, 256); i++) {
330 1.33 jmcneill if (i >= SPLASH_CMAP_OFFSET &&
331 1.29 ahoka i < SPLASH_CMAP_OFFSET + SPLASH_CMAP_SIZE) {
332 1.33 jmcneill splash_get_cmap(i,
333 1.33 jmcneill &sc->sc_cmap_red[i],
334 1.33 jmcneill &sc->sc_cmap_green[i],
335 1.33 jmcneill &sc->sc_cmap_blue[i]);
336 1.29 ahoka } else {
337 1.29 ahoka sc->sc_cmap_red[i] = rasops_cmap[j];
338 1.29 ahoka sc->sc_cmap_green[i] = rasops_cmap[j + 1];
339 1.29 ahoka sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
340 1.29 ahoka }
341 1.29 ahoka j += 3;
342 1.1 macallan }
343 1.33 jmcneill genfb_restore_palette(sc);
344 1.1 macallan
345 1.29 ahoka sc->sc_splash.si_depth = sc->sc_depth;
346 1.58 nat sc->sc_splash.si_bits = sc->sc_console_screen.scr_ri.ri_origbits;
347 1.29 ahoka sc->sc_splash.si_hwbits = sc->sc_fbaddr;
348 1.29 ahoka sc->sc_splash.si_width = sc->sc_width;
349 1.29 ahoka sc->sc_splash.si_height = sc->sc_height;
350 1.29 ahoka sc->sc_splash.si_stride = sc->sc_stride;
351 1.29 ahoka sc->sc_splash.si_fillrect = NULL;
352 1.33 jmcneill if (!DISABLESPLASH) {
353 1.33 jmcneill error = splash_render(&sc->sc_splash,
354 1.33 jmcneill SPLASH_F_CENTER|SPLASH_F_FILL);
355 1.33 jmcneill if (error) {
356 1.33 jmcneill SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
357 1.45 macallan genfb_init_palette(sc);
358 1.33 jmcneill vcons_replay_msgbuf(&sc->sc_console_screen);
359 1.33 jmcneill }
360 1.33 jmcneill }
361 1.29 ahoka #else
362 1.45 macallan genfb_init_palette(sc);
363 1.71 martin if (console && (boothowto & (AB_SILENT|AB_QUIET)) == 0)
364 1.53 riastrad vcons_replay_msgbuf(&sc->sc_console_screen);
365 1.29 ahoka #endif
366 1.27 macallan
367 1.19 jmcneill if (genfb_softc == NULL)
368 1.19 jmcneill genfb_softc = sc;
369 1.19 jmcneill
370 1.1 macallan aa.console = console;
371 1.1 macallan aa.scrdata = &sc->sc_screenlist;
372 1.41 macallan aa.accessops = &sc->sc_accessops;
373 1.1 macallan aa.accesscookie = &sc->vd;
374 1.1 macallan
375 1.29 ahoka #ifdef GENFB_DISABLE_TEXT
376 1.33 jmcneill if (!DISABLESPLASH && error == 0)
377 1.29 ahoka SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
378 1.29 ahoka #endif
379 1.29 ahoka
380 1.53 riastrad config_found_ia(sc->sc_dev, "wsemuldisplaydev", &aa,
381 1.53 riastrad wsemuldisplaydevprint);
382 1.1 macallan
383 1.1 macallan return 0;
384 1.1 macallan }
385 1.1 macallan
386 1.1 macallan static int
387 1.1 macallan genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
388 1.1 macallan struct lwp *l)
389 1.1 macallan {
390 1.1 macallan struct vcons_data *vd = v;
391 1.1 macallan struct genfb_softc *sc = vd->cookie;
392 1.1 macallan struct wsdisplay_fbinfo *wdf;
393 1.1 macallan struct vcons_screen *ms = vd->active;
394 1.31 macallan struct wsdisplay_param *param;
395 1.56 macallan int new_mode, error, val, ret;
396 1.1 macallan
397 1.1 macallan switch (cmd) {
398 1.1 macallan case WSDISPLAYIO_GINFO:
399 1.6 macallan if (ms == NULL)
400 1.6 macallan return ENODEV;
401 1.1 macallan wdf = (void *)data;
402 1.1 macallan wdf->height = ms->scr_ri.ri_height;
403 1.1 macallan wdf->width = ms->scr_ri.ri_width;
404 1.1 macallan wdf->depth = ms->scr_ri.ri_depth;
405 1.1 macallan wdf->cmsize = 256;
406 1.1 macallan return 0;
407 1.1 macallan
408 1.1 macallan case WSDISPLAYIO_GETCMAP:
409 1.1 macallan return genfb_getcmap(sc,
410 1.1 macallan (struct wsdisplay_cmap *)data);
411 1.1 macallan
412 1.1 macallan case WSDISPLAYIO_PUTCMAP:
413 1.1 macallan return genfb_putcmap(sc,
414 1.1 macallan (struct wsdisplay_cmap *)data);
415 1.1 macallan
416 1.3 macallan case WSDISPLAYIO_LINEBYTES:
417 1.3 macallan *(u_int *)data = sc->sc_stride;
418 1.3 macallan return 0;
419 1.3 macallan
420 1.1 macallan case WSDISPLAYIO_SMODE:
421 1.24 jmcneill new_mode = *(int *)data;
422 1.11 macallan
423 1.24 jmcneill /* notify the bus backend */
424 1.24 jmcneill error = 0;
425 1.24 jmcneill if (sc->sc_ops.genfb_ioctl)
426 1.24 jmcneill error = sc->sc_ops.genfb_ioctl(sc, vs,
427 1.11 macallan cmd, data, flag, l);
428 1.50 jmcneill if (error && error != EPASSTHROUGH)
429 1.24 jmcneill return error;
430 1.11 macallan
431 1.24 jmcneill if (new_mode != sc->sc_mode) {
432 1.24 jmcneill sc->sc_mode = new_mode;
433 1.36 phx if (sc->sc_modecb != NULL)
434 1.34 jmcneill sc->sc_modecb->gmc_setmode(sc,
435 1.34 jmcneill sc->sc_mode);
436 1.24 jmcneill if (new_mode == WSDISPLAYIO_MODE_EMUL) {
437 1.24 jmcneill genfb_restore_palette(sc);
438 1.24 jmcneill vcons_redraw_screen(ms);
439 1.1 macallan }
440 1.1 macallan }
441 1.1 macallan return 0;
442 1.51 macallan
443 1.29 ahoka case WSDISPLAYIO_SSPLASH:
444 1.29 ahoka #if defined(SPLASHSCREEN)
445 1.29 ahoka if(*(int *)data == 1) {
446 1.29 ahoka SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
447 1.29 ahoka splash_render(&sc->sc_splash,
448 1.29 ahoka SPLASH_F_CENTER|SPLASH_F_FILL);
449 1.29 ahoka } else {
450 1.29 ahoka SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
451 1.45 macallan genfb_init_palette(sc);
452 1.29 ahoka }
453 1.29 ahoka vcons_redraw_screen(ms);
454 1.29 ahoka return 0;
455 1.29 ahoka #else
456 1.29 ahoka return ENODEV;
457 1.29 ahoka #endif
458 1.31 macallan case WSDISPLAYIO_GETPARAM:
459 1.31 macallan param = (struct wsdisplay_param *)data;
460 1.31 macallan switch (param->param) {
461 1.31 macallan case WSDISPLAYIO_PARAM_BRIGHTNESS:
462 1.42 macallan if (sc->sc_brightness == NULL)
463 1.42 macallan return EPASSTHROUGH;
464 1.31 macallan param->min = 0;
465 1.31 macallan param->max = 255;
466 1.42 macallan return sc->sc_brightness->gpc_get_parameter(
467 1.42 macallan sc->sc_brightness->gpc_cookie,
468 1.42 macallan ¶m->curval);
469 1.31 macallan case WSDISPLAYIO_PARAM_BACKLIGHT:
470 1.42 macallan if (sc->sc_backlight == NULL)
471 1.42 macallan return EPASSTHROUGH;
472 1.31 macallan param->min = 0;
473 1.31 macallan param->max = 1;
474 1.42 macallan return sc->sc_backlight->gpc_get_parameter(
475 1.42 macallan sc->sc_backlight->gpc_cookie,
476 1.42 macallan ¶m->curval);
477 1.31 macallan }
478 1.31 macallan return EPASSTHROUGH;
479 1.31 macallan
480 1.31 macallan case WSDISPLAYIO_SETPARAM:
481 1.31 macallan param = (struct wsdisplay_param *)data;
482 1.31 macallan switch (param->param) {
483 1.31 macallan case WSDISPLAYIO_PARAM_BRIGHTNESS:
484 1.42 macallan if (sc->sc_brightness == NULL)
485 1.42 macallan return EPASSTHROUGH;
486 1.42 macallan val = param->curval;
487 1.42 macallan if (val < 0) val = 0;
488 1.42 macallan if (val > 255) val = 255;
489 1.42 macallan return sc->sc_brightness->gpc_set_parameter(
490 1.42 macallan sc->sc_brightness->gpc_cookie, val);
491 1.31 macallan case WSDISPLAYIO_PARAM_BACKLIGHT:
492 1.42 macallan if (sc->sc_backlight == NULL)
493 1.42 macallan return EPASSTHROUGH;
494 1.42 macallan val = param->curval;
495 1.42 macallan if (val < 0) val = 0;
496 1.42 macallan if (val > 1) val = 1;
497 1.42 macallan return sc->sc_backlight->gpc_set_parameter(
498 1.42 macallan sc->sc_backlight->gpc_cookie, val);
499 1.31 macallan }
500 1.56 macallan return EPASSTHROUGH;
501 1.56 macallan }
502 1.56 macallan ret = EPASSTHROUGH;
503 1.56 macallan if (sc->sc_ops.genfb_ioctl)
504 1.56 macallan ret = sc->sc_ops.genfb_ioctl(sc, vs, cmd, data, flag, l);
505 1.56 macallan if (ret != EPASSTHROUGH)
506 1.56 macallan return ret;
507 1.56 macallan /*
508 1.56 macallan * XXX
509 1.56 macallan * handle these only if there either is no ioctl() handler or it didn't
510 1.56 macallan * know how to deal with them. This allows bus frontends to override
511 1.56 macallan * them but still provides fallback implementations
512 1.56 macallan */
513 1.56 macallan switch (cmd) {
514 1.42 macallan case WSDISPLAYIO_GET_EDID: {
515 1.42 macallan struct wsdisplayio_edid_info *d = data;
516 1.42 macallan return wsdisplayio_get_edid(sc->sc_dev, d);
517 1.42 macallan }
518 1.51 macallan
519 1.51 macallan case WSDISPLAYIO_GET_FBINFO: {
520 1.51 macallan struct wsdisplayio_fbinfo *fbi = data;
521 1.77 rin return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
522 1.51 macallan }
523 1.1 macallan }
524 1.1 macallan return EPASSTHROUGH;
525 1.1 macallan }
526 1.1 macallan
527 1.1 macallan static paddr_t
528 1.1 macallan genfb_mmap(void *v, void *vs, off_t offset, int prot)
529 1.1 macallan {
530 1.1 macallan struct vcons_data *vd = v;
531 1.1 macallan struct genfb_softc *sc = vd->cookie;
532 1.1 macallan
533 1.1 macallan if (sc->sc_ops.genfb_mmap)
534 1.1 macallan return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
535 1.1 macallan
536 1.1 macallan return -1;
537 1.1 macallan }
538 1.1 macallan
539 1.1 macallan static void
540 1.52 skrll genfb_pollc(void *v, int on)
541 1.52 skrll {
542 1.52 skrll struct vcons_data *vd = v;
543 1.52 skrll struct genfb_softc *sc = vd->cookie;
544 1.52 skrll
545 1.52 skrll if (sc == NULL)
546 1.52 skrll return;
547 1.52 skrll
548 1.52 skrll if (on)
549 1.52 skrll genfb_enable_polling(sc->sc_dev);
550 1.52 skrll else
551 1.52 skrll genfb_disable_polling(sc->sc_dev);
552 1.52 skrll }
553 1.52 skrll
554 1.52 skrll static void
555 1.1 macallan genfb_init_screen(void *cookie, struct vcons_screen *scr,
556 1.1 macallan int existing, long *defattr)
557 1.1 macallan {
558 1.1 macallan struct genfb_softc *sc = cookie;
559 1.1 macallan struct rasops_info *ri = &scr->scr_ri;
560 1.65 jmcneill int wantcols;
561 1.78 rin bool is_bgr, is_swapped;
562 1.1 macallan
563 1.1 macallan ri->ri_depth = sc->sc_depth;
564 1.1 macallan ri->ri_width = sc->sc_width;
565 1.1 macallan ri->ri_height = sc->sc_height;
566 1.1 macallan ri->ri_stride = sc->sc_stride;
567 1.21 jmcneill ri->ri_flg = RI_CENTER;
568 1.21 jmcneill if (sc->sc_want_clear)
569 1.21 jmcneill ri->ri_flg |= RI_FULLCLEAR;
570 1.1 macallan
571 1.60 macallan scr->scr_flags |= VCONS_LOADFONT;
572 1.60 macallan
573 1.5 macallan if (sc->sc_shadowfb != NULL) {
574 1.5 macallan ri->ri_hwbits = (char *)sc->sc_fbaddr;
575 1.5 macallan ri->ri_bits = (char *)sc->sc_shadowfb;
576 1.59 nonaka } else {
577 1.5 macallan ri->ri_bits = (char *)sc->sc_fbaddr;
578 1.30 macallan scr->scr_flags |= VCONS_DONT_READ;
579 1.30 macallan }
580 1.1 macallan
581 1.67 rin if (existing && sc->sc_want_clear)
582 1.1 macallan ri->ri_flg |= RI_CLEAR;
583 1.1 macallan
584 1.69 rin switch (ri->ri_depth) {
585 1.69 rin case 32:
586 1.69 rin case 24:
587 1.78 rin ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
588 1.67 rin ri->ri_flg |= RI_ENABLE_ALPHA;
589 1.67 rin
590 1.70 rin is_bgr = false;
591 1.48 macallan prop_dictionary_get_bool(device_properties(sc->sc_dev),
592 1.48 macallan "is_bgr", &is_bgr);
593 1.78 rin
594 1.78 rin is_swapped = false;
595 1.78 rin prop_dictionary_get_bool(device_properties(sc->sc_dev),
596 1.78 rin "is_swapped", &is_swapped);
597 1.78 rin
598 1.48 macallan if (is_bgr) {
599 1.48 macallan /* someone requested BGR */
600 1.48 macallan ri->ri_rpos = 0;
601 1.48 macallan ri->ri_gpos = 8;
602 1.48 macallan ri->ri_bpos = 16;
603 1.78 rin } else if (is_swapped) {
604 1.78 rin /* byte-swapped, must be 32 bpp */
605 1.78 rin KASSERT(ri->ri_depth == 32);
606 1.78 rin ri->ri_rpos = 8;
607 1.78 rin ri->ri_gpos = 16;
608 1.78 rin ri->ri_bpos = 24;
609 1.48 macallan } else {
610 1.48 macallan /* assume RGB */
611 1.48 macallan ri->ri_rpos = 16;
612 1.48 macallan ri->ri_gpos = 8;
613 1.48 macallan ri->ri_bpos = 0;
614 1.48 macallan }
615 1.69 rin break;
616 1.69 rin
617 1.69 rin case 16:
618 1.69 rin case 15:
619 1.69 rin ri->ri_flg |= RI_ENABLE_ALPHA;
620 1.69 rin break;
621 1.69 rin
622 1.69 rin case 8:
623 1.69 rin if (sc->sc_cmcb != NULL)
624 1.69 rin ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB;
625 1.69 rin break;
626 1.43 macallan
627 1.69 rin case 2:
628 1.66 rin ri->ri_flg |= RI_ENABLE_ALPHA;
629 1.69 rin break;
630 1.66 rin
631 1.69 rin default:
632 1.69 rin break;
633 1.69 rin }
634 1.45 macallan
635 1.65 jmcneill wantcols = genfb_calc_cols(sc);
636 1.45 macallan
637 1.65 jmcneill rasops_init(ri, 0, wantcols);
638 1.60 macallan ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
639 1.60 macallan WSSCREEN_RESIZE;
640 1.1 macallan rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
641 1.1 macallan sc->sc_width / ri->ri_font->fontwidth);
642 1.1 macallan
643 1.1 macallan ri->ri_hw = scr;
644 1.81 macallan #if GENFB_GLYPHCACHE > 0
645 1.81 macallan sc->sc_putchar = ri->ri_ops.putchar;
646 1.81 macallan ri->ri_ops.putchar = genfb_putchar;
647 1.81 macallan #endif
648 1.29 ahoka #ifdef GENFB_DISABLE_TEXT
649 1.29 ahoka if (scr == &sc->sc_console_screen && !DISABLESPLASH)
650 1.29 ahoka SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
651 1.29 ahoka #endif
652 1.1 macallan }
653 1.1 macallan
654 1.65 jmcneill /* Returns the width of the display in millimeters, or 0 if not known. */
655 1.65 jmcneill static int
656 1.65 jmcneill genfb_calc_hsize(struct genfb_softc *sc)
657 1.65 jmcneill {
658 1.65 jmcneill device_t dev = sc->sc_dev;
659 1.65 jmcneill prop_dictionary_t dict = device_properties(dev);
660 1.65 jmcneill prop_data_t edid_data;
661 1.73 jdolecek struct edid_info *edid;
662 1.65 jmcneill const char *edid_ptr;
663 1.73 jdolecek int hsize;
664 1.65 jmcneill
665 1.65 jmcneill edid_data = prop_dictionary_get(dict, "EDID");
666 1.65 jmcneill if (edid_data == NULL || prop_data_size(edid_data) < 128)
667 1.65 jmcneill return 0;
668 1.65 jmcneill
669 1.73 jdolecek edid = kmem_alloc(sizeof(*edid), KM_SLEEP);
670 1.73 jdolecek
671 1.74 thorpej edid_ptr = prop_data_value(edid_data);
672 1.73 jdolecek if (edid_parse(__UNCONST(edid_ptr), edid) == 0)
673 1.73 jdolecek hsize = (int)edid->edid_max_hsize * 10;
674 1.73 jdolecek else
675 1.73 jdolecek hsize = 0;
676 1.73 jdolecek
677 1.73 jdolecek kmem_free(edid, sizeof(*edid));
678 1.65 jmcneill
679 1.73 jdolecek return hsize;
680 1.65 jmcneill }
681 1.65 jmcneill
682 1.65 jmcneill /* Return the minimum number of character columns based on DPI */
683 1.65 jmcneill static int
684 1.65 jmcneill genfb_calc_cols(struct genfb_softc *sc)
685 1.65 jmcneill {
686 1.65 jmcneill const int hsize = genfb_calc_hsize(sc);
687 1.65 jmcneill
688 1.65 jmcneill return MAX(RASOPS_DEFAULT_WIDTH, hsize / GENFB_CHAR_WIDTH_MM);
689 1.65 jmcneill }
690 1.65 jmcneill
691 1.1 macallan static int
692 1.1 macallan genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
693 1.1 macallan {
694 1.1 macallan u_char *r, *g, *b;
695 1.1 macallan u_int index = cm->index;
696 1.1 macallan u_int count = cm->count;
697 1.1 macallan int i, error;
698 1.1 macallan u_char rbuf[256], gbuf[256], bbuf[256];
699 1.1 macallan
700 1.1 macallan #ifdef GENFB_DEBUG
701 1.1 macallan aprint_debug("putcmap: %d %d\n",index, count);
702 1.1 macallan #endif
703 1.63 mlelstv if (index >= 256 || count > 256 || index + count > 256)
704 1.1 macallan return EINVAL;
705 1.63 mlelstv
706 1.1 macallan error = copyin(cm->red, &rbuf[index], count);
707 1.1 macallan if (error)
708 1.1 macallan return error;
709 1.1 macallan error = copyin(cm->green, &gbuf[index], count);
710 1.1 macallan if (error)
711 1.1 macallan return error;
712 1.1 macallan error = copyin(cm->blue, &bbuf[index], count);
713 1.1 macallan if (error)
714 1.1 macallan return error;
715 1.1 macallan
716 1.1 macallan memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
717 1.1 macallan memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
718 1.1 macallan memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
719 1.1 macallan
720 1.1 macallan r = &sc->sc_cmap_red[index];
721 1.1 macallan g = &sc->sc_cmap_green[index];
722 1.1 macallan b = &sc->sc_cmap_blue[index];
723 1.1 macallan
724 1.1 macallan for (i = 0; i < count; i++) {
725 1.1 macallan genfb_putpalreg(sc, index, *r, *g, *b);
726 1.1 macallan index++;
727 1.1 macallan r++, g++, b++;
728 1.1 macallan }
729 1.1 macallan return 0;
730 1.1 macallan }
731 1.1 macallan
732 1.1 macallan static int
733 1.1 macallan genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
734 1.1 macallan {
735 1.1 macallan u_int index = cm->index;
736 1.1 macallan u_int count = cm->count;
737 1.1 macallan int error;
738 1.1 macallan
739 1.63 mlelstv if (index >= 256 || count > 256 || index + count > 256)
740 1.1 macallan return EINVAL;
741 1.1 macallan
742 1.1 macallan error = copyout(&sc->sc_cmap_red[index], cm->red, count);
743 1.1 macallan if (error)
744 1.1 macallan return error;
745 1.1 macallan error = copyout(&sc->sc_cmap_green[index], cm->green, count);
746 1.1 macallan if (error)
747 1.1 macallan return error;
748 1.1 macallan error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
749 1.1 macallan if (error)
750 1.1 macallan return error;
751 1.1 macallan
752 1.1 macallan return 0;
753 1.1 macallan }
754 1.1 macallan
755 1.28 jmcneill void
756 1.1 macallan genfb_restore_palette(struct genfb_softc *sc)
757 1.1 macallan {
758 1.1 macallan int i;
759 1.1 macallan
760 1.27 macallan if (sc->sc_depth <= 8) {
761 1.27 macallan for (i = 0; i < (1 << sc->sc_depth); i++) {
762 1.27 macallan genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
763 1.27 macallan sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
764 1.27 macallan }
765 1.1 macallan }
766 1.1 macallan }
767 1.1 macallan
768 1.45 macallan static void
769 1.45 macallan genfb_init_palette(struct genfb_softc *sc)
770 1.45 macallan {
771 1.45 macallan int i, j, tmp;
772 1.45 macallan
773 1.45 macallan if (sc->sc_depth == 8) {
774 1.45 macallan /* generate an r3g3b2 colour map */
775 1.45 macallan for (i = 0; i < 256; i++) {
776 1.45 macallan tmp = i & 0xe0;
777 1.45 macallan /*
778 1.45 macallan * replicate bits so 0xe0 maps to a red value of 0xff
779 1.45 macallan * in order to make white look actually white
780 1.45 macallan */
781 1.45 macallan tmp |= (tmp >> 3) | (tmp >> 6);
782 1.45 macallan sc->sc_cmap_red[i] = tmp;
783 1.45 macallan
784 1.45 macallan tmp = (i & 0x1c) << 3;
785 1.45 macallan tmp |= (tmp >> 3) | (tmp >> 6);
786 1.45 macallan sc->sc_cmap_green[i] = tmp;
787 1.45 macallan
788 1.45 macallan tmp = (i & 0x03) << 6;
789 1.45 macallan tmp |= tmp >> 2;
790 1.45 macallan tmp |= tmp >> 4;
791 1.45 macallan sc->sc_cmap_blue[i] = tmp;
792 1.45 macallan
793 1.45 macallan genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
794 1.45 macallan sc->sc_cmap_green[i],
795 1.45 macallan sc->sc_cmap_blue[i]);
796 1.45 macallan }
797 1.45 macallan } else {
798 1.45 macallan /* steal rasops' ANSI cmap */
799 1.45 macallan j = 0;
800 1.45 macallan for (i = 0; i < 256; i++) {
801 1.45 macallan sc->sc_cmap_red[i] = rasops_cmap[j];
802 1.45 macallan sc->sc_cmap_green[i] = rasops_cmap[j + 1];
803 1.45 macallan sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
804 1.45 macallan j += 3;
805 1.45 macallan }
806 1.45 macallan }
807 1.45 macallan }
808 1.45 macallan
809 1.1 macallan static int
810 1.1 macallan genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
811 1.1 macallan uint8_t b)
812 1.1 macallan {
813 1.1 macallan
814 1.4 macallan if (sc->sc_cmcb) {
815 1.4 macallan
816 1.4 macallan sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
817 1.4 macallan idx, r, g, b);
818 1.4 macallan }
819 1.1 macallan return 0;
820 1.1 macallan }
821 1.1 macallan
822 1.18 jmcneill void
823 1.18 jmcneill genfb_cnattach(void)
824 1.18 jmcneill {
825 1.18 jmcneill genfb_cnattach_called = 1;
826 1.18 jmcneill }
827 1.18 jmcneill
828 1.20 jmcneill void
829 1.20 jmcneill genfb_disable(void)
830 1.20 jmcneill {
831 1.20 jmcneill genfb_enabled = 0;
832 1.20 jmcneill }
833 1.20 jmcneill
834 1.18 jmcneill int
835 1.18 jmcneill genfb_is_console(void)
836 1.18 jmcneill {
837 1.18 jmcneill return genfb_cnattach_called;
838 1.18 jmcneill }
839 1.19 jmcneill
840 1.19 jmcneill int
841 1.20 jmcneill genfb_is_enabled(void)
842 1.20 jmcneill {
843 1.20 jmcneill return genfb_enabled;
844 1.20 jmcneill }
845 1.20 jmcneill
846 1.20 jmcneill int
847 1.19 jmcneill genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
848 1.19 jmcneill {
849 1.19 jmcneill struct genfb_softc *sc = genfb_softc;
850 1.19 jmcneill
851 1.19 jmcneill if (sc && sc->sc_ops.genfb_borrow)
852 1.19 jmcneill return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
853 1.19 jmcneill return 0;
854 1.19 jmcneill }
855 1.31 macallan
856 1.31 macallan static void
857 1.31 macallan genfb_brightness_up(device_t dev)
858 1.31 macallan {
859 1.31 macallan struct genfb_softc *sc = device_private(dev);
860 1.31 macallan
861 1.42 macallan KASSERT(sc->sc_brightness != NULL &&
862 1.42 macallan sc->sc_brightness->gpc_upd_parameter != NULL);
863 1.42 macallan
864 1.42 macallan (void)sc->sc_brightness->gpc_upd_parameter(
865 1.42 macallan sc->sc_brightness->gpc_cookie, GENFB_BRIGHTNESS_STEP);
866 1.31 macallan }
867 1.31 macallan
868 1.31 macallan static void
869 1.31 macallan genfb_brightness_down(device_t dev)
870 1.31 macallan {
871 1.31 macallan struct genfb_softc *sc = device_private(dev);
872 1.31 macallan
873 1.42 macallan KASSERT(sc->sc_brightness != NULL &&
874 1.42 macallan sc->sc_brightness->gpc_upd_parameter != NULL);
875 1.42 macallan
876 1.42 macallan (void)sc->sc_brightness->gpc_upd_parameter(
877 1.42 macallan sc->sc_brightness->gpc_cookie, - GENFB_BRIGHTNESS_STEP);
878 1.31 macallan }
879 1.35 jmcneill
880 1.35 jmcneill void
881 1.35 jmcneill genfb_enable_polling(device_t dev)
882 1.35 jmcneill {
883 1.35 jmcneill struct genfb_softc *sc = device_private(dev);
884 1.35 jmcneill
885 1.38 jmcneill if (sc->sc_console_screen.scr_vd) {
886 1.38 jmcneill SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
887 1.38 jmcneill vcons_hard_switch(&sc->sc_console_screen);
888 1.38 jmcneill vcons_enable_polling(&sc->vd);
889 1.55 riastrad if (sc->sc_ops.genfb_enable_polling)
890 1.55 riastrad (*sc->sc_ops.genfb_enable_polling)(sc);
891 1.38 jmcneill }
892 1.35 jmcneill }
893 1.35 jmcneill
894 1.35 jmcneill void
895 1.35 jmcneill genfb_disable_polling(device_t dev)
896 1.35 jmcneill {
897 1.35 jmcneill struct genfb_softc *sc = device_private(dev);
898 1.35 jmcneill
899 1.38 jmcneill if (sc->sc_console_screen.scr_vd) {
900 1.55 riastrad if (sc->sc_ops.genfb_disable_polling)
901 1.55 riastrad (*sc->sc_ops.genfb_disable_polling)(sc);
902 1.38 jmcneill vcons_disable_polling(&sc->vd);
903 1.38 jmcneill }
904 1.35 jmcneill }
905 1.81 macallan
906 1.81 macallan #if GENFB_GLYPHCACHE > 0
907 1.81 macallan #define GLYPHCACHESIZE ((GENFB_GLYPHCACHE) * 1024 * 1024)
908 1.81 macallan
909 1.81 macallan static inline int
910 1.81 macallan attr2idx(long attr)
911 1.81 macallan {
912 1.81 macallan if ((attr & 0xf0f00ff8) != 0)
913 1.81 macallan return -1;
914 1.81 macallan
915 1.81 macallan return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0));
916 1.81 macallan }
917 1.81 macallan
918 1.81 macallan static int
919 1.81 macallan genfb_setup_glyphcache(struct genfb_softc *sc, long defattr)
920 1.81 macallan {
921 1.81 macallan struct rasops_info *ri = &sc->sc_console_screen.scr_ri,
922 1.81 macallan *cri = &sc->sc_cache_ri;
923 1.81 macallan gc_bucket *b;
924 1.81 macallan int i, usedcells = 0, idx, j;
925 1.81 macallan
926 1.81 macallan sc->sc_cache = kmem_alloc(GLYPHCACHESIZE, KM_SLEEP);
927 1.81 macallan
928 1.81 macallan /*
929 1.81 macallan * now we build a mutant rasops_info for the cache - same pixel type
930 1.81 macallan * and such as the real fb, but only one character per line for
931 1.81 macallan * simplicity and locality
932 1.81 macallan */
933 1.81 macallan memcpy(cri, ri, sizeof(struct rasops_info));
934 1.81 macallan cri->ri_ops.putchar = sc->sc_putchar;
935 1.81 macallan cri->ri_width = ri->ri_font->fontwidth;
936 1.81 macallan cri->ri_stride = ri->ri_xscale;
937 1.81 macallan cri->ri_bits = sc->sc_cache;
938 1.81 macallan cri->ri_hwbits = NULL;
939 1.81 macallan cri->ri_origbits = sc->sc_cache;
940 1.81 macallan cri->ri_cols = 1;
941 1.81 macallan cri->ri_rows = GLYPHCACHESIZE /
942 1.81 macallan (cri->ri_stride * cri->ri_font->fontheight);
943 1.81 macallan cri->ri_xorigin = 0;
944 1.81 macallan cri->ri_yorigin = 0;
945 1.81 macallan cri->ri_xscale = ri->ri_xscale;
946 1.81 macallan cri->ri_yscale = ri->ri_font->fontheight * ri->ri_xscale;
947 1.81 macallan
948 1.81 macallan printf("size %d %d %d\n", GLYPHCACHESIZE, ri->ri_width, ri->ri_stride);
949 1.81 macallan printf("cells: %d\n", cri->ri_rows);
950 1.81 macallan sc->sc_nbuckets = uimin(256, cri->ri_rows / 223);
951 1.81 macallan sc->sc_buckets = kmem_alloc(sizeof(gc_bucket) * sc->sc_nbuckets, KM_SLEEP);
952 1.81 macallan printf("buckets: %d\n", sc->sc_nbuckets);
953 1.81 macallan for (i = 0; i < sc->sc_nbuckets; i++) {
954 1.81 macallan b = &sc->sc_buckets[i];
955 1.81 macallan b->gb_firstcell = usedcells;
956 1.81 macallan b->gb_numcells = uimin(223, cri->ri_rows - usedcells);
957 1.81 macallan usedcells += 223;
958 1.81 macallan b->gb_usedcells = 0;
959 1.81 macallan b->gb_index = -1;
960 1.81 macallan for (j = 0; j < 223; j++) b->gb_map[j] = -1;
961 1.81 macallan }
962 1.81 macallan
963 1.81 macallan /* initialize the attribute map... */
964 1.81 macallan for (i = 0; i < 256; i++) {
965 1.81 macallan sc->sc_attrmap[i] = -1;
966 1.81 macallan }
967 1.81 macallan
968 1.81 macallan /* first bucket goes to default attr */
969 1.81 macallan idx = attr2idx(defattr);
970 1.81 macallan printf("defattr %08lx idx %x\n", defattr, idx);
971 1.81 macallan
972 1.81 macallan if (idx >= 0) {
973 1.81 macallan sc->sc_attrmap[idx] = 0;
974 1.81 macallan sc->sc_buckets[0].gb_index = idx;
975 1.81 macallan }
976 1.81 macallan
977 1.81 macallan return 0;
978 1.81 macallan }
979 1.81 macallan
980 1.81 macallan static void
981 1.81 macallan genfb_putchar(void *cookie, int row, int col, u_int c, long attr)
982 1.81 macallan {
983 1.81 macallan struct rasops_info *ri = cookie;
984 1.81 macallan struct vcons_screen *scr = ri->ri_hw;
985 1.81 macallan struct genfb_softc *sc = scr->scr_cookie;
986 1.81 macallan uint8_t *src, *dst;
987 1.81 macallan gc_bucket *b;
988 1.81 macallan int i, idx, bi, cell;
989 1.81 macallan
990 1.81 macallan attr &= ~WSATTR_USERMASK;
991 1.81 macallan
992 1.81 macallan idx = attr2idx(attr);
993 1.81 macallan if (c < 33 || c > 255 || idx < 0) goto nope;
994 1.81 macallan
995 1.81 macallan /* look for a bucket with the right attribute */
996 1.81 macallan bi = sc->sc_attrmap[idx];
997 1.81 macallan if (bi == -1) {
998 1.81 macallan /* nope, see if there's an empty one left */
999 1.81 macallan bi = 1;
1000 1.81 macallan while ((bi < sc->sc_nbuckets) &&
1001 1.81 macallan (sc->sc_buckets[bi].gb_index != -1)) {
1002 1.81 macallan bi++;
1003 1.81 macallan }
1004 1.81 macallan if (bi < sc->sc_nbuckets) {
1005 1.81 macallan /* found one -> grab it */
1006 1.81 macallan sc->sc_attrmap[idx] = bi;
1007 1.81 macallan b = &sc->sc_buckets[bi];
1008 1.81 macallan b->gb_index = idx;
1009 1.81 macallan b->gb_usedcells = 0;
1010 1.81 macallan /* make sure this doesn't get evicted right away */
1011 1.81 macallan b->gb_lastread = time_uptime;
1012 1.81 macallan } else {
1013 1.81 macallan /*
1014 1.81 macallan * still nothing
1015 1.81 macallan * steal the least recently read bucket
1016 1.81 macallan */
1017 1.81 macallan time_t moo = time_uptime;
1018 1.81 macallan int oldest = 1;
1019 1.81 macallan
1020 1.81 macallan for (i = 1; i < sc->sc_nbuckets; i++) {
1021 1.81 macallan if (sc->sc_buckets[i].gb_lastread < moo) {
1022 1.81 macallan oldest = i;
1023 1.81 macallan moo = sc->sc_buckets[i].gb_lastread;
1024 1.81 macallan }
1025 1.81 macallan }
1026 1.81 macallan
1027 1.81 macallan /* if we end up here all buckets must be in use */
1028 1.81 macallan b = &sc->sc_buckets[oldest];
1029 1.81 macallan sc->sc_attrmap[b->gb_index] = -1;
1030 1.81 macallan b->gb_index = idx;
1031 1.81 macallan b->gb_usedcells = 0;
1032 1.81 macallan sc->sc_attrmap[idx] = oldest;
1033 1.81 macallan /* now scrub it */
1034 1.81 macallan for (i = 0; i < 223; i++)
1035 1.81 macallan b->gb_map[i] = -1;
1036 1.81 macallan /* and set the time stamp */
1037 1.81 macallan b->gb_lastread = time_uptime;
1038 1.81 macallan }
1039 1.81 macallan } else {
1040 1.81 macallan /* found one */
1041 1.81 macallan b = &sc->sc_buckets[bi];
1042 1.81 macallan }
1043 1.81 macallan
1044 1.81 macallan /* see if there's room in the bucket */
1045 1.81 macallan if (b->gb_usedcells >= b->gb_numcells) goto nope;
1046 1.81 macallan
1047 1.81 macallan cell = b->gb_map[c - 33];
1048 1.81 macallan if (cell == -1) {
1049 1.81 macallan if (b->gb_usedcells >= b->gb_numcells)
1050 1.81 macallan goto nope;
1051 1.81 macallan cell = atomic_add_int_nv(&b->gb_usedcells, 1) - 1;
1052 1.81 macallan b->gb_map[c - 33] = cell;
1053 1.81 macallan cell += b->gb_firstcell;
1054 1.81 macallan sc->sc_putchar(&sc->sc_cache_ri, cell, 0, c, attr);
1055 1.81 macallan
1056 1.81 macallan } else
1057 1.81 macallan cell += b->gb_firstcell;
1058 1.81 macallan
1059 1.81 macallan src = sc->sc_cache + cell * sc->sc_cache_ri.ri_yscale;
1060 1.81 macallan dst = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
1061 1.81 macallan for (i = 0; i < ri->ri_font->fontheight; i++) {
1062 1.81 macallan memcpy(dst, src, ri->ri_xscale);
1063 1.81 macallan src += ri->ri_xscale;
1064 1.81 macallan dst += ri->ri_stride;
1065 1.81 macallan }
1066 1.81 macallan b->gb_lastread = time_uptime;
1067 1.81 macallan return;
1068 1.81 macallan nope:
1069 1.81 macallan sc->sc_putchar(cookie, row, col, c, attr);
1070 1.81 macallan }
1071 1.81 macallan
1072 1.81 macallan #endif
1073