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 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
);
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
)
65 * psb_gem_create_mmap_offset - invent an mmap offset
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
;
79 list
= &obj
->map_list
;
80 list
->map
= kzalloc(sizeof(struct drm_map_list
), GFP_KERNEL
);
81 if (list
->map
== NULL
)
85 map
->size
= obj
->size
;
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
);
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
) {
101 list
->hash
.key
= list
->file_offset_node
->start
;
102 ret
= drm_ht_insert_item(&mm
->offset_hash
, &list
->hash
);
104 DRM_ERROR("failed to add to map hash\n");
110 drm_mm_put_block(list
->file_offset_node
);
118 * psb_gem_dumb_map_gtt - buffer mapping for dumb interface
119 * @file: our drm client file
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
)
130 struct drm_gem_object
*obj
;
132 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
135 mutex_lock(&dev
->struct_mutex
);
137 /* GEM does all our handle to object mapping */
138 obj
= drm_gem_object_lookup(dev
, file
, handle
);
143 /* What validation is needed here ? */
145 /* Make it mmapable */
146 if (!obj
->map_list
.map
) {
147 ret
= psb_gem_create_mmap_offset(obj
);
151 /* GEM should really work out the hash offsets for us */
152 *offset
= (u64
)obj
->map_list
.hash
.key
<< PAGE_SHIFT
;
154 drm_gem_object_unreference(obj
);
156 mutex_unlock(&dev
->struct_mutex
);
161 * psb_gem_create - create a mappable object
162 * @file: the DRM file of the client
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
)
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);
186 dev_err(dev
->dev
, "no memory for %lld byte GEM object\n", size
);
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
);
197 /* Give the object a handle so we can carry it more easily */
198 ret
= drm_gem_handle_create(file
, &r
->gem
, &handle
);
200 dev_err(dev
->dev
, "GEM handle failed for %p, %lld\n",
202 drm_gem_object_release(&r
->gem
);
203 psb_gtt_free_range(dev
, r
);
206 /* We have the initial and handle reference but need only one now */
207 drm_gem_object_unreference(&r
->gem
);
209 dev_err(dev
->dev
, "GEM handle %x for %p OK\n",
215 * psb_gem_dumb_create - create a dumb buffer
216 * @drm_file: our client file
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
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
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
,
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
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
268 int psb_gem_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
270 struct drm_gem_object
*obj
;
275 struct drm_device
*dev
;
277 obj
= vma
->vm_private_data
; /* GEM object */
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
);
294 DRM_ERROR("gma500: pin failed: %d\n", ret
);
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
)
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
);
313 mutex_unlock(&dev
->struct_mutex
);
318 return VM_FAULT_NOPAGE
;
322 return VM_FAULT_SIGBUS
;