vbox_mode.c 26 KB

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