Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2013 Red Hat Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  * Authors: Dave Airlie
0023  *          Alon Levy
0024  */
0025 
0026 #include <linux/pci.h>
0027 #include <linux/uaccess.h>
0028 
0029 #include "qxl_drv.h"
0030 #include "qxl_object.h"
0031 
0032 /*
0033  * TODO: allocating a new gem(in qxl_bo) for each request.
0034  * This is wasteful since bo's are page aligned.
0035  */
0036 int qxl_alloc_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
0037 {
0038     struct qxl_device *qdev = to_qxl(dev);
0039     struct drm_qxl_alloc *qxl_alloc = data;
0040     int ret;
0041     struct qxl_bo *qobj;
0042     uint32_t handle;
0043     u32 domain = QXL_GEM_DOMAIN_VRAM;
0044 
0045     if (qxl_alloc->size == 0) {
0046         DRM_ERROR("invalid size %d\n", qxl_alloc->size);
0047         return -EINVAL;
0048     }
0049     ret = qxl_gem_object_create_with_handle(qdev, file_priv,
0050                         domain,
0051                         qxl_alloc->size,
0052                         NULL,
0053                         &qobj, &handle);
0054     if (ret) {
0055         DRM_ERROR("%s: failed to create gem ret=%d\n",
0056               __func__, ret);
0057         return -ENOMEM;
0058     }
0059     qxl_alloc->handle = handle;
0060     return 0;
0061 }
0062 
0063 int qxl_map_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
0064 {
0065     struct qxl_device *qdev = to_qxl(dev);
0066     struct drm_qxl_map *qxl_map = data;
0067 
0068     return drm_gem_ttm_dumb_map_offset(file_priv, &qdev->ddev, qxl_map->handle,
0069                        &qxl_map->offset);
0070 }
0071 
0072 struct qxl_reloc_info {
0073     int type;
0074     struct qxl_bo *dst_bo;
0075     uint32_t dst_offset;
0076     struct qxl_bo *src_bo;
0077     int src_offset;
0078 };
0079 
0080 /*
0081  * dst must be validated, i.e. whole bo on vram/surfacesram (right now all bo's
0082  * are on vram).
0083  * *(dst + dst_off) = qxl_bo_physical_address(src, src_off)
0084  */
0085 static void
0086 apply_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info)
0087 {
0088     void *reloc_page;
0089 
0090     reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK);
0091     *(uint64_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = qxl_bo_physical_address(qdev,
0092                                                   info->src_bo,
0093                                                   info->src_offset);
0094     qxl_bo_kunmap_atomic_page(qdev, info->dst_bo, reloc_page);
0095 }
0096 
0097 static void
0098 apply_surf_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info)
0099 {
0100     uint32_t id = 0;
0101     void *reloc_page;
0102 
0103     if (info->src_bo && !info->src_bo->is_primary)
0104         id = info->src_bo->surface_id;
0105 
0106     reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK);
0107     *(uint32_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = id;
0108     qxl_bo_kunmap_atomic_page(qdev, info->dst_bo, reloc_page);
0109 }
0110 
0111 /* return holding the reference to this object */
0112 static int qxlhw_handle_to_bo(struct drm_file *file_priv, uint64_t handle,
0113                   struct qxl_release *release, struct qxl_bo **qbo_p)
0114 {
0115     struct drm_gem_object *gobj;
0116     struct qxl_bo *qobj;
0117     int ret;
0118 
0119     gobj = drm_gem_object_lookup(file_priv, handle);
0120     if (!gobj)
0121         return -EINVAL;
0122 
0123     qobj = gem_to_qxl_bo(gobj);
0124 
0125     ret = qxl_release_list_add(release, qobj);
0126     drm_gem_object_put(gobj);
0127     if (ret)
0128         return ret;
0129 
0130     *qbo_p = qobj;
0131     return 0;
0132 }
0133 
0134 /*
0135  * Usage of execbuffer:
0136  * Relocations need to take into account the full QXLDrawable size.
0137  * However, the command as passed from user space must *not* contain the initial
0138  * QXLReleaseInfo struct (first XXX bytes)
0139  */
0140 static int qxl_process_single_command(struct qxl_device *qdev,
0141                       struct drm_qxl_command *cmd,
0142                       struct drm_file *file_priv)
0143 {
0144     struct qxl_reloc_info *reloc_info;
0145     int release_type;
0146     struct qxl_release *release;
0147     struct qxl_bo *cmd_bo;
0148     void *fb_cmd;
0149     int i, ret, num_relocs;
0150     int unwritten;
0151 
0152     switch (cmd->type) {
0153     case QXL_CMD_DRAW:
0154         release_type = QXL_RELEASE_DRAWABLE;
0155         break;
0156     case QXL_CMD_SURFACE:
0157     case QXL_CMD_CURSOR:
0158     default:
0159         DRM_DEBUG("Only draw commands in execbuffers\n");
0160         return -EINVAL;
0161     }
0162 
0163     if (cmd->command_size > PAGE_SIZE - sizeof(union qxl_release_info))
0164         return -EINVAL;
0165 
0166     if (!access_ok(u64_to_user_ptr(cmd->command),
0167                cmd->command_size))
0168         return -EFAULT;
0169 
0170     reloc_info = kmalloc_array(cmd->relocs_num,
0171                    sizeof(struct qxl_reloc_info), GFP_KERNEL);
0172     if (!reloc_info)
0173         return -ENOMEM;
0174 
0175     ret = qxl_alloc_release_reserved(qdev,
0176                      sizeof(union qxl_release_info) +
0177                      cmd->command_size,
0178                      release_type,
0179                      &release,
0180                      &cmd_bo);
0181     if (ret)
0182         goto out_free_reloc;
0183 
0184     /* TODO copy slow path code from i915 */
0185     fb_cmd = qxl_bo_kmap_atomic_page(qdev, cmd_bo, (release->release_offset & PAGE_MASK));
0186     unwritten = __copy_from_user_inatomic_nocache
0187         (fb_cmd + sizeof(union qxl_release_info) + (release->release_offset & ~PAGE_MASK),
0188          u64_to_user_ptr(cmd->command), cmd->command_size);
0189 
0190     {
0191         struct qxl_drawable *draw = fb_cmd;
0192 
0193         draw->mm_time = qdev->rom->mm_clock;
0194     }
0195 
0196     qxl_bo_kunmap_atomic_page(qdev, cmd_bo, fb_cmd);
0197     if (unwritten) {
0198         DRM_ERROR("got unwritten %d\n", unwritten);
0199         ret = -EFAULT;
0200         goto out_free_release;
0201     }
0202 
0203     /* fill out reloc info structs */
0204     num_relocs = 0;
0205     for (i = 0; i < cmd->relocs_num; ++i) {
0206         struct drm_qxl_reloc reloc;
0207         struct drm_qxl_reloc __user *u = u64_to_user_ptr(cmd->relocs);
0208 
0209         if (copy_from_user(&reloc, u + i, sizeof(reloc))) {
0210             ret = -EFAULT;
0211             goto out_free_bos;
0212         }
0213 
0214         /* add the bos to the list of bos to validate -
0215            need to validate first then process relocs? */
0216         if (reloc.reloc_type != QXL_RELOC_TYPE_BO && reloc.reloc_type != QXL_RELOC_TYPE_SURF) {
0217             DRM_DEBUG("unknown reloc type %d\n", reloc.reloc_type);
0218 
0219             ret = -EINVAL;
0220             goto out_free_bos;
0221         }
0222         reloc_info[i].type = reloc.reloc_type;
0223 
0224         if (reloc.dst_handle) {
0225             ret = qxlhw_handle_to_bo(file_priv, reloc.dst_handle, release,
0226                          &reloc_info[i].dst_bo);
0227             if (ret)
0228                 goto out_free_bos;
0229             reloc_info[i].dst_offset = reloc.dst_offset;
0230         } else {
0231             reloc_info[i].dst_bo = cmd_bo;
0232             reloc_info[i].dst_offset = reloc.dst_offset + release->release_offset;
0233         }
0234         num_relocs++;
0235 
0236         /* reserve and validate the reloc dst bo */
0237         if (reloc.reloc_type == QXL_RELOC_TYPE_BO || reloc.src_handle) {
0238             ret = qxlhw_handle_to_bo(file_priv, reloc.src_handle, release,
0239                          &reloc_info[i].src_bo);
0240             if (ret)
0241                 goto out_free_bos;
0242             reloc_info[i].src_offset = reloc.src_offset;
0243         } else {
0244             reloc_info[i].src_bo = NULL;
0245             reloc_info[i].src_offset = 0;
0246         }
0247     }
0248 
0249     /* validate all buffers */
0250     ret = qxl_release_reserve_list(release, false);
0251     if (ret)
0252         goto out_free_bos;
0253 
0254     for (i = 0; i < cmd->relocs_num; ++i) {
0255         if (reloc_info[i].type == QXL_RELOC_TYPE_BO)
0256             apply_reloc(qdev, &reloc_info[i]);
0257         else if (reloc_info[i].type == QXL_RELOC_TYPE_SURF)
0258             apply_surf_reloc(qdev, &reloc_info[i]);
0259     }
0260 
0261     qxl_release_fence_buffer_objects(release);
0262     ret = qxl_push_command_ring_release(qdev, release, cmd->type, true);
0263 
0264 out_free_bos:
0265 out_free_release:
0266     if (ret)
0267         qxl_release_free(qdev, release);
0268 out_free_reloc:
0269     kfree(reloc_info);
0270     return ret;
0271 }
0272 
0273 int qxl_execbuffer_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
0274 {
0275     struct qxl_device *qdev = to_qxl(dev);
0276     struct drm_qxl_execbuffer *execbuffer = data;
0277     struct drm_qxl_command user_cmd;
0278     int cmd_num;
0279     int ret;
0280 
0281     for (cmd_num = 0; cmd_num < execbuffer->commands_num; ++cmd_num) {
0282 
0283         struct drm_qxl_command __user *commands =
0284             u64_to_user_ptr(execbuffer->commands);
0285 
0286         if (copy_from_user(&user_cmd, commands + cmd_num,
0287                        sizeof(user_cmd)))
0288             return -EFAULT;
0289 
0290         ret = qxl_process_single_command(qdev, &user_cmd, file_priv);
0291         if (ret)
0292             return ret;
0293     }
0294     return 0;
0295 }
0296 
0297 int qxl_update_area_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
0298 {
0299     struct qxl_device *qdev = to_qxl(dev);
0300     struct drm_qxl_update_area *update_area = data;
0301     struct qxl_rect area = {.left = update_area->left,
0302                 .top = update_area->top,
0303                 .right = update_area->right,
0304                 .bottom = update_area->bottom};
0305     int ret;
0306     struct drm_gem_object *gobj = NULL;
0307     struct qxl_bo *qobj = NULL;
0308     struct ttm_operation_ctx ctx = { true, false };
0309 
0310     if (update_area->left >= update_area->right ||
0311         update_area->top >= update_area->bottom)
0312         return -EINVAL;
0313 
0314     gobj = drm_gem_object_lookup(file, update_area->handle);
0315     if (gobj == NULL)
0316         return -ENOENT;
0317 
0318     qobj = gem_to_qxl_bo(gobj);
0319 
0320     ret = qxl_bo_reserve(qobj);
0321     if (ret)
0322         goto out;
0323 
0324     if (!qobj->tbo.pin_count) {
0325         qxl_ttm_placement_from_domain(qobj, qobj->type);
0326         ret = ttm_bo_validate(&qobj->tbo, &qobj->placement, &ctx);
0327         if (unlikely(ret))
0328             goto out;
0329     }
0330 
0331     ret = qxl_bo_check_id(qdev, qobj);
0332     if (ret)
0333         goto out2;
0334     if (!qobj->surface_id)
0335         DRM_ERROR("got update area for surface with no id %d\n", update_area->handle);
0336     ret = qxl_io_update_area(qdev, qobj, &area);
0337 
0338 out2:
0339     qxl_bo_unreserve(qobj);
0340 
0341 out:
0342     drm_gem_object_put(gobj);
0343     return ret;
0344 }
0345 
0346 int qxl_getparam_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
0347 {
0348     struct qxl_device *qdev = to_qxl(dev);
0349     struct drm_qxl_getparam *param = data;
0350 
0351     switch (param->param) {
0352     case QXL_PARAM_NUM_SURFACES:
0353         param->value = qdev->rom->n_surfaces;
0354         break;
0355     case QXL_PARAM_MAX_RELOCS:
0356         param->value = QXL_MAX_RES;
0357         break;
0358     default:
0359         return -EINVAL;
0360     }
0361     return 0;
0362 }
0363 
0364 int qxl_clientcap_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
0365 {
0366     struct qxl_device *qdev = to_qxl(dev);
0367     struct pci_dev *pdev = to_pci_dev(dev->dev);
0368     struct drm_qxl_clientcap *param = data;
0369     int byte, idx;
0370 
0371     byte = param->index / 8;
0372     idx = param->index % 8;
0373 
0374     if (pdev->revision < 4)
0375         return -ENOSYS;
0376 
0377     if (byte >= 58)
0378         return -ENOSYS;
0379 
0380     if (qdev->rom->client_capabilities[byte] & (1 << idx))
0381         return 0;
0382     return -ENOSYS;
0383 }
0384 
0385 int qxl_alloc_surf_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
0386 {
0387     struct qxl_device *qdev = to_qxl(dev);
0388     struct drm_qxl_alloc_surf *param = data;
0389     struct qxl_bo *qobj;
0390     int handle;
0391     int ret;
0392     int size, actual_stride;
0393     struct qxl_surface surf;
0394 
0395     /* work out size allocate bo with handle */
0396     actual_stride = param->stride < 0 ? -param->stride : param->stride;
0397     size = actual_stride * param->height + actual_stride;
0398 
0399     surf.format = param->format;
0400     surf.width = param->width;
0401     surf.height = param->height;
0402     surf.stride = param->stride;
0403     surf.data = 0;
0404 
0405     ret = qxl_gem_object_create_with_handle(qdev, file,
0406                         QXL_GEM_DOMAIN_SURFACE,
0407                         size,
0408                         &surf,
0409                         &qobj, &handle);
0410     if (ret) {
0411         DRM_ERROR("%s: failed to create gem ret=%d\n",
0412               __func__, ret);
0413         return -ENOMEM;
0414     } else
0415         param->handle = handle;
0416     return ret;
0417 }