qxl_display.c 28 KB

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