xref: /openbmc/linux/drivers/gpu/drm/ttm/ttm_tt.c (revision d9f6e12f)
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3  *
4  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  **************************************************************************/
28 /*
29  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30  */
31 
32 #define pr_fmt(fmt) "[TTM] " fmt
33 
34 #include <linux/sched.h>
35 #include <linux/pagemap.h>
36 #include <linux/shmem_fs.h>
37 #include <linux/file.h>
38 #include <drm/drm_cache.h>
39 #include <drm/ttm/ttm_bo_driver.h>
40 
41 /*
42  * Allocates a ttm structure for the given BO.
43  */
44 int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
45 {
46 	struct ttm_bo_device *bdev = bo->bdev;
47 	uint32_t page_flags = 0;
48 
49 	dma_resv_assert_held(bo->base.resv);
50 
51 	if (bo->ttm)
52 		return 0;
53 
54 	switch (bo->type) {
55 	case ttm_bo_type_device:
56 		if (zero_alloc)
57 			page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
58 		break;
59 	case ttm_bo_type_kernel:
60 		break;
61 	case ttm_bo_type_sg:
62 		page_flags |= TTM_PAGE_FLAG_SG;
63 		break;
64 	default:
65 		pr_err("Illegal buffer object type\n");
66 		return -EINVAL;
67 	}
68 
69 	bo->ttm = bdev->driver->ttm_tt_create(bo, page_flags);
70 	if (unlikely(bo->ttm == NULL))
71 		return -ENOMEM;
72 
73 	return 0;
74 }
75 
76 /*
77  * Allocates storage for pointers to the pages that back the ttm.
78  */
79 static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
80 {
81 	ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*),
82 			GFP_KERNEL | __GFP_ZERO);
83 	if (!ttm->pages)
84 		return -ENOMEM;
85 	return 0;
86 }
87 
88 static int ttm_dma_tt_alloc_page_directory(struct ttm_tt *ttm)
89 {
90 	ttm->pages = kvmalloc_array(ttm->num_pages,
91 				    sizeof(*ttm->pages) +
92 				    sizeof(*ttm->dma_address),
93 				    GFP_KERNEL | __GFP_ZERO);
94 	if (!ttm->pages)
95 		return -ENOMEM;
96 
97 	ttm->dma_address = (void *)(ttm->pages + ttm->num_pages);
98 	return 0;
99 }
100 
101 static int ttm_sg_tt_alloc_page_directory(struct ttm_tt *ttm)
102 {
103 	ttm->dma_address = kvmalloc_array(ttm->num_pages,
104 					  sizeof(*ttm->dma_address),
105 					  GFP_KERNEL | __GFP_ZERO);
106 	if (!ttm->dma_address)
107 		return -ENOMEM;
108 	return 0;
109 }
110 
111 void ttm_tt_destroy_common(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
112 {
113 	ttm_tt_unpopulate(bdev, ttm);
114 
115 	if (ttm->swap_storage)
116 		fput(ttm->swap_storage);
117 
118 	ttm->swap_storage = NULL;
119 }
120 EXPORT_SYMBOL(ttm_tt_destroy_common);
121 
122 void ttm_tt_destroy(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
123 {
124 	bdev->driver->ttm_tt_destroy(bdev, ttm);
125 }
126 
127 static void ttm_tt_init_fields(struct ttm_tt *ttm,
128 			       struct ttm_buffer_object *bo,
129 			       uint32_t page_flags,
130 			       enum ttm_caching caching)
131 {
132 	ttm->num_pages = PAGE_ALIGN(bo->base.size) >> PAGE_SHIFT;
133 	ttm->caching = ttm_cached;
134 	ttm->page_flags = page_flags;
135 	ttm->dma_address = NULL;
136 	ttm->swap_storage = NULL;
137 	ttm->sg = bo->sg;
138 	ttm->caching = caching;
139 }
140 
141 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
142 		uint32_t page_flags, enum ttm_caching caching)
143 {
144 	ttm_tt_init_fields(ttm, bo, page_flags, caching);
145 
146 	if (ttm_tt_alloc_page_directory(ttm)) {
147 		pr_err("Failed allocating page table\n");
148 		return -ENOMEM;
149 	}
150 	return 0;
151 }
152 EXPORT_SYMBOL(ttm_tt_init);
153 
154 void ttm_tt_fini(struct ttm_tt *ttm)
155 {
156 	if (ttm->pages)
157 		kvfree(ttm->pages);
158 	else
159 		kvfree(ttm->dma_address);
160 	ttm->pages = NULL;
161 	ttm->dma_address = NULL;
162 }
163 EXPORT_SYMBOL(ttm_tt_fini);
164 
165 int ttm_sg_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
166 		   uint32_t page_flags, enum ttm_caching caching)
167 {
168 	int ret;
169 
170 	ttm_tt_init_fields(ttm, bo, page_flags, caching);
171 
172 	if (page_flags & TTM_PAGE_FLAG_SG)
173 		ret = ttm_sg_tt_alloc_page_directory(ttm);
174 	else
175 		ret = ttm_dma_tt_alloc_page_directory(ttm);
176 	if (ret) {
177 		pr_err("Failed allocating page table\n");
178 		return -ENOMEM;
179 	}
180 	return 0;
181 }
182 EXPORT_SYMBOL(ttm_sg_tt_init);
183 
184 int ttm_tt_swapin(struct ttm_tt *ttm)
185 {
186 	struct address_space *swap_space;
187 	struct file *swap_storage;
188 	struct page *from_page;
189 	struct page *to_page;
190 	gfp_t gfp_mask;
191 	int i, ret;
192 
193 	swap_storage = ttm->swap_storage;
194 	BUG_ON(swap_storage == NULL);
195 
196 	swap_space = swap_storage->f_mapping;
197 	gfp_mask = mapping_gfp_mask(swap_space);
198 
199 	for (i = 0; i < ttm->num_pages; ++i) {
200 		from_page = shmem_read_mapping_page_gfp(swap_space, i,
201 							gfp_mask);
202 		if (IS_ERR(from_page)) {
203 			ret = PTR_ERR(from_page);
204 			goto out_err;
205 		}
206 		to_page = ttm->pages[i];
207 		if (unlikely(to_page == NULL)) {
208 			ret = -ENOMEM;
209 			goto out_err;
210 		}
211 
212 		copy_highpage(to_page, from_page);
213 		put_page(from_page);
214 	}
215 
216 	fput(swap_storage);
217 	ttm->swap_storage = NULL;
218 	ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
219 
220 	return 0;
221 
222 out_err:
223 	return ret;
224 }
225 
226 int ttm_tt_swapout(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
227 {
228 	struct address_space *swap_space;
229 	struct file *swap_storage;
230 	struct page *from_page;
231 	struct page *to_page;
232 	gfp_t gfp_mask;
233 	int i, ret;
234 
235 	swap_storage = shmem_file_setup("ttm swap",
236 					ttm->num_pages << PAGE_SHIFT,
237 					0);
238 	if (IS_ERR(swap_storage)) {
239 		pr_err("Failed allocating swap storage\n");
240 		return PTR_ERR(swap_storage);
241 	}
242 
243 	swap_space = swap_storage->f_mapping;
244 	gfp_mask = mapping_gfp_mask(swap_space);
245 
246 	for (i = 0; i < ttm->num_pages; ++i) {
247 		from_page = ttm->pages[i];
248 		if (unlikely(from_page == NULL))
249 			continue;
250 
251 		to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
252 		if (IS_ERR(to_page)) {
253 			ret = PTR_ERR(to_page);
254 			goto out_err;
255 		}
256 		copy_highpage(to_page, from_page);
257 		set_page_dirty(to_page);
258 		mark_page_accessed(to_page);
259 		put_page(to_page);
260 	}
261 
262 	ttm_tt_unpopulate(bdev, ttm);
263 	ttm->swap_storage = swap_storage;
264 	ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
265 
266 	return 0;
267 
268 out_err:
269 	fput(swap_storage);
270 
271 	return ret;
272 }
273 
274 static void ttm_tt_add_mapping(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
275 {
276 	pgoff_t i;
277 
278 	if (ttm->page_flags & TTM_PAGE_FLAG_SG)
279 		return;
280 
281 	for (i = 0; i < ttm->num_pages; ++i)
282 		ttm->pages[i]->mapping = bdev->dev_mapping;
283 }
284 
285 int ttm_tt_populate(struct ttm_bo_device *bdev,
286 		    struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
287 {
288 	int ret;
289 
290 	if (!ttm)
291 		return -EINVAL;
292 
293 	if (ttm_tt_is_populated(ttm))
294 		return 0;
295 
296 	if (bdev->driver->ttm_tt_populate)
297 		ret = bdev->driver->ttm_tt_populate(bdev, ttm, ctx);
298 	else
299 		ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
300 	if (ret)
301 		return ret;
302 
303 	ttm_tt_add_mapping(bdev, ttm);
304 	ttm->page_flags |= TTM_PAGE_FLAG_PRIV_POPULATED;
305 	if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
306 		ret = ttm_tt_swapin(ttm);
307 		if (unlikely(ret != 0)) {
308 			ttm_tt_unpopulate(bdev, ttm);
309 			return ret;
310 		}
311 	}
312 
313 	return 0;
314 }
315 EXPORT_SYMBOL(ttm_tt_populate);
316 
317 static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
318 {
319 	pgoff_t i;
320 	struct page **page = ttm->pages;
321 
322 	if (ttm->page_flags & TTM_PAGE_FLAG_SG)
323 		return;
324 
325 	for (i = 0; i < ttm->num_pages; ++i) {
326 		(*page)->mapping = NULL;
327 		(*page++)->index = 0;
328 	}
329 }
330 
331 void ttm_tt_unpopulate(struct ttm_bo_device *bdev,
332 		       struct ttm_tt *ttm)
333 {
334 	if (!ttm_tt_is_populated(ttm))
335 		return;
336 
337 	ttm_tt_clear_mapping(ttm);
338 	if (bdev->driver->ttm_tt_unpopulate)
339 		bdev->driver->ttm_tt_unpopulate(bdev, ttm);
340 	else
341 		ttm_pool_free(&bdev->pool, ttm);
342 	ttm->page_flags &= ~TTM_PAGE_FLAG_PRIV_POPULATED;
343 }
344