fb_defio.c 3.6 KB

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