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