vmwgfx_kms.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_kms.h"
  28. #include <drm/drm_plane_helper.h>
  29. #include <drm/drm_atomic.h>
  30. #include <drm/drm_atomic_helper.h>
  31. #include <drm/drm_rect.h>
  32. /* Might need a hrtimer here? */
  33. #define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
  34. void vmw_du_cleanup(struct vmw_display_unit *du)
  35. {
  36. drm_plane_cleanup(&du->primary);
  37. drm_plane_cleanup(&du->cursor);
  38. drm_connector_unregister(&du->connector);
  39. drm_crtc_cleanup(&du->crtc);
  40. drm_encoder_cleanup(&du->encoder);
  41. drm_connector_cleanup(&du->connector);
  42. }
  43. /*
  44. * Display Unit Cursor functions
  45. */
  46. static int vmw_cursor_update_image(struct vmw_private *dev_priv,
  47. u32 *image, u32 width, u32 height,
  48. u32 hotspotX, u32 hotspotY)
  49. {
  50. struct {
  51. u32 cmd;
  52. SVGAFifoCmdDefineAlphaCursor cursor;
  53. } *cmd;
  54. u32 image_size = width * height * 4;
  55. u32 cmd_size = sizeof(*cmd) + image_size;
  56. if (!image)
  57. return -EINVAL;
  58. cmd = vmw_fifo_reserve(dev_priv, cmd_size);
  59. if (unlikely(cmd == NULL)) {
  60. DRM_ERROR("Fifo reserve failed.\n");
  61. return -ENOMEM;
  62. }
  63. memset(cmd, 0, sizeof(*cmd));
  64. memcpy(&cmd[1], image, image_size);
  65. cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
  66. cmd->cursor.id = 0;
  67. cmd->cursor.width = width;
  68. cmd->cursor.height = height;
  69. cmd->cursor.hotspotX = hotspotX;
  70. cmd->cursor.hotspotY = hotspotY;
  71. vmw_fifo_commit_flush(dev_priv, cmd_size);
  72. return 0;
  73. }
  74. static int vmw_cursor_update_bo(struct vmw_private *dev_priv,
  75. struct vmw_buffer_object *bo,
  76. u32 width, u32 height,
  77. u32 hotspotX, u32 hotspotY)
  78. {
  79. struct ttm_bo_kmap_obj map;
  80. unsigned long kmap_offset;
  81. unsigned long kmap_num;
  82. void *virtual;
  83. bool dummy;
  84. int ret;
  85. kmap_offset = 0;
  86. kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
  87. ret = ttm_bo_reserve(&bo->base, true, false, NULL);
  88. if (unlikely(ret != 0)) {
  89. DRM_ERROR("reserve failed\n");
  90. return -EINVAL;
  91. }
  92. ret = ttm_bo_kmap(&bo->base, kmap_offset, kmap_num, &map);
  93. if (unlikely(ret != 0))
  94. goto err_unreserve;
  95. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  96. ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
  97. hotspotX, hotspotY);
  98. ttm_bo_kunmap(&map);
  99. err_unreserve:
  100. ttm_bo_unreserve(&bo->base);
  101. return ret;
  102. }
  103. static void vmw_cursor_update_position(struct vmw_private *dev_priv,
  104. bool show, int x, int y)
  105. {
  106. u32 *fifo_mem = dev_priv->mmio_virt;
  107. uint32_t count;
  108. spin_lock(&dev_priv->cursor_lock);
  109. vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
  110. vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
  111. vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
  112. count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  113. vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  114. spin_unlock(&dev_priv->cursor_lock);
  115. }
  116. void vmw_kms_cursor_snoop(struct vmw_surface *srf,
  117. struct ttm_object_file *tfile,
  118. struct ttm_buffer_object *bo,
  119. SVGA3dCmdHeader *header)
  120. {
  121. struct ttm_bo_kmap_obj map;
  122. unsigned long kmap_offset;
  123. unsigned long kmap_num;
  124. SVGA3dCopyBox *box;
  125. unsigned box_count;
  126. void *virtual;
  127. bool dummy;
  128. struct vmw_dma_cmd {
  129. SVGA3dCmdHeader header;
  130. SVGA3dCmdSurfaceDMA dma;
  131. } *cmd;
  132. int i, ret;
  133. cmd = container_of(header, struct vmw_dma_cmd, header);
  134. /* No snooper installed */
  135. if (!srf->snooper.image)
  136. return;
  137. if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
  138. DRM_ERROR("face and mipmap for cursors should never != 0\n");
  139. return;
  140. }
  141. if (cmd->header.size < 64) {
  142. DRM_ERROR("at least one full copy box must be given\n");
  143. return;
  144. }
  145. box = (SVGA3dCopyBox *)&cmd[1];
  146. box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
  147. sizeof(SVGA3dCopyBox);
  148. if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
  149. box->x != 0 || box->y != 0 || box->z != 0 ||
  150. box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
  151. box->d != 1 || box_count != 1) {
  152. /* TODO handle none page aligned offsets */
  153. /* TODO handle more dst & src != 0 */
  154. /* TODO handle more then one copy */
  155. DRM_ERROR("Cant snoop dma request for cursor!\n");
  156. DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
  157. box->srcx, box->srcy, box->srcz,
  158. box->x, box->y, box->z,
  159. box->w, box->h, box->d, box_count,
  160. cmd->dma.guest.ptr.offset);
  161. return;
  162. }
  163. kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
  164. kmap_num = (64*64*4) >> PAGE_SHIFT;
  165. ret = ttm_bo_reserve(bo, true, false, NULL);
  166. if (unlikely(ret != 0)) {
  167. DRM_ERROR("reserve failed\n");
  168. return;
  169. }
  170. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  171. if (unlikely(ret != 0))
  172. goto err_unreserve;
  173. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  174. if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
  175. memcpy(srf->snooper.image, virtual, 64*64*4);
  176. } else {
  177. /* Image is unsigned pointer. */
  178. for (i = 0; i < box->h; i++)
  179. memcpy(srf->snooper.image + i * 64,
  180. virtual + i * cmd->dma.guest.pitch,
  181. box->w * 4);
  182. }
  183. srf->snooper.age++;
  184. ttm_bo_kunmap(&map);
  185. err_unreserve:
  186. ttm_bo_unreserve(bo);
  187. }
  188. /**
  189. * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
  190. *
  191. * @dev_priv: Pointer to the device private struct.
  192. *
  193. * Clears all legacy hotspots.
  194. */
  195. void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
  196. {
  197. struct drm_device *dev = dev_priv->dev;
  198. struct vmw_display_unit *du;
  199. struct drm_crtc *crtc;
  200. drm_modeset_lock_all(dev);
  201. drm_for_each_crtc(crtc, dev) {
  202. du = vmw_crtc_to_du(crtc);
  203. du->hotspot_x = 0;
  204. du->hotspot_y = 0;
  205. }
  206. drm_modeset_unlock_all(dev);
  207. }
  208. void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
  209. {
  210. struct drm_device *dev = dev_priv->dev;
  211. struct vmw_display_unit *du;
  212. struct drm_crtc *crtc;
  213. mutex_lock(&dev->mode_config.mutex);
  214. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  215. du = vmw_crtc_to_du(crtc);
  216. if (!du->cursor_surface ||
  217. du->cursor_age == du->cursor_surface->snooper.age)
  218. continue;
  219. du->cursor_age = du->cursor_surface->snooper.age;
  220. vmw_cursor_update_image(dev_priv,
  221. du->cursor_surface->snooper.image,
  222. 64, 64,
  223. du->hotspot_x + du->core_hotspot_x,
  224. du->hotspot_y + du->core_hotspot_y);
  225. }
  226. mutex_unlock(&dev->mode_config.mutex);
  227. }
  228. void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
  229. {
  230. vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
  231. drm_plane_cleanup(plane);
  232. }
  233. void vmw_du_primary_plane_destroy(struct drm_plane *plane)
  234. {
  235. drm_plane_cleanup(plane);
  236. /* Planes are static in our case so we don't free it */
  237. }
  238. /**
  239. * vmw_du_vps_unpin_surf - unpins resource associated with a framebuffer surface
  240. *
  241. * @vps: plane state associated with the display surface
  242. * @unreference: true if we also want to unreference the display.
  243. */
  244. void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
  245. bool unreference)
  246. {
  247. if (vps->surf) {
  248. if (vps->pinned) {
  249. vmw_resource_unpin(&vps->surf->res);
  250. vps->pinned--;
  251. }
  252. if (unreference) {
  253. if (vps->pinned)
  254. DRM_ERROR("Surface still pinned\n");
  255. vmw_surface_unreference(&vps->surf);
  256. }
  257. }
  258. }
  259. /**
  260. * vmw_du_plane_cleanup_fb - Unpins the cursor
  261. *
  262. * @plane: display plane
  263. * @old_state: Contains the FB to clean up
  264. *
  265. * Unpins the framebuffer surface
  266. *
  267. * Returns 0 on success
  268. */
  269. void
  270. vmw_du_plane_cleanup_fb(struct drm_plane *plane,
  271. struct drm_plane_state *old_state)
  272. {
  273. struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
  274. vmw_du_plane_unpin_surf(vps, false);
  275. }
  276. /**
  277. * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it
  278. *
  279. * @plane: display plane
  280. * @new_state: info on the new plane state, including the FB
  281. *
  282. * Returns 0 on success
  283. */
  284. int
  285. vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
  286. struct drm_plane_state *new_state)
  287. {
  288. struct drm_framebuffer *fb = new_state->fb;
  289. struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
  290. if (vps->surf)
  291. vmw_surface_unreference(&vps->surf);
  292. if (vps->bo)
  293. vmw_bo_unreference(&vps->bo);
  294. if (fb) {
  295. if (vmw_framebuffer_to_vfb(fb)->bo) {
  296. vps->bo = vmw_framebuffer_to_vfbd(fb)->buffer;
  297. vmw_bo_reference(vps->bo);
  298. } else {
  299. vps->surf = vmw_framebuffer_to_vfbs(fb)->surface;
  300. vmw_surface_reference(vps->surf);
  301. }
  302. }
  303. return 0;
  304. }
  305. void
  306. vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
  307. struct drm_plane_state *old_state)
  308. {
  309. struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
  310. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  311. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  312. struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
  313. s32 hotspot_x, hotspot_y;
  314. int ret = 0;
  315. hotspot_x = du->hotspot_x;
  316. hotspot_y = du->hotspot_y;
  317. if (plane->state->fb) {
  318. hotspot_x += plane->state->fb->hot_x;
  319. hotspot_y += plane->state->fb->hot_y;
  320. }
  321. du->cursor_surface = vps->surf;
  322. du->cursor_bo = vps->bo;
  323. if (vps->surf) {
  324. du->cursor_age = du->cursor_surface->snooper.age;
  325. ret = vmw_cursor_update_image(dev_priv,
  326. vps->surf->snooper.image,
  327. 64, 64, hotspot_x,
  328. hotspot_y);
  329. } else if (vps->bo) {
  330. ret = vmw_cursor_update_bo(dev_priv, vps->bo,
  331. plane->state->crtc_w,
  332. plane->state->crtc_h,
  333. hotspot_x, hotspot_y);
  334. } else {
  335. vmw_cursor_update_position(dev_priv, false, 0, 0);
  336. return;
  337. }
  338. if (!ret) {
  339. du->cursor_x = plane->state->crtc_x + du->set_gui_x;
  340. du->cursor_y = plane->state->crtc_y + du->set_gui_y;
  341. vmw_cursor_update_position(dev_priv, true,
  342. du->cursor_x + hotspot_x,
  343. du->cursor_y + hotspot_y);
  344. du->core_hotspot_x = hotspot_x - du->hotspot_x;
  345. du->core_hotspot_y = hotspot_y - du->hotspot_y;
  346. } else {
  347. DRM_ERROR("Failed to update cursor image\n");
  348. }
  349. }
  350. /**
  351. * vmw_du_primary_plane_atomic_check - check if the new state is okay
  352. *
  353. * @plane: display plane
  354. * @state: info on the new plane state, including the FB
  355. *
  356. * Check if the new state is settable given the current state. Other
  357. * than what the atomic helper checks, we care about crtc fitting
  358. * the FB and maintaining one active framebuffer.
  359. *
  360. * Returns 0 on success
  361. */
  362. int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
  363. struct drm_plane_state *state)
  364. {
  365. struct drm_crtc_state *crtc_state = NULL;
  366. struct drm_framebuffer *new_fb = state->fb;
  367. int ret;
  368. if (state->crtc)
  369. crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);
  370. ret = drm_atomic_helper_check_plane_state(state, crtc_state,
  371. DRM_PLANE_HELPER_NO_SCALING,
  372. DRM_PLANE_HELPER_NO_SCALING,
  373. false, true);
  374. if (!ret && new_fb) {
  375. struct drm_crtc *crtc = state->crtc;
  376. struct vmw_connector_state *vcs;
  377. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  378. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  379. struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
  380. vcs = vmw_connector_state_to_vcs(du->connector.state);
  381. /* Only one active implicit framebuffer at a time. */
  382. mutex_lock(&dev_priv->global_kms_state_mutex);
  383. if (vcs->is_implicit && dev_priv->implicit_fb &&
  384. !(dev_priv->num_implicit == 1 && du->active_implicit)
  385. && dev_priv->implicit_fb != vfb) {
  386. DRM_ERROR("Multiple implicit framebuffers "
  387. "not supported.\n");
  388. ret = -EINVAL;
  389. }
  390. mutex_unlock(&dev_priv->global_kms_state_mutex);
  391. }
  392. return ret;
  393. }
  394. /**
  395. * vmw_du_cursor_plane_atomic_check - check if the new state is okay
  396. *
  397. * @plane: cursor plane
  398. * @state: info on the new plane state
  399. *
  400. * This is a chance to fail if the new cursor state does not fit
  401. * our requirements.
  402. *
  403. * Returns 0 on success
  404. */
  405. int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
  406. struct drm_plane_state *new_state)
  407. {
  408. int ret = 0;
  409. struct vmw_surface *surface = NULL;
  410. struct drm_framebuffer *fb = new_state->fb;
  411. struct drm_rect src = drm_plane_state_src(new_state);
  412. struct drm_rect dest = drm_plane_state_dest(new_state);
  413. /* Turning off */
  414. if (!fb)
  415. return ret;
  416. ret = drm_plane_helper_check_update(plane, new_state->crtc, fb,
  417. &src, &dest,
  418. DRM_MODE_ROTATE_0,
  419. DRM_PLANE_HELPER_NO_SCALING,
  420. DRM_PLANE_HELPER_NO_SCALING,
  421. true, true, &new_state->visible);
  422. if (!ret)
  423. return ret;
  424. /* A lot of the code assumes this */
  425. if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
  426. DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
  427. new_state->crtc_w, new_state->crtc_h);
  428. ret = -EINVAL;
  429. }
  430. if (!vmw_framebuffer_to_vfb(fb)->bo)
  431. surface = vmw_framebuffer_to_vfbs(fb)->surface;
  432. if (surface && !surface->snooper.image) {
  433. DRM_ERROR("surface not suitable for cursor\n");
  434. ret = -EINVAL;
  435. }
  436. return ret;
  437. }
  438. int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
  439. struct drm_crtc_state *new_state)
  440. {
  441. struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
  442. int connector_mask = 1 << drm_connector_index(&du->connector);
  443. bool has_primary = new_state->plane_mask &
  444. BIT(drm_plane_index(crtc->primary));
  445. /* We always want to have an active plane with an active CRTC */
  446. if (has_primary != new_state->enable)
  447. return -EINVAL;
  448. if (new_state->connector_mask != connector_mask &&
  449. new_state->connector_mask != 0) {
  450. DRM_ERROR("Invalid connectors configuration\n");
  451. return -EINVAL;
  452. }
  453. /*
  454. * Our virtual device does not have a dot clock, so use the logical
  455. * clock value as the dot clock.
  456. */
  457. if (new_state->mode.crtc_clock == 0)
  458. new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
  459. return 0;
  460. }
  461. void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
  462. struct drm_crtc_state *old_crtc_state)
  463. {
  464. }
  465. void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
  466. struct drm_crtc_state *old_crtc_state)
  467. {
  468. struct drm_pending_vblank_event *event = crtc->state->event;
  469. if (event) {
  470. crtc->state->event = NULL;
  471. spin_lock_irq(&crtc->dev->event_lock);
  472. drm_crtc_send_vblank_event(crtc, event);
  473. spin_unlock_irq(&crtc->dev->event_lock);
  474. }
  475. }
  476. /**
  477. * vmw_du_crtc_duplicate_state - duplicate crtc state
  478. * @crtc: DRM crtc
  479. *
  480. * Allocates and returns a copy of the crtc state (both common and
  481. * vmw-specific) for the specified crtc.
  482. *
  483. * Returns: The newly allocated crtc state, or NULL on failure.
  484. */
  485. struct drm_crtc_state *
  486. vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
  487. {
  488. struct drm_crtc_state *state;
  489. struct vmw_crtc_state *vcs;
  490. if (WARN_ON(!crtc->state))
  491. return NULL;
  492. vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
  493. if (!vcs)
  494. return NULL;
  495. state = &vcs->base;
  496. __drm_atomic_helper_crtc_duplicate_state(crtc, state);
  497. return state;
  498. }
  499. /**
  500. * vmw_du_crtc_reset - creates a blank vmw crtc state
  501. * @crtc: DRM crtc
  502. *
  503. * Resets the atomic state for @crtc by freeing the state pointer (which
  504. * might be NULL, e.g. at driver load time) and allocating a new empty state
  505. * object.
  506. */
  507. void vmw_du_crtc_reset(struct drm_crtc *crtc)
  508. {
  509. struct vmw_crtc_state *vcs;
  510. if (crtc->state) {
  511. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  512. kfree(vmw_crtc_state_to_vcs(crtc->state));
  513. }
  514. vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
  515. if (!vcs) {
  516. DRM_ERROR("Cannot allocate vmw_crtc_state\n");
  517. return;
  518. }
  519. crtc->state = &vcs->base;
  520. crtc->state->crtc = crtc;
  521. }
  522. /**
  523. * vmw_du_crtc_destroy_state - destroy crtc state
  524. * @crtc: DRM crtc
  525. * @state: state object to destroy
  526. *
  527. * Destroys the crtc state (both common and vmw-specific) for the
  528. * specified plane.
  529. */
  530. void
  531. vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
  532. struct drm_crtc_state *state)
  533. {
  534. drm_atomic_helper_crtc_destroy_state(crtc, state);
  535. }
  536. /**
  537. * vmw_du_plane_duplicate_state - duplicate plane state
  538. * @plane: drm plane
  539. *
  540. * Allocates and returns a copy of the plane state (both common and
  541. * vmw-specific) for the specified plane.
  542. *
  543. * Returns: The newly allocated plane state, or NULL on failure.
  544. */
  545. struct drm_plane_state *
  546. vmw_du_plane_duplicate_state(struct drm_plane *plane)
  547. {
  548. struct drm_plane_state *state;
  549. struct vmw_plane_state *vps;
  550. vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
  551. if (!vps)
  552. return NULL;
  553. vps->pinned = 0;
  554. vps->cpp = 0;
  555. /* Each ref counted resource needs to be acquired again */
  556. if (vps->surf)
  557. (void) vmw_surface_reference(vps->surf);
  558. if (vps->bo)
  559. (void) vmw_bo_reference(vps->bo);
  560. state = &vps->base;
  561. __drm_atomic_helper_plane_duplicate_state(plane, state);
  562. return state;
  563. }
  564. /**
  565. * vmw_du_plane_reset - creates a blank vmw plane state
  566. * @plane: drm plane
  567. *
  568. * Resets the atomic state for @plane by freeing the state pointer (which might
  569. * be NULL, e.g. at driver load time) and allocating a new empty state object.
  570. */
  571. void vmw_du_plane_reset(struct drm_plane *plane)
  572. {
  573. struct vmw_plane_state *vps;
  574. if (plane->state)
  575. vmw_du_plane_destroy_state(plane, plane->state);
  576. vps = kzalloc(sizeof(*vps), GFP_KERNEL);
  577. if (!vps) {
  578. DRM_ERROR("Cannot allocate vmw_plane_state\n");
  579. return;
  580. }
  581. plane->state = &vps->base;
  582. plane->state->plane = plane;
  583. plane->state->rotation = DRM_MODE_ROTATE_0;
  584. }
  585. /**
  586. * vmw_du_plane_destroy_state - destroy plane state
  587. * @plane: DRM plane
  588. * @state: state object to destroy
  589. *
  590. * Destroys the plane state (both common and vmw-specific) for the
  591. * specified plane.
  592. */
  593. void
  594. vmw_du_plane_destroy_state(struct drm_plane *plane,
  595. struct drm_plane_state *state)
  596. {
  597. struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
  598. /* Should have been freed by cleanup_fb */
  599. if (vps->surf)
  600. vmw_surface_unreference(&vps->surf);
  601. if (vps->bo)
  602. vmw_bo_unreference(&vps->bo);
  603. drm_atomic_helper_plane_destroy_state(plane, state);
  604. }
  605. /**
  606. * vmw_du_connector_duplicate_state - duplicate connector state
  607. * @connector: DRM connector
  608. *
  609. * Allocates and returns a copy of the connector state (both common and
  610. * vmw-specific) for the specified connector.
  611. *
  612. * Returns: The newly allocated connector state, or NULL on failure.
  613. */
  614. struct drm_connector_state *
  615. vmw_du_connector_duplicate_state(struct drm_connector *connector)
  616. {
  617. struct drm_connector_state *state;
  618. struct vmw_connector_state *vcs;
  619. if (WARN_ON(!connector->state))
  620. return NULL;
  621. vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
  622. if (!vcs)
  623. return NULL;
  624. state = &vcs->base;
  625. __drm_atomic_helper_connector_duplicate_state(connector, state);
  626. return state;
  627. }
  628. /**
  629. * vmw_du_connector_reset - creates a blank vmw connector state
  630. * @connector: DRM connector
  631. *
  632. * Resets the atomic state for @connector by freeing the state pointer (which
  633. * might be NULL, e.g. at driver load time) and allocating a new empty state
  634. * object.
  635. */
  636. void vmw_du_connector_reset(struct drm_connector *connector)
  637. {
  638. struct vmw_connector_state *vcs;
  639. if (connector->state) {
  640. __drm_atomic_helper_connector_destroy_state(connector->state);
  641. kfree(vmw_connector_state_to_vcs(connector->state));
  642. }
  643. vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
  644. if (!vcs) {
  645. DRM_ERROR("Cannot allocate vmw_connector_state\n");
  646. return;
  647. }
  648. __drm_atomic_helper_connector_reset(connector, &vcs->base);
  649. }
  650. /**
  651. * vmw_du_connector_destroy_state - destroy connector state
  652. * @connector: DRM connector
  653. * @state: state object to destroy
  654. *
  655. * Destroys the connector state (both common and vmw-specific) for the
  656. * specified plane.
  657. */
  658. void
  659. vmw_du_connector_destroy_state(struct drm_connector *connector,
  660. struct drm_connector_state *state)
  661. {
  662. drm_atomic_helper_connector_destroy_state(connector, state);
  663. }
  664. /*
  665. * Generic framebuffer code
  666. */
  667. /*
  668. * Surface framebuffer code
  669. */
  670. static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
  671. {
  672. struct vmw_framebuffer_surface *vfbs =
  673. vmw_framebuffer_to_vfbs(framebuffer);
  674. drm_framebuffer_cleanup(framebuffer);
  675. vmw_surface_unreference(&vfbs->surface);
  676. if (vfbs->base.user_obj)
  677. ttm_base_object_unref(&vfbs->base.user_obj);
  678. kfree(vfbs);
  679. }
  680. static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
  681. struct drm_file *file_priv,
  682. unsigned flags, unsigned color,
  683. struct drm_clip_rect *clips,
  684. unsigned num_clips)
  685. {
  686. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  687. struct vmw_framebuffer_surface *vfbs =
  688. vmw_framebuffer_to_vfbs(framebuffer);
  689. struct drm_clip_rect norect;
  690. int ret, inc = 1;
  691. /* Legacy Display Unit does not support 3D */
  692. if (dev_priv->active_display_unit == vmw_du_legacy)
  693. return -EINVAL;
  694. drm_modeset_lock_all(dev_priv->dev);
  695. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  696. if (unlikely(ret != 0)) {
  697. drm_modeset_unlock_all(dev_priv->dev);
  698. return ret;
  699. }
  700. if (!num_clips) {
  701. num_clips = 1;
  702. clips = &norect;
  703. norect.x1 = norect.y1 = 0;
  704. norect.x2 = framebuffer->width;
  705. norect.y2 = framebuffer->height;
  706. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  707. num_clips /= 2;
  708. inc = 2; /* skip source rects */
  709. }
  710. if (dev_priv->active_display_unit == vmw_du_screen_object)
  711. ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
  712. clips, NULL, NULL, 0, 0,
  713. num_clips, inc, NULL, NULL);
  714. else
  715. ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
  716. clips, NULL, NULL, 0, 0,
  717. num_clips, inc, NULL, NULL);
  718. vmw_fifo_flush(dev_priv, false);
  719. ttm_read_unlock(&dev_priv->reservation_sem);
  720. drm_modeset_unlock_all(dev_priv->dev);
  721. return 0;
  722. }
  723. /**
  724. * vmw_kms_readback - Perform a readback from the screen system to
  725. * a buffer-object backed framebuffer.
  726. *
  727. * @dev_priv: Pointer to the device private structure.
  728. * @file_priv: Pointer to a struct drm_file identifying the caller.
  729. * Must be set to NULL if @user_fence_rep is NULL.
  730. * @vfb: Pointer to the buffer-object backed framebuffer.
  731. * @user_fence_rep: User-space provided structure for fence information.
  732. * Must be set to non-NULL if @file_priv is non-NULL.
  733. * @vclips: Array of clip rects.
  734. * @num_clips: Number of clip rects in @vclips.
  735. *
  736. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  737. * interrupted.
  738. */
  739. int vmw_kms_readback(struct vmw_private *dev_priv,
  740. struct drm_file *file_priv,
  741. struct vmw_framebuffer *vfb,
  742. struct drm_vmw_fence_rep __user *user_fence_rep,
  743. struct drm_vmw_rect *vclips,
  744. uint32_t num_clips)
  745. {
  746. switch (dev_priv->active_display_unit) {
  747. case vmw_du_screen_object:
  748. return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
  749. user_fence_rep, vclips, num_clips,
  750. NULL);
  751. case vmw_du_screen_target:
  752. return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
  753. user_fence_rep, NULL, vclips, num_clips,
  754. 1, false, true, NULL);
  755. default:
  756. WARN_ONCE(true,
  757. "Readback called with invalid display system.\n");
  758. }
  759. return -ENOSYS;
  760. }
  761. static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
  762. .destroy = vmw_framebuffer_surface_destroy,
  763. .dirty = vmw_framebuffer_surface_dirty,
  764. };
  765. static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
  766. struct vmw_surface *surface,
  767. struct vmw_framebuffer **out,
  768. const struct drm_mode_fb_cmd2
  769. *mode_cmd,
  770. bool is_bo_proxy)
  771. {
  772. struct drm_device *dev = dev_priv->dev;
  773. struct vmw_framebuffer_surface *vfbs;
  774. enum SVGA3dSurfaceFormat format;
  775. int ret;
  776. struct drm_format_name_buf format_name;
  777. /* 3D is only supported on HWv8 and newer hosts */
  778. if (dev_priv->active_display_unit == vmw_du_legacy)
  779. return -ENOSYS;
  780. /*
  781. * Sanity checks.
  782. */
  783. /* Surface must be marked as a scanout. */
  784. if (unlikely(!surface->scanout))
  785. return -EINVAL;
  786. if (unlikely(surface->mip_levels[0] != 1 ||
  787. surface->num_sizes != 1 ||
  788. surface->base_size.width < mode_cmd->width ||
  789. surface->base_size.height < mode_cmd->height ||
  790. surface->base_size.depth != 1)) {
  791. DRM_ERROR("Incompatible surface dimensions "
  792. "for requested mode.\n");
  793. return -EINVAL;
  794. }
  795. switch (mode_cmd->pixel_format) {
  796. case DRM_FORMAT_ARGB8888:
  797. format = SVGA3D_A8R8G8B8;
  798. break;
  799. case DRM_FORMAT_XRGB8888:
  800. format = SVGA3D_X8R8G8B8;
  801. break;
  802. case DRM_FORMAT_RGB565:
  803. format = SVGA3D_R5G6B5;
  804. break;
  805. case DRM_FORMAT_XRGB1555:
  806. format = SVGA3D_A1R5G5B5;
  807. break;
  808. default:
  809. DRM_ERROR("Invalid pixel format: %s\n",
  810. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  811. return -EINVAL;
  812. }
  813. /*
  814. * For DX, surface format validation is done when surface->scanout
  815. * is set.
  816. */
  817. if (!dev_priv->has_dx && format != surface->format) {
  818. DRM_ERROR("Invalid surface format for requested mode.\n");
  819. return -EINVAL;
  820. }
  821. vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
  822. if (!vfbs) {
  823. ret = -ENOMEM;
  824. goto out_err1;
  825. }
  826. drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
  827. vfbs->surface = vmw_surface_reference(surface);
  828. vfbs->base.user_handle = mode_cmd->handles[0];
  829. vfbs->is_bo_proxy = is_bo_proxy;
  830. *out = &vfbs->base;
  831. ret = drm_framebuffer_init(dev, &vfbs->base.base,
  832. &vmw_framebuffer_surface_funcs);
  833. if (ret)
  834. goto out_err2;
  835. return 0;
  836. out_err2:
  837. vmw_surface_unreference(&surface);
  838. kfree(vfbs);
  839. out_err1:
  840. return ret;
  841. }
  842. /*
  843. * Buffer-object framebuffer code
  844. */
  845. static void vmw_framebuffer_bo_destroy(struct drm_framebuffer *framebuffer)
  846. {
  847. struct vmw_framebuffer_bo *vfbd =
  848. vmw_framebuffer_to_vfbd(framebuffer);
  849. drm_framebuffer_cleanup(framebuffer);
  850. vmw_bo_unreference(&vfbd->buffer);
  851. if (vfbd->base.user_obj)
  852. ttm_base_object_unref(&vfbd->base.user_obj);
  853. kfree(vfbd);
  854. }
  855. static int vmw_framebuffer_bo_dirty(struct drm_framebuffer *framebuffer,
  856. struct drm_file *file_priv,
  857. unsigned int flags, unsigned int color,
  858. struct drm_clip_rect *clips,
  859. unsigned int num_clips)
  860. {
  861. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  862. struct vmw_framebuffer_bo *vfbd =
  863. vmw_framebuffer_to_vfbd(framebuffer);
  864. struct drm_clip_rect norect;
  865. int ret, increment = 1;
  866. drm_modeset_lock_all(dev_priv->dev);
  867. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  868. if (unlikely(ret != 0)) {
  869. drm_modeset_unlock_all(dev_priv->dev);
  870. return ret;
  871. }
  872. if (!num_clips) {
  873. num_clips = 1;
  874. clips = &norect;
  875. norect.x1 = norect.y1 = 0;
  876. norect.x2 = framebuffer->width;
  877. norect.y2 = framebuffer->height;
  878. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  879. num_clips /= 2;
  880. increment = 2;
  881. }
  882. switch (dev_priv->active_display_unit) {
  883. case vmw_du_screen_target:
  884. ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
  885. clips, NULL, num_clips, increment,
  886. true, true, NULL);
  887. break;
  888. case vmw_du_screen_object:
  889. ret = vmw_kms_sou_do_bo_dirty(dev_priv, &vfbd->base,
  890. clips, NULL, num_clips,
  891. increment, true, NULL, NULL);
  892. break;
  893. case vmw_du_legacy:
  894. ret = vmw_kms_ldu_do_bo_dirty(dev_priv, &vfbd->base, 0, 0,
  895. clips, num_clips, increment);
  896. break;
  897. default:
  898. ret = -EINVAL;
  899. WARN_ONCE(true, "Dirty called with invalid display system.\n");
  900. break;
  901. }
  902. vmw_fifo_flush(dev_priv, false);
  903. ttm_read_unlock(&dev_priv->reservation_sem);
  904. drm_modeset_unlock_all(dev_priv->dev);
  905. return ret;
  906. }
  907. static const struct drm_framebuffer_funcs vmw_framebuffer_bo_funcs = {
  908. .destroy = vmw_framebuffer_bo_destroy,
  909. .dirty = vmw_framebuffer_bo_dirty,
  910. };
  911. /**
  912. * Pin the bofer in a location suitable for access by the
  913. * display system.
  914. */
  915. static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
  916. {
  917. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  918. struct vmw_buffer_object *buf;
  919. struct ttm_placement *placement;
  920. int ret;
  921. buf = vfb->bo ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  922. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  923. if (!buf)
  924. return 0;
  925. switch (dev_priv->active_display_unit) {
  926. case vmw_du_legacy:
  927. vmw_overlay_pause_all(dev_priv);
  928. ret = vmw_bo_pin_in_start_of_vram(dev_priv, buf, false);
  929. vmw_overlay_resume_all(dev_priv);
  930. break;
  931. case vmw_du_screen_object:
  932. case vmw_du_screen_target:
  933. if (vfb->bo) {
  934. if (dev_priv->capabilities & SVGA_CAP_3D) {
  935. /*
  936. * Use surface DMA to get content to
  937. * sreen target surface.
  938. */
  939. placement = &vmw_vram_gmr_placement;
  940. } else {
  941. /* Use CPU blit. */
  942. placement = &vmw_sys_placement;
  943. }
  944. } else {
  945. /* Use surface / image update */
  946. placement = &vmw_mob_placement;
  947. }
  948. return vmw_bo_pin_in_placement(dev_priv, buf, placement, false);
  949. default:
  950. return -EINVAL;
  951. }
  952. return ret;
  953. }
  954. static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
  955. {
  956. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  957. struct vmw_buffer_object *buf;
  958. buf = vfb->bo ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  959. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  960. if (WARN_ON(!buf))
  961. return 0;
  962. return vmw_bo_unpin(dev_priv, buf, false);
  963. }
  964. /**
  965. * vmw_create_bo_proxy - create a proxy surface for the buffer object
  966. *
  967. * @dev: DRM device
  968. * @mode_cmd: parameters for the new surface
  969. * @bo_mob: MOB backing the buffer object
  970. * @srf_out: newly created surface
  971. *
  972. * When the content FB is a buffer object, we create a surface as a proxy to the
  973. * same buffer. This way we can do a surface copy rather than a surface DMA.
  974. * This is a more efficient approach
  975. *
  976. * RETURNS:
  977. * 0 on success, error code otherwise
  978. */
  979. static int vmw_create_bo_proxy(struct drm_device *dev,
  980. const struct drm_mode_fb_cmd2 *mode_cmd,
  981. struct vmw_buffer_object *bo_mob,
  982. struct vmw_surface **srf_out)
  983. {
  984. uint32_t format;
  985. struct drm_vmw_size content_base_size = {0};
  986. struct vmw_resource *res;
  987. unsigned int bytes_pp;
  988. struct drm_format_name_buf format_name;
  989. int ret;
  990. switch (mode_cmd->pixel_format) {
  991. case DRM_FORMAT_ARGB8888:
  992. case DRM_FORMAT_XRGB8888:
  993. format = SVGA3D_X8R8G8B8;
  994. bytes_pp = 4;
  995. break;
  996. case DRM_FORMAT_RGB565:
  997. case DRM_FORMAT_XRGB1555:
  998. format = SVGA3D_R5G6B5;
  999. bytes_pp = 2;
  1000. break;
  1001. case 8:
  1002. format = SVGA3D_P8;
  1003. bytes_pp = 1;
  1004. break;
  1005. default:
  1006. DRM_ERROR("Invalid framebuffer format %s\n",
  1007. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  1008. return -EINVAL;
  1009. }
  1010. content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
  1011. content_base_size.height = mode_cmd->height;
  1012. content_base_size.depth = 1;
  1013. ret = vmw_surface_gb_priv_define(dev,
  1014. 0, /* kernel visible only */
  1015. 0, /* flags */
  1016. format,
  1017. true, /* can be a scanout buffer */
  1018. 1, /* num of mip levels */
  1019. 0,
  1020. 0,
  1021. content_base_size,
  1022. SVGA3D_MS_PATTERN_NONE,
  1023. SVGA3D_MS_QUALITY_NONE,
  1024. srf_out);
  1025. if (ret) {
  1026. DRM_ERROR("Failed to allocate proxy content buffer\n");
  1027. return ret;
  1028. }
  1029. res = &(*srf_out)->res;
  1030. /* Reserve and switch the backing mob. */
  1031. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  1032. (void) vmw_resource_reserve(res, false, true);
  1033. vmw_bo_unreference(&res->backup);
  1034. res->backup = vmw_bo_reference(bo_mob);
  1035. res->backup_offset = 0;
  1036. vmw_resource_unreserve(res, false, NULL, 0);
  1037. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  1038. return 0;
  1039. }
  1040. static int vmw_kms_new_framebuffer_bo(struct vmw_private *dev_priv,
  1041. struct vmw_buffer_object *bo,
  1042. struct vmw_framebuffer **out,
  1043. const struct drm_mode_fb_cmd2
  1044. *mode_cmd)
  1045. {
  1046. struct drm_device *dev = dev_priv->dev;
  1047. struct vmw_framebuffer_bo *vfbd;
  1048. unsigned int requested_size;
  1049. struct drm_format_name_buf format_name;
  1050. int ret;
  1051. requested_size = mode_cmd->height * mode_cmd->pitches[0];
  1052. if (unlikely(requested_size > bo->base.num_pages * PAGE_SIZE)) {
  1053. DRM_ERROR("Screen buffer object size is too small "
  1054. "for requested mode.\n");
  1055. return -EINVAL;
  1056. }
  1057. /* Limited framebuffer color depth support for screen objects */
  1058. if (dev_priv->active_display_unit == vmw_du_screen_object) {
  1059. switch (mode_cmd->pixel_format) {
  1060. case DRM_FORMAT_XRGB8888:
  1061. case DRM_FORMAT_ARGB8888:
  1062. break;
  1063. case DRM_FORMAT_XRGB1555:
  1064. case DRM_FORMAT_RGB565:
  1065. break;
  1066. default:
  1067. DRM_ERROR("Invalid pixel format: %s\n",
  1068. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  1069. return -EINVAL;
  1070. }
  1071. }
  1072. vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
  1073. if (!vfbd) {
  1074. ret = -ENOMEM;
  1075. goto out_err1;
  1076. }
  1077. drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
  1078. vfbd->base.bo = true;
  1079. vfbd->buffer = vmw_bo_reference(bo);
  1080. vfbd->base.user_handle = mode_cmd->handles[0];
  1081. *out = &vfbd->base;
  1082. ret = drm_framebuffer_init(dev, &vfbd->base.base,
  1083. &vmw_framebuffer_bo_funcs);
  1084. if (ret)
  1085. goto out_err2;
  1086. return 0;
  1087. out_err2:
  1088. vmw_bo_unreference(&bo);
  1089. kfree(vfbd);
  1090. out_err1:
  1091. return ret;
  1092. }
  1093. /**
  1094. * vmw_kms_srf_ok - check if a surface can be created
  1095. *
  1096. * @width: requested width
  1097. * @height: requested height
  1098. *
  1099. * Surfaces need to be less than texture size
  1100. */
  1101. static bool
  1102. vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
  1103. {
  1104. if (width > dev_priv->texture_max_width ||
  1105. height > dev_priv->texture_max_height)
  1106. return false;
  1107. return true;
  1108. }
  1109. /**
  1110. * vmw_kms_new_framebuffer - Create a new framebuffer.
  1111. *
  1112. * @dev_priv: Pointer to device private struct.
  1113. * @bo: Pointer to buffer object to wrap the kms framebuffer around.
  1114. * Either @bo or @surface must be NULL.
  1115. * @surface: Pointer to a surface to wrap the kms framebuffer around.
  1116. * Either @bo or @surface must be NULL.
  1117. * @only_2d: No presents will occur to this buffer object based framebuffer.
  1118. * This helps the code to do some important optimizations.
  1119. * @mode_cmd: Frame-buffer metadata.
  1120. */
  1121. struct vmw_framebuffer *
  1122. vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
  1123. struct vmw_buffer_object *bo,
  1124. struct vmw_surface *surface,
  1125. bool only_2d,
  1126. const struct drm_mode_fb_cmd2 *mode_cmd)
  1127. {
  1128. struct vmw_framebuffer *vfb = NULL;
  1129. bool is_bo_proxy = false;
  1130. int ret;
  1131. /*
  1132. * We cannot use the SurfaceDMA command in an non-accelerated VM,
  1133. * therefore, wrap the buffer object in a surface so we can use the
  1134. * SurfaceCopy command.
  1135. */
  1136. if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
  1137. bo && only_2d &&
  1138. mode_cmd->width > 64 && /* Don't create a proxy for cursor */
  1139. dev_priv->active_display_unit == vmw_du_screen_target) {
  1140. ret = vmw_create_bo_proxy(dev_priv->dev, mode_cmd,
  1141. bo, &surface);
  1142. if (ret)
  1143. return ERR_PTR(ret);
  1144. is_bo_proxy = true;
  1145. }
  1146. /* Create the new framebuffer depending one what we have */
  1147. if (surface) {
  1148. ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
  1149. mode_cmd,
  1150. is_bo_proxy);
  1151. /*
  1152. * vmw_create_bo_proxy() adds a reference that is no longer
  1153. * needed
  1154. */
  1155. if (is_bo_proxy)
  1156. vmw_surface_unreference(&surface);
  1157. } else if (bo) {
  1158. ret = vmw_kms_new_framebuffer_bo(dev_priv, bo, &vfb,
  1159. mode_cmd);
  1160. } else {
  1161. BUG();
  1162. }
  1163. if (ret)
  1164. return ERR_PTR(ret);
  1165. vfb->pin = vmw_framebuffer_pin;
  1166. vfb->unpin = vmw_framebuffer_unpin;
  1167. return vfb;
  1168. }
  1169. /*
  1170. * Generic Kernel modesetting functions
  1171. */
  1172. static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
  1173. struct drm_file *file_priv,
  1174. const struct drm_mode_fb_cmd2 *mode_cmd)
  1175. {
  1176. struct vmw_private *dev_priv = vmw_priv(dev);
  1177. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  1178. struct vmw_framebuffer *vfb = NULL;
  1179. struct vmw_surface *surface = NULL;
  1180. struct vmw_buffer_object *bo = NULL;
  1181. struct ttm_base_object *user_obj;
  1182. int ret;
  1183. /*
  1184. * Take a reference on the user object of the resource
  1185. * backing the kms fb. This ensures that user-space handle
  1186. * lookups on that resource will always work as long as
  1187. * it's registered with a kms framebuffer. This is important,
  1188. * since vmw_execbuf_process identifies resources in the
  1189. * command stream using user-space handles.
  1190. */
  1191. user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
  1192. if (unlikely(user_obj == NULL)) {
  1193. DRM_ERROR("Could not locate requested kms frame buffer.\n");
  1194. return ERR_PTR(-ENOENT);
  1195. }
  1196. /**
  1197. * End conditioned code.
  1198. */
  1199. /* returns either a bo or surface */
  1200. ret = vmw_user_lookup_handle(dev_priv, tfile,
  1201. mode_cmd->handles[0],
  1202. &surface, &bo);
  1203. if (ret)
  1204. goto err_out;
  1205. if (!bo &&
  1206. !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
  1207. DRM_ERROR("Surface size cannot exceed %dx%d",
  1208. dev_priv->texture_max_width,
  1209. dev_priv->texture_max_height);
  1210. goto err_out;
  1211. }
  1212. vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
  1213. !(dev_priv->capabilities & SVGA_CAP_3D),
  1214. mode_cmd);
  1215. if (IS_ERR(vfb)) {
  1216. ret = PTR_ERR(vfb);
  1217. goto err_out;
  1218. }
  1219. err_out:
  1220. /* vmw_user_lookup_handle takes one ref so does new_fb */
  1221. if (bo)
  1222. vmw_bo_unreference(&bo);
  1223. if (surface)
  1224. vmw_surface_unreference(&surface);
  1225. if (ret) {
  1226. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  1227. ttm_base_object_unref(&user_obj);
  1228. return ERR_PTR(ret);
  1229. } else
  1230. vfb->user_obj = user_obj;
  1231. return &vfb->base;
  1232. }
  1233. /**
  1234. * vmw_kms_check_display_memory - Validates display memory required for a
  1235. * topology
  1236. * @dev: DRM device
  1237. * @num_rects: number of drm_rect in rects
  1238. * @rects: array of drm_rect representing the topology to validate indexed by
  1239. * crtc index.
  1240. *
  1241. * Returns:
  1242. * 0 on success otherwise negative error code
  1243. */
  1244. static int vmw_kms_check_display_memory(struct drm_device *dev,
  1245. uint32_t num_rects,
  1246. struct drm_rect *rects)
  1247. {
  1248. struct vmw_private *dev_priv = vmw_priv(dev);
  1249. struct drm_mode_config *mode_config = &dev->mode_config;
  1250. struct drm_rect bounding_box = {0};
  1251. u64 total_pixels = 0, pixel_mem, bb_mem;
  1252. int i;
  1253. for (i = 0; i < num_rects; i++) {
  1254. /*
  1255. * Currently this check is limiting the topology within max
  1256. * texture/screentarget size. This should change in future when
  1257. * user-space support multiple fb with topology.
  1258. */
  1259. if (rects[i].x1 < 0 || rects[i].y1 < 0 ||
  1260. rects[i].x2 > mode_config->max_width ||
  1261. rects[i].y2 > mode_config->max_height) {
  1262. DRM_ERROR("Invalid GUI layout.\n");
  1263. return -EINVAL;
  1264. }
  1265. /* Bounding box upper left is at (0,0). */
  1266. if (rects[i].x2 > bounding_box.x2)
  1267. bounding_box.x2 = rects[i].x2;
  1268. if (rects[i].y2 > bounding_box.y2)
  1269. bounding_box.y2 = rects[i].y2;
  1270. total_pixels += (u64) drm_rect_width(&rects[i]) *
  1271. (u64) drm_rect_height(&rects[i]);
  1272. }
  1273. /* Virtual svga device primary limits are always in 32-bpp. */
  1274. pixel_mem = total_pixels * 4;
  1275. /*
  1276. * For HV10 and below prim_bb_mem is vram size. When
  1277. * SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM is not present vram size is
  1278. * limit on primary bounding box
  1279. */
  1280. if (pixel_mem > dev_priv->prim_bb_mem) {
  1281. DRM_ERROR("Combined output size too large.\n");
  1282. return -EINVAL;
  1283. }
  1284. /* SVGA_CAP_NO_BB_RESTRICTION is available for STDU only. */
  1285. if (dev_priv->active_display_unit != vmw_du_screen_target ||
  1286. !(dev_priv->capabilities & SVGA_CAP_NO_BB_RESTRICTION)) {
  1287. bb_mem = (u64) bounding_box.x2 * bounding_box.y2 * 4;
  1288. if (bb_mem > dev_priv->prim_bb_mem) {
  1289. DRM_ERROR("Topology is beyond supported limits.\n");
  1290. return -EINVAL;
  1291. }
  1292. }
  1293. return 0;
  1294. }
  1295. /**
  1296. * vmw_kms_check_topology - Validates topology in drm_atomic_state
  1297. * @dev: DRM device
  1298. * @state: the driver state object
  1299. *
  1300. * Returns:
  1301. * 0 on success otherwise negative error code
  1302. */
  1303. static int vmw_kms_check_topology(struct drm_device *dev,
  1304. struct drm_atomic_state *state)
  1305. {
  1306. struct vmw_private *dev_priv = vmw_priv(dev);
  1307. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  1308. struct drm_rect *rects;
  1309. struct drm_crtc *crtc;
  1310. uint32_t i;
  1311. int ret = 0;
  1312. rects = kcalloc(dev->mode_config.num_crtc, sizeof(struct drm_rect),
  1313. GFP_KERNEL);
  1314. if (!rects)
  1315. return -ENOMEM;
  1316. mutex_lock(&dev_priv->requested_layout_mutex);
  1317. drm_for_each_crtc(crtc, dev) {
  1318. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  1319. struct drm_crtc_state *crtc_state = crtc->state;
  1320. i = drm_crtc_index(crtc);
  1321. if (crtc_state && crtc_state->enable) {
  1322. rects[i].x1 = du->gui_x;
  1323. rects[i].y1 = du->gui_y;
  1324. rects[i].x2 = du->gui_x + crtc_state->mode.hdisplay;
  1325. rects[i].y2 = du->gui_y + crtc_state->mode.vdisplay;
  1326. }
  1327. }
  1328. /* Determine change to topology due to new atomic state */
  1329. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
  1330. new_crtc_state, i) {
  1331. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  1332. struct drm_connector *connector;
  1333. struct drm_connector_state *conn_state;
  1334. struct vmw_connector_state *vmw_conn_state;
  1335. if (!new_crtc_state->enable && old_crtc_state->enable) {
  1336. rects[i].x1 = 0;
  1337. rects[i].y1 = 0;
  1338. rects[i].x2 = 0;
  1339. rects[i].y2 = 0;
  1340. continue;
  1341. }
  1342. if (!du->pref_active) {
  1343. ret = -EINVAL;
  1344. goto clean;
  1345. }
  1346. /*
  1347. * For vmwgfx each crtc has only one connector attached and it
  1348. * is not changed so don't really need to check the
  1349. * crtc->connector_mask and iterate over it.
  1350. */
  1351. connector = &du->connector;
  1352. conn_state = drm_atomic_get_connector_state(state, connector);
  1353. if (IS_ERR(conn_state)) {
  1354. ret = PTR_ERR(conn_state);
  1355. goto clean;
  1356. }
  1357. vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
  1358. vmw_conn_state->gui_x = du->gui_x;
  1359. vmw_conn_state->gui_y = du->gui_y;
  1360. rects[i].x1 = du->gui_x;
  1361. rects[i].y1 = du->gui_y;
  1362. rects[i].x2 = du->gui_x + new_crtc_state->mode.hdisplay;
  1363. rects[i].y2 = du->gui_y + new_crtc_state->mode.vdisplay;
  1364. }
  1365. ret = vmw_kms_check_display_memory(dev, dev->mode_config.num_crtc,
  1366. rects);
  1367. clean:
  1368. mutex_unlock(&dev_priv->requested_layout_mutex);
  1369. kfree(rects);
  1370. return ret;
  1371. }
  1372. /**
  1373. * vmw_kms_atomic_check_modeset- validate state object for modeset changes
  1374. *
  1375. * @dev: DRM device
  1376. * @state: the driver state object
  1377. *
  1378. * This is a simple wrapper around drm_atomic_helper_check_modeset() for
  1379. * us to assign a value to mode->crtc_clock so that
  1380. * drm_calc_timestamping_constants() won't throw an error message
  1381. *
  1382. * Returns:
  1383. * Zero for success or -errno
  1384. */
  1385. static int
  1386. vmw_kms_atomic_check_modeset(struct drm_device *dev,
  1387. struct drm_atomic_state *state)
  1388. {
  1389. struct drm_crtc *crtc;
  1390. struct drm_crtc_state *crtc_state;
  1391. bool need_modeset = false;
  1392. int i, ret;
  1393. ret = drm_atomic_helper_check(dev, state);
  1394. if (ret)
  1395. return ret;
  1396. if (!state->allow_modeset)
  1397. return ret;
  1398. /*
  1399. * Legacy path do not set allow_modeset properly like
  1400. * @drm_atomic_helper_update_plane, This will result in unnecessary call
  1401. * to vmw_kms_check_topology. So extra set of check.
  1402. */
  1403. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  1404. if (drm_atomic_crtc_needs_modeset(crtc_state))
  1405. need_modeset = true;
  1406. }
  1407. if (need_modeset)
  1408. return vmw_kms_check_topology(dev, state);
  1409. return ret;
  1410. }
  1411. static const struct drm_mode_config_funcs vmw_kms_funcs = {
  1412. .fb_create = vmw_kms_fb_create,
  1413. .atomic_check = vmw_kms_atomic_check_modeset,
  1414. .atomic_commit = drm_atomic_helper_commit,
  1415. };
  1416. static int vmw_kms_generic_present(struct vmw_private *dev_priv,
  1417. struct drm_file *file_priv,
  1418. struct vmw_framebuffer *vfb,
  1419. struct vmw_surface *surface,
  1420. uint32_t sid,
  1421. int32_t destX, int32_t destY,
  1422. struct drm_vmw_rect *clips,
  1423. uint32_t num_clips)
  1424. {
  1425. return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
  1426. &surface->res, destX, destY,
  1427. num_clips, 1, NULL, NULL);
  1428. }
  1429. int vmw_kms_present(struct vmw_private *dev_priv,
  1430. struct drm_file *file_priv,
  1431. struct vmw_framebuffer *vfb,
  1432. struct vmw_surface *surface,
  1433. uint32_t sid,
  1434. int32_t destX, int32_t destY,
  1435. struct drm_vmw_rect *clips,
  1436. uint32_t num_clips)
  1437. {
  1438. int ret;
  1439. switch (dev_priv->active_display_unit) {
  1440. case vmw_du_screen_target:
  1441. ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
  1442. &surface->res, destX, destY,
  1443. num_clips, 1, NULL, NULL);
  1444. break;
  1445. case vmw_du_screen_object:
  1446. ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
  1447. sid, destX, destY, clips,
  1448. num_clips);
  1449. break;
  1450. default:
  1451. WARN_ONCE(true,
  1452. "Present called with invalid display system.\n");
  1453. ret = -ENOSYS;
  1454. break;
  1455. }
  1456. if (ret)
  1457. return ret;
  1458. vmw_fifo_flush(dev_priv, false);
  1459. return 0;
  1460. }
  1461. static void
  1462. vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
  1463. {
  1464. if (dev_priv->hotplug_mode_update_property)
  1465. return;
  1466. dev_priv->hotplug_mode_update_property =
  1467. drm_property_create_range(dev_priv->dev,
  1468. DRM_MODE_PROP_IMMUTABLE,
  1469. "hotplug_mode_update", 0, 1);
  1470. if (!dev_priv->hotplug_mode_update_property)
  1471. return;
  1472. }
  1473. int vmw_kms_init(struct vmw_private *dev_priv)
  1474. {
  1475. struct drm_device *dev = dev_priv->dev;
  1476. int ret;
  1477. drm_mode_config_init(dev);
  1478. dev->mode_config.funcs = &vmw_kms_funcs;
  1479. dev->mode_config.min_width = 1;
  1480. dev->mode_config.min_height = 1;
  1481. dev->mode_config.max_width = dev_priv->texture_max_width;
  1482. dev->mode_config.max_height = dev_priv->texture_max_height;
  1483. drm_mode_create_suggested_offset_properties(dev);
  1484. vmw_kms_create_hotplug_mode_update_property(dev_priv);
  1485. ret = vmw_kms_stdu_init_display(dev_priv);
  1486. if (ret) {
  1487. ret = vmw_kms_sou_init_display(dev_priv);
  1488. if (ret) /* Fallback */
  1489. ret = vmw_kms_ldu_init_display(dev_priv);
  1490. }
  1491. return ret;
  1492. }
  1493. int vmw_kms_close(struct vmw_private *dev_priv)
  1494. {
  1495. int ret = 0;
  1496. /*
  1497. * Docs says we should take the lock before calling this function
  1498. * but since it destroys encoders and our destructor calls
  1499. * drm_encoder_cleanup which takes the lock we deadlock.
  1500. */
  1501. drm_mode_config_cleanup(dev_priv->dev);
  1502. if (dev_priv->active_display_unit == vmw_du_legacy)
  1503. ret = vmw_kms_ldu_close_display(dev_priv);
  1504. return ret;
  1505. }
  1506. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  1507. struct drm_file *file_priv)
  1508. {
  1509. struct drm_vmw_cursor_bypass_arg *arg = data;
  1510. struct vmw_display_unit *du;
  1511. struct drm_crtc *crtc;
  1512. int ret = 0;
  1513. mutex_lock(&dev->mode_config.mutex);
  1514. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  1515. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1516. du = vmw_crtc_to_du(crtc);
  1517. du->hotspot_x = arg->xhot;
  1518. du->hotspot_y = arg->yhot;
  1519. }
  1520. mutex_unlock(&dev->mode_config.mutex);
  1521. return 0;
  1522. }
  1523. crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
  1524. if (!crtc) {
  1525. ret = -ENOENT;
  1526. goto out;
  1527. }
  1528. du = vmw_crtc_to_du(crtc);
  1529. du->hotspot_x = arg->xhot;
  1530. du->hotspot_y = arg->yhot;
  1531. out:
  1532. mutex_unlock(&dev->mode_config.mutex);
  1533. return ret;
  1534. }
  1535. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  1536. unsigned width, unsigned height, unsigned pitch,
  1537. unsigned bpp, unsigned depth)
  1538. {
  1539. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1540. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  1541. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1542. vmw_mmio_write(pitch, vmw_priv->mmio_virt +
  1543. SVGA_FIFO_PITCHLOCK);
  1544. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  1545. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  1546. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  1547. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  1548. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  1549. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  1550. return -EINVAL;
  1551. }
  1552. return 0;
  1553. }
  1554. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  1555. {
  1556. struct vmw_vga_topology_state *save;
  1557. uint32_t i;
  1558. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  1559. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  1560. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  1561. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1562. vmw_priv->vga_pitchlock =
  1563. vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
  1564. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1565. vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
  1566. SVGA_FIFO_PITCHLOCK);
  1567. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1568. return 0;
  1569. vmw_priv->num_displays = vmw_read(vmw_priv,
  1570. SVGA_REG_NUM_GUEST_DISPLAYS);
  1571. if (vmw_priv->num_displays == 0)
  1572. vmw_priv->num_displays = 1;
  1573. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1574. save = &vmw_priv->vga_save[i];
  1575. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1576. save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
  1577. save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
  1578. save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
  1579. save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
  1580. save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
  1581. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1582. if (i == 0 && vmw_priv->num_displays == 1 &&
  1583. save->width == 0 && save->height == 0) {
  1584. /*
  1585. * It should be fairly safe to assume that these
  1586. * values are uninitialized.
  1587. */
  1588. save->width = vmw_priv->vga_width - save->pos_x;
  1589. save->height = vmw_priv->vga_height - save->pos_y;
  1590. }
  1591. }
  1592. return 0;
  1593. }
  1594. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  1595. {
  1596. struct vmw_vga_topology_state *save;
  1597. uint32_t i;
  1598. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  1599. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  1600. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  1601. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1602. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
  1603. vmw_priv->vga_pitchlock);
  1604. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1605. vmw_mmio_write(vmw_priv->vga_pitchlock,
  1606. vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  1607. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1608. return 0;
  1609. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1610. save = &vmw_priv->vga_save[i];
  1611. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1612. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
  1613. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
  1614. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
  1615. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
  1616. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
  1617. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1618. }
  1619. return 0;
  1620. }
  1621. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  1622. uint32_t pitch,
  1623. uint32_t height)
  1624. {
  1625. return ((u64) pitch * (u64) height) < (u64)
  1626. ((dev_priv->active_display_unit == vmw_du_screen_target) ?
  1627. dev_priv->prim_bb_mem : dev_priv->vram_size);
  1628. }
  1629. /**
  1630. * Function called by DRM code called with vbl_lock held.
  1631. */
  1632. u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
  1633. {
  1634. return 0;
  1635. }
  1636. /**
  1637. * Function called by DRM code called with vbl_lock held.
  1638. */
  1639. int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
  1640. {
  1641. return -EINVAL;
  1642. }
  1643. /**
  1644. * Function called by DRM code called with vbl_lock held.
  1645. */
  1646. void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
  1647. {
  1648. }
  1649. /**
  1650. * vmw_du_update_layout - Update the display unit with topology from resolution
  1651. * plugin and generate DRM uevent
  1652. * @dev_priv: device private
  1653. * @num_rects: number of drm_rect in rects
  1654. * @rects: toplogy to update
  1655. */
  1656. static int vmw_du_update_layout(struct vmw_private *dev_priv,
  1657. unsigned int num_rects, struct drm_rect *rects)
  1658. {
  1659. struct drm_device *dev = dev_priv->dev;
  1660. struct vmw_display_unit *du;
  1661. struct drm_connector *con;
  1662. struct drm_connector_list_iter conn_iter;
  1663. /*
  1664. * Currently only gui_x/y is protected with requested_layout_mutex.
  1665. */
  1666. mutex_lock(&dev_priv->requested_layout_mutex);
  1667. drm_connector_list_iter_begin(dev, &conn_iter);
  1668. drm_for_each_connector_iter(con, &conn_iter) {
  1669. du = vmw_connector_to_du(con);
  1670. if (num_rects > du->unit) {
  1671. du->pref_width = drm_rect_width(&rects[du->unit]);
  1672. du->pref_height = drm_rect_height(&rects[du->unit]);
  1673. du->pref_active = true;
  1674. du->gui_x = rects[du->unit].x1;
  1675. du->gui_y = rects[du->unit].y1;
  1676. } else {
  1677. du->pref_width = 800;
  1678. du->pref_height = 600;
  1679. du->pref_active = false;
  1680. du->gui_x = 0;
  1681. du->gui_y = 0;
  1682. }
  1683. }
  1684. drm_connector_list_iter_end(&conn_iter);
  1685. mutex_unlock(&dev_priv->requested_layout_mutex);
  1686. mutex_lock(&dev->mode_config.mutex);
  1687. list_for_each_entry(con, &dev->mode_config.connector_list, head) {
  1688. du = vmw_connector_to_du(con);
  1689. if (num_rects > du->unit) {
  1690. drm_object_property_set_value
  1691. (&con->base, dev->mode_config.suggested_x_property,
  1692. du->gui_x);
  1693. drm_object_property_set_value
  1694. (&con->base, dev->mode_config.suggested_y_property,
  1695. du->gui_y);
  1696. } else {
  1697. drm_object_property_set_value
  1698. (&con->base, dev->mode_config.suggested_x_property,
  1699. 0);
  1700. drm_object_property_set_value
  1701. (&con->base, dev->mode_config.suggested_y_property,
  1702. 0);
  1703. }
  1704. con->status = vmw_du_connector_detect(con, true);
  1705. }
  1706. mutex_unlock(&dev->mode_config.mutex);
  1707. drm_sysfs_hotplug_event(dev);
  1708. return 0;
  1709. }
  1710. int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  1711. u16 *r, u16 *g, u16 *b,
  1712. uint32_t size,
  1713. struct drm_modeset_acquire_ctx *ctx)
  1714. {
  1715. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  1716. int i;
  1717. for (i = 0; i < size; i++) {
  1718. DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
  1719. r[i], g[i], b[i]);
  1720. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
  1721. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
  1722. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
  1723. }
  1724. return 0;
  1725. }
  1726. int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
  1727. {
  1728. return 0;
  1729. }
  1730. enum drm_connector_status
  1731. vmw_du_connector_detect(struct drm_connector *connector, bool force)
  1732. {
  1733. uint32_t num_displays;
  1734. struct drm_device *dev = connector->dev;
  1735. struct vmw_private *dev_priv = vmw_priv(dev);
  1736. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1737. num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
  1738. return ((vmw_connector_to_du(connector)->unit < num_displays &&
  1739. du->pref_active) ?
  1740. connector_status_connected : connector_status_disconnected);
  1741. }
  1742. static struct drm_display_mode vmw_kms_connector_builtin[] = {
  1743. /* 640x480@60Hz */
  1744. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  1745. 752, 800, 0, 480, 489, 492, 525, 0,
  1746. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1747. /* 800x600@60Hz */
  1748. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  1749. 968, 1056, 0, 600, 601, 605, 628, 0,
  1750. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1751. /* 1024x768@60Hz */
  1752. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  1753. 1184, 1344, 0, 768, 771, 777, 806, 0,
  1754. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1755. /* 1152x864@75Hz */
  1756. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  1757. 1344, 1600, 0, 864, 865, 868, 900, 0,
  1758. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1759. /* 1280x768@60Hz */
  1760. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  1761. 1472, 1664, 0, 768, 771, 778, 798, 0,
  1762. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1763. /* 1280x800@60Hz */
  1764. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  1765. 1480, 1680, 0, 800, 803, 809, 831, 0,
  1766. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1767. /* 1280x960@60Hz */
  1768. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  1769. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  1770. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1771. /* 1280x1024@60Hz */
  1772. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  1773. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  1774. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1775. /* 1360x768@60Hz */
  1776. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  1777. 1536, 1792, 0, 768, 771, 777, 795, 0,
  1778. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1779. /* 1440x1050@60Hz */
  1780. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  1781. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  1782. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1783. /* 1440x900@60Hz */
  1784. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  1785. 1672, 1904, 0, 900, 903, 909, 934, 0,
  1786. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1787. /* 1600x1200@60Hz */
  1788. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  1789. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  1790. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1791. /* 1680x1050@60Hz */
  1792. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  1793. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  1794. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1795. /* 1792x1344@60Hz */
  1796. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  1797. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  1798. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1799. /* 1853x1392@60Hz */
  1800. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  1801. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  1802. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1803. /* 1920x1200@60Hz */
  1804. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  1805. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  1806. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1807. /* 1920x1440@60Hz */
  1808. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  1809. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  1810. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1811. /* 2560x1600@60Hz */
  1812. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  1813. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  1814. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1815. /* Terminate */
  1816. { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
  1817. };
  1818. /**
  1819. * vmw_guess_mode_timing - Provide fake timings for a
  1820. * 60Hz vrefresh mode.
  1821. *
  1822. * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
  1823. * members filled in.
  1824. */
  1825. void vmw_guess_mode_timing(struct drm_display_mode *mode)
  1826. {
  1827. mode->hsync_start = mode->hdisplay + 50;
  1828. mode->hsync_end = mode->hsync_start + 50;
  1829. mode->htotal = mode->hsync_end + 50;
  1830. mode->vsync_start = mode->vdisplay + 50;
  1831. mode->vsync_end = mode->vsync_start + 50;
  1832. mode->vtotal = mode->vsync_end + 50;
  1833. mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
  1834. mode->vrefresh = drm_mode_vrefresh(mode);
  1835. }
  1836. int vmw_du_connector_fill_modes(struct drm_connector *connector,
  1837. uint32_t max_width, uint32_t max_height)
  1838. {
  1839. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1840. struct drm_device *dev = connector->dev;
  1841. struct vmw_private *dev_priv = vmw_priv(dev);
  1842. struct drm_display_mode *mode = NULL;
  1843. struct drm_display_mode *bmode;
  1844. struct drm_display_mode prefmode = { DRM_MODE("preferred",
  1845. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  1846. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1847. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  1848. };
  1849. int i;
  1850. u32 assumed_bpp = 4;
  1851. if (dev_priv->assume_16bpp)
  1852. assumed_bpp = 2;
  1853. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1854. max_width = min(max_width, dev_priv->stdu_max_width);
  1855. max_width = min(max_width, dev_priv->texture_max_width);
  1856. max_height = min(max_height, dev_priv->stdu_max_height);
  1857. max_height = min(max_height, dev_priv->texture_max_height);
  1858. }
  1859. /* Add preferred mode */
  1860. mode = drm_mode_duplicate(dev, &prefmode);
  1861. if (!mode)
  1862. return 0;
  1863. mode->hdisplay = du->pref_width;
  1864. mode->vdisplay = du->pref_height;
  1865. vmw_guess_mode_timing(mode);
  1866. if (vmw_kms_validate_mode_vram(dev_priv,
  1867. mode->hdisplay * assumed_bpp,
  1868. mode->vdisplay)) {
  1869. drm_mode_probed_add(connector, mode);
  1870. } else {
  1871. drm_mode_destroy(dev, mode);
  1872. mode = NULL;
  1873. }
  1874. if (du->pref_mode) {
  1875. list_del_init(&du->pref_mode->head);
  1876. drm_mode_destroy(dev, du->pref_mode);
  1877. }
  1878. /* mode might be null here, this is intended */
  1879. du->pref_mode = mode;
  1880. for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
  1881. bmode = &vmw_kms_connector_builtin[i];
  1882. if (bmode->hdisplay > max_width ||
  1883. bmode->vdisplay > max_height)
  1884. continue;
  1885. if (!vmw_kms_validate_mode_vram(dev_priv,
  1886. bmode->hdisplay * assumed_bpp,
  1887. bmode->vdisplay))
  1888. continue;
  1889. mode = drm_mode_duplicate(dev, bmode);
  1890. if (!mode)
  1891. return 0;
  1892. mode->vrefresh = drm_mode_vrefresh(mode);
  1893. drm_mode_probed_add(connector, mode);
  1894. }
  1895. drm_mode_connector_list_update(connector);
  1896. /* Move the prefered mode first, help apps pick the right mode. */
  1897. drm_mode_sort(&connector->modes);
  1898. return 1;
  1899. }
  1900. int vmw_du_connector_set_property(struct drm_connector *connector,
  1901. struct drm_property *property,
  1902. uint64_t val)
  1903. {
  1904. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1905. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1906. if (property == dev_priv->implicit_placement_property)
  1907. du->is_implicit = val;
  1908. return 0;
  1909. }
  1910. /**
  1911. * vmw_du_connector_atomic_set_property - Atomic version of get property
  1912. *
  1913. * @crtc - crtc the property is associated with
  1914. *
  1915. * Returns:
  1916. * Zero on success, negative errno on failure.
  1917. */
  1918. int
  1919. vmw_du_connector_atomic_set_property(struct drm_connector *connector,
  1920. struct drm_connector_state *state,
  1921. struct drm_property *property,
  1922. uint64_t val)
  1923. {
  1924. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1925. struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
  1926. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1927. if (property == dev_priv->implicit_placement_property) {
  1928. vcs->is_implicit = val;
  1929. /*
  1930. * We should really be doing a drm_atomic_commit() to
  1931. * commit the new state, but since this doesn't cause
  1932. * an immedate state change, this is probably ok
  1933. */
  1934. du->is_implicit = vcs->is_implicit;
  1935. } else {
  1936. return -EINVAL;
  1937. }
  1938. return 0;
  1939. }
  1940. /**
  1941. * vmw_du_connector_atomic_get_property - Atomic version of get property
  1942. *
  1943. * @connector - connector the property is associated with
  1944. *
  1945. * Returns:
  1946. * Zero on success, negative errno on failure.
  1947. */
  1948. int
  1949. vmw_du_connector_atomic_get_property(struct drm_connector *connector,
  1950. const struct drm_connector_state *state,
  1951. struct drm_property *property,
  1952. uint64_t *val)
  1953. {
  1954. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1955. struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
  1956. if (property == dev_priv->implicit_placement_property)
  1957. *val = vcs->is_implicit;
  1958. else {
  1959. DRM_ERROR("Invalid Property %s\n", property->name);
  1960. return -EINVAL;
  1961. }
  1962. return 0;
  1963. }
  1964. /**
  1965. * vmw_kms_update_layout_ioctl - Handler for DRM_VMW_UPDATE_LAYOUT ioctl
  1966. * @dev: drm device for the ioctl
  1967. * @data: data pointer for the ioctl
  1968. * @file_priv: drm file for the ioctl call
  1969. *
  1970. * Update preferred topology of display unit as per ioctl request. The topology
  1971. * is expressed as array of drm_vmw_rect.
  1972. * e.g.
  1973. * [0 0 640 480] [640 0 800 600] [0 480 640 480]
  1974. *
  1975. * NOTE:
  1976. * The x and y offset (upper left) in drm_vmw_rect cannot be less than 0. Beside
  1977. * device limit on topology, x + w and y + h (lower right) cannot be greater
  1978. * than INT_MAX. So topology beyond these limits will return with error.
  1979. *
  1980. * Returns:
  1981. * Zero on success, negative errno on failure.
  1982. */
  1983. int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
  1984. struct drm_file *file_priv)
  1985. {
  1986. struct vmw_private *dev_priv = vmw_priv(dev);
  1987. struct drm_vmw_update_layout_arg *arg =
  1988. (struct drm_vmw_update_layout_arg *)data;
  1989. void __user *user_rects;
  1990. struct drm_vmw_rect *rects;
  1991. struct drm_rect *drm_rects;
  1992. unsigned rects_size;
  1993. int ret, i;
  1994. if (!arg->num_outputs) {
  1995. struct drm_rect def_rect = {0, 0, 800, 600};
  1996. vmw_du_update_layout(dev_priv, 1, &def_rect);
  1997. return 0;
  1998. }
  1999. rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
  2000. rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
  2001. GFP_KERNEL);
  2002. if (unlikely(!rects))
  2003. return -ENOMEM;
  2004. user_rects = (void __user *)(unsigned long)arg->rects;
  2005. ret = copy_from_user(rects, user_rects, rects_size);
  2006. if (unlikely(ret != 0)) {
  2007. DRM_ERROR("Failed to get rects.\n");
  2008. ret = -EFAULT;
  2009. goto out_free;
  2010. }
  2011. drm_rects = (struct drm_rect *)rects;
  2012. for (i = 0; i < arg->num_outputs; i++) {
  2013. struct drm_vmw_rect curr_rect;
  2014. /* Verify user-space for overflow as kernel use drm_rect */
  2015. if ((rects[i].x + rects[i].w > INT_MAX) ||
  2016. (rects[i].y + rects[i].h > INT_MAX)) {
  2017. ret = -ERANGE;
  2018. goto out_free;
  2019. }
  2020. curr_rect = rects[i];
  2021. drm_rects[i].x1 = curr_rect.x;
  2022. drm_rects[i].y1 = curr_rect.y;
  2023. drm_rects[i].x2 = curr_rect.x + curr_rect.w;
  2024. drm_rects[i].y2 = curr_rect.y + curr_rect.h;
  2025. }
  2026. ret = vmw_kms_check_display_memory(dev, arg->num_outputs, drm_rects);
  2027. if (ret == 0)
  2028. vmw_du_update_layout(dev_priv, arg->num_outputs, drm_rects);
  2029. out_free:
  2030. kfree(rects);
  2031. return ret;
  2032. }
  2033. /**
  2034. * vmw_kms_helper_dirty - Helper to build commands and perform actions based
  2035. * on a set of cliprects and a set of display units.
  2036. *
  2037. * @dev_priv: Pointer to a device private structure.
  2038. * @framebuffer: Pointer to the framebuffer on which to perform the actions.
  2039. * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
  2040. * Cliprects are given in framebuffer coordinates.
  2041. * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
  2042. * be NULL. Cliprects are given in source coordinates.
  2043. * @dest_x: X coordinate offset for the crtc / destination clip rects.
  2044. * @dest_y: Y coordinate offset for the crtc / destination clip rects.
  2045. * @num_clips: Number of cliprects in the @clips or @vclips array.
  2046. * @increment: Integer with which to increment the clip counter when looping.
  2047. * Used to skip a predetermined number of clip rects.
  2048. * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
  2049. */
  2050. int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
  2051. struct vmw_framebuffer *framebuffer,
  2052. const struct drm_clip_rect *clips,
  2053. const struct drm_vmw_rect *vclips,
  2054. s32 dest_x, s32 dest_y,
  2055. int num_clips,
  2056. int increment,
  2057. struct vmw_kms_dirty *dirty)
  2058. {
  2059. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  2060. struct drm_crtc *crtc;
  2061. u32 num_units = 0;
  2062. u32 i, k;
  2063. dirty->dev_priv = dev_priv;
  2064. /* If crtc is passed, no need to iterate over other display units */
  2065. if (dirty->crtc) {
  2066. units[num_units++] = vmw_crtc_to_du(dirty->crtc);
  2067. } else {
  2068. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
  2069. head) {
  2070. struct drm_plane *plane = crtc->primary;
  2071. if (plane->state->fb == &framebuffer->base)
  2072. units[num_units++] = vmw_crtc_to_du(crtc);
  2073. }
  2074. }
  2075. for (k = 0; k < num_units; k++) {
  2076. struct vmw_display_unit *unit = units[k];
  2077. s32 crtc_x = unit->crtc.x;
  2078. s32 crtc_y = unit->crtc.y;
  2079. s32 crtc_width = unit->crtc.mode.hdisplay;
  2080. s32 crtc_height = unit->crtc.mode.vdisplay;
  2081. const struct drm_clip_rect *clips_ptr = clips;
  2082. const struct drm_vmw_rect *vclips_ptr = vclips;
  2083. dirty->unit = unit;
  2084. if (dirty->fifo_reserve_size > 0) {
  2085. dirty->cmd = vmw_fifo_reserve(dev_priv,
  2086. dirty->fifo_reserve_size);
  2087. if (!dirty->cmd) {
  2088. DRM_ERROR("Couldn't reserve fifo space "
  2089. "for dirty blits.\n");
  2090. return -ENOMEM;
  2091. }
  2092. memset(dirty->cmd, 0, dirty->fifo_reserve_size);
  2093. }
  2094. dirty->num_hits = 0;
  2095. for (i = 0; i < num_clips; i++, clips_ptr += increment,
  2096. vclips_ptr += increment) {
  2097. s32 clip_left;
  2098. s32 clip_top;
  2099. /*
  2100. * Select clip array type. Note that integer type
  2101. * in @clips is unsigned short, whereas in @vclips
  2102. * it's 32-bit.
  2103. */
  2104. if (clips) {
  2105. dirty->fb_x = (s32) clips_ptr->x1;
  2106. dirty->fb_y = (s32) clips_ptr->y1;
  2107. dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
  2108. crtc_x;
  2109. dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
  2110. crtc_y;
  2111. } else {
  2112. dirty->fb_x = vclips_ptr->x;
  2113. dirty->fb_y = vclips_ptr->y;
  2114. dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
  2115. dest_x - crtc_x;
  2116. dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
  2117. dest_y - crtc_y;
  2118. }
  2119. dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
  2120. dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
  2121. /* Skip this clip if it's outside the crtc region */
  2122. if (dirty->unit_x1 >= crtc_width ||
  2123. dirty->unit_y1 >= crtc_height ||
  2124. dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
  2125. continue;
  2126. /* Clip right and bottom to crtc limits */
  2127. dirty->unit_x2 = min_t(s32, dirty->unit_x2,
  2128. crtc_width);
  2129. dirty->unit_y2 = min_t(s32, dirty->unit_y2,
  2130. crtc_height);
  2131. /* Clip left and top to crtc limits */
  2132. clip_left = min_t(s32, dirty->unit_x1, 0);
  2133. clip_top = min_t(s32, dirty->unit_y1, 0);
  2134. dirty->unit_x1 -= clip_left;
  2135. dirty->unit_y1 -= clip_top;
  2136. dirty->fb_x -= clip_left;
  2137. dirty->fb_y -= clip_top;
  2138. dirty->clip(dirty);
  2139. }
  2140. dirty->fifo_commit(dirty);
  2141. }
  2142. return 0;
  2143. }
  2144. /**
  2145. * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
  2146. * command submission.
  2147. *
  2148. * @dev_priv. Pointer to a device private structure.
  2149. * @buf: The buffer object
  2150. * @interruptible: Whether to perform waits as interruptible.
  2151. * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
  2152. * The buffer will be validated as a GMR. Already pinned buffers will not be
  2153. * validated.
  2154. *
  2155. * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
  2156. * interrupted by a signal.
  2157. */
  2158. int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
  2159. struct vmw_buffer_object *buf,
  2160. bool interruptible,
  2161. bool validate_as_mob,
  2162. bool for_cpu_blit)
  2163. {
  2164. struct ttm_operation_ctx ctx = {
  2165. .interruptible = interruptible,
  2166. .no_wait_gpu = false};
  2167. struct ttm_buffer_object *bo = &buf->base;
  2168. int ret;
  2169. ttm_bo_reserve(bo, false, false, NULL);
  2170. if (for_cpu_blit)
  2171. ret = ttm_bo_validate(bo, &vmw_nonfixed_placement, &ctx);
  2172. else
  2173. ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
  2174. validate_as_mob);
  2175. if (ret)
  2176. ttm_bo_unreserve(bo);
  2177. return ret;
  2178. }
  2179. /**
  2180. * vmw_kms_helper_buffer_revert - Undo the actions of
  2181. * vmw_kms_helper_buffer_prepare.
  2182. *
  2183. * @res: Pointer to the buffer object.
  2184. *
  2185. * Helper to be used if an error forces the caller to undo the actions of
  2186. * vmw_kms_helper_buffer_prepare.
  2187. */
  2188. void vmw_kms_helper_buffer_revert(struct vmw_buffer_object *buf)
  2189. {
  2190. if (buf)
  2191. ttm_bo_unreserve(&buf->base);
  2192. }
  2193. /**
  2194. * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
  2195. * kms command submission.
  2196. *
  2197. * @dev_priv: Pointer to a device private structure.
  2198. * @file_priv: Pointer to a struct drm_file representing the caller's
  2199. * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
  2200. * if non-NULL, @user_fence_rep must be non-NULL.
  2201. * @buf: The buffer object.
  2202. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  2203. * ref-counted fence pointer is returned here.
  2204. * @user_fence_rep: Optional pointer to a user-space provided struct
  2205. * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
  2206. * function copies fence data to user-space in a fail-safe manner.
  2207. */
  2208. void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
  2209. struct drm_file *file_priv,
  2210. struct vmw_buffer_object *buf,
  2211. struct vmw_fence_obj **out_fence,
  2212. struct drm_vmw_fence_rep __user *
  2213. user_fence_rep)
  2214. {
  2215. struct vmw_fence_obj *fence;
  2216. uint32_t handle;
  2217. int ret;
  2218. ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
  2219. file_priv ? &handle : NULL);
  2220. if (buf)
  2221. vmw_bo_fence_single(&buf->base, fence);
  2222. if (file_priv)
  2223. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
  2224. ret, user_fence_rep, fence,
  2225. handle, -1, NULL);
  2226. if (out_fence)
  2227. *out_fence = fence;
  2228. else
  2229. vmw_fence_obj_unreference(&fence);
  2230. vmw_kms_helper_buffer_revert(buf);
  2231. }
  2232. /**
  2233. * vmw_kms_helper_resource_revert - Undo the actions of
  2234. * vmw_kms_helper_resource_prepare.
  2235. *
  2236. * @res: Pointer to the resource. Typically a surface.
  2237. *
  2238. * Helper to be used if an error forces the caller to undo the actions of
  2239. * vmw_kms_helper_resource_prepare.
  2240. */
  2241. void vmw_kms_helper_resource_revert(struct vmw_validation_ctx *ctx)
  2242. {
  2243. struct vmw_resource *res = ctx->res;
  2244. vmw_kms_helper_buffer_revert(ctx->buf);
  2245. vmw_bo_unreference(&ctx->buf);
  2246. vmw_resource_unreserve(res, false, NULL, 0);
  2247. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2248. }
  2249. /**
  2250. * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
  2251. * command submission.
  2252. *
  2253. * @res: Pointer to the resource. Typically a surface.
  2254. * @interruptible: Whether to perform waits as interruptible.
  2255. *
  2256. * Reserves and validates also the backup buffer if a guest-backed resource.
  2257. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  2258. * interrupted by a signal.
  2259. */
  2260. int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
  2261. bool interruptible,
  2262. struct vmw_validation_ctx *ctx)
  2263. {
  2264. int ret = 0;
  2265. ctx->buf = NULL;
  2266. ctx->res = res;
  2267. if (interruptible)
  2268. ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
  2269. else
  2270. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  2271. if (unlikely(ret != 0))
  2272. return -ERESTARTSYS;
  2273. ret = vmw_resource_reserve(res, interruptible, false);
  2274. if (ret)
  2275. goto out_unlock;
  2276. if (res->backup) {
  2277. ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
  2278. interruptible,
  2279. res->dev_priv->has_mob,
  2280. false);
  2281. if (ret)
  2282. goto out_unreserve;
  2283. ctx->buf = vmw_bo_reference(res->backup);
  2284. }
  2285. ret = vmw_resource_validate(res);
  2286. if (ret)
  2287. goto out_revert;
  2288. return 0;
  2289. out_revert:
  2290. vmw_kms_helper_buffer_revert(ctx->buf);
  2291. out_unreserve:
  2292. vmw_resource_unreserve(res, false, NULL, 0);
  2293. out_unlock:
  2294. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2295. return ret;
  2296. }
  2297. /**
  2298. * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
  2299. * kms command submission.
  2300. *
  2301. * @res: Pointer to the resource. Typically a surface.
  2302. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  2303. * ref-counted fence pointer is returned here.
  2304. */
  2305. void vmw_kms_helper_resource_finish(struct vmw_validation_ctx *ctx,
  2306. struct vmw_fence_obj **out_fence)
  2307. {
  2308. struct vmw_resource *res = ctx->res;
  2309. if (ctx->buf || out_fence)
  2310. vmw_kms_helper_buffer_finish(res->dev_priv, NULL, ctx->buf,
  2311. out_fence, NULL);
  2312. vmw_bo_unreference(&ctx->buf);
  2313. vmw_resource_unreserve(res, false, NULL, 0);
  2314. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2315. }
  2316. /**
  2317. * vmw_kms_update_proxy - Helper function to update a proxy surface from
  2318. * its backing MOB.
  2319. *
  2320. * @res: Pointer to the surface resource
  2321. * @clips: Clip rects in framebuffer (surface) space.
  2322. * @num_clips: Number of clips in @clips.
  2323. * @increment: Integer with which to increment the clip counter when looping.
  2324. * Used to skip a predetermined number of clip rects.
  2325. *
  2326. * This function makes sure the proxy surface is updated from its backing MOB
  2327. * using the region given by @clips. The surface resource @res and its backing
  2328. * MOB needs to be reserved and validated on call.
  2329. */
  2330. int vmw_kms_update_proxy(struct vmw_resource *res,
  2331. const struct drm_clip_rect *clips,
  2332. unsigned num_clips,
  2333. int increment)
  2334. {
  2335. struct vmw_private *dev_priv = res->dev_priv;
  2336. struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
  2337. struct {
  2338. SVGA3dCmdHeader header;
  2339. SVGA3dCmdUpdateGBImage body;
  2340. } *cmd;
  2341. SVGA3dBox *box;
  2342. size_t copy_size = 0;
  2343. int i;
  2344. if (!clips)
  2345. return 0;
  2346. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
  2347. if (!cmd) {
  2348. DRM_ERROR("Couldn't reserve fifo space for proxy surface "
  2349. "update.\n");
  2350. return -ENOMEM;
  2351. }
  2352. for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
  2353. box = &cmd->body.box;
  2354. cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
  2355. cmd->header.size = sizeof(cmd->body);
  2356. cmd->body.image.sid = res->id;
  2357. cmd->body.image.face = 0;
  2358. cmd->body.image.mipmap = 0;
  2359. if (clips->x1 > size->width || clips->x2 > size->width ||
  2360. clips->y1 > size->height || clips->y2 > size->height) {
  2361. DRM_ERROR("Invalid clips outsize of framebuffer.\n");
  2362. return -EINVAL;
  2363. }
  2364. box->x = clips->x1;
  2365. box->y = clips->y1;
  2366. box->z = 0;
  2367. box->w = clips->x2 - clips->x1;
  2368. box->h = clips->y2 - clips->y1;
  2369. box->d = 1;
  2370. copy_size += sizeof(*cmd);
  2371. }
  2372. vmw_fifo_commit(dev_priv, copy_size);
  2373. return 0;
  2374. }
  2375. int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
  2376. unsigned unit,
  2377. u32 max_width,
  2378. u32 max_height,
  2379. struct drm_connector **p_con,
  2380. struct drm_crtc **p_crtc,
  2381. struct drm_display_mode **p_mode)
  2382. {
  2383. struct drm_connector *con;
  2384. struct vmw_display_unit *du;
  2385. struct drm_display_mode *mode;
  2386. int i = 0;
  2387. int ret = 0;
  2388. mutex_lock(&dev_priv->dev->mode_config.mutex);
  2389. list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
  2390. head) {
  2391. if (i == unit)
  2392. break;
  2393. ++i;
  2394. }
  2395. if (i != unit) {
  2396. DRM_ERROR("Could not find initial display unit.\n");
  2397. ret = -EINVAL;
  2398. goto out_unlock;
  2399. }
  2400. if (list_empty(&con->modes))
  2401. (void) vmw_du_connector_fill_modes(con, max_width, max_height);
  2402. if (list_empty(&con->modes)) {
  2403. DRM_ERROR("Could not find initial display mode.\n");
  2404. ret = -EINVAL;
  2405. goto out_unlock;
  2406. }
  2407. du = vmw_connector_to_du(con);
  2408. *p_con = con;
  2409. *p_crtc = &du->crtc;
  2410. list_for_each_entry(mode, &con->modes, head) {
  2411. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  2412. break;
  2413. }
  2414. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  2415. *p_mode = mode;
  2416. else {
  2417. WARN_ONCE(true, "Could not find initial preferred mode.\n");
  2418. *p_mode = list_first_entry(&con->modes,
  2419. struct drm_display_mode,
  2420. head);
  2421. }
  2422. out_unlock:
  2423. mutex_unlock(&dev_priv->dev->mode_config.mutex);
  2424. return ret;
  2425. }
  2426. /**
  2427. * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
  2428. *
  2429. * @dev_priv: Pointer to a device private struct.
  2430. * @du: The display unit of the crtc.
  2431. */
  2432. void vmw_kms_del_active(struct vmw_private *dev_priv,
  2433. struct vmw_display_unit *du)
  2434. {
  2435. mutex_lock(&dev_priv->global_kms_state_mutex);
  2436. if (du->active_implicit) {
  2437. if (--(dev_priv->num_implicit) == 0)
  2438. dev_priv->implicit_fb = NULL;
  2439. du->active_implicit = false;
  2440. }
  2441. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2442. }
  2443. /**
  2444. * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
  2445. *
  2446. * @vmw_priv: Pointer to a device private struct.
  2447. * @du: The display unit of the crtc.
  2448. * @vfb: The implicit framebuffer
  2449. *
  2450. * Registers a binding to an implicit framebuffer.
  2451. */
  2452. void vmw_kms_add_active(struct vmw_private *dev_priv,
  2453. struct vmw_display_unit *du,
  2454. struct vmw_framebuffer *vfb)
  2455. {
  2456. mutex_lock(&dev_priv->global_kms_state_mutex);
  2457. WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
  2458. if (!du->active_implicit && du->is_implicit) {
  2459. dev_priv->implicit_fb = vfb;
  2460. du->active_implicit = true;
  2461. dev_priv->num_implicit++;
  2462. }
  2463. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2464. }
  2465. /**
  2466. * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
  2467. *
  2468. * @dev_priv: Pointer to device-private struct.
  2469. * @crtc: The crtc we want to flip.
  2470. *
  2471. * Returns true or false depending whether it's OK to flip this crtc
  2472. * based on the criterion that we must not have more than one implicit
  2473. * frame-buffer at any one time.
  2474. */
  2475. bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
  2476. struct drm_crtc *crtc)
  2477. {
  2478. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  2479. bool ret;
  2480. mutex_lock(&dev_priv->global_kms_state_mutex);
  2481. ret = !du->is_implicit || dev_priv->num_implicit == 1;
  2482. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2483. return ret;
  2484. }
  2485. /**
  2486. * vmw_kms_update_implicit_fb - Update the implicit fb.
  2487. *
  2488. * @dev_priv: Pointer to device-private struct.
  2489. * @crtc: The crtc the new implicit frame-buffer is bound to.
  2490. */
  2491. void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
  2492. struct drm_crtc *crtc)
  2493. {
  2494. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  2495. struct drm_plane *plane = crtc->primary;
  2496. struct vmw_framebuffer *vfb;
  2497. mutex_lock(&dev_priv->global_kms_state_mutex);
  2498. if (!du->is_implicit)
  2499. goto out_unlock;
  2500. vfb = vmw_framebuffer_to_vfb(plane->state->fb);
  2501. WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
  2502. dev_priv->implicit_fb != vfb);
  2503. dev_priv->implicit_fb = vfb;
  2504. out_unlock:
  2505. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2506. }
  2507. /**
  2508. * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
  2509. * property.
  2510. *
  2511. * @dev_priv: Pointer to a device private struct.
  2512. * @immutable: Whether the property is immutable.
  2513. *
  2514. * Sets up the implicit placement property unless it's already set up.
  2515. */
  2516. void
  2517. vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
  2518. bool immutable)
  2519. {
  2520. if (dev_priv->implicit_placement_property)
  2521. return;
  2522. dev_priv->implicit_placement_property =
  2523. drm_property_create_range(dev_priv->dev,
  2524. immutable ?
  2525. DRM_MODE_PROP_IMMUTABLE : 0,
  2526. "implicit_placement", 0, 1);
  2527. }
  2528. /**
  2529. * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
  2530. *
  2531. * @set: The configuration to set.
  2532. *
  2533. * The vmwgfx Xorg driver doesn't assign the mode::type member, which
  2534. * when drm_mode_set_crtcinfo is called as part of the configuration setting
  2535. * causes it to return incorrect crtc dimensions causing severe problems in
  2536. * the vmwgfx modesetting. So explicitly clear that member before calling
  2537. * into drm_atomic_helper_set_config.
  2538. */
  2539. int vmw_kms_set_config(struct drm_mode_set *set,
  2540. struct drm_modeset_acquire_ctx *ctx)
  2541. {
  2542. if (set && set->mode)
  2543. set->mode->type = 0;
  2544. return drm_atomic_helper_set_config(set, ctx);
  2545. }
  2546. /**
  2547. * vmw_kms_suspend - Save modesetting state and turn modesetting off.
  2548. *
  2549. * @dev: Pointer to the drm device
  2550. * Return: 0 on success. Negative error code on failure.
  2551. */
  2552. int vmw_kms_suspend(struct drm_device *dev)
  2553. {
  2554. struct vmw_private *dev_priv = vmw_priv(dev);
  2555. dev_priv->suspend_state = drm_atomic_helper_suspend(dev);
  2556. if (IS_ERR(dev_priv->suspend_state)) {
  2557. int ret = PTR_ERR(dev_priv->suspend_state);
  2558. DRM_ERROR("Failed kms suspend: %d\n", ret);
  2559. dev_priv->suspend_state = NULL;
  2560. return ret;
  2561. }
  2562. return 0;
  2563. }
  2564. /**
  2565. * vmw_kms_resume - Re-enable modesetting and restore state
  2566. *
  2567. * @dev: Pointer to the drm device
  2568. * Return: 0 on success. Negative error code on failure.
  2569. *
  2570. * State is resumed from a previous vmw_kms_suspend(). It's illegal
  2571. * to call this function without a previous vmw_kms_suspend().
  2572. */
  2573. int vmw_kms_resume(struct drm_device *dev)
  2574. {
  2575. struct vmw_private *dev_priv = vmw_priv(dev);
  2576. int ret;
  2577. if (WARN_ON(!dev_priv->suspend_state))
  2578. return 0;
  2579. ret = drm_atomic_helper_resume(dev, dev_priv->suspend_state);
  2580. dev_priv->suspend_state = NULL;
  2581. return ret;
  2582. }
  2583. /**
  2584. * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
  2585. *
  2586. * @dev: Pointer to the drm device
  2587. */
  2588. void vmw_kms_lost_device(struct drm_device *dev)
  2589. {
  2590. drm_atomic_helper_shutdown(dev);
  2591. }