omap_gem_helpers.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * drivers/gpu/drm/omapdrm/omap_gem_helpers.c
  3. *
  4. * Copyright (C) 2011 Texas Instruments
  5. * Author: Rob Clark <rob.clark@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /* temporary copy of drm_gem_{get,put}_pages() until the
  20. * "drm/gem: add functions to get/put pages" patch is merged..
  21. */
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/shmem_fs.h>
  25. #include <drm/drmP.h>
  26. /**
  27. * drm_gem_get_pages - helper to allocate backing pages for a GEM object
  28. * @obj: obj in question
  29. * @gfpmask: gfp mask of requested pages
  30. */
  31. struct page **_drm_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask)
  32. {
  33. struct inode *inode;
  34. struct address_space *mapping;
  35. struct page *p, **pages;
  36. int i, npages;
  37. /* This is the shared memory object that backs the GEM resource */
  38. inode = file_inode(obj->filp);
  39. mapping = inode->i_mapping;
  40. npages = obj->size >> PAGE_SHIFT;
  41. pages = drm_malloc_ab(npages, sizeof(struct page *));
  42. if (pages == NULL)
  43. return ERR_PTR(-ENOMEM);
  44. gfpmask |= mapping_gfp_mask(mapping);
  45. for (i = 0; i < npages; i++) {
  46. p = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
  47. if (IS_ERR(p))
  48. goto fail;
  49. pages[i] = p;
  50. /* There is a hypothetical issue w/ drivers that require
  51. * buffer memory in the low 4GB.. if the pages are un-
  52. * pinned, and swapped out, they can end up swapped back
  53. * in above 4GB. If pages are already in memory, then
  54. * shmem_read_mapping_page_gfp will ignore the gfpmask,
  55. * even if the already in-memory page disobeys the mask.
  56. *
  57. * It is only a theoretical issue today, because none of
  58. * the devices with this limitation can be populated with
  59. * enough memory to trigger the issue. But this BUG_ON()
  60. * is here as a reminder in case the problem with
  61. * shmem_read_mapping_page_gfp() isn't solved by the time
  62. * it does become a real issue.
  63. *
  64. * See this thread: http://lkml.org/lkml/2011/7/11/238
  65. */
  66. BUG_ON((gfpmask & __GFP_DMA32) &&
  67. (page_to_pfn(p) >= 0x00100000UL));
  68. }
  69. return pages;
  70. fail:
  71. while (i--)
  72. page_cache_release(pages[i]);
  73. drm_free_large(pages);
  74. return ERR_CAST(p);
  75. }
  76. /**
  77. * drm_gem_put_pages - helper to free backing pages for a GEM object
  78. * @obj: obj in question
  79. * @pages: pages to free
  80. */
  81. void _drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  82. bool dirty, bool accessed)
  83. {
  84. int i, npages;
  85. npages = obj->size >> PAGE_SHIFT;
  86. for (i = 0; i < npages; i++) {
  87. if (dirty)
  88. set_page_dirty(pages[i]);
  89. if (accessed)
  90. mark_page_accessed(pages[i]);
  91. /* Undo the reference we took when populating the table */
  92. page_cache_release(pages[i]);
  93. }
  94. drm_free_large(pages);
  95. }
  96. int
  97. _drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
  98. {
  99. struct drm_device *dev = obj->dev;
  100. struct drm_gem_mm *mm = dev->mm_private;
  101. return drm_vma_offset_add(&mm->vma_manager, &obj->vma_node,
  102. size / PAGE_SIZE);
  103. }