1 /* $NetBSD: gfb.c,v 1.3 2010/08/31 03:08:23 macallan Exp $ */ 2 3 /* 4 * Copyright (c) 2009 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* 29 * A console driver for Sun XVR-1000 graphics cards 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: gfb.c,v 1.3 2010/08/31 03:08:23 macallan Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/device.h> 39 #include <sys/malloc.h> 40 #include <sys/lwp.h> 41 #include <sys/kauth.h> 42 #include <sys/kmem.h> 43 44 #include <uvm/uvm_extern.h> 45 46 #include <dev/videomode/videomode.h> 47 48 #include <machine/bus.h> 49 #include <machine/autoconf.h> 50 #include <machine/openfirm.h> 51 52 #include <dev/wscons/wsdisplayvar.h> 53 #include <dev/wscons/wsconsio.h> 54 #include <dev/wsfont/wsfont.h> 55 #include <dev/rasops/rasops.h> 56 #include <dev/wscons/wsdisplay_vconsvar.h> 57 58 59 struct gfb_softc { 60 device_t sc_dev; 61 62 bus_space_tag_t sc_memt; 63 64 bus_space_handle_t sc_fbh; 65 bus_addr_t sc_fb_paddr; 66 67 int sc_width, sc_height, sc_depth, sc_stride, sc_fblen; 68 int sc_locked; 69 void *sc_fbaddr; 70 struct vcons_screen sc_console_screen; 71 struct wsscreen_descr sc_defaultscreen_descr; 72 const struct wsscreen_descr *sc_screens[1]; 73 struct wsscreen_list sc_screenlist; 74 struct vcons_data vd; 75 int sc_mode; 76 int sc_node; 77 u_char sc_cmap_red[256]; 78 u_char sc_cmap_green[256]; 79 u_char sc_cmap_blue[256]; 80 }; 81 82 static int gfb_match(device_t, cfdata_t, void *); 83 static void gfb_attach(device_t, device_t, void *); 84 85 CFATTACH_DECL_NEW(gfb, sizeof(struct gfb_softc), 86 gfb_match, gfb_attach, NULL, NULL); 87 88 extern const u_char rasops_cmap[768]; 89 90 static int gfb_ioctl(void *, void *, u_long, void *, int, 91 struct lwp *); 92 static paddr_t gfb_mmap(void *, void *, off_t, int); 93 static void gfb_init_screen(void *, struct vcons_screen *, int, long *); 94 95 static int gfb_putcmap(struct gfb_softc *, struct wsdisplay_cmap *); 96 static int gfb_getcmap(struct gfb_softc *, struct wsdisplay_cmap *); 97 static void gfb_restore_palette(struct gfb_softc *); 98 static int gfb_putpalreg(struct gfb_softc *, uint8_t, uint8_t, 99 uint8_t, uint8_t); 100 101 struct wsdisplay_accessops gfb_accessops = { 102 gfb_ioctl, 103 gfb_mmap, 104 NULL, /* alloc_screen */ 105 NULL, /* free_screen */ 106 NULL, /* show_screen */ 107 NULL, /* load_font */ 108 NULL, /* pollc */ 109 NULL /* scroll */ 110 }; 111 112 extern int prom_stdout_node; 113 114 static int 115 gfb_match(device_t parent, cfdata_t match, void *aux) 116 { 117 struct mainbus_attach_args *ma = aux; 118 119 if (strcmp(ma->ma_name, "SUNW,gfb") == 0 ) 120 return 100; 121 return 0; 122 } 123 124 static void 125 gfb_attach(device_t parent, device_t self, void *aux) 126 { 127 struct gfb_softc *sc = device_private(self); 128 struct mainbus_attach_args *ma = aux; 129 struct rasops_info *ri; 130 struct wsemuldisplaydev_attach_args aa; 131 unsigned long defattr; 132 bool is_console; 133 int i, j; 134 135 sc->sc_memt = ma->ma_bustag; 136 sc->sc_dev = self; 137 sc->sc_node = ma->ma_node; 138 139 if (ma->ma_nreg < 7) { 140 aprint_error("%s: can't find the fb range\n", 141 device_xname(self)); 142 return; 143 } 144 145 is_console = (prom_stdout_node == ma->ma_node); 146 147 aprint_normal(": Sun XVR-1000%s\n", is_console ? " (console)" : ""); 148 149 sc->sc_depth = 32; 150 sc->sc_stride = 0x4000; 151 sc->sc_height = prom_getpropint(sc->sc_node, "height", 0); 152 sc->sc_width = prom_getpropint(sc->sc_node, "width", 0); 153 sc->sc_fblen = sc->sc_stride * sc->sc_height; 154 155 if (sc->sc_fblen == 0) { 156 aprint_error("%s: not set up by firmware, can't continue.\n", 157 device_xname(self)); 158 return; 159 } 160 161 sc->sc_locked = 0; 162 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 163 164 if (bus_space_map(sc->sc_memt, ma->ma_reg[6].ur_paddr, 165 sc->sc_fblen, BUS_SPACE_MAP_LINEAR, &sc->sc_fbh)) { 166 printf(": failed to map the framebuffer\n"); 167 return; 168 } 169 sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh); 170 sc->sc_fb_paddr = ma->ma_reg[6].ur_paddr; 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 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 183 sc->sc_locked = 0; 184 185 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr, 186 &gfb_accessops); 187 sc->vd.init_screen = gfb_init_screen; 188 189 ri = &sc->sc_console_screen.scr_ri; 190 191 j = 0; 192 for (i = 0; i < (1 << sc->sc_depth); i++) { 193 194 sc->sc_cmap_red[i] = rasops_cmap[j]; 195 sc->sc_cmap_green[i] = rasops_cmap[j + 1]; 196 sc->sc_cmap_blue[i] = rasops_cmap[j + 2]; 197 gfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1], 198 rasops_cmap[j + 2]); 199 j += 3; 200 } 201 202 if (is_console) { 203 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, 204 &defattr); 205 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 206 207 #if notyet 208 gfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, 209 ri->ri_devcmap[(defattr >> 16) & 0xff]); 210 #endif 211 sc->sc_defaultscreen_descr.textops = &ri->ri_ops; 212 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps; 213 sc->sc_defaultscreen_descr.nrows = ri->ri_rows; 214 sc->sc_defaultscreen_descr.ncols = ri->ri_cols; 215 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0, 216 defattr); 217 vcons_replay_msgbuf(&sc->sc_console_screen); 218 } else { 219 /* 220 * since we're not the console we can postpone the rest 221 * until someone actually allocates a screen for us 222 */ 223 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr); 224 } 225 226 aa.console = is_console; 227 aa.scrdata = &sc->sc_screenlist; 228 aa.accessops = &gfb_accessops; 229 aa.accesscookie = &sc->vd; 230 231 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint); 232 233 #ifdef GFB_DEBUG 234 /* 235 * now dump a register range 236 * try 1, 2 and 4 since they're only 0x2000 each 237 */ 238 bus_space_handle_t regh; 239 240 if (bus_space_map(sc->sc_memt, ma->ma_reg[3].ur_paddr, 241 0x2000, BUS_SPACE_MAP_LINEAR, ®h) == 0) { 242 for (i = 0; i < 0x200; i += 32) { 243 printf("%04x", i); 244 for (j = 0; j < 32; j += 4) { 245 printf(" %08x", bus_space_read_4(sc->sc_memt, 246 regh, i + j)); 247 } 248 printf("\n"); 249 } 250 bus_space_unmap(sc->sc_memt, regh, 0x2000); 251 } 252 #endif 253 } 254 255 static int 256 gfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 257 struct lwp *l) 258 { 259 struct vcons_data *vd = v; 260 struct gfb_softc *sc = vd->cookie; 261 struct wsdisplay_fbinfo *wdf; 262 struct vcons_screen *ms = vd->active; 263 264 switch (cmd) { 265 266 case WSDISPLAYIO_GTYPE: 267 *(u_int *)data = WSDISPLAY_TYPE_XVR1000; 268 return 0; 269 270 case WSDISPLAYIO_GINFO: 271 if (ms == NULL) 272 return ENODEV; 273 wdf = (void *)data; 274 wdf->height = ms->scr_ri.ri_height; 275 wdf->width = ms->scr_ri.ri_width; 276 wdf->depth = ms->scr_ri.ri_depth; 277 wdf->cmsize = 256; 278 return 0; 279 280 case WSDISPLAYIO_GETCMAP: 281 return gfb_getcmap(sc, 282 (struct wsdisplay_cmap *)data); 283 284 case WSDISPLAYIO_PUTCMAP: 285 return gfb_putcmap(sc, 286 (struct wsdisplay_cmap *)data); 287 288 case WSDISPLAYIO_LINEBYTES: 289 *(u_int *)data = sc->sc_stride; 290 return 0; 291 292 case WSDISPLAYIO_SMODE: 293 { 294 int new_mode = *(int*)data; 295 296 /* notify the bus backend */ 297 if (new_mode != sc->sc_mode) { 298 sc->sc_mode = new_mode; 299 if(new_mode == WSDISPLAYIO_MODE_EMUL) { 300 gfb_restore_palette(sc); 301 vcons_redraw_screen(ms); 302 } 303 } 304 } 305 return 0; 306 } 307 return EPASSTHROUGH; 308 } 309 310 static paddr_t 311 gfb_mmap(void *v, void *vs, off_t offset, int prot) 312 { 313 struct vcons_data *vd = v; 314 struct gfb_softc *sc = vd->cookie; 315 paddr_t pa; 316 317 /* 'regular' framebuffer mmap()ing */ 318 if (offset < sc->sc_fblen) { 319 pa = bus_space_mmap(sc->sc_memt, sc->sc_fb_paddr + offset, 0, 320 prot, BUS_SPACE_MAP_LINEAR); 321 return pa; 322 } 323 324 /* 325 * restrict all other mappings to processes with superuser privileges 326 * or the kernel itself 327 */ 328 if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER, 329 NULL) != 0) { 330 aprint_normal("%s: mmap() rejected.\n", 331 device_xname(sc->sc_dev)); 332 return -1; 333 } 334 335 /* let userland map other ranges */ 336 337 return -1; 338 } 339 340 static void 341 gfb_init_screen(void *cookie, struct vcons_screen *scr, 342 int existing, long *defattr) 343 { 344 struct gfb_softc *sc = cookie; 345 struct rasops_info *ri = &scr->scr_ri; 346 347 ri->ri_depth = sc->sc_depth; 348 ri->ri_width = sc->sc_width; 349 ri->ri_height = sc->sc_height; 350 ri->ri_stride = sc->sc_stride; 351 ri->ri_flg = RI_CENTER | RI_FULLCLEAR; 352 353 ri->ri_bits = (char *)sc->sc_fbaddr; 354 scr->scr_flags |= VCONS_DONT_READ; 355 356 if (existing) { 357 ri->ri_flg |= RI_CLEAR; 358 } 359 360 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8); 361 ri->ri_caps = WSSCREEN_WSCOLORS; 362 363 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight, 364 sc->sc_width / ri->ri_font->fontwidth); 365 366 ri->ri_hw = scr; 367 #if 0 368 ri->ri_ops.copyrows = gfb_copyrows; 369 ri->ri_ops.copycols = gfb_copycols; 370 ri->ri_ops.cursor = gfb_cursor; 371 ri->ri_ops.eraserows = gfb_eraserows; 372 ri->ri_ops.erasecols = gfb_erasecols; 373 ri->ri_ops.putchar = gfb_putchar; 374 #endif 375 } 376 377 static int 378 gfb_putcmap(struct gfb_softc *sc, struct wsdisplay_cmap *cm) 379 { 380 u_char *r, *g, *b; 381 u_int index = cm->index; 382 u_int count = cm->count; 383 int i, error; 384 u_char rbuf[256], gbuf[256], bbuf[256]; 385 386 #ifdef GFB_DEBUG 387 aprint_debug("putcmap: %d %d\n",index, count); 388 #endif 389 if (cm->index >= 256 || cm->count > 256 || 390 (cm->index + cm->count) > 256) 391 return EINVAL; 392 error = copyin(cm->red, &rbuf[index], count); 393 if (error) 394 return error; 395 error = copyin(cm->green, &gbuf[index], count); 396 if (error) 397 return error; 398 error = copyin(cm->blue, &bbuf[index], count); 399 if (error) 400 return error; 401 402 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count); 403 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count); 404 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count); 405 406 r = &sc->sc_cmap_red[index]; 407 g = &sc->sc_cmap_green[index]; 408 b = &sc->sc_cmap_blue[index]; 409 410 for (i = 0; i < count; i++) { 411 gfb_putpalreg(sc, index, *r, *g, *b); 412 index++; 413 r++, g++, b++; 414 } 415 return 0; 416 } 417 418 static int 419 gfb_getcmap(struct gfb_softc *sc, struct wsdisplay_cmap *cm) 420 { 421 u_int index = cm->index; 422 u_int count = cm->count; 423 int error; 424 425 if (index >= 255 || count > 256 || index + count > 256) 426 return EINVAL; 427 428 error = copyout(&sc->sc_cmap_red[index], cm->red, count); 429 if (error) 430 return error; 431 error = copyout(&sc->sc_cmap_green[index], cm->green, count); 432 if (error) 433 return error; 434 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count); 435 if (error) 436 return error; 437 438 return 0; 439 } 440 441 static void 442 gfb_restore_palette(struct gfb_softc *sc) 443 { 444 int i; 445 446 for (i = 0; i < (1 << sc->sc_depth); i++) { 447 gfb_putpalreg(sc, i, sc->sc_cmap_red[i], 448 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]); 449 } 450 } 451 452 static int 453 gfb_putpalreg(struct gfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g, 454 uint8_t b) 455 { 456 return 0; 457 } 458