GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / gpu / drm / qxl / qxl_ioctl.c
1 /*
2  * Copyright 2013 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Dave Airlie
23  *          Alon Levy
24  */
25
26 #include "qxl_drv.h"
27 #include "qxl_object.h"
28
29 /*
30  * TODO: allocating a new gem(in qxl_bo) for each request.
31  * This is wasteful since bo's are page aligned.
32  */
33 static int qxl_alloc_ioctl(struct drm_device *dev, void *data,
34                            struct drm_file *file_priv)
35 {
36         struct qxl_device *qdev = dev->dev_private;
37         struct drm_qxl_alloc *qxl_alloc = data;
38         int ret;
39         struct qxl_bo *qobj;
40         uint32_t handle;
41         u32 domain = QXL_GEM_DOMAIN_VRAM;
42
43         if (qxl_alloc->size == 0) {
44                 DRM_ERROR("invalid size %d\n", qxl_alloc->size);
45                 return -EINVAL;
46         }
47         ret = qxl_gem_object_create_with_handle(qdev, file_priv,
48                                                 domain,
49                                                 qxl_alloc->size,
50                                                 NULL,
51                                                 &qobj, &handle);
52         if (ret) {
53                 DRM_ERROR("%s: failed to create gem ret=%d\n",
54                           __func__, ret);
55                 return -ENOMEM;
56         }
57         qxl_alloc->handle = handle;
58         return 0;
59 }
60
61 static int qxl_map_ioctl(struct drm_device *dev, void *data,
62                          struct drm_file *file_priv)
63 {
64         struct qxl_device *qdev = dev->dev_private;
65         struct drm_qxl_map *qxl_map = data;
66
67         return qxl_mode_dumb_mmap(file_priv, &qdev->ddev, qxl_map->handle,
68                                   &qxl_map->offset);
69 }
70
71 struct qxl_reloc_info {
72         int type;
73         struct qxl_bo *dst_bo;
74         uint32_t dst_offset;
75         struct qxl_bo *src_bo;
76         int src_offset;
77 };
78
79 /*
80  * dst must be validated, i.e. whole bo on vram/surfacesram (right now all bo's
81  * are on vram).
82  * *(dst + dst_off) = qxl_bo_physical_address(src, src_off)
83  */
84 static void
85 apply_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info)
86 {
87         void *reloc_page;
88         reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK);
89         *(uint64_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = qxl_bo_physical_address(qdev,
90                                                                                               info->src_bo,
91                                                                                               info->src_offset);
92         qxl_bo_kunmap_atomic_page(qdev, info->dst_bo, reloc_page);
93 }
94
95 static void
96 apply_surf_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info)
97 {
98         uint32_t id = 0;
99         void *reloc_page;
100
101         if (info->src_bo && !info->src_bo->is_primary)
102                 id = info->src_bo->surface_id;
103
104         reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK);
105         *(uint32_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = id;
106         qxl_bo_kunmap_atomic_page(qdev, info->dst_bo, reloc_page);
107 }
108
109 /* return holding the reference to this object */
110 static int qxlhw_handle_to_bo(struct drm_file *file_priv, uint64_t handle,
111                               struct qxl_release *release, struct qxl_bo **qbo_p)
112 {
113         struct drm_gem_object *gobj;
114         struct qxl_bo *qobj;
115         int ret;
116
117         gobj = drm_gem_object_lookup(file_priv, handle);
118         if (!gobj)
119                 return -EINVAL;
120
121         qobj = gem_to_qxl_bo(gobj);
122
123         ret = qxl_release_list_add(release, qobj);
124         drm_gem_object_unreference_unlocked(gobj);
125         if (ret)
126                 return ret;
127
128         *qbo_p = qobj;
129         return 0;
130 }
131
132 /*
133  * Usage of execbuffer:
134  * Relocations need to take into account the full QXLDrawable size.
135  * However, the command as passed from user space must *not* contain the initial
136  * QXLReleaseInfo struct (first XXX bytes)
137  */
138 static int qxl_process_single_command(struct qxl_device *qdev,
139                                       struct drm_qxl_command *cmd,
140                                       struct drm_file *file_priv)
141 {
142         struct qxl_reloc_info *reloc_info;
143         int release_type;
144         struct qxl_release *release;
145         struct qxl_bo *cmd_bo;
146         void *fb_cmd;
147         int i, ret, num_relocs;
148         int unwritten;
149
150         switch (cmd->type) {
151         case QXL_CMD_DRAW:
152                 release_type = QXL_RELEASE_DRAWABLE;
153                 break;
154         case QXL_CMD_SURFACE:
155         case QXL_CMD_CURSOR:
156         default:
157                 DRM_DEBUG("Only draw commands in execbuffers\n");
158                 return -EINVAL;
159                 break;
160         }
161
162         if (cmd->command_size > PAGE_SIZE - sizeof(union qxl_release_info))
163                 return -EINVAL;
164
165         if (!access_ok(VERIFY_READ,
166                        u64_to_user_ptr(cmd->command),
167                        cmd->command_size))
168                 return -EFAULT;
169
170         reloc_info = kmalloc_array(cmd->relocs_num,
171                                    sizeof(struct qxl_reloc_info), GFP_KERNEL);
172         if (!reloc_info)
173                 return -ENOMEM;
174
175         ret = qxl_alloc_release_reserved(qdev,
176                                          sizeof(union qxl_release_info) +
177                                          cmd->command_size,
178                                          release_type,
179                                          &release,
180                                          &cmd_bo);
181         if (ret)
182                 goto out_free_reloc;
183
184         /* TODO copy slow path code from i915 */
185         fb_cmd = qxl_bo_kmap_atomic_page(qdev, cmd_bo, (release->release_offset & PAGE_SIZE));
186         unwritten = __copy_from_user_inatomic_nocache
187                 (fb_cmd + sizeof(union qxl_release_info) + (release->release_offset & ~PAGE_SIZE),
188                  u64_to_user_ptr(cmd->command), cmd->command_size);
189
190         {
191                 struct qxl_drawable *draw = fb_cmd;
192                 draw->mm_time = qdev->rom->mm_clock;
193         }
194
195         qxl_bo_kunmap_atomic_page(qdev, cmd_bo, fb_cmd);
196         if (unwritten) {
197                 DRM_ERROR("got unwritten %d\n", unwritten);
198                 ret = -EFAULT;
199                 goto out_free_release;
200         }
201
202         /* fill out reloc info structs */
203         num_relocs = 0;
204         for (i = 0; i < cmd->relocs_num; ++i) {
205                 struct drm_qxl_reloc reloc;
206                 struct drm_qxl_reloc __user *u = u64_to_user_ptr(cmd->relocs);
207
208                 if (copy_from_user(&reloc, u + i, sizeof(reloc))) {
209                         ret = -EFAULT;
210                         goto out_free_bos;
211                 }
212
213                 /* add the bos to the list of bos to validate -
214                    need to validate first then process relocs? */
215                 if (reloc.reloc_type != QXL_RELOC_TYPE_BO && reloc.reloc_type != QXL_RELOC_TYPE_SURF) {
216                         DRM_DEBUG("unknown reloc type %d\n", reloc.reloc_type);
217
218                         ret = -EINVAL;
219                         goto out_free_bos;
220                 }
221                 reloc_info[i].type = reloc.reloc_type;
222
223                 if (reloc.dst_handle) {
224                         ret = qxlhw_handle_to_bo(file_priv, reloc.dst_handle, release,
225                                                  &reloc_info[i].dst_bo);
226                         if (ret)
227                                 goto out_free_bos;
228                         reloc_info[i].dst_offset = reloc.dst_offset;
229                 } else {
230                         reloc_info[i].dst_bo = cmd_bo;
231                         reloc_info[i].dst_offset = reloc.dst_offset + release->release_offset;
232                 }
233                 num_relocs++;
234
235                 /* reserve and validate the reloc dst bo */
236                 if (reloc.reloc_type == QXL_RELOC_TYPE_BO || reloc.src_handle) {
237                         ret = qxlhw_handle_to_bo(file_priv, reloc.src_handle, release,
238                                                  &reloc_info[i].src_bo);
239                         if (ret)
240                                 goto out_free_bos;
241                         reloc_info[i].src_offset = reloc.src_offset;
242                 } else {
243                         reloc_info[i].src_bo = NULL;
244                         reloc_info[i].src_offset = 0;
245                 }
246         }
247
248         /* validate all buffers */
249         ret = qxl_release_reserve_list(release, false);
250         if (ret)
251                 goto out_free_bos;
252
253         for (i = 0; i < cmd->relocs_num; ++i) {
254                 if (reloc_info[i].type == QXL_RELOC_TYPE_BO)
255                         apply_reloc(qdev, &reloc_info[i]);
256                 else if (reloc_info[i].type == QXL_RELOC_TYPE_SURF)
257                         apply_surf_reloc(qdev, &reloc_info[i]);
258         }
259
260         qxl_release_fence_buffer_objects(release);
261         ret = qxl_push_command_ring_release(qdev, release, cmd->type, true);
262
263 out_free_bos:
264 out_free_release:
265         if (ret)
266                 qxl_release_free(qdev, release);
267 out_free_reloc:
268         kfree(reloc_info);
269         return ret;
270 }
271
272 static int qxl_execbuffer_ioctl(struct drm_device *dev, void *data,
273                                 struct drm_file *file_priv)
274 {
275         struct qxl_device *qdev = dev->dev_private;
276         struct drm_qxl_execbuffer *execbuffer = data;
277         struct drm_qxl_command user_cmd;
278         int cmd_num;
279         int ret;
280
281         for (cmd_num = 0; cmd_num < execbuffer->commands_num; ++cmd_num) {
282
283                 struct drm_qxl_command __user *commands =
284                         u64_to_user_ptr(execbuffer->commands);
285
286                 if (copy_from_user(&user_cmd, commands + cmd_num,
287                                        sizeof(user_cmd)))
288                         return -EFAULT;
289
290                 ret = qxl_process_single_command(qdev, &user_cmd, file_priv);
291                 if (ret)
292                         return ret;
293         }
294         return 0;
295 }
296
297 static int qxl_update_area_ioctl(struct drm_device *dev, void *data,
298                                  struct drm_file *file)
299 {
300         struct qxl_device *qdev = dev->dev_private;
301         struct drm_qxl_update_area *update_area = data;
302         struct qxl_rect area = {.left = update_area->left,
303                                 .top = update_area->top,
304                                 .right = update_area->right,
305                                 .bottom = update_area->bottom};
306         int ret;
307         struct drm_gem_object *gobj = NULL;
308         struct qxl_bo *qobj = NULL;
309
310         if (update_area->left >= update_area->right ||
311             update_area->top >= update_area->bottom)
312                 return -EINVAL;
313
314         gobj = drm_gem_object_lookup(file, update_area->handle);
315         if (gobj == NULL)
316                 return -ENOENT;
317
318         qobj = gem_to_qxl_bo(gobj);
319
320         ret = qxl_bo_reserve(qobj, false);
321         if (ret)
322                 goto out;
323
324         if (!qobj->pin_count) {
325                 qxl_ttm_placement_from_domain(qobj, qobj->type, false);
326                 ret = ttm_bo_validate(&qobj->tbo, &qobj->placement,
327                                       true, false);
328                 if (unlikely(ret))
329                         goto out;
330         }
331
332         ret = qxl_bo_check_id(qdev, qobj);
333         if (ret)
334                 goto out2;
335         if (!qobj->surface_id)
336                 DRM_ERROR("got update area for surface with no id %d\n", update_area->handle);
337         ret = qxl_io_update_area(qdev, qobj, &area);
338
339 out2:
340         qxl_bo_unreserve(qobj);
341
342 out:
343         drm_gem_object_unreference_unlocked(gobj);
344         return ret;
345 }
346
347 static int qxl_getparam_ioctl(struct drm_device *dev, void *data,
348                        struct drm_file *file_priv)
349 {
350         struct qxl_device *qdev = dev->dev_private;
351         struct drm_qxl_getparam *param = data;
352
353         switch (param->param) {
354         case QXL_PARAM_NUM_SURFACES:
355                 param->value = qdev->rom->n_surfaces;
356                 break;
357         case QXL_PARAM_MAX_RELOCS:
358                 param->value = QXL_MAX_RES;
359                 break;
360         default:
361                 return -EINVAL;
362         }
363         return 0;
364 }
365
366 static int qxl_clientcap_ioctl(struct drm_device *dev, void *data,
367                                   struct drm_file *file_priv)
368 {
369         struct qxl_device *qdev = dev->dev_private;
370         struct drm_qxl_clientcap *param = data;
371         int byte, idx;
372
373         byte = param->index / 8;
374         idx = param->index % 8;
375
376         if (dev->pdev->revision < 4)
377                 return -ENOSYS;
378
379         if (byte >= 58)
380                 return -ENOSYS;
381
382         if (qdev->rom->client_capabilities[byte] & (1 << idx))
383                 return 0;
384         return -ENOSYS;
385 }
386
387 static int qxl_alloc_surf_ioctl(struct drm_device *dev, void *data,
388                                 struct drm_file *file)
389 {
390         struct qxl_device *qdev = dev->dev_private;
391         struct drm_qxl_alloc_surf *param = data;
392         struct qxl_bo *qobj;
393         int handle;
394         int ret;
395         int size, actual_stride;
396         struct qxl_surface surf;
397
398         /* work out size allocate bo with handle */
399         actual_stride = param->stride < 0 ? -param->stride : param->stride;
400         size = actual_stride * param->height + actual_stride;
401
402         surf.format = param->format;
403         surf.width = param->width;
404         surf.height = param->height;
405         surf.stride = param->stride;
406         surf.data = 0;
407
408         ret = qxl_gem_object_create_with_handle(qdev, file,
409                                                 QXL_GEM_DOMAIN_SURFACE,
410                                                 size,
411                                                 &surf,
412                                                 &qobj, &handle);
413         if (ret) {
414                 DRM_ERROR("%s: failed to create gem ret=%d\n",
415                           __func__, ret);
416                 return -ENOMEM;
417         } else
418                 param->handle = handle;
419         return ret;
420 }
421
422 const struct drm_ioctl_desc qxl_ioctls[] = {
423         DRM_IOCTL_DEF_DRV(QXL_ALLOC, qxl_alloc_ioctl, DRM_AUTH),
424
425         DRM_IOCTL_DEF_DRV(QXL_MAP, qxl_map_ioctl, DRM_AUTH),
426
427         DRM_IOCTL_DEF_DRV(QXL_EXECBUFFER, qxl_execbuffer_ioctl,
428                                                         DRM_AUTH),
429         DRM_IOCTL_DEF_DRV(QXL_UPDATE_AREA, qxl_update_area_ioctl,
430                                                         DRM_AUTH),
431         DRM_IOCTL_DEF_DRV(QXL_GETPARAM, qxl_getparam_ioctl,
432                                                         DRM_AUTH),
433         DRM_IOCTL_DEF_DRV(QXL_CLIENTCAP, qxl_clientcap_ioctl,
434                                                         DRM_AUTH),
435
436         DRM_IOCTL_DEF_DRV(QXL_ALLOC_SURF, qxl_alloc_surf_ioctl,
437                           DRM_AUTH),
438 };
439
440 int qxl_max_ioctls = ARRAY_SIZE(qxl_ioctls);