]> Git Repo - linux.git/blob - drivers/gpu/drm/ttm/ttm_tt.c
drm/ttm: merge ttm_dma_tt back into ttm_tt
[linux.git] / drivers / gpu / drm / ttm / ttm_tt.c
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 #include <drm/ttm/ttm_page_alloc.h>
41
42 /**
43  * Allocates a ttm structure for the given BO.
44  */
45 int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
46 {
47         struct ttm_bo_device *bdev = bo->bdev;
48         uint32_t page_flags = 0;
49
50         dma_resv_assert_held(bo->base.resv);
51
52         if (bo->ttm)
53                 return 0;
54
55         if (bdev->need_dma32)
56                 page_flags |= TTM_PAGE_FLAG_DMA32;
57
58         if (bdev->no_retry)
59                 page_flags |= TTM_PAGE_FLAG_NO_RETRY;
60
61         switch (bo->type) {
62         case ttm_bo_type_device:
63                 if (zero_alloc)
64                         page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
65                 break;
66         case ttm_bo_type_kernel:
67                 break;
68         case ttm_bo_type_sg:
69                 page_flags |= TTM_PAGE_FLAG_SG;
70                 break;
71         default:
72                 pr_err("Illegal buffer object type\n");
73                 return -EINVAL;
74         }
75
76         bo->ttm = bdev->driver->ttm_tt_create(bo, page_flags);
77         if (unlikely(bo->ttm == NULL))
78                 return -ENOMEM;
79
80         return 0;
81 }
82
83 /**
84  * Allocates storage for pointers to the pages that back the ttm.
85  */
86 static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
87 {
88         ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*),
89                         GFP_KERNEL | __GFP_ZERO);
90         if (!ttm->pages)
91                 return -ENOMEM;
92         return 0;
93 }
94
95 static int ttm_dma_tt_alloc_page_directory(struct ttm_tt *ttm)
96 {
97         ttm->pages = kvmalloc_array(ttm->num_pages,
98                                     sizeof(*ttm->pages) +
99                                     sizeof(*ttm->dma_address),
100                                     GFP_KERNEL | __GFP_ZERO);
101         if (!ttm->pages)
102                 return -ENOMEM;
103
104         ttm->dma_address = (void *)(ttm->pages + ttm->num_pages);
105         return 0;
106 }
107
108 static int ttm_sg_tt_alloc_page_directory(struct ttm_tt *ttm)
109 {
110         ttm->dma_address = kvmalloc_array(ttm->num_pages,
111                                           sizeof(*ttm->dma_address),
112                                           GFP_KERNEL | __GFP_ZERO);
113         if (!ttm->dma_address)
114                 return -ENOMEM;
115         return 0;
116 }
117
118 void ttm_tt_destroy_common(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
119 {
120         ttm_tt_unpopulate(bdev, ttm);
121
122         if (ttm->swap_storage)
123                 fput(ttm->swap_storage);
124
125         ttm->swap_storage = NULL;
126 }
127 EXPORT_SYMBOL(ttm_tt_destroy_common);
128
129 void ttm_tt_destroy(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
130 {
131         bdev->driver->ttm_tt_destroy(bdev, ttm);
132 }
133
134 static void ttm_tt_init_fields(struct ttm_tt *ttm,
135                                struct ttm_buffer_object *bo,
136                                uint32_t page_flags,
137                                enum ttm_caching caching)
138 {
139         ttm->num_pages = bo->num_pages;
140         ttm->caching = ttm_cached;
141         ttm->page_flags = page_flags;
142         ttm->dma_address = NULL;
143         ttm->swap_storage = NULL;
144         ttm->sg = bo->sg;
145         INIT_LIST_HEAD(&ttm->pages_list);
146         ttm->caching = caching;
147 }
148
149 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
150                 uint32_t page_flags, enum ttm_caching caching)
151 {
152         ttm_tt_init_fields(ttm, bo, page_flags, caching);
153
154         if (ttm_tt_alloc_page_directory(ttm)) {
155                 pr_err("Failed allocating page table\n");
156                 return -ENOMEM;
157         }
158         return 0;
159 }
160 EXPORT_SYMBOL(ttm_tt_init);
161
162 void ttm_tt_fini(struct ttm_tt *ttm)
163 {
164         if (ttm->pages)
165                 kvfree(ttm->pages);
166         else
167                 kvfree(ttm->dma_address);
168         ttm->pages = NULL;
169         ttm->dma_address = NULL;
170 }
171 EXPORT_SYMBOL(ttm_tt_fini);
172
173 int ttm_dma_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
174                     uint32_t page_flags, enum ttm_caching caching)
175 {
176         ttm_tt_init_fields(ttm, bo, page_flags, caching);
177
178         if (ttm_dma_tt_alloc_page_directory(ttm)) {
179                 pr_err("Failed allocating page table\n");
180                 return -ENOMEM;
181         }
182         return 0;
183 }
184 EXPORT_SYMBOL(ttm_dma_tt_init);
185
186 int ttm_sg_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
187                    uint32_t page_flags, enum ttm_caching caching)
188 {
189         int ret;
190
191         ttm_tt_init_fields(ttm, bo, page_flags, caching);
192
193         if (page_flags & TTM_PAGE_FLAG_SG)
194                 ret = ttm_sg_tt_alloc_page_directory(ttm);
195         else
196                 ret = ttm_dma_tt_alloc_page_directory(ttm);
197         if (ret) {
198                 pr_err("Failed allocating page table\n");
199                 return -ENOMEM;
200         }
201         return 0;
202 }
203 EXPORT_SYMBOL(ttm_sg_tt_init);
204
205 int ttm_tt_swapin(struct ttm_tt *ttm)
206 {
207         struct address_space *swap_space;
208         struct file *swap_storage;
209         struct page *from_page;
210         struct page *to_page;
211         gfp_t gfp_mask;
212         int i, ret;
213
214         swap_storage = ttm->swap_storage;
215         BUG_ON(swap_storage == NULL);
216
217         swap_space = swap_storage->f_mapping;
218         gfp_mask = mapping_gfp_mask(swap_space);
219         if (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY)
220                 gfp_mask |= __GFP_RETRY_MAYFAIL;
221
222         for (i = 0; i < ttm->num_pages; ++i) {
223                 from_page = shmem_read_mapping_page_gfp(swap_space, i,
224                                                         gfp_mask);
225                 if (IS_ERR(from_page)) {
226                         ret = PTR_ERR(from_page);
227                         goto out_err;
228                 }
229                 to_page = ttm->pages[i];
230                 if (unlikely(to_page == NULL)) {
231                         ret = -ENOMEM;
232                         goto out_err;
233                 }
234
235                 copy_highpage(to_page, from_page);
236                 put_page(from_page);
237         }
238
239         fput(swap_storage);
240         ttm->swap_storage = NULL;
241         ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
242
243         return 0;
244
245 out_err:
246         return ret;
247 }
248
249 int ttm_tt_swapout(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
250 {
251         struct address_space *swap_space;
252         struct file *swap_storage;
253         struct page *from_page;
254         struct page *to_page;
255         gfp_t gfp_mask;
256         int i, ret;
257
258         swap_storage = shmem_file_setup("ttm swap",
259                                         ttm->num_pages << PAGE_SHIFT,
260                                         0);
261         if (IS_ERR(swap_storage)) {
262                 pr_err("Failed allocating swap storage\n");
263                 return PTR_ERR(swap_storage);
264         }
265
266         swap_space = swap_storage->f_mapping;
267         gfp_mask = mapping_gfp_mask(swap_space);
268         if (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY)
269                 gfp_mask |= __GFP_RETRY_MAYFAIL;
270
271         for (i = 0; i < ttm->num_pages; ++i) {
272                 from_page = ttm->pages[i];
273                 if (unlikely(from_page == NULL))
274                         continue;
275
276                 to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
277                 if (IS_ERR(to_page)) {
278                         ret = PTR_ERR(to_page);
279                         goto out_err;
280                 }
281                 copy_highpage(to_page, from_page);
282                 set_page_dirty(to_page);
283                 mark_page_accessed(to_page);
284                 put_page(to_page);
285         }
286
287         ttm_tt_unpopulate(bdev, ttm);
288         ttm->swap_storage = swap_storage;
289         ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
290
291         return 0;
292
293 out_err:
294         fput(swap_storage);
295
296         return ret;
297 }
298
299 static void ttm_tt_add_mapping(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
300 {
301         pgoff_t i;
302
303         if (ttm->page_flags & TTM_PAGE_FLAG_SG)
304                 return;
305
306         for (i = 0; i < ttm->num_pages; ++i)
307                 ttm->pages[i]->mapping = bdev->dev_mapping;
308 }
309
310 int ttm_tt_populate(struct ttm_bo_device *bdev,
311                     struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
312 {
313         int ret;
314
315         if (!ttm)
316                 return -EINVAL;
317
318         if (ttm_tt_is_populated(ttm))
319                 return 0;
320
321         if (bdev->driver->ttm_tt_populate)
322                 ret = bdev->driver->ttm_tt_populate(bdev, ttm, ctx);
323         else
324                 ret = ttm_pool_populate(ttm, ctx);
325         if (ret)
326                 return ret;
327
328         ttm_tt_add_mapping(bdev, ttm);
329         ttm->page_flags |= TTM_PAGE_FLAG_PRIV_POPULATED;
330         if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
331                 ret = ttm_tt_swapin(ttm);
332                 if (unlikely(ret != 0)) {
333                         ttm_tt_unpopulate(bdev, ttm);
334                         return ret;
335                 }
336         }
337
338         return 0;
339 }
340 EXPORT_SYMBOL(ttm_tt_populate);
341
342 static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
343 {
344         pgoff_t i;
345         struct page **page = ttm->pages;
346
347         if (ttm->page_flags & TTM_PAGE_FLAG_SG)
348                 return;
349
350         for (i = 0; i < ttm->num_pages; ++i) {
351                 (*page)->mapping = NULL;
352                 (*page++)->index = 0;
353         }
354 }
355
356 void ttm_tt_unpopulate(struct ttm_bo_device *bdev,
357                        struct ttm_tt *ttm)
358 {
359         if (!ttm_tt_is_populated(ttm))
360                 return;
361
362         ttm_tt_clear_mapping(ttm);
363         if (bdev->driver->ttm_tt_unpopulate)
364                 bdev->driver->ttm_tt_unpopulate(bdev, ttm);
365         else
366                 ttm_pool_unpopulate(ttm);
367         ttm->page_flags &= ~TTM_PAGE_FLAG_PRIV_POPULATED;
368 }
This page took 0.058226 seconds and 4 git commands to generate.