qxl_display.c 26 KB

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