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