genfb.c revision 1.90 1 1.90 riastrad /* $NetBSD: genfb.c,v 1.90 2022/08/01 23:30:10 riastradh 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.90 riastrad __KERNEL_RCSID(0, "$NetBSD: genfb.c,v 1.90 2022/08/01 23:30:10 riastradh 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.89 riastrad struct genfb_private {
79 1.89 riastrad struct genfb_ops sc_ops;
80 1.89 riastrad struct vcons_screen sc_console_screen;
81 1.89 riastrad struct wsscreen_descr sc_defaultscreen_descr;
82 1.89 riastrad const struct wsscreen_descr *sc_screens[1];
83 1.89 riastrad struct wsscreen_list sc_screenlist;
84 1.89 riastrad struct genfb_colormap_callback *sc_cmcb;
85 1.89 riastrad struct genfb_parameter_callback *sc_backlight;
86 1.89 riastrad struct genfb_parameter_callback *sc_brightness;
87 1.89 riastrad struct genfb_mode_callback *sc_modecb;
88 1.89 riastrad int sc_backlight_level, sc_backlight_on;
89 1.89 riastrad void *sc_shadowfb;
90 1.89 riastrad bool sc_enable_shadowfb;
91 1.89 riastrad int sc_mode;
92 1.89 riastrad u_char sc_cmap_red[256];
93 1.89 riastrad u_char sc_cmap_green[256];
94 1.89 riastrad u_char sc_cmap_blue[256];
95 1.89 riastrad bool sc_want_clear;
96 1.89 riastrad #ifdef SPLASHSCREEN
97 1.89 riastrad struct splash_info sc_splash;
98 1.89 riastrad #endif
99 1.89 riastrad struct wsdisplay_accessops sc_accessops;
100 1.89 riastrad #if GENFB_GLYPHCACHE > 0
101 1.89 riastrad /*
102 1.89 riastrad * The generic glyphcache code makes a bunch of assumptions that are
103 1.89 riastrad * true for most graphics hardware with a directly supported blitter.
104 1.89 riastrad * For example it assume that
105 1.89 riastrad * - VRAM access from the host is expensive
106 1.89 riastrad * - copying data around in VRAM is cheap and can happen in parallel
107 1.89 riastrad * to the host CPU
108 1.89 riastrad * -> therefore we draw glyphs normally if we have to, so the ( assumed
109 1.89 riastrad * to be hardware assisted ) driver supplied putchar() method doesn't
110 1.89 riastrad * need to be glyphcache aware, then copy them away for later use
111 1.89 riastrad * for genfb things are a bit different. On most hardware:
112 1.89 riastrad * - VRAM access from the host is still expensive
113 1.89 riastrad * - copying data around in VRAM is also expensive since we don't have
114 1.89 riastrad * a blitter and VRAM is mapped uncached
115 1.89 riastrad * - VRAM reads are usually slower than writes ( write combining and
116 1.89 riastrad * such help writes but not reads, and VRAM might be behind an
117 1.89 riastrad * asymmetric bus like AGP ) and must be avoided, both are much
118 1.89 riastrad * slower than main memory
119 1.89 riastrad * -> therefore we cache glyphs in main memory, no reason to map it
120 1.89 riastrad * uncached, we draw into the cache first and then copy the glyph
121 1.89 riastrad * into video memory to avoid framebuffer reads and to allow more
122 1.89 riastrad * efficient write accesses than putchar() would offer
123 1.89 riastrad * Because of this we can't use the generic code but we can recycle a
124 1.89 riastrad * few data structures.
125 1.89 riastrad */
126 1.89 riastrad uint8_t *sc_cache;
127 1.89 riastrad struct rasops_info sc_cache_ri;
128 1.89 riastrad void (*sc_putchar)(void *, int, int, u_int, long);
129 1.89 riastrad int sc_cache_cells;
130 1.89 riastrad int sc_nbuckets; /* buckets allocated */
131 1.89 riastrad gc_bucket *sc_buckets; /* we allocate as many as we can get into ram */
132 1.89 riastrad int sc_attrmap[256]; /* mapping a colour attribute to a bucket */
133 1.89 riastrad #endif
134 1.89 riastrad };
135 1.89 riastrad
136 1.1 macallan static int genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
137 1.1 macallan static paddr_t genfb_mmap(void *, void *, off_t, int);
138 1.52 skrll static void genfb_pollc(void *, int);
139 1.52 skrll
140 1.1 macallan static void genfb_init_screen(void *, struct vcons_screen *, int, long *);
141 1.65 jmcneill static int genfb_calc_hsize(struct genfb_softc *);
142 1.85 jmcneill static int genfb_calc_cols(struct genfb_softc *, struct rasops_info *);
143 1.1 macallan
144 1.1 macallan static int genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
145 1.1 macallan static int genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
146 1.1 macallan static int genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
147 1.1 macallan uint8_t, uint8_t);
148 1.45 macallan static void genfb_init_palette(struct genfb_softc *);
149 1.1 macallan
150 1.31 macallan static void genfb_brightness_up(device_t);
151 1.31 macallan static void genfb_brightness_down(device_t);
152 1.31 macallan
153 1.81 macallan #if GENFB_GLYPHCACHE > 0
154 1.81 macallan static int genfb_setup_glyphcache(struct genfb_softc *, long);
155 1.81 macallan static void genfb_putchar(void *, int, int, u_int, long);
156 1.81 macallan #endif
157 1.81 macallan
158 1.1 macallan extern const u_char rasops_cmap[768];
159 1.1 macallan
160 1.18 jmcneill static int genfb_cnattach_called = 0;
161 1.20 jmcneill static int genfb_enabled = 1;
162 1.18 jmcneill
163 1.19 jmcneill static struct genfb_softc *genfb_softc = NULL;
164 1.19 jmcneill
165 1.2 macallan void
166 1.2 macallan genfb_init(struct genfb_softc *sc)
167 1.2 macallan {
168 1.89 riastrad struct genfb_private *scp;
169 1.2 macallan prop_dictionary_t dict;
170 1.42 macallan uint64_t cmap_cb, pmf_cb, mode_cb, bl_cb, br_cb, fbaddr;
171 1.72 msaitoh uint64_t fboffset;
172 1.31 macallan bool console;
173 1.2 macallan
174 1.89 riastrad scp = sc->sc_private = kmem_zalloc(sizeof(*sc->sc_private), KM_SLEEP);
175 1.89 riastrad
176 1.32 macallan dict = device_properties(sc->sc_dev);
177 1.2 macallan #ifdef GENFB_DEBUG
178 1.54 riastrad printf("%s", prop_dictionary_externalize(dict));
179 1.2 macallan #endif
180 1.31 macallan prop_dictionary_get_bool(dict, "is_console", &console);
181 1.31 macallan
182 1.11 macallan if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
183 1.12 macallan GPRINTF("no width property\n");
184 1.89 riastrad goto bad;
185 1.11 macallan }
186 1.11 macallan if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
187 1.12 macallan GPRINTF("no height property\n");
188 1.89 riastrad goto bad;
189 1.11 macallan }
190 1.11 macallan if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
191 1.12 macallan GPRINTF("no depth property\n");
192 1.89 riastrad goto bad;
193 1.11 macallan }
194 1.2 macallan
195 1.72 msaitoh if (!prop_dictionary_get_uint64(dict, "address", &fboffset)) {
196 1.12 macallan GPRINTF("no address property\n");
197 1.89 riastrad goto bad;
198 1.11 macallan }
199 1.11 macallan
200 1.72 msaitoh sc->sc_fboffset = (bus_addr_t)fboffset;
201 1.2 macallan
202 1.39 macallan sc->sc_fbaddr = NULL;
203 1.39 macallan if (prop_dictionary_get_uint64(dict, "virtual_address", &fbaddr)) {
204 1.40 macallan sc->sc_fbaddr = (void *)(uintptr_t)fbaddr;
205 1.39 macallan }
206 1.39 macallan
207 1.89 riastrad scp->sc_shadowfb = NULL;
208 1.59 nonaka if (!prop_dictionary_get_bool(dict, "enable_shadowfb",
209 1.89 riastrad &scp->sc_enable_shadowfb))
210 1.59 nonaka #ifdef GENFB_SHADOWFB
211 1.89 riastrad scp->sc_enable_shadowfb = true;
212 1.59 nonaka #else
213 1.89 riastrad scp->sc_enable_shadowfb = false;
214 1.59 nonaka #endif
215 1.59 nonaka
216 1.2 macallan if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
217 1.2 macallan sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
218 1.13 macallan
219 1.13 macallan /*
220 1.13 macallan * deal with a bug in the Raptor firmware which always sets
221 1.13 macallan * stride = width even when depth != 8
222 1.13 macallan */
223 1.13 macallan if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3))
224 1.13 macallan sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
225 1.13 macallan
226 1.6 macallan sc->sc_fbsize = sc->sc_height * sc->sc_stride;
227 1.4 macallan
228 1.4 macallan /* optional colour map callback */
229 1.89 riastrad scp->sc_cmcb = NULL;
230 1.4 macallan if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
231 1.4 macallan if (cmap_cb != 0)
232 1.89 riastrad scp->sc_cmcb = (void *)(vaddr_t)cmap_cb;
233 1.4 macallan }
234 1.31 macallan
235 1.28 jmcneill /* optional pmf callback */
236 1.28 jmcneill sc->sc_pmfcb = NULL;
237 1.28 jmcneill if (prop_dictionary_get_uint64(dict, "pmf_callback", &pmf_cb)) {
238 1.28 jmcneill if (pmf_cb != 0)
239 1.28 jmcneill sc->sc_pmfcb = (void *)(vaddr_t)pmf_cb;
240 1.28 jmcneill }
241 1.31 macallan
242 1.34 jmcneill /* optional mode callback */
243 1.89 riastrad scp->sc_modecb = NULL;
244 1.34 jmcneill if (prop_dictionary_get_uint64(dict, "mode_callback", &mode_cb)) {
245 1.34 jmcneill if (mode_cb != 0)
246 1.89 riastrad scp->sc_modecb = (void *)(vaddr_t)mode_cb;
247 1.34 jmcneill }
248 1.34 jmcneill
249 1.31 macallan /* optional backlight control callback */
250 1.89 riastrad scp->sc_backlight = NULL;
251 1.31 macallan if (prop_dictionary_get_uint64(dict, "backlight_callback", &bl_cb)) {
252 1.31 macallan if (bl_cb != 0) {
253 1.89 riastrad scp->sc_backlight = (void *)(vaddr_t)bl_cb;
254 1.42 macallan aprint_naive_dev(sc->sc_dev,
255 1.31 macallan "enabling backlight control\n");
256 1.42 macallan }
257 1.42 macallan }
258 1.42 macallan
259 1.42 macallan /* optional brightness control callback */
260 1.89 riastrad scp->sc_brightness = NULL;
261 1.42 macallan if (prop_dictionary_get_uint64(dict, "brightness_callback", &br_cb)) {
262 1.42 macallan if (br_cb != 0) {
263 1.89 riastrad scp->sc_brightness = (void *)(vaddr_t)br_cb;
264 1.42 macallan aprint_naive_dev(sc->sc_dev,
265 1.42 macallan "enabling brightness control\n");
266 1.42 macallan if (console &&
267 1.89 riastrad scp->sc_brightness->gpc_upd_parameter != NULL) {
268 1.42 macallan pmf_event_register(sc->sc_dev,
269 1.31 macallan PMFE_DISPLAY_BRIGHTNESS_UP,
270 1.31 macallan genfb_brightness_up, TRUE);
271 1.42 macallan pmf_event_register(sc->sc_dev,
272 1.31 macallan PMFE_DISPLAY_BRIGHTNESS_DOWN,
273 1.31 macallan genfb_brightness_down, TRUE);
274 1.31 macallan }
275 1.31 macallan }
276 1.31 macallan }
277 1.89 riastrad
278 1.89 riastrad return;
279 1.89 riastrad
280 1.89 riastrad bad: kmem_free(sc->sc_private, sizeof(*sc->sc_private));
281 1.89 riastrad sc->sc_private = NULL;
282 1.2 macallan }
283 1.2 macallan
284 1.2 macallan int
285 1.2 macallan genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
286 1.1 macallan {
287 1.89 riastrad struct genfb_private *scp = sc->sc_private;
288 1.1 macallan struct wsemuldisplaydev_attach_args aa;
289 1.1 macallan prop_dictionary_t dict;
290 1.1 macallan struct rasops_info *ri;
291 1.79 jmcneill paddr_t fb_phys;
292 1.21 jmcneill uint16_t crow;
293 1.1 macallan long defattr;
294 1.7 macallan bool console;
295 1.33 jmcneill #ifdef SPLASHSCREEN
296 1.45 macallan int i, j;
297 1.33 jmcneill int error = ENXIO;
298 1.33 jmcneill #endif
299 1.9 jmmv
300 1.89 riastrad KASSERTMSG(scp != NULL, "missing genfb_init");
301 1.89 riastrad
302 1.32 macallan dict = device_properties(sc->sc_dev);
303 1.14 phx prop_dictionary_get_bool(dict, "is_console", &console);
304 1.14 phx
305 1.21 jmcneill if (prop_dictionary_get_uint16(dict, "cursor-row", &crow) == false)
306 1.21 jmcneill crow = 0;
307 1.89 riastrad if (prop_dictionary_get_bool(dict, "clear-screen", &scp->sc_want_clear)
308 1.21 jmcneill == false)
309 1.89 riastrad scp->sc_want_clear = true;
310 1.21 jmcneill
311 1.79 jmcneill fb_phys = (paddr_t)sc->sc_fboffset;
312 1.79 jmcneill if (fb_phys == 0) {
313 1.79 jmcneill KASSERT(sc->sc_fbaddr != NULL);
314 1.79 jmcneill (void)pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_fbaddr,
315 1.79 jmcneill &fb_phys);
316 1.79 jmcneill }
317 1.79 jmcneill
318 1.42 macallan aprint_verbose_dev(sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
319 1.42 macallan "stride %d\n",
320 1.79 jmcneill fb_phys ? (void *)(intptr_t)fb_phys : sc->sc_fbaddr,
321 1.9 jmmv sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
322 1.9 jmmv
323 1.89 riastrad scp->sc_defaultscreen_descr = (struct wsscreen_descr){
324 1.1 macallan "default",
325 1.1 macallan 0, 0,
326 1.1 macallan NULL,
327 1.1 macallan 8, 16,
328 1.60 macallan WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
329 1.60 macallan WSSCREEN_RESIZE,
330 1.3 macallan NULL
331 1.1 macallan };
332 1.89 riastrad scp->sc_screens[0] = &scp->sc_defaultscreen_descr;
333 1.89 riastrad scp->sc_screenlist = (struct wsscreen_list){1, scp->sc_screens};
334 1.89 riastrad memcpy(&scp->sc_ops, ops, sizeof(struct genfb_ops));
335 1.89 riastrad scp->sc_mode = WSDISPLAYIO_MODE_EMUL;
336 1.89 riastrad if (scp->sc_modecb != NULL)
337 1.89 riastrad scp->sc_modecb->gmc_setmode(sc, scp->sc_mode);
338 1.89 riastrad
339 1.89 riastrad scp->sc_accessops.ioctl = genfb_ioctl;
340 1.89 riastrad scp->sc_accessops.mmap = genfb_mmap;
341 1.89 riastrad scp->sc_accessops.pollc = genfb_pollc;
342 1.89 riastrad
343 1.89 riastrad if (scp->sc_enable_shadowfb) {
344 1.89 riastrad scp->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP);
345 1.89 riastrad if (scp->sc_want_clear == false) {
346 1.89 riastrad memcpy(scp->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize);
347 1.89 riastrad }
348 1.68 rin aprint_verbose_dev(sc->sc_dev,
349 1.68 rin "shadow framebuffer enabled, size %zu KB\n",
350 1.68 rin sc->sc_fbsize >> 10);
351 1.59 nonaka }
352 1.5 macallan
353 1.89 riastrad vcons_init(&sc->vd, sc, &scp->sc_defaultscreen_descr,
354 1.89 riastrad &scp->sc_accessops);
355 1.1 macallan sc->vd.init_screen = genfb_init_screen;
356 1.1 macallan
357 1.10 jmmv /* Do not print anything between this point and the screen
358 1.10 jmmv * clear operation below. Otherwise it will be lost. */
359 1.10 jmmv
360 1.89 riastrad ri = &scp->sc_console_screen.scr_ri;
361 1.1 macallan
362 1.89 riastrad vcons_init_screen(&sc->vd, &scp->sc_console_screen, 1,
363 1.14 phx &defattr);
364 1.89 riastrad scp->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
365 1.14 phx
366 1.81 macallan #if GENFB_GLYPHCACHE > 0
367 1.81 macallan genfb_setup_glyphcache(sc, defattr);
368 1.81 macallan #endif
369 1.81 macallan
370 1.29 ahoka #ifdef SPLASHSCREEN
371 1.29 ahoka /*
372 1.29 ahoka * If system isn't going to go multiuser, or user has requested to see
373 1.29 ahoka * boot text, don't render splash screen immediately
374 1.29 ahoka */
375 1.29 ahoka if (DISABLESPLASH)
376 1.29 ahoka #endif
377 1.89 riastrad vcons_redraw_screen(&scp->sc_console_screen);
378 1.29 ahoka
379 1.89 riastrad scp->sc_defaultscreen_descr.textops = &ri->ri_ops;
380 1.89 riastrad scp->sc_defaultscreen_descr.capabilities = ri->ri_caps;
381 1.89 riastrad scp->sc_defaultscreen_descr.nrows = ri->ri_rows;
382 1.89 riastrad scp->sc_defaultscreen_descr.ncols = ri->ri_cols;
383 1.41 macallan
384 1.44 macallan if (crow >= ri->ri_rows) {
385 1.44 macallan crow = 0;
386 1.89 riastrad scp->sc_want_clear = 1;
387 1.44 macallan }
388 1.44 macallan
389 1.41 macallan if (console)
390 1.89 riastrad wsdisplay_cnattach(&scp->sc_defaultscreen_descr, ri, 0, crow,
391 1.41 macallan defattr);
392 1.1 macallan
393 1.10 jmmv /* Clear the whole screen to bring it to a known state. */
394 1.89 riastrad if (scp->sc_want_clear)
395 1.21 jmcneill (*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
396 1.10 jmmv
397 1.45 macallan #ifdef SPLASHSCREEN
398 1.1 macallan j = 0;
399 1.64 riastrad for (i = 0; i < uimin(1 << sc->sc_depth, 256); i++) {
400 1.33 jmcneill if (i >= SPLASH_CMAP_OFFSET &&
401 1.29 ahoka i < SPLASH_CMAP_OFFSET + SPLASH_CMAP_SIZE) {
402 1.33 jmcneill splash_get_cmap(i,
403 1.89 riastrad &scp->sc_cmap_red[i],
404 1.89 riastrad &scp->sc_cmap_green[i],
405 1.89 riastrad &scp->sc_cmap_blue[i]);
406 1.29 ahoka } else {
407 1.89 riastrad scp->sc_cmap_red[i] = rasops_cmap[j];
408 1.89 riastrad scp->sc_cmap_green[i] = rasops_cmap[j + 1];
409 1.89 riastrad scp->sc_cmap_blue[i] = rasops_cmap[j + 2];
410 1.29 ahoka }
411 1.29 ahoka j += 3;
412 1.1 macallan }
413 1.33 jmcneill genfb_restore_palette(sc);
414 1.1 macallan
415 1.89 riastrad scp->sc_splash.si_depth = sc->sc_depth;
416 1.89 riastrad scp->sc_splash.si_bits = scp->sc_console_screen.scr_ri.ri_origbits;
417 1.89 riastrad scp->sc_splash.si_hwbits = sc->sc_fbaddr;
418 1.89 riastrad scp->sc_splash.si_width = sc->sc_width;
419 1.89 riastrad scp->sc_splash.si_height = sc->sc_height;
420 1.89 riastrad scp->sc_splash.si_stride = sc->sc_stride;
421 1.89 riastrad scp->sc_splash.si_fillrect = NULL;
422 1.33 jmcneill if (!DISABLESPLASH) {
423 1.89 riastrad error = splash_render(&scp->sc_splash,
424 1.33 jmcneill SPLASH_F_CENTER|SPLASH_F_FILL);
425 1.33 jmcneill if (error) {
426 1.89 riastrad SCREEN_ENABLE_DRAWING(&scp->sc_console_screen);
427 1.45 macallan genfb_init_palette(sc);
428 1.89 riastrad vcons_replay_msgbuf(&scp->sc_console_screen);
429 1.33 jmcneill }
430 1.33 jmcneill }
431 1.29 ahoka #else
432 1.45 macallan genfb_init_palette(sc);
433 1.71 martin if (console && (boothowto & (AB_SILENT|AB_QUIET)) == 0)
434 1.89 riastrad vcons_replay_msgbuf(&scp->sc_console_screen);
435 1.29 ahoka #endif
436 1.27 macallan
437 1.19 jmcneill if (genfb_softc == NULL)
438 1.19 jmcneill genfb_softc = sc;
439 1.19 jmcneill
440 1.1 macallan aa.console = console;
441 1.89 riastrad aa.scrdata = &scp->sc_screenlist;
442 1.89 riastrad aa.accessops = &scp->sc_accessops;
443 1.1 macallan aa.accesscookie = &sc->vd;
444 1.1 macallan
445 1.29 ahoka #ifdef GENFB_DISABLE_TEXT
446 1.33 jmcneill if (!DISABLESPLASH && error == 0)
447 1.89 riastrad SCREEN_DISABLE_DRAWING(&scp->sc_console_screen);
448 1.29 ahoka #endif
449 1.29 ahoka
450 1.82 thorpej config_found(sc->sc_dev, &aa, wsemuldisplaydevprint,
451 1.83 thorpej CFARGS(.iattr = "wsemuldisplaydev"));
452 1.1 macallan
453 1.1 macallan return 0;
454 1.1 macallan }
455 1.1 macallan
456 1.1 macallan static int
457 1.1 macallan genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
458 1.1 macallan struct lwp *l)
459 1.1 macallan {
460 1.1 macallan struct vcons_data *vd = v;
461 1.1 macallan struct genfb_softc *sc = vd->cookie;
462 1.89 riastrad struct genfb_private *scp = sc->sc_private;
463 1.1 macallan struct wsdisplay_fbinfo *wdf;
464 1.1 macallan struct vcons_screen *ms = vd->active;
465 1.31 macallan struct wsdisplay_param *param;
466 1.56 macallan int new_mode, error, val, ret;
467 1.1 macallan
468 1.1 macallan switch (cmd) {
469 1.1 macallan case WSDISPLAYIO_GINFO:
470 1.6 macallan if (ms == NULL)
471 1.6 macallan return ENODEV;
472 1.1 macallan wdf = (void *)data;
473 1.1 macallan wdf->height = ms->scr_ri.ri_height;
474 1.1 macallan wdf->width = ms->scr_ri.ri_width;
475 1.1 macallan wdf->depth = ms->scr_ri.ri_depth;
476 1.1 macallan wdf->cmsize = 256;
477 1.1 macallan return 0;
478 1.1 macallan
479 1.1 macallan case WSDISPLAYIO_GETCMAP:
480 1.1 macallan return genfb_getcmap(sc,
481 1.1 macallan (struct wsdisplay_cmap *)data);
482 1.1 macallan
483 1.1 macallan case WSDISPLAYIO_PUTCMAP:
484 1.1 macallan return genfb_putcmap(sc,
485 1.1 macallan (struct wsdisplay_cmap *)data);
486 1.1 macallan
487 1.3 macallan case WSDISPLAYIO_LINEBYTES:
488 1.3 macallan *(u_int *)data = sc->sc_stride;
489 1.3 macallan return 0;
490 1.3 macallan
491 1.1 macallan case WSDISPLAYIO_SMODE:
492 1.24 jmcneill new_mode = *(int *)data;
493 1.11 macallan
494 1.24 jmcneill /* notify the bus backend */
495 1.24 jmcneill error = 0;
496 1.89 riastrad if (scp->sc_ops.genfb_ioctl) {
497 1.89 riastrad error = scp->sc_ops.genfb_ioctl(sc, vs,
498 1.89 riastrad cmd, data, flag, l);
499 1.89 riastrad }
500 1.50 jmcneill if (error && error != EPASSTHROUGH)
501 1.24 jmcneill return error;
502 1.11 macallan
503 1.89 riastrad if (new_mode != scp->sc_mode) {
504 1.89 riastrad scp->sc_mode = new_mode;
505 1.89 riastrad if (scp->sc_modecb != NULL) {
506 1.89 riastrad scp->sc_modecb->gmc_setmode(sc,
507 1.89 riastrad scp->sc_mode);
508 1.89 riastrad }
509 1.24 jmcneill if (new_mode == WSDISPLAYIO_MODE_EMUL) {
510 1.24 jmcneill genfb_restore_palette(sc);
511 1.24 jmcneill vcons_redraw_screen(ms);
512 1.1 macallan }
513 1.1 macallan }
514 1.1 macallan return 0;
515 1.89 riastrad
516 1.29 ahoka case WSDISPLAYIO_SSPLASH:
517 1.29 ahoka #if defined(SPLASHSCREEN)
518 1.29 ahoka if(*(int *)data == 1) {
519 1.89 riastrad SCREEN_DISABLE_DRAWING(&scp->sc_console_screen);
520 1.89 riastrad splash_render(&scp->sc_splash,
521 1.89 riastrad SPLASH_F_CENTER|SPLASH_F_FILL);
522 1.29 ahoka } else {
523 1.89 riastrad SCREEN_ENABLE_DRAWING(&scp->sc_console_screen);
524 1.45 macallan genfb_init_palette(sc);
525 1.29 ahoka }
526 1.29 ahoka vcons_redraw_screen(ms);
527 1.29 ahoka return 0;
528 1.29 ahoka #else
529 1.29 ahoka return ENODEV;
530 1.29 ahoka #endif
531 1.31 macallan case WSDISPLAYIO_GETPARAM:
532 1.31 macallan param = (struct wsdisplay_param *)data;
533 1.31 macallan switch (param->param) {
534 1.31 macallan case WSDISPLAYIO_PARAM_BRIGHTNESS:
535 1.89 riastrad if (scp->sc_brightness == NULL)
536 1.42 macallan return EPASSTHROUGH;
537 1.31 macallan param->min = 0;
538 1.31 macallan param->max = 255;
539 1.89 riastrad return scp->sc_brightness->gpc_get_parameter(
540 1.89 riastrad scp->sc_brightness->gpc_cookie,
541 1.42 macallan ¶m->curval);
542 1.31 macallan case WSDISPLAYIO_PARAM_BACKLIGHT:
543 1.89 riastrad if (scp->sc_backlight == NULL)
544 1.42 macallan return EPASSTHROUGH;
545 1.31 macallan param->min = 0;
546 1.31 macallan param->max = 1;
547 1.89 riastrad return scp->sc_backlight->gpc_get_parameter(
548 1.89 riastrad scp->sc_backlight->gpc_cookie,
549 1.42 macallan ¶m->curval);
550 1.31 macallan }
551 1.31 macallan return EPASSTHROUGH;
552 1.31 macallan
553 1.31 macallan case WSDISPLAYIO_SETPARAM:
554 1.31 macallan param = (struct wsdisplay_param *)data;
555 1.31 macallan switch (param->param) {
556 1.31 macallan case WSDISPLAYIO_PARAM_BRIGHTNESS:
557 1.89 riastrad if (scp->sc_brightness == NULL)
558 1.42 macallan return EPASSTHROUGH;
559 1.42 macallan val = param->curval;
560 1.42 macallan if (val < 0) val = 0;
561 1.42 macallan if (val > 255) val = 255;
562 1.89 riastrad return scp->sc_brightness->gpc_set_parameter(
563 1.89 riastrad scp->sc_brightness->gpc_cookie, val);
564 1.31 macallan case WSDISPLAYIO_PARAM_BACKLIGHT:
565 1.89 riastrad if (scp->sc_backlight == NULL)
566 1.42 macallan return EPASSTHROUGH;
567 1.42 macallan val = param->curval;
568 1.42 macallan if (val < 0) val = 0;
569 1.42 macallan if (val > 1) val = 1;
570 1.89 riastrad return scp->sc_backlight->gpc_set_parameter(
571 1.89 riastrad scp->sc_backlight->gpc_cookie, val);
572 1.31 macallan }
573 1.89 riastrad return EPASSTHROUGH;
574 1.56 macallan }
575 1.56 macallan ret = EPASSTHROUGH;
576 1.89 riastrad if (scp->sc_ops.genfb_ioctl)
577 1.89 riastrad ret = scp->sc_ops.genfb_ioctl(sc, vs, cmd, data, flag, l);
578 1.56 macallan if (ret != EPASSTHROUGH)
579 1.56 macallan return ret;
580 1.56 macallan /*
581 1.56 macallan * XXX
582 1.56 macallan * handle these only if there either is no ioctl() handler or it didn't
583 1.56 macallan * know how to deal with them. This allows bus frontends to override
584 1.56 macallan * them but still provides fallback implementations
585 1.56 macallan */
586 1.56 macallan switch (cmd) {
587 1.42 macallan case WSDISPLAYIO_GET_EDID: {
588 1.42 macallan struct wsdisplayio_edid_info *d = data;
589 1.42 macallan return wsdisplayio_get_edid(sc->sc_dev, d);
590 1.42 macallan }
591 1.51 macallan case WSDISPLAYIO_GET_FBINFO: {
592 1.51 macallan struct wsdisplayio_fbinfo *fbi = data;
593 1.77 rin return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
594 1.51 macallan }
595 1.1 macallan }
596 1.1 macallan return EPASSTHROUGH;
597 1.1 macallan }
598 1.1 macallan
599 1.1 macallan static paddr_t
600 1.1 macallan genfb_mmap(void *v, void *vs, off_t offset, int prot)
601 1.1 macallan {
602 1.1 macallan struct vcons_data *vd = v;
603 1.1 macallan struct genfb_softc *sc = vd->cookie;
604 1.89 riastrad struct genfb_private *scp = sc->sc_private;
605 1.1 macallan
606 1.89 riastrad if (scp->sc_ops.genfb_mmap)
607 1.89 riastrad return scp->sc_ops.genfb_mmap(sc, vs, offset, prot);
608 1.1 macallan
609 1.1 macallan return -1;
610 1.1 macallan }
611 1.1 macallan
612 1.1 macallan static void
613 1.52 skrll genfb_pollc(void *v, int on)
614 1.52 skrll {
615 1.52 skrll struct vcons_data *vd = v;
616 1.52 skrll struct genfb_softc *sc = vd->cookie;
617 1.52 skrll
618 1.52 skrll if (sc == NULL)
619 1.52 skrll return;
620 1.52 skrll
621 1.52 skrll if (on)
622 1.52 skrll genfb_enable_polling(sc->sc_dev);
623 1.52 skrll else
624 1.52 skrll genfb_disable_polling(sc->sc_dev);
625 1.52 skrll }
626 1.52 skrll
627 1.52 skrll static void
628 1.1 macallan genfb_init_screen(void *cookie, struct vcons_screen *scr,
629 1.1 macallan int existing, long *defattr)
630 1.1 macallan {
631 1.1 macallan struct genfb_softc *sc = cookie;
632 1.89 riastrad struct genfb_private *scp = sc->sc_private;
633 1.1 macallan struct rasops_info *ri = &scr->scr_ri;
634 1.65 jmcneill int wantcols;
635 1.84 jmcneill bool is_bgr, is_swapped, is_10bit;
636 1.1 macallan
637 1.1 macallan ri->ri_depth = sc->sc_depth;
638 1.1 macallan ri->ri_width = sc->sc_width;
639 1.1 macallan ri->ri_height = sc->sc_height;
640 1.1 macallan ri->ri_stride = sc->sc_stride;
641 1.21 jmcneill ri->ri_flg = RI_CENTER;
642 1.89 riastrad if (scp->sc_want_clear)
643 1.21 jmcneill ri->ri_flg |= RI_FULLCLEAR;
644 1.1 macallan
645 1.60 macallan scr->scr_flags |= VCONS_LOADFONT;
646 1.60 macallan
647 1.89 riastrad if (scp->sc_shadowfb != NULL) {
648 1.5 macallan ri->ri_hwbits = (char *)sc->sc_fbaddr;
649 1.89 riastrad ri->ri_bits = (char *)scp->sc_shadowfb;
650 1.59 nonaka } else {
651 1.5 macallan ri->ri_bits = (char *)sc->sc_fbaddr;
652 1.30 macallan scr->scr_flags |= VCONS_DONT_READ;
653 1.30 macallan }
654 1.1 macallan
655 1.89 riastrad if (existing && scp->sc_want_clear)
656 1.1 macallan ri->ri_flg |= RI_CLEAR;
657 1.1 macallan
658 1.69 rin switch (ri->ri_depth) {
659 1.69 rin case 32:
660 1.69 rin case 24:
661 1.67 rin ri->ri_flg |= RI_ENABLE_ALPHA;
662 1.67 rin
663 1.70 rin is_bgr = false;
664 1.48 macallan prop_dictionary_get_bool(device_properties(sc->sc_dev),
665 1.48 macallan "is_bgr", &is_bgr);
666 1.78 rin
667 1.78 rin is_swapped = false;
668 1.78 rin prop_dictionary_get_bool(device_properties(sc->sc_dev),
669 1.78 rin "is_swapped", &is_swapped);
670 1.78 rin
671 1.84 jmcneill is_10bit = false;
672 1.84 jmcneill prop_dictionary_get_bool(device_properties(sc->sc_dev),
673 1.84 jmcneill "is_10bit", &is_10bit);
674 1.84 jmcneill
675 1.84 jmcneill const int bits = is_10bit ? 10 : 8;
676 1.84 jmcneill ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = bits;
677 1.84 jmcneill
678 1.48 macallan if (is_bgr) {
679 1.48 macallan /* someone requested BGR */
680 1.84 jmcneill ri->ri_rpos = bits * 0;
681 1.84 jmcneill ri->ri_gpos = bits * 1;
682 1.84 jmcneill ri->ri_bpos = bits * 2;
683 1.78 rin } else if (is_swapped) {
684 1.78 rin /* byte-swapped, must be 32 bpp */
685 1.88 riastrad KASSERT(ri->ri_depth == 32);
686 1.88 riastrad KASSERT(bits == 8);
687 1.78 rin ri->ri_rpos = 8;
688 1.78 rin ri->ri_gpos = 16;
689 1.78 rin ri->ri_bpos = 24;
690 1.48 macallan } else {
691 1.48 macallan /* assume RGB */
692 1.84 jmcneill ri->ri_rpos = bits * 2;
693 1.84 jmcneill ri->ri_gpos = bits * 1;
694 1.84 jmcneill ri->ri_bpos = bits * 0;
695 1.48 macallan }
696 1.69 rin break;
697 1.69 rin
698 1.69 rin case 16:
699 1.69 rin case 15:
700 1.69 rin ri->ri_flg |= RI_ENABLE_ALPHA;
701 1.69 rin break;
702 1.69 rin
703 1.69 rin case 8:
704 1.89 riastrad if (scp->sc_cmcb != NULL)
705 1.69 rin ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB;
706 1.69 rin break;
707 1.43 macallan
708 1.69 rin case 2:
709 1.66 rin ri->ri_flg |= RI_ENABLE_ALPHA;
710 1.69 rin break;
711 1.66 rin
712 1.69 rin default:
713 1.69 rin break;
714 1.69 rin }
715 1.45 macallan
716 1.85 jmcneill wantcols = genfb_calc_cols(sc, ri);
717 1.45 macallan
718 1.65 jmcneill rasops_init(ri, 0, wantcols);
719 1.60 macallan ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
720 1.89 riastrad WSSCREEN_RESIZE;
721 1.1 macallan rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
722 1.89 riastrad sc->sc_width / ri->ri_font->fontwidth);
723 1.1 macallan
724 1.1 macallan ri->ri_hw = scr;
725 1.81 macallan #if GENFB_GLYPHCACHE > 0
726 1.89 riastrad scp->sc_putchar = ri->ri_ops.putchar;
727 1.81 macallan ri->ri_ops.putchar = genfb_putchar;
728 1.81 macallan #endif
729 1.29 ahoka #ifdef GENFB_DISABLE_TEXT
730 1.89 riastrad if (scr == &scp->sc_console_screen && !DISABLESPLASH)
731 1.89 riastrad SCREEN_DISABLE_DRAWING(&scp->sc_console_screen);
732 1.29 ahoka #endif
733 1.1 macallan }
734 1.1 macallan
735 1.65 jmcneill /* Returns the width of the display in millimeters, or 0 if not known. */
736 1.65 jmcneill static int
737 1.65 jmcneill genfb_calc_hsize(struct genfb_softc *sc)
738 1.65 jmcneill {
739 1.65 jmcneill device_t dev = sc->sc_dev;
740 1.65 jmcneill prop_dictionary_t dict = device_properties(dev);
741 1.65 jmcneill prop_data_t edid_data;
742 1.73 jdolecek struct edid_info *edid;
743 1.65 jmcneill const char *edid_ptr;
744 1.73 jdolecek int hsize;
745 1.65 jmcneill
746 1.65 jmcneill edid_data = prop_dictionary_get(dict, "EDID");
747 1.65 jmcneill if (edid_data == NULL || prop_data_size(edid_data) < 128)
748 1.65 jmcneill return 0;
749 1.65 jmcneill
750 1.73 jdolecek edid = kmem_alloc(sizeof(*edid), KM_SLEEP);
751 1.73 jdolecek
752 1.74 thorpej edid_ptr = prop_data_value(edid_data);
753 1.73 jdolecek if (edid_parse(__UNCONST(edid_ptr), edid) == 0)
754 1.73 jdolecek hsize = (int)edid->edid_max_hsize * 10;
755 1.73 jdolecek else
756 1.73 jdolecek hsize = 0;
757 1.73 jdolecek
758 1.73 jdolecek kmem_free(edid, sizeof(*edid));
759 1.65 jmcneill
760 1.73 jdolecek return hsize;
761 1.65 jmcneill }
762 1.65 jmcneill
763 1.65 jmcneill /* Return the minimum number of character columns based on DPI */
764 1.65 jmcneill static int
765 1.85 jmcneill genfb_calc_cols(struct genfb_softc *sc, struct rasops_info *ri)
766 1.65 jmcneill {
767 1.65 jmcneill const int hsize = genfb_calc_hsize(sc);
768 1.65 jmcneill
769 1.85 jmcneill if (hsize != 0) {
770 1.85 jmcneill ri->ri_flg |= RI_PREFER_WIDEFONT;
771 1.85 jmcneill }
772 1.85 jmcneill
773 1.65 jmcneill return MAX(RASOPS_DEFAULT_WIDTH, hsize / GENFB_CHAR_WIDTH_MM);
774 1.65 jmcneill }
775 1.65 jmcneill
776 1.1 macallan static int
777 1.1 macallan genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
778 1.1 macallan {
779 1.89 riastrad struct genfb_private *scp = sc->sc_private;
780 1.1 macallan u_char *r, *g, *b;
781 1.1 macallan u_int index = cm->index;
782 1.1 macallan u_int count = cm->count;
783 1.1 macallan int i, error;
784 1.1 macallan u_char rbuf[256], gbuf[256], bbuf[256];
785 1.1 macallan
786 1.1 macallan #ifdef GENFB_DEBUG
787 1.1 macallan aprint_debug("putcmap: %d %d\n",index, count);
788 1.1 macallan #endif
789 1.63 mlelstv if (index >= 256 || count > 256 || index + count > 256)
790 1.1 macallan return EINVAL;
791 1.63 mlelstv
792 1.1 macallan error = copyin(cm->red, &rbuf[index], count);
793 1.1 macallan if (error)
794 1.1 macallan return error;
795 1.1 macallan error = copyin(cm->green, &gbuf[index], count);
796 1.1 macallan if (error)
797 1.1 macallan return error;
798 1.1 macallan error = copyin(cm->blue, &bbuf[index], count);
799 1.1 macallan if (error)
800 1.1 macallan return error;
801 1.1 macallan
802 1.89 riastrad memcpy(&scp->sc_cmap_red[index], &rbuf[index], count);
803 1.89 riastrad memcpy(&scp->sc_cmap_green[index], &gbuf[index], count);
804 1.89 riastrad memcpy(&scp->sc_cmap_blue[index], &bbuf[index], count);
805 1.89 riastrad
806 1.89 riastrad r = &scp->sc_cmap_red[index];
807 1.89 riastrad g = &scp->sc_cmap_green[index];
808 1.89 riastrad b = &scp->sc_cmap_blue[index];
809 1.1 macallan
810 1.1 macallan for (i = 0; i < count; i++) {
811 1.1 macallan genfb_putpalreg(sc, index, *r, *g, *b);
812 1.1 macallan index++;
813 1.1 macallan r++, g++, b++;
814 1.1 macallan }
815 1.1 macallan return 0;
816 1.1 macallan }
817 1.1 macallan
818 1.1 macallan static int
819 1.1 macallan genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
820 1.1 macallan {
821 1.89 riastrad struct genfb_private *scp = sc->sc_private;
822 1.1 macallan u_int index = cm->index;
823 1.1 macallan u_int count = cm->count;
824 1.1 macallan int error;
825 1.1 macallan
826 1.63 mlelstv if (index >= 256 || count > 256 || index + count > 256)
827 1.1 macallan return EINVAL;
828 1.1 macallan
829 1.89 riastrad error = copyout(&scp->sc_cmap_red[index], cm->red, count);
830 1.1 macallan if (error)
831 1.1 macallan return error;
832 1.89 riastrad error = copyout(&scp->sc_cmap_green[index], cm->green, count);
833 1.1 macallan if (error)
834 1.1 macallan return error;
835 1.89 riastrad error = copyout(&scp->sc_cmap_blue[index], cm->blue, count);
836 1.1 macallan if (error)
837 1.1 macallan return error;
838 1.1 macallan
839 1.1 macallan return 0;
840 1.1 macallan }
841 1.1 macallan
842 1.28 jmcneill void
843 1.1 macallan genfb_restore_palette(struct genfb_softc *sc)
844 1.1 macallan {
845 1.89 riastrad struct genfb_private *scp = sc->sc_private;
846 1.1 macallan int i;
847 1.1 macallan
848 1.27 macallan if (sc->sc_depth <= 8) {
849 1.27 macallan for (i = 0; i < (1 << sc->sc_depth); i++) {
850 1.89 riastrad genfb_putpalreg(sc, i, scp->sc_cmap_red[i],
851 1.89 riastrad scp->sc_cmap_green[i], scp->sc_cmap_blue[i]);
852 1.27 macallan }
853 1.1 macallan }
854 1.1 macallan }
855 1.1 macallan
856 1.45 macallan static void
857 1.45 macallan genfb_init_palette(struct genfb_softc *sc)
858 1.45 macallan {
859 1.89 riastrad struct genfb_private *scp = sc->sc_private;
860 1.45 macallan int i, j, tmp;
861 1.45 macallan
862 1.45 macallan if (sc->sc_depth == 8) {
863 1.45 macallan /* generate an r3g3b2 colour map */
864 1.45 macallan for (i = 0; i < 256; i++) {
865 1.45 macallan tmp = i & 0xe0;
866 1.45 macallan /*
867 1.45 macallan * replicate bits so 0xe0 maps to a red value of 0xff
868 1.45 macallan * in order to make white look actually white
869 1.45 macallan */
870 1.45 macallan tmp |= (tmp >> 3) | (tmp >> 6);
871 1.89 riastrad scp->sc_cmap_red[i] = tmp;
872 1.45 macallan
873 1.45 macallan tmp = (i & 0x1c) << 3;
874 1.45 macallan tmp |= (tmp >> 3) | (tmp >> 6);
875 1.89 riastrad scp->sc_cmap_green[i] = tmp;
876 1.45 macallan
877 1.45 macallan tmp = (i & 0x03) << 6;
878 1.45 macallan tmp |= tmp >> 2;
879 1.45 macallan tmp |= tmp >> 4;
880 1.89 riastrad scp->sc_cmap_blue[i] = tmp;
881 1.45 macallan
882 1.89 riastrad genfb_putpalreg(sc, i, scp->sc_cmap_red[i],
883 1.89 riastrad scp->sc_cmap_green[i],
884 1.89 riastrad scp->sc_cmap_blue[i]);
885 1.45 macallan }
886 1.45 macallan } else {
887 1.45 macallan /* steal rasops' ANSI cmap */
888 1.45 macallan j = 0;
889 1.45 macallan for (i = 0; i < 256; i++) {
890 1.89 riastrad scp->sc_cmap_red[i] = rasops_cmap[j];
891 1.89 riastrad scp->sc_cmap_green[i] = rasops_cmap[j + 1];
892 1.89 riastrad scp->sc_cmap_blue[i] = rasops_cmap[j + 2];
893 1.45 macallan j += 3;
894 1.45 macallan }
895 1.45 macallan }
896 1.45 macallan }
897 1.45 macallan
898 1.1 macallan static int
899 1.1 macallan genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
900 1.1 macallan uint8_t b)
901 1.1 macallan {
902 1.89 riastrad struct genfb_private *scp = sc->sc_private;
903 1.1 macallan
904 1.89 riastrad if (scp->sc_cmcb) {
905 1.89 riastrad scp->sc_cmcb->gcc_set_mapreg(scp->sc_cmcb->gcc_cookie,
906 1.4 macallan idx, r, g, b);
907 1.4 macallan }
908 1.1 macallan return 0;
909 1.1 macallan }
910 1.1 macallan
911 1.18 jmcneill void
912 1.18 jmcneill genfb_cnattach(void)
913 1.18 jmcneill {
914 1.18 jmcneill genfb_cnattach_called = 1;
915 1.18 jmcneill }
916 1.18 jmcneill
917 1.86 mlelstv int
918 1.86 mlelstv genfb_cndetach(void)
919 1.86 mlelstv {
920 1.86 mlelstv
921 1.86 mlelstv if (genfb_cnattach_called) {
922 1.86 mlelstv genfb_cnattach_called = 0;
923 1.86 mlelstv return 1;
924 1.86 mlelstv }
925 1.86 mlelstv return 0;
926 1.86 mlelstv }
927 1.86 mlelstv
928 1.20 jmcneill void
929 1.20 jmcneill genfb_disable(void)
930 1.20 jmcneill {
931 1.20 jmcneill genfb_enabled = 0;
932 1.20 jmcneill }
933 1.20 jmcneill
934 1.18 jmcneill int
935 1.18 jmcneill genfb_is_console(void)
936 1.18 jmcneill {
937 1.18 jmcneill return genfb_cnattach_called;
938 1.18 jmcneill }
939 1.19 jmcneill
940 1.19 jmcneill int
941 1.20 jmcneill genfb_is_enabled(void)
942 1.20 jmcneill {
943 1.20 jmcneill return genfb_enabled;
944 1.20 jmcneill }
945 1.20 jmcneill
946 1.20 jmcneill int
947 1.19 jmcneill genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
948 1.19 jmcneill {
949 1.19 jmcneill struct genfb_softc *sc = genfb_softc;
950 1.19 jmcneill
951 1.89 riastrad if (sc && sc->sc_private && sc->sc_private->sc_ops.genfb_borrow)
952 1.89 riastrad return sc->sc_private->sc_ops.genfb_borrow(sc, addr, hdlp);
953 1.19 jmcneill return 0;
954 1.19 jmcneill }
955 1.31 macallan
956 1.31 macallan static void
957 1.31 macallan genfb_brightness_up(device_t dev)
958 1.31 macallan {
959 1.31 macallan struct genfb_softc *sc = device_private(dev);
960 1.89 riastrad struct genfb_private *scp = sc->sc_private;
961 1.31 macallan
962 1.89 riastrad KASSERT(scp->sc_brightness != NULL);
963 1.89 riastrad KASSERT(scp->sc_brightness->gpc_upd_parameter != NULL);
964 1.42 macallan
965 1.89 riastrad (void)scp->sc_brightness->gpc_upd_parameter(
966 1.89 riastrad scp->sc_brightness->gpc_cookie, GENFB_BRIGHTNESS_STEP);
967 1.31 macallan }
968 1.31 macallan
969 1.31 macallan static void
970 1.31 macallan genfb_brightness_down(device_t dev)
971 1.31 macallan {
972 1.31 macallan struct genfb_softc *sc = device_private(dev);
973 1.89 riastrad struct genfb_private *scp = sc->sc_private;
974 1.31 macallan
975 1.89 riastrad KASSERT(scp->sc_brightness != NULL);
976 1.89 riastrad KASSERT(scp->sc_brightness->gpc_upd_parameter != NULL);
977 1.42 macallan
978 1.89 riastrad (void)scp->sc_brightness->gpc_upd_parameter(
979 1.89 riastrad scp->sc_brightness->gpc_cookie, -GENFB_BRIGHTNESS_STEP);
980 1.31 macallan }
981 1.35 jmcneill
982 1.35 jmcneill void
983 1.35 jmcneill genfb_enable_polling(device_t dev)
984 1.35 jmcneill {
985 1.35 jmcneill struct genfb_softc *sc = device_private(dev);
986 1.89 riastrad struct genfb_private *scp = sc->sc_private;
987 1.35 jmcneill
988 1.90 riastrad if (scp == NULL)
989 1.90 riastrad return;
990 1.90 riastrad
991 1.89 riastrad if (scp->sc_console_screen.scr_vd) {
992 1.89 riastrad SCREEN_ENABLE_DRAWING(&scp->sc_console_screen);
993 1.89 riastrad vcons_hard_switch(&scp->sc_console_screen);
994 1.38 jmcneill vcons_enable_polling(&sc->vd);
995 1.89 riastrad if (scp->sc_ops.genfb_enable_polling)
996 1.89 riastrad (*scp->sc_ops.genfb_enable_polling)(sc);
997 1.38 jmcneill }
998 1.35 jmcneill }
999 1.35 jmcneill
1000 1.35 jmcneill void
1001 1.35 jmcneill genfb_disable_polling(device_t dev)
1002 1.35 jmcneill {
1003 1.35 jmcneill struct genfb_softc *sc = device_private(dev);
1004 1.89 riastrad struct genfb_private *scp = sc->sc_private;
1005 1.35 jmcneill
1006 1.90 riastrad if (scp == NULL)
1007 1.90 riastrad return;
1008 1.90 riastrad
1009 1.89 riastrad if (scp->sc_console_screen.scr_vd) {
1010 1.89 riastrad if (scp->sc_ops.genfb_disable_polling)
1011 1.89 riastrad (*scp->sc_ops.genfb_disable_polling)(sc);
1012 1.38 jmcneill vcons_disable_polling(&sc->vd);
1013 1.38 jmcneill }
1014 1.35 jmcneill }
1015 1.81 macallan
1016 1.81 macallan #if GENFB_GLYPHCACHE > 0
1017 1.81 macallan #define GLYPHCACHESIZE ((GENFB_GLYPHCACHE) * 1024 * 1024)
1018 1.81 macallan
1019 1.81 macallan static inline int
1020 1.81 macallan attr2idx(long attr)
1021 1.81 macallan {
1022 1.81 macallan if ((attr & 0xf0f00ff8) != 0)
1023 1.81 macallan return -1;
1024 1.89 riastrad
1025 1.81 macallan return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0));
1026 1.81 macallan }
1027 1.81 macallan
1028 1.81 macallan static int
1029 1.81 macallan genfb_setup_glyphcache(struct genfb_softc *sc, long defattr)
1030 1.81 macallan {
1031 1.89 riastrad struct genfb_private *scp = sc->sc_private;
1032 1.89 riastrad struct rasops_info *ri = &scp->sc_console_screen.scr_ri,
1033 1.89 riastrad *cri = &scp->sc_cache_ri;
1034 1.81 macallan gc_bucket *b;
1035 1.81 macallan int i, usedcells = 0, idx, j;
1036 1.81 macallan
1037 1.89 riastrad scp->sc_cache = kmem_alloc(GLYPHCACHESIZE, KM_SLEEP);
1038 1.81 macallan
1039 1.81 macallan /*
1040 1.81 macallan * now we build a mutant rasops_info for the cache - same pixel type
1041 1.89 riastrad * and such as the real fb, but only one character per line for
1042 1.81 macallan * simplicity and locality
1043 1.81 macallan */
1044 1.81 macallan memcpy(cri, ri, sizeof(struct rasops_info));
1045 1.89 riastrad cri->ri_ops.putchar = scp->sc_putchar;
1046 1.81 macallan cri->ri_width = ri->ri_font->fontwidth;
1047 1.81 macallan cri->ri_stride = ri->ri_xscale;
1048 1.89 riastrad cri->ri_bits = scp->sc_cache;
1049 1.81 macallan cri->ri_hwbits = NULL;
1050 1.89 riastrad cri->ri_origbits = scp->sc_cache;
1051 1.81 macallan cri->ri_cols = 1;
1052 1.81 macallan cri->ri_rows = GLYPHCACHESIZE /
1053 1.81 macallan (cri->ri_stride * cri->ri_font->fontheight);
1054 1.81 macallan cri->ri_xorigin = 0;
1055 1.81 macallan cri->ri_yorigin = 0;
1056 1.81 macallan cri->ri_xscale = ri->ri_xscale;
1057 1.81 macallan cri->ri_yscale = ri->ri_font->fontheight * ri->ri_xscale;
1058 1.89 riastrad
1059 1.81 macallan printf("size %d %d %d\n", GLYPHCACHESIZE, ri->ri_width, ri->ri_stride);
1060 1.81 macallan printf("cells: %d\n", cri->ri_rows);
1061 1.89 riastrad scp->sc_nbuckets = uimin(256, cri->ri_rows / 223);
1062 1.89 riastrad scp->sc_buckets = kmem_alloc(sizeof(gc_bucket) * scp->sc_nbuckets,
1063 1.89 riastrad KM_SLEEP);
1064 1.89 riastrad printf("buckets: %d\n", scp->sc_nbuckets);
1065 1.89 riastrad for (i = 0; i < scp->sc_nbuckets; i++) {
1066 1.89 riastrad b = &scp->sc_buckets[i];
1067 1.81 macallan b->gb_firstcell = usedcells;
1068 1.81 macallan b->gb_numcells = uimin(223, cri->ri_rows - usedcells);
1069 1.81 macallan usedcells += 223;
1070 1.81 macallan b->gb_usedcells = 0;
1071 1.81 macallan b->gb_index = -1;
1072 1.81 macallan for (j = 0; j < 223; j++) b->gb_map[j] = -1;
1073 1.81 macallan }
1074 1.81 macallan
1075 1.81 macallan /* initialize the attribute map... */
1076 1.81 macallan for (i = 0; i < 256; i++) {
1077 1.89 riastrad scp->sc_attrmap[i] = -1;
1078 1.81 macallan }
1079 1.81 macallan
1080 1.81 macallan /* first bucket goes to default attr */
1081 1.81 macallan idx = attr2idx(defattr);
1082 1.81 macallan printf("defattr %08lx idx %x\n", defattr, idx);
1083 1.89 riastrad
1084 1.81 macallan if (idx >= 0) {
1085 1.89 riastrad scp->sc_attrmap[idx] = 0;
1086 1.89 riastrad scp->sc_buckets[0].gb_index = idx;
1087 1.81 macallan }
1088 1.89 riastrad
1089 1.81 macallan return 0;
1090 1.81 macallan }
1091 1.81 macallan
1092 1.81 macallan static void
1093 1.81 macallan genfb_putchar(void *cookie, int row, int col, u_int c, long attr)
1094 1.81 macallan {
1095 1.81 macallan struct rasops_info *ri = cookie;
1096 1.81 macallan struct vcons_screen *scr = ri->ri_hw;
1097 1.81 macallan struct genfb_softc *sc = scr->scr_cookie;
1098 1.89 riastrad struct genfb_private *scp = sc->sc_private;
1099 1.87 rin uint8_t *src, *dst, *hwdst;
1100 1.81 macallan gc_bucket *b;
1101 1.81 macallan int i, idx, bi, cell;
1102 1.81 macallan
1103 1.81 macallan attr &= ~WSATTR_USERMASK;
1104 1.81 macallan
1105 1.81 macallan idx = attr2idx(attr);
1106 1.81 macallan if (c < 33 || c > 255 || idx < 0) goto nope;
1107 1.81 macallan
1108 1.81 macallan /* look for a bucket with the right attribute */
1109 1.89 riastrad bi = scp->sc_attrmap[idx];
1110 1.81 macallan if (bi == -1) {
1111 1.81 macallan /* nope, see if there's an empty one left */
1112 1.81 macallan bi = 1;
1113 1.89 riastrad while ((bi < scp->sc_nbuckets) &&
1114 1.89 riastrad (scp->sc_buckets[bi].gb_index != -1)) {
1115 1.81 macallan bi++;
1116 1.81 macallan }
1117 1.89 riastrad if (bi < scp->sc_nbuckets) {
1118 1.81 macallan /* found one -> grab it */
1119 1.89 riastrad scp->sc_attrmap[idx] = bi;
1120 1.89 riastrad b = &scp->sc_buckets[bi];
1121 1.81 macallan b->gb_index = idx;
1122 1.81 macallan b->gb_usedcells = 0;
1123 1.81 macallan /* make sure this doesn't get evicted right away */
1124 1.81 macallan b->gb_lastread = time_uptime;
1125 1.81 macallan } else {
1126 1.81 macallan /*
1127 1.81 macallan * still nothing
1128 1.81 macallan * steal the least recently read bucket
1129 1.81 macallan */
1130 1.81 macallan time_t moo = time_uptime;
1131 1.81 macallan int oldest = 1;
1132 1.81 macallan
1133 1.89 riastrad for (i = 1; i < scp->sc_nbuckets; i++) {
1134 1.89 riastrad if (scp->sc_buckets[i].gb_lastread < moo) {
1135 1.81 macallan oldest = i;
1136 1.89 riastrad moo = scp->sc_buckets[i].gb_lastread;
1137 1.81 macallan }
1138 1.81 macallan }
1139 1.81 macallan
1140 1.81 macallan /* if we end up here all buckets must be in use */
1141 1.89 riastrad b = &scp->sc_buckets[oldest];
1142 1.89 riastrad scp->sc_attrmap[b->gb_index] = -1;
1143 1.81 macallan b->gb_index = idx;
1144 1.81 macallan b->gb_usedcells = 0;
1145 1.89 riastrad scp->sc_attrmap[idx] = oldest;
1146 1.81 macallan /* now scrub it */
1147 1.81 macallan for (i = 0; i < 223; i++)
1148 1.81 macallan b->gb_map[i] = -1;
1149 1.81 macallan /* and set the time stamp */
1150 1.81 macallan b->gb_lastread = time_uptime;
1151 1.81 macallan }
1152 1.81 macallan } else {
1153 1.81 macallan /* found one */
1154 1.89 riastrad b = &scp->sc_buckets[bi];
1155 1.81 macallan }
1156 1.81 macallan
1157 1.81 macallan /* see if there's room in the bucket */
1158 1.81 macallan if (b->gb_usedcells >= b->gb_numcells) goto nope;
1159 1.81 macallan
1160 1.81 macallan cell = b->gb_map[c - 33];
1161 1.81 macallan if (cell == -1) {
1162 1.81 macallan if (b->gb_usedcells >= b->gb_numcells)
1163 1.81 macallan goto nope;
1164 1.81 macallan cell = atomic_add_int_nv(&b->gb_usedcells, 1) - 1;
1165 1.81 macallan b->gb_map[c - 33] = cell;
1166 1.81 macallan cell += b->gb_firstcell;
1167 1.89 riastrad scp->sc_putchar(&scp->sc_cache_ri, cell, 0, c, attr);
1168 1.81 macallan } else
1169 1.81 macallan cell += b->gb_firstcell;
1170 1.81 macallan
1171 1.89 riastrad src = scp->sc_cache + cell * scp->sc_cache_ri.ri_yscale;
1172 1.81 macallan dst = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
1173 1.81 macallan for (i = 0; i < ri->ri_font->fontheight; i++) {
1174 1.81 macallan memcpy(dst, src, ri->ri_xscale);
1175 1.81 macallan src += ri->ri_xscale;
1176 1.81 macallan dst += ri->ri_stride;
1177 1.81 macallan }
1178 1.87 rin if (ri->ri_hwbits) {
1179 1.89 riastrad src = scp->sc_cache + cell * scp->sc_cache_ri.ri_yscale;
1180 1.87 rin hwdst = ri->ri_hwbits
1181 1.87 rin + row * ri->ri_yscale + col * ri->ri_xscale;
1182 1.87 rin for (i = 0; i < ri->ri_font->fontheight; i++) {
1183 1.87 rin memcpy(hwdst, src, ri->ri_xscale);
1184 1.87 rin src += ri->ri_xscale;
1185 1.87 rin hwdst += ri->ri_stride;
1186 1.87 rin }
1187 1.87 rin }
1188 1.81 macallan b->gb_lastread = time_uptime;
1189 1.81 macallan return;
1190 1.81 macallan nope:
1191 1.89 riastrad scp->sc_putchar(cookie, row, col, c, attr);
1192 1.81 macallan }
1193 1.81 macallan
1194 1.81 macallan #endif
1195