drm/ttm: remove split btw highmen and lowmem page
[linux-2.6.git] / drivers / gpu / drm / ttm / ttm_tt.c
1 /**************************************************************************
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
31 #include <linux/sched.h>
32 #include <linux/highmem.h>
33 #include <linux/pagemap.h>
34 #include <linux/shmem_fs.h>
35 #include <linux/file.h>
36 #include <linux/swap.h>
37 #include <linux/slab.h>
38 #include <linux/export.h>
39 #include "drm_cache.h"
40 #include "drm_mem_util.h"
41 #include "ttm/ttm_module.h"
42 #include "ttm/ttm_bo_driver.h"
43 #include "ttm/ttm_placement.h"
44 #include "ttm/ttm_page_alloc.h"
45
46 static int ttm_tt_swapin(struct ttm_tt *ttm);
47
48 /**
49  * Allocates storage for pointers to the pages that back the ttm.
50  */
51 static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
52 {
53         ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(*ttm->pages));
54         ttm->dma_address = drm_calloc_large(ttm->num_pages,
55                                             sizeof(*ttm->dma_address));
56 }
57
58 static void ttm_tt_free_page_directory(struct ttm_tt *ttm)
59 {
60         drm_free_large(ttm->pages);
61         ttm->pages = NULL;
62         drm_free_large(ttm->dma_address);
63         ttm->dma_address = NULL;
64 }
65
66 static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
67 {
68         struct page *p;
69         struct list_head h;
70         struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
71         int ret;
72
73         if (NULL == (p = ttm->pages[index])) {
74
75                 INIT_LIST_HEAD(&h);
76
77                 ret = ttm_get_pages(&h, ttm->page_flags, ttm->caching_state, 1,
78                                     &ttm->dma_address[index]);
79
80                 if (ret != 0)
81                         return NULL;
82
83                 p = list_first_entry(&h, struct page, lru);
84
85                 ret = ttm_mem_global_alloc_page(mem_glob, p, false, false);
86                 if (unlikely(ret != 0))
87                         goto out_err;
88
89                 ttm->pages[index] = p;
90         }
91         return p;
92 out_err:
93         put_page(p);
94         return NULL;
95 }
96
97 struct page *ttm_tt_get_page(struct ttm_tt *ttm, int index)
98 {
99         int ret;
100
101         if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
102                 ret = ttm_tt_swapin(ttm);
103                 if (unlikely(ret != 0))
104                         return NULL;
105         }
106         return __ttm_tt_get_page(ttm, index);
107 }
108
109 int ttm_tt_populate(struct ttm_tt *ttm)
110 {
111         struct page *page;
112         unsigned long i;
113         struct ttm_backend *be;
114         int ret;
115
116         if (ttm->state != tt_unpopulated)
117                 return 0;
118
119         if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
120                 ret = ttm_tt_swapin(ttm);
121                 if (unlikely(ret != 0))
122                         return ret;
123         }
124
125         be = ttm->be;
126
127         for (i = 0; i < ttm->num_pages; ++i) {
128                 page = __ttm_tt_get_page(ttm, i);
129                 if (!page)
130                         return -ENOMEM;
131         }
132
133         be->func->populate(be, ttm->num_pages, ttm->pages,
134                            ttm->dummy_read_page, ttm->dma_address);
135         ttm->state = tt_unbound;
136         return 0;
137 }
138 EXPORT_SYMBOL(ttm_tt_populate);
139
140 #ifdef CONFIG_X86
141 static inline int ttm_tt_set_page_caching(struct page *p,
142                                           enum ttm_caching_state c_old,
143                                           enum ttm_caching_state c_new)
144 {
145         int ret = 0;
146
147         if (PageHighMem(p))
148                 return 0;
149
150         if (c_old != tt_cached) {
151                 /* p isn't in the default caching state, set it to
152                  * writeback first to free its current memtype. */
153
154                 ret = set_pages_wb(p, 1);
155                 if (ret)
156                         return ret;
157         }
158
159         if (c_new == tt_wc)
160                 ret = set_memory_wc((unsigned long) page_address(p), 1);
161         else if (c_new == tt_uncached)
162                 ret = set_pages_uc(p, 1);
163
164         return ret;
165 }
166 #else /* CONFIG_X86 */
167 static inline int ttm_tt_set_page_caching(struct page *p,
168                                           enum ttm_caching_state c_old,
169                                           enum ttm_caching_state c_new)
170 {
171         return 0;
172 }
173 #endif /* CONFIG_X86 */
174
175 /*
176  * Change caching policy for the linear kernel map
177  * for range of pages in a ttm.
178  */
179
180 static int ttm_tt_set_caching(struct ttm_tt *ttm,
181                               enum ttm_caching_state c_state)
182 {
183         int i, j;
184         struct page *cur_page;
185         int ret;
186
187         if (ttm->caching_state == c_state)
188                 return 0;
189
190         if (ttm->state == tt_unpopulated) {
191                 /* Change caching but don't populate */
192                 ttm->caching_state = c_state;
193                 return 0;
194         }
195
196         if (ttm->caching_state == tt_cached)
197                 drm_clflush_pages(ttm->pages, ttm->num_pages);
198
199         for (i = 0; i < ttm->num_pages; ++i) {
200                 cur_page = ttm->pages[i];
201                 if (likely(cur_page != NULL)) {
202                         ret = ttm_tt_set_page_caching(cur_page,
203                                                       ttm->caching_state,
204                                                       c_state);
205                         if (unlikely(ret != 0))
206                                 goto out_err;
207                 }
208         }
209
210         ttm->caching_state = c_state;
211
212         return 0;
213
214 out_err:
215         for (j = 0; j < i; ++j) {
216                 cur_page = ttm->pages[j];
217                 if (likely(cur_page != NULL)) {
218                         (void)ttm_tt_set_page_caching(cur_page, c_state,
219                                                       ttm->caching_state);
220                 }
221         }
222
223         return ret;
224 }
225
226 int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
227 {
228         enum ttm_caching_state state;
229
230         if (placement & TTM_PL_FLAG_WC)
231                 state = tt_wc;
232         else if (placement & TTM_PL_FLAG_UNCACHED)
233                 state = tt_uncached;
234         else
235                 state = tt_cached;
236
237         return ttm_tt_set_caching(ttm, state);
238 }
239 EXPORT_SYMBOL(ttm_tt_set_placement_caching);
240
241 static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
242 {
243         int i;
244         unsigned count = 0;
245         struct list_head h;
246         struct page *cur_page;
247         struct ttm_backend *be = ttm->be;
248
249         INIT_LIST_HEAD(&h);
250
251         if (be)
252                 be->func->clear(be);
253         for (i = 0; i < ttm->num_pages; ++i) {
254
255                 cur_page = ttm->pages[i];
256                 ttm->pages[i] = NULL;
257                 if (cur_page) {
258                         if (page_count(cur_page) != 1)
259                                 printk(KERN_ERR TTM_PFX
260                                        "Erroneous page count. "
261                                        "Leaking pages.\n");
262                         ttm_mem_global_free_page(ttm->glob->mem_glob,
263                                                  cur_page);
264                         list_add(&cur_page->lru, &h);
265                         count++;
266                 }
267         }
268         ttm_put_pages(&h, count, ttm->page_flags, ttm->caching_state,
269                       ttm->dma_address);
270         ttm->state = tt_unpopulated;
271 }
272
273 void ttm_tt_destroy(struct ttm_tt *ttm)
274 {
275         struct ttm_backend *be;
276
277         if (unlikely(ttm == NULL))
278                 return;
279
280         be = ttm->be;
281         if (likely(be != NULL)) {
282                 be->func->destroy(be);
283                 ttm->be = NULL;
284         }
285
286         if (likely(ttm->pages != NULL)) {
287                 ttm_tt_free_alloced_pages(ttm);
288
289                 ttm_tt_free_page_directory(ttm);
290         }
291
292         if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
293             ttm->swap_storage)
294                 fput(ttm->swap_storage);
295
296         kfree(ttm);
297 }
298
299 struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
300                              uint32_t page_flags, struct page *dummy_read_page)
301 {
302         struct ttm_bo_driver *bo_driver = bdev->driver;
303         struct ttm_tt *ttm;
304
305         if (!bo_driver)
306                 return NULL;
307
308         ttm = kzalloc(sizeof(*ttm), GFP_KERNEL);
309         if (!ttm)
310                 return NULL;
311
312         ttm->glob = bdev->glob;
313         ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
314         ttm->caching_state = tt_cached;
315         ttm->page_flags = page_flags;
316
317         ttm->dummy_read_page = dummy_read_page;
318
319         ttm_tt_alloc_page_directory(ttm);
320         if (!ttm->pages) {
321                 ttm_tt_destroy(ttm);
322                 printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
323                 return NULL;
324         }
325         ttm->be = bo_driver->create_ttm_backend_entry(bdev);
326         if (!ttm->be) {
327                 ttm_tt_destroy(ttm);
328                 printk(KERN_ERR TTM_PFX "Failed creating ttm backend entry\n");
329                 return NULL;
330         }
331         ttm->state = tt_unpopulated;
332         return ttm;
333 }
334
335 void ttm_tt_unbind(struct ttm_tt *ttm)
336 {
337         int ret;
338         struct ttm_backend *be = ttm->be;
339
340         if (ttm->state == tt_bound) {
341                 ret = be->func->unbind(be);
342                 BUG_ON(ret);
343                 ttm->state = tt_unbound;
344         }
345 }
346
347 int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
348 {
349         int ret = 0;
350         struct ttm_backend *be;
351
352         if (!ttm)
353                 return -EINVAL;
354
355         if (ttm->state == tt_bound)
356                 return 0;
357
358         be = ttm->be;
359
360         ret = ttm_tt_populate(ttm);
361         if (ret)
362                 return ret;
363
364         ret = be->func->bind(be, bo_mem);
365         if (unlikely(ret != 0))
366                 return ret;
367
368         ttm->state = tt_bound;
369
370         return 0;
371 }
372 EXPORT_SYMBOL(ttm_tt_bind);
373
374 static int ttm_tt_swapin(struct ttm_tt *ttm)
375 {
376         struct address_space *swap_space;
377         struct file *swap_storage;
378         struct page *from_page;
379         struct page *to_page;
380         void *from_virtual;
381         void *to_virtual;
382         int i;
383         int ret = -ENOMEM;
384
385         swap_storage = ttm->swap_storage;
386         BUG_ON(swap_storage == NULL);
387
388         swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
389
390         for (i = 0; i < ttm->num_pages; ++i) {
391                 from_page = shmem_read_mapping_page(swap_space, i);
392                 if (IS_ERR(from_page)) {
393                         ret = PTR_ERR(from_page);
394                         goto out_err;
395                 }
396                 to_page = __ttm_tt_get_page(ttm, i);
397                 if (unlikely(to_page == NULL))
398                         goto out_err;
399
400                 preempt_disable();
401                 from_virtual = kmap_atomic(from_page, KM_USER0);
402                 to_virtual = kmap_atomic(to_page, KM_USER1);
403                 memcpy(to_virtual, from_virtual, PAGE_SIZE);
404                 kunmap_atomic(to_virtual, KM_USER1);
405                 kunmap_atomic(from_virtual, KM_USER0);
406                 preempt_enable();
407                 page_cache_release(from_page);
408         }
409
410         if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
411                 fput(swap_storage);
412         ttm->swap_storage = NULL;
413         ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
414
415         return 0;
416 out_err:
417         ttm_tt_free_alloced_pages(ttm);
418         return ret;
419 }
420
421 int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
422 {
423         struct address_space *swap_space;
424         struct file *swap_storage;
425         struct page *from_page;
426         struct page *to_page;
427         void *from_virtual;
428         void *to_virtual;
429         int i;
430         int ret = -ENOMEM;
431
432         BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
433         BUG_ON(ttm->caching_state != tt_cached);
434
435         if (!persistent_swap_storage) {
436                 swap_storage = shmem_file_setup("ttm swap",
437                                                 ttm->num_pages << PAGE_SHIFT,
438                                                 0);
439                 if (unlikely(IS_ERR(swap_storage))) {
440                         printk(KERN_ERR "Failed allocating swap storage.\n");
441                         return PTR_ERR(swap_storage);
442                 }
443         } else
444                 swap_storage = persistent_swap_storage;
445
446         swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
447
448         for (i = 0; i < ttm->num_pages; ++i) {
449                 from_page = ttm->pages[i];
450                 if (unlikely(from_page == NULL))
451                         continue;
452                 to_page = shmem_read_mapping_page(swap_space, i);
453                 if (unlikely(IS_ERR(to_page))) {
454                         ret = PTR_ERR(to_page);
455                         goto out_err;
456                 }
457                 preempt_disable();
458                 from_virtual = kmap_atomic(from_page, KM_USER0);
459                 to_virtual = kmap_atomic(to_page, KM_USER1);
460                 memcpy(to_virtual, from_virtual, PAGE_SIZE);
461                 kunmap_atomic(to_virtual, KM_USER1);
462                 kunmap_atomic(from_virtual, KM_USER0);
463                 preempt_enable();
464                 set_page_dirty(to_page);
465                 mark_page_accessed(to_page);
466                 page_cache_release(to_page);
467         }
468
469         ttm_tt_free_alloced_pages(ttm);
470         ttm->swap_storage = swap_storage;
471         ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
472         if (persistent_swap_storage)
473                 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
474
475         return 0;
476 out_err:
477         if (!persistent_swap_storage)
478                 fput(swap_storage);
479
480         return ret;
481 }