1e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie/* 2e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * Copyright 2011 Red Hat Inc. 3e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * 4e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * Permission is hereby granted, free of charge, to any person obtaining a 5e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * copy of this software and associated documentation files (the "Software"), 6e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * to deal in the Software without restriction, including without limitation 7e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * and/or sell copies of the Software, and to permit persons to whom the 9e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * Software is furnished to do so, subject to the following conditions: 10e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * 11e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * The above copyright notice and this permission notice shall be included in 12e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * all copies or substantial portions of the Software. 13e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * 14e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * OTHER DEALINGS IN THE SOFTWARE. 21e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * 22e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie * Authors: Dave Airlie 23e9bf5f36b09f8ec6c168ef58ee7d4890545ede1cDave Airlie */ 2422b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie 25612a9aab56a93533e76e3ad91642db7033e03b69Linus Torvalds#include <drm/drmP.h> 26b5e9c1a25fcf8a0d664606251a53b5caa6a19d08Maarten Lankhorst#include <linux/dma-buf.h> 2722b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie 2877145f1cbdf8d28b46ff8070ca749bad821e0774Ben Skeggs#include "nouveau_drm.h" 2977145f1cbdf8d28b46ff8070ca749bad821e0774Ben Skeggs#include "nouveau_gem.h" 3022b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie 31ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattnerstruct sg_table *nouveau_gem_prime_get_sg_table(struct drm_gem_object *obj) 3222b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie{ 33ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattner struct nouveau_bo *nvbo = nouveau_gem_object(obj); 3422b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie int npages = nvbo->bo.num_pages; 3522b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie 36ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattner return drm_prime_pages_to_sg(nvbo->bo.ttm->pages, npages); 3722b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie} 3822b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie 39ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattnervoid *nouveau_gem_prime_vmap(struct drm_gem_object *obj) 4022b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie{ 41ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattner struct nouveau_bo *nvbo = nouveau_gem_object(obj); 4235916acedd8dadb361ef6439d05d60fbe8f53032Dave Airlie int ret; 4335916acedd8dadb361ef6439d05d60fbe8f53032Dave Airlie 4435916acedd8dadb361ef6439d05d60fbe8f53032Dave Airlie ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.num_pages, 4535916acedd8dadb361ef6439d05d60fbe8f53032Dave Airlie &nvbo->dma_buf_vmap); 46ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattner if (ret) 4735916acedd8dadb361ef6439d05d60fbe8f53032Dave Airlie return ERR_PTR(ret); 48ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattner 4935916acedd8dadb361ef6439d05d60fbe8f53032Dave Airlie return nvbo->dma_buf_vmap.virtual; 5035916acedd8dadb361ef6439d05d60fbe8f53032Dave Airlie} 5135916acedd8dadb361ef6439d05d60fbe8f53032Dave Airlie 52ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattnervoid nouveau_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr) 5335916acedd8dadb361ef6439d05d60fbe8f53032Dave Airlie{ 54ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattner struct nouveau_bo *nvbo = nouveau_gem_object(obj); 5535916acedd8dadb361ef6439d05d60fbe8f53032Dave Airlie 56ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattner ttm_bo_kunmap(&nvbo->dma_buf_vmap); 5735916acedd8dadb361ef6439d05d60fbe8f53032Dave Airlie} 5835916acedd8dadb361ef6439d05d60fbe8f53032Dave Airlie 59ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattnerstruct drm_gem_object *nouveau_gem_prime_import_sg_table(struct drm_device *dev, 60b5e9c1a25fcf8a0d664606251a53b5caa6a19d08Maarten Lankhorst struct dma_buf_attachment *attach, 61ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattner struct sg_table *sg) 6222b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie{ 6322b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie struct nouveau_bo *nvbo; 64bb6178b04f5ef6f62990306713fb6afdf5d8bc56Maarten Lankhorst struct reservation_object *robj = attach->dmabuf->resv; 6522b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie u32 flags = 0; 6622b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie int ret; 6722b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie 6822b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie flags = TTM_PL_FLAG_TT; 6922b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie 70bb6178b04f5ef6f62990306713fb6afdf5d8bc56Maarten Lankhorst ww_mutex_lock(&robj->lock, NULL); 71b5e9c1a25fcf8a0d664606251a53b5caa6a19d08Maarten Lankhorst ret = nouveau_bo_new(dev, attach->dmabuf->size, 0, flags, 0, 0, 72bb6178b04f5ef6f62990306713fb6afdf5d8bc56Maarten Lankhorst sg, robj, &nvbo); 73bb6178b04f5ef6f62990306713fb6afdf5d8bc56Maarten Lankhorst ww_mutex_unlock(&robj->lock); 7422b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie if (ret) 75ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattner return ERR_PTR(ret); 7622b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie 7722b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART; 7855fb74adc628b99424360b0123f47ea4484c56fdDavid Herrmann 7955fb74adc628b99424360b0123f47ea4484c56fdDavid Herrmann /* Initialize the embedded gem-object. We return a single gem-reference 8055fb74adc628b99424360b0123f47ea4484c56fdDavid Herrmann * to the caller, instead of a normal nouveau_bo ttm reference. */ 8155fb74adc628b99424360b0123f47ea4484c56fdDavid Herrmann ret = drm_gem_object_init(dev, &nvbo->gem, nvbo->bo.mem.size); 8255fb74adc628b99424360b0123f47ea4484c56fdDavid Herrmann if (ret) { 83ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattner nouveau_bo_ref(NULL, &nvbo); 84ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattner return ERR_PTR(-ENOMEM); 8522b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie } 8622b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie 8755fb74adc628b99424360b0123f47ea4484c56fdDavid Herrmann return &nvbo->gem; 8822b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie} 8922b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie 90ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattnerint nouveau_gem_prime_pin(struct drm_gem_object *obj) 9122b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie{ 9222b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie struct nouveau_bo *nvbo = nouveau_gem_object(obj); 931af7c7dd2142b04f5e70746f8b65f5988e226e77Maarten Lankhorst int ret; 9422b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie 9522b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie /* pin buffer into GTT */ 9622b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_TT); 9722b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie if (ret) 98ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattner return -EINVAL; 9922b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie 100ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6Aaron Plattner return 0; 10122b33e8ed0e38b8ddcf082e35580f2e67a3a0262Dave Airlie} 1021af7c7dd2142b04f5e70746f8b65f5988e226e77Maarten Lankhorst 1031af7c7dd2142b04f5e70746f8b65f5988e226e77Maarten Lankhorstvoid nouveau_gem_prime_unpin(struct drm_gem_object *obj) 1041af7c7dd2142b04f5e70746f8b65f5988e226e77Maarten Lankhorst{ 1051af7c7dd2142b04f5e70746f8b65f5988e226e77Maarten Lankhorst struct nouveau_bo *nvbo = nouveau_gem_object(obj); 1061af7c7dd2142b04f5e70746f8b65f5988e226e77Maarten Lankhorst 1071af7c7dd2142b04f5e70746f8b65f5988e226e77Maarten Lankhorst nouveau_bo_unpin(nvbo); 1081af7c7dd2142b04f5e70746f8b65f5988e226e77Maarten Lankhorst} 1093aac4502fd3f80dcf7e65dbf6edd8676893c1f46Maarten Lankhorst 1103aac4502fd3f80dcf7e65dbf6edd8676893c1f46Maarten Lankhorststruct reservation_object *nouveau_gem_prime_res_obj(struct drm_gem_object *obj) 1113aac4502fd3f80dcf7e65dbf6edd8676893c1f46Maarten Lankhorst{ 1123aac4502fd3f80dcf7e65dbf6edd8676893c1f46Maarten Lankhorst struct nouveau_bo *nvbo = nouveau_gem_object(obj); 1133aac4502fd3f80dcf7e65dbf6edd8676893c1f46Maarten Lankhorst 1143aac4502fd3f80dcf7e65dbf6edd8676893c1f46Maarten Lankhorst return nvbo->bo.resv; 1153aac4502fd3f80dcf7e65dbf6edd8676893c1f46Maarten Lankhorst} 116