vbox_mode.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright (C) 2013-2017 Oracle Corporation
  4. * This file is based on ast_mode.c
  5. * Copyright 2012 Red Hat Inc.
  6. * Parts based on xf86-video-ast
  7. * Copyright (c) 2005 ASPEED Technology Inc.
  8. * Authors: Dave Airlie <airlied@redhat.com>
  9. * Michael Thayer <michael.thayer@oracle.com,
  10. * Hans de Goede <hdegoede@redhat.com>
  11. */
  12. #include <linux/iosys-map.h>
  13. #include <linux/export.h>
  14. #include <drm/drm_atomic.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <drm/drm_edid.h>
  17. #include <drm/drm_fb_helper.h>
  18. #include <drm/drm_fourcc.h>
  19. #include <drm/drm_framebuffer.h>
  20. #include <drm/drm_gem_atomic_helper.h>
  21. #include <drm/drm_gem_framebuffer_helper.h>
  22. #include <drm/drm_plane_helper.h>
  23. #include <drm/drm_print.h>
  24. #include <drm/drm_probe_helper.h>
  25. #include "hgsmi_channels.h"
  26. #include "vbox_drv.h"
  27. #include "vboxvideo.h"
  28. /*
  29. * Set a graphics mode. Poke any required values into registers, do an HGSMI
  30. * mode set and tell the host we support advanced graphics functions.
  31. */
  32. static void vbox_do_modeset(struct drm_crtc *crtc)
  33. {
  34. struct drm_framebuffer *fb = crtc->primary->state->fb;
  35. struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
  36. struct vbox_private *vbox;
  37. int width, height, bpp, pitch;
  38. u16 flags;
  39. s32 x_offset, y_offset;
  40. vbox = to_vbox_dev(crtc->dev);
  41. width = vbox_crtc->width ? vbox_crtc->width : 640;
  42. height = vbox_crtc->height ? vbox_crtc->height : 480;
  43. bpp = fb ? fb->format->cpp[0] * 8 : 32;
  44. pitch = fb ? fb->pitches[0] : width * bpp / 8;
  45. x_offset = vbox->single_framebuffer ? vbox_crtc->x : vbox_crtc->x_hint;
  46. y_offset = vbox->single_framebuffer ? vbox_crtc->y : vbox_crtc->y_hint;
  47. /*
  48. * This is the old way of setting graphics modes. It assumed one screen
  49. * and a frame-buffer at the start of video RAM. On older versions of
  50. * VirtualBox, certain parts of the code still assume that the first
  51. * screen is programmed this way, so try to fake it.
  52. */
  53. if (vbox_crtc->crtc_id == 0 && fb &&
  54. vbox_crtc->fb_offset / pitch < 0xffff - crtc->y &&
  55. vbox_crtc->fb_offset % (bpp / 8) == 0) {
  56. vbox_write_ioport(VBE_DISPI_INDEX_XRES, width);
  57. vbox_write_ioport(VBE_DISPI_INDEX_YRES, height);
  58. vbox_write_ioport(VBE_DISPI_INDEX_VIRT_WIDTH, pitch * 8 / bpp);
  59. vbox_write_ioport(VBE_DISPI_INDEX_BPP, bpp);
  60. vbox_write_ioport(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED);
  61. vbox_write_ioport(VBE_DISPI_INDEX_X_OFFSET,
  62. vbox_crtc->fb_offset % pitch / bpp * 8 + vbox_crtc->x);
  63. vbox_write_ioport(VBE_DISPI_INDEX_Y_OFFSET,
  64. vbox_crtc->fb_offset / pitch + vbox_crtc->y);
  65. }
  66. flags = VBVA_SCREEN_F_ACTIVE;
  67. flags |= (fb && crtc->state->enable) ? 0 : VBVA_SCREEN_F_BLANK;
  68. flags |= vbox_crtc->disconnected ? VBVA_SCREEN_F_DISABLED : 0;
  69. hgsmi_process_display_info(vbox->guest_pool, vbox_crtc->crtc_id,
  70. x_offset, y_offset,
  71. vbox_crtc->x * bpp / 8 +
  72. vbox_crtc->y * pitch,
  73. pitch, width, height, bpp, flags);
  74. }
  75. static int vbox_set_view(struct drm_crtc *crtc)
  76. {
  77. struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
  78. struct vbox_private *vbox = to_vbox_dev(crtc->dev);
  79. struct vbva_infoview *p;
  80. /*
  81. * Tell the host about the view. This design originally targeted the
  82. * Windows XP driver architecture and assumed that each screen would
  83. * have a dedicated frame buffer with the command buffer following it,
  84. * the whole being a "view". The host works out which screen a command
  85. * buffer belongs to by checking whether it is in the first view, then
  86. * whether it is in the second and so on. The first match wins. We
  87. * cheat around this by making the first view be the managed memory
  88. * plus the first command buffer, the second the same plus the second
  89. * buffer and so on.
  90. */
  91. p = hgsmi_buffer_alloc(vbox->guest_pool, sizeof(*p),
  92. HGSMI_CH_VBVA, VBVA_INFO_VIEW);
  93. if (!p)
  94. return -ENOMEM;
  95. p->view_index = vbox_crtc->crtc_id;
  96. p->view_offset = vbox_crtc->fb_offset;
  97. p->view_size = vbox->available_vram_size - vbox_crtc->fb_offset +
  98. vbox_crtc->crtc_id * VBVA_MIN_BUFFER_SIZE;
  99. p->max_screen_size = vbox->available_vram_size - vbox_crtc->fb_offset;
  100. hgsmi_buffer_submit(vbox->guest_pool, p);
  101. hgsmi_buffer_free(vbox->guest_pool, p);
  102. return 0;
  103. }
  104. /*
  105. * Try to map the layout of virtual screens to the range of the input device.
  106. * Return true if we need to re-set the crtc modes due to screen offset
  107. * changes.
  108. */
  109. static bool vbox_set_up_input_mapping(struct vbox_private *vbox)
  110. {
  111. struct drm_crtc *crtci;
  112. struct drm_connector *connectori;
  113. struct drm_framebuffer *fb, *fb1 = NULL;
  114. bool single_framebuffer = true;
  115. bool old_single_framebuffer = vbox->single_framebuffer;
  116. u16 width = 0, height = 0;
  117. /*
  118. * Are we using an X.Org-style single large frame-buffer for all crtcs?
  119. * If so then screen layout can be deduced from the crtc offsets.
  120. * Same fall-back if this is the fbdev frame-buffer.
  121. */
  122. list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, head) {
  123. fb = crtci->primary->state->fb;
  124. if (!fb)
  125. continue;
  126. if (!fb1) {
  127. fb1 = fb;
  128. if (fb1 == vbox->ddev.fb_helper->fb)
  129. break;
  130. } else if (fb != fb1) {
  131. single_framebuffer = false;
  132. }
  133. }
  134. if (!fb1)
  135. return false;
  136. if (single_framebuffer) {
  137. vbox->single_framebuffer = true;
  138. vbox->input_mapping_width = fb1->width;
  139. vbox->input_mapping_height = fb1->height;
  140. return old_single_framebuffer != vbox->single_framebuffer;
  141. }
  142. /* Otherwise calculate the total span of all screens. */
  143. list_for_each_entry(connectori, &vbox->ddev.mode_config.connector_list,
  144. head) {
  145. struct vbox_connector *vbox_connector =
  146. to_vbox_connector(connectori);
  147. struct vbox_crtc *vbox_crtc = vbox_connector->vbox_crtc;
  148. width = max_t(u16, width, vbox_crtc->x_hint +
  149. vbox_connector->mode_hint.width);
  150. height = max_t(u16, height, vbox_crtc->y_hint +
  151. vbox_connector->mode_hint.height);
  152. }
  153. vbox->single_framebuffer = false;
  154. vbox->input_mapping_width = width;
  155. vbox->input_mapping_height = height;
  156. return old_single_framebuffer != vbox->single_framebuffer;
  157. }
  158. static void vbox_crtc_set_base_and_mode(struct drm_crtc *crtc,
  159. struct drm_framebuffer *fb,
  160. int x, int y)
  161. {
  162. struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(fb->obj[0]);
  163. struct vbox_private *vbox = to_vbox_dev(crtc->dev);
  164. struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
  165. bool needs_modeset = drm_atomic_crtc_needs_modeset(crtc->state);
  166. mutex_lock(&vbox->hw_mutex);
  167. if (crtc->state->enable) {
  168. vbox_crtc->width = crtc->state->mode.hdisplay;
  169. vbox_crtc->height = crtc->state->mode.vdisplay;
  170. }
  171. vbox_crtc->x = x;
  172. vbox_crtc->y = y;
  173. vbox_crtc->fb_offset = drm_gem_vram_offset(gbo);
  174. /* vbox_do_modeset() checks vbox->single_framebuffer so update it now */
  175. if (needs_modeset && vbox_set_up_input_mapping(vbox)) {
  176. struct drm_crtc *crtci;
  177. list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list,
  178. head) {
  179. if (crtci == crtc)
  180. continue;
  181. vbox_do_modeset(crtci);
  182. }
  183. }
  184. vbox_set_view(crtc);
  185. vbox_do_modeset(crtc);
  186. if (needs_modeset)
  187. hgsmi_update_input_mapping(vbox->guest_pool, 0, 0,
  188. vbox->input_mapping_width,
  189. vbox->input_mapping_height);
  190. mutex_unlock(&vbox->hw_mutex);
  191. }
  192. static void vbox_crtc_atomic_enable(struct drm_crtc *crtc,
  193. struct drm_atomic_state *state)
  194. {
  195. }
  196. static void vbox_crtc_atomic_disable(struct drm_crtc *crtc,
  197. struct drm_atomic_state *state)
  198. {
  199. }
  200. static void vbox_crtc_atomic_flush(struct drm_crtc *crtc,
  201. struct drm_atomic_state *state)
  202. {
  203. }
  204. static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs = {
  205. .atomic_enable = vbox_crtc_atomic_enable,
  206. .atomic_disable = vbox_crtc_atomic_disable,
  207. .atomic_flush = vbox_crtc_atomic_flush,
  208. };
  209. static void vbox_crtc_destroy(struct drm_crtc *crtc)
  210. {
  211. drm_crtc_cleanup(crtc);
  212. kfree(crtc);
  213. }
  214. static const struct drm_crtc_funcs vbox_crtc_funcs = {
  215. .set_config = drm_atomic_helper_set_config,
  216. .page_flip = drm_atomic_helper_page_flip,
  217. /* .gamma_set = vbox_crtc_gamma_set, */
  218. .destroy = vbox_crtc_destroy,
  219. .reset = drm_atomic_helper_crtc_reset,
  220. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  221. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  222. };
  223. static int vbox_primary_atomic_check(struct drm_plane *plane,
  224. struct drm_atomic_state *state)
  225. {
  226. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  227. plane);
  228. struct drm_crtc_state *crtc_state = NULL;
  229. if (new_state->crtc) {
  230. crtc_state = drm_atomic_get_new_crtc_state(state,
  231. new_state->crtc);
  232. if (WARN_ON(!crtc_state))
  233. return -EINVAL;
  234. }
  235. return drm_atomic_helper_check_plane_state(new_state, crtc_state,
  236. DRM_PLANE_NO_SCALING,
  237. DRM_PLANE_NO_SCALING,
  238. false, true);
  239. }
  240. static void vbox_primary_atomic_update(struct drm_plane *plane,
  241. struct drm_atomic_state *state)
  242. {
  243. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  244. plane);
  245. struct drm_crtc *crtc = new_state->crtc;
  246. struct drm_framebuffer *fb = new_state->fb;
  247. struct vbox_private *vbox = to_vbox_dev(fb->dev);
  248. struct drm_mode_rect *clips;
  249. uint32_t num_clips, i;
  250. vbox_crtc_set_base_and_mode(crtc, fb,
  251. new_state->src_x >> 16,
  252. new_state->src_y >> 16);
  253. /* Send information about dirty rectangles to VBVA. */
  254. clips = drm_plane_get_damage_clips(new_state);
  255. num_clips = drm_plane_get_damage_clips_count(new_state);
  256. if (!num_clips)
  257. return;
  258. mutex_lock(&vbox->hw_mutex);
  259. for (i = 0; i < num_clips; ++i, ++clips) {
  260. struct vbva_cmd_hdr cmd_hdr;
  261. unsigned int crtc_id = to_vbox_crtc(crtc)->crtc_id;
  262. cmd_hdr.x = (s16)clips->x1;
  263. cmd_hdr.y = (s16)clips->y1;
  264. cmd_hdr.w = (u16)clips->x2 - clips->x1;
  265. cmd_hdr.h = (u16)clips->y2 - clips->y1;
  266. if (!vbva_buffer_begin_update(&vbox->vbva_info[crtc_id],
  267. vbox->guest_pool))
  268. continue;
  269. vbva_write(&vbox->vbva_info[crtc_id], vbox->guest_pool,
  270. &cmd_hdr, sizeof(cmd_hdr));
  271. vbva_buffer_end_update(&vbox->vbva_info[crtc_id]);
  272. }
  273. mutex_unlock(&vbox->hw_mutex);
  274. }
  275. static void vbox_primary_atomic_disable(struct drm_plane *plane,
  276. struct drm_atomic_state *state)
  277. {
  278. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  279. plane);
  280. struct drm_crtc *crtc = old_state->crtc;
  281. /* vbox_do_modeset checks plane->state->fb and will disable if NULL */
  282. vbox_crtc_set_base_and_mode(crtc, old_state->fb,
  283. old_state->src_x >> 16,
  284. old_state->src_y >> 16);
  285. }
  286. static int vbox_cursor_atomic_check(struct drm_plane *plane,
  287. struct drm_atomic_state *state)
  288. {
  289. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  290. plane);
  291. struct drm_crtc_state *crtc_state = NULL;
  292. u32 width = new_state->crtc_w;
  293. u32 height = new_state->crtc_h;
  294. int ret;
  295. if (new_state->crtc) {
  296. crtc_state = drm_atomic_get_new_crtc_state(state,
  297. new_state->crtc);
  298. if (WARN_ON(!crtc_state))
  299. return -EINVAL;
  300. }
  301. ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
  302. DRM_PLANE_NO_SCALING,
  303. DRM_PLANE_NO_SCALING,
  304. true, true);
  305. if (ret)
  306. return ret;
  307. if (!new_state->fb)
  308. return 0;
  309. if (width > VBOX_MAX_CURSOR_WIDTH || height > VBOX_MAX_CURSOR_HEIGHT ||
  310. width == 0 || height == 0)
  311. return -EINVAL;
  312. return 0;
  313. }
  314. /*
  315. * Copy the ARGB image and generate the mask, which is needed in case the host
  316. * does not support ARGB cursors. The mask is a 1BPP bitmap with the bit set
  317. * if the corresponding alpha value in the ARGB image is greater than 0xF0.
  318. */
  319. static void copy_cursor_image(u8 *src, u8 *dst, u32 width, u32 height,
  320. size_t mask_size)
  321. {
  322. size_t line_size = (width + 7) / 8;
  323. u32 i, j;
  324. memcpy(dst + mask_size, src, width * height * 4);
  325. for (i = 0; i < height; ++i)
  326. for (j = 0; j < width; ++j)
  327. if (((u32 *)src)[i * width + j] > 0xf0000000)
  328. dst[i * line_size + j / 8] |= (0x80 >> (j % 8));
  329. }
  330. static void vbox_cursor_atomic_update(struct drm_plane *plane,
  331. struct drm_atomic_state *state)
  332. {
  333. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  334. plane);
  335. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  336. plane);
  337. struct vbox_private *vbox =
  338. container_of(plane->dev, struct vbox_private, ddev);
  339. struct vbox_crtc *vbox_crtc = to_vbox_crtc(new_state->crtc);
  340. struct drm_framebuffer *fb = new_state->fb;
  341. u32 width = new_state->crtc_w;
  342. u32 height = new_state->crtc_h;
  343. struct drm_shadow_plane_state *shadow_plane_state =
  344. to_drm_shadow_plane_state(new_state);
  345. struct iosys_map map = shadow_plane_state->data[0];
  346. u8 *src = map.vaddr; /* TODO: Use mapping abstraction properly */
  347. size_t data_size, mask_size;
  348. u32 flags;
  349. /*
  350. * VirtualBox uses the host windowing system to draw the cursor so
  351. * moves are a no-op, we only need to upload new cursor sprites.
  352. */
  353. if (fb == old_state->fb)
  354. return;
  355. mutex_lock(&vbox->hw_mutex);
  356. vbox_crtc->cursor_enabled = true;
  357. /*
  358. * The mask must be calculated based on the alpha
  359. * channel, one bit per ARGB word, and must be 32-bit
  360. * padded.
  361. */
  362. mask_size = ((width + 7) / 8 * height + 3) & ~3;
  363. data_size = width * height * 4 + mask_size;
  364. copy_cursor_image(src, vbox->cursor_data, width, height, mask_size);
  365. flags = VBOX_MOUSE_POINTER_VISIBLE | VBOX_MOUSE_POINTER_SHAPE |
  366. VBOX_MOUSE_POINTER_ALPHA;
  367. hgsmi_update_pointer_shape(vbox->guest_pool, flags,
  368. min_t(u32, max(new_state->hotspot_x, 0), width),
  369. min_t(u32, max(new_state->hotspot_y, 0), height),
  370. width, height, vbox->cursor_data, data_size);
  371. mutex_unlock(&vbox->hw_mutex);
  372. }
  373. static void vbox_cursor_atomic_disable(struct drm_plane *plane,
  374. struct drm_atomic_state *state)
  375. {
  376. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  377. plane);
  378. struct vbox_private *vbox =
  379. container_of(plane->dev, struct vbox_private, ddev);
  380. struct vbox_crtc *vbox_crtc = to_vbox_crtc(old_state->crtc);
  381. bool cursor_enabled = false;
  382. struct drm_crtc *crtci;
  383. mutex_lock(&vbox->hw_mutex);
  384. vbox_crtc->cursor_enabled = false;
  385. list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, head) {
  386. if (to_vbox_crtc(crtci)->cursor_enabled)
  387. cursor_enabled = true;
  388. }
  389. if (!cursor_enabled)
  390. hgsmi_update_pointer_shape(vbox->guest_pool, 0, 0, 0,
  391. 0, 0, NULL, 0);
  392. mutex_unlock(&vbox->hw_mutex);
  393. }
  394. static const u32 vbox_cursor_plane_formats[] = {
  395. DRM_FORMAT_ARGB8888,
  396. };
  397. static const struct drm_plane_helper_funcs vbox_cursor_helper_funcs = {
  398. .atomic_check = vbox_cursor_atomic_check,
  399. .atomic_update = vbox_cursor_atomic_update,
  400. .atomic_disable = vbox_cursor_atomic_disable,
  401. DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
  402. };
  403. static const struct drm_plane_funcs vbox_cursor_plane_funcs = {
  404. .update_plane = drm_atomic_helper_update_plane,
  405. .disable_plane = drm_atomic_helper_disable_plane,
  406. .destroy = drm_plane_helper_destroy,
  407. DRM_GEM_SHADOW_PLANE_FUNCS,
  408. };
  409. static const u32 vbox_primary_plane_formats[] = {
  410. DRM_FORMAT_XRGB8888,
  411. DRM_FORMAT_ARGB8888,
  412. };
  413. static const struct drm_plane_helper_funcs vbox_primary_helper_funcs = {
  414. .atomic_check = vbox_primary_atomic_check,
  415. .atomic_update = vbox_primary_atomic_update,
  416. .atomic_disable = vbox_primary_atomic_disable,
  417. DRM_GEM_VRAM_PLANE_HELPER_FUNCS,
  418. };
  419. static const struct drm_plane_funcs vbox_primary_plane_funcs = {
  420. .update_plane = drm_atomic_helper_update_plane,
  421. .disable_plane = drm_atomic_helper_disable_plane,
  422. .destroy = drm_plane_helper_destroy,
  423. .reset = drm_atomic_helper_plane_reset,
  424. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  425. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  426. };
  427. static struct drm_plane *vbox_create_plane(struct vbox_private *vbox,
  428. unsigned int possible_crtcs,
  429. enum drm_plane_type type)
  430. {
  431. const struct drm_plane_helper_funcs *helper_funcs = NULL;
  432. const struct drm_plane_funcs *funcs;
  433. struct drm_plane *plane;
  434. const u32 *formats;
  435. int num_formats;
  436. int err;
  437. if (type == DRM_PLANE_TYPE_PRIMARY) {
  438. funcs = &vbox_primary_plane_funcs;
  439. formats = vbox_primary_plane_formats;
  440. helper_funcs = &vbox_primary_helper_funcs;
  441. num_formats = ARRAY_SIZE(vbox_primary_plane_formats);
  442. } else if (type == DRM_PLANE_TYPE_CURSOR) {
  443. funcs = &vbox_cursor_plane_funcs;
  444. formats = vbox_cursor_plane_formats;
  445. helper_funcs = &vbox_cursor_helper_funcs;
  446. num_formats = ARRAY_SIZE(vbox_cursor_plane_formats);
  447. } else {
  448. return ERR_PTR(-EINVAL);
  449. }
  450. plane = kzalloc_obj(*plane);
  451. if (!plane)
  452. return ERR_PTR(-ENOMEM);
  453. err = drm_universal_plane_init(&vbox->ddev, plane, possible_crtcs,
  454. funcs, formats, num_formats,
  455. NULL, type, NULL);
  456. if (err)
  457. goto free_plane;
  458. drm_plane_helper_add(plane, helper_funcs);
  459. return plane;
  460. free_plane:
  461. kfree(plane);
  462. return ERR_PTR(-EINVAL);
  463. }
  464. static struct vbox_crtc *vbox_crtc_init(struct drm_device *dev, unsigned int i)
  465. {
  466. struct vbox_private *vbox =
  467. container_of(dev, struct vbox_private, ddev);
  468. struct drm_plane *cursor = NULL;
  469. struct vbox_crtc *vbox_crtc;
  470. struct drm_plane *primary;
  471. u32 caps = 0;
  472. int ret;
  473. ret = hgsmi_query_conf(vbox->guest_pool,
  474. VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &caps);
  475. if (ret)
  476. return ERR_PTR(ret);
  477. vbox_crtc = kzalloc_obj(*vbox_crtc);
  478. if (!vbox_crtc)
  479. return ERR_PTR(-ENOMEM);
  480. primary = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_PRIMARY);
  481. if (IS_ERR(primary)) {
  482. ret = PTR_ERR(primary);
  483. goto free_mem;
  484. }
  485. if ((caps & VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE)) {
  486. cursor = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_CURSOR);
  487. if (IS_ERR(cursor)) {
  488. ret = PTR_ERR(cursor);
  489. goto clean_primary;
  490. }
  491. } else {
  492. DRM_WARN("VirtualBox host is too old, no cursor support\n");
  493. }
  494. vbox_crtc->crtc_id = i;
  495. ret = drm_crtc_init_with_planes(dev, &vbox_crtc->base, primary, cursor,
  496. &vbox_crtc_funcs, NULL);
  497. if (ret)
  498. goto clean_cursor;
  499. drm_mode_crtc_set_gamma_size(&vbox_crtc->base, 256);
  500. drm_crtc_helper_add(&vbox_crtc->base, &vbox_crtc_helper_funcs);
  501. return vbox_crtc;
  502. clean_cursor:
  503. if (cursor) {
  504. drm_plane_cleanup(cursor);
  505. kfree(cursor);
  506. }
  507. clean_primary:
  508. drm_plane_cleanup(primary);
  509. kfree(primary);
  510. free_mem:
  511. kfree(vbox_crtc);
  512. return ERR_PTR(ret);
  513. }
  514. static void vbox_encoder_destroy(struct drm_encoder *encoder)
  515. {
  516. drm_encoder_cleanup(encoder);
  517. kfree(encoder);
  518. }
  519. static const struct drm_encoder_funcs vbox_enc_funcs = {
  520. .destroy = vbox_encoder_destroy,
  521. };
  522. static struct drm_encoder *vbox_encoder_init(struct drm_device *dev,
  523. unsigned int i)
  524. {
  525. struct vbox_encoder *vbox_encoder;
  526. vbox_encoder = kzalloc_obj(*vbox_encoder);
  527. if (!vbox_encoder)
  528. return NULL;
  529. drm_encoder_init(dev, &vbox_encoder->base, &vbox_enc_funcs,
  530. DRM_MODE_ENCODER_DAC, NULL);
  531. vbox_encoder->base.possible_crtcs = 1 << i;
  532. return &vbox_encoder->base;
  533. }
  534. /*
  535. * Generate EDID data with a mode-unique serial number for the virtual
  536. * monitor to try to persuade Unity that different modes correspond to
  537. * different monitors and it should not try to force the same resolution on
  538. * them.
  539. */
  540. static void vbox_set_edid(struct drm_connector *connector, int width,
  541. int height)
  542. {
  543. enum { EDID_SIZE = 128 };
  544. unsigned char edid[EDID_SIZE] = {
  545. 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, /* header */
  546. 0x58, 0x58, /* manufacturer (VBX) */
  547. 0x00, 0x00, /* product code */
  548. 0x00, 0x00, 0x00, 0x00, /* serial number goes here */
  549. 0x01, /* week of manufacture */
  550. 0x00, /* year of manufacture */
  551. 0x01, 0x03, /* EDID version */
  552. 0x80, /* capabilities - digital */
  553. 0x00, /* horiz. res in cm, zero for projectors */
  554. 0x00, /* vert. res in cm */
  555. 0x78, /* display gamma (120 == 2.2). */
  556. 0xEE, /* features (standby, suspend, off, RGB, std */
  557. /* colour space, preferred timing mode) */
  558. 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54,
  559. /* chromaticity for standard colour space. */
  560. 0x00, 0x00, 0x00, /* no default timings */
  561. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  562. 0x01, 0x01,
  563. 0x01, 0x01, 0x01, 0x01, /* no standard timings */
  564. 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02,
  565. 0x02, 0x02,
  566. /* descriptor block 1 goes below */
  567. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  568. /* descriptor block 2, monitor ranges */
  569. 0x00, 0x00, 0x00, 0xFD, 0x00,
  570. 0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20,
  571. 0x20, 0x20,
  572. /* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */
  573. 0x20,
  574. /* descriptor block 3, monitor name */
  575. 0x00, 0x00, 0x00, 0xFC, 0x00,
  576. 'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r',
  577. '\n',
  578. /* descriptor block 4: dummy data */
  579. 0x00, 0x00, 0x00, 0x10, 0x00,
  580. 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
  581. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  582. 0x20,
  583. 0x00, /* number of extensions */
  584. 0x00 /* checksum goes here */
  585. };
  586. int clock = (width + 6) * (height + 6) * 60 / 10000;
  587. unsigned int i, sum = 0;
  588. edid[12] = width & 0xff;
  589. edid[13] = width >> 8;
  590. edid[14] = height & 0xff;
  591. edid[15] = height >> 8;
  592. edid[54] = clock & 0xff;
  593. edid[55] = clock >> 8;
  594. edid[56] = width & 0xff;
  595. edid[58] = (width >> 4) & 0xf0;
  596. edid[59] = height & 0xff;
  597. edid[61] = (height >> 4) & 0xf0;
  598. for (i = 0; i < EDID_SIZE - 1; ++i)
  599. sum += edid[i];
  600. edid[EDID_SIZE - 1] = (0x100 - (sum & 0xFF)) & 0xFF;
  601. drm_connector_update_edid_property(connector, (struct edid *)edid);
  602. }
  603. static int vbox_get_modes(struct drm_connector *connector)
  604. {
  605. struct vbox_connector *vbox_connector = NULL;
  606. struct drm_display_mode *mode = NULL;
  607. struct vbox_private *vbox = NULL;
  608. unsigned int num_modes = 0;
  609. int preferred_width, preferred_height;
  610. vbox_connector = to_vbox_connector(connector);
  611. vbox = to_vbox_dev(connector->dev);
  612. hgsmi_report_flags_location(vbox->guest_pool, GUEST_HEAP_OFFSET(vbox) +
  613. HOST_FLAGS_OFFSET);
  614. if (vbox_connector->vbox_crtc->crtc_id == 0)
  615. vbox_report_caps(vbox);
  616. num_modes = drm_add_modes_noedid(connector, 2560, 1600);
  617. preferred_width = vbox_connector->mode_hint.width ?
  618. vbox_connector->mode_hint.width : 1024;
  619. preferred_height = vbox_connector->mode_hint.height ?
  620. vbox_connector->mode_hint.height : 768;
  621. mode = drm_cvt_mode(connector->dev, preferred_width, preferred_height,
  622. 60, false, false, false);
  623. if (mode) {
  624. mode->type |= DRM_MODE_TYPE_PREFERRED;
  625. drm_mode_probed_add(connector, mode);
  626. ++num_modes;
  627. }
  628. vbox_set_edid(connector, preferred_width, preferred_height);
  629. if (vbox_connector->vbox_crtc->x_hint != -1)
  630. drm_object_property_set_value(&connector->base,
  631. vbox->ddev.mode_config.suggested_x_property,
  632. vbox_connector->vbox_crtc->x_hint);
  633. else
  634. drm_object_property_set_value(&connector->base,
  635. vbox->ddev.mode_config.suggested_x_property, 0);
  636. if (vbox_connector->vbox_crtc->y_hint != -1)
  637. drm_object_property_set_value(&connector->base,
  638. vbox->ddev.mode_config.suggested_y_property,
  639. vbox_connector->vbox_crtc->y_hint);
  640. else
  641. drm_object_property_set_value(&connector->base,
  642. vbox->ddev.mode_config.suggested_y_property, 0);
  643. return num_modes;
  644. }
  645. static void vbox_connector_destroy(struct drm_connector *connector)
  646. {
  647. drm_connector_unregister(connector);
  648. drm_connector_cleanup(connector);
  649. kfree(connector);
  650. }
  651. static enum drm_connector_status
  652. vbox_connector_detect(struct drm_connector *connector, bool force)
  653. {
  654. struct vbox_connector *vbox_connector;
  655. vbox_connector = to_vbox_connector(connector);
  656. return vbox_connector->mode_hint.disconnected ?
  657. connector_status_disconnected : connector_status_connected;
  658. }
  659. static int vbox_fill_modes(struct drm_connector *connector, u32 max_x,
  660. u32 max_y)
  661. {
  662. struct vbox_connector *vbox_connector;
  663. struct drm_device *dev;
  664. struct drm_display_mode *mode, *iterator;
  665. vbox_connector = to_vbox_connector(connector);
  666. dev = vbox_connector->base.dev;
  667. list_for_each_entry_safe(mode, iterator, &connector->modes, head) {
  668. list_del(&mode->head);
  669. drm_mode_destroy(dev, mode);
  670. }
  671. return drm_helper_probe_single_connector_modes(connector, max_x, max_y);
  672. }
  673. static const struct drm_connector_helper_funcs vbox_connector_helper_funcs = {
  674. .get_modes = vbox_get_modes,
  675. };
  676. static const struct drm_connector_funcs vbox_connector_funcs = {
  677. .detect = vbox_connector_detect,
  678. .fill_modes = vbox_fill_modes,
  679. .destroy = vbox_connector_destroy,
  680. .reset = drm_atomic_helper_connector_reset,
  681. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  682. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  683. };
  684. static int vbox_connector_init(struct drm_device *dev,
  685. struct vbox_crtc *vbox_crtc,
  686. struct drm_encoder *encoder)
  687. {
  688. struct vbox_connector *vbox_connector;
  689. struct drm_connector *connector;
  690. vbox_connector = kzalloc_obj(*vbox_connector);
  691. if (!vbox_connector)
  692. return -ENOMEM;
  693. connector = &vbox_connector->base;
  694. vbox_connector->vbox_crtc = vbox_crtc;
  695. drm_connector_init(dev, connector, &vbox_connector_funcs,
  696. DRM_MODE_CONNECTOR_VGA);
  697. drm_connector_helper_add(connector, &vbox_connector_helper_funcs);
  698. connector->interlace_allowed = 0;
  699. connector->doublescan_allowed = 0;
  700. drm_mode_create_suggested_offset_properties(dev);
  701. drm_object_attach_property(&connector->base,
  702. dev->mode_config.suggested_x_property, 0);
  703. drm_object_attach_property(&connector->base,
  704. dev->mode_config.suggested_y_property, 0);
  705. drm_connector_attach_encoder(connector, encoder);
  706. return 0;
  707. }
  708. static const struct drm_mode_config_funcs vbox_mode_funcs = {
  709. .fb_create = drm_gem_fb_create_with_dirty,
  710. .mode_valid = drm_vram_helper_mode_valid,
  711. .atomic_check = drm_atomic_helper_check,
  712. .atomic_commit = drm_atomic_helper_commit,
  713. };
  714. int vbox_mode_init(struct vbox_private *vbox)
  715. {
  716. struct drm_device *dev = &vbox->ddev;
  717. struct drm_encoder *encoder;
  718. struct vbox_crtc *vbox_crtc;
  719. unsigned int i;
  720. int ret;
  721. drm_mode_config_init(dev);
  722. dev->mode_config.funcs = (void *)&vbox_mode_funcs;
  723. dev->mode_config.min_width = 0;
  724. dev->mode_config.min_height = 0;
  725. dev->mode_config.preferred_depth = 24;
  726. dev->mode_config.max_width = VBE_DISPI_MAX_XRES;
  727. dev->mode_config.max_height = VBE_DISPI_MAX_YRES;
  728. for (i = 0; i < vbox->num_crtcs; ++i) {
  729. vbox_crtc = vbox_crtc_init(dev, i);
  730. if (IS_ERR(vbox_crtc)) {
  731. ret = PTR_ERR(vbox_crtc);
  732. goto err_drm_mode_cleanup;
  733. }
  734. encoder = vbox_encoder_init(dev, i);
  735. if (!encoder) {
  736. ret = -ENOMEM;
  737. goto err_drm_mode_cleanup;
  738. }
  739. ret = vbox_connector_init(dev, vbox_crtc, encoder);
  740. if (ret)
  741. goto err_drm_mode_cleanup;
  742. }
  743. drm_mode_config_reset(dev);
  744. return 0;
  745. err_drm_mode_cleanup:
  746. drm_mode_config_cleanup(dev);
  747. return ret;
  748. }
  749. void vbox_mode_fini(struct vbox_private *vbox)
  750. {
  751. drm_mode_config_cleanup(&vbox->ddev);
  752. }