blob: a0c5d9d90d741499e5cdb6087d138337c494b3e9 [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 */
28static 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;
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
37 offset = (vaddr - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
38 if (offset >= info->fix.smem_len)
39 return NOPAGE_SIGBUS;
40
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)
43 return NOPAGE_OOM;
44
45 get_page(page);
46 if (type)
47 *type = VM_FAULT_MINOR;
48 return page;
49}
50
Paul Mundt5e841b82007-05-08 00:37:41 -070051int 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}
61EXPORT_SYMBOL_GPL(fb_deferred_io_fsync);
62
Jaya Kumar60b59be2007-05-08 00:37:37 -070063/* vm_ops->page_mkwrite handler */
Adrian Bunk7bf1ea32007-05-08 00:37:38 -070064static int fb_deferred_io_mkwrite(struct vm_area_struct *vma,
65 struct page *page)
Jaya Kumar60b59be2007-05-08 00:37:37 -070066{
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
86static struct vm_operations_struct fb_deferred_io_vm_ops = {
87 .nopage = fb_deferred_io_nopage,
88 .page_mkwrite = fb_deferred_io_mkwrite,
89};
90
91static 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 */
100static 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
126void 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}
138EXPORT_SYMBOL_GPL(fb_deferred_io_init);
139
140void 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}
148EXPORT_SYMBOL_GPL(fb_deferred_io_cleanup);
149
150MODULE_LICENSE("GPL");