qxl_display.c 26 KB

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