qxl_display.c 27 KB

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