qxl_display.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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 (qxl_fb != qdev->active_user_framebuffer) {
  378. DRM_INFO("%s: qxl_fb 0x%p != qdev->active_user_framebuffer 0x%p\n",
  379. __func__, qxl_fb, qdev->active_user_framebuffer);
  380. }
  381. if (!num_clips) {
  382. num_clips = 1;
  383. clips = &norect;
  384. norect.x1 = norect.y1 = 0;
  385. norect.x2 = fb->width;
  386. norect.y2 = fb->height;
  387. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  388. num_clips /= 2;
  389. inc = 2; /* skip source rects */
  390. }
  391. qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
  392. clips, num_clips, inc);
  393. return 0;
  394. }
  395. static const struct drm_framebuffer_funcs qxl_fb_funcs = {
  396. .destroy = qxl_user_framebuffer_destroy,
  397. .dirty = qxl_framebuffer_surface_dirty,
  398. /* TODO?
  399. * .create_handle = qxl_user_framebuffer_create_handle, */
  400. };
  401. int
  402. qxl_framebuffer_init(struct drm_device *dev,
  403. struct qxl_framebuffer *qfb,
  404. struct drm_mode_fb_cmd2 *mode_cmd,
  405. struct drm_gem_object *obj)
  406. {
  407. int ret;
  408. qfb->obj = obj;
  409. ret = drm_framebuffer_init(dev, &qfb->base, &qxl_fb_funcs);
  410. if (ret) {
  411. qfb->obj = NULL;
  412. return ret;
  413. }
  414. drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
  415. return 0;
  416. }
  417. static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
  418. {
  419. }
  420. static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
  421. const struct drm_display_mode *mode,
  422. struct drm_display_mode *adjusted_mode)
  423. {
  424. struct drm_device *dev = crtc->dev;
  425. struct qxl_device *qdev = dev->dev_private;
  426. qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
  427. __func__,
  428. mode->hdisplay, mode->vdisplay,
  429. adjusted_mode->hdisplay,
  430. adjusted_mode->vdisplay);
  431. return true;
  432. }
  433. void
  434. qxl_send_monitors_config(struct qxl_device *qdev)
  435. {
  436. int i;
  437. BUG_ON(!qdev->ram_header->monitors_config);
  438. if (qdev->monitors_config->count == 0) {
  439. qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
  440. return;
  441. }
  442. for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
  443. struct qxl_head *head = &qdev->monitors_config->heads[i];
  444. if (head->y > 8192 || head->y < head->x ||
  445. head->width > 8192 || head->height > 8192) {
  446. DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
  447. i, head->width, head->height,
  448. head->x, head->y);
  449. return;
  450. }
  451. }
  452. qxl_io_monitors_config(qdev);
  453. }
  454. static void qxl_monitors_config_set_single(struct qxl_device *qdev,
  455. unsigned x, unsigned y,
  456. unsigned width, unsigned height)
  457. {
  458. DRM_DEBUG("%dx%d+%d+%d\n", width, height, x, y);
  459. qdev->monitors_config->count = 1;
  460. qdev->monitors_config->heads[0].x = x;
  461. qdev->monitors_config->heads[0].y = y;
  462. qdev->monitors_config->heads[0].width = width;
  463. qdev->monitors_config->heads[0].height = height;
  464. }
  465. static int qxl_crtc_mode_set(struct drm_crtc *crtc,
  466. struct drm_display_mode *mode,
  467. struct drm_display_mode *adjusted_mode,
  468. int x, int y,
  469. struct drm_framebuffer *old_fb)
  470. {
  471. struct drm_device *dev = crtc->dev;
  472. struct qxl_device *qdev = dev->dev_private;
  473. struct qxl_mode *m = (void *)mode->private;
  474. struct qxl_framebuffer *qfb;
  475. struct qxl_bo *bo, *old_bo = NULL;
  476. uint32_t width, height, base_offset;
  477. bool recreate_primary = false;
  478. int ret;
  479. if (!crtc->fb) {
  480. DRM_DEBUG_KMS("No FB bound\n");
  481. return 0;
  482. }
  483. if (old_fb) {
  484. qfb = to_qxl_framebuffer(old_fb);
  485. old_bo = gem_to_qxl_bo(qfb->obj);
  486. }
  487. qfb = to_qxl_framebuffer(crtc->fb);
  488. bo = gem_to_qxl_bo(qfb->obj);
  489. if (!m)
  490. /* and do we care? */
  491. DRM_DEBUG("%dx%d: not a native mode\n", x, y);
  492. else
  493. DRM_DEBUG("%dx%d: qxl id %d\n",
  494. mode->hdisplay, mode->vdisplay, m->id);
  495. DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
  496. x, y,
  497. mode->hdisplay, mode->vdisplay,
  498. adjusted_mode->hdisplay,
  499. adjusted_mode->vdisplay);
  500. recreate_primary = true;
  501. width = mode->hdisplay;
  502. height = mode->vdisplay;
  503. base_offset = 0;
  504. ret = qxl_bo_reserve(bo, false);
  505. if (ret != 0)
  506. return ret;
  507. ret = qxl_bo_pin(bo, bo->type, NULL);
  508. if (ret != 0) {
  509. qxl_bo_unreserve(bo);
  510. return -EINVAL;
  511. }
  512. qxl_bo_unreserve(bo);
  513. if (recreate_primary) {
  514. qxl_io_destroy_primary(qdev);
  515. qxl_io_log(qdev,
  516. "recreate primary: %dx%d (was %dx%d,%d,%d)\n",
  517. width, height, bo->surf.width,
  518. bo->surf.height, bo->surf.stride, bo->surf.format);
  519. qxl_io_create_primary(qdev, width, height, base_offset, bo);
  520. bo->is_primary = true;
  521. }
  522. if (old_bo && old_bo != bo) {
  523. old_bo->is_primary = false;
  524. ret = qxl_bo_reserve(old_bo, false);
  525. qxl_bo_unpin(old_bo);
  526. qxl_bo_unreserve(old_bo);
  527. }
  528. if (qdev->monitors_config->count == 0) {
  529. qxl_monitors_config_set_single(qdev, x, y,
  530. mode->hdisplay,
  531. mode->vdisplay);
  532. }
  533. qdev->mode_set = true;
  534. return 0;
  535. }
  536. static void qxl_crtc_prepare(struct drm_crtc *crtc)
  537. {
  538. DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
  539. crtc->mode.hdisplay, crtc->mode.vdisplay,
  540. crtc->x, crtc->y, crtc->enabled);
  541. }
  542. static void qxl_crtc_commit(struct drm_crtc *crtc)
  543. {
  544. DRM_DEBUG("\n");
  545. }
  546. static void qxl_crtc_load_lut(struct drm_crtc *crtc)
  547. {
  548. DRM_DEBUG("\n");
  549. }
  550. static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
  551. .dpms = qxl_crtc_dpms,
  552. .mode_fixup = qxl_crtc_mode_fixup,
  553. .mode_set = qxl_crtc_mode_set,
  554. .prepare = qxl_crtc_prepare,
  555. .commit = qxl_crtc_commit,
  556. .load_lut = qxl_crtc_load_lut,
  557. };
  558. static int qdev_crtc_init(struct drm_device *dev, int num_crtc)
  559. {
  560. struct qxl_crtc *qxl_crtc;
  561. qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
  562. if (!qxl_crtc)
  563. return -ENOMEM;
  564. drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
  565. drm_mode_crtc_set_gamma_size(&qxl_crtc->base, 256);
  566. drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
  567. return 0;
  568. }
  569. static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
  570. {
  571. DRM_DEBUG("\n");
  572. }
  573. static bool qxl_enc_mode_fixup(struct drm_encoder *encoder,
  574. const struct drm_display_mode *mode,
  575. struct drm_display_mode *adjusted_mode)
  576. {
  577. DRM_DEBUG("\n");
  578. return true;
  579. }
  580. static void qxl_enc_prepare(struct drm_encoder *encoder)
  581. {
  582. DRM_DEBUG("\n");
  583. }
  584. static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
  585. struct drm_encoder *encoder)
  586. {
  587. int i;
  588. struct qxl_head *head;
  589. struct drm_display_mode *mode;
  590. BUG_ON(!encoder);
  591. /* TODO: ugly, do better */
  592. for (i = 0 ; (encoder->possible_crtcs != (1 << i)) && i < 32; ++i)
  593. ;
  594. if (encoder->possible_crtcs != (1 << i)) {
  595. DRM_ERROR("encoder has wrong possible_crtcs: %x\n",
  596. encoder->possible_crtcs);
  597. return;
  598. }
  599. if (!qdev->monitors_config ||
  600. qdev->monitors_config->max_allowed <= i) {
  601. DRM_ERROR(
  602. "head number too large or missing monitors config: %p, %d",
  603. qdev->monitors_config,
  604. qdev->monitors_config ?
  605. qdev->monitors_config->max_allowed : -1);
  606. return;
  607. }
  608. if (!encoder->crtc) {
  609. DRM_ERROR("missing crtc on encoder %p\n", encoder);
  610. return;
  611. }
  612. if (i != 0)
  613. DRM_DEBUG("missing for multiple monitors: no head holes\n");
  614. head = &qdev->monitors_config->heads[i];
  615. head->id = i;
  616. head->surface_id = 0;
  617. if (encoder->crtc->enabled) {
  618. mode = &encoder->crtc->mode;
  619. head->width = mode->hdisplay;
  620. head->height = mode->vdisplay;
  621. head->x = encoder->crtc->x;
  622. head->y = encoder->crtc->y;
  623. if (qdev->monitors_config->count < i + 1)
  624. qdev->monitors_config->count = i + 1;
  625. } else {
  626. head->width = 0;
  627. head->height = 0;
  628. head->x = 0;
  629. head->y = 0;
  630. }
  631. DRM_DEBUG("setting head %d to +%d+%d %dx%d\n",
  632. i, head->x, head->y, head->width, head->height);
  633. head->flags = 0;
  634. /* TODO - somewhere else to call this for multiple monitors
  635. * (config_commit?) */
  636. qxl_send_monitors_config(qdev);
  637. }
  638. static void qxl_enc_commit(struct drm_encoder *encoder)
  639. {
  640. struct qxl_device *qdev = encoder->dev->dev_private;
  641. qxl_write_monitors_config_for_encoder(qdev, encoder);
  642. DRM_DEBUG("\n");
  643. }
  644. static void qxl_enc_mode_set(struct drm_encoder *encoder,
  645. struct drm_display_mode *mode,
  646. struct drm_display_mode *adjusted_mode)
  647. {
  648. DRM_DEBUG("\n");
  649. }
  650. static int qxl_conn_get_modes(struct drm_connector *connector)
  651. {
  652. int ret = 0;
  653. struct qxl_device *qdev = connector->dev->dev_private;
  654. DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
  655. /* TODO: what should we do here? only show the configured modes for the
  656. * device, or allow the full list, or both? */
  657. if (qdev->monitors_config && qdev->monitors_config->count) {
  658. ret = qxl_add_monitors_config_modes(connector);
  659. if (ret < 0)
  660. return ret;
  661. }
  662. ret += qxl_add_common_modes(connector);
  663. return ret;
  664. }
  665. static int qxl_conn_mode_valid(struct drm_connector *connector,
  666. struct drm_display_mode *mode)
  667. {
  668. /* TODO: is this called for user defined modes? (xrandr --add-mode)
  669. * TODO: check that the mode fits in the framebuffer */
  670. DRM_DEBUG("%s: %dx%d status=%d\n", mode->name, mode->hdisplay,
  671. mode->vdisplay, mode->status);
  672. return MODE_OK;
  673. }
  674. static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
  675. {
  676. struct qxl_output *qxl_output =
  677. drm_connector_to_qxl_output(connector);
  678. DRM_DEBUG("\n");
  679. return &qxl_output->enc;
  680. }
  681. static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
  682. .dpms = qxl_enc_dpms,
  683. .mode_fixup = qxl_enc_mode_fixup,
  684. .prepare = qxl_enc_prepare,
  685. .mode_set = qxl_enc_mode_set,
  686. .commit = qxl_enc_commit,
  687. };
  688. static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
  689. .get_modes = qxl_conn_get_modes,
  690. .mode_valid = qxl_conn_mode_valid,
  691. .best_encoder = qxl_best_encoder,
  692. };
  693. static void qxl_conn_save(struct drm_connector *connector)
  694. {
  695. DRM_DEBUG("\n");
  696. }
  697. static void qxl_conn_restore(struct drm_connector *connector)
  698. {
  699. DRM_DEBUG("\n");
  700. }
  701. static enum drm_connector_status qxl_conn_detect(
  702. struct drm_connector *connector,
  703. bool force)
  704. {
  705. struct qxl_output *output =
  706. drm_connector_to_qxl_output(connector);
  707. struct drm_device *ddev = connector->dev;
  708. struct qxl_device *qdev = ddev->dev_private;
  709. int connected;
  710. /* The first monitor is always connected */
  711. connected = (output->index == 0) ||
  712. (qdev->monitors_config &&
  713. qdev->monitors_config->count > output->index);
  714. DRM_DEBUG("\n");
  715. return connected ? connector_status_connected
  716. : connector_status_disconnected;
  717. }
  718. static int qxl_conn_set_property(struct drm_connector *connector,
  719. struct drm_property *property,
  720. uint64_t value)
  721. {
  722. DRM_DEBUG("\n");
  723. return 0;
  724. }
  725. static void qxl_conn_destroy(struct drm_connector *connector)
  726. {
  727. struct qxl_output *qxl_output =
  728. drm_connector_to_qxl_output(connector);
  729. drm_sysfs_connector_remove(connector);
  730. drm_connector_cleanup(connector);
  731. kfree(qxl_output);
  732. }
  733. static const struct drm_connector_funcs qxl_connector_funcs = {
  734. .dpms = drm_helper_connector_dpms,
  735. .save = qxl_conn_save,
  736. .restore = qxl_conn_restore,
  737. .detect = qxl_conn_detect,
  738. .fill_modes = drm_helper_probe_single_connector_modes,
  739. .set_property = qxl_conn_set_property,
  740. .destroy = qxl_conn_destroy,
  741. };
  742. static void qxl_enc_destroy(struct drm_encoder *encoder)
  743. {
  744. drm_encoder_cleanup(encoder);
  745. }
  746. static const struct drm_encoder_funcs qxl_enc_funcs = {
  747. .destroy = qxl_enc_destroy,
  748. };
  749. static int qdev_output_init(struct drm_device *dev, int num_output)
  750. {
  751. struct qxl_output *qxl_output;
  752. struct drm_connector *connector;
  753. struct drm_encoder *encoder;
  754. qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
  755. if (!qxl_output)
  756. return -ENOMEM;
  757. qxl_output->index = num_output;
  758. connector = &qxl_output->base;
  759. encoder = &qxl_output->enc;
  760. drm_connector_init(dev, &qxl_output->base,
  761. &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
  762. drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
  763. DRM_MODE_ENCODER_VIRTUAL);
  764. encoder->possible_crtcs = 1 << num_output;
  765. drm_mode_connector_attach_encoder(&qxl_output->base,
  766. &qxl_output->enc);
  767. drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
  768. drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
  769. drm_sysfs_connector_add(connector);
  770. return 0;
  771. }
  772. static struct drm_framebuffer *
  773. qxl_user_framebuffer_create(struct drm_device *dev,
  774. struct drm_file *file_priv,
  775. struct drm_mode_fb_cmd2 *mode_cmd)
  776. {
  777. struct drm_gem_object *obj;
  778. struct qxl_framebuffer *qxl_fb;
  779. struct qxl_device *qdev = dev->dev_private;
  780. int ret;
  781. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  782. qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
  783. if (qxl_fb == NULL)
  784. return NULL;
  785. ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj);
  786. if (ret) {
  787. kfree(qxl_fb);
  788. drm_gem_object_unreference_unlocked(obj);
  789. return NULL;
  790. }
  791. if (qdev->active_user_framebuffer) {
  792. DRM_INFO("%s: active_user_framebuffer %p -> %p\n",
  793. __func__,
  794. qdev->active_user_framebuffer, qxl_fb);
  795. }
  796. qdev->active_user_framebuffer = qxl_fb;
  797. return &qxl_fb->base;
  798. }
  799. static const struct drm_mode_config_funcs qxl_mode_funcs = {
  800. .fb_create = qxl_user_framebuffer_create,
  801. };
  802. int qxl_modeset_init(struct qxl_device *qdev)
  803. {
  804. int i;
  805. int ret;
  806. struct drm_gem_object *gobj;
  807. int max_allowed = QXL_NUM_OUTPUTS;
  808. int monitors_config_size = sizeof(struct qxl_monitors_config) +
  809. max_allowed * sizeof(struct qxl_head);
  810. drm_mode_config_init(qdev->ddev);
  811. ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
  812. QXL_GEM_DOMAIN_VRAM,
  813. false, false, NULL, &gobj);
  814. if (ret) {
  815. DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
  816. return -ENOMEM;
  817. }
  818. qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
  819. qxl_bo_kmap(qdev->monitors_config_bo, NULL);
  820. qdev->monitors_config = qdev->monitors_config_bo->kptr;
  821. qdev->ram_header->monitors_config =
  822. qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
  823. memset(qdev->monitors_config, 0, monitors_config_size);
  824. qdev->monitors_config->max_allowed = max_allowed;
  825. qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
  826. /* modes will be validated against the framebuffer size */
  827. qdev->ddev->mode_config.min_width = 320;
  828. qdev->ddev->mode_config.min_height = 200;
  829. qdev->ddev->mode_config.max_width = 8192;
  830. qdev->ddev->mode_config.max_height = 8192;
  831. qdev->ddev->mode_config.fb_base = qdev->vram_base;
  832. for (i = 0 ; i < QXL_NUM_OUTPUTS; ++i) {
  833. qdev_crtc_init(qdev->ddev, i);
  834. qdev_output_init(qdev->ddev, i);
  835. }
  836. qdev->mode_info.mode_config_initialized = true;
  837. /* primary surface must be created by this point, to allow
  838. * issuing command queue commands and having them read by
  839. * spice server. */
  840. qxl_fbdev_init(qdev);
  841. return 0;
  842. }
  843. void qxl_modeset_fini(struct qxl_device *qdev)
  844. {
  845. qxl_fbdev_fini(qdev);
  846. if (qdev->mode_info.mode_config_initialized) {
  847. drm_mode_config_cleanup(qdev->ddev);
  848. qdev->mode_info.mode_config_initialized = false;
  849. }
  850. }