1 /*
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/vmalloc.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/fb.h>
20 #include <linux/list.h>
21 
22 /* to support deferred IO */
23 #include <linux/rmap.h>
24 #include <linux/pagemap.h>
25 
26 static struct page *fb_deferred_io_page(struct fb_info *info, unsigned long offs)
27 {
28 	void *screen_base = (void __force *) info->screen_base;
29 	struct page *page;
30 
31 	if (is_vmalloc_addr(screen_base + offs))
32 		page = vmalloc_to_page(screen_base + offs);
33 	else
34 		page = pfn_to_page((info->fix.smem_start + offs) >> PAGE_SHIFT);
35 
36 	return page;
37 }
38 
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)
41 {
42 	unsigned long offset;
43 	struct page *page;
44 	struct fb_info *info = vmf->vma->vm_private_data;
45 
46 	offset = vmf->pgoff << PAGE_SHIFT;
47 	if (offset >= info->fix.smem_len)
48 		return VM_FAULT_SIGBUS;
49 
50 	page = fb_deferred_io_page(info, offset);
51 	if (!page)
52 		return VM_FAULT_SIGBUS;
53 
54 	get_page(page);
55 
56 	if (vmf->vma->vm_file)
57 		page->mapping = vmf->vma->vm_file->f_mapping;
58 	else
59 		printk(KERN_ERR "no mapping available\n");
60 
61 	BUG_ON(!page->mapping);
62 	INIT_LIST_HEAD(&page->lru);
63 	page->index = vmf->pgoff;
64 
65 	vmf->page = page;
66 	return 0;
67 }
68 
69 int fb_deferred_io_fsync(struct file *file, loff_t start, loff_t end, int datasync)
70 {
71 	struct fb_info *info = file->private_data;
72 	struct inode *inode = file_inode(file);
73 	int err = file_write_and_wait_range(file, start, end);
74 	if (err)
75 		return err;
76 
77 	/* Skip if deferred io is compiled-in but disabled on this fbdev */
78 	if (!info->fbdefio)
79 		return 0;
80 
81 	inode_lock(inode);
82 	/* Kill off the delayed work */
83 	cancel_delayed_work_sync(&info->deferred_work);
84 
85 	/* Run it immediately */
86 	schedule_delayed_work(&info->deferred_work, 0);
87 	inode_unlock(inode);
88 
89 	return 0;
90 }
91 EXPORT_SYMBOL_GPL(fb_deferred_io_fsync);
92 
93 /* vm_ops->page_mkwrite handler */
94 static vm_fault_t fb_deferred_io_mkwrite(struct vm_fault *vmf)
95 {
96 	struct page *page = vmf->page;
97 	struct fb_info *info = vmf->vma->vm_private_data;
98 	struct fb_deferred_io *fbdefio = info->fbdefio;
99 	struct list_head *pos = &fbdefio->pagelist;
100 
101 	/* this is a callback we get when userspace first tries to
102 	write to the page. we schedule a workqueue. that workqueue
103 	will eventually mkclean the touched pages and execute the
104 	deferred framebuffer IO. then if userspace touches a page
105 	again, we repeat the same scheme */
106 
107 	file_update_time(vmf->vma->vm_file);
108 
109 	/* protect against the workqueue changing the page list */
110 	mutex_lock(&fbdefio->lock);
111 
112 	/* first write in this cycle, notify the driver */
113 	if (fbdefio->first_io && list_empty(&fbdefio->pagelist))
114 		fbdefio->first_io(info);
115 
116 	/*
117 	 * We want the page to remain locked from ->page_mkwrite until
118 	 * the PTE is marked dirty to avoid page_mkclean() being called
119 	 * before the PTE is updated, which would leave the page ignored
120 	 * by defio.
121 	 * Do this by locking the page here and informing the caller
122 	 * about it with VM_FAULT_LOCKED.
123 	 */
124 	lock_page(page);
125 
126 	/*
127 	 * This check is to catch the case where a new process could start
128 	 * writing to the same page through a new PTE. This new access
129 	 * can cause a call to .page_mkwrite even if the original process'
130 	 * PTE is marked writable.
131 	 *
132 	 * TODO: The lru field is owned by the page cache; hence the name.
133 	 *       We dequeue in fb_deferred_io_work() after flushing the
134 	 *       page's content into video memory. Instead of lru, fbdefio
135 	 *       should have it's own field.
136 	 */
137 	if (!list_empty(&page->lru))
138 		goto page_already_added;
139 
140 	if (unlikely(fbdefio->sort_pagelist)) {
141 		/*
142 		 * We loop through the pagelist before adding in order to
143 		 * keep the pagelist sorted. This has significant overhead
144 		 * of O(n^2) with n being the number of written pages. If
145 		 * possible, drivers should try to work with unsorted page
146 		 * lists instead.
147 		 */
148 		struct page *cur;
149 
150 		list_for_each_entry(cur, &fbdefio->pagelist, lru) {
151 			if (cur->index > page->index)
152 				break;
153 		}
154 		pos = &cur->lru;
155 	}
156 
157 	list_add_tail(&page->lru, pos);
158 
159 page_already_added:
160 	mutex_unlock(&fbdefio->lock);
161 
162 	/* come back after delay to process the deferred IO */
163 	schedule_delayed_work(&info->deferred_work, fbdefio->delay);
164 	return VM_FAULT_LOCKED;
165 }
166 
167 static const struct vm_operations_struct fb_deferred_io_vm_ops = {
168 	.fault		= fb_deferred_io_fault,
169 	.page_mkwrite	= fb_deferred_io_mkwrite,
170 };
171 
172 static const struct address_space_operations fb_deferred_io_aops = {
173 	.dirty_folio	= noop_dirty_folio,
174 };
175 
176 int fb_deferred_io_mmap(struct fb_info *info, struct vm_area_struct *vma)
177 {
178 	vma->vm_ops = &fb_deferred_io_vm_ops;
179 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
180 	if (!(info->flags & FBINFO_VIRTFB))
181 		vma->vm_flags |= VM_IO;
182 	vma->vm_private_data = info;
183 	return 0;
184 }
185 
186 /* workqueue callback */
187 static void fb_deferred_io_work(struct work_struct *work)
188 {
189 	struct fb_info *info = container_of(work, struct fb_info,
190 						deferred_work.work);
191 	struct list_head *node, *next;
192 	struct page *cur;
193 	struct fb_deferred_io *fbdefio = info->fbdefio;
194 
195 	/* here we mkclean the pages, then do all deferred IO */
196 	mutex_lock(&fbdefio->lock);
197 	list_for_each_entry(cur, &fbdefio->pagelist, lru) {
198 		lock_page(cur);
199 		page_mkclean(cur);
200 		unlock_page(cur);
201 	}
202 
203 	/* driver's callback with pagelist */
204 	fbdefio->deferred_io(info, &fbdefio->pagelist);
205 
206 	/* clear the list */
207 	list_for_each_safe(node, next, &fbdefio->pagelist) {
208 		list_del_init(node);
209 	}
210 	mutex_unlock(&fbdefio->lock);
211 }
212 
213 void fb_deferred_io_init(struct fb_info *info)
214 {
215 	struct fb_deferred_io *fbdefio = info->fbdefio;
216 
217 	BUG_ON(!fbdefio);
218 	mutex_init(&fbdefio->lock);
219 	INIT_DELAYED_WORK(&info->deferred_work, fb_deferred_io_work);
220 	INIT_LIST_HEAD(&fbdefio->pagelist);
221 	if (fbdefio->delay == 0) /* set a default of 1 s */
222 		fbdefio->delay = HZ;
223 }
224 EXPORT_SYMBOL_GPL(fb_deferred_io_init);
225 
226 void fb_deferred_io_open(struct fb_info *info,
227 			 struct inode *inode,
228 			 struct file *file)
229 {
230 	file->f_mapping->a_ops = &fb_deferred_io_aops;
231 }
232 EXPORT_SYMBOL_GPL(fb_deferred_io_open);
233 
234 void fb_deferred_io_cleanup(struct fb_info *info)
235 {
236 	struct fb_deferred_io *fbdefio = info->fbdefio;
237 	struct page *page;
238 	int i;
239 
240 	BUG_ON(!fbdefio);
241 	cancel_delayed_work_sync(&info->deferred_work);
242 
243 	/* clear out the mapping that we setup */
244 	for (i = 0 ; i < info->fix.smem_len; i += PAGE_SIZE) {
245 		page = fb_deferred_io_page(info, i);
246 		page->mapping = NULL;
247 	}
248 
249 	mutex_destroy(&fbdefio->lock);
250 }
251 EXPORT_SYMBOL_GPL(fb_deferred_io_cleanup);
252