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;
125 uint64_t sc_fbhandle;
126 uint64_t sc_fbcontext;
127 uint64_t sc_dma_control;
128 uint64_t sc_driver_info;
130 uint64_t sc_reports_size;
133 static video_switch_t ps3fbvidsw = {
134 .probe = ps3fb_probe,
136 .get_info = ps3fb_get_info,
137 .query_mode = ps3fb_query_mode,
138 .set_mode = ps3fb_set_mode,
139 .save_font = ps3fb_save_font,
140 .load_font = ps3fb_load_font,
141 .show_font = ps3fb_show_font,
142 .save_palette = ps3fb_save_palette,
143 .load_palette = ps3fb_load_palette,
144 .set_border = ps3fb_set_border,
145 .save_state = ps3fb_save_state,
146 .load_state = ps3fb_load_state,
147 .set_win_org = ps3fb_set_win_org,
148 .read_hw_cursor = ps3fb_read_hw_cursor,
149 .set_hw_cursor = ps3fb_set_hw_cursor,
150 .set_hw_cursor_shape = ps3fb_set_hw_cursor_shape,
151 .blank_display = ps3fb_blank_display,
153 .ioctl = ps3fb_ioctl,
154 .clear = ps3fb_clear,
155 .fill_rect = ps3fb_fill_rect,
156 .bitblt = ps3fb_bitblt,
158 .save_cursor_palette = ps3fb_save_cursor_palette,
159 .load_cursor_palette = ps3fb_load_cursor_palette,
167 VIDEO_DRIVER(ps3fb, ps3fbvidsw, ps3fb_configure);
169 extern sc_rndr_sw_t txtrndrsw;
170 RENDERER(ps3fb, 0, txtrndrsw, gfb_set);
172 RENDERER_MODULE(ps3fb, gfb_set);
175 * Define the iso6429-1983 colormap
181 } ps3fb_cmap[16] = { /* # R G B Color */
183 { 0x00, 0x00, 0x00 }, /* 0 0 0 0 Black */
184 { 0x00, 0x00, 0xaa }, /* 1 0 0 2/3 Blue */
185 { 0x00, 0xaa, 0x00 }, /* 2 0 2/3 0 Green */
186 { 0x00, 0xaa, 0xaa }, /* 3 0 2/3 2/3 Cyan */
187 { 0xaa, 0x00, 0x00 }, /* 4 2/3 0 0 Red */
188 { 0xaa, 0x00, 0xaa }, /* 5 2/3 0 2/3 Magenta */
189 { 0xaa, 0x55, 0x00 }, /* 6 2/3 1/3 0 Brown */
190 { 0xaa, 0xaa, 0xaa }, /* 7 2/3 2/3 2/3 White */
191 { 0x55, 0x55, 0x55 }, /* 8 1/3 1/3 1/3 Gray */
192 { 0x55, 0x55, 0xff }, /* 9 1/3 1/3 1 Bright Blue */
193 { 0x55, 0xff, 0x55 }, /* 10 1/3 1 1/3 Bright Green */
194 { 0x55, 0xff, 0xff }, /* 11 1/3 1 1 Bright Cyan */
195 { 0xff, 0x55, 0x55 }, /* 12 1 1/3 1/3 Bright Red */
196 { 0xff, 0x55, 0xff }, /* 13 1 1/3 1 Bright Magenta */
197 { 0xff, 0xff, 0x80 }, /* 14 1 1 1/3 Bright Yellow */
198 { 0xff, 0xff, 0xff } /* 15 1 1 1 Bright White */
201 #define TODO printf("%s: unimplemented\n", __func__)
203 static u_int16_t ps3fb_static_window[ROW*COL];
205 static struct ps3fb_softc ps3fb_softc;
208 ps3fb_background(uint8_t attr)
214 ps3fb_foreground(uint8_t attr)
216 return (attr & 0x0f);
220 ps3fb_pix32(int attr)
224 retval = (ps3fb_cmap[attr].red << 16) |
225 (ps3fb_cmap[attr].green << 8) |
226 ps3fb_cmap[attr].blue;
232 ps3fb_configure(int flags)
234 struct ps3fb_softc *sc;
243 TUNABLE_INT_FETCH("hw.syscons.disable", &disable);
254 root = OF_finddevice("/");
255 if (OF_getprop(root, "compatible", compatible, sizeof(compatible)) <= 0)
258 if (strncmp(compatible, "sony,ps3", sizeof(compatible)) != 0)
261 TUNABLE_STR_FETCH("hw.platform", compatible, sizeof(compatible));
262 if (strcmp(compatible, "ps3") != 0)
268 /* XXX: get from HV repository */
271 sc->sc_stride = sc->sc_width*4;
274 * The loader puts the FB at 0x10000000, so use that for now.
277 sc->sc_addr = 0x10000000;
278 ps3fb_init(0, &sc->sc_va, 0);
286 struct ps3fb_softc *sc;
287 vm_offset_t va, fb_paddr;
294 lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_MODE_SET,
296 lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_MODE_SET,
298 lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
299 0,L1GPU_DISPLAY_SYNC_VSYNC,0,0);
300 lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
301 1,L1GPU_DISPLAY_SYNC_VSYNC,0,0);
302 lv1_gpu_memory_allocate(PS3FB_SIZE, 0, 0, 0, 0, &sc->sc_fbhandle, &fb_paddr);
303 lv1_gpu_context_allocate(sc->sc_fbhandle, 0, &sc->sc_fbcontext, &sc->sc_dma_control,
304 &sc->sc_driver_info, &sc->sc_reports, &sc->sc_reports_size);
306 lv1_gpu_context_attribute(sc->sc_fbcontext,
307 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP, 0, 0, 0, 0);
308 lv1_gpu_context_attribute(sc->sc_fbcontext,
309 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP, 1, 0, 0, 0);
311 for (va = 0; va < PS3FB_SIZE; va += PAGE_SIZE)
312 pmap_kenter_attr(0x10000000 + va, fb_paddr + va,
313 VM_MEMATTR_WRITE_COMBINING);
317 ps3fb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
324 ps3fb_init(int unit, video_adapter_t *adp, int flags)
326 struct ps3fb_softc *sc;
328 int cxborder, cyborder;
331 sc = (struct ps3fb_softc *)adp;
334 vid_init_struct(adp, "ps3fb", -1, unit);
336 /* The default font size can be overridden by loader */
338 TUNABLE_INT_FETCH("hw.syscons.fsize", &font_height);
339 if (font_height == 8) {
340 sc->sc_font = dflt_font_8;
341 sc->sc_font_height = 8;
342 } else if (font_height == 14) {
343 sc->sc_font = dflt_font_14;
344 sc->sc_font_height = 14;
346 /* default is 8x16 */
347 sc->sc_font = dflt_font_16;
348 sc->sc_font_height = 16;
351 /* The user can set a border in chars - default is 1 char width */
354 TUNABLE_INT_FETCH("hw.syscons.xborder", &cxborder);
355 TUNABLE_INT_FETCH("hw.syscons.yborder", &cyborder);
357 vi->vi_cheight = sc->sc_font_height;
358 vi->vi_width = sc->sc_width/8 - 2*cxborder;
359 vi->vi_height = sc->sc_height/sc->sc_font_height - 2*cyborder;
363 * Clamp width/height to syscons maximums
365 if (vi->vi_width > COL)
367 if (vi->vi_height > ROW)
370 sc->sc_xmargin = (sc->sc_width - (vi->vi_width * vi->vi_cwidth)) / 2;
371 sc->sc_ymargin = (sc->sc_height - (vi->vi_height * vi->vi_cheight))/2;
374 * Avoid huge amounts of conditional code in syscons by
375 * defining a dummy h/w text display buffer.
377 adp->va_window = (vm_offset_t) ps3fb_static_window;
380 * Enable future font-loading and flag color support, as well as
381 * adding V_ADP_MODECHANGE so that we ps3fb_set_mode() gets called
382 * when the X server shuts down. This enables us to get the console
383 * back when X disappears.
385 adp->va_flags |= V_ADP_FONT | V_ADP_COLOR | V_ADP_MODECHANGE;
387 ps3fb_set_mode(&sc->sc_va, 0);
388 vid_register(&sc->sc_va);
394 ps3fb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
396 bcopy(&adp->va_info, info, sizeof(*info));
401 ps3fb_query_mode(video_adapter_t *adp, video_info_t *info)
408 ps3fb_set_mode(video_adapter_t *adp, int mode)
410 struct ps3fb_softc *sc;
412 sc = (struct ps3fb_softc *)adp;
414 /* XXX: no real mode setting at the moment */
416 ps3fb_blank_display(&sc->sc_va, V_DISPLAY_ON);
418 lv1_gpu_context_attribute(sc->sc_fbcontext,
419 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP, 0, 0, 0, 0);
420 lv1_gpu_context_attribute(sc->sc_fbcontext,
421 L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP, 1, 0, 0, 0);
427 ps3fb_save_font(video_adapter_t *adp, int page, int size, int width,
428 u_char *data, int c, int count)
435 ps3fb_load_font(video_adapter_t *adp, int page, int size, int width,
436 u_char *data, int c, int count)
438 struct ps3fb_softc *sc;
440 sc = (struct ps3fb_softc *)adp;
443 * syscons code has already determined that current width/height
444 * are unchanged for this new font
451 ps3fb_show_font(video_adapter_t *adp, int page)
458 ps3fb_save_palette(video_adapter_t *adp, u_char *palette)
465 ps3fb_load_palette(video_adapter_t *adp, u_char *palette)
472 ps3fb_set_border(video_adapter_t *adp, int border)
474 /* XXX Be lazy for now and blank entire screen */
475 return (ps3fb_blank_display(adp, border));
479 ps3fb_save_state(video_adapter_t *adp, void *p, size_t size)
486 ps3fb_load_state(video_adapter_t *adp, void *p)
493 ps3fb_set_win_org(video_adapter_t *adp, off_t offset)
500 ps3fb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
509 ps3fb_set_hw_cursor(video_adapter_t *adp, int col, int row)
516 ps3fb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
517 int celsize, int blink)
523 ps3fb_blank_display(video_adapter_t *adp, int mode)
525 struct ps3fb_softc *sc;
529 sc = (struct ps3fb_softc *)adp;
530 addr = (uint32_t *) sc->sc_addr;
532 for (i = 0; i < (sc->sc_stride/4)*sc->sc_height; i++)
533 *(addr + i) = ps3fb_pix32(ps3fb_background(SC_NORM_ATTR));
539 ps3fb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
540 int prot, vm_memattr_t *memattr)
542 struct ps3fb_softc *sc;
544 sc = (struct ps3fb_softc *)adp;
547 * This might be a legacy VGA mem request: if so, just point it at the
548 * framebuffer, since it shouldn't be touched
550 if (offset < sc->sc_stride*sc->sc_height) {
551 *paddr = sc->sc_addr + offset;
559 ps3fb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
566 ps3fb_clear(video_adapter_t *adp)
573 ps3fb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
580 ps3fb_bitblt(video_adapter_t *adp, ...)
587 ps3fb_diag(video_adapter_t *adp, int level)
594 ps3fb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
601 ps3fb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
608 ps3fb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
615 ps3fb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
616 int size, int bpp, int bit_ltor, int byte_ltor)
623 ps3fb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
625 struct ps3fb_softc *sc;
632 sc = (struct ps3fb_softc *)adp;
633 row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
634 col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
635 p = sc->sc_font + c*sc->sc_font_height;
636 addr = (uint32_t *)sc->sc_addr
637 + (row + sc->sc_ymargin)*(sc->sc_stride/4)
638 + col + sc->sc_xmargin;
640 for (i = 0; i < sc->sc_font_height; i++) {
641 for (j = 0, k = 7; j < 8; j++, k--) {
642 if ((p[i] & (1 << k)) == 0)
643 *(addr + j) = ps3fb_pix32(ps3fb_background(a));
645 *(addr + j) = ps3fb_pix32(ps3fb_foreground(a));
647 addr += (sc->sc_stride/4);
654 ps3fb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
658 for (i = 0; i < len; i++) {
659 ps3fb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
665 ps3fb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
666 uint32_t pixel_mask, int size, int width)
668 struct ps3fb_softc *sc;
673 sc = (struct ps3fb_softc *)adp;
674 addr = (uint32_t *)sc->sc_addr
675 + (y + sc->sc_ymargin)*(sc->sc_stride/4)
676 + x + sc->sc_xmargin;
678 fg = ps3fb_pix32(ps3fb_foreground(SC_NORM_ATTR));
679 bg = ps3fb_pix32(ps3fb_background(SC_NORM_ATTR));
681 for (i = 0; i < size && i+y < sc->sc_height - 2*sc->sc_ymargin; i++) {
682 for (j = 0, k = width; j < 8; j++, k--) {
683 if (x + j >= sc->sc_width - 2*sc->sc_xmargin)
686 if (pixel_image[i] & (1 << k))
687 *(addr + j) = (*(addr + j) == fg) ? bg : fg;
689 addr += (sc->sc_stride/4);
696 * Define the syscons nexus device attachment
699 ps3fb_scidentify(driver_t *driver, device_t parent)
704 * Add with a priority guaranteed to make it last on
707 if (strcmp(installed_platform(), "ps3") == 0)
708 child = BUS_ADD_CHILD(parent, INT_MAX, SC_DRIVER_NAME, 0);
712 ps3fb_scprobe(device_t dev)
716 if (strcmp(installed_platform(), "ps3") != 0)
719 device_set_desc(dev, "System console");
721 error = sc_probe_unit(device_get_unit(dev),
722 device_get_flags(dev) | SC_AUTODETECT_KBD);
726 /* This is a fake device, so make sure we added it ourselves */
727 return (BUS_PROBE_NOWILDCARD);
731 ps3fb_scattach(device_t dev)
733 return (sc_attach_unit(device_get_unit(dev),
734 device_get_flags(dev) | SC_AUTODETECT_KBD));
737 static device_method_t ps3fb_sc_methods[] = {
738 DEVMETHOD(device_identify, ps3fb_scidentify),
739 DEVMETHOD(device_probe, ps3fb_scprobe),
740 DEVMETHOD(device_attach, ps3fb_scattach),
744 static driver_t ps3fb_sc_driver = {
750 static devclass_t sc_devclass;
752 DRIVER_MODULE(ps3fb, nexus, ps3fb_sc_driver, sc_devclass, 0, 0);
755 * Define a stub keyboard driver in case one hasn't been
756 * compiled into the kernel
758 #include <sys/kbio.h>
759 #include <dev/kbd/kbdreg.h>
761 static int dummy_kbd_configure(int flags);
763 keyboard_switch_t ps3dummysw;
766 dummy_kbd_configure(int flags)
771 KEYBOARD_DRIVER(ps3dummy, ps3dummysw, dummy_kbd_configure);