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>
23 /* to support deferred IO */
24 #include <linux/rmap.h>
25 #include <linux/pagemap.h>
27 /* this is to find and return the vmalloc-ed fb pages */
28 static int fb_deferred_io_fault(struct vm_area_struct *vma,
33 struct fb_info *info = vma->vm_private_data;
34 /* info->screen_base is virtual memory */
35 void *screen_base = (void __force *) info->screen_base;
37 offset = vmf->pgoff << PAGE_SHIFT;
38 if (offset >= info->fix.smem_len)
39 return VM_FAULT_SIGBUS;
41 page = vmalloc_to_page(screen_base + offset);
43 return VM_FAULT_SIGBUS;
48 page->mapping = vma->vm_file->f_mapping;
50 printk(KERN_ERR "no mapping available\n");
52 BUG_ON(!page->mapping);
53 page->index = vmf->pgoff;
59 int fb_deferred_io_fsync(struct file *file, struct dentry *dentry, int datasync)
61 struct fb_info *info = file->private_data;
63 /* Skip if deferred io is complied-in but disabled on this fbdev */
67 /* Kill off the delayed work */
68 cancel_rearming_delayed_work(&info->deferred_work);
70 /* Run it immediately */
71 return schedule_delayed_work(&info->deferred_work, 0);
73 EXPORT_SYMBOL_GPL(fb_deferred_io_fsync);
75 /* vm_ops->page_mkwrite handler */
76 static int fb_deferred_io_mkwrite(struct vm_area_struct *vma,
79 struct fb_info *info = vma->vm_private_data;
80 struct fb_deferred_io *fbdefio = info->fbdefio;
83 /* this is a callback we get when userspace first tries to
84 write to the page. we schedule a workqueue. that workqueue
85 will eventually mkclean the touched pages and execute the
86 deferred framebuffer IO. then if userspace touches a page
87 again, we repeat the same scheme */
89 /* protect against the workqueue changing the page list */
90 mutex_lock(&fbdefio->lock);
92 /* we loop through the pagelist before adding in order
93 to keep the pagelist sorted */
94 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
95 /* this check is to catch the case where a new
96 process could start writing to the same page
97 through a new pte. this new access can cause the
98 mkwrite even when the original ps's pte is marked
100 if (unlikely(cur == page))
101 goto page_already_added;
102 else if (cur->index > page->index)
106 list_add_tail(&page->lru, &cur->lru);
109 mutex_unlock(&fbdefio->lock);
111 /* come back after delay to process the deferred IO */
112 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
116 static struct vm_operations_struct fb_deferred_io_vm_ops = {
117 .fault = fb_deferred_io_fault,
118 .page_mkwrite = fb_deferred_io_mkwrite,
121 static int fb_deferred_io_set_page_dirty(struct page *page)
123 if (!PageDirty(page))
128 static const struct address_space_operations fb_deferred_io_aops = {
129 .set_page_dirty = fb_deferred_io_set_page_dirty,
132 static int fb_deferred_io_mmap(struct fb_info *info, struct vm_area_struct *vma)
134 vma->vm_ops = &fb_deferred_io_vm_ops;
135 vma->vm_flags |= ( VM_IO | VM_RESERVED | VM_DONTEXPAND );
136 vma->vm_private_data = info;
140 /* workqueue callback */
141 static void fb_deferred_io_work(struct work_struct *work)
143 struct fb_info *info = container_of(work, struct fb_info,
145 struct list_head *node, *next;
147 struct fb_deferred_io *fbdefio = info->fbdefio;
149 /* here we mkclean the pages, then do all deferred IO */
150 mutex_lock(&fbdefio->lock);
151 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
157 /* driver's callback with pagelist */
158 fbdefio->deferred_io(info, &fbdefio->pagelist);
161 list_for_each_safe(node, next, &fbdefio->pagelist) {
164 mutex_unlock(&fbdefio->lock);
167 void fb_deferred_io_init(struct fb_info *info)
169 struct fb_deferred_io *fbdefio = info->fbdefio;
172 mutex_init(&fbdefio->lock);
173 info->fbops->fb_mmap = fb_deferred_io_mmap;
174 INIT_DELAYED_WORK(&info->deferred_work, fb_deferred_io_work);
175 INIT_LIST_HEAD(&fbdefio->pagelist);
176 if (fbdefio->delay == 0) /* set a default of 1 s */
179 EXPORT_SYMBOL_GPL(fb_deferred_io_init);
181 void fb_deferred_io_open(struct fb_info *info,
185 file->f_mapping->a_ops = &fb_deferred_io_aops;
187 EXPORT_SYMBOL_GPL(fb_deferred_io_open);
189 void fb_deferred_io_cleanup(struct fb_info *info)
191 void *screen_base = (void __force *) info->screen_base;
192 struct fb_deferred_io *fbdefio = info->fbdefio;
197 cancel_delayed_work(&info->deferred_work);
198 flush_scheduled_work();
200 /* clear out the mapping that we setup */
201 for (i = 0 ; i < info->fix.smem_len; i += PAGE_SIZE) {
202 page = vmalloc_to_page(screen_base + i);
203 page->mapping = NULL;
206 EXPORT_SYMBOL_GPL(fb_deferred_io_cleanup);
208 MODULE_LICENSE("GPL");