1e9bf5f36SDave Airlie /*
2e9bf5f36SDave Airlie * Copyright 2011 Red Hat Inc.
3e9bf5f36SDave Airlie *
4e9bf5f36SDave Airlie * Permission is hereby granted, free of charge, to any person obtaining a
5e9bf5f36SDave Airlie * copy of this software and associated documentation files (the "Software"),
6e9bf5f36SDave Airlie * to deal in the Software without restriction, including without limitation
7e9bf5f36SDave Airlie * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e9bf5f36SDave Airlie * and/or sell copies of the Software, and to permit persons to whom the
9e9bf5f36SDave Airlie * Software is furnished to do so, subject to the following conditions:
10e9bf5f36SDave Airlie *
11e9bf5f36SDave Airlie * The above copyright notice and this permission notice shall be included in
12e9bf5f36SDave Airlie * all copies or substantial portions of the Software.
13e9bf5f36SDave Airlie *
14e9bf5f36SDave Airlie * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15e9bf5f36SDave Airlie * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16e9bf5f36SDave Airlie * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17e9bf5f36SDave Airlie * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18e9bf5f36SDave Airlie * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19e9bf5f36SDave Airlie * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20e9bf5f36SDave Airlie * OTHER DEALINGS IN THE SOFTWARE.
21e9bf5f36SDave Airlie *
22e9bf5f36SDave Airlie * Authors: Dave Airlie
23e9bf5f36SDave Airlie */
2422b33e8eSDave Airlie
25b5e9c1a2SMaarten Lankhorst #include <linux/dma-buf.h>
26a3185f91SChristian König #include <drm/ttm/ttm_tt.h>
2722b33e8eSDave Airlie
284dc28134SBen Skeggs #include "nouveau_drv.h"
2977145f1cSBen Skeggs #include "nouveau_gem.h"
3022b33e8eSDave Airlie
nouveau_gem_prime_get_sg_table(struct drm_gem_object * obj)31ab9ccb96SAaron Plattner struct sg_table *nouveau_gem_prime_get_sg_table(struct drm_gem_object *obj)
3222b33e8eSDave Airlie {
33ab9ccb96SAaron Plattner struct nouveau_bo *nvbo = nouveau_gem_object(obj);
3422b33e8eSDave Airlie
35e11bfb99SChristian König return drm_prime_pages_to_sg(obj->dev, nvbo->bo.ttm->pages,
36e11bfb99SChristian König nvbo->bo.ttm->num_pages);
3722b33e8eSDave Airlie }
3822b33e8eSDave Airlie
nouveau_gem_prime_import_sg_table(struct drm_device * dev,struct dma_buf_attachment * attach,struct sg_table * sg)39ab9ccb96SAaron Plattner struct drm_gem_object *nouveau_gem_prime_import_sg_table(struct drm_device *dev,
40b5e9c1a2SMaarten Lankhorst struct dma_buf_attachment *attach,
41ab9ccb96SAaron Plattner struct sg_table *sg)
4222b33e8eSDave Airlie {
43bab7cc18SBen Skeggs struct nouveau_drm *drm = nouveau_drm(dev);
440bb21c96SThierry Reding struct drm_gem_object *obj;
4522b33e8eSDave Airlie struct nouveau_bo *nvbo;
4652791eeeSChristian König struct dma_resv *robj = attach->dmabuf->resv;
479ca7f796SThierry Reding u64 size = attach->dmabuf->size;
489ca7f796SThierry Reding int align = 0;
4922b33e8eSDave Airlie int ret;
5022b33e8eSDave Airlie
5152791eeeSChristian König dma_resv_lock(robj, NULL);
5281b61579SChristian König nvbo = nouveau_bo_alloc(&drm->client, &size, &align,
53b88baab8SDanilo Krummrich NOUVEAU_GEM_DOMAIN_GART, 0, 0, true);
540bb21c96SThierry Reding if (IS_ERR(nvbo)) {
550bb21c96SThierry Reding obj = ERR_CAST(nvbo);
560bb21c96SThierry Reding goto unlock;
570bb21c96SThierry Reding }
5822b33e8eSDave Airlie
5922b33e8eSDave Airlie nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART;
6055fb74adSDavid Herrmann
61ed853f6cSThomas Zimmermann nvbo->bo.base.funcs = &nouveau_gem_object_funcs;
62ed853f6cSThomas Zimmermann
6355fb74adSDavid Herrmann /* Initialize the embedded gem-object. We return a single gem-reference
6455fb74adSDavid Herrmann * to the caller, instead of a normal nouveau_bo ttm reference. */
65019cbd4aSThierry Reding ret = drm_gem_object_init(dev, &nvbo->bo.base, size);
6655fb74adSDavid Herrmann if (ret) {
67*a9bf3efcSDanilo Krummrich drm_gem_object_release(&nvbo->bo.base);
68*a9bf3efcSDanilo Krummrich kfree(nvbo);
690bb21c96SThierry Reding obj = ERR_PTR(-ENOMEM);
700bb21c96SThierry Reding goto unlock;
7122b33e8eSDave Airlie }
7222b33e8eSDave Airlie
7381b61579SChristian König ret = nouveau_bo_init(nvbo, size, align, NOUVEAU_GEM_DOMAIN_GART,
7481b61579SChristian König sg, robj);
75019cbd4aSThierry Reding if (ret) {
760bb21c96SThierry Reding obj = ERR_PTR(ret);
770bb21c96SThierry Reding goto unlock;
78019cbd4aSThierry Reding }
79019cbd4aSThierry Reding
800bb21c96SThierry Reding obj = &nvbo->bo.base;
810bb21c96SThierry Reding
820bb21c96SThierry Reding unlock:
830bb21c96SThierry Reding dma_resv_unlock(robj);
840bb21c96SThierry Reding return obj;
8522b33e8eSDave Airlie }
8622b33e8eSDave Airlie
nouveau_gem_prime_pin(struct drm_gem_object * obj)87ab9ccb96SAaron Plattner int nouveau_gem_prime_pin(struct drm_gem_object *obj)
8822b33e8eSDave Airlie {
8922b33e8eSDave Airlie struct nouveau_bo *nvbo = nouveau_gem_object(obj);
901af7c7ddSMaarten Lankhorst int ret;
9122b33e8eSDave Airlie
921a8326deSThomas Zimmermann /* pin buffer into GTT */
931a8326deSThomas Zimmermann ret = nouveau_bo_pin_locked(nvbo, NOUVEAU_GEM_DOMAIN_GART, false);
941a8326deSThomas Zimmermann if (ret)
951a8326deSThomas Zimmermann ret = -EINVAL;
9622b33e8eSDave Airlie
971a8326deSThomas Zimmermann return ret;
9822b33e8eSDave Airlie }
991af7c7ddSMaarten Lankhorst
nouveau_gem_prime_unpin(struct drm_gem_object * obj)1001af7c7ddSMaarten Lankhorst void nouveau_gem_prime_unpin(struct drm_gem_object *obj)
1011af7c7ddSMaarten Lankhorst {
1021af7c7ddSMaarten Lankhorst struct nouveau_bo *nvbo = nouveau_gem_object(obj);
1031af7c7ddSMaarten Lankhorst
1041a8326deSThomas Zimmermann nouveau_bo_unpin_locked(nvbo);
1051af7c7ddSMaarten Lankhorst }
106b88baab8SDanilo Krummrich
nouveau_gem_prime_export(struct drm_gem_object * gobj,int flags)107b88baab8SDanilo Krummrich struct dma_buf *nouveau_gem_prime_export(struct drm_gem_object *gobj,
108b88baab8SDanilo Krummrich int flags)
109b88baab8SDanilo Krummrich {
110b88baab8SDanilo Krummrich struct nouveau_bo *nvbo = nouveau_gem_object(gobj);
111b88baab8SDanilo Krummrich
112b88baab8SDanilo Krummrich if (nvbo->no_share)
113b88baab8SDanilo Krummrich return ERR_PTR(-EPERM);
114b88baab8SDanilo Krummrich
115b88baab8SDanilo Krummrich return drm_gem_prime_export(gobj, flags);
116b88baab8SDanilo Krummrich }
117