qxl_display.c 26 KB

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