2 * linux/drivers/video/fb_defio.c
4 * Copyright (C) 2006 Jaya Kumar
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/delay.h>
19 #include <linux/interrupt.h>
21 #include <linux/list.h>
22 #include <asm/uaccess.h>
24 /* to support deferred IO */
25 #include <linux/rmap.h>
26 #include <linux/pagemap.h>
28 /* this is to find and return the vmalloc-ed fb pages */
29 static struct page
* fb_deferred_io_nopage(struct vm_area_struct
*vma
,
30 unsigned long vaddr
, int *type
)
34 struct fb_info
*info
= vma
->vm_private_data
;
36 offset
= (vaddr
- vma
->vm_start
) + (vma
->vm_pgoff
<< PAGE_SHIFT
);
37 if (offset
>= info
->fix
.smem_len
)
40 page
= vmalloc_to_page(info
->screen_base
+ offset
);
46 *type
= VM_FAULT_MINOR
;
50 /* vm_ops->page_mkwrite handler */
51 int fb_deferred_io_mkwrite(struct vm_area_struct
*vma
,
54 struct fb_info
*info
= vma
->vm_private_data
;
55 struct fb_deferred_io
*fbdefio
= info
->fbdefio
;
57 /* this is a callback we get when userspace first tries to
58 write to the page. we schedule a workqueue. that workqueue
59 will eventually mkclean the touched pages and execute the
60 deferred framebuffer IO. then if userspace touches a page
61 again, we repeat the same scheme */
63 /* protect against the workqueue changing the page list */
64 mutex_lock(&fbdefio
->lock
);
65 list_add(&page
->lru
, &fbdefio
->pagelist
);
66 mutex_unlock(&fbdefio
->lock
);
68 /* come back after delay to process the deferred IO */
69 schedule_delayed_work(&info
->deferred_work
, fbdefio
->delay
);
73 static struct vm_operations_struct fb_deferred_io_vm_ops
= {
74 .nopage
= fb_deferred_io_nopage
,
75 .page_mkwrite
= fb_deferred_io_mkwrite
,
78 static int fb_deferred_io_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
80 vma
->vm_ops
= &fb_deferred_io_vm_ops
;
81 vma
->vm_flags
|= ( VM_IO
| VM_RESERVED
| VM_DONTEXPAND
);
82 vma
->vm_private_data
= info
;
86 /* workqueue callback */
87 static void fb_deferred_io_work(struct work_struct
*work
)
89 struct fb_info
*info
= container_of(work
, struct fb_info
,
91 struct list_head
*node
, *next
;
93 struct fb_deferred_io
*fbdefio
= info
->fbdefio
;
95 /* here we mkclean the pages, then do all deferred IO */
96 mutex_lock(&fbdefio
->lock
);
97 list_for_each_entry(cur
, &fbdefio
->pagelist
, lru
) {
103 /* driver's callback with pagelist */
104 fbdefio
->deferred_io(info
, &fbdefio
->pagelist
);
107 list_for_each_safe(node
, next
, &fbdefio
->pagelist
) {
110 mutex_unlock(&fbdefio
->lock
);
113 void fb_deferred_io_init(struct fb_info
*info
)
115 struct fb_deferred_io
*fbdefio
= info
->fbdefio
;
118 mutex_init(&fbdefio
->lock
);
119 info
->fbops
->fb_mmap
= fb_deferred_io_mmap
;
120 INIT_DELAYED_WORK(&info
->deferred_work
, fb_deferred_io_work
);
121 INIT_LIST_HEAD(&fbdefio
->pagelist
);
122 if (fbdefio
->delay
== 0) /* set a default of 1 s */
125 EXPORT_SYMBOL_GPL(fb_deferred_io_init
);
127 void fb_deferred_io_cleanup(struct fb_info
*info
)
129 struct fb_deferred_io
*fbdefio
= info
->fbdefio
;
132 cancel_delayed_work(&info
->deferred_work
);
133 flush_scheduled_work();
135 EXPORT_SYMBOL_GPL(fb_deferred_io_cleanup
);
137 MODULE_LICENSE("GPL");