qxl_display.c 25 KB

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