| @@ -1,553 +1,559 @@ | | | @@ -1,553 +1,559 @@ |
1 | /************************************************************************** | | 1 | /************************************************************************** |
2 | * | | 2 | * |
3 | * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA | | 3 | * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA |
4 | * All Rights Reserved. | | 4 | * All Rights Reserved. |
5 | * | | 5 | * |
6 | * Permission is hereby granted, free of charge, to any person obtaining a | | 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
7 | * copy of this software and associated documentation files (the | | 7 | * copy of this software and associated documentation files (the |
8 | * "Software"), to deal in the Software without restriction, including | | 8 | * "Software"), to deal in the Software without restriction, including |
9 | * without limitation the rights to use, copy, modify, merge, publish, | | 9 | * without limitation the rights to use, copy, modify, merge, publish, |
10 | * distribute, sub license, and/or sell copies of the Software, and to | | 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 | | 11 | * permit persons to whom the Software is furnished to do so, subject to |
12 | * the following conditions: | | 12 | * the following conditions: |
13 | * | | 13 | * |
14 | * The above copyright notice and this permission notice (including the | | 14 | * The above copyright notice and this permission notice (including the |
15 | * next paragraph) shall be included in all copies or substantial portions | | 15 | * next paragraph) shall be included in all copies or substantial portions |
16 | * of the Software. | | 16 | * of the Software. |
17 | * | | 17 | * |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | | 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, | | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL | | 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, | | 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 | | 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 | | 23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. | | 24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
25 | * | | 25 | * |
26 | **************************************************************************/ | | 26 | **************************************************************************/ |
27 | /* | | 27 | /* |
28 | * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> | | 28 | * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> |
29 | */ | | 29 | */ |
30 | | | 30 | |
31 | #define pr_fmt(fmt) "[TTM] " fmt | | 31 | #define pr_fmt(fmt) "[TTM] " fmt |
32 | | | 32 | |
33 | #include <linux/sched.h> | | 33 | #include <linux/sched.h> |
34 | #include <linux/highmem.h> | | 34 | #include <linux/highmem.h> |
35 | #include <linux/pagemap.h> | | 35 | #include <linux/pagemap.h> |
36 | #include <linux/shmem_fs.h> | | 36 | #include <linux/shmem_fs.h> |
37 | #include <linux/file.h> | | 37 | #include <linux/file.h> |
38 | #include <linux/swap.h> | | 38 | #include <linux/swap.h> |
39 | #include <linux/slab.h> | | 39 | #include <linux/slab.h> |
40 | #include <linux/export.h> | | 40 | #include <linux/export.h> |
41 | #include <linux/printk.h> | | 41 | #include <linux/printk.h> |
42 | #include <drm/drm_cache.h> | | 42 | #include <drm/drm_cache.h> |
43 | #include <drm/drm_mem_util.h> | | 43 | #include <drm/drm_mem_util.h> |
44 | #include <drm/ttm/ttm_module.h> | | 44 | #include <drm/ttm/ttm_module.h> |
45 | #include <drm/ttm/ttm_bo_driver.h> | | 45 | #include <drm/ttm/ttm_bo_driver.h> |
46 | #include <drm/ttm/ttm_placement.h> | | 46 | #include <drm/ttm/ttm_placement.h> |
47 | #include <drm/ttm/ttm_page_alloc.h> | | 47 | #include <drm/ttm/ttm_page_alloc.h> |
48 | #include <drm/bus_dma_hacks.h> | | 48 | #include <drm/bus_dma_hacks.h> |
49 | | | 49 | |
50 | /** | | 50 | /** |
51 | * Allocates storage for pointers to the pages that back the ttm. | | 51 | * Allocates storage for pointers to the pages that back the ttm. |
52 | */ | | 52 | */ |
53 | static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm) | | 53 | static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm) |
54 | { | | 54 | { |
55 | ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(void*)); | | 55 | ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(void*)); |
56 | } | | 56 | } |
57 | | | 57 | |
58 | static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm) | | 58 | static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm) |
59 | { | | 59 | { |
60 | ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages, sizeof(void*)); | | 60 | ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages, sizeof(void*)); |
61 | #ifndef __NetBSD__ | | 61 | #ifndef __NetBSD__ |
62 | ttm->dma_address = drm_calloc_large(ttm->ttm.num_pages, | | 62 | ttm->dma_address = drm_calloc_large(ttm->ttm.num_pages, |
63 | sizeof(*ttm->dma_address)); | | 63 | sizeof(*ttm->dma_address)); |
64 | #endif | | 64 | #endif |
65 | } | | 65 | } |
66 | | | 66 | |
67 | #ifdef CONFIG_X86 | | 67 | #ifdef CONFIG_X86 |
68 | static inline int ttm_tt_set_page_caching(struct page *p, | | 68 | static inline int ttm_tt_set_page_caching(struct page *p, |
69 | enum ttm_caching_state c_old, | | 69 | enum ttm_caching_state c_old, |
70 | enum ttm_caching_state c_new) | | 70 | enum ttm_caching_state c_new) |
71 | { | | 71 | { |
72 | #ifdef __NetBSD__ | | 72 | #ifdef __NetBSD__ |
73 | return 0; | | 73 | return 0; |
74 | #else | | 74 | #else |
75 | int ret = 0; | | 75 | int ret = 0; |
76 | | | 76 | |
77 | if (PageHighMem(p)) | | 77 | if (PageHighMem(p)) |
78 | return 0; | | 78 | return 0; |
79 | | | 79 | |
80 | if (c_old != tt_cached) { | | 80 | if (c_old != tt_cached) { |
81 | /* p isn't in the default caching state, set it to | | 81 | /* p isn't in the default caching state, set it to |
82 | * writeback first to free its current memtype. */ | | 82 | * writeback first to free its current memtype. */ |
83 | | | 83 | |
84 | ret = set_pages_wb(p, 1); | | 84 | ret = set_pages_wb(p, 1); |
85 | if (ret) | | 85 | if (ret) |
86 | return ret; | | 86 | return ret; |
87 | } | | 87 | } |
88 | | | 88 | |
89 | if (c_new == tt_wc) | | 89 | if (c_new == tt_wc) |
90 | ret = set_memory_wc((unsigned long) page_address(p), 1); | | 90 | ret = set_memory_wc((unsigned long) page_address(p), 1); |
91 | else if (c_new == tt_uncached) | | 91 | else if (c_new == tt_uncached) |
92 | ret = set_pages_uc(p, 1); | | 92 | ret = set_pages_uc(p, 1); |
93 | | | 93 | |
94 | return ret; | | 94 | return ret; |
95 | #endif | | 95 | #endif |
96 | } | | 96 | } |
97 | #else /* CONFIG_X86 */ | | 97 | #else /* CONFIG_X86 */ |
98 | static inline int ttm_tt_set_page_caching(struct page *p, | | 98 | static inline int ttm_tt_set_page_caching(struct page *p, |
99 | enum ttm_caching_state c_old, | | 99 | enum ttm_caching_state c_old, |
100 | enum ttm_caching_state c_new) | | 100 | enum ttm_caching_state c_new) |
101 | { | | 101 | { |
102 | return 0; | | 102 | return 0; |
103 | } | | 103 | } |
104 | #endif /* CONFIG_X86 */ | | 104 | #endif /* CONFIG_X86 */ |
105 | | | 105 | |
106 | /* | | 106 | /* |
107 | * Change caching policy for the linear kernel map | | 107 | * Change caching policy for the linear kernel map |
108 | * for range of pages in a ttm. | | 108 | * for range of pages in a ttm. |
109 | */ | | 109 | */ |
110 | | | 110 | |
111 | static int ttm_tt_set_caching(struct ttm_tt *ttm, | | 111 | static int ttm_tt_set_caching(struct ttm_tt *ttm, |
112 | enum ttm_caching_state c_state) | | 112 | enum ttm_caching_state c_state) |
113 | { | | 113 | { |
114 | int i, j; | | 114 | int i, j; |
115 | struct page *cur_page; | | 115 | struct page *cur_page; |
116 | int ret; | | 116 | int ret; |
117 | | | 117 | |
118 | if (ttm->caching_state == c_state) | | 118 | if (ttm->caching_state == c_state) |
119 | return 0; | | 119 | return 0; |
120 | | | 120 | |
121 | if (ttm->state == tt_unpopulated) { | | 121 | if (ttm->state == tt_unpopulated) { |
122 | /* Change caching but don't populate */ | | 122 | /* Change caching but don't populate */ |
123 | ttm->caching_state = c_state; | | 123 | ttm->caching_state = c_state; |
124 | return 0; | | 124 | return 0; |
125 | } | | 125 | } |
126 | | | 126 | |
127 | if (ttm->caching_state == tt_cached) | | 127 | if (ttm->caching_state == tt_cached) |
128 | drm_clflush_pages(ttm->pages, ttm->num_pages); | | 128 | drm_clflush_pages(ttm->pages, ttm->num_pages); |
129 | | | 129 | |
130 | for (i = 0; i < ttm->num_pages; ++i) { | | 130 | for (i = 0; i < ttm->num_pages; ++i) { |
131 | cur_page = ttm->pages[i]; | | 131 | cur_page = ttm->pages[i]; |
132 | if (likely(cur_page != NULL)) { | | 132 | if (likely(cur_page != NULL)) { |
133 | ret = ttm_tt_set_page_caching(cur_page, | | 133 | ret = ttm_tt_set_page_caching(cur_page, |
134 | ttm->caching_state, | | 134 | ttm->caching_state, |
135 | c_state); | | 135 | c_state); |
136 | if (unlikely(ret != 0)) | | 136 | if (unlikely(ret != 0)) |
137 | goto out_err; | | 137 | goto out_err; |
138 | } | | 138 | } |
139 | } | | 139 | } |
140 | | | 140 | |
141 | ttm->caching_state = c_state; | | 141 | ttm->caching_state = c_state; |
142 | | | 142 | |
143 | return 0; | | 143 | return 0; |
144 | | | 144 | |
145 | out_err: | | 145 | out_err: |
146 | for (j = 0; j < i; ++j) { | | 146 | for (j = 0; j < i; ++j) { |
147 | cur_page = ttm->pages[j]; | | 147 | cur_page = ttm->pages[j]; |
148 | if (likely(cur_page != NULL)) { | | 148 | if (likely(cur_page != NULL)) { |
149 | (void)ttm_tt_set_page_caching(cur_page, c_state, | | 149 | (void)ttm_tt_set_page_caching(cur_page, c_state, |
150 | ttm->caching_state); | | 150 | ttm->caching_state); |
151 | } | | 151 | } |
152 | } | | 152 | } |
153 | | | 153 | |
154 | return ret; | | 154 | return ret; |
155 | } | | 155 | } |
156 | | | 156 | |
157 | int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement) | | 157 | int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement) |
158 | { | | 158 | { |
159 | enum ttm_caching_state state; | | 159 | enum ttm_caching_state state; |
160 | | | 160 | |
161 | if (placement & TTM_PL_FLAG_WC) | | 161 | if (placement & TTM_PL_FLAG_WC) |
162 | state = tt_wc; | | 162 | state = tt_wc; |
163 | else if (placement & TTM_PL_FLAG_UNCACHED) | | 163 | else if (placement & TTM_PL_FLAG_UNCACHED) |
164 | state = tt_uncached; | | 164 | state = tt_uncached; |
165 | else | | 165 | else |
166 | state = tt_cached; | | 166 | state = tt_cached; |
167 | | | 167 | |
168 | return ttm_tt_set_caching(ttm, state); | | 168 | return ttm_tt_set_caching(ttm, state); |
169 | } | | 169 | } |
170 | EXPORT_SYMBOL(ttm_tt_set_placement_caching); | | 170 | EXPORT_SYMBOL(ttm_tt_set_placement_caching); |
171 | | | 171 | |
172 | void ttm_tt_destroy(struct ttm_tt *ttm) | | 172 | void ttm_tt_destroy(struct ttm_tt *ttm) |
173 | { | | 173 | { |
174 | if (unlikely(ttm == NULL)) | | 174 | if (unlikely(ttm == NULL)) |
175 | return; | | 175 | return; |
176 | | | 176 | |
177 | if (ttm->state == tt_bound) { | | 177 | if (ttm->state == tt_bound) { |
178 | ttm_tt_unbind(ttm); | | 178 | ttm_tt_unbind(ttm); |
179 | } | | 179 | } |
180 | | | 180 | |
181 | if (ttm->state == tt_unbound) | | 181 | if (ttm->state == tt_unbound) |
182 | ttm_tt_unpopulate(ttm); | | 182 | ttm_tt_unpopulate(ttm); |
183 | | | 183 | |
184 | #ifndef __NetBSD__ | | 184 | #ifndef __NetBSD__ |
185 | if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) && | | 185 | if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) && |
186 | ttm->swap_storage) | | 186 | ttm->swap_storage) |
187 | fput(ttm->swap_storage); | | 187 | fput(ttm->swap_storage); |
188 | | | 188 | |
189 | ttm->swap_storage = NULL; | | 189 | ttm->swap_storage = NULL; |
190 | #endif | | 190 | #endif |
191 | ttm->func->destroy(ttm); | | 191 | ttm->func->destroy(ttm); |
192 | } | | 192 | } |
193 | | | 193 | |
194 | int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev, | | 194 | int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev, |
195 | unsigned long size, uint32_t page_flags, | | 195 | unsigned long size, uint32_t page_flags, |
196 | struct page *dummy_read_page) | | 196 | struct page *dummy_read_page) |
197 | { | | 197 | { |
198 | ttm->bdev = bdev; | | 198 | ttm->bdev = bdev; |
199 | ttm->glob = bdev->glob; | | 199 | ttm->glob = bdev->glob; |
200 | ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; | | 200 | ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
201 | ttm->caching_state = tt_cached; | | 201 | ttm->caching_state = tt_cached; |
202 | ttm->page_flags = page_flags; | | 202 | ttm->page_flags = page_flags; |
203 | ttm->dummy_read_page = dummy_read_page; | | 203 | ttm->dummy_read_page = dummy_read_page; |
204 | ttm->state = tt_unpopulated; | | 204 | ttm->state = tt_unpopulated; |
205 | #ifdef __NetBSD__ | | 205 | #ifdef __NetBSD__ |
| | | 206 | WARN(size == 0, "zero-size allocation in %s, please file a NetBSD PR", |
| | | 207 | __func__); /* paranoia -- can't prove in five minutes */ |
| | | 208 | size = MAX(size, 1); |
206 | ttm->swap_storage = uao_create(roundup2(size, PAGE_SIZE), 0); | | 209 | ttm->swap_storage = uao_create(roundup2(size, PAGE_SIZE), 0); |
207 | uao_set_pgfl(ttm->swap_storage, bus_dmamem_pgfl(bdev->dmat)); | | 210 | uao_set_pgfl(ttm->swap_storage, bus_dmamem_pgfl(bdev->dmat)); |
208 | #else | | 211 | #else |
209 | ttm->swap_storage = NULL; | | 212 | ttm->swap_storage = NULL; |
210 | #endif | | 213 | #endif |
211 | TAILQ_INIT(&ttm->pglist); | | 214 | TAILQ_INIT(&ttm->pglist); |
212 | | | 215 | |
213 | ttm_tt_alloc_page_directory(ttm); | | 216 | ttm_tt_alloc_page_directory(ttm); |
214 | if (!ttm->pages) { | | 217 | if (!ttm->pages) { |
215 | ttm_tt_destroy(ttm); | | 218 | ttm_tt_destroy(ttm); |
216 | pr_err("Failed allocating page table\n"); | | 219 | pr_err("Failed allocating page table\n"); |
217 | return -ENOMEM; | | 220 | return -ENOMEM; |
218 | } | | 221 | } |
219 | return 0; | | 222 | return 0; |
220 | } | | 223 | } |
221 | EXPORT_SYMBOL(ttm_tt_init); | | 224 | EXPORT_SYMBOL(ttm_tt_init); |
222 | | | 225 | |
223 | void ttm_tt_fini(struct ttm_tt *ttm) | | 226 | void ttm_tt_fini(struct ttm_tt *ttm) |
224 | { | | 227 | { |
225 | #ifdef __NetBSD__ | | 228 | #ifdef __NetBSD__ |
226 | uao_detach(ttm->swap_storage); | | 229 | uao_detach(ttm->swap_storage); |
227 | ttm->swap_storage = NULL; | | 230 | ttm->swap_storage = NULL; |
228 | #endif | | 231 | #endif |
229 | drm_free_large(ttm->pages); | | 232 | drm_free_large(ttm->pages); |
230 | ttm->pages = NULL; | | 233 | ttm->pages = NULL; |
231 | } | | 234 | } |
232 | EXPORT_SYMBOL(ttm_tt_fini); | | 235 | EXPORT_SYMBOL(ttm_tt_fini); |
233 | | | 236 | |
234 | int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev, | | 237 | int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev, |
235 | unsigned long size, uint32_t page_flags, | | 238 | unsigned long size, uint32_t page_flags, |
236 | struct page *dummy_read_page) | | 239 | struct page *dummy_read_page) |
237 | { | | 240 | { |
238 | struct ttm_tt *ttm = &ttm_dma->ttm; | | 241 | struct ttm_tt *ttm = &ttm_dma->ttm; |
239 | | | 242 | |
240 | ttm->bdev = bdev; | | 243 | ttm->bdev = bdev; |
241 | ttm->glob = bdev->glob; | | 244 | ttm->glob = bdev->glob; |
242 | ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; | | 245 | ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
243 | ttm->caching_state = tt_cached; | | 246 | ttm->caching_state = tt_cached; |
244 | ttm->page_flags = page_flags; | | 247 | ttm->page_flags = page_flags; |
245 | ttm->dummy_read_page = dummy_read_page; | | 248 | ttm->dummy_read_page = dummy_read_page; |
246 | ttm->state = tt_unpopulated; | | 249 | ttm->state = tt_unpopulated; |
247 | #ifdef __NetBSD__ | | 250 | #ifdef __NetBSD__ |
| | | 251 | WARN(size == 0, "zero-size allocation in %s, please file a NetBSD PR", |
| | | 252 | __func__); /* paranoia -- can't prove in five minutes */ |
| | | 253 | size = MAX(size, 1); |
248 | ttm->swap_storage = uao_create(roundup2(size, PAGE_SIZE), 0); | | 254 | ttm->swap_storage = uao_create(roundup2(size, PAGE_SIZE), 0); |
249 | uao_set_pgfl(ttm->swap_storage, bus_dmamem_pgfl(bdev->dmat)); | | 255 | uao_set_pgfl(ttm->swap_storage, bus_dmamem_pgfl(bdev->dmat)); |
250 | #else | | 256 | #else |
251 | ttm->swap_storage = NULL; | | 257 | ttm->swap_storage = NULL; |
252 | #endif | | 258 | #endif |
253 | TAILQ_INIT(&ttm->pglist); | | 259 | TAILQ_INIT(&ttm->pglist); |
254 | | | 260 | |
255 | INIT_LIST_HEAD(&ttm_dma->pages_list); | | 261 | INIT_LIST_HEAD(&ttm_dma->pages_list); |
256 | ttm_dma_tt_alloc_page_directory(ttm_dma); | | 262 | ttm_dma_tt_alloc_page_directory(ttm_dma); |
257 | #ifdef __NetBSD__ | | 263 | #ifdef __NetBSD__ |
258 | { | | 264 | { |
259 | int error; | | 265 | int error; |
260 | | | 266 | |
261 | if (ttm->num_pages > (SIZE_MAX / | | 267 | if (ttm->num_pages > (SIZE_MAX / |
262 | MIN(sizeof(ttm_dma->dma_segs[0]), PAGE_SIZE))) { | | 268 | MIN(sizeof(ttm_dma->dma_segs[0]), PAGE_SIZE))) { |
263 | error = ENOMEM; | | 269 | error = ENOMEM; |
264 | goto fail0; | | 270 | goto fail0; |
265 | } | | 271 | } |
266 | ttm_dma->dma_segs = kmem_alloc((ttm->num_pages * | | 272 | ttm_dma->dma_segs = kmem_alloc((ttm->num_pages * |
267 | sizeof(ttm_dma->dma_segs[0])), KM_SLEEP); | | 273 | sizeof(ttm_dma->dma_segs[0])), KM_SLEEP); |
268 | error = bus_dmamap_create(ttm->bdev->dmat, | | 274 | error = bus_dmamap_create(ttm->bdev->dmat, |
269 | (ttm->num_pages * PAGE_SIZE), ttm->num_pages, PAGE_SIZE, 0, | | 275 | (ttm->num_pages * PAGE_SIZE), ttm->num_pages, PAGE_SIZE, 0, |
270 | BUS_DMA_WAITOK, &ttm_dma->dma_address); | | 276 | BUS_DMA_WAITOK, &ttm_dma->dma_address); |
271 | if (error) | | 277 | if (error) |
272 | goto fail1; | | 278 | goto fail1; |
273 | | | 279 | |
274 | return 0; | | 280 | return 0; |
275 | | | 281 | |
276 | fail2: __unused | | 282 | fail2: __unused |
277 | bus_dmamap_destroy(ttm->bdev->dmat, ttm_dma->dma_address); | | 283 | bus_dmamap_destroy(ttm->bdev->dmat, ttm_dma->dma_address); |
278 | fail1: kmem_free(ttm_dma->dma_segs, (ttm->num_pages * | | 284 | fail1: kmem_free(ttm_dma->dma_segs, (ttm->num_pages * |
279 | sizeof(ttm_dma->dma_segs[0]))); | | 285 | sizeof(ttm_dma->dma_segs[0]))); |
280 | fail0: KASSERT(error); | | 286 | fail0: KASSERT(error); |
281 | ttm_tt_destroy(ttm); | | 287 | ttm_tt_destroy(ttm); |
282 | /* XXX errno NetBSD->Linux */ | | 288 | /* XXX errno NetBSD->Linux */ |
283 | return -error; | | 289 | return -error; |
284 | } | | 290 | } |
285 | #else | | 291 | #else |
286 | if (!ttm->pages || !ttm_dma->dma_address) { | | 292 | if (!ttm->pages || !ttm_dma->dma_address) { |
287 | ttm_tt_destroy(ttm); | | 293 | ttm_tt_destroy(ttm); |
288 | pr_err("Failed allocating page table\n"); | | 294 | pr_err("Failed allocating page table\n"); |
289 | return -ENOMEM; | | 295 | return -ENOMEM; |
290 | } | | 296 | } |
291 | return 0; | | 297 | return 0; |
292 | #endif | | 298 | #endif |
293 | } | | 299 | } |
294 | EXPORT_SYMBOL(ttm_dma_tt_init); | | 300 | EXPORT_SYMBOL(ttm_dma_tt_init); |
295 | | | 301 | |
296 | void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma) | | 302 | void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma) |
297 | { | | 303 | { |
298 | struct ttm_tt *ttm = &ttm_dma->ttm; | | 304 | struct ttm_tt *ttm = &ttm_dma->ttm; |
299 | | | 305 | |
300 | #ifdef __NetBSD__ | | 306 | #ifdef __NetBSD__ |
301 | uao_detach(ttm->swap_storage); | | 307 | uao_detach(ttm->swap_storage); |
302 | ttm->swap_storage = NULL; | | 308 | ttm->swap_storage = NULL; |
303 | #endif | | 309 | #endif |
304 | drm_free_large(ttm->pages); | | 310 | drm_free_large(ttm->pages); |
305 | ttm->pages = NULL; | | 311 | ttm->pages = NULL; |
306 | #ifdef __NetBSD__ | | 312 | #ifdef __NetBSD__ |
307 | bus_dmamap_destroy(ttm->bdev->dmat, ttm_dma->dma_address); | | 313 | bus_dmamap_destroy(ttm->bdev->dmat, ttm_dma->dma_address); |
308 | kmem_free(ttm_dma->dma_segs, (ttm->num_pages * | | 314 | kmem_free(ttm_dma->dma_segs, (ttm->num_pages * |
309 | sizeof(ttm_dma->dma_segs[0]))); | | 315 | sizeof(ttm_dma->dma_segs[0]))); |
310 | #else | | 316 | #else |
311 | drm_free_large(ttm_dma->dma_address); | | 317 | drm_free_large(ttm_dma->dma_address); |
312 | ttm_dma->dma_address = NULL; | | 318 | ttm_dma->dma_address = NULL; |
313 | #endif | | 319 | #endif |
314 | } | | 320 | } |
315 | EXPORT_SYMBOL(ttm_dma_tt_fini); | | 321 | EXPORT_SYMBOL(ttm_dma_tt_fini); |
316 | | | 322 | |
317 | void ttm_tt_unbind(struct ttm_tt *ttm) | | 323 | void ttm_tt_unbind(struct ttm_tt *ttm) |
318 | { | | 324 | { |
319 | int ret __diagused; | | 325 | int ret __diagused; |
320 | | | 326 | |
321 | if (ttm->state == tt_bound) { | | 327 | if (ttm->state == tt_bound) { |
322 | ret = ttm->func->unbind(ttm); | | 328 | ret = ttm->func->unbind(ttm); |
323 | BUG_ON(ret); | | 329 | BUG_ON(ret); |
324 | ttm->state = tt_unbound; | | 330 | ttm->state = tt_unbound; |
325 | } | | 331 | } |
326 | } | | 332 | } |
327 | | | 333 | |
328 | int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem) | | 334 | int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem) |
329 | { | | 335 | { |
330 | int ret = 0; | | 336 | int ret = 0; |
331 | | | 337 | |
332 | if (!ttm) | | 338 | if (!ttm) |
333 | return -EINVAL; | | 339 | return -EINVAL; |
334 | | | 340 | |
335 | if (ttm->state == tt_bound) | | 341 | if (ttm->state == tt_bound) |
336 | return 0; | | 342 | return 0; |
337 | | | 343 | |
338 | ret = ttm->bdev->driver->ttm_tt_populate(ttm); | | 344 | ret = ttm->bdev->driver->ttm_tt_populate(ttm); |
339 | if (ret) | | 345 | if (ret) |
340 | return ret; | | 346 | return ret; |
341 | | | 347 | |
342 | ret = ttm->func->bind(ttm, bo_mem); | | 348 | ret = ttm->func->bind(ttm, bo_mem); |
343 | if (unlikely(ret != 0)) | | 349 | if (unlikely(ret != 0)) |
344 | return ret; | | 350 | return ret; |
345 | | | 351 | |
346 | ttm->state = tt_bound; | | 352 | ttm->state = tt_bound; |
347 | | | 353 | |
348 | return 0; | | 354 | return 0; |
349 | } | | 355 | } |
350 | EXPORT_SYMBOL(ttm_tt_bind); | | 356 | EXPORT_SYMBOL(ttm_tt_bind); |
351 | | | 357 | |
352 | #ifdef __NetBSD__ | | 358 | #ifdef __NetBSD__ |
353 | /* | | 359 | /* |
354 | * ttm_tt_wire(ttm) | | 360 | * ttm_tt_wire(ttm) |
355 | * | | 361 | * |
356 | * Wire the uvm pages of ttm and fill the ttm page array. ttm | | 362 | * Wire the uvm pages of ttm and fill the ttm page array. ttm |
357 | * must be unpopulated or unbound, and must be marked swapped. | | 363 | * must be unpopulated or unbound, and must be marked swapped. |
358 | * This does not change either state -- the caller is expected to | | 364 | * This does not change either state -- the caller is expected to |
359 | * include it among other operations for such a state transition. | | 365 | * include it among other operations for such a state transition. |
360 | */ | | 366 | */ |
361 | int | | 367 | int |
362 | ttm_tt_wire(struct ttm_tt *ttm) | | 368 | ttm_tt_wire(struct ttm_tt *ttm) |
363 | { | | 369 | { |
364 | struct uvm_object *uobj = ttm->swap_storage; | | 370 | struct uvm_object *uobj = ttm->swap_storage; |
365 | struct vm_page *page; | | 371 | struct vm_page *page; |
366 | unsigned i; | | 372 | unsigned i; |
367 | int error; | | 373 | int error; |
368 | | | 374 | |
369 | KASSERTMSG((ttm->state == tt_unpopulated || ttm->state == tt_unbound), | | 375 | KASSERTMSG((ttm->state == tt_unpopulated || ttm->state == tt_unbound), |
370 | "ttm_tt %p must be unpopulated or unbound for wiring," | | 376 | "ttm_tt %p must be unpopulated or unbound for wiring," |
371 | " but state=%d", | | 377 | " but state=%d", |
372 | ttm, (int)ttm->state); | | 378 | ttm, (int)ttm->state); |
373 | KASSERT(ISSET(ttm->page_flags, TTM_PAGE_FLAG_SWAPPED)); | | 379 | KASSERT(ISSET(ttm->page_flags, TTM_PAGE_FLAG_SWAPPED)); |
374 | KASSERT(uobj != NULL); | | 380 | KASSERT(uobj != NULL); |
375 | | | 381 | |
376 | error = uvm_obj_wirepages(uobj, 0, (ttm->num_pages << PAGE_SHIFT), | | 382 | error = uvm_obj_wirepages(uobj, 0, (ttm->num_pages << PAGE_SHIFT), |
377 | &ttm->pglist); | | 383 | &ttm->pglist); |
378 | if (error) | | 384 | if (error) |
379 | /* XXX errno NetBSD->Linux */ | | 385 | /* XXX errno NetBSD->Linux */ |
380 | return -error; | | 386 | return -error; |
381 | | | 387 | |
382 | i = 0; | | 388 | i = 0; |
383 | TAILQ_FOREACH(page, &ttm->pglist, pageq.queue) { | | 389 | TAILQ_FOREACH(page, &ttm->pglist, pageq.queue) { |
384 | KASSERT(i < ttm->num_pages); | | 390 | KASSERT(i < ttm->num_pages); |
385 | KASSERT(ttm->pages[i] == NULL); | | 391 | KASSERT(ttm->pages[i] == NULL); |
386 | ttm->pages[i] = container_of(page, struct page, p_vmp); | | 392 | ttm->pages[i] = container_of(page, struct page, p_vmp); |
387 | i++; | | 393 | i++; |
388 | } | | 394 | } |
389 | KASSERT(i == ttm->num_pages); | | 395 | KASSERT(i == ttm->num_pages); |
390 | | | 396 | |
391 | /* Success! */ | | 397 | /* Success! */ |
392 | return 0; | | 398 | return 0; |
393 | } | | 399 | } |
394 | | | 400 | |
395 | /* | | 401 | /* |
396 | * ttm_tt_unwire(ttm) | | 402 | * ttm_tt_unwire(ttm) |
397 | * | | 403 | * |
398 | * Nullify the ttm page array and unwire the uvm pages of ttm. | | 404 | * Nullify the ttm page array and unwire the uvm pages of ttm. |
399 | * ttm must be unbound and must be marked swapped. This does not | | 405 | * ttm must be unbound and must be marked swapped. This does not |
400 | * change either state -- the caller is expected to include it | | 406 | * change either state -- the caller is expected to include it |
401 | * among other operations for such a state transition. | | 407 | * among other operations for such a state transition. |
402 | */ | | 408 | */ |
403 | void | | 409 | void |
404 | ttm_tt_unwire(struct ttm_tt *ttm) | | 410 | ttm_tt_unwire(struct ttm_tt *ttm) |
405 | { | | 411 | { |
406 | struct uvm_object *uobj = ttm->swap_storage; | | 412 | struct uvm_object *uobj = ttm->swap_storage; |
407 | unsigned i; | | 413 | unsigned i; |
408 | | | 414 | |
409 | KASSERTMSG((ttm->state == tt_unbound), | | 415 | KASSERTMSG((ttm->state == tt_unbound), |
410 | "ttm_tt %p must be unbound for unwiring, but state=%d", | | 416 | "ttm_tt %p must be unbound for unwiring, but state=%d", |
411 | ttm, (int)ttm->state); | | 417 | ttm, (int)ttm->state); |
412 | KASSERT(!ISSET(ttm->page_flags, TTM_PAGE_FLAG_SWAPPED)); | | 418 | KASSERT(!ISSET(ttm->page_flags, TTM_PAGE_FLAG_SWAPPED)); |
413 | KASSERT(uobj != NULL); | | 419 | KASSERT(uobj != NULL); |
414 | | | 420 | |
415 | uvm_obj_unwirepages(uobj, 0, (ttm->num_pages << PAGE_SHIFT)); | | 421 | uvm_obj_unwirepages(uobj, 0, (ttm->num_pages << PAGE_SHIFT)); |
416 | for (i = 0; i < ttm->num_pages; i++) | | 422 | for (i = 0; i < ttm->num_pages; i++) |
417 | ttm->pages[i] = NULL; | | 423 | ttm->pages[i] = NULL; |
418 | } | | 424 | } |
419 | #endif | | 425 | #endif |
420 | | | 426 | |
421 | #ifndef __NetBSD__ | | 427 | #ifndef __NetBSD__ |
422 | int ttm_tt_swapin(struct ttm_tt *ttm) | | 428 | int ttm_tt_swapin(struct ttm_tt *ttm) |
423 | { | | 429 | { |
424 | struct address_space *swap_space; | | 430 | struct address_space *swap_space; |
425 | struct file *swap_storage; | | 431 | struct file *swap_storage; |
426 | struct page *from_page; | | 432 | struct page *from_page; |
427 | struct page *to_page; | | 433 | struct page *to_page; |
428 | int i; | | 434 | int i; |
429 | int ret = -ENOMEM; | | 435 | int ret = -ENOMEM; |
430 | | | 436 | |
431 | swap_storage = ttm->swap_storage; | | 437 | swap_storage = ttm->swap_storage; |
432 | BUG_ON(swap_storage == NULL); | | 438 | BUG_ON(swap_storage == NULL); |
433 | | | 439 | |
434 | swap_space = file_inode(swap_storage)->i_mapping; | | 440 | swap_space = file_inode(swap_storage)->i_mapping; |
435 | | | 441 | |
436 | for (i = 0; i < ttm->num_pages; ++i) { | | 442 | for (i = 0; i < ttm->num_pages; ++i) { |
437 | from_page = shmem_read_mapping_page(swap_space, i); | | 443 | from_page = shmem_read_mapping_page(swap_space, i); |
438 | if (IS_ERR(from_page)) { | | 444 | if (IS_ERR(from_page)) { |
439 | ret = PTR_ERR(from_page); | | 445 | ret = PTR_ERR(from_page); |
440 | goto out_err; | | 446 | goto out_err; |
441 | } | | 447 | } |
442 | to_page = ttm->pages[i]; | | 448 | to_page = ttm->pages[i]; |
443 | if (unlikely(to_page == NULL)) | | 449 | if (unlikely(to_page == NULL)) |
444 | goto out_err; | | 450 | goto out_err; |
445 | | | 451 | |
446 | copy_highpage(to_page, from_page); | | 452 | copy_highpage(to_page, from_page); |
447 | page_cache_release(from_page); | | 453 | page_cache_release(from_page); |
448 | } | | 454 | } |
449 | | | 455 | |
450 | if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP)) | | 456 | if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP)) |
451 | fput(swap_storage); | | 457 | fput(swap_storage); |
452 | ttm->swap_storage = NULL; | | 458 | ttm->swap_storage = NULL; |
453 | ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED; | | 459 | ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED; |
454 | | | 460 | |
455 | return 0; | | 461 | return 0; |
456 | out_err: | | 462 | out_err: |
457 | return ret; | | 463 | return ret; |
458 | } | | 464 | } |
459 | #endif | | 465 | #endif |
460 | | | 466 | |
461 | int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage) | | 467 | int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage) |
462 | { | | 468 | { |
463 | #ifdef __NetBSD__ | | 469 | #ifdef __NetBSD__ |
464 | | | 470 | |
465 | KASSERTMSG((ttm->state == tt_unpopulated || ttm->state == tt_unbound), | | 471 | KASSERTMSG((ttm->state == tt_unpopulated || ttm->state == tt_unbound), |
466 | "ttm_tt %p must be unpopulated or unbound for swapout," | | 472 | "ttm_tt %p must be unpopulated or unbound for swapout," |
467 | " but state=%d", | | 473 | " but state=%d", |
468 | ttm, (int)ttm->state); | | 474 | ttm, (int)ttm->state); |
469 | KASSERTMSG((ttm->caching_state == tt_cached), | | 475 | KASSERTMSG((ttm->caching_state == tt_cached), |
470 | "ttm_tt %p must be cached for swapout, but caching_state=%d", | | 476 | "ttm_tt %p must be cached for swapout, but caching_state=%d", |
471 | ttm, (int)ttm->caching_state); | | 477 | ttm, (int)ttm->caching_state); |
472 | KASSERT(persistent_swap_storage == NULL); | | 478 | KASSERT(persistent_swap_storage == NULL); |
473 | | | 479 | |
474 | ttm->bdev->driver->ttm_tt_swapout(ttm); | | 480 | ttm->bdev->driver->ttm_tt_swapout(ttm); |
475 | return 0; | | 481 | return 0; |
476 | #else | | 482 | #else |
477 | struct address_space *swap_space; | | 483 | struct address_space *swap_space; |
478 | struct file *swap_storage; | | 484 | struct file *swap_storage; |
479 | struct page *from_page; | | 485 | struct page *from_page; |
480 | struct page *to_page; | | 486 | struct page *to_page; |
481 | int i; | | 487 | int i; |
482 | int ret = -ENOMEM; | | 488 | int ret = -ENOMEM; |
483 | | | 489 | |
484 | BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated); | | 490 | BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated); |
485 | BUG_ON(ttm->caching_state != tt_cached); | | 491 | BUG_ON(ttm->caching_state != tt_cached); |
486 | | | 492 | |
487 | if (!persistent_swap_storage) { | | 493 | if (!persistent_swap_storage) { |
488 | swap_storage = shmem_file_setup("ttm swap", | | 494 | swap_storage = shmem_file_setup("ttm swap", |
489 | ttm->num_pages << PAGE_SHIFT, | | 495 | ttm->num_pages << PAGE_SHIFT, |
490 | 0); | | 496 | 0); |
491 | if (unlikely(IS_ERR(swap_storage))) { | | 497 | if (unlikely(IS_ERR(swap_storage))) { |
492 | pr_err("Failed allocating swap storage\n"); | | 498 | pr_err("Failed allocating swap storage\n"); |
493 | return PTR_ERR(swap_storage); | | 499 | return PTR_ERR(swap_storage); |
494 | } | | 500 | } |
495 | } else | | 501 | } else |
496 | swap_storage = persistent_swap_storage; | | 502 | swap_storage = persistent_swap_storage; |
497 | | | 503 | |
498 | swap_space = file_inode(swap_storage)->i_mapping; | | 504 | swap_space = file_inode(swap_storage)->i_mapping; |
499 | | | 505 | |
500 | for (i = 0; i < ttm->num_pages; ++i) { | | 506 | for (i = 0; i < ttm->num_pages; ++i) { |
501 | from_page = ttm->pages[i]; | | 507 | from_page = ttm->pages[i]; |
502 | if (unlikely(from_page == NULL)) | | 508 | if (unlikely(from_page == NULL)) |
503 | continue; | | 509 | continue; |
504 | to_page = shmem_read_mapping_page(swap_space, i); | | 510 | to_page = shmem_read_mapping_page(swap_space, i); |
505 | if (unlikely(IS_ERR(to_page))) { | | 511 | if (unlikely(IS_ERR(to_page))) { |
506 | ret = PTR_ERR(to_page); | | 512 | ret = PTR_ERR(to_page); |
507 | goto out_err; | | 513 | goto out_err; |
508 | } | | 514 | } |
509 | copy_highpage(to_page, from_page); | | 515 | copy_highpage(to_page, from_page); |
510 | set_page_dirty(to_page); | | 516 | set_page_dirty(to_page); |
511 | mark_page_accessed(to_page); | | 517 | mark_page_accessed(to_page); |
512 | page_cache_release(to_page); | | 518 | page_cache_release(to_page); |
513 | } | | 519 | } |
514 | | | 520 | |
515 | ttm_tt_unpopulate(ttm); | | 521 | ttm_tt_unpopulate(ttm); |
516 | ttm->swap_storage = swap_storage; | | 522 | ttm->swap_storage = swap_storage; |
517 | ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED; | | 523 | ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED; |
518 | if (persistent_swap_storage) | | 524 | if (persistent_swap_storage) |
519 | ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP; | | 525 | ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP; |
520 | | | 526 | |
521 | return 0; | | 527 | return 0; |
522 | out_err: | | 528 | out_err: |
523 | if (!persistent_swap_storage) | | 529 | if (!persistent_swap_storage) |
524 | fput(swap_storage); | | 530 | fput(swap_storage); |
525 | | | 531 | |
526 | return ret; | | 532 | return ret; |
527 | #endif | | 533 | #endif |
528 | } | | 534 | } |
529 | | | 535 | |
530 | static void ttm_tt_clear_mapping(struct ttm_tt *ttm) | | 536 | static void ttm_tt_clear_mapping(struct ttm_tt *ttm) |
531 | { | | 537 | { |
532 | #ifndef __NetBSD__ | | 538 | #ifndef __NetBSD__ |
533 | pgoff_t i; | | 539 | pgoff_t i; |
534 | struct page **page = ttm->pages; | | 540 | struct page **page = ttm->pages; |
535 | | | 541 | |
536 | if (ttm->page_flags & TTM_PAGE_FLAG_SG) | | 542 | if (ttm->page_flags & TTM_PAGE_FLAG_SG) |
537 | return; | | 543 | return; |
538 | | | 544 | |
539 | for (i = 0; i < ttm->num_pages; ++i) { | | 545 | for (i = 0; i < ttm->num_pages; ++i) { |
540 | (*page)->mapping = NULL; | | 546 | (*page)->mapping = NULL; |
541 | (*page++)->index = 0; | | 547 | (*page++)->index = 0; |
542 | } | | 548 | } |
543 | #endif | | 549 | #endif |
544 | } | | 550 | } |
545 | | | 551 | |
546 | void ttm_tt_unpopulate(struct ttm_tt *ttm) | | 552 | void ttm_tt_unpopulate(struct ttm_tt *ttm) |
547 | { | | 553 | { |
548 | if (ttm->state == tt_unpopulated) | | 554 | if (ttm->state == tt_unpopulated) |
549 | return; | | 555 | return; |
550 | | | 556 | |
551 | ttm_tt_clear_mapping(ttm); | | 557 | ttm_tt_clear_mapping(ttm); |
552 | ttm->bdev->driver->ttm_tt_unpopulate(ttm); | | 558 | ttm->bdev->driver->ttm_tt_unpopulate(ttm); |
553 | } | | 559 | } |