xref: /freebsd/sys/dev/drm2/ttm/ttm_tt.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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  * Copyright (c) 2013 The FreeBSD Foundation
32  * All rights reserved.
33  *
34  * Portions of this software were developed by Konstantin Belousov
35  * <kib@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
36  */
37 
38 #include <sys/cdefs.h>
39 #include <dev/drm2/drmP.h>
40 #include <dev/drm2/ttm/ttm_module.h>
41 #include <dev/drm2/ttm/ttm_bo_driver.h>
42 #include <dev/drm2/ttm/ttm_placement.h>
43 #include <dev/drm2/ttm/ttm_page_alloc.h>
44 
45 MALLOC_DEFINE(M_TTM_PD, "ttm_pd", "TTM Page Directories");
46 
47 /**
48  * Allocates storage for pointers to the pages that back the ttm.
49  */
50 static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
51 {
52 	ttm->pages = malloc(ttm->num_pages * sizeof(void *),
53 	    M_TTM_PD, M_WAITOK | M_ZERO);
54 }
55 
56 static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
57 {
58 	ttm->ttm.pages = malloc(ttm->ttm.num_pages * sizeof(void *),
59 	    M_TTM_PD, M_WAITOK | M_ZERO);
60 	ttm->dma_address = malloc(ttm->ttm.num_pages *
61 	    sizeof(*ttm->dma_address), M_TTM_PD, M_WAITOK);
62 }
63 
64 #if defined(__i386__) || defined(__amd64__)
65 static inline int ttm_tt_set_page_caching(vm_page_t p,
66 					  enum ttm_caching_state c_old,
67 					  enum ttm_caching_state c_new)
68 {
69 
70 	/* XXXKIB our VM does not need this. */
71 #if 0
72 	if (c_old != tt_cached) {
73 		/* p isn't in the default caching state, set it to
74 		 * writeback first to free its current memtype. */
75 		pmap_page_set_memattr(p, VM_MEMATTR_WRITE_BACK);
76 	}
77 #endif
78 
79 	if (c_new == tt_wc)
80 		pmap_page_set_memattr(p, VM_MEMATTR_WRITE_COMBINING);
81 	else if (c_new == tt_uncached)
82 		pmap_page_set_memattr(p, VM_MEMATTR_UNCACHEABLE);
83 
84 	return (0);
85 }
86 #else
87 static inline int ttm_tt_set_page_caching(vm_page_t p,
88 					  enum ttm_caching_state c_old,
89 					  enum ttm_caching_state c_new)
90 {
91 	return 0;
92 }
93 #endif
94 
95 /*
96  * Change caching policy for the linear kernel map
97  * for range of pages in a ttm.
98  */
99 
100 static int ttm_tt_set_caching(struct ttm_tt *ttm,
101 			      enum ttm_caching_state c_state)
102 {
103 	int i, j;
104 	vm_page_t cur_page;
105 	int ret;
106 
107 	if (ttm->caching_state == c_state)
108 		return 0;
109 
110 	if (ttm->state == tt_unpopulated) {
111 		/* Change caching but don't populate */
112 		ttm->caching_state = c_state;
113 		return 0;
114 	}
115 
116 	if (ttm->caching_state == tt_cached)
117 		drm_clflush_pages(ttm->pages, ttm->num_pages);
118 
119 	for (i = 0; i < ttm->num_pages; ++i) {
120 		cur_page = ttm->pages[i];
121 		if (likely(cur_page != NULL)) {
122 			ret = ttm_tt_set_page_caching(cur_page,
123 						      ttm->caching_state,
124 						      c_state);
125 			if (unlikely(ret != 0))
126 				goto out_err;
127 		}
128 	}
129 
130 	ttm->caching_state = c_state;
131 
132 	return 0;
133 
134 out_err:
135 	for (j = 0; j < i; ++j) {
136 		cur_page = ttm->pages[j];
137 		if (cur_page != NULL) {
138 			(void)ttm_tt_set_page_caching(cur_page, c_state,
139 						      ttm->caching_state);
140 		}
141 	}
142 
143 	return ret;
144 }
145 
146 int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
147 {
148 	enum ttm_caching_state state;
149 
150 	if (placement & TTM_PL_FLAG_WC)
151 		state = tt_wc;
152 	else if (placement & TTM_PL_FLAG_UNCACHED)
153 		state = tt_uncached;
154 	else
155 		state = tt_cached;
156 
157 	return ttm_tt_set_caching(ttm, state);
158 }
159 
160 void ttm_tt_destroy(struct ttm_tt *ttm)
161 {
162 	if (unlikely(ttm == NULL))
163 		return;
164 
165 	if (ttm->state == tt_bound) {
166 		ttm_tt_unbind(ttm);
167 	}
168 
169 	if (likely(ttm->pages != NULL)) {
170 		ttm->bdev->driver->ttm_tt_unpopulate(ttm);
171 	}
172 
173 	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
174 	    ttm->swap_storage)
175 		vm_object_deallocate(ttm->swap_storage);
176 
177 	ttm->swap_storage = NULL;
178 	ttm->func->destroy(ttm);
179 }
180 
181 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
182 		unsigned long size, uint32_t page_flags,
183 		vm_page_t dummy_read_page)
184 {
185 	ttm->bdev = bdev;
186 	ttm->glob = bdev->glob;
187 	ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
188 	ttm->caching_state = tt_cached;
189 	ttm->page_flags = page_flags;
190 	ttm->dummy_read_page = dummy_read_page;
191 	ttm->state = tt_unpopulated;
192 	ttm->swap_storage = NULL;
193 
194 	ttm_tt_alloc_page_directory(ttm);
195 	if (!ttm->pages) {
196 		ttm_tt_destroy(ttm);
197 		printf("Failed allocating page table\n");
198 		return -ENOMEM;
199 	}
200 	return 0;
201 }
202 
203 void ttm_tt_fini(struct ttm_tt *ttm)
204 {
205 	free(ttm->pages, M_TTM_PD);
206 	ttm->pages = NULL;
207 }
208 
209 int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
210 		unsigned long size, uint32_t page_flags,
211 		vm_page_t dummy_read_page)
212 {
213 	struct ttm_tt *ttm = &ttm_dma->ttm;
214 
215 	ttm->bdev = bdev;
216 	ttm->glob = bdev->glob;
217 	ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
218 	ttm->caching_state = tt_cached;
219 	ttm->page_flags = page_flags;
220 	ttm->dummy_read_page = dummy_read_page;
221 	ttm->state = tt_unpopulated;
222 	ttm->swap_storage = NULL;
223 
224 	INIT_LIST_HEAD(&ttm_dma->pages_list);
225 	ttm_dma_tt_alloc_page_directory(ttm_dma);
226 	if (!ttm->pages || !ttm_dma->dma_address) {
227 		ttm_tt_destroy(ttm);
228 		printf("Failed allocating page table\n");
229 		return -ENOMEM;
230 	}
231 	return 0;
232 }
233 
234 void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
235 {
236 	struct ttm_tt *ttm = &ttm_dma->ttm;
237 
238 	free(ttm->pages, M_TTM_PD);
239 	ttm->pages = NULL;
240 	free(ttm_dma->dma_address, M_TTM_PD);
241 	ttm_dma->dma_address = NULL;
242 }
243 
244 void ttm_tt_unbind(struct ttm_tt *ttm)
245 {
246 	int ret;
247 
248 	if (ttm->state == tt_bound) {
249 		ret = ttm->func->unbind(ttm);
250 		MPASS(ret == 0);
251 		ttm->state = tt_unbound;
252 	}
253 }
254 
255 int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
256 {
257 	int ret = 0;
258 
259 	if (!ttm)
260 		return -EINVAL;
261 
262 	if (ttm->state == tt_bound)
263 		return 0;
264 
265 	ret = ttm->bdev->driver->ttm_tt_populate(ttm);
266 	if (ret)
267 		return ret;
268 
269 	ret = ttm->func->bind(ttm, bo_mem);
270 	if (unlikely(ret != 0))
271 		return ret;
272 
273 	ttm->state = tt_bound;
274 
275 	return 0;
276 }
277 
278 int ttm_tt_swapin(struct ttm_tt *ttm)
279 {
280 	vm_object_t obj;
281 	vm_page_t from_page, to_page;
282 	int i, ret, rv;
283 
284 	obj = ttm->swap_storage;
285 
286 	vm_object_pip_add(obj, 1);
287 	for (i = 0; i < ttm->num_pages; ++i) {
288 		rv = vm_page_grab_valid_unlocked(&from_page, obj, i,
289 		    VM_ALLOC_NORMAL | VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY);
290 		if (rv != VM_PAGER_OK) {
291 			ret = -EIO;
292 			goto err_ret;
293 		}
294 		to_page = ttm->pages[i];
295 		if (unlikely(to_page == NULL)) {
296 			vm_page_sunbusy(from_page);
297 			ret = -ENOMEM;
298 			goto err_ret;
299 		}
300 		pmap_copy_page(from_page, to_page);
301 		vm_page_sunbusy(from_page);
302 	}
303 	vm_object_pip_wakeup(obj);
304 
305 	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
306 		vm_object_deallocate(obj);
307 	ttm->swap_storage = NULL;
308 	ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
309 	return (0);
310 
311 err_ret:
312 	vm_object_pip_wakeup(obj);
313 	return (ret);
314 }
315 
316 int ttm_tt_swapout(struct ttm_tt *ttm, vm_object_t persistent_swap_storage)
317 {
318 	vm_object_t obj;
319 	vm_page_t from_page, to_page;
320 	int i;
321 
322 	MPASS(ttm->state == tt_unbound || ttm->state == tt_unpopulated);
323 	MPASS(ttm->caching_state == tt_cached);
324 
325 	if (persistent_swap_storage == NULL) {
326 		obj = vm_pager_allocate(OBJT_SWAP, NULL,
327 		    IDX_TO_OFF(ttm->num_pages), VM_PROT_DEFAULT, 0,
328 		    curthread->td_ucred);
329 		if (obj == NULL) {
330 			printf("[TTM] Failed allocating swap storage\n");
331 			return (-ENOMEM);
332 		}
333 	} else
334 		obj = persistent_swap_storage;
335 
336 	VM_OBJECT_WLOCK(obj);
337 	vm_object_pip_add(obj, 1);
338 	for (i = 0; i < ttm->num_pages; ++i) {
339 		from_page = ttm->pages[i];
340 		if (unlikely(from_page == NULL))
341 			continue;
342 		to_page = vm_page_grab(obj, i, VM_ALLOC_NORMAL);
343 		pmap_copy_page(from_page, to_page);
344 		vm_page_valid(to_page);
345 		vm_page_dirty(to_page);
346 		vm_page_xunbusy(to_page);
347 	}
348 	vm_object_pip_wakeup(obj);
349 	VM_OBJECT_WUNLOCK(obj);
350 
351 	ttm->bdev->driver->ttm_tt_unpopulate(ttm);
352 	ttm->swap_storage = obj;
353 	ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
354 	if (persistent_swap_storage != NULL)
355 		ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
356 	return (0);
357 }
358