gem.c 8.3 KB

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