1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2011-2014 Nathan Whitehorn
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 AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/kernel.h>
35#include <sys/sysctl.h>
36#include <sys/limits.h>
37#include <sys/conf.h>
38#include <sys/cons.h>
39#include <sys/fbio.h>
40
41#include <vm/vm.h>
42#include <vm/pmap.h>
43
44#include <machine/platform.h>
45
46#include <dev/ofw/openfirm.h>
47#include <dev/vt/vt.h>
48#include <dev/vt/hw/fb/vt_fb.h>
49#include <dev/vt/colors/vt_termcolors.h>
50
51#include "ps3-hvcall.h"
52
53#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_MODE_SET	0x0100
54#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC		0x0101
55#define  L1GPU_DISPLAY_SYNC_HSYNC			1
56#define  L1GPU_DISPLAY_SYNC_VSYNC			2
57#define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP		0x0102
58
59static vd_init_t ps3fb_init;
60static vd_probe_t ps3fb_probe;
61void ps3fb_remap(void);
62
63struct ps3fb_softc {
64	struct fb_info	fb_info;
65
66	uint64_t	sc_fbhandle;
67	uint64_t	sc_fbcontext;
68	uint64_t	sc_dma_control;
69	uint64_t	sc_driver_info;
70	uint64_t	sc_reports;
71	uint64_t	sc_reports_size;
72};
73
74static struct vt_driver vt_ps3fb_driver = {
75	.vd_name = "ps3fb",
76	.vd_probe = ps3fb_probe,
77	.vd_init = ps3fb_init,
78	.vd_blank = vt_fb_blank,
79	.vd_bitblt_text = vt_fb_bitblt_text,
80	.vd_bitblt_bmp = vt_fb_bitblt_bitmap,
81	.vd_drawrect = vt_fb_drawrect,
82	.vd_setpixel = vt_fb_setpixel,
83	.vd_fb_ioctl = vt_fb_ioctl,
84	.vd_fb_mmap = vt_fb_mmap,
85	/* Better than VGA, but still generic driver. */
86	.vd_priority = VD_PRIORITY_GENERIC + 1,
87};
88
89VT_DRIVER_DECLARE(vt_ps3fb, vt_ps3fb_driver);
90static struct ps3fb_softc ps3fb_softc;
91
92static int
93ps3fb_probe(struct vt_device *vd)
94{
95	struct ps3fb_softc *sc;
96	int disable;
97	char compatible[64];
98	phandle_t root;
99
100	disable = 0;
101	TUNABLE_INT_FETCH("hw.syscons.disable", &disable);
102	if (disable != 0)
103		return (0);
104
105	sc = &ps3fb_softc;
106
107	TUNABLE_STR_FETCH("hw.platform", compatible, sizeof(compatible));
108	if (strcmp(compatible, "ps3") == 0)
109		return (CN_INTERNAL);
110
111	root = OF_finddevice("/");
112	if (OF_getprop(root, "compatible", compatible, sizeof(compatible)) <= 0)
113                return (CN_DEAD);
114
115	if (strncmp(compatible, "sony,ps3", sizeof(compatible)) != 0)
116		return (CN_DEAD);
117
118	return (CN_INTERNAL);
119}
120
121void
122ps3fb_remap(void)
123{
124	struct ps3fb_softc *sc;
125	vm_offset_t va, fb_paddr;
126
127	sc = &ps3fb_softc;
128
129	lv1_gpu_close();
130	lv1_gpu_open(0);
131
132	lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_MODE_SET,
133	    0,0,0,0);
134	lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_MODE_SET,
135	    0,0,1,0);
136	lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
137	    0,L1GPU_DISPLAY_SYNC_VSYNC,0,0);
138	lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
139	    1,L1GPU_DISPLAY_SYNC_VSYNC,0,0);
140	lv1_gpu_memory_allocate(roundup2(sc->fb_info.fb_size, 1024*1024),
141	    0, 0, 0, 0, &sc->sc_fbhandle, &fb_paddr);
142	lv1_gpu_context_allocate(sc->sc_fbhandle, 0, &sc->sc_fbcontext,
143	    &sc->sc_dma_control, &sc->sc_driver_info, &sc->sc_reports,
144	    &sc->sc_reports_size);
145
146	lv1_gpu_context_attribute(sc->sc_fbcontext,
147	    L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP, 0, 0, 0, 0);
148	lv1_gpu_context_attribute(sc->sc_fbcontext,
149	    L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP, 1, 0, 0, 0);
150
151	sc->fb_info.fb_pbase = fb_paddr;
152	for (va = 0; va < sc->fb_info.fb_size; va += PAGE_SIZE)
153		pmap_kenter_attr(0x10000000 + va, fb_paddr + va,
154		    VM_MEMATTR_WRITE_COMBINING);
155	sc->fb_info.fb_flags &= ~FB_FLAG_NOWRITE;
156}
157
158static int
159ps3fb_init(struct vt_device *vd)
160{
161	struct ps3fb_softc *sc;
162	char linux_video_mode[24];
163	int linux_video_mode_num = 0;
164
165	/* Init softc */
166	vd->vd_softc = sc = &ps3fb_softc;
167
168	sc->fb_info.fb_depth = 32;
169	sc->fb_info.fb_height = 1080;
170	sc->fb_info.fb_width = 1920;
171
172	/* See if the bootloader has passed a graphics mode to use */
173	bzero(linux_video_mode, sizeof(linux_video_mode));
174	TUNABLE_STR_FETCH("video", linux_video_mode, sizeof(linux_video_mode));
175	sscanf(linux_video_mode, "ps3fb:mode:%d", &linux_video_mode_num);
176
177	switch (linux_video_mode_num) {
178	case 1:
179	case 2:
180		sc->fb_info.fb_height = 480;
181		sc->fb_info.fb_width = 720;
182		break;
183	case 3:
184	case 8:
185		sc->fb_info.fb_height = 720;
186		sc->fb_info.fb_width = 1280;
187		break;
188	case 4:
189	case 5:
190	case 9:
191	case 10:
192		sc->fb_info.fb_height = 1080;
193		sc->fb_info.fb_width = 1920;
194		break;
195	case 6:
196	case 7:
197		sc->fb_info.fb_height = 576;
198		sc->fb_info.fb_width = 720;
199		break;
200	case 11:
201		sc->fb_info.fb_height = 768;
202		sc->fb_info.fb_width = 1024;
203		break;
204	case 12:
205		sc->fb_info.fb_height = 1024;
206		sc->fb_info.fb_width = 1280;
207		break;
208	case 13:
209		sc->fb_info.fb_height = 1200;
210		sc->fb_info.fb_width = 1920;
211		break;
212	}
213
214	/* Allow explicitly-specified values for us to override everything */
215	TUNABLE_INT_FETCH("hw.ps3fb.height", &sc->fb_info.fb_height);
216	TUNABLE_INT_FETCH("hw.ps3fb.width", &sc->fb_info.fb_width);
217
218	sc->fb_info.fb_stride = sc->fb_info.fb_width*4;
219	sc->fb_info.fb_size = sc->fb_info.fb_height * sc->fb_info.fb_stride;
220	sc->fb_info.fb_bpp = sc->fb_info.fb_stride / sc->fb_info.fb_width * 8;
221
222	/*
223	 * Arbitrarily choose address for the framebuffer
224	 */
225
226	sc->fb_info.fb_vbase = 0x10000000;
227	sc->fb_info.fb_flags |= FB_FLAG_NOWRITE; /* Not available yet */
228	sc->fb_info.fb_cmsize = 16;
229
230	/* 32-bit VGA palette */
231	vt_generate_cons_palette(sc->fb_info.fb_cmap, COLOR_FORMAT_RGB,
232	    255, 16, 255, 8, 255, 0);
233
234	/* Set correct graphics context */
235	lv1_gpu_context_attribute(sc->sc_fbcontext,
236	    L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP, 0, 0, 0, 0);
237	lv1_gpu_context_attribute(sc->sc_fbcontext,
238	    L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP, 1, 0, 0, 0);
239
240	vt_fb_init(vd);
241
242	return (CN_INTERNAL);
243}
244