65451e1bc30332845f31f9b3555b4c7b761eeb96
[linux-2.6-microblaze.git] / drivers / gpu / drm / ttm / ttm_resource.c
1 /*
2  * Copyright 2020 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Christian König
23  */
24
25 #include <drm/ttm/ttm_resource.h>
26 #include <drm/ttm/ttm_bo_driver.h>
27
28 int ttm_resource_alloc(struct ttm_buffer_object *bo,
29                        const struct ttm_place *place,
30                        struct ttm_resource **res_ptr)
31 {
32         struct ttm_resource_manager *man =
33                 ttm_manager_type(bo->bdev, place->mem_type);
34         struct ttm_resource *res;
35         int r;
36
37         res = kmalloc(sizeof(*res), GFP_KERNEL);
38         if (!res)
39                 return -ENOMEM;
40
41         res->mm_node = NULL;
42         res->start = 0;
43         res->num_pages = PFN_UP(bo->base.size);
44         res->mem_type = place->mem_type;
45         res->placement = place->flags;
46         res->bus.addr = NULL;
47         res->bus.offset = 0;
48         res->bus.is_iomem = false;
49         res->bus.caching = ttm_cached;
50         r = man->func->alloc(man, bo, place, res);
51         if (r) {
52                 kfree(res);
53                 return r;
54         }
55
56         *res_ptr = res;
57         return 0;
58 }
59
60 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
61 {
62         struct ttm_resource_manager *man;
63
64         if (!*res)
65                 return;
66
67         man = ttm_manager_type(bo->bdev, (*res)->mem_type);
68         man->func->free(man, *res);
69         kfree(*res);
70         *res = NULL;
71 }
72 EXPORT_SYMBOL(ttm_resource_free);
73
74 /**
75  * ttm_resource_manager_init
76  *
77  * @man: memory manager object to init
78  * @p_size: size managed area in pages.
79  *
80  * Initialise core parts of a manager object.
81  */
82 void ttm_resource_manager_init(struct ttm_resource_manager *man,
83                                unsigned long p_size)
84 {
85         unsigned i;
86
87         spin_lock_init(&man->move_lock);
88         man->size = p_size;
89
90         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
91                 INIT_LIST_HEAD(&man->lru[i]);
92         man->move = NULL;
93 }
94 EXPORT_SYMBOL(ttm_resource_manager_init);
95
96 /*
97  * ttm_resource_manager_evict_all
98  *
99  * @bdev - device to use
100  * @man - manager to use
101  *
102  * Evict all the objects out of a memory manager until it is empty.
103  * Part of memory manager cleanup sequence.
104  */
105 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
106                                    struct ttm_resource_manager *man)
107 {
108         struct ttm_operation_ctx ctx = {
109                 .interruptible = false,
110                 .no_wait_gpu = false,
111                 .force_alloc = true
112         };
113         struct dma_fence *fence;
114         int ret;
115         unsigned i;
116
117         /*
118          * Can't use standard list traversal since we're unlocking.
119          */
120
121         spin_lock(&bdev->lru_lock);
122         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
123                 while (!list_empty(&man->lru[i])) {
124                         spin_unlock(&bdev->lru_lock);
125                         ret = ttm_mem_evict_first(bdev, man, NULL, &ctx,
126                                                   NULL);
127                         if (ret)
128                                 return ret;
129                         spin_lock(&bdev->lru_lock);
130                 }
131         }
132         spin_unlock(&bdev->lru_lock);
133
134         spin_lock(&man->move_lock);
135         fence = dma_fence_get(man->move);
136         spin_unlock(&man->move_lock);
137
138         if (fence) {
139                 ret = dma_fence_wait(fence, false);
140                 dma_fence_put(fence);
141                 if (ret)
142                         return ret;
143         }
144
145         return 0;
146 }
147 EXPORT_SYMBOL(ttm_resource_manager_evict_all);
148
149 /**
150  * ttm_resource_manager_debug
151  *
152  * @man: manager type to dump.
153  * @p: printer to use for debug.
154  */
155 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
156                                 struct drm_printer *p)
157 {
158         drm_printf(p, "  use_type: %d\n", man->use_type);
159         drm_printf(p, "  use_tt: %d\n", man->use_tt);
160         drm_printf(p, "  size: %llu\n", man->size);
161         if (man->func->debug)
162                 man->func->debug(man, p);
163 }
164 EXPORT_SYMBOL(ttm_resource_manager_debug);