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
31
32
33
34#include <drm/drmP.h>
35#include <drm/sis_drm.h>
36#include "sis_drv.h"
37
38#include <video/sisfb.h>
39
40#define VIDEO_TYPE 0
41#define AGP_TYPE 1
42
43
44struct sis_memblock {
45 struct drm_mm_node mm_node;
46 struct sis_memreq req;
47 struct list_head owner_list;
48};
49
50#if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
51
52
53#define SIS_MM_ALIGN_SHIFT 0
54#define SIS_MM_ALIGN_MASK 0
55
56#else
57
58#define SIS_MM_ALIGN_SHIFT 4
59#define SIS_MM_ALIGN_MASK ((1 << SIS_MM_ALIGN_SHIFT) - 1)
60
61#endif
62
63static int sis_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
64{
65 drm_sis_private_t *dev_priv = dev->dev_private;
66 drm_sis_fb_t *fb = data;
67
68 mutex_lock(&dev->struct_mutex);
69
70
71 drm_mm_init(&dev_priv->vram_mm, 0, fb->size >> SIS_MM_ALIGN_SHIFT);
72
73 dev_priv->vram_initialized = 1;
74 dev_priv->vram_offset = fb->offset;
75
76 mutex_unlock(&dev->struct_mutex);
77 DRM_DEBUG("offset = %lu, size = %lu\n", fb->offset, fb->size);
78
79 return 0;
80}
81
82static int sis_drm_alloc(struct drm_device *dev, struct drm_file *file,
83 void *data, int pool)
84{
85 drm_sis_private_t *dev_priv = dev->dev_private;
86 drm_sis_mem_t *mem = data;
87 int retval = 0, user_key;
88 struct sis_memblock *item;
89 struct sis_file_private *file_priv = file->driver_priv;
90 unsigned long offset;
91
92 mutex_lock(&dev->struct_mutex);
93
94 if (0 == ((pool == 0) ? dev_priv->vram_initialized :
95 dev_priv->agp_initialized)) {
96 DRM_ERROR
97 ("Attempt to allocate from uninitialized memory manager.\n");
98 mutex_unlock(&dev->struct_mutex);
99 return -EINVAL;
100 }
101
102 item = kzalloc(sizeof(*item), GFP_KERNEL);
103 if (!item) {
104 retval = -ENOMEM;
105 goto fail_alloc;
106 }
107
108 mem->size = (mem->size + SIS_MM_ALIGN_MASK) >> SIS_MM_ALIGN_SHIFT;
109 if (pool == AGP_TYPE) {
110 retval = drm_mm_insert_node(&dev_priv->agp_mm,
111 &item->mm_node,
112 mem->size);
113 offset = item->mm_node.start;
114 } else {
115#if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
116 item->req.size = mem->size;
117 sis_malloc(&item->req);
118 if (item->req.size == 0)
119 retval = -ENOMEM;
120 offset = item->req.offset;
121#else
122 retval = drm_mm_insert_node(&dev_priv->vram_mm,
123 &item->mm_node,
124 mem->size);
125 offset = item->mm_node.start;
126#endif
127 }
128 if (retval)
129 goto fail_alloc;
130
131 retval = idr_alloc(&dev_priv->object_idr, item, 1, 0, GFP_KERNEL);
132 if (retval < 0)
133 goto fail_idr;
134 user_key = retval;
135
136 list_add(&item->owner_list, &file_priv->obj_list);
137 mutex_unlock(&dev->struct_mutex);
138
139 mem->offset = ((pool == 0) ?
140 dev_priv->vram_offset : dev_priv->agp_offset) +
141 (offset << SIS_MM_ALIGN_SHIFT);
142 mem->free = user_key;
143 mem->size = mem->size << SIS_MM_ALIGN_SHIFT;
144
145 return 0;
146
147fail_idr:
148 drm_mm_remove_node(&item->mm_node);
149fail_alloc:
150 kfree(item);
151 mutex_unlock(&dev->struct_mutex);
152
153 mem->offset = 0;
154 mem->size = 0;
155 mem->free = 0;
156
157 DRM_DEBUG("alloc %d, size = %ld, offset = %ld\n", pool, mem->size,
158 mem->offset);
159
160 return retval;
161}
162
163static int sis_drm_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
164{
165 drm_sis_private_t *dev_priv = dev->dev_private;
166 drm_sis_mem_t *mem = data;
167 struct sis_memblock *obj;
168
169 mutex_lock(&dev->struct_mutex);
170 obj = idr_find(&dev_priv->object_idr, mem->free);
171 if (obj == NULL) {
172 mutex_unlock(&dev->struct_mutex);
173 return -EINVAL;
174 }
175
176 idr_remove(&dev_priv->object_idr, mem->free);
177 list_del(&obj->owner_list);
178 if (drm_mm_node_allocated(&obj->mm_node))
179 drm_mm_remove_node(&obj->mm_node);
180#if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
181 else
182 sis_free(obj->req.offset);
183#endif
184 kfree(obj);
185 mutex_unlock(&dev->struct_mutex);
186 DRM_DEBUG("free = 0x%lx\n", mem->free);
187
188 return 0;
189}
190
191static int sis_fb_alloc(struct drm_device *dev, void *data,
192 struct drm_file *file_priv)
193{
194 return sis_drm_alloc(dev, file_priv, data, VIDEO_TYPE);
195}
196
197static int sis_ioctl_agp_init(struct drm_device *dev, void *data,
198 struct drm_file *file_priv)
199{
200 drm_sis_private_t *dev_priv = dev->dev_private;
201 drm_sis_agp_t *agp = data;
202 dev_priv = dev->dev_private;
203
204 mutex_lock(&dev->struct_mutex);
205 drm_mm_init(&dev_priv->agp_mm, 0, agp->size >> SIS_MM_ALIGN_SHIFT);
206
207 dev_priv->agp_initialized = 1;
208 dev_priv->agp_offset = agp->offset;
209 mutex_unlock(&dev->struct_mutex);
210
211 DRM_DEBUG("offset = %lu, size = %lu\n", agp->offset, agp->size);
212 return 0;
213}
214
215static int sis_ioctl_agp_alloc(struct drm_device *dev, void *data,
216 struct drm_file *file_priv)
217{
218
219 return sis_drm_alloc(dev, file_priv, data, AGP_TYPE);
220}
221
222static drm_local_map_t *sis_reg_init(struct drm_device *dev)
223{
224 struct drm_map_list *entry;
225 drm_local_map_t *map;
226
227 list_for_each_entry(entry, &dev->maplist, head) {
228 map = entry->map;
229 if (!map)
230 continue;
231 if (map->type == _DRM_REGISTERS)
232 return map;
233 }
234 return NULL;
235}
236
237int sis_idle(struct drm_device *dev)
238{
239 drm_sis_private_t *dev_priv = dev->dev_private;
240 uint32_t idle_reg;
241 unsigned long end;
242 int i;
243
244 if (dev_priv->idle_fault)
245 return 0;
246
247 if (dev_priv->mmio == NULL) {
248 dev_priv->mmio = sis_reg_init(dev);
249 if (dev_priv->mmio == NULL) {
250 DRM_ERROR("Could not find register map.\n");
251 return 0;
252 }
253 }
254
255
256
257
258
259 if (dev_priv->chipset != SIS_CHIP_315)
260 return 0;
261
262
263
264
265
266
267 end = jiffies + (HZ * 3);
268
269 for (i = 0; i < 4; ++i) {
270 do {
271 idle_reg = SIS_READ(0x85cc);
272 } while (!time_after_eq(jiffies, end) &&
273 ((idle_reg & 0x80000000) != 0x80000000));
274 }
275
276 if (time_after_eq(jiffies, end)) {
277 DRM_ERROR("Graphics engine idle timeout. "
278 "Disabling idle check\n");
279 dev_priv->idle_fault = 1;
280 }
281
282
283
284
285
286
287 return 0;
288}
289
290
291void sis_lastclose(struct drm_device *dev)
292{
293 drm_sis_private_t *dev_priv = dev->dev_private;
294
295 if (!dev_priv)
296 return;
297
298 mutex_lock(&dev->struct_mutex);
299 if (dev_priv->vram_initialized) {
300 drm_mm_takedown(&dev_priv->vram_mm);
301 dev_priv->vram_initialized = 0;
302 }
303 if (dev_priv->agp_initialized) {
304 drm_mm_takedown(&dev_priv->agp_mm);
305 dev_priv->agp_initialized = 0;
306 }
307 dev_priv->mmio = NULL;
308 mutex_unlock(&dev->struct_mutex);
309}
310
311void sis_reclaim_buffers_locked(struct drm_device *dev,
312 struct drm_file *file)
313{
314 struct sis_file_private *file_priv = file->driver_priv;
315 struct sis_memblock *entry, *next;
316
317 if (!(dev->master && file->master->lock.hw_lock))
318 return;
319
320 drm_legacy_idlelock_take(&file->master->lock);
321
322 mutex_lock(&dev->struct_mutex);
323 if (list_empty(&file_priv->obj_list)) {
324 mutex_unlock(&dev->struct_mutex);
325 drm_legacy_idlelock_release(&file->master->lock);
326
327 return;
328 }
329
330 sis_idle(dev);
331
332
333 list_for_each_entry_safe(entry, next, &file_priv->obj_list,
334 owner_list) {
335 list_del(&entry->owner_list);
336 if (drm_mm_node_allocated(&entry->mm_node))
337 drm_mm_remove_node(&entry->mm_node);
338#if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
339 else
340 sis_free(entry->req.offset);
341#endif
342 kfree(entry);
343 }
344 mutex_unlock(&dev->struct_mutex);
345
346 drm_legacy_idlelock_release(&file->master->lock);
347
348 return;
349}
350
351const struct drm_ioctl_desc sis_ioctls[] = {
352 DRM_IOCTL_DEF_DRV(SIS_FB_ALLOC, sis_fb_alloc, DRM_AUTH),
353 DRM_IOCTL_DEF_DRV(SIS_FB_FREE, sis_drm_free, DRM_AUTH),
354 DRM_IOCTL_DEF_DRV(SIS_AGP_INIT, sis_ioctl_agp_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
355 DRM_IOCTL_DEF_DRV(SIS_AGP_ALLOC, sis_ioctl_agp_alloc, DRM_AUTH),
356 DRM_IOCTL_DEF_DRV(SIS_AGP_FREE, sis_drm_free, DRM_AUTH),
357 DRM_IOCTL_DEF_DRV(SIS_FB_INIT, sis_fb_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
358};
359
360int sis_max_ioctl = ARRAY_SIZE(sis_ioctls);
361