Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2016 Intel Corporation
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  * on the rights to use, copy, modify, merge, publish, distribute, sub
0008  * license, and/or sell copies of the Software, and to permit persons to whom
0009  * them Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice (including the next
0012  * paragraph) shall be included in all copies or substantial portions of the
0013  * Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
0017  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
0018  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
0019  * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
0020  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0021  */
0022 
0023 #include <linux/dma-buf.h>
0024 #include <linux/dma-resv.h>
0025 
0026 #include <drm/drm_file.h>
0027 
0028 #include "vgem_drv.h"
0029 
0030 #define VGEM_FENCE_TIMEOUT (10*HZ)
0031 
0032 struct vgem_fence {
0033     struct dma_fence base;
0034     struct spinlock lock;
0035     struct timer_list timer;
0036 };
0037 
0038 static const char *vgem_fence_get_driver_name(struct dma_fence *fence)
0039 {
0040     return "vgem";
0041 }
0042 
0043 static const char *vgem_fence_get_timeline_name(struct dma_fence *fence)
0044 {
0045     return "unbound";
0046 }
0047 
0048 static void vgem_fence_release(struct dma_fence *base)
0049 {
0050     struct vgem_fence *fence = container_of(base, typeof(*fence), base);
0051 
0052     del_timer_sync(&fence->timer);
0053     dma_fence_free(&fence->base);
0054 }
0055 
0056 static void vgem_fence_value_str(struct dma_fence *fence, char *str, int size)
0057 {
0058     snprintf(str, size, "%llu", fence->seqno);
0059 }
0060 
0061 static void vgem_fence_timeline_value_str(struct dma_fence *fence, char *str,
0062                       int size)
0063 {
0064     snprintf(str, size, "%llu",
0065          dma_fence_is_signaled(fence) ? fence->seqno : 0);
0066 }
0067 
0068 static const struct dma_fence_ops vgem_fence_ops = {
0069     .get_driver_name = vgem_fence_get_driver_name,
0070     .get_timeline_name = vgem_fence_get_timeline_name,
0071     .release = vgem_fence_release,
0072 
0073     .fence_value_str = vgem_fence_value_str,
0074     .timeline_value_str = vgem_fence_timeline_value_str,
0075 };
0076 
0077 static void vgem_fence_timeout(struct timer_list *t)
0078 {
0079     struct vgem_fence *fence = from_timer(fence, t, timer);
0080 
0081     dma_fence_signal(&fence->base);
0082 }
0083 
0084 static struct dma_fence *vgem_fence_create(struct vgem_file *vfile,
0085                        unsigned int flags)
0086 {
0087     struct vgem_fence *fence;
0088 
0089     fence = kzalloc(sizeof(*fence), GFP_KERNEL);
0090     if (!fence)
0091         return NULL;
0092 
0093     spin_lock_init(&fence->lock);
0094     dma_fence_init(&fence->base, &vgem_fence_ops, &fence->lock,
0095                dma_fence_context_alloc(1), 1);
0096 
0097     timer_setup(&fence->timer, vgem_fence_timeout, 0);
0098 
0099     /* We force the fence to expire within 10s to prevent driver hangs */
0100     mod_timer(&fence->timer, jiffies + VGEM_FENCE_TIMEOUT);
0101 
0102     return &fence->base;
0103 }
0104 
0105 /*
0106  * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH):
0107  *
0108  * Create and attach a fence to the vGEM handle. This fence is then exposed
0109  * via the dma-buf reservation object and visible to consumers of the exported
0110  * dma-buf. If the flags contain VGEM_FENCE_WRITE, the fence indicates the
0111  * vGEM buffer is being written to by the client and is exposed as an exclusive
0112  * fence, otherwise the fence indicates the client is current reading from the
0113  * buffer and all future writes should wait for the client to signal its
0114  * completion. Note that if a conflicting fence is already on the dma-buf (i.e.
0115  * an exclusive fence when adding a read, or any fence when adding a write),
0116  * -EBUSY is reported. Serialisation between operations should be handled
0117  * by waiting upon the dma-buf.
0118  *
0119  * This returns the handle for the new fence that must be signaled within 10
0120  * seconds (or otherwise it will automatically expire). See
0121  * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL).
0122  *
0123  * If the vGEM handle does not exist, vgem_fence_attach_ioctl returns -ENOENT.
0124  */
0125 int vgem_fence_attach_ioctl(struct drm_device *dev,
0126                 void *data,
0127                 struct drm_file *file)
0128 {
0129     struct drm_vgem_fence_attach *arg = data;
0130     struct vgem_file *vfile = file->driver_priv;
0131     struct dma_resv *resv;
0132     struct drm_gem_object *obj;
0133     enum dma_resv_usage usage;
0134     struct dma_fence *fence;
0135     int ret;
0136 
0137     if (arg->flags & ~VGEM_FENCE_WRITE)
0138         return -EINVAL;
0139 
0140     if (arg->pad)
0141         return -EINVAL;
0142 
0143     obj = drm_gem_object_lookup(file, arg->handle);
0144     if (!obj)
0145         return -ENOENT;
0146 
0147     fence = vgem_fence_create(vfile, arg->flags);
0148     if (!fence) {
0149         ret = -ENOMEM;
0150         goto err;
0151     }
0152 
0153     /* Check for a conflicting fence */
0154     resv = obj->resv;
0155     usage = dma_resv_usage_rw(arg->flags & VGEM_FENCE_WRITE);
0156     if (!dma_resv_test_signaled(resv, usage)) {
0157         ret = -EBUSY;
0158         goto err_fence;
0159     }
0160 
0161     /* Expose the fence via the dma-buf */
0162     dma_resv_lock(resv, NULL);
0163     ret = dma_resv_reserve_fences(resv, 1);
0164     if (!ret)
0165         dma_resv_add_fence(resv, fence, arg->flags & VGEM_FENCE_WRITE ?
0166                    DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_READ);
0167     dma_resv_unlock(resv);
0168 
0169     /* Record the fence in our idr for later signaling */
0170     if (ret == 0) {
0171         mutex_lock(&vfile->fence_mutex);
0172         ret = idr_alloc(&vfile->fence_idr, fence, 1, 0, GFP_KERNEL);
0173         mutex_unlock(&vfile->fence_mutex);
0174         if (ret > 0) {
0175             arg->out_fence = ret;
0176             ret = 0;
0177         }
0178     }
0179 err_fence:
0180     if (ret) {
0181         dma_fence_signal(fence);
0182         dma_fence_put(fence);
0183     }
0184 err:
0185     drm_gem_object_put(obj);
0186     return ret;
0187 }
0188 
0189 /*
0190  * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL):
0191  *
0192  * Signal and consume a fence ealier attached to a vGEM handle using
0193  * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH).
0194  *
0195  * All fences must be signaled within 10s of attachment or otherwise they
0196  * will automatically expire (and a vgem_fence_signal_ioctl returns -ETIMEDOUT).
0197  *
0198  * Signaling a fence indicates to all consumers of the dma-buf that the
0199  * client has completed the operation associated with the fence, and that the
0200  * buffer is then ready for consumption.
0201  *
0202  * If the fence does not exist (or has already been signaled by the client),
0203  * vgem_fence_signal_ioctl returns -ENOENT.
0204  */
0205 int vgem_fence_signal_ioctl(struct drm_device *dev,
0206                 void *data,
0207                 struct drm_file *file)
0208 {
0209     struct vgem_file *vfile = file->driver_priv;
0210     struct drm_vgem_fence_signal *arg = data;
0211     struct dma_fence *fence;
0212     int ret = 0;
0213 
0214     if (arg->flags)
0215         return -EINVAL;
0216 
0217     mutex_lock(&vfile->fence_mutex);
0218     fence = idr_replace(&vfile->fence_idr, NULL, arg->fence);
0219     mutex_unlock(&vfile->fence_mutex);
0220     if (!fence)
0221         return -ENOENT;
0222     if (IS_ERR(fence))
0223         return PTR_ERR(fence);
0224 
0225     if (dma_fence_is_signaled(fence))
0226         ret = -ETIMEDOUT;
0227 
0228     dma_fence_signal(fence);
0229     dma_fence_put(fence);
0230     return ret;
0231 }
0232 
0233 int vgem_fence_open(struct vgem_file *vfile)
0234 {
0235     mutex_init(&vfile->fence_mutex);
0236     idr_init_base(&vfile->fence_idr, 1);
0237 
0238     return 0;
0239 }
0240 
0241 static int __vgem_fence_idr_fini(int id, void *p, void *data)
0242 {
0243     dma_fence_signal(p);
0244     dma_fence_put(p);
0245     return 0;
0246 }
0247 
0248 void vgem_fence_close(struct vgem_file *vfile)
0249 {
0250     idr_for_each(&vfile->fence_idr, __vgem_fence_idr_fini, vfile);
0251     idr_destroy(&vfile->fence_idr);
0252 }