blob: 0f8cfb988c901a7f45314387149a759d8b252929 [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
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>
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;
Antonino A. Daplas98a11532007-05-08 00:38:44 -070034 /* info->screen_base is in System RAM */
35 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);
Nick Piggin529e55b2008-02-06 01:39:10 -080046 vmf->page = page;
47 return 0;
Jaya Kumar60b59be2007-05-08 00:37:37 -070048}
49
Paul Mundt5e841b82007-05-08 00:37:41 -070050int fb_deferred_io_fsync(struct file *file, struct dentry *dentry, int datasync)
51{
52 struct fb_info *info = file->private_data;
53
54 /* Kill off the delayed work */
55 cancel_rearming_delayed_work(&info->deferred_work);
56
57 /* Run it immediately */
58 return schedule_delayed_work(&info->deferred_work, 0);
59}
60EXPORT_SYMBOL_GPL(fb_deferred_io_fsync);
61
Jaya Kumar60b59be2007-05-08 00:37:37 -070062/* vm_ops->page_mkwrite handler */
Adrian Bunk7bf1ea32007-05-08 00:37:38 -070063static int fb_deferred_io_mkwrite(struct vm_area_struct *vma,
64 struct page *page)
Jaya Kumar60b59be2007-05-08 00:37:37 -070065{
66 struct fb_info *info = vma->vm_private_data;
67 struct fb_deferred_io *fbdefio = info->fbdefio;
68
69 /* this is a callback we get when userspace first tries to
70 write to the page. we schedule a workqueue. that workqueue
71 will eventually mkclean the touched pages and execute the
72 deferred framebuffer IO. then if userspace touches a page
73 again, we repeat the same scheme */
74
75 /* protect against the workqueue changing the page list */
76 mutex_lock(&fbdefio->lock);
77 list_add(&page->lru, &fbdefio->pagelist);
78 mutex_unlock(&fbdefio->lock);
79
80 /* come back after delay to process the deferred IO */
81 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
82 return 0;
83}
84
85static struct vm_operations_struct fb_deferred_io_vm_ops = {
Nick Piggin529e55b2008-02-06 01:39:10 -080086 .fault = fb_deferred_io_fault,
Jaya Kumar60b59be2007-05-08 00:37:37 -070087 .page_mkwrite = fb_deferred_io_mkwrite,
88};
89
90static int fb_deferred_io_mmap(struct fb_info *info, struct vm_area_struct *vma)
91{
92 vma->vm_ops = &fb_deferred_io_vm_ops;
93 vma->vm_flags |= ( VM_IO | VM_RESERVED | VM_DONTEXPAND );
94 vma->vm_private_data = info;
95 return 0;
96}
97
98/* workqueue callback */
99static void fb_deferred_io_work(struct work_struct *work)
100{
101 struct fb_info *info = container_of(work, struct fb_info,
102 deferred_work.work);
103 struct list_head *node, *next;
104 struct page *cur;
105 struct fb_deferred_io *fbdefio = info->fbdefio;
106
107 /* here we mkclean the pages, then do all deferred IO */
108 mutex_lock(&fbdefio->lock);
109 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
110 lock_page(cur);
111 page_mkclean(cur);
112 unlock_page(cur);
113 }
114
115 /* driver's callback with pagelist */
116 fbdefio->deferred_io(info, &fbdefio->pagelist);
117
118 /* clear the list */
119 list_for_each_safe(node, next, &fbdefio->pagelist) {
120 list_del(node);
121 }
122 mutex_unlock(&fbdefio->lock);
123}
124
125void fb_deferred_io_init(struct fb_info *info)
126{
127 struct fb_deferred_io *fbdefio = info->fbdefio;
128
129 BUG_ON(!fbdefio);
130 mutex_init(&fbdefio->lock);
131 info->fbops->fb_mmap = fb_deferred_io_mmap;
132 INIT_DELAYED_WORK(&info->deferred_work, fb_deferred_io_work);
133 INIT_LIST_HEAD(&fbdefio->pagelist);
134 if (fbdefio->delay == 0) /* set a default of 1 s */
135 fbdefio->delay = HZ;
136}
137EXPORT_SYMBOL_GPL(fb_deferred_io_init);
138
139void fb_deferred_io_cleanup(struct fb_info *info)
140{
141 struct fb_deferred_io *fbdefio = info->fbdefio;
142
143 BUG_ON(!fbdefio);
144 cancel_delayed_work(&info->deferred_work);
145 flush_scheduled_work();
146}
147EXPORT_SYMBOL_GPL(fb_deferred_io_cleanup);
148
149MODULE_LICENSE("GPL");