Merge branch 'for-2.6.24' of master.kernel.org:/pub/scm/linux/kernel/git/olof/pasemi...
[linux-2.6] / drivers / video / fb_defio.c
1 /*
2  *  linux/drivers/video/fb_defio.c
3  *
4  *  Copyright (C) 2006 Jaya Kumar
5  *
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
8  * for more details.
9  */
10
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/mm.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/delay.h>
19 #include <linux/interrupt.h>
20 #include <linux/fb.h>
21 #include <linux/list.h>
22
23 /* to support deferred IO */
24 #include <linux/rmap.h>
25 #include <linux/pagemap.h>
26
27 /* this is to find and return the vmalloc-ed fb pages */
28 static struct page* fb_deferred_io_nopage(struct vm_area_struct *vma,
29                                         unsigned long vaddr, int *type)
30 {
31         unsigned long offset;
32         struct page *page;
33         struct fb_info *info = vma->vm_private_data;
34         /* info->screen_base is in System RAM */
35         void *screen_base = (void __force *) info->screen_base;
36
37         offset = (vaddr - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
38         if (offset >= info->fix.smem_len)
39                 return NOPAGE_SIGBUS;
40
41         page = vmalloc_to_page(screen_base + offset);
42         if (!page)
43                 return NOPAGE_OOM;
44
45         get_page(page);
46         if (type)
47                 *type = VM_FAULT_MINOR;
48         return page;
49 }
50
51 int fb_deferred_io_fsync(struct file *file, struct dentry *dentry, int datasync)
52 {
53         struct fb_info *info = file->private_data;
54
55         /* Kill off the delayed work */
56         cancel_rearming_delayed_work(&info->deferred_work);
57
58         /* Run it immediately */
59         return schedule_delayed_work(&info->deferred_work, 0);
60 }
61 EXPORT_SYMBOL_GPL(fb_deferred_io_fsync);
62
63 /* vm_ops->page_mkwrite handler */
64 static int fb_deferred_io_mkwrite(struct vm_area_struct *vma,
65                                   struct page *page)
66 {
67         struct fb_info *info = vma->vm_private_data;
68         struct fb_deferred_io *fbdefio = info->fbdefio;
69
70         /* this is a callback we get when userspace first tries to
71         write to the page. we schedule a workqueue. that workqueue
72         will eventually mkclean the touched pages and execute the
73         deferred framebuffer IO. then if userspace touches a page
74         again, we repeat the same scheme */
75
76         /* protect against the workqueue changing the page list */
77         mutex_lock(&fbdefio->lock);
78         list_add(&page->lru, &fbdefio->pagelist);
79         mutex_unlock(&fbdefio->lock);
80
81         /* come back after delay to process the deferred IO */
82         schedule_delayed_work(&info->deferred_work, fbdefio->delay);
83         return 0;
84 }
85
86 static struct vm_operations_struct fb_deferred_io_vm_ops = {
87         .nopage         = fb_deferred_io_nopage,
88         .page_mkwrite   = fb_deferred_io_mkwrite,
89 };
90
91 static int fb_deferred_io_mmap(struct fb_info *info, struct vm_area_struct *vma)
92 {
93         vma->vm_ops = &fb_deferred_io_vm_ops;
94         vma->vm_flags |= ( VM_IO | VM_RESERVED | VM_DONTEXPAND );
95         vma->vm_private_data = info;
96         return 0;
97 }
98
99 /* workqueue callback */
100 static void fb_deferred_io_work(struct work_struct *work)
101 {
102         struct fb_info *info = container_of(work, struct fb_info,
103                                                 deferred_work.work);
104         struct list_head *node, *next;
105         struct page *cur;
106         struct fb_deferred_io *fbdefio = info->fbdefio;
107
108         /* here we mkclean the pages, then do all deferred IO */
109         mutex_lock(&fbdefio->lock);
110         list_for_each_entry(cur, &fbdefio->pagelist, lru) {
111                 lock_page(cur);
112                 page_mkclean(cur);
113                 unlock_page(cur);
114         }
115
116         /* driver's callback with pagelist */
117         fbdefio->deferred_io(info, &fbdefio->pagelist);
118
119         /* clear the list */
120         list_for_each_safe(node, next, &fbdefio->pagelist) {
121                 list_del(node);
122         }
123         mutex_unlock(&fbdefio->lock);
124 }
125
126 void fb_deferred_io_init(struct fb_info *info)
127 {
128         struct fb_deferred_io *fbdefio = info->fbdefio;
129
130         BUG_ON(!fbdefio);
131         mutex_init(&fbdefio->lock);
132         info->fbops->fb_mmap = fb_deferred_io_mmap;
133         INIT_DELAYED_WORK(&info->deferred_work, fb_deferred_io_work);
134         INIT_LIST_HEAD(&fbdefio->pagelist);
135         if (fbdefio->delay == 0) /* set a default of 1 s */
136                 fbdefio->delay = HZ;
137 }
138 EXPORT_SYMBOL_GPL(fb_deferred_io_init);
139
140 void fb_deferred_io_cleanup(struct fb_info *info)
141 {
142         struct fb_deferred_io *fbdefio = info->fbdefio;
143
144         BUG_ON(!fbdefio);
145         cancel_delayed_work(&info->deferred_work);
146         flush_scheduled_work();
147 }
148 EXPORT_SYMBOL_GPL(fb_deferred_io_cleanup);
149
150 MODULE_LICENSE("GPL");