blob: 87c6e6156d7de8bc809a62bab06dbf64a7da512b [file] [log] [blame]
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001/**************************************************************************
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
28#include "vmwgfx_kms.h"
29
30/* Might need a hrtimer here? */
31#define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
32
Thomas Hellstrom22ee8612010-05-28 11:22:00 +020033static int vmw_surface_dmabuf_pin(struct vmw_framebuffer *vfb);
34static int vmw_surface_dmabuf_unpin(struct vmw_framebuffer *vfb);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000035
36void vmw_display_unit_cleanup(struct vmw_display_unit *du)
37{
38 if (du->cursor_surface)
39 vmw_surface_unreference(&du->cursor_surface);
40 if (du->cursor_dmabuf)
41 vmw_dmabuf_unreference(&du->cursor_dmabuf);
42 drm_crtc_cleanup(&du->crtc);
43 drm_encoder_cleanup(&du->encoder);
44 drm_connector_cleanup(&du->connector);
45}
46
47/*
48 * Display Unit Cursor functions
49 */
50
51int vmw_cursor_update_image(struct vmw_private *dev_priv,
52 u32 *image, u32 width, u32 height,
53 u32 hotspotX, u32 hotspotY)
54{
55 struct {
56 u32 cmd;
57 SVGAFifoCmdDefineAlphaCursor cursor;
58 } *cmd;
59 u32 image_size = width * height * 4;
60 u32 cmd_size = sizeof(*cmd) + image_size;
61
62 if (!image)
63 return -EINVAL;
64
65 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
66 if (unlikely(cmd == NULL)) {
67 DRM_ERROR("Fifo reserve failed.\n");
68 return -ENOMEM;
69 }
70
71 memset(cmd, 0, sizeof(*cmd));
72
73 memcpy(&cmd[1], image, image_size);
74
75 cmd->cmd = cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR);
76 cmd->cursor.id = cpu_to_le32(0);
77 cmd->cursor.width = cpu_to_le32(width);
78 cmd->cursor.height = cpu_to_le32(height);
79 cmd->cursor.hotspotX = cpu_to_le32(hotspotX);
80 cmd->cursor.hotspotY = cpu_to_le32(hotspotY);
81
82 vmw_fifo_commit(dev_priv, cmd_size);
83
84 return 0;
85}
86
87void vmw_cursor_update_position(struct vmw_private *dev_priv,
88 bool show, int x, int y)
89{
90 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
91 uint32_t count;
92
93 iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
94 iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
95 iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
96 count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
97 iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
98}
99
100int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
101 uint32_t handle, uint32_t width, uint32_t height)
102{
103 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
104 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
105 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
106 struct vmw_surface *surface = NULL;
107 struct vmw_dma_buffer *dmabuf = NULL;
108 int ret;
109
110 if (handle) {
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100111 ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
112 handle, &surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000113 if (!ret) {
114 if (!surface->snooper.image) {
115 DRM_ERROR("surface not suitable for cursor\n");
116 return -EINVAL;
117 }
118 } else {
119 ret = vmw_user_dmabuf_lookup(tfile,
120 handle, &dmabuf);
121 if (ret) {
122 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
123 return -EINVAL;
124 }
125 }
126 }
127
128 /* takedown old cursor */
129 if (du->cursor_surface) {
130 du->cursor_surface->snooper.crtc = NULL;
131 vmw_surface_unreference(&du->cursor_surface);
132 }
133 if (du->cursor_dmabuf)
134 vmw_dmabuf_unreference(&du->cursor_dmabuf);
135
136 /* setup new image */
137 if (surface) {
138 /* vmw_user_surface_lookup takes one reference */
139 du->cursor_surface = surface;
140
141 du->cursor_surface->snooper.crtc = crtc;
142 du->cursor_age = du->cursor_surface->snooper.age;
143 vmw_cursor_update_image(dev_priv, surface->snooper.image,
144 64, 64, du->hotspot_x, du->hotspot_y);
145 } else if (dmabuf) {
146 struct ttm_bo_kmap_obj map;
147 unsigned long kmap_offset;
148 unsigned long kmap_num;
149 void *virtual;
150 bool dummy;
151
152 /* vmw_user_surface_lookup takes one reference */
153 du->cursor_dmabuf = dmabuf;
154
155 kmap_offset = 0;
156 kmap_num = (64*64*4) >> PAGE_SHIFT;
157
158 ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
159 if (unlikely(ret != 0)) {
160 DRM_ERROR("reserve failed\n");
161 return -EINVAL;
162 }
163
164 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
165 if (unlikely(ret != 0))
166 goto err_unreserve;
167
168 virtual = ttm_kmap_obj_virtual(&map, &dummy);
169 vmw_cursor_update_image(dev_priv, virtual, 64, 64,
170 du->hotspot_x, du->hotspot_y);
171
172 ttm_bo_kunmap(&map);
173err_unreserve:
174 ttm_bo_unreserve(&dmabuf->base);
175
176 } else {
177 vmw_cursor_update_position(dev_priv, false, 0, 0);
178 return 0;
179 }
180
181 vmw_cursor_update_position(dev_priv, true, du->cursor_x, du->cursor_y);
182
183 return 0;
184}
185
186int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
187{
188 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
189 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
190 bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
191
192 du->cursor_x = x + crtc->x;
193 du->cursor_y = y + crtc->y;
194
195 vmw_cursor_update_position(dev_priv, shown,
196 du->cursor_x, du->cursor_y);
197
198 return 0;
199}
200
201void vmw_kms_cursor_snoop(struct vmw_surface *srf,
202 struct ttm_object_file *tfile,
203 struct ttm_buffer_object *bo,
204 SVGA3dCmdHeader *header)
205{
206 struct ttm_bo_kmap_obj map;
207 unsigned long kmap_offset;
208 unsigned long kmap_num;
209 SVGA3dCopyBox *box;
210 unsigned box_count;
211 void *virtual;
212 bool dummy;
213 struct vmw_dma_cmd {
214 SVGA3dCmdHeader header;
215 SVGA3dCmdSurfaceDMA dma;
216 } *cmd;
217 int ret;
218
219 cmd = container_of(header, struct vmw_dma_cmd, header);
220
221 /* No snooper installed */
222 if (!srf->snooper.image)
223 return;
224
225 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
226 DRM_ERROR("face and mipmap for cursors should never != 0\n");
227 return;
228 }
229
230 if (cmd->header.size < 64) {
231 DRM_ERROR("at least one full copy box must be given\n");
232 return;
233 }
234
235 box = (SVGA3dCopyBox *)&cmd[1];
236 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
237 sizeof(SVGA3dCopyBox);
238
239 if (cmd->dma.guest.pitch != (64 * 4) ||
240 cmd->dma.guest.ptr.offset % PAGE_SIZE ||
241 box->x != 0 || box->y != 0 || box->z != 0 ||
242 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
243 box->w != 64 || box->h != 64 || box->d != 1 ||
244 box_count != 1) {
245 /* TODO handle none page aligned offsets */
246 /* TODO handle partial uploads and pitch != 256 */
247 /* TODO handle more then one copy (size != 64) */
248 DRM_ERROR("lazy programer, cant handle wierd stuff\n");
249 return;
250 }
251
252 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
253 kmap_num = (64*64*4) >> PAGE_SHIFT;
254
255 ret = ttm_bo_reserve(bo, true, false, false, 0);
256 if (unlikely(ret != 0)) {
257 DRM_ERROR("reserve failed\n");
258 return;
259 }
260
261 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
262 if (unlikely(ret != 0))
263 goto err_unreserve;
264
265 virtual = ttm_kmap_obj_virtual(&map, &dummy);
266
267 memcpy(srf->snooper.image, virtual, 64*64*4);
268 srf->snooper.age++;
269
270 /* we can't call this function from this function since execbuf has
271 * reserved fifo space.
272 *
273 * if (srf->snooper.crtc)
274 * vmw_ldu_crtc_cursor_update_image(dev_priv,
275 * srf->snooper.image, 64, 64,
276 * du->hotspot_x, du->hotspot_y);
277 */
278
279 ttm_bo_kunmap(&map);
280err_unreserve:
281 ttm_bo_unreserve(bo);
282}
283
284void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
285{
286 struct drm_device *dev = dev_priv->dev;
287 struct vmw_display_unit *du;
288 struct drm_crtc *crtc;
289
290 mutex_lock(&dev->mode_config.mutex);
291
292 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
293 du = vmw_crtc_to_du(crtc);
294 if (!du->cursor_surface ||
295 du->cursor_age == du->cursor_surface->snooper.age)
296 continue;
297
298 du->cursor_age = du->cursor_surface->snooper.age;
299 vmw_cursor_update_image(dev_priv,
300 du->cursor_surface->snooper.image,
301 64, 64, du->hotspot_x, du->hotspot_y);
302 }
303
304 mutex_unlock(&dev->mode_config.mutex);
305}
306
307/*
308 * Generic framebuffer code
309 */
310
311int vmw_framebuffer_create_handle(struct drm_framebuffer *fb,
312 struct drm_file *file_priv,
313 unsigned int *handle)
314{
315 if (handle)
316 handle = 0;
317
318 return 0;
319}
320
321/*
322 * Surface framebuffer code
323 */
324
325#define vmw_framebuffer_to_vfbs(x) \
326 container_of(x, struct vmw_framebuffer_surface, base.base)
327
328struct vmw_framebuffer_surface {
329 struct vmw_framebuffer base;
330 struct vmw_surface *surface;
Thomas Hellstrom22ee8612010-05-28 11:22:00 +0200331 struct vmw_dma_buffer *buffer;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000332 struct delayed_work d_work;
333 struct mutex work_lock;
334 bool present_fs;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200335 struct list_head head;
336 struct drm_master *master;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000337};
338
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200339/**
340 * vmw_kms_idle_workqueues - Flush workqueues on this master
341 *
342 * @vmaster - Pointer identifying the master, for the surfaces of which
343 * we idle the dirty work queues.
344 *
345 * This function should be called with the ttm lock held in exclusive mode
346 * to idle all dirty work queues before the fifo is taken down.
347 *
348 * The work task may actually requeue itself, but after the flush returns we're
349 * sure that there's nothing to present, since the ttm lock is held in
350 * exclusive mode, so the fifo will never get used.
351 */
352
353void vmw_kms_idle_workqueues(struct vmw_master *vmaster)
354{
355 struct vmw_framebuffer_surface *entry;
356
357 mutex_lock(&vmaster->fb_surf_mutex);
358 list_for_each_entry(entry, &vmaster->fb_surf, head) {
359 if (cancel_delayed_work_sync(&entry->d_work))
360 (void) entry->d_work.work.func(&entry->d_work.work);
361
362 (void) cancel_delayed_work_sync(&entry->d_work);
363 }
364 mutex_unlock(&vmaster->fb_surf_mutex);
365}
366
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000367void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
368{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200369 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000370 vmw_framebuffer_to_vfbs(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200371 struct vmw_master *vmaster = vmw_master(vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000372
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200373
374 mutex_lock(&vmaster->fb_surf_mutex);
375 list_del(&vfbs->head);
376 mutex_unlock(&vmaster->fb_surf_mutex);
377
378 cancel_delayed_work_sync(&vfbs->d_work);
379 drm_master_put(&vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000380 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200381 vmw_surface_unreference(&vfbs->surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000382
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200383 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000384}
385
386static void vmw_framebuffer_present_fs_callback(struct work_struct *work)
387{
388 struct delayed_work *d_work =
389 container_of(work, struct delayed_work, work);
390 struct vmw_framebuffer_surface *vfbs =
391 container_of(d_work, struct vmw_framebuffer_surface, d_work);
392 struct vmw_surface *surf = vfbs->surface;
393 struct drm_framebuffer *framebuffer = &vfbs->base.base;
394 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
395
396 struct {
397 SVGA3dCmdHeader header;
398 SVGA3dCmdPresent body;
399 SVGA3dCopyRect cr;
400 } *cmd;
401
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200402 /**
403 * Strictly we should take the ttm_lock in read mode before accessing
404 * the fifo, to make sure the fifo is present and up. However,
405 * instead we flush all workqueues under the ttm lock in exclusive mode
406 * before taking down the fifo.
407 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000408 mutex_lock(&vfbs->work_lock);
409 if (!vfbs->present_fs)
410 goto out_unlock;
411
412 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
413 if (unlikely(cmd == NULL))
414 goto out_resched;
415
416 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_PRESENT);
417 cmd->header.size = cpu_to_le32(sizeof(cmd->body) + sizeof(cmd->cr));
418 cmd->body.sid = cpu_to_le32(surf->res.id);
419 cmd->cr.x = cpu_to_le32(0);
420 cmd->cr.y = cpu_to_le32(0);
421 cmd->cr.srcx = cmd->cr.x;
422 cmd->cr.srcy = cmd->cr.y;
423 cmd->cr.w = cpu_to_le32(framebuffer->width);
424 cmd->cr.h = cpu_to_le32(framebuffer->height);
425 vfbs->present_fs = false;
426 vmw_fifo_commit(dev_priv, sizeof(*cmd));
427out_resched:
428 /**
429 * Will not re-add if already pending.
430 */
431 schedule_delayed_work(&vfbs->d_work, VMWGFX_PRESENT_RATE);
432out_unlock:
433 mutex_unlock(&vfbs->work_lock);
434}
435
436
437int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200438 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000439 unsigned flags, unsigned color,
440 struct drm_clip_rect *clips,
441 unsigned num_clips)
442{
443 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200444 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000445 struct vmw_framebuffer_surface *vfbs =
446 vmw_framebuffer_to_vfbs(framebuffer);
447 struct vmw_surface *surf = vfbs->surface;
448 struct drm_clip_rect norect;
449 SVGA3dCopyRect *cr;
450 int i, inc = 1;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200451 int ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000452
453 struct {
454 SVGA3dCmdHeader header;
455 SVGA3dCmdPresent body;
456 SVGA3dCopyRect cr;
457 } *cmd;
458
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200459 if (unlikely(vfbs->master != file_priv->master))
460 return -EINVAL;
461
462 ret = ttm_read_lock(&vmaster->lock, true);
463 if (unlikely(ret != 0))
464 return ret;
465
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000466 if (!num_clips ||
467 !(dev_priv->fifo.capabilities &
468 SVGA_FIFO_CAP_SCREEN_OBJECT)) {
469 int ret;
470
471 mutex_lock(&vfbs->work_lock);
472 vfbs->present_fs = true;
473 ret = schedule_delayed_work(&vfbs->d_work, VMWGFX_PRESENT_RATE);
474 mutex_unlock(&vfbs->work_lock);
475 if (ret) {
476 /**
477 * No work pending, Force immediate present.
478 */
479 vmw_framebuffer_present_fs_callback(&vfbs->d_work.work);
480 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200481 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000482 return 0;
483 }
484
485 if (!num_clips) {
486 num_clips = 1;
487 clips = &norect;
488 norect.x1 = norect.y1 = 0;
489 norect.x2 = framebuffer->width;
490 norect.y2 = framebuffer->height;
491 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
492 num_clips /= 2;
493 inc = 2; /* skip source rects */
494 }
495
496 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) + (num_clips - 1) * sizeof(cmd->cr));
497 if (unlikely(cmd == NULL)) {
498 DRM_ERROR("Fifo reserve failed.\n");
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200499 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000500 return -ENOMEM;
501 }
502
503 memset(cmd, 0, sizeof(*cmd));
504
505 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_PRESENT);
506 cmd->header.size = cpu_to_le32(sizeof(cmd->body) + num_clips * sizeof(cmd->cr));
507 cmd->body.sid = cpu_to_le32(surf->res.id);
508
509 for (i = 0, cr = &cmd->cr; i < num_clips; i++, cr++, clips += inc) {
510 cr->x = cpu_to_le16(clips->x1);
511 cr->y = cpu_to_le16(clips->y1);
512 cr->srcx = cr->x;
513 cr->srcy = cr->y;
514 cr->w = cpu_to_le16(clips->x2 - clips->x1);
515 cr->h = cpu_to_le16(clips->y2 - clips->y1);
516 }
517
518 vmw_fifo_commit(dev_priv, sizeof(*cmd) + (num_clips - 1) * sizeof(cmd->cr));
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200519 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000520 return 0;
521}
522
523static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
524 .destroy = vmw_framebuffer_surface_destroy,
525 .dirty = vmw_framebuffer_surface_dirty,
526 .create_handle = vmw_framebuffer_create_handle,
527};
528
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200529static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200530 struct drm_file *file_priv,
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200531 struct vmw_surface *surface,
532 struct vmw_framebuffer **out,
533 const struct drm_mode_fb_cmd
534 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000535
536{
537 struct drm_device *dev = dev_priv->dev;
538 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200539 enum SVGA3dSurfaceFormat format;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200540 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000541 int ret;
542
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200543 /*
544 * Sanity checks.
545 */
546
547 if (unlikely(surface->mip_levels[0] != 1 ||
548 surface->num_sizes != 1 ||
549 surface->sizes[0].width < mode_cmd->width ||
550 surface->sizes[0].height < mode_cmd->height ||
551 surface->sizes[0].depth != 1)) {
552 DRM_ERROR("Incompatible surface dimensions "
553 "for requested mode.\n");
554 return -EINVAL;
555 }
556
557 switch (mode_cmd->depth) {
558 case 32:
559 format = SVGA3D_A8R8G8B8;
560 break;
561 case 24:
562 format = SVGA3D_X8R8G8B8;
563 break;
564 case 16:
565 format = SVGA3D_R5G6B5;
566 break;
567 case 15:
568 format = SVGA3D_A1R5G5B5;
569 break;
570 default:
571 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
572 return -EINVAL;
573 }
574
575 if (unlikely(format != surface->format)) {
576 DRM_ERROR("Invalid surface format for requested mode.\n");
577 return -EINVAL;
578 }
579
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000580 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
581 if (!vfbs) {
582 ret = -ENOMEM;
583 goto out_err1;
584 }
585
586 ret = drm_framebuffer_init(dev, &vfbs->base.base,
587 &vmw_framebuffer_surface_funcs);
588 if (ret)
589 goto out_err2;
590
591 if (!vmw_surface_reference(surface)) {
592 DRM_ERROR("failed to reference surface %p\n", surface);
593 goto out_err3;
594 }
595
596 /* XXX get the first 3 from the surface info */
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200597 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
598 vfbs->base.base.pitch = mode_cmd->pitch;
599 vfbs->base.base.depth = mode_cmd->depth;
600 vfbs->base.base.width = mode_cmd->width;
601 vfbs->base.base.height = mode_cmd->height;
Thomas Hellstrom22ee8612010-05-28 11:22:00 +0200602 vfbs->base.pin = &vmw_surface_dmabuf_pin;
603 vfbs->base.unpin = &vmw_surface_dmabuf_unpin;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000604 vfbs->surface = surface;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200605 vfbs->master = drm_master_get(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000606 mutex_init(&vfbs->work_lock);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200607
608 mutex_lock(&vmaster->fb_surf_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000609 INIT_DELAYED_WORK(&vfbs->d_work, &vmw_framebuffer_present_fs_callback);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200610 list_add_tail(&vfbs->head, &vmaster->fb_surf);
611 mutex_unlock(&vmaster->fb_surf_mutex);
612
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000613 *out = &vfbs->base;
614
615 return 0;
616
617out_err3:
618 drm_framebuffer_cleanup(&vfbs->base.base);
619out_err2:
620 kfree(vfbs);
621out_err1:
622 return ret;
623}
624
625/*
626 * Dmabuf framebuffer code
627 */
628
629#define vmw_framebuffer_to_vfbd(x) \
630 container_of(x, struct vmw_framebuffer_dmabuf, base.base)
631
632struct vmw_framebuffer_dmabuf {
633 struct vmw_framebuffer base;
634 struct vmw_dma_buffer *buffer;
635};
636
637void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
638{
639 struct vmw_framebuffer_dmabuf *vfbd =
640 vmw_framebuffer_to_vfbd(framebuffer);
641
642 drm_framebuffer_cleanup(framebuffer);
643 vmw_dmabuf_unreference(&vfbd->buffer);
644
645 kfree(vfbd);
646}
647
648int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200649 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000650 unsigned flags, unsigned color,
651 struct drm_clip_rect *clips,
652 unsigned num_clips)
653{
654 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200655 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000656 struct drm_clip_rect norect;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200657 int ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000658 struct {
659 uint32_t header;
660 SVGAFifoCmdUpdate body;
661 } *cmd;
662 int i, increment = 1;
663
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200664 ret = ttm_read_lock(&vmaster->lock, true);
665 if (unlikely(ret != 0))
666 return ret;
667
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100668 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000669 num_clips = 1;
670 clips = &norect;
671 norect.x1 = norect.y1 = 0;
672 norect.x2 = framebuffer->width;
673 norect.y2 = framebuffer->height;
674 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
675 num_clips /= 2;
676 increment = 2;
677 }
678
679 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
680 if (unlikely(cmd == NULL)) {
681 DRM_ERROR("Fifo reserve failed.\n");
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200682 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000683 return -ENOMEM;
684 }
685
686 for (i = 0; i < num_clips; i++, clips += increment) {
687 cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100688 cmd[i].body.x = cpu_to_le32(clips->x1);
689 cmd[i].body.y = cpu_to_le32(clips->y1);
690 cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
691 cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000692 }
693
694 vmw_fifo_commit(dev_priv, sizeof(*cmd) * num_clips);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200695 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000696
697 return 0;
698}
699
700static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
701 .destroy = vmw_framebuffer_dmabuf_destroy,
702 .dirty = vmw_framebuffer_dmabuf_dirty,
703 .create_handle = vmw_framebuffer_create_handle,
704};
705
Thomas Hellstrom22ee8612010-05-28 11:22:00 +0200706static int vmw_surface_dmabuf_pin(struct vmw_framebuffer *vfb)
707{
708 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
709 struct vmw_framebuffer_surface *vfbs =
710 vmw_framebuffer_to_vfbs(&vfb->base);
711 unsigned long size = vfbs->base.base.pitch * vfbs->base.base.height;
712 int ret;
713
714 vfbs->buffer = kzalloc(sizeof(*vfbs->buffer), GFP_KERNEL);
715 if (unlikely(vfbs->buffer == NULL))
716 return -ENOMEM;
717
718 vmw_overlay_pause_all(dev_priv);
719 ret = vmw_dmabuf_init(dev_priv, vfbs->buffer, size,
720 &vmw_vram_ne_placement,
721 false, &vmw_dmabuf_bo_free);
722 vmw_overlay_resume_all(dev_priv);
723
724 return ret;
725}
726
727static int vmw_surface_dmabuf_unpin(struct vmw_framebuffer *vfb)
728{
729 struct ttm_buffer_object *bo;
730 struct vmw_framebuffer_surface *vfbs =
731 vmw_framebuffer_to_vfbs(&vfb->base);
732
733 bo = &vfbs->buffer->base;
734 ttm_bo_unref(&bo);
735 vfbs->buffer = NULL;
736
737 return 0;
738}
739
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000740static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
741{
742 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
743 struct vmw_framebuffer_dmabuf *vfbd =
744 vmw_framebuffer_to_vfbd(&vfb->base);
745 int ret;
746
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200747
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000748 vmw_overlay_pause_all(dev_priv);
749
750 ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer);
751
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000752 vmw_overlay_resume_all(dev_priv);
753
Jakob Bornecrantz316ab132010-05-28 11:22:05 +0200754 WARN_ON(ret != 0);
755
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000756 return 0;
757}
758
759static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
760{
761 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
762 struct vmw_framebuffer_dmabuf *vfbd =
763 vmw_framebuffer_to_vfbd(&vfb->base);
764
765 if (!vfbd->buffer) {
766 WARN_ON(!vfbd->buffer);
767 return 0;
768 }
769
770 return vmw_dmabuf_from_vram(dev_priv, vfbd->buffer);
771}
772
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200773static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
774 struct vmw_dma_buffer *dmabuf,
775 struct vmw_framebuffer **out,
776 const struct drm_mode_fb_cmd
777 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000778
779{
780 struct drm_device *dev = dev_priv->dev;
781 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200782 unsigned int requested_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000783 int ret;
784
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200785 requested_size = mode_cmd->height * mode_cmd->pitch;
786 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
787 DRM_ERROR("Screen buffer object size is too small "
788 "for requested mode.\n");
789 return -EINVAL;
790 }
791
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000792 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
793 if (!vfbd) {
794 ret = -ENOMEM;
795 goto out_err1;
796 }
797
798 ret = drm_framebuffer_init(dev, &vfbd->base.base,
799 &vmw_framebuffer_dmabuf_funcs);
800 if (ret)
801 goto out_err2;
802
803 if (!vmw_dmabuf_reference(dmabuf)) {
804 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
805 goto out_err3;
806 }
807
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200808 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
809 vfbd->base.base.pitch = mode_cmd->pitch;
810 vfbd->base.base.depth = mode_cmd->depth;
811 vfbd->base.base.width = mode_cmd->width;
812 vfbd->base.base.height = mode_cmd->height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000813 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
814 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
815 vfbd->buffer = dmabuf;
816 *out = &vfbd->base;
817
818 return 0;
819
820out_err3:
821 drm_framebuffer_cleanup(&vfbd->base.base);
822out_err2:
823 kfree(vfbd);
824out_err1:
825 return ret;
826}
827
828/*
829 * Generic Kernel modesetting functions
830 */
831
832static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
833 struct drm_file *file_priv,
834 struct drm_mode_fb_cmd *mode_cmd)
835{
836 struct vmw_private *dev_priv = vmw_priv(dev);
837 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
838 struct vmw_framebuffer *vfb = NULL;
839 struct vmw_surface *surface = NULL;
840 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrome133e732010-10-05 12:43:04 +0200841 u64 required_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000842 int ret;
843
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200844 /**
845 * This code should be conditioned on Screen Objects not being used.
846 * If screen objects are used, we can allocate a GMR to hold the
847 * requested framebuffer.
848 */
849
850 required_size = mode_cmd->pitch * mode_cmd->height;
Thomas Hellstrome133e732010-10-05 12:43:04 +0200851 if (unlikely(required_size > (u64) dev_priv->vram_size)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200852 DRM_ERROR("VRAM size is too small for requested mode.\n");
853 return NULL;
854 }
855
856 /**
857 * End conditioned code.
858 */
859
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100860 ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
861 mode_cmd->handle, &surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000862 if (ret)
863 goto try_dmabuf;
864
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +0000865 if (!surface->scanout)
866 goto err_not_scanout;
867
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200868 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv, surface,
869 &vfb, mode_cmd);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000870
871 /* vmw_user_surface_lookup takes one ref so does new_fb */
872 vmw_surface_unreference(&surface);
873
874 if (ret) {
875 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100876 return ERR_PTR(ret);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000877 }
878 return &vfb->base;
879
880try_dmabuf:
881 DRM_INFO("%s: trying buffer\n", __func__);
882
883 ret = vmw_user_dmabuf_lookup(tfile, mode_cmd->handle, &bo);
884 if (ret) {
885 DRM_ERROR("failed to find buffer: %i\n", ret);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100886 return ERR_PTR(-ENOENT);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000887 }
888
889 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200890 mode_cmd);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000891
892 /* vmw_user_dmabuf_lookup takes one ref so does new_fb */
893 vmw_dmabuf_unreference(&bo);
894
895 if (ret) {
896 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100897 return ERR_PTR(ret);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000898 }
899
900 return &vfb->base;
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +0000901
902err_not_scanout:
903 DRM_ERROR("surface not marked as scanout\n");
904 /* vmw_user_surface_lookup takes one ref */
905 vmw_surface_unreference(&surface);
906
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100907 return ERR_PTR(-EINVAL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000908}
909
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000910static struct drm_mode_config_funcs vmw_kms_funcs = {
911 .fb_create = vmw_kms_fb_create,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000912};
913
914int vmw_kms_init(struct vmw_private *dev_priv)
915{
916 struct drm_device *dev = dev_priv->dev;
917 int ret;
918
919 drm_mode_config_init(dev);
920 dev->mode_config.funcs = &vmw_kms_funcs;
Jakob Bornecrantz3bef3572010-02-09 19:41:57 +0000921 dev->mode_config.min_width = 1;
922 dev->mode_config.min_height = 1;
Jakob Bornecrantz7e71f8a2010-05-28 11:21:54 +0200923 /* assumed largest fb size */
924 dev->mode_config.max_width = 8192;
925 dev->mode_config.max_height = 8192;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000926
927 ret = vmw_kms_init_legacy_display_system(dev_priv);
928
929 return 0;
930}
931
932int vmw_kms_close(struct vmw_private *dev_priv)
933{
934 /*
935 * Docs says we should take the lock before calling this function
936 * but since it destroys encoders and our destructor calls
937 * drm_encoder_cleanup which takes the lock we deadlock.
938 */
939 drm_mode_config_cleanup(dev_priv->dev);
940 vmw_kms_close_legacy_display_system(dev_priv);
941 return 0;
942}
943
944int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
945 struct drm_file *file_priv)
946{
947 struct drm_vmw_cursor_bypass_arg *arg = data;
948 struct vmw_display_unit *du;
949 struct drm_mode_object *obj;
950 struct drm_crtc *crtc;
951 int ret = 0;
952
953
954 mutex_lock(&dev->mode_config.mutex);
955 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
956
957 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
958 du = vmw_crtc_to_du(crtc);
959 du->hotspot_x = arg->xhot;
960 du->hotspot_y = arg->yhot;
961 }
962
963 mutex_unlock(&dev->mode_config.mutex);
964 return 0;
965 }
966
967 obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
968 if (!obj) {
969 ret = -EINVAL;
970 goto out;
971 }
972
973 crtc = obj_to_crtc(obj);
974 du = vmw_crtc_to_du(crtc);
975
976 du->hotspot_x = arg->xhot;
977 du->hotspot_y = arg->yhot;
978
979out:
980 mutex_unlock(&dev->mode_config.mutex);
981
982 return ret;
983}
984
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200985void vmw_kms_write_svga(struct vmw_private *vmw_priv,
986 unsigned width, unsigned height, unsigned pitch,
987 unsigned bbp, unsigned depth)
988{
989 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
990 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
991 else if (vmw_fifo_have_pitchlock(vmw_priv))
992 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
993 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
994 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
995 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bbp);
996 vmw_write(vmw_priv, SVGA_REG_DEPTH, depth);
997 vmw_write(vmw_priv, SVGA_REG_RED_MASK, 0x00ff0000);
998 vmw_write(vmw_priv, SVGA_REG_GREEN_MASK, 0x0000ff00);
999 vmw_write(vmw_priv, SVGA_REG_BLUE_MASK, 0x000000ff);
1000}
1001
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001002int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1003{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001004 struct vmw_vga_topology_state *save;
1005 uint32_t i;
1006
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001007 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1008 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001009 vmw_priv->vga_depth = vmw_read(vmw_priv, SVGA_REG_DEPTH);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001010 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001011 vmw_priv->vga_pseudo = vmw_read(vmw_priv, SVGA_REG_PSEUDOCOLOR);
1012 vmw_priv->vga_red_mask = vmw_read(vmw_priv, SVGA_REG_RED_MASK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001013 vmw_priv->vga_blue_mask = vmw_read(vmw_priv, SVGA_REG_BLUE_MASK);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001014 vmw_priv->vga_green_mask = vmw_read(vmw_priv, SVGA_REG_GREEN_MASK);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001015 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1016 vmw_priv->vga_pitchlock =
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001017 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001018 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001019 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
1020 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001021
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001022 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1023 return 0;
1024
1025 vmw_priv->num_displays = vmw_read(vmw_priv,
1026 SVGA_REG_NUM_GUEST_DISPLAYS);
1027
Thomas Hellstrom029e50b2010-10-05 12:43:08 +02001028 if (vmw_priv->num_displays == 0)
1029 vmw_priv->num_displays = 1;
1030
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001031 for (i = 0; i < vmw_priv->num_displays; ++i) {
1032 save = &vmw_priv->vga_save[i];
1033 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1034 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1035 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1036 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1037 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1038 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1039 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001040 if (i == 0 && vmw_priv->num_displays == 1 &&
1041 save->width == 0 && save->height == 0) {
1042
1043 /*
1044 * It should be fairly safe to assume that these
1045 * values are uninitialized.
1046 */
1047
1048 save->width = vmw_priv->vga_width - save->pos_x;
1049 save->height = vmw_priv->vga_height - save->pos_y;
1050 }
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001051 }
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001052
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001053 return 0;
1054}
1055
1056int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1057{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001058 struct vmw_vga_topology_state *save;
1059 uint32_t i;
1060
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001061 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1062 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001063 vmw_write(vmw_priv, SVGA_REG_DEPTH, vmw_priv->vga_depth);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001064 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001065 vmw_write(vmw_priv, SVGA_REG_PSEUDOCOLOR, vmw_priv->vga_pseudo);
1066 vmw_write(vmw_priv, SVGA_REG_RED_MASK, vmw_priv->vga_red_mask);
1067 vmw_write(vmw_priv, SVGA_REG_GREEN_MASK, vmw_priv->vga_green_mask);
1068 vmw_write(vmw_priv, SVGA_REG_BLUE_MASK, vmw_priv->vga_blue_mask);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001069 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1070 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1071 vmw_priv->vga_pitchlock);
1072 else if (vmw_fifo_have_pitchlock(vmw_priv))
1073 iowrite32(vmw_priv->vga_pitchlock,
1074 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001075
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001076 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1077 return 0;
1078
1079 for (i = 0; i < vmw_priv->num_displays; ++i) {
1080 save = &vmw_priv->vga_save[i];
1081 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1082 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1083 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1084 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1085 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1086 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1087 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1088 }
1089
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001090 return 0;
1091}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001092
1093int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1094 struct drm_file *file_priv)
1095{
1096 struct vmw_private *dev_priv = vmw_priv(dev);
1097 struct drm_vmw_update_layout_arg *arg =
1098 (struct drm_vmw_update_layout_arg *)data;
1099 struct vmw_master *vmaster = vmw_master(file_priv->master);
1100 void __user *user_rects;
1101 struct drm_vmw_rect *rects;
1102 unsigned rects_size;
1103 int ret;
1104
1105 ret = ttm_read_lock(&vmaster->lock, true);
1106 if (unlikely(ret != 0))
1107 return ret;
1108
1109 if (!arg->num_outputs) {
1110 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1111 vmw_kms_ldu_update_layout(dev_priv, 1, &def_rect);
1112 goto out_unlock;
1113 }
1114
1115 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
1116 rects = kzalloc(rects_size, GFP_KERNEL);
1117 if (unlikely(!rects)) {
1118 ret = -ENOMEM;
1119 goto out_unlock;
1120 }
1121
1122 user_rects = (void __user *)(unsigned long)arg->rects;
1123 ret = copy_from_user(rects, user_rects, rects_size);
1124 if (unlikely(ret != 0)) {
1125 DRM_ERROR("Failed to get rects.\n");
Dan Carpenter4ede00c2010-07-10 16:30:04 +02001126 ret = -EFAULT;
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001127 goto out_free;
1128 }
1129
1130 vmw_kms_ldu_update_layout(dev_priv, arg->num_outputs, rects);
1131
1132out_free:
1133 kfree(rects);
1134out_unlock:
1135 ttm_read_unlock(&vmaster->lock);
1136 return ret;
1137}
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001138
Thomas Hellstrome133e732010-10-05 12:43:04 +02001139bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1140 uint32_t pitch,
1141 uint32_t height)
1142{
1143 return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
1144}
1145
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001146u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1147{
1148 return 0;
1149}