Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2012 Advanced Micro Devices, 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  * based on nouveau_prime.c
0023  *
0024  * Authors: Alex Deucher
0025  */
0026 
0027 #include <linux/dma-buf.h>
0028 
0029 #include <drm/drm_prime.h>
0030 #include <drm/radeon_drm.h>
0031 
0032 #include "radeon.h"
0033 #include "radeon_prime.h"
0034 
0035 struct sg_table *radeon_gem_prime_get_sg_table(struct drm_gem_object *obj)
0036 {
0037     struct radeon_bo *bo = gem_to_radeon_bo(obj);
0038 
0039     return drm_prime_pages_to_sg(obj->dev, bo->tbo.ttm->pages,
0040                      bo->tbo.ttm->num_pages);
0041 }
0042 
0043 struct drm_gem_object *radeon_gem_prime_import_sg_table(struct drm_device *dev,
0044                             struct dma_buf_attachment *attach,
0045                             struct sg_table *sg)
0046 {
0047     struct dma_resv *resv = attach->dmabuf->resv;
0048     struct radeon_device *rdev = dev->dev_private;
0049     struct radeon_bo *bo;
0050     int ret;
0051 
0052     dma_resv_lock(resv, NULL);
0053     ret = radeon_bo_create(rdev, attach->dmabuf->size, PAGE_SIZE, false,
0054                    RADEON_GEM_DOMAIN_GTT, 0, sg, resv, &bo);
0055     dma_resv_unlock(resv);
0056     if (ret)
0057         return ERR_PTR(ret);
0058 
0059     bo->tbo.base.funcs = &radeon_gem_object_funcs;
0060 
0061     mutex_lock(&rdev->gem.mutex);
0062     list_add_tail(&bo->list, &rdev->gem.objects);
0063     mutex_unlock(&rdev->gem.mutex);
0064 
0065     bo->prime_shared_count = 1;
0066     return &bo->tbo.base;
0067 }
0068 
0069 int radeon_gem_prime_pin(struct drm_gem_object *obj)
0070 {
0071     struct radeon_bo *bo = gem_to_radeon_bo(obj);
0072     int ret = 0;
0073 
0074     ret = radeon_bo_reserve(bo, false);
0075     if (unlikely(ret != 0))
0076         return ret;
0077 
0078     /* pin buffer into GTT */
0079     ret = radeon_bo_pin(bo, RADEON_GEM_DOMAIN_GTT, NULL);
0080     if (likely(ret == 0))
0081         bo->prime_shared_count++;
0082 
0083     radeon_bo_unreserve(bo);
0084     return ret;
0085 }
0086 
0087 void radeon_gem_prime_unpin(struct drm_gem_object *obj)
0088 {
0089     struct radeon_bo *bo = gem_to_radeon_bo(obj);
0090     int ret = 0;
0091 
0092     ret = radeon_bo_reserve(bo, false);
0093     if (unlikely(ret != 0))
0094         return;
0095 
0096     radeon_bo_unpin(bo);
0097     if (bo->prime_shared_count)
0098         bo->prime_shared_count--;
0099     radeon_bo_unreserve(bo);
0100 }
0101 
0102 
0103 struct dma_buf *radeon_gem_prime_export(struct drm_gem_object *gobj,
0104                     int flags)
0105 {
0106     struct radeon_bo *bo = gem_to_radeon_bo(gobj);
0107     if (radeon_ttm_tt_has_userptr(bo->rdev, bo->tbo.ttm))
0108         return ERR_PTR(-EPERM);
0109     return drm_gem_prime_export(gobj, flags);
0110 }