vmwgfx_kms.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009 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. /* Might need a hrtimer here? */
  29. #define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
  30. void vmw_display_unit_cleanup(struct vmw_display_unit *du)
  31. {
  32. if (du->cursor_surface)
  33. vmw_surface_unreference(&du->cursor_surface);
  34. if (du->cursor_dmabuf)
  35. vmw_dmabuf_unreference(&du->cursor_dmabuf);
  36. drm_crtc_cleanup(&du->crtc);
  37. drm_encoder_cleanup(&du->encoder);
  38. drm_connector_cleanup(&du->connector);
  39. }
  40. /*
  41. * Display Unit Cursor functions
  42. */
  43. int vmw_cursor_update_image(struct vmw_private *dev_priv,
  44. u32 *image, u32 width, u32 height,
  45. u32 hotspotX, u32 hotspotY)
  46. {
  47. struct {
  48. u32 cmd;
  49. SVGAFifoCmdDefineAlphaCursor cursor;
  50. } *cmd;
  51. u32 image_size = width * height * 4;
  52. u32 cmd_size = sizeof(*cmd) + image_size;
  53. if (!image)
  54. return -EINVAL;
  55. cmd = vmw_fifo_reserve(dev_priv, cmd_size);
  56. if (unlikely(cmd == NULL)) {
  57. DRM_ERROR("Fifo reserve failed.\n");
  58. return -ENOMEM;
  59. }
  60. memset(cmd, 0, sizeof(*cmd));
  61. memcpy(&cmd[1], image, image_size);
  62. cmd->cmd = cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR);
  63. cmd->cursor.id = cpu_to_le32(0);
  64. cmd->cursor.width = cpu_to_le32(width);
  65. cmd->cursor.height = cpu_to_le32(height);
  66. cmd->cursor.hotspotX = cpu_to_le32(hotspotX);
  67. cmd->cursor.hotspotY = cpu_to_le32(hotspotY);
  68. vmw_fifo_commit(dev_priv, cmd_size);
  69. return 0;
  70. }
  71. void vmw_cursor_update_position(struct vmw_private *dev_priv,
  72. bool show, int x, int y)
  73. {
  74. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  75. uint32_t count;
  76. iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
  77. iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
  78. iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
  79. count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  80. iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  81. }
  82. int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  83. uint32_t handle, uint32_t width, uint32_t height)
  84. {
  85. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  86. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  87. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  88. struct vmw_surface *surface = NULL;
  89. struct vmw_dma_buffer *dmabuf = NULL;
  90. int ret;
  91. /* A lot of the code assumes this */
  92. if (handle && (width != 64 || height != 64))
  93. return -EINVAL;
  94. if (handle) {
  95. ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
  96. handle, &surface);
  97. if (!ret) {
  98. if (!surface->snooper.image) {
  99. DRM_ERROR("surface not suitable for cursor\n");
  100. vmw_surface_unreference(&surface);
  101. return -EINVAL;
  102. }
  103. } else {
  104. ret = vmw_user_dmabuf_lookup(tfile,
  105. handle, &dmabuf);
  106. if (ret) {
  107. DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
  108. return -EINVAL;
  109. }
  110. }
  111. }
  112. /* takedown old cursor */
  113. if (du->cursor_surface) {
  114. du->cursor_surface->snooper.crtc = NULL;
  115. vmw_surface_unreference(&du->cursor_surface);
  116. }
  117. if (du->cursor_dmabuf)
  118. vmw_dmabuf_unreference(&du->cursor_dmabuf);
  119. /* setup new image */
  120. if (surface) {
  121. /* vmw_user_surface_lookup takes one reference */
  122. du->cursor_surface = surface;
  123. du->cursor_surface->snooper.crtc = crtc;
  124. du->cursor_age = du->cursor_surface->snooper.age;
  125. vmw_cursor_update_image(dev_priv, surface->snooper.image,
  126. 64, 64, du->hotspot_x, du->hotspot_y);
  127. } else if (dmabuf) {
  128. struct ttm_bo_kmap_obj map;
  129. unsigned long kmap_offset;
  130. unsigned long kmap_num;
  131. void *virtual;
  132. bool dummy;
  133. /* vmw_user_surface_lookup takes one reference */
  134. du->cursor_dmabuf = dmabuf;
  135. kmap_offset = 0;
  136. kmap_num = (64*64*4) >> PAGE_SHIFT;
  137. ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
  138. if (unlikely(ret != 0)) {
  139. DRM_ERROR("reserve failed\n");
  140. return -EINVAL;
  141. }
  142. ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
  143. if (unlikely(ret != 0))
  144. goto err_unreserve;
  145. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  146. vmw_cursor_update_image(dev_priv, virtual, 64, 64,
  147. du->hotspot_x, du->hotspot_y);
  148. ttm_bo_kunmap(&map);
  149. err_unreserve:
  150. ttm_bo_unreserve(&dmabuf->base);
  151. } else {
  152. vmw_cursor_update_position(dev_priv, false, 0, 0);
  153. return 0;
  154. }
  155. vmw_cursor_update_position(dev_priv, true,
  156. du->cursor_x + du->hotspot_x,
  157. du->cursor_y + du->hotspot_y);
  158. return 0;
  159. }
  160. int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  161. {
  162. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  163. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  164. bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
  165. du->cursor_x = x + crtc->x;
  166. du->cursor_y = y + crtc->y;
  167. vmw_cursor_update_position(dev_priv, shown,
  168. du->cursor_x + du->hotspot_x,
  169. du->cursor_y + du->hotspot_y);
  170. return 0;
  171. }
  172. void vmw_kms_cursor_snoop(struct vmw_surface *srf,
  173. struct ttm_object_file *tfile,
  174. struct ttm_buffer_object *bo,
  175. SVGA3dCmdHeader *header)
  176. {
  177. struct ttm_bo_kmap_obj map;
  178. unsigned long kmap_offset;
  179. unsigned long kmap_num;
  180. SVGA3dCopyBox *box;
  181. unsigned box_count;
  182. void *virtual;
  183. bool dummy;
  184. struct vmw_dma_cmd {
  185. SVGA3dCmdHeader header;
  186. SVGA3dCmdSurfaceDMA dma;
  187. } *cmd;
  188. int i, ret;
  189. cmd = container_of(header, struct vmw_dma_cmd, header);
  190. /* No snooper installed */
  191. if (!srf->snooper.image)
  192. return;
  193. if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
  194. DRM_ERROR("face and mipmap for cursors should never != 0\n");
  195. return;
  196. }
  197. if (cmd->header.size < 64) {
  198. DRM_ERROR("at least one full copy box must be given\n");
  199. return;
  200. }
  201. box = (SVGA3dCopyBox *)&cmd[1];
  202. box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
  203. sizeof(SVGA3dCopyBox);
  204. if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
  205. box->x != 0 || box->y != 0 || box->z != 0 ||
  206. box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
  207. box->d != 1 || box_count != 1) {
  208. /* TODO handle none page aligned offsets */
  209. /* TODO handle more dst & src != 0 */
  210. /* TODO handle more then one copy */
  211. DRM_ERROR("Cant snoop dma request for cursor!\n");
  212. DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
  213. box->srcx, box->srcy, box->srcz,
  214. box->x, box->y, box->z,
  215. box->w, box->h, box->d, box_count,
  216. cmd->dma.guest.ptr.offset);
  217. return;
  218. }
  219. kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
  220. kmap_num = (64*64*4) >> PAGE_SHIFT;
  221. ret = ttm_bo_reserve(bo, true, false, false, 0);
  222. if (unlikely(ret != 0)) {
  223. DRM_ERROR("reserve failed\n");
  224. return;
  225. }
  226. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  227. if (unlikely(ret != 0))
  228. goto err_unreserve;
  229. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  230. if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
  231. memcpy(srf->snooper.image, virtual, 64*64*4);
  232. } else {
  233. /* Image is unsigned pointer. */
  234. for (i = 0; i < box->h; i++)
  235. memcpy(srf->snooper.image + i * 64,
  236. virtual + i * cmd->dma.guest.pitch,
  237. box->w * 4);
  238. }
  239. srf->snooper.age++;
  240. /* we can't call this function from this function since execbuf has
  241. * reserved fifo space.
  242. *
  243. * if (srf->snooper.crtc)
  244. * vmw_ldu_crtc_cursor_update_image(dev_priv,
  245. * srf->snooper.image, 64, 64,
  246. * du->hotspot_x, du->hotspot_y);
  247. */
  248. ttm_bo_kunmap(&map);
  249. err_unreserve:
  250. ttm_bo_unreserve(bo);
  251. }
  252. void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
  253. {
  254. struct drm_device *dev = dev_priv->dev;
  255. struct vmw_display_unit *du;
  256. struct drm_crtc *crtc;
  257. mutex_lock(&dev->mode_config.mutex);
  258. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  259. du = vmw_crtc_to_du(crtc);
  260. if (!du->cursor_surface ||
  261. du->cursor_age == du->cursor_surface->snooper.age)
  262. continue;
  263. du->cursor_age = du->cursor_surface->snooper.age;
  264. vmw_cursor_update_image(dev_priv,
  265. du->cursor_surface->snooper.image,
  266. 64, 64, du->hotspot_x, du->hotspot_y);
  267. }
  268. mutex_unlock(&dev->mode_config.mutex);
  269. }
  270. /*
  271. * Generic framebuffer code
  272. */
  273. int vmw_framebuffer_create_handle(struct drm_framebuffer *fb,
  274. struct drm_file *file_priv,
  275. unsigned int *handle)
  276. {
  277. if (handle)
  278. handle = 0;
  279. return 0;
  280. }
  281. /*
  282. * Surface framebuffer code
  283. */
  284. #define vmw_framebuffer_to_vfbs(x) \
  285. container_of(x, struct vmw_framebuffer_surface, base.base)
  286. struct vmw_framebuffer_surface {
  287. struct vmw_framebuffer base;
  288. struct vmw_surface *surface;
  289. struct vmw_dma_buffer *buffer;
  290. struct list_head head;
  291. struct drm_master *master;
  292. };
  293. void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
  294. {
  295. struct vmw_framebuffer_surface *vfbs =
  296. vmw_framebuffer_to_vfbs(framebuffer);
  297. struct vmw_master *vmaster = vmw_master(vfbs->master);
  298. mutex_lock(&vmaster->fb_surf_mutex);
  299. list_del(&vfbs->head);
  300. mutex_unlock(&vmaster->fb_surf_mutex);
  301. drm_master_put(&vfbs->master);
  302. drm_framebuffer_cleanup(framebuffer);
  303. vmw_surface_unreference(&vfbs->surface);
  304. ttm_base_object_unref(&vfbs->base.user_obj);
  305. kfree(vfbs);
  306. }
  307. static int do_surface_dirty_sou(struct vmw_private *dev_priv,
  308. struct drm_file *file_priv,
  309. struct vmw_framebuffer *framebuffer,
  310. unsigned flags, unsigned color,
  311. struct drm_clip_rect *clips,
  312. unsigned num_clips, int inc)
  313. {
  314. struct drm_clip_rect *clips_ptr;
  315. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  316. struct drm_crtc *crtc;
  317. size_t fifo_size;
  318. int i, num_units;
  319. int ret = 0; /* silence warning */
  320. int left, right, top, bottom;
  321. struct {
  322. SVGA3dCmdHeader header;
  323. SVGA3dCmdBlitSurfaceToScreen body;
  324. } *cmd;
  325. SVGASignedRect *blits;
  326. num_units = 0;
  327. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
  328. head) {
  329. if (crtc->fb != &framebuffer->base)
  330. continue;
  331. units[num_units++] = vmw_crtc_to_du(crtc);
  332. }
  333. BUG_ON(!clips || !num_clips);
  334. fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
  335. cmd = kzalloc(fifo_size, GFP_KERNEL);
  336. if (unlikely(cmd == NULL)) {
  337. DRM_ERROR("Temporary fifo memory alloc failed.\n");
  338. return -ENOMEM;
  339. }
  340. left = clips->x1;
  341. right = clips->x2;
  342. top = clips->y1;
  343. bottom = clips->y2;
  344. /* skip the first clip rect */
  345. for (i = 1, clips_ptr = clips + inc;
  346. i < num_clips; i++, clips_ptr += inc) {
  347. left = min_t(int, left, (int)clips_ptr->x1);
  348. right = max_t(int, right, (int)clips_ptr->x2);
  349. top = min_t(int, top, (int)clips_ptr->y1);
  350. bottom = max_t(int, bottom, (int)clips_ptr->y2);
  351. }
  352. /* only need to do this once */
  353. memset(cmd, 0, fifo_size);
  354. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
  355. cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
  356. cmd->body.srcRect.left = left;
  357. cmd->body.srcRect.right = right;
  358. cmd->body.srcRect.top = top;
  359. cmd->body.srcRect.bottom = bottom;
  360. clips_ptr = clips;
  361. blits = (SVGASignedRect *)&cmd[1];
  362. for (i = 0; i < num_clips; i++, clips_ptr += inc) {
  363. blits[i].left = clips_ptr->x1 - left;
  364. blits[i].right = clips_ptr->x2 - left;
  365. blits[i].top = clips_ptr->y1 - top;
  366. blits[i].bottom = clips_ptr->y2 - top;
  367. }
  368. /* do per unit writing, reuse fifo for each */
  369. for (i = 0; i < num_units; i++) {
  370. struct vmw_display_unit *unit = units[i];
  371. int clip_x1 = left - unit->crtc.x;
  372. int clip_y1 = top - unit->crtc.y;
  373. int clip_x2 = right - unit->crtc.x;
  374. int clip_y2 = bottom - unit->crtc.y;
  375. /* skip any crtcs that misses the clip region */
  376. if (clip_x1 >= unit->crtc.mode.hdisplay ||
  377. clip_y1 >= unit->crtc.mode.vdisplay ||
  378. clip_x2 <= 0 || clip_y2 <= 0)
  379. continue;
  380. /* need to reset sid as it is changed by execbuf */
  381. cmd->body.srcImage.sid = cpu_to_le32(framebuffer->user_handle);
  382. cmd->body.destScreenId = unit->unit;
  383. /*
  384. * The blit command is a lot more resilient then the
  385. * readback command when it comes to clip rects. So its
  386. * okay to go out of bounds.
  387. */
  388. cmd->body.destRect.left = clip_x1;
  389. cmd->body.destRect.right = clip_x2;
  390. cmd->body.destRect.top = clip_y1;
  391. cmd->body.destRect.bottom = clip_y2;
  392. ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
  393. fifo_size, 0, NULL);
  394. if (unlikely(ret != 0))
  395. break;
  396. }
  397. kfree(cmd);
  398. return ret;
  399. }
  400. int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
  401. struct drm_file *file_priv,
  402. unsigned flags, unsigned color,
  403. struct drm_clip_rect *clips,
  404. unsigned num_clips)
  405. {
  406. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  407. struct vmw_master *vmaster = vmw_master(file_priv->master);
  408. struct vmw_framebuffer_surface *vfbs =
  409. vmw_framebuffer_to_vfbs(framebuffer);
  410. struct drm_clip_rect norect;
  411. int ret, inc = 1;
  412. if (unlikely(vfbs->master != file_priv->master))
  413. return -EINVAL;
  414. /* Require ScreenObject support for 3D */
  415. if (!dev_priv->sou_priv)
  416. return -EINVAL;
  417. ret = ttm_read_lock(&vmaster->lock, true);
  418. if (unlikely(ret != 0))
  419. return ret;
  420. if (!num_clips) {
  421. num_clips = 1;
  422. clips = &norect;
  423. norect.x1 = norect.y1 = 0;
  424. norect.x2 = framebuffer->width;
  425. norect.y2 = framebuffer->height;
  426. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  427. num_clips /= 2;
  428. inc = 2; /* skip source rects */
  429. }
  430. ret = do_surface_dirty_sou(dev_priv, file_priv, &vfbs->base,
  431. flags, color,
  432. clips, num_clips, inc);
  433. ttm_read_unlock(&vmaster->lock);
  434. return 0;
  435. }
  436. static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
  437. .destroy = vmw_framebuffer_surface_destroy,
  438. .dirty = vmw_framebuffer_surface_dirty,
  439. .create_handle = vmw_framebuffer_create_handle,
  440. };
  441. static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
  442. struct drm_file *file_priv,
  443. struct vmw_surface *surface,
  444. struct vmw_framebuffer **out,
  445. const struct drm_mode_fb_cmd
  446. *mode_cmd)
  447. {
  448. struct drm_device *dev = dev_priv->dev;
  449. struct vmw_framebuffer_surface *vfbs;
  450. enum SVGA3dSurfaceFormat format;
  451. struct vmw_master *vmaster = vmw_master(file_priv->master);
  452. int ret;
  453. /* 3D is only supported on HWv8 hosts which supports screen objects */
  454. if (!dev_priv->sou_priv)
  455. return -ENOSYS;
  456. /*
  457. * Sanity checks.
  458. */
  459. if (unlikely(surface->mip_levels[0] != 1 ||
  460. surface->num_sizes != 1 ||
  461. surface->sizes[0].width < mode_cmd->width ||
  462. surface->sizes[0].height < mode_cmd->height ||
  463. surface->sizes[0].depth != 1)) {
  464. DRM_ERROR("Incompatible surface dimensions "
  465. "for requested mode.\n");
  466. return -EINVAL;
  467. }
  468. switch (mode_cmd->depth) {
  469. case 32:
  470. format = SVGA3D_A8R8G8B8;
  471. break;
  472. case 24:
  473. format = SVGA3D_X8R8G8B8;
  474. break;
  475. case 16:
  476. format = SVGA3D_R5G6B5;
  477. break;
  478. case 15:
  479. format = SVGA3D_A1R5G5B5;
  480. break;
  481. case 8:
  482. format = SVGA3D_LUMINANCE8;
  483. break;
  484. default:
  485. DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
  486. return -EINVAL;
  487. }
  488. if (unlikely(format != surface->format)) {
  489. DRM_ERROR("Invalid surface format for requested mode.\n");
  490. return -EINVAL;
  491. }
  492. vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
  493. if (!vfbs) {
  494. ret = -ENOMEM;
  495. goto out_err1;
  496. }
  497. ret = drm_framebuffer_init(dev, &vfbs->base.base,
  498. &vmw_framebuffer_surface_funcs);
  499. if (ret)
  500. goto out_err2;
  501. if (!vmw_surface_reference(surface)) {
  502. DRM_ERROR("failed to reference surface %p\n", surface);
  503. goto out_err3;
  504. }
  505. /* XXX get the first 3 from the surface info */
  506. vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
  507. vfbs->base.base.pitch = mode_cmd->pitch;
  508. vfbs->base.base.depth = mode_cmd->depth;
  509. vfbs->base.base.width = mode_cmd->width;
  510. vfbs->base.base.height = mode_cmd->height;
  511. vfbs->surface = surface;
  512. vfbs->base.user_handle = mode_cmd->handle;
  513. vfbs->master = drm_master_get(file_priv->master);
  514. mutex_lock(&vmaster->fb_surf_mutex);
  515. list_add_tail(&vfbs->head, &vmaster->fb_surf);
  516. mutex_unlock(&vmaster->fb_surf_mutex);
  517. *out = &vfbs->base;
  518. return 0;
  519. out_err3:
  520. drm_framebuffer_cleanup(&vfbs->base.base);
  521. out_err2:
  522. kfree(vfbs);
  523. out_err1:
  524. return ret;
  525. }
  526. /*
  527. * Dmabuf framebuffer code
  528. */
  529. #define vmw_framebuffer_to_vfbd(x) \
  530. container_of(x, struct vmw_framebuffer_dmabuf, base.base)
  531. struct vmw_framebuffer_dmabuf {
  532. struct vmw_framebuffer base;
  533. struct vmw_dma_buffer *buffer;
  534. };
  535. void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
  536. {
  537. struct vmw_framebuffer_dmabuf *vfbd =
  538. vmw_framebuffer_to_vfbd(framebuffer);
  539. drm_framebuffer_cleanup(framebuffer);
  540. vmw_dmabuf_unreference(&vfbd->buffer);
  541. ttm_base_object_unref(&vfbd->base.user_obj);
  542. kfree(vfbd);
  543. }
  544. static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
  545. struct vmw_framebuffer *framebuffer,
  546. unsigned flags, unsigned color,
  547. struct drm_clip_rect *clips,
  548. unsigned num_clips, int increment)
  549. {
  550. size_t fifo_size;
  551. int i;
  552. struct {
  553. uint32_t header;
  554. SVGAFifoCmdUpdate body;
  555. } *cmd;
  556. fifo_size = sizeof(*cmd) * num_clips;
  557. cmd = vmw_fifo_reserve(dev_priv, fifo_size);
  558. if (unlikely(cmd == NULL)) {
  559. DRM_ERROR("Fifo reserve failed.\n");
  560. return -ENOMEM;
  561. }
  562. memset(cmd, 0, fifo_size);
  563. for (i = 0; i < num_clips; i++, clips += increment) {
  564. cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
  565. cmd[i].body.x = cpu_to_le32(clips->x1);
  566. cmd[i].body.y = cpu_to_le32(clips->y1);
  567. cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
  568. cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
  569. }
  570. vmw_fifo_commit(dev_priv, fifo_size);
  571. return 0;
  572. }
  573. static int do_dmabuf_define_gmrfb(struct drm_file *file_priv,
  574. struct vmw_private *dev_priv,
  575. struct vmw_framebuffer *framebuffer)
  576. {
  577. int depth = framebuffer->base.depth;
  578. size_t fifo_size;
  579. int ret;
  580. struct {
  581. uint32_t header;
  582. SVGAFifoCmdDefineGMRFB body;
  583. } *cmd;
  584. /* Emulate RGBA support, contrary to svga_reg.h this is not
  585. * supported by hosts. This is only a problem if we are reading
  586. * this value later and expecting what we uploaded back.
  587. */
  588. if (depth == 32)
  589. depth = 24;
  590. fifo_size = sizeof(*cmd);
  591. cmd = kmalloc(fifo_size, GFP_KERNEL);
  592. if (unlikely(cmd == NULL)) {
  593. DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
  594. return -ENOMEM;
  595. }
  596. memset(cmd, 0, fifo_size);
  597. cmd->header = SVGA_CMD_DEFINE_GMRFB;
  598. cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
  599. cmd->body.format.colorDepth = depth;
  600. cmd->body.format.reserved = 0;
  601. cmd->body.bytesPerLine = framebuffer->base.pitch;
  602. cmd->body.ptr.gmrId = framebuffer->user_handle;
  603. cmd->body.ptr.offset = 0;
  604. ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
  605. fifo_size, 0, NULL);
  606. kfree(cmd);
  607. return ret;
  608. }
  609. static int do_dmabuf_dirty_sou(struct drm_file *file_priv,
  610. struct vmw_private *dev_priv,
  611. struct vmw_framebuffer *framebuffer,
  612. unsigned flags, unsigned color,
  613. struct drm_clip_rect *clips,
  614. unsigned num_clips, int increment)
  615. {
  616. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  617. struct drm_clip_rect *clips_ptr;
  618. int i, k, num_units, ret;
  619. struct drm_crtc *crtc;
  620. size_t fifo_size;
  621. struct {
  622. uint32_t header;
  623. SVGAFifoCmdBlitGMRFBToScreen body;
  624. } *blits;
  625. ret = do_dmabuf_define_gmrfb(file_priv, dev_priv, framebuffer);
  626. if (unlikely(ret != 0))
  627. return ret; /* define_gmrfb prints warnings */
  628. fifo_size = sizeof(*blits) * num_clips;
  629. blits = kmalloc(fifo_size, GFP_KERNEL);
  630. if (unlikely(blits == NULL)) {
  631. DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
  632. return -ENOMEM;
  633. }
  634. num_units = 0;
  635. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
  636. if (crtc->fb != &framebuffer->base)
  637. continue;
  638. units[num_units++] = vmw_crtc_to_du(crtc);
  639. }
  640. for (k = 0; k < num_units; k++) {
  641. struct vmw_display_unit *unit = units[k];
  642. int hit_num = 0;
  643. clips_ptr = clips;
  644. for (i = 0; i < num_clips; i++, clips_ptr += increment) {
  645. int clip_x1 = clips_ptr->x1 - unit->crtc.x;
  646. int clip_y1 = clips_ptr->y1 - unit->crtc.y;
  647. int clip_x2 = clips_ptr->x2 - unit->crtc.x;
  648. int clip_y2 = clips_ptr->y2 - unit->crtc.y;
  649. /* skip any crtcs that misses the clip region */
  650. if (clip_x1 >= unit->crtc.mode.hdisplay ||
  651. clip_y1 >= unit->crtc.mode.vdisplay ||
  652. clip_x2 <= 0 || clip_y2 <= 0)
  653. continue;
  654. blits[hit_num].header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
  655. blits[hit_num].body.destScreenId = unit->unit;
  656. blits[hit_num].body.srcOrigin.x = clips_ptr->x1;
  657. blits[hit_num].body.srcOrigin.y = clips_ptr->y1;
  658. blits[hit_num].body.destRect.left = clip_x1;
  659. blits[hit_num].body.destRect.top = clip_y1;
  660. blits[hit_num].body.destRect.right = clip_x2;
  661. blits[hit_num].body.destRect.bottom = clip_y2;
  662. hit_num++;
  663. }
  664. /* no clips hit the crtc */
  665. if (hit_num == 0)
  666. continue;
  667. fifo_size = sizeof(*blits) * hit_num;
  668. ret = vmw_execbuf_process(file_priv, dev_priv, NULL, blits,
  669. fifo_size, 0, NULL);
  670. if (unlikely(ret != 0))
  671. break;
  672. }
  673. kfree(blits);
  674. return ret;
  675. }
  676. int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
  677. struct drm_file *file_priv,
  678. unsigned flags, unsigned color,
  679. struct drm_clip_rect *clips,
  680. unsigned num_clips)
  681. {
  682. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  683. struct vmw_master *vmaster = vmw_master(file_priv->master);
  684. struct vmw_framebuffer_dmabuf *vfbd =
  685. vmw_framebuffer_to_vfbd(framebuffer);
  686. struct drm_clip_rect norect;
  687. int ret, increment = 1;
  688. ret = ttm_read_lock(&vmaster->lock, true);
  689. if (unlikely(ret != 0))
  690. return ret;
  691. if (!num_clips) {
  692. num_clips = 1;
  693. clips = &norect;
  694. norect.x1 = norect.y1 = 0;
  695. norect.x2 = framebuffer->width;
  696. norect.y2 = framebuffer->height;
  697. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  698. num_clips /= 2;
  699. increment = 2;
  700. }
  701. if (dev_priv->ldu_priv) {
  702. ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base,
  703. flags, color,
  704. clips, num_clips, increment);
  705. } else {
  706. ret = do_dmabuf_dirty_sou(file_priv, dev_priv, &vfbd->base,
  707. flags, color,
  708. clips, num_clips, increment);
  709. }
  710. ttm_read_unlock(&vmaster->lock);
  711. return ret;
  712. }
  713. static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
  714. .destroy = vmw_framebuffer_dmabuf_destroy,
  715. .dirty = vmw_framebuffer_dmabuf_dirty,
  716. .create_handle = vmw_framebuffer_create_handle,
  717. };
  718. /**
  719. * Pin the dmabuffer to the start of vram.
  720. */
  721. static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
  722. {
  723. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  724. struct vmw_framebuffer_dmabuf *vfbd =
  725. vmw_framebuffer_to_vfbd(&vfb->base);
  726. int ret;
  727. /* This code should not be used with screen objects */
  728. BUG_ON(dev_priv->sou_priv);
  729. vmw_overlay_pause_all(dev_priv);
  730. ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
  731. vmw_overlay_resume_all(dev_priv);
  732. WARN_ON(ret != 0);
  733. return 0;
  734. }
  735. static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
  736. {
  737. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  738. struct vmw_framebuffer_dmabuf *vfbd =
  739. vmw_framebuffer_to_vfbd(&vfb->base);
  740. if (!vfbd->buffer) {
  741. WARN_ON(!vfbd->buffer);
  742. return 0;
  743. }
  744. return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
  745. }
  746. static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
  747. struct vmw_dma_buffer *dmabuf,
  748. struct vmw_framebuffer **out,
  749. const struct drm_mode_fb_cmd
  750. *mode_cmd)
  751. {
  752. struct drm_device *dev = dev_priv->dev;
  753. struct vmw_framebuffer_dmabuf *vfbd;
  754. unsigned int requested_size;
  755. int ret;
  756. requested_size = mode_cmd->height * mode_cmd->pitch;
  757. if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
  758. DRM_ERROR("Screen buffer object size is too small "
  759. "for requested mode.\n");
  760. return -EINVAL;
  761. }
  762. /* Limited framebuffer color depth support for screen objects */
  763. if (dev_priv->sou_priv) {
  764. switch (mode_cmd->depth) {
  765. case 32:
  766. case 24:
  767. /* Only support 32 bpp for 32 and 24 depth fbs */
  768. if (mode_cmd->bpp == 32)
  769. break;
  770. DRM_ERROR("Invalid color depth/bbp: %d %d\n",
  771. mode_cmd->depth, mode_cmd->bpp);
  772. return -EINVAL;
  773. case 16:
  774. case 15:
  775. /* Only support 16 bpp for 16 and 15 depth fbs */
  776. if (mode_cmd->bpp == 16)
  777. break;
  778. DRM_ERROR("Invalid color depth/bbp: %d %d\n",
  779. mode_cmd->depth, mode_cmd->bpp);
  780. return -EINVAL;
  781. default:
  782. DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
  783. return -EINVAL;
  784. }
  785. }
  786. vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
  787. if (!vfbd) {
  788. ret = -ENOMEM;
  789. goto out_err1;
  790. }
  791. ret = drm_framebuffer_init(dev, &vfbd->base.base,
  792. &vmw_framebuffer_dmabuf_funcs);
  793. if (ret)
  794. goto out_err2;
  795. if (!vmw_dmabuf_reference(dmabuf)) {
  796. DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
  797. goto out_err3;
  798. }
  799. vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
  800. vfbd->base.base.pitch = mode_cmd->pitch;
  801. vfbd->base.base.depth = mode_cmd->depth;
  802. vfbd->base.base.width = mode_cmd->width;
  803. vfbd->base.base.height = mode_cmd->height;
  804. if (!dev_priv->sou_priv) {
  805. vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
  806. vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
  807. }
  808. vfbd->base.dmabuf = true;
  809. vfbd->buffer = dmabuf;
  810. vfbd->base.user_handle = mode_cmd->handle;
  811. *out = &vfbd->base;
  812. return 0;
  813. out_err3:
  814. drm_framebuffer_cleanup(&vfbd->base.base);
  815. out_err2:
  816. kfree(vfbd);
  817. out_err1:
  818. return ret;
  819. }
  820. /*
  821. * Generic Kernel modesetting functions
  822. */
  823. static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
  824. struct drm_file *file_priv,
  825. struct drm_mode_fb_cmd *mode_cmd)
  826. {
  827. struct vmw_private *dev_priv = vmw_priv(dev);
  828. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  829. struct vmw_framebuffer *vfb = NULL;
  830. struct vmw_surface *surface = NULL;
  831. struct vmw_dma_buffer *bo = NULL;
  832. struct ttm_base_object *user_obj;
  833. u64 required_size;
  834. int ret;
  835. /**
  836. * This code should be conditioned on Screen Objects not being used.
  837. * If screen objects are used, we can allocate a GMR to hold the
  838. * requested framebuffer.
  839. */
  840. required_size = mode_cmd->pitch * mode_cmd->height;
  841. if (unlikely(required_size > (u64) dev_priv->vram_size)) {
  842. DRM_ERROR("VRAM size is too small for requested mode.\n");
  843. return ERR_PTR(-ENOMEM);
  844. }
  845. /*
  846. * Take a reference on the user object of the resource
  847. * backing the kms fb. This ensures that user-space handle
  848. * lookups on that resource will always work as long as
  849. * it's registered with a kms framebuffer. This is important,
  850. * since vmw_execbuf_process identifies resources in the
  851. * command stream using user-space handles.
  852. */
  853. user_obj = ttm_base_object_lookup(tfile, mode_cmd->handle);
  854. if (unlikely(user_obj == NULL)) {
  855. DRM_ERROR("Could not locate requested kms frame buffer.\n");
  856. return ERR_PTR(-ENOENT);
  857. }
  858. /**
  859. * End conditioned code.
  860. */
  861. ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
  862. mode_cmd->handle, &surface);
  863. if (ret)
  864. goto try_dmabuf;
  865. if (!surface->scanout)
  866. goto err_not_scanout;
  867. ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv, surface,
  868. &vfb, mode_cmd);
  869. /* vmw_user_surface_lookup takes one ref so does new_fb */
  870. vmw_surface_unreference(&surface);
  871. if (ret) {
  872. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  873. ttm_base_object_unref(&user_obj);
  874. return ERR_PTR(ret);
  875. } else
  876. vfb->user_obj = user_obj;
  877. return &vfb->base;
  878. try_dmabuf:
  879. DRM_INFO("%s: trying buffer\n", __func__);
  880. ret = vmw_user_dmabuf_lookup(tfile, mode_cmd->handle, &bo);
  881. if (ret) {
  882. DRM_ERROR("failed to find buffer: %i\n", ret);
  883. return ERR_PTR(-ENOENT);
  884. }
  885. ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
  886. mode_cmd);
  887. /* vmw_user_dmabuf_lookup takes one ref so does new_fb */
  888. vmw_dmabuf_unreference(&bo);
  889. if (ret) {
  890. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  891. ttm_base_object_unref(&user_obj);
  892. return ERR_PTR(ret);
  893. } else
  894. vfb->user_obj = user_obj;
  895. return &vfb->base;
  896. err_not_scanout:
  897. DRM_ERROR("surface not marked as scanout\n");
  898. /* vmw_user_surface_lookup takes one ref */
  899. vmw_surface_unreference(&surface);
  900. ttm_base_object_unref(&user_obj);
  901. return ERR_PTR(-EINVAL);
  902. }
  903. static struct drm_mode_config_funcs vmw_kms_funcs = {
  904. .fb_create = vmw_kms_fb_create,
  905. };
  906. int vmw_kms_present(struct vmw_private *dev_priv,
  907. struct drm_file *file_priv,
  908. struct vmw_framebuffer *vfb,
  909. struct vmw_surface *surface,
  910. uint32_t sid,
  911. int32_t destX, int32_t destY,
  912. struct drm_vmw_rect *clips,
  913. uint32_t num_clips)
  914. {
  915. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  916. struct drm_crtc *crtc;
  917. size_t fifo_size;
  918. int i, k, num_units;
  919. int ret = 0; /* silence warning */
  920. struct {
  921. SVGA3dCmdHeader header;
  922. SVGA3dCmdBlitSurfaceToScreen body;
  923. } *cmd;
  924. SVGASignedRect *blits;
  925. num_units = 0;
  926. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
  927. if (crtc->fb != &vfb->base)
  928. continue;
  929. units[num_units++] = vmw_crtc_to_du(crtc);
  930. }
  931. BUG_ON(surface == NULL);
  932. BUG_ON(!clips || !num_clips);
  933. fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
  934. cmd = kmalloc(fifo_size, GFP_KERNEL);
  935. if (unlikely(cmd == NULL)) {
  936. DRM_ERROR("Failed to allocate temporary fifo memory.\n");
  937. return -ENOMEM;
  938. }
  939. /* only need to do this once */
  940. memset(cmd, 0, fifo_size);
  941. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
  942. cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
  943. cmd->body.srcRect.left = 0;
  944. cmd->body.srcRect.right = surface->sizes[0].width;
  945. cmd->body.srcRect.top = 0;
  946. cmd->body.srcRect.bottom = surface->sizes[0].height;
  947. blits = (SVGASignedRect *)&cmd[1];
  948. for (i = 0; i < num_clips; i++) {
  949. blits[i].left = clips[i].x;
  950. blits[i].right = clips[i].x + clips[i].w;
  951. blits[i].top = clips[i].y;
  952. blits[i].bottom = clips[i].y + clips[i].h;
  953. }
  954. for (k = 0; k < num_units; k++) {
  955. struct vmw_display_unit *unit = units[k];
  956. int clip_x1 = destX - unit->crtc.x;
  957. int clip_y1 = destY - unit->crtc.y;
  958. int clip_x2 = clip_x1 + surface->sizes[0].width;
  959. int clip_y2 = clip_y1 + surface->sizes[0].height;
  960. /* skip any crtcs that misses the clip region */
  961. if (clip_x1 >= unit->crtc.mode.hdisplay ||
  962. clip_y1 >= unit->crtc.mode.vdisplay ||
  963. clip_x2 <= 0 || clip_y2 <= 0)
  964. continue;
  965. /* need to reset sid as it is changed by execbuf */
  966. cmd->body.srcImage.sid = sid;
  967. cmd->body.destScreenId = unit->unit;
  968. /*
  969. * The blit command is a lot more resilient then the
  970. * readback command when it comes to clip rects. So its
  971. * okay to go out of bounds.
  972. */
  973. cmd->body.destRect.left = clip_x1;
  974. cmd->body.destRect.right = clip_x2;
  975. cmd->body.destRect.top = clip_y1;
  976. cmd->body.destRect.bottom = clip_y2;
  977. ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
  978. fifo_size, 0, NULL);
  979. if (unlikely(ret != 0))
  980. break;
  981. }
  982. kfree(cmd);
  983. return ret;
  984. }
  985. int vmw_kms_readback(struct vmw_private *dev_priv,
  986. struct drm_file *file_priv,
  987. struct vmw_framebuffer *vfb,
  988. struct drm_vmw_fence_rep __user *user_fence_rep,
  989. struct drm_vmw_rect *clips,
  990. uint32_t num_clips)
  991. {
  992. struct vmw_framebuffer_dmabuf *vfbd =
  993. vmw_framebuffer_to_vfbd(&vfb->base);
  994. struct vmw_dma_buffer *dmabuf = vfbd->buffer;
  995. struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
  996. struct drm_crtc *crtc;
  997. size_t fifo_size;
  998. int i, k, ret, num_units, blits_pos;
  999. struct {
  1000. uint32_t header;
  1001. SVGAFifoCmdDefineGMRFB body;
  1002. } *cmd;
  1003. struct {
  1004. uint32_t header;
  1005. SVGAFifoCmdBlitScreenToGMRFB body;
  1006. } *blits;
  1007. num_units = 0;
  1008. list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
  1009. if (crtc->fb != &vfb->base)
  1010. continue;
  1011. units[num_units++] = vmw_crtc_to_du(crtc);
  1012. }
  1013. BUG_ON(dmabuf == NULL);
  1014. BUG_ON(!clips || !num_clips);
  1015. /* take a safe guess at fifo size */
  1016. fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
  1017. cmd = kmalloc(fifo_size, GFP_KERNEL);
  1018. if (unlikely(cmd == NULL)) {
  1019. DRM_ERROR("Failed to allocate temporary fifo memory.\n");
  1020. return -ENOMEM;
  1021. }
  1022. memset(cmd, 0, fifo_size);
  1023. cmd->header = SVGA_CMD_DEFINE_GMRFB;
  1024. cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
  1025. cmd->body.format.colorDepth = vfb->base.depth;
  1026. cmd->body.format.reserved = 0;
  1027. cmd->body.bytesPerLine = vfb->base.pitch;
  1028. cmd->body.ptr.gmrId = vfb->user_handle;
  1029. cmd->body.ptr.offset = 0;
  1030. blits = (void *)&cmd[1];
  1031. blits_pos = 0;
  1032. for (i = 0; i < num_units; i++) {
  1033. struct drm_vmw_rect *c = clips;
  1034. for (k = 0; k < num_clips; k++, c++) {
  1035. /* transform clip coords to crtc origin based coords */
  1036. int clip_x1 = c->x - units[i]->crtc.x;
  1037. int clip_x2 = c->x - units[i]->crtc.x + c->w;
  1038. int clip_y1 = c->y - units[i]->crtc.y;
  1039. int clip_y2 = c->y - units[i]->crtc.y + c->h;
  1040. int dest_x = c->x;
  1041. int dest_y = c->y;
  1042. /* compensate for clipping, we negate
  1043. * a negative number and add that.
  1044. */
  1045. if (clip_x1 < 0)
  1046. dest_x += -clip_x1;
  1047. if (clip_y1 < 0)
  1048. dest_y += -clip_y1;
  1049. /* clip */
  1050. clip_x1 = max(clip_x1, 0);
  1051. clip_y1 = max(clip_y1, 0);
  1052. clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
  1053. clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
  1054. /* and cull any rects that misses the crtc */
  1055. if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
  1056. clip_y1 >= units[i]->crtc.mode.vdisplay ||
  1057. clip_x2 <= 0 || clip_y2 <= 0)
  1058. continue;
  1059. blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
  1060. blits[blits_pos].body.srcScreenId = units[i]->unit;
  1061. blits[blits_pos].body.destOrigin.x = dest_x;
  1062. blits[blits_pos].body.destOrigin.y = dest_y;
  1063. blits[blits_pos].body.srcRect.left = clip_x1;
  1064. blits[blits_pos].body.srcRect.top = clip_y1;
  1065. blits[blits_pos].body.srcRect.right = clip_x2;
  1066. blits[blits_pos].body.srcRect.bottom = clip_y2;
  1067. blits_pos++;
  1068. }
  1069. }
  1070. /* reset size here and use calculated exact size from loops */
  1071. fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
  1072. ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
  1073. 0, user_fence_rep);
  1074. kfree(cmd);
  1075. return ret;
  1076. }
  1077. int vmw_kms_init(struct vmw_private *dev_priv)
  1078. {
  1079. struct drm_device *dev = dev_priv->dev;
  1080. int ret;
  1081. drm_mode_config_init(dev);
  1082. dev->mode_config.funcs = &vmw_kms_funcs;
  1083. dev->mode_config.min_width = 1;
  1084. dev->mode_config.min_height = 1;
  1085. /* assumed largest fb size */
  1086. dev->mode_config.max_width = 8192;
  1087. dev->mode_config.max_height = 8192;
  1088. ret = vmw_kms_init_screen_object_display(dev_priv);
  1089. if (ret) /* Fallback */
  1090. (void)vmw_kms_init_legacy_display_system(dev_priv);
  1091. return 0;
  1092. }
  1093. int vmw_kms_close(struct vmw_private *dev_priv)
  1094. {
  1095. /*
  1096. * Docs says we should take the lock before calling this function
  1097. * but since it destroys encoders and our destructor calls
  1098. * drm_encoder_cleanup which takes the lock we deadlock.
  1099. */
  1100. drm_mode_config_cleanup(dev_priv->dev);
  1101. if (dev_priv->sou_priv)
  1102. vmw_kms_close_screen_object_display(dev_priv);
  1103. else
  1104. vmw_kms_close_legacy_display_system(dev_priv);
  1105. return 0;
  1106. }
  1107. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  1108. struct drm_file *file_priv)
  1109. {
  1110. struct drm_vmw_cursor_bypass_arg *arg = data;
  1111. struct vmw_display_unit *du;
  1112. struct drm_mode_object *obj;
  1113. struct drm_crtc *crtc;
  1114. int ret = 0;
  1115. mutex_lock(&dev->mode_config.mutex);
  1116. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  1117. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1118. du = vmw_crtc_to_du(crtc);
  1119. du->hotspot_x = arg->xhot;
  1120. du->hotspot_y = arg->yhot;
  1121. }
  1122. mutex_unlock(&dev->mode_config.mutex);
  1123. return 0;
  1124. }
  1125. obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
  1126. if (!obj) {
  1127. ret = -EINVAL;
  1128. goto out;
  1129. }
  1130. crtc = obj_to_crtc(obj);
  1131. du = vmw_crtc_to_du(crtc);
  1132. du->hotspot_x = arg->xhot;
  1133. du->hotspot_y = arg->yhot;
  1134. out:
  1135. mutex_unlock(&dev->mode_config.mutex);
  1136. return ret;
  1137. }
  1138. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  1139. unsigned width, unsigned height, unsigned pitch,
  1140. unsigned bpp, unsigned depth)
  1141. {
  1142. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1143. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  1144. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1145. iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  1146. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  1147. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  1148. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  1149. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  1150. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  1151. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  1152. return -EINVAL;
  1153. }
  1154. return 0;
  1155. }
  1156. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  1157. {
  1158. struct vmw_vga_topology_state *save;
  1159. uint32_t i;
  1160. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  1161. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  1162. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  1163. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1164. vmw_priv->vga_pitchlock =
  1165. vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
  1166. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1167. vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
  1168. SVGA_FIFO_PITCHLOCK);
  1169. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1170. return 0;
  1171. vmw_priv->num_displays = vmw_read(vmw_priv,
  1172. SVGA_REG_NUM_GUEST_DISPLAYS);
  1173. if (vmw_priv->num_displays == 0)
  1174. vmw_priv->num_displays = 1;
  1175. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1176. save = &vmw_priv->vga_save[i];
  1177. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1178. save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
  1179. save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
  1180. save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
  1181. save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
  1182. save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
  1183. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1184. if (i == 0 && vmw_priv->num_displays == 1 &&
  1185. save->width == 0 && save->height == 0) {
  1186. /*
  1187. * It should be fairly safe to assume that these
  1188. * values are uninitialized.
  1189. */
  1190. save->width = vmw_priv->vga_width - save->pos_x;
  1191. save->height = vmw_priv->vga_height - save->pos_y;
  1192. }
  1193. }
  1194. return 0;
  1195. }
  1196. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  1197. {
  1198. struct vmw_vga_topology_state *save;
  1199. uint32_t i;
  1200. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  1201. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  1202. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  1203. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  1204. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
  1205. vmw_priv->vga_pitchlock);
  1206. else if (vmw_fifo_have_pitchlock(vmw_priv))
  1207. iowrite32(vmw_priv->vga_pitchlock,
  1208. vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  1209. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  1210. return 0;
  1211. for (i = 0; i < vmw_priv->num_displays; ++i) {
  1212. save = &vmw_priv->vga_save[i];
  1213. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  1214. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
  1215. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
  1216. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
  1217. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
  1218. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
  1219. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  1220. }
  1221. return 0;
  1222. }
  1223. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  1224. uint32_t pitch,
  1225. uint32_t height)
  1226. {
  1227. return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
  1228. }
  1229. /**
  1230. * Function called by DRM code called with vbl_lock held.
  1231. */
  1232. u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
  1233. {
  1234. return 0;
  1235. }
  1236. /**
  1237. * Function called by DRM code called with vbl_lock held.
  1238. */
  1239. int vmw_enable_vblank(struct drm_device *dev, int crtc)
  1240. {
  1241. return -ENOSYS;
  1242. }
  1243. /**
  1244. * Function called by DRM code called with vbl_lock held.
  1245. */
  1246. void vmw_disable_vblank(struct drm_device *dev, int crtc)
  1247. {
  1248. }
  1249. /*
  1250. * Small shared kms functions.
  1251. */
  1252. int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
  1253. struct drm_vmw_rect *rects)
  1254. {
  1255. struct drm_device *dev = dev_priv->dev;
  1256. struct vmw_display_unit *du;
  1257. struct drm_connector *con;
  1258. mutex_lock(&dev->mode_config.mutex);
  1259. #if 0
  1260. {
  1261. unsigned int i;
  1262. DRM_INFO("%s: new layout ", __func__);
  1263. for (i = 0; i < num; i++)
  1264. DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
  1265. rects[i].w, rects[i].h);
  1266. DRM_INFO("\n");
  1267. }
  1268. #endif
  1269. list_for_each_entry(con, &dev->mode_config.connector_list, head) {
  1270. du = vmw_connector_to_du(con);
  1271. if (num > du->unit) {
  1272. du->pref_width = rects[du->unit].w;
  1273. du->pref_height = rects[du->unit].h;
  1274. du->pref_active = true;
  1275. du->gui_x = rects[du->unit].x;
  1276. du->gui_y = rects[du->unit].y;
  1277. } else {
  1278. du->pref_width = 800;
  1279. du->pref_height = 600;
  1280. du->pref_active = false;
  1281. }
  1282. con->status = vmw_du_connector_detect(con, true);
  1283. }
  1284. mutex_unlock(&dev->mode_config.mutex);
  1285. return 0;
  1286. }
  1287. void vmw_du_crtc_save(struct drm_crtc *crtc)
  1288. {
  1289. }
  1290. void vmw_du_crtc_restore(struct drm_crtc *crtc)
  1291. {
  1292. }
  1293. void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  1294. u16 *r, u16 *g, u16 *b,
  1295. uint32_t start, uint32_t size)
  1296. {
  1297. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  1298. int i;
  1299. for (i = 0; i < size; i++) {
  1300. DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
  1301. r[i], g[i], b[i]);
  1302. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
  1303. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
  1304. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
  1305. }
  1306. }
  1307. void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
  1308. {
  1309. }
  1310. void vmw_du_connector_save(struct drm_connector *connector)
  1311. {
  1312. }
  1313. void vmw_du_connector_restore(struct drm_connector *connector)
  1314. {
  1315. }
  1316. enum drm_connector_status
  1317. vmw_du_connector_detect(struct drm_connector *connector, bool force)
  1318. {
  1319. uint32_t num_displays;
  1320. struct drm_device *dev = connector->dev;
  1321. struct vmw_private *dev_priv = vmw_priv(dev);
  1322. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1323. mutex_lock(&dev_priv->hw_mutex);
  1324. num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
  1325. mutex_unlock(&dev_priv->hw_mutex);
  1326. return ((vmw_connector_to_du(connector)->unit < num_displays &&
  1327. du->pref_active) ?
  1328. connector_status_connected : connector_status_disconnected);
  1329. }
  1330. static struct drm_display_mode vmw_kms_connector_builtin[] = {
  1331. /* 640x480@60Hz */
  1332. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  1333. 752, 800, 0, 480, 489, 492, 525, 0,
  1334. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1335. /* 800x600@60Hz */
  1336. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  1337. 968, 1056, 0, 600, 601, 605, 628, 0,
  1338. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1339. /* 1024x768@60Hz */
  1340. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  1341. 1184, 1344, 0, 768, 771, 777, 806, 0,
  1342. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1343. /* 1152x864@75Hz */
  1344. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  1345. 1344, 1600, 0, 864, 865, 868, 900, 0,
  1346. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1347. /* 1280x768@60Hz */
  1348. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  1349. 1472, 1664, 0, 768, 771, 778, 798, 0,
  1350. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1351. /* 1280x800@60Hz */
  1352. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  1353. 1480, 1680, 0, 800, 803, 809, 831, 0,
  1354. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1355. /* 1280x960@60Hz */
  1356. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  1357. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  1358. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1359. /* 1280x1024@60Hz */
  1360. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  1361. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  1362. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1363. /* 1360x768@60Hz */
  1364. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  1365. 1536, 1792, 0, 768, 771, 777, 795, 0,
  1366. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1367. /* 1440x1050@60Hz */
  1368. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  1369. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  1370. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1371. /* 1440x900@60Hz */
  1372. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  1373. 1672, 1904, 0, 900, 903, 909, 934, 0,
  1374. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1375. /* 1600x1200@60Hz */
  1376. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  1377. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  1378. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1379. /* 1680x1050@60Hz */
  1380. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  1381. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  1382. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1383. /* 1792x1344@60Hz */
  1384. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  1385. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  1386. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1387. /* 1853x1392@60Hz */
  1388. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  1389. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  1390. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1391. /* 1920x1200@60Hz */
  1392. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  1393. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  1394. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1395. /* 1920x1440@60Hz */
  1396. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  1397. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  1398. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1399. /* 2560x1600@60Hz */
  1400. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  1401. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  1402. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1403. /* Terminate */
  1404. { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
  1405. };
  1406. /**
  1407. * vmw_guess_mode_timing - Provide fake timings for a
  1408. * 60Hz vrefresh mode.
  1409. *
  1410. * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
  1411. * members filled in.
  1412. */
  1413. static void vmw_guess_mode_timing(struct drm_display_mode *mode)
  1414. {
  1415. mode->hsync_start = mode->hdisplay + 50;
  1416. mode->hsync_end = mode->hsync_start + 50;
  1417. mode->htotal = mode->hsync_end + 50;
  1418. mode->vsync_start = mode->vdisplay + 50;
  1419. mode->vsync_end = mode->vsync_start + 50;
  1420. mode->vtotal = mode->vsync_end + 50;
  1421. mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
  1422. mode->vrefresh = drm_mode_vrefresh(mode);
  1423. }
  1424. int vmw_du_connector_fill_modes(struct drm_connector *connector,
  1425. uint32_t max_width, uint32_t max_height)
  1426. {
  1427. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1428. struct drm_device *dev = connector->dev;
  1429. struct vmw_private *dev_priv = vmw_priv(dev);
  1430. struct drm_display_mode *mode = NULL;
  1431. struct drm_display_mode *bmode;
  1432. struct drm_display_mode prefmode = { DRM_MODE("preferred",
  1433. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  1434. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1435. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  1436. };
  1437. int i;
  1438. /* Add preferred mode */
  1439. {
  1440. mode = drm_mode_duplicate(dev, &prefmode);
  1441. if (!mode)
  1442. return 0;
  1443. mode->hdisplay = du->pref_width;
  1444. mode->vdisplay = du->pref_height;
  1445. vmw_guess_mode_timing(mode);
  1446. if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
  1447. mode->vdisplay)) {
  1448. drm_mode_probed_add(connector, mode);
  1449. } else {
  1450. drm_mode_destroy(dev, mode);
  1451. mode = NULL;
  1452. }
  1453. if (du->pref_mode) {
  1454. list_del_init(&du->pref_mode->head);
  1455. drm_mode_destroy(dev, du->pref_mode);
  1456. }
  1457. /* mode might be null here, this is intended */
  1458. du->pref_mode = mode;
  1459. }
  1460. for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
  1461. bmode = &vmw_kms_connector_builtin[i];
  1462. if (bmode->hdisplay > max_width ||
  1463. bmode->vdisplay > max_height)
  1464. continue;
  1465. if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
  1466. bmode->vdisplay))
  1467. continue;
  1468. mode = drm_mode_duplicate(dev, bmode);
  1469. if (!mode)
  1470. return 0;
  1471. mode->vrefresh = drm_mode_vrefresh(mode);
  1472. drm_mode_probed_add(connector, mode);
  1473. }
  1474. /* Move the prefered mode first, help apps pick the right mode. */
  1475. if (du->pref_mode)
  1476. list_move(&du->pref_mode->head, &connector->probed_modes);
  1477. drm_mode_connector_list_update(connector);
  1478. return 1;
  1479. }
  1480. int vmw_du_connector_set_property(struct drm_connector *connector,
  1481. struct drm_property *property,
  1482. uint64_t val)
  1483. {
  1484. return 0;
  1485. }
  1486. int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
  1487. struct drm_file *file_priv)
  1488. {
  1489. struct vmw_private *dev_priv = vmw_priv(dev);
  1490. struct drm_vmw_update_layout_arg *arg =
  1491. (struct drm_vmw_update_layout_arg *)data;
  1492. struct vmw_master *vmaster = vmw_master(file_priv->master);
  1493. void __user *user_rects;
  1494. struct drm_vmw_rect *rects;
  1495. unsigned rects_size;
  1496. int ret;
  1497. int i;
  1498. struct drm_mode_config *mode_config = &dev->mode_config;
  1499. ret = ttm_read_lock(&vmaster->lock, true);
  1500. if (unlikely(ret != 0))
  1501. return ret;
  1502. if (!arg->num_outputs) {
  1503. struct drm_vmw_rect def_rect = {0, 0, 800, 600};
  1504. vmw_du_update_layout(dev_priv, 1, &def_rect);
  1505. goto out_unlock;
  1506. }
  1507. rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
  1508. rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
  1509. GFP_KERNEL);
  1510. if (unlikely(!rects)) {
  1511. ret = -ENOMEM;
  1512. goto out_unlock;
  1513. }
  1514. user_rects = (void __user *)(unsigned long)arg->rects;
  1515. ret = copy_from_user(rects, user_rects, rects_size);
  1516. if (unlikely(ret != 0)) {
  1517. DRM_ERROR("Failed to get rects.\n");
  1518. ret = -EFAULT;
  1519. goto out_free;
  1520. }
  1521. for (i = 0; i < arg->num_outputs; ++i) {
  1522. if (rects[i].x < 0 ||
  1523. rects[i].y < 0 ||
  1524. rects[i].x + rects[i].w > mode_config->max_width ||
  1525. rects[i].y + rects[i].h > mode_config->max_height) {
  1526. DRM_ERROR("Invalid GUI layout.\n");
  1527. ret = -EINVAL;
  1528. goto out_free;
  1529. }
  1530. }
  1531. vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
  1532. out_free:
  1533. kfree(rects);
  1534. out_unlock:
  1535. ttm_read_unlock(&vmaster->lock);
  1536. return ret;
  1537. }