qxl_display.c 26 KB

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