vmwgfx_kms.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853
  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_dmabuf(struct vmw_private *dev_priv,
  75. struct vmw_dma_buffer *dmabuf,
  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(&dmabuf->base, true, false, NULL);
  88. if (unlikely(ret != 0)) {
  89. DRM_ERROR("reserve failed\n");
  90. return -EINVAL;
  91. }
  92. ret = ttm_bo_kmap(&dmabuf->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(&dmabuf->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->dmabuf)
  293. vmw_dmabuf_unreference(&vps->dmabuf);
  294. if (fb) {
  295. if (vmw_framebuffer_to_vfb(fb)->dmabuf) {
  296. vps->dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer;
  297. vmw_dmabuf_reference(vps->dmabuf);
  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->fb) {
  318. hotspot_x += plane->fb->hot_x;
  319. hotspot_y += plane->fb->hot_y;
  320. }
  321. du->cursor_surface = vps->surf;
  322. du->cursor_dmabuf = vps->dmabuf;
  323. /* setup new image */
  324. if (vps->surf) {
  325. du->cursor_age = du->cursor_surface->snooper.age;
  326. ret = vmw_cursor_update_image(dev_priv,
  327. vps->surf->snooper.image,
  328. 64, 64, hotspot_x, hotspot_y);
  329. } else if (vps->dmabuf) {
  330. ret = vmw_cursor_update_dmabuf(dev_priv, vps->dmabuf,
  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. struct drm_rect clip = {};
  368. int ret;
  369. if (state->crtc)
  370. crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);
  371. if (crtc_state && crtc_state->enable) {
  372. clip.x2 = crtc_state->adjusted_mode.hdisplay;
  373. clip.y2 = crtc_state->adjusted_mode.vdisplay;
  374. }
  375. ret = drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
  376. DRM_PLANE_HELPER_NO_SCALING,
  377. DRM_PLANE_HELPER_NO_SCALING,
  378. false, true);
  379. if (!ret && new_fb) {
  380. struct drm_crtc *crtc = state->crtc;
  381. struct vmw_connector_state *vcs;
  382. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  383. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  384. struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
  385. vcs = vmw_connector_state_to_vcs(du->connector.state);
  386. /* Only one active implicit framebuffer at a time. */
  387. mutex_lock(&dev_priv->global_kms_state_mutex);
  388. if (vcs->is_implicit && dev_priv->implicit_fb &&
  389. !(dev_priv->num_implicit == 1 && du->active_implicit)
  390. && dev_priv->implicit_fb != vfb) {
  391. DRM_ERROR("Multiple implicit framebuffers "
  392. "not supported.\n");
  393. ret = -EINVAL;
  394. }
  395. mutex_unlock(&dev_priv->global_kms_state_mutex);
  396. }
  397. return ret;
  398. }
  399. /**
  400. * vmw_du_cursor_plane_atomic_check - check if the new state is okay
  401. *
  402. * @plane: cursor plane
  403. * @state: info on the new plane state
  404. *
  405. * This is a chance to fail if the new cursor state does not fit
  406. * our requirements.
  407. *
  408. * Returns 0 on success
  409. */
  410. int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
  411. struct drm_plane_state *new_state)
  412. {
  413. int ret = 0;
  414. struct vmw_surface *surface = NULL;
  415. struct drm_framebuffer *fb = new_state->fb;
  416. /* Turning off */
  417. if (!fb)
  418. return ret;
  419. /* A lot of the code assumes this */
  420. if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
  421. DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
  422. new_state->crtc_w, new_state->crtc_h);
  423. ret = -EINVAL;
  424. }
  425. if (!vmw_framebuffer_to_vfb(fb)->dmabuf)
  426. surface = vmw_framebuffer_to_vfbs(fb)->surface;
  427. if (surface && !surface->snooper.image) {
  428. DRM_ERROR("surface not suitable for cursor\n");
  429. ret = -EINVAL;
  430. }
  431. return ret;
  432. }
  433. int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
  434. struct drm_crtc_state *new_state)
  435. {
  436. struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
  437. int connector_mask = 1 << drm_connector_index(&du->connector);
  438. bool has_primary = new_state->plane_mask &
  439. BIT(drm_plane_index(crtc->primary));
  440. /* We always want to have an active plane with an active CRTC */
  441. if (has_primary != new_state->enable)
  442. return -EINVAL;
  443. if (new_state->connector_mask != connector_mask &&
  444. new_state->connector_mask != 0) {
  445. DRM_ERROR("Invalid connectors configuration\n");
  446. return -EINVAL;
  447. }
  448. /*
  449. * Our virtual device does not have a dot clock, so use the logical
  450. * clock value as the dot clock.
  451. */
  452. if (new_state->mode.crtc_clock == 0)
  453. new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
  454. return 0;
  455. }
  456. void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
  457. struct drm_crtc_state *old_crtc_state)
  458. {
  459. }
  460. void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
  461. struct drm_crtc_state *old_crtc_state)
  462. {
  463. struct drm_pending_vblank_event *event = crtc->state->event;
  464. if (event) {
  465. crtc->state->event = NULL;
  466. spin_lock_irq(&crtc->dev->event_lock);
  467. if (drm_crtc_vblank_get(crtc) == 0)
  468. drm_crtc_arm_vblank_event(crtc, event);
  469. else
  470. drm_crtc_send_vblank_event(crtc, event);
  471. spin_unlock_irq(&crtc->dev->event_lock);
  472. }
  473. }
  474. /**
  475. * vmw_du_crtc_duplicate_state - duplicate crtc state
  476. * @crtc: DRM crtc
  477. *
  478. * Allocates and returns a copy of the crtc state (both common and
  479. * vmw-specific) for the specified crtc.
  480. *
  481. * Returns: The newly allocated crtc state, or NULL on failure.
  482. */
  483. struct drm_crtc_state *
  484. vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
  485. {
  486. struct drm_crtc_state *state;
  487. struct vmw_crtc_state *vcs;
  488. if (WARN_ON(!crtc->state))
  489. return NULL;
  490. vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
  491. if (!vcs)
  492. return NULL;
  493. state = &vcs->base;
  494. __drm_atomic_helper_crtc_duplicate_state(crtc, state);
  495. return state;
  496. }
  497. /**
  498. * vmw_du_crtc_reset - creates a blank vmw crtc state
  499. * @crtc: DRM crtc
  500. *
  501. * Resets the atomic state for @crtc by freeing the state pointer (which
  502. * might be NULL, e.g. at driver load time) and allocating a new empty state
  503. * object.
  504. */
  505. void vmw_du_crtc_reset(struct drm_crtc *crtc)
  506. {
  507. struct vmw_crtc_state *vcs;
  508. if (crtc->state) {
  509. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  510. kfree(vmw_crtc_state_to_vcs(crtc->state));
  511. }
  512. vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
  513. if (!vcs) {
  514. DRM_ERROR("Cannot allocate vmw_crtc_state\n");
  515. return;
  516. }
  517. crtc->state = &vcs->base;
  518. crtc->state->crtc = crtc;
  519. }
  520. /**
  521. * vmw_du_crtc_destroy_state - destroy crtc state
  522. * @crtc: DRM crtc
  523. * @state: state object to destroy
  524. *
  525. * Destroys the crtc state (both common and vmw-specific) for the
  526. * specified plane.
  527. */
  528. void
  529. vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
  530. struct drm_crtc_state *state)
  531. {
  532. drm_atomic_helper_crtc_destroy_state(crtc, state);
  533. }
  534. /**
  535. * vmw_du_plane_duplicate_state - duplicate plane state
  536. * @plane: drm plane
  537. *
  538. * Allocates and returns a copy of the plane state (both common and
  539. * vmw-specific) for the specified plane.
  540. *
  541. * Returns: The newly allocated plane state, or NULL on failure.
  542. */
  543. struct drm_plane_state *
  544. vmw_du_plane_duplicate_state(struct drm_plane *plane)
  545. {
  546. struct drm_plane_state *state;
  547. struct vmw_plane_state *vps;
  548. vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
  549. if (!vps)
  550. return NULL;
  551. vps->pinned = 0;
  552. /* Mapping is managed by prepare_fb/cleanup_fb */
  553. memset(&vps->host_map, 0, sizeof(vps->host_map));
  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->dmabuf)
  559. (void) vmw_dmabuf_reference(vps->dmabuf);
  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->host_map.virtual) {
  600. DRM_ERROR("Host mapping not freed\n");
  601. ttm_bo_kunmap(&vps->host_map);
  602. }
  603. if (vps->surf)
  604. vmw_surface_unreference(&vps->surf);
  605. if (vps->dmabuf)
  606. vmw_dmabuf_unreference(&vps->dmabuf);
  607. drm_atomic_helper_plane_destroy_state(plane, state);
  608. }
  609. /**
  610. * vmw_du_connector_duplicate_state - duplicate connector state
  611. * @connector: DRM connector
  612. *
  613. * Allocates and returns a copy of the connector state (both common and
  614. * vmw-specific) for the specified connector.
  615. *
  616. * Returns: The newly allocated connector state, or NULL on failure.
  617. */
  618. struct drm_connector_state *
  619. vmw_du_connector_duplicate_state(struct drm_connector *connector)
  620. {
  621. struct drm_connector_state *state;
  622. struct vmw_connector_state *vcs;
  623. if (WARN_ON(!connector->state))
  624. return NULL;
  625. vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
  626. if (!vcs)
  627. return NULL;
  628. state = &vcs->base;
  629. __drm_atomic_helper_connector_duplicate_state(connector, state);
  630. return state;
  631. }
  632. /**
  633. * vmw_du_connector_reset - creates a blank vmw connector state
  634. * @connector: DRM connector
  635. *
  636. * Resets the atomic state for @connector by freeing the state pointer (which
  637. * might be NULL, e.g. at driver load time) and allocating a new empty state
  638. * object.
  639. */
  640. void vmw_du_connector_reset(struct drm_connector *connector)
  641. {
  642. struct vmw_connector_state *vcs;
  643. if (connector->state) {
  644. __drm_atomic_helper_connector_destroy_state(connector->state);
  645. kfree(vmw_connector_state_to_vcs(connector->state));
  646. }
  647. vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
  648. if (!vcs) {
  649. DRM_ERROR("Cannot allocate vmw_connector_state\n");
  650. return;
  651. }
  652. __drm_atomic_helper_connector_reset(connector, &vcs->base);
  653. }
  654. /**
  655. * vmw_du_connector_destroy_state - destroy connector state
  656. * @connector: DRM connector
  657. * @state: state object to destroy
  658. *
  659. * Destroys the connector state (both common and vmw-specific) for the
  660. * specified plane.
  661. */
  662. void
  663. vmw_du_connector_destroy_state(struct drm_connector *connector,
  664. struct drm_connector_state *state)
  665. {
  666. drm_atomic_helper_connector_destroy_state(connector, state);
  667. }
  668. /*
  669. * Generic framebuffer code
  670. */
  671. /*
  672. * Surface framebuffer code
  673. */
  674. static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
  675. {
  676. struct vmw_framebuffer_surface *vfbs =
  677. vmw_framebuffer_to_vfbs(framebuffer);
  678. drm_framebuffer_cleanup(framebuffer);
  679. vmw_surface_unreference(&vfbs->surface);
  680. if (vfbs->base.user_obj)
  681. ttm_base_object_unref(&vfbs->base.user_obj);
  682. kfree(vfbs);
  683. }
  684. static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
  685. struct drm_file *file_priv,
  686. unsigned flags, unsigned color,
  687. struct drm_clip_rect *clips,
  688. unsigned num_clips)
  689. {
  690. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  691. struct vmw_framebuffer_surface *vfbs =
  692. vmw_framebuffer_to_vfbs(framebuffer);
  693. struct drm_clip_rect norect;
  694. int ret, inc = 1;
  695. /* Legacy Display Unit does not support 3D */
  696. if (dev_priv->active_display_unit == vmw_du_legacy)
  697. return -EINVAL;
  698. drm_modeset_lock_all(dev_priv->dev);
  699. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  700. if (unlikely(ret != 0)) {
  701. drm_modeset_unlock_all(dev_priv->dev);
  702. return ret;
  703. }
  704. if (!num_clips) {
  705. num_clips = 1;
  706. clips = &norect;
  707. norect.x1 = norect.y1 = 0;
  708. norect.x2 = framebuffer->width;
  709. norect.y2 = framebuffer->height;
  710. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  711. num_clips /= 2;
  712. inc = 2; /* skip source rects */
  713. }
  714. if (dev_priv->active_display_unit == vmw_du_screen_object)
  715. ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
  716. clips, NULL, NULL, 0, 0,
  717. num_clips, inc, NULL);
  718. else
  719. ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
  720. clips, NULL, NULL, 0, 0,
  721. num_clips, inc, NULL);
  722. vmw_fifo_flush(dev_priv, false);
  723. ttm_read_unlock(&dev_priv->reservation_sem);
  724. drm_modeset_unlock_all(dev_priv->dev);
  725. return 0;
  726. }
  727. /**
  728. * vmw_kms_readback - Perform a readback from the screen system to
  729. * a dma-buffer backed framebuffer.
  730. *
  731. * @dev_priv: Pointer to the device private structure.
  732. * @file_priv: Pointer to a struct drm_file identifying the caller.
  733. * Must be set to NULL if @user_fence_rep is NULL.
  734. * @vfb: Pointer to the dma-buffer backed framebuffer.
  735. * @user_fence_rep: User-space provided structure for fence information.
  736. * Must be set to non-NULL if @file_priv is non-NULL.
  737. * @vclips: Array of clip rects.
  738. * @num_clips: Number of clip rects in @vclips.
  739. *
  740. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  741. * interrupted.
  742. */
  743. int vmw_kms_readback(struct vmw_private *dev_priv,
  744. struct drm_file *file_priv,
  745. struct vmw_framebuffer *vfb,
  746. struct drm_vmw_fence_rep __user *user_fence_rep,
  747. struct drm_vmw_rect *vclips,
  748. uint32_t num_clips)
  749. {
  750. switch (dev_priv->active_display_unit) {
  751. case vmw_du_screen_object:
  752. return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
  753. user_fence_rep, vclips, num_clips);
  754. case vmw_du_screen_target:
  755. return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
  756. user_fence_rep, NULL, vclips, num_clips,
  757. 1, false, true);
  758. default:
  759. WARN_ONCE(true,
  760. "Readback called with invalid display system.\n");
  761. }
  762. return -ENOSYS;
  763. }
  764. static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
  765. .destroy = vmw_framebuffer_surface_destroy,
  766. .dirty = vmw_framebuffer_surface_dirty,
  767. };
  768. static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
  769. struct vmw_surface *surface,
  770. struct vmw_framebuffer **out,
  771. const struct drm_mode_fb_cmd2
  772. *mode_cmd,
  773. bool is_dmabuf_proxy)
  774. {
  775. struct drm_device *dev = dev_priv->dev;
  776. struct vmw_framebuffer_surface *vfbs;
  777. enum SVGA3dSurfaceFormat format;
  778. int ret;
  779. struct drm_format_name_buf format_name;
  780. /* 3D is only supported on HWv8 and newer hosts */
  781. if (dev_priv->active_display_unit == vmw_du_legacy)
  782. return -ENOSYS;
  783. /*
  784. * Sanity checks.
  785. */
  786. /* Surface must be marked as a scanout. */
  787. if (unlikely(!surface->scanout))
  788. return -EINVAL;
  789. if (unlikely(surface->mip_levels[0] != 1 ||
  790. surface->num_sizes != 1 ||
  791. surface->base_size.width < mode_cmd->width ||
  792. surface->base_size.height < mode_cmd->height ||
  793. surface->base_size.depth != 1)) {
  794. DRM_ERROR("Incompatible surface dimensions "
  795. "for requested mode.\n");
  796. return -EINVAL;
  797. }
  798. switch (mode_cmd->pixel_format) {
  799. case DRM_FORMAT_ARGB8888:
  800. format = SVGA3D_A8R8G8B8;
  801. break;
  802. case DRM_FORMAT_XRGB8888:
  803. format = SVGA3D_X8R8G8B8;
  804. break;
  805. case DRM_FORMAT_RGB565:
  806. format = SVGA3D_R5G6B5;
  807. break;
  808. case DRM_FORMAT_XRGB1555:
  809. format = SVGA3D_A1R5G5B5;
  810. break;
  811. default:
  812. DRM_ERROR("Invalid pixel format: %s\n",
  813. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  814. return -EINVAL;
  815. }
  816. /*
  817. * For DX, surface format validation is done when surface->scanout
  818. * is set.
  819. */
  820. if (!dev_priv->has_dx && format != surface->format) {
  821. DRM_ERROR("Invalid surface format for requested mode.\n");
  822. return -EINVAL;
  823. }
  824. vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
  825. if (!vfbs) {
  826. ret = -ENOMEM;
  827. goto out_err1;
  828. }
  829. drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
  830. vfbs->surface = vmw_surface_reference(surface);
  831. vfbs->base.user_handle = mode_cmd->handles[0];
  832. vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
  833. *out = &vfbs->base;
  834. ret = drm_framebuffer_init(dev, &vfbs->base.base,
  835. &vmw_framebuffer_surface_funcs);
  836. if (ret)
  837. goto out_err2;
  838. return 0;
  839. out_err2:
  840. vmw_surface_unreference(&surface);
  841. kfree(vfbs);
  842. out_err1:
  843. return ret;
  844. }
  845. /*
  846. * Dmabuf framebuffer code
  847. */
  848. static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
  849. {
  850. struct vmw_framebuffer_dmabuf *vfbd =
  851. vmw_framebuffer_to_vfbd(framebuffer);
  852. drm_framebuffer_cleanup(framebuffer);
  853. vmw_dmabuf_unreference(&vfbd->buffer);
  854. if (vfbd->base.user_obj)
  855. ttm_base_object_unref(&vfbd->base.user_obj);
  856. kfree(vfbd);
  857. }
  858. static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
  859. struct drm_file *file_priv,
  860. unsigned flags, unsigned color,
  861. struct drm_clip_rect *clips,
  862. unsigned num_clips)
  863. {
  864. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  865. struct vmw_framebuffer_dmabuf *vfbd =
  866. vmw_framebuffer_to_vfbd(framebuffer);
  867. struct drm_clip_rect norect;
  868. int ret, increment = 1;
  869. drm_modeset_lock_all(dev_priv->dev);
  870. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  871. if (unlikely(ret != 0)) {
  872. drm_modeset_unlock_all(dev_priv->dev);
  873. return ret;
  874. }
  875. if (!num_clips) {
  876. num_clips = 1;
  877. clips = &norect;
  878. norect.x1 = norect.y1 = 0;
  879. norect.x2 = framebuffer->width;
  880. norect.y2 = framebuffer->height;
  881. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  882. num_clips /= 2;
  883. increment = 2;
  884. }
  885. switch (dev_priv->active_display_unit) {
  886. case vmw_du_screen_target:
  887. ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
  888. clips, NULL, num_clips, increment,
  889. true, true);
  890. break;
  891. case vmw_du_screen_object:
  892. ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
  893. clips, NULL, num_clips,
  894. increment, true, NULL);
  895. break;
  896. case vmw_du_legacy:
  897. ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
  898. clips, num_clips, increment);
  899. break;
  900. default:
  901. ret = -EINVAL;
  902. WARN_ONCE(true, "Dirty called with invalid display system.\n");
  903. break;
  904. }
  905. vmw_fifo_flush(dev_priv, false);
  906. ttm_read_unlock(&dev_priv->reservation_sem);
  907. drm_modeset_unlock_all(dev_priv->dev);
  908. return ret;
  909. }
  910. static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
  911. .destroy = vmw_framebuffer_dmabuf_destroy,
  912. .dirty = vmw_framebuffer_dmabuf_dirty,
  913. };
  914. /**
  915. * Pin the dmabuffer to the start of vram.
  916. */
  917. static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
  918. {
  919. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  920. struct vmw_dma_buffer *buf;
  921. int ret;
  922. buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  923. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  924. if (!buf)
  925. return 0;
  926. switch (dev_priv->active_display_unit) {
  927. case vmw_du_legacy:
  928. vmw_overlay_pause_all(dev_priv);
  929. ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
  930. vmw_overlay_resume_all(dev_priv);
  931. break;
  932. case vmw_du_screen_object:
  933. case vmw_du_screen_target:
  934. if (vfb->dmabuf)
  935. return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
  936. false);
  937. return vmw_dmabuf_pin_in_placement(dev_priv, buf,
  938. &vmw_mob_placement, false);
  939. default:
  940. return -EINVAL;
  941. }
  942. return ret;
  943. }
  944. static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
  945. {
  946. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  947. struct vmw_dma_buffer *buf;
  948. buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
  949. vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
  950. if (WARN_ON(!buf))
  951. return 0;
  952. return vmw_dmabuf_unpin(dev_priv, buf, false);
  953. }
  954. /**
  955. * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
  956. *
  957. * @dev: DRM device
  958. * @mode_cmd: parameters for the new surface
  959. * @dmabuf_mob: MOB backing the DMA buf
  960. * @srf_out: newly created surface
  961. *
  962. * When the content FB is a DMA buf, we create a surface as a proxy to the
  963. * same buffer. This way we can do a surface copy rather than a surface DMA.
  964. * This is a more efficient approach
  965. *
  966. * RETURNS:
  967. * 0 on success, error code otherwise
  968. */
  969. static int vmw_create_dmabuf_proxy(struct drm_device *dev,
  970. const struct drm_mode_fb_cmd2 *mode_cmd,
  971. struct vmw_dma_buffer *dmabuf_mob,
  972. struct vmw_surface **srf_out)
  973. {
  974. uint32_t format;
  975. struct drm_vmw_size content_base_size = {0};
  976. struct vmw_resource *res;
  977. unsigned int bytes_pp;
  978. struct drm_format_name_buf format_name;
  979. int ret;
  980. switch (mode_cmd->pixel_format) {
  981. case DRM_FORMAT_ARGB8888:
  982. case DRM_FORMAT_XRGB8888:
  983. format = SVGA3D_X8R8G8B8;
  984. bytes_pp = 4;
  985. break;
  986. case DRM_FORMAT_RGB565:
  987. case DRM_FORMAT_XRGB1555:
  988. format = SVGA3D_R5G6B5;
  989. bytes_pp = 2;
  990. break;
  991. case 8:
  992. format = SVGA3D_P8;
  993. bytes_pp = 1;
  994. break;
  995. default:
  996. DRM_ERROR("Invalid framebuffer format %s\n",
  997. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  998. return -EINVAL;
  999. }
  1000. content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
  1001. content_base_size.height = mode_cmd->height;
  1002. content_base_size.depth = 1;
  1003. ret = vmw_surface_gb_priv_define(dev,
  1004. 0, /* kernel visible only */
  1005. 0, /* flags */
  1006. format,
  1007. true, /* can be a scanout buffer */
  1008. 1, /* num of mip levels */
  1009. 0,
  1010. 0,
  1011. content_base_size,
  1012. srf_out);
  1013. if (ret) {
  1014. DRM_ERROR("Failed to allocate proxy content buffer\n");
  1015. return ret;
  1016. }
  1017. res = &(*srf_out)->res;
  1018. /* Reserve and switch the backing mob. */
  1019. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  1020. (void) vmw_resource_reserve(res, false, true);
  1021. vmw_dmabuf_unreference(&res->backup);
  1022. res->backup = vmw_dmabuf_reference(dmabuf_mob);
  1023. res->backup_offset = 0;
  1024. vmw_resource_unreserve(res, false, NULL, 0);
  1025. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  1026. return 0;
  1027. }
  1028. static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
  1029. struct vmw_dma_buffer *dmabuf,
  1030. struct vmw_framebuffer **out,
  1031. const struct drm_mode_fb_cmd2
  1032. *mode_cmd)
  1033. {
  1034. struct drm_device *dev = dev_priv->dev;
  1035. struct vmw_framebuffer_dmabuf *vfbd;
  1036. unsigned int requested_size;
  1037. struct drm_format_name_buf format_name;
  1038. int ret;
  1039. requested_size = mode_cmd->height * mode_cmd->pitches[0];
  1040. if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
  1041. DRM_ERROR("Screen buffer object size is too small "
  1042. "for requested mode.\n");
  1043. return -EINVAL;
  1044. }
  1045. /* Limited framebuffer color depth support for screen objects */
  1046. if (dev_priv->active_display_unit == vmw_du_screen_object) {
  1047. switch (mode_cmd->pixel_format) {
  1048. case DRM_FORMAT_XRGB8888:
  1049. case DRM_FORMAT_ARGB8888:
  1050. break;
  1051. case DRM_FORMAT_XRGB1555:
  1052. case DRM_FORMAT_RGB565:
  1053. break;
  1054. default:
  1055. DRM_ERROR("Invalid pixel format: %s\n",
  1056. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  1057. return -EINVAL;
  1058. }
  1059. }
  1060. vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
  1061. if (!vfbd) {
  1062. ret = -ENOMEM;
  1063. goto out_err1;
  1064. }
  1065. drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
  1066. vfbd->base.dmabuf = true;
  1067. vfbd->buffer = vmw_dmabuf_reference(dmabuf);
  1068. vfbd->base.user_handle = mode_cmd->handles[0];
  1069. *out = &vfbd->base;
  1070. ret = drm_framebuffer_init(dev, &vfbd->base.base,
  1071. &vmw_framebuffer_dmabuf_funcs);
  1072. if (ret)
  1073. goto out_err2;
  1074. return 0;
  1075. out_err2:
  1076. vmw_dmabuf_unreference(&dmabuf);
  1077. kfree(vfbd);
  1078. out_err1:
  1079. return ret;
  1080. }
  1081. /**
  1082. * vmw_kms_srf_ok - check if a surface can be created
  1083. *
  1084. * @width: requested width
  1085. * @height: requested height
  1086. *
  1087. * Surfaces need to be less than texture size
  1088. */
  1089. static bool
  1090. vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
  1091. {
  1092. if (width > dev_priv->texture_max_width ||
  1093. height > dev_priv->texture_max_height)
  1094. return false;
  1095. return true;
  1096. }
  1097. /**
  1098. * vmw_kms_new_framebuffer - Create a new framebuffer.
  1099. *
  1100. * @dev_priv: Pointer to device private struct.
  1101. * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
  1102. * Either @dmabuf or @surface must be NULL.
  1103. * @surface: Pointer to a surface to wrap the kms framebuffer around.
  1104. * Either @dmabuf or @surface must be NULL.
  1105. * @only_2d: No presents will occur to this dma buffer based framebuffer. This
  1106. * Helps the code to do some important optimizations.
  1107. * @mode_cmd: Frame-buffer metadata.
  1108. */
  1109. struct vmw_framebuffer *
  1110. vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
  1111. struct vmw_dma_buffer *dmabuf,
  1112. struct vmw_surface *surface,
  1113. bool only_2d,
  1114. const struct drm_mode_fb_cmd2 *mode_cmd)
  1115. {
  1116. struct vmw_framebuffer *vfb = NULL;
  1117. bool is_dmabuf_proxy = false;
  1118. int ret;
  1119. /*
  1120. * We cannot use the SurfaceDMA command in an non-accelerated VM,
  1121. * therefore, wrap the DMA buf in a surface so we can use the
  1122. * SurfaceCopy command.
  1123. */
  1124. if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
  1125. dmabuf && only_2d &&
  1126. mode_cmd->width > 64 && /* Don't create a proxy for cursor */
  1127. dev_priv->active_display_unit == vmw_du_screen_target) {
  1128. ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
  1129. dmabuf, &surface);
  1130. if (ret)
  1131. return ERR_PTR(ret);
  1132. is_dmabuf_proxy = true;
  1133. }
  1134. /* Create the new framebuffer depending one what we have */
  1135. if (surface) {
  1136. ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
  1137. mode_cmd,
  1138. is_dmabuf_proxy);
  1139. /*
  1140. * vmw_create_dmabuf_proxy() adds a reference that is no longer
  1141. * needed
  1142. */
  1143. if (is_dmabuf_proxy)
  1144. vmw_surface_unreference(&surface);
  1145. } else if (dmabuf) {
  1146. ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
  1147. mode_cmd);
  1148. } else {
  1149. BUG();
  1150. }
  1151. if (ret)
  1152. return ERR_PTR(ret);
  1153. vfb->pin = vmw_framebuffer_pin;
  1154. vfb->unpin = vmw_framebuffer_unpin;
  1155. return vfb;
  1156. }
  1157. /*
  1158. * Generic Kernel modesetting functions
  1159. */
  1160. static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
  1161. struct drm_file *file_priv,
  1162. const struct drm_mode_fb_cmd2 *mode_cmd)
  1163. {
  1164. struct vmw_private *dev_priv = vmw_priv(dev);
  1165. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  1166. struct vmw_framebuffer *vfb = NULL;
  1167. struct vmw_surface *surface = NULL;
  1168. struct vmw_dma_buffer *bo = NULL;
  1169. struct ttm_base_object *user_obj;
  1170. int ret;
  1171. /**
  1172. * This code should be conditioned on Screen Objects not being used.
  1173. * If screen objects are used, we can allocate a GMR to hold the
  1174. * requested framebuffer.
  1175. */
  1176. if (!vmw_kms_validate_mode_vram(dev_priv,
  1177. mode_cmd->pitches[0],
  1178. mode_cmd->height)) {
  1179. DRM_ERROR("Requested mode exceed bounding box limit.\n");
  1180. return ERR_PTR(-ENOMEM);
  1181. }
  1182. /*
  1183. * Take a reference on the user object of the resource
  1184. * backing the kms fb. This ensures that user-space handle
  1185. * lookups on that resource will always work as long as
  1186. * it's registered with a kms framebuffer. This is important,
  1187. * since vmw_execbuf_process identifies resources in the
  1188. * command stream using user-space handles.
  1189. */
  1190. user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
  1191. if (unlikely(user_obj == NULL)) {
  1192. DRM_ERROR("Could not locate requested kms frame buffer.\n");
  1193. return ERR_PTR(-ENOENT);
  1194. }
  1195. /**
  1196. * End conditioned code.
  1197. */
  1198. /* returns either a dmabuf or surface */
  1199. ret = vmw_user_lookup_handle(dev_priv, tfile,
  1200. mode_cmd->handles[0],
  1201. &surface, &bo);
  1202. if (ret)
  1203. goto err_out;
  1204. if (!bo &&
  1205. !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
  1206. DRM_ERROR("Surface size cannot exceed %dx%d",
  1207. dev_priv->texture_max_width,
  1208. dev_priv->texture_max_height);
  1209. goto err_out;
  1210. }
  1211. vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
  1212. !(dev_priv->capabilities & SVGA_CAP_3D),
  1213. mode_cmd);
  1214. if (IS_ERR(vfb)) {
  1215. ret = PTR_ERR(vfb);
  1216. goto err_out;
  1217. }
  1218. err_out:
  1219. /* vmw_user_lookup_handle takes one ref so does new_fb */
  1220. if (bo)
  1221. vmw_dmabuf_unreference(&bo);
  1222. if (surface)
  1223. vmw_surface_unreference(&surface);
  1224. if (ret) {
  1225. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  1226. ttm_base_object_unref(&user_obj);
  1227. return ERR_PTR(ret);
  1228. } else
  1229. vfb->user_obj = user_obj;
  1230. return &vfb->base;
  1231. }
  1232. /**
  1233. * vmw_kms_atomic_check_modeset- validate state object for modeset changes
  1234. *
  1235. * @dev: DRM device
  1236. * @state: the driver state object
  1237. *
  1238. * This is a simple wrapper around drm_atomic_helper_check_modeset() for
  1239. * us to assign a value to mode->crtc_clock so that
  1240. * drm_calc_timestamping_constants() won't throw an error message
  1241. *
  1242. * RETURNS
  1243. * Zero for success or -errno
  1244. */
  1245. static int
  1246. vmw_kms_atomic_check_modeset(struct drm_device *dev,
  1247. struct drm_atomic_state *state)
  1248. {
  1249. struct drm_crtc_state *crtc_state;
  1250. struct drm_crtc *crtc;
  1251. struct vmw_private *dev_priv = vmw_priv(dev);
  1252. int i;
  1253. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  1254. unsigned long requested_bb_mem = 0;
  1255. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1256. if (crtc->primary->fb) {
  1257. int cpp = crtc->primary->fb->pitches[0] /
  1258. crtc->primary->fb->width;
  1259. requested_bb_mem += crtc->mode.hdisplay * cpp *
  1260. crtc->mode.vdisplay;
  1261. }
  1262. if (requested_bb_mem > dev_priv->prim_bb_mem)
  1263. return -EINVAL;
  1264. }
  1265. }
  1266. return drm_atomic_helper_check(dev, state);
  1267. }
  1268. /**
  1269. * vmw_kms_atomic_commit - Perform an atomic state commit
  1270. *
  1271. * @dev: DRM device
  1272. * @state: the driver state object
  1273. * @nonblock: Whether nonblocking behaviour is requested
  1274. *
  1275. * This is a simple wrapper around drm_atomic_helper_commit() for
  1276. * us to clear the nonblocking value.
  1277. *
  1278. * Nonblocking commits currently cause synchronization issues
  1279. * for vmwgfx.
  1280. *
  1281. * RETURNS
  1282. * Zero for success or negative error code on failure.
  1283. */
  1284. int vmw_kms_atomic_commit(struct drm_device *dev,
  1285. struct drm_atomic_state *state,
  1286. bool nonblock)
  1287. {
  1288. return drm_atomic_helper_commit(dev, state, false);
  1289. }
  1290. static const struct drm_mode_config_funcs vmw_kms_funcs = {
  1291. .fb_create = vmw_kms_fb_create,
  1292. .atomic_check = vmw_kms_atomic_check_modeset,
  1293. .atomic_commit = vmw_kms_atomic_commit,
  1294. };
  1295. static int vmw_kms_generic_present(struct vmw_private *dev_priv,
  1296. struct drm_file *file_priv,
  1297. struct vmw_framebuffer *vfb,
  1298. struct vmw_surface *surface,
  1299. uint32_t sid,
  1300. int32_t destX, int32_t destY,
  1301. struct drm_vmw_rect *clips,
  1302. uint32_t num_clips)
  1303. {
  1304. return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
  1305. &surface->res, destX, destY,
  1306. num_clips, 1, NULL);
  1307. }
  1308. int vmw_kms_present(struct vmw_private *dev_priv,
  1309. struct drm_file *file_priv,
  1310. struct vmw_framebuffer *vfb,
  1311. struct vmw_surface *surface,
  1312. uint32_t sid,
  1313. int32_t destX, int32_t destY,
  1314. struct drm_vmw_rect *clips,
  1315. uint32_t num_clips)
  1316. {
  1317. int ret;
  1318. switch (dev_priv->active_display_unit) {
  1319. case vmw_du_screen_target:
  1320. ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
  1321. &surface->res, destX, destY,
  1322. num_clips, 1, NULL);
  1323. break;
  1324. case vmw_du_screen_object:
  1325. ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
  1326. sid, destX, destY, clips,
  1327. num_clips);
  1328. break;
  1329. default:
  1330. WARN_ONCE(true,
  1331. "Present called with invalid display system.\n");
  1332. ret = -ENOSYS;
  1333. break;
  1334. }
  1335. if (ret)
  1336. return ret;
  1337. vmw_fifo_flush(dev_priv, false);
  1338. return 0;
  1339. }
  1340. static void
  1341. vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
  1342. {
  1343. if (dev_priv->hotplug_mode_update_property)
  1344. return;
  1345. dev_priv->hotplug_mode_update_property =
  1346. drm_property_create_range(dev_priv->dev,
  1347. DRM_MODE_PROP_IMMUTABLE,
  1348. "hotplug_mode_update", 0, 1);
  1349. if (!dev_priv->hotplug_mode_update_property)
  1350. return;
  1351. }
  1352. int vmw_kms_init(struct vmw_private *dev_priv)
  1353. {
  1354. struct drm_device *dev = dev_priv->dev;
  1355. int ret;
  1356. drm_mode_config_init(dev);
  1357. dev->mode_config.funcs = &vmw_kms_funcs;
  1358. dev->mode_config.min_width = 1;
  1359. dev->mode_config.min_height = 1;
  1360. dev->mode_config.max_width = dev_priv->texture_max_width;
  1361. dev->mode_config.max_height = dev_priv->texture_max_height;
  1362. drm_mode_create_suggested_offset_properties(dev);
  1363. vmw_kms_create_hotplug_mode_update_property(dev_priv);
  1364. ret = vmw_kms_stdu_init_display(dev_priv);
  1365. if (ret) {
  1366. ret = vmw_kms_sou_init_display(dev_priv);
  1367. if (ret) /* Fallback */
  1368. ret = vmw_kms_ldu_init_display(dev_priv);
  1369. }
  1370. return ret;
  1371. }
  1372. int vmw_kms_close(struct vmw_private *dev_priv)
  1373. {
  1374. int ret = 0;
  1375. /*
  1376. * Docs says we should take the lock before calling this function
  1377. * but since it destroys encoders and our destructor calls
  1378. * drm_encoder_cleanup which takes the lock we deadlock.
  1379. */
  1380. drm_mode_config_cleanup(dev_priv->dev);
  1381. if (dev_priv->active_display_unit == vmw_du_legacy)
  1382. ret = vmw_kms_ldu_close_display(dev_priv);
  1383. return ret;
  1384. }
  1385. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  1386. struct drm_file *file_priv)
  1387. {
  1388. struct drm_vmw_cursor_bypass_arg *arg = data;
  1389. struct vmw_display_unit *du;
  1390. struct drm_crtc *crtc;
  1391. int ret = 0;
  1392. mutex_lock(&dev->mode_config.mutex);
  1393. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  1394. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1395. du = vmw_crtc_to_du(crtc);
  1396. du->hotspot_x = arg->xhot;
  1397. du->hotspot_y = arg->yhot;
  1398. }
  1399. mutex_unlock(&dev->mode_config.mutex);
  1400. return 0;
  1401. }
  1402. crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
  1403. if (!crtc) {
  1404. ret = -ENOENT;
  1405. goto out;
  1406. }
  1407. du = vmw_crtc_to_du(crtc);
  1408. du->hotspot_x = arg->xhot;
  1409. du->hotspot_y = arg->yhot;
  1410. out:
  1411. mutex_unlock(&dev->mode_config.mutex);
  1412. return ret;
  1413. }
  1414. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  1415. unsigned width, unsigned height, unsigned pitch,
  1416. unsigned bpp, unsigned depth)
  1417. {
  1418. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1419. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  1420. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1421. vmw_mmio_write(pitch, vmw_priv->mmio_virt +
  1422. SVGA_FIFO_PITCHLOCK);
  1423. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  1424. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  1425. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  1426. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  1427. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  1428. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  1429. return -EINVAL;
  1430. }
  1431. return 0;
  1432. }
  1433. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  1434. {
  1435. struct vmw_vga_topology_state *save;
  1436. uint32_t i;
  1437. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  1438. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  1439. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  1440. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1441. vmw_priv->vga_pitchlock =
  1442. vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
  1443. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1444. vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
  1445. SVGA_FIFO_PITCHLOCK);
  1446. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1447. return 0;
  1448. vmw_priv->num_displays = vmw_read(vmw_priv,
  1449. SVGA_REG_NUM_GUEST_DISPLAYS);
  1450. if (vmw_priv->num_displays == 0)
  1451. vmw_priv->num_displays = 1;
  1452. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1453. save = &vmw_priv->vga_save[i];
  1454. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1455. save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
  1456. save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
  1457. save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
  1458. save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
  1459. save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
  1460. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1461. if (i == 0 && vmw_priv->num_displays == 1 &&
  1462. save->width == 0 && save->height == 0) {
  1463. /*
  1464. * It should be fairly safe to assume that these
  1465. * values are uninitialized.
  1466. */
  1467. save->width = vmw_priv->vga_width - save->pos_x;
  1468. save->height = vmw_priv->vga_height - save->pos_y;
  1469. }
  1470. }
  1471. return 0;
  1472. }
  1473. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  1474. {
  1475. struct vmw_vga_topology_state *save;
  1476. uint32_t i;
  1477. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  1478. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  1479. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  1480. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1481. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
  1482. vmw_priv->vga_pitchlock);
  1483. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1484. vmw_mmio_write(vmw_priv->vga_pitchlock,
  1485. vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  1486. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1487. return 0;
  1488. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1489. save = &vmw_priv->vga_save[i];
  1490. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1491. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
  1492. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
  1493. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
  1494. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
  1495. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
  1496. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1497. }
  1498. return 0;
  1499. }
  1500. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  1501. uint32_t pitch,
  1502. uint32_t height)
  1503. {
  1504. return ((u64) pitch * (u64) height) < (u64)
  1505. ((dev_priv->active_display_unit == vmw_du_screen_target) ?
  1506. dev_priv->prim_bb_mem : dev_priv->vram_size);
  1507. }
  1508. /**
  1509. * Function called by DRM code called with vbl_lock held.
  1510. */
  1511. u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
  1512. {
  1513. return 0;
  1514. }
  1515. /**
  1516. * Function called by DRM code called with vbl_lock held.
  1517. */
  1518. int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
  1519. {
  1520. return -ENOSYS;
  1521. }
  1522. /**
  1523. * Function called by DRM code called with vbl_lock held.
  1524. */
  1525. void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
  1526. {
  1527. }
  1528. /*
  1529. * Small shared kms functions.
  1530. */
  1531. static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
  1532. struct drm_vmw_rect *rects)
  1533. {
  1534. struct drm_device *dev = dev_priv->dev;
  1535. struct vmw_display_unit *du;
  1536. struct drm_connector *con;
  1537. mutex_lock(&dev->mode_config.mutex);
  1538. #if 0
  1539. {
  1540. unsigned int i;
  1541. DRM_INFO("%s: new layout ", __func__);
  1542. for (i = 0; i < num; i++)
  1543. DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
  1544. rects[i].w, rects[i].h);
  1545. DRM_INFO("\n");
  1546. }
  1547. #endif
  1548. list_for_each_entry(con, &dev->mode_config.connector_list, head) {
  1549. du = vmw_connector_to_du(con);
  1550. if (num > du->unit) {
  1551. du->pref_width = rects[du->unit].w;
  1552. du->pref_height = rects[du->unit].h;
  1553. du->pref_active = true;
  1554. du->gui_x = rects[du->unit].x;
  1555. du->gui_y = rects[du->unit].y;
  1556. drm_object_property_set_value
  1557. (&con->base, dev->mode_config.suggested_x_property,
  1558. du->gui_x);
  1559. drm_object_property_set_value
  1560. (&con->base, dev->mode_config.suggested_y_property,
  1561. du->gui_y);
  1562. } else {
  1563. du->pref_width = 800;
  1564. du->pref_height = 600;
  1565. du->pref_active = false;
  1566. drm_object_property_set_value
  1567. (&con->base, dev->mode_config.suggested_x_property,
  1568. 0);
  1569. drm_object_property_set_value
  1570. (&con->base, dev->mode_config.suggested_y_property,
  1571. 0);
  1572. }
  1573. con->status = vmw_du_connector_detect(con, true);
  1574. }
  1575. mutex_unlock(&dev->mode_config.mutex);
  1576. drm_sysfs_hotplug_event(dev);
  1577. return 0;
  1578. }
  1579. int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  1580. u16 *r, u16 *g, u16 *b,
  1581. uint32_t size,
  1582. struct drm_modeset_acquire_ctx *ctx)
  1583. {
  1584. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  1585. int i;
  1586. for (i = 0; i < size; i++) {
  1587. DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
  1588. r[i], g[i], b[i]);
  1589. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
  1590. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
  1591. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
  1592. }
  1593. return 0;
  1594. }
  1595. int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
  1596. {
  1597. return 0;
  1598. }
  1599. enum drm_connector_status
  1600. vmw_du_connector_detect(struct drm_connector *connector, bool force)
  1601. {
  1602. uint32_t num_displays;
  1603. struct drm_device *dev = connector->dev;
  1604. struct vmw_private *dev_priv = vmw_priv(dev);
  1605. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1606. num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
  1607. return ((vmw_connector_to_du(connector)->unit < num_displays &&
  1608. du->pref_active) ?
  1609. connector_status_connected : connector_status_disconnected);
  1610. }
  1611. static struct drm_display_mode vmw_kms_connector_builtin[] = {
  1612. /* 640x480@60Hz */
  1613. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  1614. 752, 800, 0, 480, 489, 492, 525, 0,
  1615. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1616. /* 800x600@60Hz */
  1617. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  1618. 968, 1056, 0, 600, 601, 605, 628, 0,
  1619. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1620. /* 1024x768@60Hz */
  1621. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  1622. 1184, 1344, 0, 768, 771, 777, 806, 0,
  1623. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1624. /* 1152x864@75Hz */
  1625. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  1626. 1344, 1600, 0, 864, 865, 868, 900, 0,
  1627. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1628. /* 1280x768@60Hz */
  1629. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  1630. 1472, 1664, 0, 768, 771, 778, 798, 0,
  1631. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1632. /* 1280x800@60Hz */
  1633. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  1634. 1480, 1680, 0, 800, 803, 809, 831, 0,
  1635. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1636. /* 1280x960@60Hz */
  1637. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  1638. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  1639. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1640. /* 1280x1024@60Hz */
  1641. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  1642. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  1643. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1644. /* 1360x768@60Hz */
  1645. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  1646. 1536, 1792, 0, 768, 771, 777, 795, 0,
  1647. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1648. /* 1440x1050@60Hz */
  1649. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  1650. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  1651. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1652. /* 1440x900@60Hz */
  1653. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  1654. 1672, 1904, 0, 900, 903, 909, 934, 0,
  1655. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1656. /* 1600x1200@60Hz */
  1657. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  1658. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  1659. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1660. /* 1680x1050@60Hz */
  1661. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  1662. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  1663. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1664. /* 1792x1344@60Hz */
  1665. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  1666. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  1667. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1668. /* 1853x1392@60Hz */
  1669. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  1670. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  1671. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1672. /* 1920x1200@60Hz */
  1673. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  1674. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  1675. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1676. /* 1920x1440@60Hz */
  1677. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  1678. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  1679. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1680. /* 2560x1600@60Hz */
  1681. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  1682. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  1683. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1684. /* Terminate */
  1685. { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
  1686. };
  1687. /**
  1688. * vmw_guess_mode_timing - Provide fake timings for a
  1689. * 60Hz vrefresh mode.
  1690. *
  1691. * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
  1692. * members filled in.
  1693. */
  1694. void vmw_guess_mode_timing(struct drm_display_mode *mode)
  1695. {
  1696. mode->hsync_start = mode->hdisplay + 50;
  1697. mode->hsync_end = mode->hsync_start + 50;
  1698. mode->htotal = mode->hsync_end + 50;
  1699. mode->vsync_start = mode->vdisplay + 50;
  1700. mode->vsync_end = mode->vsync_start + 50;
  1701. mode->vtotal = mode->vsync_end + 50;
  1702. mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
  1703. mode->vrefresh = drm_mode_vrefresh(mode);
  1704. }
  1705. int vmw_du_connector_fill_modes(struct drm_connector *connector,
  1706. uint32_t max_width, uint32_t max_height)
  1707. {
  1708. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1709. struct drm_device *dev = connector->dev;
  1710. struct vmw_private *dev_priv = vmw_priv(dev);
  1711. struct drm_display_mode *mode = NULL;
  1712. struct drm_display_mode *bmode;
  1713. struct drm_display_mode prefmode = { DRM_MODE("preferred",
  1714. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  1715. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1716. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  1717. };
  1718. int i;
  1719. u32 assumed_bpp = 4;
  1720. if (dev_priv->assume_16bpp)
  1721. assumed_bpp = 2;
  1722. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1723. max_width = min(max_width, dev_priv->stdu_max_width);
  1724. max_width = min(max_width, dev_priv->texture_max_width);
  1725. max_height = min(max_height, dev_priv->stdu_max_height);
  1726. max_height = min(max_height, dev_priv->texture_max_height);
  1727. }
  1728. /* Add preferred mode */
  1729. mode = drm_mode_duplicate(dev, &prefmode);
  1730. if (!mode)
  1731. return 0;
  1732. mode->hdisplay = du->pref_width;
  1733. mode->vdisplay = du->pref_height;
  1734. vmw_guess_mode_timing(mode);
  1735. if (vmw_kms_validate_mode_vram(dev_priv,
  1736. mode->hdisplay * assumed_bpp,
  1737. mode->vdisplay)) {
  1738. drm_mode_probed_add(connector, mode);
  1739. } else {
  1740. drm_mode_destroy(dev, mode);
  1741. mode = NULL;
  1742. }
  1743. if (du->pref_mode) {
  1744. list_del_init(&du->pref_mode->head);
  1745. drm_mode_destroy(dev, du->pref_mode);
  1746. }
  1747. /* mode might be null here, this is intended */
  1748. du->pref_mode = mode;
  1749. for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
  1750. bmode = &vmw_kms_connector_builtin[i];
  1751. if (bmode->hdisplay > max_width ||
  1752. bmode->vdisplay > max_height)
  1753. continue;
  1754. if (!vmw_kms_validate_mode_vram(dev_priv,
  1755. bmode->hdisplay * assumed_bpp,
  1756. bmode->vdisplay))
  1757. continue;
  1758. mode = drm_mode_duplicate(dev, bmode);
  1759. if (!mode)
  1760. return 0;
  1761. mode->vrefresh = drm_mode_vrefresh(mode);
  1762. drm_mode_probed_add(connector, mode);
  1763. }
  1764. drm_mode_connector_list_update(connector);
  1765. /* Move the prefered mode first, help apps pick the right mode. */
  1766. drm_mode_sort(&connector->modes);
  1767. return 1;
  1768. }
  1769. int vmw_du_connector_set_property(struct drm_connector *connector,
  1770. struct drm_property *property,
  1771. uint64_t val)
  1772. {
  1773. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1774. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1775. if (property == dev_priv->implicit_placement_property)
  1776. du->is_implicit = val;
  1777. return 0;
  1778. }
  1779. /**
  1780. * vmw_du_connector_atomic_set_property - Atomic version of get property
  1781. *
  1782. * @crtc - crtc the property is associated with
  1783. *
  1784. * Returns:
  1785. * Zero on success, negative errno on failure.
  1786. */
  1787. int
  1788. vmw_du_connector_atomic_set_property(struct drm_connector *connector,
  1789. struct drm_connector_state *state,
  1790. struct drm_property *property,
  1791. uint64_t val)
  1792. {
  1793. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1794. struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
  1795. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1796. if (property == dev_priv->implicit_placement_property) {
  1797. vcs->is_implicit = val;
  1798. /*
  1799. * We should really be doing a drm_atomic_commit() to
  1800. * commit the new state, but since this doesn't cause
  1801. * an immedate state change, this is probably ok
  1802. */
  1803. du->is_implicit = vcs->is_implicit;
  1804. } else {
  1805. return -EINVAL;
  1806. }
  1807. return 0;
  1808. }
  1809. /**
  1810. * vmw_du_connector_atomic_get_property - Atomic version of get property
  1811. *
  1812. * @connector - connector the property is associated with
  1813. *
  1814. * Returns:
  1815. * Zero on success, negative errno on failure.
  1816. */
  1817. int
  1818. vmw_du_connector_atomic_get_property(struct drm_connector *connector,
  1819. const struct drm_connector_state *state,
  1820. struct drm_property *property,
  1821. uint64_t *val)
  1822. {
  1823. struct vmw_private *dev_priv = vmw_priv(connector->dev);
  1824. struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
  1825. if (property == dev_priv->implicit_placement_property)
  1826. *val = vcs->is_implicit;
  1827. else {
  1828. DRM_ERROR("Invalid Property %s\n", property->name);
  1829. return -EINVAL;
  1830. }
  1831. return 0;
  1832. }
  1833. int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
  1834. struct drm_file *file_priv)
  1835. {
  1836. struct vmw_private *dev_priv = vmw_priv(dev);
  1837. struct drm_vmw_update_layout_arg *arg =
  1838. (struct drm_vmw_update_layout_arg *)data;
  1839. void __user *user_rects;
  1840. struct drm_vmw_rect *rects;
  1841. unsigned rects_size;
  1842. int ret;
  1843. int i;
  1844. u64 total_pixels = 0;
  1845. struct drm_mode_config *mode_config = &dev->mode_config;
  1846. struct drm_vmw_rect bounding_box = {0};
  1847. if (!arg->num_outputs) {
  1848. struct drm_vmw_rect def_rect = {0, 0, 800, 600};
  1849. vmw_du_update_layout(dev_priv, 1, &def_rect);
  1850. return 0;
  1851. }
  1852. rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
  1853. rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
  1854. GFP_KERNEL);
  1855. if (unlikely(!rects))
  1856. return -ENOMEM;
  1857. user_rects = (void __user *)(unsigned long)arg->rects;
  1858. ret = copy_from_user(rects, user_rects, rects_size);
  1859. if (unlikely(ret != 0)) {
  1860. DRM_ERROR("Failed to get rects.\n");
  1861. ret = -EFAULT;
  1862. goto out_free;
  1863. }
  1864. for (i = 0; i < arg->num_outputs; ++i) {
  1865. if (rects[i].x < 0 ||
  1866. rects[i].y < 0 ||
  1867. rects[i].x + rects[i].w > mode_config->max_width ||
  1868. rects[i].y + rects[i].h > mode_config->max_height) {
  1869. DRM_ERROR("Invalid GUI layout.\n");
  1870. ret = -EINVAL;
  1871. goto out_free;
  1872. }
  1873. /*
  1874. * bounding_box.w and bunding_box.h are used as
  1875. * lower-right coordinates
  1876. */
  1877. if (rects[i].x + rects[i].w > bounding_box.w)
  1878. bounding_box.w = rects[i].x + rects[i].w;
  1879. if (rects[i].y + rects[i].h > bounding_box.h)
  1880. bounding_box.h = rects[i].y + rects[i].h;
  1881. total_pixels += (u64) rects[i].w * (u64) rects[i].h;
  1882. }
  1883. if (dev_priv->active_display_unit == vmw_du_screen_target) {
  1884. /*
  1885. * For Screen Targets, the limits for a toplogy are:
  1886. * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
  1887. * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
  1888. */
  1889. u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
  1890. u64 pixel_mem = total_pixels * 4;
  1891. if (bb_mem > dev_priv->prim_bb_mem) {
  1892. DRM_ERROR("Topology is beyond supported limits.\n");
  1893. ret = -EINVAL;
  1894. goto out_free;
  1895. }
  1896. if (pixel_mem > dev_priv->prim_bb_mem) {
  1897. DRM_ERROR("Combined output size too large\n");
  1898. ret = -EINVAL;
  1899. goto out_free;
  1900. }
  1901. }
  1902. vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
  1903. out_free:
  1904. kfree(rects);
  1905. return ret;
  1906. }
  1907. /**
  1908. * vmw_kms_helper_dirty - Helper to build commands and perform actions based
  1909. * on a set of cliprects and a set of display units.
  1910. *
  1911. * @dev_priv: Pointer to a device private structure.
  1912. * @framebuffer: Pointer to the framebuffer on which to perform the actions.
  1913. * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
  1914. * Cliprects are given in framebuffer coordinates.
  1915. * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
  1916. * be NULL. Cliprects are given in source coordinates.
  1917. * @dest_x: X coordinate offset for the crtc / destination clip rects.
  1918. * @dest_y: Y coordinate offset for the crtc / destination clip rects.
  1919. * @num_clips: Number of cliprects in the @clips or @vclips array.
  1920. * @increment: Integer with which to increment the clip counter when looping.
  1921. * Used to skip a predetermined number of clip rects.
  1922. * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
  1923. */
  1924. int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
  1925. struct vmw_framebuffer *framebuffer,
  1926. const struct drm_clip_rect *clips,
  1927. const struct drm_vmw_rect *vclips,
  1928. s32 dest_x, s32 dest_y,
  1929. int num_clips,
  1930. int increment,
  1931. struct vmw_kms_dirty *dirty)
  1932. {
  1933. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  1934. struct drm_crtc *crtc;
  1935. u32 num_units = 0;
  1936. u32 i, k;
  1937. dirty->dev_priv = dev_priv;
  1938. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
  1939. if (crtc->primary->fb != &framebuffer->base)
  1940. continue;
  1941. units[num_units++] = vmw_crtc_to_du(crtc);
  1942. }
  1943. for (k = 0; k < num_units; k++) {
  1944. struct vmw_display_unit *unit = units[k];
  1945. s32 crtc_x = unit->crtc.x;
  1946. s32 crtc_y = unit->crtc.y;
  1947. s32 crtc_width = unit->crtc.mode.hdisplay;
  1948. s32 crtc_height = unit->crtc.mode.vdisplay;
  1949. const struct drm_clip_rect *clips_ptr = clips;
  1950. const struct drm_vmw_rect *vclips_ptr = vclips;
  1951. dirty->unit = unit;
  1952. if (dirty->fifo_reserve_size > 0) {
  1953. dirty->cmd = vmw_fifo_reserve(dev_priv,
  1954. dirty->fifo_reserve_size);
  1955. if (!dirty->cmd) {
  1956. DRM_ERROR("Couldn't reserve fifo space "
  1957. "for dirty blits.\n");
  1958. return -ENOMEM;
  1959. }
  1960. memset(dirty->cmd, 0, dirty->fifo_reserve_size);
  1961. }
  1962. dirty->num_hits = 0;
  1963. for (i = 0; i < num_clips; i++, clips_ptr += increment,
  1964. vclips_ptr += increment) {
  1965. s32 clip_left;
  1966. s32 clip_top;
  1967. /*
  1968. * Select clip array type. Note that integer type
  1969. * in @clips is unsigned short, whereas in @vclips
  1970. * it's 32-bit.
  1971. */
  1972. if (clips) {
  1973. dirty->fb_x = (s32) clips_ptr->x1;
  1974. dirty->fb_y = (s32) clips_ptr->y1;
  1975. dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
  1976. crtc_x;
  1977. dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
  1978. crtc_y;
  1979. } else {
  1980. dirty->fb_x = vclips_ptr->x;
  1981. dirty->fb_y = vclips_ptr->y;
  1982. dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
  1983. dest_x - crtc_x;
  1984. dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
  1985. dest_y - crtc_y;
  1986. }
  1987. dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
  1988. dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
  1989. /* Skip this clip if it's outside the crtc region */
  1990. if (dirty->unit_x1 >= crtc_width ||
  1991. dirty->unit_y1 >= crtc_height ||
  1992. dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
  1993. continue;
  1994. /* Clip right and bottom to crtc limits */
  1995. dirty->unit_x2 = min_t(s32, dirty->unit_x2,
  1996. crtc_width);
  1997. dirty->unit_y2 = min_t(s32, dirty->unit_y2,
  1998. crtc_height);
  1999. /* Clip left and top to crtc limits */
  2000. clip_left = min_t(s32, dirty->unit_x1, 0);
  2001. clip_top = min_t(s32, dirty->unit_y1, 0);
  2002. dirty->unit_x1 -= clip_left;
  2003. dirty->unit_y1 -= clip_top;
  2004. dirty->fb_x -= clip_left;
  2005. dirty->fb_y -= clip_top;
  2006. dirty->clip(dirty);
  2007. }
  2008. dirty->fifo_commit(dirty);
  2009. }
  2010. return 0;
  2011. }
  2012. /**
  2013. * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
  2014. * command submission.
  2015. *
  2016. * @dev_priv. Pointer to a device private structure.
  2017. * @buf: The buffer object
  2018. * @interruptible: Whether to perform waits as interruptible.
  2019. * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
  2020. * The buffer will be validated as a GMR. Already pinned buffers will not be
  2021. * validated.
  2022. *
  2023. * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
  2024. * interrupted by a signal.
  2025. */
  2026. int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
  2027. struct vmw_dma_buffer *buf,
  2028. bool interruptible,
  2029. bool validate_as_mob)
  2030. {
  2031. struct ttm_buffer_object *bo = &buf->base;
  2032. int ret;
  2033. ttm_bo_reserve(bo, false, false, NULL);
  2034. ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
  2035. validate_as_mob);
  2036. if (ret)
  2037. ttm_bo_unreserve(bo);
  2038. return ret;
  2039. }
  2040. /**
  2041. * vmw_kms_helper_buffer_revert - Undo the actions of
  2042. * vmw_kms_helper_buffer_prepare.
  2043. *
  2044. * @res: Pointer to the buffer object.
  2045. *
  2046. * Helper to be used if an error forces the caller to undo the actions of
  2047. * vmw_kms_helper_buffer_prepare.
  2048. */
  2049. void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
  2050. {
  2051. if (buf)
  2052. ttm_bo_unreserve(&buf->base);
  2053. }
  2054. /**
  2055. * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
  2056. * kms command submission.
  2057. *
  2058. * @dev_priv: Pointer to a device private structure.
  2059. * @file_priv: Pointer to a struct drm_file representing the caller's
  2060. * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
  2061. * if non-NULL, @user_fence_rep must be non-NULL.
  2062. * @buf: The buffer object.
  2063. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  2064. * ref-counted fence pointer is returned here.
  2065. * @user_fence_rep: Optional pointer to a user-space provided struct
  2066. * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
  2067. * function copies fence data to user-space in a fail-safe manner.
  2068. */
  2069. void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
  2070. struct drm_file *file_priv,
  2071. struct vmw_dma_buffer *buf,
  2072. struct vmw_fence_obj **out_fence,
  2073. struct drm_vmw_fence_rep __user *
  2074. user_fence_rep)
  2075. {
  2076. struct vmw_fence_obj *fence;
  2077. uint32_t handle;
  2078. int ret;
  2079. ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
  2080. file_priv ? &handle : NULL);
  2081. if (buf)
  2082. vmw_fence_single_bo(&buf->base, fence);
  2083. if (file_priv)
  2084. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
  2085. ret, user_fence_rep, fence,
  2086. handle, -1, NULL);
  2087. if (out_fence)
  2088. *out_fence = fence;
  2089. else
  2090. vmw_fence_obj_unreference(&fence);
  2091. vmw_kms_helper_buffer_revert(buf);
  2092. }
  2093. /**
  2094. * vmw_kms_helper_resource_revert - Undo the actions of
  2095. * vmw_kms_helper_resource_prepare.
  2096. *
  2097. * @res: Pointer to the resource. Typically a surface.
  2098. *
  2099. * Helper to be used if an error forces the caller to undo the actions of
  2100. * vmw_kms_helper_resource_prepare.
  2101. */
  2102. void vmw_kms_helper_resource_revert(struct vmw_resource *res)
  2103. {
  2104. vmw_kms_helper_buffer_revert(res->backup);
  2105. vmw_resource_unreserve(res, false, NULL, 0);
  2106. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2107. }
  2108. /**
  2109. * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
  2110. * command submission.
  2111. *
  2112. * @res: Pointer to the resource. Typically a surface.
  2113. * @interruptible: Whether to perform waits as interruptible.
  2114. *
  2115. * Reserves and validates also the backup buffer if a guest-backed resource.
  2116. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  2117. * interrupted by a signal.
  2118. */
  2119. int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
  2120. bool interruptible)
  2121. {
  2122. int ret = 0;
  2123. if (interruptible)
  2124. ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
  2125. else
  2126. mutex_lock(&res->dev_priv->cmdbuf_mutex);
  2127. if (unlikely(ret != 0))
  2128. return -ERESTARTSYS;
  2129. ret = vmw_resource_reserve(res, interruptible, false);
  2130. if (ret)
  2131. goto out_unlock;
  2132. if (res->backup) {
  2133. ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
  2134. interruptible,
  2135. res->dev_priv->has_mob);
  2136. if (ret)
  2137. goto out_unreserve;
  2138. }
  2139. ret = vmw_resource_validate(res);
  2140. if (ret)
  2141. goto out_revert;
  2142. return 0;
  2143. out_revert:
  2144. vmw_kms_helper_buffer_revert(res->backup);
  2145. out_unreserve:
  2146. vmw_resource_unreserve(res, false, NULL, 0);
  2147. out_unlock:
  2148. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2149. return ret;
  2150. }
  2151. /**
  2152. * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
  2153. * kms command submission.
  2154. *
  2155. * @res: Pointer to the resource. Typically a surface.
  2156. * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
  2157. * ref-counted fence pointer is returned here.
  2158. */
  2159. void vmw_kms_helper_resource_finish(struct vmw_resource *res,
  2160. struct vmw_fence_obj **out_fence)
  2161. {
  2162. if (res->backup || out_fence)
  2163. vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
  2164. out_fence, NULL);
  2165. vmw_resource_unreserve(res, false, NULL, 0);
  2166. mutex_unlock(&res->dev_priv->cmdbuf_mutex);
  2167. }
  2168. /**
  2169. * vmw_kms_update_proxy - Helper function to update a proxy surface from
  2170. * its backing MOB.
  2171. *
  2172. * @res: Pointer to the surface resource
  2173. * @clips: Clip rects in framebuffer (surface) space.
  2174. * @num_clips: Number of clips in @clips.
  2175. * @increment: Integer with which to increment the clip counter when looping.
  2176. * Used to skip a predetermined number of clip rects.
  2177. *
  2178. * This function makes sure the proxy surface is updated from its backing MOB
  2179. * using the region given by @clips. The surface resource @res and its backing
  2180. * MOB needs to be reserved and validated on call.
  2181. */
  2182. int vmw_kms_update_proxy(struct vmw_resource *res,
  2183. const struct drm_clip_rect *clips,
  2184. unsigned num_clips,
  2185. int increment)
  2186. {
  2187. struct vmw_private *dev_priv = res->dev_priv;
  2188. struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
  2189. struct {
  2190. SVGA3dCmdHeader header;
  2191. SVGA3dCmdUpdateGBImage body;
  2192. } *cmd;
  2193. SVGA3dBox *box;
  2194. size_t copy_size = 0;
  2195. int i;
  2196. if (!clips)
  2197. return 0;
  2198. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
  2199. if (!cmd) {
  2200. DRM_ERROR("Couldn't reserve fifo space for proxy surface "
  2201. "update.\n");
  2202. return -ENOMEM;
  2203. }
  2204. for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
  2205. box = &cmd->body.box;
  2206. cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
  2207. cmd->header.size = sizeof(cmd->body);
  2208. cmd->body.image.sid = res->id;
  2209. cmd->body.image.face = 0;
  2210. cmd->body.image.mipmap = 0;
  2211. if (clips->x1 > size->width || clips->x2 > size->width ||
  2212. clips->y1 > size->height || clips->y2 > size->height) {
  2213. DRM_ERROR("Invalid clips outsize of framebuffer.\n");
  2214. return -EINVAL;
  2215. }
  2216. box->x = clips->x1;
  2217. box->y = clips->y1;
  2218. box->z = 0;
  2219. box->w = clips->x2 - clips->x1;
  2220. box->h = clips->y2 - clips->y1;
  2221. box->d = 1;
  2222. copy_size += sizeof(*cmd);
  2223. }
  2224. vmw_fifo_commit(dev_priv, copy_size);
  2225. return 0;
  2226. }
  2227. int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
  2228. unsigned unit,
  2229. u32 max_width,
  2230. u32 max_height,
  2231. struct drm_connector **p_con,
  2232. struct drm_crtc **p_crtc,
  2233. struct drm_display_mode **p_mode)
  2234. {
  2235. struct drm_connector *con;
  2236. struct vmw_display_unit *du;
  2237. struct drm_display_mode *mode;
  2238. int i = 0;
  2239. list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
  2240. head) {
  2241. if (i == unit)
  2242. break;
  2243. ++i;
  2244. }
  2245. if (i != unit) {
  2246. DRM_ERROR("Could not find initial display unit.\n");
  2247. return -EINVAL;
  2248. }
  2249. if (list_empty(&con->modes))
  2250. (void) vmw_du_connector_fill_modes(con, max_width, max_height);
  2251. if (list_empty(&con->modes)) {
  2252. DRM_ERROR("Could not find initial display mode.\n");
  2253. return -EINVAL;
  2254. }
  2255. du = vmw_connector_to_du(con);
  2256. *p_con = con;
  2257. *p_crtc = &du->crtc;
  2258. list_for_each_entry(mode, &con->modes, head) {
  2259. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  2260. break;
  2261. }
  2262. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  2263. *p_mode = mode;
  2264. else {
  2265. WARN_ONCE(true, "Could not find initial preferred mode.\n");
  2266. *p_mode = list_first_entry(&con->modes,
  2267. struct drm_display_mode,
  2268. head);
  2269. }
  2270. return 0;
  2271. }
  2272. /**
  2273. * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
  2274. *
  2275. * @dev_priv: Pointer to a device private struct.
  2276. * @du: The display unit of the crtc.
  2277. */
  2278. void vmw_kms_del_active(struct vmw_private *dev_priv,
  2279. struct vmw_display_unit *du)
  2280. {
  2281. mutex_lock(&dev_priv->global_kms_state_mutex);
  2282. if (du->active_implicit) {
  2283. if (--(dev_priv->num_implicit) == 0)
  2284. dev_priv->implicit_fb = NULL;
  2285. du->active_implicit = false;
  2286. }
  2287. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2288. }
  2289. /**
  2290. * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
  2291. *
  2292. * @vmw_priv: Pointer to a device private struct.
  2293. * @du: The display unit of the crtc.
  2294. * @vfb: The implicit framebuffer
  2295. *
  2296. * Registers a binding to an implicit framebuffer.
  2297. */
  2298. void vmw_kms_add_active(struct vmw_private *dev_priv,
  2299. struct vmw_display_unit *du,
  2300. struct vmw_framebuffer *vfb)
  2301. {
  2302. mutex_lock(&dev_priv->global_kms_state_mutex);
  2303. WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
  2304. if (!du->active_implicit && du->is_implicit) {
  2305. dev_priv->implicit_fb = vfb;
  2306. du->active_implicit = true;
  2307. dev_priv->num_implicit++;
  2308. }
  2309. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2310. }
  2311. /**
  2312. * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
  2313. *
  2314. * @dev_priv: Pointer to device-private struct.
  2315. * @crtc: The crtc we want to flip.
  2316. *
  2317. * Returns true or false depending whether it's OK to flip this crtc
  2318. * based on the criterion that we must not have more than one implicit
  2319. * frame-buffer at any one time.
  2320. */
  2321. bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
  2322. struct drm_crtc *crtc)
  2323. {
  2324. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  2325. bool ret;
  2326. mutex_lock(&dev_priv->global_kms_state_mutex);
  2327. ret = !du->is_implicit || dev_priv->num_implicit == 1;
  2328. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2329. return ret;
  2330. }
  2331. /**
  2332. * vmw_kms_update_implicit_fb - Update the implicit fb.
  2333. *
  2334. * @dev_priv: Pointer to device-private struct.
  2335. * @crtc: The crtc the new implicit frame-buffer is bound to.
  2336. */
  2337. void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
  2338. struct drm_crtc *crtc)
  2339. {
  2340. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  2341. struct vmw_framebuffer *vfb;
  2342. mutex_lock(&dev_priv->global_kms_state_mutex);
  2343. if (!du->is_implicit)
  2344. goto out_unlock;
  2345. vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
  2346. WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
  2347. dev_priv->implicit_fb != vfb);
  2348. dev_priv->implicit_fb = vfb;
  2349. out_unlock:
  2350. mutex_unlock(&dev_priv->global_kms_state_mutex);
  2351. }
  2352. /**
  2353. * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
  2354. * property.
  2355. *
  2356. * @dev_priv: Pointer to a device private struct.
  2357. * @immutable: Whether the property is immutable.
  2358. *
  2359. * Sets up the implicit placement property unless it's already set up.
  2360. */
  2361. void
  2362. vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
  2363. bool immutable)
  2364. {
  2365. if (dev_priv->implicit_placement_property)
  2366. return;
  2367. dev_priv->implicit_placement_property =
  2368. drm_property_create_range(dev_priv->dev,
  2369. immutable ?
  2370. DRM_MODE_PROP_IMMUTABLE : 0,
  2371. "implicit_placement", 0, 1);
  2372. }
  2373. /**
  2374. * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
  2375. *
  2376. * @set: The configuration to set.
  2377. *
  2378. * The vmwgfx Xorg driver doesn't assign the mode::type member, which
  2379. * when drm_mode_set_crtcinfo is called as part of the configuration setting
  2380. * causes it to return incorrect crtc dimensions causing severe problems in
  2381. * the vmwgfx modesetting. So explicitly clear that member before calling
  2382. * into drm_atomic_helper_set_config.
  2383. */
  2384. int vmw_kms_set_config(struct drm_mode_set *set,
  2385. struct drm_modeset_acquire_ctx *ctx)
  2386. {
  2387. if (set && set->mode)
  2388. set->mode->type = 0;
  2389. return drm_atomic_helper_set_config(set, ctx);
  2390. }