qxl_display.c 29 KB

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