vmwgfx_kms.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931
  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. srf_out);
  1023. if (ret) {
  1024. DRM_ERROR("Failed to allocate proxy content buffer\n");
  1025. return ret;
  1026. }
  1027. res = &(*srf_out)->res;
  1028. /* Reserve and switch the backing mob. */
  1029. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  1030. (void) vmw_resource_reserve(res, false, true);
  1031. vmw_bo_unreference(&res->backup);
  1032. res->backup = vmw_bo_reference(bo_mob);
  1033. res->backup_offset = 0;
  1034. vmw_resource_unreserve(res, false, NULL, 0);
  1035. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  1036. return 0;
  1037. }
  1038. static int vmw_kms_new_framebuffer_bo(struct vmw_private *dev_priv,
  1039. struct vmw_buffer_object *bo,
  1040. struct vmw_framebuffer **out,
  1041. const struct drm_mode_fb_cmd2
  1042. *mode_cmd)
  1043. {
  1044. struct drm_device *dev = dev_priv->dev;
  1045. struct vmw_framebuffer_bo *vfbd;
  1046. unsigned int requested_size;
  1047. struct drm_format_name_buf format_name;
  1048. int ret;
  1049. requested_size = mode_cmd->height * mode_cmd->pitches[0];
  1050. if (unlikely(requested_size > bo->base.num_pages * PAGE_SIZE)) {
  1051. DRM_ERROR("Screen buffer object size is too small "
  1052. "for requested mode.\n");
  1053. return -EINVAL;
  1054. }
  1055. /* Limited framebuffer color depth support for screen objects */
  1056. if (dev_priv->active_display_unit == vmw_du_screen_object) {
  1057. switch (mode_cmd->pixel_format) {
  1058. case DRM_FORMAT_XRGB8888:
  1059. case DRM_FORMAT_ARGB8888:
  1060. break;
  1061. case DRM_FORMAT_XRGB1555:
  1062. case DRM_FORMAT_RGB565:
  1063. break;
  1064. default:
  1065. DRM_ERROR("Invalid pixel format: %s\n",
  1066. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  1067. return -EINVAL;
  1068. }
  1069. }
  1070. vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
  1071. if (!vfbd) {
  1072. ret = -ENOMEM;
  1073. goto out_err1;
  1074. }
  1075. drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
  1076. vfbd->base.bo = true;
  1077. vfbd->buffer = vmw_bo_reference(bo);
  1078. vfbd->base.user_handle = mode_cmd->handles[0];
  1079. *out = &vfbd->base;
  1080. ret = drm_framebuffer_init(dev, &vfbd->base.base,
  1081. &vmw_framebuffer_bo_funcs);
  1082. if (ret)
  1083. goto out_err2;
  1084. return 0;
  1085. out_err2:
  1086. vmw_bo_unreference(&bo);
  1087. kfree(vfbd);
  1088. out_err1:
  1089. return ret;
  1090. }
  1091. /**
  1092. * vmw_kms_srf_ok - check if a surface can be created
  1093. *
  1094. * @width: requested width
  1095. * @height: requested height
  1096. *
  1097. * Surfaces need to be less than texture size
  1098. */
  1099. static bool
  1100. vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
  1101. {
  1102. if (width > dev_priv->texture_max_width ||
  1103. height > dev_priv->texture_max_height)
  1104. return false;
  1105. return true;
  1106. }
  1107. /**
  1108. * vmw_kms_new_framebuffer - Create a new framebuffer.
  1109. *
  1110. * @dev_priv: Pointer to device private struct.
  1111. * @bo: Pointer to buffer object to wrap the kms framebuffer around.
  1112. * Either @bo or @surface must be NULL.
  1113. * @surface: Pointer to a surface to wrap the kms framebuffer around.
  1114. * Either @bo or @surface must be NULL.
  1115. * @only_2d: No presents will occur to this buffer object based framebuffer.
  1116. * This helps the code to do some important optimizations.
  1117. * @mode_cmd: Frame-buffer metadata.
  1118. */
  1119. struct vmw_framebuffer *
  1120. vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
  1121. struct vmw_buffer_object *bo,
  1122. struct vmw_surface *surface,
  1123. bool only_2d,
  1124. const struct drm_mode_fb_cmd2 *mode_cmd)
  1125. {
  1126. struct vmw_framebuffer *vfb = NULL;
  1127. bool is_bo_proxy = false;
  1128. int ret;
  1129. /*
  1130. * We cannot use the SurfaceDMA command in an non-accelerated VM,
  1131. * therefore, wrap the buffer object in a surface so we can use the
  1132. * SurfaceCopy command.
  1133. */
  1134. if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
  1135. bo && only_2d &&
  1136. mode_cmd->width > 64 && /* Don't create a proxy for cursor */
  1137. dev_priv->active_display_unit == vmw_du_screen_target) {
  1138. ret = vmw_create_bo_proxy(dev_priv->dev, mode_cmd,
  1139. bo, &surface);
  1140. if (ret)
  1141. return ERR_PTR(ret);
  1142. is_bo_proxy = true;
  1143. }
  1144. /* Create the new framebuffer depending one what we have */
  1145. if (surface) {
  1146. ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
  1147. mode_cmd,
  1148. is_bo_proxy);
  1149. /*
  1150. * vmw_create_bo_proxy() adds a reference that is no longer
  1151. * needed
  1152. */
  1153. if (is_bo_proxy)
  1154. vmw_surface_unreference(&surface);
  1155. } else if (bo) {
  1156. ret = vmw_kms_new_framebuffer_bo(dev_priv, bo, &vfb,
  1157. mode_cmd);
  1158. } else {
  1159. BUG();
  1160. }
  1161. if (ret)
  1162. return ERR_PTR(ret);
  1163. vfb->pin = vmw_framebuffer_pin;
  1164. vfb->unpin = vmw_framebuffer_unpin;
  1165. return vfb;
  1166. }
  1167. /*
  1168. * Generic Kernel modesetting functions
  1169. */
  1170. static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
  1171. struct drm_file *file_priv,
  1172. const struct drm_mode_fb_cmd2 *mode_cmd)
  1173. {
  1174. struct vmw_private *dev_priv = vmw_priv(dev);
  1175. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  1176. struct vmw_framebuffer *vfb = NULL;
  1177. struct vmw_surface *surface = NULL;
  1178. struct vmw_buffer_object *bo = NULL;
  1179. struct ttm_base_object *user_obj;
  1180. int ret;
  1181. /**
  1182. * This code should be conditioned on Screen Objects not being used.
  1183. * If screen objects are used, we can allocate a GMR to hold the
  1184. * requested framebuffer.
  1185. */
  1186. if (!vmw_kms_validate_mode_vram(dev_priv,
  1187. mode_cmd->pitches[0],
  1188. mode_cmd->height)) {
  1189. DRM_ERROR("Requested mode exceed bounding box limit.\n");
  1190. return ERR_PTR(-ENOMEM);
  1191. }
  1192. /*
  1193. * Take a reference on the user object of the resource
  1194. * backing the kms fb. This ensures that user-space handle
  1195. * lookups on that resource will always work as long as
  1196. * it's registered with a kms framebuffer. This is important,
  1197. * since vmw_execbuf_process identifies resources in the
  1198. * command stream using user-space handles.
  1199. */
  1200. user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
  1201. if (unlikely(user_obj == NULL)) {
  1202. DRM_ERROR("Could not locate requested kms frame buffer.\n");
  1203. return ERR_PTR(-ENOENT);
  1204. }
  1205. /**
  1206. * End conditioned code.
  1207. */
  1208. /* returns either a bo or surface */
  1209. ret = vmw_user_lookup_handle(dev_priv, tfile,
  1210. mode_cmd->handles[0],
  1211. &surface, &bo);
  1212. if (ret)
  1213. goto err_out;
  1214. if (!bo &&
  1215. !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
  1216. DRM_ERROR("Surface size cannot exceed %dx%d",
  1217. dev_priv->texture_max_width,
  1218. dev_priv->texture_max_height);
  1219. goto err_out;
  1220. }
  1221. vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
  1222. !(dev_priv->capabilities & SVGA_CAP_3D),
  1223. mode_cmd);
  1224. if (IS_ERR(vfb)) {
  1225. ret = PTR_ERR(vfb);
  1226. goto err_out;
  1227. }
  1228. err_out:
  1229. /* vmw_user_lookup_handle takes one ref so does new_fb */
  1230. if (bo)
  1231. vmw_bo_unreference(&bo);
  1232. if (surface)
  1233. vmw_surface_unreference(&surface);
  1234. if (ret) {
  1235. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  1236. ttm_base_object_unref(&user_obj);
  1237. return ERR_PTR(ret);
  1238. } else
  1239. vfb->user_obj = user_obj;
  1240. return &vfb->base;
  1241. }
  1242. /**
  1243. * vmw_kms_atomic_check_modeset- validate state object for modeset changes
  1244. *
  1245. * @dev: DRM device
  1246. * @state: the driver state object
  1247. *
  1248. * This is a simple wrapper around drm_atomic_helper_check_modeset() for
  1249. * us to assign a value to mode->crtc_clock so that
  1250. * drm_calc_timestamping_constants() won't throw an error message
  1251. *
  1252. * RETURNS
  1253. * Zero for success or -errno
  1254. */
  1255. static int
  1256. vmw_kms_atomic_check_modeset(struct drm_device *dev,
  1257. struct drm_atomic_state *state)
  1258. {
  1259. struct drm_crtc_state *crtc_state;
  1260. struct drm_crtc *crtc;
  1261. struct vmw_private *dev_priv = vmw_priv(dev);
  1262. int i;
  1263. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  1264. unsigned long requested_bb_mem = 0;
  1265. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1266. struct drm_plane *plane = crtc->primary;
  1267. struct drm_plane_state *plane_state;
  1268. plane_state = drm_atomic_get_new_plane_state(state, plane);
  1269. if (plane_state && plane_state->fb) {
  1270. int cpp = plane_state->fb->format->cpp[0];
  1271. requested_bb_mem += crtc->mode.hdisplay * cpp *
  1272. crtc->mode.vdisplay;
  1273. }
  1274. if (requested_bb_mem > dev_priv->prim_bb_mem)
  1275. return -EINVAL;
  1276. }
  1277. }
  1278. return drm_atomic_helper_check(dev, state);
  1279. }
  1280. static const struct drm_mode_config_funcs vmw_kms_funcs = {
  1281. .fb_create = vmw_kms_fb_create,
  1282. .atomic_check = vmw_kms_atomic_check_modeset,
  1283. .atomic_commit = drm_atomic_helper_commit,
  1284. };
  1285. static int vmw_kms_generic_present(struct vmw_private *dev_priv,
  1286. struct drm_file *file_priv,
  1287. struct vmw_framebuffer *vfb,
  1288. struct vmw_surface *surface,
  1289. uint32_t sid,
  1290. int32_t destX, int32_t destY,
  1291. struct drm_vmw_rect *clips,
  1292. uint32_t num_clips)
  1293. {
  1294. return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
  1295. &surface->res, destX, destY,
  1296. num_clips, 1, NULL, NULL);
  1297. }
  1298. int vmw_kms_present(struct vmw_private *dev_priv,
  1299. struct drm_file *file_priv,
  1300. struct vmw_framebuffer *vfb,
  1301. struct vmw_surface *surface,
  1302. uint32_t sid,
  1303. int32_t destX, int32_t destY,
  1304. struct drm_vmw_rect *clips,
  1305. uint32_t num_clips)
  1306. {
  1307. int ret;
  1308. switch (dev_priv->active_display_unit) {
  1309. case vmw_du_screen_target:
  1310. ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
  1311. &surface->res, destX, destY,
  1312. num_clips, 1, NULL, NULL);
  1313. break;
  1314. case vmw_du_screen_object:
  1315. ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
  1316. sid, destX, destY, clips,
  1317. num_clips);
  1318. break;
  1319. default:
  1320. WARN_ONCE(true,
  1321. "Present called with invalid display system.\n");
  1322. ret = -ENOSYS;
  1323. break;
  1324. }
  1325. if (ret)
  1326. return ret;
  1327. vmw_fifo_flush(dev_priv, false);
  1328. return 0;
  1329. }
  1330. static void
  1331. vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
  1332. {
  1333. if (dev_priv->hotplug_mode_update_property)
  1334. return;
  1335. dev_priv->hotplug_mode_update_property =
  1336. drm_property_create_range(dev_priv->dev,
  1337. DRM_MODE_PROP_IMMUTABLE,
  1338. "hotplug_mode_update", 0, 1);
  1339. if (!dev_priv->hotplug_mode_update_property)
  1340. return;
  1341. }
  1342. int vmw_kms_init(struct vmw_private *dev_priv)
  1343. {
  1344. struct drm_device *dev = dev_priv->dev;
  1345. int ret;
  1346. drm_mode_config_init(dev);
  1347. dev->mode_config.funcs = &vmw_kms_funcs;
  1348. dev->mode_config.min_width = 1;
  1349. dev->mode_config.min_height = 1;
  1350. dev->mode_config.max_width = dev_priv->texture_max_width;
  1351. dev->mode_config.max_height = dev_priv->texture_max_height;
  1352. drm_mode_create_suggested_offset_properties(dev);
  1353. vmw_kms_create_hotplug_mode_update_property(dev_priv);
  1354. ret = vmw_kms_stdu_init_display(dev_priv);
  1355. if (ret) {
  1356. ret = vmw_kms_sou_init_display(dev_priv);
  1357. if (ret) /* Fallback */
  1358. ret = vmw_kms_ldu_init_display(dev_priv);
  1359. }
  1360. return ret;
  1361. }
  1362. int vmw_kms_close(struct vmw_private *dev_priv)
  1363. {
  1364. int ret = 0;
  1365. /*
  1366. * Docs says we should take the lock before calling this function
  1367. * but since it destroys encoders and our destructor calls
  1368. * drm_encoder_cleanup which takes the lock we deadlock.
  1369. */
  1370. drm_mode_config_cleanup(dev_priv->dev);
  1371. if (dev_priv->active_display_unit == vmw_du_legacy)
  1372. ret = vmw_kms_ldu_close_display(dev_priv);
  1373. return ret;
  1374. }
  1375. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  1376. struct drm_file *file_priv)
  1377. {
  1378. struct drm_vmw_cursor_bypass_arg *arg = data;
  1379. struct vmw_display_unit *du;
  1380. struct drm_crtc *crtc;
  1381. int ret = 0;
  1382. mutex_lock(&dev->mode_config.mutex);
  1383. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  1384. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1385. du = vmw_crtc_to_du(crtc);
  1386. du->hotspot_x = arg->xhot;
  1387. du->hotspot_y = arg->yhot;
  1388. }
  1389. mutex_unlock(&dev->mode_config.mutex);
  1390. return 0;
  1391. }
  1392. crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
  1393. if (!crtc) {
  1394. ret = -ENOENT;
  1395. goto out;
  1396. }
  1397. du = vmw_crtc_to_du(crtc);
  1398. du->hotspot_x = arg->xhot;
  1399. du->hotspot_y = arg->yhot;
  1400. out:
  1401. mutex_unlock(&dev->mode_config.mutex);
  1402. return ret;
  1403. }
  1404. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  1405. unsigned width, unsigned height, unsigned pitch,
  1406. unsigned bpp, unsigned depth)
  1407. {
  1408. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1409. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  1410. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1411. vmw_mmio_write(pitch, vmw_priv->mmio_virt +
  1412. SVGA_FIFO_PITCHLOCK);
  1413. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  1414. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  1415. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  1416. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  1417. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  1418. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  1419. return -EINVAL;
  1420. }
  1421. return 0;
  1422. }
  1423. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  1424. {
  1425. struct vmw_vga_topology_state *save;
  1426. uint32_t i;
  1427. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  1428. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  1429. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  1430. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1431. vmw_priv->vga_pitchlock =
  1432. vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
  1433. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1434. vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
  1435. SVGA_FIFO_PITCHLOCK);
  1436. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1437. return 0;
  1438. vmw_priv->num_displays = vmw_read(vmw_priv,
  1439. SVGA_REG_NUM_GUEST_DISPLAYS);
  1440. if (vmw_priv->num_displays == 0)
  1441. vmw_priv->num_displays = 1;
  1442. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1443. save = &vmw_priv->vga_save[i];
  1444. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1445. save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
  1446. save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
  1447. save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
  1448. save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
  1449. save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
  1450. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1451. if (i == 0 && vmw_priv->num_displays == 1 &&
  1452. save->width == 0 && save->height == 0) {
  1453. /*
  1454. * It should be fairly safe to assume that these
  1455. * values are uninitialized.
  1456. */
  1457. save->width = vmw_priv->vga_width - save->pos_x;
  1458. save->height = vmw_priv->vga_height - save->pos_y;
  1459. }
  1460. }
  1461. return 0;
  1462. }
  1463. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  1464. {
  1465. struct vmw_vga_topology_state *save;
  1466. uint32_t i;
  1467. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  1468. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  1469. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  1470. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1471. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
  1472. vmw_priv->vga_pitchlock);
  1473. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1474. vmw_mmio_write(vmw_priv->vga_pitchlock,
  1475. vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  1476. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1477. return 0;
  1478. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1479. save = &vmw_priv->vga_save[i];
  1480. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1481. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
  1482. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
  1483. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
  1484. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
  1485. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
  1486. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1487. }
  1488. return 0;
  1489. }
  1490. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  1491. uint32_t pitch,
  1492. uint32_t height)
  1493. {
  1494. return ((u64) pitch * (u64) height) < (u64)
  1495. ((dev_priv->active_display_unit == vmw_du_screen_target) ?
  1496. dev_priv->prim_bb_mem : dev_priv->vram_size);
  1497. }
  1498. /**
  1499. * Function called by DRM code called with vbl_lock held.
  1500. */
  1501. u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
  1502. {
  1503. return 0;
  1504. }
  1505. /**
  1506. * Function called by DRM code called with vbl_lock held.
  1507. */
  1508. int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
  1509. {
  1510. return -EINVAL;
  1511. }
  1512. /**
  1513. * Function called by DRM code called with vbl_lock held.
  1514. */
  1515. void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
  1516. {
  1517. }
  1518. /*
  1519. * Small shared kms functions.
  1520. */
  1521. static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
  1522. struct drm_vmw_rect *rects)
  1523. {
  1524. struct drm_device *dev = dev_priv->dev;
  1525. struct vmw_display_unit *du;
  1526. struct drm_connector *con;
  1527. mutex_lock(&dev->mode_config.mutex);
  1528. #if 0
  1529. {
  1530. unsigned int i;
  1531. DRM_INFO("%s: new layout ", __func__);
  1532. for (i = 0; i < num; i++)
  1533. DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
  1534. rects[i].w, rects[i].h);
  1535. DRM_INFO("\n");
  1536. }
  1537. #endif
  1538. list_for_each_entry(con, &dev->mode_config.connector_list, head) {
  1539. du = vmw_connector_to_du(con);
  1540. if (num > du->unit) {
  1541. du->pref_width = rects[du->unit].w;
  1542. du->pref_height = rects[du->unit].h;
  1543. du->pref_active = true;
  1544. du->gui_x = rects[du->unit].x;
  1545. du->gui_y = rects[du->unit].y;
  1546. drm_object_property_set_value
  1547. (&con->base, dev->mode_config.suggested_x_property,
  1548. du->gui_x);
  1549. drm_object_property_set_value
  1550. (&con->base, dev->mode_config.suggested_y_property,
  1551. du->gui_y);
  1552. } else {
  1553. du->pref_width = 800;
  1554. du->pref_height = 600;
  1555. du->pref_active = false;
  1556. drm_object_property_set_value
  1557. (&con->base, dev->mode_config.suggested_x_property,
  1558. 0);
  1559. drm_object_property_set_value
  1560. (&con->base, dev->mode_config.suggested_y_property,
  1561. 0);
  1562. }
  1563. con->status = vmw_du_connector_detect(con, true);
  1564. }
  1565. mutex_unlock(&dev->mode_config.mutex);
  1566. drm_sysfs_hotplug_event(dev);
  1567. return 0;
  1568. }
  1569. int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  1570. u16 *r, u16 *g, u16 *b,
  1571. uint32_t size,
  1572. struct drm_modeset_acquire_ctx *ctx)
  1573. {
  1574. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  1575. int i;
  1576. for (i = 0; i < size; i++) {
  1577. DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
  1578. r[i], g[i], b[i]);
  1579. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
  1580. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
  1581. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
  1582. }
  1583. return 0;
  1584. }
  1585. int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
  1586. {
  1587. return 0;
  1588. }
  1589. enum drm_connector_status
  1590. vmw_du_connector_detect(struct drm_connector *connector, bool force)
  1591. {
  1592. uint32_t num_displays;
  1593. struct drm_device *dev = connector->dev;
  1594. struct vmw_private *dev_priv = vmw_priv(dev);
  1595. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1596. num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
  1597. return ((vmw_connector_to_du(connector)->unit < num_displays &&
  1598. du->pref_active) ?
  1599. connector_status_connected : connector_status_disconnected);
  1600. }
  1601. static struct drm_display_mode vmw_kms_connector_builtin[] = {
  1602. /* 640x480@60Hz */
  1603. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  1604. 752, 800, 0, 480, 489, 492, 525, 0,
  1605. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1606. /* 800x600@60Hz */
  1607. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  1608. 968, 1056, 0, 600, 601, 605, 628, 0,
  1609. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1610. /* 1024x768@60Hz */
  1611. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  1612. 1184, 1344, 0, 768, 771, 777, 806, 0,
  1613. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1614. /* 1152x864@75Hz */
  1615. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  1616. 1344, 1600, 0, 864, 865, 868, 900, 0,
  1617. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1618. /* 1280x768@60Hz */
  1619. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  1620. 1472, 1664, 0, 768, 771, 778, 798, 0,
  1621. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1622. /* 1280x800@60Hz */
  1623. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  1624. 1480, 1680, 0, 800, 803, 809, 831, 0,
  1625. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1626. /* 1280x960@60Hz */
  1627. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  1628. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  1629. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1630. /* 1280x1024@60Hz */
  1631. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  1632. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  1633. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1634. /* 1360x768@60Hz */
  1635. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  1636. 1536, 1792, 0, 768, 771, 777, 795, 0,
  1637. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1638. /* 1440x1050@60Hz */
  1639. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  1640. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  1641. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1642. /* 1440x900@60Hz */
  1643. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  1644. 1672, 1904, 0, 900, 903, 909, 934, 0,
  1645. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1646. /* 1600x1200@60Hz */
  1647. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  1648. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  1649. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1650. /* 1680x1050@60Hz */
  1651. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  1652. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  1653. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1654. /* 1792x1344@60Hz */
  1655. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  1656. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  1657. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1658. /* 1853x1392@60Hz */
  1659. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  1660. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  1661. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1662. /* 1920x1200@60Hz */
  1663. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  1664. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  1665. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1666. /* 1920x1440@60Hz */
  1667. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  1668. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  1669. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1670. /* 2560x1600@60Hz */
  1671. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  1672. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  1673. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1674. /* Terminate */
  1675. { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
  1676. };
  1677. /**
  1678. * vmw_guess_mode_timing - Provide fake timings for a
  1679. * 60Hz vrefresh mode.
  1680. *
  1681. * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
  1682. * members filled in.
  1683. */
  1684. void vmw_guess_mode_timing(struct drm_display_mode *mode)
  1685. {
  1686. mode->hsync_start = mode->hdisplay + 50;
  1687. mode->hsync_end = mode->hsync_start + 50;
  1688. mode->htotal = mode->hsync_end + 50;
  1689. mode->vsync_start = mode->vdisplay + 50;
  1690. mode->vsync_end = mode->vsync_start + 50;
  1691. mode->vtotal = mode->vsync_end + 50;
  1692. mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
  1693. mode->vrefresh = drm_mode_vrefresh(mode);
  1694. }
  1695. int vmw_du_connector_fill_modes(struct drm_connector *connector,
  1696. uint32_t max_width, uint32_t max_height)
  1697. {
  1698. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1699. struct drm_device *dev = connector->dev;
  1700. struct vmw_private *dev_priv = vmw_priv(dev);
  1701. struct drm_display_mode *mode = NULL;
  1702. struct drm_display_mode *bmode;
  1703. struct drm_display_mode prefmode = { DRM_MODE("preferred",
  1704. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  1705. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1706. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  1707. };
  1708. int i;
  1709. u32 assumed_bpp = 4;
  1710. if (dev_priv->assume_16bpp)
  1711. assumed_bpp = 2;
  1712. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1713. max_width = min(max_width, dev_priv->stdu_max_width);
  1714. max_width = min(max_width, dev_priv->texture_max_width);
  1715. max_height = min(max_height, dev_priv->stdu_max_height);
  1716. max_height = min(max_height, dev_priv->texture_max_height);
  1717. }
  1718. /* Add preferred mode */
  1719. mode = drm_mode_duplicate(dev, &prefmode);
  1720. if (!mode)
  1721. return 0;
  1722. mode->hdisplay = du->pref_width;
  1723. mode->vdisplay = du->pref_height;
  1724. vmw_guess_mode_timing(mode);
  1725. if (vmw_kms_validate_mode_vram(dev_priv,
  1726. mode->hdisplay * assumed_bpp,
  1727. mode->vdisplay)) {
  1728. drm_mode_probed_add(connector, mode);
  1729. } else {
  1730. drm_mode_destroy(dev, mode);
  1731. mode = NULL;
  1732. }
  1733. if (du->pref_mode) {
  1734. list_del_init(&du->pref_mode->head);
  1735. drm_mode_destroy(dev, du->pref_mode);
  1736. }
  1737. /* mode might be null here, this is intended */
  1738. du->pref_mode = mode;
  1739. for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
  1740. bmode = &vmw_kms_connector_builtin[i];
  1741. if (bmode->hdisplay > max_width ||
  1742. bmode->vdisplay > max_height)
  1743. continue;
  1744. if (!vmw_kms_validate_mode_vram(dev_priv,
  1745. bmode->hdisplay * assumed_bpp,
  1746. bmode->vdisplay))
  1747. continue;
  1748. mode = drm_mode_duplicate(dev, bmode);
  1749. if (!mode)
  1750. return 0;
  1751. mode->vrefresh = drm_mode_vrefresh(mode);
  1752. drm_mode_probed_add(connector, mode);
  1753. }
  1754. drm_mode_connector_list_update(connector);
  1755. /* Move the prefered mode first, help apps pick the right mode. */
  1756. drm_mode_sort(&connector->modes);
  1757. return 1;
  1758. }
  1759. int vmw_du_connector_set_property(struct drm_connector *connector,
  1760. struct drm_property *property,
  1761. uint64_t val)
  1762. {
  1763. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1764. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1765. if (property == dev_priv->implicit_placement_property)
  1766. du->is_implicit = val;
  1767. return 0;
  1768. }
  1769. /**
  1770. * vmw_du_connector_atomic_set_property - Atomic version of get property
  1771. *
  1772. * @crtc - crtc the property is associated with
  1773. *
  1774. * Returns:
  1775. * Zero on success, negative errno on failure.
  1776. */
  1777. int
  1778. vmw_du_connector_atomic_set_property(struct drm_connector *connector,
  1779. struct drm_connector_state *state,
  1780. struct drm_property *property,
  1781. uint64_t val)
  1782. {
  1783. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1784. struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
  1785. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1786. if (property == dev_priv->implicit_placement_property) {
  1787. vcs->is_implicit = val;
  1788. /*
  1789. * We should really be doing a drm_atomic_commit() to
  1790. * commit the new state, but since this doesn't cause
  1791. * an immedate state change, this is probably ok
  1792. */
  1793. du->is_implicit = vcs->is_implicit;
  1794. } else {
  1795. return -EINVAL;
  1796. }
  1797. return 0;
  1798. }
  1799. /**
  1800. * vmw_du_connector_atomic_get_property - Atomic version of get property
  1801. *
  1802. * @connector - connector the property is associated with
  1803. *
  1804. * Returns:
  1805. * Zero on success, negative errno on failure.
  1806. */
  1807. int
  1808. vmw_du_connector_atomic_get_property(struct drm_connector *connector,
  1809. const struct drm_connector_state *state,
  1810. struct drm_property *property,
  1811. uint64_t *val)
  1812. {
  1813. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1814. struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
  1815. if (property == dev_priv->implicit_placement_property)
  1816. *val = vcs->is_implicit;
  1817. else {
  1818. DRM_ERROR("Invalid Property %s\n", property->name);
  1819. return -EINVAL;
  1820. }
  1821. return 0;
  1822. }
  1823. int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
  1824. struct drm_file *file_priv)
  1825. {
  1826. struct vmw_private *dev_priv = vmw_priv(dev);
  1827. struct drm_vmw_update_layout_arg *arg =
  1828. (struct drm_vmw_update_layout_arg *)data;
  1829. void __user *user_rects;
  1830. struct drm_vmw_rect *rects;
  1831. unsigned rects_size;
  1832. int ret;
  1833. int i;
  1834. u64 total_pixels = 0;
  1835. struct drm_mode_config *mode_config = &dev->mode_config;
  1836. struct drm_vmw_rect bounding_box = {0};
  1837. if (!arg->num_outputs) {
  1838. struct drm_vmw_rect def_rect = {0, 0, 800, 600};
  1839. vmw_du_update_layout(dev_priv, 1, &def_rect);
  1840. return 0;
  1841. }
  1842. rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
  1843. rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
  1844. GFP_KERNEL);
  1845. if (unlikely(!rects))
  1846. return -ENOMEM;
  1847. user_rects = (void __user *)(unsigned long)arg->rects;
  1848. ret = copy_from_user(rects, user_rects, rects_size);
  1849. if (unlikely(ret != 0)) {
  1850. DRM_ERROR("Failed to get rects.\n");
  1851. ret = -EFAULT;
  1852. goto out_free;
  1853. }
  1854. for (i = 0; i < arg->num_outputs; ++i) {
  1855. if (rects[i].x < 0 ||
  1856. rects[i].y < 0 ||
  1857. rects[i].x + rects[i].w > mode_config->max_width ||
  1858. rects[i].y + rects[i].h > mode_config->max_height) {
  1859. DRM_ERROR("Invalid GUI layout.\n");
  1860. ret = -EINVAL;
  1861. goto out_free;
  1862. }
  1863. /*
  1864. * bounding_box.w and bunding_box.h are used as
  1865. * lower-right coordinates
  1866. */
  1867. if (rects[i].x + rects[i].w > bounding_box.w)
  1868. bounding_box.w = rects[i].x + rects[i].w;
  1869. if (rects[i].y + rects[i].h > bounding_box.h)
  1870. bounding_box.h = rects[i].y + rects[i].h;
  1871. total_pixels += (u64) rects[i].w * (u64) rects[i].h;
  1872. }
  1873. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1874. /*
  1875. * For Screen Targets, the limits for a toplogy are:
  1876. * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
  1877. * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
  1878. */
  1879. u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
  1880. u64 pixel_mem = total_pixels * 4;
  1881. if (bb_mem > dev_priv->prim_bb_mem) {
  1882. DRM_ERROR("Topology is beyond supported limits.\n");
  1883. ret = -EINVAL;
  1884. goto out_free;
  1885. }
  1886. if (pixel_mem > dev_priv->prim_bb_mem) {
  1887. DRM_ERROR("Combined output size too large\n");
  1888. ret = -EINVAL;
  1889. goto out_free;
  1890. }
  1891. }
  1892. vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
  1893. out_free:
  1894. kfree(rects);
  1895. return ret;
  1896. }
  1897. /**
  1898. * vmw_kms_helper_dirty - Helper to build commands and perform actions based
  1899. * on a set of cliprects and a set of display units.
  1900. *
  1901. * @dev_priv: Pointer to a device private structure.
  1902. * @framebuffer: Pointer to the framebuffer on which to perform the actions.
  1903. * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
  1904. * Cliprects are given in framebuffer coordinates.
  1905. * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
  1906. * be NULL. Cliprects are given in source coordinates.
  1907. * @dest_x: X coordinate offset for the crtc / destination clip rects.
  1908. * @dest_y: Y coordinate offset for the crtc / destination clip rects.
  1909. * @num_clips: Number of cliprects in the @clips or @vclips array.
  1910. * @increment: Integer with which to increment the clip counter when looping.
  1911. * Used to skip a predetermined number of clip rects.
  1912. * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
  1913. */
  1914. int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
  1915. struct vmw_framebuffer *framebuffer,
  1916. const struct drm_clip_rect *clips,
  1917. const struct drm_vmw_rect *vclips,
  1918. s32 dest_x, s32 dest_y,
  1919. int num_clips,
  1920. int increment,
  1921. struct vmw_kms_dirty *dirty)
  1922. {
  1923. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  1924. struct drm_crtc *crtc;
  1925. u32 num_units = 0;
  1926. u32 i, k;
  1927. dirty->dev_priv = dev_priv;
  1928. /* If crtc is passed, no need to iterate over other display units */
  1929. if (dirty->crtc) {
  1930. units[num_units++] = vmw_crtc_to_du(dirty->crtc);
  1931. } else {
  1932. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
  1933. head) {
  1934. struct drm_plane *plane = crtc->primary;
  1935. if (plane->state->fb == &framebuffer->base)
  1936. units[num_units++] = vmw_crtc_to_du(crtc);
  1937. }
  1938. }
  1939. for (k = 0; k < num_units; k++) {
  1940. struct vmw_display_unit *unit = units[k];
  1941. s32 crtc_x = unit->crtc.x;
  1942. s32 crtc_y = unit->crtc.y;
  1943. s32 crtc_width = unit->crtc.mode.hdisplay;
  1944. s32 crtc_height = unit->crtc.mode.vdisplay;
  1945. const struct drm_clip_rect *clips_ptr = clips;
  1946. const struct drm_vmw_rect *vclips_ptr = vclips;
  1947. dirty->unit = unit;
  1948. if (dirty->fifo_reserve_size > 0) {
  1949. dirty->cmd = vmw_fifo_reserve(dev_priv,
  1950. dirty->fifo_reserve_size);
  1951. if (!dirty->cmd) {
  1952. DRM_ERROR("Couldn't reserve fifo space "
  1953. "for dirty blits.\n");
  1954. return -ENOMEM;
  1955. }
  1956. memset(dirty->cmd, 0, dirty->fifo_reserve_size);
  1957. }
  1958. dirty->num_hits = 0;
  1959. for (i = 0; i < num_clips; i++, clips_ptr += increment,
  1960. vclips_ptr += increment) {
  1961. s32 clip_left;
  1962. s32 clip_top;
  1963. /*
  1964. * Select clip array type. Note that integer type
  1965. * in @clips is unsigned short, whereas in @vclips
  1966. * it's 32-bit.
  1967. */
  1968. if (clips) {
  1969. dirty->fb_x = (s32) clips_ptr->x1;
  1970. dirty->fb_y = (s32) clips_ptr->y1;
  1971. dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
  1972. crtc_x;
  1973. dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
  1974. crtc_y;
  1975. } else {
  1976. dirty->fb_x = vclips_ptr->x;
  1977. dirty->fb_y = vclips_ptr->y;
  1978. dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
  1979. dest_x - crtc_x;
  1980. dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
  1981. dest_y - crtc_y;
  1982. }
  1983. dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
  1984. dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
  1985. /* Skip this clip if it's outside the crtc region */
  1986. if (dirty->unit_x1 >= crtc_width ||
  1987. dirty->unit_y1 >= crtc_height ||
  1988. dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
  1989. continue;
  1990. /* Clip right and bottom to crtc limits */
  1991. dirty->unit_x2 = min_t(s32, dirty->unit_x2,
  1992. crtc_width);
  1993. dirty->unit_y2 = min_t(s32, dirty->unit_y2,
  1994. crtc_height);
  1995. /* Clip left and top to crtc limits */
  1996. clip_left = min_t(s32, dirty->unit_x1, 0);
  1997. clip_top = min_t(s32, dirty->unit_y1, 0);
  1998. dirty->unit_x1 -= clip_left;
  1999. dirty->unit_y1 -= clip_top;
  2000. dirty->fb_x -= clip_left;
  2001. dirty->fb_y -= clip_top;
  2002. dirty->clip(dirty);
  2003. }
  2004. dirty->fifo_commit(dirty);
  2005. }
  2006. return 0;
  2007. }
  2008. /**
  2009. * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
  2010. * command submission.
  2011. *
  2012. * @dev_priv. Pointer to a device private structure.
  2013. * @buf: The buffer object
  2014. * @interruptible: Whether to perform waits as interruptible.
  2015. * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
  2016. * The buffer will be validated as a GMR. Already pinned buffers will not be
  2017. * validated.
  2018. *
  2019. * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
  2020. * interrupted by a signal.
  2021. */
  2022. int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
  2023. struct vmw_buffer_object *buf,
  2024. bool interruptible,
  2025. bool validate_as_mob,
  2026. bool for_cpu_blit)
  2027. {
  2028. struct ttm_operation_ctx ctx = {
  2029. .interruptible = interruptible,
  2030. .no_wait_gpu = false};
  2031. struct ttm_buffer_object *bo = &buf->base;
  2032. int ret;
  2033. ttm_bo_reserve(bo, false, false, NULL);
  2034. if (for_cpu_blit)
  2035. ret = ttm_bo_validate(bo, &vmw_nonfixed_placement, &ctx);
  2036. else
  2037. ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
  2038. validate_as_mob);
  2039. if (ret)
  2040. ttm_bo_unreserve(bo);
  2041. return ret;
  2042. }
  2043. /**
  2044. * vmw_kms_helper_buffer_revert - Undo the actions of
  2045. * vmw_kms_helper_buffer_prepare.
  2046. *
  2047. * @res: Pointer to the buffer object.
  2048. *
  2049. * Helper to be used if an error forces the caller to undo the actions of
  2050. * vmw_kms_helper_buffer_prepare.
  2051. */
  2052. void vmw_kms_helper_buffer_revert(struct vmw_buffer_object *buf)
  2053. {
  2054. if (buf)
  2055. ttm_bo_unreserve(&buf->base);
  2056. }
  2057. /**
  2058. * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
  2059. * kms command submission.
  2060. *
  2061. * @dev_priv: Pointer to a device private structure.
  2062. * @file_priv: Pointer to a struct drm_file representing the caller's
  2063. * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
  2064. * if non-NULL, @user_fence_rep must be non-NULL.
  2065. * @buf: The buffer object.
  2066. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  2067. * ref-counted fence pointer is returned here.
  2068. * @user_fence_rep: Optional pointer to a user-space provided struct
  2069. * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
  2070. * function copies fence data to user-space in a fail-safe manner.
  2071. */
  2072. void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
  2073. struct drm_file *file_priv,
  2074. struct vmw_buffer_object *buf,
  2075. struct vmw_fence_obj **out_fence,
  2076. struct drm_vmw_fence_rep __user *
  2077. user_fence_rep)
  2078. {
  2079. struct vmw_fence_obj *fence;
  2080. uint32_t handle;
  2081. int ret;
  2082. ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
  2083. file_priv ? &handle : NULL);
  2084. if (buf)
  2085. vmw_fence_single_bo(&buf->base, fence);
  2086. if (file_priv)
  2087. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
  2088. ret, user_fence_rep, fence,
  2089. handle, -1, NULL);
  2090. if (out_fence)
  2091. *out_fence = fence;
  2092. else
  2093. vmw_fence_obj_unreference(&fence);
  2094. vmw_kms_helper_buffer_revert(buf);
  2095. }
  2096. /**
  2097. * vmw_kms_helper_resource_revert - Undo the actions of
  2098. * vmw_kms_helper_resource_prepare.
  2099. *
  2100. * @res: Pointer to the resource. Typically a surface.
  2101. *
  2102. * Helper to be used if an error forces the caller to undo the actions of
  2103. * vmw_kms_helper_resource_prepare.
  2104. */
  2105. void vmw_kms_helper_resource_revert(struct vmw_validation_ctx *ctx)
  2106. {
  2107. struct vmw_resource *res = ctx->res;
  2108. vmw_kms_helper_buffer_revert(ctx->buf);
  2109. vmw_bo_unreference(&ctx->buf);
  2110. vmw_resource_unreserve(res, false, NULL, 0);
  2111. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2112. }
  2113. /**
  2114. * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
  2115. * command submission.
  2116. *
  2117. * @res: Pointer to the resource. Typically a surface.
  2118. * @interruptible: Whether to perform waits as interruptible.
  2119. *
  2120. * Reserves and validates also the backup buffer if a guest-backed resource.
  2121. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  2122. * interrupted by a signal.
  2123. */
  2124. int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
  2125. bool interruptible,
  2126. struct vmw_validation_ctx *ctx)
  2127. {
  2128. int ret = 0;
  2129. ctx->buf = NULL;
  2130. ctx->res = res;
  2131. if (interruptible)
  2132. ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
  2133. else
  2134. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  2135. if (unlikely(ret != 0))
  2136. return -ERESTARTSYS;
  2137. ret = vmw_resource_reserve(res, interruptible, false);
  2138. if (ret)
  2139. goto out_unlock;
  2140. if (res->backup) {
  2141. ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
  2142. interruptible,
  2143. res->dev_priv->has_mob,
  2144. false);
  2145. if (ret)
  2146. goto out_unreserve;
  2147. ctx->buf = vmw_bo_reference(res->backup);
  2148. }
  2149. ret = vmw_resource_validate(res);
  2150. if (ret)
  2151. goto out_revert;
  2152. return 0;
  2153. out_revert:
  2154. vmw_kms_helper_buffer_revert(ctx->buf);
  2155. out_unreserve:
  2156. vmw_resource_unreserve(res, false, NULL, 0);
  2157. out_unlock:
  2158. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2159. return ret;
  2160. }
  2161. /**
  2162. * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
  2163. * kms command submission.
  2164. *
  2165. * @res: Pointer to the resource. Typically a surface.
  2166. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  2167. * ref-counted fence pointer is returned here.
  2168. */
  2169. void vmw_kms_helper_resource_finish(struct vmw_validation_ctx *ctx,
  2170. struct vmw_fence_obj **out_fence)
  2171. {
  2172. struct vmw_resource *res = ctx->res;
  2173. if (ctx->buf || out_fence)
  2174. vmw_kms_helper_buffer_finish(res->dev_priv, NULL, ctx->buf,
  2175. out_fence, NULL);
  2176. vmw_bo_unreference(&ctx->buf);
  2177. vmw_resource_unreserve(res, false, NULL, 0);
  2178. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2179. }
  2180. /**
  2181. * vmw_kms_update_proxy - Helper function to update a proxy surface from
  2182. * its backing MOB.
  2183. *
  2184. * @res: Pointer to the surface resource
  2185. * @clips: Clip rects in framebuffer (surface) space.
  2186. * @num_clips: Number of clips in @clips.
  2187. * @increment: Integer with which to increment the clip counter when looping.
  2188. * Used to skip a predetermined number of clip rects.
  2189. *
  2190. * This function makes sure the proxy surface is updated from its backing MOB
  2191. * using the region given by @clips. The surface resource @res and its backing
  2192. * MOB needs to be reserved and validated on call.
  2193. */
  2194. int vmw_kms_update_proxy(struct vmw_resource *res,
  2195. const struct drm_clip_rect *clips,
  2196. unsigned num_clips,
  2197. int increment)
  2198. {
  2199. struct vmw_private *dev_priv = res->dev_priv;
  2200. struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
  2201. struct {
  2202. SVGA3dCmdHeader header;
  2203. SVGA3dCmdUpdateGBImage body;
  2204. } *cmd;
  2205. SVGA3dBox *box;
  2206. size_t copy_size = 0;
  2207. int i;
  2208. if (!clips)
  2209. return 0;
  2210. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
  2211. if (!cmd) {
  2212. DRM_ERROR("Couldn't reserve fifo space for proxy surface "
  2213. "update.\n");
  2214. return -ENOMEM;
  2215. }
  2216. for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
  2217. box = &cmd->body.box;
  2218. cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
  2219. cmd->header.size = sizeof(cmd->body);
  2220. cmd->body.image.sid = res->id;
  2221. cmd->body.image.face = 0;
  2222. cmd->body.image.mipmap = 0;
  2223. if (clips->x1 > size->width || clips->x2 > size->width ||
  2224. clips->y1 > size->height || clips->y2 > size->height) {
  2225. DRM_ERROR("Invalid clips outsize of framebuffer.\n");
  2226. return -EINVAL;
  2227. }
  2228. box->x = clips->x1;
  2229. box->y = clips->y1;
  2230. box->z = 0;
  2231. box->w = clips->x2 - clips->x1;
  2232. box->h = clips->y2 - clips->y1;
  2233. box->d = 1;
  2234. copy_size += sizeof(*cmd);
  2235. }
  2236. vmw_fifo_commit(dev_priv, copy_size);
  2237. return 0;
  2238. }
  2239. int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
  2240. unsigned unit,
  2241. u32 max_width,
  2242. u32 max_height,
  2243. struct drm_connector **p_con,
  2244. struct drm_crtc **p_crtc,
  2245. struct drm_display_mode **p_mode)
  2246. {
  2247. struct drm_connector *con;
  2248. struct vmw_display_unit *du;
  2249. struct drm_display_mode *mode;
  2250. int i = 0;
  2251. int ret = 0;
  2252. mutex_lock(&dev_priv->dev->mode_config.mutex);
  2253. list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
  2254. head) {
  2255. if (i == unit)
  2256. break;
  2257. ++i;
  2258. }
  2259. if (i != unit) {
  2260. DRM_ERROR("Could not find initial display unit.\n");
  2261. ret = -EINVAL;
  2262. goto out_unlock;
  2263. }
  2264. if (list_empty(&con->modes))
  2265. (void) vmw_du_connector_fill_modes(con, max_width, max_height);
  2266. if (list_empty(&con->modes)) {
  2267. DRM_ERROR("Could not find initial display mode.\n");
  2268. ret = -EINVAL;
  2269. goto out_unlock;
  2270. }
  2271. du = vmw_connector_to_du(con);
  2272. *p_con = con;
  2273. *p_crtc = &du->crtc;
  2274. list_for_each_entry(mode, &con->modes, head) {
  2275. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  2276. break;
  2277. }
  2278. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  2279. *p_mode = mode;
  2280. else {
  2281. WARN_ONCE(true, "Could not find initial preferred mode.\n");
  2282. *p_mode = list_first_entry(&con->modes,
  2283. struct drm_display_mode,
  2284. head);
  2285. }
  2286. out_unlock:
  2287. mutex_unlock(&dev_priv->dev->mode_config.mutex);
  2288. return ret;
  2289. }
  2290. /**
  2291. * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
  2292. *
  2293. * @dev_priv: Pointer to a device private struct.
  2294. * @du: The display unit of the crtc.
  2295. */
  2296. void vmw_kms_del_active(struct vmw_private *dev_priv,
  2297. struct vmw_display_unit *du)
  2298. {
  2299. mutex_lock(&dev_priv->global_kms_state_mutex);
  2300. if (du->active_implicit) {
  2301. if (--(dev_priv->num_implicit) == 0)
  2302. dev_priv->implicit_fb = NULL;
  2303. du->active_implicit = false;
  2304. }
  2305. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2306. }
  2307. /**
  2308. * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
  2309. *
  2310. * @vmw_priv: Pointer to a device private struct.
  2311. * @du: The display unit of the crtc.
  2312. * @vfb: The implicit framebuffer
  2313. *
  2314. * Registers a binding to an implicit framebuffer.
  2315. */
  2316. void vmw_kms_add_active(struct vmw_private *dev_priv,
  2317. struct vmw_display_unit *du,
  2318. struct vmw_framebuffer *vfb)
  2319. {
  2320. mutex_lock(&dev_priv->global_kms_state_mutex);
  2321. WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
  2322. if (!du->active_implicit && du->is_implicit) {
  2323. dev_priv->implicit_fb = vfb;
  2324. du->active_implicit = true;
  2325. dev_priv->num_implicit++;
  2326. }
  2327. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2328. }
  2329. /**
  2330. * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
  2331. *
  2332. * @dev_priv: Pointer to device-private struct.
  2333. * @crtc: The crtc we want to flip.
  2334. *
  2335. * Returns true or false depending whether it's OK to flip this crtc
  2336. * based on the criterion that we must not have more than one implicit
  2337. * frame-buffer at any one time.
  2338. */
  2339. bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
  2340. struct drm_crtc *crtc)
  2341. {
  2342. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  2343. bool ret;
  2344. mutex_lock(&dev_priv->global_kms_state_mutex);
  2345. ret = !du->is_implicit || dev_priv->num_implicit == 1;
  2346. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2347. return ret;
  2348. }
  2349. /**
  2350. * vmw_kms_update_implicit_fb - Update the implicit fb.
  2351. *
  2352. * @dev_priv: Pointer to device-private struct.
  2353. * @crtc: The crtc the new implicit frame-buffer is bound to.
  2354. */
  2355. void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
  2356. struct drm_crtc *crtc)
  2357. {
  2358. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  2359. struct drm_plane *plane = crtc->primary;
  2360. struct vmw_framebuffer *vfb;
  2361. mutex_lock(&dev_priv->global_kms_state_mutex);
  2362. if (!du->is_implicit)
  2363. goto out_unlock;
  2364. vfb = vmw_framebuffer_to_vfb(plane->state->fb);
  2365. WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
  2366. dev_priv->implicit_fb != vfb);
  2367. dev_priv->implicit_fb = vfb;
  2368. out_unlock:
  2369. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2370. }
  2371. /**
  2372. * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
  2373. * property.
  2374. *
  2375. * @dev_priv: Pointer to a device private struct.
  2376. * @immutable: Whether the property is immutable.
  2377. *
  2378. * Sets up the implicit placement property unless it's already set up.
  2379. */
  2380. void
  2381. vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
  2382. bool immutable)
  2383. {
  2384. if (dev_priv->implicit_placement_property)
  2385. return;
  2386. dev_priv->implicit_placement_property =
  2387. drm_property_create_range(dev_priv->dev,
  2388. immutable ?
  2389. DRM_MODE_PROP_IMMUTABLE : 0,
  2390. "implicit_placement", 0, 1);
  2391. }
  2392. /**
  2393. * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
  2394. *
  2395. * @set: The configuration to set.
  2396. *
  2397. * The vmwgfx Xorg driver doesn't assign the mode::type member, which
  2398. * when drm_mode_set_crtcinfo is called as part of the configuration setting
  2399. * causes it to return incorrect crtc dimensions causing severe problems in
  2400. * the vmwgfx modesetting. So explicitly clear that member before calling
  2401. * into drm_atomic_helper_set_config.
  2402. */
  2403. int vmw_kms_set_config(struct drm_mode_set *set,
  2404. struct drm_modeset_acquire_ctx *ctx)
  2405. {
  2406. if (set && set->mode)
  2407. set->mode->type = 0;
  2408. return drm_atomic_helper_set_config(set, ctx);
  2409. }
  2410. /**
  2411. * vmw_kms_suspend - Save modesetting state and turn modesetting off.
  2412. *
  2413. * @dev: Pointer to the drm device
  2414. * Return: 0 on success. Negative error code on failure.
  2415. */
  2416. int vmw_kms_suspend(struct drm_device *dev)
  2417. {
  2418. struct vmw_private *dev_priv = vmw_priv(dev);
  2419. dev_priv->suspend_state = drm_atomic_helper_suspend(dev);
  2420. if (IS_ERR(dev_priv->suspend_state)) {
  2421. int ret = PTR_ERR(dev_priv->suspend_state);
  2422. DRM_ERROR("Failed kms suspend: %d\n", ret);
  2423. dev_priv->suspend_state = NULL;
  2424. return ret;
  2425. }
  2426. return 0;
  2427. }
  2428. /**
  2429. * vmw_kms_resume - Re-enable modesetting and restore state
  2430. *
  2431. * @dev: Pointer to the drm device
  2432. * Return: 0 on success. Negative error code on failure.
  2433. *
  2434. * State is resumed from a previous vmw_kms_suspend(). It's illegal
  2435. * to call this function without a previous vmw_kms_suspend().
  2436. */
  2437. int vmw_kms_resume(struct drm_device *dev)
  2438. {
  2439. struct vmw_private *dev_priv = vmw_priv(dev);
  2440. int ret;
  2441. if (WARN_ON(!dev_priv->suspend_state))
  2442. return 0;
  2443. ret = drm_atomic_helper_resume(dev, dev_priv->suspend_state);
  2444. dev_priv->suspend_state = NULL;
  2445. return ret;
  2446. }
  2447. /**
  2448. * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
  2449. *
  2450. * @dev: Pointer to the drm device
  2451. */
  2452. void vmw_kms_lost_device(struct drm_device *dev)
  2453. {
  2454. drm_atomic_helper_shutdown(dev);
  2455. }