genfb.c revision 1.27 1 /* $NetBSD: genfb.c,v 1.27 2009/08/20 02:51:27 macallan Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: genfb.c,v 1.27 2009/08/20 02:51:27 macallan Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/proc.h>
37 #include <sys/mutex.h>
38 #include <sys/ioctl.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/kmem.h>
42
43 #include <dev/wscons/wsconsio.h>
44 #include <dev/wscons/wsdisplayvar.h>
45 #include <dev/rasops/rasops.h>
46 #include <dev/wsfont/wsfont.h>
47
48 #include <dev/wscons/wsdisplay_vconsvar.h>
49
50 #include <dev/wsfb/genfbvar.h>
51
52 #include "opt_genfb.h"
53 #include "opt_wsfb.h"
54
55 #ifdef GENFB_DEBUG
56 #define GPRINTF panic
57 #else
58 #define GPRINTF aprint_verbose
59 #endif
60
61 static int genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
62 static paddr_t genfb_mmap(void *, void *, off_t, int);
63 static void genfb_init_screen(void *, struct vcons_screen *, int, long *);
64
65 static int genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
66 static int genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
67 static void genfb_restore_palette(struct genfb_softc *);
68 static int genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
69 uint8_t, uint8_t);
70
71 extern const u_char rasops_cmap[768];
72
73 static int genfb_cnattach_called = 0;
74 static int genfb_enabled = 1;
75
76 struct wsdisplay_accessops genfb_accessops = {
77 genfb_ioctl,
78 genfb_mmap,
79 NULL, /* alloc_screen */
80 NULL, /* free_screen */
81 NULL, /* show_screen */
82 NULL, /* load_font */
83 NULL, /* pollc */
84 NULL /* scroll */
85 };
86
87 static struct genfb_softc *genfb_softc = NULL;
88
89 void
90 genfb_init(struct genfb_softc *sc)
91 {
92 prop_dictionary_t dict;
93 uint64_t cmap_cb;
94 uint32_t fboffset;
95
96 dict = device_properties(&sc->sc_dev);
97 #ifdef GENFB_DEBUG
98 printf(prop_dictionary_externalize(dict));
99 #endif
100 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
101 GPRINTF("no width property\n");
102 return;
103 }
104 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
105 GPRINTF("no height property\n");
106 return;
107 }
108 if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
109 GPRINTF("no depth property\n");
110 return;
111 }
112
113 /* XXX should be a 64bit value */
114 if (!prop_dictionary_get_uint32(dict, "address", &fboffset)) {
115 GPRINTF("no address property\n");
116 return;
117 }
118
119 sc->sc_fboffset = fboffset;
120
121 if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
122 sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
123
124 /*
125 * deal with a bug in the Raptor firmware which always sets
126 * stride = width even when depth != 8
127 */
128 if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3))
129 sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
130
131 sc->sc_fbsize = sc->sc_height * sc->sc_stride;
132
133 /* optional colour map callback */
134 sc->sc_cmcb = NULL;
135 if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
136 if (cmap_cb != 0)
137 sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
138 }
139 }
140
141 int
142 genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
143 {
144 struct wsemuldisplaydev_attach_args aa;
145 prop_dictionary_t dict;
146 struct rasops_info *ri;
147 uint16_t crow;
148 long defattr;
149 int i, j;
150 bool console;
151
152 dict = device_properties(&sc->sc_dev);
153 prop_dictionary_get_bool(dict, "is_console", &console);
154
155 if (prop_dictionary_get_uint16(dict, "cursor-row", &crow) == false)
156 crow = 0;
157 if (prop_dictionary_get_bool(dict, "clear-screen", &sc->sc_want_clear)
158 == false)
159 sc->sc_want_clear = true;
160
161 /* do not attach when we're not console */
162 if (!console) {
163 aprint_normal_dev(&sc->sc_dev, "no console, unable to continue\n");
164 return -1;
165 }
166
167 aprint_verbose_dev(&sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
168 "stride %d\n",
169 sc->sc_fboffset ? (void *)(intptr_t)sc->sc_fboffset : sc->sc_fbaddr,
170 sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
171
172 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
173 "default",
174 0, 0,
175 NULL,
176 8, 16,
177 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
178 NULL
179 };
180 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
181 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
182 memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
183 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
184
185 sc->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP);
186 if (sc->sc_want_clear == false && sc->sc_shadowfb != NULL)
187 memcpy(sc->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize);
188
189 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
190 &genfb_accessops);
191 sc->vd.init_screen = genfb_init_screen;
192
193 /* Do not print anything between this point and the screen
194 * clear operation below. Otherwise it will be lost. */
195
196 ri = &sc->sc_console_screen.scr_ri;
197
198 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
199 &defattr);
200 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
201
202 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
203 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
204 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
205 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
206 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, crow,
207 defattr);
208
209 /* Clear the whole screen to bring it to a known state. */
210 if (sc->sc_want_clear)
211 (*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
212
213 j = 0;
214 for (i = 0; i < min(1 << sc->sc_depth, 256); i++) {
215
216 sc->sc_cmap_red[i] = rasops_cmap[j];
217 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
218 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
219 genfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
220 rasops_cmap[j + 2]);
221 j += 3;
222 }
223
224 vcons_replay_msgbuf(&sc->sc_console_screen);
225
226 if (genfb_softc == NULL)
227 genfb_softc = sc;
228
229 aa.console = console;
230 aa.scrdata = &sc->sc_screenlist;
231 aa.accessops = &genfb_accessops;
232 aa.accesscookie = &sc->vd;
233
234 config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
235
236 return 0;
237 }
238
239 static int
240 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
241 struct lwp *l)
242 {
243 struct vcons_data *vd = v;
244 struct genfb_softc *sc = vd->cookie;
245 struct wsdisplay_fbinfo *wdf;
246 struct vcons_screen *ms = vd->active;
247 int new_mode, error;
248
249 switch (cmd) {
250 case WSDISPLAYIO_GINFO:
251 if (ms == NULL)
252 return ENODEV;
253 wdf = (void *)data;
254 wdf->height = ms->scr_ri.ri_height;
255 wdf->width = ms->scr_ri.ri_width;
256 wdf->depth = ms->scr_ri.ri_depth;
257 wdf->cmsize = 256;
258 return 0;
259
260 case WSDISPLAYIO_GETCMAP:
261 return genfb_getcmap(sc,
262 (struct wsdisplay_cmap *)data);
263
264 case WSDISPLAYIO_PUTCMAP:
265 return genfb_putcmap(sc,
266 (struct wsdisplay_cmap *)data);
267
268 case WSDISPLAYIO_LINEBYTES:
269 *(u_int *)data = sc->sc_stride;
270 return 0;
271
272 case WSDISPLAYIO_SMODE:
273 new_mode = *(int *)data;
274
275 /* notify the bus backend */
276 error = 0;
277 if (sc->sc_ops.genfb_ioctl)
278 error = sc->sc_ops.genfb_ioctl(sc, vs,
279 cmd, data, flag, l);
280 if (error)
281 return error;
282
283 if (new_mode != sc->sc_mode) {
284 sc->sc_mode = new_mode;
285 if (new_mode == WSDISPLAYIO_MODE_EMUL) {
286 genfb_restore_palette(sc);
287 vcons_redraw_screen(ms);
288 }
289 }
290 return 0;
291 default:
292 if (sc->sc_ops.genfb_ioctl)
293 return sc->sc_ops.genfb_ioctl(sc, vs, cmd,
294 data, flag, l);
295 }
296 return EPASSTHROUGH;
297 }
298
299 static paddr_t
300 genfb_mmap(void *v, void *vs, off_t offset, int prot)
301 {
302 struct vcons_data *vd = v;
303 struct genfb_softc *sc = vd->cookie;
304
305 if (sc->sc_ops.genfb_mmap)
306 return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
307
308 return -1;
309 }
310
311 static void
312 genfb_init_screen(void *cookie, struct vcons_screen *scr,
313 int existing, long *defattr)
314 {
315 struct genfb_softc *sc = cookie;
316 struct rasops_info *ri = &scr->scr_ri;
317
318 ri->ri_depth = sc->sc_depth;
319 ri->ri_width = sc->sc_width;
320 ri->ri_height = sc->sc_height;
321 ri->ri_stride = sc->sc_stride;
322 ri->ri_flg = RI_CENTER;
323 if (sc->sc_want_clear)
324 ri->ri_flg |= RI_FULLCLEAR;
325
326 if (sc->sc_shadowfb != NULL) {
327
328 ri->ri_hwbits = (char *)sc->sc_fbaddr;
329 ri->ri_bits = (char *)sc->sc_shadowfb;
330 } else
331 ri->ri_bits = (char *)sc->sc_fbaddr;
332
333 if (existing && sc->sc_want_clear) {
334 ri->ri_flg |= RI_CLEAR;
335 }
336
337 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
338 ri->ri_caps = WSSCREEN_WSCOLORS;
339
340 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
341 sc->sc_width / ri->ri_font->fontwidth);
342
343 /* TODO: actually center output */
344 ri->ri_hw = scr;
345 }
346
347 static int
348 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
349 {
350 u_char *r, *g, *b;
351 u_int index = cm->index;
352 u_int count = cm->count;
353 int i, error;
354 u_char rbuf[256], gbuf[256], bbuf[256];
355
356 #ifdef GENFB_DEBUG
357 aprint_debug("putcmap: %d %d\n",index, count);
358 #endif
359 if (cm->index >= 256 || cm->count > 256 ||
360 (cm->index + cm->count) > 256)
361 return EINVAL;
362 error = copyin(cm->red, &rbuf[index], count);
363 if (error)
364 return error;
365 error = copyin(cm->green, &gbuf[index], count);
366 if (error)
367 return error;
368 error = copyin(cm->blue, &bbuf[index], count);
369 if (error)
370 return error;
371
372 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
373 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
374 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
375
376 r = &sc->sc_cmap_red[index];
377 g = &sc->sc_cmap_green[index];
378 b = &sc->sc_cmap_blue[index];
379
380 for (i = 0; i < count; i++) {
381 genfb_putpalreg(sc, index, *r, *g, *b);
382 index++;
383 r++, g++, b++;
384 }
385 return 0;
386 }
387
388 static int
389 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
390 {
391 u_int index = cm->index;
392 u_int count = cm->count;
393 int error;
394
395 if (index >= 255 || count > 256 || index + count > 256)
396 return EINVAL;
397
398 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
399 if (error)
400 return error;
401 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
402 if (error)
403 return error;
404 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
405 if (error)
406 return error;
407
408 return 0;
409 }
410
411 static void
412 genfb_restore_palette(struct genfb_softc *sc)
413 {
414 int i;
415
416 if (sc->sc_depth <= 8) {
417 for (i = 0; i < (1 << sc->sc_depth); i++) {
418 genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
419 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
420 }
421 }
422 }
423
424 static int
425 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
426 uint8_t b)
427 {
428
429 if (sc->sc_cmcb) {
430
431 sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
432 idx, r, g, b);
433 }
434 return 0;
435 }
436
437 void
438 genfb_cnattach(void)
439 {
440 genfb_cnattach_called = 1;
441 }
442
443 void
444 genfb_disable(void)
445 {
446 genfb_enabled = 0;
447 }
448
449 int
450 genfb_is_console(void)
451 {
452 return genfb_cnattach_called;
453 }
454
455 int
456 genfb_is_enabled(void)
457 {
458 return genfb_enabled;
459 }
460
461 int
462 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
463 {
464 struct genfb_softc *sc = genfb_softc;
465
466 if (sc && sc->sc_ops.genfb_borrow)
467 return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
468 return 0;
469 }
470