blob: 24843fdd5395d981c0d27000b895fd0dc8ba32d9 [file] [log] [blame]
Jaya Kumar60b59be2007-05-08 00:37:37 -07001/*
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
Jaya Kumarde7c6d12008-03-19 17:01:10 -07007 * License. See the file COPYING in the main directory of this archive
Jaya Kumar60b59be2007-05-08 00:37:37 -07008 * 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>
Jaya Kumar60b59be2007-05-08 00:37:37 -070022
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 */
Nick Piggin529e55b2008-02-06 01:39:10 -080028static int fb_deferred_io_fault(struct vm_area_struct *vma,
29 struct vm_fault *vmf)
Jaya Kumar60b59be2007-05-08 00:37:37 -070030{
31 unsigned long offset;
32 struct page *page;
33 struct fb_info *info = vma->vm_private_data;
Jaya Kumarde7c6d12008-03-19 17:01:10 -070034 /* info->screen_base is virtual memory */
Antonino A. Daplas98a11532007-05-08 00:38:44 -070035 void *screen_base = (void __force *) info->screen_base;
Jaya Kumar60b59be2007-05-08 00:37:37 -070036
Nick Piggin529e55b2008-02-06 01:39:10 -080037 offset = vmf->pgoff << PAGE_SHIFT;
Jaya Kumar60b59be2007-05-08 00:37:37 -070038 if (offset >= info->fix.smem_len)
Nick Piggin529e55b2008-02-06 01:39:10 -080039 return VM_FAULT_SIGBUS;
Jaya Kumar60b59be2007-05-08 00:37:37 -070040
Antonino A. Daplas98a11532007-05-08 00:38:44 -070041 page = vmalloc_to_page(screen_base + offset);
Jaya Kumar60b59be2007-05-08 00:37:37 -070042 if (!page)
Nick Piggin529e55b2008-02-06 01:39:10 -080043 return VM_FAULT_SIGBUS;
Jaya Kumar60b59be2007-05-08 00:37:37 -070044
45 get_page(page);
Jaya Kumarde7c6d12008-03-19 17:01:10 -070046
47 if (vma->vm_file)
48 page->mapping = vma->vm_file->f_mapping;
49 else
50 printk(KERN_ERR "no mapping available\n");
51
52 BUG_ON(!page->mapping);
53 page->index = vmf->pgoff;
54
Nick Piggin529e55b2008-02-06 01:39:10 -080055 vmf->page = page;
56 return 0;
Jaya Kumar60b59be2007-05-08 00:37:37 -070057}
58
Paul Mundt5e841b82007-05-08 00:37:41 -070059int fb_deferred_io_fsync(struct file *file, struct dentry *dentry, int datasync)
60{
61 struct fb_info *info = file->private_data;
62
63 /* Kill off the delayed work */
64 cancel_rearming_delayed_work(&info->deferred_work);
65
66 /* Run it immediately */
67 return schedule_delayed_work(&info->deferred_work, 0);
68}
69EXPORT_SYMBOL_GPL(fb_deferred_io_fsync);
70
Jaya Kumar60b59be2007-05-08 00:37:37 -070071/* vm_ops->page_mkwrite handler */
Adrian Bunk7bf1ea32007-05-08 00:37:38 -070072static int fb_deferred_io_mkwrite(struct vm_area_struct *vma,
73 struct page *page)
Jaya Kumar60b59be2007-05-08 00:37:37 -070074{
75 struct fb_info *info = vma->vm_private_data;
76 struct fb_deferred_io *fbdefio = info->fbdefio;
77
78 /* this is a callback we get when userspace first tries to
79 write to the page. we schedule a workqueue. that workqueue
80 will eventually mkclean the touched pages and execute the
81 deferred framebuffer IO. then if userspace touches a page
82 again, we repeat the same scheme */
83
84 /* protect against the workqueue changing the page list */
85 mutex_lock(&fbdefio->lock);
86 list_add(&page->lru, &fbdefio->pagelist);
87 mutex_unlock(&fbdefio->lock);
88
89 /* come back after delay to process the deferred IO */
90 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
91 return 0;
92}
93
94static struct vm_operations_struct fb_deferred_io_vm_ops = {
Nick Piggin529e55b2008-02-06 01:39:10 -080095 .fault = fb_deferred_io_fault,
Jaya Kumar60b59be2007-05-08 00:37:37 -070096 .page_mkwrite = fb_deferred_io_mkwrite,
97};
98
99static int fb_deferred_io_mmap(struct fb_info *info, struct vm_area_struct *vma)
100{
101 vma->vm_ops = &fb_deferred_io_vm_ops;
102 vma->vm_flags |= ( VM_IO | VM_RESERVED | VM_DONTEXPAND );
103 vma->vm_private_data = info;
104 return 0;
105}
106
107/* workqueue callback */
108static void fb_deferred_io_work(struct work_struct *work)
109{
110 struct fb_info *info = container_of(work, struct fb_info,
111 deferred_work.work);
112 struct list_head *node, *next;
113 struct page *cur;
114 struct fb_deferred_io *fbdefio = info->fbdefio;
115
116 /* here we mkclean the pages, then do all deferred IO */
117 mutex_lock(&fbdefio->lock);
118 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
119 lock_page(cur);
120 page_mkclean(cur);
121 unlock_page(cur);
122 }
123
124 /* driver's callback with pagelist */
125 fbdefio->deferred_io(info, &fbdefio->pagelist);
126
127 /* clear the list */
128 list_for_each_safe(node, next, &fbdefio->pagelist) {
129 list_del(node);
130 }
131 mutex_unlock(&fbdefio->lock);
132}
133
134void fb_deferred_io_init(struct fb_info *info)
135{
136 struct fb_deferred_io *fbdefio = info->fbdefio;
137
138 BUG_ON(!fbdefio);
139 mutex_init(&fbdefio->lock);
140 info->fbops->fb_mmap = fb_deferred_io_mmap;
141 INIT_DELAYED_WORK(&info->deferred_work, fb_deferred_io_work);
142 INIT_LIST_HEAD(&fbdefio->pagelist);
143 if (fbdefio->delay == 0) /* set a default of 1 s */
144 fbdefio->delay = HZ;
145}
146EXPORT_SYMBOL_GPL(fb_deferred_io_init);
147
148void fb_deferred_io_cleanup(struct fb_info *info)
149{
Jaya Kumarde7c6d12008-03-19 17:01:10 -0700150 void *screen_base = (void __force *) info->screen_base;
Jaya Kumar60b59be2007-05-08 00:37:37 -0700151 struct fb_deferred_io *fbdefio = info->fbdefio;
Jaya Kumarde7c6d12008-03-19 17:01:10 -0700152 struct page *page;
153 int i;
Jaya Kumar60b59be2007-05-08 00:37:37 -0700154
155 BUG_ON(!fbdefio);
156 cancel_delayed_work(&info->deferred_work);
157 flush_scheduled_work();
Jaya Kumarde7c6d12008-03-19 17:01:10 -0700158
159 /* clear out the mapping that we setup */
160 for (i = 0 ; i < info->fix.smem_len; i += PAGE_SIZE) {
161 page = vmalloc_to_page(screen_base + i);
162 page->mapping = NULL;
163 }
Jaya Kumar60b59be2007-05-08 00:37:37 -0700164}
165EXPORT_SYMBOL_GPL(fb_deferred_io_cleanup);
166
167MODULE_LICENSE("GPL");