fb_defio.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 struct page* fb_deferred_io_nopage(struct vm_area_struct *vma,
  26. unsigned long vaddr, int *type)
  27. {
  28. unsigned long offset;
  29. struct page *page;
  30. struct fb_info *info = vma->vm_private_data;
  31. /* info->screen_base is in System RAM */
  32. void *screen_base = (void __force *) info->screen_base;
  33. offset = (vaddr - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
  34. if (offset >= info->fix.smem_len)
  35. return NOPAGE_SIGBUS;
  36. page = vmalloc_to_page(screen_base + offset);
  37. if (!page)
  38. return NOPAGE_OOM;
  39. get_page(page);
  40. if (type)
  41. *type = VM_FAULT_MINOR;
  42. return page;
  43. }
  44. int fb_deferred_io_fsync(struct file *file, struct dentry *dentry, int datasync)
  45. {
  46. struct fb_info *info = file->private_data;
  47. /* Kill off the delayed work */
  48. cancel_rearming_delayed_work(&info->deferred_work);
  49. /* Run it immediately */
  50. return schedule_delayed_work(&info->deferred_work, 0);
  51. }
  52. EXPORT_SYMBOL_GPL(fb_deferred_io_fsync);
  53. /* vm_ops->page_mkwrite handler */
  54. static int fb_deferred_io_mkwrite(struct vm_area_struct *vma,
  55. struct page *page)
  56. {
  57. struct fb_info *info = vma->vm_private_data;
  58. struct fb_deferred_io *fbdefio = info->fbdefio;
  59. /* this is a callback we get when userspace first tries to
  60. write to the page. we schedule a workqueue. that workqueue
  61. will eventually mkclean the touched pages and execute the
  62. deferred framebuffer IO. then if userspace touches a page
  63. again, we repeat the same scheme */
  64. /* protect against the workqueue changing the page list */
  65. mutex_lock(&fbdefio->lock);
  66. list_add(&page->lru, &fbdefio->pagelist);
  67. mutex_unlock(&fbdefio->lock);
  68. /* come back after delay to process the deferred IO */
  69. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  70. return 0;
  71. }
  72. static struct vm_operations_struct fb_deferred_io_vm_ops = {
  73. .nopage = fb_deferred_io_nopage,
  74. .page_mkwrite = fb_deferred_io_mkwrite,
  75. };
  76. static int fb_deferred_io_mmap(struct fb_info *info, struct vm_area_struct *vma)
  77. {
  78. vma->vm_ops = &fb_deferred_io_vm_ops;
  79. vma->vm_flags |= ( VM_IO | VM_RESERVED | VM_DONTEXPAND );
  80. vma->vm_private_data = info;
  81. return 0;
  82. }
  83. /* workqueue callback */
  84. static void fb_deferred_io_work(struct work_struct *work)
  85. {
  86. struct fb_info *info = container_of(work, struct fb_info,
  87. deferred_work.work);
  88. struct list_head *node, *next;
  89. struct page *cur;
  90. struct fb_deferred_io *fbdefio = info->fbdefio;
  91. /* here we mkclean the pages, then do all deferred IO */
  92. mutex_lock(&fbdefio->lock);
  93. list_for_each_entry(cur, &fbdefio->pagelist, lru) {
  94. lock_page(cur);
  95. page_mkclean(cur);
  96. unlock_page(cur);
  97. }
  98. /* driver's callback with pagelist */
  99. fbdefio->deferred_io(info, &fbdefio->pagelist);
  100. /* clear the list */
  101. list_for_each_safe(node, next, &fbdefio->pagelist) {
  102. list_del(node);
  103. }
  104. mutex_unlock(&fbdefio->lock);
  105. }
  106. void fb_deferred_io_init(struct fb_info *info)
  107. {
  108. struct fb_deferred_io *fbdefio = info->fbdefio;
  109. BUG_ON(!fbdefio);
  110. mutex_init(&fbdefio->lock);
  111. info->fbops->fb_mmap = fb_deferred_io_mmap;
  112. INIT_DELAYED_WORK(&info->deferred_work, fb_deferred_io_work);
  113. INIT_LIST_HEAD(&fbdefio->pagelist);
  114. if (fbdefio->delay == 0) /* set a default of 1 s */
  115. fbdefio->delay = HZ;
  116. }
  117. EXPORT_SYMBOL_GPL(fb_deferred_io_init);
  118. void fb_deferred_io_cleanup(struct fb_info *info)
  119. {
  120. struct fb_deferred_io *fbdefio = info->fbdefio;
  121. BUG_ON(!fbdefio);
  122. cancel_delayed_work(&info->deferred_work);
  123. flush_scheduled_work();
  124. }
  125. EXPORT_SYMBOL_GPL(fb_deferred_io_cleanup);
  126. MODULE_LICENSE("GPL");