wcfb.c revision 1.1 1 /* $NetBSD: wcfb.c,v 1.1 2010/02/25 03:33:09 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: wcfb.c,v 1.1 2010/02/25 03:33:09 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 uint32_t *tcfb;
158
159 sc->sc_dev = self;
160 sc->putchar = NULL;
161 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
162 aprint_naive("\n");
163 aprint_normal(": %s\n", devinfo);
164
165 dict = device_properties(self);
166 prop_dictionary_get_bool(dict, "is_console", &is_console);
167 if(!is_console) return;
168
169 sc->sc_memt = pa->pa_memt;
170 sc->sc_iot = pa->pa_iot;
171 sc->sc_pc = pa->pa_pc;
172 sc->sc_pcitag = pa->pa_tag;
173
174 /* fill in parameters from properties */
175 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
176 aprint_error("%s: no width property\n", device_xname(self));
177 return;
178 }
179 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
180 aprint_error("%s: no height property\n", device_xname(self));
181 return;
182 }
183
184 if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM, 0,
185 &sc->sc_regt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
186 aprint_error("%s: failed to map registers.\n",
187 device_xname(sc->sc_dev));
188 }
189
190 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, BUS_SPACE_MAP_LINEAR,
191 &sc->sc_memt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
192 aprint_error("%s: failed to map framebuffer.\n",
193 device_xname(sc->sc_dev));
194 }
195
196 if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, BUS_SPACE_MAP_LINEAR,
197 &sc->sc_wtft, &sc->sc_wtfh, &sc->sc_wtf, &sc->sc_wtfsize)) {
198 aprint_error("%s: failed to map wtf.\n",
199 device_xname(sc->sc_dev));
200 }
201 wtf = bus_space_vaddr(sc->sc_wtft, sc->sc_wtfh);
202 memset(wtf, 0, 0x100000);
203
204 sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
205
206 sc->sc_fb0off = bus_space_read_4(sc->sc_regt, sc->sc_regh, 0x8080) -
207 sc->sc_fb;
208 sc->sc_stride = 1 <<
209 ((bus_space_read_4(sc->sc_regt, sc->sc_regh, 0x8074) & 0x00ff0000) >> 16);
210 printf("%s: %d x %d, %d\n", device_xname(sc->sc_dev),
211 sc->sc_width, sc->sc_height, sc->sc_stride);
212 for (i = 0x40; i < 0x100; i += 16) {
213 printf("%04x:", i);
214 for (j = 0; j < 16; j += 4) {
215 printf(" %08x", bus_space_read_4(sc->sc_regt,
216 sc->sc_regh, 0x8000 + i + j));
217 }
218 printf("\n");
219 }
220
221 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
222 "default",
223 0, 0,
224 NULL,
225 8, 16,
226 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
227 NULL
228 };
229 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
230 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
231 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
232 sc->sc_locked = 0;
233
234 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
235 &wcfb_accessops);
236 sc->vd.init_screen = wcfb_init_screen;
237
238 /* init engine here */
239 #if 0
240 wcfb_init(sc);
241 #endif
242
243 ri = &sc->sc_console_screen.scr_ri;
244
245 j = 0;
246 for (i = 0; i < 256; i++) {
247
248 sc->sc_cmap_red[i] = rasops_cmap[j];
249 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
250 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
251 wcfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
252 rasops_cmap[j + 2]);
253 j += 3;
254 }
255 wcfb_putpalreg(sc, 0, 0, 0, 0);
256 wcfb_putpalreg(sc, 15, 0xff, 0xff, 0xff);
257
258 if (is_console) {
259 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
260 &defattr);
261 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
262
263 #if 0
264 wcfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
265 ri->ri_devcmap[(defattr >> 16) & 0xff]);
266 #else
267 /*memset(sc->sc_fbaddr + sc->sc_fb0off, 0, 0x400000);*/
268 #endif
269 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
270 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
271 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
272 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
273 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
274 defattr);
275 vcons_replay_msgbuf(&sc->sc_console_screen);
276 } else {
277 /*
278 * since we're not the console we can postpone the rest
279 * until someone actually allocates a screen for us
280 */
281 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
282 }
283
284 aa.console = is_console;
285 aa.scrdata = &sc->sc_screenlist;
286 aa.accessops = &wcfb_accessops;
287 aa.accesscookie = &sc->vd;
288
289 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
290 }
291
292 static int
293 wcfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
294 struct lwp *l)
295 {
296 struct wcfb_softc *sc = v;
297
298 switch (cmd) {
299 case WSDISPLAYIO_GTYPE:
300 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
301 return 0;
302
303 /* PCI config read/write passthrough. */
304 case PCI_IOC_CFGREAD:
305 case PCI_IOC_CFGWRITE:
306 return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
307 cmd, data, flag, l));
308 case WSDISPLAYIO_SMODE:
309 {
310 /*int new_mode = *(int*)data, i;*/
311 }
312 return 0;
313 }
314
315 return EPASSTHROUGH;
316 }
317
318 static paddr_t
319 wcfb_mmap(void *v, void *vs, off_t offset, int prot)
320 {
321 struct wcfb_softc *sc = v;
322
323 /* no point in allowing a wsfb map if we can't provide one */
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_dev(sc->sc_dev, "mmap() rejected.\n");
331 return -1;
332 }
333
334 #ifdef WSFB_FAKE_VGA_FB
335 if ((offset >= 0xa0000) && (offset < 0xbffff)) {
336
337 return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
338 offset - 0xa0000, prot, BUS_SPACE_MAP_LINEAR);
339 }
340 #endif
341
342 /*
343 * XXX this should be generalized, let's just
344 * #define PCI_IOAREA_PADDR
345 * #define PCI_IOAREA_OFFSET
346 * #define PCI_IOAREA_SIZE
347 * somewhere in a MD header and compile this code only if all are
348 * present
349 */
350 #ifdef PCI_MAGIC_IO_RANGE
351 /* allow to map our IO space */
352 if ((offset >= PCI_MAGIC_IO_RANGE) &&
353 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
354 return bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
355 0, prot, BUS_SPACE_MAP_LINEAR);
356 }
357 #endif
358 return -1;
359 }
360
361 static void
362 wcfb_init_screen(void *cookie, struct vcons_screen *scr,
363 int existing, long *defattr)
364 {
365 struct wcfb_softc *sc = cookie;
366 struct rasops_info *ri = &scr->scr_ri;
367
368 ri->ri_depth = 8;
369 ri->ri_width = sc->sc_width;
370 ri->ri_height = sc->sc_height;
371 ri->ri_stride = sc->sc_stride;
372 ri->ri_flg = RI_CENTER /*| RI_FULLCLEAR*/;
373
374 ri->ri_bits = (char *)sc->sc_fbaddr + sc->sc_fb0off;
375
376 if (existing) {
377 ri->ri_flg |= RI_CLEAR;
378 }
379
380 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
381 ri->ri_caps = WSSCREEN_WSCOLORS;
382
383 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
384 sc->sc_width / ri->ri_font->fontwidth);
385
386 ri->ri_hw = scr;
387 sc->putchar = ri->ri_ops.putchar;
388 sc->copyrows = ri->ri_ops.copyrows;
389 sc->eraserows = ri->ri_ops.eraserows;
390 sc->copycols = ri->ri_ops.copycols;
391 sc->erasecols = ri->ri_ops.erasecols;
392
393 ri->ri_ops.copyrows = wcfb_copyrows;
394 ri->ri_ops.copycols = wcfb_copycols;
395 ri->ri_ops.eraserows = wcfb_eraserows;
396 ri->ri_ops.erasecols = wcfb_erasecols;
397 ri->ri_ops.putchar = wcfb_putchar;
398 ri->ri_ops.cursor = wcfb_cursor;
399 }
400
401 static void
402 wcfb_putchar(void *cookie, int row, int col, u_int c, long attr)
403 {
404 struct rasops_info *ri = cookie;
405 struct vcons_screen *scr = ri->ri_hw;
406 struct wcfb_softc *sc = scr->scr_cookie;
407
408 sc->putchar(ri, row, col, c, attr);
409 ri->ri_bits += 0x200000;
410 sc->putchar(ri, row, col, c, attr);
411 ri->ri_bits -= 0x200000;
412 }
413
414 static void
415 wcfb_putpalreg(struct wcfb_softc *sc, int i, int r, int g, int b)
416 {
417 uint32_t rgb;
418
419 bus_space_write_4(sc->sc_regt, sc->sc_regh, 0x80bc, i);
420 rgb = (b << 22) | (g << 12) | (r << 2);
421 bus_space_write_4(sc->sc_regt, sc->sc_regh, 0x80c0, rgb);
422 }
423
424 static void
425 wcfb_cursor(void *cookie, int on, int row, int col)
426 {
427 #if 0
428 struct rasops_info *ri = cookie;
429 struct vcons_screen *scr = ri->ri_hw;
430 struct wcfb_softc *sc = scr->scr_cookie;
431 #endif
432 }
433
434 static void
435 wcfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
436 {
437 struct rasops_info *ri = cookie;
438 struct vcons_screen *scr = ri->ri_hw;
439 struct wcfb_softc *sc = scr->scr_cookie;
440
441 sc->copycols(ri, row, srccol, dstcol, ncols);
442 ri->ri_bits += 0x200000;
443 sc->copycols(ri, row, srccol, dstcol, ncols);
444 ri->ri_bits -= 0x200000;
445 }
446
447 static void
448 wcfb_erasecols(void *cookie, int row, int startcol, int ncol, long fillattr)
449 {
450 struct rasops_info *ri = cookie;
451 struct vcons_screen *scr = ri->ri_hw;
452 struct wcfb_softc *sc = scr->scr_cookie;
453
454 sc->erasecols(ri, row, startcol, ncol, fillattr);
455 ri->ri_bits += 0x200000;
456 sc->erasecols(ri, row, startcol, ncol, fillattr);
457 ri->ri_bits -= 0x200000;
458 }
459
460 static void
461 wcfb_copyrows(void *cookie, int srcrow, int dstrow, int nrow)
462 {
463 struct rasops_info *ri = cookie;
464 struct vcons_screen *scr = ri->ri_hw;
465 struct wcfb_softc *sc = scr->scr_cookie;
466
467 sc->copyrows(ri, srcrow, dstrow, nrow);
468 ri->ri_bits += 0x200000;
469 sc->copyrows(ri, srcrow, dstrow, nrow);
470 ri->ri_bits -= 0x200000;
471 }
472
473 static void
474 wcfb_eraserows(void *cookie, int row, int nrows, long fillattr)
475 {
476 struct rasops_info *ri = cookie;
477 struct vcons_screen *scr = ri->ri_hw;
478 struct wcfb_softc *sc = scr->scr_cookie;
479
480 sc->eraserows(ri, row, nrows, fillattr);
481 ri->ri_bits += 0x200000;
482 sc->eraserows(ri, row, nrows, fillattr);
483 ri->ri_bits -= 0x200000;
484 }
485