i915_mm.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright © 2014 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 <linux/mm.h>
  25. #include <linux/io-mapping.h>
  26. #include "i915_drv.h"
  27. #include "i915_mm.h"
  28. struct remap_pfn {
  29. struct mm_struct *mm;
  30. unsigned long pfn;
  31. pgprot_t prot;
  32. struct sgt_iter sgt;
  33. resource_size_t iobase;
  34. };
  35. #define use_dma(io) ((io) != -1)
  36. static inline unsigned long sgt_pfn(const struct remap_pfn *r)
  37. {
  38. if (use_dma(r->iobase))
  39. return (r->sgt.dma + r->sgt.curr + r->iobase) >> PAGE_SHIFT;
  40. else
  41. return r->sgt.pfn + (r->sgt.curr >> PAGE_SHIFT);
  42. }
  43. static int remap_sg(pte_t *pte, unsigned long addr, void *data)
  44. {
  45. struct remap_pfn *r = data;
  46. if (GEM_WARN_ON(!r->sgt.sgp))
  47. return -EINVAL;
  48. /* Special PTE are not associated with any struct page */
  49. set_pte_at(r->mm, addr, pte,
  50. pte_mkspecial(pfn_pte(sgt_pfn(r), r->prot)));
  51. r->pfn++; /* track insertions in case we need to unwind later */
  52. r->sgt.curr += PAGE_SIZE;
  53. if (r->sgt.curr >= r->sgt.max)
  54. r->sgt = __sgt_iter(__sg_next(r->sgt.sgp), use_dma(r->iobase));
  55. return 0;
  56. }
  57. #define EXPECTED_FLAGS (VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP)
  58. #if IS_ENABLED(CONFIG_X86)
  59. static int remap_pfn(pte_t *pte, unsigned long addr, void *data)
  60. {
  61. struct remap_pfn *r = data;
  62. /* Special PTE are not associated with any struct page */
  63. set_pte_at(r->mm, addr, pte, pte_mkspecial(pfn_pte(r->pfn, r->prot)));
  64. r->pfn++;
  65. return 0;
  66. }
  67. /**
  68. * remap_io_mapping - remap an IO mapping to userspace
  69. * @vma: user vma to map to
  70. * @addr: target user address to start at
  71. * @pfn: physical address of kernel memory
  72. * @size: size of map area
  73. * @iomap: the source io_mapping
  74. *
  75. * Note: this is only safe if the mm semaphore is held when called.
  76. */
  77. int remap_io_mapping(struct vm_area_struct *vma,
  78. unsigned long addr, unsigned long pfn, unsigned long size,
  79. struct io_mapping *iomap)
  80. {
  81. struct remap_pfn r;
  82. int err;
  83. GEM_BUG_ON((vma->vm_flags & EXPECTED_FLAGS) != EXPECTED_FLAGS);
  84. /* We rely on prevalidation of the io-mapping to skip track_pfn(). */
  85. r.mm = vma->vm_mm;
  86. r.pfn = pfn;
  87. r.prot = __pgprot((pgprot_val(iomap->prot) & _PAGE_CACHE_MASK) |
  88. (pgprot_val(vma->vm_page_prot) & ~_PAGE_CACHE_MASK));
  89. err = apply_to_page_range(r.mm, addr, size, remap_pfn, &r);
  90. if (unlikely(err)) {
  91. zap_vma_ptes(vma, addr, (r.pfn - pfn) << PAGE_SHIFT);
  92. return err;
  93. }
  94. return 0;
  95. }
  96. #endif
  97. /**
  98. * remap_io_sg - remap an IO mapping to userspace
  99. * @vma: user vma to map to
  100. * @addr: target user address to start at
  101. * @size: size of map area
  102. * @sgl: Start sg entry
  103. * @iobase: Use stored dma address offset by this address or pfn if -1
  104. *
  105. * Note: this is only safe if the mm semaphore is held when called.
  106. */
  107. int remap_io_sg(struct vm_area_struct *vma,
  108. unsigned long addr, unsigned long size,
  109. struct scatterlist *sgl, resource_size_t iobase)
  110. {
  111. struct remap_pfn r = {
  112. .mm = vma->vm_mm,
  113. .prot = vma->vm_page_prot,
  114. .sgt = __sgt_iter(sgl, use_dma(iobase)),
  115. .iobase = iobase,
  116. };
  117. int err;
  118. /* We rely on prevalidation of the io-mapping to skip track_pfn(). */
  119. GEM_BUG_ON((vma->vm_flags & EXPECTED_FLAGS) != EXPECTED_FLAGS);
  120. if (!use_dma(iobase))
  121. flush_cache_range(vma, addr, size);
  122. err = apply_to_page_range(r.mm, addr, size, remap_sg, &r);
  123. if (unlikely(err)) {
  124. zap_vma_ptes(vma, addr, r.pfn << PAGE_SHIFT);
  125. return err;
  126. }
  127. return 0;
  128. }