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