vmwgfx_kms.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2009-2025 Broadcom. All Rights Reserved. The term
  5. * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
  6. *
  7. **************************************************************************/
  8. #include "vmwgfx_kms.h"
  9. #include "vmwgfx_bo.h"
  10. #include "vmwgfx_resource_priv.h"
  11. #include "vmwgfx_vkms.h"
  12. #include "vmw_surface_cache.h"
  13. #include <drm/drm_atomic.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_damage_helper.h>
  16. #include <drm/drm_fourcc.h>
  17. #include <drm/drm_rect.h>
  18. #include <drm/drm_sysfs.h>
  19. #include <drm/drm_edid.h>
  20. void vmw_du_init(struct vmw_display_unit *du)
  21. {
  22. vmw_vkms_crtc_init(&du->crtc);
  23. }
  24. void vmw_du_cleanup(struct vmw_display_unit *du)
  25. {
  26. struct vmw_private *dev_priv = vmw_priv(du->primary.dev);
  27. vmw_vkms_crtc_cleanup(&du->crtc);
  28. drm_plane_cleanup(&du->primary);
  29. if (vmw_cmd_supported(dev_priv))
  30. drm_plane_cleanup(&du->cursor.base);
  31. drm_connector_unregister(&du->connector);
  32. drm_crtc_cleanup(&du->crtc);
  33. drm_encoder_cleanup(&du->encoder);
  34. drm_connector_cleanup(&du->connector);
  35. }
  36. void vmw_du_primary_plane_destroy(struct drm_plane *plane)
  37. {
  38. drm_plane_cleanup(plane);
  39. /* Planes are static in our case so we don't free it */
  40. }
  41. /**
  42. * vmw_du_plane_unpin_surf - unpins resource associated with a framebuffer surface
  43. *
  44. * @vps: plane state associated with the display surface
  45. */
  46. void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps)
  47. {
  48. struct vmw_surface *surf = vmw_user_object_surface(&vps->uo);
  49. if (surf) {
  50. if (vps->pinned) {
  51. vmw_resource_unpin(&surf->res);
  52. vps->pinned--;
  53. }
  54. }
  55. }
  56. /**
  57. * vmw_du_plane_cleanup_fb - Unpins the plane surface
  58. *
  59. * @plane: display plane
  60. * @old_state: Contains the FB to clean up
  61. *
  62. * Unpins the framebuffer surface
  63. *
  64. * Returns 0 on success
  65. */
  66. void
  67. vmw_du_plane_cleanup_fb(struct drm_plane *plane,
  68. struct drm_plane_state *old_state)
  69. {
  70. struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
  71. vmw_du_plane_unpin_surf(vps);
  72. }
  73. /**
  74. * vmw_du_primary_plane_atomic_check - check if the new state is okay
  75. *
  76. * @plane: display plane
  77. * @state: info on the new plane state, including the FB
  78. *
  79. * Check if the new state is settable given the current state. Other
  80. * than what the atomic helper checks, we care about crtc fitting
  81. * the FB and maintaining one active framebuffer.
  82. *
  83. * Returns 0 on success
  84. */
  85. int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
  86. struct drm_atomic_state *state)
  87. {
  88. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  89. plane);
  90. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  91. plane);
  92. struct drm_crtc_state *crtc_state = NULL;
  93. struct drm_framebuffer *new_fb = new_state->fb;
  94. struct drm_framebuffer *old_fb = old_state->fb;
  95. int ret;
  96. /*
  97. * Ignore damage clips if the framebuffer attached to the plane's state
  98. * has changed since the last plane update (page-flip). In this case, a
  99. * full plane update should happen because uploads are done per-buffer.
  100. */
  101. if (old_fb != new_fb)
  102. new_state->ignore_damage_clips = true;
  103. if (new_state->crtc)
  104. crtc_state = drm_atomic_get_new_crtc_state(state,
  105. new_state->crtc);
  106. ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
  107. DRM_PLANE_NO_SCALING,
  108. DRM_PLANE_NO_SCALING,
  109. false, true);
  110. return ret;
  111. }
  112. int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
  113. struct drm_atomic_state *state)
  114. {
  115. struct vmw_private *vmw = vmw_priv(crtc->dev);
  116. struct drm_crtc_state *new_state = drm_atomic_get_new_crtc_state(state,
  117. crtc);
  118. struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
  119. int connector_mask = drm_connector_mask(&du->connector);
  120. bool has_primary = new_state->plane_mask &
  121. drm_plane_mask(crtc->primary);
  122. /*
  123. * This is fine in general, but broken userspace might expect
  124. * some actual rendering so give a clue as why it's blank.
  125. */
  126. if (new_state->enable && !has_primary)
  127. drm_dbg_driver(&vmw->drm,
  128. "CRTC without a primary plane will be blank.\n");
  129. if (new_state->connector_mask != connector_mask &&
  130. new_state->connector_mask != 0) {
  131. DRM_ERROR("Invalid connectors configuration\n");
  132. return -EINVAL;
  133. }
  134. /*
  135. * Our virtual device does not have a dot clock, so use the logical
  136. * clock value as the dot clock.
  137. */
  138. if (new_state->mode.crtc_clock == 0)
  139. new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
  140. return 0;
  141. }
  142. void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
  143. struct drm_atomic_state *state)
  144. {
  145. vmw_vkms_crtc_atomic_begin(crtc, state);
  146. }
  147. /**
  148. * vmw_du_crtc_duplicate_state - duplicate crtc state
  149. * @crtc: DRM crtc
  150. *
  151. * Allocates and returns a copy of the crtc state (both common and
  152. * vmw-specific) for the specified crtc.
  153. *
  154. * Returns: The newly allocated crtc state, or NULL on failure.
  155. */
  156. struct drm_crtc_state *
  157. vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
  158. {
  159. struct drm_crtc_state *state;
  160. struct vmw_crtc_state *vcs;
  161. if (WARN_ON(!crtc->state))
  162. return NULL;
  163. vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
  164. if (!vcs)
  165. return NULL;
  166. state = &vcs->base;
  167. __drm_atomic_helper_crtc_duplicate_state(crtc, state);
  168. return state;
  169. }
  170. /**
  171. * vmw_du_crtc_reset - creates a blank vmw crtc state
  172. * @crtc: DRM crtc
  173. *
  174. * Resets the atomic state for @crtc by freeing the state pointer (which
  175. * might be NULL, e.g. at driver load time) and allocating a new empty state
  176. * object.
  177. */
  178. void vmw_du_crtc_reset(struct drm_crtc *crtc)
  179. {
  180. struct vmw_crtc_state *vcs;
  181. if (crtc->state) {
  182. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  183. kfree(vmw_crtc_state_to_vcs(crtc->state));
  184. }
  185. vcs = kzalloc_obj(*vcs);
  186. if (!vcs) {
  187. DRM_ERROR("Cannot allocate vmw_crtc_state\n");
  188. return;
  189. }
  190. __drm_atomic_helper_crtc_reset(crtc, &vcs->base);
  191. }
  192. /**
  193. * vmw_du_crtc_destroy_state - destroy crtc state
  194. * @crtc: DRM crtc
  195. * @state: state object to destroy
  196. *
  197. * Destroys the crtc state (both common and vmw-specific) for the
  198. * specified plane.
  199. */
  200. void
  201. vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
  202. struct drm_crtc_state *state)
  203. {
  204. drm_atomic_helper_crtc_destroy_state(crtc, state);
  205. }
  206. /**
  207. * vmw_du_plane_duplicate_state - duplicate plane state
  208. * @plane: drm plane
  209. *
  210. * Allocates and returns a copy of the plane state (both common and
  211. * vmw-specific) for the specified plane.
  212. *
  213. * Returns: The newly allocated plane state, or NULL on failure.
  214. */
  215. struct drm_plane_state *
  216. vmw_du_plane_duplicate_state(struct drm_plane *plane)
  217. {
  218. struct drm_plane_state *state;
  219. struct vmw_plane_state *vps;
  220. vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
  221. if (!vps)
  222. return NULL;
  223. vps->pinned = 0;
  224. vps->cpp = 0;
  225. vps->cursor.mob = NULL;
  226. /* Each ref counted resource needs to be acquired again */
  227. vmw_user_object_ref(&vps->uo);
  228. state = &vps->base;
  229. __drm_atomic_helper_plane_duplicate_state(plane, state);
  230. return state;
  231. }
  232. /**
  233. * vmw_du_plane_reset - creates a blank vmw plane state
  234. * @plane: drm plane
  235. *
  236. * Resets the atomic state for @plane by freeing the state pointer (which might
  237. * be NULL, e.g. at driver load time) and allocating a new empty state object.
  238. */
  239. void vmw_du_plane_reset(struct drm_plane *plane)
  240. {
  241. struct vmw_plane_state *vps;
  242. if (plane->state)
  243. vmw_du_plane_destroy_state(plane, plane->state);
  244. vps = kzalloc_obj(*vps);
  245. if (!vps) {
  246. DRM_ERROR("Cannot allocate vmw_plane_state\n");
  247. return;
  248. }
  249. __drm_atomic_helper_plane_reset(plane, &vps->base);
  250. }
  251. /**
  252. * vmw_du_plane_destroy_state - destroy plane state
  253. * @plane: DRM plane
  254. * @state: state object to destroy
  255. *
  256. * Destroys the plane state (both common and vmw-specific) for the
  257. * specified plane.
  258. */
  259. void
  260. vmw_du_plane_destroy_state(struct drm_plane *plane,
  261. struct drm_plane_state *state)
  262. {
  263. struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
  264. /* Should have been freed by cleanup_fb */
  265. vmw_user_object_unref(&vps->uo);
  266. drm_atomic_helper_plane_destroy_state(plane, state);
  267. }
  268. /**
  269. * vmw_du_connector_duplicate_state - duplicate connector state
  270. * @connector: DRM connector
  271. *
  272. * Allocates and returns a copy of the connector state (both common and
  273. * vmw-specific) for the specified connector.
  274. *
  275. * Returns: The newly allocated connector state, or NULL on failure.
  276. */
  277. struct drm_connector_state *
  278. vmw_du_connector_duplicate_state(struct drm_connector *connector)
  279. {
  280. struct drm_connector_state *state;
  281. struct vmw_connector_state *vcs;
  282. if (WARN_ON(!connector->state))
  283. return NULL;
  284. vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
  285. if (!vcs)
  286. return NULL;
  287. state = &vcs->base;
  288. __drm_atomic_helper_connector_duplicate_state(connector, state);
  289. return state;
  290. }
  291. /**
  292. * vmw_du_connector_reset - creates a blank vmw connector state
  293. * @connector: DRM connector
  294. *
  295. * Resets the atomic state for @connector by freeing the state pointer (which
  296. * might be NULL, e.g. at driver load time) and allocating a new empty state
  297. * object.
  298. */
  299. void vmw_du_connector_reset(struct drm_connector *connector)
  300. {
  301. struct vmw_connector_state *vcs;
  302. if (connector->state) {
  303. __drm_atomic_helper_connector_destroy_state(connector->state);
  304. kfree(vmw_connector_state_to_vcs(connector->state));
  305. }
  306. vcs = kzalloc_obj(*vcs);
  307. if (!vcs) {
  308. DRM_ERROR("Cannot allocate vmw_connector_state\n");
  309. return;
  310. }
  311. __drm_atomic_helper_connector_reset(connector, &vcs->base);
  312. }
  313. /**
  314. * vmw_du_connector_destroy_state - destroy connector state
  315. * @connector: DRM connector
  316. * @state: state object to destroy
  317. *
  318. * Destroys the connector state (both common and vmw-specific) for the
  319. * specified plane.
  320. */
  321. void
  322. vmw_du_connector_destroy_state(struct drm_connector *connector,
  323. struct drm_connector_state *state)
  324. {
  325. drm_atomic_helper_connector_destroy_state(connector, state);
  326. }
  327. /*
  328. * Generic framebuffer code
  329. */
  330. /*
  331. * Surface framebuffer code
  332. */
  333. static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
  334. {
  335. struct vmw_framebuffer_surface *vfbs =
  336. vmw_framebuffer_to_vfbs(framebuffer);
  337. struct vmw_bo *bo = vmw_user_object_buffer(&vfbs->uo);
  338. struct vmw_surface *surf = vmw_user_object_surface(&vfbs->uo);
  339. if (bo) {
  340. vmw_bo_dirty_release(bo);
  341. /*
  342. * bo->dirty is reference counted so it being NULL
  343. * means that the surface wasn't coherent to begin
  344. * with and so we have to free the dirty tracker
  345. * in the vmw_resource
  346. */
  347. if (!bo->dirty && surf && surf->res.dirty)
  348. surf->res.func->dirty_free(&surf->res);
  349. }
  350. drm_framebuffer_cleanup(framebuffer);
  351. vmw_user_object_unref(&vfbs->uo);
  352. kfree(vfbs);
  353. }
  354. /**
  355. * vmw_kms_readback - Perform a readback from the screen system to
  356. * a buffer-object backed framebuffer.
  357. *
  358. * @dev_priv: Pointer to the device private structure.
  359. * @file_priv: Pointer to a struct drm_file identifying the caller.
  360. * Must be set to NULL if @user_fence_rep is NULL.
  361. * @vfb: Pointer to the buffer-object backed framebuffer.
  362. * @user_fence_rep: User-space provided structure for fence information.
  363. * Must be set to non-NULL if @file_priv is non-NULL.
  364. * @vclips: Array of clip rects.
  365. * @num_clips: Number of clip rects in @vclips.
  366. *
  367. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  368. * interrupted.
  369. */
  370. int vmw_kms_readback(struct vmw_private *dev_priv,
  371. struct drm_file *file_priv,
  372. struct vmw_framebuffer *vfb,
  373. struct drm_vmw_fence_rep __user *user_fence_rep,
  374. struct drm_vmw_rect *vclips,
  375. uint32_t num_clips)
  376. {
  377. switch (dev_priv->active_display_unit) {
  378. case vmw_du_screen_object:
  379. return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
  380. user_fence_rep, vclips, num_clips,
  381. NULL);
  382. case vmw_du_screen_target:
  383. return vmw_kms_stdu_readback(dev_priv, file_priv, vfb,
  384. user_fence_rep, NULL, vclips, num_clips,
  385. 1, NULL);
  386. default:
  387. WARN_ONCE(true,
  388. "Readback called with invalid display system.\n");
  389. }
  390. return -ENOSYS;
  391. }
  392. static int vmw_framebuffer_surface_create_handle(struct drm_framebuffer *fb,
  393. struct drm_file *file_priv,
  394. unsigned int *handle)
  395. {
  396. struct vmw_framebuffer_surface *vfbs = vmw_framebuffer_to_vfbs(fb);
  397. struct vmw_bo *bo = vmw_user_object_buffer(&vfbs->uo);
  398. if (WARN_ON(!bo))
  399. return -EINVAL;
  400. return drm_gem_handle_create(file_priv, &bo->tbo.base, handle);
  401. }
  402. static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
  403. .create_handle = vmw_framebuffer_surface_create_handle,
  404. .destroy = vmw_framebuffer_surface_destroy,
  405. .dirty = drm_atomic_helper_dirtyfb,
  406. };
  407. static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
  408. struct vmw_user_object *uo,
  409. struct vmw_framebuffer **out,
  410. const struct drm_format_info *info,
  411. const struct drm_mode_fb_cmd2
  412. *mode_cmd)
  413. {
  414. struct drm_device *dev = &dev_priv->drm;
  415. struct vmw_framebuffer_surface *vfbs;
  416. struct vmw_surface *surface;
  417. int ret;
  418. /* 3D is only supported on HWv8 and newer hosts */
  419. if (dev_priv->active_display_unit == vmw_du_legacy)
  420. return -ENOSYS;
  421. surface = vmw_user_object_surface(uo);
  422. /*
  423. * Sanity checks.
  424. */
  425. if (!drm_any_plane_has_format(&dev_priv->drm,
  426. mode_cmd->pixel_format,
  427. mode_cmd->modifier[0])) {
  428. drm_dbg(&dev_priv->drm,
  429. "unsupported pixel format %p4cc / modifier 0x%llx\n",
  430. &mode_cmd->pixel_format, mode_cmd->modifier[0]);
  431. return -EINVAL;
  432. }
  433. /* Surface must be marked as a scanout. */
  434. if (unlikely(!surface->metadata.scanout))
  435. return -EINVAL;
  436. if (unlikely(surface->metadata.mip_levels[0] != 1 ||
  437. surface->metadata.num_sizes != 1 ||
  438. surface->metadata.base_size.width < mode_cmd->width ||
  439. surface->metadata.base_size.height < mode_cmd->height ||
  440. surface->metadata.base_size.depth != 1)) {
  441. DRM_ERROR("Incompatible surface dimensions "
  442. "for requested mode.\n");
  443. return -EINVAL;
  444. }
  445. vfbs = kzalloc_obj(*vfbs);
  446. if (!vfbs) {
  447. ret = -ENOMEM;
  448. goto out_err1;
  449. }
  450. drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, info, mode_cmd);
  451. memcpy(&vfbs->uo, uo, sizeof(vfbs->uo));
  452. vmw_user_object_ref(&vfbs->uo);
  453. if (vfbs->uo.buffer)
  454. vfbs->base.base.obj[0] = &vfbs->uo.buffer->tbo.base;
  455. *out = &vfbs->base;
  456. ret = drm_framebuffer_init(dev, &vfbs->base.base,
  457. &vmw_framebuffer_surface_funcs);
  458. if (ret)
  459. goto out_err2;
  460. return 0;
  461. out_err2:
  462. vmw_user_object_unref(&vfbs->uo);
  463. kfree(vfbs);
  464. out_err1:
  465. return ret;
  466. }
  467. /*
  468. * Buffer-object framebuffer code
  469. */
  470. static int vmw_framebuffer_bo_create_handle(struct drm_framebuffer *fb,
  471. struct drm_file *file_priv,
  472. unsigned int *handle)
  473. {
  474. struct vmw_framebuffer_bo *vfbd =
  475. vmw_framebuffer_to_vfbd(fb);
  476. return drm_gem_handle_create(file_priv, &vfbd->buffer->tbo.base, handle);
  477. }
  478. static void vmw_framebuffer_bo_destroy(struct drm_framebuffer *framebuffer)
  479. {
  480. struct vmw_framebuffer_bo *vfbd =
  481. vmw_framebuffer_to_vfbd(framebuffer);
  482. vmw_bo_dirty_release(vfbd->buffer);
  483. drm_framebuffer_cleanup(framebuffer);
  484. vmw_bo_unreference(&vfbd->buffer);
  485. kfree(vfbd);
  486. }
  487. static const struct drm_framebuffer_funcs vmw_framebuffer_bo_funcs = {
  488. .create_handle = vmw_framebuffer_bo_create_handle,
  489. .destroy = vmw_framebuffer_bo_destroy,
  490. .dirty = drm_atomic_helper_dirtyfb,
  491. };
  492. static int vmw_kms_new_framebuffer_bo(struct vmw_private *dev_priv,
  493. struct vmw_bo *bo,
  494. struct vmw_framebuffer **out,
  495. const struct drm_format_info *info,
  496. const struct drm_mode_fb_cmd2
  497. *mode_cmd)
  498. {
  499. struct drm_device *dev = &dev_priv->drm;
  500. struct vmw_framebuffer_bo *vfbd;
  501. unsigned int requested_size;
  502. int ret;
  503. requested_size = mode_cmd->height * mode_cmd->pitches[0];
  504. if (unlikely(requested_size > bo->tbo.base.size)) {
  505. DRM_ERROR("Screen buffer object size is too small "
  506. "for requested mode.\n");
  507. return -EINVAL;
  508. }
  509. if (!drm_any_plane_has_format(&dev_priv->drm,
  510. mode_cmd->pixel_format,
  511. mode_cmd->modifier[0])) {
  512. drm_dbg(&dev_priv->drm,
  513. "unsupported pixel format %p4cc / modifier 0x%llx\n",
  514. &mode_cmd->pixel_format, mode_cmd->modifier[0]);
  515. return -EINVAL;
  516. }
  517. vfbd = kzalloc_obj(*vfbd);
  518. if (!vfbd) {
  519. ret = -ENOMEM;
  520. goto out_err1;
  521. }
  522. vfbd->base.base.obj[0] = &bo->tbo.base;
  523. drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, info, mode_cmd);
  524. vfbd->base.bo = true;
  525. vfbd->buffer = vmw_bo_reference(bo);
  526. *out = &vfbd->base;
  527. ret = drm_framebuffer_init(dev, &vfbd->base.base,
  528. &vmw_framebuffer_bo_funcs);
  529. if (ret)
  530. goto out_err2;
  531. return 0;
  532. out_err2:
  533. vmw_bo_unreference(&bo);
  534. kfree(vfbd);
  535. out_err1:
  536. return ret;
  537. }
  538. /**
  539. * vmw_kms_srf_ok - check if a surface can be created
  540. *
  541. * @dev_priv: Pointer to device private struct.
  542. * @width: requested width
  543. * @height: requested height
  544. *
  545. * Surfaces need to be less than texture size
  546. */
  547. static bool
  548. vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
  549. {
  550. if (width > dev_priv->texture_max_width ||
  551. height > dev_priv->texture_max_height)
  552. return false;
  553. return true;
  554. }
  555. /**
  556. * vmw_kms_new_framebuffer - Create a new framebuffer.
  557. *
  558. * @dev_priv: Pointer to device private struct.
  559. * @uo: Pointer to user object to wrap the kms framebuffer around.
  560. * Either the buffer or surface inside the user object must be NULL.
  561. * @info: pixel format information.
  562. * @mode_cmd: Frame-buffer metadata.
  563. */
  564. struct vmw_framebuffer *
  565. vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
  566. struct vmw_user_object *uo,
  567. const struct drm_format_info *info,
  568. const struct drm_mode_fb_cmd2 *mode_cmd)
  569. {
  570. struct vmw_framebuffer *vfb = NULL;
  571. int ret;
  572. /* Create the new framebuffer depending one what we have */
  573. if (vmw_user_object_surface(uo)) {
  574. ret = vmw_kms_new_framebuffer_surface(dev_priv, uo, &vfb,
  575. info, mode_cmd);
  576. } else if (uo->buffer) {
  577. ret = vmw_kms_new_framebuffer_bo(dev_priv, uo->buffer, &vfb,
  578. info, mode_cmd);
  579. } else {
  580. BUG();
  581. }
  582. if (ret)
  583. return ERR_PTR(ret);
  584. return vfb;
  585. }
  586. /*
  587. * Generic Kernel modesetting functions
  588. */
  589. static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
  590. struct drm_file *file_priv,
  591. const struct drm_format_info *info,
  592. const struct drm_mode_fb_cmd2 *mode_cmd)
  593. {
  594. struct vmw_private *dev_priv = vmw_priv(dev);
  595. struct vmw_framebuffer *vfb = NULL;
  596. struct vmw_user_object uo = {0};
  597. struct vmw_bo *bo;
  598. struct vmw_surface *surface;
  599. int ret;
  600. /* returns either a bo or surface */
  601. ret = vmw_user_object_lookup(dev_priv, file_priv, mode_cmd->handles[0],
  602. &uo);
  603. if (ret) {
  604. DRM_ERROR("Invalid buffer object handle %u (0x%x).\n",
  605. mode_cmd->handles[0], mode_cmd->handles[0]);
  606. goto err_out;
  607. }
  608. if (vmw_user_object_surface(&uo) &&
  609. !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
  610. DRM_ERROR("Surface size cannot exceed %dx%d\n",
  611. dev_priv->texture_max_width,
  612. dev_priv->texture_max_height);
  613. ret = -EINVAL;
  614. goto err_out;
  615. }
  616. vfb = vmw_kms_new_framebuffer(dev_priv, &uo, info, mode_cmd);
  617. if (IS_ERR(vfb)) {
  618. ret = PTR_ERR(vfb);
  619. goto err_out;
  620. }
  621. err_out:
  622. bo = vmw_user_object_buffer(&uo);
  623. surface = vmw_user_object_surface(&uo);
  624. /* vmw_user_object_lookup takes one ref so does new_fb */
  625. vmw_user_object_unref(&uo);
  626. if (ret) {
  627. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  628. return ERR_PTR(ret);
  629. }
  630. if (bo) {
  631. ttm_bo_reserve(&bo->tbo, false, false, NULL);
  632. ret = vmw_bo_dirty_add(bo);
  633. if (!ret && surface && surface->res.func->dirty_alloc) {
  634. surface->res.coherent = true;
  635. if (surface->res.dirty == NULL)
  636. ret = surface->res.func->dirty_alloc(&surface->res);
  637. }
  638. ttm_bo_unreserve(&bo->tbo);
  639. }
  640. return &vfb->base;
  641. }
  642. /**
  643. * vmw_kms_check_display_memory - Validates display memory required for a
  644. * topology
  645. * @dev: DRM device
  646. * @num_rects: number of drm_rect in rects
  647. * @rects: array of drm_rect representing the topology to validate indexed by
  648. * crtc index.
  649. *
  650. * Returns:
  651. * 0 on success otherwise negative error code
  652. */
  653. static int vmw_kms_check_display_memory(struct drm_device *dev,
  654. uint32_t num_rects,
  655. struct drm_rect *rects)
  656. {
  657. struct vmw_private *dev_priv = vmw_priv(dev);
  658. struct drm_rect bounding_box = {0};
  659. u64 total_pixels = 0, pixel_mem, bb_mem;
  660. int i;
  661. for (i = 0; i < num_rects; i++) {
  662. /*
  663. * For STDU only individual screen (screen target) is limited by
  664. * SCREENTARGET_MAX_WIDTH/HEIGHT registers.
  665. */
  666. if (dev_priv->active_display_unit == vmw_du_screen_target &&
  667. (drm_rect_width(&rects[i]) > dev_priv->stdu_max_width ||
  668. drm_rect_height(&rects[i]) > dev_priv->stdu_max_height)) {
  669. VMW_DEBUG_KMS("Screen size not supported.\n");
  670. return -EINVAL;
  671. }
  672. /* Bounding box upper left is at (0,0). */
  673. if (rects[i].x2 > bounding_box.x2)
  674. bounding_box.x2 = rects[i].x2;
  675. if (rects[i].y2 > bounding_box.y2)
  676. bounding_box.y2 = rects[i].y2;
  677. total_pixels += (u64) drm_rect_width(&rects[i]) *
  678. (u64) drm_rect_height(&rects[i]);
  679. }
  680. /* Virtual svga device primary limits are always in 32-bpp. */
  681. pixel_mem = total_pixels * 4;
  682. /*
  683. * For HV10 and below prim_bb_mem is vram size. When
  684. * SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM is not present vram size is
  685. * limit on primary bounding box
  686. */
  687. if (pixel_mem > dev_priv->max_primary_mem) {
  688. VMW_DEBUG_KMS("Combined output size too large.\n");
  689. return -EINVAL;
  690. }
  691. /* SVGA_CAP_NO_BB_RESTRICTION is available for STDU only. */
  692. if (dev_priv->active_display_unit != vmw_du_screen_target ||
  693. !(dev_priv->capabilities & SVGA_CAP_NO_BB_RESTRICTION)) {
  694. bb_mem = (u64) bounding_box.x2 * bounding_box.y2 * 4;
  695. if (bb_mem > dev_priv->max_primary_mem) {
  696. VMW_DEBUG_KMS("Topology is beyond supported limits.\n");
  697. return -EINVAL;
  698. }
  699. }
  700. return 0;
  701. }
  702. /**
  703. * vmw_crtc_state_and_lock - Return new or current crtc state with locked
  704. * crtc mutex
  705. * @state: The atomic state pointer containing the new atomic state
  706. * @crtc: The crtc
  707. *
  708. * This function returns the new crtc state if it's part of the state update.
  709. * Otherwise returns the current crtc state. It also makes sure that the
  710. * crtc mutex is locked.
  711. *
  712. * Returns: A valid crtc state pointer or NULL. It may also return a
  713. * pointer error, in particular -EDEADLK if locking needs to be rerun.
  714. */
  715. static struct drm_crtc_state *
  716. vmw_crtc_state_and_lock(struct drm_atomic_state *state, struct drm_crtc *crtc)
  717. {
  718. struct drm_crtc_state *crtc_state;
  719. crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  720. if (crtc_state) {
  721. lockdep_assert_held(&crtc->mutex.mutex.base);
  722. } else {
  723. int ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
  724. if (ret != 0 && ret != -EALREADY)
  725. return ERR_PTR(ret);
  726. crtc_state = crtc->state;
  727. }
  728. return crtc_state;
  729. }
  730. /**
  731. * vmw_kms_check_implicit - Verify that all implicit display units scan out
  732. * from the same fb after the new state is committed.
  733. * @dev: The drm_device.
  734. * @state: The new state to be checked.
  735. *
  736. * Returns:
  737. * Zero on success,
  738. * -EINVAL on invalid state,
  739. * -EDEADLK if modeset locking needs to be rerun.
  740. */
  741. static int vmw_kms_check_implicit(struct drm_device *dev,
  742. struct drm_atomic_state *state)
  743. {
  744. struct drm_framebuffer *implicit_fb = NULL;
  745. struct drm_crtc *crtc;
  746. struct drm_crtc_state *crtc_state;
  747. struct drm_plane_state *plane_state;
  748. drm_for_each_crtc(crtc, dev) {
  749. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  750. if (!du->is_implicit)
  751. continue;
  752. crtc_state = vmw_crtc_state_and_lock(state, crtc);
  753. if (IS_ERR(crtc_state))
  754. return PTR_ERR(crtc_state);
  755. if (!crtc_state || !crtc_state->enable)
  756. continue;
  757. /*
  758. * Can't move primary planes across crtcs, so this is OK.
  759. * It also means we don't need to take the plane mutex.
  760. */
  761. plane_state = du->primary.state;
  762. if (plane_state->crtc != crtc)
  763. continue;
  764. if (!implicit_fb)
  765. implicit_fb = plane_state->fb;
  766. else if (implicit_fb != plane_state->fb)
  767. return -EINVAL;
  768. }
  769. return 0;
  770. }
  771. /**
  772. * vmw_kms_check_topology - Validates topology in drm_atomic_state
  773. * @dev: DRM device
  774. * @state: the driver state object
  775. *
  776. * Returns:
  777. * 0 on success otherwise negative error code
  778. */
  779. static int vmw_kms_check_topology(struct drm_device *dev,
  780. struct drm_atomic_state *state)
  781. {
  782. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  783. struct drm_rect *rects;
  784. struct drm_crtc *crtc;
  785. uint32_t i;
  786. int ret = 0;
  787. rects = kzalloc_objs(struct drm_rect, dev->mode_config.num_crtc);
  788. if (!rects)
  789. return -ENOMEM;
  790. drm_for_each_crtc(crtc, dev) {
  791. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  792. struct drm_crtc_state *crtc_state;
  793. i = drm_crtc_index(crtc);
  794. crtc_state = vmw_crtc_state_and_lock(state, crtc);
  795. if (IS_ERR(crtc_state)) {
  796. ret = PTR_ERR(crtc_state);
  797. goto clean;
  798. }
  799. if (!crtc_state)
  800. continue;
  801. if (crtc_state->enable) {
  802. rects[i].x1 = du->gui_x;
  803. rects[i].y1 = du->gui_y;
  804. rects[i].x2 = du->gui_x + crtc_state->mode.hdisplay;
  805. rects[i].y2 = du->gui_y + crtc_state->mode.vdisplay;
  806. } else {
  807. rects[i].x1 = 0;
  808. rects[i].y1 = 0;
  809. rects[i].x2 = 0;
  810. rects[i].y2 = 0;
  811. }
  812. }
  813. /* Determine change to topology due to new atomic state */
  814. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
  815. new_crtc_state, i) {
  816. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  817. struct drm_connector *connector;
  818. struct drm_connector_state *conn_state;
  819. struct vmw_connector_state *vmw_conn_state;
  820. if (!du->pref_active && new_crtc_state->enable) {
  821. VMW_DEBUG_KMS("Enabling a disabled display unit\n");
  822. ret = -EINVAL;
  823. goto clean;
  824. }
  825. /*
  826. * For vmwgfx each crtc has only one connector attached and it
  827. * is not changed so don't really need to check the
  828. * crtc->connector_mask and iterate over it.
  829. */
  830. connector = &du->connector;
  831. conn_state = drm_atomic_get_connector_state(state, connector);
  832. if (IS_ERR(conn_state)) {
  833. ret = PTR_ERR(conn_state);
  834. goto clean;
  835. }
  836. vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
  837. vmw_conn_state->gui_x = du->gui_x;
  838. vmw_conn_state->gui_y = du->gui_y;
  839. }
  840. ret = vmw_kms_check_display_memory(dev, dev->mode_config.num_crtc,
  841. rects);
  842. clean:
  843. kfree(rects);
  844. return ret;
  845. }
  846. /**
  847. * vmw_kms_atomic_check_modeset- validate state object for modeset changes
  848. *
  849. * @dev: DRM device
  850. * @state: the driver state object
  851. *
  852. * This is a simple wrapper around drm_atomic_helper_check_modeset() for
  853. * us to assign a value to mode->crtc_clock so that
  854. * drm_calc_timestamping_constants() won't throw an error message
  855. *
  856. * Returns:
  857. * Zero for success or -errno
  858. */
  859. static int
  860. vmw_kms_atomic_check_modeset(struct drm_device *dev,
  861. struct drm_atomic_state *state)
  862. {
  863. struct drm_crtc *crtc;
  864. struct drm_crtc_state *crtc_state;
  865. bool need_modeset = false;
  866. int i, ret;
  867. ret = drm_atomic_helper_check(dev, state);
  868. if (ret)
  869. return ret;
  870. ret = vmw_kms_check_implicit(dev, state);
  871. if (ret) {
  872. VMW_DEBUG_KMS("Invalid implicit state\n");
  873. return ret;
  874. }
  875. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  876. if (drm_atomic_crtc_needs_modeset(crtc_state))
  877. need_modeset = true;
  878. }
  879. if (need_modeset)
  880. return vmw_kms_check_topology(dev, state);
  881. return ret;
  882. }
  883. static const struct drm_mode_config_funcs vmw_kms_funcs = {
  884. .fb_create = vmw_kms_fb_create,
  885. .atomic_check = vmw_kms_atomic_check_modeset,
  886. .atomic_commit = drm_atomic_helper_commit,
  887. };
  888. static int vmw_kms_generic_present(struct vmw_private *dev_priv,
  889. struct drm_file *file_priv,
  890. struct vmw_framebuffer *vfb,
  891. struct vmw_surface *surface,
  892. uint32_t sid,
  893. int32_t destX, int32_t destY,
  894. struct drm_vmw_rect *clips,
  895. uint32_t num_clips)
  896. {
  897. return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
  898. &surface->res, destX, destY,
  899. num_clips, 1, NULL, NULL);
  900. }
  901. int vmw_kms_present(struct vmw_private *dev_priv,
  902. struct drm_file *file_priv,
  903. struct vmw_framebuffer *vfb,
  904. struct vmw_surface *surface,
  905. uint32_t sid,
  906. int32_t destX, int32_t destY,
  907. struct drm_vmw_rect *clips,
  908. uint32_t num_clips)
  909. {
  910. int ret;
  911. switch (dev_priv->active_display_unit) {
  912. case vmw_du_screen_target:
  913. ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
  914. &surface->res, destX, destY,
  915. num_clips, 1, NULL, NULL);
  916. break;
  917. case vmw_du_screen_object:
  918. ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
  919. sid, destX, destY, clips,
  920. num_clips);
  921. break;
  922. default:
  923. WARN_ONCE(true,
  924. "Present called with invalid display system.\n");
  925. ret = -ENOSYS;
  926. break;
  927. }
  928. if (ret)
  929. return ret;
  930. vmw_cmd_flush(dev_priv, false);
  931. return 0;
  932. }
  933. static void
  934. vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
  935. {
  936. if (dev_priv->hotplug_mode_update_property)
  937. return;
  938. dev_priv->hotplug_mode_update_property =
  939. drm_property_create_range(&dev_priv->drm,
  940. DRM_MODE_PROP_IMMUTABLE,
  941. "hotplug_mode_update", 0, 1);
  942. }
  943. static void
  944. vmw_atomic_commit_tail(struct drm_atomic_state *old_state)
  945. {
  946. struct vmw_private *vmw = vmw_priv(old_state->dev);
  947. struct drm_crtc *crtc;
  948. struct drm_crtc_state *old_crtc_state;
  949. int i;
  950. drm_atomic_helper_commit_tail(old_state);
  951. if (vmw->vkms_enabled) {
  952. for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  953. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  954. (void)old_crtc_state;
  955. flush_work(&du->vkms.crc_generator_work);
  956. }
  957. }
  958. }
  959. static const struct drm_mode_config_helper_funcs vmw_mode_config_helpers = {
  960. .atomic_commit_tail = vmw_atomic_commit_tail,
  961. };
  962. int vmw_kms_init(struct vmw_private *dev_priv)
  963. {
  964. struct drm_device *dev = &dev_priv->drm;
  965. int ret;
  966. static const char *display_unit_names[] = {
  967. "Invalid",
  968. "Legacy",
  969. "Screen Object",
  970. "Screen Target",
  971. "Invalid (max)"
  972. };
  973. drm_mode_config_init(dev);
  974. dev->mode_config.funcs = &vmw_kms_funcs;
  975. dev->mode_config.min_width = 1;
  976. dev->mode_config.min_height = 1;
  977. dev->mode_config.max_width = dev_priv->texture_max_width;
  978. dev->mode_config.max_height = dev_priv->texture_max_height;
  979. dev->mode_config.preferred_depth = dev_priv->assume_16bpp ? 16 : 32;
  980. dev->mode_config.helper_private = &vmw_mode_config_helpers;
  981. drm_mode_create_suggested_offset_properties(dev);
  982. vmw_kms_create_hotplug_mode_update_property(dev_priv);
  983. ret = vmw_kms_stdu_init_display(dev_priv);
  984. if (ret) {
  985. ret = vmw_kms_sou_init_display(dev_priv);
  986. if (ret) /* Fallback */
  987. ret = vmw_kms_ldu_init_display(dev_priv);
  988. }
  989. BUILD_BUG_ON(ARRAY_SIZE(display_unit_names) != (vmw_du_max + 1));
  990. drm_info(&dev_priv->drm, "%s display unit initialized\n",
  991. display_unit_names[dev_priv->active_display_unit]);
  992. return ret;
  993. }
  994. int vmw_kms_close(struct vmw_private *dev_priv)
  995. {
  996. int ret = 0;
  997. /*
  998. * Docs says we should take the lock before calling this function
  999. * but since it destroys encoders and our destructor calls
  1000. * drm_encoder_cleanup which takes the lock we deadlock.
  1001. */
  1002. drm_mode_config_cleanup(&dev_priv->drm);
  1003. if (dev_priv->active_display_unit == vmw_du_legacy)
  1004. ret = vmw_kms_ldu_close_display(dev_priv);
  1005. return ret;
  1006. }
  1007. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  1008. unsigned width, unsigned height, unsigned pitch,
  1009. unsigned bpp, unsigned depth)
  1010. {
  1011. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1012. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  1013. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1014. vmw_fifo_mem_write(vmw_priv, SVGA_FIFO_PITCHLOCK, pitch);
  1015. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  1016. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  1017. if ((vmw_priv->capabilities & SVGA_CAP_8BIT_EMULATION) != 0)
  1018. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  1019. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  1020. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  1021. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  1022. return -EINVAL;
  1023. }
  1024. return 0;
  1025. }
  1026. static
  1027. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  1028. u64 pitch,
  1029. u64 height)
  1030. {
  1031. return (pitch * height) < (u64)dev_priv->vram_size;
  1032. }
  1033. /**
  1034. * vmw_du_update_layout - Update the display unit with topology from resolution
  1035. * plugin and generate DRM uevent
  1036. * @dev_priv: device private
  1037. * @num_rects: number of drm_rect in rects
  1038. * @rects: toplogy to update
  1039. */
  1040. static int vmw_du_update_layout(struct vmw_private *dev_priv,
  1041. unsigned int num_rects, struct drm_rect *rects)
  1042. {
  1043. struct drm_device *dev = &dev_priv->drm;
  1044. struct vmw_display_unit *du;
  1045. struct drm_connector *con;
  1046. struct drm_connector_list_iter conn_iter;
  1047. struct drm_modeset_acquire_ctx ctx;
  1048. struct drm_crtc *crtc;
  1049. int ret;
  1050. /* Currently gui_x/y is protected with the crtc mutex */
  1051. mutex_lock(&dev->mode_config.mutex);
  1052. drm_modeset_acquire_init(&ctx, 0);
  1053. retry:
  1054. drm_for_each_crtc(crtc, dev) {
  1055. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  1056. if (ret < 0) {
  1057. if (ret == -EDEADLK) {
  1058. drm_modeset_backoff(&ctx);
  1059. goto retry;
  1060. }
  1061. goto out_fini;
  1062. }
  1063. }
  1064. drm_connector_list_iter_begin(dev, &conn_iter);
  1065. drm_for_each_connector_iter(con, &conn_iter) {
  1066. du = vmw_connector_to_du(con);
  1067. if (num_rects > du->unit) {
  1068. du->pref_width = drm_rect_width(&rects[du->unit]);
  1069. du->pref_height = drm_rect_height(&rects[du->unit]);
  1070. du->pref_active = true;
  1071. du->gui_x = rects[du->unit].x1;
  1072. du->gui_y = rects[du->unit].y1;
  1073. } else {
  1074. du->pref_width = VMWGFX_MIN_INITIAL_WIDTH;
  1075. du->pref_height = VMWGFX_MIN_INITIAL_HEIGHT;
  1076. du->pref_active = false;
  1077. du->gui_x = 0;
  1078. du->gui_y = 0;
  1079. }
  1080. }
  1081. drm_connector_list_iter_end(&conn_iter);
  1082. list_for_each_entry(con, &dev->mode_config.connector_list, head) {
  1083. du = vmw_connector_to_du(con);
  1084. if (num_rects > du->unit) {
  1085. drm_object_property_set_value
  1086. (&con->base, dev->mode_config.suggested_x_property,
  1087. du->gui_x);
  1088. drm_object_property_set_value
  1089. (&con->base, dev->mode_config.suggested_y_property,
  1090. du->gui_y);
  1091. } else {
  1092. drm_object_property_set_value
  1093. (&con->base, dev->mode_config.suggested_x_property,
  1094. 0);
  1095. drm_object_property_set_value
  1096. (&con->base, dev->mode_config.suggested_y_property,
  1097. 0);
  1098. }
  1099. con->status = vmw_du_connector_detect(con, true);
  1100. }
  1101. out_fini:
  1102. drm_modeset_drop_locks(&ctx);
  1103. drm_modeset_acquire_fini(&ctx);
  1104. mutex_unlock(&dev->mode_config.mutex);
  1105. drm_sysfs_hotplug_event(dev);
  1106. return 0;
  1107. }
  1108. int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  1109. u16 *r, u16 *g, u16 *b,
  1110. uint32_t size,
  1111. struct drm_modeset_acquire_ctx *ctx)
  1112. {
  1113. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  1114. int i;
  1115. for (i = 0; i < size; i++) {
  1116. DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
  1117. r[i], g[i], b[i]);
  1118. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
  1119. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
  1120. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
  1121. }
  1122. return 0;
  1123. }
  1124. int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
  1125. {
  1126. return 0;
  1127. }
  1128. enum drm_connector_status
  1129. vmw_du_connector_detect(struct drm_connector *connector, bool force)
  1130. {
  1131. uint32_t num_displays;
  1132. struct drm_device *dev = connector->dev;
  1133. struct vmw_private *dev_priv = vmw_priv(dev);
  1134. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1135. num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
  1136. return ((vmw_connector_to_du(connector)->unit < num_displays &&
  1137. du->pref_active) ?
  1138. connector_status_connected : connector_status_disconnected);
  1139. }
  1140. /**
  1141. * vmw_guess_mode_timing - Provide fake timings for a
  1142. * 60Hz vrefresh mode.
  1143. *
  1144. * @mode: Pointer to a struct drm_display_mode with hdisplay and vdisplay
  1145. * members filled in.
  1146. */
  1147. void vmw_guess_mode_timing(struct drm_display_mode *mode)
  1148. {
  1149. mode->hsync_start = mode->hdisplay + 50;
  1150. mode->hsync_end = mode->hsync_start + 50;
  1151. mode->htotal = mode->hsync_end + 50;
  1152. mode->vsync_start = mode->vdisplay + 50;
  1153. mode->vsync_end = mode->vsync_start + 50;
  1154. mode->vtotal = mode->vsync_end + 50;
  1155. mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
  1156. }
  1157. /**
  1158. * vmw_kms_update_layout_ioctl - Handler for DRM_VMW_UPDATE_LAYOUT ioctl
  1159. * @dev: drm device for the ioctl
  1160. * @data: data pointer for the ioctl
  1161. * @file_priv: drm file for the ioctl call
  1162. *
  1163. * Update preferred topology of display unit as per ioctl request. The topology
  1164. * is expressed as array of drm_vmw_rect.
  1165. * e.g.
  1166. * [0 0 640 480] [640 0 800 600] [0 480 640 480]
  1167. *
  1168. * NOTE:
  1169. * The x and y offset (upper left) in drm_vmw_rect cannot be less than 0. Beside
  1170. * device limit on topology, x + w and y + h (lower right) cannot be greater
  1171. * than INT_MAX. So topology beyond these limits will return with error.
  1172. *
  1173. * Returns:
  1174. * Zero on success, negative errno on failure.
  1175. */
  1176. int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
  1177. struct drm_file *file_priv)
  1178. {
  1179. struct vmw_private *dev_priv = vmw_priv(dev);
  1180. struct drm_mode_config *mode_config = &dev->mode_config;
  1181. struct drm_vmw_update_layout_arg *arg =
  1182. (struct drm_vmw_update_layout_arg *)data;
  1183. const void __user *user_rects;
  1184. struct drm_vmw_rect *rects;
  1185. struct drm_rect *drm_rects;
  1186. unsigned rects_size;
  1187. int ret, i;
  1188. if (!arg->num_outputs) {
  1189. struct drm_rect def_rect = {0, 0,
  1190. VMWGFX_MIN_INITIAL_WIDTH,
  1191. VMWGFX_MIN_INITIAL_HEIGHT};
  1192. vmw_du_update_layout(dev_priv, 1, &def_rect);
  1193. return 0;
  1194. } else if (arg->num_outputs > VMWGFX_NUM_DISPLAY_UNITS) {
  1195. return -E2BIG;
  1196. }
  1197. rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
  1198. rects = kzalloc_objs(struct drm_vmw_rect, arg->num_outputs);
  1199. if (unlikely(!rects))
  1200. return -ENOMEM;
  1201. user_rects = (void __user *)(unsigned long)arg->rects;
  1202. ret = copy_from_user(rects, user_rects, rects_size);
  1203. if (unlikely(ret != 0)) {
  1204. DRM_ERROR("Failed to get rects.\n");
  1205. ret = -EFAULT;
  1206. goto out_free;
  1207. }
  1208. drm_rects = (struct drm_rect *)rects;
  1209. VMW_DEBUG_KMS("Layout count = %u\n", arg->num_outputs);
  1210. for (i = 0; i < arg->num_outputs; i++) {
  1211. struct drm_vmw_rect curr_rect;
  1212. /* Verify user-space for overflow as kernel use drm_rect */
  1213. if ((rects[i].x + rects[i].w > INT_MAX) ||
  1214. (rects[i].y + rects[i].h > INT_MAX)) {
  1215. ret = -ERANGE;
  1216. goto out_free;
  1217. }
  1218. curr_rect = rects[i];
  1219. drm_rects[i].x1 = curr_rect.x;
  1220. drm_rects[i].y1 = curr_rect.y;
  1221. drm_rects[i].x2 = curr_rect.x + curr_rect.w;
  1222. drm_rects[i].y2 = curr_rect.y + curr_rect.h;
  1223. VMW_DEBUG_KMS(" x1 = %d y1 = %d x2 = %d y2 = %d\n",
  1224. drm_rects[i].x1, drm_rects[i].y1,
  1225. drm_rects[i].x2, drm_rects[i].y2);
  1226. /*
  1227. * Currently this check is limiting the topology within
  1228. * mode_config->max (which actually is max texture size
  1229. * supported by virtual device). This limit is here to address
  1230. * window managers that create a big framebuffer for whole
  1231. * topology.
  1232. */
  1233. if (drm_rects[i].x1 < 0 || drm_rects[i].y1 < 0 ||
  1234. drm_rects[i].x2 > mode_config->max_width ||
  1235. drm_rects[i].y2 > mode_config->max_height) {
  1236. VMW_DEBUG_KMS("Invalid layout %d %d %d %d\n",
  1237. drm_rects[i].x1, drm_rects[i].y1,
  1238. drm_rects[i].x2, drm_rects[i].y2);
  1239. ret = -EINVAL;
  1240. goto out_free;
  1241. }
  1242. }
  1243. ret = vmw_kms_check_display_memory(dev, arg->num_outputs, drm_rects);
  1244. if (ret == 0)
  1245. vmw_du_update_layout(dev_priv, arg->num_outputs, drm_rects);
  1246. out_free:
  1247. kfree(rects);
  1248. return ret;
  1249. }
  1250. /**
  1251. * vmw_kms_helper_dirty - Helper to build commands and perform actions based
  1252. * on a set of cliprects and a set of display units.
  1253. *
  1254. * @dev_priv: Pointer to a device private structure.
  1255. * @framebuffer: Pointer to the framebuffer on which to perform the actions.
  1256. * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
  1257. * Cliprects are given in framebuffer coordinates.
  1258. * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
  1259. * be NULL. Cliprects are given in source coordinates.
  1260. * @dest_x: X coordinate offset for the crtc / destination clip rects.
  1261. * @dest_y: Y coordinate offset for the crtc / destination clip rects.
  1262. * @num_clips: Number of cliprects in the @clips or @vclips array.
  1263. * @increment: Integer with which to increment the clip counter when looping.
  1264. * Used to skip a predetermined number of clip rects.
  1265. * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
  1266. */
  1267. int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
  1268. struct vmw_framebuffer *framebuffer,
  1269. const struct drm_clip_rect *clips,
  1270. const struct drm_vmw_rect *vclips,
  1271. s32 dest_x, s32 dest_y,
  1272. int num_clips,
  1273. int increment,
  1274. struct vmw_kms_dirty *dirty)
  1275. {
  1276. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  1277. struct drm_crtc *crtc;
  1278. u32 num_units = 0;
  1279. u32 i, k;
  1280. dirty->dev_priv = dev_priv;
  1281. /* If crtc is passed, no need to iterate over other display units */
  1282. if (dirty->crtc) {
  1283. units[num_units++] = vmw_crtc_to_du(dirty->crtc);
  1284. } else {
  1285. list_for_each_entry(crtc, &dev_priv->drm.mode_config.crtc_list,
  1286. head) {
  1287. struct drm_plane *plane = crtc->primary;
  1288. if (plane->state->fb == &framebuffer->base)
  1289. units[num_units++] = vmw_crtc_to_du(crtc);
  1290. }
  1291. }
  1292. for (k = 0; k < num_units; k++) {
  1293. struct vmw_display_unit *unit = units[k];
  1294. s32 crtc_x = unit->crtc.x;
  1295. s32 crtc_y = unit->crtc.y;
  1296. s32 crtc_width = unit->crtc.mode.hdisplay;
  1297. s32 crtc_height = unit->crtc.mode.vdisplay;
  1298. const struct drm_clip_rect *clips_ptr = clips;
  1299. const struct drm_vmw_rect *vclips_ptr = vclips;
  1300. dirty->unit = unit;
  1301. if (dirty->fifo_reserve_size > 0) {
  1302. dirty->cmd = VMW_CMD_RESERVE(dev_priv,
  1303. dirty->fifo_reserve_size);
  1304. if (!dirty->cmd)
  1305. return -ENOMEM;
  1306. memset(dirty->cmd, 0, dirty->fifo_reserve_size);
  1307. }
  1308. dirty->num_hits = 0;
  1309. for (i = 0; i < num_clips; i++, clips_ptr += increment,
  1310. vclips_ptr += increment) {
  1311. s32 clip_left;
  1312. s32 clip_top;
  1313. /*
  1314. * Select clip array type. Note that integer type
  1315. * in @clips is unsigned short, whereas in @vclips
  1316. * it's 32-bit.
  1317. */
  1318. if (clips) {
  1319. dirty->fb_x = (s32) clips_ptr->x1;
  1320. dirty->fb_y = (s32) clips_ptr->y1;
  1321. dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
  1322. crtc_x;
  1323. dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
  1324. crtc_y;
  1325. } else {
  1326. dirty->fb_x = vclips_ptr->x;
  1327. dirty->fb_y = vclips_ptr->y;
  1328. dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
  1329. dest_x - crtc_x;
  1330. dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
  1331. dest_y - crtc_y;
  1332. }
  1333. dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
  1334. dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
  1335. /* Skip this clip if it's outside the crtc region */
  1336. if (dirty->unit_x1 >= crtc_width ||
  1337. dirty->unit_y1 >= crtc_height ||
  1338. dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
  1339. continue;
  1340. /* Clip right and bottom to crtc limits */
  1341. dirty->unit_x2 = min_t(s32, dirty->unit_x2,
  1342. crtc_width);
  1343. dirty->unit_y2 = min_t(s32, dirty->unit_y2,
  1344. crtc_height);
  1345. /* Clip left and top to crtc limits */
  1346. clip_left = min_t(s32, dirty->unit_x1, 0);
  1347. clip_top = min_t(s32, dirty->unit_y1, 0);
  1348. dirty->unit_x1 -= clip_left;
  1349. dirty->unit_y1 -= clip_top;
  1350. dirty->fb_x -= clip_left;
  1351. dirty->fb_y -= clip_top;
  1352. dirty->clip(dirty);
  1353. }
  1354. dirty->fifo_commit(dirty);
  1355. }
  1356. return 0;
  1357. }
  1358. /**
  1359. * vmw_kms_helper_validation_finish - Helper for post KMS command submission
  1360. * cleanup and fencing
  1361. * @dev_priv: Pointer to the device-private struct
  1362. * @file_priv: Pointer identifying the client when user-space fencing is used
  1363. * @ctx: Pointer to the validation context
  1364. * @out_fence: If non-NULL, returned refcounted fence-pointer
  1365. * @user_fence_rep: If non-NULL, pointer to user-space address area
  1366. * in which to copy user-space fence info
  1367. */
  1368. void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv,
  1369. struct drm_file *file_priv,
  1370. struct vmw_validation_context *ctx,
  1371. struct vmw_fence_obj **out_fence,
  1372. struct drm_vmw_fence_rep __user *
  1373. user_fence_rep)
  1374. {
  1375. struct vmw_fence_obj *fence = NULL;
  1376. uint32_t handle = 0;
  1377. int ret = 0;
  1378. if (file_priv || user_fence_rep || vmw_validation_has_bos(ctx) ||
  1379. out_fence)
  1380. ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
  1381. file_priv ? &handle : NULL);
  1382. vmw_validation_done(ctx, fence);
  1383. if (file_priv)
  1384. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
  1385. ret, user_fence_rep, fence,
  1386. handle, -1);
  1387. if (out_fence)
  1388. *out_fence = fence;
  1389. else
  1390. vmw_fence_obj_unreference(&fence);
  1391. }
  1392. /**
  1393. * vmw_kms_create_implicit_placement_property - Set up the implicit placement
  1394. * property.
  1395. *
  1396. * @dev_priv: Pointer to a device private struct.
  1397. *
  1398. * Sets up the implicit placement property unless it's already set up.
  1399. */
  1400. void
  1401. vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv)
  1402. {
  1403. if (dev_priv->implicit_placement_property)
  1404. return;
  1405. dev_priv->implicit_placement_property =
  1406. drm_property_create_range(&dev_priv->drm,
  1407. DRM_MODE_PROP_IMMUTABLE,
  1408. "implicit_placement", 0, 1);
  1409. }
  1410. /**
  1411. * vmw_kms_suspend - Save modesetting state and turn modesetting off.
  1412. *
  1413. * @dev: Pointer to the drm device
  1414. * Return: 0 on success. Negative error code on failure.
  1415. */
  1416. int vmw_kms_suspend(struct drm_device *dev)
  1417. {
  1418. struct vmw_private *dev_priv = vmw_priv(dev);
  1419. dev_priv->suspend_state = drm_atomic_helper_suspend(dev);
  1420. if (IS_ERR(dev_priv->suspend_state)) {
  1421. int ret = PTR_ERR(dev_priv->suspend_state);
  1422. DRM_ERROR("Failed kms suspend: %d\n", ret);
  1423. dev_priv->suspend_state = NULL;
  1424. return ret;
  1425. }
  1426. return 0;
  1427. }
  1428. /**
  1429. * vmw_kms_resume - Re-enable modesetting and restore state
  1430. *
  1431. * @dev: Pointer to the drm device
  1432. * Return: 0 on success. Negative error code on failure.
  1433. *
  1434. * State is resumed from a previous vmw_kms_suspend(). It's illegal
  1435. * to call this function without a previous vmw_kms_suspend().
  1436. */
  1437. int vmw_kms_resume(struct drm_device *dev)
  1438. {
  1439. struct vmw_private *dev_priv = vmw_priv(dev);
  1440. int ret;
  1441. if (WARN_ON(!dev_priv->suspend_state))
  1442. return 0;
  1443. ret = drm_atomic_helper_resume(dev, dev_priv->suspend_state);
  1444. dev_priv->suspend_state = NULL;
  1445. return ret;
  1446. }
  1447. /**
  1448. * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
  1449. *
  1450. * @dev: Pointer to the drm device
  1451. */
  1452. void vmw_kms_lost_device(struct drm_device *dev)
  1453. {
  1454. drm_atomic_helper_shutdown(dev);
  1455. }
  1456. /**
  1457. * vmw_du_helper_plane_update - Helper to do plane update on a display unit.
  1458. * @update: The closure structure.
  1459. *
  1460. * Call this helper after setting callbacks in &vmw_du_update_plane to do plane
  1461. * update on display unit.
  1462. *
  1463. * Return: 0 on success or a negative error code on failure.
  1464. */
  1465. int vmw_du_helper_plane_update(struct vmw_du_update_plane *update)
  1466. {
  1467. struct drm_plane_state *state = update->plane->state;
  1468. struct drm_plane_state *old_state = update->old_state;
  1469. struct drm_atomic_helper_damage_iter iter;
  1470. struct drm_rect clip;
  1471. struct drm_rect bb;
  1472. DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
  1473. uint32_t reserved_size = 0;
  1474. uint32_t submit_size = 0;
  1475. uint32_t curr_size = 0;
  1476. uint32_t num_hits = 0;
  1477. void *cmd_start;
  1478. char *cmd_next;
  1479. int ret;
  1480. /*
  1481. * Iterate in advance to check if really need plane update and find the
  1482. * number of clips that actually are in plane src for fifo allocation.
  1483. */
  1484. drm_atomic_helper_damage_iter_init(&iter, old_state, state);
  1485. drm_atomic_for_each_plane_damage(&iter, &clip)
  1486. num_hits++;
  1487. if (num_hits == 0)
  1488. return 0;
  1489. if (update->vfb->bo) {
  1490. struct vmw_framebuffer_bo *vfbbo =
  1491. container_of(update->vfb, typeof(*vfbbo), base);
  1492. /*
  1493. * For screen targets we want a mappable bo, for everything else we want
  1494. * accelerated i.e. host backed (vram or gmr) bo. If the display unit
  1495. * is not screen target then mob's shouldn't be available.
  1496. */
  1497. if (update->dev_priv->active_display_unit == vmw_du_screen_target) {
  1498. vmw_bo_placement_set(vfbbo->buffer,
  1499. VMW_BO_DOMAIN_SYS | VMW_BO_DOMAIN_MOB | VMW_BO_DOMAIN_GMR,
  1500. VMW_BO_DOMAIN_SYS | VMW_BO_DOMAIN_MOB | VMW_BO_DOMAIN_GMR);
  1501. } else {
  1502. WARN_ON(update->dev_priv->has_mob);
  1503. vmw_bo_placement_set_default_accelerated(vfbbo->buffer);
  1504. }
  1505. ret = vmw_validation_add_bo(&val_ctx, vfbbo->buffer);
  1506. } else {
  1507. struct vmw_framebuffer_surface *vfbs =
  1508. container_of(update->vfb, typeof(*vfbs), base);
  1509. struct vmw_surface *surf = vmw_user_object_surface(&vfbs->uo);
  1510. ret = vmw_validation_add_resource(&val_ctx, &surf->res,
  1511. 0, VMW_RES_DIRTY_NONE, NULL,
  1512. NULL);
  1513. }
  1514. if (ret)
  1515. return ret;
  1516. ret = vmw_validation_prepare(&val_ctx, update->mutex, update->intr);
  1517. if (ret)
  1518. goto out_unref;
  1519. reserved_size = update->calc_fifo_size(update, num_hits);
  1520. cmd_start = VMW_CMD_RESERVE(update->dev_priv, reserved_size);
  1521. if (!cmd_start) {
  1522. ret = -ENOMEM;
  1523. goto out_revert;
  1524. }
  1525. cmd_next = cmd_start;
  1526. if (update->post_prepare) {
  1527. curr_size = update->post_prepare(update, cmd_next);
  1528. cmd_next += curr_size;
  1529. submit_size += curr_size;
  1530. }
  1531. if (update->pre_clip) {
  1532. curr_size = update->pre_clip(update, cmd_next, num_hits);
  1533. cmd_next += curr_size;
  1534. submit_size += curr_size;
  1535. }
  1536. bb.x1 = INT_MAX;
  1537. bb.y1 = INT_MAX;
  1538. bb.x2 = INT_MIN;
  1539. bb.y2 = INT_MIN;
  1540. drm_atomic_helper_damage_iter_init(&iter, old_state, state);
  1541. drm_atomic_for_each_plane_damage(&iter, &clip) {
  1542. uint32_t fb_x = clip.x1;
  1543. uint32_t fb_y = clip.y1;
  1544. vmw_du_translate_to_crtc(state, &clip);
  1545. if (update->clip) {
  1546. curr_size = update->clip(update, cmd_next, &clip, fb_x,
  1547. fb_y);
  1548. cmd_next += curr_size;
  1549. submit_size += curr_size;
  1550. }
  1551. bb.x1 = min_t(int, bb.x1, clip.x1);
  1552. bb.y1 = min_t(int, bb.y1, clip.y1);
  1553. bb.x2 = max_t(int, bb.x2, clip.x2);
  1554. bb.y2 = max_t(int, bb.y2, clip.y2);
  1555. }
  1556. curr_size = update->post_clip(update, cmd_next, &bb);
  1557. submit_size += curr_size;
  1558. if (reserved_size < submit_size)
  1559. submit_size = 0;
  1560. vmw_cmd_commit(update->dev_priv, submit_size);
  1561. vmw_kms_helper_validation_finish(update->dev_priv, NULL, &val_ctx,
  1562. update->out_fence, NULL);
  1563. return ret;
  1564. out_revert:
  1565. vmw_validation_revert(&val_ctx);
  1566. out_unref:
  1567. vmw_validation_unref_lists(&val_ctx);
  1568. return ret;
  1569. }
  1570. /**
  1571. * vmw_connector_mode_valid - implements drm_connector_helper_funcs.mode_valid callback
  1572. *
  1573. * @connector: the drm connector, part of a DU container
  1574. * @mode: drm mode to check
  1575. *
  1576. * Returns MODE_OK on success, or a drm_mode_status error code.
  1577. */
  1578. enum drm_mode_status vmw_connector_mode_valid(struct drm_connector *connector,
  1579. const struct drm_display_mode *mode)
  1580. {
  1581. enum drm_mode_status ret;
  1582. struct drm_device *dev = connector->dev;
  1583. struct vmw_private *dev_priv = vmw_priv(dev);
  1584. u32 assumed_cpp = 4;
  1585. if (dev_priv->assume_16bpp)
  1586. assumed_cpp = 2;
  1587. ret = drm_mode_validate_size(mode, dev_priv->texture_max_width,
  1588. dev_priv->texture_max_height);
  1589. if (ret != MODE_OK)
  1590. return ret;
  1591. if (!vmw_kms_validate_mode_vram(dev_priv,
  1592. mode->hdisplay * assumed_cpp,
  1593. mode->vdisplay))
  1594. return MODE_MEM;
  1595. return MODE_OK;
  1596. }
  1597. /**
  1598. * vmw_connector_get_modes - implements drm_connector_helper_funcs.get_modes callback
  1599. *
  1600. * @connector: the drm connector, part of a DU container
  1601. *
  1602. * Returns the number of added modes.
  1603. */
  1604. int vmw_connector_get_modes(struct drm_connector *connector)
  1605. {
  1606. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1607. struct drm_device *dev = connector->dev;
  1608. struct vmw_private *dev_priv = vmw_priv(dev);
  1609. struct drm_display_mode *mode = NULL;
  1610. struct drm_display_mode prefmode = { DRM_MODE("preferred",
  1611. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  1612. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1613. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  1614. };
  1615. u32 max_width;
  1616. u32 max_height;
  1617. u32 num_modes;
  1618. /* Add preferred mode */
  1619. mode = drm_mode_duplicate(dev, &prefmode);
  1620. if (!mode)
  1621. return 0;
  1622. mode->hdisplay = du->pref_width;
  1623. mode->vdisplay = du->pref_height;
  1624. vmw_guess_mode_timing(mode);
  1625. drm_mode_set_name(mode);
  1626. drm_mode_probed_add(connector, mode);
  1627. drm_dbg_kms(dev, "preferred mode " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
  1628. /* Probe connector for all modes not exceeding our geom limits */
  1629. max_width = dev_priv->texture_max_width;
  1630. max_height = dev_priv->texture_max_height;
  1631. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1632. max_width = min(dev_priv->stdu_max_width, max_width);
  1633. max_height = min(dev_priv->stdu_max_height, max_height);
  1634. }
  1635. num_modes = 1 + drm_add_modes_noedid(connector, max_width, max_height);
  1636. return num_modes;
  1637. }
  1638. struct vmw_user_object *vmw_user_object_ref(struct vmw_user_object *uo)
  1639. {
  1640. if (uo->buffer)
  1641. vmw_user_bo_ref(uo->buffer);
  1642. else if (uo->surface)
  1643. vmw_surface_reference(uo->surface);
  1644. return uo;
  1645. }
  1646. void vmw_user_object_unref(struct vmw_user_object *uo)
  1647. {
  1648. if (uo->buffer)
  1649. vmw_user_bo_unref(&uo->buffer);
  1650. else if (uo->surface)
  1651. vmw_surface_unreference(&uo->surface);
  1652. }
  1653. struct vmw_bo *
  1654. vmw_user_object_buffer(struct vmw_user_object *uo)
  1655. {
  1656. if (uo->buffer)
  1657. return uo->buffer;
  1658. else if (uo->surface)
  1659. return uo->surface->res.guest_memory_bo;
  1660. return NULL;
  1661. }
  1662. struct vmw_surface *
  1663. vmw_user_object_surface(struct vmw_user_object *uo)
  1664. {
  1665. if (uo->buffer)
  1666. return uo->buffer->dumb_surface;
  1667. return uo->surface;
  1668. }
  1669. void *vmw_user_object_map(struct vmw_user_object *uo)
  1670. {
  1671. struct vmw_bo *bo = vmw_user_object_buffer(uo);
  1672. WARN_ON(!bo);
  1673. return vmw_bo_map_and_cache(bo);
  1674. }
  1675. void *vmw_user_object_map_size(struct vmw_user_object *uo, size_t size)
  1676. {
  1677. struct vmw_bo *bo = vmw_user_object_buffer(uo);
  1678. WARN_ON(!bo);
  1679. return vmw_bo_map_and_cache_size(bo, size);
  1680. }
  1681. void vmw_user_object_unmap(struct vmw_user_object *uo)
  1682. {
  1683. struct vmw_bo *bo = vmw_user_object_buffer(uo);
  1684. int ret;
  1685. WARN_ON(!bo);
  1686. /* Fence the mob creation so we are guarateed to have the mob */
  1687. ret = ttm_bo_reserve(&bo->tbo, false, false, NULL);
  1688. if (ret != 0)
  1689. return;
  1690. vmw_bo_unmap(bo);
  1691. vmw_bo_pin_reserved(bo, false);
  1692. ttm_bo_unreserve(&bo->tbo);
  1693. }
  1694. bool vmw_user_object_is_mapped(struct vmw_user_object *uo)
  1695. {
  1696. struct vmw_bo *bo;
  1697. if (!uo || vmw_user_object_is_null(uo))
  1698. return false;
  1699. bo = vmw_user_object_buffer(uo);
  1700. if (WARN_ON(!bo))
  1701. return false;
  1702. WARN_ON(bo->map.bo && !bo->map.virtual);
  1703. return bo->map.virtual;
  1704. }
  1705. bool vmw_user_object_is_null(struct vmw_user_object *uo)
  1706. {
  1707. return !uo->buffer && !uo->surface;
  1708. }