ps3_syscons.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #include <sys/cdefs.h>
  29. __FBSDID("$FreeBSD$");
  30. #include <sys/param.h>
  31. #include <sys/systm.h>
  32. #include <sys/kernel.h>
  33. #include <sys/sysctl.h>
  34. #include <sys/limits.h>
  35. #include <sys/conf.h>
  36. #include <sys/cons.h>
  37. #include <sys/fbio.h>
  38. #include <vm/vm.h>
  39. #include <vm/pmap.h>
  40. #include <machine/platform.h>
  41. #include <dev/ofw/openfirm.h>
  42. #include <dev/vt/vt.h>
  43. #include <dev/vt/hw/fb/vt_fb.h>
  44. #include <dev/vt/colors/vt_termcolors.h>
  45. #include "ps3-hvcall.h"
  46. #define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_MODE_SET 0x0100
  47. #define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC 0x0101
  48. #define L1GPU_DISPLAY_SYNC_HSYNC 1
  49. #define L1GPU_DISPLAY_SYNC_VSYNC 2
  50. #define L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP 0x0102
  51. static vd_init_t ps3fb_init;
  52. static vd_probe_t ps3fb_probe;
  53. void ps3fb_remap(void);
  54. struct ps3fb_softc {
  55. struct fb_info fb_info;
  56. uint64_t sc_fbhandle;
  57. uint64_t sc_fbcontext;
  58. uint64_t sc_dma_control;
  59. uint64_t sc_driver_info;
  60. uint64_t sc_reports;
  61. uint64_t sc_reports_size;
  62. };
  63. static struct vt_driver vt_ps3fb_driver = {
  64. .vd_name = "ps3fb",
  65. .vd_probe = ps3fb_probe,
  66. .vd_init = ps3fb_init,
  67. .vd_blank = vt_fb_blank,
  68. .vd_bitblt_text = vt_fb_bitblt_text,
  69. .vd_bitblt_bmp = vt_fb_bitblt_bitmap,
  70. .vd_drawrect = vt_fb_drawrect,
  71. .vd_setpixel = vt_fb_setpixel,
  72. .vd_fb_ioctl = vt_fb_ioctl,
  73. .vd_fb_mmap = vt_fb_mmap,
  74. /* Better than VGA, but still generic driver. */
  75. .vd_priority = VD_PRIORITY_GENERIC + 1,
  76. };
  77. VT_DRIVER_DECLARE(vt_ps3fb, vt_ps3fb_driver);
  78. static struct ps3fb_softc ps3fb_softc;
  79. static int
  80. ps3fb_probe(struct vt_device *vd)
  81. {
  82. struct ps3fb_softc *sc;
  83. int disable;
  84. char compatible[64];
  85. phandle_t root;
  86. disable = 0;
  87. TUNABLE_INT_FETCH("hw.syscons.disable", &disable);
  88. if (disable != 0)
  89. return (0);
  90. sc = &ps3fb_softc;
  91. TUNABLE_STR_FETCH("hw.platform", compatible, sizeof(compatible));
  92. if (strcmp(compatible, "ps3") == 0)
  93. return (CN_INTERNAL);
  94. root = OF_finddevice("/");
  95. if (OF_getprop(root, "compatible", compatible, sizeof(compatible)) <= 0)
  96. return (CN_DEAD);
  97. if (strncmp(compatible, "sony,ps3", sizeof(compatible)) != 0)
  98. return (CN_DEAD);
  99. return (CN_INTERNAL);
  100. }
  101. void
  102. ps3fb_remap(void)
  103. {
  104. struct ps3fb_softc *sc;
  105. vm_offset_t va, fb_paddr;
  106. sc = &ps3fb_softc;
  107. lv1_gpu_close();
  108. lv1_gpu_open(0);
  109. lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_MODE_SET,
  110. 0,0,0,0);
  111. lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_MODE_SET,
  112. 0,0,1,0);
  113. lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
  114. 0,L1GPU_DISPLAY_SYNC_VSYNC,0,0);
  115. lv1_gpu_context_attribute(0, L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_SYNC,
  116. 1,L1GPU_DISPLAY_SYNC_VSYNC,0,0);
  117. lv1_gpu_memory_allocate(roundup2(sc->fb_info.fb_size, 1024*1024),
  118. 0, 0, 0, 0, &sc->sc_fbhandle, &fb_paddr);
  119. lv1_gpu_context_allocate(sc->sc_fbhandle, 0, &sc->sc_fbcontext,
  120. &sc->sc_dma_control, &sc->sc_driver_info, &sc->sc_reports,
  121. &sc->sc_reports_size);
  122. lv1_gpu_context_attribute(sc->sc_fbcontext,
  123. L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP, 0, 0, 0, 0);
  124. lv1_gpu_context_attribute(sc->sc_fbcontext,
  125. L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP, 1, 0, 0, 0);
  126. sc->fb_info.fb_pbase = fb_paddr;
  127. for (va = 0; va < sc->fb_info.fb_size; va += PAGE_SIZE)
  128. pmap_kenter_attr(0x10000000 + va, fb_paddr + va,
  129. VM_MEMATTR_WRITE_COMBINING);
  130. sc->fb_info.fb_flags &= ~FB_FLAG_NOWRITE;
  131. }
  132. static int
  133. ps3fb_init(struct vt_device *vd)
  134. {
  135. struct ps3fb_softc *sc;
  136. char linux_video_mode[24];
  137. int linux_video_mode_num = 0;
  138. /* Init softc */
  139. vd->vd_softc = sc = &ps3fb_softc;
  140. sc->fb_info.fb_depth = 32;
  141. sc->fb_info.fb_height = 1080;
  142. sc->fb_info.fb_width = 1920;
  143. /* See if the bootloader has passed a graphics mode to use */
  144. bzero(linux_video_mode, sizeof(linux_video_mode));
  145. TUNABLE_STR_FETCH("video", linux_video_mode, sizeof(linux_video_mode));
  146. sscanf(linux_video_mode, "ps3fb:mode:%d", &linux_video_mode_num);
  147. switch (linux_video_mode_num) {
  148. case 1:
  149. case 2:
  150. sc->fb_info.fb_height = 480;
  151. sc->fb_info.fb_width = 720;
  152. break;
  153. case 3:
  154. case 8:
  155. sc->fb_info.fb_height = 720;
  156. sc->fb_info.fb_width = 1280;
  157. break;
  158. case 4:
  159. case 5:
  160. case 9:
  161. case 10:
  162. sc->fb_info.fb_height = 1080;
  163. sc->fb_info.fb_width = 1920;
  164. break;
  165. case 6:
  166. case 7:
  167. sc->fb_info.fb_height = 576;
  168. sc->fb_info.fb_width = 720;
  169. break;
  170. case 11:
  171. sc->fb_info.fb_height = 768;
  172. sc->fb_info.fb_width = 1024;
  173. break;
  174. case 12:
  175. sc->fb_info.fb_height = 1024;
  176. sc->fb_info.fb_width = 1280;
  177. break;
  178. case 13:
  179. sc->fb_info.fb_height = 1200;
  180. sc->fb_info.fb_width = 1920;
  181. break;
  182. }
  183. /* Allow explicitly-specified values for us to override everything */
  184. TUNABLE_INT_FETCH("hw.ps3fb.height", &sc->fb_info.fb_height);
  185. TUNABLE_INT_FETCH("hw.ps3fb.width", &sc->fb_info.fb_width);
  186. sc->fb_info.fb_stride = sc->fb_info.fb_width*4;
  187. sc->fb_info.fb_size = sc->fb_info.fb_height * sc->fb_info.fb_stride;
  188. sc->fb_info.fb_bpp = sc->fb_info.fb_stride / sc->fb_info.fb_width * 8;
  189. /*
  190. * Arbitrarily choose address for the framebuffer
  191. */
  192. sc->fb_info.fb_vbase = 0x10000000;
  193. sc->fb_info.fb_flags |= FB_FLAG_NOWRITE; /* Not available yet */
  194. sc->fb_info.fb_cmsize = 16;
  195. /* 32-bit VGA palette */
  196. vt_generate_cons_palette(sc->fb_info.fb_cmap, COLOR_FORMAT_RGB,
  197. 255, 16, 255, 8, 255, 0);
  198. /* Set correct graphics context */
  199. lv1_gpu_context_attribute(sc->sc_fbcontext,
  200. L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP, 0, 0, 0, 0);
  201. lv1_gpu_context_attribute(sc->sc_fbcontext,
  202. L1GPU_CONTEXT_ATTRIBUTE_DISPLAY_FLIP, 1, 0, 0, 0);
  203. vt_fb_init(vd);
  204. return (CN_INTERNAL);
  205. }