Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2011 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  */
0024 
0025 #include <linux/dma-buf.h>
0026 
0027 #include "nouveau_drv.h"
0028 #include "nouveau_gem.h"
0029 
0030 struct sg_table *nouveau_gem_prime_get_sg_table(struct drm_gem_object *obj)
0031 {
0032     struct nouveau_bo *nvbo = nouveau_gem_object(obj);
0033 
0034     return drm_prime_pages_to_sg(obj->dev, nvbo->bo.ttm->pages,
0035                      nvbo->bo.ttm->num_pages);
0036 }
0037 
0038 struct drm_gem_object *nouveau_gem_prime_import_sg_table(struct drm_device *dev,
0039                              struct dma_buf_attachment *attach,
0040                              struct sg_table *sg)
0041 {
0042     struct nouveau_drm *drm = nouveau_drm(dev);
0043     struct drm_gem_object *obj;
0044     struct nouveau_bo *nvbo;
0045     struct dma_resv *robj = attach->dmabuf->resv;
0046     u64 size = attach->dmabuf->size;
0047     int align = 0;
0048     int ret;
0049 
0050     dma_resv_lock(robj, NULL);
0051     nvbo = nouveau_bo_alloc(&drm->client, &size, &align,
0052                 NOUVEAU_GEM_DOMAIN_GART, 0, 0);
0053     if (IS_ERR(nvbo)) {
0054         obj = ERR_CAST(nvbo);
0055         goto unlock;
0056     }
0057 
0058     nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART;
0059 
0060     nvbo->bo.base.funcs = &nouveau_gem_object_funcs;
0061 
0062     /* Initialize the embedded gem-object. We return a single gem-reference
0063      * to the caller, instead of a normal nouveau_bo ttm reference. */
0064     ret = drm_gem_object_init(dev, &nvbo->bo.base, size);
0065     if (ret) {
0066         nouveau_bo_ref(NULL, &nvbo);
0067         obj = ERR_PTR(-ENOMEM);
0068         goto unlock;
0069     }
0070 
0071     ret = nouveau_bo_init(nvbo, size, align, NOUVEAU_GEM_DOMAIN_GART,
0072                   sg, robj);
0073     if (ret) {
0074         nouveau_bo_ref(NULL, &nvbo);
0075         obj = ERR_PTR(ret);
0076         goto unlock;
0077     }
0078 
0079     obj = &nvbo->bo.base;
0080 
0081 unlock:
0082     dma_resv_unlock(robj);
0083     return obj;
0084 }
0085 
0086 int nouveau_gem_prime_pin(struct drm_gem_object *obj)
0087 {
0088     struct nouveau_bo *nvbo = nouveau_gem_object(obj);
0089     int ret;
0090 
0091     /* pin buffer into GTT */
0092     ret = nouveau_bo_pin(nvbo, NOUVEAU_GEM_DOMAIN_GART, false);
0093     if (ret)
0094         return -EINVAL;
0095 
0096     return 0;
0097 }
0098 
0099 void nouveau_gem_prime_unpin(struct drm_gem_object *obj)
0100 {
0101     struct nouveau_bo *nvbo = nouveau_gem_object(obj);
0102 
0103     nouveau_bo_unpin(nvbo);
0104 }