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
66 obj = drm_gem_object_lookup(file, handle);
67 if (obj == NULL)
68 return -ENOENT;
69
70
71 ret = drm_gem_create_mmap_offset(obj);
72 if (ret)
73 goto out;
74 *offset = drm_vma_node_offset_addr(&obj->vma_node);
75out:
76 drm_gem_object_unreference_unlocked(obj);
77 return ret;
78}
79
80
81
82
83
84
85
86
87
88
89
90
91int psb_gem_create(struct drm_file *file, struct drm_device *dev, u64 size,
92 u32 *handlep, int stolen, u32 align)
93{
94 struct gtt_range *r;
95 int ret;
96 u32 handle;
97
98 size = roundup(size, PAGE_SIZE);
99
100
101
102 r = psb_gtt_alloc_range(dev, size, "gem", 0, PAGE_SIZE);
103 if (r == NULL) {
104 dev_err(dev->dev, "no memory for %lld byte GEM object\n", size);
105 return -ENOSPC;
106 }
107
108 if (drm_gem_object_init(dev, &r->gem, size) != 0) {
109 psb_gtt_free_range(dev, r);
110
111 dev_err(dev->dev, "GEM init failed for %lld\n", size);
112 return -ENOMEM;
113 }
114
115 mapping_set_gfp_mask(r->gem.filp->f_mapping, GFP_KERNEL | __GFP_DMA32);
116
117 ret = drm_gem_handle_create(file, &r->gem, &handle);
118 if (ret) {
119 dev_err(dev->dev, "GEM handle failed for %p, %lld\n",
120 &r->gem, size);
121 drm_gem_object_release(&r->gem);
122 psb_gtt_free_range(dev, r);
123 return ret;
124 }
125
126 drm_gem_object_unreference_unlocked(&r->gem);
127 *handlep = handle;
128 return 0;
129}
130
131
132
133
134
135
136
137
138
139
140
141int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
142 struct drm_mode_create_dumb *args)
143{
144 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
145 args->size = args->pitch * args->height;
146 return psb_gem_create(file, dev, args->size, &args->handle, 0,
147 PAGE_SIZE);
148}
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167int psb_gem_fault(struct vm_fault *vmf)
168{
169 struct vm_area_struct *vma = vmf->vma;
170 struct drm_gem_object *obj;
171 struct gtt_range *r;
172 int ret;
173 unsigned long pfn;
174 pgoff_t page_offset;
175 struct drm_device *dev;
176 struct drm_psb_private *dev_priv;
177
178 obj = vma->vm_private_data;
179 dev = obj->dev;
180 dev_priv = dev->dev_private;
181
182 r = container_of(obj, struct gtt_range, gem);
183
184
185
186 mutex_lock(&dev_priv->mmap_mutex);
187
188
189
190 if (r->mmapping == 0) {
191 ret = psb_gtt_pin(r);
192 if (ret < 0) {
193 dev_err(dev->dev, "gma500: pin failed: %d\n", ret);
194 goto fail;
195 }
196 r->mmapping = 1;
197 }
198
199
200
201 page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
202
203
204 if (r->stolen)
205 pfn = (dev_priv->stolen_base + r->offset) >> PAGE_SHIFT;
206 else
207 pfn = page_to_pfn(r->pages[page_offset]);
208 ret = vm_insert_pfn(vma, vmf->address, pfn);
209
210fail:
211 mutex_unlock(&dev_priv->mmap_mutex);
212 switch (ret) {
213 case 0:
214 case -ERESTARTSYS:
215 case -EINTR:
216 return VM_FAULT_NOPAGE;
217 case -ENOMEM:
218 return VM_FAULT_OOM;
219 default:
220 return VM_FAULT_SIGBUS;
221 }
222}
223