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__FBSDID("$FreeBSD: stable/10/sys/dev/drm2/ttm/ttm_bo_vm.c 307672 2016-10-20 13:12:19Z kib $");
40
41#include "opt_vm.h"
42
43#include <dev/drm2/drmP.h>
44#include <dev/drm2/ttm/ttm_module.h>
45#include <dev/drm2/ttm/ttm_bo_driver.h>
46#include <dev/drm2/ttm/ttm_placement.h>
47
48#include <vm/vm.h>
49#include <vm/vm_page.h>
50#include <vm/vm_pageout.h>
51
52#define TTM_BO_VM_NUM_PREFAULT 16
53
54RB_GENERATE(ttm_bo_device_buffer_objects, ttm_buffer_object, vm_rb,
55    ttm_bo_cmp_rb_tree_items);
56
57int
58ttm_bo_cmp_rb_tree_items(struct ttm_buffer_object *a,
59    struct ttm_buffer_object *b)
60{
61
62	if (a->vm_node->start < b->vm_node->start) {
63		return (-1);
64	} else if (a->vm_node->start > b->vm_node->start) {
65		return (1);
66	} else {
67		return (0);
68	}
69}
70
71static struct ttm_buffer_object *ttm_bo_vm_lookup_rb(struct ttm_bo_device *bdev,
72						     unsigned long page_start,
73						     unsigned long num_pages)
74{
75	unsigned long cur_offset;
76	struct ttm_buffer_object *bo;
77	struct ttm_buffer_object *best_bo = NULL;
78
79	bo = RB_ROOT(&bdev->addr_space_rb);
80	while (bo != NULL) {
81		cur_offset = bo->vm_node->start;
82		if (page_start >= cur_offset) {
83			best_bo = bo;
84			if (page_start == cur_offset)
85				break;
86			bo = RB_RIGHT(bo, vm_rb);
87		} else
88			bo = RB_LEFT(bo, vm_rb);
89	}
90
91	if (unlikely(best_bo == NULL))
92		return NULL;
93
94	if (unlikely((best_bo->vm_node->start + best_bo->num_pages) <
95		     (page_start + num_pages)))
96		return NULL;
97
98	return best_bo;
99}
100
101static int
102ttm_bo_vm_fault(vm_object_t vm_obj, vm_ooffset_t offset,
103    int prot, vm_page_t *mres)
104{
105
106	struct ttm_buffer_object *bo = vm_obj->handle;
107	struct ttm_bo_device *bdev = bo->bdev;
108	struct ttm_tt *ttm = NULL;
109	vm_page_t m, m1, oldm;
110	int ret;
111	int retval = VM_PAGER_OK;
112	struct ttm_mem_type_manager *man =
113		&bdev->man[bo->mem.mem_type];
114
115	vm_object_pip_add(vm_obj, 1);
116	oldm = *mres;
117	if (oldm != NULL) {
118		vm_page_lock(oldm);
119		vm_page_remove(oldm);
120		vm_page_unlock(oldm);
121		*mres = NULL;
122	} else
123		oldm = NULL;
124retry:
125	VM_OBJECT_WUNLOCK(vm_obj);
126	m = NULL;
127
128reserve:
129	ret = ttm_bo_reserve(bo, false, false, false, 0);
130	if (unlikely(ret != 0)) {
131		if (ret == -EBUSY) {
132			kern_yield(0);
133			goto reserve;
134		}
135	}
136
137	if (bdev->driver->fault_reserve_notify) {
138		ret = bdev->driver->fault_reserve_notify(bo);
139		switch (ret) {
140		case 0:
141			break;
142		case -EBUSY:
143		case -ERESTARTSYS:
144		case -EINTR:
145			kern_yield(0);
146			goto reserve;
147		default:
148			retval = VM_PAGER_ERROR;
149			goto out_unlock;
150		}
151	}
152
153	/*
154	 * Wait for buffer data in transit, due to a pipelined
155	 * move.
156	 */
157
158	mtx_lock(&bdev->fence_lock);
159	if (test_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags)) {
160		/*
161		 * Here, the behavior differs between Linux and FreeBSD.
162		 *
163		 * On Linux, the wait is interruptible (3rd argument to
164		 * ttm_bo_wait). There must be some mechanism to resume
165		 * page fault handling, once the signal is processed.
166		 *
167		 * On FreeBSD, the wait is uninteruptible. This is not a
168		 * problem as we can't end up with an unkillable process
169		 * here, because the wait will eventually time out.
170		 *
171		 * An example of this situation is the Xorg process
172		 * which uses SIGALRM internally. The signal could
173		 * interrupt the wait, causing the page fault to fail
174		 * and the process to receive SIGSEGV.
175		 */
176		ret = ttm_bo_wait(bo, false, false, false);
177		mtx_unlock(&bdev->fence_lock);
178		if (unlikely(ret != 0)) {
179			retval = VM_PAGER_ERROR;
180			goto out_unlock;
181		}
182	} else
183		mtx_unlock(&bdev->fence_lock);
184
185	ret = ttm_mem_io_lock(man, true);
186	if (unlikely(ret != 0)) {
187		retval = VM_PAGER_ERROR;
188		goto out_unlock;
189	}
190	ret = ttm_mem_io_reserve_vm(bo);
191	if (unlikely(ret != 0)) {
192		retval = VM_PAGER_ERROR;
193		goto out_io_unlock;
194	}
195
196	/*
197	 * Strictly, we're not allowed to modify vma->vm_page_prot here,
198	 * since the mmap_sem is only held in read mode. However, we
199	 * modify only the caching bits of vma->vm_page_prot and
200	 * consider those bits protected by
201	 * the bo->mutex, as we should be the only writers.
202	 * There shouldn't really be any readers of these bits except
203	 * within vm_insert_mixed()? fork?
204	 *
205	 * TODO: Add a list of vmas to the bo, and change the
206	 * vma->vm_page_prot when the object changes caching policy, with
207	 * the correct locks held.
208	 */
209	if (!bo->mem.bus.is_iomem) {
210		/* Allocate all page at once, most common usage */
211		ttm = bo->ttm;
212		if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
213			retval = VM_PAGER_ERROR;
214			goto out_io_unlock;
215		}
216	}
217
218	if (bo->mem.bus.is_iomem) {
219		m = PHYS_TO_VM_PAGE(bo->mem.bus.base + bo->mem.bus.offset +
220		    offset);
221		KASSERT((m->flags & PG_FICTITIOUS) != 0,
222		    ("physical address %#jx not fictitious",
223		    (uintmax_t)(bo->mem.bus.base + bo->mem.bus.offset
224		    + offset)));
225		pmap_page_set_memattr(m, ttm_io_prot(bo->mem.placement));
226	} else {
227		ttm = bo->ttm;
228		m = ttm->pages[OFF_TO_IDX(offset)];
229		if (unlikely(!m)) {
230			retval = VM_PAGER_ERROR;
231			goto out_io_unlock;
232		}
233		pmap_page_set_memattr(m,
234		    (bo->mem.placement & TTM_PL_FLAG_CACHED) ?
235		    VM_MEMATTR_WRITE_BACK : ttm_io_prot(bo->mem.placement));
236	}
237
238	VM_OBJECT_WLOCK(vm_obj);
239	if (vm_page_busied(m)) {
240		vm_page_lock(m);
241		VM_OBJECT_WUNLOCK(vm_obj);
242		vm_page_busy_sleep(m, "ttmpbs", false);
243		VM_OBJECT_WLOCK(vm_obj);
244		ttm_mem_io_unlock(man);
245		ttm_bo_unreserve(bo);
246		goto retry;
247	}
248	m1 = vm_page_lookup(vm_obj, OFF_TO_IDX(offset));
249	if (m1 == NULL) {
250		if (vm_page_insert(m, vm_obj, OFF_TO_IDX(offset))) {
251			VM_OBJECT_WUNLOCK(vm_obj);
252			VM_WAIT;
253			VM_OBJECT_WLOCK(vm_obj);
254			ttm_mem_io_unlock(man);
255			ttm_bo_unreserve(bo);
256			goto retry;
257		}
258	} else {
259		KASSERT(m == m1,
260		    ("inconsistent insert bo %p m %p m1 %p offset %jx",
261		    bo, m, m1, (uintmax_t)offset));
262	}
263	m->valid = VM_PAGE_BITS_ALL;
264	*mres = m;
265	vm_page_xbusy(m);
266
267	if (oldm != NULL) {
268		vm_page_lock(oldm);
269		vm_page_free(oldm);
270		vm_page_unlock(oldm);
271	}
272
273out_io_unlock1:
274	ttm_mem_io_unlock(man);
275out_unlock1:
276	ttm_bo_unreserve(bo);
277	vm_object_pip_wakeup(vm_obj);
278	return (retval);
279
280out_io_unlock:
281	VM_OBJECT_WLOCK(vm_obj);
282	goto out_io_unlock1;
283
284out_unlock:
285	VM_OBJECT_WLOCK(vm_obj);
286	goto out_unlock1;
287}
288
289static int
290ttm_bo_vm_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
291    vm_ooffset_t foff, struct ucred *cred, u_short *color)
292{
293
294	/*
295	 * On Linux, a reference to the buffer object is acquired here.
296	 * The reason is that this function is not called when the
297	 * mmap() is initialized, but only when a process forks for
298	 * instance. Therefore on Linux, the reference on the bo is
299	 * acquired either in ttm_bo_mmap() or ttm_bo_vm_open(). It's
300	 * then released in ttm_bo_vm_close().
301	 *
302	 * Here, this function is called during mmap() initialization.
303	 * Thus, the reference acquired in ttm_bo_mmap_single() is
304	 * sufficient.
305	 */
306
307	*color = 0;
308	return (0);
309}
310
311static void
312ttm_bo_vm_dtor(void *handle)
313{
314	struct ttm_buffer_object *bo = handle;
315
316	ttm_bo_unref(&bo);
317}
318
319static struct cdev_pager_ops ttm_pager_ops = {
320	.cdev_pg_fault = ttm_bo_vm_fault,
321	.cdev_pg_ctor = ttm_bo_vm_ctor,
322	.cdev_pg_dtor = ttm_bo_vm_dtor
323};
324
325int
326ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset, vm_size_t size,
327    struct vm_object **obj_res, int nprot)
328{
329	struct ttm_bo_driver *driver;
330	struct ttm_buffer_object *bo;
331	struct vm_object *vm_obj;
332	int ret;
333
334	rw_wlock(&bdev->vm_lock);
335	bo = ttm_bo_vm_lookup_rb(bdev, OFF_TO_IDX(*offset), OFF_TO_IDX(size));
336	if (likely(bo != NULL))
337		refcount_acquire(&bo->kref);
338	rw_wunlock(&bdev->vm_lock);
339
340	if (unlikely(bo == NULL)) {
341		printf("[TTM] Could not find buffer object to map\n");
342		return (-EINVAL);
343	}
344
345	driver = bo->bdev->driver;
346	if (unlikely(!driver->verify_access)) {
347		ret = -EPERM;
348		goto out_unref;
349	}
350	ret = driver->verify_access(bo);
351	if (unlikely(ret != 0))
352		goto out_unref;
353
354	vm_obj = cdev_pager_allocate(bo, OBJT_MGTDEVICE, &ttm_pager_ops,
355	    size, nprot, 0, curthread->td_ucred);
356	if (vm_obj == NULL) {
357		ret = -EINVAL;
358		goto out_unref;
359	}
360	/*
361	 * Note: We're transferring the bo reference to vm_obj->handle here.
362	 */
363	*offset = 0;
364	*obj_res = vm_obj;
365	return 0;
366out_unref:
367	ttm_bo_unref(&bo);
368	return ret;
369}
370
371void
372ttm_bo_release_mmap(struct ttm_buffer_object *bo)
373{
374	vm_object_t vm_obj;
375	vm_page_t m;
376	int i;
377
378	vm_obj = cdev_pager_lookup(bo);
379	if (vm_obj == NULL)
380		return;
381
382	VM_OBJECT_WLOCK(vm_obj);
383retry:
384	for (i = 0; i < bo->num_pages; i++) {
385		m = vm_page_lookup(vm_obj, i);
386		if (m == NULL)
387			continue;
388		if (vm_page_sleep_if_busy(m, "ttm_unm"))
389			goto retry;
390		cdev_pager_free_page(vm_obj, m);
391	}
392	VM_OBJECT_WUNLOCK(vm_obj);
393
394	vm_object_deallocate(vm_obj);
395}
396
397#if 0
398int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
399{
400	if (vma->vm_pgoff != 0)
401		return -EACCES;
402
403	vma->vm_ops = &ttm_bo_vm_ops;
404	vma->vm_private_data = ttm_bo_reference(bo);
405	vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
406	return 0;
407}
408
409ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
410		  const char __user *wbuf, char __user *rbuf, size_t count,
411		  loff_t *f_pos, bool write)
412{
413	struct ttm_buffer_object *bo;
414	struct ttm_bo_driver *driver;
415	struct ttm_bo_kmap_obj map;
416	unsigned long dev_offset = (*f_pos >> PAGE_SHIFT);
417	unsigned long kmap_offset;
418	unsigned long kmap_end;
419	unsigned long kmap_num;
420	size_t io_size;
421	unsigned int page_offset;
422	char *virtual;
423	int ret;
424	bool no_wait = false;
425	bool dummy;
426
427	read_lock(&bdev->vm_lock);
428	bo = ttm_bo_vm_lookup_rb(bdev, dev_offset, 1);
429	if (likely(bo != NULL))
430		ttm_bo_reference(bo);
431	read_unlock(&bdev->vm_lock);
432
433	if (unlikely(bo == NULL))
434		return -EFAULT;
435
436	driver = bo->bdev->driver;
437	if (unlikely(!driver->verify_access)) {
438		ret = -EPERM;
439		goto out_unref;
440	}
441
442	ret = driver->verify_access(bo, filp);
443	if (unlikely(ret != 0))
444		goto out_unref;
445
446	kmap_offset = dev_offset - bo->vm_node->start;
447	if (unlikely(kmap_offset >= bo->num_pages)) {
448		ret = -EFBIG;
449		goto out_unref;
450	}
451
452	page_offset = *f_pos & ~PAGE_MASK;
453	io_size = bo->num_pages - kmap_offset;
454	io_size = (io_size << PAGE_SHIFT) - page_offset;
455	if (count < io_size)
456		io_size = count;
457
458	kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
459	kmap_num = kmap_end - kmap_offset + 1;
460
461	ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
462
463	switch (ret) {
464	case 0:
465		break;
466	case -EBUSY:
467		ret = -EAGAIN;
468		goto out_unref;
469	default:
470		goto out_unref;
471	}
472
473	ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
474	if (unlikely(ret != 0)) {
475		ttm_bo_unreserve(bo);
476		goto out_unref;
477	}
478
479	virtual = ttm_kmap_obj_virtual(&map, &dummy);
480	virtual += page_offset;
481
482	if (write)
483		ret = copy_from_user(virtual, wbuf, io_size);
484	else
485		ret = copy_to_user(rbuf, virtual, io_size);
486
487	ttm_bo_kunmap(&map);
488	ttm_bo_unreserve(bo);
489	ttm_bo_unref(&bo);
490
491	if (unlikely(ret != 0))
492		return -EFBIG;
493
494	*f_pos += io_size;
495
496	return io_size;
497out_unref:
498	ttm_bo_unref(&bo);
499	return ret;
500}
501
502ssize_t ttm_bo_fbdev_io(struct ttm_buffer_object *bo, const char __user *wbuf,
503			char __user *rbuf, size_t count, loff_t *f_pos,
504			bool write)
505{
506	struct ttm_bo_kmap_obj map;
507	unsigned long kmap_offset;
508	unsigned long kmap_end;
509	unsigned long kmap_num;
510	size_t io_size;
511	unsigned int page_offset;
512	char *virtual;
513	int ret;
514	bool no_wait = false;
515	bool dummy;
516
517	kmap_offset = (*f_pos >> PAGE_SHIFT);
518	if (unlikely(kmap_offset >= bo->num_pages))
519		return -EFBIG;
520
521	page_offset = *f_pos & ~PAGE_MASK;
522	io_size = bo->num_pages - kmap_offset;
523	io_size = (io_size << PAGE_SHIFT) - page_offset;
524	if (count < io_size)
525		io_size = count;
526
527	kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
528	kmap_num = kmap_end - kmap_offset + 1;
529
530	ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
531
532	switch (ret) {
533	case 0:
534		break;
535	case -EBUSY:
536		return -EAGAIN;
537	default:
538		return ret;
539	}
540
541	ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
542	if (unlikely(ret != 0)) {
543		ttm_bo_unreserve(bo);
544		return ret;
545	}
546
547	virtual = ttm_kmap_obj_virtual(&map, &dummy);
548	virtual += page_offset;
549
550	if (write)
551		ret = copy_from_user(virtual, wbuf, io_size);
552	else
553		ret = copy_to_user(rbuf, virtual, io_size);
554
555	ttm_bo_kunmap(&map);
556	ttm_bo_unreserve(bo);
557	ttm_bo_unref(&bo);
558
559	if (unlikely(ret != 0))
560		return ret;
561
562	*f_pos += io_size;
563
564	return io_size;
565}
566#endif
567