qxl_display.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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 <drm/drm_crtc_helper.h>
  27. #include <drm/drm_plane_helper.h>
  28. #include <drm/drm_atomic_helper.h>
  29. #include <drm/drm_atomic.h>
  30. #include <drm/drm_gem_framebuffer_helper.h>
  31. #include "qxl_drv.h"
  32. #include "qxl_object.h"
  33. static bool qxl_head_enabled(struct qxl_head *head)
  34. {
  35. return head->width && head->height;
  36. }
  37. static int qxl_alloc_client_monitors_config(struct qxl_device *qdev,
  38. unsigned int count)
  39. {
  40. if (qdev->client_monitors_config &&
  41. count > qdev->client_monitors_config->count) {
  42. kfree(qdev->client_monitors_config);
  43. qdev->client_monitors_config = NULL;
  44. }
  45. if (!qdev->client_monitors_config) {
  46. qdev->client_monitors_config = kzalloc(
  47. sizeof(struct qxl_monitors_config) +
  48. sizeof(struct qxl_head) * count, GFP_KERNEL);
  49. if (!qdev->client_monitors_config)
  50. return -ENOMEM;
  51. }
  52. qdev->client_monitors_config->count = count;
  53. return 0;
  54. }
  55. enum {
  56. MONITORS_CONFIG_MODIFIED,
  57. MONITORS_CONFIG_UNCHANGED,
  58. MONITORS_CONFIG_BAD_CRC,
  59. MONITORS_CONFIG_ERROR,
  60. };
  61. static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
  62. {
  63. int i;
  64. int num_monitors;
  65. uint32_t crc;
  66. int status = MONITORS_CONFIG_UNCHANGED;
  67. num_monitors = qdev->rom->client_monitors_config.count;
  68. crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
  69. sizeof(qdev->rom->client_monitors_config));
  70. if (crc != qdev->rom->client_monitors_config_crc)
  71. return MONITORS_CONFIG_BAD_CRC;
  72. if (!num_monitors) {
  73. DRM_DEBUG_KMS("no client monitors configured\n");
  74. return status;
  75. }
  76. if (num_monitors > qdev->monitors_config->max_allowed) {
  77. DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
  78. qdev->monitors_config->max_allowed, num_monitors);
  79. num_monitors = qdev->monitors_config->max_allowed;
  80. } else {
  81. num_monitors = qdev->rom->client_monitors_config.count;
  82. }
  83. if (qdev->client_monitors_config
  84. && (num_monitors != qdev->client_monitors_config->count)) {
  85. status = MONITORS_CONFIG_MODIFIED;
  86. }
  87. if (qxl_alloc_client_monitors_config(qdev, num_monitors)) {
  88. status = MONITORS_CONFIG_ERROR;
  89. return status;
  90. }
  91. /* we copy max from the client but it isn't used */
  92. qdev->client_monitors_config->max_allowed =
  93. qdev->monitors_config->max_allowed;
  94. for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
  95. struct qxl_urect *c_rect =
  96. &qdev->rom->client_monitors_config.heads[i];
  97. struct qxl_head *client_head =
  98. &qdev->client_monitors_config->heads[i];
  99. if (client_head->x != c_rect->left) {
  100. client_head->x = c_rect->left;
  101. status = MONITORS_CONFIG_MODIFIED;
  102. }
  103. if (client_head->y != c_rect->top) {
  104. client_head->y = c_rect->top;
  105. status = MONITORS_CONFIG_MODIFIED;
  106. }
  107. if (client_head->width != c_rect->right - c_rect->left) {
  108. client_head->width = c_rect->right - c_rect->left;
  109. status = MONITORS_CONFIG_MODIFIED;
  110. }
  111. if (client_head->height != c_rect->bottom - c_rect->top) {
  112. client_head->height = c_rect->bottom - c_rect->top;
  113. status = MONITORS_CONFIG_MODIFIED;
  114. }
  115. if (client_head->surface_id != 0) {
  116. client_head->surface_id = 0;
  117. status = MONITORS_CONFIG_MODIFIED;
  118. }
  119. if (client_head->id != i) {
  120. client_head->id = i;
  121. status = MONITORS_CONFIG_MODIFIED;
  122. }
  123. if (client_head->flags != 0) {
  124. client_head->flags = 0;
  125. status = MONITORS_CONFIG_MODIFIED;
  126. }
  127. DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
  128. client_head->x, client_head->y);
  129. }
  130. return status;
  131. }
  132. static void qxl_update_offset_props(struct qxl_device *qdev)
  133. {
  134. struct drm_device *dev = &qdev->ddev;
  135. struct drm_connector *connector;
  136. struct qxl_output *output;
  137. struct qxl_head *head;
  138. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  139. output = drm_connector_to_qxl_output(connector);
  140. head = &qdev->client_monitors_config->heads[output->index];
  141. drm_object_property_set_value(&connector->base,
  142. dev->mode_config.suggested_x_property, head->x);
  143. drm_object_property_set_value(&connector->base,
  144. dev->mode_config.suggested_y_property, head->y);
  145. }
  146. }
  147. void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
  148. {
  149. struct drm_device *dev = &qdev->ddev;
  150. int status, retries;
  151. for (retries = 0; retries < 10; retries++) {
  152. status = qxl_display_copy_rom_client_monitors_config(qdev);
  153. if (status != MONITORS_CONFIG_BAD_CRC)
  154. break;
  155. udelay(5);
  156. }
  157. if (status == MONITORS_CONFIG_ERROR) {
  158. DRM_DEBUG_KMS("ignoring client monitors config: error");
  159. return;
  160. }
  161. if (status == MONITORS_CONFIG_BAD_CRC) {
  162. DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
  163. return;
  164. }
  165. if (status == MONITORS_CONFIG_UNCHANGED) {
  166. DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
  167. return;
  168. }
  169. drm_modeset_lock_all(dev);
  170. qxl_update_offset_props(qdev);
  171. drm_modeset_unlock_all(dev);
  172. if (!drm_helper_hpd_irq_event(dev)) {
  173. /* notify that the monitor configuration changed, to
  174. adjust at the arbitrary resolution */
  175. drm_kms_helper_hotplug_event(dev);
  176. }
  177. }
  178. static int qxl_add_monitors_config_modes(struct drm_connector *connector,
  179. unsigned *pwidth,
  180. unsigned *pheight)
  181. {
  182. struct drm_device *dev = connector->dev;
  183. struct qxl_device *qdev = dev->dev_private;
  184. struct qxl_output *output = drm_connector_to_qxl_output(connector);
  185. int h = output->index;
  186. struct drm_display_mode *mode = NULL;
  187. struct qxl_head *head;
  188. if (!qdev->monitors_config)
  189. return 0;
  190. if (h >= qdev->monitors_config->max_allowed)
  191. return 0;
  192. if (!qdev->client_monitors_config)
  193. return 0;
  194. if (h >= qdev->client_monitors_config->count)
  195. return 0;
  196. head = &qdev->client_monitors_config->heads[h];
  197. DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
  198. mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
  199. false);
  200. mode->type |= DRM_MODE_TYPE_PREFERRED;
  201. mode->hdisplay = head->width;
  202. mode->vdisplay = head->height;
  203. drm_mode_set_name(mode);
  204. *pwidth = head->width;
  205. *pheight = head->height;
  206. drm_mode_probed_add(connector, mode);
  207. /* remember the last custom size for mode validation */
  208. qdev->monitors_config_width = mode->hdisplay;
  209. qdev->monitors_config_height = mode->vdisplay;
  210. return 1;
  211. }
  212. static struct mode_size {
  213. int w;
  214. int h;
  215. } common_modes[] = {
  216. { 640, 480},
  217. { 720, 480},
  218. { 800, 600},
  219. { 848, 480},
  220. {1024, 768},
  221. {1152, 768},
  222. {1280, 720},
  223. {1280, 800},
  224. {1280, 854},
  225. {1280, 960},
  226. {1280, 1024},
  227. {1440, 900},
  228. {1400, 1050},
  229. {1680, 1050},
  230. {1600, 1200},
  231. {1920, 1080},
  232. {1920, 1200}
  233. };
  234. static int qxl_add_common_modes(struct drm_connector *connector,
  235. unsigned pwidth,
  236. unsigned pheight)
  237. {
  238. struct drm_device *dev = connector->dev;
  239. struct drm_display_mode *mode = NULL;
  240. int i;
  241. for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
  242. mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
  243. 60, false, false, false);
  244. if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
  245. mode->type |= DRM_MODE_TYPE_PREFERRED;
  246. drm_mode_probed_add(connector, mode);
  247. }
  248. return i - 1;
  249. }
  250. static void qxl_send_monitors_config(struct qxl_device *qdev)
  251. {
  252. int i;
  253. BUG_ON(!qdev->ram_header->monitors_config);
  254. if (qdev->monitors_config->count == 0)
  255. return;
  256. for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
  257. struct qxl_head *head = &qdev->monitors_config->heads[i];
  258. if (head->y > 8192 || head->x > 8192 ||
  259. head->width > 8192 || head->height > 8192) {
  260. DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
  261. i, head->width, head->height,
  262. head->x, head->y);
  263. return;
  264. }
  265. }
  266. qxl_io_monitors_config(qdev);
  267. }
  268. static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc,
  269. const char *reason)
  270. {
  271. struct drm_device *dev = crtc->dev;
  272. struct qxl_device *qdev = dev->dev_private;
  273. struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
  274. struct qxl_head head;
  275. int oldcount, i = qcrtc->index;
  276. if (!qdev->primary_created) {
  277. DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason);
  278. return;
  279. }
  280. if (!qdev->monitors_config ||
  281. qdev->monitors_config->max_allowed <= i)
  282. return;
  283. head.id = i;
  284. head.flags = 0;
  285. oldcount = qdev->monitors_config->count;
  286. if (crtc->state->active) {
  287. struct drm_display_mode *mode = &crtc->mode;
  288. head.width = mode->hdisplay;
  289. head.height = mode->vdisplay;
  290. head.x = crtc->x;
  291. head.y = crtc->y;
  292. if (qdev->monitors_config->count < i + 1)
  293. qdev->monitors_config->count = i + 1;
  294. } else if (i > 0) {
  295. head.width = 0;
  296. head.height = 0;
  297. head.x = 0;
  298. head.y = 0;
  299. if (qdev->monitors_config->count == i + 1)
  300. qdev->monitors_config->count = i;
  301. } else {
  302. DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason);
  303. return;
  304. }
  305. if (head.width == qdev->monitors_config->heads[i].width &&
  306. head.height == qdev->monitors_config->heads[i].height &&
  307. head.x == qdev->monitors_config->heads[i].x &&
  308. head.y == qdev->monitors_config->heads[i].y &&
  309. oldcount == qdev->monitors_config->count)
  310. return;
  311. DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n",
  312. i, head.width, head.height, head.x, head.y,
  313. crtc->state->active ? "on" : "off", reason);
  314. if (oldcount != qdev->monitors_config->count)
  315. DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n",
  316. oldcount, qdev->monitors_config->count,
  317. qdev->monitors_config->max_allowed);
  318. qdev->monitors_config->heads[i] = head;
  319. qxl_send_monitors_config(qdev);
  320. }
  321. static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
  322. struct drm_crtc_state *old_crtc_state)
  323. {
  324. struct drm_device *dev = crtc->dev;
  325. struct drm_pending_vblank_event *event;
  326. unsigned long flags;
  327. if (crtc->state && crtc->state->event) {
  328. event = crtc->state->event;
  329. crtc->state->event = NULL;
  330. spin_lock_irqsave(&dev->event_lock, flags);
  331. drm_crtc_send_vblank_event(crtc, event);
  332. spin_unlock_irqrestore(&dev->event_lock, flags);
  333. }
  334. qxl_crtc_update_monitors_config(crtc, "flush");
  335. }
  336. static void qxl_crtc_destroy(struct drm_crtc *crtc)
  337. {
  338. struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
  339. qxl_bo_unref(&qxl_crtc->cursor_bo);
  340. drm_crtc_cleanup(crtc);
  341. kfree(qxl_crtc);
  342. }
  343. static const struct drm_crtc_funcs qxl_crtc_funcs = {
  344. .set_config = drm_atomic_helper_set_config,
  345. .destroy = qxl_crtc_destroy,
  346. .page_flip = drm_atomic_helper_page_flip,
  347. .reset = drm_atomic_helper_crtc_reset,
  348. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  349. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  350. };
  351. static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
  352. struct drm_file *file_priv,
  353. unsigned flags, unsigned color,
  354. struct drm_clip_rect *clips,
  355. unsigned num_clips)
  356. {
  357. /* TODO: vmwgfx where this was cribbed from had locking. Why? */
  358. struct qxl_device *qdev = fb->dev->dev_private;
  359. struct drm_clip_rect norect;
  360. struct qxl_bo *qobj;
  361. int inc = 1;
  362. drm_modeset_lock_all(fb->dev);
  363. qobj = gem_to_qxl_bo(fb->obj[0]);
  364. /* if we aren't primary surface ignore this */
  365. if (!qobj->is_primary) {
  366. drm_modeset_unlock_all(fb->dev);
  367. return 0;
  368. }
  369. if (!num_clips) {
  370. num_clips = 1;
  371. clips = &norect;
  372. norect.x1 = norect.y1 = 0;
  373. norect.x2 = fb->width;
  374. norect.y2 = fb->height;
  375. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  376. num_clips /= 2;
  377. inc = 2; /* skip source rects */
  378. }
  379. qxl_draw_dirty_fb(qdev, fb, qobj, flags, color,
  380. clips, num_clips, inc);
  381. drm_modeset_unlock_all(fb->dev);
  382. return 0;
  383. }
  384. static const struct drm_framebuffer_funcs qxl_fb_funcs = {
  385. .destroy = drm_gem_fb_destroy,
  386. .dirty = qxl_framebuffer_surface_dirty,
  387. .create_handle = drm_gem_fb_create_handle,
  388. };
  389. static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
  390. struct drm_crtc_state *old_state)
  391. {
  392. qxl_crtc_update_monitors_config(crtc, "enable");
  393. }
  394. static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
  395. struct drm_crtc_state *old_state)
  396. {
  397. qxl_crtc_update_monitors_config(crtc, "disable");
  398. }
  399. static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
  400. .atomic_flush = qxl_crtc_atomic_flush,
  401. .atomic_enable = qxl_crtc_atomic_enable,
  402. .atomic_disable = qxl_crtc_atomic_disable,
  403. };
  404. static int qxl_primary_atomic_check(struct drm_plane *plane,
  405. struct drm_plane_state *state)
  406. {
  407. struct qxl_device *qdev = plane->dev->dev_private;
  408. struct qxl_bo *bo;
  409. if (!state->crtc || !state->fb)
  410. return 0;
  411. bo = gem_to_qxl_bo(state->fb->obj[0]);
  412. if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
  413. DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
  414. return -EINVAL;
  415. }
  416. return 0;
  417. }
  418. static int qxl_primary_apply_cursor(struct drm_plane *plane)
  419. {
  420. struct drm_device *dev = plane->dev;
  421. struct qxl_device *qdev = dev->dev_private;
  422. struct drm_framebuffer *fb = plane->state->fb;
  423. struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
  424. struct qxl_cursor_cmd *cmd;
  425. struct qxl_release *release;
  426. int ret = 0;
  427. if (!qcrtc->cursor_bo)
  428. return 0;
  429. ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
  430. QXL_RELEASE_CURSOR_CMD,
  431. &release, NULL);
  432. if (ret)
  433. return ret;
  434. ret = qxl_release_list_add(release, qcrtc->cursor_bo);
  435. if (ret)
  436. goto out_free_release;
  437. ret = qxl_release_reserve_list(release, false);
  438. if (ret)
  439. goto out_free_release;
  440. cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
  441. cmd->type = QXL_CURSOR_SET;
  442. cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x;
  443. cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y;
  444. cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
  445. cmd->u.set.visible = 1;
  446. qxl_release_unmap(qdev, release, &cmd->release_info);
  447. qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
  448. qxl_release_fence_buffer_objects(release);
  449. return ret;
  450. out_free_release:
  451. qxl_release_free(qdev, release);
  452. return ret;
  453. }
  454. static void qxl_primary_atomic_update(struct drm_plane *plane,
  455. struct drm_plane_state *old_state)
  456. {
  457. struct qxl_device *qdev = plane->dev->dev_private;
  458. struct qxl_bo *bo = gem_to_qxl_bo(plane->state->fb->obj[0]);
  459. struct qxl_bo *bo_old;
  460. struct drm_clip_rect norect = {
  461. .x1 = 0,
  462. .y1 = 0,
  463. .x2 = plane->state->fb->width,
  464. .y2 = plane->state->fb->height
  465. };
  466. int ret;
  467. bool same_shadow = false;
  468. if (old_state->fb) {
  469. bo_old = gem_to_qxl_bo(old_state->fb->obj[0]);
  470. } else {
  471. bo_old = NULL;
  472. }
  473. if (bo == bo_old)
  474. return;
  475. if (bo_old && bo_old->shadow && bo->shadow &&
  476. bo_old->shadow == bo->shadow) {
  477. same_shadow = true;
  478. }
  479. if (bo_old && bo_old->is_primary) {
  480. if (!same_shadow)
  481. qxl_io_destroy_primary(qdev);
  482. bo_old->is_primary = false;
  483. ret = qxl_primary_apply_cursor(plane);
  484. if (ret)
  485. DRM_ERROR(
  486. "could not set cursor after creating primary");
  487. }
  488. if (!bo->is_primary) {
  489. if (!same_shadow)
  490. qxl_io_create_primary(qdev, 0, bo);
  491. bo->is_primary = true;
  492. }
  493. qxl_draw_dirty_fb(qdev, plane->state->fb, bo, 0, 0, &norect, 1, 1);
  494. }
  495. static void qxl_primary_atomic_disable(struct drm_plane *plane,
  496. struct drm_plane_state *old_state)
  497. {
  498. struct qxl_device *qdev = plane->dev->dev_private;
  499. if (old_state->fb) {
  500. struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]);
  501. if (bo->is_primary) {
  502. qxl_io_destroy_primary(qdev);
  503. bo->is_primary = false;
  504. }
  505. }
  506. }
  507. static void qxl_cursor_atomic_update(struct drm_plane *plane,
  508. struct drm_plane_state *old_state)
  509. {
  510. struct drm_device *dev = plane->dev;
  511. struct qxl_device *qdev = dev->dev_private;
  512. struct drm_framebuffer *fb = plane->state->fb;
  513. struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
  514. struct qxl_release *release;
  515. struct qxl_cursor_cmd *cmd;
  516. struct qxl_cursor *cursor;
  517. struct drm_gem_object *obj;
  518. struct qxl_bo *cursor_bo = NULL, *user_bo = NULL, *old_cursor_bo = NULL;
  519. int ret;
  520. void *user_ptr;
  521. int size = 64*64*4;
  522. ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
  523. QXL_RELEASE_CURSOR_CMD,
  524. &release, NULL);
  525. if (ret)
  526. return;
  527. if (fb != old_state->fb) {
  528. obj = fb->obj[0];
  529. user_bo = gem_to_qxl_bo(obj);
  530. /* pinning is done in the prepare/cleanup framevbuffer */
  531. ret = qxl_bo_kmap(user_bo, &user_ptr);
  532. if (ret)
  533. goto out_free_release;
  534. ret = qxl_alloc_bo_reserved(qdev, release,
  535. sizeof(struct qxl_cursor) + size,
  536. &cursor_bo);
  537. if (ret)
  538. goto out_kunmap;
  539. ret = qxl_release_reserve_list(release, true);
  540. if (ret)
  541. goto out_free_bo;
  542. ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
  543. if (ret)
  544. goto out_backoff;
  545. cursor->header.unique = 0;
  546. cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
  547. cursor->header.width = 64;
  548. cursor->header.height = 64;
  549. cursor->header.hot_spot_x = fb->hot_x;
  550. cursor->header.hot_spot_y = fb->hot_y;
  551. cursor->data_size = size;
  552. cursor->chunk.next_chunk = 0;
  553. cursor->chunk.prev_chunk = 0;
  554. cursor->chunk.data_size = size;
  555. memcpy(cursor->chunk.data, user_ptr, size);
  556. qxl_bo_kunmap(cursor_bo);
  557. qxl_bo_kunmap(user_bo);
  558. cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
  559. cmd->u.set.visible = 1;
  560. cmd->u.set.shape = qxl_bo_physical_address(qdev,
  561. cursor_bo, 0);
  562. cmd->type = QXL_CURSOR_SET;
  563. old_cursor_bo = qcrtc->cursor_bo;
  564. qcrtc->cursor_bo = cursor_bo;
  565. cursor_bo = NULL;
  566. } else {
  567. ret = qxl_release_reserve_list(release, true);
  568. if (ret)
  569. goto out_free_release;
  570. cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
  571. cmd->type = QXL_CURSOR_MOVE;
  572. }
  573. cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
  574. cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
  575. qxl_release_unmap(qdev, release, &cmd->release_info);
  576. qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
  577. qxl_release_fence_buffer_objects(release);
  578. if (old_cursor_bo)
  579. qxl_bo_unref(&old_cursor_bo);
  580. qxl_bo_unref(&cursor_bo);
  581. return;
  582. out_backoff:
  583. qxl_release_backoff_reserve_list(release);
  584. out_free_bo:
  585. qxl_bo_unref(&cursor_bo);
  586. out_kunmap:
  587. qxl_bo_kunmap(user_bo);
  588. out_free_release:
  589. qxl_release_free(qdev, release);
  590. return;
  591. }
  592. static void qxl_cursor_atomic_disable(struct drm_plane *plane,
  593. struct drm_plane_state *old_state)
  594. {
  595. struct qxl_device *qdev = plane->dev->dev_private;
  596. struct qxl_release *release;
  597. struct qxl_cursor_cmd *cmd;
  598. int ret;
  599. ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
  600. QXL_RELEASE_CURSOR_CMD,
  601. &release, NULL);
  602. if (ret)
  603. return;
  604. ret = qxl_release_reserve_list(release, true);
  605. if (ret) {
  606. qxl_release_free(qdev, release);
  607. return;
  608. }
  609. cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
  610. cmd->type = QXL_CURSOR_HIDE;
  611. qxl_release_unmap(qdev, release, &cmd->release_info);
  612. qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
  613. qxl_release_fence_buffer_objects(release);
  614. }
  615. static int qxl_plane_prepare_fb(struct drm_plane *plane,
  616. struct drm_plane_state *new_state)
  617. {
  618. struct qxl_device *qdev = plane->dev->dev_private;
  619. struct drm_gem_object *obj;
  620. struct qxl_bo *user_bo, *old_bo = NULL;
  621. int ret;
  622. if (!new_state->fb)
  623. return 0;
  624. obj = new_state->fb->obj[0];
  625. user_bo = gem_to_qxl_bo(obj);
  626. if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
  627. user_bo->is_dumb && !user_bo->shadow) {
  628. if (plane->state->fb) {
  629. obj = plane->state->fb->obj[0];
  630. old_bo = gem_to_qxl_bo(obj);
  631. }
  632. if (old_bo && old_bo->shadow &&
  633. user_bo->gem_base.size == old_bo->gem_base.size &&
  634. plane->state->crtc == new_state->crtc &&
  635. plane->state->crtc_w == new_state->crtc_w &&
  636. plane->state->crtc_h == new_state->crtc_h &&
  637. plane->state->src_x == new_state->src_x &&
  638. plane->state->src_y == new_state->src_y &&
  639. plane->state->src_w == new_state->src_w &&
  640. plane->state->src_h == new_state->src_h &&
  641. plane->state->rotation == new_state->rotation &&
  642. plane->state->zpos == new_state->zpos) {
  643. drm_gem_object_get(&old_bo->shadow->gem_base);
  644. user_bo->shadow = old_bo->shadow;
  645. } else {
  646. qxl_bo_create(qdev, user_bo->gem_base.size,
  647. true, true, QXL_GEM_DOMAIN_VRAM, NULL,
  648. &user_bo->shadow);
  649. }
  650. }
  651. ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
  652. if (ret)
  653. return ret;
  654. return 0;
  655. }
  656. static void qxl_plane_cleanup_fb(struct drm_plane *plane,
  657. struct drm_plane_state *old_state)
  658. {
  659. struct drm_gem_object *obj;
  660. struct qxl_bo *user_bo;
  661. if (!old_state->fb) {
  662. /*
  663. * we never executed prepare_fb, so there's nothing to
  664. * unpin.
  665. */
  666. return;
  667. }
  668. obj = old_state->fb->obj[0];
  669. user_bo = gem_to_qxl_bo(obj);
  670. qxl_bo_unpin(user_bo);
  671. if (user_bo->shadow && !user_bo->is_primary) {
  672. drm_gem_object_put_unlocked(&user_bo->shadow->gem_base);
  673. user_bo->shadow = NULL;
  674. }
  675. }
  676. static const uint32_t qxl_cursor_plane_formats[] = {
  677. DRM_FORMAT_ARGB8888,
  678. };
  679. static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
  680. .atomic_update = qxl_cursor_atomic_update,
  681. .atomic_disable = qxl_cursor_atomic_disable,
  682. .prepare_fb = qxl_plane_prepare_fb,
  683. .cleanup_fb = qxl_plane_cleanup_fb,
  684. };
  685. static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
  686. .update_plane = drm_atomic_helper_update_plane,
  687. .disable_plane = drm_atomic_helper_disable_plane,
  688. .destroy = drm_primary_helper_destroy,
  689. .reset = drm_atomic_helper_plane_reset,
  690. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  691. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  692. };
  693. static const uint32_t qxl_primary_plane_formats[] = {
  694. DRM_FORMAT_XRGB8888,
  695. DRM_FORMAT_ARGB8888,
  696. };
  697. static const struct drm_plane_helper_funcs primary_helper_funcs = {
  698. .atomic_check = qxl_primary_atomic_check,
  699. .atomic_update = qxl_primary_atomic_update,
  700. .atomic_disable = qxl_primary_atomic_disable,
  701. .prepare_fb = qxl_plane_prepare_fb,
  702. .cleanup_fb = qxl_plane_cleanup_fb,
  703. };
  704. static const struct drm_plane_funcs qxl_primary_plane_funcs = {
  705. .update_plane = drm_atomic_helper_update_plane,
  706. .disable_plane = drm_atomic_helper_disable_plane,
  707. .destroy = drm_primary_helper_destroy,
  708. .reset = drm_atomic_helper_plane_reset,
  709. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  710. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  711. };
  712. static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
  713. unsigned int possible_crtcs,
  714. enum drm_plane_type type)
  715. {
  716. const struct drm_plane_helper_funcs *helper_funcs = NULL;
  717. struct drm_plane *plane;
  718. const struct drm_plane_funcs *funcs;
  719. const uint32_t *formats;
  720. int num_formats;
  721. int err;
  722. if (type == DRM_PLANE_TYPE_PRIMARY) {
  723. funcs = &qxl_primary_plane_funcs;
  724. formats = qxl_primary_plane_formats;
  725. num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
  726. helper_funcs = &primary_helper_funcs;
  727. } else if (type == DRM_PLANE_TYPE_CURSOR) {
  728. funcs = &qxl_cursor_plane_funcs;
  729. formats = qxl_cursor_plane_formats;
  730. helper_funcs = &qxl_cursor_helper_funcs;
  731. num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
  732. } else {
  733. return ERR_PTR(-EINVAL);
  734. }
  735. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  736. if (!plane)
  737. return ERR_PTR(-ENOMEM);
  738. err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
  739. funcs, formats, num_formats,
  740. NULL, type, NULL);
  741. if (err)
  742. goto free_plane;
  743. drm_plane_helper_add(plane, helper_funcs);
  744. return plane;
  745. free_plane:
  746. kfree(plane);
  747. return ERR_PTR(-EINVAL);
  748. }
  749. static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
  750. {
  751. struct qxl_crtc *qxl_crtc;
  752. struct drm_plane *primary, *cursor;
  753. struct qxl_device *qdev = dev->dev_private;
  754. int r;
  755. qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
  756. if (!qxl_crtc)
  757. return -ENOMEM;
  758. primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
  759. if (IS_ERR(primary)) {
  760. r = -ENOMEM;
  761. goto free_mem;
  762. }
  763. cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
  764. if (IS_ERR(cursor)) {
  765. r = -ENOMEM;
  766. goto clean_primary;
  767. }
  768. r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
  769. &qxl_crtc_funcs, NULL);
  770. if (r)
  771. goto clean_cursor;
  772. qxl_crtc->index = crtc_id;
  773. drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
  774. return 0;
  775. clean_cursor:
  776. drm_plane_cleanup(cursor);
  777. kfree(cursor);
  778. clean_primary:
  779. drm_plane_cleanup(primary);
  780. kfree(primary);
  781. free_mem:
  782. kfree(qxl_crtc);
  783. return r;
  784. }
  785. static int qxl_conn_get_modes(struct drm_connector *connector)
  786. {
  787. unsigned pwidth = 1024;
  788. unsigned pheight = 768;
  789. int ret = 0;
  790. ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
  791. if (ret < 0)
  792. return ret;
  793. ret += qxl_add_common_modes(connector, pwidth, pheight);
  794. return ret;
  795. }
  796. static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
  797. struct drm_display_mode *mode)
  798. {
  799. struct drm_device *ddev = connector->dev;
  800. struct qxl_device *qdev = ddev->dev_private;
  801. int i;
  802. /* TODO: is this called for user defined modes? (xrandr --add-mode)
  803. * TODO: check that the mode fits in the framebuffer */
  804. if(qdev->monitors_config_width == mode->hdisplay &&
  805. qdev->monitors_config_height == mode->vdisplay)
  806. return MODE_OK;
  807. for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
  808. if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
  809. return MODE_OK;
  810. }
  811. return MODE_BAD;
  812. }
  813. static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
  814. {
  815. struct qxl_output *qxl_output =
  816. drm_connector_to_qxl_output(connector);
  817. DRM_DEBUG("\n");
  818. return &qxl_output->enc;
  819. }
  820. static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
  821. };
  822. static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
  823. .get_modes = qxl_conn_get_modes,
  824. .mode_valid = qxl_conn_mode_valid,
  825. .best_encoder = qxl_best_encoder,
  826. };
  827. static enum drm_connector_status qxl_conn_detect(
  828. struct drm_connector *connector,
  829. bool force)
  830. {
  831. struct qxl_output *output =
  832. drm_connector_to_qxl_output(connector);
  833. struct drm_device *ddev = connector->dev;
  834. struct qxl_device *qdev = ddev->dev_private;
  835. bool connected = false;
  836. /* The first monitor is always connected */
  837. if (!qdev->client_monitors_config) {
  838. if (output->index == 0)
  839. connected = true;
  840. } else
  841. connected = qdev->client_monitors_config->count > output->index &&
  842. qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
  843. DRM_DEBUG("#%d connected: %d\n", output->index, connected);
  844. return connected ? connector_status_connected
  845. : connector_status_disconnected;
  846. }
  847. static void qxl_conn_destroy(struct drm_connector *connector)
  848. {
  849. struct qxl_output *qxl_output =
  850. drm_connector_to_qxl_output(connector);
  851. drm_connector_unregister(connector);
  852. drm_connector_cleanup(connector);
  853. kfree(qxl_output);
  854. }
  855. static const struct drm_connector_funcs qxl_connector_funcs = {
  856. .dpms = drm_helper_connector_dpms,
  857. .detect = qxl_conn_detect,
  858. .fill_modes = drm_helper_probe_single_connector_modes,
  859. .destroy = qxl_conn_destroy,
  860. .reset = drm_atomic_helper_connector_reset,
  861. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  862. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  863. };
  864. static void qxl_enc_destroy(struct drm_encoder *encoder)
  865. {
  866. drm_encoder_cleanup(encoder);
  867. }
  868. static const struct drm_encoder_funcs qxl_enc_funcs = {
  869. .destroy = qxl_enc_destroy,
  870. };
  871. static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
  872. {
  873. if (qdev->hotplug_mode_update_property)
  874. return 0;
  875. qdev->hotplug_mode_update_property =
  876. drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
  877. "hotplug_mode_update", 0, 1);
  878. return 0;
  879. }
  880. static int qdev_output_init(struct drm_device *dev, int num_output)
  881. {
  882. struct qxl_device *qdev = dev->dev_private;
  883. struct qxl_output *qxl_output;
  884. struct drm_connector *connector;
  885. struct drm_encoder *encoder;
  886. qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
  887. if (!qxl_output)
  888. return -ENOMEM;
  889. qxl_output->index = num_output;
  890. connector = &qxl_output->base;
  891. encoder = &qxl_output->enc;
  892. drm_connector_init(dev, &qxl_output->base,
  893. &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
  894. drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
  895. DRM_MODE_ENCODER_VIRTUAL, NULL);
  896. /* we get HPD via client monitors config */
  897. connector->polled = DRM_CONNECTOR_POLL_HPD;
  898. encoder->possible_crtcs = 1 << num_output;
  899. drm_connector_attach_encoder(&qxl_output->base,
  900. &qxl_output->enc);
  901. drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
  902. drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
  903. drm_object_attach_property(&connector->base,
  904. qdev->hotplug_mode_update_property, 0);
  905. drm_object_attach_property(&connector->base,
  906. dev->mode_config.suggested_x_property, 0);
  907. drm_object_attach_property(&connector->base,
  908. dev->mode_config.suggested_y_property, 0);
  909. return 0;
  910. }
  911. static struct drm_framebuffer *
  912. qxl_user_framebuffer_create(struct drm_device *dev,
  913. struct drm_file *file_priv,
  914. const struct drm_mode_fb_cmd2 *mode_cmd)
  915. {
  916. return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd,
  917. &qxl_fb_funcs);
  918. }
  919. static const struct drm_mode_config_funcs qxl_mode_funcs = {
  920. .fb_create = qxl_user_framebuffer_create,
  921. .atomic_check = drm_atomic_helper_check,
  922. .atomic_commit = drm_atomic_helper_commit,
  923. };
  924. int qxl_create_monitors_object(struct qxl_device *qdev)
  925. {
  926. int ret;
  927. struct drm_gem_object *gobj;
  928. int max_allowed = qxl_num_crtc;
  929. int monitors_config_size = sizeof(struct qxl_monitors_config) +
  930. max_allowed * sizeof(struct qxl_head);
  931. ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
  932. QXL_GEM_DOMAIN_VRAM,
  933. false, false, NULL, &gobj);
  934. if (ret) {
  935. DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
  936. return -ENOMEM;
  937. }
  938. qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
  939. ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
  940. if (ret)
  941. return ret;
  942. qxl_bo_kmap(qdev->monitors_config_bo, NULL);
  943. qdev->monitors_config = qdev->monitors_config_bo->kptr;
  944. qdev->ram_header->monitors_config =
  945. qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
  946. memset(qdev->monitors_config, 0, monitors_config_size);
  947. qdev->monitors_config->max_allowed = max_allowed;
  948. return 0;
  949. }
  950. int qxl_destroy_monitors_object(struct qxl_device *qdev)
  951. {
  952. int ret;
  953. qdev->monitors_config = NULL;
  954. qdev->ram_header->monitors_config = 0;
  955. qxl_bo_kunmap(qdev->monitors_config_bo);
  956. ret = qxl_bo_unpin(qdev->monitors_config_bo);
  957. if (ret)
  958. return ret;
  959. qxl_bo_unref(&qdev->monitors_config_bo);
  960. return 0;
  961. }
  962. int qxl_modeset_init(struct qxl_device *qdev)
  963. {
  964. int i;
  965. int ret;
  966. drm_mode_config_init(&qdev->ddev);
  967. ret = qxl_create_monitors_object(qdev);
  968. if (ret)
  969. return ret;
  970. qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
  971. /* modes will be validated against the framebuffer size */
  972. qdev->ddev.mode_config.min_width = 0;
  973. qdev->ddev.mode_config.min_height = 0;
  974. qdev->ddev.mode_config.max_width = 8192;
  975. qdev->ddev.mode_config.max_height = 8192;
  976. qdev->ddev.mode_config.fb_base = qdev->vram_base;
  977. drm_mode_create_suggested_offset_properties(&qdev->ddev);
  978. qxl_mode_create_hotplug_mode_update_property(qdev);
  979. for (i = 0 ; i < qxl_num_crtc; ++i) {
  980. qdev_crtc_init(&qdev->ddev, i);
  981. qdev_output_init(&qdev->ddev, i);
  982. }
  983. qxl_display_read_client_monitors_config(qdev);
  984. drm_mode_config_reset(&qdev->ddev);
  985. /* primary surface must be created by this point, to allow
  986. * issuing command queue commands and having them read by
  987. * spice server. */
  988. qxl_fbdev_init(qdev);
  989. return 0;
  990. }
  991. void qxl_modeset_fini(struct qxl_device *qdev)
  992. {
  993. qxl_fbdev_fini(qdev);
  994. qxl_destroy_monitors_object(qdev);
  995. drm_mode_config_cleanup(&qdev->ddev);
  996. }