fb_defio.c 6.2 KB
Newer Older
J
Jaya Kumar 已提交
1 2 3 4 5 6
/*
 *  linux/drivers/video/fb_defio.c
 *
 *  Copyright (C) 2006 Jaya Kumar
 *
 * This file is subject to the terms and conditions of the GNU General Public
J
Jaya Kumar 已提交
7
 * License. See the file COPYING in the main directory of this archive
J
Jaya Kumar 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * for more details.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/fb.h>
#include <linux/list.h>

/* to support deferred IO */
#include <linux/rmap.h>
#include <linux/pagemap.h>

26
static struct page *fb_deferred_io_page(struct fb_info *info, unsigned long offs)
27 28 29 30 31 32 33 34 35 36 37 38
{
	void *screen_base = (void __force *) info->screen_base;
	struct page *page;

	if (is_vmalloc_addr(screen_base + offs))
		page = vmalloc_to_page(screen_base + offs);
	else
		page = pfn_to_page((info->fix.smem_start + offs) >> PAGE_SHIFT);

	return page;
}

J
Jaya Kumar 已提交
39
/* this is to find and return the vmalloc-ed fb pages */
40
static vm_fault_t fb_deferred_io_fault(struct vm_fault *vmf)
J
Jaya Kumar 已提交
41 42 43
{
	unsigned long offset;
	struct page *page;
44
	struct fb_info *info = vmf->vma->vm_private_data;
J
Jaya Kumar 已提交
45

N
Nick Piggin 已提交
46
	offset = vmf->pgoff << PAGE_SHIFT;
J
Jaya Kumar 已提交
47
	if (offset >= info->fix.smem_len)
N
Nick Piggin 已提交
48
		return VM_FAULT_SIGBUS;
J
Jaya Kumar 已提交
49

50
	page = fb_deferred_io_page(info, offset);
J
Jaya Kumar 已提交
51
	if (!page)
N
Nick Piggin 已提交
52
		return VM_FAULT_SIGBUS;
J
Jaya Kumar 已提交
53 54

	get_page(page);
55 56 57 58 59 60 61

	if (vmf->vma->vm_file)
		page->mapping = vmf->vma->vm_file->f_mapping;
	else
		printk(KERN_ERR "no mapping available\n");

	BUG_ON(!page->mapping);
J
Jaya Kumar 已提交
62 63
	page->index = vmf->pgoff;

N
Nick Piggin 已提交
64 65
	vmf->page = page;
	return 0;
J
Jaya Kumar 已提交
66 67
}

68
int fb_deferred_io_fsync(struct file *file, loff_t start, loff_t end, int datasync)
69 70
{
	struct fb_info *info = file->private_data;
A
Al Viro 已提交
71
	struct inode *inode = file_inode(file);
72
	int err = file_write_and_wait_range(file, start, end);
73 74
	if (err)
		return err;
75

76
	/* Skip if deferred io is compiled-in but disabled on this fbdev */
M
Magnus Damm 已提交
77 78 79
	if (!info->fbdefio)
		return 0;

A
Al Viro 已提交
80
	inode_lock(inode);
81
	/* Kill off the delayed work */
82
	cancel_delayed_work_sync(&info->deferred_work);
83 84

	/* Run it immediately */
T
Tomi Valkeinen 已提交
85
	schedule_delayed_work(&info->deferred_work, 0);
A
Al Viro 已提交
86
	inode_unlock(inode);
T
Tomi Valkeinen 已提交
87 88

	return 0;
89 90 91
}
EXPORT_SYMBOL_GPL(fb_deferred_io_fsync);

J
Jaya Kumar 已提交
92
/* vm_ops->page_mkwrite handler */
93
static vm_fault_t fb_deferred_io_mkwrite(struct vm_fault *vmf)
J
Jaya Kumar 已提交
94
{
95
	struct page *page = vmf->page;
96
	struct fb_info *info = vmf->vma->vm_private_data;
J
Jaya Kumar 已提交
97
	struct fb_deferred_io *fbdefio = info->fbdefio;
98
	struct page *cur;
J
Jaya Kumar 已提交
99 100 101 102 103 104 105

	/* this is a callback we get when userspace first tries to
	write to the page. we schedule a workqueue. that workqueue
	will eventually mkclean the touched pages and execute the
	deferred framebuffer IO. then if userspace touches a page
	again, we repeat the same scheme */

106
	file_update_time(vmf->vma->vm_file);
107

J
Jaya Kumar 已提交
108 109
	/* protect against the workqueue changing the page list */
	mutex_lock(&fbdefio->lock);
110

H
Heiko Stübner 已提交
111 112 113 114
	/* first write in this cycle, notify the driver */
	if (fbdefio->first_io && list_empty(&fbdefio->pagelist))
		fbdefio->first_io(info);

115 116 117 118 119 120 121 122 123 124
	/*
	 * We want the page to remain locked from ->page_mkwrite until
	 * the PTE is marked dirty to avoid page_mkclean() being called
	 * before the PTE is updated, which would leave the page ignored
	 * by defio.
	 * Do this by locking the page here and informing the caller
	 * about it with VM_FAULT_LOCKED.
	 */
	lock_page(page);

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	/* we loop through the pagelist before adding in order
	to keep the pagelist sorted */
	list_for_each_entry(cur, &fbdefio->pagelist, lru) {
		/* this check is to catch the case where a new
		process could start writing to the same page
		through a new pte. this new access can cause the
		mkwrite even when the original ps's pte is marked
		writable */
		if (unlikely(cur == page))
			goto page_already_added;
		else if (cur->index > page->index)
			break;
	}

	list_add_tail(&page->lru, &cur->lru);

page_already_added:
J
Jaya Kumar 已提交
142 143 144 145
	mutex_unlock(&fbdefio->lock);

	/* come back after delay to process the deferred IO */
	schedule_delayed_work(&info->deferred_work, fbdefio->delay);
146
	return VM_FAULT_LOCKED;
J
Jaya Kumar 已提交
147 148
}

149
static const struct vm_operations_struct fb_deferred_io_vm_ops = {
N
Nick Piggin 已提交
150
	.fault		= fb_deferred_io_fault,
J
Jaya Kumar 已提交
151 152 153
	.page_mkwrite	= fb_deferred_io_mkwrite,
};

154 155 156 157 158 159 160 161 162 163 164
static int fb_deferred_io_set_page_dirty(struct page *page)
{
	if (!PageDirty(page))
		SetPageDirty(page);
	return 0;
}

static const struct address_space_operations fb_deferred_io_aops = {
	.set_page_dirty = fb_deferred_io_set_page_dirty,
};

165
int fb_deferred_io_mmap(struct fb_info *info, struct vm_area_struct *vma)
J
Jaya Kumar 已提交
166 167
{
	vma->vm_ops = &fb_deferred_io_vm_ops;
168
	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
169 170
	if (!(info->flags & FBINFO_VIRTFB))
		vma->vm_flags |= VM_IO;
J
Jaya Kumar 已提交
171 172 173 174 175 176 177 178 179
	vma->vm_private_data = info;
	return 0;
}

/* workqueue callback */
static void fb_deferred_io_work(struct work_struct *work)
{
	struct fb_info *info = container_of(work, struct fb_info,
						deferred_work.work);
180 181
	struct list_head *node, *next;
	struct page *cur;
J
Jaya Kumar 已提交
182 183 184 185
	struct fb_deferred_io *fbdefio = info->fbdefio;

	/* here we mkclean the pages, then do all deferred IO */
	mutex_lock(&fbdefio->lock);
186 187 188 189
	list_for_each_entry(cur, &fbdefio->pagelist, lru) {
		lock_page(cur);
		page_mkclean(cur);
		unlock_page(cur);
J
Jaya Kumar 已提交
190 191 192 193 194
	}

	/* driver's callback with pagelist */
	fbdefio->deferred_io(info, &fbdefio->pagelist);

195 196
	/* clear the list */
	list_for_each_safe(node, next, &fbdefio->pagelist) {
J
Jaya Kumar 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
		list_del(node);
	}
	mutex_unlock(&fbdefio->lock);
}

void fb_deferred_io_init(struct fb_info *info)
{
	struct fb_deferred_io *fbdefio = info->fbdefio;

	BUG_ON(!fbdefio);
	mutex_init(&fbdefio->lock);
	INIT_DELAYED_WORK(&info->deferred_work, fb_deferred_io_work);
	INIT_LIST_HEAD(&fbdefio->pagelist);
	if (fbdefio->delay == 0) /* set a default of 1 s */
		fbdefio->delay = HZ;
}
EXPORT_SYMBOL_GPL(fb_deferred_io_init);

215 216 217 218 219 220 221 222
void fb_deferred_io_open(struct fb_info *info,
			 struct inode *inode,
			 struct file *file)
{
	file->f_mapping->a_ops = &fb_deferred_io_aops;
}
EXPORT_SYMBOL_GPL(fb_deferred_io_open);

J
Jaya Kumar 已提交
223 224 225
void fb_deferred_io_cleanup(struct fb_info *info)
{
	struct fb_deferred_io *fbdefio = info->fbdefio;
226 227
	struct page *page;
	int i;
J
Jaya Kumar 已提交
228 229

	BUG_ON(!fbdefio);
230
	cancel_delayed_work_sync(&info->deferred_work);
231 232 233 234 235 236 237

	/* clear out the mapping that we setup */
	for (i = 0 ; i < info->fix.smem_len; i += PAGE_SIZE) {
		page = fb_deferred_io_page(info, i);
		page->mapping = NULL;
	}

M
Magnus Damm 已提交
238
	mutex_destroy(&fbdefio->lock);
J
Jaya Kumar 已提交
239 240
}
EXPORT_SYMBOL_GPL(fb_deferred_io_cleanup);