Merge remote branch 'airlied/drm-core-next' into tmp
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / ttm / ttm_bo_manager.c
1 /**************************************************************************
2  *
3  * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., 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 Hellstrom <thellstrom-at-vmware-dot-com>
29  */
30
31 #include "ttm/ttm_module.h"
32 #include "ttm/ttm_bo_driver.h"
33 #include "ttm/ttm_placement.h"
34 #include <linux/jiffies.h>
35 #include <linux/slab.h>
36 #include <linux/sched.h>
37 #include <linux/mm.h>
38 #include <linux/file.h>
39 #include <linux/module.h>
40
41 static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
42                                struct ttm_buffer_object *bo,
43                                struct ttm_placement *placement,
44                                struct ttm_mem_reg *mem)
45 {
46         struct ttm_bo_global *glob = man->bdev->glob;
47         struct drm_mm *mm = man->priv;
48         struct drm_mm_node *node = NULL;
49         unsigned long lpfn;
50         int ret;
51
52         lpfn = placement->lpfn;
53         if (!lpfn)
54                 lpfn = man->size;
55         do {
56                 ret = drm_mm_pre_get(mm);
57                 if (unlikely(ret))
58                         return ret;
59
60                 spin_lock(&glob->lru_lock);
61                 node = drm_mm_search_free_in_range(mm,
62                                         mem->num_pages, mem->page_alignment,
63                                         placement->fpfn, lpfn, 1);
64                 if (unlikely(node == NULL)) {
65                         spin_unlock(&glob->lru_lock);
66                         return 0;
67                 }
68                 node = drm_mm_get_block_atomic_range(node, mem->num_pages,
69                                                         mem->page_alignment,
70                                                         placement->fpfn,
71                                                         lpfn);
72                 spin_unlock(&glob->lru_lock);
73         } while (node == NULL);
74
75         mem->mm_node = node;
76         mem->start = node->start;
77         return 0;
78 }
79
80 static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
81                                 struct ttm_mem_reg *mem)
82 {
83         struct ttm_bo_global *glob = man->bdev->glob;
84
85         if (mem->mm_node) {
86                 spin_lock(&glob->lru_lock);
87                 drm_mm_put_block(mem->mm_node);
88                 spin_unlock(&glob->lru_lock);
89                 mem->mm_node = NULL;
90         }
91 }
92
93 static void ttm_bo_man_put_node_locked(struct ttm_mem_type_manager *man,
94                                        struct ttm_mem_reg *mem)
95 {
96         if (mem->mm_node) {
97                 drm_mm_put_block(mem->mm_node);
98                 mem->mm_node = NULL;
99         }
100 }
101
102 static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
103                            unsigned long p_size)
104 {
105         struct drm_mm *mm;
106         int ret;
107
108         mm = kzalloc(sizeof(*mm), GFP_KERNEL);
109         if (!mm)
110                 return -ENOMEM;
111
112         ret = drm_mm_init(mm, 0, p_size);
113         if (ret) {
114                 kfree(mm);
115                 return ret;
116         }
117
118         man->priv = mm;
119         return 0;
120 }
121
122 static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
123 {
124         struct ttm_bo_global *glob = man->bdev->glob;
125         struct drm_mm *mm = man->priv;
126         int ret = 0;
127
128         spin_lock(&glob->lru_lock);
129         if (drm_mm_clean(mm)) {
130                 drm_mm_takedown(mm);
131                 kfree(mm);
132                 man->priv = NULL;
133         } else
134                 ret = -EBUSY;
135         spin_unlock(&glob->lru_lock);
136         return ret;
137 }
138
139 static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
140                              const char *prefix)
141 {
142         struct ttm_bo_global *glob = man->bdev->glob;
143         struct drm_mm *mm = man->priv;
144
145         spin_lock(&glob->lru_lock);
146         drm_mm_debug_table(mm, prefix);
147         spin_unlock(&glob->lru_lock);
148 }
149
150 const struct ttm_mem_type_manager_func ttm_bo_manager_func = {
151         ttm_bo_man_init,
152         ttm_bo_man_takedown,
153         ttm_bo_man_get_node,
154         ttm_bo_man_put_node,
155         ttm_bo_man_put_node_locked,
156         ttm_bo_man_debug
157 };
158 EXPORT_SYMBOL(ttm_bo_manager_func);