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
27
28
29
30#include <drm/drmP.h>
31#include <drm/drm.h>
32#include "psb_drm.h"
33#include "psb_drv.h"
34
35int psb_gem_init_object(struct drm_gem_object *obj)
36{
37 return -EINVAL;
38}
39
40void psb_gem_free_object(struct drm_gem_object *obj)
41{
42 struct gtt_range *gtt = container_of(obj, struct gtt_range, gem);
43 psb_gtt_free_range(obj->dev, gtt);
44 if (obj->map_list.map) {
45
46 struct drm_gem_mm *mm = obj->dev->mm_private;
47 struct drm_map_list *list = &obj->map_list;
48 drm_ht_remove_item(&mm->offset_hash, &list->hash);
49 drm_mm_put_block(list->file_offset_node);
50 kfree(list->map);
51 list->map = NULL;
52 }
53 drm_gem_object_release(obj);
54}
55
56int psb_gem_get_aperture(struct drm_device *dev, void *data,
57 struct drm_file *file)
58{
59 return -EINVAL;
60}
61
62
63
64
65
66
67
68
69static int psb_gem_create_mmap_offset(struct drm_gem_object *obj)
70{
71 struct drm_device *dev = obj->dev;
72 struct drm_gem_mm *mm = dev->mm_private;
73 struct drm_map_list *list;
74 struct drm_local_map *map;
75 int ret;
76
77 list = &obj->map_list;
78 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
79 if (list->map == NULL)
80 return -ENOMEM;
81 map = list->map;
82 map->type = _DRM_GEM;
83 map->size = obj->size;
84 map->handle =obj;
85
86 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
87 obj->size / PAGE_SIZE, 0, 0);
88 if (!list->file_offset_node) {
89 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
90 ret = -ENOSPC;
91 goto free_it;
92 }
93 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
94 obj->size / PAGE_SIZE, 0);
95 if (!list->file_offset_node) {
96 ret = -ENOMEM;
97 goto free_it;
98 }
99 list->hash.key = list->file_offset_node->start;
100 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
101 if (ret) {
102 DRM_ERROR("failed to add to map hash\n");
103 goto free_mm;
104 }
105 return 0;
106
107free_mm:
108 drm_mm_put_block(list->file_offset_node);
109free_it:
110 kfree(list->map);
111 list->map = NULL;
112 return ret;
113}
114
115
116
117
118
119
120
121
122
123
124int psb_gem_dumb_map_gtt(struct drm_file *file, struct drm_device *dev,
125 uint32_t handle, uint64_t *offset)
126{
127 int ret = 0;
128 struct drm_gem_object *obj;
129
130 if (!(dev->driver->driver_features & DRIVER_GEM))
131 return -ENODEV;
132
133 mutex_lock(&dev->struct_mutex);
134
135
136 obj = drm_gem_object_lookup(dev, file, handle);
137 if (obj == NULL) {
138 ret = -ENOENT;
139 goto unlock;
140 }
141
142
143
144 if (!obj->map_list.map) {
145 ret = psb_gem_create_mmap_offset(obj);
146 if (ret)
147 goto out;
148 }
149
150 *offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
151out:
152 drm_gem_object_unreference(obj);
153unlock:
154 mutex_unlock(&dev->struct_mutex);
155 return ret;
156}
157
158
159
160
161
162
163
164
165
166
167
168
169static int psb_gem_create(struct drm_file *file,
170 struct drm_device *dev, uint64_t size, uint32_t *handlep)
171{
172 struct gtt_range *r;
173 int ret;
174 u32 handle;
175
176 size = roundup(size, PAGE_SIZE);
177
178
179
180 r = psb_gtt_alloc_range(dev, size, "gem", 0);
181 if (r == NULL)
182 return -ENOSPC;
183
184 if (drm_gem_object_init(dev, &r->gem, size) != 0) {
185 psb_gtt_free_range(dev, r);
186
187
188 return -ENOMEM;
189 }
190
191 ret = drm_gem_handle_create(file, &r->gem, &handle);
192 if (ret) {
193 drm_gem_object_release(&r->gem);
194 psb_gtt_free_range(dev, r);
195 return ret;
196 }
197
198 drm_gem_object_unreference(&r->gem);
199 *handlep = handle;
200 return 0;
201}
202
203
204
205
206
207
208
209
210
211
212
213int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
214 struct drm_mode_create_dumb *args)
215{
216 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
217 args->size = args->pitch * args->height;
218 return psb_gem_create(file, dev, args->size, &args->handle);
219}
220
221
222
223
224
225
226
227
228
229
230
231int psb_gem_dumb_destroy(struct drm_file *file, struct drm_device *dev,
232 uint32_t handle)
233{
234
235 return drm_gem_handle_delete(file, handle);
236}
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263int psb_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
264{
265 struct drm_gem_object *obj;
266 struct gtt_range *r;
267 int ret;
268 unsigned long pfn;
269 pgoff_t page_offset;
270 struct drm_device *dev;
271
272 obj = vma->vm_private_data;
273 dev = obj->dev;
274
275 r = container_of(obj, struct gtt_range, gem);
276
277
278
279 mutex_lock(&dev->struct_mutex);
280
281
282
283 if (r->mmapping == 0) {
284 ret = psb_gtt_pin(r);
285 if (ret < 0) {
286 DRM_ERROR("gma500: pin failed: %d\n", ret);
287 goto fail;
288 }
289 r->mmapping = 1;
290 }
291
292
293
294
295 page_offset = ((unsigned long) vmf->virtual_address - vma->vm_start)
296 >> PAGE_SHIFT;
297
298
299
300 pfn = (r->resource.start >> PAGE_SHIFT) + page_offset;
301
302 pr_debug("Object GTT base at %p\n", (void *)(r->resource.start));
303 pr_debug("Inserting %p pfn %lx, pa %lx\n", vmf->virtual_address,
304 pfn, pfn << PAGE_SHIFT);
305
306 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
307
308fail:
309 mutex_unlock(&dev->struct_mutex);
310 switch (ret) {
311 case 0:
312 case -ERESTARTSYS:
313 case -EINTR:
314 return VM_FAULT_NOPAGE;
315 case -ENOMEM:
316 return VM_FAULT_OOM;
317 default:
318 return VM_FAULT_SIGBUS;
319 }
320}
321