vmwgfx_kms.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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. static int vmw_surface_dmabuf_pin(struct vmw_framebuffer *vfb);
  31. static int vmw_surface_dmabuf_unpin(struct vmw_framebuffer *vfb);
  32. void vmw_display_unit_cleanup(struct vmw_display_unit *du)
  33. {
  34. if (du->cursor_surface)
  35. vmw_surface_unreference(&du->cursor_surface);
  36. if (du->cursor_dmabuf)
  37. vmw_dmabuf_unreference(&du->cursor_dmabuf);
  38. drm_crtc_cleanup(&du->crtc);
  39. drm_encoder_cleanup(&du->encoder);
  40. drm_connector_cleanup(&du->connector);
  41. }
  42. /*
  43. * Display Unit Cursor functions
  44. */
  45. int vmw_cursor_update_image(struct vmw_private *dev_priv,
  46. u32 *image, u32 width, u32 height,
  47. u32 hotspotX, u32 hotspotY)
  48. {
  49. struct {
  50. u32 cmd;
  51. SVGAFifoCmdDefineAlphaCursor cursor;
  52. } *cmd;
  53. u32 image_size = width * height * 4;
  54. u32 cmd_size = sizeof(*cmd) + image_size;
  55. if (!image)
  56. return -EINVAL;
  57. cmd = vmw_fifo_reserve(dev_priv, cmd_size);
  58. if (unlikely(cmd == NULL)) {
  59. DRM_ERROR("Fifo reserve failed.\n");
  60. return -ENOMEM;
  61. }
  62. memset(cmd, 0, sizeof(*cmd));
  63. memcpy(&cmd[1], image, image_size);
  64. cmd->cmd = cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR);
  65. cmd->cursor.id = cpu_to_le32(0);
  66. cmd->cursor.width = cpu_to_le32(width);
  67. cmd->cursor.height = cpu_to_le32(height);
  68. cmd->cursor.hotspotX = cpu_to_le32(hotspotX);
  69. cmd->cursor.hotspotY = cpu_to_le32(hotspotY);
  70. vmw_fifo_commit(dev_priv, cmd_size);
  71. return 0;
  72. }
  73. void vmw_cursor_update_position(struct vmw_private *dev_priv,
  74. bool show, int x, int y)
  75. {
  76. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  77. uint32_t count;
  78. iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
  79. iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
  80. iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
  81. count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  82. iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
  83. }
  84. int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
  85. uint32_t handle, uint32_t width, uint32_t height)
  86. {
  87. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  88. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  89. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  90. struct vmw_surface *surface = NULL;
  91. struct vmw_dma_buffer *dmabuf = NULL;
  92. int ret;
  93. if (handle) {
  94. ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
  95. handle, &surface);
  96. if (!ret) {
  97. if (!surface->snooper.image) {
  98. DRM_ERROR("surface not suitable for cursor\n");
  99. return -EINVAL;
  100. }
  101. } else {
  102. ret = vmw_user_dmabuf_lookup(tfile,
  103. handle, &dmabuf);
  104. if (ret) {
  105. DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
  106. return -EINVAL;
  107. }
  108. }
  109. }
  110. /* takedown old cursor */
  111. if (du->cursor_surface) {
  112. du->cursor_surface->snooper.crtc = NULL;
  113. vmw_surface_unreference(&du->cursor_surface);
  114. }
  115. if (du->cursor_dmabuf)
  116. vmw_dmabuf_unreference(&du->cursor_dmabuf);
  117. /* setup new image */
  118. if (surface) {
  119. /* vmw_user_surface_lookup takes one reference */
  120. du->cursor_surface = surface;
  121. du->cursor_surface->snooper.crtc = crtc;
  122. du->cursor_age = du->cursor_surface->snooper.age;
  123. vmw_cursor_update_image(dev_priv, surface->snooper.image,
  124. 64, 64, du->hotspot_x, du->hotspot_y);
  125. } else if (dmabuf) {
  126. struct ttm_bo_kmap_obj map;
  127. unsigned long kmap_offset;
  128. unsigned long kmap_num;
  129. void *virtual;
  130. bool dummy;
  131. /* vmw_user_surface_lookup takes one reference */
  132. du->cursor_dmabuf = dmabuf;
  133. kmap_offset = 0;
  134. kmap_num = (64*64*4) >> PAGE_SHIFT;
  135. ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
  136. if (unlikely(ret != 0)) {
  137. DRM_ERROR("reserve failed\n");
  138. return -EINVAL;
  139. }
  140. ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
  141. if (unlikely(ret != 0))
  142. goto err_unreserve;
  143. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  144. vmw_cursor_update_image(dev_priv, virtual, 64, 64,
  145. du->hotspot_x, du->hotspot_y);
  146. ttm_bo_kunmap(&map);
  147. err_unreserve:
  148. ttm_bo_unreserve(&dmabuf->base);
  149. } else {
  150. vmw_cursor_update_position(dev_priv, false, 0, 0);
  151. return 0;
  152. }
  153. vmw_cursor_update_position(dev_priv, true, du->cursor_x, du->cursor_y);
  154. return 0;
  155. }
  156. int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  157. {
  158. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  159. struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
  160. bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
  161. du->cursor_x = x + crtc->x;
  162. du->cursor_y = y + crtc->y;
  163. vmw_cursor_update_position(dev_priv, shown,
  164. du->cursor_x, du->cursor_y);
  165. return 0;
  166. }
  167. void vmw_kms_cursor_snoop(struct vmw_surface *srf,
  168. struct ttm_object_file *tfile,
  169. struct ttm_buffer_object *bo,
  170. SVGA3dCmdHeader *header)
  171. {
  172. struct ttm_bo_kmap_obj map;
  173. unsigned long kmap_offset;
  174. unsigned long kmap_num;
  175. SVGA3dCopyBox *box;
  176. unsigned box_count;
  177. void *virtual;
  178. bool dummy;
  179. struct vmw_dma_cmd {
  180. SVGA3dCmdHeader header;
  181. SVGA3dCmdSurfaceDMA dma;
  182. } *cmd;
  183. int ret;
  184. cmd = container_of(header, struct vmw_dma_cmd, header);
  185. /* No snooper installed */
  186. if (!srf->snooper.image)
  187. return;
  188. if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
  189. DRM_ERROR("face and mipmap for cursors should never != 0\n");
  190. return;
  191. }
  192. if (cmd->header.size < 64) {
  193. DRM_ERROR("at least one full copy box must be given\n");
  194. return;
  195. }
  196. box = (SVGA3dCopyBox *)&cmd[1];
  197. box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
  198. sizeof(SVGA3dCopyBox);
  199. if (cmd->dma.guest.pitch != (64 * 4) ||
  200. cmd->dma.guest.ptr.offset % PAGE_SIZE ||
  201. box->x != 0 || box->y != 0 || box->z != 0 ||
  202. box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
  203. box->w != 64 || box->h != 64 || box->d != 1 ||
  204. box_count != 1) {
  205. /* TODO handle none page aligned offsets */
  206. /* TODO handle partial uploads and pitch != 256 */
  207. /* TODO handle more then one copy (size != 64) */
  208. DRM_ERROR("lazy programmer, can't handle weird stuff\n");
  209. return;
  210. }
  211. kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
  212. kmap_num = (64*64*4) >> PAGE_SHIFT;
  213. ret = ttm_bo_reserve(bo, true, false, false, 0);
  214. if (unlikely(ret != 0)) {
  215. DRM_ERROR("reserve failed\n");
  216. return;
  217. }
  218. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  219. if (unlikely(ret != 0))
  220. goto err_unreserve;
  221. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  222. memcpy(srf->snooper.image, virtual, 64*64*4);
  223. srf->snooper.age++;
  224. /* we can't call this function from this function since execbuf has
  225. * reserved fifo space.
  226. *
  227. * if (srf->snooper.crtc)
  228. * vmw_ldu_crtc_cursor_update_image(dev_priv,
  229. * srf->snooper.image, 64, 64,
  230. * du->hotspot_x, du->hotspot_y);
  231. */
  232. ttm_bo_kunmap(&map);
  233. err_unreserve:
  234. ttm_bo_unreserve(bo);
  235. }
  236. void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
  237. {
  238. struct drm_device *dev = dev_priv->dev;
  239. struct vmw_display_unit *du;
  240. struct drm_crtc *crtc;
  241. mutex_lock(&dev->mode_config.mutex);
  242. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  243. du = vmw_crtc_to_du(crtc);
  244. if (!du->cursor_surface ||
  245. du->cursor_age == du->cursor_surface->snooper.age)
  246. continue;
  247. du->cursor_age = du->cursor_surface->snooper.age;
  248. vmw_cursor_update_image(dev_priv,
  249. du->cursor_surface->snooper.image,
  250. 64, 64, du->hotspot_x, du->hotspot_y);
  251. }
  252. mutex_unlock(&dev->mode_config.mutex);
  253. }
  254. /*
  255. * Generic framebuffer code
  256. */
  257. int vmw_framebuffer_create_handle(struct drm_framebuffer *fb,
  258. struct drm_file *file_priv,
  259. unsigned int *handle)
  260. {
  261. if (handle)
  262. handle = 0;
  263. return 0;
  264. }
  265. /*
  266. * Surface framebuffer code
  267. */
  268. #define vmw_framebuffer_to_vfbs(x) \
  269. container_of(x, struct vmw_framebuffer_surface, base.base)
  270. struct vmw_framebuffer_surface {
  271. struct vmw_framebuffer base;
  272. struct vmw_surface *surface;
  273. struct vmw_dma_buffer *buffer;
  274. struct delayed_work d_work;
  275. struct mutex work_lock;
  276. bool present_fs;
  277. struct list_head head;
  278. struct drm_master *master;
  279. };
  280. /**
  281. * vmw_kms_idle_workqueues - Flush workqueues on this master
  282. *
  283. * @vmaster - Pointer identifying the master, for the surfaces of which
  284. * we idle the dirty work queues.
  285. *
  286. * This function should be called with the ttm lock held in exclusive mode
  287. * to idle all dirty work queues before the fifo is taken down.
  288. *
  289. * The work task may actually requeue itself, but after the flush returns we're
  290. * sure that there's nothing to present, since the ttm lock is held in
  291. * exclusive mode, so the fifo will never get used.
  292. */
  293. void vmw_kms_idle_workqueues(struct vmw_master *vmaster)
  294. {
  295. struct vmw_framebuffer_surface *entry;
  296. mutex_lock(&vmaster->fb_surf_mutex);
  297. list_for_each_entry(entry, &vmaster->fb_surf, head) {
  298. if (cancel_delayed_work_sync(&entry->d_work))
  299. (void) entry->d_work.work.func(&entry->d_work.work);
  300. (void) cancel_delayed_work_sync(&entry->d_work);
  301. }
  302. mutex_unlock(&vmaster->fb_surf_mutex);
  303. }
  304. void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
  305. {
  306. struct vmw_framebuffer_surface *vfbs =
  307. vmw_framebuffer_to_vfbs(framebuffer);
  308. struct vmw_master *vmaster = vmw_master(vfbs->master);
  309. mutex_lock(&vmaster->fb_surf_mutex);
  310. list_del(&vfbs->head);
  311. mutex_unlock(&vmaster->fb_surf_mutex);
  312. cancel_delayed_work_sync(&vfbs->d_work);
  313. drm_master_put(&vfbs->master);
  314. drm_framebuffer_cleanup(framebuffer);
  315. vmw_surface_unreference(&vfbs->surface);
  316. kfree(vfbs);
  317. }
  318. static void vmw_framebuffer_present_fs_callback(struct work_struct *work)
  319. {
  320. struct delayed_work *d_work =
  321. container_of(work, struct delayed_work, work);
  322. struct vmw_framebuffer_surface *vfbs =
  323. container_of(d_work, struct vmw_framebuffer_surface, d_work);
  324. struct vmw_surface *surf = vfbs->surface;
  325. struct drm_framebuffer *framebuffer = &vfbs->base.base;
  326. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  327. struct {
  328. SVGA3dCmdHeader header;
  329. SVGA3dCmdPresent body;
  330. SVGA3dCopyRect cr;
  331. } *cmd;
  332. /**
  333. * Strictly we should take the ttm_lock in read mode before accessing
  334. * the fifo, to make sure the fifo is present and up. However,
  335. * instead we flush all workqueues under the ttm lock in exclusive mode
  336. * before taking down the fifo.
  337. */
  338. mutex_lock(&vfbs->work_lock);
  339. if (!vfbs->present_fs)
  340. goto out_unlock;
  341. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  342. if (unlikely(cmd == NULL))
  343. goto out_resched;
  344. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_PRESENT);
  345. cmd->header.size = cpu_to_le32(sizeof(cmd->body) + sizeof(cmd->cr));
  346. cmd->body.sid = cpu_to_le32(surf->res.id);
  347. cmd->cr.x = cpu_to_le32(0);
  348. cmd->cr.y = cpu_to_le32(0);
  349. cmd->cr.srcx = cmd->cr.x;
  350. cmd->cr.srcy = cmd->cr.y;
  351. cmd->cr.w = cpu_to_le32(framebuffer->width);
  352. cmd->cr.h = cpu_to_le32(framebuffer->height);
  353. vfbs->present_fs = false;
  354. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  355. out_resched:
  356. /**
  357. * Will not re-add if already pending.
  358. */
  359. schedule_delayed_work(&vfbs->d_work, VMWGFX_PRESENT_RATE);
  360. out_unlock:
  361. mutex_unlock(&vfbs->work_lock);
  362. }
  363. static int do_surface_dirty_ldu(struct vmw_private *dev_priv,
  364. struct vmw_framebuffer *framebuffer,
  365. struct vmw_surface *surf,
  366. unsigned flags, unsigned color,
  367. struct drm_clip_rect *clips,
  368. unsigned num_clips, int inc)
  369. {
  370. SVGA3dCopyRect *cr;
  371. int i;
  372. struct {
  373. SVGA3dCmdHeader header;
  374. SVGA3dCmdPresent body;
  375. SVGA3dCopyRect cr;
  376. } *cmd;
  377. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) + (num_clips - 1) *
  378. sizeof(cmd->cr));
  379. if (unlikely(cmd == NULL)) {
  380. DRM_ERROR("Fifo reserve failed.\n");
  381. return -ENOMEM;
  382. }
  383. memset(cmd, 0, sizeof(*cmd));
  384. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_PRESENT);
  385. cmd->header.size = cpu_to_le32(sizeof(cmd->body) + num_clips *
  386. sizeof(cmd->cr));
  387. cmd->body.sid = cpu_to_le32(surf->res.id);
  388. for (i = 0, cr = &cmd->cr; i < num_clips; i++, cr++, clips += inc) {
  389. cr->x = cpu_to_le16(clips->x1);
  390. cr->y = cpu_to_le16(clips->y1);
  391. cr->srcx = cr->x;
  392. cr->srcy = cr->y;
  393. cr->w = cpu_to_le16(clips->x2 - clips->x1);
  394. cr->h = cpu_to_le16(clips->y2 - clips->y1);
  395. }
  396. vmw_fifo_commit(dev_priv, sizeof(*cmd) + (num_clips - 1) *
  397. sizeof(cmd->cr));
  398. return 0;
  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 vmw_surface *surf = vfbs->surface;
  411. struct drm_clip_rect norect;
  412. int ret, inc = 1;
  413. if (unlikely(vfbs->master != file_priv->master))
  414. return -EINVAL;
  415. ret = ttm_read_lock(&vmaster->lock, true);
  416. if (unlikely(ret != 0))
  417. return ret;
  418. if (!num_clips ||
  419. !(dev_priv->fifo.capabilities &
  420. SVGA_FIFO_CAP_SCREEN_OBJECT)) {
  421. int ret;
  422. mutex_lock(&vfbs->work_lock);
  423. vfbs->present_fs = true;
  424. ret = schedule_delayed_work(&vfbs->d_work, VMWGFX_PRESENT_RATE);
  425. mutex_unlock(&vfbs->work_lock);
  426. if (ret) {
  427. /**
  428. * No work pending, Force immediate present.
  429. */
  430. vmw_framebuffer_present_fs_callback(&vfbs->d_work.work);
  431. }
  432. ttm_read_unlock(&vmaster->lock);
  433. return 0;
  434. }
  435. if (!num_clips) {
  436. num_clips = 1;
  437. clips = &norect;
  438. norect.x1 = norect.y1 = 0;
  439. norect.x2 = framebuffer->width;
  440. norect.y2 = framebuffer->height;
  441. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  442. num_clips /= 2;
  443. inc = 2; /* skip source rects */
  444. }
  445. ret = do_surface_dirty_ldu(dev_priv, &vfbs->base, surf,
  446. flags, color,
  447. clips, num_clips, inc);
  448. ttm_read_unlock(&vmaster->lock);
  449. return 0;
  450. }
  451. static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
  452. .destroy = vmw_framebuffer_surface_destroy,
  453. .dirty = vmw_framebuffer_surface_dirty,
  454. .create_handle = vmw_framebuffer_create_handle,
  455. };
  456. static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
  457. struct drm_file *file_priv,
  458. struct vmw_surface *surface,
  459. struct vmw_framebuffer **out,
  460. const struct drm_mode_fb_cmd
  461. *mode_cmd)
  462. {
  463. struct drm_device *dev = dev_priv->dev;
  464. struct vmw_framebuffer_surface *vfbs;
  465. enum SVGA3dSurfaceFormat format;
  466. struct vmw_master *vmaster = vmw_master(file_priv->master);
  467. int ret;
  468. /*
  469. * Sanity checks.
  470. */
  471. if (unlikely(surface->mip_levels[0] != 1 ||
  472. surface->num_sizes != 1 ||
  473. surface->sizes[0].width < mode_cmd->width ||
  474. surface->sizes[0].height < mode_cmd->height ||
  475. surface->sizes[0].depth != 1)) {
  476. DRM_ERROR("Incompatible surface dimensions "
  477. "for requested mode.\n");
  478. return -EINVAL;
  479. }
  480. switch (mode_cmd->depth) {
  481. case 32:
  482. format = SVGA3D_A8R8G8B8;
  483. break;
  484. case 24:
  485. format = SVGA3D_X8R8G8B8;
  486. break;
  487. case 16:
  488. format = SVGA3D_R5G6B5;
  489. break;
  490. case 15:
  491. format = SVGA3D_A1R5G5B5;
  492. break;
  493. case 8:
  494. format = SVGA3D_LUMINANCE8;
  495. break;
  496. default:
  497. DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
  498. return -EINVAL;
  499. }
  500. if (unlikely(format != surface->format)) {
  501. DRM_ERROR("Invalid surface format for requested mode.\n");
  502. return -EINVAL;
  503. }
  504. vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
  505. if (!vfbs) {
  506. ret = -ENOMEM;
  507. goto out_err1;
  508. }
  509. ret = drm_framebuffer_init(dev, &vfbs->base.base,
  510. &vmw_framebuffer_surface_funcs);
  511. if (ret)
  512. goto out_err2;
  513. if (!vmw_surface_reference(surface)) {
  514. DRM_ERROR("failed to reference surface %p\n", surface);
  515. goto out_err3;
  516. }
  517. /* XXX get the first 3 from the surface info */
  518. vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
  519. vfbs->base.base.pitch = mode_cmd->pitch;
  520. vfbs->base.base.depth = mode_cmd->depth;
  521. vfbs->base.base.width = mode_cmd->width;
  522. vfbs->base.base.height = mode_cmd->height;
  523. vfbs->base.pin = &vmw_surface_dmabuf_pin;
  524. vfbs->base.unpin = &vmw_surface_dmabuf_unpin;
  525. vfbs->surface = surface;
  526. vfbs->master = drm_master_get(file_priv->master);
  527. mutex_init(&vfbs->work_lock);
  528. mutex_lock(&vmaster->fb_surf_mutex);
  529. INIT_DELAYED_WORK(&vfbs->d_work, &vmw_framebuffer_present_fs_callback);
  530. list_add_tail(&vfbs->head, &vmaster->fb_surf);
  531. mutex_unlock(&vmaster->fb_surf_mutex);
  532. *out = &vfbs->base;
  533. return 0;
  534. out_err3:
  535. drm_framebuffer_cleanup(&vfbs->base.base);
  536. out_err2:
  537. kfree(vfbs);
  538. out_err1:
  539. return ret;
  540. }
  541. /*
  542. * Dmabuf framebuffer code
  543. */
  544. #define vmw_framebuffer_to_vfbd(x) \
  545. container_of(x, struct vmw_framebuffer_dmabuf, base.base)
  546. struct vmw_framebuffer_dmabuf {
  547. struct vmw_framebuffer base;
  548. struct vmw_dma_buffer *buffer;
  549. };
  550. void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
  551. {
  552. struct vmw_framebuffer_dmabuf *vfbd =
  553. vmw_framebuffer_to_vfbd(framebuffer);
  554. drm_framebuffer_cleanup(framebuffer);
  555. vmw_dmabuf_unreference(&vfbd->buffer);
  556. kfree(vfbd);
  557. }
  558. static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
  559. struct vmw_framebuffer *framebuffer,
  560. struct vmw_dma_buffer *buffer,
  561. unsigned flags, unsigned color,
  562. struct drm_clip_rect *clips,
  563. unsigned num_clips, int increment)
  564. {
  565. size_t fifo_size;
  566. int i;
  567. struct {
  568. uint32_t header;
  569. SVGAFifoCmdUpdate body;
  570. } *cmd;
  571. fifo_size = sizeof(*cmd) * num_clips;
  572. cmd = vmw_fifo_reserve(dev_priv, fifo_size);
  573. if (unlikely(cmd == NULL)) {
  574. DRM_ERROR("Fifo reserve failed.\n");
  575. return -ENOMEM;
  576. }
  577. memset(cmd, 0, fifo_size);
  578. for (i = 0; i < num_clips; i++, clips += increment) {
  579. cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
  580. cmd[i].body.x = cpu_to_le32(clips->x1);
  581. cmd[i].body.y = cpu_to_le32(clips->y1);
  582. cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
  583. cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
  584. }
  585. vmw_fifo_commit(dev_priv, fifo_size);
  586. return 0;
  587. }
  588. int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
  589. struct drm_file *file_priv,
  590. unsigned flags, unsigned color,
  591. struct drm_clip_rect *clips,
  592. unsigned num_clips)
  593. {
  594. struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
  595. struct vmw_master *vmaster = vmw_master(file_priv->master);
  596. struct vmw_framebuffer_dmabuf *vfbd =
  597. vmw_framebuffer_to_vfbd(framebuffer);
  598. struct vmw_dma_buffer *dmabuf = vfbd->buffer;
  599. struct drm_clip_rect norect;
  600. int ret, increment = 1;
  601. ret = ttm_read_lock(&vmaster->lock, true);
  602. if (unlikely(ret != 0))
  603. return ret;
  604. if (!num_clips) {
  605. num_clips = 1;
  606. clips = &norect;
  607. norect.x1 = norect.y1 = 0;
  608. norect.x2 = framebuffer->width;
  609. norect.y2 = framebuffer->height;
  610. } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
  611. num_clips /= 2;
  612. increment = 2;
  613. }
  614. ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base, dmabuf,
  615. flags, color,
  616. clips, num_clips, increment);
  617. ttm_read_unlock(&vmaster->lock);
  618. return ret;
  619. }
  620. static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
  621. .destroy = vmw_framebuffer_dmabuf_destroy,
  622. .dirty = vmw_framebuffer_dmabuf_dirty,
  623. .create_handle = vmw_framebuffer_create_handle,
  624. };
  625. /**
  626. * We need to reserve the start of vram because the host might
  627. * scribble to it at mode changes, so we need to reserve it.
  628. */
  629. static int vmw_surface_dmabuf_pin(struct vmw_framebuffer *vfb)
  630. {
  631. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  632. struct vmw_framebuffer_surface *vfbs =
  633. vmw_framebuffer_to_vfbs(&vfb->base);
  634. unsigned long size = vfbs->base.base.pitch * vfbs->base.base.height;
  635. int ret;
  636. struct ttm_placement ne_placement = vmw_vram_ne_placement;
  637. ne_placement.lpfn = (size + (PAGE_SIZE - 1)) / PAGE_SIZE;
  638. vfbs->buffer = kzalloc(sizeof(*vfbs->buffer), GFP_KERNEL);
  639. if (unlikely(vfbs->buffer == NULL))
  640. return -ENOMEM;
  641. vmw_overlay_pause_all(dev_priv);
  642. ret = vmw_dmabuf_init(dev_priv, vfbs->buffer, size,
  643. &vmw_vram_ne_placement,
  644. false, &vmw_dmabuf_bo_free);
  645. vmw_overlay_resume_all(dev_priv);
  646. if (unlikely(ret != 0))
  647. vfbs->buffer = NULL;
  648. return ret;
  649. }
  650. /**
  651. * See vmw_surface_dmabuf_pin.
  652. */
  653. static int vmw_surface_dmabuf_unpin(struct vmw_framebuffer *vfb)
  654. {
  655. struct ttm_buffer_object *bo;
  656. struct vmw_framebuffer_surface *vfbs =
  657. vmw_framebuffer_to_vfbs(&vfb->base);
  658. if (unlikely(vfbs->buffer == NULL))
  659. return 0;
  660. bo = &vfbs->buffer->base;
  661. ttm_bo_unref(&bo);
  662. vfbs->buffer = NULL;
  663. return 0;
  664. }
  665. /**
  666. * Pin the dmabuffer to the start of vram.
  667. */
  668. static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
  669. {
  670. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  671. struct vmw_framebuffer_dmabuf *vfbd =
  672. vmw_framebuffer_to_vfbd(&vfb->base);
  673. int ret;
  674. vmw_overlay_pause_all(dev_priv);
  675. ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
  676. vmw_overlay_resume_all(dev_priv);
  677. WARN_ON(ret != 0);
  678. return 0;
  679. }
  680. static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
  681. {
  682. struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
  683. struct vmw_framebuffer_dmabuf *vfbd =
  684. vmw_framebuffer_to_vfbd(&vfb->base);
  685. if (!vfbd->buffer) {
  686. WARN_ON(!vfbd->buffer);
  687. return 0;
  688. }
  689. return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
  690. }
  691. static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
  692. struct vmw_dma_buffer *dmabuf,
  693. struct vmw_framebuffer **out,
  694. const struct drm_mode_fb_cmd
  695. *mode_cmd)
  696. {
  697. struct drm_device *dev = dev_priv->dev;
  698. struct vmw_framebuffer_dmabuf *vfbd;
  699. unsigned int requested_size;
  700. int ret;
  701. requested_size = mode_cmd->height * mode_cmd->pitch;
  702. if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
  703. DRM_ERROR("Screen buffer object size is too small "
  704. "for requested mode.\n");
  705. return -EINVAL;
  706. }
  707. vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
  708. if (!vfbd) {
  709. ret = -ENOMEM;
  710. goto out_err1;
  711. }
  712. ret = drm_framebuffer_init(dev, &vfbd->base.base,
  713. &vmw_framebuffer_dmabuf_funcs);
  714. if (ret)
  715. goto out_err2;
  716. if (!vmw_dmabuf_reference(dmabuf)) {
  717. DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
  718. goto out_err3;
  719. }
  720. vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
  721. vfbd->base.base.pitch = mode_cmd->pitch;
  722. vfbd->base.base.depth = mode_cmd->depth;
  723. vfbd->base.base.width = mode_cmd->width;
  724. vfbd->base.base.height = mode_cmd->height;
  725. vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
  726. vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
  727. vfbd->buffer = dmabuf;
  728. *out = &vfbd->base;
  729. return 0;
  730. out_err3:
  731. drm_framebuffer_cleanup(&vfbd->base.base);
  732. out_err2:
  733. kfree(vfbd);
  734. out_err1:
  735. return ret;
  736. }
  737. /*
  738. * Generic Kernel modesetting functions
  739. */
  740. static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
  741. struct drm_file *file_priv,
  742. struct drm_mode_fb_cmd *mode_cmd)
  743. {
  744. struct vmw_private *dev_priv = vmw_priv(dev);
  745. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  746. struct vmw_framebuffer *vfb = NULL;
  747. struct vmw_surface *surface = NULL;
  748. struct vmw_dma_buffer *bo = NULL;
  749. u64 required_size;
  750. int ret;
  751. /**
  752. * This code should be conditioned on Screen Objects not being used.
  753. * If screen objects are used, we can allocate a GMR to hold the
  754. * requested framebuffer.
  755. */
  756. required_size = mode_cmd->pitch * mode_cmd->height;
  757. if (unlikely(required_size > (u64) dev_priv->vram_size)) {
  758. DRM_ERROR("VRAM size is too small for requested mode.\n");
  759. return NULL;
  760. }
  761. /**
  762. * End conditioned code.
  763. */
  764. ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
  765. mode_cmd->handle, &surface);
  766. if (ret)
  767. goto try_dmabuf;
  768. if (!surface->scanout)
  769. goto err_not_scanout;
  770. ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv, surface,
  771. &vfb, mode_cmd);
  772. /* vmw_user_surface_lookup takes one ref so does new_fb */
  773. vmw_surface_unreference(&surface);
  774. if (ret) {
  775. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  776. return ERR_PTR(ret);
  777. }
  778. return &vfb->base;
  779. try_dmabuf:
  780. DRM_INFO("%s: trying buffer\n", __func__);
  781. ret = vmw_user_dmabuf_lookup(tfile, mode_cmd->handle, &bo);
  782. if (ret) {
  783. DRM_ERROR("failed to find buffer: %i\n", ret);
  784. return ERR_PTR(-ENOENT);
  785. }
  786. ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
  787. mode_cmd);
  788. /* vmw_user_dmabuf_lookup takes one ref so does new_fb */
  789. vmw_dmabuf_unreference(&bo);
  790. if (ret) {
  791. DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
  792. return ERR_PTR(ret);
  793. }
  794. return &vfb->base;
  795. err_not_scanout:
  796. DRM_ERROR("surface not marked as scanout\n");
  797. /* vmw_user_surface_lookup takes one ref */
  798. vmw_surface_unreference(&surface);
  799. return ERR_PTR(-EINVAL);
  800. }
  801. static struct drm_mode_config_funcs vmw_kms_funcs = {
  802. .fb_create = vmw_kms_fb_create,
  803. };
  804. int vmw_kms_init(struct vmw_private *dev_priv)
  805. {
  806. struct drm_device *dev = dev_priv->dev;
  807. int ret;
  808. drm_mode_config_init(dev);
  809. dev->mode_config.funcs = &vmw_kms_funcs;
  810. dev->mode_config.min_width = 1;
  811. dev->mode_config.min_height = 1;
  812. /* assumed largest fb size */
  813. dev->mode_config.max_width = 8192;
  814. dev->mode_config.max_height = 8192;
  815. ret = vmw_kms_init_legacy_display_system(dev_priv);
  816. return 0;
  817. }
  818. int vmw_kms_close(struct vmw_private *dev_priv)
  819. {
  820. /*
  821. * Docs says we should take the lock before calling this function
  822. * but since it destroys encoders and our destructor calls
  823. * drm_encoder_cleanup which takes the lock we deadlock.
  824. */
  825. drm_mode_config_cleanup(dev_priv->dev);
  826. vmw_kms_close_legacy_display_system(dev_priv);
  827. return 0;
  828. }
  829. int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
  830. struct drm_file *file_priv)
  831. {
  832. struct drm_vmw_cursor_bypass_arg *arg = data;
  833. struct vmw_display_unit *du;
  834. struct drm_mode_object *obj;
  835. struct drm_crtc *crtc;
  836. int ret = 0;
  837. mutex_lock(&dev->mode_config.mutex);
  838. if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
  839. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  840. du = vmw_crtc_to_du(crtc);
  841. du->hotspot_x = arg->xhot;
  842. du->hotspot_y = arg->yhot;
  843. }
  844. mutex_unlock(&dev->mode_config.mutex);
  845. return 0;
  846. }
  847. obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
  848. if (!obj) {
  849. ret = -EINVAL;
  850. goto out;
  851. }
  852. crtc = obj_to_crtc(obj);
  853. du = vmw_crtc_to_du(crtc);
  854. du->hotspot_x = arg->xhot;
  855. du->hotspot_y = arg->yhot;
  856. out:
  857. mutex_unlock(&dev->mode_config.mutex);
  858. return ret;
  859. }
  860. int vmw_kms_write_svga(struct vmw_private *vmw_priv,
  861. unsigned width, unsigned height, unsigned pitch,
  862. unsigned bpp, unsigned depth)
  863. {
  864. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  865. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
  866. else if (vmw_fifo_have_pitchlock(vmw_priv))
  867. iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  868. vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
  869. vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
  870. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
  871. if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
  872. DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
  873. depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
  874. return -EINVAL;
  875. }
  876. return 0;
  877. }
  878. int vmw_kms_save_vga(struct vmw_private *vmw_priv)
  879. {
  880. struct vmw_vga_topology_state *save;
  881. uint32_t i;
  882. vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
  883. vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
  884. vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
  885. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  886. vmw_priv->vga_pitchlock =
  887. vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
  888. else if (vmw_fifo_have_pitchlock(vmw_priv))
  889. vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
  890. SVGA_FIFO_PITCHLOCK);
  891. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  892. return 0;
  893. vmw_priv->num_displays = vmw_read(vmw_priv,
  894. SVGA_REG_NUM_GUEST_DISPLAYS);
  895. if (vmw_priv->num_displays == 0)
  896. vmw_priv->num_displays = 1;
  897. for (i = 0; i < vmw_priv->num_displays; ++i) {
  898. save = &vmw_priv->vga_save[i];
  899. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  900. save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
  901. save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
  902. save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
  903. save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
  904. save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
  905. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  906. if (i == 0 && vmw_priv->num_displays == 1 &&
  907. save->width == 0 && save->height == 0) {
  908. /*
  909. * It should be fairly safe to assume that these
  910. * values are uninitialized.
  911. */
  912. save->width = vmw_priv->vga_width - save->pos_x;
  913. save->height = vmw_priv->vga_height - save->pos_y;
  914. }
  915. }
  916. return 0;
  917. }
  918. int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
  919. {
  920. struct vmw_vga_topology_state *save;
  921. uint32_t i;
  922. vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
  923. vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
  924. vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
  925. if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
  926. vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
  927. vmw_priv->vga_pitchlock);
  928. else if (vmw_fifo_have_pitchlock(vmw_priv))
  929. iowrite32(vmw_priv->vga_pitchlock,
  930. vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
  931. if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
  932. return 0;
  933. for (i = 0; i < vmw_priv->num_displays; ++i) {
  934. save = &vmw_priv->vga_save[i];
  935. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
  936. vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
  937. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
  938. vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
  939. vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
  940. vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
  941. vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  942. }
  943. return 0;
  944. }
  945. bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
  946. uint32_t pitch,
  947. uint32_t height)
  948. {
  949. return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
  950. }
  951. u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
  952. {
  953. return 0;
  954. }
  955. /*
  956. * Small shared kms functions.
  957. */
  958. int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
  959. struct drm_vmw_rect *rects)
  960. {
  961. struct drm_device *dev = dev_priv->dev;
  962. struct vmw_display_unit *du;
  963. struct drm_connector *con;
  964. int i;
  965. mutex_lock(&dev->mode_config.mutex);
  966. #if 0
  967. DRM_INFO("%s: new layout ", __func__);
  968. for (i = 0; i < (int)num; i++)
  969. DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
  970. rects[i].w, rects[i].h);
  971. DRM_INFO("\n");
  972. #else
  973. (void)i;
  974. #endif
  975. list_for_each_entry(con, &dev->mode_config.connector_list, head) {
  976. du = vmw_connector_to_du(con);
  977. if (num > du->unit) {
  978. du->pref_width = rects[du->unit].w;
  979. du->pref_height = rects[du->unit].h;
  980. du->pref_active = true;
  981. } else {
  982. du->pref_width = 800;
  983. du->pref_height = 600;
  984. du->pref_active = false;
  985. }
  986. con->status = vmw_du_connector_detect(con, true);
  987. }
  988. mutex_unlock(&dev->mode_config.mutex);
  989. return 0;
  990. }
  991. void vmw_du_crtc_save(struct drm_crtc *crtc)
  992. {
  993. }
  994. void vmw_du_crtc_restore(struct drm_crtc *crtc)
  995. {
  996. }
  997. void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  998. u16 *r, u16 *g, u16 *b,
  999. uint32_t start, uint32_t size)
  1000. {
  1001. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  1002. int i;
  1003. for (i = 0; i < size; i++) {
  1004. DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
  1005. r[i], g[i], b[i]);
  1006. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
  1007. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
  1008. vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
  1009. }
  1010. }
  1011. void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
  1012. {
  1013. }
  1014. void vmw_du_connector_save(struct drm_connector *connector)
  1015. {
  1016. }
  1017. void vmw_du_connector_restore(struct drm_connector *connector)
  1018. {
  1019. }
  1020. enum drm_connector_status
  1021. vmw_du_connector_detect(struct drm_connector *connector, bool force)
  1022. {
  1023. uint32_t num_displays;
  1024. struct drm_device *dev = connector->dev;
  1025. struct vmw_private *dev_priv = vmw_priv(dev);
  1026. mutex_lock(&dev_priv->hw_mutex);
  1027. num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
  1028. mutex_unlock(&dev_priv->hw_mutex);
  1029. return ((vmw_connector_to_du(connector)->unit < num_displays) ?
  1030. connector_status_connected : connector_status_disconnected);
  1031. }
  1032. static struct drm_display_mode vmw_kms_connector_builtin[] = {
  1033. /* 640x480@60Hz */
  1034. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  1035. 752, 800, 0, 480, 489, 492, 525, 0,
  1036. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1037. /* 800x600@60Hz */
  1038. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  1039. 968, 1056, 0, 600, 601, 605, 628, 0,
  1040. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1041. /* 1024x768@60Hz */
  1042. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  1043. 1184, 1344, 0, 768, 771, 777, 806, 0,
  1044. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1045. /* 1152x864@75Hz */
  1046. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  1047. 1344, 1600, 0, 864, 865, 868, 900, 0,
  1048. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1049. /* 1280x768@60Hz */
  1050. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  1051. 1472, 1664, 0, 768, 771, 778, 798, 0,
  1052. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1053. /* 1280x800@60Hz */
  1054. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  1055. 1480, 1680, 0, 800, 803, 809, 831, 0,
  1056. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  1057. /* 1280x960@60Hz */
  1058. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  1059. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  1060. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1061. /* 1280x1024@60Hz */
  1062. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  1063. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  1064. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1065. /* 1360x768@60Hz */
  1066. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  1067. 1536, 1792, 0, 768, 771, 777, 795, 0,
  1068. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1069. /* 1440x1050@60Hz */
  1070. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  1071. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  1072. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1073. /* 1440x900@60Hz */
  1074. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  1075. 1672, 1904, 0, 900, 903, 909, 934, 0,
  1076. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1077. /* 1600x1200@60Hz */
  1078. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  1079. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  1080. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1081. /* 1680x1050@60Hz */
  1082. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  1083. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  1084. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1085. /* 1792x1344@60Hz */
  1086. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  1087. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  1088. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1089. /* 1853x1392@60Hz */
  1090. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  1091. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  1092. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1093. /* 1920x1200@60Hz */
  1094. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  1095. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  1096. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1097. /* 1920x1440@60Hz */
  1098. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  1099. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  1100. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1101. /* 2560x1600@60Hz */
  1102. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  1103. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  1104. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1105. /* Terminate */
  1106. { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
  1107. };
  1108. int vmw_du_connector_fill_modes(struct drm_connector *connector,
  1109. uint32_t max_width, uint32_t max_height)
  1110. {
  1111. struct vmw_display_unit *du = vmw_connector_to_du(connector);
  1112. struct drm_device *dev = connector->dev;
  1113. struct vmw_private *dev_priv = vmw_priv(dev);
  1114. struct drm_display_mode *mode = NULL;
  1115. struct drm_display_mode *bmode;
  1116. struct drm_display_mode prefmode = { DRM_MODE("preferred",
  1117. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  1118. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1119. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  1120. };
  1121. int i;
  1122. /* Add preferred mode */
  1123. {
  1124. mode = drm_mode_duplicate(dev, &prefmode);
  1125. if (!mode)
  1126. return 0;
  1127. mode->hdisplay = du->pref_width;
  1128. mode->vdisplay = du->pref_height;
  1129. mode->vrefresh = drm_mode_vrefresh(mode);
  1130. if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
  1131. mode->vdisplay)) {
  1132. drm_mode_probed_add(connector, mode);
  1133. if (du->pref_mode) {
  1134. list_del_init(&du->pref_mode->head);
  1135. drm_mode_destroy(dev, du->pref_mode);
  1136. }
  1137. du->pref_mode = mode;
  1138. }
  1139. }
  1140. for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
  1141. bmode = &vmw_kms_connector_builtin[i];
  1142. if (bmode->hdisplay > max_width ||
  1143. bmode->vdisplay > max_height)
  1144. continue;
  1145. if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
  1146. bmode->vdisplay))
  1147. continue;
  1148. mode = drm_mode_duplicate(dev, bmode);
  1149. if (!mode)
  1150. return 0;
  1151. mode->vrefresh = drm_mode_vrefresh(mode);
  1152. drm_mode_probed_add(connector, mode);
  1153. }
  1154. drm_mode_connector_list_update(connector);
  1155. return 1;
  1156. }
  1157. int vmw_du_connector_set_property(struct drm_connector *connector,
  1158. struct drm_property *property,
  1159. uint64_t val)
  1160. {
  1161. return 0;
  1162. }