qxl_display.c 30 KB

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