qxl_display.c 30 KB

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