2 * Copyright (c) 2003 Peter Grehan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
34 #include <sys/kernel.h>
35 #include <sys/sysctl.h>
36 #include <sys/limits.h>
40 #include <sys/fcntl.h>
41 #include <sys/malloc.h>
43 #include <sys/consio.h>
48 #include <machine/bus.h>
49 #include <machine/sc_machdep.h>
50 #include <machine/platform.h>
51 #include <machine/pmap.h>
55 #include <dev/fb/fbreg.h>
56 #include <dev/syscons/syscons.h>
58 #include "ps3-hvcall.h"
60 #define PS3FB_SIZE (4*1024*1024)
62 #define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_MODE_SET 0x0100
63 #define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC 0x0101
64 #define L1GPU_DISPLAY_SYNC_HSYNC 1
65 #define L1GPU_DISPLAY_SYNC_VSYNC 2
66 #define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP 0x0102
68 extern u_char dflt_font_16[];
69 extern u_char dflt_font_14[];
70 extern u_char dflt_font_8[];
72 static int ps3fb_configure(int flags);
73 void ps3fb_remap(void);
75 static vi_probe_t ps3fb_probe;
76 static vi_init_t ps3fb_init;
77 static vi_get_info_t ps3fb_get_info;
78 static vi_query_mode_t ps3fb_query_mode;
79 static vi_set_mode_t ps3fb_set_mode;
80 static vi_save_font_t ps3fb_save_font;
81 static vi_load_font_t ps3fb_load_font;
82 static vi_show_font_t ps3fb_show_font;
83 static vi_save_palette_t ps3fb_save_palette;
84 static vi_load_palette_t ps3fb_load_palette;
85 static vi_set_border_t ps3fb_set_border;
86 static vi_save_state_t ps3fb_save_state;
87 static vi_load_state_t ps3fb_load_state;
88 static vi_set_win_org_t ps3fb_set_win_org;
89 static vi_read_hw_cursor_t ps3fb_read_hw_cursor;
90 static vi_set_hw_cursor_t ps3fb_set_hw_cursor;
91 static vi_set_hw_cursor_shape_t ps3fb_set_hw_cursor_shape;
92 static vi_blank_display_t ps3fb_blank_display;
93 static vi_mmap_t ps3fb_mmap;
94 static vi_ioctl_t ps3fb_ioctl;
95 static vi_clear_t ps3fb_clear;
96 static vi_fill_rect_t ps3fb_fill_rect;
97 static vi_bitblt_t ps3fb_bitblt;
98 static vi_diag_t ps3fb_diag;
99 static vi_save_cursor_palette_t ps3fb_save_cursor_palette;
100 static vi_load_cursor_palette_t ps3fb_load_cursor_palette;
101 static vi_copy_t ps3fb_copy;
102 static vi_putp_t ps3fb_putp;
103 static vi_putc_t ps3fb_putc;
104 static vi_puts_t ps3fb_puts;
105 static vi_putm_t ps3fb_putm;
108 video_adapter_t sc_va;
126 static video_switch_t ps3fbvidsw = {
127 .probe = ps3fb_probe,
129 .get_info = ps3fb_get_info,
130 .query_mode = ps3fb_query_mode,
131 .set_mode = ps3fb_set_mode,
132 .save_font = ps3fb_save_font,
133 .load_font = ps3fb_load_font,
134 .show_font = ps3fb_show_font,
135 .save_palette = ps3fb_save_palette,
136 .load_palette = ps3fb_load_palette,
137 .set_border = ps3fb_set_border,
138 .save_state = ps3fb_save_state,
139 .load_state = ps3fb_load_state,
140 .set_win_org = ps3fb_set_win_org,
141 .read_hw_cursor = ps3fb_read_hw_cursor,
142 .set_hw_cursor = ps3fb_set_hw_cursor,
143 .set_hw_cursor_shape = ps3fb_set_hw_cursor_shape,
144 .blank_display = ps3fb_blank_display,
146 .ioctl = ps3fb_ioctl,
147 .clear = ps3fb_clear,
148 .fill_rect = ps3fb_fill_rect,
149 .bitblt = ps3fb_bitblt,
151 .save_cursor_palette = ps3fb_save_cursor_palette,
152 .load_cursor_palette = ps3fb_load_cursor_palette,
160 VIDEO_DRIVER(ps3fb, ps3fbvidsw, ps3fb_configure);
162 extern sc_rndr_sw_t txtrndrsw;
163 RENDERER(ps3fb, 0, txtrndrsw, gfb_set);
165 RENDERER_MODULE(ps3fb, gfb_set);
168 * Define the iso6429-1983 colormap
174 } ps3fb_cmap[16] = { /* # R G B Color */
176 { 0x00, 0x00, 0x00 }, /* 0 0 0 0 Black */
177 { 0x00, 0x00, 0xaa }, /* 1 0 0 2/3 Blue */
178 { 0x00, 0xaa, 0x00 }, /* 2 0 2/3 0 Green */
179 { 0x00, 0xaa, 0xaa }, /* 3 0 2/3 2/3 Cyan */
180 { 0xaa, 0x00, 0x00 }, /* 4 2/3 0 0 Red */
181 { 0xaa, 0x00, 0xaa }, /* 5 2/3 0 2/3 Magenta */
182 { 0xaa, 0x55, 0x00 }, /* 6 2/3 1/3 0 Brown */
183 { 0xaa, 0xaa, 0xaa }, /* 7 2/3 2/3 2/3 White */
184 { 0x55, 0x55, 0x55 }, /* 8 1/3 1/3 1/3 Gray */
185 { 0x55, 0x55, 0xff }, /* 9 1/3 1/3 1 Bright Blue */
186 { 0x55, 0xff, 0x55 }, /* 10 1/3 1 1/3 Bright Green */
187 { 0x55, 0xff, 0xff }, /* 11 1/3 1 1 Bright Cyan */
188 { 0xff, 0x55, 0x55 }, /* 12 1 1/3 1/3 Bright Red */
189 { 0xff, 0x55, 0xff }, /* 13 1 1/3 1 Bright Magenta */
190 { 0xff, 0xff, 0x80 }, /* 14 1 1 1/3 Bright Yellow */
191 { 0xff, 0xff, 0xff } /* 15 1 1 1 Bright White */
194 #define TODO printf("%s: unimplemented\n", __func__)
196 static u_int16_t ps3fb_static_window[ROW*COL];
198 static struct ps3fb_softc ps3fb_softc;
201 ps3fb_background(uint8_t attr)
207 ps3fb_foreground(uint8_t attr)
209 return (attr & 0x0f);
213 ps3fb_pix32(int attr)
217 retval = (ps3fb_cmap[attr].red << 16) |
218 (ps3fb_cmap[attr].green << 8) |
219 ps3fb_cmap[attr].blue;
225 ps3fb_configure(int flags)
227 struct ps3fb_softc *sc;
236 TUNABLE_INT_FETCH("hw.syscons.disable", &disable);
247 root = OF_finddevice("/");
248 if (OF_getprop(root, "compatible", compatible, sizeof(compatible)) <= 0)
251 if (strncmp(compatible, "sony,ps3", sizeof(compatible)) != 0)
254 TUNABLE_STR_FETCH("hw.platform", compatible, sizeof(compatible));
255 if (strcmp(compatible, "ps3") != 0)
261 /* XXX: get from HV repository */
264 sc->sc_stride = sc->sc_width*4;
267 * The loader puts the FB at 0x10000000, so use that for now.
270 sc->sc_addr = 0x10000000;
271 ps3fb_init(0, &sc->sc_va, 0);
279 vm_offset_t va, fb_paddr;
280 uint64_t fbhandle, fbcontext;
285 lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_MODE_SET,
287 lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_MODE_SET,
289 lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
290 0,L1GPU_DISPLAY_SYNC_VSYNC,0,0);
291 lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
292 1,L1GPU_DISPLAY_SYNC_VSYNC,0,0);
293 lv1_gpu_memory_allocate(PS3FB_SIZE, 0, 0, 0, 0, &fbhandle, &fb_paddr);
294 lv1_gpu_context_allocate(fbhandle, 0, &fbcontext);
296 lv1_gpu_context_attribute(fbcontext,
297 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP, 0, 0, 0, 0);
298 lv1_gpu_context_attribute(fbcontext,
299 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP, 1, 0, 0, 0);
301 for (va = 0; va < PS3FB_SIZE; va += PAGE_SIZE)
302 pmap_kenter_attr(0x10000000 + va, fb_paddr + va,
303 VM_MEMATTR_WRITE_COMBINING);
307 ps3fb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
314 ps3fb_init(int unit, video_adapter_t *adp, int flags)
316 struct ps3fb_softc *sc;
318 int cxborder, cyborder;
321 sc = (struct ps3fb_softc *)adp;
324 vid_init_struct(adp, "ps3fb", -1, unit);
326 /* The default font size can be overridden by loader */
328 TUNABLE_INT_FETCH("hw.syscons.fsize", &font_height);
329 if (font_height == 8) {
330 sc->sc_font = dflt_font_8;
331 sc->sc_font_height = 8;
332 } else if (font_height == 14) {
333 sc->sc_font = dflt_font_14;
334 sc->sc_font_height = 14;
336 /* default is 8x16 */
337 sc->sc_font = dflt_font_16;
338 sc->sc_font_height = 16;
341 /* The user can set a border in chars - default is 1 char width */
344 TUNABLE_INT_FETCH("hw.syscons.xborder", &cxborder);
345 TUNABLE_INT_FETCH("hw.syscons.yborder", &cyborder);
347 vi->vi_cheight = sc->sc_font_height;
348 vi->vi_width = sc->sc_width/8 - 2*cxborder;
349 vi->vi_height = sc->sc_height/sc->sc_font_height - 2*cyborder;
353 * Clamp width/height to syscons maximums
355 if (vi->vi_width > COL)
357 if (vi->vi_height > ROW)
360 sc->sc_xmargin = (sc->sc_width - (vi->vi_width * vi->vi_cwidth)) / 2;
361 sc->sc_ymargin = (sc->sc_height - (vi->vi_height * vi->vi_cheight))/2;
364 * Avoid huge amounts of conditional code in syscons by
365 * defining a dummy h/w text display buffer.
367 adp->va_window = (vm_offset_t) ps3fb_static_window;
370 * Enable future font-loading and flag color support, as well as
371 * adding V_ADP_MODECHANGE so that we ps3fb_set_mode() gets called
372 * when the X server shuts down. This enables us to get the console
373 * back when X disappears.
375 adp->va_flags |= V_ADP_FONT | V_ADP_COLOR | V_ADP_MODECHANGE;
377 ps3fb_set_mode(&sc->sc_va, 0);
378 vid_register(&sc->sc_va);
384 ps3fb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
386 bcopy(&adp->va_info, info, sizeof(*info));
391 ps3fb_query_mode(video_adapter_t *adp, video_info_t *info)
398 ps3fb_set_mode(video_adapter_t *adp, int mode)
400 struct ps3fb_softc *sc;
402 sc = (struct ps3fb_softc *)adp;
404 /* XXX: no real mode setting at the moment */
406 ps3fb_blank_display(&sc->sc_va, V_DISPLAY_ON);
412 ps3fb_save_font(video_adapter_t *adp, int page, int size, int width,
413 u_char *data, int c, int count)
420 ps3fb_load_font(video_adapter_t *adp, int page, int size, int width,
421 u_char *data, int c, int count)
423 struct ps3fb_softc *sc;
425 sc = (struct ps3fb_softc *)adp;
428 * syscons code has already determined that current width/height
429 * are unchanged for this new font
436 ps3fb_show_font(video_adapter_t *adp, int page)
443 ps3fb_save_palette(video_adapter_t *adp, u_char *palette)
450 ps3fb_load_palette(video_adapter_t *adp, u_char *palette)
457 ps3fb_set_border(video_adapter_t *adp, int border)
459 /* XXX Be lazy for now and blank entire screen */
460 return (ps3fb_blank_display(adp, border));
464 ps3fb_save_state(video_adapter_t *adp, void *p, size_t size)
471 ps3fb_load_state(video_adapter_t *adp, void *p)
478 ps3fb_set_win_org(video_adapter_t *adp, off_t offset)
485 ps3fb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
494 ps3fb_set_hw_cursor(video_adapter_t *adp, int col, int row)
501 ps3fb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
502 int celsize, int blink)
508 ps3fb_blank_display(video_adapter_t *adp, int mode)
510 struct ps3fb_softc *sc;
514 sc = (struct ps3fb_softc *)adp;
515 addr = (uint32_t *) sc->sc_addr;
517 for (i = 0; i < (sc->sc_stride/4)*sc->sc_height; i++)
518 *(addr + i) = ps3fb_pix32(ps3fb_background(SC_NORM_ATTR));
524 ps3fb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
525 int prot, vm_memattr_t *memattr)
527 struct ps3fb_softc *sc;
529 sc = (struct ps3fb_softc *)adp;
532 * This might be a legacy VGA mem request: if so, just point it at the
533 * framebuffer, since it shouldn't be touched
535 if (offset < sc->sc_stride*sc->sc_height) {
536 *paddr = sc->sc_addr + offset;
544 ps3fb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
551 ps3fb_clear(video_adapter_t *adp)
558 ps3fb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
565 ps3fb_bitblt(video_adapter_t *adp, ...)
572 ps3fb_diag(video_adapter_t *adp, int level)
579 ps3fb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
586 ps3fb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
593 ps3fb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
600 ps3fb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
601 int size, int bpp, int bit_ltor, int byte_ltor)
608 ps3fb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
610 struct ps3fb_softc *sc;
617 sc = (struct ps3fb_softc *)adp;
618 row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
619 col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
620 p = sc->sc_font + c*sc->sc_font_height;
621 addr = (uint32_t *)sc->sc_addr
622 + (row + sc->sc_ymargin)*(sc->sc_stride/4)
623 + col + sc->sc_xmargin;
625 for (i = 0; i < sc->sc_font_height; i++) {
626 for (j = 0, k = 7; j < 8; j++, k--) {
627 if ((p[i] & (1 << k)) == 0)
628 *(addr + j) = ps3fb_pix32(ps3fb_background(a));
630 *(addr + j) = ps3fb_pix32(ps3fb_foreground(a));
632 addr += (sc->sc_stride/4);
639 ps3fb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
643 for (i = 0; i < len; i++) {
644 ps3fb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
650 ps3fb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
651 uint32_t pixel_mask, int size, int width)
653 struct ps3fb_softc *sc;
658 sc = (struct ps3fb_softc *)adp;
659 addr = (uint32_t *)sc->sc_addr
660 + (y + sc->sc_ymargin)*(sc->sc_stride/4)
661 + x + sc->sc_xmargin;
663 fg = ps3fb_pix32(ps3fb_foreground(SC_NORM_ATTR));
664 bg = ps3fb_pix32(ps3fb_background(SC_NORM_ATTR));
666 for (i = 0; i < size && i+y < sc->sc_height - 2*sc->sc_ymargin; i++) {
667 for (j = 0, k = width; j < 8; j++, k--) {
668 if (x + j >= sc->sc_width - 2*sc->sc_xmargin)
671 if (pixel_image[i] & (1 << k))
672 *(addr + j) = (*(addr + j) == fg) ? bg : fg;
674 addr += (sc->sc_stride/4);
681 * Define the syscons nexus device attachment
684 ps3fb_scidentify(driver_t *driver, device_t parent)
689 * Add with a priority guaranteed to make it last on
692 if (strcmp(installed_platform(), "ps3") == 0)
693 child = BUS_ADD_CHILD(parent, INT_MAX, SC_DRIVER_NAME, 0);
697 ps3fb_scprobe(device_t dev)
701 if (strcmp(installed_platform(), "ps3") != 0)
704 device_set_desc(dev, "System console");
706 error = sc_probe_unit(device_get_unit(dev),
707 device_get_flags(dev) | SC_AUTODETECT_KBD);
711 /* This is a fake device, so make sure we added it ourselves */
712 return (BUS_PROBE_NOWILDCARD);
716 ps3fb_scattach(device_t dev)
718 return (sc_attach_unit(device_get_unit(dev),
719 device_get_flags(dev) | SC_AUTODETECT_KBD));
722 static device_method_t ps3fb_sc_methods[] = {
723 DEVMETHOD(device_identify, ps3fb_scidentify),
724 DEVMETHOD(device_probe, ps3fb_scprobe),
725 DEVMETHOD(device_attach, ps3fb_scattach),
729 static driver_t ps3fb_sc_driver = {
735 static devclass_t sc_devclass;
737 DRIVER_MODULE(sc, nexus, ps3fb_sc_driver, sc_devclass, 0, 0);
740 * Define a stub keyboard driver in case one hasn't been
741 * compiled into the kernel
743 #include <sys/kbio.h>
744 #include <dev/kbd/kbdreg.h>
746 static int dummy_kbd_configure(int flags);
748 keyboard_switch_t ps3dummysw;
751 dummy_kbd_configure(int flags)
756 KEYBOARD_DRIVER(ps3dummy, ps3dummysw, dummy_kbd_configure);