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 need to work out if the MMU is relevant (eg for
23 * accelerated operations on a GEM object)
31 int psb_gem_init_object(struct drm_gem_object *obj)
36 void psb_gem_free_object(struct drm_gem_object *obj)
38 struct gtt_range *gtt = container_of(obj, struct gtt_range, gem);
39 drm_gem_object_release_wrap(obj);
40 /* This must occur last as it frees up the memory of the GEM object */
41 psb_gtt_free_range(obj->dev, gtt);
44 int psb_gem_get_aperture(struct drm_device *dev, void *data,
45 struct drm_file *file)
51 * psb_gem_dumb_map_gtt - buffer mapping for dumb interface
52 * @file: our drm client file
54 * @handle: GEM handle to the object (from dumb_create)
56 * Do the necessary setup to allow the mapping of the frame buffer
57 * into user memory. We don't have to do much here at the moment.
59 int psb_gem_dumb_map_gtt(struct drm_file *file, struct drm_device *dev,
60 uint32_t handle, uint64_t *offset)
63 struct drm_gem_object *obj;
65 if (!(dev->driver->driver_features & DRIVER_GEM))
68 mutex_lock(&dev->struct_mutex);
70 /* GEM does all our handle to object mapping */
71 obj = drm_gem_object_lookup(dev, file, handle);
76 /* What validation is needed here ? */
78 /* Make it mmapable */
79 if (!obj->map_list.map) {
80 ret = gem_create_mmap_offset(obj);
84 /* GEM should really work out the hash offsets for us */
85 *offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
87 drm_gem_object_unreference(obj);
89 mutex_unlock(&dev->struct_mutex);
94 * psb_gem_create - create a mappable object
95 * @file: the DRM file of the client
97 * @size: the size requested
98 * @handlep: returned handle (opaque number)
100 * Create a GEM object, fill in the boilerplate and attach a handle to
101 * it so that userspace can speak about it. This does the core work
102 * for the various methods that do/will create GEM objects for things
104 static int psb_gem_create(struct drm_file *file,
105 struct drm_device *dev, uint64_t size, uint32_t *handlep)
111 size = roundup(size, PAGE_SIZE);
113 /* Allocate our object - for now a direct gtt range which is not
114 stolen memory backed */
115 r = psb_gtt_alloc_range(dev, size, "gem", 0);
117 dev_err(dev->dev, "no memory for %lld byte GEM object\n", size);
120 /* Initialize the extra goodies GEM needs to do all the hard work */
121 if (drm_gem_object_init(dev, &r->gem, size) != 0) {
122 psb_gtt_free_range(dev, r);
123 /* GEM doesn't give an error code and we don't have an
124 EGEMSUCKS so make something up for now - FIXME */
125 dev_err(dev->dev, "GEM init failed for %lld\n", size);
128 /* Give the object a handle so we can carry it more easily */
129 ret = drm_gem_handle_create(file, &r->gem, &handle);
131 dev_err(dev->dev, "GEM handle failed for %p, %lld\n",
133 drm_gem_object_release(&r->gem);
134 psb_gtt_free_range(dev, r);
137 /* We have the initial and handle reference but need only one now */
138 drm_gem_object_unreference(&r->gem);
144 * psb_gem_dumb_create - create a dumb buffer
145 * @drm_file: our client file
147 * @args: the requested arguments copied from userspace
149 * Allocate a buffer suitable for use for a frame buffer of the
150 * form described by user space. Give userspace a handle by which
153 int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
154 struct drm_mode_create_dumb *args)
156 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
157 args->size = args->pitch * args->height;
158 return psb_gem_create(file, dev, args->size, &args->handle);
162 * psb_gem_dumb_destroy - destroy a dumb buffer
164 * @dev: our DRM device
165 * @handle: the object handle
167 * Destroy a handle that was created via psb_gem_dumb_create, at least
168 * we hope it was created that way. i915 seems to assume the caller
169 * does the checking but that might be worth review ! FIXME
171 int psb_gem_dumb_destroy(struct drm_file *file, struct drm_device *dev,
174 /* No special work needed, drop the reference and see what falls out */
175 return drm_gem_handle_delete(file, handle);
179 * psb_gem_fault - pagefault handler for GEM objects
180 * @vma: the VMA of the GEM object
183 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
184 * does most of the work for us including the actual map/unmap calls
185 * but we need to do the actual page work.
187 * This code eventually needs to handle faulting objects in and out
188 * of the GTT and repacking it when we run out of space. We can put
189 * that off for now and for our simple uses
191 * The VMA was set up by GEM. In doing so it also ensured that the
192 * vma->vm_private_data points to the GEM object that is backing this
197 int psb_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
199 struct drm_gem_object *obj;
204 struct drm_device *dev;
205 struct drm_psb_private *dev_priv;
207 obj = vma->vm_private_data; /* GEM object */
209 dev_priv = dev->dev_private;
211 r = container_of(obj, struct gtt_range, gem); /* Get the gtt range */
213 /* Make sure we don't parallel update on a fault, nor move or remove
214 something from beneath our feet */
215 mutex_lock(&dev->struct_mutex);
217 /* For now the mmap pins the object and it stays pinned. As things
218 stand that will do us no harm */
219 if (r->mmapping == 0) {
220 ret = psb_gtt_pin(r);
222 dev_err(dev->dev, "gma500: pin failed: %d\n", ret);
228 /* Page relative to the VMA start - we must calculate this ourselves
229 because vmf->pgoff is the fake GEM offset */
230 page_offset = ((unsigned long) vmf->virtual_address - vma->vm_start)
233 /* CPU view of the page, don't go via the GART for CPU writes */
235 pfn = (dev_priv->stolen_base + r->offset) >> PAGE_SHIFT;
237 pfn = page_to_pfn(r->pages[page_offset]);
238 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
241 mutex_unlock(&dev->struct_mutex);
246 return VM_FAULT_NOPAGE;
250 return VM_FAULT_SIGBUS;
254 static int psb_gem_create_stolen(struct drm_file *file, struct drm_device *dev,
255 int size, u32 *handle)
257 struct gtt_range *gtt = psb_gtt_alloc_range(dev, size, "gem", 1);
260 if (drm_gem_private_object_init(dev, >t->gem, size) != 0)
262 if (drm_gem_handle_create(file, >t->gem, handle) == 0)
265 psb_gtt_free_range(dev, gtt);
270 * GEM interfaces for our specific client
272 int psb_gem_create_ioctl(struct drm_device *dev, void *data,
273 struct drm_file *file)
275 struct drm_psb_gem_create *args = data;
277 if (args->flags & PSB_GEM_CREATE_STOLEN) {
278 ret = psb_gem_create_stolen(file, dev, args->size,
283 args->flags &= ~PSB_GEM_CREATE_STOLEN;
285 return psb_gem_create(file, dev, args->size, &args->handle);
288 int psb_gem_mmap_ioctl(struct drm_device *dev, void *data,
289 struct drm_file *file)
291 struct drm_psb_gem_mmap *args = data;
292 return dev->driver->dumb_map_offset(file, dev,
293 args->handle, &args->offset);