i915_gem_internal.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright © 2014-2016 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include <drm/drmP.h>
  25. #include <drm/i915_drm.h>
  26. #include "i915_drv.h"
  27. #define QUIET (__GFP_NORETRY | __GFP_NOWARN)
  28. /* convert swiotlb segment size into sensible units (pages)! */
  29. #define IO_TLB_SEGPAGES (IO_TLB_SEGSIZE << IO_TLB_SHIFT >> PAGE_SHIFT)
  30. static void internal_free_pages(struct sg_table *st)
  31. {
  32. struct scatterlist *sg;
  33. for (sg = st->sgl; sg; sg = __sg_next(sg))
  34. __free_pages(sg_page(sg), get_order(sg->length));
  35. sg_free_table(st);
  36. kfree(st);
  37. }
  38. static struct sg_table *
  39. i915_gem_object_get_pages_internal(struct drm_i915_gem_object *obj)
  40. {
  41. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  42. unsigned int npages = obj->base.size / PAGE_SIZE;
  43. struct sg_table *st;
  44. struct scatterlist *sg;
  45. int max_order;
  46. gfp_t gfp;
  47. st = kmalloc(sizeof(*st), GFP_KERNEL);
  48. if (!st)
  49. return ERR_PTR(-ENOMEM);
  50. if (sg_alloc_table(st, npages, GFP_KERNEL)) {
  51. kfree(st);
  52. return ERR_PTR(-ENOMEM);
  53. }
  54. sg = st->sgl;
  55. st->nents = 0;
  56. max_order = MAX_ORDER;
  57. #ifdef CONFIG_SWIOTLB
  58. if (swiotlb_nr_tbl()) /* minimum max swiotlb size is IO_TLB_SEGSIZE */
  59. max_order = min(max_order, ilog2(IO_TLB_SEGPAGES));
  60. #endif
  61. gfp = GFP_KERNEL | __GFP_HIGHMEM | __GFP_RECLAIMABLE;
  62. if (IS_I965GM(i915) || IS_I965G(i915)) {
  63. /* 965gm cannot relocate objects above 4GiB. */
  64. gfp &= ~__GFP_HIGHMEM;
  65. gfp |= __GFP_DMA32;
  66. }
  67. do {
  68. int order = min(fls(npages) - 1, max_order);
  69. struct page *page;
  70. do {
  71. page = alloc_pages(gfp | (order ? QUIET : 0), order);
  72. if (page)
  73. break;
  74. if (!order--)
  75. goto err;
  76. /* Limit subsequent allocations as well */
  77. max_order = order;
  78. } while (1);
  79. sg_set_page(sg, page, PAGE_SIZE << order, 0);
  80. st->nents++;
  81. npages -= 1 << order;
  82. if (!npages) {
  83. sg_mark_end(sg);
  84. break;
  85. }
  86. sg = __sg_next(sg);
  87. } while (1);
  88. if (i915_gem_gtt_prepare_pages(obj, st))
  89. goto err;
  90. /* Mark the pages as dontneed whilst they are still pinned. As soon
  91. * as they are unpinned they are allowed to be reaped by the shrinker,
  92. * and the caller is expected to repopulate - the contents of this
  93. * object are only valid whilst active and pinned.
  94. */
  95. obj->mm.madv = I915_MADV_DONTNEED;
  96. return st;
  97. err:
  98. sg_mark_end(sg);
  99. internal_free_pages(st);
  100. return ERR_PTR(-ENOMEM);
  101. }
  102. static void i915_gem_object_put_pages_internal(struct drm_i915_gem_object *obj,
  103. struct sg_table *pages)
  104. {
  105. i915_gem_gtt_finish_pages(obj, pages);
  106. internal_free_pages(pages);
  107. obj->mm.dirty = false;
  108. obj->mm.madv = I915_MADV_WILLNEED;
  109. }
  110. static const struct drm_i915_gem_object_ops i915_gem_object_internal_ops = {
  111. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
  112. I915_GEM_OBJECT_IS_SHRINKABLE,
  113. .get_pages = i915_gem_object_get_pages_internal,
  114. .put_pages = i915_gem_object_put_pages_internal,
  115. };
  116. /**
  117. * Creates a new object that wraps some internal memory for private use.
  118. * This object is not backed by swappable storage, and as such its contents
  119. * are volatile and only valid whilst pinned. If the object is reaped by the
  120. * shrinker, its pages and data will be discarded. Equally, it is not a full
  121. * GEM object and so not valid for access from userspace. This makes it useful
  122. * for hardware interfaces like ringbuffers (which are pinned from the time
  123. * the request is written to the time the hardware stops accessing it), but
  124. * not for contexts (which need to be preserved when not active for later
  125. * reuse). Note that it is not cleared upon allocation.
  126. */
  127. struct drm_i915_gem_object *
  128. i915_gem_object_create_internal(struct drm_i915_private *i915,
  129. phys_addr_t size)
  130. {
  131. struct drm_i915_gem_object *obj;
  132. GEM_BUG_ON(!size);
  133. GEM_BUG_ON(!IS_ALIGNED(size, PAGE_SIZE));
  134. if (overflows_type(size, obj->base.size))
  135. return ERR_PTR(-E2BIG);
  136. obj = i915_gem_object_alloc(i915);
  137. if (!obj)
  138. return ERR_PTR(-ENOMEM);
  139. drm_gem_private_object_init(&i915->drm, &obj->base, size);
  140. i915_gem_object_init(obj, &i915_gem_object_internal_ops);
  141. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  142. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  143. obj->cache_level = HAS_LLC(i915) ? I915_CACHE_LLC : I915_CACHE_NONE;
  144. return obj;
  145. }