qxl_display.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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. }
  80. if (!qdev->client_monitors_config) {
  81. qdev->client_monitors_config = kzalloc(
  82. sizeof(struct qxl_monitors_config) +
  83. sizeof(struct qxl_head) * count, GFP_KERNEL);
  84. if (!qdev->client_monitors_config) {
  85. qxl_io_log(qdev,
  86. "%s: allocation failure for %u heads\n",
  87. __func__, count);
  88. return;
  89. }
  90. }
  91. qdev->client_monitors_config->count = count;
  92. }
  93. static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
  94. {
  95. int i;
  96. int num_monitors;
  97. uint32_t crc;
  98. BUG_ON(!qdev->monitors_config);
  99. num_monitors = qdev->rom->client_monitors_config.count;
  100. crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
  101. sizeof(qdev->rom->client_monitors_config));
  102. if (crc != qdev->rom->client_monitors_config_crc) {
  103. qxl_io_log(qdev, "crc mismatch: have %X (%d) != %X\n", crc,
  104. sizeof(qdev->rom->client_monitors_config),
  105. qdev->rom->client_monitors_config_crc);
  106. return 1;
  107. }
  108. if (num_monitors > qdev->monitors_config->max_allowed) {
  109. DRM_INFO("client monitors list will be truncated: %d < %d\n",
  110. qdev->monitors_config->max_allowed, num_monitors);
  111. num_monitors = qdev->monitors_config->max_allowed;
  112. } else {
  113. num_monitors = qdev->rom->client_monitors_config.count;
  114. }
  115. qxl_alloc_client_monitors_config(qdev, num_monitors);
  116. /* we copy max from the client but it isn't used */
  117. qdev->client_monitors_config->max_allowed =
  118. qdev->monitors_config->max_allowed;
  119. for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
  120. struct qxl_urect *c_rect =
  121. &qdev->rom->client_monitors_config.heads[i];
  122. struct qxl_head *client_head =
  123. &qdev->client_monitors_config->heads[i];
  124. struct qxl_head *head = &qdev->monitors_config->heads[i];
  125. client_head->x = head->x = c_rect->left;
  126. client_head->y = head->y = c_rect->top;
  127. client_head->width = head->width =
  128. c_rect->right - c_rect->left;
  129. client_head->height = head->height =
  130. c_rect->bottom - c_rect->top;
  131. client_head->surface_id = head->surface_id = 0;
  132. client_head->id = head->id = i;
  133. client_head->flags = head->flags = 0;
  134. QXL_DEBUG(qdev, "read %dx%d+%d+%d\n", head->width, head->height,
  135. head->x, head->y);
  136. }
  137. return 0;
  138. }
  139. void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
  140. {
  141. while (qxl_display_copy_rom_client_monitors_config(qdev)) {
  142. qxl_io_log(qdev, "failed crc check for client_monitors_config,"
  143. " retrying\n");
  144. }
  145. qxl_crtc_set_from_monitors_config(qdev);
  146. /* fire off a uevent and let userspace tell us what to do */
  147. qxl_io_log(qdev, "calling drm_sysfs_hotplug_event\n");
  148. drm_sysfs_hotplug_event(qdev->ddev);
  149. }
  150. static int qxl_add_monitors_config_modes(struct drm_connector *connector)
  151. {
  152. struct drm_device *dev = connector->dev;
  153. struct qxl_device *qdev = dev->dev_private;
  154. struct qxl_output *output = drm_connector_to_qxl_output(connector);
  155. int h = output->index;
  156. struct drm_display_mode *mode = NULL;
  157. struct qxl_head *head;
  158. if (!qdev->monitors_config)
  159. return 0;
  160. head = &qdev->monitors_config->heads[h];
  161. mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
  162. false);
  163. mode->type |= DRM_MODE_TYPE_PREFERRED;
  164. drm_mode_probed_add(connector, mode);
  165. return 1;
  166. }
  167. static int qxl_add_common_modes(struct drm_connector *connector)
  168. {
  169. struct drm_device *dev = connector->dev;
  170. struct drm_display_mode *mode = NULL;
  171. int i;
  172. struct mode_size {
  173. int w;
  174. int h;
  175. } common_modes[] = {
  176. { 640, 480},
  177. { 720, 480},
  178. { 800, 600},
  179. { 848, 480},
  180. {1024, 768},
  181. {1152, 768},
  182. {1280, 720},
  183. {1280, 800},
  184. {1280, 854},
  185. {1280, 960},
  186. {1280, 1024},
  187. {1440, 900},
  188. {1400, 1050},
  189. {1680, 1050},
  190. {1600, 1200},
  191. {1920, 1080},
  192. {1920, 1200}
  193. };
  194. for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
  195. if (common_modes[i].w < 320 || common_modes[i].h < 200)
  196. continue;
  197. mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
  198. 60, false, false, false);
  199. if (common_modes[i].w == 1024 && common_modes[i].h == 768)
  200. mode->type |= DRM_MODE_TYPE_PREFERRED;
  201. drm_mode_probed_add(connector, mode);
  202. }
  203. return i - 1;
  204. }
  205. static void qxl_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  206. u16 *blue, uint32_t start, uint32_t size)
  207. {
  208. /* TODO */
  209. }
  210. static void qxl_crtc_destroy(struct drm_crtc *crtc)
  211. {
  212. struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
  213. drm_crtc_cleanup(crtc);
  214. kfree(qxl_crtc);
  215. }
  216. static void
  217. qxl_hide_cursor(struct qxl_device *qdev)
  218. {
  219. struct qxl_release *release;
  220. struct qxl_cursor_cmd *cmd;
  221. int ret;
  222. ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
  223. &release, NULL);
  224. cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
  225. cmd->type = QXL_CURSOR_HIDE;
  226. qxl_release_unmap(qdev, release, &cmd->release_info);
  227. qxl_fence_releaseable(qdev, release);
  228. qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
  229. qxl_release_unreserve(qdev, release);
  230. }
  231. static int qxl_crtc_cursor_set(struct drm_crtc *crtc,
  232. struct drm_file *file_priv,
  233. uint32_t handle,
  234. uint32_t width,
  235. uint32_t height)
  236. {
  237. struct drm_device *dev = crtc->dev;
  238. struct qxl_device *qdev = dev->dev_private;
  239. struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
  240. struct drm_gem_object *obj;
  241. struct qxl_cursor *cursor;
  242. struct qxl_cursor_cmd *cmd;
  243. struct qxl_bo *cursor_bo, *user_bo;
  244. struct qxl_release *release;
  245. void *user_ptr;
  246. int size = 64*64*4;
  247. int ret = 0;
  248. if (!handle) {
  249. qxl_hide_cursor(qdev);
  250. return 0;
  251. }
  252. obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
  253. if (!obj) {
  254. DRM_ERROR("cannot find cursor object\n");
  255. return -ENOENT;
  256. }
  257. user_bo = gem_to_qxl_bo(obj);
  258. ret = qxl_bo_reserve(user_bo, false);
  259. if (ret)
  260. goto out_unref;
  261. ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
  262. if (ret)
  263. goto out_unreserve;
  264. ret = qxl_bo_kmap(user_bo, &user_ptr);
  265. if (ret)
  266. goto out_unpin;
  267. ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
  268. QXL_RELEASE_CURSOR_CMD,
  269. &release, NULL);
  270. if (ret)
  271. goto out_kunmap;
  272. ret = qxl_alloc_bo_reserved(qdev, sizeof(struct qxl_cursor) + size,
  273. &cursor_bo);
  274. if (ret)
  275. goto out_free_release;
  276. ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
  277. if (ret)
  278. goto out_free_bo;
  279. cursor->header.unique = 0;
  280. cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
  281. cursor->header.width = 64;
  282. cursor->header.height = 64;
  283. cursor->header.hot_spot_x = 0;
  284. cursor->header.hot_spot_y = 0;
  285. cursor->data_size = size;
  286. cursor->chunk.next_chunk = 0;
  287. cursor->chunk.prev_chunk = 0;
  288. cursor->chunk.data_size = size;
  289. memcpy(cursor->chunk.data, user_ptr, size);
  290. qxl_bo_kunmap(cursor_bo);
  291. /* finish with the userspace bo */
  292. qxl_bo_kunmap(user_bo);
  293. qxl_bo_unpin(user_bo);
  294. qxl_bo_unreserve(user_bo);
  295. drm_gem_object_unreference_unlocked(obj);
  296. cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
  297. cmd->type = QXL_CURSOR_SET;
  298. cmd->u.set.position.x = qcrtc->cur_x;
  299. cmd->u.set.position.y = qcrtc->cur_y;
  300. cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
  301. qxl_release_add_res(qdev, release, cursor_bo);
  302. cmd->u.set.visible = 1;
  303. qxl_release_unmap(qdev, release, &cmd->release_info);
  304. qxl_fence_releaseable(qdev, release);
  305. qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
  306. qxl_release_unreserve(qdev, release);
  307. qxl_bo_unreserve(cursor_bo);
  308. qxl_bo_unref(&cursor_bo);
  309. return ret;
  310. out_free_bo:
  311. qxl_bo_unref(&cursor_bo);
  312. out_free_release:
  313. qxl_release_unreserve(qdev, release);
  314. qxl_release_free(qdev, release);
  315. out_kunmap:
  316. qxl_bo_kunmap(user_bo);
  317. out_unpin:
  318. qxl_bo_unpin(user_bo);
  319. out_unreserve:
  320. qxl_bo_unreserve(user_bo);
  321. out_unref:
  322. drm_gem_object_unreference_unlocked(obj);
  323. return ret;
  324. }
  325. static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
  326. int x, int y)
  327. {
  328. struct drm_device *dev = crtc->dev;
  329. struct qxl_device *qdev = dev->dev_private;
  330. struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
  331. struct qxl_release *release;
  332. struct qxl_cursor_cmd *cmd;
  333. int ret;
  334. ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
  335. &release, NULL);
  336. qcrtc->cur_x = x;
  337. qcrtc->cur_y = y;
  338. cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
  339. cmd->type = QXL_CURSOR_MOVE;
  340. cmd->u.position.x = qcrtc->cur_x;
  341. cmd->u.position.y = qcrtc->cur_y;
  342. qxl_release_unmap(qdev, release, &cmd->release_info);
  343. qxl_fence_releaseable(qdev, release);
  344. qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
  345. qxl_release_unreserve(qdev, release);
  346. return 0;
  347. }
  348. static const struct drm_crtc_funcs qxl_crtc_funcs = {
  349. .cursor_set = qxl_crtc_cursor_set,
  350. .cursor_move = qxl_crtc_cursor_move,
  351. .gamma_set = qxl_crtc_gamma_set,
  352. .set_config = drm_crtc_helper_set_config,
  353. .destroy = qxl_crtc_destroy,
  354. };
  355. static void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
  356. {
  357. struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
  358. if (qxl_fb->obj)
  359. drm_gem_object_unreference_unlocked(qxl_fb->obj);
  360. drm_framebuffer_cleanup(fb);
  361. kfree(qxl_fb);
  362. }
  363. static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
  364. struct drm_file *file_priv,
  365. unsigned flags, unsigned color,
  366. struct drm_clip_rect *clips,
  367. unsigned num_clips)
  368. {
  369. /* TODO: vmwgfx where this was cribbed from had locking. Why? */
  370. struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
  371. struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
  372. struct drm_clip_rect norect;
  373. struct qxl_bo *qobj;
  374. int inc = 1;
  375. qobj = gem_to_qxl_bo(qxl_fb->obj);
  376. if (qxl_fb != qdev->active_user_framebuffer) {
  377. DRM_INFO("%s: qxl_fb 0x%p != qdev->active_user_framebuffer 0x%p\n",
  378. __func__, qxl_fb, qdev->active_user_framebuffer);
  379. }
  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. qdev->mode_set = true;
  533. return 0;
  534. }
  535. static void qxl_crtc_prepare(struct drm_crtc *crtc)
  536. {
  537. DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
  538. crtc->mode.hdisplay, crtc->mode.vdisplay,
  539. crtc->x, crtc->y, crtc->enabled);
  540. }
  541. static void qxl_crtc_commit(struct drm_crtc *crtc)
  542. {
  543. DRM_DEBUG("\n");
  544. }
  545. static void qxl_crtc_load_lut(struct drm_crtc *crtc)
  546. {
  547. DRM_DEBUG("\n");
  548. }
  549. static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
  550. .dpms = qxl_crtc_dpms,
  551. .mode_fixup = qxl_crtc_mode_fixup,
  552. .mode_set = qxl_crtc_mode_set,
  553. .prepare = qxl_crtc_prepare,
  554. .commit = qxl_crtc_commit,
  555. .load_lut = qxl_crtc_load_lut,
  556. };
  557. static int qdev_crtc_init(struct drm_device *dev, int num_crtc)
  558. {
  559. struct qxl_crtc *qxl_crtc;
  560. qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
  561. if (!qxl_crtc)
  562. return -ENOMEM;
  563. drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
  564. drm_mode_crtc_set_gamma_size(&qxl_crtc->base, 256);
  565. drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
  566. return 0;
  567. }
  568. static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
  569. {
  570. DRM_DEBUG("\n");
  571. }
  572. static bool qxl_enc_mode_fixup(struct drm_encoder *encoder,
  573. const struct drm_display_mode *mode,
  574. struct drm_display_mode *adjusted_mode)
  575. {
  576. DRM_DEBUG("\n");
  577. return true;
  578. }
  579. static void qxl_enc_prepare(struct drm_encoder *encoder)
  580. {
  581. DRM_DEBUG("\n");
  582. }
  583. static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
  584. struct drm_encoder *encoder)
  585. {
  586. int i;
  587. struct qxl_head *head;
  588. struct drm_display_mode *mode;
  589. BUG_ON(!encoder);
  590. /* TODO: ugly, do better */
  591. for (i = 0 ; (encoder->possible_crtcs != (1 << i)) && i < 32; ++i)
  592. ;
  593. if (encoder->possible_crtcs != (1 << i)) {
  594. DRM_ERROR("encoder has wrong possible_crtcs: %x\n",
  595. encoder->possible_crtcs);
  596. return;
  597. }
  598. if (!qdev->monitors_config ||
  599. qdev->monitors_config->max_allowed <= i) {
  600. DRM_ERROR(
  601. "head number too large or missing monitors config: %p, %d",
  602. qdev->monitors_config,
  603. qdev->monitors_config ?
  604. qdev->monitors_config->max_allowed : -1);
  605. return;
  606. }
  607. if (!encoder->crtc) {
  608. DRM_ERROR("missing crtc on encoder %p\n", encoder);
  609. return;
  610. }
  611. if (i != 0)
  612. DRM_DEBUG("missing for multiple monitors: no head holes\n");
  613. head = &qdev->monitors_config->heads[i];
  614. head->id = i;
  615. head->surface_id = 0;
  616. if (encoder->crtc->enabled) {
  617. mode = &encoder->crtc->mode;
  618. head->width = mode->hdisplay;
  619. head->height = mode->vdisplay;
  620. head->x = encoder->crtc->x;
  621. head->y = encoder->crtc->y;
  622. if (qdev->monitors_config->count < i + 1)
  623. qdev->monitors_config->count = i + 1;
  624. } else {
  625. head->width = 0;
  626. head->height = 0;
  627. head->x = 0;
  628. head->y = 0;
  629. }
  630. DRM_DEBUG("setting head %d to +%d+%d %dx%d\n",
  631. i, head->x, head->y, head->width, head->height);
  632. head->flags = 0;
  633. /* TODO - somewhere else to call this for multiple monitors
  634. * (config_commit?) */
  635. qxl_send_monitors_config(qdev);
  636. }
  637. static void qxl_enc_commit(struct drm_encoder *encoder)
  638. {
  639. struct qxl_device *qdev = encoder->dev->dev_private;
  640. qxl_write_monitors_config_for_encoder(qdev, encoder);
  641. DRM_DEBUG("\n");
  642. }
  643. static void qxl_enc_mode_set(struct drm_encoder *encoder,
  644. struct drm_display_mode *mode,
  645. struct drm_display_mode *adjusted_mode)
  646. {
  647. DRM_DEBUG("\n");
  648. }
  649. static int qxl_conn_get_modes(struct drm_connector *connector)
  650. {
  651. int ret = 0;
  652. struct qxl_device *qdev = connector->dev->dev_private;
  653. DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
  654. /* TODO: what should we do here? only show the configured modes for the
  655. * device, or allow the full list, or both? */
  656. if (qdev->monitors_config && qdev->monitors_config->count) {
  657. ret = qxl_add_monitors_config_modes(connector);
  658. if (ret < 0)
  659. return ret;
  660. }
  661. ret += qxl_add_common_modes(connector);
  662. return ret;
  663. }
  664. static int qxl_conn_mode_valid(struct drm_connector *connector,
  665. struct drm_display_mode *mode)
  666. {
  667. /* TODO: is this called for user defined modes? (xrandr --add-mode)
  668. * TODO: check that the mode fits in the framebuffer */
  669. DRM_DEBUG("%s: %dx%d status=%d\n", mode->name, mode->hdisplay,
  670. mode->vdisplay, mode->status);
  671. return MODE_OK;
  672. }
  673. static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
  674. {
  675. struct qxl_output *qxl_output =
  676. drm_connector_to_qxl_output(connector);
  677. DRM_DEBUG("\n");
  678. return &qxl_output->enc;
  679. }
  680. static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
  681. .dpms = qxl_enc_dpms,
  682. .mode_fixup = qxl_enc_mode_fixup,
  683. .prepare = qxl_enc_prepare,
  684. .mode_set = qxl_enc_mode_set,
  685. .commit = qxl_enc_commit,
  686. };
  687. static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
  688. .get_modes = qxl_conn_get_modes,
  689. .mode_valid = qxl_conn_mode_valid,
  690. .best_encoder = qxl_best_encoder,
  691. };
  692. static void qxl_conn_save(struct drm_connector *connector)
  693. {
  694. DRM_DEBUG("\n");
  695. }
  696. static void qxl_conn_restore(struct drm_connector *connector)
  697. {
  698. DRM_DEBUG("\n");
  699. }
  700. static enum drm_connector_status qxl_conn_detect(
  701. struct drm_connector *connector,
  702. bool force)
  703. {
  704. struct qxl_output *output =
  705. drm_connector_to_qxl_output(connector);
  706. struct drm_device *ddev = connector->dev;
  707. struct qxl_device *qdev = ddev->dev_private;
  708. int connected;
  709. /* The first monitor is always connected */
  710. connected = (output->index == 0) ||
  711. (qdev->monitors_config &&
  712. qdev->monitors_config->count > output->index);
  713. DRM_DEBUG("\n");
  714. return connected ? connector_status_connected
  715. : connector_status_disconnected;
  716. }
  717. static int qxl_conn_set_property(struct drm_connector *connector,
  718. struct drm_property *property,
  719. uint64_t value)
  720. {
  721. DRM_DEBUG("\n");
  722. return 0;
  723. }
  724. static void qxl_conn_destroy(struct drm_connector *connector)
  725. {
  726. struct qxl_output *qxl_output =
  727. drm_connector_to_qxl_output(connector);
  728. drm_sysfs_connector_remove(connector);
  729. drm_connector_cleanup(connector);
  730. kfree(qxl_output);
  731. }
  732. static const struct drm_connector_funcs qxl_connector_funcs = {
  733. .dpms = drm_helper_connector_dpms,
  734. .save = qxl_conn_save,
  735. .restore = qxl_conn_restore,
  736. .detect = qxl_conn_detect,
  737. .fill_modes = drm_helper_probe_single_connector_modes,
  738. .set_property = qxl_conn_set_property,
  739. .destroy = qxl_conn_destroy,
  740. };
  741. static void qxl_enc_destroy(struct drm_encoder *encoder)
  742. {
  743. drm_encoder_cleanup(encoder);
  744. }
  745. static const struct drm_encoder_funcs qxl_enc_funcs = {
  746. .destroy = qxl_enc_destroy,
  747. };
  748. static int qdev_output_init(struct drm_device *dev, int num_output)
  749. {
  750. struct qxl_output *qxl_output;
  751. struct drm_connector *connector;
  752. struct drm_encoder *encoder;
  753. qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
  754. if (!qxl_output)
  755. return -ENOMEM;
  756. qxl_output->index = num_output;
  757. connector = &qxl_output->base;
  758. encoder = &qxl_output->enc;
  759. drm_connector_init(dev, &qxl_output->base,
  760. &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
  761. drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
  762. DRM_MODE_ENCODER_VIRTUAL);
  763. encoder->possible_crtcs = 1 << num_output;
  764. drm_mode_connector_attach_encoder(&qxl_output->base,
  765. &qxl_output->enc);
  766. drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
  767. drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
  768. drm_sysfs_connector_add(connector);
  769. return 0;
  770. }
  771. static struct drm_framebuffer *
  772. qxl_user_framebuffer_create(struct drm_device *dev,
  773. struct drm_file *file_priv,
  774. struct drm_mode_fb_cmd2 *mode_cmd)
  775. {
  776. struct drm_gem_object *obj;
  777. struct qxl_framebuffer *qxl_fb;
  778. struct qxl_device *qdev = dev->dev_private;
  779. int ret;
  780. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  781. qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
  782. if (qxl_fb == NULL)
  783. return NULL;
  784. ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj);
  785. if (ret) {
  786. kfree(qxl_fb);
  787. drm_gem_object_unreference_unlocked(obj);
  788. return NULL;
  789. }
  790. if (qdev->active_user_framebuffer) {
  791. DRM_INFO("%s: active_user_framebuffer %p -> %p\n",
  792. __func__,
  793. qdev->active_user_framebuffer, qxl_fb);
  794. }
  795. qdev->active_user_framebuffer = qxl_fb;
  796. return &qxl_fb->base;
  797. }
  798. static const struct drm_mode_config_funcs qxl_mode_funcs = {
  799. .fb_create = qxl_user_framebuffer_create,
  800. };
  801. int qxl_modeset_init(struct qxl_device *qdev)
  802. {
  803. int i;
  804. int ret;
  805. struct drm_gem_object *gobj;
  806. int max_allowed = QXL_NUM_OUTPUTS;
  807. int monitors_config_size = sizeof(struct qxl_monitors_config) +
  808. max_allowed * sizeof(struct qxl_head);
  809. drm_mode_config_init(qdev->ddev);
  810. ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
  811. QXL_GEM_DOMAIN_VRAM,
  812. false, false, NULL, &gobj);
  813. if (ret) {
  814. DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
  815. return -ENOMEM;
  816. }
  817. qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
  818. qxl_bo_kmap(qdev->monitors_config_bo, NULL);
  819. qdev->monitors_config = qdev->monitors_config_bo->kptr;
  820. qdev->ram_header->monitors_config =
  821. qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
  822. memset(qdev->monitors_config, 0, monitors_config_size);
  823. qdev->monitors_config->max_allowed = max_allowed;
  824. qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
  825. /* modes will be validated against the framebuffer size */
  826. qdev->ddev->mode_config.min_width = 320;
  827. qdev->ddev->mode_config.min_height = 200;
  828. qdev->ddev->mode_config.max_width = 8192;
  829. qdev->ddev->mode_config.max_height = 8192;
  830. qdev->ddev->mode_config.fb_base = qdev->vram_base;
  831. for (i = 0 ; i < QXL_NUM_OUTPUTS; ++i) {
  832. qdev_crtc_init(qdev->ddev, i);
  833. qdev_output_init(qdev->ddev, i);
  834. }
  835. qdev->mode_info.mode_config_initialized = true;
  836. /* primary surface must be created by this point, to allow
  837. * issuing command queue commands and having them read by
  838. * spice server. */
  839. qxl_fbdev_init(qdev);
  840. return 0;
  841. }
  842. void qxl_modeset_fini(struct qxl_device *qdev)
  843. {
  844. qxl_fbdev_fini(qdev);
  845. if (qdev->mode_info.mode_config_initialized) {
  846. drm_mode_config_cleanup(qdev->ddev);
  847. qdev->mode_info.mode_config_initialized = false;
  848. }
  849. }