fb_defio.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/string.h>
  14. #include <linux/mm.h>
  15. #include <linux/slab.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. /* to support deferred IO */
  22. #include <linux/rmap.h>
  23. #include <linux/pagemap.h>
  24. struct page *fb_deferred_io_page(struct fb_info *info, unsigned long offs)
  25. {
  26. void *screen_base = (void __force *) info->screen_base;
  27. struct page *page;
  28. if (is_vmalloc_addr(screen_base + offs))
  29. page = vmalloc_to_page(screen_base + offs);
  30. else
  31. page = pfn_to_page((info->fix.smem_start + offs) >> PAGE_SHIFT);
  32. return page;
  33. }
  34. /* this is to find and return the vmalloc-ed fb pages */
  35. static int fb_deferred_io_fault(struct vm_area_struct *vma,
  36. struct vm_fault *vmf)
  37. {
  38. unsigned long offset;
  39. struct page *page;
  40. struct fb_info *info = vma->vm_private_data;
  41. offset = vmf->pgoff << PAGE_SHIFT;
  42. if (offset >= info->fix.smem_len)
  43. return VM_FAULT_SIGBUS;
  44. page = fb_deferred_io_page(info, offset);
  45. if (!page)
  46. return VM_FAULT_SIGBUS;
  47. get_page(page);
  48. if (vma->vm_file)
  49. page->mapping = vma->vm_file->f_mapping;
  50. else
  51. printk(KERN_ERR "no mapping available\n");
  52. BUG_ON(!page->mapping);
  53. page->index = vmf->pgoff;
  54. vmf->page = page;
  55. return 0;
  56. }
  57. int fb_deferred_io_fsync(struct file *file, struct dentry *dentry, int datasync)
  58. {
  59. struct fb_info *info = file->private_data;
  60. /* Skip if deferred io is complied-in but disabled on this fbdev */
  61. if (!info->fbdefio)
  62. return 0;
  63. /* Kill off the delayed work */
  64. cancel_rearming_delayed_work(&info->deferred_work);
  65. /* Run it immediately */
  66. return schedule_delayed_work(&info->deferred_work, 0);
  67. }
  68. EXPORT_SYMBOL_GPL(fb_deferred_io_fsync);
  69. /* vm_ops->page_mkwrite handler */
  70. static int fb_deferred_io_mkwrite(struct vm_area_struct *vma,
  71. struct vm_fault *vmf)
  72. {
  73. struct page *page = vmf->page;
  74. struct fb_info *info = vma->vm_private_data;
  75. struct fb_deferred_io *fbdefio = info->fbdefio;
  76. struct page *cur;
  77. /* this is a callback we get when userspace first tries to
  78. write to the page. we schedule a workqueue. that workqueue
  79. will eventually mkclean the touched pages and execute the
  80. deferred framebuffer IO. then if userspace touches a page
  81. again, we repeat the same scheme */
  82. /* protect against the workqueue changing the page list */
  83. mutex_lock(&fbdefio->lock);
  84. /* we loop through the pagelist before adding in order
  85. to keep the pagelist sorted */
  86. list_for_each_entry(cur, &fbdefio->pagelist, lru) {
  87. /* this check is to catch the case where a new
  88. process could start writing to the same page
  89. through a new pte. this new access can cause the
  90. mkwrite even when the original ps's pte is marked
  91. writable */
  92. if (unlikely(cur == page))
  93. goto page_already_added;
  94. else if (cur->index > page->index)
  95. break;
  96. }
  97. list_add_tail(&page->lru, &cur->lru);
  98. page_already_added:
  99. mutex_unlock(&fbdefio->lock);
  100. /* come back after delay to process the deferred IO */
  101. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  102. return 0;
  103. }
  104. static struct vm_operations_struct fb_deferred_io_vm_ops = {
  105. .fault = fb_deferred_io_fault,
  106. .page_mkwrite = fb_deferred_io_mkwrite,
  107. };
  108. static int fb_deferred_io_set_page_dirty(struct page *page)
  109. {
  110. if (!PageDirty(page))
  111. SetPageDirty(page);
  112. return 0;
  113. }
  114. static const struct address_space_operations fb_deferred_io_aops = {
  115. .set_page_dirty = fb_deferred_io_set_page_dirty,
  116. };
  117. static int fb_deferred_io_mmap(struct fb_info *info, struct vm_area_struct *vma)
  118. {
  119. vma->vm_ops = &fb_deferred_io_vm_ops;
  120. vma->vm_flags |= ( VM_IO | VM_RESERVED | VM_DONTEXPAND );
  121. vma->vm_private_data = info;
  122. return 0;
  123. }
  124. /* workqueue callback */
  125. static void fb_deferred_io_work(struct work_struct *work)
  126. {
  127. struct fb_info *info = container_of(work, struct fb_info,
  128. deferred_work.work);
  129. struct list_head *node, *next;
  130. struct page *cur;
  131. struct fb_deferred_io *fbdefio = info->fbdefio;
  132. /* here we mkclean the pages, then do all deferred IO */
  133. mutex_lock(&fbdefio->lock);
  134. list_for_each_entry(cur, &fbdefio->pagelist, lru) {
  135. lock_page(cur);
  136. page_mkclean(cur);
  137. unlock_page(cur);
  138. }
  139. /* driver's callback with pagelist */
  140. fbdefio->deferred_io(info, &fbdefio->pagelist);
  141. /* clear the list */
  142. list_for_each_safe(node, next, &fbdefio->pagelist) {
  143. list_del(node);
  144. }
  145. mutex_unlock(&fbdefio->lock);
  146. }
  147. void fb_deferred_io_init(struct fb_info *info)
  148. {
  149. struct fb_deferred_io *fbdefio = info->fbdefio;
  150. BUG_ON(!fbdefio);
  151. mutex_init(&fbdefio->lock);
  152. info->fbops->fb_mmap = fb_deferred_io_mmap;
  153. INIT_DELAYED_WORK(&info->deferred_work, fb_deferred_io_work);
  154. INIT_LIST_HEAD(&fbdefio->pagelist);
  155. if (fbdefio->delay == 0) /* set a default of 1 s */
  156. fbdefio->delay = HZ;
  157. }
  158. EXPORT_SYMBOL_GPL(fb_deferred_io_init);
  159. void fb_deferred_io_open(struct fb_info *info,
  160. struct inode *inode,
  161. struct file *file)
  162. {
  163. file->f_mapping->a_ops = &fb_deferred_io_aops;
  164. }
  165. EXPORT_SYMBOL_GPL(fb_deferred_io_open);
  166. void fb_deferred_io_cleanup(struct fb_info *info)
  167. {
  168. struct fb_deferred_io *fbdefio = info->fbdefio;
  169. struct page *page;
  170. int i;
  171. BUG_ON(!fbdefio);
  172. cancel_delayed_work(&info->deferred_work);
  173. flush_scheduled_work();
  174. /* clear out the mapping that we setup */
  175. for (i = 0 ; i < info->fix.smem_len; i += PAGE_SIZE) {
  176. page = fb_deferred_io_page(info, i);
  177. page->mapping = NULL;
  178. }
  179. info->fbops->fb_mmap = NULL;
  180. mutex_destroy(&fbdefio->lock);
  181. }
  182. EXPORT_SYMBOL_GPL(fb_deferred_io_cleanup);
  183. MODULE_LICENSE("GPL");