qxl_display.c 27 KB

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