Subversion Repositories Kolibri OS

Rev

Rev 5078 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4075 Serge 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 
29
 */
30
 
31
#define pr_fmt(fmt) "[TTM] " fmt
32
 
33
#include 
34
 
35
#include 
36
//#include 
37
//#include 
38
#include 
39
//#include 
40
//#include 
41
#include 
42
#include 
43
//#include 
44
#include 
45
#include 
46
#include 
47
#include 
48
#include 
49
 
50
/**
51
 * Allocates storage for pointers to the pages that back the ttm.
52
 */
53
static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
54
{
55
	ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(void*));
56
}
57
 
58
static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
59
{
60
	ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages, sizeof(void*));
61
	ttm->dma_address = drm_calloc_large(ttm->ttm.num_pages,
62
					    sizeof(*ttm->dma_address));
63
}
64
 
65
#ifdef CONFIG_X86
66
static inline int ttm_tt_set_page_caching(struct page *p,
67
					  enum ttm_caching_state c_old,
68
					  enum ttm_caching_state c_new)
69
{
70
	int ret = 0;
71
 
72
	if (PageHighMem(p))
73
		return 0;
74
 
75
	if (c_old != tt_cached) {
76
		/* p isn't in the default caching state, set it to
77
		 * writeback first to free its current memtype. */
78
 
79
		ret = set_pages_wb(p, 1);
80
		if (ret)
81
			return ret;
82
	}
83
 
84
	if (c_new == tt_wc)
85
		ret = set_memory_wc((unsigned long) page_address(p), 1);
86
	else if (c_new == tt_uncached)
87
		ret = set_pages_uc(p, 1);
88
 
89
	return ret;
90
}
91
#else /* CONFIG_X86 */
92
static inline int ttm_tt_set_page_caching(struct page *p,
93
					  enum ttm_caching_state c_old,
94
					  enum ttm_caching_state c_new)
95
{
96
	return 0;
97
}
98
#endif /* CONFIG_X86 */
99
 
100
/*
101
 * Change caching policy for the linear kernel map
102
 * for range of pages in a ttm.
103
 */
104
 
105
static int ttm_tt_set_caching(struct ttm_tt *ttm,
106
			      enum ttm_caching_state c_state)
107
{
108
	int i, j;
109
	struct page *cur_page;
110
	int ret;
111
 
112
	if (ttm->caching_state == c_state)
113
		return 0;
114
 
115
	if (ttm->state == tt_unpopulated) {
116
		/* Change caching but don't populate */
117
		ttm->caching_state = c_state;
118
		return 0;
119
	}
120
 
121
//   if (ttm->caching_state == tt_cached)
122
//       drm_clflush_pages(ttm->pages, ttm->num_pages);
123
 
124
	for (i = 0; i < ttm->num_pages; ++i) {
125
		cur_page = ttm->pages[i];
126
		if (likely(cur_page != NULL)) {
127
			ret = ttm_tt_set_page_caching(cur_page,
128
						      ttm->caching_state,
129
						      c_state);
130
			if (unlikely(ret != 0))
131
				goto out_err;
132
		}
133
	}
134
 
135
	ttm->caching_state = c_state;
136
 
137
	return 0;
138
 
139
out_err:
140
	for (j = 0; j < i; ++j) {
141
		cur_page = ttm->pages[j];
142
		if (likely(cur_page != NULL)) {
143
			(void)ttm_tt_set_page_caching(cur_page, c_state,
144
						      ttm->caching_state);
145
		}
146
	}
147
 
148
	return ret;
149
}
150
 
151
int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
152
{
153
	enum ttm_caching_state state;
154
 
155
	if (placement & TTM_PL_FLAG_WC)
156
		state = tt_wc;
157
	else if (placement & TTM_PL_FLAG_UNCACHED)
158
		state = tt_uncached;
159
	else
160
		state = tt_cached;
161
 
162
	return ttm_tt_set_caching(ttm, state);
163
}
164
EXPORT_SYMBOL(ttm_tt_set_placement_caching);
165
 
166
void ttm_tt_destroy(struct ttm_tt *ttm)
167
{
168
	if (unlikely(ttm == NULL))
169
		return;
170
 
171
	if (ttm->state == tt_bound) {
172
		ttm_tt_unbind(ttm);
173
	}
174
 
175
	if (likely(ttm->pages != NULL)) {
176
		ttm->bdev->driver->ttm_tt_unpopulate(ttm);
177
	}
178
 
179
//   if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
180
//       ttm->swap_storage)
181
//       fput(ttm->swap_storage);
182
 
183
	ttm->swap_storage = NULL;
184
	ttm->func->destroy(ttm);
185
}
186
 
187
int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
188
		unsigned long size, uint32_t page_flags,
189
		struct page *dummy_read_page)
190
{
191
	ttm->bdev = bdev;
192
	ttm->glob = bdev->glob;
193
	ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
194
	ttm->caching_state = tt_cached;
195
	ttm->page_flags = page_flags;
196
	ttm->dummy_read_page = dummy_read_page;
197
	ttm->state = tt_unpopulated;
198
	ttm->swap_storage = NULL;
199
 
200
	ttm_tt_alloc_page_directory(ttm);
201
	if (!ttm->pages) {
202
		ttm_tt_destroy(ttm);
203
        printf("Failed allocating page table\n");
204
		return -ENOMEM;
205
	}
206
	return 0;
207
}
208
EXPORT_SYMBOL(ttm_tt_init);
209
 
210
void ttm_tt_fini(struct ttm_tt *ttm)
211
{
212
	drm_free_large(ttm->pages);
213
	ttm->pages = NULL;
214
}
215
EXPORT_SYMBOL(ttm_tt_fini);
216
 
217
int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
218
		unsigned long size, uint32_t page_flags,
219
		struct page *dummy_read_page)
220
{
221
	struct ttm_tt *ttm = &ttm_dma->ttm;
222
 
223
	ttm->bdev = bdev;
224
	ttm->glob = bdev->glob;
225
	ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
226
	ttm->caching_state = tt_cached;
227
	ttm->page_flags = page_flags;
228
	ttm->dummy_read_page = dummy_read_page;
229
	ttm->state = tt_unpopulated;
230
	ttm->swap_storage = NULL;
231
 
232
	INIT_LIST_HEAD(&ttm_dma->pages_list);
233
	ttm_dma_tt_alloc_page_directory(ttm_dma);
234
	if (!ttm->pages || !ttm_dma->dma_address) {
235
		ttm_tt_destroy(ttm);
236
        printf("Failed allocating page table\n");
237
		return -ENOMEM;
238
	}
239
	return 0;
240
}
241
EXPORT_SYMBOL(ttm_dma_tt_init);
242
 
243
void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
244
{
245
	struct ttm_tt *ttm = &ttm_dma->ttm;
246
 
247
	drm_free_large(ttm->pages);
248
	ttm->pages = NULL;
249
	drm_free_large(ttm_dma->dma_address);
250
	ttm_dma->dma_address = NULL;
251
}
252
EXPORT_SYMBOL(ttm_dma_tt_fini);
253
 
254
void ttm_tt_unbind(struct ttm_tt *ttm)
255
{
256
	int ret;
257
 
258
	if (ttm->state == tt_bound) {
259
		ret = ttm->func->unbind(ttm);
260
		BUG_ON(ret);
261
		ttm->state = tt_unbound;
262
	}
263
}
264
 
265
#if 0
266
 
267
int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
268
{
269
	int ret = 0;
270
 
271
	if (!ttm)
272
		return -EINVAL;
273
 
274
	if (ttm->state == tt_bound)
275
		return 0;
276
 
277
	ret = ttm->bdev->driver->ttm_tt_populate(ttm);
278
	if (ret)
279
		return ret;
280
 
281
	ret = ttm->func->bind(ttm, bo_mem);
282
	if (unlikely(ret != 0))
283
		return ret;
284
 
285
	ttm->state = tt_bound;
286
 
287
	return 0;
288
}
289
EXPORT_SYMBOL(ttm_tt_bind);
290
#endif
291
 
292
/*
293
int ttm_tt_swapin(struct ttm_tt *ttm)
294
{
295
	struct address_space *swap_space;
296
	struct file *swap_storage;
297
	struct page *from_page;
298
	struct page *to_page;
299
	int i;
300
	int ret = -ENOMEM;
301
 
302
	swap_storage = ttm->swap_storage;
303
	BUG_ON(swap_storage == NULL);
304
 
305
	swap_space = file_inode(swap_storage)->i_mapping;
306
 
307
	for (i = 0; i < ttm->num_pages; ++i) {
308
		from_page = shmem_read_mapping_page(swap_space, i);
309
		if (IS_ERR(from_page)) {
310
			ret = PTR_ERR(from_page);
311
			goto out_err;
312
		}
313
		to_page = ttm->pages[i];
314
		if (unlikely(to_page == NULL))
315
			goto out_err;
316
 
317
		copy_highpage(to_page, from_page);
318
		page_cache_release(from_page);
319
	}
320
 
321
	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
322
		fput(swap_storage);
323
	ttm->swap_storage = NULL;
324
	ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
325
 
326
	return 0;
327
out_err:
328
	return ret;
329
}
330
 
331
int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
332
{
333
	struct address_space *swap_space;
334
	struct file *swap_storage;
335
	struct page *from_page;
336
	struct page *to_page;
337
	int i;
338
	int ret = -ENOMEM;
339
 
340
	BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
341
	BUG_ON(ttm->caching_state != tt_cached);
342
 
343
	if (!persistent_swap_storage) {
344
		swap_storage = shmem_file_setup("ttm swap",
345
						ttm->num_pages << PAGE_SHIFT,
346
						0);
347
		if (unlikely(IS_ERR(swap_storage))) {
348
			pr_err("Failed allocating swap storage\n");
349
			return PTR_ERR(swap_storage);
350
		}
351
	} else
352
		swap_storage = persistent_swap_storage;
353
 
354
	swap_space = file_inode(swap_storage)->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;
360
		to_page = shmem_read_mapping_page(swap_space, i);
361
		if (unlikely(IS_ERR(to_page))) {
362
			ret = PTR_ERR(to_page);
363
			goto out_err;
364
		}
365
		copy_highpage(to_page, from_page);
366
		set_page_dirty(to_page);
367
		mark_page_accessed(to_page);
368
		page_cache_release(to_page);
369
	}
370
 
371
	ttm->bdev->driver->ttm_tt_unpopulate(ttm);
372
	ttm->swap_storage = swap_storage;
373
	ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
374
	if (persistent_swap_storage)
375
		ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
376
 
377
	return 0;
378
out_err:
379
	if (!persistent_swap_storage)
380
		fput(swap_storage);
381
 
382
	return ret;
383
}
384
 
385
*/
386