Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gem.c
Go to the documentation of this file.
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 
26 #include <drm/drmP.h>
27 #include <drm/drm.h>
28 #include <drm/gma_drm.h>
29 #include "psb_drv.h"
30 
31 int psb_gem_init_object(struct drm_gem_object *obj)
32 {
33  return -EINVAL;
34 }
35 
36 void psb_gem_free_object(struct drm_gem_object *obj)
37 {
38  struct gtt_range *gtt = container_of(obj, struct gtt_range, gem);
39 
40  /* Remove the list map if one is present */
41  if (obj->map_list.map)
44 
45  /* This must occur last as it frees up the memory of the GEM object */
46  psb_gtt_free_range(obj->dev, gtt);
47 }
48 
50  struct drm_file *file)
51 {
52  return -EINVAL;
53 }
54 
64 int psb_gem_dumb_map_gtt(struct drm_file *file, struct drm_device *dev,
66 {
67  int ret = 0;
68  struct drm_gem_object *obj;
69 
70  if (!(dev->driver->driver_features & DRIVER_GEM))
71  return -ENODEV;
72 
73  mutex_lock(&dev->struct_mutex);
74 
75  /* GEM does all our handle to object mapping */
76  obj = drm_gem_object_lookup(dev, file, handle);
77  if (obj == NULL) {
78  ret = -ENOENT;
79  goto unlock;
80  }
81  /* What validation is needed here ? */
82 
83  /* Make it mmapable */
84  if (!obj->map_list.map) {
85  ret = drm_gem_create_mmap_offset(obj);
86  if (ret)
87  goto out;
88  }
89  /* GEM should really work out the hash offsets for us */
90  *offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
91 out:
92  drm_gem_object_unreference(obj);
93 unlock:
94  mutex_unlock(&dev->struct_mutex);
95  return ret;
96 }
97 
109 static int psb_gem_create(struct drm_file *file,
110  struct drm_device *dev, uint64_t size, uint32_t *handlep)
111 {
112  struct gtt_range *r;
113  int ret;
114  u32 handle;
115 
116  size = roundup(size, PAGE_SIZE);
117 
118  /* Allocate our object - for now a direct gtt range which is not
119  stolen memory backed */
120  r = psb_gtt_alloc_range(dev, size, "gem", 0);
121  if (r == NULL) {
122  dev_err(dev->dev, "no memory for %lld byte GEM object\n", size);
123  return -ENOSPC;
124  }
125  /* Initialize the extra goodies GEM needs to do all the hard work */
126  if (drm_gem_object_init(dev, &r->gem, size) != 0) {
127  psb_gtt_free_range(dev, r);
128  /* GEM doesn't give an error code so use -ENOMEM */
129  dev_err(dev->dev, "GEM init failed for %lld\n", size);
130  return -ENOMEM;
131  }
132  /* Limit the object to 32bit mappings */
133  mapping_set_gfp_mask(r->gem.filp->f_mapping, GFP_KERNEL | __GFP_DMA32);
134  /* Give the object a handle so we can carry it more easily */
135  ret = drm_gem_handle_create(file, &r->gem, &handle);
136  if (ret) {
137  dev_err(dev->dev, "GEM handle failed for %p, %lld\n",
138  &r->gem, size);
140  psb_gtt_free_range(dev, r);
141  return ret;
142  }
143  /* We have the initial and handle reference but need only one now */
144  drm_gem_object_unreference(&r->gem);
145  *handlep = handle;
146  return 0;
147 }
148 
159 int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
160  struct drm_mode_create_dumb *args)
161 {
162  args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
163  args->size = args->pitch * args->height;
164  return psb_gem_create(file, dev, args->size, &args->handle);
165 }
166 
177 int psb_gem_dumb_destroy(struct drm_file *file, struct drm_device *dev,
178  uint32_t handle)
179 {
180  /* No special work needed, drop the reference and see what falls out */
181  return drm_gem_handle_delete(file, handle);
182 }
183 
201 int psb_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
202 {
203  struct drm_gem_object *obj;
204  struct gtt_range *r;
205  int ret;
206  unsigned long pfn;
208  struct drm_device *dev;
209  struct drm_psb_private *dev_priv;
210 
211  obj = vma->vm_private_data; /* GEM object */
212  dev = obj->dev;
213  dev_priv = dev->dev_private;
214 
215  r = container_of(obj, struct gtt_range, gem); /* Get the gtt range */
216 
217  /* Make sure we don't parallel update on a fault, nor move or remove
218  something from beneath our feet */
219  mutex_lock(&dev->struct_mutex);
220 
221  /* For now the mmap pins the object and it stays pinned. As things
222  stand that will do us no harm */
223  if (r->mmapping == 0) {
224  ret = psb_gtt_pin(r);
225  if (ret < 0) {
226  dev_err(dev->dev, "gma500: pin failed: %d\n", ret);
227  goto fail;
228  }
229  r->mmapping = 1;
230  }
231 
232  /* Page relative to the VMA start - we must calculate this ourselves
233  because vmf->pgoff is the fake GEM offset */
234  page_offset = ((unsigned long) vmf->virtual_address - vma->vm_start)
235  >> PAGE_SHIFT;
236 
237  /* CPU view of the page, don't go via the GART for CPU writes */
238  if (r->stolen)
239  pfn = (dev_priv->stolen_base + r->offset) >> PAGE_SHIFT;
240  else
241  pfn = page_to_pfn(r->pages[page_offset]);
242  ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
243 
244 fail:
245  mutex_unlock(&dev->struct_mutex);
246  switch (ret) {
247  case 0:
248  case -ERESTARTSYS:
249  case -EINTR:
250  return VM_FAULT_NOPAGE;
251  case -ENOMEM:
252  return VM_FAULT_OOM;
253  default:
254  return VM_FAULT_SIGBUS;
255  }
256 }
257 
258 static int psb_gem_create_stolen(struct drm_file *file, struct drm_device *dev,
259  int size, u32 *handle)
260 {
261  struct gtt_range *gtt = psb_gtt_alloc_range(dev, size, "gem", 1);
262  if (gtt == NULL)
263  return -ENOMEM;
264  if (drm_gem_private_object_init(dev, &gtt->gem, size) != 0)
265  goto free_gtt;
266  if (drm_gem_handle_create(file, &gtt->gem, handle) == 0)
267  return 0;
268 free_gtt:
269  psb_gtt_free_range(dev, gtt);
270  return -ENOMEM;
271 }
272 
273 /*
274  * GEM interfaces for our specific client
275  */
276 int psb_gem_create_ioctl(struct drm_device *dev, void *data,
277  struct drm_file *file)
278 {
279  struct drm_psb_gem_create *args = data;
280  int ret;
281  if (args->flags & GMA_GEM_CREATE_STOLEN) {
282  ret = psb_gem_create_stolen(file, dev, args->size,
283  &args->handle);
284  if (ret == 0)
285  return 0;
286  /* Fall throguh */
287  args->flags &= ~GMA_GEM_CREATE_STOLEN;
288  }
289  return psb_gem_create(file, dev, args->size, &args->handle);
290 }
291 
292 int psb_gem_mmap_ioctl(struct drm_device *dev, void *data,
293  struct drm_file *file)
294 {
295  struct drm_psb_gem_mmap *args = data;
296  return dev->driver->dumb_map_offset(file, dev,
297  args->handle, &args->offset);
298 }
299