qxl_display.c 26 KB

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