lfbvideo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2014-2018 K. Lange
  5. *
  6. * Generic linear framebuffer driver.
  7. *
  8. * Supports several cases:
  9. * - Bochs/QEMU/VirtualBox "Bochs VBE" with modesetting.
  10. * - VMware SVGA with modesetting.
  11. * - Linear framebuffers set by the bootloader with no modesetting.
  12. */
  13. #include <kernel/system.h>
  14. #include <kernel/fs.h>
  15. #include <kernel/printf.h>
  16. #include <kernel/types.h>
  17. #include <kernel/logging.h>
  18. #include <kernel/pci.h>
  19. #include <kernel/boot.h>
  20. #include <kernel/args.h>
  21. #include <kernel/tokenize.h>
  22. #include <kernel/module.h>
  23. #include <kernel/video.h>
  24. #include <kernel/mod/procfs.h>
  25. #define PREFERRED_W 1024
  26. #define PREFERRED_H 768
  27. #define PREFERRED_VY 4096
  28. #define PREFERRED_B 32
  29. /* Exported to other modules */
  30. uint16_t lfb_resolution_x = 0;
  31. uint16_t lfb_resolution_y = 0;
  32. uint16_t lfb_resolution_b = 0;
  33. uint32_t lfb_resolution_s = 0;
  34. uint8_t * lfb_vid_memory = (uint8_t *)0xE0000000;
  35. const char * lfb_driver_name = NULL;
  36. /* Where to send display size change signals */
  37. static pid_t display_change_recipient = 0;
  38. /* Driver-specific modesetting function */
  39. static void (*lfb_resolution_impl)(uint16_t,uint16_t) = NULL;
  40. /* Called by ioctl on /dev/fb0 */
  41. void lfb_set_resolution(uint16_t x, uint16_t y) {
  42. if (lfb_resolution_impl) {
  43. lfb_resolution_impl(x,y);
  44. if (display_change_recipient) {
  45. send_signal(display_change_recipient, SIGWINEVENT, 1);
  46. debug_print(WARNING, "Telling %d to SIGWINEVENT", display_change_recipient);
  47. }
  48. }
  49. }
  50. /**
  51. * Framebuffer control ioctls.
  52. * Used by the compositor to get display sizes and by the
  53. * resolution changer to initiate modesetting.
  54. */
  55. static int ioctl_vid(fs_node_t * node, int request, void * argp) {
  56. switch (request) {
  57. case IO_VID_WIDTH:
  58. /* Get framebuffer width */
  59. validate(argp);
  60. *((size_t *)argp) = lfb_resolution_x;
  61. return 0;
  62. case IO_VID_HEIGHT:
  63. /* Get framebuffer height */
  64. validate(argp);
  65. *((size_t *)argp) = lfb_resolution_y;
  66. return 0;
  67. case IO_VID_DEPTH:
  68. /* Get framebuffer bit depth */
  69. validate(argp);
  70. *((size_t *)argp) = lfb_resolution_b;
  71. return 0;
  72. case IO_VID_STRIDE:
  73. /* Get framebuffer scanline stride */
  74. validate(argp);
  75. *((size_t *)argp) = lfb_resolution_s;
  76. return 0;
  77. case IO_VID_ADDR:
  78. /* Get framebuffer address - TODO: map the framebuffer? */
  79. validate(argp);
  80. *((uintptr_t *)argp) = (uintptr_t)lfb_vid_memory;
  81. return 0;
  82. case IO_VID_SIGNAL:
  83. /* ioctl to register for a signal (vid device change? idk) on display change */
  84. display_change_recipient = getpid();
  85. return 0;
  86. case IO_VID_SET:
  87. /* Initiate mode setting */
  88. validate(argp);
  89. lfb_set_resolution(((struct vid_size *)argp)->width, ((struct vid_size *)argp)->height);
  90. return 0;
  91. case IO_VID_DRIVER:
  92. validate(argp);
  93. memcpy(argp, lfb_driver_name, strlen(lfb_driver_name));
  94. return 0;
  95. default:
  96. return -EINVAL;
  97. }
  98. }
  99. /* Framebuffer device file initializer */
  100. static fs_node_t * lfb_video_device_create(void /* TODO */) {
  101. fs_node_t * fnode = malloc(sizeof(fs_node_t));
  102. memset(fnode, 0x00, sizeof(fs_node_t));
  103. sprintf(fnode->name, "fb0"); /* TODO */
  104. fnode->length = lfb_resolution_s * lfb_resolution_y; /* Size is framebuffer size in bytes */
  105. fnode->flags = FS_BLOCKDEVICE; /* Framebuffers are block devices */
  106. fnode->mask = 0660; /* Only accessible to root user/group */
  107. fnode->ioctl = ioctl_vid; /* control function defined above */
  108. return fnode;
  109. }
  110. /**
  111. * Framebuffer fatal error presentation.
  112. *
  113. * This is called by a kernel hook to render fatal error messages
  114. * (panic / oops / bsod) to the graphraical framebuffer. Mostly,
  115. * that means the "out of memory" error. Bescause this is a fatal
  116. * error condition, we don't care much about speed, so we can do
  117. * silly things like ready from the framebuffer, which we do to
  118. * produce a vignetting and desaturation effect.
  119. */
  120. static int vignette_at(int x, int y) {
  121. int amount = 0;
  122. int level = 100;
  123. if (x < level) amount += (level - x);
  124. if (x > lfb_resolution_x - level) amount += (level - (lfb_resolution_x - x));
  125. if (y < level) amount += (level - y);
  126. if (y > lfb_resolution_y - level) amount += (level - (lfb_resolution_y - y));
  127. return amount;
  128. }
  129. #include "../apps/terminal-font.h"
  130. /* XXX Why is this not defined in the font header... */
  131. #define char_height 20
  132. #define char_width 9
  133. /* Set point in framebuffer */
  134. static void set_point(int x, int y, uint32_t value) {
  135. uint32_t * disp = (uint32_t *)lfb_vid_memory;
  136. uint32_t * cell = &disp[y * (lfb_resolution_s / 4) + x];
  137. *cell = value;
  138. }
  139. /* Draw text on framebuffer */
  140. static void write_char(int x, int y, int val, uint32_t color) {
  141. if (val > 128) {
  142. val = 4;
  143. }
  144. uint16_t * c = large_font[val];
  145. for (uint8_t i = 0; i < char_height; ++i) {
  146. for (uint8_t j = 0; j < char_width; ++j) {
  147. if (c[i] & (1 << (15-j))) {
  148. set_point(x+j,y+i,color);
  149. }
  150. }
  151. }
  152. }
  153. #define _RED(color) ((color & 0x00FF0000) / 0x10000)
  154. #define _GRE(color) ((color & 0x0000FF00) / 0x100)
  155. #define _BLU(color) ((color & 0x000000FF) / 0x1)
  156. #define _ALP(color) ((color & 0xFF000000) / 0x1000000)
  157. static void lfb_video_panic(char ** msgs) {
  158. /* Desaturate the display */
  159. uint32_t * disp = (uint32_t *)lfb_vid_memory;
  160. for (int y = 0; y < lfb_resolution_y; y++) {
  161. for (int x = 0; x < lfb_resolution_x; x++) {
  162. uint32_t * cell = &disp[y * (lfb_resolution_s / 4) + x];
  163. int r = _RED(*cell);
  164. int g = _GRE(*cell);
  165. int b = _BLU(*cell);
  166. int l = 3 * r + 6 * g + 1 * b;
  167. r = (l) / 10;
  168. g = (l) / 10;
  169. b = (l) / 10;
  170. r = r > 255 ? 255 : r;
  171. g = g > 255 ? 255 : g;
  172. b = b > 255 ? 255 : b;
  173. int amount = vignette_at(x,y);
  174. r = (r - amount < 0) ? 0 : r - amount;
  175. g = (g - amount < 0) ? 0 : g - amount;
  176. b = (b - amount < 0) ? 0 : b - amount;
  177. *cell = 0xFF000000 + ((0xFF & r) * 0x10000) + ((0xFF & g) * 0x100) + ((0xFF & b) * 0x1);
  178. }
  179. }
  180. /* Now print the message, divided on line feeds, into the center of the screen */
  181. int num_entries = 0;
  182. for (char ** m = msgs; *m; m++, num_entries++);
  183. int y = (lfb_resolution_y - (num_entries * char_height)) / 2;
  184. for (char ** message = msgs; *message; message++) {
  185. int x = (lfb_resolution_x - (strlen(*message) * char_width)) / 2;
  186. for (char * c = *message; *c; c++) {
  187. write_char(x+1, y+1, *c, 0xFF000000);
  188. write_char(x, y, *c, 0xFFFF0000);
  189. x += char_width;
  190. }
  191. y += char_height;
  192. }
  193. }
  194. static uint32_t framebuffer_func(fs_node_t * node, uint32_t offset, uint32_t size, uint8_t * buffer) {
  195. char * buf = malloc(4096);
  196. if (lfb_driver_name) {
  197. sprintf(buf,
  198. "Driver:\t%s\n"
  199. "XRes:\t%d\n"
  200. "YRes:\t%d\n"
  201. "BitsPerPixel:\t%d\n"
  202. "Stride:\t%d\n"
  203. "Address:\t0x%x\n",
  204. lfb_driver_name,
  205. lfb_resolution_x,
  206. lfb_resolution_y,
  207. lfb_resolution_b,
  208. lfb_resolution_s,
  209. lfb_vid_memory);
  210. } else {
  211. sprintf(buf, "Driver:\tnone\n");
  212. }
  213. size_t _bsize = strlen(buf);
  214. if (offset > _bsize) {
  215. free(buf);
  216. return 0;
  217. }
  218. if (size > _bsize - offset) size = _bsize - offset;
  219. memcpy(buffer, buf + offset, size);
  220. free(buf);
  221. return size;
  222. }
  223. static struct procfs_entry framebuffer_entry = {
  224. 0,
  225. "framebuffer",
  226. framebuffer_func,
  227. };
  228. /* Install framebuffer device */
  229. static void finalize_graphics(const char * driver) {
  230. lfb_driver_name = driver;
  231. fs_node_t * fb_device = lfb_video_device_create();
  232. vfs_mount("/dev/fb0", fb_device);
  233. debug_video_crash = lfb_video_panic;
  234. int (*procfs_install)(struct procfs_entry *) = (int (*)(struct procfs_entry *))(uintptr_t)hashmap_get(modules_get_symbols(),"procfs_install");
  235. if (procfs_install) {
  236. procfs_install(&framebuffer_entry);
  237. }
  238. }
  239. /* Bochs support {{{ */
  240. static void bochs_scan_pci(uint32_t device, uint16_t v, uint16_t d, void * extra) {
  241. if ((v == 0x1234 && d == 0x1111) ||
  242. (v == 0x80EE && d == 0xBEEF) ||
  243. (v == 0x10de && d == 0x0a20)) {
  244. uintptr_t t = pci_read_field(device, PCI_BAR0, 4);
  245. if (t > 0) {
  246. *((uint8_t **)extra) = (uint8_t *)(t & 0xFFFFFFF0);
  247. }
  248. }
  249. }
  250. static void bochs_set_resolution(uint16_t x, uint16_t y) {
  251. outports(0x1CE, 0x04);
  252. outports(0x1CF, 0x00);
  253. /* Uh oh, here we go. */
  254. outports(0x1CE, 0x01);
  255. outports(0x1CF, x);
  256. /* Set Y resolution to 768 */
  257. outports(0x1CE, 0x02);
  258. outports(0x1CF, y);
  259. /* Set bpp to 32 */
  260. outports(0x1CE, 0x03);
  261. outports(0x1CF, PREFERRED_B);
  262. /* Set Virtual Height to stuff */
  263. outports(0x1CE, 0x07);
  264. outports(0x1CF, PREFERRED_VY);
  265. /* Turn it back on */
  266. outports(0x1CE, 0x04);
  267. outports(0x1CF, 0x41);
  268. /* Read X to see if it's something else */
  269. outports(0x1CE, 0x01);
  270. uint16_t new_x = inports(0x1CF);
  271. if (x != new_x) {
  272. x = new_x;
  273. }
  274. lfb_resolution_x = x;
  275. lfb_resolution_s = x * 4;
  276. lfb_resolution_y = y;
  277. lfb_resolution_b = 32;
  278. }
  279. static void graphics_install_bochs(uint16_t resolution_x, uint16_t resolution_y) {
  280. uint32_t vid_memsize;
  281. debug_print(NOTICE, "Setting up BOCHS/QEMU graphics controller...");
  282. outports(0x1CE, 0x00);
  283. uint16_t i = inports(0x1CF);
  284. if (i < 0xB0C0 || i > 0xB0C6) {
  285. return;
  286. }
  287. outports(0x1CF, 0xB0C4);
  288. i = inports(0x1CF);
  289. bochs_set_resolution(resolution_x, resolution_y);
  290. resolution_x = lfb_resolution_x; /* may have changed */
  291. pci_scan(bochs_scan_pci, -1, &lfb_vid_memory);
  292. lfb_resolution_impl = &bochs_set_resolution;
  293. if (!lfb_vid_memory) {
  294. debug_print(ERROR, "Failed to locate video memory.");
  295. return;
  296. }
  297. /* Enable the higher memory */
  298. uintptr_t fb_offset = (uintptr_t)lfb_vid_memory;
  299. for (uintptr_t i = fb_offset; i <= fb_offset + 0xFF0000; i += 0x1000) {
  300. page_t * p = get_page(i, 1, kernel_directory);
  301. dma_frame(p, 0, 1, i);
  302. p->pat = 1;
  303. p->writethrough = 1;
  304. p->cachedisable = 1;
  305. }
  306. outports(0x1CE, 0x0a);
  307. i = inports(0x1CF);
  308. if (i > 1) {
  309. vid_memsize = (uint32_t)i * 64 * 1024;
  310. } else {
  311. vid_memsize = inportl(0x1CF);
  312. }
  313. debug_print(WARNING, "Video memory size is 0x%x", vid_memsize);
  314. for (uintptr_t i = (uintptr_t)lfb_vid_memory; i <= (uintptr_t)lfb_vid_memory + vid_memsize; i += 0x1000) {
  315. dma_frame(get_page(i, 1, kernel_directory), 0, 1, i);
  316. }
  317. finalize_graphics("bochs");
  318. }
  319. static void graphics_install_preset(uint16_t w, uint16_t h) {
  320. if (!(mboot_ptr && (mboot_ptr->flags & (1 << 12)))) {
  321. debug_print(ERROR, "Failed to locate preset video memory - missing multiboot header.");
  322. return;
  323. }
  324. /* Extract framebuffer information from multiboot */
  325. lfb_vid_memory = (void *)mboot_ptr->framebuffer_addr;
  326. lfb_resolution_x = mboot_ptr->framebuffer_width;
  327. lfb_resolution_y = mboot_ptr->framebuffer_height;
  328. lfb_resolution_s = mboot_ptr->framebuffer_pitch;
  329. lfb_resolution_b = 32;
  330. debug_print(WARNING, "Mode was set by bootloader: %dx%d bpp should be 32, framebuffer is at 0x%x", w, h, (uintptr_t)lfb_vid_memory);
  331. for (uintptr_t i = (uintptr_t)lfb_vid_memory; i <= (uintptr_t)lfb_vid_memory + w * h * 4; i += 0x1000) {
  332. page_t * p = get_page(i, 1, kernel_directory);
  333. dma_frame(p, 0, 1, i);
  334. p->pat = 1;
  335. p->writethrough = 1;
  336. p->cachedisable = 1;
  337. }
  338. finalize_graphics("preset");
  339. }
  340. #define SVGA_IO_BASE (vmware_io)
  341. #define SVGA_IO_MUL 1
  342. #define SVGA_INDEX_PORT 0
  343. #define SVGA_VALUE_PORT 1
  344. #define SVGA_REG_ID 0
  345. #define SVGA_REG_ENABLE 1
  346. #define SVGA_REG_WIDTH 2
  347. #define SVGA_REG_HEIGHT 3
  348. #define SVGA_REG_BITS_PER_PIXEL 7
  349. #define SVGA_REG_BYTES_PER_LINE 12
  350. #define SVGA_REG_FB_START 13
  351. static uint32_t vmware_io = 0;
  352. static void vmware_scan_pci(uint32_t device, uint16_t v, uint16_t d, void * extra) {
  353. if ((v == 0x15ad && d == 0x0405)) {
  354. uintptr_t t = pci_read_field(device, PCI_BAR0, 4);
  355. if (t > 0) {
  356. *((uint8_t **)extra) = (uint8_t *)(t & 0xFFFFFFF0);
  357. }
  358. }
  359. }
  360. static void vmware_write(int reg, int value) {
  361. outportl(SVGA_IO_MUL * SVGA_INDEX_PORT + SVGA_IO_BASE, reg);
  362. outportl(SVGA_IO_MUL * SVGA_VALUE_PORT + SVGA_IO_BASE, value);
  363. }
  364. static uint32_t vmware_read(int reg) {
  365. outportl(SVGA_IO_MUL * SVGA_INDEX_PORT + SVGA_IO_BASE, reg);
  366. return inportl(SVGA_IO_MUL * SVGA_VALUE_PORT + SVGA_IO_BASE);
  367. }
  368. static void vmware_set_resolution(uint16_t w, uint16_t h) {
  369. vmware_write(SVGA_REG_ENABLE, 0);
  370. vmware_write(SVGA_REG_ID, 0);
  371. vmware_write(SVGA_REG_WIDTH, w);
  372. vmware_write(SVGA_REG_HEIGHT, h);
  373. vmware_write(SVGA_REG_BITS_PER_PIXEL, 32);
  374. vmware_write(SVGA_REG_ENABLE, 1);
  375. uint32_t bpl = vmware_read(SVGA_REG_BYTES_PER_LINE);
  376. lfb_resolution_x = w;
  377. lfb_resolution_s = bpl;
  378. lfb_resolution_y = h;
  379. lfb_resolution_b = 32;
  380. }
  381. static void graphics_install_vmware(uint16_t w, uint16_t h) {
  382. pci_scan(vmware_scan_pci, -1, &vmware_io);
  383. if (!vmware_io) {
  384. debug_print(ERROR, "No vmware device found?");
  385. return;
  386. } else {
  387. debug_print(WARNING, "vmware io base: 0x%x", vmware_io);
  388. }
  389. vmware_set_resolution(w,h);
  390. lfb_resolution_impl = &vmware_set_resolution;
  391. uint32_t fb_addr = vmware_read(SVGA_REG_FB_START);
  392. debug_print(WARNING, "vmware fb address: 0x%x", fb_addr);
  393. uint32_t fb_size = vmware_read(15);
  394. debug_print(WARNING, "vmware fb size: 0x%x", fb_size);
  395. lfb_vid_memory = (uint8_t *)fb_addr;
  396. uintptr_t fb_offset = (uintptr_t)lfb_vid_memory;
  397. for (uintptr_t i = fb_offset; i <= fb_offset + fb_size; i += 0x1000) {
  398. page_t * p = get_page(i, 1, kernel_directory);
  399. dma_frame(p, 0, 1, i);
  400. p->pat = 1;
  401. p->writethrough = 1;
  402. p->cachedisable = 1;
  403. }
  404. finalize_graphics("vmware");
  405. }
  406. struct disp_mode {
  407. int16_t x;
  408. int16_t y;
  409. int set;
  410. };
  411. static void auto_scan_pci(uint32_t device, uint16_t v, uint16_t d, void * extra) {
  412. struct disp_mode * mode = extra;
  413. if (mode->set) return;
  414. if ((v == 0x1234 && d == 0x1111) ||
  415. (v == 0x80EE && d == 0xBEEF) ||
  416. (v == 0x10de && d == 0x0a20)) {
  417. mode->set = 1;
  418. graphics_install_bochs(mode->x, mode->y);
  419. } else if ((v == 0x15ad && d == 0x0405)) {
  420. mode->set = 1;
  421. graphics_install_vmware(mode->x, mode->y);
  422. }
  423. }
  424. static int init(void) {
  425. if (mboot_ptr->vbe_mode_info) {
  426. lfb_vid_memory = (uint8_t *)((vbe_info_t *)(mboot_ptr->vbe_mode_info))->physbase;
  427. }
  428. char * c;
  429. if ((c = args_value("vid"))) {
  430. debug_print(NOTICE, "Video mode requested: %s", c);
  431. char * arg = strdup(c);
  432. char * argv[10];
  433. int argc = tokenize(arg, ",", argv);
  434. uint16_t x, y;
  435. if (argc < 3) {
  436. x = PREFERRED_W;
  437. y = PREFERRED_H;
  438. } else {
  439. x = atoi(argv[1]);
  440. y = atoi(argv[2]);
  441. }
  442. if (!strcmp(argv[0], "auto")) {
  443. /* Attempt autodetection */
  444. debug_print(NOTICE, "Automatically detecting display driver...");
  445. struct disp_mode mode = {x,y,0};
  446. pci_scan(auto_scan_pci, -1, &mode);
  447. if (!mode.set) {
  448. graphics_install_preset(x,y);
  449. }
  450. } else if (!strcmp(argv[0], "qemu")) {
  451. /* Bochs / Qemu Video Device */
  452. graphics_install_bochs(x,y);
  453. } else if (!strcmp(argv[0],"vmware")) {
  454. /* VMware SVGA */
  455. graphics_install_vmware(x,y);
  456. } else if (!strcmp(argv[0],"preset")) {
  457. /* Set by bootloader (UEFI) */
  458. graphics_install_preset(x,y);
  459. } else {
  460. debug_print(WARNING, "Unrecognized video adapter: %s", argv[0]);
  461. }
  462. free(arg);
  463. }
  464. return 0;
  465. }
  466. static int fini(void) {
  467. return 0;
  468. }
  469. MODULE_DEF(lfbvideo, init, fini);