1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <drm/drmP.h>
27#include <drm/drm.h>
28#include <drm/gma_drm.h>
29#include <drm/drm_vma_manager.h>
30#include "psb_drv.h"
31
32void psb_gem_free_object(struct drm_gem_object *obj)
33{
34 struct gtt_range *gtt = container_of(obj, struct gtt_range, gem);
35
36
37 drm_gem_free_mmap_offset(obj);
38 drm_gem_object_release(obj);
39
40
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
52
53
54
55
56
57
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 mutex_lock(&dev->struct_mutex);
66
67
68 obj = drm_gem_object_lookup(dev, file, handle);
69 if (obj == NULL) {
70 ret = -ENOENT;
71 goto unlock;
72 }
73
74
75
76 ret = drm_gem_create_mmap_offset(obj);
77 if (ret)
78 goto out;
79 *offset = drm_vma_node_offset_addr(&obj->vma_node);
80out:
81 drm_gem_object_unreference(obj);
82unlock:
83 mutex_unlock(&dev->struct_mutex);
84 return ret;
85}
86
87
88
89
90
91
92
93
94
95
96
97
98int psb_gem_create(struct drm_file *file, struct drm_device *dev, u64 size,
99 u32 *handlep, int stolen, u32 align)
100{
101 struct gtt_range *r;
102 int ret;
103 u32 handle;
104
105 size = roundup(size, PAGE_SIZE);
106
107
108
109 r = psb_gtt_alloc_range(dev, size, "gem", 0, PAGE_SIZE);
110 if (r == NULL) {
111 dev_err(dev->dev, "no memory for %lld byte GEM object\n", size);
112 return -ENOSPC;
113 }
114
115 if (drm_gem_object_init(dev, &r->gem, size) != 0) {
116 psb_gtt_free_range(dev, r);
117
118 dev_err(dev->dev, "GEM init failed for %lld\n", size);
119 return -ENOMEM;
120 }
121
122 mapping_set_gfp_mask(r->gem.filp->f_mapping, GFP_KERNEL | __GFP_DMA32);
123
124 ret = drm_gem_handle_create(file, &r->gem, &handle);
125 if (ret) {
126 dev_err(dev->dev, "GEM handle failed for %p, %lld\n",
127 &r->gem, size);
128 drm_gem_object_release(&r->gem);
129 psb_gtt_free_range(dev, r);
130 return ret;
131 }
132
133 drm_gem_object_unreference(&r->gem);
134 *handlep = handle;
135 return 0;
136}
137
138
139
140
141
142
143
144
145
146
147
148int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
149 struct drm_mode_create_dumb *args)
150{
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,
154 PAGE_SIZE);
155}
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174int psb_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
175{
176 struct drm_gem_object *obj;
177 struct gtt_range *r;
178 int ret;
179 unsigned long pfn;
180 pgoff_t page_offset;
181 struct drm_device *dev;
182 struct drm_psb_private *dev_priv;
183
184 obj = vma->vm_private_data;
185 dev = obj->dev;
186 dev_priv = dev->dev_private;
187
188 r = container_of(obj, struct gtt_range, gem);
189
190
191
192 mutex_lock(&dev->struct_mutex);
193
194
195
196 if (r->mmapping == 0) {
197 ret = psb_gtt_pin(r);
198 if (ret < 0) {
199 dev_err(dev->dev, "gma500: pin failed: %d\n", ret);
200 goto fail;
201 }
202 r->mmapping = 1;
203 }
204
205
206
207 page_offset = ((unsigned long) vmf->virtual_address - vma->vm_start)
208 >> PAGE_SHIFT;
209
210
211 if (r->stolen)
212 pfn = (dev_priv->stolen_base + r->offset) >> PAGE_SHIFT;
213 else
214 pfn = page_to_pfn(r->pages[page_offset]);
215 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
216
217fail:
218 mutex_unlock(&dev->struct_mutex);
219 switch (ret) {
220 case 0:
221 case -ERESTARTSYS:
222 case -EINTR:
223 return VM_FAULT_NOPAGE;
224 case -ENOMEM:
225 return VM_FAULT_OOM;
226 default:
227 return VM_FAULT_SIGBUS;
228 }
229}
230