1 /* $NetBSD: gfb.c,v 1.10 2021/04/24 23:36:49 thorpej 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.10 2021/04/24 23:36:49 thorpej 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 <sys/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 | BUS_SPACE_MAP_PREFETCHABLE, 166 &sc->sc_fbh)) { 167 printf(": failed to map the framebuffer\n"); 168 return; 169 } 170 sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh); 171 sc->sc_fb_paddr = ma->ma_reg[6].ur_paddr; 172 173 sc->sc_defaultscreen_descr = (struct wsscreen_descr){ 174 "default", 175 0, 0, 176 NULL, 177 8, 16, 178 WSSCREEN_WSCOLORS | WSSCREEN_HILIT, 179 NULL 180 }; 181 sc->sc_screens[0] = &sc->sc_defaultscreen_descr; 182 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens}; 183 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 184 sc->sc_locked = 0; 185 186 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr, 187 &gfb_accessops); 188 sc->vd.init_screen = gfb_init_screen; 189 190 ri = &sc->sc_console_screen.scr_ri; 191 192 j = 0; 193 for (i = 0; i < 256; i++) { 194 195 sc->sc_cmap_red[i] = rasops_cmap[j]; 196 sc->sc_cmap_green[i] = rasops_cmap[j + 1]; 197 sc->sc_cmap_blue[i] = rasops_cmap[j + 2]; 198 gfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1], 199 rasops_cmap[j + 2]); 200 j += 3; 201 } 202 203 if (is_console) { 204 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, 205 &defattr); 206 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 207 208 #if notyet 209 gfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, 210 ri->ri_devcmap[(defattr >> 16) & 0xff]); 211 #endif 212 sc->sc_defaultscreen_descr.textops = &ri->ri_ops; 213 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps; 214 sc->sc_defaultscreen_descr.nrows = ri->ri_rows; 215 sc->sc_defaultscreen_descr.ncols = ri->ri_cols; 216 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0, 217 defattr); 218 vcons_replay_msgbuf(&sc->sc_console_screen); 219 } else { 220 /* 221 * since we're not the console we can postpone the rest 222 * until someone actually allocates a screen for us 223 */ 224 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr); 225 } 226 227 aa.console = is_console; 228 aa.scrdata = &sc->sc_screenlist; 229 aa.accessops = &gfb_accessops; 230 aa.accesscookie = &sc->vd; 231 232 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARG_EOL); 233 234 #ifdef GFB_DEBUG 235 /* 236 * now dump a register range 237 * try 1, 2 and 4 since they're only 0x2000 each 238 */ 239 bus_space_handle_t regh; 240 241 if (bus_space_map(sc->sc_memt, ma->ma_reg[3].ur_paddr, 242 0x2000, BUS_SPACE_MAP_LINEAR, ®h) == 0) { 243 for (i = 0; i < 0x200; i += 32) { 244 printf("%04x", i); 245 for (j = 0; j < 32; j += 4) { 246 printf(" %08x", bus_space_read_4(sc->sc_memt, 247 regh, i + j)); 248 } 249 printf("\n"); 250 } 251 bus_space_unmap(sc->sc_memt, regh, 0x2000); 252 } 253 #endif 254 } 255 256 static int 257 gfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 258 struct lwp *l) 259 { 260 struct vcons_data *vd = v; 261 struct gfb_softc *sc = vd->cookie; 262 struct wsdisplay_fbinfo *wdf; 263 struct vcons_screen *ms = vd->active; 264 265 switch (cmd) { 266 267 case WSDISPLAYIO_GTYPE: 268 *(u_int *)data = WSDISPLAY_TYPE_XVR1000; 269 return 0; 270 271 case WSDISPLAYIO_GINFO: 272 if (ms == NULL) 273 return ENODEV; 274 wdf = (void *)data; 275 wdf->height = ms->scr_ri.ri_height; 276 wdf->width = ms->scr_ri.ri_width; 277 wdf->depth = ms->scr_ri.ri_depth; 278 wdf->cmsize = 256; 279 return 0; 280 281 case WSDISPLAYIO_GETCMAP: 282 return gfb_getcmap(sc, 283 (struct wsdisplay_cmap *)data); 284 285 case WSDISPLAYIO_PUTCMAP: 286 return gfb_putcmap(sc, 287 (struct wsdisplay_cmap *)data); 288 289 case WSDISPLAYIO_LINEBYTES: 290 *(u_int *)data = sc->sc_stride; 291 return 0; 292 293 case WSDISPLAYIO_SMODE: 294 { 295 int new_mode = *(int*)data; 296 297 /* notify the bus backend */ 298 if (new_mode != sc->sc_mode) { 299 sc->sc_mode = new_mode; 300 if(new_mode == WSDISPLAYIO_MODE_EMUL) { 301 gfb_restore_palette(sc); 302 vcons_redraw_screen(ms); 303 } 304 } 305 } 306 return 0; 307 case WSDISPLAYIO_GET_FBINFO: 308 { 309 struct wsdisplayio_fbinfo *fbi = data; 310 return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi); 311 } 312 } 313 return EPASSTHROUGH; 314 } 315 316 static paddr_t 317 gfb_mmap(void *v, void *vs, off_t offset, int prot) 318 { 319 struct vcons_data *vd = v; 320 struct gfb_softc *sc = vd->cookie; 321 paddr_t pa; 322 323 /* 'regular' framebuffer mmap()ing */ 324 if (offset < sc->sc_fblen) { 325 /* 326 * XXX 327 * BUS_SPACE_MAP_PREFETCHABLE produces artifacts with X, but 328 * not with the console code, so don't set it here 329 */ 330 pa = bus_space_mmap(sc->sc_memt, sc->sc_fb_paddr + offset, 0, 331 prot, BUS_SPACE_MAP_LINEAR); 332 return pa; 333 } 334 335 /* 336 * restrict all other mappings to processes with superuser privileges 337 * or the kernel itself 338 */ 339 if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM, 340 NULL, NULL, NULL, NULL) != 0) { 341 aprint_normal("%s: mmap() rejected.\n", 342 device_xname(sc->sc_dev)); 343 return -1; 344 } 345 346 /* let userland map other ranges */ 347 348 return -1; 349 } 350 351 static void 352 gfb_init_screen(void *cookie, struct vcons_screen *scr, 353 int existing, long *defattr) 354 { 355 struct gfb_softc *sc = cookie; 356 struct rasops_info *ri = &scr->scr_ri; 357 358 ri->ri_depth = sc->sc_depth; 359 ri->ri_width = sc->sc_width; 360 ri->ri_height = sc->sc_height; 361 ri->ri_stride = sc->sc_stride; 362 ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_ENABLE_ALPHA; 363 364 ri->ri_bits = (char *)sc->sc_fbaddr; 365 scr->scr_flags |= VCONS_DONT_READ; 366 367 if (existing) { 368 ri->ri_flg |= RI_CLEAR; 369 } 370 371 /* explicitly request BGR in case the default changes */ 372 ri->ri_rnum = 8; 373 ri->ri_gnum = 8; 374 ri->ri_bnum = 8; 375 ri->ri_rpos = 0; 376 ri->ri_gpos = 8; 377 ri->ri_bpos = 16; 378 379 rasops_init(ri, 0, 0); 380 ri->ri_caps = WSSCREEN_WSCOLORS; 381 382 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight, 383 sc->sc_width / ri->ri_font->fontwidth); 384 385 ri->ri_hw = scr; 386 #if 0 387 ri->ri_ops.copyrows = gfb_copyrows; 388 ri->ri_ops.copycols = gfb_copycols; 389 ri->ri_ops.cursor = gfb_cursor; 390 ri->ri_ops.eraserows = gfb_eraserows; 391 ri->ri_ops.erasecols = gfb_erasecols; 392 ri->ri_ops.putchar = gfb_putchar; 393 #endif 394 } 395 396 static int 397 gfb_putcmap(struct gfb_softc *sc, struct wsdisplay_cmap *cm) 398 { 399 u_char *r, *g, *b; 400 u_int index = cm->index; 401 u_int count = cm->count; 402 int i, error; 403 u_char rbuf[256], gbuf[256], bbuf[256]; 404 405 #ifdef GFB_DEBUG 406 aprint_debug("putcmap: %d %d\n",index, count); 407 #endif 408 if (cm->index >= 256 || cm->count > 256 || 409 (cm->index + cm->count) > 256) 410 return EINVAL; 411 error = copyin(cm->red, &rbuf[index], count); 412 if (error) 413 return error; 414 error = copyin(cm->green, &gbuf[index], count); 415 if (error) 416 return error; 417 error = copyin(cm->blue, &bbuf[index], count); 418 if (error) 419 return error; 420 421 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count); 422 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count); 423 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count); 424 425 r = &sc->sc_cmap_red[index]; 426 g = &sc->sc_cmap_green[index]; 427 b = &sc->sc_cmap_blue[index]; 428 429 for (i = 0; i < count; i++) { 430 gfb_putpalreg(sc, index, *r, *g, *b); 431 index++; 432 r++, g++, b++; 433 } 434 return 0; 435 } 436 437 static int 438 gfb_getcmap(struct gfb_softc *sc, struct wsdisplay_cmap *cm) 439 { 440 u_int index = cm->index; 441 u_int count = cm->count; 442 int error; 443 444 if (index >= 255 || count > 256 || index + count > 256) 445 return EINVAL; 446 447 error = copyout(&sc->sc_cmap_red[index], cm->red, count); 448 if (error) 449 return error; 450 error = copyout(&sc->sc_cmap_green[index], cm->green, count); 451 if (error) 452 return error; 453 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count); 454 if (error) 455 return error; 456 457 return 0; 458 } 459 460 static void 461 gfb_restore_palette(struct gfb_softc *sc) 462 { 463 int i; 464 465 for (i = 0; i < (1 << sc->sc_depth); i++) { 466 gfb_putpalreg(sc, i, sc->sc_cmap_red[i], 467 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]); 468 } 469 } 470 471 static int 472 gfb_putpalreg(struct gfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g, 473 uint8_t b) 474 { 475 return 0; 476 } 477