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)
28 #include <drm/gma_drm.h>
29 #include <drm/drm_vma_manager.h>
32 void psb_gem_free_object(struct drm_gem_object *obj)
34 struct gtt_range *gtt = container_of(obj, struct gtt_range, gem);
36 /* Remove the list map if one is present */
37 drm_gem_free_mmap_offset(obj);
38 drm_gem_object_release(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 mutex_lock(&dev->struct_mutex);
67 /* GEM does all our handle to object mapping */
68 obj = drm_gem_object_lookup(dev, file, handle);
73 /* What validation is needed here ? */
75 /* Make it mmapable */
76 ret = drm_gem_create_mmap_offset(obj);
79 *offset = drm_vma_node_offset_addr(&obj->vma_node);
81 drm_gem_object_unreference(obj);
83 mutex_unlock(&dev->struct_mutex);
88 * psb_gem_create - create a mappable object
89 * @file: the DRM file of the client
91 * @size: the size requested
92 * @handlep: returned handle (opaque number)
94 * Create a GEM object, fill in the boilerplate and attach a handle to
95 * it so that userspace can speak about it. This does the core work
96 * for the various methods that do/will create GEM objects for things
98 int psb_gem_create(struct drm_file *file, struct drm_device *dev, u64 size,
99 u32 *handlep, int stolen, u32 align)
105 size = roundup(size, PAGE_SIZE);
107 /* Allocate our object - for now a direct gtt range which is not
108 stolen memory backed */
109 r = psb_gtt_alloc_range(dev, size, "gem", 0, PAGE_SIZE);
111 dev_err(dev->dev, "no memory for %lld byte GEM object\n", size);
114 /* Initialize the extra goodies GEM needs to do all the hard work */
115 if (drm_gem_object_init(dev, &r->gem, size) != 0) {
116 psb_gtt_free_range(dev, r);
117 /* GEM doesn't give an error code so use -ENOMEM */
118 dev_err(dev->dev, "GEM init failed for %lld\n", size);
121 /* Limit the object to 32bit mappings */
122 mapping_set_gfp_mask(r->gem.filp->f_mapping, GFP_KERNEL | __GFP_DMA32);
123 /* Give the object a handle so we can carry it more easily */
124 ret = drm_gem_handle_create(file, &r->gem, &handle);
126 dev_err(dev->dev, "GEM handle failed for %p, %lld\n",
128 drm_gem_object_release(&r->gem);
129 psb_gtt_free_range(dev, r);
132 /* We have the initial and handle reference but need only one now */
133 drm_gem_object_unreference(&r->gem);
139 * psb_gem_dumb_create - create a dumb buffer
140 * @drm_file: our client file
142 * @args: the requested arguments copied from userspace
144 * Allocate a buffer suitable for use for a frame buffer of the
145 * form described by user space. Give userspace a handle by which
148 int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
149 struct drm_mode_create_dumb *args)
151 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
152 args->size = args->pitch * args->height;
153 return psb_gem_create(file, dev, args->size, &args->handle, 0,
158 * psb_gem_fault - pagefault handler for GEM objects
159 * @vma: the VMA of the GEM object
162 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
163 * does most of the work for us including the actual map/unmap calls
164 * but we need to do the actual page work.
166 * This code eventually needs to handle faulting objects in and out
167 * of the GTT and repacking it when we run out of space. We can put
168 * that off for now and for our simple uses
170 * The VMA was set up by GEM. In doing so it also ensured that the
171 * vma->vm_private_data points to the GEM object that is backing this
174 int psb_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
176 struct drm_gem_object *obj;
181 struct drm_device *dev;
182 struct drm_psb_private *dev_priv;
184 obj = vma->vm_private_data; /* GEM object */
186 dev_priv = dev->dev_private;
188 r = container_of(obj, struct gtt_range, gem); /* Get the gtt range */
190 /* Make sure we don't parallel update on a fault, nor move or remove
191 something from beneath our feet */
192 mutex_lock(&dev->struct_mutex);
194 /* For now the mmap pins the object and it stays pinned. As things
195 stand that will do us no harm */
196 if (r->mmapping == 0) {
197 ret = psb_gtt_pin(r);
199 dev_err(dev->dev, "gma500: pin failed: %d\n", ret);
205 /* Page relative to the VMA start - we must calculate this ourselves
206 because vmf->pgoff is the fake GEM offset */
207 page_offset = ((unsigned long) vmf->virtual_address - vma->vm_start)
210 /* CPU view of the page, don't go via the GART for CPU writes */
212 pfn = (dev_priv->stolen_base + r->offset) >> PAGE_SHIFT;
214 pfn = page_to_pfn(r->pages[page_offset]);
215 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
218 mutex_unlock(&dev->struct_mutex);
223 return VM_FAULT_NOPAGE;
227 return VM_FAULT_SIGBUS;