Rev 4075 | Rev 5078 | Go to most recent revision | Details | Compare with Previous | 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 | |||
4569 | Serge | 175 | // if (ttm->state == tt_unbound) |
176 | // ttm_tt_unpopulate(ttm); |
||
4075 | Serge | 177 | |
178 | // if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) && |
||
179 | // ttm->swap_storage) |
||
180 | // fput(ttm->swap_storage); |
||
181 | |||
182 | ttm->swap_storage = NULL; |
||
183 | ttm->func->destroy(ttm); |
||
184 | } |
||
185 | |||
186 | int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev, |
||
187 | unsigned long size, uint32_t page_flags, |
||
188 | struct page *dummy_read_page) |
||
189 | { |
||
190 | ttm->bdev = bdev; |
||
191 | ttm->glob = bdev->glob; |
||
192 | ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
||
193 | ttm->caching_state = tt_cached; |
||
194 | ttm->page_flags = page_flags; |
||
195 | ttm->dummy_read_page = dummy_read_page; |
||
196 | ttm->state = tt_unpopulated; |
||
197 | ttm->swap_storage = NULL; |
||
198 | |||
199 | ttm_tt_alloc_page_directory(ttm); |
||
200 | if (!ttm->pages) { |
||
201 | ttm_tt_destroy(ttm); |
||
202 | printf("Failed allocating page table\n"); |
||
203 | return -ENOMEM; |
||
204 | } |
||
205 | return 0; |
||
206 | } |
||
207 | EXPORT_SYMBOL(ttm_tt_init); |
||
208 | |||
209 | void ttm_tt_fini(struct ttm_tt *ttm) |
||
210 | { |
||
211 | drm_free_large(ttm->pages); |
||
212 | ttm->pages = NULL; |
||
213 | } |
||
214 | EXPORT_SYMBOL(ttm_tt_fini); |
||
215 | |||
216 | int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev, |
||
217 | unsigned long size, uint32_t page_flags, |
||
218 | struct page *dummy_read_page) |
||
219 | { |
||
220 | struct ttm_tt *ttm = &ttm_dma->ttm; |
||
221 | |||
222 | ttm->bdev = bdev; |
||
223 | ttm->glob = bdev->glob; |
||
224 | ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
||
225 | ttm->caching_state = tt_cached; |
||
226 | ttm->page_flags = page_flags; |
||
227 | ttm->dummy_read_page = dummy_read_page; |
||
228 | ttm->state = tt_unpopulated; |
||
229 | ttm->swap_storage = NULL; |
||
230 | |||
231 | INIT_LIST_HEAD(&ttm_dma->pages_list); |
||
232 | ttm_dma_tt_alloc_page_directory(ttm_dma); |
||
233 | if (!ttm->pages || !ttm_dma->dma_address) { |
||
234 | ttm_tt_destroy(ttm); |
||
235 | printf("Failed allocating page table\n"); |
||
236 | return -ENOMEM; |
||
237 | } |
||
238 | return 0; |
||
239 | } |
||
240 | EXPORT_SYMBOL(ttm_dma_tt_init); |
||
241 | |||
242 | void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma) |
||
243 | { |
||
244 | struct ttm_tt *ttm = &ttm_dma->ttm; |
||
245 | |||
246 | drm_free_large(ttm->pages); |
||
247 | ttm->pages = NULL; |
||
248 | drm_free_large(ttm_dma->dma_address); |
||
249 | ttm_dma->dma_address = NULL; |
||
250 | } |
||
251 | EXPORT_SYMBOL(ttm_dma_tt_fini); |
||
252 | |||
253 | void ttm_tt_unbind(struct ttm_tt *ttm) |
||
254 | { |
||
255 | int ret; |
||
256 | |||
257 | if (ttm->state == tt_bound) { |
||
258 | ret = ttm->func->unbind(ttm); |
||
259 | BUG_ON(ret); |
||
260 | ttm->state = tt_unbound; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | #if 0 |
||
265 | |||
266 | int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem) |
||
267 | { |
||
268 | int ret = 0; |
||
269 | |||
270 | if (!ttm) |
||
271 | return -EINVAL; |
||
272 | |||
273 | if (ttm->state == tt_bound) |
||
274 | return 0; |
||
275 | |||
276 | ret = ttm->bdev->driver->ttm_tt_populate(ttm); |
||
277 | if (ret) |
||
278 | return ret; |
||
279 | |||
280 | ret = ttm->func->bind(ttm, bo_mem); |
||
281 | if (unlikely(ret != 0)) |
||
282 | return ret; |
||
283 | |||
284 | ttm->state = tt_bound; |
||
285 | |||
286 | return 0; |
||
287 | } |
||
288 | EXPORT_SYMBOL(ttm_tt_bind); |
||
289 | #endif |
||
290 | |||
291 | /* |
||
292 | int ttm_tt_swapin(struct ttm_tt *ttm) |
||
293 | { |
||
294 | struct address_space *swap_space; |
||
295 | struct file *swap_storage; |
||
296 | struct page *from_page; |
||
297 | struct page *to_page; |
||
298 | int i; |
||
299 | int ret = -ENOMEM; |
||
300 | |||
301 | swap_storage = ttm->swap_storage; |
||
302 | BUG_ON(swap_storage == NULL); |
||
303 | |||
304 | swap_space = file_inode(swap_storage)->i_mapping; |
||
305 | |||
306 | for (i = 0; i < ttm->num_pages; ++i) { |
||
307 | from_page = shmem_read_mapping_page(swap_space, i); |
||
308 | if (IS_ERR(from_page)) { |
||
309 | ret = PTR_ERR(from_page); |
||
310 | goto out_err; |
||
311 | } |
||
312 | to_page = ttm->pages[i]; |
||
313 | if (unlikely(to_page == NULL)) |
||
314 | goto out_err; |
||
315 | |||
316 | copy_highpage(to_page, from_page); |
||
317 | page_cache_release(from_page); |
||
318 | } |
||
319 | |||
320 | if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP)) |
||
321 | fput(swap_storage); |
||
322 | ttm->swap_storage = NULL; |
||
323 | ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED; |
||
324 | |||
325 | return 0; |
||
326 | out_err: |
||
327 | return ret; |
||
328 | } |
||
329 | |||
330 | int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage) |
||
331 | { |
||
332 | struct address_space *swap_space; |
||
333 | struct file *swap_storage; |
||
334 | struct page *from_page; |
||
335 | struct page *to_page; |
||
336 | int i; |
||
337 | int ret = -ENOMEM; |
||
338 | |||
339 | BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated); |
||
340 | BUG_ON(ttm->caching_state != tt_cached); |
||
341 | |||
342 | if (!persistent_swap_storage) { |
||
343 | swap_storage = shmem_file_setup("ttm swap", |
||
344 | ttm->num_pages << PAGE_SHIFT, |
||
345 | 0); |
||
346 | if (unlikely(IS_ERR(swap_storage))) { |
||
347 | pr_err("Failed allocating swap storage\n"); |
||
348 | return PTR_ERR(swap_storage); |
||
349 | } |
||
350 | } else |
||
351 | swap_storage = persistent_swap_storage; |
||
352 | |||
353 | swap_space = file_inode(swap_storage)->i_mapping; |
||
354 | |||
355 | for (i = 0; i < ttm->num_pages; ++i) { |
||
356 | from_page = ttm->pages[i]; |
||
357 | if (unlikely(from_page == NULL)) |
||
358 | continue; |
||
359 | to_page = shmem_read_mapping_page(swap_space, i); |
||
360 | if (unlikely(IS_ERR(to_page))) { |
||
361 | ret = PTR_ERR(to_page); |
||
362 | goto out_err; |
||
363 | } |
||
364 | copy_highpage(to_page, from_page); |
||
365 | set_page_dirty(to_page); |
||
366 | mark_page_accessed(to_page); |
||
367 | page_cache_release(to_page); |
||
368 | } |
||
369 | |||
4569 | Serge | 370 | ttm_tt_unpopulate(ttm); |
4075 | Serge | 371 | ttm->swap_storage = swap_storage; |
372 | ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED; |
||
373 | if (persistent_swap_storage) |
||
374 | ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP; |
||
375 | |||
376 | return 0; |
||
377 | out_err: |
||
378 | if (!persistent_swap_storage) |
||
379 | fput(swap_storage); |
||
380 | |||
381 | return ret; |
||
382 | } |
||
383 | |||
384 | */>><>>>> |
||
385 |