blob: 58e1fa14fe3a021239785b36d742d83877a1e6ad [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
2 *
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020031#include <linux/sched.h>
32#include <linux/highmem.h>
33#include <linux/pagemap.h>
Hugh Dickins3142b652011-06-27 16:18:17 -070034#include <linux/shmem_fs.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020035#include <linux/file.h>
36#include <linux/swap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040038#include <linux/export.h>
Dave Airliec9c97b82009-08-27 09:53:47 +100039#include "drm_cache.h"
Dave Airlie72e942d2010-03-09 06:33:26 +000040#include "drm_mem_util.h"
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020041#include "ttm/ttm_module.h"
42#include "ttm/ttm_bo_driver.h"
43#include "ttm/ttm_placement.h"
Pauli Nieminen1403b1a2010-04-01 12:44:57 +000044#include "ttm/ttm_page_alloc.h"
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020045
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020046/**
47 * Allocates storage for pointers to the pages that back the ttm.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020048 */
49static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
50{
Jerome Glisse8e7e7052011-11-09 17:15:26 -050051 ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(void*));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020052}
53
Jerome Glisse8e7e7052011-11-09 17:15:26 -050054static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020055{
Jerome Glisse8e7e7052011-11-09 17:15:26 -050056 ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages, sizeof(void*));
57 ttm->dma_address = drm_calloc_large(ttm->ttm.num_pages,
58 sizeof(*ttm->dma_address));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020059}
60
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020061#ifdef CONFIG_X86
62static inline int ttm_tt_set_page_caching(struct page *p,
Francisco Jerezf0e2f382010-02-20 07:30:15 +100063 enum ttm_caching_state c_old,
64 enum ttm_caching_state c_new)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020065{
Francisco Jerezdb78e272010-01-12 18:49:43 +010066 int ret = 0;
67
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020068 if (PageHighMem(p))
69 return 0;
70
Francisco Jerezf0e2f382010-02-20 07:30:15 +100071 if (c_old != tt_cached) {
Francisco Jerezdb78e272010-01-12 18:49:43 +010072 /* p isn't in the default caching state, set it to
73 * writeback first to free its current memtype. */
74
75 ret = set_pages_wb(p, 1);
76 if (ret)
77 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020078 }
Francisco Jerezdb78e272010-01-12 18:49:43 +010079
Francisco Jerezf0e2f382010-02-20 07:30:15 +100080 if (c_new == tt_wc)
Francisco Jerezdb78e272010-01-12 18:49:43 +010081 ret = set_memory_wc((unsigned long) page_address(p), 1);
Francisco Jerezf0e2f382010-02-20 07:30:15 +100082 else if (c_new == tt_uncached)
Francisco Jerezdb78e272010-01-12 18:49:43 +010083 ret = set_pages_uc(p, 1);
84
85 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020086}
87#else /* CONFIG_X86 */
88static inline int ttm_tt_set_page_caching(struct page *p,
Francisco Jerezf0e2f382010-02-20 07:30:15 +100089 enum ttm_caching_state c_old,
90 enum ttm_caching_state c_new)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020091{
92 return 0;
93}
94#endif /* CONFIG_X86 */
95
96/*
97 * Change caching policy for the linear kernel map
98 * for range of pages in a ttm.
99 */
100
101static int ttm_tt_set_caching(struct ttm_tt *ttm,
102 enum ttm_caching_state c_state)
103{
104 int i, j;
105 struct page *cur_page;
106 int ret;
107
108 if (ttm->caching_state == c_state)
109 return 0;
110
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000111 if (ttm->state == tt_unpopulated) {
112 /* Change caching but don't populate */
113 ttm->caching_state = c_state;
114 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200115 }
116
117 if (ttm->caching_state == tt_cached)
Dave Airliec9c97b82009-08-27 09:53:47 +1000118 drm_clflush_pages(ttm->pages, ttm->num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200119
120 for (i = 0; i < ttm->num_pages; ++i) {
121 cur_page = ttm->pages[i];
122 if (likely(cur_page != NULL)) {
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000123 ret = ttm_tt_set_page_caching(cur_page,
124 ttm->caching_state,
125 c_state);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200126 if (unlikely(ret != 0))
127 goto out_err;
128 }
129 }
130
131 ttm->caching_state = c_state;
132
133 return 0;
134
135out_err:
136 for (j = 0; j < i; ++j) {
137 cur_page = ttm->pages[j];
138 if (likely(cur_page != NULL)) {
Francisco Jerezf0e2f382010-02-20 07:30:15 +1000139 (void)ttm_tt_set_page_caching(cur_page, c_state,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200140 ttm->caching_state);
141 }
142 }
143
144 return ret;
145}
146
147int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
148{
149 enum ttm_caching_state state;
150
151 if (placement & TTM_PL_FLAG_WC)
152 state = tt_wc;
153 else if (placement & TTM_PL_FLAG_UNCACHED)
154 state = tt_uncached;
155 else
156 state = tt_cached;
157
158 return ttm_tt_set_caching(ttm, state);
159}
Dave Airliedf67bed2009-10-30 13:31:26 +1000160EXPORT_SYMBOL(ttm_tt_set_placement_caching);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200161
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200162void ttm_tt_destroy(struct ttm_tt *ttm)
163{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200164 if (unlikely(ttm == NULL))
165 return;
166
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400167 if (ttm->state == tt_bound) {
168 ttm_tt_unbind(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200169 }
170
171 if (likely(ttm->pages != NULL)) {
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400172 ttm->bdev->driver->ttm_tt_unpopulate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200173 }
174
Jan Engelhardt5df23972011-04-04 01:25:18 +0200175 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200176 ttm->swap_storage)
177 fput(ttm->swap_storage);
178
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400179 ttm->swap_storage = NULL;
180 ttm->func->destroy(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200181}
182
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400183int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
184 unsigned long size, uint32_t page_flags,
185 struct page *dummy_read_page)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200186{
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400187 ttm->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200188 ttm->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200189 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200190 ttm->caching_state = tt_cached;
191 ttm->page_flags = page_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200192 ttm->dummy_read_page = dummy_read_page;
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400193 ttm->state = tt_unpopulated;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200194
195 ttm_tt_alloc_page_directory(ttm);
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500196 if (!ttm->pages) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200197 ttm_tt_destroy(ttm);
198 printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400199 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200200 }
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400201 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200202}
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400203EXPORT_SYMBOL(ttm_tt_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200204
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500205void ttm_tt_fini(struct ttm_tt *ttm)
206{
207 drm_free_large(ttm->pages);
208 ttm->pages = NULL;
209}
210EXPORT_SYMBOL(ttm_tt_fini);
211
212int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
213 unsigned long size, uint32_t page_flags,
214 struct page *dummy_read_page)
215{
216 struct ttm_tt *ttm = &ttm_dma->ttm;
217
218 ttm->bdev = bdev;
219 ttm->glob = bdev->glob;
220 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
221 ttm->caching_state = tt_cached;
222 ttm->page_flags = page_flags;
223 ttm->dummy_read_page = dummy_read_page;
224 ttm->state = tt_unpopulated;
225
226 INIT_LIST_HEAD(&ttm_dma->pages_list);
227 ttm_dma_tt_alloc_page_directory(ttm_dma);
228 if (!ttm->pages || !ttm_dma->dma_address) {
229 ttm_tt_destroy(ttm);
230 printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
231 return -ENOMEM;
232 }
233 return 0;
234}
235EXPORT_SYMBOL(ttm_dma_tt_init);
236
237void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
238{
239 struct ttm_tt *ttm = &ttm_dma->ttm;
240
241 drm_free_large(ttm->pages);
242 ttm->pages = NULL;
243 drm_free_large(ttm_dma->dma_address);
244 ttm_dma->dma_address = NULL;
245}
246EXPORT_SYMBOL(ttm_dma_tt_fini);
247
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200248void ttm_tt_unbind(struct ttm_tt *ttm)
249{
250 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200251
252 if (ttm->state == tt_bound) {
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400253 ret = ttm->func->unbind(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200254 BUG_ON(ret);
255 ttm->state = tt_unbound;
256 }
257}
258
259int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
260{
261 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200262
263 if (!ttm)
264 return -EINVAL;
265
266 if (ttm->state == tt_bound)
267 return 0;
268
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400269 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200270 if (ret)
271 return ret;
272
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400273 ret = ttm->func->bind(ttm, bo_mem);
Thomas Hellstrom7dcebb52010-10-29 10:46:49 +0200274 if (unlikely(ret != 0))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200275 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200276
277 ttm->state = tt_bound;
278
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200279 return 0;
280}
281EXPORT_SYMBOL(ttm_tt_bind);
282
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400283int ttm_tt_swapin(struct ttm_tt *ttm)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200284{
285 struct address_space *swap_space;
286 struct file *swap_storage;
287 struct page *from_page;
288 struct page *to_page;
289 void *from_virtual;
290 void *to_virtual;
291 int i;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100292 int ret = -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200293
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200294 swap_storage = ttm->swap_storage;
295 BUG_ON(swap_storage == NULL);
296
297 swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
298
299 for (i = 0; i < ttm->num_pages; ++i) {
Hugh Dickins3142b652011-06-27 16:18:17 -0700300 from_page = shmem_read_mapping_page(swap_space, i);
Maarten Maathuis290e55052010-02-20 03:22:21 +0100301 if (IS_ERR(from_page)) {
302 ret = PTR_ERR(from_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200303 goto out_err;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100304 }
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400305 to_page = ttm->pages[i];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200306 if (unlikely(to_page == NULL))
307 goto out_err;
308
309 preempt_disable();
310 from_virtual = kmap_atomic(from_page, KM_USER0);
311 to_virtual = kmap_atomic(to_page, KM_USER1);
312 memcpy(to_virtual, from_virtual, PAGE_SIZE);
313 kunmap_atomic(to_virtual, KM_USER1);
314 kunmap_atomic(from_virtual, KM_USER0);
315 preempt_enable();
316 page_cache_release(from_page);
317 }
318
Jan Engelhardt5df23972011-04-04 01:25:18 +0200319 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200320 fput(swap_storage);
321 ttm->swap_storage = NULL;
322 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
323
324 return 0;
325out_err:
Maarten Maathuis290e55052010-02-20 03:22:21 +0100326 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200327}
328
Jan Engelhardt5df23972011-04-04 01:25:18 +0200329int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200330{
331 struct address_space *swap_space;
332 struct file *swap_storage;
333 struct page *from_page;
334 struct page *to_page;
335 void *from_virtual;
336 void *to_virtual;
337 int i;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100338 int ret = -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200339
340 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
341 BUG_ON(ttm->caching_state != tt_cached);
342
Jan Engelhardt5df23972011-04-04 01:25:18 +0200343 if (!persistent_swap_storage) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200344 swap_storage = shmem_file_setup("ttm swap",
345 ttm->num_pages << PAGE_SHIFT,
346 0);
347 if (unlikely(IS_ERR(swap_storage))) {
348 printk(KERN_ERR "Failed allocating swap storage.\n");
Maarten Maathuis290e55052010-02-20 03:22:21 +0100349 return PTR_ERR(swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200350 }
351 } else
Jan Engelhardt5df23972011-04-04 01:25:18 +0200352 swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200353
354 swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
355
356 for (i = 0; i < ttm->num_pages; ++i) {
357 from_page = ttm->pages[i];
358 if (unlikely(from_page == NULL))
359 continue;
Hugh Dickins3142b652011-06-27 16:18:17 -0700360 to_page = shmem_read_mapping_page(swap_space, i);
Maarten Maathuis290e55052010-02-20 03:22:21 +0100361 if (unlikely(IS_ERR(to_page))) {
362 ret = PTR_ERR(to_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200363 goto out_err;
Maarten Maathuis290e55052010-02-20 03:22:21 +0100364 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200365 preempt_disable();
366 from_virtual = kmap_atomic(from_page, KM_USER0);
367 to_virtual = kmap_atomic(to_page, KM_USER1);
368 memcpy(to_virtual, from_virtual, PAGE_SIZE);
369 kunmap_atomic(to_virtual, KM_USER1);
370 kunmap_atomic(from_virtual, KM_USER0);
371 preempt_enable();
372 set_page_dirty(to_page);
373 mark_page_accessed(to_page);
374 page_cache_release(to_page);
375 }
376
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400377 ttm->bdev->driver->ttm_tt_unpopulate(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200378 ttm->swap_storage = swap_storage;
379 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
Jan Engelhardt5df23972011-04-04 01:25:18 +0200380 if (persistent_swap_storage)
381 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200382
383 return 0;
384out_err:
Jan Engelhardt5df23972011-04-04 01:25:18 +0200385 if (!persistent_swap_storage)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200386 fput(swap_storage);
387
Maarten Maathuis290e55052010-02-20 03:22:21 +0100388 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200389}