xref: /openbmc/linux/drivers/gpu/drm/ttm/ttm_resource.c (revision 9e255e2b)
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)
31 {
32 	struct ttm_resource_manager *man =
33 		ttm_manager_type(bo->bdev, res->mem_type);
34 
35 	res->mm_node = NULL;
36 	if (!man->func || !man->func->alloc)
37 		return 0;
38 
39 	return man->func->alloc(man, bo, place, res);
40 }
41 
42 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource *res)
43 {
44 	struct ttm_resource_manager *man =
45 		ttm_manager_type(bo->bdev, res->mem_type);
46 
47 	if (man->func && man->func->free)
48 		man->func->free(man, res);
49 
50 	res->mm_node = NULL;
51 	res->mem_type = TTM_PL_SYSTEM;
52 }
53 EXPORT_SYMBOL(ttm_resource_free);
54 
55 /**
56  * ttm_resource_manager_init
57  *
58  * @man: memory manager object to init
59  * @p_size: size managed area in pages.
60  *
61  * Initialise core parts of a manager object.
62  */
63 void ttm_resource_manager_init(struct ttm_resource_manager *man,
64 			       unsigned long p_size)
65 {
66 	unsigned i;
67 
68 	spin_lock_init(&man->move_lock);
69 	man->size = p_size;
70 
71 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
72 		INIT_LIST_HEAD(&man->lru[i]);
73 	man->move = NULL;
74 }
75 EXPORT_SYMBOL(ttm_resource_manager_init);
76 
77 /*
78  * ttm_resource_manager_evict_all
79  *
80  * @bdev - device to use
81  * @man - manager to use
82  *
83  * Evict all the objects out of a memory manager until it is empty.
84  * Part of memory manager cleanup sequence.
85  */
86 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
87 				   struct ttm_resource_manager *man)
88 {
89 	struct ttm_operation_ctx ctx = {
90 		.interruptible = false,
91 		.no_wait_gpu = false,
92 		.force_alloc = true
93 	};
94 	struct dma_fence *fence;
95 	int ret;
96 	unsigned i;
97 
98 	/*
99 	 * Can't use standard list traversal since we're unlocking.
100 	 */
101 
102 	spin_lock(&bdev->lru_lock);
103 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
104 		while (!list_empty(&man->lru[i])) {
105 			spin_unlock(&bdev->lru_lock);
106 			ret = ttm_mem_evict_first(bdev, man, NULL, &ctx,
107 						  NULL);
108 			if (ret)
109 				return ret;
110 			spin_lock(&bdev->lru_lock);
111 		}
112 	}
113 	spin_unlock(&bdev->lru_lock);
114 
115 	spin_lock(&man->move_lock);
116 	fence = dma_fence_get(man->move);
117 	spin_unlock(&man->move_lock);
118 
119 	if (fence) {
120 		ret = dma_fence_wait(fence, false);
121 		dma_fence_put(fence);
122 		if (ret)
123 			return ret;
124 	}
125 
126 	return 0;
127 }
128 EXPORT_SYMBOL(ttm_resource_manager_evict_all);
129 
130 /**
131  * ttm_resource_manager_debug
132  *
133  * @man: manager type to dump.
134  * @p: printer to use for debug.
135  */
136 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
137 				struct drm_printer *p)
138 {
139 	drm_printf(p, "  use_type: %d\n", man->use_type);
140 	drm_printf(p, "  use_tt: %d\n", man->use_tt);
141 	drm_printf(p, "  size: %llu\n", man->size);
142 	if (man->func && man->func->debug)
143 		(*man->func->debug)(man, p);
144 }
145 EXPORT_SYMBOL(ttm_resource_manager_debug);
146