fb_defio.c 5.3 KB

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