2 * Copyright 2006 Tungsten Graphics Inc., Bismarck, ND., USA.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
28 #include <linux/slab.h>
30 #include <drm/drm_device.h>
31 #include <drm/drm_file.h>
32 #include <drm/drm_irq.h>
33 #include <drm/via_drm.h>
37 #define VIA_MM_ALIGN_SHIFT 4
38 #define VIA_MM_ALIGN_MASK ((1 << VIA_MM_ALIGN_SHIFT) - 1)
41 struct drm_mm_node mm_node
;
42 struct list_head owner_list
;
45 int via_agp_init(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
47 drm_via_agp_t
*agp
= data
;
48 drm_via_private_t
*dev_priv
= (drm_via_private_t
*) dev
->dev_private
;
50 mutex_lock(&dev
->struct_mutex
);
51 drm_mm_init(&dev_priv
->agp_mm
, 0, agp
->size
>> VIA_MM_ALIGN_SHIFT
);
53 dev_priv
->agp_initialized
= 1;
54 dev_priv
->agp_offset
= agp
->offset
;
55 mutex_unlock(&dev
->struct_mutex
);
57 DRM_DEBUG("offset = %u, size = %u\n", agp
->offset
, agp
->size
);
61 int via_fb_init(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
63 drm_via_fb_t
*fb
= data
;
64 drm_via_private_t
*dev_priv
= (drm_via_private_t
*) dev
->dev_private
;
66 mutex_lock(&dev
->struct_mutex
);
67 drm_mm_init(&dev_priv
->vram_mm
, 0, fb
->size
>> VIA_MM_ALIGN_SHIFT
);
69 dev_priv
->vram_initialized
= 1;
70 dev_priv
->vram_offset
= fb
->offset
;
72 mutex_unlock(&dev
->struct_mutex
);
73 DRM_DEBUG("offset = %u, size = %u\n", fb
->offset
, fb
->size
);
79 int via_final_context(struct drm_device
*dev
, int context
)
81 drm_via_private_t
*dev_priv
= (drm_via_private_t
*) dev
->dev_private
;
83 via_release_futex(dev_priv
, context
);
85 /* Linux specific until context tracking code gets ported to BSD */
86 /* Last context, perform cleanup */
87 if (list_is_singular(&dev
->ctxlist
)) {
88 DRM_DEBUG("Last Context\n");
89 drm_irq_uninstall(dev
);
90 via_cleanup_futex(dev_priv
);
91 via_do_cleanup_map(dev
);
96 void via_lastclose(struct drm_device
*dev
)
98 drm_via_private_t
*dev_priv
= (drm_via_private_t
*) dev
->dev_private
;
103 mutex_lock(&dev
->struct_mutex
);
104 if (dev_priv
->vram_initialized
) {
105 drm_mm_takedown(&dev_priv
->vram_mm
);
106 dev_priv
->vram_initialized
= 0;
108 if (dev_priv
->agp_initialized
) {
109 drm_mm_takedown(&dev_priv
->agp_mm
);
110 dev_priv
->agp_initialized
= 0;
112 mutex_unlock(&dev
->struct_mutex
);
115 int via_mem_alloc(struct drm_device
*dev
, void *data
,
116 struct drm_file
*file
)
118 drm_via_mem_t
*mem
= data
;
119 int retval
= 0, user_key
;
120 struct via_memblock
*item
;
121 drm_via_private_t
*dev_priv
= (drm_via_private_t
*) dev
->dev_private
;
122 struct via_file_private
*file_priv
= file
->driver_priv
;
123 unsigned long tmpSize
;
125 if (mem
->type
> VIA_MEM_AGP
) {
126 DRM_ERROR("Unknown memory type allocation\n");
129 mutex_lock(&dev
->struct_mutex
);
130 if (0 == ((mem
->type
== VIA_MEM_VIDEO
) ? dev_priv
->vram_initialized
:
131 dev_priv
->agp_initialized
)) {
133 ("Attempt to allocate from uninitialized memory manager.\n");
134 mutex_unlock(&dev
->struct_mutex
);
138 item
= kzalloc(sizeof(*item
), GFP_KERNEL
);
144 tmpSize
= (mem
->size
+ VIA_MM_ALIGN_MASK
) >> VIA_MM_ALIGN_SHIFT
;
145 if (mem
->type
== VIA_MEM_AGP
)
146 retval
= drm_mm_insert_node(&dev_priv
->agp_mm
,
150 retval
= drm_mm_insert_node(&dev_priv
->vram_mm
,
156 retval
= idr_alloc(&dev_priv
->object_idr
, item
, 1, 0, GFP_KERNEL
);
161 list_add(&item
->owner_list
, &file_priv
->obj_list
);
162 mutex_unlock(&dev
->struct_mutex
);
164 mem
->offset
= ((mem
->type
== VIA_MEM_VIDEO
) ?
165 dev_priv
->vram_offset
: dev_priv
->agp_offset
) +
166 ((item
->mm_node
.start
) << VIA_MM_ALIGN_SHIFT
);
167 mem
->index
= user_key
;
172 drm_mm_remove_node(&item
->mm_node
);
175 mutex_unlock(&dev
->struct_mutex
);
180 DRM_DEBUG("Video memory allocation failed\n");
185 int via_mem_free(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
187 drm_via_private_t
*dev_priv
= dev
->dev_private
;
188 drm_via_mem_t
*mem
= data
;
189 struct via_memblock
*obj
;
191 mutex_lock(&dev
->struct_mutex
);
192 obj
= idr_find(&dev_priv
->object_idr
, mem
->index
);
194 mutex_unlock(&dev
->struct_mutex
);
198 idr_remove(&dev_priv
->object_idr
, mem
->index
);
199 list_del(&obj
->owner_list
);
200 drm_mm_remove_node(&obj
->mm_node
);
202 mutex_unlock(&dev
->struct_mutex
);
204 DRM_DEBUG("free = 0x%lx\n", mem
->index
);
210 void via_reclaim_buffers_locked(struct drm_device
*dev
,
211 struct drm_file
*file
)
213 struct via_file_private
*file_priv
= file
->driver_priv
;
214 struct via_memblock
*entry
, *next
;
216 if (!(dev
->master
&& file
->master
->lock
.hw_lock
))
219 drm_legacy_idlelock_take(&file
->master
->lock
);
221 mutex_lock(&dev
->struct_mutex
);
222 if (list_empty(&file_priv
->obj_list
)) {
223 mutex_unlock(&dev
->struct_mutex
);
224 drm_legacy_idlelock_release(&file
->master
->lock
);
229 via_driver_dma_quiescent(dev
);
231 list_for_each_entry_safe(entry
, next
, &file_priv
->obj_list
,
233 list_del(&entry
->owner_list
);
234 drm_mm_remove_node(&entry
->mm_node
);
237 mutex_unlock(&dev
->struct_mutex
);
239 drm_legacy_idlelock_release(&file
->master
->lock
);