]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/gpu/drm/via/via_mm.c
drm/via: track user->memblock mapping with idr
[mv-sheeva.git] / drivers / gpu / drm / via / via_mm.c
1 /*
2  * Copyright 2006 Tungsten Graphics Inc., Bismarck, ND., USA.
3  * All rights reserved.
4  *
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:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
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.
23  */
24 /*
25  * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
26  */
27
28 #include "drmP.h"
29 #include "via_drm.h"
30 #include "via_drv.h"
31 #include "drm_sman.h"
32
33 #define VIA_MM_ALIGN_SHIFT 4
34 #define VIA_MM_ALIGN_MASK ((1 << VIA_MM_ALIGN_SHIFT) - 1)
35
36 int via_agp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
37 {
38         drm_via_agp_t *agp = data;
39         drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
40         int ret;
41
42         mutex_lock(&dev->struct_mutex);
43         ret = drm_sman_set_range(&dev_priv->sman, VIA_MEM_AGP, 0,
44                                  agp->size >> VIA_MM_ALIGN_SHIFT);
45
46         if (ret) {
47                 DRM_ERROR("AGP memory manager initialisation error\n");
48                 mutex_unlock(&dev->struct_mutex);
49                 return ret;
50         }
51
52         dev_priv->agp_initialized = 1;
53         dev_priv->agp_offset = agp->offset;
54         mutex_unlock(&dev->struct_mutex);
55
56         DRM_DEBUG("offset = %u, size = %u\n", agp->offset, agp->size);
57         return 0;
58 }
59
60 int via_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
61 {
62         drm_via_fb_t *fb = data;
63         drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
64         int ret;
65
66         mutex_lock(&dev->struct_mutex);
67         ret = drm_sman_set_range(&dev_priv->sman, VIA_MEM_VIDEO, 0,
68                                  fb->size >> VIA_MM_ALIGN_SHIFT);
69
70         if (ret) {
71                 DRM_ERROR("VRAM memory manager initialisation error\n");
72                 mutex_unlock(&dev->struct_mutex);
73                 return ret;
74         }
75
76         dev_priv->vram_initialized = 1;
77         dev_priv->vram_offset = fb->offset;
78
79         mutex_unlock(&dev->struct_mutex);
80         DRM_DEBUG("offset = %u, size = %u\n", fb->offset, fb->size);
81
82         return 0;
83
84 }
85
86 int via_final_context(struct drm_device *dev, int context)
87 {
88         drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
89
90         via_release_futex(dev_priv, context);
91
92         /* Linux specific until context tracking code gets ported to BSD */
93         /* Last context, perform cleanup */
94         if (dev->ctx_count == 1 && dev->dev_private) {
95                 DRM_DEBUG("Last Context\n");
96                 drm_irq_uninstall(dev);
97                 via_cleanup_futex(dev_priv);
98                 via_do_cleanup_map(dev);
99         }
100         return 1;
101 }
102
103 void via_lastclose(struct drm_device *dev)
104 {
105         drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
106
107         if (!dev_priv)
108                 return;
109
110         mutex_lock(&dev->struct_mutex);
111         drm_sman_cleanup(&dev_priv->sman);
112         dev_priv->vram_initialized = 0;
113         dev_priv->agp_initialized = 0;
114         mutex_unlock(&dev->struct_mutex);
115 }
116
117 int via_mem_alloc(struct drm_device *dev, void *data,
118                   struct drm_file *file)
119 {
120         drm_via_mem_t *mem = data;
121         int retval = 0, user_key;
122         struct drm_memblock_item *item;
123         drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
124         struct via_file_private *file_priv = file->driver_priv;
125         unsigned long tmpSize;
126
127         if (mem->type > VIA_MEM_AGP) {
128                 DRM_ERROR("Unknown memory type allocation\n");
129                 return -EINVAL;
130         }
131         mutex_lock(&dev->struct_mutex);
132         if (0 == ((mem->type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized :
133                       dev_priv->agp_initialized)) {
134                 DRM_ERROR
135                     ("Attempt to allocate from uninitialized memory manager.\n");
136                 mutex_unlock(&dev->struct_mutex);
137                 return -EINVAL;
138         }
139
140         tmpSize = (mem->size + VIA_MM_ALIGN_MASK) >> VIA_MM_ALIGN_SHIFT;
141         item = drm_sman_alloc(&dev_priv->sman, mem->type, tmpSize, 0, 0);
142         if (!item) {
143                 retval = -ENOMEM;
144                 goto fail_alloc;
145         }
146
147 again:
148         if (idr_pre_get(&dev_priv->object_idr, GFP_KERNEL) == 0) {
149                 retval = -ENOMEM;
150                 goto fail_idr;
151         }
152
153         retval = idr_get_new_above(&dev_priv->object_idr, item, 1, &user_key);
154         if (retval == -EAGAIN)
155                 goto again;
156         if (retval)
157                 goto fail_idr;
158
159         list_add(&item->owner_list, &file_priv->obj_list);
160         mutex_unlock(&dev->struct_mutex);
161
162         mem->offset = ((mem->type == VIA_MEM_VIDEO) ?
163                       dev_priv->vram_offset : dev_priv->agp_offset) +
164             (item->mm->
165              offset(item->mm, item->mm_info) << VIA_MM_ALIGN_SHIFT);
166         mem->index = user_key;
167
168         return 0;
169
170 fail_idr:
171         drm_sman_free(item);
172 fail_alloc:
173         mutex_unlock(&dev->struct_mutex);
174
175         mem->offset = 0;
176         mem->size = 0;
177         mem->index = 0;
178         DRM_DEBUG("Video memory allocation failed\n");
179
180         return retval;
181 }
182
183 int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
184 {
185         drm_via_private_t *dev_priv = dev->dev_private;
186         drm_via_mem_t *mem = data;
187         struct drm_memblock_item *obj;
188         int ret;
189
190         mutex_lock(&dev->struct_mutex);
191         obj = idr_find(&dev_priv->object_idr, mem->index);
192         if (obj == NULL) {
193                 mutex_unlock(&dev->struct_mutex);
194                 return -EINVAL;
195         }
196
197         idr_remove(&dev_priv->object_idr, mem->index);
198         drm_sman_free(obj);
199         mutex_unlock(&dev->struct_mutex);
200
201         DRM_DEBUG("free = 0x%lx\n", mem->index);
202
203         return ret;
204 }
205
206
207 void via_reclaim_buffers_locked(struct drm_device *dev,
208                                 struct drm_file *file)
209 {
210         struct via_file_private *file_priv = file->driver_priv;
211         struct drm_memblock_item *entry, *next;
212
213         mutex_lock(&dev->struct_mutex);
214         if (list_empty(&file_priv->obj_list)) {
215                 mutex_unlock(&dev->struct_mutex);
216                 return;
217         }
218
219         if (dev->driver->dma_quiescent)
220                 dev->driver->dma_quiescent(dev);
221
222         list_for_each_entry_safe(entry, next, &file_priv->obj_list,
223                                  owner_list) {
224                 drm_sman_free(entry);
225         }
226         mutex_unlock(&dev->struct_mutex);
227         return;
228 }