blob: 00ec61921f2fa4d95926c47aef89afc622ffb201 [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
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +020030
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000031/* Might need a hrtimer here? */
32#define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
33
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000034void vmw_display_unit_cleanup(struct vmw_display_unit *du)
35{
36 if (du->cursor_surface)
37 vmw_surface_unreference(&du->cursor_surface);
38 if (du->cursor_dmabuf)
39 vmw_dmabuf_unreference(&du->cursor_dmabuf);
40 drm_crtc_cleanup(&du->crtc);
41 drm_encoder_cleanup(&du->encoder);
42 drm_connector_cleanup(&du->connector);
43}
44
45/*
46 * Display Unit Cursor functions
47 */
48
49int vmw_cursor_update_image(struct vmw_private *dev_priv,
50 u32 *image, u32 width, u32 height,
51 u32 hotspotX, u32 hotspotY)
52{
53 struct {
54 u32 cmd;
55 SVGAFifoCmdDefineAlphaCursor cursor;
56 } *cmd;
57 u32 image_size = width * height * 4;
58 u32 cmd_size = sizeof(*cmd) + image_size;
59
60 if (!image)
61 return -EINVAL;
62
63 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
64 if (unlikely(cmd == NULL)) {
65 DRM_ERROR("Fifo reserve failed.\n");
66 return -ENOMEM;
67 }
68
69 memset(cmd, 0, sizeof(*cmd));
70
71 memcpy(&cmd[1], image, image_size);
72
73 cmd->cmd = cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR);
74 cmd->cursor.id = cpu_to_le32(0);
75 cmd->cursor.width = cpu_to_le32(width);
76 cmd->cursor.height = cpu_to_le32(height);
77 cmd->cursor.hotspotX = cpu_to_le32(hotspotX);
78 cmd->cursor.hotspotY = cpu_to_le32(hotspotY);
79
80 vmw_fifo_commit(dev_priv, cmd_size);
81
82 return 0;
83}
84
85void vmw_cursor_update_position(struct vmw_private *dev_priv,
86 bool show, int x, int y)
87{
88 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
89 uint32_t count;
90
91 iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
92 iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
93 iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
94 count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
95 iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
96}
97
98int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
99 uint32_t handle, uint32_t width, uint32_t height)
100{
101 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
102 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
103 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
104 struct vmw_surface *surface = NULL;
105 struct vmw_dma_buffer *dmabuf = NULL;
106 int ret;
107
108 if (handle) {
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100109 ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
110 handle, &surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000111 if (!ret) {
112 if (!surface->snooper.image) {
113 DRM_ERROR("surface not suitable for cursor\n");
114 return -EINVAL;
115 }
116 } else {
117 ret = vmw_user_dmabuf_lookup(tfile,
118 handle, &dmabuf);
119 if (ret) {
120 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
121 return -EINVAL;
122 }
123 }
124 }
125
126 /* takedown old cursor */
127 if (du->cursor_surface) {
128 du->cursor_surface->snooper.crtc = NULL;
129 vmw_surface_unreference(&du->cursor_surface);
130 }
131 if (du->cursor_dmabuf)
132 vmw_dmabuf_unreference(&du->cursor_dmabuf);
133
134 /* setup new image */
135 if (surface) {
136 /* vmw_user_surface_lookup takes one reference */
137 du->cursor_surface = surface;
138
139 du->cursor_surface->snooper.crtc = crtc;
140 du->cursor_age = du->cursor_surface->snooper.age;
141 vmw_cursor_update_image(dev_priv, surface->snooper.image,
142 64, 64, du->hotspot_x, du->hotspot_y);
143 } else if (dmabuf) {
144 struct ttm_bo_kmap_obj map;
145 unsigned long kmap_offset;
146 unsigned long kmap_num;
147 void *virtual;
148 bool dummy;
149
150 /* vmw_user_surface_lookup takes one reference */
151 du->cursor_dmabuf = dmabuf;
152
153 kmap_offset = 0;
154 kmap_num = (64*64*4) >> PAGE_SHIFT;
155
156 ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
157 if (unlikely(ret != 0)) {
158 DRM_ERROR("reserve failed\n");
159 return -EINVAL;
160 }
161
162 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
163 if (unlikely(ret != 0))
164 goto err_unreserve;
165
166 virtual = ttm_kmap_obj_virtual(&map, &dummy);
167 vmw_cursor_update_image(dev_priv, virtual, 64, 64,
168 du->hotspot_x, du->hotspot_y);
169
170 ttm_bo_kunmap(&map);
171err_unreserve:
172 ttm_bo_unreserve(&dmabuf->base);
173
174 } else {
175 vmw_cursor_update_position(dev_priv, false, 0, 0);
176 return 0;
177 }
178
179 vmw_cursor_update_position(dev_priv, true, du->cursor_x, du->cursor_y);
180
181 return 0;
182}
183
184int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
185{
186 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
187 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
188 bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
189
190 du->cursor_x = x + crtc->x;
191 du->cursor_y = y + crtc->y;
192
193 vmw_cursor_update_position(dev_priv, shown,
194 du->cursor_x, du->cursor_y);
195
196 return 0;
197}
198
199void vmw_kms_cursor_snoop(struct vmw_surface *srf,
200 struct ttm_object_file *tfile,
201 struct ttm_buffer_object *bo,
202 SVGA3dCmdHeader *header)
203{
204 struct ttm_bo_kmap_obj map;
205 unsigned long kmap_offset;
206 unsigned long kmap_num;
207 SVGA3dCopyBox *box;
208 unsigned box_count;
209 void *virtual;
210 bool dummy;
211 struct vmw_dma_cmd {
212 SVGA3dCmdHeader header;
213 SVGA3dCmdSurfaceDMA dma;
214 } *cmd;
215 int ret;
216
217 cmd = container_of(header, struct vmw_dma_cmd, header);
218
219 /* No snooper installed */
220 if (!srf->snooper.image)
221 return;
222
223 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
224 DRM_ERROR("face and mipmap for cursors should never != 0\n");
225 return;
226 }
227
228 if (cmd->header.size < 64) {
229 DRM_ERROR("at least one full copy box must be given\n");
230 return;
231 }
232
233 box = (SVGA3dCopyBox *)&cmd[1];
234 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
235 sizeof(SVGA3dCopyBox);
236
237 if (cmd->dma.guest.pitch != (64 * 4) ||
238 cmd->dma.guest.ptr.offset % PAGE_SIZE ||
239 box->x != 0 || box->y != 0 || box->z != 0 ||
240 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
241 box->w != 64 || box->h != 64 || box->d != 1 ||
242 box_count != 1) {
243 /* TODO handle none page aligned offsets */
244 /* TODO handle partial uploads and pitch != 256 */
245 /* TODO handle more then one copy (size != 64) */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300246 DRM_ERROR("lazy programmer, can't handle weird stuff\n");
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000247 return;
248 }
249
250 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
251 kmap_num = (64*64*4) >> PAGE_SHIFT;
252
253 ret = ttm_bo_reserve(bo, true, false, false, 0);
254 if (unlikely(ret != 0)) {
255 DRM_ERROR("reserve failed\n");
256 return;
257 }
258
259 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
260 if (unlikely(ret != 0))
261 goto err_unreserve;
262
263 virtual = ttm_kmap_obj_virtual(&map, &dummy);
264
265 memcpy(srf->snooper.image, virtual, 64*64*4);
266 srf->snooper.age++;
267
268 /* we can't call this function from this function since execbuf has
269 * reserved fifo space.
270 *
271 * if (srf->snooper.crtc)
272 * vmw_ldu_crtc_cursor_update_image(dev_priv,
273 * srf->snooper.image, 64, 64,
274 * du->hotspot_x, du->hotspot_y);
275 */
276
277 ttm_bo_kunmap(&map);
278err_unreserve:
279 ttm_bo_unreserve(bo);
280}
281
282void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
283{
284 struct drm_device *dev = dev_priv->dev;
285 struct vmw_display_unit *du;
286 struct drm_crtc *crtc;
287
288 mutex_lock(&dev->mode_config.mutex);
289
290 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
291 du = vmw_crtc_to_du(crtc);
292 if (!du->cursor_surface ||
293 du->cursor_age == du->cursor_surface->snooper.age)
294 continue;
295
296 du->cursor_age = du->cursor_surface->snooper.age;
297 vmw_cursor_update_image(dev_priv,
298 du->cursor_surface->snooper.image,
299 64, 64, du->hotspot_x, du->hotspot_y);
300 }
301
302 mutex_unlock(&dev->mode_config.mutex);
303}
304
305/*
306 * Generic framebuffer code
307 */
308
309int vmw_framebuffer_create_handle(struct drm_framebuffer *fb,
310 struct drm_file *file_priv,
311 unsigned int *handle)
312{
313 if (handle)
314 handle = 0;
315
316 return 0;
317}
318
319/*
320 * Surface framebuffer code
321 */
322
323#define vmw_framebuffer_to_vfbs(x) \
324 container_of(x, struct vmw_framebuffer_surface, base.base)
325
326struct vmw_framebuffer_surface {
327 struct vmw_framebuffer base;
328 struct vmw_surface *surface;
Thomas Hellstrom22ee8612010-05-28 11:22:00 +0200329 struct vmw_dma_buffer *buffer;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200330 struct list_head head;
331 struct drm_master *master;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000332};
333
334void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
335{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200336 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000337 vmw_framebuffer_to_vfbs(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200338 struct vmw_master *vmaster = vmw_master(vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000339
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200340
341 mutex_lock(&vmaster->fb_surf_mutex);
342 list_del(&vfbs->head);
343 mutex_unlock(&vmaster->fb_surf_mutex);
344
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200345 drm_master_put(&vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000346 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200347 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200348 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000349
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200350 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000351}
352
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200353static int do_surface_dirty_sou(struct vmw_private *dev_priv,
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200354 struct drm_file *file_priv,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200355 struct vmw_framebuffer *framebuffer,
356 struct vmw_surface *surf,
357 unsigned flags, unsigned color,
358 struct drm_clip_rect *clips,
359 unsigned num_clips, int inc)
360{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200361 struct drm_clip_rect *clips_ptr;
362 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
363 struct drm_crtc *crtc;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200364 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200365 int i, num_units;
366 int ret = 0; /* silence warning */
367 int left, right, top, bottom;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200368
369 struct {
370 SVGA3dCmdHeader header;
371 SVGA3dCmdBlitSurfaceToScreen body;
372 } *cmd;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200373 SVGASignedRect *blits;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200374
375
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200376 num_units = 0;
377 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
378 head) {
379 if (crtc->fb != &framebuffer->base)
380 continue;
381 units[num_units++] = vmw_crtc_to_du(crtc);
382 }
383
384 BUG_ON(surf == NULL);
385 BUG_ON(!clips || !num_clips);
386
387 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200388 cmd = kzalloc(fifo_size, GFP_KERNEL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200389 if (unlikely(cmd == NULL)) {
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200390 DRM_ERROR("Temporary fifo memory alloc failed.\n");
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200391 return -ENOMEM;
392 }
393
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200394 left = clips->x1;
395 right = clips->x2;
396 top = clips->y1;
397 bottom = clips->y2;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200398
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200399 clips_ptr = clips;
400 for (i = 1; i < num_clips; i++, clips_ptr += inc) {
401 left = min_t(int, left, (int)clips_ptr->x1);
402 right = max_t(int, right, (int)clips_ptr->x2);
403 top = min_t(int, top, (int)clips_ptr->y1);
404 bottom = max_t(int, bottom, (int)clips_ptr->y2);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200405 }
406
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200407 /* only need to do this once */
408 memset(cmd, 0, fifo_size);
409 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
410 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
411
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200412 cmd->body.srcRect.left = left;
413 cmd->body.srcRect.right = right;
414 cmd->body.srcRect.top = top;
415 cmd->body.srcRect.bottom = bottom;
416
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200417 clips_ptr = clips;
418 blits = (SVGASignedRect *)&cmd[1];
419 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
420 blits[i].left = clips_ptr->x1 - left;
421 blits[i].right = clips_ptr->x2 - left;
422 blits[i].top = clips_ptr->y1 - top;
423 blits[i].bottom = clips_ptr->y2 - top;
424 }
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200425
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200426 /* do per unit writing, reuse fifo for each */
427 for (i = 0; i < num_units; i++) {
428 struct vmw_display_unit *unit = units[i];
429 int clip_x1 = left - unit->crtc.x;
430 int clip_y1 = top - unit->crtc.y;
431 int clip_x2 = right - unit->crtc.x;
432 int clip_y2 = bottom - unit->crtc.y;
433
434 /* skip any crtcs that misses the clip region */
435 if (clip_x1 >= unit->crtc.mode.hdisplay ||
436 clip_y1 >= unit->crtc.mode.vdisplay ||
437 clip_x2 <= 0 || clip_y2 <= 0)
438 continue;
439
440 /* need to reset sid as it is changed by execbuf */
441 cmd->body.srcImage.sid = cpu_to_le32(framebuffer->user_handle);
442
443 cmd->body.destScreenId = unit->unit;
444
445 /*
446 * The blit command is a lot more resilient then the
447 * readback command when it comes to clip rects. So its
448 * okay to go out of bounds.
449 */
450
451 cmd->body.destRect.left = clip_x1;
452 cmd->body.destRect.right = clip_x2;
453 cmd->body.destRect.top = clip_y1;
454 cmd->body.destRect.bottom = clip_y2;
455
456
457 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
458 fifo_size, 0, NULL);
459
460 if (unlikely(ret != 0))
461 break;
462 }
463
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200464 kfree(cmd);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200465
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200466 return ret;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200467}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000468
469int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200470 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000471 unsigned flags, unsigned color,
472 struct drm_clip_rect *clips,
473 unsigned num_clips)
474{
475 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200476 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000477 struct vmw_framebuffer_surface *vfbs =
478 vmw_framebuffer_to_vfbs(framebuffer);
479 struct vmw_surface *surf = vfbs->surface;
480 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200481 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000482
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200483 if (unlikely(vfbs->master != file_priv->master))
484 return -EINVAL;
485
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200486 /* Require ScreenObject support for 3D */
487 if (!dev_priv->sou_priv)
488 return -EINVAL;
489
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200490 ret = ttm_read_lock(&vmaster->lock, true);
491 if (unlikely(ret != 0))
492 return ret;
493
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000494 if (!num_clips) {
495 num_clips = 1;
496 clips = &norect;
497 norect.x1 = norect.y1 = 0;
498 norect.x2 = framebuffer->width;
499 norect.y2 = framebuffer->height;
500 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
501 num_clips /= 2;
502 inc = 2; /* skip source rects */
503 }
504
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200505 ret = do_surface_dirty_sou(dev_priv, file_priv, &vfbs->base, surf,
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200506 flags, color,
507 clips, num_clips, inc);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000508
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200509 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000510 return 0;
511}
512
513static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
514 .destroy = vmw_framebuffer_surface_destroy,
515 .dirty = vmw_framebuffer_surface_dirty,
516 .create_handle = vmw_framebuffer_create_handle,
517};
518
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200519static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200520 struct drm_file *file_priv,
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200521 struct vmw_surface *surface,
522 struct vmw_framebuffer **out,
523 const struct drm_mode_fb_cmd
524 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000525
526{
527 struct drm_device *dev = dev_priv->dev;
528 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200529 enum SVGA3dSurfaceFormat format;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200530 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000531 int ret;
532
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200533 /* 3D is only supported on HWv8 hosts which supports screen objects */
534 if (!dev_priv->sou_priv)
535 return -ENOSYS;
536
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200537 /*
538 * Sanity checks.
539 */
540
541 if (unlikely(surface->mip_levels[0] != 1 ||
542 surface->num_sizes != 1 ||
543 surface->sizes[0].width < mode_cmd->width ||
544 surface->sizes[0].height < mode_cmd->height ||
545 surface->sizes[0].depth != 1)) {
546 DRM_ERROR("Incompatible surface dimensions "
547 "for requested mode.\n");
548 return -EINVAL;
549 }
550
551 switch (mode_cmd->depth) {
552 case 32:
553 format = SVGA3D_A8R8G8B8;
554 break;
555 case 24:
556 format = SVGA3D_X8R8G8B8;
557 break;
558 case 16:
559 format = SVGA3D_R5G6B5;
560 break;
561 case 15:
562 format = SVGA3D_A1R5G5B5;
563 break;
Michel Dänzerf01b7ba2011-08-31 07:42:47 +0000564 case 8:
565 format = SVGA3D_LUMINANCE8;
566 break;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200567 default:
568 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
569 return -EINVAL;
570 }
571
572 if (unlikely(format != surface->format)) {
573 DRM_ERROR("Invalid surface format for requested mode.\n");
574 return -EINVAL;
575 }
576
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000577 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
578 if (!vfbs) {
579 ret = -ENOMEM;
580 goto out_err1;
581 }
582
583 ret = drm_framebuffer_init(dev, &vfbs->base.base,
584 &vmw_framebuffer_surface_funcs);
585 if (ret)
586 goto out_err2;
587
588 if (!vmw_surface_reference(surface)) {
589 DRM_ERROR("failed to reference surface %p\n", surface);
590 goto out_err3;
591 }
592
593 /* XXX get the first 3 from the surface info */
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200594 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
595 vfbs->base.base.pitch = mode_cmd->pitch;
596 vfbs->base.base.depth = mode_cmd->depth;
597 vfbs->base.base.width = mode_cmd->width;
598 vfbs->base.base.height = mode_cmd->height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000599 vfbs->surface = surface;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200600 vfbs->base.user_handle = mode_cmd->handle;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200601 vfbs->master = drm_master_get(file_priv->master);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200602
603 mutex_lock(&vmaster->fb_surf_mutex);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200604 list_add_tail(&vfbs->head, &vmaster->fb_surf);
605 mutex_unlock(&vmaster->fb_surf_mutex);
606
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000607 *out = &vfbs->base;
608
609 return 0;
610
611out_err3:
612 drm_framebuffer_cleanup(&vfbs->base.base);
613out_err2:
614 kfree(vfbs);
615out_err1:
616 return ret;
617}
618
619/*
620 * Dmabuf framebuffer code
621 */
622
623#define vmw_framebuffer_to_vfbd(x) \
624 container_of(x, struct vmw_framebuffer_dmabuf, base.base)
625
626struct vmw_framebuffer_dmabuf {
627 struct vmw_framebuffer base;
628 struct vmw_dma_buffer *buffer;
629};
630
631void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
632{
633 struct vmw_framebuffer_dmabuf *vfbd =
634 vmw_framebuffer_to_vfbd(framebuffer);
635
636 drm_framebuffer_cleanup(framebuffer);
637 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200638 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000639
640 kfree(vfbd);
641}
642
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200643static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
644 struct vmw_framebuffer *framebuffer,
645 struct vmw_dma_buffer *buffer,
646 unsigned flags, unsigned color,
647 struct drm_clip_rect *clips,
648 unsigned num_clips, int increment)
649{
650 size_t fifo_size;
651 int i;
652
653 struct {
654 uint32_t header;
655 SVGAFifoCmdUpdate body;
656 } *cmd;
657
658 fifo_size = sizeof(*cmd) * num_clips;
659 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
660 if (unlikely(cmd == NULL)) {
661 DRM_ERROR("Fifo reserve failed.\n");
662 return -ENOMEM;
663 }
664
665 memset(cmd, 0, fifo_size);
666 for (i = 0; i < num_clips; i++, clips += increment) {
667 cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
668 cmd[i].body.x = cpu_to_le32(clips->x1);
669 cmd[i].body.y = cpu_to_le32(clips->y1);
670 cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
671 cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
672 }
673
674 vmw_fifo_commit(dev_priv, fifo_size);
675 return 0;
676}
677
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200678static int do_dmabuf_define_gmrfb(struct drm_file *file_priv,
679 struct vmw_private *dev_priv,
680 struct vmw_framebuffer *framebuffer)
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200681{
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200682 int depth = framebuffer->base.depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200683 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200684 int ret;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200685
686 struct {
687 uint32_t header;
688 SVGAFifoCmdDefineGMRFB body;
689 } *cmd;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200690
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200691 /* Emulate RGBA support, contrary to svga_reg.h this is not
692 * supported by hosts. This is only a problem if we are reading
693 * this value later and expecting what we uploaded back.
694 */
695 if (depth == 32)
696 depth = 24;
697
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200698 fifo_size = sizeof(*cmd);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200699 cmd = kmalloc(fifo_size, GFP_KERNEL);
700 if (unlikely(cmd == NULL)) {
701 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
702 return -ENOMEM;
703 }
704
705 memset(cmd, 0, fifo_size);
706 cmd->header = SVGA_CMD_DEFINE_GMRFB;
707 cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200708 cmd->body.format.colorDepth = depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200709 cmd->body.format.reserved = 0;
710 cmd->body.bytesPerLine = framebuffer->base.pitch;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200711 cmd->body.ptr.gmrId = framebuffer->user_handle;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200712 cmd->body.ptr.offset = 0;
713
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200714 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
715 fifo_size, 0, NULL);
716
717 kfree(cmd);
718
719 return ret;
720}
721
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200722static int do_dmabuf_dirty_sou(struct drm_file *file_priv,
723 struct vmw_private *dev_priv,
724 struct vmw_framebuffer *framebuffer,
725 struct vmw_dma_buffer *buffer,
726 unsigned flags, unsigned color,
727 struct drm_clip_rect *clips,
728 unsigned num_clips, int increment)
729{
730 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
731 struct drm_clip_rect *clips_ptr;
732 int i, k, num_units, ret;
733 struct drm_crtc *crtc;
734 size_t fifo_size;
735
736 struct {
737 uint32_t header;
738 SVGAFifoCmdBlitGMRFBToScreen body;
739 } *blits;
740
741 ret = do_dmabuf_define_gmrfb(file_priv, dev_priv, framebuffer);
742 if (unlikely(ret != 0))
743 return ret; /* define_gmrfb prints warnings */
744
745 fifo_size = sizeof(*blits) * num_clips;
746 blits = kmalloc(fifo_size, GFP_KERNEL);
747 if (unlikely(blits == NULL)) {
748 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
749 return -ENOMEM;
750 }
751
752 num_units = 0;
753 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
754 if (crtc->fb != &framebuffer->base)
755 continue;
756 units[num_units++] = vmw_crtc_to_du(crtc);
757 }
758
759 for (k = 0; k < num_units; k++) {
760 struct vmw_display_unit *unit = units[k];
761 int hit_num = 0;
762
763 clips_ptr = clips;
764 for (i = 0; i < num_clips; i++, clips_ptr += increment) {
765 int clip_x1 = clips_ptr->x1 - unit->crtc.x;
766 int clip_y1 = clips_ptr->y1 - unit->crtc.y;
767 int clip_x2 = clips_ptr->x2 - unit->crtc.x;
768 int clip_y2 = clips_ptr->y2 - unit->crtc.y;
769
770 /* skip any crtcs that misses the clip region */
771 if (clip_x1 >= unit->crtc.mode.hdisplay ||
772 clip_y1 >= unit->crtc.mode.vdisplay ||
773 clip_x2 <= 0 || clip_y2 <= 0)
774 continue;
775
776 blits[hit_num].header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
777 blits[hit_num].body.destScreenId = unit->unit;
778 blits[hit_num].body.srcOrigin.x = clips_ptr->x1;
779 blits[hit_num].body.srcOrigin.y = clips_ptr->y1;
780 blits[hit_num].body.destRect.left = clip_x1;
781 blits[hit_num].body.destRect.top = clip_y1;
782 blits[hit_num].body.destRect.right = clip_x2;
783 blits[hit_num].body.destRect.bottom = clip_y2;
784 hit_num++;
785 }
786
787 /* no clips hit the crtc */
788 if (hit_num == 0)
789 continue;
790
791 fifo_size = sizeof(*blits) * hit_num;
792 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, blits,
793 fifo_size, 0, NULL);
794
795 if (unlikely(ret != 0))
796 break;
797 }
798
799 kfree(blits);
800
801 return ret;
802}
803
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000804int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200805 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000806 unsigned flags, unsigned color,
807 struct drm_clip_rect *clips,
808 unsigned num_clips)
809{
810 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200811 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200812 struct vmw_framebuffer_dmabuf *vfbd =
813 vmw_framebuffer_to_vfbd(framebuffer);
814 struct vmw_dma_buffer *dmabuf = vfbd->buffer;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000815 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200816 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000817
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200818 ret = ttm_read_lock(&vmaster->lock, true);
819 if (unlikely(ret != 0))
820 return ret;
821
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100822 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000823 num_clips = 1;
824 clips = &norect;
825 norect.x1 = norect.y1 = 0;
826 norect.x2 = framebuffer->width;
827 norect.y2 = framebuffer->height;
828 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
829 num_clips /= 2;
830 increment = 2;
831 }
832
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200833 if (dev_priv->ldu_priv) {
834 ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base, dmabuf,
835 flags, color,
836 clips, num_clips, increment);
837 } else {
838 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, &vfbd->base,
839 dmabuf, flags, color,
840 clips, num_clips, increment);
841 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000842
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200843 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200844 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000845}
846
847static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
848 .destroy = vmw_framebuffer_dmabuf_destroy,
849 .dirty = vmw_framebuffer_dmabuf_dirty,
850 .create_handle = vmw_framebuffer_create_handle,
851};
852
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200853/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200854 * Pin the dmabuffer to the start of vram.
855 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000856static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
857{
858 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
859 struct vmw_framebuffer_dmabuf *vfbd =
860 vmw_framebuffer_to_vfbd(&vfb->base);
861 int ret;
862
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200863 /* This code should not be used with screen objects */
864 BUG_ON(dev_priv->sou_priv);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200865
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000866 vmw_overlay_pause_all(dev_priv);
867
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200868 ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000869
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000870 vmw_overlay_resume_all(dev_priv);
871
Jakob Bornecrantz316ab132010-05-28 11:22:05 +0200872 WARN_ON(ret != 0);
873
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000874 return 0;
875}
876
877static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
878{
879 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
880 struct vmw_framebuffer_dmabuf *vfbd =
881 vmw_framebuffer_to_vfbd(&vfb->base);
882
883 if (!vfbd->buffer) {
884 WARN_ON(!vfbd->buffer);
885 return 0;
886 }
887
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200888 return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000889}
890
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200891static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
892 struct vmw_dma_buffer *dmabuf,
893 struct vmw_framebuffer **out,
894 const struct drm_mode_fb_cmd
895 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000896
897{
898 struct drm_device *dev = dev_priv->dev;
899 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200900 unsigned int requested_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000901 int ret;
902
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200903 requested_size = mode_cmd->height * mode_cmd->pitch;
904 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
905 DRM_ERROR("Screen buffer object size is too small "
906 "for requested mode.\n");
907 return -EINVAL;
908 }
909
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200910 /* Limited framebuffer color depth support for screen objects */
911 if (dev_priv->sou_priv) {
912 switch (mode_cmd->depth) {
913 case 32:
914 case 24:
915 /* Only support 32 bpp for 32 and 24 depth fbs */
916 if (mode_cmd->bpp == 32)
917 break;
918
919 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
920 mode_cmd->depth, mode_cmd->bpp);
921 return -EINVAL;
922 case 16:
923 case 15:
924 /* Only support 16 bpp for 16 and 15 depth fbs */
925 if (mode_cmd->bpp == 16)
926 break;
927
928 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
929 mode_cmd->depth, mode_cmd->bpp);
930 return -EINVAL;
931 default:
932 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
933 return -EINVAL;
934 }
935 }
936
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000937 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
938 if (!vfbd) {
939 ret = -ENOMEM;
940 goto out_err1;
941 }
942
943 ret = drm_framebuffer_init(dev, &vfbd->base.base,
944 &vmw_framebuffer_dmabuf_funcs);
945 if (ret)
946 goto out_err2;
947
948 if (!vmw_dmabuf_reference(dmabuf)) {
949 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
950 goto out_err3;
951 }
952
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200953 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
954 vfbd->base.base.pitch = mode_cmd->pitch;
955 vfbd->base.base.depth = mode_cmd->depth;
956 vfbd->base.base.width = mode_cmd->width;
957 vfbd->base.base.height = mode_cmd->height;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200958 if (!dev_priv->sou_priv) {
959 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
960 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
961 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200962 vfbd->base.dmabuf = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000963 vfbd->buffer = dmabuf;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200964 vfbd->base.user_handle = mode_cmd->handle;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000965 *out = &vfbd->base;
966
967 return 0;
968
969out_err3:
970 drm_framebuffer_cleanup(&vfbd->base.base);
971out_err2:
972 kfree(vfbd);
973out_err1:
974 return ret;
975}
976
977/*
978 * Generic Kernel modesetting functions
979 */
980
981static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
982 struct drm_file *file_priv,
983 struct drm_mode_fb_cmd *mode_cmd)
984{
985 struct vmw_private *dev_priv = vmw_priv(dev);
986 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
987 struct vmw_framebuffer *vfb = NULL;
988 struct vmw_surface *surface = NULL;
989 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200990 struct ttm_base_object *user_obj;
Thomas Hellstrome133e732010-10-05 12:43:04 +0200991 u64 required_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000992 int ret;
993
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200994 /**
995 * This code should be conditioned on Screen Objects not being used.
996 * If screen objects are used, we can allocate a GMR to hold the
997 * requested framebuffer.
998 */
999
1000 required_size = mode_cmd->pitch * mode_cmd->height;
Thomas Hellstrome133e732010-10-05 12:43:04 +02001001 if (unlikely(required_size > (u64) dev_priv->vram_size)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001002 DRM_ERROR("VRAM size is too small for requested mode.\n");
1003 return NULL;
1004 }
1005
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001006 /*
1007 * Take a reference on the user object of the resource
1008 * backing the kms fb. This ensures that user-space handle
1009 * lookups on that resource will always work as long as
1010 * it's registered with a kms framebuffer. This is important,
1011 * since vmw_execbuf_process identifies resources in the
1012 * command stream using user-space handles.
1013 */
1014
1015 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handle);
1016 if (unlikely(user_obj == NULL)) {
1017 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1018 return ERR_PTR(-ENOENT);
1019 }
1020
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001021 /**
1022 * End conditioned code.
1023 */
1024
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001025 ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
1026 mode_cmd->handle, &surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001027 if (ret)
1028 goto try_dmabuf;
1029
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001030 if (!surface->scanout)
1031 goto err_not_scanout;
1032
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001033 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv, surface,
1034 &vfb, mode_cmd);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001035
1036 /* vmw_user_surface_lookup takes one ref so does new_fb */
1037 vmw_surface_unreference(&surface);
1038
1039 if (ret) {
1040 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001041 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001042 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001043 } else
1044 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001045 return &vfb->base;
1046
1047try_dmabuf:
1048 DRM_INFO("%s: trying buffer\n", __func__);
1049
1050 ret = vmw_user_dmabuf_lookup(tfile, mode_cmd->handle, &bo);
1051 if (ret) {
1052 DRM_ERROR("failed to find buffer: %i\n", ret);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001053 return ERR_PTR(-ENOENT);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001054 }
1055
1056 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001057 mode_cmd);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001058
1059 /* vmw_user_dmabuf_lookup takes one ref so does new_fb */
1060 vmw_dmabuf_unreference(&bo);
1061
1062 if (ret) {
1063 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001064 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001065 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001066 } else
1067 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001068
1069 return &vfb->base;
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001070
1071err_not_scanout:
1072 DRM_ERROR("surface not marked as scanout\n");
1073 /* vmw_user_surface_lookup takes one ref */
1074 vmw_surface_unreference(&surface);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001075 ttm_base_object_unref(&user_obj);
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001076
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001077 return ERR_PTR(-EINVAL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001078}
1079
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001080static struct drm_mode_config_funcs vmw_kms_funcs = {
1081 .fb_create = vmw_kms_fb_create,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001082};
1083
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001084int vmw_kms_present(struct vmw_private *dev_priv,
1085 struct drm_file *file_priv,
1086 struct vmw_framebuffer *vfb,
1087 struct vmw_surface *surface,
1088 uint32_t sid,
1089 int32_t destX, int32_t destY,
1090 struct drm_vmw_rect *clips,
1091 uint32_t num_clips)
1092{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001093 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1094 struct drm_crtc *crtc;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001095 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001096 int i, k, num_units;
1097 int ret = 0; /* silence warning */
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001098
1099 struct {
1100 SVGA3dCmdHeader header;
1101 SVGA3dCmdBlitSurfaceToScreen body;
1102 } *cmd;
1103 SVGASignedRect *blits;
1104
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001105 num_units = 0;
1106 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1107 if (crtc->fb != &vfb->base)
1108 continue;
1109 units[num_units++] = vmw_crtc_to_du(crtc);
1110 }
1111
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001112 BUG_ON(surface == NULL);
1113 BUG_ON(!clips || !num_clips);
1114
1115 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
1116 cmd = kmalloc(fifo_size, GFP_KERNEL);
1117 if (unlikely(cmd == NULL)) {
1118 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1119 return -ENOMEM;
1120 }
1121
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001122 /* only need to do this once */
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001123 memset(cmd, 0, fifo_size);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001124 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
1125 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
1126
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001127 cmd->body.srcRect.left = 0;
1128 cmd->body.srcRect.right = surface->sizes[0].width;
1129 cmd->body.srcRect.top = 0;
1130 cmd->body.srcRect.bottom = surface->sizes[0].height;
1131
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001132 blits = (SVGASignedRect *)&cmd[1];
1133 for (i = 0; i < num_clips; i++) {
1134 blits[i].left = clips[i].x;
1135 blits[i].right = clips[i].x + clips[i].w;
1136 blits[i].top = clips[i].y;
1137 blits[i].bottom = clips[i].y + clips[i].h;
1138 }
1139
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001140 for (k = 0; k < num_units; k++) {
1141 struct vmw_display_unit *unit = units[k];
1142 int clip_x1 = destX - unit->crtc.x;
1143 int clip_y1 = destY - unit->crtc.y;
1144 int clip_x2 = clip_x1 + surface->sizes[0].width;
1145 int clip_y2 = clip_y1 + surface->sizes[0].height;
1146
1147 /* skip any crtcs that misses the clip region */
1148 if (clip_x1 >= unit->crtc.mode.hdisplay ||
1149 clip_y1 >= unit->crtc.mode.vdisplay ||
1150 clip_x2 <= 0 || clip_y2 <= 0)
1151 continue;
1152
1153 /* need to reset sid as it is changed by execbuf */
1154 cmd->body.srcImage.sid = sid;
1155
1156 cmd->body.destScreenId = unit->unit;
1157
1158 /*
1159 * The blit command is a lot more resilient then the
1160 * readback command when it comes to clip rects. So its
1161 * okay to go out of bounds.
1162 */
1163
1164 cmd->body.destRect.left = clip_x1;
1165 cmd->body.destRect.right = clip_x2;
1166 cmd->body.destRect.top = clip_y1;
1167 cmd->body.destRect.bottom = clip_y2;
1168
1169 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
1170 fifo_size, 0, NULL);
1171
1172 if (unlikely(ret != 0))
1173 break;
1174 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001175
1176 kfree(cmd);
1177
1178 return ret;
1179}
1180
1181int vmw_kms_readback(struct vmw_private *dev_priv,
1182 struct drm_file *file_priv,
1183 struct vmw_framebuffer *vfb,
1184 struct drm_vmw_fence_rep __user *user_fence_rep,
1185 struct drm_vmw_rect *clips,
1186 uint32_t num_clips)
1187{
1188 struct vmw_framebuffer_dmabuf *vfbd =
1189 vmw_framebuffer_to_vfbd(&vfb->base);
1190 struct vmw_dma_buffer *dmabuf = vfbd->buffer;
1191 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1192 struct drm_crtc *crtc;
1193 size_t fifo_size;
1194 int i, k, ret, num_units, blits_pos;
1195
1196 struct {
1197 uint32_t header;
1198 SVGAFifoCmdDefineGMRFB body;
1199 } *cmd;
1200 struct {
1201 uint32_t header;
1202 SVGAFifoCmdBlitScreenToGMRFB body;
1203 } *blits;
1204
1205 num_units = 0;
1206 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1207 if (crtc->fb != &vfb->base)
1208 continue;
1209 units[num_units++] = vmw_crtc_to_du(crtc);
1210 }
1211
1212 BUG_ON(dmabuf == NULL);
1213 BUG_ON(!clips || !num_clips);
1214
1215 /* take a safe guess at fifo size */
1216 fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
1217 cmd = kmalloc(fifo_size, GFP_KERNEL);
1218 if (unlikely(cmd == NULL)) {
1219 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1220 return -ENOMEM;
1221 }
1222
1223 memset(cmd, 0, fifo_size);
1224 cmd->header = SVGA_CMD_DEFINE_GMRFB;
1225 cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
1226 cmd->body.format.colorDepth = vfb->base.depth;
1227 cmd->body.format.reserved = 0;
1228 cmd->body.bytesPerLine = vfb->base.pitch;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001229 cmd->body.ptr.gmrId = vfb->user_handle;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001230 cmd->body.ptr.offset = 0;
1231
1232 blits = (void *)&cmd[1];
1233 blits_pos = 0;
1234 for (i = 0; i < num_units; i++) {
1235 struct drm_vmw_rect *c = clips;
1236 for (k = 0; k < num_clips; k++, c++) {
1237 /* transform clip coords to crtc origin based coords */
1238 int clip_x1 = c->x - units[i]->crtc.x;
1239 int clip_x2 = c->x - units[i]->crtc.x + c->w;
1240 int clip_y1 = c->y - units[i]->crtc.y;
1241 int clip_y2 = c->y - units[i]->crtc.y + c->h;
1242 int dest_x = c->x;
1243 int dest_y = c->y;
1244
1245 /* compensate for clipping, we negate
1246 * a negative number and add that.
1247 */
1248 if (clip_x1 < 0)
1249 dest_x += -clip_x1;
1250 if (clip_y1 < 0)
1251 dest_y += -clip_y1;
1252
1253 /* clip */
1254 clip_x1 = max(clip_x1, 0);
1255 clip_y1 = max(clip_y1, 0);
1256 clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
1257 clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
1258
1259 /* and cull any rects that misses the crtc */
1260 if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
1261 clip_y1 >= units[i]->crtc.mode.vdisplay ||
1262 clip_x2 <= 0 || clip_y2 <= 0)
1263 continue;
1264
1265 blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1266 blits[blits_pos].body.srcScreenId = units[i]->unit;
1267 blits[blits_pos].body.destOrigin.x = dest_x;
1268 blits[blits_pos].body.destOrigin.y = dest_y;
1269
1270 blits[blits_pos].body.srcRect.left = clip_x1;
1271 blits[blits_pos].body.srcRect.top = clip_y1;
1272 blits[blits_pos].body.srcRect.right = clip_x2;
1273 blits[blits_pos].body.srcRect.bottom = clip_y2;
1274 blits_pos++;
1275 }
1276 }
1277 /* reset size here and use calculated exact size from loops */
1278 fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
1279
1280 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
1281 0, user_fence_rep);
1282
1283 kfree(cmd);
1284
1285 return ret;
1286}
1287
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001288int vmw_kms_init(struct vmw_private *dev_priv)
1289{
1290 struct drm_device *dev = dev_priv->dev;
1291 int ret;
1292
1293 drm_mode_config_init(dev);
1294 dev->mode_config.funcs = &vmw_kms_funcs;
Jakob Bornecrantz3bef3572010-02-09 19:41:57 +00001295 dev->mode_config.min_width = 1;
1296 dev->mode_config.min_height = 1;
Jakob Bornecrantz7e71f8a2010-05-28 11:21:54 +02001297 /* assumed largest fb size */
1298 dev->mode_config.max_width = 8192;
1299 dev->mode_config.max_height = 8192;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001300
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001301 ret = vmw_kms_init_screen_object_display(dev_priv);
1302 if (ret) /* Fallback */
1303 (void)vmw_kms_init_legacy_display_system(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001304
1305 return 0;
1306}
1307
1308int vmw_kms_close(struct vmw_private *dev_priv)
1309{
1310 /*
1311 * Docs says we should take the lock before calling this function
1312 * but since it destroys encoders and our destructor calls
1313 * drm_encoder_cleanup which takes the lock we deadlock.
1314 */
1315 drm_mode_config_cleanup(dev_priv->dev);
1316 vmw_kms_close_legacy_display_system(dev_priv);
1317 return 0;
1318}
1319
1320int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1321 struct drm_file *file_priv)
1322{
1323 struct drm_vmw_cursor_bypass_arg *arg = data;
1324 struct vmw_display_unit *du;
1325 struct drm_mode_object *obj;
1326 struct drm_crtc *crtc;
1327 int ret = 0;
1328
1329
1330 mutex_lock(&dev->mode_config.mutex);
1331 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1332
1333 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1334 du = vmw_crtc_to_du(crtc);
1335 du->hotspot_x = arg->xhot;
1336 du->hotspot_y = arg->yhot;
1337 }
1338
1339 mutex_unlock(&dev->mode_config.mutex);
1340 return 0;
1341 }
1342
1343 obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
1344 if (!obj) {
1345 ret = -EINVAL;
1346 goto out;
1347 }
1348
1349 crtc = obj_to_crtc(obj);
1350 du = vmw_crtc_to_du(crtc);
1351
1352 du->hotspot_x = arg->xhot;
1353 du->hotspot_y = arg->yhot;
1354
1355out:
1356 mutex_unlock(&dev->mode_config.mutex);
1357
1358 return ret;
1359}
1360
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001361int vmw_kms_write_svga(struct vmw_private *vmw_priv,
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001362 unsigned width, unsigned height, unsigned pitch,
Michel Dänzer6558429b2011-08-31 07:42:49 +00001363 unsigned bpp, unsigned depth)
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001364{
1365 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1366 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1367 else if (vmw_fifo_have_pitchlock(vmw_priv))
1368 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1369 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1370 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
Michel Dänzer6558429b2011-08-31 07:42:49 +00001371 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001372
1373 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1374 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1375 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1376 return -EINVAL;
1377 }
1378
1379 return 0;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001380}
1381
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001382int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1383{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001384 struct vmw_vga_topology_state *save;
1385 uint32_t i;
1386
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001387 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1388 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001389 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001390 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1391 vmw_priv->vga_pitchlock =
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001392 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001393 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001394 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
1395 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001396
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001397 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1398 return 0;
1399
1400 vmw_priv->num_displays = vmw_read(vmw_priv,
1401 SVGA_REG_NUM_GUEST_DISPLAYS);
1402
Thomas Hellstrom029e50b2010-10-05 12:43:08 +02001403 if (vmw_priv->num_displays == 0)
1404 vmw_priv->num_displays = 1;
1405
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001406 for (i = 0; i < vmw_priv->num_displays; ++i) {
1407 save = &vmw_priv->vga_save[i];
1408 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1409 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1410 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1411 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1412 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1413 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1414 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001415 if (i == 0 && vmw_priv->num_displays == 1 &&
1416 save->width == 0 && save->height == 0) {
1417
1418 /*
1419 * It should be fairly safe to assume that these
1420 * values are uninitialized.
1421 */
1422
1423 save->width = vmw_priv->vga_width - save->pos_x;
1424 save->height = vmw_priv->vga_height - save->pos_y;
1425 }
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001426 }
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001427
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001428 return 0;
1429}
1430
1431int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1432{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001433 struct vmw_vga_topology_state *save;
1434 uint32_t i;
1435
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001436 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1437 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001438 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001439 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1440 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1441 vmw_priv->vga_pitchlock);
1442 else if (vmw_fifo_have_pitchlock(vmw_priv))
1443 iowrite32(vmw_priv->vga_pitchlock,
1444 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001445
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001446 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1447 return 0;
1448
1449 for (i = 0; i < vmw_priv->num_displays; ++i) {
1450 save = &vmw_priv->vga_save[i];
1451 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1452 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1453 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1454 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1455 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1456 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1457 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1458 }
1459
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001460 return 0;
1461}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001462
Thomas Hellstrome133e732010-10-05 12:43:04 +02001463bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1464 uint32_t pitch,
1465 uint32_t height)
1466{
1467 return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
1468}
1469
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001470
1471/**
1472 * Function called by DRM code called with vbl_lock held.
1473 */
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001474u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1475{
1476 return 0;
1477}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001478
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001479/**
1480 * Function called by DRM code called with vbl_lock held.
1481 */
1482int vmw_enable_vblank(struct drm_device *dev, int crtc)
1483{
1484 return -ENOSYS;
1485}
1486
1487/**
1488 * Function called by DRM code called with vbl_lock held.
1489 */
1490void vmw_disable_vblank(struct drm_device *dev, int crtc)
1491{
1492}
1493
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001494
1495/*
1496 * Small shared kms functions.
1497 */
1498
1499int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1500 struct drm_vmw_rect *rects)
1501{
1502 struct drm_device *dev = dev_priv->dev;
1503 struct vmw_display_unit *du;
1504 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001505
1506 mutex_lock(&dev->mode_config.mutex);
1507
1508#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001509 {
1510 unsigned int i;
1511
1512 DRM_INFO("%s: new layout ", __func__);
1513 for (i = 0; i < num; i++)
1514 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1515 rects[i].w, rects[i].h);
1516 DRM_INFO("\n");
1517 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001518#endif
1519
1520 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1521 du = vmw_connector_to_du(con);
1522 if (num > du->unit) {
1523 du->pref_width = rects[du->unit].w;
1524 du->pref_height = rects[du->unit].h;
1525 du->pref_active = true;
1526 } else {
1527 du->pref_width = 800;
1528 du->pref_height = 600;
1529 du->pref_active = false;
1530 }
1531 con->status = vmw_du_connector_detect(con, true);
1532 }
1533
1534 mutex_unlock(&dev->mode_config.mutex);
1535
1536 return 0;
1537}
1538
1539void vmw_du_crtc_save(struct drm_crtc *crtc)
1540{
1541}
1542
1543void vmw_du_crtc_restore(struct drm_crtc *crtc)
1544{
1545}
1546
1547void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1548 u16 *r, u16 *g, u16 *b,
1549 uint32_t start, uint32_t size)
1550{
1551 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1552 int i;
1553
1554 for (i = 0; i < size; i++) {
1555 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1556 r[i], g[i], b[i]);
1557 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1558 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1559 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1560 }
1561}
1562
1563void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1564{
1565}
1566
1567void vmw_du_connector_save(struct drm_connector *connector)
1568{
1569}
1570
1571void vmw_du_connector_restore(struct drm_connector *connector)
1572{
1573}
1574
1575enum drm_connector_status
1576vmw_du_connector_detect(struct drm_connector *connector, bool force)
1577{
1578 uint32_t num_displays;
1579 struct drm_device *dev = connector->dev;
1580 struct vmw_private *dev_priv = vmw_priv(dev);
1581
1582 mutex_lock(&dev_priv->hw_mutex);
1583 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1584 mutex_unlock(&dev_priv->hw_mutex);
1585
1586 return ((vmw_connector_to_du(connector)->unit < num_displays) ?
1587 connector_status_connected : connector_status_disconnected);
1588}
1589
1590static struct drm_display_mode vmw_kms_connector_builtin[] = {
1591 /* 640x480@60Hz */
1592 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1593 752, 800, 0, 480, 489, 492, 525, 0,
1594 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1595 /* 800x600@60Hz */
1596 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1597 968, 1056, 0, 600, 601, 605, 628, 0,
1598 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1599 /* 1024x768@60Hz */
1600 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1601 1184, 1344, 0, 768, 771, 777, 806, 0,
1602 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1603 /* 1152x864@75Hz */
1604 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1605 1344, 1600, 0, 864, 865, 868, 900, 0,
1606 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1607 /* 1280x768@60Hz */
1608 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1609 1472, 1664, 0, 768, 771, 778, 798, 0,
1610 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1611 /* 1280x800@60Hz */
1612 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1613 1480, 1680, 0, 800, 803, 809, 831, 0,
1614 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1615 /* 1280x960@60Hz */
1616 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1617 1488, 1800, 0, 960, 961, 964, 1000, 0,
1618 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1619 /* 1280x1024@60Hz */
1620 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1621 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1622 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1623 /* 1360x768@60Hz */
1624 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1625 1536, 1792, 0, 768, 771, 777, 795, 0,
1626 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1627 /* 1440x1050@60Hz */
1628 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1629 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1630 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1631 /* 1440x900@60Hz */
1632 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1633 1672, 1904, 0, 900, 903, 909, 934, 0,
1634 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1635 /* 1600x1200@60Hz */
1636 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1637 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1638 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1639 /* 1680x1050@60Hz */
1640 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1641 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1642 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1643 /* 1792x1344@60Hz */
1644 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1645 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1646 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1647 /* 1853x1392@60Hz */
1648 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1649 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1650 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1651 /* 1920x1200@60Hz */
1652 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1653 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1654 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1655 /* 1920x1440@60Hz */
1656 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1657 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1658 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1659 /* 2560x1600@60Hz */
1660 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1661 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1662 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1663 /* Terminate */
1664 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1665};
1666
1667int vmw_du_connector_fill_modes(struct drm_connector *connector,
1668 uint32_t max_width, uint32_t max_height)
1669{
1670 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1671 struct drm_device *dev = connector->dev;
1672 struct vmw_private *dev_priv = vmw_priv(dev);
1673 struct drm_display_mode *mode = NULL;
1674 struct drm_display_mode *bmode;
1675 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1676 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1677 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1678 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1679 };
1680 int i;
1681
1682 /* Add preferred mode */
1683 {
1684 mode = drm_mode_duplicate(dev, &prefmode);
1685 if (!mode)
1686 return 0;
1687 mode->hdisplay = du->pref_width;
1688 mode->vdisplay = du->pref_height;
1689 mode->vrefresh = drm_mode_vrefresh(mode);
1690 if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
1691 mode->vdisplay)) {
1692 drm_mode_probed_add(connector, mode);
1693
1694 if (du->pref_mode) {
1695 list_del_init(&du->pref_mode->head);
1696 drm_mode_destroy(dev, du->pref_mode);
1697 }
1698
1699 du->pref_mode = mode;
1700 }
1701 }
1702
1703 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1704 bmode = &vmw_kms_connector_builtin[i];
1705 if (bmode->hdisplay > max_width ||
1706 bmode->vdisplay > max_height)
1707 continue;
1708
1709 if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
1710 bmode->vdisplay))
1711 continue;
1712
1713 mode = drm_mode_duplicate(dev, bmode);
1714 if (!mode)
1715 return 0;
1716 mode->vrefresh = drm_mode_vrefresh(mode);
1717
1718 drm_mode_probed_add(connector, mode);
1719 }
1720
1721 drm_mode_connector_list_update(connector);
1722
1723 return 1;
1724}
1725
1726int vmw_du_connector_set_property(struct drm_connector *connector,
1727 struct drm_property *property,
1728 uint64_t val)
1729{
1730 return 0;
1731}