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 89 reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK); 90 *(uint64_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = qxl_bo_physical_address(qdev, 91 info->src_bo, 92 info->src_offset); 93 qxl_bo_kunmap_atomic_page(qdev, info->dst_bo, reloc_page); 94 } 95 96 static void 97 apply_surf_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info) 98 { 99 uint32_t id = 0; 100 void *reloc_page; 101 102 if (info->src_bo && !info->src_bo->is_primary) 103 id = info->src_bo->surface_id; 104 105 reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK); 106 *(uint32_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = id; 107 qxl_bo_kunmap_atomic_page(qdev, info->dst_bo, reloc_page); 108 } 109 110 /* return holding the reference to this object */ 111 static int qxlhw_handle_to_bo(struct drm_file *file_priv, uint64_t handle, 112 struct qxl_release *release, struct qxl_bo **qbo_p) 113 { 114 struct drm_gem_object *gobj; 115 struct qxl_bo *qobj; 116 int ret; 117 118 gobj = drm_gem_object_lookup(file_priv, handle); 119 if (!gobj) 120 return -EINVAL; 121 122 qobj = gem_to_qxl_bo(gobj); 123 124 ret = qxl_release_list_add(release, qobj); 125 drm_gem_object_put_unlocked(gobj); 126 if (ret) 127 return ret; 128 129 *qbo_p = qobj; 130 return 0; 131 } 132 133 /* 134 * Usage of execbuffer: 135 * Relocations need to take into account the full QXLDrawable size. 136 * However, the command as passed from user space must *not* contain the initial 137 * QXLReleaseInfo struct (first XXX bytes) 138 */ 139 static int qxl_process_single_command(struct qxl_device *qdev, 140 struct drm_qxl_command *cmd, 141 struct drm_file *file_priv) 142 { 143 struct qxl_reloc_info *reloc_info; 144 int release_type; 145 struct qxl_release *release; 146 struct qxl_bo *cmd_bo; 147 void *fb_cmd; 148 int i, ret, num_relocs; 149 int unwritten; 150 151 switch (cmd->type) { 152 case QXL_CMD_DRAW: 153 release_type = QXL_RELEASE_DRAWABLE; 154 break; 155 case QXL_CMD_SURFACE: 156 case QXL_CMD_CURSOR: 157 default: 158 DRM_DEBUG("Only draw commands in execbuffers\n"); 159 return -EINVAL; 160 break; 161 } 162 163 if (cmd->command_size > PAGE_SIZE - sizeof(union qxl_release_info)) 164 return -EINVAL; 165 166 if (!access_ok(VERIFY_READ, 167 u64_to_user_ptr(cmd->command), 168 cmd->command_size)) 169 return -EFAULT; 170 171 reloc_info = kmalloc_array(cmd->relocs_num, 172 sizeof(struct qxl_reloc_info), GFP_KERNEL); 173 if (!reloc_info) 174 return -ENOMEM; 175 176 ret = qxl_alloc_release_reserved(qdev, 177 sizeof(union qxl_release_info) + 178 cmd->command_size, 179 release_type, 180 &release, 181 &cmd_bo); 182 if (ret) 183 goto out_free_reloc; 184 185 /* TODO copy slow path code from i915 */ 186 fb_cmd = qxl_bo_kmap_atomic_page(qdev, cmd_bo, (release->release_offset & PAGE_MASK)); 187 unwritten = __copy_from_user_inatomic_nocache 188 (fb_cmd + sizeof(union qxl_release_info) + (release->release_offset & ~PAGE_MASK), 189 u64_to_user_ptr(cmd->command), cmd->command_size); 190 191 { 192 struct qxl_drawable *draw = fb_cmd; 193 194 draw->mm_time = qdev->rom->mm_clock; 195 } 196 197 qxl_bo_kunmap_atomic_page(qdev, cmd_bo, fb_cmd); 198 if (unwritten) { 199 DRM_ERROR("got unwritten %d\n", unwritten); 200 ret = -EFAULT; 201 goto out_free_release; 202 } 203 204 /* fill out reloc info structs */ 205 num_relocs = 0; 206 for (i = 0; i < cmd->relocs_num; ++i) { 207 struct drm_qxl_reloc reloc; 208 struct drm_qxl_reloc __user *u = u64_to_user_ptr(cmd->relocs); 209 210 if (copy_from_user(&reloc, u + i, sizeof(reloc))) { 211 ret = -EFAULT; 212 goto out_free_bos; 213 } 214 215 /* add the bos to the list of bos to validate - 216 need to validate first then process relocs? */ 217 if (reloc.reloc_type != QXL_RELOC_TYPE_BO && reloc.reloc_type != QXL_RELOC_TYPE_SURF) { 218 DRM_DEBUG("unknown reloc type %d\n", reloc.reloc_type); 219 220 ret = -EINVAL; 221 goto out_free_bos; 222 } 223 reloc_info[i].type = reloc.reloc_type; 224 225 if (reloc.dst_handle) { 226 ret = qxlhw_handle_to_bo(file_priv, reloc.dst_handle, release, 227 &reloc_info[i].dst_bo); 228 if (ret) 229 goto out_free_bos; 230 reloc_info[i].dst_offset = reloc.dst_offset; 231 } else { 232 reloc_info[i].dst_bo = cmd_bo; 233 reloc_info[i].dst_offset = reloc.dst_offset + release->release_offset; 234 } 235 num_relocs++; 236 237 /* reserve and validate the reloc dst bo */ 238 if (reloc.reloc_type == QXL_RELOC_TYPE_BO || reloc.src_handle) { 239 ret = qxlhw_handle_to_bo(file_priv, reloc.src_handle, release, 240 &reloc_info[i].src_bo); 241 if (ret) 242 goto out_free_bos; 243 reloc_info[i].src_offset = reloc.src_offset; 244 } else { 245 reloc_info[i].src_bo = NULL; 246 reloc_info[i].src_offset = 0; 247 } 248 } 249 250 /* validate all buffers */ 251 ret = qxl_release_reserve_list(release, false); 252 if (ret) 253 goto out_free_bos; 254 255 for (i = 0; i < cmd->relocs_num; ++i) { 256 if (reloc_info[i].type == QXL_RELOC_TYPE_BO) 257 apply_reloc(qdev, &reloc_info[i]); 258 else if (reloc_info[i].type == QXL_RELOC_TYPE_SURF) 259 apply_surf_reloc(qdev, &reloc_info[i]); 260 } 261 262 ret = qxl_push_command_ring_release(qdev, release, cmd->type, true); 263 if (ret) 264 qxl_release_backoff_reserve_list(release); 265 else 266 qxl_release_fence_buffer_objects(release); 267 268 out_free_bos: 269 out_free_release: 270 if (ret) 271 qxl_release_free(qdev, release); 272 out_free_reloc: 273 kfree(reloc_info); 274 return ret; 275 } 276 277 static int qxl_execbuffer_ioctl(struct drm_device *dev, void *data, 278 struct drm_file *file_priv) 279 { 280 struct qxl_device *qdev = dev->dev_private; 281 struct drm_qxl_execbuffer *execbuffer = data; 282 struct drm_qxl_command user_cmd; 283 int cmd_num; 284 int ret; 285 286 for (cmd_num = 0; cmd_num < execbuffer->commands_num; ++cmd_num) { 287 288 struct drm_qxl_command __user *commands = 289 u64_to_user_ptr(execbuffer->commands); 290 291 if (copy_from_user(&user_cmd, commands + cmd_num, 292 sizeof(user_cmd))) 293 return -EFAULT; 294 295 ret = qxl_process_single_command(qdev, &user_cmd, file_priv); 296 if (ret) 297 return ret; 298 } 299 return 0; 300 } 301 302 static int qxl_update_area_ioctl(struct drm_device *dev, void *data, 303 struct drm_file *file) 304 { 305 struct qxl_device *qdev = dev->dev_private; 306 struct drm_qxl_update_area *update_area = data; 307 struct qxl_rect area = {.left = update_area->left, 308 .top = update_area->top, 309 .right = update_area->right, 310 .bottom = update_area->bottom}; 311 int ret; 312 struct drm_gem_object *gobj = NULL; 313 struct qxl_bo *qobj = NULL; 314 struct ttm_operation_ctx ctx = { true, false }; 315 316 if (update_area->left >= update_area->right || 317 update_area->top >= update_area->bottom) 318 return -EINVAL; 319 320 gobj = drm_gem_object_lookup(file, update_area->handle); 321 if (gobj == NULL) 322 return -ENOENT; 323 324 qobj = gem_to_qxl_bo(gobj); 325 326 ret = qxl_bo_reserve(qobj, false); 327 if (ret) 328 goto out; 329 330 if (!qobj->pin_count) { 331 qxl_ttm_placement_from_domain(qobj, qobj->type, false); 332 ret = ttm_bo_validate(&qobj->tbo, &qobj->placement, &ctx); 333 if (unlikely(ret)) 334 goto out; 335 } 336 337 ret = qxl_bo_check_id(qdev, qobj); 338 if (ret) 339 goto out2; 340 if (!qobj->surface_id) 341 DRM_ERROR("got update area for surface with no id %d\n", update_area->handle); 342 ret = qxl_io_update_area(qdev, qobj, &area); 343 344 out2: 345 qxl_bo_unreserve(qobj); 346 347 out: 348 drm_gem_object_put_unlocked(gobj); 349 return ret; 350 } 351 352 static int qxl_getparam_ioctl(struct drm_device *dev, void *data, 353 struct drm_file *file_priv) 354 { 355 struct qxl_device *qdev = dev->dev_private; 356 struct drm_qxl_getparam *param = data; 357 358 switch (param->param) { 359 case QXL_PARAM_NUM_SURFACES: 360 param->value = qdev->rom->n_surfaces; 361 break; 362 case QXL_PARAM_MAX_RELOCS: 363 param->value = QXL_MAX_RES; 364 break; 365 default: 366 return -EINVAL; 367 } 368 return 0; 369 } 370 371 static int qxl_clientcap_ioctl(struct drm_device *dev, void *data, 372 struct drm_file *file_priv) 373 { 374 struct qxl_device *qdev = dev->dev_private; 375 struct drm_qxl_clientcap *param = data; 376 int byte, idx; 377 378 byte = param->index / 8; 379 idx = param->index % 8; 380 381 if (dev->pdev->revision < 4) 382 return -ENOSYS; 383 384 if (byte >= 58) 385 return -ENOSYS; 386 387 if (qdev->rom->client_capabilities[byte] & (1 << idx)) 388 return 0; 389 return -ENOSYS; 390 } 391 392 static int qxl_alloc_surf_ioctl(struct drm_device *dev, void *data, 393 struct drm_file *file) 394 { 395 struct qxl_device *qdev = dev->dev_private; 396 struct drm_qxl_alloc_surf *param = data; 397 struct qxl_bo *qobj; 398 int handle; 399 int ret; 400 int size, actual_stride; 401 struct qxl_surface surf; 402 403 /* work out size allocate bo with handle */ 404 actual_stride = param->stride < 0 ? -param->stride : param->stride; 405 size = actual_stride * param->height + actual_stride; 406 407 surf.format = param->format; 408 surf.width = param->width; 409 surf.height = param->height; 410 surf.stride = param->stride; 411 surf.data = 0; 412 413 ret = qxl_gem_object_create_with_handle(qdev, file, 414 QXL_GEM_DOMAIN_SURFACE, 415 size, 416 &surf, 417 &qobj, &handle); 418 if (ret) { 419 DRM_ERROR("%s: failed to create gem ret=%d\n", 420 __func__, ret); 421 return -ENOMEM; 422 } else 423 param->handle = handle; 424 return ret; 425 } 426 427 const struct drm_ioctl_desc qxl_ioctls[] = { 428 DRM_IOCTL_DEF_DRV(QXL_ALLOC, qxl_alloc_ioctl, DRM_AUTH), 429 430 DRM_IOCTL_DEF_DRV(QXL_MAP, qxl_map_ioctl, DRM_AUTH), 431 432 DRM_IOCTL_DEF_DRV(QXL_EXECBUFFER, qxl_execbuffer_ioctl, 433 DRM_AUTH), 434 DRM_IOCTL_DEF_DRV(QXL_UPDATE_AREA, qxl_update_area_ioctl, 435 DRM_AUTH), 436 DRM_IOCTL_DEF_DRV(QXL_GETPARAM, qxl_getparam_ioctl, 437 DRM_AUTH), 438 DRM_IOCTL_DEF_DRV(QXL_CLIENTCAP, qxl_clientcap_ioctl, 439 DRM_AUTH), 440 441 DRM_IOCTL_DEF_DRV(QXL_ALLOC_SURF, qxl_alloc_surf_ioctl, 442 DRM_AUTH), 443 }; 444 445 int qxl_max_ioctls = ARRAY_SIZE(qxl_ioctls); 446