qxl_display.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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_framebuffer *qfb_src = to_qxl_framebuffer(fb);
  203. struct qxl_framebuffer *qfb_old = to_qxl_framebuffer(crtc->primary->fb);
  204. struct qxl_bo *bo_old = gem_to_qxl_bo(qfb_old->obj);
  205. struct qxl_bo *bo = gem_to_qxl_bo(qfb_src->obj);
  206. unsigned long flags;
  207. struct drm_clip_rect norect = {
  208. .x1 = 0,
  209. .y1 = 0,
  210. .x2 = fb->width,
  211. .y2 = fb->height
  212. };
  213. int inc = 1;
  214. int one_clip_rect = 1;
  215. int ret = 0;
  216. crtc->primary->fb = fb;
  217. bo_old->is_primary = false;
  218. bo->is_primary = true;
  219. ret = qxl_bo_reserve(bo, false);
  220. if (ret)
  221. return ret;
  222. ret = qxl_bo_pin(bo, bo->type, NULL);
  223. qxl_bo_unreserve(bo);
  224. if (ret)
  225. return ret;
  226. qxl_draw_dirty_fb(qdev, qfb_src, bo, 0, 0,
  227. &norect, one_clip_rect, inc);
  228. drm_crtc_vblank_get(crtc);
  229. if (event) {
  230. spin_lock_irqsave(&dev->event_lock, flags);
  231. drm_crtc_send_vblank_event(crtc, event);
  232. spin_unlock_irqrestore(&dev->event_lock, flags);
  233. }
  234. drm_crtc_vblank_put(crtc);
  235. ret = qxl_bo_reserve(bo, false);
  236. if (!ret) {
  237. qxl_bo_unpin(bo);
  238. qxl_bo_unreserve(bo);
  239. }
  240. return 0;
  241. }
  242. static int
  243. qxl_hide_cursor(struct qxl_device *qdev)
  244. {
  245. struct qxl_release *release;
  246. struct qxl_cursor_cmd *cmd;
  247. int ret;
  248. ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
  249. &release, NULL);
  250. if (ret)
  251. return ret;
  252. ret = qxl_release_reserve_list(release, true);
  253. if (ret) {
  254. qxl_release_free(qdev, release);
  255. return ret;
  256. }
  257. cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
  258. cmd->type = QXL_CURSOR_HIDE;
  259. qxl_release_unmap(qdev, release, &cmd->release_info);
  260. qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
  261. qxl_release_fence_buffer_objects(release);
  262. return 0;
  263. }
  264. static int qxl_crtc_cursor_set2(struct drm_crtc *crtc,
  265. struct drm_file *file_priv,
  266. uint32_t handle,
  267. uint32_t width,
  268. uint32_t height, int32_t hot_x, int32_t hot_y)
  269. {
  270. struct drm_device *dev = crtc->dev;
  271. struct qxl_device *qdev = dev->dev_private;
  272. struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
  273. struct drm_gem_object *obj;
  274. struct qxl_cursor *cursor;
  275. struct qxl_cursor_cmd *cmd;
  276. struct qxl_bo *cursor_bo, *user_bo;
  277. struct qxl_release *release;
  278. void *user_ptr;
  279. int size = 64*64*4;
  280. int ret = 0;
  281. if (!handle)
  282. return qxl_hide_cursor(qdev);
  283. obj = drm_gem_object_lookup(file_priv, handle);
  284. if (!obj) {
  285. DRM_ERROR("cannot find cursor object\n");
  286. return -ENOENT;
  287. }
  288. user_bo = gem_to_qxl_bo(obj);
  289. ret = qxl_bo_reserve(user_bo, false);
  290. if (ret)
  291. goto out_unref;
  292. ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
  293. qxl_bo_unreserve(user_bo);
  294. if (ret)
  295. goto out_unref;
  296. ret = qxl_bo_kmap(user_bo, &user_ptr);
  297. if (ret)
  298. goto out_unpin;
  299. ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
  300. QXL_RELEASE_CURSOR_CMD,
  301. &release, NULL);
  302. if (ret)
  303. goto out_kunmap;
  304. ret = qxl_alloc_bo_reserved(qdev, release, sizeof(struct qxl_cursor) + size,
  305. &cursor_bo);
  306. if (ret)
  307. goto out_free_release;
  308. ret = qxl_release_reserve_list(release, false);
  309. if (ret)
  310. goto out_free_bo;
  311. ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
  312. if (ret)
  313. goto out_backoff;
  314. cursor->header.unique = 0;
  315. cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
  316. cursor->header.width = 64;
  317. cursor->header.height = 64;
  318. cursor->header.hot_spot_x = hot_x;
  319. cursor->header.hot_spot_y = hot_y;
  320. cursor->data_size = size;
  321. cursor->chunk.next_chunk = 0;
  322. cursor->chunk.prev_chunk = 0;
  323. cursor->chunk.data_size = size;
  324. memcpy(cursor->chunk.data, user_ptr, size);
  325. qxl_bo_kunmap(cursor_bo);
  326. qxl_bo_kunmap(user_bo);
  327. qcrtc->cur_x += qcrtc->hot_spot_x - hot_x;
  328. qcrtc->cur_y += qcrtc->hot_spot_y - hot_y;
  329. qcrtc->hot_spot_x = hot_x;
  330. qcrtc->hot_spot_y = hot_y;
  331. cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
  332. cmd->type = QXL_CURSOR_SET;
  333. cmd->u.set.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
  334. cmd->u.set.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
  335. cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
  336. cmd->u.set.visible = 1;
  337. qxl_release_unmap(qdev, release, &cmd->release_info);
  338. qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
  339. qxl_release_fence_buffer_objects(release);
  340. /* finish with the userspace bo */
  341. ret = qxl_bo_reserve(user_bo, false);
  342. if (!ret) {
  343. qxl_bo_unpin(user_bo);
  344. qxl_bo_unreserve(user_bo);
  345. }
  346. drm_gem_object_unreference_unlocked(obj);
  347. qxl_bo_unref(&cursor_bo);
  348. return ret;
  349. out_backoff:
  350. qxl_release_backoff_reserve_list(release);
  351. out_free_bo:
  352. qxl_bo_unref(&cursor_bo);
  353. out_free_release:
  354. qxl_release_free(qdev, release);
  355. out_kunmap:
  356. qxl_bo_kunmap(user_bo);
  357. out_unpin:
  358. qxl_bo_unpin(user_bo);
  359. out_unref:
  360. drm_gem_object_unreference_unlocked(obj);
  361. return ret;
  362. }
  363. static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
  364. int x, int y)
  365. {
  366. struct drm_device *dev = crtc->dev;
  367. struct qxl_device *qdev = dev->dev_private;
  368. struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
  369. struct qxl_release *release;
  370. struct qxl_cursor_cmd *cmd;
  371. int ret;
  372. ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
  373. &release, NULL);
  374. if (ret)
  375. return ret;
  376. ret = qxl_release_reserve_list(release, true);
  377. if (ret) {
  378. qxl_release_free(qdev, release);
  379. return ret;
  380. }
  381. qcrtc->cur_x = x;
  382. qcrtc->cur_y = y;
  383. cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
  384. cmd->type = QXL_CURSOR_MOVE;
  385. cmd->u.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
  386. cmd->u.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
  387. qxl_release_unmap(qdev, release, &cmd->release_info);
  388. qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
  389. qxl_release_fence_buffer_objects(release);
  390. return 0;
  391. }
  392. static const struct drm_crtc_funcs qxl_crtc_funcs = {
  393. .cursor_set2 = qxl_crtc_cursor_set2,
  394. .cursor_move = qxl_crtc_cursor_move,
  395. .set_config = drm_crtc_helper_set_config,
  396. .destroy = qxl_crtc_destroy,
  397. .page_flip = qxl_crtc_page_flip,
  398. };
  399. void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
  400. {
  401. struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
  402. drm_gem_object_unreference_unlocked(qxl_fb->obj);
  403. drm_framebuffer_cleanup(fb);
  404. kfree(qxl_fb);
  405. }
  406. static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
  407. struct drm_file *file_priv,
  408. unsigned flags, unsigned color,
  409. struct drm_clip_rect *clips,
  410. unsigned num_clips)
  411. {
  412. /* TODO: vmwgfx where this was cribbed from had locking. Why? */
  413. struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
  414. struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
  415. struct drm_clip_rect norect;
  416. struct qxl_bo *qobj;
  417. int inc = 1;
  418. drm_modeset_lock_all(fb->dev);
  419. qobj = gem_to_qxl_bo(qxl_fb->obj);
  420. /* if we aren't primary surface ignore this */
  421. if (!qobj->is_primary) {
  422. drm_modeset_unlock_all(fb->dev);
  423. return 0;
  424. }
  425. if (!num_clips) {
  426. num_clips = 1;
  427. clips = &norect;
  428. norect.x1 = norect.y1 = 0;
  429. norect.x2 = fb->width;
  430. norect.y2 = fb->height;
  431. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  432. num_clips /= 2;
  433. inc = 2; /* skip source rects */
  434. }
  435. qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
  436. clips, num_clips, inc);
  437. drm_modeset_unlock_all(fb->dev);
  438. return 0;
  439. }
  440. static const struct drm_framebuffer_funcs qxl_fb_funcs = {
  441. .destroy = qxl_user_framebuffer_destroy,
  442. .dirty = qxl_framebuffer_surface_dirty,
  443. /* TODO?
  444. * .create_handle = qxl_user_framebuffer_create_handle, */
  445. };
  446. int
  447. qxl_framebuffer_init(struct drm_device *dev,
  448. struct qxl_framebuffer *qfb,
  449. const struct drm_mode_fb_cmd2 *mode_cmd,
  450. struct drm_gem_object *obj,
  451. const struct drm_framebuffer_funcs *funcs)
  452. {
  453. int ret;
  454. qfb->obj = obj;
  455. ret = drm_framebuffer_init(dev, &qfb->base, funcs);
  456. if (ret) {
  457. qfb->obj = NULL;
  458. return ret;
  459. }
  460. drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
  461. return 0;
  462. }
  463. static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
  464. {
  465. }
  466. static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
  467. const struct drm_display_mode *mode,
  468. struct drm_display_mode *adjusted_mode)
  469. {
  470. struct drm_device *dev = crtc->dev;
  471. struct qxl_device *qdev = dev->dev_private;
  472. qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
  473. __func__,
  474. mode->hdisplay, mode->vdisplay,
  475. adjusted_mode->hdisplay,
  476. adjusted_mode->vdisplay);
  477. return true;
  478. }
  479. void
  480. qxl_send_monitors_config(struct qxl_device *qdev)
  481. {
  482. int i;
  483. BUG_ON(!qdev->ram_header->monitors_config);
  484. if (qdev->monitors_config->count == 0) {
  485. qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
  486. return;
  487. }
  488. for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
  489. struct qxl_head *head = &qdev->monitors_config->heads[i];
  490. if (head->y > 8192 || head->x > 8192 ||
  491. head->width > 8192 || head->height > 8192) {
  492. DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
  493. i, head->width, head->height,
  494. head->x, head->y);
  495. return;
  496. }
  497. }
  498. qxl_io_monitors_config(qdev);
  499. }
  500. static void qxl_monitors_config_set(struct qxl_device *qdev,
  501. int index,
  502. unsigned x, unsigned y,
  503. unsigned width, unsigned height,
  504. unsigned surf_id)
  505. {
  506. DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
  507. qdev->monitors_config->heads[index].x = x;
  508. qdev->monitors_config->heads[index].y = y;
  509. qdev->monitors_config->heads[index].width = width;
  510. qdev->monitors_config->heads[index].height = height;
  511. qdev->monitors_config->heads[index].surface_id = surf_id;
  512. }
  513. static int qxl_crtc_mode_set(struct drm_crtc *crtc,
  514. struct drm_display_mode *mode,
  515. struct drm_display_mode *adjusted_mode,
  516. int x, int y,
  517. struct drm_framebuffer *old_fb)
  518. {
  519. struct drm_device *dev = crtc->dev;
  520. struct qxl_device *qdev = dev->dev_private;
  521. struct qxl_framebuffer *qfb;
  522. struct qxl_bo *bo, *old_bo = NULL;
  523. struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
  524. bool recreate_primary = false;
  525. int ret;
  526. int surf_id;
  527. if (!crtc->primary->fb) {
  528. DRM_DEBUG_KMS("No FB bound\n");
  529. return 0;
  530. }
  531. if (old_fb) {
  532. qfb = to_qxl_framebuffer(old_fb);
  533. old_bo = gem_to_qxl_bo(qfb->obj);
  534. }
  535. qfb = to_qxl_framebuffer(crtc->primary->fb);
  536. bo = gem_to_qxl_bo(qfb->obj);
  537. DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
  538. x, y,
  539. mode->hdisplay, mode->vdisplay,
  540. adjusted_mode->hdisplay,
  541. adjusted_mode->vdisplay);
  542. if (bo->is_primary == false)
  543. recreate_primary = true;
  544. if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
  545. DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
  546. return -EINVAL;
  547. }
  548. ret = qxl_bo_reserve(bo, false);
  549. if (ret != 0)
  550. return ret;
  551. ret = qxl_bo_pin(bo, bo->type, NULL);
  552. if (ret != 0) {
  553. qxl_bo_unreserve(bo);
  554. return -EINVAL;
  555. }
  556. qxl_bo_unreserve(bo);
  557. if (recreate_primary) {
  558. qxl_io_destroy_primary(qdev);
  559. qxl_io_log(qdev,
  560. "recreate primary: %dx%d,%d,%d\n",
  561. bo->surf.width, bo->surf.height,
  562. bo->surf.stride, bo->surf.format);
  563. qxl_io_create_primary(qdev, 0, bo);
  564. bo->is_primary = true;
  565. }
  566. if (bo->is_primary) {
  567. DRM_DEBUG_KMS("setting surface_id to 0 for primary surface %d on crtc %d\n", bo->surface_id, qcrtc->index);
  568. surf_id = 0;
  569. } else {
  570. surf_id = bo->surface_id;
  571. }
  572. if (old_bo && old_bo != bo) {
  573. old_bo->is_primary = false;
  574. ret = qxl_bo_reserve(old_bo, false);
  575. qxl_bo_unpin(old_bo);
  576. qxl_bo_unreserve(old_bo);
  577. }
  578. qxl_monitors_config_set(qdev, qcrtc->index, x, y,
  579. mode->hdisplay,
  580. mode->vdisplay, surf_id);
  581. return 0;
  582. }
  583. static void qxl_crtc_prepare(struct drm_crtc *crtc)
  584. {
  585. DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
  586. crtc->mode.hdisplay, crtc->mode.vdisplay,
  587. crtc->x, crtc->y, crtc->enabled);
  588. }
  589. static void qxl_crtc_commit(struct drm_crtc *crtc)
  590. {
  591. DRM_DEBUG("\n");
  592. }
  593. static void qxl_crtc_disable(struct drm_crtc *crtc)
  594. {
  595. struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
  596. struct drm_device *dev = crtc->dev;
  597. struct qxl_device *qdev = dev->dev_private;
  598. if (crtc->primary->fb) {
  599. struct qxl_framebuffer *qfb = to_qxl_framebuffer(crtc->primary->fb);
  600. struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
  601. int ret;
  602. ret = qxl_bo_reserve(bo, false);
  603. qxl_bo_unpin(bo);
  604. qxl_bo_unreserve(bo);
  605. crtc->primary->fb = NULL;
  606. }
  607. qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
  608. qxl_send_monitors_config(qdev);
  609. }
  610. static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
  611. .dpms = qxl_crtc_dpms,
  612. .disable = qxl_crtc_disable,
  613. .mode_fixup = qxl_crtc_mode_fixup,
  614. .mode_set = qxl_crtc_mode_set,
  615. .prepare = qxl_crtc_prepare,
  616. .commit = qxl_crtc_commit,
  617. };
  618. static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
  619. {
  620. struct qxl_crtc *qxl_crtc;
  621. qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
  622. if (!qxl_crtc)
  623. return -ENOMEM;
  624. drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
  625. qxl_crtc->index = crtc_id;
  626. drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
  627. return 0;
  628. }
  629. static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
  630. {
  631. DRM_DEBUG("\n");
  632. }
  633. static void qxl_enc_prepare(struct drm_encoder *encoder)
  634. {
  635. DRM_DEBUG("\n");
  636. }
  637. static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
  638. struct drm_encoder *encoder)
  639. {
  640. int i;
  641. struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
  642. struct qxl_head *head;
  643. struct drm_display_mode *mode;
  644. BUG_ON(!encoder);
  645. /* TODO: ugly, do better */
  646. i = output->index;
  647. if (!qdev->monitors_config ||
  648. qdev->monitors_config->max_allowed <= i) {
  649. DRM_ERROR(
  650. "head number too large or missing monitors config: %p, %d",
  651. qdev->monitors_config,
  652. qdev->monitors_config ?
  653. qdev->monitors_config->max_allowed : -1);
  654. return;
  655. }
  656. if (!encoder->crtc) {
  657. DRM_ERROR("missing crtc on encoder %p\n", encoder);
  658. return;
  659. }
  660. if (i != 0)
  661. DRM_DEBUG("missing for multiple monitors: no head holes\n");
  662. head = &qdev->monitors_config->heads[i];
  663. head->id = i;
  664. if (encoder->crtc->enabled) {
  665. mode = &encoder->crtc->mode;
  666. head->width = mode->hdisplay;
  667. head->height = mode->vdisplay;
  668. head->x = encoder->crtc->x;
  669. head->y = encoder->crtc->y;
  670. if (qdev->monitors_config->count < i + 1)
  671. qdev->monitors_config->count = i + 1;
  672. } else {
  673. head->width = 0;
  674. head->height = 0;
  675. head->x = 0;
  676. head->y = 0;
  677. }
  678. DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
  679. i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
  680. head->flags = 0;
  681. /* TODO - somewhere else to call this for multiple monitors
  682. * (config_commit?) */
  683. qxl_send_monitors_config(qdev);
  684. }
  685. static void qxl_enc_commit(struct drm_encoder *encoder)
  686. {
  687. struct qxl_device *qdev = encoder->dev->dev_private;
  688. qxl_write_monitors_config_for_encoder(qdev, encoder);
  689. DRM_DEBUG("\n");
  690. }
  691. static void qxl_enc_mode_set(struct drm_encoder *encoder,
  692. struct drm_display_mode *mode,
  693. struct drm_display_mode *adjusted_mode)
  694. {
  695. DRM_DEBUG("\n");
  696. }
  697. static int qxl_conn_get_modes(struct drm_connector *connector)
  698. {
  699. int ret = 0;
  700. struct qxl_device *qdev = connector->dev->dev_private;
  701. unsigned pwidth = 1024;
  702. unsigned pheight = 768;
  703. DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
  704. /* TODO: what should we do here? only show the configured modes for the
  705. * device, or allow the full list, or both? */
  706. if (qdev->monitors_config && qdev->monitors_config->count) {
  707. ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
  708. if (ret < 0)
  709. return ret;
  710. }
  711. ret += qxl_add_common_modes(connector, pwidth, pheight);
  712. return ret;
  713. }
  714. static int qxl_conn_mode_valid(struct drm_connector *connector,
  715. struct drm_display_mode *mode)
  716. {
  717. struct drm_device *ddev = connector->dev;
  718. struct qxl_device *qdev = ddev->dev_private;
  719. int i;
  720. /* TODO: is this called for user defined modes? (xrandr --add-mode)
  721. * TODO: check that the mode fits in the framebuffer */
  722. if(qdev->monitors_config_width == mode->hdisplay &&
  723. qdev->monitors_config_height == mode->vdisplay)
  724. return MODE_OK;
  725. for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
  726. if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
  727. return MODE_OK;
  728. }
  729. return MODE_BAD;
  730. }
  731. static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
  732. {
  733. struct qxl_output *qxl_output =
  734. drm_connector_to_qxl_output(connector);
  735. DRM_DEBUG("\n");
  736. return &qxl_output->enc;
  737. }
  738. static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
  739. .dpms = qxl_enc_dpms,
  740. .prepare = qxl_enc_prepare,
  741. .mode_set = qxl_enc_mode_set,
  742. .commit = qxl_enc_commit,
  743. };
  744. static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
  745. .get_modes = qxl_conn_get_modes,
  746. .mode_valid = qxl_conn_mode_valid,
  747. .best_encoder = qxl_best_encoder,
  748. };
  749. static enum drm_connector_status qxl_conn_detect(
  750. struct drm_connector *connector,
  751. bool force)
  752. {
  753. struct qxl_output *output =
  754. drm_connector_to_qxl_output(connector);
  755. struct drm_device *ddev = connector->dev;
  756. struct qxl_device *qdev = ddev->dev_private;
  757. bool connected = false;
  758. /* The first monitor is always connected */
  759. if (!qdev->client_monitors_config) {
  760. if (output->index == 0)
  761. connected = true;
  762. } else
  763. connected = qdev->client_monitors_config->count > output->index &&
  764. qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
  765. DRM_DEBUG("#%d connected: %d\n", output->index, connected);
  766. if (!connected)
  767. qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
  768. return connected ? connector_status_connected
  769. : connector_status_disconnected;
  770. }
  771. static int qxl_conn_set_property(struct drm_connector *connector,
  772. struct drm_property *property,
  773. uint64_t value)
  774. {
  775. DRM_DEBUG("\n");
  776. return 0;
  777. }
  778. static void qxl_conn_destroy(struct drm_connector *connector)
  779. {
  780. struct qxl_output *qxl_output =
  781. drm_connector_to_qxl_output(connector);
  782. drm_connector_unregister(connector);
  783. drm_connector_cleanup(connector);
  784. kfree(qxl_output);
  785. }
  786. static const struct drm_connector_funcs qxl_connector_funcs = {
  787. .dpms = drm_helper_connector_dpms,
  788. .detect = qxl_conn_detect,
  789. .fill_modes = drm_helper_probe_single_connector_modes,
  790. .set_property = qxl_conn_set_property,
  791. .destroy = qxl_conn_destroy,
  792. };
  793. static void qxl_enc_destroy(struct drm_encoder *encoder)
  794. {
  795. drm_encoder_cleanup(encoder);
  796. }
  797. static const struct drm_encoder_funcs qxl_enc_funcs = {
  798. .destroy = qxl_enc_destroy,
  799. };
  800. static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
  801. {
  802. if (qdev->hotplug_mode_update_property)
  803. return 0;
  804. qdev->hotplug_mode_update_property =
  805. drm_property_create_range(qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
  806. "hotplug_mode_update", 0, 1);
  807. return 0;
  808. }
  809. static int qdev_output_init(struct drm_device *dev, int num_output)
  810. {
  811. struct qxl_device *qdev = dev->dev_private;
  812. struct qxl_output *qxl_output;
  813. struct drm_connector *connector;
  814. struct drm_encoder *encoder;
  815. qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
  816. if (!qxl_output)
  817. return -ENOMEM;
  818. qxl_output->index = num_output;
  819. connector = &qxl_output->base;
  820. encoder = &qxl_output->enc;
  821. drm_connector_init(dev, &qxl_output->base,
  822. &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
  823. drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
  824. DRM_MODE_ENCODER_VIRTUAL, NULL);
  825. /* we get HPD via client monitors config */
  826. connector->polled = DRM_CONNECTOR_POLL_HPD;
  827. encoder->possible_crtcs = 1 << num_output;
  828. drm_mode_connector_attach_encoder(&qxl_output->base,
  829. &qxl_output->enc);
  830. drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
  831. drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
  832. drm_object_attach_property(&connector->base,
  833. qdev->hotplug_mode_update_property, 0);
  834. drm_object_attach_property(&connector->base,
  835. dev->mode_config.suggested_x_property, 0);
  836. drm_object_attach_property(&connector->base,
  837. dev->mode_config.suggested_y_property, 0);
  838. drm_connector_register(connector);
  839. return 0;
  840. }
  841. static struct drm_framebuffer *
  842. qxl_user_framebuffer_create(struct drm_device *dev,
  843. struct drm_file *file_priv,
  844. const struct drm_mode_fb_cmd2 *mode_cmd)
  845. {
  846. struct drm_gem_object *obj;
  847. struct qxl_framebuffer *qxl_fb;
  848. int ret;
  849. obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
  850. if (!obj)
  851. return NULL;
  852. qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
  853. if (qxl_fb == NULL)
  854. return NULL;
  855. ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
  856. if (ret) {
  857. kfree(qxl_fb);
  858. drm_gem_object_unreference_unlocked(obj);
  859. return NULL;
  860. }
  861. return &qxl_fb->base;
  862. }
  863. static const struct drm_mode_config_funcs qxl_mode_funcs = {
  864. .fb_create = qxl_user_framebuffer_create,
  865. };
  866. int qxl_create_monitors_object(struct qxl_device *qdev)
  867. {
  868. int ret;
  869. struct drm_gem_object *gobj;
  870. int max_allowed = qxl_num_crtc;
  871. int monitors_config_size = sizeof(struct qxl_monitors_config) +
  872. max_allowed * sizeof(struct qxl_head);
  873. ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
  874. QXL_GEM_DOMAIN_VRAM,
  875. false, false, NULL, &gobj);
  876. if (ret) {
  877. DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
  878. return -ENOMEM;
  879. }
  880. qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
  881. ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
  882. if (ret)
  883. return ret;
  884. ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
  885. if (ret) {
  886. qxl_bo_unreserve(qdev->monitors_config_bo);
  887. return ret;
  888. }
  889. qxl_bo_unreserve(qdev->monitors_config_bo);
  890. qxl_bo_kmap(qdev->monitors_config_bo, NULL);
  891. qdev->monitors_config = qdev->monitors_config_bo->kptr;
  892. qdev->ram_header->monitors_config =
  893. qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
  894. memset(qdev->monitors_config, 0, monitors_config_size);
  895. qdev->monitors_config->max_allowed = max_allowed;
  896. return 0;
  897. }
  898. int qxl_destroy_monitors_object(struct qxl_device *qdev)
  899. {
  900. int ret;
  901. qdev->monitors_config = NULL;
  902. qdev->ram_header->monitors_config = 0;
  903. qxl_bo_kunmap(qdev->monitors_config_bo);
  904. ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
  905. if (ret)
  906. return ret;
  907. qxl_bo_unpin(qdev->monitors_config_bo);
  908. qxl_bo_unreserve(qdev->monitors_config_bo);
  909. qxl_bo_unref(&qdev->monitors_config_bo);
  910. return 0;
  911. }
  912. int qxl_modeset_init(struct qxl_device *qdev)
  913. {
  914. int i;
  915. int ret;
  916. drm_mode_config_init(qdev->ddev);
  917. ret = qxl_create_monitors_object(qdev);
  918. if (ret)
  919. return ret;
  920. qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
  921. /* modes will be validated against the framebuffer size */
  922. qdev->ddev->mode_config.min_width = 320;
  923. qdev->ddev->mode_config.min_height = 200;
  924. qdev->ddev->mode_config.max_width = 8192;
  925. qdev->ddev->mode_config.max_height = 8192;
  926. qdev->ddev->mode_config.fb_base = qdev->vram_base;
  927. drm_mode_create_suggested_offset_properties(qdev->ddev);
  928. qxl_mode_create_hotplug_mode_update_property(qdev);
  929. for (i = 0 ; i < qxl_num_crtc; ++i) {
  930. qdev_crtc_init(qdev->ddev, i);
  931. qdev_output_init(qdev->ddev, i);
  932. }
  933. qdev->mode_info.mode_config_initialized = true;
  934. /* primary surface must be created by this point, to allow
  935. * issuing command queue commands and having them read by
  936. * spice server. */
  937. qxl_fbdev_init(qdev);
  938. return 0;
  939. }
  940. void qxl_modeset_fini(struct qxl_device *qdev)
  941. {
  942. qxl_fbdev_fini(qdev);
  943. qxl_destroy_monitors_object(qdev);
  944. if (qdev->mode_info.mode_config_initialized) {
  945. drm_mode_config_cleanup(qdev->ddev);
  946. qdev->mode_info.mode_config_initialized = false;
  947. }
  948. }