Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 *  psb GEM interface
  4 *
  5 * Copyright (c) 2011, Intel Corporation.
  6 *
 
 
 
 
 
 
 
 
 
 
 
 
 
  7 * Authors: Alan Cox
  8 *
  9 * TODO:
 10 *	-	we need to work out if the MMU is relevant (eg for
 11 *		accelerated operations on a GEM object)
 12 */
 13
 14#include <linux/pagemap.h>
 15
 16#include <drm/drm.h>
 17#include <drm/drm_vma_manager.h>
 18
 19#include "psb_drv.h"
 20
 
 
 
 
 
 21void psb_gem_free_object(struct drm_gem_object *obj)
 22{
 23	struct gtt_range *gtt = container_of(obj, struct gtt_range, gem);
 24
 25	/* Remove the list map if one is present */
 26	drm_gem_free_mmap_offset(obj);
 27	drm_gem_object_release(obj);
 28
 29	/* This must occur last as it frees up the memory of the GEM object */
 30	psb_gtt_free_range(obj->dev, gtt);
 31}
 32
 33int psb_gem_get_aperture(struct drm_device *dev, void *data,
 34				struct drm_file *file)
 35{
 36	return -EINVAL;
 37}
 38
 39/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 40 *	psb_gem_create		-	create a mappable object
 41 *	@file: the DRM file of the client
 42 *	@dev: our device
 43 *	@size: the size requested
 44 *	@handlep: returned handle (opaque number)
 45 *
 46 *	Create a GEM object, fill in the boilerplate and attach a handle to
 47 *	it so that userspace can speak about it. This does the core work
 48 *	for the various methods that do/will create GEM objects for things
 49 */
 50int psb_gem_create(struct drm_file *file, struct drm_device *dev, u64 size,
 51		   u32 *handlep, int stolen, u32 align)
 52{
 53	struct gtt_range *r;
 54	int ret;
 55	u32 handle;
 56
 57	size = roundup(size, PAGE_SIZE);
 58
 59	/* Allocate our object - for now a direct gtt range which is not
 60	   stolen memory backed */
 61	r = psb_gtt_alloc_range(dev, size, "gem", 0, PAGE_SIZE);
 62	if (r == NULL) {
 63		dev_err(dev->dev, "no memory for %lld byte GEM object\n", size);
 64		return -ENOSPC;
 65	}
 66	/* Initialize the extra goodies GEM needs to do all the hard work */
 67	if (drm_gem_object_init(dev, &r->gem, size) != 0) {
 68		psb_gtt_free_range(dev, r);
 69		/* GEM doesn't give an error code so use -ENOMEM */
 70		dev_err(dev->dev, "GEM init failed for %lld\n", size);
 71		return -ENOMEM;
 72	}
 73	/* Limit the object to 32bit mappings */
 74	mapping_set_gfp_mask(r->gem.filp->f_mapping, GFP_KERNEL | __GFP_DMA32);
 75	/* Give the object a handle so we can carry it more easily */
 76	ret = drm_gem_handle_create(file, &r->gem, &handle);
 77	if (ret) {
 78		dev_err(dev->dev, "GEM handle failed for %p, %lld\n",
 79							&r->gem, size);
 80		drm_gem_object_release(&r->gem);
 81		psb_gtt_free_range(dev, r);
 82		return ret;
 83	}
 84	/* We have the initial and handle reference but need only one now */
 85	drm_gem_object_put_unlocked(&r->gem);
 86	*handlep = handle;
 87	return 0;
 88}
 89
 90/**
 91 *	psb_gem_dumb_create	-	create a dumb buffer
 92 *	@drm_file: our client file
 93 *	@dev: our device
 94 *	@args: the requested arguments copied from userspace
 95 *
 96 *	Allocate a buffer suitable for use for a frame buffer of the
 97 *	form described by user space. Give userspace a handle by which
 98 *	to reference it.
 99 */
100int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
101			struct drm_mode_create_dumb *args)
102{
103	args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
104	args->size = args->pitch * args->height;
105	return psb_gem_create(file, dev, args->size, &args->handle, 0,
106			      PAGE_SIZE);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107}
108
109/**
110 *	psb_gem_fault		-	pagefault handler for GEM objects
111 *	@vma: the VMA of the GEM object
112 *	@vmf: fault detail
113 *
114 *	Invoked when a fault occurs on an mmap of a GEM managed area. GEM
115 *	does most of the work for us including the actual map/unmap calls
116 *	but we need to do the actual page work.
117 *
118 *	This code eventually needs to handle faulting objects in and out
119 *	of the GTT and repacking it when we run out of space. We can put
120 *	that off for now and for our simple uses
121 *
122 *	The VMA was set up by GEM. In doing so it also ensured that the
123 *	vma->vm_private_data points to the GEM object that is backing this
124 *	mapping.
125 */
126vm_fault_t psb_gem_fault(struct vm_fault *vmf)
127{
128	struct vm_area_struct *vma = vmf->vma;
129	struct drm_gem_object *obj;
130	struct gtt_range *r;
131	int err;
132	vm_fault_t ret;
133	unsigned long pfn;
134	pgoff_t page_offset;
135	struct drm_device *dev;
136	struct drm_psb_private *dev_priv;
137
138	obj = vma->vm_private_data;	/* GEM object */
139	dev = obj->dev;
140	dev_priv = dev->dev_private;
141
142	r = container_of(obj, struct gtt_range, gem);	/* Get the gtt range */
143
144	/* Make sure we don't parallel update on a fault, nor move or remove
145	   something from beneath our feet */
146	mutex_lock(&dev_priv->mmap_mutex);
147
148	/* For now the mmap pins the object and it stays pinned. As things
149	   stand that will do us no harm */
150	if (r->mmapping == 0) {
151		err = psb_gtt_pin(r);
152		if (err < 0) {
153			dev_err(dev->dev, "gma500: pin failed: %d\n", err);
154			ret = vmf_error(err);
155			goto fail;
156		}
157		r->mmapping = 1;
158	}
159
160	/* Page relative to the VMA start - we must calculate this ourselves
161	   because vmf->pgoff is the fake GEM offset */
162	page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
 
163
164	/* CPU view of the page, don't go via the GART for CPU writes */
165	if (r->stolen)
166		pfn = (dev_priv->stolen_base + r->offset) >> PAGE_SHIFT;
167	else
168		pfn = page_to_pfn(r->pages[page_offset]);
169	ret = vmf_insert_pfn(vma, vmf->address, pfn);
 
170fail:
171	mutex_unlock(&dev_priv->mmap_mutex);
 
 
 
 
 
 
 
 
 
 
 
172
173	return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174}
v3.5.6
 
  1/*
  2 *  psb GEM interface
  3 *
  4 * Copyright (c) 2011, Intel Corporation.
  5 *
  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.
  9 *
 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.
 14 *
 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.
 18 *
 19 * Authors: Alan Cox
 20 *
 21 * TODO:
 22 *	-	we need to work out if the MMU is relevant (eg for
 23 *		accelerated operations on a GEM object)
 24 */
 25
 26#include <drm/drmP.h>
 
 27#include <drm/drm.h>
 28#include "gma_drm.h"
 
 29#include "psb_drv.h"
 30
 31int psb_gem_init_object(struct drm_gem_object *obj)
 32{
 33	return -EINVAL;
 34}
 35
 36void psb_gem_free_object(struct drm_gem_object *obj)
 37{
 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);
 42}
 43
 44int psb_gem_get_aperture(struct drm_device *dev, void *data,
 45				struct drm_file *file)
 46{
 47	return -EINVAL;
 48}
 49
 50/**
 51 *	psb_gem_dumb_map_gtt	-	buffer mapping for dumb interface
 52 *	@file: our drm client file
 53 *	@dev: drm device
 54 *	@handle: GEM handle to the object (from dumb_create)
 55 *
 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.
 58 */
 59int psb_gem_dumb_map_gtt(struct drm_file *file, struct drm_device *dev,
 60			 uint32_t handle, uint64_t *offset)
 61{
 62	int ret = 0;
 63	struct drm_gem_object *obj;
 64
 65	if (!(dev->driver->driver_features & DRIVER_GEM))
 66		return -ENODEV;
 67
 68	mutex_lock(&dev->struct_mutex);
 69
 70	/* GEM does all our handle to object mapping */
 71	obj = drm_gem_object_lookup(dev, file, handle);
 72	if (obj == NULL) {
 73		ret = -ENOENT;
 74		goto unlock;
 75	}
 76	/* What validation is needed here ? */
 77
 78	/* Make it mmapable */
 79	if (!obj->map_list.map) {
 80		ret = gem_create_mmap_offset(obj);
 81		if (ret)
 82			goto out;
 83	}
 84	/* GEM should really work out the hash offsets for us */
 85	*offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
 86out:
 87	drm_gem_object_unreference(obj);
 88unlock:
 89	mutex_unlock(&dev->struct_mutex);
 90	return ret;
 91}
 92
 93/**
 94 *	psb_gem_create		-	create a mappable object
 95 *	@file: the DRM file of the client
 96 *	@dev: our device
 97 *	@size: the size requested
 98 *	@handlep: returned handle (opaque number)
 99 *
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
103 */
104static int psb_gem_create(struct drm_file *file,
105	struct drm_device *dev, uint64_t size, uint32_t *handlep)
106{
107	struct gtt_range *r;
108	int ret;
109	u32 handle;
110
111	size = roundup(size, PAGE_SIZE);
112
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);
116	if (r == NULL) {
117		dev_err(dev->dev, "no memory for %lld byte GEM object\n", size);
118		return -ENOSPC;
119	}
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 so use -ENOMEM */
124		dev_err(dev->dev, "GEM init failed for %lld\n", size);
125		return -ENOMEM;
126	}
127	/* Limit the object to 32bit mappings */
128	mapping_set_gfp_mask(r->gem.filp->f_mapping, GFP_KERNEL | __GFP_DMA32);
129	/* Give the object a handle so we can carry it more easily */
130	ret = drm_gem_handle_create(file, &r->gem, &handle);
131	if (ret) {
132		dev_err(dev->dev, "GEM handle failed for %p, %lld\n",
133							&r->gem, size);
134		drm_gem_object_release(&r->gem);
135		psb_gtt_free_range(dev, r);
136		return ret;
137	}
138	/* We have the initial and handle reference but need only one now */
139	drm_gem_object_unreference(&r->gem);
140	*handlep = handle;
141	return 0;
142}
143
144/**
145 *	psb_gem_dumb_create	-	create a dumb buffer
146 *	@drm_file: our client file
147 *	@dev: our device
148 *	@args: the requested arguments copied from userspace
149 *
150 *	Allocate a buffer suitable for use for a frame buffer of the
151 *	form described by user space. Give userspace a handle by which
152 *	to reference it.
153 */
154int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
155			struct drm_mode_create_dumb *args)
156{
157	args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
158	args->size = args->pitch * args->height;
159	return psb_gem_create(file, dev, args->size, &args->handle);
160}
161
162/**
163 *	psb_gem_dumb_destroy	-	destroy a dumb buffer
164 *	@file: client file
165 *	@dev: our DRM device
166 *	@handle: the object handle
167 *
168 *	Destroy a handle that was created via psb_gem_dumb_create, at least
169 *	we hope it was created that way. i915 seems to assume the caller
170 *	does the checking but that might be worth review ! FIXME
171 */
172int psb_gem_dumb_destroy(struct drm_file *file, struct drm_device *dev,
173			uint32_t handle)
174{
175	/* No special work needed, drop the reference and see what falls out */
176	return drm_gem_handle_delete(file, handle);
177}
178
179/**
180 *	psb_gem_fault		-	pagefault handler for GEM objects
181 *	@vma: the VMA of the GEM object
182 *	@vmf: fault detail
183 *
184 *	Invoked when a fault occurs on an mmap of a GEM managed area. GEM
185 *	does most of the work for us including the actual map/unmap calls
186 *	but we need to do the actual page work.
187 *
188 *	This code eventually needs to handle faulting objects in and out
189 *	of the GTT and repacking it when we run out of space. We can put
190 *	that off for now and for our simple uses
191 *
192 *	The VMA was set up by GEM. In doing so it also ensured that the
193 *	vma->vm_private_data points to the GEM object that is backing this
194 *	mapping.
195 */
196int psb_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
197{
 
198	struct drm_gem_object *obj;
199	struct gtt_range *r;
200	int ret;
 
201	unsigned long pfn;
202	pgoff_t page_offset;
203	struct drm_device *dev;
204	struct drm_psb_private *dev_priv;
205
206	obj = vma->vm_private_data;	/* GEM object */
207	dev = obj->dev;
208	dev_priv = dev->dev_private;
209
210	r = container_of(obj, struct gtt_range, gem);	/* Get the gtt range */
211
212	/* Make sure we don't parallel update on a fault, nor move or remove
213	   something from beneath our feet */
214	mutex_lock(&dev->struct_mutex);
215
216	/* For now the mmap pins the object and it stays pinned. As things
217	   stand that will do us no harm */
218	if (r->mmapping == 0) {
219		ret = psb_gtt_pin(r);
220		if (ret < 0) {
221			dev_err(dev->dev, "gma500: pin failed: %d\n", ret);
 
222			goto fail;
223		}
224		r->mmapping = 1;
225	}
226
227	/* Page relative to the VMA start - we must calculate this ourselves
228	   because vmf->pgoff is the fake GEM offset */
229	page_offset = ((unsigned long) vmf->virtual_address - vma->vm_start)
230				>> PAGE_SHIFT;
231
232	/* CPU view of the page, don't go via the GART for CPU writes */
233	if (r->stolen)
234		pfn = (dev_priv->stolen_base + r->offset) >> PAGE_SHIFT;
235	else
236		pfn = page_to_pfn(r->pages[page_offset]);
237	ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
238
239fail:
240	mutex_unlock(&dev->struct_mutex);
241	switch (ret) {
242	case 0:
243	case -ERESTARTSYS:
244	case -EINTR:
245		return VM_FAULT_NOPAGE;
246	case -ENOMEM:
247		return VM_FAULT_OOM;
248	default:
249		return VM_FAULT_SIGBUS;
250	}
251}
252
253static int psb_gem_create_stolen(struct drm_file *file, struct drm_device *dev,
254						int size, u32 *handle)
255{
256	struct gtt_range *gtt = psb_gtt_alloc_range(dev, size, "gem", 1);
257	if (gtt == NULL)
258		return -ENOMEM;
259	if (drm_gem_private_object_init(dev, &gtt->gem, size) != 0)
260		goto free_gtt;
261	if (drm_gem_handle_create(file, &gtt->gem, handle) == 0)
262		return 0;
263free_gtt:
264	psb_gtt_free_range(dev, gtt);
265	return -ENOMEM;
266}
267
268/*
269 *	GEM interfaces for our specific client
270 */
271int psb_gem_create_ioctl(struct drm_device *dev, void *data,
272					struct drm_file *file)
273{
274	struct drm_psb_gem_create *args = data;
275	int ret;
276	if (args->flags & GMA_GEM_CREATE_STOLEN) {
277		ret = psb_gem_create_stolen(file, dev, args->size,
278							&args->handle);
279		if (ret == 0)
280			return 0;
281		/* Fall throguh */
282		args->flags &= ~GMA_GEM_CREATE_STOLEN;
283	}
284	return psb_gem_create(file, dev, args->size, &args->handle);
285}
286
287int psb_gem_mmap_ioctl(struct drm_device *dev, void *data,
288					struct drm_file *file)
289{
290	struct drm_psb_gem_mmap *args = data;
291	return dev->driver->dumb_map_offset(file, dev,
292						args->handle, &args->offset);
293}
294