ttm_tt.c revision 1.1.1.3 1 /* $NetBSD: ttm_tt.c,v 1.1.1.3 2018/08/27 01:34:59 riastradh Exp $ */
2
3 /**************************************************************************
4 *
5 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29 /*
30 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: ttm_tt.c,v 1.1.1.3 2018/08/27 01:34:59 riastradh Exp $");
35
36 #define pr_fmt(fmt) "[TTM] " fmt
37
38 #include <linux/sched.h>
39 #include <linux/highmem.h>
40 #include <linux/pagemap.h>
41 #include <linux/shmem_fs.h>
42 #include <linux/file.h>
43 #include <linux/swap.h>
44 #include <linux/slab.h>
45 #include <linux/export.h>
46 #include <drm/drm_cache.h>
47 #include <drm/drm_mem_util.h>
48 #include <drm/ttm/ttm_module.h>
49 #include <drm/ttm/ttm_bo_driver.h>
50 #include <drm/ttm/ttm_placement.h>
51 #include <drm/ttm/ttm_page_alloc.h>
52
53 /**
54 * Allocates storage for pointers to the pages that back the ttm.
55 */
56 static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
57 {
58 ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(void*));
59 }
60
61 static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
62 {
63 ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages,
64 sizeof(*ttm->ttm.pages) +
65 sizeof(*ttm->dma_address) +
66 sizeof(*ttm->cpu_address));
67 ttm->cpu_address = (void *) (ttm->ttm.pages + ttm->ttm.num_pages);
68 ttm->dma_address = (void *) (ttm->cpu_address + ttm->ttm.num_pages);
69 }
70
71 #ifdef CONFIG_X86
72 static inline int ttm_tt_set_page_caching(struct page *p,
73 enum ttm_caching_state c_old,
74 enum ttm_caching_state c_new)
75 {
76 int ret = 0;
77
78 if (PageHighMem(p))
79 return 0;
80
81 if (c_old != tt_cached) {
82 /* p isn't in the default caching state, set it to
83 * writeback first to free its current memtype. */
84
85 ret = set_pages_wb(p, 1);
86 if (ret)
87 return ret;
88 }
89
90 if (c_new == tt_wc)
91 ret = set_memory_wc((unsigned long) page_address(p), 1);
92 else if (c_new == tt_uncached)
93 ret = set_pages_uc(p, 1);
94
95 return ret;
96 }
97 #else /* CONFIG_X86 */
98 static inline int ttm_tt_set_page_caching(struct page *p,
99 enum ttm_caching_state c_old,
100 enum ttm_caching_state c_new)
101 {
102 return 0;
103 }
104 #endif /* CONFIG_X86 */
105
106 /*
107 * Change caching policy for the linear kernel map
108 * for range of pages in a ttm.
109 */
110
111 static int ttm_tt_set_caching(struct ttm_tt *ttm,
112 enum ttm_caching_state c_state)
113 {
114 int i, j;
115 struct page *cur_page;
116 int ret;
117
118 if (ttm->caching_state == c_state)
119 return 0;
120
121 if (ttm->state == tt_unpopulated) {
122 /* Change caching but don't populate */
123 ttm->caching_state = c_state;
124 return 0;
125 }
126
127 if (ttm->caching_state == tt_cached)
128 drm_clflush_pages(ttm->pages, ttm->num_pages);
129
130 for (i = 0; i < ttm->num_pages; ++i) {
131 cur_page = ttm->pages[i];
132 if (likely(cur_page != NULL)) {
133 ret = ttm_tt_set_page_caching(cur_page,
134 ttm->caching_state,
135 c_state);
136 if (unlikely(ret != 0))
137 goto out_err;
138 }
139 }
140
141 ttm->caching_state = c_state;
142
143 return 0;
144
145 out_err:
146 for (j = 0; j < i; ++j) {
147 cur_page = ttm->pages[j];
148 if (likely(cur_page != NULL)) {
149 (void)ttm_tt_set_page_caching(cur_page, c_state,
150 ttm->caching_state);
151 }
152 }
153
154 return ret;
155 }
156
157 int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
158 {
159 enum ttm_caching_state state;
160
161 if (placement & TTM_PL_FLAG_WC)
162 state = tt_wc;
163 else if (placement & TTM_PL_FLAG_UNCACHED)
164 state = tt_uncached;
165 else
166 state = tt_cached;
167
168 return ttm_tt_set_caching(ttm, state);
169 }
170 EXPORT_SYMBOL(ttm_tt_set_placement_caching);
171
172 void ttm_tt_destroy(struct ttm_tt *ttm)
173 {
174 if (unlikely(ttm == NULL))
175 return;
176
177 if (ttm->state == tt_bound) {
178 ttm_tt_unbind(ttm);
179 }
180
181 if (ttm->state == tt_unbound)
182 ttm_tt_unpopulate(ttm);
183
184 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
185 ttm->swap_storage)
186 fput(ttm->swap_storage);
187
188 ttm->swap_storage = NULL;
189 ttm->func->destroy(ttm);
190 }
191
192 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
193 unsigned long size, uint32_t page_flags,
194 struct page *dummy_read_page)
195 {
196 ttm->bdev = bdev;
197 ttm->glob = bdev->glob;
198 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
199 ttm->caching_state = tt_cached;
200 ttm->page_flags = page_flags;
201 ttm->dummy_read_page = dummy_read_page;
202 ttm->state = tt_unpopulated;
203 ttm->swap_storage = NULL;
204
205 ttm_tt_alloc_page_directory(ttm);
206 if (!ttm->pages) {
207 ttm_tt_destroy(ttm);
208 pr_err("Failed allocating page table\n");
209 return -ENOMEM;
210 }
211 return 0;
212 }
213 EXPORT_SYMBOL(ttm_tt_init);
214
215 void ttm_tt_fini(struct ttm_tt *ttm)
216 {
217 drm_free_large(ttm->pages);
218 ttm->pages = NULL;
219 }
220 EXPORT_SYMBOL(ttm_tt_fini);
221
222 int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
223 unsigned long size, uint32_t page_flags,
224 struct page *dummy_read_page)
225 {
226 struct ttm_tt *ttm = &ttm_dma->ttm;
227
228 ttm->bdev = bdev;
229 ttm->glob = bdev->glob;
230 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
231 ttm->caching_state = tt_cached;
232 ttm->page_flags = page_flags;
233 ttm->dummy_read_page = dummy_read_page;
234 ttm->state = tt_unpopulated;
235 ttm->swap_storage = NULL;
236
237 INIT_LIST_HEAD(&ttm_dma->pages_list);
238 ttm_dma_tt_alloc_page_directory(ttm_dma);
239 if (!ttm->pages) {
240 ttm_tt_destroy(ttm);
241 pr_err("Failed allocating page table\n");
242 return -ENOMEM;
243 }
244 return 0;
245 }
246 EXPORT_SYMBOL(ttm_dma_tt_init);
247
248 void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
249 {
250 struct ttm_tt *ttm = &ttm_dma->ttm;
251
252 drm_free_large(ttm->pages);
253 ttm->pages = NULL;
254 ttm_dma->cpu_address = NULL;
255 ttm_dma->dma_address = NULL;
256 }
257 EXPORT_SYMBOL(ttm_dma_tt_fini);
258
259 void ttm_tt_unbind(struct ttm_tt *ttm)
260 {
261 int ret;
262
263 if (ttm->state == tt_bound) {
264 ret = ttm->func->unbind(ttm);
265 BUG_ON(ret);
266 ttm->state = tt_unbound;
267 }
268 }
269
270 int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
271 {
272 int ret = 0;
273
274 if (!ttm)
275 return -EINVAL;
276
277 if (ttm->state == tt_bound)
278 return 0;
279
280 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
281 if (ret)
282 return ret;
283
284 ret = ttm->func->bind(ttm, bo_mem);
285 if (unlikely(ret != 0))
286 return ret;
287
288 ttm->state = tt_bound;
289
290 return 0;
291 }
292 EXPORT_SYMBOL(ttm_tt_bind);
293
294 int ttm_tt_swapin(struct ttm_tt *ttm)
295 {
296 struct address_space *swap_space;
297 struct file *swap_storage;
298 struct page *from_page;
299 struct page *to_page;
300 int i;
301 int ret = -ENOMEM;
302
303 swap_storage = ttm->swap_storage;
304 BUG_ON(swap_storage == NULL);
305
306 swap_space = file_inode(swap_storage)->i_mapping;
307
308 for (i = 0; i < ttm->num_pages; ++i) {
309 from_page = shmem_read_mapping_page(swap_space, i);
310 if (IS_ERR(from_page)) {
311 ret = PTR_ERR(from_page);
312 goto out_err;
313 }
314 to_page = ttm->pages[i];
315 if (unlikely(to_page == NULL))
316 goto out_err;
317
318 copy_highpage(to_page, from_page);
319 page_cache_release(from_page);
320 }
321
322 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
323 fput(swap_storage);
324 ttm->swap_storage = NULL;
325 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
326
327 return 0;
328 out_err:
329 return ret;
330 }
331
332 int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
333 {
334 struct address_space *swap_space;
335 struct file *swap_storage;
336 struct page *from_page;
337 struct page *to_page;
338 int i;
339 int ret = -ENOMEM;
340
341 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
342 BUG_ON(ttm->caching_state != tt_cached);
343
344 if (!persistent_swap_storage) {
345 swap_storage = shmem_file_setup("ttm swap",
346 ttm->num_pages << PAGE_SHIFT,
347 0);
348 if (IS_ERR(swap_storage)) {
349 pr_err("Failed allocating swap storage\n");
350 return PTR_ERR(swap_storage);
351 }
352 } else
353 swap_storage = persistent_swap_storage;
354
355 swap_space = file_inode(swap_storage)->i_mapping;
356
357 for (i = 0; i < ttm->num_pages; ++i) {
358 from_page = ttm->pages[i];
359 if (unlikely(from_page == NULL))
360 continue;
361 to_page = shmem_read_mapping_page(swap_space, i);
362 if (IS_ERR(to_page)) {
363 ret = PTR_ERR(to_page);
364 goto out_err;
365 }
366 copy_highpage(to_page, from_page);
367 set_page_dirty(to_page);
368 mark_page_accessed(to_page);
369 page_cache_release(to_page);
370 }
371
372 ttm_tt_unpopulate(ttm);
373 ttm->swap_storage = swap_storage;
374 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
375 if (persistent_swap_storage)
376 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
377
378 return 0;
379 out_err:
380 if (!persistent_swap_storage)
381 fput(swap_storage);
382
383 return ret;
384 }
385
386 static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
387 {
388 pgoff_t i;
389 struct page **page = ttm->pages;
390
391 if (ttm->page_flags & TTM_PAGE_FLAG_SG)
392 return;
393
394 for (i = 0; i < ttm->num_pages; ++i) {
395 (*page)->mapping = NULL;
396 (*page++)->index = 0;
397 }
398 }
399
400 void ttm_tt_unpopulate(struct ttm_tt *ttm)
401 {
402 if (ttm->state == tt_unpopulated)
403 return;
404
405 ttm_tt_clear_mapping(ttm);
406 ttm->bdev->driver->ttm_tt_unpopulate(ttm);
407 }
408