xref: /linux/drivers/gpu/drm/gma500/gem.c (revision 44f57d78)
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 <drm/drmP.h>
15 #include <drm/drm.h>
16 #include <drm/gma_drm.h>
17 #include <drm/drm_vma_manager.h>
18 #include "psb_drv.h"
19 
20 void psb_gem_free_object(struct drm_gem_object *obj)
21 {
22 	struct gtt_range *gtt = container_of(obj, struct gtt_range, gem);
23 
24 	/* Remove the list map if one is present */
25 	drm_gem_free_mmap_offset(obj);
26 	drm_gem_object_release(obj);
27 
28 	/* This must occur last as it frees up the memory of the GEM object */
29 	psb_gtt_free_range(obj->dev, gtt);
30 }
31 
32 int psb_gem_get_aperture(struct drm_device *dev, void *data,
33 				struct drm_file *file)
34 {
35 	return -EINVAL;
36 }
37 
38 /**
39  *	psb_gem_create		-	create a mappable object
40  *	@file: the DRM file of the client
41  *	@dev: our device
42  *	@size: the size requested
43  *	@handlep: returned handle (opaque number)
44  *
45  *	Create a GEM object, fill in the boilerplate and attach a handle to
46  *	it so that userspace can speak about it. This does the core work
47  *	for the various methods that do/will create GEM objects for things
48  */
49 int psb_gem_create(struct drm_file *file, struct drm_device *dev, u64 size,
50 		   u32 *handlep, int stolen, u32 align)
51 {
52 	struct gtt_range *r;
53 	int ret;
54 	u32 handle;
55 
56 	size = roundup(size, PAGE_SIZE);
57 
58 	/* Allocate our object - for now a direct gtt range which is not
59 	   stolen memory backed */
60 	r = psb_gtt_alloc_range(dev, size, "gem", 0, PAGE_SIZE);
61 	if (r == NULL) {
62 		dev_err(dev->dev, "no memory for %lld byte GEM object\n", size);
63 		return -ENOSPC;
64 	}
65 	/* Initialize the extra goodies GEM needs to do all the hard work */
66 	if (drm_gem_object_init(dev, &r->gem, size) != 0) {
67 		psb_gtt_free_range(dev, r);
68 		/* GEM doesn't give an error code so use -ENOMEM */
69 		dev_err(dev->dev, "GEM init failed for %lld\n", size);
70 		return -ENOMEM;
71 	}
72 	/* Limit the object to 32bit mappings */
73 	mapping_set_gfp_mask(r->gem.filp->f_mapping, GFP_KERNEL | __GFP_DMA32);
74 	/* Give the object a handle so we can carry it more easily */
75 	ret = drm_gem_handle_create(file, &r->gem, &handle);
76 	if (ret) {
77 		dev_err(dev->dev, "GEM handle failed for %p, %lld\n",
78 							&r->gem, size);
79 		drm_gem_object_release(&r->gem);
80 		psb_gtt_free_range(dev, r);
81 		return ret;
82 	}
83 	/* We have the initial and handle reference but need only one now */
84 	drm_gem_object_put_unlocked(&r->gem);
85 	*handlep = handle;
86 	return 0;
87 }
88 
89 /**
90  *	psb_gem_dumb_create	-	create a dumb buffer
91  *	@drm_file: our client file
92  *	@dev: our device
93  *	@args: the requested arguments copied from userspace
94  *
95  *	Allocate a buffer suitable for use for a frame buffer of the
96  *	form described by user space. Give userspace a handle by which
97  *	to reference it.
98  */
99 int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
100 			struct drm_mode_create_dumb *args)
101 {
102 	args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
103 	args->size = args->pitch * args->height;
104 	return psb_gem_create(file, dev, args->size, &args->handle, 0,
105 			      PAGE_SIZE);
106 }
107 
108 /**
109  *	psb_gem_fault		-	pagefault handler for GEM objects
110  *	@vma: the VMA of the GEM object
111  *	@vmf: fault detail
112  *
113  *	Invoked when a fault occurs on an mmap of a GEM managed area. GEM
114  *	does most of the work for us including the actual map/unmap calls
115  *	but we need to do the actual page work.
116  *
117  *	This code eventually needs to handle faulting objects in and out
118  *	of the GTT and repacking it when we run out of space. We can put
119  *	that off for now and for our simple uses
120  *
121  *	The VMA was set up by GEM. In doing so it also ensured that the
122  *	vma->vm_private_data points to the GEM object that is backing this
123  *	mapping.
124  */
125 vm_fault_t psb_gem_fault(struct vm_fault *vmf)
126 {
127 	struct vm_area_struct *vma = vmf->vma;
128 	struct drm_gem_object *obj;
129 	struct gtt_range *r;
130 	int err;
131 	vm_fault_t ret;
132 	unsigned long pfn;
133 	pgoff_t page_offset;
134 	struct drm_device *dev;
135 	struct drm_psb_private *dev_priv;
136 
137 	obj = vma->vm_private_data;	/* GEM object */
138 	dev = obj->dev;
139 	dev_priv = dev->dev_private;
140 
141 	r = container_of(obj, struct gtt_range, gem);	/* Get the gtt range */
142 
143 	/* Make sure we don't parallel update on a fault, nor move or remove
144 	   something from beneath our feet */
145 	mutex_lock(&dev_priv->mmap_mutex);
146 
147 	/* For now the mmap pins the object and it stays pinned. As things
148 	   stand that will do us no harm */
149 	if (r->mmapping == 0) {
150 		err = psb_gtt_pin(r);
151 		if (err < 0) {
152 			dev_err(dev->dev, "gma500: pin failed: %d\n", err);
153 			ret = vmf_error(err);
154 			goto fail;
155 		}
156 		r->mmapping = 1;
157 	}
158 
159 	/* Page relative to the VMA start - we must calculate this ourselves
160 	   because vmf->pgoff is the fake GEM offset */
161 	page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
162 
163 	/* CPU view of the page, don't go via the GART for CPU writes */
164 	if (r->stolen)
165 		pfn = (dev_priv->stolen_base + r->offset) >> PAGE_SHIFT;
166 	else
167 		pfn = page_to_pfn(r->pages[page_offset]);
168 	ret = vmf_insert_pfn(vma, vmf->address, pfn);
169 fail:
170 	mutex_unlock(&dev_priv->mmap_mutex);
171 
172 	return ret;
173 }
174