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