qxl_display.c 35 KB

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