2 * Copyright 2013 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Dave Airlie
26 #include <linux/pci.h>
27 #include <linux/uaccess.h>
30 #include "qxl_object.h"
33 * TODO: allocating a new gem(in qxl_bo) for each request.
34 * This is wasteful since bo's are page aligned.
36 static int qxl_alloc_ioctl(struct drm_device
*dev
, void *data
,
37 struct drm_file
*file_priv
)
39 struct qxl_device
*qdev
= dev
->dev_private
;
40 struct drm_qxl_alloc
*qxl_alloc
= data
;
44 u32 domain
= QXL_GEM_DOMAIN_VRAM
;
46 if (qxl_alloc
->size
== 0) {
47 DRM_ERROR("invalid size %d\n", qxl_alloc
->size
);
50 ret
= qxl_gem_object_create_with_handle(qdev
, file_priv
,
56 DRM_ERROR("%s: failed to create gem ret=%d\n",
60 qxl_alloc
->handle
= handle
;
64 static int qxl_map_ioctl(struct drm_device
*dev
, void *data
,
65 struct drm_file
*file_priv
)
67 struct qxl_device
*qdev
= dev
->dev_private
;
68 struct drm_qxl_map
*qxl_map
= data
;
70 return qxl_mode_dumb_mmap(file_priv
, &qdev
->ddev
, qxl_map
->handle
,
74 struct qxl_reloc_info
{
76 struct qxl_bo
*dst_bo
;
78 struct qxl_bo
*src_bo
;
83 * dst must be validated, i.e. whole bo on vram/surfacesram (right now all bo's
85 * *(dst + dst_off) = qxl_bo_physical_address(src, src_off)
88 apply_reloc(struct qxl_device
*qdev
, struct qxl_reloc_info
*info
)
92 reloc_page
= qxl_bo_kmap_atomic_page(qdev
, info
->dst_bo
, info
->dst_offset
& PAGE_MASK
);
93 *(uint64_t *)(reloc_page
+ (info
->dst_offset
& ~PAGE_MASK
)) = qxl_bo_physical_address(qdev
,
96 qxl_bo_kunmap_atomic_page(qdev
, info
->dst_bo
, reloc_page
);
100 apply_surf_reloc(struct qxl_device
*qdev
, struct qxl_reloc_info
*info
)
105 if (info
->src_bo
&& !info
->src_bo
->is_primary
)
106 id
= info
->src_bo
->surface_id
;
108 reloc_page
= qxl_bo_kmap_atomic_page(qdev
, info
->dst_bo
, info
->dst_offset
& PAGE_MASK
);
109 *(uint32_t *)(reloc_page
+ (info
->dst_offset
& ~PAGE_MASK
)) = id
;
110 qxl_bo_kunmap_atomic_page(qdev
, info
->dst_bo
, reloc_page
);
113 /* return holding the reference to this object */
114 static int qxlhw_handle_to_bo(struct drm_file
*file_priv
, uint64_t handle
,
115 struct qxl_release
*release
, struct qxl_bo
**qbo_p
)
117 struct drm_gem_object
*gobj
;
121 gobj
= drm_gem_object_lookup(file_priv
, handle
);
125 qobj
= gem_to_qxl_bo(gobj
);
127 ret
= qxl_release_list_add(release
, qobj
);
128 drm_gem_object_put_unlocked(gobj
);
137 * Usage of execbuffer:
138 * Relocations need to take into account the full QXLDrawable size.
139 * However, the command as passed from user space must *not* contain the initial
140 * QXLReleaseInfo struct (first XXX bytes)
142 static int qxl_process_single_command(struct qxl_device
*qdev
,
143 struct drm_qxl_command
*cmd
,
144 struct drm_file
*file_priv
)
146 struct qxl_reloc_info
*reloc_info
;
148 struct qxl_release
*release
;
149 struct qxl_bo
*cmd_bo
;
151 int i
, ret
, num_relocs
;
156 release_type
= QXL_RELEASE_DRAWABLE
;
158 case QXL_CMD_SURFACE
:
161 DRM_DEBUG("Only draw commands in execbuffers\n");
166 if (cmd
->command_size
> PAGE_SIZE
- sizeof(union qxl_release_info
))
169 if (!access_ok(u64_to_user_ptr(cmd
->command
),
173 reloc_info
= kmalloc_array(cmd
->relocs_num
,
174 sizeof(struct qxl_reloc_info
), GFP_KERNEL
);
178 ret
= qxl_alloc_release_reserved(qdev
,
179 sizeof(union qxl_release_info
) +
187 /* TODO copy slow path code from i915 */
188 fb_cmd
= qxl_bo_kmap_atomic_page(qdev
, cmd_bo
, (release
->release_offset
& PAGE_MASK
));
189 unwritten
= __copy_from_user_inatomic_nocache
190 (fb_cmd
+ sizeof(union qxl_release_info
) + (release
->release_offset
& ~PAGE_MASK
),
191 u64_to_user_ptr(cmd
->command
), cmd
->command_size
);
194 struct qxl_drawable
*draw
= fb_cmd
;
196 draw
->mm_time
= qdev
->rom
->mm_clock
;
199 qxl_bo_kunmap_atomic_page(qdev
, cmd_bo
, fb_cmd
);
201 DRM_ERROR("got unwritten %d\n", unwritten
);
203 goto out_free_release
;
206 /* fill out reloc info structs */
208 for (i
= 0; i
< cmd
->relocs_num
; ++i
) {
209 struct drm_qxl_reloc reloc
;
210 struct drm_qxl_reloc __user
*u
= u64_to_user_ptr(cmd
->relocs
);
212 if (copy_from_user(&reloc
, u
+ i
, sizeof(reloc
))) {
217 /* add the bos to the list of bos to validate -
218 need to validate first then process relocs? */
219 if (reloc
.reloc_type
!= QXL_RELOC_TYPE_BO
&& reloc
.reloc_type
!= QXL_RELOC_TYPE_SURF
) {
220 DRM_DEBUG("unknown reloc type %d\n", reloc
.reloc_type
);
225 reloc_info
[i
].type
= reloc
.reloc_type
;
227 if (reloc
.dst_handle
) {
228 ret
= qxlhw_handle_to_bo(file_priv
, reloc
.dst_handle
, release
,
229 &reloc_info
[i
].dst_bo
);
232 reloc_info
[i
].dst_offset
= reloc
.dst_offset
;
234 reloc_info
[i
].dst_bo
= cmd_bo
;
235 reloc_info
[i
].dst_offset
= reloc
.dst_offset
+ release
->release_offset
;
239 /* reserve and validate the reloc dst bo */
240 if (reloc
.reloc_type
== QXL_RELOC_TYPE_BO
|| reloc
.src_handle
) {
241 ret
= qxlhw_handle_to_bo(file_priv
, reloc
.src_handle
, release
,
242 &reloc_info
[i
].src_bo
);
245 reloc_info
[i
].src_offset
= reloc
.src_offset
;
247 reloc_info
[i
].src_bo
= NULL
;
248 reloc_info
[i
].src_offset
= 0;
252 /* validate all buffers */
253 ret
= qxl_release_reserve_list(release
, false);
257 for (i
= 0; i
< cmd
->relocs_num
; ++i
) {
258 if (reloc_info
[i
].type
== QXL_RELOC_TYPE_BO
)
259 apply_reloc(qdev
, &reloc_info
[i
]);
260 else if (reloc_info
[i
].type
== QXL_RELOC_TYPE_SURF
)
261 apply_surf_reloc(qdev
, &reloc_info
[i
]);
264 ret
= qxl_push_command_ring_release(qdev
, release
, cmd
->type
, true);
266 qxl_release_backoff_reserve_list(release
);
268 qxl_release_fence_buffer_objects(release
);
273 qxl_release_free(qdev
, release
);
279 static int qxl_execbuffer_ioctl(struct drm_device
*dev
, void *data
,
280 struct drm_file
*file_priv
)
282 struct qxl_device
*qdev
= dev
->dev_private
;
283 struct drm_qxl_execbuffer
*execbuffer
= data
;
284 struct drm_qxl_command user_cmd
;
288 for (cmd_num
= 0; cmd_num
< execbuffer
->commands_num
; ++cmd_num
) {
290 struct drm_qxl_command __user
*commands
=
291 u64_to_user_ptr(execbuffer
->commands
);
293 if (copy_from_user(&user_cmd
, commands
+ cmd_num
,
297 ret
= qxl_process_single_command(qdev
, &user_cmd
, file_priv
);
304 static int qxl_update_area_ioctl(struct drm_device
*dev
, void *data
,
305 struct drm_file
*file
)
307 struct qxl_device
*qdev
= dev
->dev_private
;
308 struct drm_qxl_update_area
*update_area
= data
;
309 struct qxl_rect area
= {.left
= update_area
->left
,
310 .top
= update_area
->top
,
311 .right
= update_area
->right
,
312 .bottom
= update_area
->bottom
};
314 struct drm_gem_object
*gobj
= NULL
;
315 struct qxl_bo
*qobj
= NULL
;
316 struct ttm_operation_ctx ctx
= { true, false };
318 if (update_area
->left
>= update_area
->right
||
319 update_area
->top
>= update_area
->bottom
)
322 gobj
= drm_gem_object_lookup(file
, update_area
->handle
);
326 qobj
= gem_to_qxl_bo(gobj
);
328 ret
= qxl_bo_reserve(qobj
, false);
332 if (!qobj
->pin_count
) {
333 qxl_ttm_placement_from_domain(qobj
, qobj
->type
, false);
334 ret
= ttm_bo_validate(&qobj
->tbo
, &qobj
->placement
, &ctx
);
339 ret
= qxl_bo_check_id(qdev
, qobj
);
342 if (!qobj
->surface_id
)
343 DRM_ERROR("got update area for surface with no id %d\n", update_area
->handle
);
344 ret
= qxl_io_update_area(qdev
, qobj
, &area
);
347 qxl_bo_unreserve(qobj
);
350 drm_gem_object_put_unlocked(gobj
);
354 static int qxl_getparam_ioctl(struct drm_device
*dev
, void *data
,
355 struct drm_file
*file_priv
)
357 struct qxl_device
*qdev
= dev
->dev_private
;
358 struct drm_qxl_getparam
*param
= data
;
360 switch (param
->param
) {
361 case QXL_PARAM_NUM_SURFACES
:
362 param
->value
= qdev
->rom
->n_surfaces
;
364 case QXL_PARAM_MAX_RELOCS
:
365 param
->value
= QXL_MAX_RES
;
373 static int qxl_clientcap_ioctl(struct drm_device
*dev
, void *data
,
374 struct drm_file
*file_priv
)
376 struct qxl_device
*qdev
= dev
->dev_private
;
377 struct drm_qxl_clientcap
*param
= data
;
380 byte
= param
->index
/ 8;
381 idx
= param
->index
% 8;
383 if (dev
->pdev
->revision
< 4)
389 if (qdev
->rom
->client_capabilities
[byte
] & (1 << idx
))
394 static int qxl_alloc_surf_ioctl(struct drm_device
*dev
, void *data
,
395 struct drm_file
*file
)
397 struct qxl_device
*qdev
= dev
->dev_private
;
398 struct drm_qxl_alloc_surf
*param
= data
;
402 int size
, actual_stride
;
403 struct qxl_surface surf
;
405 /* work out size allocate bo with handle */
406 actual_stride
= param
->stride
< 0 ? -param
->stride
: param
->stride
;
407 size
= actual_stride
* param
->height
+ actual_stride
;
409 surf
.format
= param
->format
;
410 surf
.width
= param
->width
;
411 surf
.height
= param
->height
;
412 surf
.stride
= param
->stride
;
415 ret
= qxl_gem_object_create_with_handle(qdev
, file
,
416 QXL_GEM_DOMAIN_SURFACE
,
421 DRM_ERROR("%s: failed to create gem ret=%d\n",
425 param
->handle
= handle
;
429 const struct drm_ioctl_desc qxl_ioctls
[] = {
430 DRM_IOCTL_DEF_DRV(QXL_ALLOC
, qxl_alloc_ioctl
, DRM_AUTH
),
432 DRM_IOCTL_DEF_DRV(QXL_MAP
, qxl_map_ioctl
, DRM_AUTH
),
434 DRM_IOCTL_DEF_DRV(QXL_EXECBUFFER
, qxl_execbuffer_ioctl
,
436 DRM_IOCTL_DEF_DRV(QXL_UPDATE_AREA
, qxl_update_area_ioctl
,
438 DRM_IOCTL_DEF_DRV(QXL_GETPARAM
, qxl_getparam_ioctl
,
440 DRM_IOCTL_DEF_DRV(QXL_CLIENTCAP
, qxl_clientcap_ioctl
,
443 DRM_IOCTL_DEF_DRV(QXL_ALLOC_SURF
, qxl_alloc_surf_ioctl
,
447 int qxl_max_ioctls
= ARRAY_SIZE(qxl_ioctls
);