]>
Commit | Line | Data |
---|---|---|
ce65a44d | 1 | /************************************************************************** |
1da177e4 | 2 | * |
ce65a44d TH |
3 | * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA. |
4 | * All Rights Reserved. | |
1da177e4 LT |
5 | * |
6 | * Permission is hereby granted, free of charge, to any person obtaining a | |
ce65a44d TH |
7 | * copy of this software and associated documentation files (the |
8 | * "Software"), to deal in the Software without restriction, including | |
9 | * without limitation the rights to use, copy, modify, merge, publish, | |
10 | * distribute, sub license, and/or sell copies of the Software, and to | |
11 | * permit persons to whom the Software is furnished to do so, subject to | |
12 | * the following conditions: | |
b5e89ed5 | 13 | * |
ce65a44d TH |
14 | * The above copyright notice and this permission notice (including the |
15 | * next paragraph) shall be included in all copies or substantial portions | |
16 | * of the Software. | |
b5e89ed5 | 17 | * |
1da177e4 LT |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
ce65a44d TH |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
21 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, | |
22 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. | |
b5e89ed5 | 25 | * |
b5e89ed5 | 26 | * |
ce65a44d TH |
27 | **************************************************************************/ |
28 | ||
29 | /* | |
30 | * Authors: | |
96de0e25 | 31 | * Thomas Hellström <thomas-at-tungstengraphics-dot-com> |
1da177e4 LT |
32 | */ |
33 | ||
760285e7 DH |
34 | #include <drm/drmP.h> |
35 | #include <drm/sis_drm.h> | |
1da177e4 | 36 | #include "sis_drv.h" |
ce65a44d | 37 | |
1da177e4 | 38 | #include <video/sisfb.h> |
1da177e4 | 39 | |
b5e89ed5 | 40 | #define VIDEO_TYPE 0 |
1da177e4 LT |
41 | #define AGP_TYPE 1 |
42 | ||
1da177e4 | 43 | |
be2fb9da DV |
44 | struct sis_memblock { |
45 | struct drm_mm_node mm_node; | |
46 | struct sis_memreq req; | |
47 | struct list_head owner_list; | |
48 | }; | |
49 | ||
6bb9e4bf | 50 | #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE) |
ce65a44d | 51 | /* fb management via fb device */ |
b5e89ed5 | 52 | |
ce65a44d TH |
53 | #define SIS_MM_ALIGN_SHIFT 0 |
54 | #define SIS_MM_ALIGN_MASK 0 | |
1da177e4 | 55 | |
6bb9e4bf | 56 | #else /* CONFIG_FB_SIS[_MODULE] */ |
fb41e54b AB |
57 | |
58 | #define SIS_MM_ALIGN_SHIFT 4 | |
a7b98b67 | 59 | #define SIS_MM_ALIGN_MASK ((1 << SIS_MM_ALIGN_SHIFT) - 1) |
fb41e54b | 60 | |
6bb9e4bf | 61 | #endif /* CONFIG_FB_SIS[_MODULE] */ |
1da177e4 | 62 | |
c153f45f | 63 | static int sis_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv) |
1da177e4 | 64 | { |
1da177e4 | 65 | drm_sis_private_t *dev_priv = dev->dev_private; |
c153f45f | 66 | drm_sis_fb_t *fb = data; |
1da177e4 | 67 | |
ce65a44d | 68 | mutex_lock(&dev->struct_mutex); |
be2fb9da DV |
69 | /* Unconditionally init the drm_mm, even though we don't use it when the |
70 | * fb sis driver is available - make cleanup easier. */ | |
71 | drm_mm_init(&dev_priv->vram_mm, 0, fb->size >> SIS_MM_ALIGN_SHIFT); | |
1da177e4 | 72 | |
a1d0fcf5 | 73 | dev_priv->vram_initialized = 1; |
c153f45f | 74 | dev_priv->vram_offset = fb->offset; |
1da177e4 | 75 | |
ce65a44d | 76 | mutex_unlock(&dev->struct_mutex); |
83bc5fd2 | 77 | DRM_DEBUG("offset = %lu, size = %lu\n", fb->offset, fb->size); |
1da177e4 LT |
78 | |
79 | return 0; | |
80 | } | |
81 | ||
fdc0b8a6 | 82 | static int sis_drm_alloc(struct drm_device *dev, struct drm_file *file, |
c153f45f | 83 | void *data, int pool) |
1da177e4 | 84 | { |
1da177e4 | 85 | drm_sis_private_t *dev_priv = dev->dev_private; |
c153f45f | 86 | drm_sis_mem_t *mem = data; |
6de8a748 | 87 | int retval = 0, user_key; |
be2fb9da | 88 | struct sis_memblock *item; |
fdc0b8a6 | 89 | struct sis_file_private *file_priv = file->driver_priv; |
be2fb9da | 90 | unsigned long offset; |
1da177e4 | 91 | |
ce65a44d TH |
92 | mutex_lock(&dev->struct_mutex); |
93 | ||
a1d0fcf5 | 94 | if (0 == ((pool == 0) ? dev_priv->vram_initialized : |
ce65a44d TH |
95 | dev_priv->agp_initialized)) { |
96 | DRM_ERROR | |
97 | ("Attempt to allocate from uninitialized memory manager.\n"); | |
038477aa | 98 | mutex_unlock(&dev->struct_mutex); |
20caafa6 | 99 | return -EINVAL; |
ce65a44d | 100 | } |
b5e89ed5 | 101 | |
be2fb9da | 102 | item = kzalloc(sizeof(*item), GFP_KERNEL); |
6de8a748 DV |
103 | if (!item) { |
104 | retval = -ENOMEM; | |
105 | goto fail_alloc; | |
106 | } | |
ce65a44d | 107 | |
be2fb9da DV |
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, 0); | |
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, 0); | |
125 | offset = item->mm_node.start; | |
126 | #endif | |
127 | } | |
128 | if (retval) | |
129 | goto fail_alloc; | |
130 | ||
6de8a748 DV |
131 | again: |
132 | if (idr_pre_get(&dev_priv->object_idr, GFP_KERNEL) == 0) { | |
20caafa6 | 133 | retval = -ENOMEM; |
6de8a748 | 134 | goto fail_idr; |
1da177e4 | 135 | } |
6de8a748 DV |
136 | |
137 | retval = idr_get_new_above(&dev_priv->object_idr, item, 1, &user_key); | |
138 | if (retval == -EAGAIN) | |
139 | goto again; | |
140 | if (retval) | |
141 | goto fail_idr; | |
142 | ||
143 | list_add(&item->owner_list, &file_priv->obj_list); | |
144 | mutex_unlock(&dev->struct_mutex); | |
145 | ||
146 | mem->offset = ((pool == 0) ? | |
147 | dev_priv->vram_offset : dev_priv->agp_offset) + | |
be2fb9da | 148 | (offset << SIS_MM_ALIGN_SHIFT); |
6de8a748 DV |
149 | mem->free = user_key; |
150 | mem->size = mem->size << SIS_MM_ALIGN_SHIFT; | |
151 | ||
152 | return 0; | |
153 | ||
154 | fail_idr: | |
be2fb9da | 155 | drm_mm_remove_node(&item->mm_node); |
6de8a748 | 156 | fail_alloc: |
be2fb9da | 157 | kfree(item); |
fdc0b8a6 | 158 | mutex_unlock(&dev->struct_mutex); |
1da177e4 | 159 | |
6de8a748 DV |
160 | mem->offset = 0; |
161 | mem->size = 0; | |
162 | mem->free = 0; | |
163 | ||
83bc5fd2 | 164 | DRM_DEBUG("alloc %d, size = %ld, offset = %ld\n", pool, mem->size, |
c153f45f | 165 | mem->offset); |
1da177e4 LT |
166 | |
167 | return retval; | |
168 | } | |
169 | ||
c153f45f | 170 | static int sis_drm_free(struct drm_device *dev, void *data, struct drm_file *file_priv) |
1da177e4 | 171 | { |
1da177e4 | 172 | drm_sis_private_t *dev_priv = dev->dev_private; |
c153f45f | 173 | drm_sis_mem_t *mem = data; |
be2fb9da | 174 | struct sis_memblock *obj; |
1da177e4 | 175 | |
ce65a44d | 176 | mutex_lock(&dev->struct_mutex); |
6de8a748 DV |
177 | obj = idr_find(&dev_priv->object_idr, mem->free); |
178 | if (obj == NULL) { | |
179 | mutex_unlock(&dev->struct_mutex); | |
180 | return -EINVAL; | |
181 | } | |
182 | ||
183 | idr_remove(&dev_priv->object_idr, mem->free); | |
be2fb9da DV |
184 | list_del(&obj->owner_list); |
185 | if (drm_mm_node_allocated(&obj->mm_node)) | |
186 | drm_mm_remove_node(&obj->mm_node); | |
187 | #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE) | |
188 | else | |
189 | sis_free(obj->req.offset); | |
190 | #endif | |
191 | kfree(obj); | |
ce65a44d | 192 | mutex_unlock(&dev->struct_mutex); |
c153f45f | 193 | DRM_DEBUG("free = 0x%lx\n", mem->free); |
1da177e4 | 194 | |
b5215ef1 | 195 | return 0; |
1da177e4 LT |
196 | } |
197 | ||
c153f45f EA |
198 | static int sis_fb_alloc(struct drm_device *dev, void *data, |
199 | struct drm_file *file_priv) | |
ce65a44d | 200 | { |
6c340eac | 201 | return sis_drm_alloc(dev, file_priv, data, VIDEO_TYPE); |
ce65a44d | 202 | } |
1da177e4 | 203 | |
c153f45f EA |
204 | static int sis_ioctl_agp_init(struct drm_device *dev, void *data, |
205 | struct drm_file *file_priv) | |
1da177e4 | 206 | { |
1da177e4 | 207 | drm_sis_private_t *dev_priv = dev->dev_private; |
c153f45f | 208 | drm_sis_agp_t *agp = data; |
ce65a44d | 209 | dev_priv = dev->dev_private; |
1da177e4 | 210 | |
ce65a44d | 211 | mutex_lock(&dev->struct_mutex); |
be2fb9da | 212 | drm_mm_init(&dev_priv->agp_mm, 0, agp->size >> SIS_MM_ALIGN_SHIFT); |
1da177e4 | 213 | |
a1d0fcf5 | 214 | dev_priv->agp_initialized = 1; |
c153f45f | 215 | dev_priv->agp_offset = agp->offset; |
ce65a44d | 216 | mutex_unlock(&dev->struct_mutex); |
1da177e4 | 217 | |
83bc5fd2 | 218 | DRM_DEBUG("offset = %lu, size = %lu\n", agp->offset, agp->size); |
1da177e4 LT |
219 | return 0; |
220 | } | |
221 | ||
c153f45f EA |
222 | static int sis_ioctl_agp_alloc(struct drm_device *dev, void *data, |
223 | struct drm_file *file_priv) | |
1da177e4 | 224 | { |
1da177e4 | 225 | |
6c340eac | 226 | return sis_drm_alloc(dev, file_priv, data, AGP_TYPE); |
1da177e4 LT |
227 | } |
228 | ||
84b1fd10 | 229 | static drm_local_map_t *sis_reg_init(struct drm_device *dev) |
7981bf7d | 230 | { |
55910517 | 231 | struct drm_map_list *entry; |
7981bf7d TH |
232 | drm_local_map_t *map; |
233 | ||
bd1b331f | 234 | list_for_each_entry(entry, &dev->maplist, head) { |
7981bf7d TH |
235 | map = entry->map; |
236 | if (!map) | |
237 | continue; | |
a7b98b67 | 238 | if (map->type == _DRM_REGISTERS) |
7981bf7d | 239 | return map; |
7981bf7d TH |
240 | } |
241 | return NULL; | |
242 | } | |
243 | ||
84b1fd10 | 244 | int sis_idle(struct drm_device *dev) |
7981bf7d TH |
245 | { |
246 | drm_sis_private_t *dev_priv = dev->dev_private; | |
247 | uint32_t idle_reg; | |
248 | unsigned long end; | |
249 | int i; | |
250 | ||
251 | if (dev_priv->idle_fault) | |
252 | return 0; | |
253 | ||
254 | if (dev_priv->mmio == NULL) { | |
255 | dev_priv->mmio = sis_reg_init(dev); | |
256 | if (dev_priv->mmio == NULL) { | |
257 | DRM_ERROR("Could not find register map.\n"); | |
258 | return 0; | |
259 | } | |
260 | } | |
bc5f4523 | 261 | |
7981bf7d TH |
262 | /* |
263 | * Implement a device switch here if needed | |
264 | */ | |
265 | ||
266 | if (dev_priv->chipset != SIS_CHIP_315) | |
267 | return 0; | |
268 | ||
269 | /* | |
270 | * Timeout after 3 seconds. We cannot use DRM_WAIT_ON here | |
271 | * because its polling frequency is too low. | |
272 | */ | |
273 | ||
274 | end = jiffies + (DRM_HZ * 3); | |
275 | ||
a7b98b67 | 276 | for (i = 0; i < 4; ++i) { |
7981bf7d TH |
277 | do { |
278 | idle_reg = SIS_READ(0x85cc); | |
a7b98b67 | 279 | } while (!time_after_eq(jiffies, end) && |
7981bf7d TH |
280 | ((idle_reg & 0x80000000) != 0x80000000)); |
281 | } | |
282 | ||
283 | if (time_after_eq(jiffies, end)) { | |
284 | DRM_ERROR("Graphics engine idle timeout. " | |
285 | "Disabling idle check\n"); | |
a1d0fcf5 | 286 | dev_priv->idle_fault = 1; |
7981bf7d TH |
287 | } |
288 | ||
289 | /* | |
290 | * The caller never sees an error code. It gets trapped | |
291 | * in libdrm. | |
292 | */ | |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
297 | ||
ce65a44d | 298 | void sis_lastclose(struct drm_device *dev) |
1da177e4 | 299 | { |
1da177e4 | 300 | drm_sis_private_t *dev_priv = dev->dev_private; |
1da177e4 | 301 | |
ce65a44d TH |
302 | if (!dev_priv) |
303 | return; | |
1da177e4 | 304 | |
ce65a44d | 305 | mutex_lock(&dev->struct_mutex); |
be2fb9da DV |
306 | if (dev_priv->vram_initialized) { |
307 | drm_mm_takedown(&dev_priv->vram_mm); | |
308 | dev_priv->vram_initialized = 0; | |
309 | } | |
310 | if (dev_priv->agp_initialized) { | |
311 | drm_mm_takedown(&dev_priv->agp_mm); | |
312 | dev_priv->agp_initialized = 0; | |
313 | } | |
7981bf7d | 314 | dev_priv->mmio = NULL; |
ce65a44d | 315 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
316 | } |
317 | ||
a7b98b67 | 318 | void sis_reclaim_buffers_locked(struct drm_device *dev, |
fdc0b8a6 | 319 | struct drm_file *file) |
1da177e4 | 320 | { |
fdc0b8a6 | 321 | struct sis_file_private *file_priv = file->driver_priv; |
be2fb9da | 322 | struct sis_memblock *entry, *next; |
1da177e4 | 323 | |
ea5e4374 DV |
324 | if (!(file->minor->master && file->master->lock.hw_lock)) |
325 | return; | |
326 | ||
327 | drm_idlelock_take(&file->master->lock); | |
328 | ||
ce65a44d | 329 | mutex_lock(&dev->struct_mutex); |
fdc0b8a6 | 330 | if (list_empty(&file_priv->obj_list)) { |
ce65a44d | 331 | mutex_unlock(&dev->struct_mutex); |
ea5e4374 DV |
332 | drm_idlelock_release(&file->master->lock); |
333 | ||
ce65a44d | 334 | return; |
1da177e4 LT |
335 | } |
336 | ||
ea5e4374 | 337 | sis_idle(dev); |
b5e89ed5 | 338 | |
fdc0b8a6 DV |
339 | |
340 | list_for_each_entry_safe(entry, next, &file_priv->obj_list, | |
341 | owner_list) { | |
be2fb9da DV |
342 | list_del(&entry->owner_list); |
343 | if (drm_mm_node_allocated(&entry->mm_node)) | |
344 | drm_mm_remove_node(&entry->mm_node); | |
345 | #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE) | |
346 | else | |
347 | sis_free(entry->req.offset); | |
348 | #endif | |
349 | kfree(entry); | |
fdc0b8a6 | 350 | } |
ce65a44d | 351 | mutex_unlock(&dev->struct_mutex); |
ea5e4374 DV |
352 | |
353 | drm_idlelock_release(&file->master->lock); | |
354 | ||
ce65a44d | 355 | return; |
1da177e4 LT |
356 | } |
357 | ||
c153f45f | 358 | struct drm_ioctl_desc sis_ioctls[] = { |
1b2f1489 DA |
359 | DRM_IOCTL_DEF_DRV(SIS_FB_ALLOC, sis_fb_alloc, DRM_AUTH), |
360 | DRM_IOCTL_DEF_DRV(SIS_FB_FREE, sis_drm_free, DRM_AUTH), | |
361 | DRM_IOCTL_DEF_DRV(SIS_AGP_INIT, sis_ioctl_agp_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY), | |
362 | DRM_IOCTL_DEF_DRV(SIS_AGP_ALLOC, sis_ioctl_agp_alloc, DRM_AUTH), | |
363 | DRM_IOCTL_DEF_DRV(SIS_AGP_FREE, sis_drm_free, DRM_AUTH), | |
364 | DRM_IOCTL_DEF_DRV(SIS_FB_INIT, sis_fb_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY), | |
1da177e4 LT |
365 | }; |
366 | ||
367 | int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls); |