qxl_display.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*
  2. * Copyright 2013 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Dave Airlie
  23. * Alon Levy
  24. */
  25. #include <linux/crc32.h>
  26. #include "qxl_drv.h"
  27. #include "qxl_object.h"
  28. #include "drm_crtc_helper.h"
  29. #include <drm/drm_plane_helper.h>
  30. static bool qxl_head_enabled(struct qxl_head *head)
  31. {
  32. return head->width && head->height;
  33. }
  34. void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
  35. {
  36. if (qdev->client_monitors_config &&
  37. count > qdev->client_monitors_config->count) {
  38. kfree(qdev->client_monitors_config);
  39. qdev->client_monitors_config = NULL;
  40. }
  41. if (!qdev->client_monitors_config) {
  42. qdev->client_monitors_config = kzalloc(
  43. sizeof(struct qxl_monitors_config) +
  44. sizeof(struct qxl_head) * count, GFP_KERNEL);
  45. if (!qdev->client_monitors_config) {
  46. qxl_io_log(qdev,
  47. "%s: allocation failure for %u heads\n",
  48. __func__, count);
  49. return;
  50. }
  51. }
  52. qdev->client_monitors_config->count = count;
  53. }
  54. static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
  55. {
  56. int i;
  57. int num_monitors;
  58. uint32_t crc;
  59. num_monitors = qdev->rom->client_monitors_config.count;
  60. crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
  61. sizeof(qdev->rom->client_monitors_config));
  62. if (crc != qdev->rom->client_monitors_config_crc) {
  63. qxl_io_log(qdev, "crc mismatch: have %X (%zd) != %X\n", crc,
  64. sizeof(qdev->rom->client_monitors_config),
  65. qdev->rom->client_monitors_config_crc);
  66. return 1;
  67. }
  68. if (num_monitors > qdev->monitors_config->max_allowed) {
  69. DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
  70. qdev->monitors_config->max_allowed, num_monitors);
  71. num_monitors = qdev->monitors_config->max_allowed;
  72. } else {
  73. num_monitors = qdev->rom->client_monitors_config.count;
  74. }
  75. qxl_alloc_client_monitors_config(qdev, num_monitors);
  76. /* we copy max from the client but it isn't used */
  77. qdev->client_monitors_config->max_allowed =
  78. qdev->monitors_config->max_allowed;
  79. for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
  80. struct qxl_urect *c_rect =
  81. &qdev->rom->client_monitors_config.heads[i];
  82. struct qxl_head *client_head =
  83. &qdev->client_monitors_config->heads[i];
  84. client_head->x = c_rect->left;
  85. client_head->y = c_rect->top;
  86. client_head->width = c_rect->right - c_rect->left;
  87. client_head->height = c_rect->bottom - c_rect->top;
  88. client_head->surface_id = 0;
  89. client_head->id = i;
  90. client_head->flags = 0;
  91. DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
  92. client_head->x, client_head->y);
  93. }
  94. return 0;
  95. }
  96. static void qxl_update_offset_props(struct qxl_device *qdev)
  97. {
  98. struct drm_device *dev = qdev->ddev;
  99. struct drm_connector *connector;
  100. struct qxl_output *output;
  101. struct qxl_head *head;
  102. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  103. output = drm_connector_to_qxl_output(connector);
  104. head = &qdev->client_monitors_config->heads[output->index];
  105. drm_object_property_set_value(&connector->base,
  106. dev->mode_config.suggested_x_property, head->x);
  107. drm_object_property_set_value(&connector->base,
  108. dev->mode_config.suggested_y_property, head->y);
  109. }
  110. }
  111. void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
  112. {
  113. struct drm_device *dev = qdev->ddev;
  114. while (qxl_display_copy_rom_client_monitors_config(qdev)) {
  115. qxl_io_log(qdev, "failed crc check for client_monitors_config,"
  116. " retrying\n");
  117. }
  118. drm_modeset_lock_all(dev);
  119. qxl_update_offset_props(qdev);
  120. drm_modeset_unlock_all(dev);
  121. if (!drm_helper_hpd_irq_event(qdev->ddev)) {
  122. /* notify that the monitor configuration changed, to
  123. adjust at the arbitrary resolution */
  124. drm_kms_helper_hotplug_event(qdev->ddev);
  125. }
  126. }
  127. static int qxl_add_monitors_config_modes(struct drm_connector *connector,
  128. unsigned *pwidth,
  129. unsigned *pheight)
  130. {
  131. struct drm_device *dev = connector->dev;
  132. struct qxl_device *qdev = dev->dev_private;
  133. struct qxl_output *output = drm_connector_to_qxl_output(connector);
  134. int h = output->index;
  135. struct drm_display_mode *mode = NULL;
  136. struct qxl_head *head;
  137. if (!qdev->client_monitors_config)
  138. return 0;
  139. head = &qdev->client_monitors_config->heads[h];
  140. mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
  141. false);
  142. mode->type |= DRM_MODE_TYPE_PREFERRED;
  143. *pwidth = head->width;
  144. *pheight = head->height;
  145. drm_mode_probed_add(connector, mode);
  146. /* remember the last custom size for mode validation */
  147. qdev->monitors_config_width = mode->hdisplay;
  148. qdev->monitors_config_height = mode->vdisplay;
  149. return 1;
  150. }
  151. static struct mode_size {
  152. int w;
  153. int h;
  154. } common_modes[] = {
  155. { 640, 480},
  156. { 720, 480},
  157. { 800, 600},
  158. { 848, 480},
  159. {1024, 768},
  160. {1152, 768},
  161. {1280, 720},
  162. {1280, 800},
  163. {1280, 854},
  164. {1280, 960},
  165. {1280, 1024},
  166. {1440, 900},
  167. {1400, 1050},
  168. {1680, 1050},
  169. {1600, 1200},
  170. {1920, 1080},
  171. {1920, 1200}
  172. };
  173. static int qxl_add_common_modes(struct drm_connector *connector,
  174. unsigned pwidth,
  175. unsigned pheight)
  176. {
  177. struct drm_device *dev = connector->dev;
  178. struct drm_display_mode *mode = NULL;
  179. int i;
  180. for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
  181. mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
  182. 60, false, false, false);
  183. if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
  184. mode->type |= DRM_MODE_TYPE_PREFERRED;
  185. drm_mode_probed_add(connector, mode);
  186. }
  187. return i - 1;
  188. }
  189. static void qxl_crtc_destroy(struct drm_crtc *crtc)
  190. {
  191. struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
  192. drm_crtc_cleanup(crtc);
  193. kfree(qxl_crtc);
  194. }
  195. static int qxl_crtc_page_flip(struct drm_crtc *crtc,
  196. struct drm_framebuffer *fb,
  197. struct drm_pending_vblank_event *event,
  198. uint32_t page_flip_flags)
  199. {
  200. struct drm_device *dev = crtc->dev;
  201. struct qxl_device *qdev = dev->dev_private;
  202. struct qxl_framebuffer *qfb_src = to_qxl_framebuffer(fb);
  203. struct qxl_framebuffer *qfb_old = to_qxl_framebuffer(crtc->primary->fb);
  204. struct qxl_bo *bo_old = gem_to_qxl_bo(qfb_old->obj);
  205. struct qxl_bo *bo = gem_to_qxl_bo(qfb_src->obj);
  206. unsigned long flags;
  207. struct drm_clip_rect norect = {
  208. .x1 = 0,
  209. .y1 = 0,
  210. .x2 = fb->width,
  211. .y2 = fb->height
  212. };
  213. int inc = 1;
  214. int one_clip_rect = 1;
  215. int ret = 0;
  216. crtc->primary->fb = fb;
  217. bo_old->is_primary = false;
  218. bo->is_primary = true;
  219. ret = qxl_bo_reserve(bo, false);
  220. if (ret)
  221. return ret;
  222. ret = qxl_bo_pin(bo, bo->type, NULL);
  223. qxl_bo_unreserve(bo);
  224. if (ret)
  225. return ret;
  226. qxl_draw_dirty_fb(qdev, qfb_src, bo, 0, 0,
  227. &norect, one_clip_rect, inc);
  228. drm_crtc_vblank_get(crtc);
  229. if (event) {
  230. spin_lock_irqsave(&dev->event_lock, flags);
  231. drm_crtc_send_vblank_event(crtc, event);
  232. spin_unlock_irqrestore(&dev->event_lock, flags);
  233. }
  234. drm_crtc_vblank_put(crtc);
  235. ret = qxl_bo_reserve(bo, false);
  236. if (!ret) {
  237. qxl_bo_unpin(bo);
  238. qxl_bo_unreserve(bo);
  239. }
  240. return 0;
  241. }
  242. static int
  243. qxl_hide_cursor(struct qxl_device *qdev)
  244. {
  245. struct qxl_release *release;
  246. struct qxl_cursor_cmd *cmd;
  247. int ret;
  248. ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
  249. &release, NULL);
  250. if (ret)
  251. return ret;
  252. ret = qxl_release_reserve_list(release, true);
  253. if (ret) {
  254. qxl_release_free(qdev, release);
  255. return ret;
  256. }
  257. cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
  258. cmd->type = QXL_CURSOR_HIDE;
  259. qxl_release_unmap(qdev, release, &cmd->release_info);
  260. qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
  261. qxl_release_fence_buffer_objects(release);
  262. return 0;
  263. }
  264. static int qxl_crtc_cursor_set2(struct drm_crtc *crtc,
  265. struct drm_file *file_priv,
  266. uint32_t handle,
  267. uint32_t width,
  268. uint32_t height, int32_t hot_x, int32_t hot_y)
  269. {
  270. struct drm_device *dev = crtc->dev;
  271. struct qxl_device *qdev = dev->dev_private;
  272. struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
  273. struct drm_gem_object *obj;
  274. struct qxl_cursor *cursor;
  275. struct qxl_cursor_cmd *cmd;
  276. struct qxl_bo *cursor_bo, *user_bo;
  277. struct qxl_release *release;
  278. void *user_ptr;
  279. int size = 64*64*4;
  280. int ret = 0;
  281. if (!handle)
  282. return qxl_hide_cursor(qdev);
  283. obj = drm_gem_object_lookup(file_priv, handle);
  284. if (!obj) {
  285. DRM_ERROR("cannot find cursor object\n");
  286. return -ENOENT;
  287. }
  288. user_bo = gem_to_qxl_bo(obj);
  289. ret = qxl_bo_reserve(user_bo, false);
  290. if (ret)
  291. goto out_unref;
  292. ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
  293. qxl_bo_unreserve(user_bo);
  294. if (ret)
  295. goto out_unref;
  296. ret = qxl_bo_kmap(user_bo, &user_ptr);
  297. if (ret)
  298. goto out_unpin;
  299. ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
  300. QXL_RELEASE_CURSOR_CMD,
  301. &release, NULL);
  302. if (ret)
  303. goto out_kunmap;
  304. ret = qxl_alloc_bo_reserved(qdev, release, sizeof(struct qxl_cursor) + size,
  305. &cursor_bo);
  306. if (ret)
  307. goto out_free_release;
  308. ret = qxl_release_reserve_list(release, false);
  309. if (ret)
  310. goto out_free_bo;
  311. ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
  312. if (ret)
  313. goto out_backoff;
  314. cursor->header.unique = 0;
  315. cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
  316. cursor->header.width = 64;
  317. cursor->header.height = 64;
  318. cursor->header.hot_spot_x = hot_x;
  319. cursor->header.hot_spot_y = hot_y;
  320. cursor->data_size = size;
  321. cursor->chunk.next_chunk = 0;
  322. cursor->chunk.prev_chunk = 0;
  323. cursor->chunk.data_size = size;
  324. memcpy(cursor->chunk.data, user_ptr, size);
  325. qxl_bo_kunmap(cursor_bo);
  326. qxl_bo_kunmap(user_bo);
  327. qcrtc->cur_x += qcrtc->hot_spot_x - hot_x;
  328. qcrtc->cur_y += qcrtc->hot_spot_y - hot_y;
  329. qcrtc->hot_spot_x = hot_x;
  330. qcrtc->hot_spot_y = hot_y;
  331. cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
  332. cmd->type = QXL_CURSOR_SET;
  333. cmd->u.set.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
  334. cmd->u.set.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
  335. cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
  336. cmd->u.set.visible = 1;
  337. qxl_release_unmap(qdev, release, &cmd->release_info);
  338. qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
  339. qxl_release_fence_buffer_objects(release);
  340. /* finish with the userspace bo */
  341. ret = qxl_bo_reserve(user_bo, false);
  342. if (!ret) {
  343. qxl_bo_unpin(user_bo);
  344. qxl_bo_unreserve(user_bo);
  345. }
  346. drm_gem_object_unreference_unlocked(obj);
  347. qxl_bo_unref(&cursor_bo);
  348. return ret;
  349. out_backoff:
  350. qxl_release_backoff_reserve_list(release);
  351. out_free_bo:
  352. qxl_bo_unref(&cursor_bo);
  353. out_free_release:
  354. qxl_release_free(qdev, release);
  355. out_kunmap:
  356. qxl_bo_kunmap(user_bo);
  357. out_unpin:
  358. qxl_bo_unpin(user_bo);
  359. out_unref:
  360. drm_gem_object_unreference_unlocked(obj);
  361. return ret;
  362. }
  363. static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
  364. int x, int y)
  365. {
  366. struct drm_device *dev = crtc->dev;
  367. struct qxl_device *qdev = dev->dev_private;
  368. struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
  369. struct qxl_release *release;
  370. struct qxl_cursor_cmd *cmd;
  371. int ret;
  372. ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
  373. &release, NULL);
  374. if (ret)
  375. return ret;
  376. ret = qxl_release_reserve_list(release, true);
  377. if (ret) {
  378. qxl_release_free(qdev, release);
  379. return ret;
  380. }
  381. qcrtc->cur_x = x;
  382. qcrtc->cur_y = y;
  383. cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
  384. cmd->type = QXL_CURSOR_MOVE;
  385. cmd->u.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
  386. cmd->u.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
  387. qxl_release_unmap(qdev, release, &cmd->release_info);
  388. qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
  389. qxl_release_fence_buffer_objects(release);
  390. return 0;
  391. }
  392. static const struct drm_crtc_funcs qxl_crtc_funcs = {
  393. .cursor_set2 = qxl_crtc_cursor_set2,
  394. .cursor_move = qxl_crtc_cursor_move,
  395. .set_config = drm_crtc_helper_set_config,
  396. .destroy = qxl_crtc_destroy,
  397. .page_flip = qxl_crtc_page_flip,
  398. };
  399. void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
  400. {
  401. struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
  402. if (qxl_fb->obj)
  403. drm_gem_object_unreference_unlocked(qxl_fb->obj);
  404. drm_framebuffer_cleanup(fb);
  405. kfree(qxl_fb);
  406. }
  407. static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
  408. struct drm_file *file_priv,
  409. unsigned flags, unsigned color,
  410. struct drm_clip_rect *clips,
  411. unsigned num_clips)
  412. {
  413. /* TODO: vmwgfx where this was cribbed from had locking. Why? */
  414. struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
  415. struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
  416. struct drm_clip_rect norect;
  417. struct qxl_bo *qobj;
  418. int inc = 1;
  419. drm_modeset_lock_all(fb->dev);
  420. qobj = gem_to_qxl_bo(qxl_fb->obj);
  421. /* if we aren't primary surface ignore this */
  422. if (!qobj->is_primary) {
  423. drm_modeset_unlock_all(fb->dev);
  424. return 0;
  425. }
  426. if (!num_clips) {
  427. num_clips = 1;
  428. clips = &norect;
  429. norect.x1 = norect.y1 = 0;
  430. norect.x2 = fb->width;
  431. norect.y2 = fb->height;
  432. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  433. num_clips /= 2;
  434. inc = 2; /* skip source rects */
  435. }
  436. qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
  437. clips, num_clips, inc);
  438. drm_modeset_unlock_all(fb->dev);
  439. return 0;
  440. }
  441. static const struct drm_framebuffer_funcs qxl_fb_funcs = {
  442. .destroy = qxl_user_framebuffer_destroy,
  443. .dirty = qxl_framebuffer_surface_dirty,
  444. /* TODO?
  445. * .create_handle = qxl_user_framebuffer_create_handle, */
  446. };
  447. int
  448. qxl_framebuffer_init(struct drm_device *dev,
  449. struct qxl_framebuffer *qfb,
  450. const struct drm_mode_fb_cmd2 *mode_cmd,
  451. struct drm_gem_object *obj,
  452. const struct drm_framebuffer_funcs *funcs)
  453. {
  454. int ret;
  455. qfb->obj = obj;
  456. ret = drm_framebuffer_init(dev, &qfb->base, funcs);
  457. if (ret) {
  458. qfb->obj = NULL;
  459. return ret;
  460. }
  461. drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
  462. return 0;
  463. }
  464. static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
  465. {
  466. }
  467. static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
  468. const struct drm_display_mode *mode,
  469. struct drm_display_mode *adjusted_mode)
  470. {
  471. struct drm_device *dev = crtc->dev;
  472. struct qxl_device *qdev = dev->dev_private;
  473. qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
  474. __func__,
  475. mode->hdisplay, mode->vdisplay,
  476. adjusted_mode->hdisplay,
  477. adjusted_mode->vdisplay);
  478. return true;
  479. }
  480. void
  481. qxl_send_monitors_config(struct qxl_device *qdev)
  482. {
  483. int i;
  484. BUG_ON(!qdev->ram_header->monitors_config);
  485. if (qdev->monitors_config->count == 0) {
  486. qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
  487. return;
  488. }
  489. for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
  490. struct qxl_head *head = &qdev->monitors_config->heads[i];
  491. if (head->y > 8192 || head->x > 8192 ||
  492. head->width > 8192 || head->height > 8192) {
  493. DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
  494. i, head->width, head->height,
  495. head->x, head->y);
  496. return;
  497. }
  498. }
  499. qxl_io_monitors_config(qdev);
  500. }
  501. static void qxl_monitors_config_set(struct qxl_device *qdev,
  502. int index,
  503. unsigned x, unsigned y,
  504. unsigned width, unsigned height,
  505. unsigned surf_id)
  506. {
  507. DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
  508. qdev->monitors_config->heads[index].x = x;
  509. qdev->monitors_config->heads[index].y = y;
  510. qdev->monitors_config->heads[index].width = width;
  511. qdev->monitors_config->heads[index].height = height;
  512. qdev->monitors_config->heads[index].surface_id = surf_id;
  513. }
  514. static int qxl_crtc_mode_set(struct drm_crtc *crtc,
  515. struct drm_display_mode *mode,
  516. struct drm_display_mode *adjusted_mode,
  517. int x, int y,
  518. struct drm_framebuffer *old_fb)
  519. {
  520. struct drm_device *dev = crtc->dev;
  521. struct qxl_device *qdev = dev->dev_private;
  522. struct qxl_framebuffer *qfb;
  523. struct qxl_bo *bo, *old_bo = NULL;
  524. struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
  525. bool recreate_primary = false;
  526. int ret;
  527. int surf_id;
  528. if (!crtc->primary->fb) {
  529. DRM_DEBUG_KMS("No FB bound\n");
  530. return 0;
  531. }
  532. if (old_fb) {
  533. qfb = to_qxl_framebuffer(old_fb);
  534. old_bo = gem_to_qxl_bo(qfb->obj);
  535. }
  536. qfb = to_qxl_framebuffer(crtc->primary->fb);
  537. bo = gem_to_qxl_bo(qfb->obj);
  538. DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
  539. x, y,
  540. mode->hdisplay, mode->vdisplay,
  541. adjusted_mode->hdisplay,
  542. adjusted_mode->vdisplay);
  543. if (bo->is_primary == false)
  544. recreate_primary = true;
  545. if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
  546. DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
  547. return -EINVAL;
  548. }
  549. ret = qxl_bo_reserve(bo, false);
  550. if (ret != 0)
  551. return ret;
  552. ret = qxl_bo_pin(bo, bo->type, NULL);
  553. if (ret != 0) {
  554. qxl_bo_unreserve(bo);
  555. return -EINVAL;
  556. }
  557. qxl_bo_unreserve(bo);
  558. if (recreate_primary) {
  559. qxl_io_destroy_primary(qdev);
  560. qxl_io_log(qdev,
  561. "recreate primary: %dx%d,%d,%d\n",
  562. bo->surf.width, bo->surf.height,
  563. bo->surf.stride, bo->surf.format);
  564. qxl_io_create_primary(qdev, 0, bo);
  565. bo->is_primary = true;
  566. }
  567. if (bo->is_primary) {
  568. DRM_DEBUG_KMS("setting surface_id to 0 for primary surface %d on crtc %d\n", bo->surface_id, qcrtc->index);
  569. surf_id = 0;
  570. } else {
  571. surf_id = bo->surface_id;
  572. }
  573. if (old_bo && old_bo != bo) {
  574. old_bo->is_primary = false;
  575. ret = qxl_bo_reserve(old_bo, false);
  576. qxl_bo_unpin(old_bo);
  577. qxl_bo_unreserve(old_bo);
  578. }
  579. qxl_monitors_config_set(qdev, qcrtc->index, x, y,
  580. mode->hdisplay,
  581. mode->vdisplay, surf_id);
  582. return 0;
  583. }
  584. static void qxl_crtc_prepare(struct drm_crtc *crtc)
  585. {
  586. DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
  587. crtc->mode.hdisplay, crtc->mode.vdisplay,
  588. crtc->x, crtc->y, crtc->enabled);
  589. }
  590. static void qxl_crtc_commit(struct drm_crtc *crtc)
  591. {
  592. DRM_DEBUG("\n");
  593. }
  594. static void qxl_crtc_disable(struct drm_crtc *crtc)
  595. {
  596. struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
  597. struct drm_device *dev = crtc->dev;
  598. struct qxl_device *qdev = dev->dev_private;
  599. if (crtc->primary->fb) {
  600. struct qxl_framebuffer *qfb = to_qxl_framebuffer(crtc->primary->fb);
  601. struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
  602. int ret;
  603. ret = qxl_bo_reserve(bo, false);
  604. qxl_bo_unpin(bo);
  605. qxl_bo_unreserve(bo);
  606. crtc->primary->fb = NULL;
  607. }
  608. qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
  609. qxl_send_monitors_config(qdev);
  610. }
  611. static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
  612. .dpms = qxl_crtc_dpms,
  613. .disable = qxl_crtc_disable,
  614. .mode_fixup = qxl_crtc_mode_fixup,
  615. .mode_set = qxl_crtc_mode_set,
  616. .prepare = qxl_crtc_prepare,
  617. .commit = qxl_crtc_commit,
  618. };
  619. static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
  620. {
  621. struct qxl_crtc *qxl_crtc;
  622. qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
  623. if (!qxl_crtc)
  624. return -ENOMEM;
  625. drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
  626. qxl_crtc->index = crtc_id;
  627. drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
  628. return 0;
  629. }
  630. static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
  631. {
  632. DRM_DEBUG("\n");
  633. }
  634. static void qxl_enc_prepare(struct drm_encoder *encoder)
  635. {
  636. DRM_DEBUG("\n");
  637. }
  638. static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
  639. struct drm_encoder *encoder)
  640. {
  641. int i;
  642. struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
  643. struct qxl_head *head;
  644. struct drm_display_mode *mode;
  645. BUG_ON(!encoder);
  646. /* TODO: ugly, do better */
  647. i = output->index;
  648. if (!qdev->monitors_config ||
  649. qdev->monitors_config->max_allowed <= i) {
  650. DRM_ERROR(
  651. "head number too large or missing monitors config: %p, %d",
  652. qdev->monitors_config,
  653. qdev->monitors_config ?
  654. qdev->monitors_config->max_allowed : -1);
  655. return;
  656. }
  657. if (!encoder->crtc) {
  658. DRM_ERROR("missing crtc on encoder %p\n", encoder);
  659. return;
  660. }
  661. if (i != 0)
  662. DRM_DEBUG("missing for multiple monitors: no head holes\n");
  663. head = &qdev->monitors_config->heads[i];
  664. head->id = i;
  665. if (encoder->crtc->enabled) {
  666. mode = &encoder->crtc->mode;
  667. head->width = mode->hdisplay;
  668. head->height = mode->vdisplay;
  669. head->x = encoder->crtc->x;
  670. head->y = encoder->crtc->y;
  671. if (qdev->monitors_config->count < i + 1)
  672. qdev->monitors_config->count = i + 1;
  673. } else {
  674. head->width = 0;
  675. head->height = 0;
  676. head->x = 0;
  677. head->y = 0;
  678. }
  679. DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
  680. i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
  681. head->flags = 0;
  682. /* TODO - somewhere else to call this for multiple monitors
  683. * (config_commit?) */
  684. qxl_send_monitors_config(qdev);
  685. }
  686. static void qxl_enc_commit(struct drm_encoder *encoder)
  687. {
  688. struct qxl_device *qdev = encoder->dev->dev_private;
  689. qxl_write_monitors_config_for_encoder(qdev, encoder);
  690. DRM_DEBUG("\n");
  691. }
  692. static void qxl_enc_mode_set(struct drm_encoder *encoder,
  693. struct drm_display_mode *mode,
  694. struct drm_display_mode *adjusted_mode)
  695. {
  696. DRM_DEBUG("\n");
  697. }
  698. static int qxl_conn_get_modes(struct drm_connector *connector)
  699. {
  700. int ret = 0;
  701. struct qxl_device *qdev = connector->dev->dev_private;
  702. unsigned pwidth = 1024;
  703. unsigned pheight = 768;
  704. DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
  705. /* TODO: what should we do here? only show the configured modes for the
  706. * device, or allow the full list, or both? */
  707. if (qdev->monitors_config && qdev->monitors_config->count) {
  708. ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
  709. if (ret < 0)
  710. return ret;
  711. }
  712. ret += qxl_add_common_modes(connector, pwidth, pheight);
  713. return ret;
  714. }
  715. static int qxl_conn_mode_valid(struct drm_connector *connector,
  716. struct drm_display_mode *mode)
  717. {
  718. struct drm_device *ddev = connector->dev;
  719. struct qxl_device *qdev = ddev->dev_private;
  720. int i;
  721. /* TODO: is this called for user defined modes? (xrandr --add-mode)
  722. * TODO: check that the mode fits in the framebuffer */
  723. if(qdev->monitors_config_width == mode->hdisplay &&
  724. qdev->monitors_config_height == mode->vdisplay)
  725. return MODE_OK;
  726. for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
  727. if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
  728. return MODE_OK;
  729. }
  730. return MODE_BAD;
  731. }
  732. static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
  733. {
  734. struct qxl_output *qxl_output =
  735. drm_connector_to_qxl_output(connector);
  736. DRM_DEBUG("\n");
  737. return &qxl_output->enc;
  738. }
  739. static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
  740. .dpms = qxl_enc_dpms,
  741. .prepare = qxl_enc_prepare,
  742. .mode_set = qxl_enc_mode_set,
  743. .commit = qxl_enc_commit,
  744. };
  745. static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
  746. .get_modes = qxl_conn_get_modes,
  747. .mode_valid = qxl_conn_mode_valid,
  748. .best_encoder = qxl_best_encoder,
  749. };
  750. static enum drm_connector_status qxl_conn_detect(
  751. struct drm_connector *connector,
  752. bool force)
  753. {
  754. struct qxl_output *output =
  755. drm_connector_to_qxl_output(connector);
  756. struct drm_device *ddev = connector->dev;
  757. struct qxl_device *qdev = ddev->dev_private;
  758. bool connected = false;
  759. /* The first monitor is always connected */
  760. if (!qdev->client_monitors_config) {
  761. if (output->index == 0)
  762. connected = true;
  763. } else
  764. connected = qdev->client_monitors_config->count > output->index &&
  765. qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
  766. DRM_DEBUG("#%d connected: %d\n", output->index, connected);
  767. if (!connected)
  768. qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
  769. return connected ? connector_status_connected
  770. : connector_status_disconnected;
  771. }
  772. static int qxl_conn_set_property(struct drm_connector *connector,
  773. struct drm_property *property,
  774. uint64_t value)
  775. {
  776. DRM_DEBUG("\n");
  777. return 0;
  778. }
  779. static void qxl_conn_destroy(struct drm_connector *connector)
  780. {
  781. struct qxl_output *qxl_output =
  782. drm_connector_to_qxl_output(connector);
  783. drm_connector_unregister(connector);
  784. drm_connector_cleanup(connector);
  785. kfree(qxl_output);
  786. }
  787. static const struct drm_connector_funcs qxl_connector_funcs = {
  788. .dpms = drm_helper_connector_dpms,
  789. .detect = qxl_conn_detect,
  790. .fill_modes = drm_helper_probe_single_connector_modes,
  791. .set_property = qxl_conn_set_property,
  792. .destroy = qxl_conn_destroy,
  793. };
  794. static void qxl_enc_destroy(struct drm_encoder *encoder)
  795. {
  796. drm_encoder_cleanup(encoder);
  797. }
  798. static const struct drm_encoder_funcs qxl_enc_funcs = {
  799. .destroy = qxl_enc_destroy,
  800. };
  801. static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
  802. {
  803. if (qdev->hotplug_mode_update_property)
  804. return 0;
  805. qdev->hotplug_mode_update_property =
  806. drm_property_create_range(qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
  807. "hotplug_mode_update", 0, 1);
  808. return 0;
  809. }
  810. static int qdev_output_init(struct drm_device *dev, int num_output)
  811. {
  812. struct qxl_device *qdev = dev->dev_private;
  813. struct qxl_output *qxl_output;
  814. struct drm_connector *connector;
  815. struct drm_encoder *encoder;
  816. qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
  817. if (!qxl_output)
  818. return -ENOMEM;
  819. qxl_output->index = num_output;
  820. connector = &qxl_output->base;
  821. encoder = &qxl_output->enc;
  822. drm_connector_init(dev, &qxl_output->base,
  823. &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
  824. drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
  825. DRM_MODE_ENCODER_VIRTUAL, NULL);
  826. /* we get HPD via client monitors config */
  827. connector->polled = DRM_CONNECTOR_POLL_HPD;
  828. encoder->possible_crtcs = 1 << num_output;
  829. drm_mode_connector_attach_encoder(&qxl_output->base,
  830. &qxl_output->enc);
  831. drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
  832. drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
  833. drm_object_attach_property(&connector->base,
  834. qdev->hotplug_mode_update_property, 0);
  835. drm_object_attach_property(&connector->base,
  836. dev->mode_config.suggested_x_property, 0);
  837. drm_object_attach_property(&connector->base,
  838. dev->mode_config.suggested_y_property, 0);
  839. drm_connector_register(connector);
  840. return 0;
  841. }
  842. static struct drm_framebuffer *
  843. qxl_user_framebuffer_create(struct drm_device *dev,
  844. struct drm_file *file_priv,
  845. const struct drm_mode_fb_cmd2 *mode_cmd)
  846. {
  847. struct drm_gem_object *obj;
  848. struct qxl_framebuffer *qxl_fb;
  849. int ret;
  850. obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
  851. if (!obj)
  852. return NULL;
  853. qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
  854. if (qxl_fb == NULL)
  855. return NULL;
  856. ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
  857. if (ret) {
  858. kfree(qxl_fb);
  859. drm_gem_object_unreference_unlocked(obj);
  860. return NULL;
  861. }
  862. return &qxl_fb->base;
  863. }
  864. static const struct drm_mode_config_funcs qxl_mode_funcs = {
  865. .fb_create = qxl_user_framebuffer_create,
  866. };
  867. int qxl_create_monitors_object(struct qxl_device *qdev)
  868. {
  869. int ret;
  870. struct drm_gem_object *gobj;
  871. int max_allowed = qxl_num_crtc;
  872. int monitors_config_size = sizeof(struct qxl_monitors_config) +
  873. max_allowed * sizeof(struct qxl_head);
  874. ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
  875. QXL_GEM_DOMAIN_VRAM,
  876. false, false, NULL, &gobj);
  877. if (ret) {
  878. DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
  879. return -ENOMEM;
  880. }
  881. qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
  882. ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
  883. if (ret)
  884. return ret;
  885. ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
  886. if (ret) {
  887. qxl_bo_unreserve(qdev->monitors_config_bo);
  888. return ret;
  889. }
  890. qxl_bo_unreserve(qdev->monitors_config_bo);
  891. qxl_bo_kmap(qdev->monitors_config_bo, NULL);
  892. qdev->monitors_config = qdev->monitors_config_bo->kptr;
  893. qdev->ram_header->monitors_config =
  894. qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
  895. memset(qdev->monitors_config, 0, monitors_config_size);
  896. qdev->monitors_config->max_allowed = max_allowed;
  897. return 0;
  898. }
  899. int qxl_destroy_monitors_object(struct qxl_device *qdev)
  900. {
  901. int ret;
  902. qdev->monitors_config = NULL;
  903. qdev->ram_header->monitors_config = 0;
  904. qxl_bo_kunmap(qdev->monitors_config_bo);
  905. ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
  906. if (ret)
  907. return ret;
  908. qxl_bo_unpin(qdev->monitors_config_bo);
  909. qxl_bo_unreserve(qdev->monitors_config_bo);
  910. qxl_bo_unref(&qdev->monitors_config_bo);
  911. return 0;
  912. }
  913. int qxl_modeset_init(struct qxl_device *qdev)
  914. {
  915. int i;
  916. int ret;
  917. drm_mode_config_init(qdev->ddev);
  918. ret = qxl_create_monitors_object(qdev);
  919. if (ret)
  920. return ret;
  921. qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
  922. /* modes will be validated against the framebuffer size */
  923. qdev->ddev->mode_config.min_width = 320;
  924. qdev->ddev->mode_config.min_height = 200;
  925. qdev->ddev->mode_config.max_width = 8192;
  926. qdev->ddev->mode_config.max_height = 8192;
  927. qdev->ddev->mode_config.fb_base = qdev->vram_base;
  928. drm_mode_create_suggested_offset_properties(qdev->ddev);
  929. qxl_mode_create_hotplug_mode_update_property(qdev);
  930. for (i = 0 ; i < qxl_num_crtc; ++i) {
  931. qdev_crtc_init(qdev->ddev, i);
  932. qdev_output_init(qdev->ddev, i);
  933. }
  934. qdev->mode_info.mode_config_initialized = true;
  935. /* primary surface must be created by this point, to allow
  936. * issuing command queue commands and having them read by
  937. * spice server. */
  938. qxl_fbdev_init(qdev);
  939. return 0;
  940. }
  941. void qxl_modeset_fini(struct qxl_device *qdev)
  942. {
  943. qxl_fbdev_fini(qdev);
  944. qxl_destroy_monitors_object(qdev);
  945. if (qdev->mode_info.mode_config_initialized) {
  946. drm_mode_config_cleanup(qdev->ddev);
  947. qdev->mode_info.mode_config_initialized = false;
  948. }
  949. }