641908e06eab3b0d6b30def5f689ebc7a2ecfbd4
[pandora-kernel.git] / drivers / gpu / drm / vmwgfx / vmwgfx_ioctl.c
1 /**************************************************************************
2  *
3  * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27
28 #include "vmwgfx_drv.h"
29 #include <drm/vmwgfx_drm.h>
30 #include "vmwgfx_kms.h"
31
32 int vmw_getparam_ioctl(struct drm_device *dev, void *data,
33                        struct drm_file *file_priv)
34 {
35         struct vmw_private *dev_priv = vmw_priv(dev);
36         struct drm_vmw_getparam_arg *param =
37             (struct drm_vmw_getparam_arg *)data;
38
39         switch (param->param) {
40         case DRM_VMW_PARAM_NUM_STREAMS:
41                 param->value = vmw_overlay_num_overlays(dev_priv);
42                 break;
43         case DRM_VMW_PARAM_NUM_FREE_STREAMS:
44                 param->value = vmw_overlay_num_free_overlays(dev_priv);
45                 break;
46         case DRM_VMW_PARAM_3D:
47                 param->value = vmw_fifo_have_3d(dev_priv) ? 1 : 0;
48                 break;
49         case DRM_VMW_PARAM_HW_CAPS:
50                 param->value = dev_priv->capabilities;
51                 break;
52         case DRM_VMW_PARAM_FIFO_CAPS:
53                 param->value = dev_priv->fifo.capabilities;
54                 break;
55         case DRM_VMW_PARAM_MAX_FB_SIZE:
56                 param->value = dev_priv->prim_bb_mem;
57                 break;
58         case DRM_VMW_PARAM_FIFO_HW_VERSION:
59         {
60                 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
61                 const struct vmw_fifo_state *fifo = &dev_priv->fifo;
62
63                 param->value =
64                         ioread32(fifo_mem +
65                                  ((fifo->capabilities &
66                                    SVGA_FIFO_CAP_3D_HWVERSION_REVISED) ?
67                                   SVGA_FIFO_3D_HWVERSION_REVISED :
68                                   SVGA_FIFO_3D_HWVERSION));
69                 break;
70         }
71         case DRM_VMW_PARAM_MAX_SURF_MEMORY:
72                 param->value = dev_priv->memory_size;
73                 break;
74         case DRM_VMW_PARAM_3D_CAPS_SIZE:
75                 if (dev_priv->capabilities & SVGA_CAP_GBOBJECTS)
76                         param->value = SVGA3D_DEVCAP_MAX;
77                 else
78                         param->value = (SVGA_FIFO_3D_CAPS_LAST -
79                                         SVGA_FIFO_3D_CAPS + 1);
80                 param->value *= sizeof(uint32_t);
81                 break;
82         default:
83                 DRM_ERROR("Illegal vmwgfx get param request: %d\n",
84                           param->param);
85                 return -EINVAL;
86         }
87
88         return 0;
89 }
90
91
92 int vmw_get_cap_3d_ioctl(struct drm_device *dev, void *data,
93                          struct drm_file *file_priv)
94 {
95         struct drm_vmw_get_3d_cap_arg *arg =
96                 (struct drm_vmw_get_3d_cap_arg *) data;
97         struct vmw_private *dev_priv = vmw_priv(dev);
98         uint32_t size;
99         __le32 __iomem *fifo_mem;
100         void __user *buffer = (void __user *)((unsigned long)(arg->buffer));
101         void *bounce;
102         int ret;
103         bool gb_objects = !!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS);
104
105         if (unlikely(arg->pad64 != 0)) {
106                 DRM_ERROR("Illegal GET_3D_CAP argument.\n");
107                 return -EINVAL;
108         }
109
110         if (gb_objects)
111                 size = SVGA3D_DEVCAP_MAX;
112         else
113                 size = (SVGA_FIFO_3D_CAPS_LAST - SVGA_FIFO_3D_CAPS + 1);
114
115         size *= sizeof(uint32_t);
116
117         if (arg->max_size < size)
118                 size = arg->max_size;
119
120         bounce = vmalloc(size);
121         if (unlikely(bounce == NULL)) {
122                 DRM_ERROR("Failed to allocate bounce buffer for 3D caps.\n");
123                 return -ENOMEM;
124         }
125
126         if (gb_objects) {
127                 int i;
128                 uint32_t *bounce32 = (uint32_t *) bounce;
129
130                 mutex_lock(&dev_priv->hw_mutex);
131                 for (i = 0; i < SVGA3D_DEVCAP_MAX; ++i) {
132                         vmw_write(dev_priv, SVGA_REG_DEV_CAP, i);
133                         *bounce32++ = vmw_read(dev_priv, SVGA_REG_DEV_CAP);
134                 }
135                 mutex_unlock(&dev_priv->hw_mutex);
136
137         } else {
138
139                 fifo_mem = dev_priv->mmio_virt;
140                 memcpy_fromio(bounce, &fifo_mem[SVGA_FIFO_3D_CAPS], size);
141         }
142
143         ret = copy_to_user(buffer, bounce, size);
144         if (ret)
145                 ret = -EFAULT;
146         vfree(bounce);
147
148         if (unlikely(ret != 0))
149                 DRM_ERROR("Failed to report 3D caps info.\n");
150
151         return ret;
152 }
153
154 int vmw_present_ioctl(struct drm_device *dev, void *data,
155                       struct drm_file *file_priv)
156 {
157         struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
158         struct vmw_private *dev_priv = vmw_priv(dev);
159         struct drm_vmw_present_arg *arg =
160                 (struct drm_vmw_present_arg *)data;
161         struct vmw_surface *surface;
162         struct vmw_master *vmaster = vmw_master(file_priv->master);
163         struct drm_vmw_rect __user *clips_ptr;
164         struct drm_vmw_rect *clips = NULL;
165         struct drm_framebuffer *fb;
166         struct vmw_framebuffer *vfb;
167         struct vmw_resource *res;
168         uint32_t num_clips;
169         int ret;
170
171         num_clips = arg->num_clips;
172         clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
173
174         if (unlikely(num_clips == 0))
175                 return 0;
176
177         if (clips_ptr == NULL) {
178                 DRM_ERROR("Variable clips_ptr must be specified.\n");
179                 ret = -EINVAL;
180                 goto out_clips;
181         }
182
183         clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
184         if (clips == NULL) {
185                 DRM_ERROR("Failed to allocate clip rect list.\n");
186                 ret = -ENOMEM;
187                 goto out_clips;
188         }
189
190         ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
191         if (ret) {
192                 DRM_ERROR("Failed to copy clip rects from userspace.\n");
193                 ret = -EFAULT;
194                 goto out_no_copy;
195         }
196
197         drm_modeset_lock_all(dev);
198
199         fb = drm_framebuffer_lookup(dev, arg->fb_id);
200         if (!fb) {
201                 DRM_ERROR("Invalid framebuffer id.\n");
202                 ret = -ENOENT;
203                 goto out_no_fb;
204         }
205         vfb = vmw_framebuffer_to_vfb(fb);
206
207         ret = ttm_read_lock(&vmaster->lock, true);
208         if (unlikely(ret != 0))
209                 goto out_no_ttm_lock;
210
211         ret = vmw_user_resource_lookup_handle(dev_priv, tfile, arg->sid,
212                                               user_surface_converter,
213                                               &res);
214         if (ret)
215                 goto out_no_surface;
216
217         surface = vmw_res_to_srf(res);
218         ret = vmw_kms_present(dev_priv, file_priv,
219                               vfb, surface, arg->sid,
220                               arg->dest_x, arg->dest_y,
221                               clips, num_clips);
222
223         /* vmw_user_surface_lookup takes one ref so does new_fb */
224         vmw_surface_unreference(&surface);
225
226 out_no_surface:
227         ttm_read_unlock(&vmaster->lock);
228 out_no_ttm_lock:
229         drm_framebuffer_unreference(fb);
230 out_no_fb:
231         drm_modeset_unlock_all(dev);
232 out_no_copy:
233         kfree(clips);
234 out_clips:
235         return ret;
236 }
237
238 int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
239                                struct drm_file *file_priv)
240 {
241         struct vmw_private *dev_priv = vmw_priv(dev);
242         struct drm_vmw_present_readback_arg *arg =
243                 (struct drm_vmw_present_readback_arg *)data;
244         struct drm_vmw_fence_rep __user *user_fence_rep =
245                 (struct drm_vmw_fence_rep __user *)
246                 (unsigned long)arg->fence_rep;
247         struct vmw_master *vmaster = vmw_master(file_priv->master);
248         struct drm_vmw_rect __user *clips_ptr;
249         struct drm_vmw_rect *clips = NULL;
250         struct drm_framebuffer *fb;
251         struct vmw_framebuffer *vfb;
252         uint32_t num_clips;
253         int ret;
254
255         num_clips = arg->num_clips;
256         clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
257
258         if (unlikely(num_clips == 0))
259                 return 0;
260
261         if (clips_ptr == NULL) {
262                 DRM_ERROR("Argument clips_ptr must be specified.\n");
263                 ret = -EINVAL;
264                 goto out_clips;
265         }
266
267         clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
268         if (clips == NULL) {
269                 DRM_ERROR("Failed to allocate clip rect list.\n");
270                 ret = -ENOMEM;
271                 goto out_clips;
272         }
273
274         ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
275         if (ret) {
276                 DRM_ERROR("Failed to copy clip rects from userspace.\n");
277                 ret = -EFAULT;
278                 goto out_no_copy;
279         }
280
281         drm_modeset_lock_all(dev);
282
283         fb = drm_framebuffer_lookup(dev, arg->fb_id);
284         if (!fb) {
285                 DRM_ERROR("Invalid framebuffer id.\n");
286                 ret = -ENOENT;
287                 goto out_no_fb;
288         }
289
290         vfb = vmw_framebuffer_to_vfb(fb);
291         if (!vfb->dmabuf) {
292                 DRM_ERROR("Framebuffer not dmabuf backed.\n");
293                 ret = -EINVAL;
294                 goto out_no_ttm_lock;
295         }
296
297         ret = ttm_read_lock(&vmaster->lock, true);
298         if (unlikely(ret != 0))
299                 goto out_no_ttm_lock;
300
301         ret = vmw_kms_readback(dev_priv, file_priv,
302                                vfb, user_fence_rep,
303                                clips, num_clips);
304
305         ttm_read_unlock(&vmaster->lock);
306 out_no_ttm_lock:
307         drm_framebuffer_unreference(fb);
308 out_no_fb:
309         drm_modeset_unlock_all(dev);
310 out_no_copy:
311         kfree(clips);
312 out_clips:
313         return ret;
314 }
315
316
317 /**
318  * vmw_fops_poll - wrapper around the drm_poll function
319  *
320  * @filp: See the linux fops poll documentation.
321  * @wait: See the linux fops poll documentation.
322  *
323  * Wrapper around the drm_poll function that makes sure the device is
324  * processing the fifo if drm_poll decides to wait.
325  */
326 unsigned int vmw_fops_poll(struct file *filp, struct poll_table_struct *wait)
327 {
328         struct drm_file *file_priv = filp->private_data;
329         struct vmw_private *dev_priv =
330                 vmw_priv(file_priv->minor->dev);
331
332         vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
333         return drm_poll(filp, wait);
334 }
335
336
337 /**
338  * vmw_fops_read - wrapper around the drm_read function
339  *
340  * @filp: See the linux fops read documentation.
341  * @buffer: See the linux fops read documentation.
342  * @count: See the linux fops read documentation.
343  * offset: See the linux fops read documentation.
344  *
345  * Wrapper around the drm_read function that makes sure the device is
346  * processing the fifo if drm_read decides to wait.
347  */
348 ssize_t vmw_fops_read(struct file *filp, char __user *buffer,
349                       size_t count, loff_t *offset)
350 {
351         struct drm_file *file_priv = filp->private_data;
352         struct vmw_private *dev_priv =
353                 vmw_priv(file_priv->minor->dev);
354
355         vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
356         return drm_read(filp, buffer, count, offset);
357 }