]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/staging/gma500/psb_gem.c
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
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.
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)
27 * ie this is just an initial framework to get us going.
35 int psb_gem_init_object(struct drm_gem_object
*obj
)
40 void psb_gem_free_object(struct drm_gem_object
*obj
)
42 struct gtt_range
*gtt
= container_of(obj
, struct gtt_range
, gem
);
43 psb_gtt_free_range(obj
->dev
, gtt
);
44 if (obj
->map_list
.map
) {
45 /* Do things GEM should do for us */
46 struct drm_gem_mm
*mm
= obj
->dev
->mm_private
;
47 struct drm_map_list
*list
= &obj
->map_list
;
48 drm_ht_remove_item(&mm
->offset_hash
, &list
->hash
);
49 drm_mm_put_block(list
->file_offset_node
);
53 drm_gem_object_release(obj
);
56 int psb_gem_get_aperture(struct drm_device
*dev
, void *data
,
57 struct drm_file
*file
)
63 * psb_gem_create_mmap_offset - invent an mmap offset
66 * This is basically doing by hand a pile of ugly crap which should
67 * be done automatically by the GEM library code but isn't
69 static int psb_gem_create_mmap_offset(struct drm_gem_object
*obj
)
71 struct drm_device
*dev
= obj
->dev
;
72 struct drm_gem_mm
*mm
= dev
->mm_private
;
73 struct drm_map_list
*list
;
74 struct drm_local_map
*map
;
77 list
= &obj
->map_list
;
78 list
->map
= kzalloc(sizeof(struct drm_map_list
), GFP_KERNEL
);
79 if (list
->map
== NULL
)
83 map
->size
= obj
->size
;
86 list
->file_offset_node
= drm_mm_search_free(&mm
->offset_manager
,
87 obj
->size
/ PAGE_SIZE
, 0, 0);
88 if (!list
->file_offset_node
) {
89 DRM_ERROR("failed to allocate offset for bo %d\n", obj
->name
);
93 list
->file_offset_node
= drm_mm_get_block(list
->file_offset_node
,
94 obj
->size
/ PAGE_SIZE
, 0);
95 if (!list
->file_offset_node
) {
99 list
->hash
.key
= list
->file_offset_node
->start
;
100 ret
= drm_ht_insert_item(&mm
->offset_hash
, &list
->hash
);
102 DRM_ERROR("failed to add to map hash\n");
108 drm_mm_put_block(list
->file_offset_node
);
116 * psb_gem_dumb_map_gtt - buffer mapping for dumb interface
117 * @file: our drm client file
119 * @handle: GEM handle to the object (from dumb_create)
121 * Do the necessary setup to allow the mapping of the frame buffer
122 * into user memory. We don't have to do much here at the moment.
124 int psb_gem_dumb_map_gtt(struct drm_file
*file
, struct drm_device
*dev
,
125 uint32_t handle
, uint64_t *offset
)
128 struct drm_gem_object
*obj
;
130 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
133 mutex_lock(&dev
->struct_mutex
);
135 /* GEM does all our handle to object mapping */
136 obj
= drm_gem_object_lookup(dev
, file
, handle
);
141 /* What validation is needed here ? */
143 /* Make it mmapable */
144 if (!obj
->map_list
.map
) {
145 ret
= psb_gem_create_mmap_offset(obj
);
149 /* GEM should really work out the hash offsets for us */
150 *offset
= (u64
)obj
->map_list
.hash
.key
<< PAGE_SHIFT
;
152 drm_gem_object_unreference(obj
);
154 mutex_unlock(&dev
->struct_mutex
);
159 * psb_gem_create - create a mappable object
160 * @file: the DRM file of the client
162 * @size: the size requested
163 * @handlep: returned handle (opaque number)
165 * Create a GEM object, fill in the boilerplate and attach a handle to
166 * it so that userspace can speak about it. This does the core work
167 * for the various methods that do/will create GEM objects for things
169 static int psb_gem_create(struct drm_file
*file
,
170 struct drm_device
*dev
, uint64_t size
, uint32_t *handlep
)
176 size
= roundup(size
, PAGE_SIZE
);
178 /* Allocate our object - for now a direct gtt range which is not
179 stolen memory backed */
180 r
= psb_gtt_alloc_range(dev
, size
, "gem", 0);
183 /* Initialize the extra goodies GEM needs to do all the hard work */
184 if (drm_gem_object_init(dev
, &r
->gem
, size
) != 0) {
185 psb_gtt_free_range(dev
, r
);
186 /* GEM doesn't give an error code and we don't have an
187 EGEMSUCKS so make something up for now - FIXME */
190 /* Give the object a handle so we can carry it more easily */
191 ret
= drm_gem_handle_create(file
, &r
->gem
, &handle
);
193 drm_gem_object_release(&r
->gem
);
194 psb_gtt_free_range(dev
, r
);
197 /* We have the initial and handle reference but need only one now */
198 drm_gem_object_unreference(&r
->gem
);
204 * psb_gem_dumb_create - create a dumb buffer
205 * @drm_file: our client file
207 * @args: the requested arguments copied from userspace
209 * Allocate a buffer suitable for use for a frame buffer of the
210 * form described by user space. Give userspace a handle by which
213 int psb_gem_dumb_create(struct drm_file
*file
, struct drm_device
*dev
,
214 struct drm_mode_create_dumb
*args
)
216 args
->pitch
= ALIGN(args
->width
* ((args
->bpp
+ 7) / 8), 64);
217 args
->size
= args
->pitch
* args
->height
;
218 return psb_gem_create(file
, dev
, args
->size
, &args
->handle
);
222 * psb_gem_dumb_destroy - destroy a dumb buffer
224 * @dev: our DRM device
225 * @handle: the object handle
227 * Destroy a handle that was created via psb_gem_dumb_create, at least
228 * we hope it was created that way. i915 seems to assume the caller
229 * does the checking but that might be worth review ! FIXME
231 int psb_gem_dumb_destroy(struct drm_file
*file
, struct drm_device
*dev
,
234 /* No special work needed, drop the reference and see what falls out */
235 return drm_gem_handle_delete(file
, handle
);
239 * psb_gem_fault - pagefault handler for GEM objects
240 * @vma: the VMA of the GEM object
243 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
244 * does most of the work for us including the actual map/unmap calls
245 * but we need to do the actual page work.
247 * This code eventually needs to handle faulting objects in and out
248 * of the GART and repacking it when we run out of space. We can put
249 * that off for now and for our simple uses
251 * The VMA was set up by GEM. In doing so it also ensured that the
252 * vma->vm_private_data points to the GEM object that is backing this
255 * To avoid aliasing and cache funnies we want to map the object
256 * through the GART. For the moment this is slightly hackish. It would
257 * be nicer if GEM provided mmap opened/closed hooks for us giving
258 * the object so that we could track things nicely. That needs changes
259 * to the core GEM code so must be tackled post staging
263 int psb_gem_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
265 struct drm_gem_object
*obj
;
270 struct drm_device
*dev
;
272 obj
= vma
->vm_private_data
; /* GEM object */
275 r
= container_of(obj
, struct gtt_range
, gem
); /* Get the gtt range */
277 /* Make sure we don't parallel update on a fault, nor move or remove
278 something from beneath our feet */
279 mutex_lock(&dev
->struct_mutex
);
281 /* For now the mmap pins the object and it stays pinned. As things
282 stand that will do us no harm */
283 if (r
->mmapping
== 0) {
284 ret
= psb_gtt_pin(r
);
286 DRM_ERROR("gma500: pin failed: %d\n", ret
);
292 /* FIXME: Locking. We may also need to repack the GART sometimes */
294 /* Page relative to the VMA start */
295 page_offset
= ((unsigned long) vmf
->virtual_address
- vma
->vm_start
)
298 /* Bus address of the page is gart + object offset + page offset */
299 /* Assumes gtt allocations are page aligned */
300 pfn
= (r
->resource
.start
>> PAGE_SHIFT
) + page_offset
;
302 pr_debug("Object GTT base at %p\n", (void *)(r
->resource
.start
));
303 pr_debug("Inserting %p pfn %lx, pa %lx\n", vmf
->virtual_address
,
304 pfn
, pfn
<< PAGE_SHIFT
);
306 ret
= vm_insert_pfn(vma
, (unsigned long)vmf
->virtual_address
, pfn
);
309 mutex_unlock(&dev
->struct_mutex
);
314 return VM_FAULT_NOPAGE
;
318 return VM_FAULT_SIGBUS
;