gem.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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_dumb_map_gtt - buffer mapping for dumb interface
  46. * @file: our drm client file
  47. * @dev: drm device
  48. * @handle: GEM handle to the object (from dumb_create)
  49. *
  50. * Do the necessary setup to allow the mapping of the frame buffer
  51. * into user memory. We don't have to do much here at the moment.
  52. */
  53. int psb_gem_dumb_map_gtt(struct drm_file *file, struct drm_device *dev,
  54. uint32_t handle, uint64_t *offset)
  55. {
  56. int ret = 0;
  57. struct drm_gem_object *obj;
  58. if (!(dev->driver->driver_features & DRIVER_GEM))
  59. return -ENODEV;
  60. mutex_lock(&dev->struct_mutex);
  61. /* GEM does all our handle to object mapping */
  62. obj = drm_gem_object_lookup(dev, file, handle);
  63. if (obj == NULL) {
  64. ret = -ENOENT;
  65. goto unlock;
  66. }
  67. /* What validation is needed here ? */
  68. /* Make it mmapable */
  69. ret = drm_gem_create_mmap_offset(obj);
  70. if (ret)
  71. goto out;
  72. *offset = drm_vma_node_offset_addr(&obj->vma_node);
  73. out:
  74. drm_gem_object_unreference(obj);
  75. unlock:
  76. mutex_unlock(&dev->struct_mutex);
  77. return ret;
  78. }
  79. /**
  80. * psb_gem_create - create a mappable object
  81. * @file: the DRM file of the client
  82. * @dev: our device
  83. * @size: the size requested
  84. * @handlep: returned handle (opaque number)
  85. *
  86. * Create a GEM object, fill in the boilerplate and attach a handle to
  87. * it so that userspace can speak about it. This does the core work
  88. * for the various methods that do/will create GEM objects for things
  89. */
  90. static int psb_gem_create(struct drm_file *file,
  91. struct drm_device *dev, uint64_t size, uint32_t *handlep)
  92. {
  93. struct gtt_range *r;
  94. int ret;
  95. u32 handle;
  96. size = roundup(size, PAGE_SIZE);
  97. /* Allocate our object - for now a direct gtt range which is not
  98. stolen memory backed */
  99. r = psb_gtt_alloc_range(dev, size, "gem", 0);
  100. if (r == NULL) {
  101. dev_err(dev->dev, "no memory for %lld byte GEM object\n", size);
  102. return -ENOSPC;
  103. }
  104. /* Initialize the extra goodies GEM needs to do all the hard work */
  105. if (drm_gem_object_init(dev, &r->gem, size) != 0) {
  106. psb_gtt_free_range(dev, r);
  107. /* GEM doesn't give an error code so use -ENOMEM */
  108. dev_err(dev->dev, "GEM init failed for %lld\n", size);
  109. return -ENOMEM;
  110. }
  111. /* Limit the object to 32bit mappings */
  112. mapping_set_gfp_mask(r->gem.filp->f_mapping, GFP_KERNEL | __GFP_DMA32);
  113. /* Give the object a handle so we can carry it more easily */
  114. ret = drm_gem_handle_create(file, &r->gem, &handle);
  115. if (ret) {
  116. dev_err(dev->dev, "GEM handle failed for %p, %lld\n",
  117. &r->gem, size);
  118. drm_gem_object_release(&r->gem);
  119. psb_gtt_free_range(dev, r);
  120. return ret;
  121. }
  122. /* We have the initial and handle reference but need only one now */
  123. drm_gem_object_unreference(&r->gem);
  124. *handlep = handle;
  125. return 0;
  126. }
  127. /**
  128. * psb_gem_dumb_create - create a dumb buffer
  129. * @drm_file: our client file
  130. * @dev: our device
  131. * @args: the requested arguments copied from userspace
  132. *
  133. * Allocate a buffer suitable for use for a frame buffer of the
  134. * form described by user space. Give userspace a handle by which
  135. * to reference it.
  136. */
  137. int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
  138. struct drm_mode_create_dumb *args)
  139. {
  140. args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
  141. args->size = args->pitch * args->height;
  142. return psb_gem_create(file, dev, args->size, &args->handle);
  143. }
  144. /**
  145. * psb_gem_fault - pagefault handler for GEM objects
  146. * @vma: the VMA of the GEM object
  147. * @vmf: fault detail
  148. *
  149. * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
  150. * does most of the work for us including the actual map/unmap calls
  151. * but we need to do the actual page work.
  152. *
  153. * This code eventually needs to handle faulting objects in and out
  154. * of the GTT and repacking it when we run out of space. We can put
  155. * that off for now and for our simple uses
  156. *
  157. * The VMA was set up by GEM. In doing so it also ensured that the
  158. * vma->vm_private_data points to the GEM object that is backing this
  159. * mapping.
  160. */
  161. int psb_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  162. {
  163. struct drm_gem_object *obj;
  164. struct gtt_range *r;
  165. int ret;
  166. unsigned long pfn;
  167. pgoff_t page_offset;
  168. struct drm_device *dev;
  169. struct drm_psb_private *dev_priv;
  170. obj = vma->vm_private_data; /* GEM object */
  171. dev = obj->dev;
  172. dev_priv = dev->dev_private;
  173. r = container_of(obj, struct gtt_range, gem); /* Get the gtt range */
  174. /* Make sure we don't parallel update on a fault, nor move or remove
  175. something from beneath our feet */
  176. mutex_lock(&dev->struct_mutex);
  177. /* For now the mmap pins the object and it stays pinned. As things
  178. stand that will do us no harm */
  179. if (r->mmapping == 0) {
  180. ret = psb_gtt_pin(r);
  181. if (ret < 0) {
  182. dev_err(dev->dev, "gma500: pin failed: %d\n", ret);
  183. goto fail;
  184. }
  185. r->mmapping = 1;
  186. }
  187. /* Page relative to the VMA start - we must calculate this ourselves
  188. because vmf->pgoff is the fake GEM offset */
  189. page_offset = ((unsigned long) vmf->virtual_address - vma->vm_start)
  190. >> PAGE_SHIFT;
  191. /* CPU view of the page, don't go via the GART for CPU writes */
  192. if (r->stolen)
  193. pfn = (dev_priv->stolen_base + r->offset) >> PAGE_SHIFT;
  194. else
  195. pfn = page_to_pfn(r->pages[page_offset]);
  196. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  197. fail:
  198. mutex_unlock(&dev->struct_mutex);
  199. switch (ret) {
  200. case 0:
  201. case -ERESTARTSYS:
  202. case -EINTR:
  203. return VM_FAULT_NOPAGE;
  204. case -ENOMEM:
  205. return VM_FAULT_OOM;
  206. default:
  207. return VM_FAULT_SIGBUS;
  208. }
  209. }
  210. static int psb_gem_create_stolen(struct drm_file *file, struct drm_device *dev,
  211. int size, u32 *handle)
  212. {
  213. struct gtt_range *gtt = psb_gtt_alloc_range(dev, size, "gem", 1);
  214. if (gtt == NULL)
  215. return -ENOMEM;
  216. drm_gem_private_object_init(dev, &gtt->gem, size);
  217. if (drm_gem_handle_create(file, &gtt->gem, handle) == 0)
  218. return 0;
  219. drm_gem_object_release(&gtt->gem);
  220. psb_gtt_free_range(dev, gtt);
  221. return -ENOMEM;
  222. }
  223. /*
  224. * GEM interfaces for our specific client
  225. */
  226. int psb_gem_create_ioctl(struct drm_device *dev, void *data,
  227. struct drm_file *file)
  228. {
  229. struct drm_psb_gem_create *args = data;
  230. int ret;
  231. if (args->flags & GMA_GEM_CREATE_STOLEN) {
  232. ret = psb_gem_create_stolen(file, dev, args->size,
  233. &args->handle);
  234. if (ret == 0)
  235. return 0;
  236. /* Fall throguh */
  237. args->flags &= ~GMA_GEM_CREATE_STOLEN;
  238. }
  239. return psb_gem_create(file, dev, args->size, &args->handle);
  240. }
  241. int psb_gem_mmap_ioctl(struct drm_device *dev, void *data,
  242. struct drm_file *file)
  243. {
  244. struct drm_psb_gem_mmap *args = data;
  245. return dev->driver->dumb_map_offset(file, dev,
  246. args->handle, &args->offset);
  247. }