abfa8f8065636c0f52b9301cfa3d25616ef7bdc2
[platform/upstream/libdrm.git] / linux-core / drm_bo_move.c
1 /**************************************************************************
2  * 
3  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
4  * All Rights Reserved.
5  * 
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  * 
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 /*
28  * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
29  */
30
31 #include "drmP.h"
32
33 int drm_bo_move_ttm(drm_device_t *dev,
34                     drm_ttm_t *ttm, 
35                     int evict,
36                     int no_wait,
37                     drm_bo_mem_reg_t *old_mem,
38                     drm_bo_mem_reg_t *new_mem)
39 {
40         uint32_t save_flags = old_mem->flags;
41         uint32_t save_mask = old_mem->mask;
42         int ret;
43
44         if (old_mem->mem_type == DRM_BO_MEM_TT) {
45
46                 if (evict)
47                         drm_ttm_evict(ttm);
48                 else
49                         drm_ttm_unbind(ttm);
50
51                 mutex_lock(&dev->struct_mutex);
52                 drm_mm_put_block(old_mem->mm_node);
53                 mutex_unlock(&dev->struct_mutex);
54                 save_flags |= DRM_BO_FLAG_CACHED;
55
56         } else {
57
58                 ret = drm_bind_ttm(ttm, 
59                                    new_mem->flags & DRM_BO_FLAG_BIND_CACHED, 
60                                    new_mem->mm_node->start);
61                 if (ret)
62                         return ret;
63
64                 if (!(new_mem->flags & DRM_BO_FLAG_BIND_CACHED)) {
65                         save_flags &= ~DRM_BO_FLAG_CACHED;
66                 }
67
68         }
69
70         *old_mem = *new_mem;
71         new_mem->mm_node = NULL;
72         old_mem->mask = save_mask;
73         DRM_MASK_VAL(save_flags, new_mem->flags, DRM_BO_MASK_MEM);
74         return 0;
75 }