blob: 116c49736763ee81a4b4d664bfba2ab0a5e3d5cd [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_drv.h"
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/vmwgfx_drm.h>
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +020030#include "vmwgfx_kms.h"
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000031
32int 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:
Jakob Bornecrantz8e19a952010-01-30 03:38:06 +000047 param->value = vmw_fifo_have_3d(dev_priv) ? 1 : 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000048 break;
Thomas Hellstromf77cef32010-02-09 19:41:55 +000049 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;
Thomas Hellstrom30f47fc82010-10-05 12:43:06 +020055 case DRM_VMW_PARAM_MAX_FB_SIZE:
Thomas Hellstrombc2d6502012-11-21 10:32:36 +010056 param->value = dev_priv->prim_bb_mem;
Thomas Hellstrom30f47fc82010-10-05 12:43:06 +020057 break;
Thomas Hellstromf63f6a52011-09-01 20:18:41 +000058 case DRM_VMW_PARAM_FIFO_HW_VERSION:
59 {
60 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
Thomas Hellstromebd4c6f2011-11-28 13:19:08 +010061 const struct vmw_fifo_state *fifo = &dev_priv->fifo;
Thomas Hellstromf63f6a52011-09-01 20:18:41 +000062
Thomas Hellstromebd4c6f2011-11-28 13:19:08 +010063 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));
Thomas Hellstromf63f6a52011-09-01 20:18:41 +000069 break;
70 }
Thomas Hellstrom716a2fd2012-11-21 10:37:20 +010071 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;
Thomas Hellstrom311474d2012-11-21 12:34:47 +010082 case DRM_VMW_PARAM_MAX_MOB_MEMORY:
83 param->value = dev_priv->max_mob_pages * PAGE_SIZE;
84 break;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000085 default:
86 DRM_ERROR("Illegal vmwgfx get param request: %d\n",
87 param->param);
88 return -EINVAL;
89 }
90
91 return 0;
92}
Thomas Hellstromf63f6a52011-09-01 20:18:41 +000093
94
95int vmw_get_cap_3d_ioctl(struct drm_device *dev, void *data,
96 struct drm_file *file_priv)
97{
98 struct drm_vmw_get_3d_cap_arg *arg =
99 (struct drm_vmw_get_3d_cap_arg *) data;
100 struct vmw_private *dev_priv = vmw_priv(dev);
101 uint32_t size;
102 __le32 __iomem *fifo_mem;
103 void __user *buffer = (void __user *)((unsigned long)(arg->buffer));
104 void *bounce;
105 int ret;
Thomas Hellstrom716a2fd2012-11-21 10:37:20 +0100106 bool gb_objects = !!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS);
Thomas Hellstromf63f6a52011-09-01 20:18:41 +0000107
108 if (unlikely(arg->pad64 != 0)) {
109 DRM_ERROR("Illegal GET_3D_CAP argument.\n");
110 return -EINVAL;
111 }
112
Thomas Hellstrom716a2fd2012-11-21 10:37:20 +0100113 if (gb_objects)
114 size = SVGA3D_DEVCAP_MAX;
115 else
116 size = (SVGA_FIFO_3D_CAPS_LAST - SVGA_FIFO_3D_CAPS + 1);
117
118 size *= sizeof(uint32_t);
Thomas Hellstromf63f6a52011-09-01 20:18:41 +0000119
120 if (arg->max_size < size)
121 size = arg->max_size;
122
123 bounce = vmalloc(size);
124 if (unlikely(bounce == NULL)) {
125 DRM_ERROR("Failed to allocate bounce buffer for 3D caps.\n");
126 return -ENOMEM;
127 }
128
Thomas Hellstrom716a2fd2012-11-21 10:37:20 +0100129 if (gb_objects) {
130 int i;
131 uint32_t *bounce32 = (uint32_t *) bounce;
132
133 mutex_lock(&dev_priv->hw_mutex);
134 for (i = 0; i < SVGA3D_DEVCAP_MAX; ++i) {
135 vmw_write(dev_priv, SVGA_REG_DEV_CAP, i);
136 *bounce32++ = vmw_read(dev_priv, SVGA_REG_DEV_CAP);
137 }
138 mutex_unlock(&dev_priv->hw_mutex);
139
140 } else {
141
142 fifo_mem = dev_priv->mmio_virt;
143 memcpy_fromio(bounce, &fifo_mem[SVGA_FIFO_3D_CAPS], size);
144 }
Thomas Hellstromf63f6a52011-09-01 20:18:41 +0000145
146 ret = copy_to_user(buffer, bounce, size);
Dan Carpenter888155b2012-11-12 11:07:24 +0000147 if (ret)
148 ret = -EFAULT;
Thomas Hellstromf63f6a52011-09-01 20:18:41 +0000149 vfree(bounce);
150
151 if (unlikely(ret != 0))
152 DRM_ERROR("Failed to report 3D caps info.\n");
153
154 return ret;
155}
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200156
157int vmw_present_ioctl(struct drm_device *dev, void *data,
158 struct drm_file *file_priv)
159{
160 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
161 struct vmw_private *dev_priv = vmw_priv(dev);
162 struct drm_vmw_present_arg *arg =
163 (struct drm_vmw_present_arg *)data;
164 struct vmw_surface *surface;
165 struct vmw_master *vmaster = vmw_master(file_priv->master);
166 struct drm_vmw_rect __user *clips_ptr;
167 struct drm_vmw_rect *clips = NULL;
Daniel Vetter786b99e2012-12-02 21:53:40 +0100168 struct drm_framebuffer *fb;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200169 struct vmw_framebuffer *vfb;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000170 struct vmw_resource *res;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200171 uint32_t num_clips;
172 int ret;
173
174 num_clips = arg->num_clips;
175 clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
176
177 if (unlikely(num_clips == 0))
178 return 0;
179
180 if (clips_ptr == NULL) {
181 DRM_ERROR("Variable clips_ptr must be specified.\n");
182 ret = -EINVAL;
183 goto out_clips;
184 }
185
Thomas Meyer24bb5a02011-11-29 22:08:00 +0100186 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200187 if (clips == NULL) {
188 DRM_ERROR("Failed to allocate clip rect list.\n");
189 ret = -ENOMEM;
190 goto out_clips;
191 }
192
193 ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
194 if (ret) {
195 DRM_ERROR("Failed to copy clip rects from userspace.\n");
Dan Carpenterd2c184f2011-10-18 09:09:19 +0300196 ret = -EFAULT;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200197 goto out_no_copy;
198 }
199
Daniel Vetterbbe4b992012-12-02 01:48:38 +0100200 drm_modeset_lock_all(dev);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200201
Daniel Vetter786b99e2012-12-02 21:53:40 +0100202 fb = drm_framebuffer_lookup(dev, arg->fb_id);
203 if (!fb) {
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200204 DRM_ERROR("Invalid framebuffer id.\n");
Ville Syrjälä43789b92013-10-17 13:35:06 +0300205 ret = -ENOENT;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200206 goto out_no_fb;
207 }
Daniel Vetter786b99e2012-12-02 21:53:40 +0100208 vfb = vmw_framebuffer_to_vfb(fb);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200209
210 ret = ttm_read_lock(&vmaster->lock, true);
211 if (unlikely(ret != 0))
212 goto out_no_ttm_lock;
213
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000214 ret = vmw_user_resource_lookup_handle(dev_priv, tfile, arg->sid,
215 user_surface_converter,
216 &res);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200217 if (ret)
218 goto out_no_surface;
219
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000220 surface = vmw_res_to_srf(res);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200221 ret = vmw_kms_present(dev_priv, file_priv,
222 vfb, surface, arg->sid,
223 arg->dest_x, arg->dest_y,
224 clips, num_clips);
225
226 /* vmw_user_surface_lookup takes one ref so does new_fb */
227 vmw_surface_unreference(&surface);
228
229out_no_surface:
230 ttm_read_unlock(&vmaster->lock);
231out_no_ttm_lock:
Daniel Vetter2fd5eab2012-12-11 16:28:34 +0100232 drm_framebuffer_unreference(fb);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200233out_no_fb:
Daniel Vetterbbe4b992012-12-02 01:48:38 +0100234 drm_modeset_unlock_all(dev);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200235out_no_copy:
236 kfree(clips);
237out_clips:
238 return ret;
239}
240
241int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
242 struct drm_file *file_priv)
243{
244 struct vmw_private *dev_priv = vmw_priv(dev);
245 struct drm_vmw_present_readback_arg *arg =
246 (struct drm_vmw_present_readback_arg *)data;
247 struct drm_vmw_fence_rep __user *user_fence_rep =
248 (struct drm_vmw_fence_rep __user *)
249 (unsigned long)arg->fence_rep;
250 struct vmw_master *vmaster = vmw_master(file_priv->master);
251 struct drm_vmw_rect __user *clips_ptr;
252 struct drm_vmw_rect *clips = NULL;
Daniel Vetter786b99e2012-12-02 21:53:40 +0100253 struct drm_framebuffer *fb;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200254 struct vmw_framebuffer *vfb;
255 uint32_t num_clips;
256 int ret;
257
258 num_clips = arg->num_clips;
259 clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
260
261 if (unlikely(num_clips == 0))
262 return 0;
263
264 if (clips_ptr == NULL) {
265 DRM_ERROR("Argument clips_ptr must be specified.\n");
266 ret = -EINVAL;
267 goto out_clips;
268 }
269
Thomas Meyer24bb5a02011-11-29 22:08:00 +0100270 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200271 if (clips == NULL) {
272 DRM_ERROR("Failed to allocate clip rect list.\n");
273 ret = -ENOMEM;
274 goto out_clips;
275 }
276
277 ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
278 if (ret) {
279 DRM_ERROR("Failed to copy clip rects from userspace.\n");
Dan Carpenterd2c184f2011-10-18 09:09:19 +0300280 ret = -EFAULT;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200281 goto out_no_copy;
282 }
283
Daniel Vetterbbe4b992012-12-02 01:48:38 +0100284 drm_modeset_lock_all(dev);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200285
Daniel Vetter786b99e2012-12-02 21:53:40 +0100286 fb = drm_framebuffer_lookup(dev, arg->fb_id);
287 if (!fb) {
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200288 DRM_ERROR("Invalid framebuffer id.\n");
Ville Syrjälä43789b92013-10-17 13:35:06 +0300289 ret = -ENOENT;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200290 goto out_no_fb;
291 }
292
Daniel Vetter786b99e2012-12-02 21:53:40 +0100293 vfb = vmw_framebuffer_to_vfb(fb);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200294 if (!vfb->dmabuf) {
295 DRM_ERROR("Framebuffer not dmabuf backed.\n");
296 ret = -EINVAL;
Daniel Vetter2fd5eab2012-12-11 16:28:34 +0100297 goto out_no_ttm_lock;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200298 }
299
300 ret = ttm_read_lock(&vmaster->lock, true);
301 if (unlikely(ret != 0))
302 goto out_no_ttm_lock;
303
304 ret = vmw_kms_readback(dev_priv, file_priv,
305 vfb, user_fence_rep,
306 clips, num_clips);
307
308 ttm_read_unlock(&vmaster->lock);
309out_no_ttm_lock:
Daniel Vetter2fd5eab2012-12-11 16:28:34 +0100310 drm_framebuffer_unreference(fb);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200311out_no_fb:
Daniel Vetterbbe4b992012-12-02 01:48:38 +0100312 drm_modeset_unlock_all(dev);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200313out_no_copy:
314 kfree(clips);
315out_clips:
316 return ret;
317}
Thomas Hellstrom5438ae82011-10-10 12:23:27 +0200318
319
320/**
321 * vmw_fops_poll - wrapper around the drm_poll function
322 *
323 * @filp: See the linux fops poll documentation.
324 * @wait: See the linux fops poll documentation.
325 *
326 * Wrapper around the drm_poll function that makes sure the device is
327 * processing the fifo if drm_poll decides to wait.
328 */
329unsigned int vmw_fops_poll(struct file *filp, struct poll_table_struct *wait)
330{
331 struct drm_file *file_priv = filp->private_data;
332 struct vmw_private *dev_priv =
333 vmw_priv(file_priv->minor->dev);
334
335 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
336 return drm_poll(filp, wait);
337}
338
339
340/**
341 * vmw_fops_read - wrapper around the drm_read function
342 *
343 * @filp: See the linux fops read documentation.
344 * @buffer: See the linux fops read documentation.
345 * @count: See the linux fops read documentation.
346 * offset: See the linux fops read documentation.
347 *
348 * Wrapper around the drm_read function that makes sure the device is
349 * processing the fifo if drm_read decides to wait.
350 */
351ssize_t vmw_fops_read(struct file *filp, char __user *buffer,
352 size_t count, loff_t *offset)
353{
354 struct drm_file *file_priv = filp->private_data;
355 struct vmw_private *dev_priv =
356 vmw_priv(file_priv->minor->dev);
357
358 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
359 return drm_read(filp, buffer, count, offset);
360}