gem.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * psb GEM interface
  3. *
  4. * Copyright (c) 2011, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Authors: Alan Cox
  20. *
  21. * TODO:
  22. * - we need to work out if the MMU is relevant (eg for
  23. * accelerated operations on a GEM object)
  24. */
  25. #include <drm/drmP.h>
  26. #include <drm/drm.h>
  27. #include <drm/gma_drm.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include "psb_drv.h"
  30. void psb_gem_free_object(struct drm_gem_object *obj)
  31. {
  32. struct gtt_range *gtt = container_of(obj, struct gtt_range, gem);
  33. /* Remove the list map if one is present */
  34. drm_gem_free_mmap_offset(obj);
  35. drm_gem_object_release(obj);
  36. /* This must occur last as it frees up the memory of the GEM object */
  37. psb_gtt_free_range(obj->dev, gtt);
  38. }
  39. int psb_gem_get_aperture(struct drm_device *dev, void *data,
  40. struct drm_file *file)
  41. {
  42. return -EINVAL;
  43. }
  44. /**
  45. * psb_gem_create - create a mappable object
  46. * @file: the DRM file of the client
  47. * @dev: our device
  48. * @size: the size requested
  49. * @handlep: returned handle (opaque number)
  50. *
  51. * Create a GEM object, fill in the boilerplate and attach a handle to
  52. * it so that userspace can speak about it. This does the core work
  53. * for the various methods that do/will create GEM objects for things
  54. */
  55. int psb_gem_create(struct drm_file *file, struct drm_device *dev, u64 size,
  56. u32 *handlep, int stolen, u32 align)
  57. {
  58. struct gtt_range *r;
  59. int ret;
  60. u32 handle;
  61. size = roundup(size, PAGE_SIZE);
  62. /* Allocate our object - for now a direct gtt range which is not
  63. stolen memory backed */
  64. r = psb_gtt_alloc_range(dev, size, "gem", 0, PAGE_SIZE);
  65. if (r == NULL) {
  66. dev_err(dev->dev, "no memory for %lld byte GEM object\n", size);
  67. return -ENOSPC;
  68. }
  69. /* Initialize the extra goodies GEM needs to do all the hard work */
  70. if (drm_gem_object_init(dev, &r->gem, size) != 0) {
  71. psb_gtt_free_range(dev, r);
  72. /* GEM doesn't give an error code so use -ENOMEM */
  73. dev_err(dev->dev, "GEM init failed for %lld\n", size);
  74. return -ENOMEM;
  75. }
  76. /* Limit the object to 32bit mappings */
  77. mapping_set_gfp_mask(r->gem.filp->f_mapping, GFP_KERNEL | __GFP_DMA32);
  78. /* Give the object a handle so we can carry it more easily */
  79. ret = drm_gem_handle_create(file, &r->gem, &handle);
  80. if (ret) {
  81. dev_err(dev->dev, "GEM handle failed for %p, %lld\n",
  82. &r->gem, size);
  83. drm_gem_object_release(&r->gem);
  84. psb_gtt_free_range(dev, r);
  85. return ret;
  86. }
  87. /* We have the initial and handle reference but need only one now */
  88. drm_gem_object_put_unlocked(&r->gem);
  89. *handlep = handle;
  90. return 0;
  91. }
  92. /**
  93. * psb_gem_dumb_create - create a dumb buffer
  94. * @drm_file: our client file
  95. * @dev: our device
  96. * @args: the requested arguments copied from userspace
  97. *
  98. * Allocate a buffer suitable for use for a frame buffer of the
  99. * form described by user space. Give userspace a handle by which
  100. * to reference it.
  101. */
  102. int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
  103. struct drm_mode_create_dumb *args)
  104. {
  105. args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
  106. args->size = args->pitch * args->height;
  107. return psb_gem_create(file, dev, args->size, &args->handle, 0,
  108. PAGE_SIZE);
  109. }
  110. /**
  111. * psb_gem_fault - pagefault handler for GEM objects
  112. * @vma: the VMA of the GEM object
  113. * @vmf: fault detail
  114. *
  115. * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
  116. * does most of the work for us including the actual map/unmap calls
  117. * but we need to do the actual page work.
  118. *
  119. * This code eventually needs to handle faulting objects in and out
  120. * of the GTT and repacking it when we run out of space. We can put
  121. * that off for now and for our simple uses
  122. *
  123. * The VMA was set up by GEM. In doing so it also ensured that the
  124. * vma->vm_private_data points to the GEM object that is backing this
  125. * mapping.
  126. */
  127. vm_fault_t psb_gem_fault(struct vm_fault *vmf)
  128. {
  129. struct vm_area_struct *vma = vmf->vma;
  130. struct drm_gem_object *obj;
  131. struct gtt_range *r;
  132. int err;
  133. vm_fault_t ret;
  134. unsigned long pfn;
  135. pgoff_t page_offset;
  136. struct drm_device *dev;
  137. struct drm_psb_private *dev_priv;
  138. obj = vma->vm_private_data; /* GEM object */
  139. dev = obj->dev;
  140. dev_priv = dev->dev_private;
  141. r = container_of(obj, struct gtt_range, gem); /* Get the gtt range */
  142. /* Make sure we don't parallel update on a fault, nor move or remove
  143. something from beneath our feet */
  144. mutex_lock(&dev_priv->mmap_mutex);
  145. /* For now the mmap pins the object and it stays pinned. As things
  146. stand that will do us no harm */
  147. if (r->mmapping == 0) {
  148. err = psb_gtt_pin(r);
  149. if (err < 0) {
  150. dev_err(dev->dev, "gma500: pin failed: %d\n", err);
  151. ret = vmf_error(err);
  152. goto fail;
  153. }
  154. r->mmapping = 1;
  155. }
  156. /* Page relative to the VMA start - we must calculate this ourselves
  157. because vmf->pgoff is the fake GEM offset */
  158. page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
  159. /* CPU view of the page, don't go via the GART for CPU writes */
  160. if (r->stolen)
  161. pfn = (dev_priv->stolen_base + r->offset) >> PAGE_SHIFT;
  162. else
  163. pfn = page_to_pfn(r->pages[page_offset]);
  164. ret = vmf_insert_pfn(vma, vmf->address, pfn);
  165. fail:
  166. mutex_unlock(&dev_priv->mmap_mutex);
  167. return ret;
  168. }