qxl_display.c 30 KB

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