i915_mm.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <asm/pgtable.h>
  27. #include "i915_drv.h"
  28. struct remap_pfn {
  29. struct mm_struct *mm;
  30. unsigned long pfn;
  31. pgprot_t prot;
  32. };
  33. static int remap_pfn(pte_t *pte, pgtable_t token,
  34. unsigned long addr, void *data)
  35. {
  36. struct remap_pfn *r = data;
  37. /* Special PTE are not associated with any struct page */
  38. set_pte_at(r->mm, addr, pte, pte_mkspecial(pfn_pte(r->pfn, r->prot)));
  39. r->pfn++;
  40. return 0;
  41. }
  42. /**
  43. * remap_io_mapping - remap an IO mapping to userspace
  44. * @vma: user vma to map to
  45. * @addr: target user address to start at
  46. * @pfn: physical address of kernel memory
  47. * @size: size of map area
  48. * @iomap: the source io_mapping
  49. *
  50. * Note: this is only safe if the mm semaphore is held when called.
  51. */
  52. int remap_io_mapping(struct vm_area_struct *vma,
  53. unsigned long addr, unsigned long pfn, unsigned long size,
  54. struct io_mapping *iomap)
  55. {
  56. struct remap_pfn r;
  57. int err;
  58. GEM_BUG_ON((vma->vm_flags &
  59. (VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP)) !=
  60. (VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP));
  61. /* We rely on prevalidation of the io-mapping to skip track_pfn(). */
  62. r.mm = vma->vm_mm;
  63. r.pfn = pfn;
  64. r.prot = __pgprot((pgprot_val(iomap->prot) & _PAGE_CACHE_MASK) |
  65. (pgprot_val(vma->vm_page_prot) & ~_PAGE_CACHE_MASK));
  66. err = apply_to_page_range(r.mm, addr, size, remap_pfn, &r);
  67. if (unlikely(err)) {
  68. zap_vma_ptes(vma, addr, (r.pfn - pfn) << PAGE_SHIFT);
  69. return err;
  70. }
  71. return 0;
  72. }