1 /* $NetBSD: wcfb.c,v 1.2 2010/02/25 20:56:20 macallan Exp $ */ 2 3 /*- 4 * Copyright (c) 2010 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: wcfb.c,v 1.2 2010/02/25 20:56:20 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/kauth.h> 42 43 #include <dev/pci/pcidevs.h> 44 #include <dev/pci/pcireg.h> 45 #include <dev/pci/pcivar.h> 46 #include <dev/pci/pciio.h> 47 48 #include <dev/wscons/wsdisplayvar.h> 49 #include <dev/wscons/wsconsio.h> 50 #include <dev/wsfont/wsfont.h> 51 #include <dev/rasops/rasops.h> 52 #include <dev/wscons/wsdisplay_vconsvar.h> 53 54 #include "opt_wsfb.h" 55 #include "opt_wcfb.h" 56 57 #ifdef WCFB_DEBUG 58 # define DPRINTF printf 59 #else 60 # define DPRINTF while (0) printf 61 #endif 62 63 static int wcfb_match(device_t, cfdata_t, void *); 64 static void wcfb_attach(device_t, device_t, void *); 65 static int wcfb_ioctl(void *, void *, u_long, void *, int, 66 struct lwp *); 67 static paddr_t wcfb_mmap(void *, void *, off_t, int); 68 69 struct wcfb_softc { 70 device_t sc_dev; 71 72 pci_chipset_tag_t sc_pc; 73 pcitag_t sc_pcitag; 74 75 bus_space_tag_t sc_memt; 76 bus_space_tag_t sc_regt, sc_wtft; 77 bus_space_tag_t sc_iot; 78 79 bus_space_handle_t sc_fbh, sc_wtfh; 80 bus_space_handle_t sc_regh; 81 bus_addr_t sc_fb, sc_reg, sc_wtf; 82 bus_size_t sc_fbsize, sc_regsize, sc_wtfsize; 83 84 int sc_width, sc_height, sc_stride; 85 int sc_locked; 86 uint8_t *sc_fbaddr; 87 struct vcons_screen sc_console_screen; 88 struct wsscreen_descr sc_defaultscreen_descr; 89 const struct wsscreen_descr *sc_screens[1]; 90 struct wsscreen_list sc_screenlist; 91 struct vcons_data vd; 92 int sc_mode; 93 u_char sc_cmap_red[256]; 94 u_char sc_cmap_green[256]; 95 u_char sc_cmap_blue[256]; 96 uint32_t sc_fb0off; 97 98 void (*copycols)(void *, int, int, int, int); 99 void (*erasecols)(void *, int, int, int, long); 100 void (*copyrows)(void *, int, int, int); 101 void (*eraserows)(void *, int, int, long); 102 void (*putchar)(void *, int, int, u_int, long); 103 void (*cursor)(void *, int, int, int); 104 105 }; 106 107 static void wcfb_init_screen(void *, struct vcons_screen *, int, long *); 108 109 CFATTACH_DECL_NEW(wcfb, sizeof(struct wcfb_softc), 110 wcfb_match, wcfb_attach, NULL, NULL); 111 112 struct wsdisplay_accessops wcfb_accessops = { 113 wcfb_ioctl, 114 wcfb_mmap, 115 NULL, /* alloc_screen */ 116 NULL, /* free_screen */ 117 NULL, /* show_screen */ 118 NULL, /* load_font */ 119 NULL, /* pollc */ 120 NULL /* scroll */ 121 }; 122 123 static void wcfb_putchar(void *, int, int, u_int, long); 124 static void wcfb_cursor(void *, int, int, int); 125 static void wcfb_copycols(void *, int, int, int, int); 126 static void wcfb_erasecols(void *, int, int, int, long); 127 static void wcfb_copyrows(void *, int, int, int); 128 static void wcfb_eraserows(void *, int, int, long); 129 130 static void wcfb_putpalreg(struct wcfb_softc *, int, int, int, int); 131 132 static int 133 wcfb_match(device_t parent, cfdata_t match, void *aux) 134 { 135 struct pci_attach_args *pa = aux; 136 137 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3DLABS && 138 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_WILDCAT5110) 139 return 100; 140 141 return 0; 142 } 143 144 static void 145 wcfb_attach(device_t parent, device_t self, void *aux) 146 { 147 struct wcfb_softc *sc = device_private(self); 148 struct pci_attach_args *pa = aux; 149 struct rasops_info *ri; 150 prop_dictionary_t dict; 151 struct wsemuldisplaydev_attach_args aa; 152 int i, j; 153 unsigned long defattr; 154 bool is_console; 155 char devinfo[256]; 156 void *wtf; 157 158 sc->sc_dev = self; 159 sc->putchar = NULL; 160 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 161 aprint_naive("\n"); 162 aprint_normal(": %s\n", devinfo); 163 164 dict = device_properties(self); 165 prop_dictionary_get_bool(dict, "is_console", &is_console); 166 if(!is_console) return; 167 168 sc->sc_memt = pa->pa_memt; 169 sc->sc_iot = pa->pa_iot; 170 sc->sc_pc = pa->pa_pc; 171 sc->sc_pcitag = pa->pa_tag; 172 173 /* fill in parameters from properties */ 174 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) { 175 aprint_error("%s: no width property\n", device_xname(self)); 176 return; 177 } 178 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) { 179 aprint_error("%s: no height property\n", device_xname(self)); 180 return; 181 } 182 183 if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM, 0, 184 &sc->sc_regt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) { 185 aprint_error("%s: failed to map registers.\n", 186 device_xname(sc->sc_dev)); 187 } 188 189 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, BUS_SPACE_MAP_LINEAR, 190 &sc->sc_memt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) { 191 aprint_error("%s: failed to map framebuffer.\n", 192 device_xname(sc->sc_dev)); 193 } 194 195 if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, BUS_SPACE_MAP_LINEAR, 196 &sc->sc_wtft, &sc->sc_wtfh, &sc->sc_wtf, &sc->sc_wtfsize)) { 197 aprint_error("%s: failed to map wtf.\n", 198 device_xname(sc->sc_dev)); 199 } 200 wtf = bus_space_vaddr(sc->sc_wtft, sc->sc_wtfh); 201 memset(wtf, 0, 0x100000); 202 203 sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh); 204 205 sc->sc_fb0off = bus_space_read_4(sc->sc_regt, sc->sc_regh, 0x8080) - 206 sc->sc_fb; 207 sc->sc_stride = 1 << 208 ((bus_space_read_4(sc->sc_regt, sc->sc_regh, 0x8074) & 0x00ff0000) >> 16); 209 printf("%s: %d x %d, %d\n", device_xname(sc->sc_dev), 210 sc->sc_width, sc->sc_height, sc->sc_stride); 211 for (i = 0x40; i < 0x100; i += 16) { 212 printf("%04x:", i); 213 for (j = 0; j < 16; j += 4) { 214 printf(" %08x", bus_space_read_4(sc->sc_regt, 215 sc->sc_regh, 0x8000 + i + j)); 216 } 217 printf("\n"); 218 } 219 220 sc->sc_defaultscreen_descr = (struct wsscreen_descr){ 221 "default", 222 0, 0, 223 NULL, 224 8, 16, 225 WSSCREEN_WSCOLORS | WSSCREEN_HILIT, 226 NULL 227 }; 228 sc->sc_screens[0] = &sc->sc_defaultscreen_descr; 229 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens}; 230 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 231 sc->sc_locked = 0; 232 233 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr, 234 &wcfb_accessops); 235 sc->vd.init_screen = wcfb_init_screen; 236 237 /* init engine here */ 238 #if 0 239 wcfb_init(sc); 240 #endif 241 242 ri = &sc->sc_console_screen.scr_ri; 243 244 j = 0; 245 for (i = 0; i < 256; i++) { 246 247 sc->sc_cmap_red[i] = rasops_cmap[j]; 248 sc->sc_cmap_green[i] = rasops_cmap[j + 1]; 249 sc->sc_cmap_blue[i] = rasops_cmap[j + 2]; 250 wcfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1], 251 rasops_cmap[j + 2]); 252 j += 3; 253 } 254 wcfb_putpalreg(sc, 0, 0, 0, 0); 255 wcfb_putpalreg(sc, 15, 0xff, 0xff, 0xff); 256 257 if (is_console) { 258 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, 259 &defattr); 260 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 261 262 #if 0 263 wcfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, 264 ri->ri_devcmap[(defattr >> 16) & 0xff]); 265 #else 266 /*memset(sc->sc_fbaddr + sc->sc_fb0off, 0, 0x400000);*/ 267 #endif 268 sc->sc_defaultscreen_descr.textops = &ri->ri_ops; 269 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps; 270 sc->sc_defaultscreen_descr.nrows = ri->ri_rows; 271 sc->sc_defaultscreen_descr.ncols = ri->ri_cols; 272 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0, 273 defattr); 274 vcons_replay_msgbuf(&sc->sc_console_screen); 275 } else { 276 /* 277 * since we're not the console we can postpone the rest 278 * until someone actually allocates a screen for us 279 */ 280 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr); 281 } 282 283 aa.console = is_console; 284 aa.scrdata = &sc->sc_screenlist; 285 aa.accessops = &wcfb_accessops; 286 aa.accesscookie = &sc->vd; 287 288 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint); 289 } 290 291 static int 292 wcfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 293 struct lwp *l) 294 { 295 struct wcfb_softc *sc = v; 296 297 switch (cmd) { 298 case WSDISPLAYIO_GTYPE: 299 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; 300 return 0; 301 302 /* PCI config read/write passthrough. */ 303 case PCI_IOC_CFGREAD: 304 case PCI_IOC_CFGWRITE: 305 return (pci_devioctl(sc->sc_pc, sc->sc_pcitag, 306 cmd, data, flag, l)); 307 case WSDISPLAYIO_SMODE: 308 { 309 /*int new_mode = *(int*)data, i;*/ 310 } 311 return 0; 312 } 313 314 return EPASSTHROUGH; 315 } 316 317 static paddr_t 318 wcfb_mmap(void *v, void *vs, off_t offset, int prot) 319 { 320 struct wcfb_softc *sc = v; 321 322 /* no point in allowing a wsfb map if we can't provide one */ 323 /* 324 * restrict all other mappings to processes with superuser privileges 325 * or the kernel itself 326 */ 327 if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER, 328 NULL) != 0) { 329 aprint_normal_dev(sc->sc_dev, "mmap() rejected.\n"); 330 return -1; 331 } 332 333 #ifdef WSFB_FAKE_VGA_FB 334 if ((offset >= 0xa0000) && (offset < 0xbffff)) { 335 336 return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset, 337 offset - 0xa0000, prot, BUS_SPACE_MAP_LINEAR); 338 } 339 #endif 340 341 /* 342 * XXX this should be generalized, let's just 343 * #define PCI_IOAREA_PADDR 344 * #define PCI_IOAREA_OFFSET 345 * #define PCI_IOAREA_SIZE 346 * somewhere in a MD header and compile this code only if all are 347 * present 348 */ 349 /* 350 * PCI_IOAREA_PADDR is useless, that's what the IO tag is for 351 * the address isn't guaranteed to be the same on each host bridge 352 * either, never mind the fact that it would be a bus address 353 */ 354 #ifdef PCI_MAGIC_IO_RANGE 355 /* allow to map our IO space */ 356 if ((offset >= PCI_MAGIC_IO_RANGE) && 357 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) { 358 return bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE, 359 0, prot, BUS_SPACE_MAP_LINEAR); 360 } 361 #endif 362 return -1; 363 } 364 365 static void 366 wcfb_init_screen(void *cookie, struct vcons_screen *scr, 367 int existing, long *defattr) 368 { 369 struct wcfb_softc *sc = cookie; 370 struct rasops_info *ri = &scr->scr_ri; 371 372 ri->ri_depth = 8; 373 ri->ri_width = sc->sc_width; 374 ri->ri_height = sc->sc_height; 375 ri->ri_stride = sc->sc_stride; 376 ri->ri_flg = RI_CENTER /*| RI_FULLCLEAR*/; 377 378 ri->ri_bits = (char *)sc->sc_fbaddr + sc->sc_fb0off; 379 380 if (existing) { 381 ri->ri_flg |= RI_CLEAR; 382 } 383 384 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8); 385 ri->ri_caps = WSSCREEN_WSCOLORS; 386 387 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight, 388 sc->sc_width / ri->ri_font->fontwidth); 389 390 ri->ri_hw = scr; 391 sc->putchar = ri->ri_ops.putchar; 392 sc->copyrows = ri->ri_ops.copyrows; 393 sc->eraserows = ri->ri_ops.eraserows; 394 sc->copycols = ri->ri_ops.copycols; 395 sc->erasecols = ri->ri_ops.erasecols; 396 397 ri->ri_ops.copyrows = wcfb_copyrows; 398 ri->ri_ops.copycols = wcfb_copycols; 399 ri->ri_ops.eraserows = wcfb_eraserows; 400 ri->ri_ops.erasecols = wcfb_erasecols; 401 ri->ri_ops.putchar = wcfb_putchar; 402 ri->ri_ops.cursor = wcfb_cursor; 403 } 404 405 static void 406 wcfb_putchar(void *cookie, int row, int col, u_int c, long attr) 407 { 408 struct rasops_info *ri = cookie; 409 struct vcons_screen *scr = ri->ri_hw; 410 struct wcfb_softc *sc = scr->scr_cookie; 411 412 sc->putchar(ri, row, col, c, attr); 413 ri->ri_bits += 0x200000; 414 sc->putchar(ri, row, col, c, attr); 415 ri->ri_bits -= 0x200000; 416 } 417 418 static void 419 wcfb_putpalreg(struct wcfb_softc *sc, int i, int r, int g, int b) 420 { 421 uint32_t rgb; 422 423 bus_space_write_4(sc->sc_regt, sc->sc_regh, 0x80bc, i); 424 rgb = (b << 22) | (g << 12) | (r << 2); 425 bus_space_write_4(sc->sc_regt, sc->sc_regh, 0x80c0, rgb); 426 } 427 428 static void 429 wcfb_cursor(void *cookie, int on, int row, int col) 430 { 431 #if 0 432 struct rasops_info *ri = cookie; 433 struct vcons_screen *scr = ri->ri_hw; 434 struct wcfb_softc *sc = scr->scr_cookie; 435 #endif 436 } 437 438 static void 439 wcfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols) 440 { 441 struct rasops_info *ri = cookie; 442 struct vcons_screen *scr = ri->ri_hw; 443 struct wcfb_softc *sc = scr->scr_cookie; 444 445 sc->copycols(ri, row, srccol, dstcol, ncols); 446 ri->ri_bits += 0x200000; 447 sc->copycols(ri, row, srccol, dstcol, ncols); 448 ri->ri_bits -= 0x200000; 449 } 450 451 static void 452 wcfb_erasecols(void *cookie, int row, int startcol, int ncol, long fillattr) 453 { 454 struct rasops_info *ri = cookie; 455 struct vcons_screen *scr = ri->ri_hw; 456 struct wcfb_softc *sc = scr->scr_cookie; 457 458 sc->erasecols(ri, row, startcol, ncol, fillattr); 459 ri->ri_bits += 0x200000; 460 sc->erasecols(ri, row, startcol, ncol, fillattr); 461 ri->ri_bits -= 0x200000; 462 } 463 464 static void 465 wcfb_copyrows(void *cookie, int srcrow, int dstrow, int nrow) 466 { 467 struct rasops_info *ri = cookie; 468 struct vcons_screen *scr = ri->ri_hw; 469 struct wcfb_softc *sc = scr->scr_cookie; 470 471 sc->copyrows(ri, srcrow, dstrow, nrow); 472 ri->ri_bits += 0x200000; 473 sc->copyrows(ri, srcrow, dstrow, nrow); 474 ri->ri_bits -= 0x200000; 475 } 476 477 static void 478 wcfb_eraserows(void *cookie, int row, int nrows, long fillattr) 479 { 480 struct rasops_info *ri = cookie; 481 struct vcons_screen *scr = ri->ri_hw; 482 struct wcfb_softc *sc = scr->scr_cookie; 483 484 sc->eraserows(ri, row, nrows, fillattr); 485 ri->ri_bits += 0x200000; 486 sc->eraserows(ri, row, nrows, fillattr); 487 ri->ri_bits -= 0x200000; 488 } 489