blob: d31ae338cfc1d433bd2fecc7107fba06fdc7c648 [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
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +010085int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
86 struct vmw_dma_buffer *dmabuf,
87 u32 width, u32 height,
88 u32 hotspotX, u32 hotspotY)
89{
90 struct ttm_bo_kmap_obj map;
91 unsigned long kmap_offset;
92 unsigned long kmap_num;
93 void *virtual;
94 bool dummy;
95 int ret;
96
97 kmap_offset = 0;
98 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
99
100 ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
101 if (unlikely(ret != 0)) {
102 DRM_ERROR("reserve failed\n");
103 return -EINVAL;
104 }
105
106 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
107 if (unlikely(ret != 0))
108 goto err_unreserve;
109
110 virtual = ttm_kmap_obj_virtual(&map, &dummy);
111 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
112 hotspotX, hotspotY);
113
114 ttm_bo_kunmap(&map);
115err_unreserve:
116 ttm_bo_unreserve(&dmabuf->base);
117
118 return ret;
119}
120
121
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000122void vmw_cursor_update_position(struct vmw_private *dev_priv,
123 bool show, int x, int y)
124{
125 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
126 uint32_t count;
127
128 iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
129 iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
130 iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
131 count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
132 iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
133}
134
135int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
136 uint32_t handle, uint32_t width, uint32_t height)
137{
138 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
139 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
140 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
141 struct vmw_surface *surface = NULL;
142 struct vmw_dma_buffer *dmabuf = NULL;
143 int ret;
144
Jakob Bornecrantzbaa91d642011-11-09 10:25:28 +0100145 /* A lot of the code assumes this */
146 if (handle && (width != 64 || height != 64))
147 return -EINVAL;
148
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000149 if (handle) {
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100150 ret = vmw_user_lookup_handle(dev_priv, tfile,
151 handle, &surface, &dmabuf);
152 if (ret) {
153 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
154 return -EINVAL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000155 }
156 }
157
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100158 /* need to do this before taking down old image */
159 if (surface && !surface->snooper.image) {
160 DRM_ERROR("surface not suitable for cursor\n");
161 vmw_surface_unreference(&surface);
162 return -EINVAL;
163 }
164
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000165 /* takedown old cursor */
166 if (du->cursor_surface) {
167 du->cursor_surface->snooper.crtc = NULL;
168 vmw_surface_unreference(&du->cursor_surface);
169 }
170 if (du->cursor_dmabuf)
171 vmw_dmabuf_unreference(&du->cursor_dmabuf);
172
173 /* setup new image */
174 if (surface) {
175 /* vmw_user_surface_lookup takes one reference */
176 du->cursor_surface = surface;
177
178 du->cursor_surface->snooper.crtc = crtc;
179 du->cursor_age = du->cursor_surface->snooper.age;
180 vmw_cursor_update_image(dev_priv, surface->snooper.image,
181 64, 64, du->hotspot_x, du->hotspot_y);
182 } else if (dmabuf) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000183 /* vmw_user_surface_lookup takes one reference */
184 du->cursor_dmabuf = dmabuf;
185
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100186 ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, width, height,
187 du->hotspot_x, du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000188 } else {
189 vmw_cursor_update_position(dev_priv, false, 0, 0);
190 return 0;
191 }
192
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100193 vmw_cursor_update_position(dev_priv, true,
194 du->cursor_x + du->hotspot_x,
195 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000196
197 return 0;
198}
199
200int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
201{
202 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
203 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
204 bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
205
206 du->cursor_x = x + crtc->x;
207 du->cursor_y = y + crtc->y;
208
209 vmw_cursor_update_position(dev_priv, shown,
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100210 du->cursor_x + du->hotspot_x,
211 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000212
213 return 0;
214}
215
216void vmw_kms_cursor_snoop(struct vmw_surface *srf,
217 struct ttm_object_file *tfile,
218 struct ttm_buffer_object *bo,
219 SVGA3dCmdHeader *header)
220{
221 struct ttm_bo_kmap_obj map;
222 unsigned long kmap_offset;
223 unsigned long kmap_num;
224 SVGA3dCopyBox *box;
225 unsigned box_count;
226 void *virtual;
227 bool dummy;
228 struct vmw_dma_cmd {
229 SVGA3dCmdHeader header;
230 SVGA3dCmdSurfaceDMA dma;
231 } *cmd;
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100232 int i, ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000233
234 cmd = container_of(header, struct vmw_dma_cmd, header);
235
236 /* No snooper installed */
237 if (!srf->snooper.image)
238 return;
239
240 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
241 DRM_ERROR("face and mipmap for cursors should never != 0\n");
242 return;
243 }
244
245 if (cmd->header.size < 64) {
246 DRM_ERROR("at least one full copy box must be given\n");
247 return;
248 }
249
250 box = (SVGA3dCopyBox *)&cmd[1];
251 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
252 sizeof(SVGA3dCopyBox);
253
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100254 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000255 box->x != 0 || box->y != 0 || box->z != 0 ||
256 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100257 box->d != 1 || box_count != 1) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000258 /* TODO handle none page aligned offsets */
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100259 /* TODO handle more dst & src != 0 */
260 /* TODO handle more then one copy */
261 DRM_ERROR("Cant snoop dma request for cursor!\n");
262 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
263 box->srcx, box->srcy, box->srcz,
264 box->x, box->y, box->z,
265 box->w, box->h, box->d, box_count,
266 cmd->dma.guest.ptr.offset);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000267 return;
268 }
269
270 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
271 kmap_num = (64*64*4) >> PAGE_SHIFT;
272
273 ret = ttm_bo_reserve(bo, true, false, false, 0);
274 if (unlikely(ret != 0)) {
275 DRM_ERROR("reserve failed\n");
276 return;
277 }
278
279 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
280 if (unlikely(ret != 0))
281 goto err_unreserve;
282
283 virtual = ttm_kmap_obj_virtual(&map, &dummy);
284
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100285 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
286 memcpy(srf->snooper.image, virtual, 64*64*4);
287 } else {
288 /* Image is unsigned pointer. */
289 for (i = 0; i < box->h; i++)
290 memcpy(srf->snooper.image + i * 64,
291 virtual + i * cmd->dma.guest.pitch,
292 box->w * 4);
293 }
294
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000295 srf->snooper.age++;
296
297 /* we can't call this function from this function since execbuf has
298 * reserved fifo space.
299 *
300 * if (srf->snooper.crtc)
301 * vmw_ldu_crtc_cursor_update_image(dev_priv,
302 * srf->snooper.image, 64, 64,
303 * du->hotspot_x, du->hotspot_y);
304 */
305
306 ttm_bo_kunmap(&map);
307err_unreserve:
308 ttm_bo_unreserve(bo);
309}
310
311void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
312{
313 struct drm_device *dev = dev_priv->dev;
314 struct vmw_display_unit *du;
315 struct drm_crtc *crtc;
316
317 mutex_lock(&dev->mode_config.mutex);
318
319 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
320 du = vmw_crtc_to_du(crtc);
321 if (!du->cursor_surface ||
322 du->cursor_age == du->cursor_surface->snooper.age)
323 continue;
324
325 du->cursor_age = du->cursor_surface->snooper.age;
326 vmw_cursor_update_image(dev_priv,
327 du->cursor_surface->snooper.image,
328 64, 64, du->hotspot_x, du->hotspot_y);
329 }
330
331 mutex_unlock(&dev->mode_config.mutex);
332}
333
334/*
335 * Generic framebuffer code
336 */
337
338int vmw_framebuffer_create_handle(struct drm_framebuffer *fb,
339 struct drm_file *file_priv,
340 unsigned int *handle)
341{
342 if (handle)
343 handle = 0;
344
345 return 0;
346}
347
348/*
349 * Surface framebuffer code
350 */
351
352#define vmw_framebuffer_to_vfbs(x) \
353 container_of(x, struct vmw_framebuffer_surface, base.base)
354
355struct vmw_framebuffer_surface {
356 struct vmw_framebuffer base;
357 struct vmw_surface *surface;
Thomas Hellstrom22ee8612010-05-28 11:22:00 +0200358 struct vmw_dma_buffer *buffer;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200359 struct list_head head;
360 struct drm_master *master;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000361};
362
363void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
364{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200365 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000366 vmw_framebuffer_to_vfbs(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200367 struct vmw_master *vmaster = vmw_master(vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000368
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200369
370 mutex_lock(&vmaster->fb_surf_mutex);
371 list_del(&vfbs->head);
372 mutex_unlock(&vmaster->fb_surf_mutex);
373
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200374 drm_master_put(&vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000375 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200376 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200377 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000378
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200379 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000380}
381
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200382static int do_surface_dirty_sou(struct vmw_private *dev_priv,
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200383 struct drm_file *file_priv,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200384 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200385 unsigned flags, unsigned color,
386 struct drm_clip_rect *clips,
387 unsigned num_clips, int inc)
388{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200389 struct drm_clip_rect *clips_ptr;
390 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
391 struct drm_crtc *crtc;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200392 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200393 int i, num_units;
394 int ret = 0; /* silence warning */
395 int left, right, top, bottom;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200396
397 struct {
398 SVGA3dCmdHeader header;
399 SVGA3dCmdBlitSurfaceToScreen body;
400 } *cmd;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200401 SVGASignedRect *blits;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200402
403
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200404 num_units = 0;
405 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
406 head) {
407 if (crtc->fb != &framebuffer->base)
408 continue;
409 units[num_units++] = vmw_crtc_to_du(crtc);
410 }
411
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200412 BUG_ON(!clips || !num_clips);
413
414 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200415 cmd = kzalloc(fifo_size, GFP_KERNEL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200416 if (unlikely(cmd == NULL)) {
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200417 DRM_ERROR("Temporary fifo memory alloc failed.\n");
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200418 return -ENOMEM;
419 }
420
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200421 left = clips->x1;
422 right = clips->x2;
423 top = clips->y1;
424 bottom = clips->y2;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200425
Jakob Bornecrantzf0c8a652011-11-09 10:25:27 +0100426 /* skip the first clip rect */
427 for (i = 1, clips_ptr = clips + inc;
428 i < num_clips; i++, clips_ptr += inc) {
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200429 left = min_t(int, left, (int)clips_ptr->x1);
430 right = max_t(int, right, (int)clips_ptr->x2);
431 top = min_t(int, top, (int)clips_ptr->y1);
432 bottom = max_t(int, bottom, (int)clips_ptr->y2);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200433 }
434
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200435 /* only need to do this once */
436 memset(cmd, 0, fifo_size);
437 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
438 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
439
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200440 cmd->body.srcRect.left = left;
441 cmd->body.srcRect.right = right;
442 cmd->body.srcRect.top = top;
443 cmd->body.srcRect.bottom = bottom;
444
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200445 clips_ptr = clips;
446 blits = (SVGASignedRect *)&cmd[1];
447 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
448 blits[i].left = clips_ptr->x1 - left;
449 blits[i].right = clips_ptr->x2 - left;
450 blits[i].top = clips_ptr->y1 - top;
451 blits[i].bottom = clips_ptr->y2 - top;
452 }
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200453
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200454 /* do per unit writing, reuse fifo for each */
455 for (i = 0; i < num_units; i++) {
456 struct vmw_display_unit *unit = units[i];
457 int clip_x1 = left - unit->crtc.x;
458 int clip_y1 = top - unit->crtc.y;
459 int clip_x2 = right - unit->crtc.x;
460 int clip_y2 = bottom - unit->crtc.y;
461
462 /* skip any crtcs that misses the clip region */
463 if (clip_x1 >= unit->crtc.mode.hdisplay ||
464 clip_y1 >= unit->crtc.mode.vdisplay ||
465 clip_x2 <= 0 || clip_y2 <= 0)
466 continue;
467
468 /* need to reset sid as it is changed by execbuf */
469 cmd->body.srcImage.sid = cpu_to_le32(framebuffer->user_handle);
470
471 cmd->body.destScreenId = unit->unit;
472
473 /*
474 * The blit command is a lot more resilient then the
475 * readback command when it comes to clip rects. So its
476 * okay to go out of bounds.
477 */
478
479 cmd->body.destRect.left = clip_x1;
480 cmd->body.destRect.right = clip_x2;
481 cmd->body.destRect.top = clip_y1;
482 cmd->body.destRect.bottom = clip_y2;
483
484
485 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
486 fifo_size, 0, NULL);
487
488 if (unlikely(ret != 0))
489 break;
490 }
491
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200492 kfree(cmd);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200493
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200494 return ret;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200495}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000496
497int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200498 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000499 unsigned flags, unsigned color,
500 struct drm_clip_rect *clips,
501 unsigned num_clips)
502{
503 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200504 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000505 struct vmw_framebuffer_surface *vfbs =
506 vmw_framebuffer_to_vfbs(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000507 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200508 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000509
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200510 if (unlikely(vfbs->master != file_priv->master))
511 return -EINVAL;
512
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200513 /* Require ScreenObject support for 3D */
514 if (!dev_priv->sou_priv)
515 return -EINVAL;
516
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200517 ret = ttm_read_lock(&vmaster->lock, true);
518 if (unlikely(ret != 0))
519 return ret;
520
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000521 if (!num_clips) {
522 num_clips = 1;
523 clips = &norect;
524 norect.x1 = norect.y1 = 0;
525 norect.x2 = framebuffer->width;
526 norect.y2 = framebuffer->height;
527 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
528 num_clips /= 2;
529 inc = 2; /* skip source rects */
530 }
531
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200532 ret = do_surface_dirty_sou(dev_priv, file_priv, &vfbs->base,
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200533 flags, color,
534 clips, num_clips, inc);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000535
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200536 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000537 return 0;
538}
539
540static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
541 .destroy = vmw_framebuffer_surface_destroy,
542 .dirty = vmw_framebuffer_surface_dirty,
543 .create_handle = vmw_framebuffer_create_handle,
544};
545
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200546static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200547 struct drm_file *file_priv,
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200548 struct vmw_surface *surface,
549 struct vmw_framebuffer **out,
550 const struct drm_mode_fb_cmd
551 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000552
553{
554 struct drm_device *dev = dev_priv->dev;
555 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200556 enum SVGA3dSurfaceFormat format;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200557 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000558 int ret;
559
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200560 /* 3D is only supported on HWv8 hosts which supports screen objects */
561 if (!dev_priv->sou_priv)
562 return -ENOSYS;
563
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200564 /*
565 * Sanity checks.
566 */
567
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100568 /* Surface must be marked as a scanout. */
569 if (unlikely(!surface->scanout))
570 return -EINVAL;
571
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200572 if (unlikely(surface->mip_levels[0] != 1 ||
573 surface->num_sizes != 1 ||
574 surface->sizes[0].width < mode_cmd->width ||
575 surface->sizes[0].height < mode_cmd->height ||
576 surface->sizes[0].depth != 1)) {
577 DRM_ERROR("Incompatible surface dimensions "
578 "for requested mode.\n");
579 return -EINVAL;
580 }
581
582 switch (mode_cmd->depth) {
583 case 32:
584 format = SVGA3D_A8R8G8B8;
585 break;
586 case 24:
587 format = SVGA3D_X8R8G8B8;
588 break;
589 case 16:
590 format = SVGA3D_R5G6B5;
591 break;
592 case 15:
593 format = SVGA3D_A1R5G5B5;
594 break;
Michel Dänzerf01b7ba2011-08-31 07:42:47 +0000595 case 8:
596 format = SVGA3D_LUMINANCE8;
597 break;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200598 default:
599 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
600 return -EINVAL;
601 }
602
603 if (unlikely(format != surface->format)) {
604 DRM_ERROR("Invalid surface format for requested mode.\n");
605 return -EINVAL;
606 }
607
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000608 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
609 if (!vfbs) {
610 ret = -ENOMEM;
611 goto out_err1;
612 }
613
614 ret = drm_framebuffer_init(dev, &vfbs->base.base,
615 &vmw_framebuffer_surface_funcs);
616 if (ret)
617 goto out_err2;
618
619 if (!vmw_surface_reference(surface)) {
620 DRM_ERROR("failed to reference surface %p\n", surface);
621 goto out_err3;
622 }
623
624 /* XXX get the first 3 from the surface info */
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200625 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
626 vfbs->base.base.pitch = mode_cmd->pitch;
627 vfbs->base.base.depth = mode_cmd->depth;
628 vfbs->base.base.width = mode_cmd->width;
629 vfbs->base.base.height = mode_cmd->height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000630 vfbs->surface = surface;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200631 vfbs->base.user_handle = mode_cmd->handle;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200632 vfbs->master = drm_master_get(file_priv->master);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200633
634 mutex_lock(&vmaster->fb_surf_mutex);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200635 list_add_tail(&vfbs->head, &vmaster->fb_surf);
636 mutex_unlock(&vmaster->fb_surf_mutex);
637
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000638 *out = &vfbs->base;
639
640 return 0;
641
642out_err3:
643 drm_framebuffer_cleanup(&vfbs->base.base);
644out_err2:
645 kfree(vfbs);
646out_err1:
647 return ret;
648}
649
650/*
651 * Dmabuf framebuffer code
652 */
653
654#define vmw_framebuffer_to_vfbd(x) \
655 container_of(x, struct vmw_framebuffer_dmabuf, base.base)
656
657struct vmw_framebuffer_dmabuf {
658 struct vmw_framebuffer base;
659 struct vmw_dma_buffer *buffer;
660};
661
662void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
663{
664 struct vmw_framebuffer_dmabuf *vfbd =
665 vmw_framebuffer_to_vfbd(framebuffer);
666
667 drm_framebuffer_cleanup(framebuffer);
668 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200669 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000670
671 kfree(vfbd);
672}
673
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200674static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
675 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200676 unsigned flags, unsigned color,
677 struct drm_clip_rect *clips,
678 unsigned num_clips, int increment)
679{
680 size_t fifo_size;
681 int i;
682
683 struct {
684 uint32_t header;
685 SVGAFifoCmdUpdate body;
686 } *cmd;
687
688 fifo_size = sizeof(*cmd) * num_clips;
689 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
690 if (unlikely(cmd == NULL)) {
691 DRM_ERROR("Fifo reserve failed.\n");
692 return -ENOMEM;
693 }
694
695 memset(cmd, 0, fifo_size);
696 for (i = 0; i < num_clips; i++, clips += increment) {
697 cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
698 cmd[i].body.x = cpu_to_le32(clips->x1);
699 cmd[i].body.y = cpu_to_le32(clips->y1);
700 cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
701 cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
702 }
703
704 vmw_fifo_commit(dev_priv, fifo_size);
705 return 0;
706}
707
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200708static int do_dmabuf_define_gmrfb(struct drm_file *file_priv,
709 struct vmw_private *dev_priv,
710 struct vmw_framebuffer *framebuffer)
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200711{
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200712 int depth = framebuffer->base.depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200713 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200714 int ret;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200715
716 struct {
717 uint32_t header;
718 SVGAFifoCmdDefineGMRFB body;
719 } *cmd;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200720
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200721 /* Emulate RGBA support, contrary to svga_reg.h this is not
722 * supported by hosts. This is only a problem if we are reading
723 * this value later and expecting what we uploaded back.
724 */
725 if (depth == 32)
726 depth = 24;
727
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200728 fifo_size = sizeof(*cmd);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200729 cmd = kmalloc(fifo_size, GFP_KERNEL);
730 if (unlikely(cmd == NULL)) {
731 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
732 return -ENOMEM;
733 }
734
735 memset(cmd, 0, fifo_size);
736 cmd->header = SVGA_CMD_DEFINE_GMRFB;
737 cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200738 cmd->body.format.colorDepth = depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200739 cmd->body.format.reserved = 0;
740 cmd->body.bytesPerLine = framebuffer->base.pitch;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200741 cmd->body.ptr.gmrId = framebuffer->user_handle;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200742 cmd->body.ptr.offset = 0;
743
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200744 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
745 fifo_size, 0, NULL);
746
747 kfree(cmd);
748
749 return ret;
750}
751
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200752static int do_dmabuf_dirty_sou(struct drm_file *file_priv,
753 struct vmw_private *dev_priv,
754 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200755 unsigned flags, unsigned color,
756 struct drm_clip_rect *clips,
757 unsigned num_clips, int increment)
758{
759 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
760 struct drm_clip_rect *clips_ptr;
761 int i, k, num_units, ret;
762 struct drm_crtc *crtc;
763 size_t fifo_size;
764
765 struct {
766 uint32_t header;
767 SVGAFifoCmdBlitGMRFBToScreen body;
768 } *blits;
769
770 ret = do_dmabuf_define_gmrfb(file_priv, dev_priv, framebuffer);
771 if (unlikely(ret != 0))
772 return ret; /* define_gmrfb prints warnings */
773
774 fifo_size = sizeof(*blits) * num_clips;
775 blits = kmalloc(fifo_size, GFP_KERNEL);
776 if (unlikely(blits == NULL)) {
777 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
778 return -ENOMEM;
779 }
780
781 num_units = 0;
782 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
783 if (crtc->fb != &framebuffer->base)
784 continue;
785 units[num_units++] = vmw_crtc_to_du(crtc);
786 }
787
788 for (k = 0; k < num_units; k++) {
789 struct vmw_display_unit *unit = units[k];
790 int hit_num = 0;
791
792 clips_ptr = clips;
793 for (i = 0; i < num_clips; i++, clips_ptr += increment) {
794 int clip_x1 = clips_ptr->x1 - unit->crtc.x;
795 int clip_y1 = clips_ptr->y1 - unit->crtc.y;
796 int clip_x2 = clips_ptr->x2 - unit->crtc.x;
797 int clip_y2 = clips_ptr->y2 - unit->crtc.y;
798
799 /* skip any crtcs that misses the clip region */
800 if (clip_x1 >= unit->crtc.mode.hdisplay ||
801 clip_y1 >= unit->crtc.mode.vdisplay ||
802 clip_x2 <= 0 || clip_y2 <= 0)
803 continue;
804
805 blits[hit_num].header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
806 blits[hit_num].body.destScreenId = unit->unit;
807 blits[hit_num].body.srcOrigin.x = clips_ptr->x1;
808 blits[hit_num].body.srcOrigin.y = clips_ptr->y1;
809 blits[hit_num].body.destRect.left = clip_x1;
810 blits[hit_num].body.destRect.top = clip_y1;
811 blits[hit_num].body.destRect.right = clip_x2;
812 blits[hit_num].body.destRect.bottom = clip_y2;
813 hit_num++;
814 }
815
816 /* no clips hit the crtc */
817 if (hit_num == 0)
818 continue;
819
820 fifo_size = sizeof(*blits) * hit_num;
821 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, blits,
822 fifo_size, 0, NULL);
823
824 if (unlikely(ret != 0))
825 break;
826 }
827
828 kfree(blits);
829
830 return ret;
831}
832
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000833int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200834 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000835 unsigned flags, unsigned color,
836 struct drm_clip_rect *clips,
837 unsigned num_clips)
838{
839 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200840 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200841 struct vmw_framebuffer_dmabuf *vfbd =
842 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000843 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200844 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000845
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200846 ret = ttm_read_lock(&vmaster->lock, true);
847 if (unlikely(ret != 0))
848 return ret;
849
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100850 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000851 num_clips = 1;
852 clips = &norect;
853 norect.x1 = norect.y1 = 0;
854 norect.x2 = framebuffer->width;
855 norect.y2 = framebuffer->height;
856 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
857 num_clips /= 2;
858 increment = 2;
859 }
860
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200861 if (dev_priv->ldu_priv) {
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200862 ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200863 flags, color,
864 clips, num_clips, increment);
865 } else {
866 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, &vfbd->base,
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200867 flags, color,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200868 clips, num_clips, increment);
869 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000870
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200871 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200872 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000873}
874
875static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
876 .destroy = vmw_framebuffer_dmabuf_destroy,
877 .dirty = vmw_framebuffer_dmabuf_dirty,
878 .create_handle = vmw_framebuffer_create_handle,
879};
880
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200881/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200882 * Pin the dmabuffer to the start of vram.
883 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000884static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
885{
886 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
887 struct vmw_framebuffer_dmabuf *vfbd =
888 vmw_framebuffer_to_vfbd(&vfb->base);
889 int ret;
890
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200891 /* This code should not be used with screen objects */
892 BUG_ON(dev_priv->sou_priv);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200893
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000894 vmw_overlay_pause_all(dev_priv);
895
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200896 ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000897
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000898 vmw_overlay_resume_all(dev_priv);
899
Jakob Bornecrantz316ab132010-05-28 11:22:05 +0200900 WARN_ON(ret != 0);
901
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000902 return 0;
903}
904
905static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
906{
907 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
908 struct vmw_framebuffer_dmabuf *vfbd =
909 vmw_framebuffer_to_vfbd(&vfb->base);
910
911 if (!vfbd->buffer) {
912 WARN_ON(!vfbd->buffer);
913 return 0;
914 }
915
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200916 return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000917}
918
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200919static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
920 struct vmw_dma_buffer *dmabuf,
921 struct vmw_framebuffer **out,
922 const struct drm_mode_fb_cmd
923 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000924
925{
926 struct drm_device *dev = dev_priv->dev;
927 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200928 unsigned int requested_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000929 int ret;
930
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200931 requested_size = mode_cmd->height * mode_cmd->pitch;
932 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
933 DRM_ERROR("Screen buffer object size is too small "
934 "for requested mode.\n");
935 return -EINVAL;
936 }
937
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200938 /* Limited framebuffer color depth support for screen objects */
939 if (dev_priv->sou_priv) {
940 switch (mode_cmd->depth) {
941 case 32:
942 case 24:
943 /* Only support 32 bpp for 32 and 24 depth fbs */
944 if (mode_cmd->bpp == 32)
945 break;
946
947 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
948 mode_cmd->depth, mode_cmd->bpp);
949 return -EINVAL;
950 case 16:
951 case 15:
952 /* Only support 16 bpp for 16 and 15 depth fbs */
953 if (mode_cmd->bpp == 16)
954 break;
955
956 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
957 mode_cmd->depth, mode_cmd->bpp);
958 return -EINVAL;
959 default:
960 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
961 return -EINVAL;
962 }
963 }
964
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000965 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
966 if (!vfbd) {
967 ret = -ENOMEM;
968 goto out_err1;
969 }
970
971 ret = drm_framebuffer_init(dev, &vfbd->base.base,
972 &vmw_framebuffer_dmabuf_funcs);
973 if (ret)
974 goto out_err2;
975
976 if (!vmw_dmabuf_reference(dmabuf)) {
977 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
978 goto out_err3;
979 }
980
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200981 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
982 vfbd->base.base.pitch = mode_cmd->pitch;
983 vfbd->base.base.depth = mode_cmd->depth;
984 vfbd->base.base.width = mode_cmd->width;
985 vfbd->base.base.height = mode_cmd->height;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200986 if (!dev_priv->sou_priv) {
987 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
988 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
989 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200990 vfbd->base.dmabuf = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000991 vfbd->buffer = dmabuf;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200992 vfbd->base.user_handle = mode_cmd->handle;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000993 *out = &vfbd->base;
994
995 return 0;
996
997out_err3:
998 drm_framebuffer_cleanup(&vfbd->base.base);
999out_err2:
1000 kfree(vfbd);
1001out_err1:
1002 return ret;
1003}
1004
1005/*
1006 * Generic Kernel modesetting functions
1007 */
1008
1009static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1010 struct drm_file *file_priv,
1011 struct drm_mode_fb_cmd *mode_cmd)
1012{
1013 struct vmw_private *dev_priv = vmw_priv(dev);
1014 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1015 struct vmw_framebuffer *vfb = NULL;
1016 struct vmw_surface *surface = NULL;
1017 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001018 struct ttm_base_object *user_obj;
Thomas Hellstrome133e732010-10-05 12:43:04 +02001019 u64 required_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001020 int ret;
1021
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001022 /**
1023 * This code should be conditioned on Screen Objects not being used.
1024 * If screen objects are used, we can allocate a GMR to hold the
1025 * requested framebuffer.
1026 */
1027
1028 required_size = mode_cmd->pitch * mode_cmd->height;
Thomas Hellstrome133e732010-10-05 12:43:04 +02001029 if (unlikely(required_size > (u64) dev_priv->vram_size)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001030 DRM_ERROR("VRAM size is too small for requested mode.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +01001031 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001032 }
1033
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001034 /*
1035 * Take a reference on the user object of the resource
1036 * backing the kms fb. This ensures that user-space handle
1037 * lookups on that resource will always work as long as
1038 * it's registered with a kms framebuffer. This is important,
1039 * since vmw_execbuf_process identifies resources in the
1040 * command stream using user-space handles.
1041 */
1042
1043 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handle);
1044 if (unlikely(user_obj == NULL)) {
1045 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1046 return ERR_PTR(-ENOENT);
1047 }
1048
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001049 /* returns either a dmabuf or surface */
1050 ret = vmw_user_lookup_handle(dev_priv, tfile,
1051 mode_cmd->handle,
1052 &surface, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001053 if (ret)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001054 goto err_out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001055
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001056 /* Create the new framebuffer depending one what we got back */
1057 if (bo)
1058 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
1059 mode_cmd);
1060 else if (surface)
1061 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv,
1062 surface, &vfb, mode_cmd);
1063 else
1064 BUG();
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001065
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001066err_out:
1067 /* vmw_user_lookup_handle takes one ref so does new_fb */
1068 if (bo)
1069 vmw_dmabuf_unreference(&bo);
1070 if (surface)
1071 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001072
1073 if (ret) {
1074 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001075 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001076 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001077 } else
1078 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001079
1080 return &vfb->base;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001081}
1082
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001083static struct drm_mode_config_funcs vmw_kms_funcs = {
1084 .fb_create = vmw_kms_fb_create,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001085};
1086
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001087int vmw_kms_present(struct vmw_private *dev_priv,
1088 struct drm_file *file_priv,
1089 struct vmw_framebuffer *vfb,
1090 struct vmw_surface *surface,
1091 uint32_t sid,
1092 int32_t destX, int32_t destY,
1093 struct drm_vmw_rect *clips,
1094 uint32_t num_clips)
1095{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001096 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1097 struct drm_crtc *crtc;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001098 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001099 int i, k, num_units;
1100 int ret = 0; /* silence warning */
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001101 int left, right, top, bottom;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001102
1103 struct {
1104 SVGA3dCmdHeader header;
1105 SVGA3dCmdBlitSurfaceToScreen body;
1106 } *cmd;
1107 SVGASignedRect *blits;
1108
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001109 num_units = 0;
1110 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1111 if (crtc->fb != &vfb->base)
1112 continue;
1113 units[num_units++] = vmw_crtc_to_du(crtc);
1114 }
1115
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001116 BUG_ON(surface == NULL);
1117 BUG_ON(!clips || !num_clips);
1118
1119 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
1120 cmd = kmalloc(fifo_size, GFP_KERNEL);
1121 if (unlikely(cmd == NULL)) {
1122 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1123 return -ENOMEM;
1124 }
1125
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001126 left = clips->x;
1127 right = clips->x + clips->w;
1128 top = clips->y;
1129 bottom = clips->y + clips->h;
1130
1131 for (i = 1; i < num_clips; i++) {
1132 left = min_t(int, left, (int)clips[i].x);
1133 right = max_t(int, right, (int)clips[i].x + clips[i].w);
1134 top = min_t(int, top, (int)clips[i].y);
1135 bottom = max_t(int, bottom, (int)clips[i].y + clips[i].h);
1136 }
1137
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001138 /* only need to do this once */
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001139 memset(cmd, 0, fifo_size);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001140 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
1141 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
1142
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001143 cmd->body.srcRect.left = left;
1144 cmd->body.srcRect.right = right;
1145 cmd->body.srcRect.top = top;
1146 cmd->body.srcRect.bottom = bottom;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001147
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001148 blits = (SVGASignedRect *)&cmd[1];
1149 for (i = 0; i < num_clips; i++) {
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001150 blits[i].left = clips[i].x - left;
1151 blits[i].right = clips[i].x + clips[i].w - left;
1152 blits[i].top = clips[i].y - top;
1153 blits[i].bottom = clips[i].y + clips[i].h - top;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001154 }
1155
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001156 for (k = 0; k < num_units; k++) {
1157 struct vmw_display_unit *unit = units[k];
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001158 int clip_x1 = left + destX - unit->crtc.x;
1159 int clip_y1 = top + destY - unit->crtc.y;
1160 int clip_x2 = right + destX - unit->crtc.x;
1161 int clip_y2 = bottom + destY - unit->crtc.y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001162
1163 /* skip any crtcs that misses the clip region */
1164 if (clip_x1 >= unit->crtc.mode.hdisplay ||
1165 clip_y1 >= unit->crtc.mode.vdisplay ||
1166 clip_x2 <= 0 || clip_y2 <= 0)
1167 continue;
1168
1169 /* need to reset sid as it is changed by execbuf */
1170 cmd->body.srcImage.sid = sid;
1171
1172 cmd->body.destScreenId = unit->unit;
1173
1174 /*
1175 * The blit command is a lot more resilient then the
1176 * readback command when it comes to clip rects. So its
1177 * okay to go out of bounds.
1178 */
1179
1180 cmd->body.destRect.left = clip_x1;
1181 cmd->body.destRect.right = clip_x2;
1182 cmd->body.destRect.top = clip_y1;
1183 cmd->body.destRect.bottom = clip_y2;
1184
1185 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
1186 fifo_size, 0, NULL);
1187
1188 if (unlikely(ret != 0))
1189 break;
1190 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001191
1192 kfree(cmd);
1193
1194 return ret;
1195}
1196
1197int vmw_kms_readback(struct vmw_private *dev_priv,
1198 struct drm_file *file_priv,
1199 struct vmw_framebuffer *vfb,
1200 struct drm_vmw_fence_rep __user *user_fence_rep,
1201 struct drm_vmw_rect *clips,
1202 uint32_t num_clips)
1203{
1204 struct vmw_framebuffer_dmabuf *vfbd =
1205 vmw_framebuffer_to_vfbd(&vfb->base);
1206 struct vmw_dma_buffer *dmabuf = vfbd->buffer;
1207 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1208 struct drm_crtc *crtc;
1209 size_t fifo_size;
1210 int i, k, ret, num_units, blits_pos;
1211
1212 struct {
1213 uint32_t header;
1214 SVGAFifoCmdDefineGMRFB body;
1215 } *cmd;
1216 struct {
1217 uint32_t header;
1218 SVGAFifoCmdBlitScreenToGMRFB body;
1219 } *blits;
1220
1221 num_units = 0;
1222 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1223 if (crtc->fb != &vfb->base)
1224 continue;
1225 units[num_units++] = vmw_crtc_to_du(crtc);
1226 }
1227
1228 BUG_ON(dmabuf == NULL);
1229 BUG_ON(!clips || !num_clips);
1230
1231 /* take a safe guess at fifo size */
1232 fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
1233 cmd = kmalloc(fifo_size, GFP_KERNEL);
1234 if (unlikely(cmd == NULL)) {
1235 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1236 return -ENOMEM;
1237 }
1238
1239 memset(cmd, 0, fifo_size);
1240 cmd->header = SVGA_CMD_DEFINE_GMRFB;
1241 cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
1242 cmd->body.format.colorDepth = vfb->base.depth;
1243 cmd->body.format.reserved = 0;
1244 cmd->body.bytesPerLine = vfb->base.pitch;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001245 cmd->body.ptr.gmrId = vfb->user_handle;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001246 cmd->body.ptr.offset = 0;
1247
1248 blits = (void *)&cmd[1];
1249 blits_pos = 0;
1250 for (i = 0; i < num_units; i++) {
1251 struct drm_vmw_rect *c = clips;
1252 for (k = 0; k < num_clips; k++, c++) {
1253 /* transform clip coords to crtc origin based coords */
1254 int clip_x1 = c->x - units[i]->crtc.x;
1255 int clip_x2 = c->x - units[i]->crtc.x + c->w;
1256 int clip_y1 = c->y - units[i]->crtc.y;
1257 int clip_y2 = c->y - units[i]->crtc.y + c->h;
1258 int dest_x = c->x;
1259 int dest_y = c->y;
1260
1261 /* compensate for clipping, we negate
1262 * a negative number and add that.
1263 */
1264 if (clip_x1 < 0)
1265 dest_x += -clip_x1;
1266 if (clip_y1 < 0)
1267 dest_y += -clip_y1;
1268
1269 /* clip */
1270 clip_x1 = max(clip_x1, 0);
1271 clip_y1 = max(clip_y1, 0);
1272 clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
1273 clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
1274
1275 /* and cull any rects that misses the crtc */
1276 if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
1277 clip_y1 >= units[i]->crtc.mode.vdisplay ||
1278 clip_x2 <= 0 || clip_y2 <= 0)
1279 continue;
1280
1281 blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1282 blits[blits_pos].body.srcScreenId = units[i]->unit;
1283 blits[blits_pos].body.destOrigin.x = dest_x;
1284 blits[blits_pos].body.destOrigin.y = dest_y;
1285
1286 blits[blits_pos].body.srcRect.left = clip_x1;
1287 blits[blits_pos].body.srcRect.top = clip_y1;
1288 blits[blits_pos].body.srcRect.right = clip_x2;
1289 blits[blits_pos].body.srcRect.bottom = clip_y2;
1290 blits_pos++;
1291 }
1292 }
1293 /* reset size here and use calculated exact size from loops */
1294 fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
1295
1296 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
1297 0, user_fence_rep);
1298
1299 kfree(cmd);
1300
1301 return ret;
1302}
1303
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001304int vmw_kms_init(struct vmw_private *dev_priv)
1305{
1306 struct drm_device *dev = dev_priv->dev;
1307 int ret;
1308
1309 drm_mode_config_init(dev);
1310 dev->mode_config.funcs = &vmw_kms_funcs;
Jakob Bornecrantz3bef3572010-02-09 19:41:57 +00001311 dev->mode_config.min_width = 1;
1312 dev->mode_config.min_height = 1;
Jakob Bornecrantz7e71f8a2010-05-28 11:21:54 +02001313 /* assumed largest fb size */
1314 dev->mode_config.max_width = 8192;
1315 dev->mode_config.max_height = 8192;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001316
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001317 ret = vmw_kms_init_screen_object_display(dev_priv);
1318 if (ret) /* Fallback */
1319 (void)vmw_kms_init_legacy_display_system(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001320
1321 return 0;
1322}
1323
1324int vmw_kms_close(struct vmw_private *dev_priv)
1325{
1326 /*
1327 * Docs says we should take the lock before calling this function
1328 * but since it destroys encoders and our destructor calls
1329 * drm_encoder_cleanup which takes the lock we deadlock.
1330 */
1331 drm_mode_config_cleanup(dev_priv->dev);
Jakob Bornecrantzc0d18312011-11-09 10:25:26 +01001332 if (dev_priv->sou_priv)
1333 vmw_kms_close_screen_object_display(dev_priv);
1334 else
1335 vmw_kms_close_legacy_display_system(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001336 return 0;
1337}
1338
1339int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1340 struct drm_file *file_priv)
1341{
1342 struct drm_vmw_cursor_bypass_arg *arg = data;
1343 struct vmw_display_unit *du;
1344 struct drm_mode_object *obj;
1345 struct drm_crtc *crtc;
1346 int ret = 0;
1347
1348
1349 mutex_lock(&dev->mode_config.mutex);
1350 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1351
1352 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1353 du = vmw_crtc_to_du(crtc);
1354 du->hotspot_x = arg->xhot;
1355 du->hotspot_y = arg->yhot;
1356 }
1357
1358 mutex_unlock(&dev->mode_config.mutex);
1359 return 0;
1360 }
1361
1362 obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
1363 if (!obj) {
1364 ret = -EINVAL;
1365 goto out;
1366 }
1367
1368 crtc = obj_to_crtc(obj);
1369 du = vmw_crtc_to_du(crtc);
1370
1371 du->hotspot_x = arg->xhot;
1372 du->hotspot_y = arg->yhot;
1373
1374out:
1375 mutex_unlock(&dev->mode_config.mutex);
1376
1377 return ret;
1378}
1379
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001380int vmw_kms_write_svga(struct vmw_private *vmw_priv,
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001381 unsigned width, unsigned height, unsigned pitch,
Michel Dänzer6558429b2011-08-31 07:42:49 +00001382 unsigned bpp, unsigned depth)
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001383{
1384 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1385 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1386 else if (vmw_fifo_have_pitchlock(vmw_priv))
1387 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1388 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1389 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
Michel Dänzer6558429b2011-08-31 07:42:49 +00001390 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001391
1392 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1393 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1394 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1395 return -EINVAL;
1396 }
1397
1398 return 0;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001399}
1400
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001401int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1402{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001403 struct vmw_vga_topology_state *save;
1404 uint32_t i;
1405
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001406 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1407 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001408 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001409 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1410 vmw_priv->vga_pitchlock =
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001411 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001412 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001413 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
1414 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001415
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001416 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1417 return 0;
1418
1419 vmw_priv->num_displays = vmw_read(vmw_priv,
1420 SVGA_REG_NUM_GUEST_DISPLAYS);
1421
Thomas Hellstrom029e50b2010-10-05 12:43:08 +02001422 if (vmw_priv->num_displays == 0)
1423 vmw_priv->num_displays = 1;
1424
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001425 for (i = 0; i < vmw_priv->num_displays; ++i) {
1426 save = &vmw_priv->vga_save[i];
1427 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1428 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1429 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1430 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1431 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1432 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1433 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001434 if (i == 0 && vmw_priv->num_displays == 1 &&
1435 save->width == 0 && save->height == 0) {
1436
1437 /*
1438 * It should be fairly safe to assume that these
1439 * values are uninitialized.
1440 */
1441
1442 save->width = vmw_priv->vga_width - save->pos_x;
1443 save->height = vmw_priv->vga_height - save->pos_y;
1444 }
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001445 }
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001446
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001447 return 0;
1448}
1449
1450int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1451{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001452 struct vmw_vga_topology_state *save;
1453 uint32_t i;
1454
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001455 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1456 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001457 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001458 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1459 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1460 vmw_priv->vga_pitchlock);
1461 else if (vmw_fifo_have_pitchlock(vmw_priv))
1462 iowrite32(vmw_priv->vga_pitchlock,
1463 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001464
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001465 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1466 return 0;
1467
1468 for (i = 0; i < vmw_priv->num_displays; ++i) {
1469 save = &vmw_priv->vga_save[i];
1470 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1471 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1472 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1473 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1474 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1475 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1476 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1477 }
1478
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001479 return 0;
1480}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001481
Thomas Hellstrome133e732010-10-05 12:43:04 +02001482bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1483 uint32_t pitch,
1484 uint32_t height)
1485{
1486 return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
1487}
1488
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001489
1490/**
1491 * Function called by DRM code called with vbl_lock held.
1492 */
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001493u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1494{
1495 return 0;
1496}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001497
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001498/**
1499 * Function called by DRM code called with vbl_lock held.
1500 */
1501int vmw_enable_vblank(struct drm_device *dev, int crtc)
1502{
1503 return -ENOSYS;
1504}
1505
1506/**
1507 * Function called by DRM code called with vbl_lock held.
1508 */
1509void vmw_disable_vblank(struct drm_device *dev, int crtc)
1510{
1511}
1512
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001513
1514/*
1515 * Small shared kms functions.
1516 */
1517
1518int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1519 struct drm_vmw_rect *rects)
1520{
1521 struct drm_device *dev = dev_priv->dev;
1522 struct vmw_display_unit *du;
1523 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001524
1525 mutex_lock(&dev->mode_config.mutex);
1526
1527#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001528 {
1529 unsigned int i;
1530
1531 DRM_INFO("%s: new layout ", __func__);
1532 for (i = 0; i < num; i++)
1533 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1534 rects[i].w, rects[i].h);
1535 DRM_INFO("\n");
1536 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001537#endif
1538
1539 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1540 du = vmw_connector_to_du(con);
1541 if (num > du->unit) {
1542 du->pref_width = rects[du->unit].w;
1543 du->pref_height = rects[du->unit].h;
1544 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001545 du->gui_x = rects[du->unit].x;
1546 du->gui_y = rects[du->unit].y;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001547 } else {
1548 du->pref_width = 800;
1549 du->pref_height = 600;
1550 du->pref_active = false;
1551 }
1552 con->status = vmw_du_connector_detect(con, true);
1553 }
1554
1555 mutex_unlock(&dev->mode_config.mutex);
1556
1557 return 0;
1558}
1559
1560void vmw_du_crtc_save(struct drm_crtc *crtc)
1561{
1562}
1563
1564void vmw_du_crtc_restore(struct drm_crtc *crtc)
1565{
1566}
1567
1568void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1569 u16 *r, u16 *g, u16 *b,
1570 uint32_t start, uint32_t size)
1571{
1572 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1573 int i;
1574
1575 for (i = 0; i < size; i++) {
1576 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1577 r[i], g[i], b[i]);
1578 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1579 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1580 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1581 }
1582}
1583
1584void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1585{
1586}
1587
1588void vmw_du_connector_save(struct drm_connector *connector)
1589{
1590}
1591
1592void vmw_du_connector_restore(struct drm_connector *connector)
1593{
1594}
1595
1596enum drm_connector_status
1597vmw_du_connector_detect(struct drm_connector *connector, bool force)
1598{
1599 uint32_t num_displays;
1600 struct drm_device *dev = connector->dev;
1601 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001602 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001603
1604 mutex_lock(&dev_priv->hw_mutex);
1605 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1606 mutex_unlock(&dev_priv->hw_mutex);
1607
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001608 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1609 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001610 connector_status_connected : connector_status_disconnected);
1611}
1612
1613static struct drm_display_mode vmw_kms_connector_builtin[] = {
1614 /* 640x480@60Hz */
1615 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1616 752, 800, 0, 480, 489, 492, 525, 0,
1617 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1618 /* 800x600@60Hz */
1619 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1620 968, 1056, 0, 600, 601, 605, 628, 0,
1621 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1622 /* 1024x768@60Hz */
1623 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1624 1184, 1344, 0, 768, 771, 777, 806, 0,
1625 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1626 /* 1152x864@75Hz */
1627 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1628 1344, 1600, 0, 864, 865, 868, 900, 0,
1629 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1630 /* 1280x768@60Hz */
1631 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1632 1472, 1664, 0, 768, 771, 778, 798, 0,
1633 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1634 /* 1280x800@60Hz */
1635 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1636 1480, 1680, 0, 800, 803, 809, 831, 0,
1637 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1638 /* 1280x960@60Hz */
1639 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1640 1488, 1800, 0, 960, 961, 964, 1000, 0,
1641 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1642 /* 1280x1024@60Hz */
1643 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1644 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1645 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1646 /* 1360x768@60Hz */
1647 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1648 1536, 1792, 0, 768, 771, 777, 795, 0,
1649 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1650 /* 1440x1050@60Hz */
1651 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1652 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1653 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1654 /* 1440x900@60Hz */
1655 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1656 1672, 1904, 0, 900, 903, 909, 934, 0,
1657 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1658 /* 1600x1200@60Hz */
1659 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1660 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1661 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1662 /* 1680x1050@60Hz */
1663 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1664 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1665 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1666 /* 1792x1344@60Hz */
1667 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1668 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1669 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1670 /* 1853x1392@60Hz */
1671 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1672 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1673 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1674 /* 1920x1200@60Hz */
1675 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1676 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1677 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1678 /* 1920x1440@60Hz */
1679 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1680 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1681 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1682 /* 2560x1600@60Hz */
1683 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1684 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1685 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1686 /* Terminate */
1687 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1688};
1689
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001690/**
1691 * vmw_guess_mode_timing - Provide fake timings for a
1692 * 60Hz vrefresh mode.
1693 *
1694 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1695 * members filled in.
1696 */
1697static void vmw_guess_mode_timing(struct drm_display_mode *mode)
1698{
1699 mode->hsync_start = mode->hdisplay + 50;
1700 mode->hsync_end = mode->hsync_start + 50;
1701 mode->htotal = mode->hsync_end + 50;
1702
1703 mode->vsync_start = mode->vdisplay + 50;
1704 mode->vsync_end = mode->vsync_start + 50;
1705 mode->vtotal = mode->vsync_end + 50;
1706
1707 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1708 mode->vrefresh = drm_mode_vrefresh(mode);
1709}
1710
1711
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001712int vmw_du_connector_fill_modes(struct drm_connector *connector,
1713 uint32_t max_width, uint32_t max_height)
1714{
1715 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1716 struct drm_device *dev = connector->dev;
1717 struct vmw_private *dev_priv = vmw_priv(dev);
1718 struct drm_display_mode *mode = NULL;
1719 struct drm_display_mode *bmode;
1720 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1721 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1722 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1723 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1724 };
1725 int i;
1726
1727 /* Add preferred mode */
1728 {
1729 mode = drm_mode_duplicate(dev, &prefmode);
1730 if (!mode)
1731 return 0;
1732 mode->hdisplay = du->pref_width;
1733 mode->vdisplay = du->pref_height;
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001734 vmw_guess_mode_timing(mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001735
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001736 if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
1737 mode->vdisplay)) {
1738 drm_mode_probed_add(connector, mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001739 } else {
1740 drm_mode_destroy(dev, mode);
1741 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001742 }
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001743
1744 if (du->pref_mode) {
1745 list_del_init(&du->pref_mode->head);
1746 drm_mode_destroy(dev, du->pref_mode);
1747 }
1748
1749 /* mode might be null here, this is intended */
1750 du->pref_mode = mode;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001751 }
1752
1753 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1754 bmode = &vmw_kms_connector_builtin[i];
1755 if (bmode->hdisplay > max_width ||
1756 bmode->vdisplay > max_height)
1757 continue;
1758
1759 if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
1760 bmode->vdisplay))
1761 continue;
1762
1763 mode = drm_mode_duplicate(dev, bmode);
1764 if (!mode)
1765 return 0;
1766 mode->vrefresh = drm_mode_vrefresh(mode);
1767
1768 drm_mode_probed_add(connector, mode);
1769 }
1770
Jakob Bornecrantzd41025c2011-11-03 21:03:07 +01001771 /* Move the prefered mode first, help apps pick the right mode. */
1772 if (du->pref_mode)
1773 list_move(&du->pref_mode->head, &connector->probed_modes);
1774
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001775 drm_mode_connector_list_update(connector);
1776
1777 return 1;
1778}
1779
1780int vmw_du_connector_set_property(struct drm_connector *connector,
1781 struct drm_property *property,
1782 uint64_t val)
1783{
1784 return 0;
1785}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001786
1787
1788int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1789 struct drm_file *file_priv)
1790{
1791 struct vmw_private *dev_priv = vmw_priv(dev);
1792 struct drm_vmw_update_layout_arg *arg =
1793 (struct drm_vmw_update_layout_arg *)data;
1794 struct vmw_master *vmaster = vmw_master(file_priv->master);
1795 void __user *user_rects;
1796 struct drm_vmw_rect *rects;
1797 unsigned rects_size;
1798 int ret;
1799 int i;
1800 struct drm_mode_config *mode_config = &dev->mode_config;
1801
1802 ret = ttm_read_lock(&vmaster->lock, true);
1803 if (unlikely(ret != 0))
1804 return ret;
1805
1806 if (!arg->num_outputs) {
1807 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1808 vmw_du_update_layout(dev_priv, 1, &def_rect);
1809 goto out_unlock;
1810 }
1811
1812 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01001813 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
1814 GFP_KERNEL);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001815 if (unlikely(!rects)) {
1816 ret = -ENOMEM;
1817 goto out_unlock;
1818 }
1819
1820 user_rects = (void __user *)(unsigned long)arg->rects;
1821 ret = copy_from_user(rects, user_rects, rects_size);
1822 if (unlikely(ret != 0)) {
1823 DRM_ERROR("Failed to get rects.\n");
1824 ret = -EFAULT;
1825 goto out_free;
1826 }
1827
1828 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01001829 if (rects[i].x < 0 ||
1830 rects[i].y < 0 ||
1831 rects[i].x + rects[i].w > mode_config->max_width ||
1832 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001833 DRM_ERROR("Invalid GUI layout.\n");
1834 ret = -EINVAL;
1835 goto out_free;
1836 }
1837 }
1838
1839 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
1840
1841out_free:
1842 kfree(rects);
1843out_unlock:
1844 ttm_read_unlock(&vmaster->lock);
1845 return ret;
1846}