ion_system_mapper.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * drivers/gpu/ion/ion_system_mapper.c
  3. *
  4. * Copyright (C) 2011 Google, Inc.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/err.h>
  17. #include <linux/ion.h>
  18. #include <linux/memory.h>
  19. #include <linux/mm.h>
  20. #include <linux/slab.h>
  21. #include <linux/vmalloc.h>
  22. #include "ion_priv.h"
  23. /*
  24. * This mapper is valid for any heap that allocates memory that already has
  25. * a kernel mapping, this includes vmalloc'd memory, kmalloc'd memory,
  26. * pages obtained via io_remap, etc.
  27. */
  28. static void *ion_kernel_mapper_map(struct ion_mapper *mapper,
  29. struct ion_buffer *buffer,
  30. struct ion_mapping **mapping)
  31. {
  32. if (!((1 << buffer->heap->type) & mapper->heap_mask)) {
  33. pr_err("%s: attempting to map an unsupported heap\n", __func__);
  34. return ERR_PTR(-EINVAL);
  35. }
  36. /* XXX REVISIT ME!!! */
  37. *((unsigned long *)mapping) = (unsigned long)buffer->priv;
  38. return buffer->priv;
  39. }
  40. static void ion_kernel_mapper_unmap(struct ion_mapper *mapper,
  41. struct ion_buffer *buffer,
  42. struct ion_mapping *mapping)
  43. {
  44. if (!((1 << buffer->heap->type) & mapper->heap_mask))
  45. pr_err("%s: attempting to unmap an unsupported heap\n",
  46. __func__);
  47. }
  48. static void *ion_kernel_mapper_map_kernel(struct ion_mapper *mapper,
  49. struct ion_buffer *buffer,
  50. struct ion_mapping *mapping)
  51. {
  52. if (!((1 << buffer->heap->type) & mapper->heap_mask)) {
  53. pr_err("%s: attempting to unmap an unsupported heap\n",
  54. __func__);
  55. return ERR_PTR(-EINVAL);
  56. }
  57. return buffer->priv;
  58. }
  59. static int ion_kernel_mapper_map_user(struct ion_mapper *mapper,
  60. struct ion_buffer *buffer,
  61. struct vm_area_struct *vma,
  62. struct ion_mapping *mapping)
  63. {
  64. int ret;
  65. switch (buffer->heap->type) {
  66. case ION_HEAP_KMALLOC:
  67. {
  68. unsigned long pfn = __phys_to_pfn(virt_to_phys(buffer->priv));
  69. ret = remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
  70. vma->vm_end - vma->vm_start,
  71. vma->vm_page_prot);
  72. break;
  73. }
  74. case ION_HEAP_VMALLOC:
  75. ret = remap_vmalloc_range(vma, buffer->priv, vma->vm_pgoff);
  76. break;
  77. default:
  78. pr_err("%s: attempting to map unsupported heap to userspace\n",
  79. __func__);
  80. return -EINVAL;
  81. }
  82. return ret;
  83. }
  84. static struct ion_mapper_ops ops = {
  85. .map = ion_kernel_mapper_map,
  86. .map_kernel = ion_kernel_mapper_map_kernel,
  87. .map_user = ion_kernel_mapper_map_user,
  88. .unmap = ion_kernel_mapper_unmap,
  89. };
  90. struct ion_mapper *ion_system_mapper_create(void)
  91. {
  92. struct ion_mapper *mapper;
  93. mapper = kzalloc(sizeof(struct ion_mapper), GFP_KERNEL);
  94. if (!mapper)
  95. return ERR_PTR(-ENOMEM);
  96. mapper->type = ION_SYSTEM_MAPPER;
  97. mapper->ops = &ops;
  98. mapper->heap_mask = (1 << ION_HEAP_VMALLOC) | (1 << ION_HEAP_KMALLOC);
  99. return mapper;
  100. }
  101. void ion_system_mapper_destroy(struct ion_mapper *mapper)
  102. {
  103. kfree(mapper);
  104. }