gma500: revamp frame buffer creation and handling
[linux-2.6/x86.git] / drivers / staging / gma500 / psb_gem.c
blobb24b9642ab61387ae1733bff246a6e7948091da4
1 /*
2 * psb GEM interface
4 * Copyright (c) 2011, Intel Corporation.
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.
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.
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.
19 * Authors: Alan Cox
21 * TODO:
22 * - we don't actually put GEM objects into the GART yet
23 * - we need to work out if the MMU is relevant as well (eg for
24 * accelerated operations on a GEM object)
25 * - cache coherency
27 * ie this is just an initial framework to get us going.
30 #include <drm/drmP.h>
31 #include <drm/drm.h>
32 #include "psb_drm.h"
33 #include "psb_drv.h"
35 int psb_gem_init_object(struct drm_gem_object *obj)
37 return -EINVAL;
40 void psb_gem_free_object(struct drm_gem_object *obj)
42 struct gtt_range *gtt = container_of(obj, struct gtt_range, gem);
43 if (obj->map_list.map) {
44 /* Do things GEM should do for us */
45 struct drm_gem_mm *mm = obj->dev->mm_private;
46 struct drm_map_list *list = &obj->map_list;
47 drm_ht_remove_item(&mm->offset_hash, &list->hash);
48 drm_mm_put_block(list->file_offset_node);
49 kfree(list->map);
50 list->map = NULL;
52 drm_gem_object_release(obj);
53 /* This must occur last as it frees up the memory of the GEM object */
54 pr_err("GEM destroyed %p, %p\n", gtt, obj);
55 psb_gtt_free_range(obj->dev, gtt);
58 int psb_gem_get_aperture(struct drm_device *dev, void *data,
59 struct drm_file *file)
61 return -EINVAL;
64 /**
65 * psb_gem_create_mmap_offset - invent an mmap offset
66 * @obj: our object
68 * This is basically doing by hand a pile of ugly crap which should
69 * be done automatically by the GEM library code but isn't
71 static int psb_gem_create_mmap_offset(struct drm_gem_object *obj)
73 struct drm_device *dev = obj->dev;
74 struct drm_gem_mm *mm = dev->mm_private;
75 struct drm_map_list *list;
76 struct drm_local_map *map;
77 int ret;
79 list = &obj->map_list;
80 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
81 if (list->map == NULL)
82 return -ENOMEM;
83 map = list->map;
84 map->type = _DRM_GEM;
85 map->size = obj->size;
86 map->handle =obj;
88 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
89 obj->size / PAGE_SIZE, 0, 0);
90 if (!list->file_offset_node) {
91 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
92 ret = -ENOSPC;
93 goto free_it;
95 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
96 obj->size / PAGE_SIZE, 0);
97 if (!list->file_offset_node) {
98 ret = -ENOMEM;
99 goto free_it;
101 list->hash.key = list->file_offset_node->start;
102 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
103 if (ret) {
104 DRM_ERROR("failed to add to map hash\n");
105 goto free_mm;
107 return 0;
109 free_mm:
110 drm_mm_put_block(list->file_offset_node);
111 free_it:
112 kfree(list->map);
113 list->map = NULL;
114 return ret;
118 * psb_gem_dumb_map_gtt - buffer mapping for dumb interface
119 * @file: our drm client file
120 * @dev: drm device
121 * @handle: GEM handle to the object (from dumb_create)
123 * Do the necessary setup to allow the mapping of the frame buffer
124 * into user memory. We don't have to do much here at the moment.
126 int psb_gem_dumb_map_gtt(struct drm_file *file, struct drm_device *dev,
127 uint32_t handle, uint64_t *offset)
129 int ret = 0;
130 struct drm_gem_object *obj;
132 if (!(dev->driver->driver_features & DRIVER_GEM))
133 return -ENODEV;
135 mutex_lock(&dev->struct_mutex);
137 /* GEM does all our handle to object mapping */
138 obj = drm_gem_object_lookup(dev, file, handle);
139 if (obj == NULL) {
140 ret = -ENOENT;
141 goto unlock;
143 /* What validation is needed here ? */
145 /* Make it mmapable */
146 if (!obj->map_list.map) {
147 ret = psb_gem_create_mmap_offset(obj);
148 if (ret)
149 goto out;
151 /* GEM should really work out the hash offsets for us */
152 *offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
153 out:
154 drm_gem_object_unreference(obj);
155 unlock:
156 mutex_unlock(&dev->struct_mutex);
157 return ret;
161 * psb_gem_create - create a mappable object
162 * @file: the DRM file of the client
163 * @dev: our device
164 * @size: the size requested
165 * @handlep: returned handle (opaque number)
167 * Create a GEM object, fill in the boilerplate and attach a handle to
168 * it so that userspace can speak about it. This does the core work
169 * for the various methods that do/will create GEM objects for things
171 static int psb_gem_create(struct drm_file *file,
172 struct drm_device *dev, uint64_t size, uint32_t *handlep)
174 struct gtt_range *r;
175 int ret;
176 u32 handle;
178 size = roundup(size, PAGE_SIZE);
180 dev_err(dev->dev, "GEM creating %lld\n", size);
182 /* Allocate our object - for now a direct gtt range which is not
183 stolen memory backed */
184 r = psb_gtt_alloc_range(dev, size, "gem", 0);
185 if (r == NULL) {
186 dev_err(dev->dev, "no memory for %lld byte GEM object\n", size);
187 return -ENOSPC;
189 /* Initialize the extra goodies GEM needs to do all the hard work */
190 if (drm_gem_object_init(dev, &r->gem, size) != 0) {
191 psb_gtt_free_range(dev, r);
192 /* GEM doesn't give an error code and we don't have an
193 EGEMSUCKS so make something up for now - FIXME */
194 dev_err(dev->dev, "GEM init failed for %lld\n", size);
195 return -ENOMEM;
197 /* Give the object a handle so we can carry it more easily */
198 ret = drm_gem_handle_create(file, &r->gem, &handle);
199 if (ret) {
200 dev_err(dev->dev, "GEM handle failed for %p, %lld\n",
201 &r->gem, size);
202 drm_gem_object_release(&r->gem);
203 psb_gtt_free_range(dev, r);
204 return ret;
206 /* We have the initial and handle reference but need only one now */
207 drm_gem_object_unreference(&r->gem);
208 *handlep = handle;
209 dev_err(dev->dev, "GEM handle %x for %p OK\n",
210 handle, &r->gem);
211 return 0;
215 * psb_gem_dumb_create - create a dumb buffer
216 * @drm_file: our client file
217 * @dev: our device
218 * @args: the requested arguments copied from userspace
220 * Allocate a buffer suitable for use for a frame buffer of the
221 * form described by user space. Give userspace a handle by which
222 * to reference it.
224 int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
225 struct drm_mode_create_dumb *args)
227 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
228 args->size = args->pitch * args->height;
229 return psb_gem_create(file, dev, args->size, &args->handle);
233 * psb_gem_dumb_destroy - destroy a dumb buffer
234 * @file: client file
235 * @dev: our DRM device
236 * @handle: the object handle
238 * Destroy a handle that was created via psb_gem_dumb_create, at least
239 * we hope it was created that way. i915 seems to assume the caller
240 * does the checking but that might be worth review ! FIXME
242 int psb_gem_dumb_destroy(struct drm_file *file, struct drm_device *dev,
243 uint32_t handle)
245 /* No special work needed, drop the reference and see what falls out */
246 return drm_gem_handle_delete(file, handle);
250 * psb_gem_fault - pagefault handler for GEM objects
251 * @vma: the VMA of the GEM object
252 * @vmf: fault detail
254 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
255 * does most of the work for us including the actual map/unmap calls
256 * but we need to do the actual page work.
258 * This code eventually needs to handle faulting objects in and out
259 * of the GTT and repacking it when we run out of space. We can put
260 * that off for now and for our simple uses
262 * The VMA was set up by GEM. In doing so it also ensured that the
263 * vma->vm_private_data points to the GEM object that is backing this
264 * mapping.
266 * FIXME
268 int psb_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
270 struct drm_gem_object *obj;
271 struct gtt_range *r;
272 int ret;
273 unsigned long pfn;
274 pgoff_t page_offset;
275 struct drm_device *dev;
277 obj = vma->vm_private_data; /* GEM object */
278 dev = obj->dev;
280 r = container_of(obj, struct gtt_range, gem); /* Get the gtt range */
282 /* Make sure we don't parallel update on a fault, nor move or remove
283 something from beneath our feet */
284 mutex_lock(&dev->struct_mutex);
286 dev_err(dev->dev, "Fault on GTT %p\n", r);
288 /* For now the mmap pins the object and it stays pinned. As things
289 stand that will do us no harm */
290 if (r->mmapping == 0) {
291 dev_err(dev->dev, "Need to pin %p\n", r);
292 ret = psb_gtt_pin(r);
293 if (ret < 0) {
294 DRM_ERROR("gma500: pin failed: %d\n", ret);
295 goto fail;
297 r->mmapping = 1;
300 /* Page relative to the VMA start - we must calculate this ourselves
301 because vmf->pgoff is the fake GEM offset */
302 page_offset = ((unsigned long) vmf->virtual_address - vma->vm_start)
303 >> PAGE_SHIFT;
305 dev_err(dev->dev, "Page offset %p %d\n", r, (int)page_offset);
306 /* CPU view of the page, don't go via the GART for CPU writes */
307 pfn = page_to_phys(r->pages[page_offset]) >> PAGE_SHIFT;
308 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
310 dev_err(dev->dev, "PFN %ld for VA %p = %d\n", pfn, vmf->virtual_address, ret);
312 fail:
313 mutex_unlock(&dev->struct_mutex);
314 switch (ret) {
315 case 0:
316 case -ERESTARTSYS:
317 case -EINTR:
318 return VM_FAULT_NOPAGE;
319 case -ENOMEM:
320 return VM_FAULT_OOM;
321 default:
322 return VM_FAULT_SIGBUS;