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