Loading...
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
36int 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
60int 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
86int 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
103void 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
117int via_mem_alloc(struct drm_device *dev, void *data,
118 struct drm_file *file_priv)
119{
120 drm_via_mem_t *mem = data;
121 int retval = 0;
122 struct drm_memblock_item *item;
123 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
124 unsigned long tmpSize;
125
126 if (mem->type > VIA_MEM_AGP) {
127 DRM_ERROR("Unknown memory type allocation\n");
128 return -EINVAL;
129 }
130 mutex_lock(&dev->struct_mutex);
131 if (0 == ((mem->type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized :
132 dev_priv->agp_initialized)) {
133 DRM_ERROR
134 ("Attempt to allocate from uninitialized memory manager.\n");
135 mutex_unlock(&dev->struct_mutex);
136 return -EINVAL;
137 }
138
139 tmpSize = (mem->size + VIA_MM_ALIGN_MASK) >> VIA_MM_ALIGN_SHIFT;
140 item = drm_sman_alloc(&dev_priv->sman, mem->type, tmpSize, 0,
141 (unsigned long)file_priv);
142 mutex_unlock(&dev->struct_mutex);
143 if (item) {
144 mem->offset = ((mem->type == VIA_MEM_VIDEO) ?
145 dev_priv->vram_offset : dev_priv->agp_offset) +
146 (item->mm->
147 offset(item->mm, item->mm_info) << VIA_MM_ALIGN_SHIFT);
148 mem->index = item->user_hash.key;
149 } else {
150 mem->offset = 0;
151 mem->size = 0;
152 mem->index = 0;
153 DRM_DEBUG("Video memory allocation failed\n");
154 retval = -ENOMEM;
155 }
156
157 return retval;
158}
159
160int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
161{
162 drm_via_private_t *dev_priv = dev->dev_private;
163 drm_via_mem_t *mem = data;
164 int ret;
165
166 mutex_lock(&dev->struct_mutex);
167 ret = drm_sman_free_key(&dev_priv->sman, mem->index);
168 mutex_unlock(&dev->struct_mutex);
169 DRM_DEBUG("free = 0x%lx\n", mem->index);
170
171 return ret;
172}
173
174
175void via_reclaim_buffers_locked(struct drm_device *dev,
176 struct drm_file *file_priv)
177{
178 drm_via_private_t *dev_priv = dev->dev_private;
179
180 mutex_lock(&dev->struct_mutex);
181 if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)file_priv)) {
182 mutex_unlock(&dev->struct_mutex);
183 return;
184 }
185
186 if (dev->driver->dma_quiescent)
187 dev->driver->dma_quiescent(dev);
188
189 drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)file_priv);
190 mutex_unlock(&dev->struct_mutex);
191 return;
192}
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 <linux/slab.h>
29
30#include <drm/drm_device.h>
31#include <drm/drm_file.h>
32#include <drm/drm_irq.h>
33#include <drm/via_drm.h>
34
35#include "via_drv.h"
36
37#define VIA_MM_ALIGN_SHIFT 4
38#define VIA_MM_ALIGN_MASK ((1 << VIA_MM_ALIGN_SHIFT) - 1)
39
40struct via_memblock {
41 struct drm_mm_node mm_node;
42 struct list_head owner_list;
43};
44
45int via_agp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
46{
47 drm_via_agp_t *agp = data;
48 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
49
50 mutex_lock(&dev->struct_mutex);
51 drm_mm_init(&dev_priv->agp_mm, 0, agp->size >> VIA_MM_ALIGN_SHIFT);
52
53 dev_priv->agp_initialized = 1;
54 dev_priv->agp_offset = agp->offset;
55 mutex_unlock(&dev->struct_mutex);
56
57 DRM_DEBUG("offset = %u, size = %u\n", agp->offset, agp->size);
58 return 0;
59}
60
61int via_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
62{
63 drm_via_fb_t *fb = data;
64 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
65
66 mutex_lock(&dev->struct_mutex);
67 drm_mm_init(&dev_priv->vram_mm, 0, fb->size >> VIA_MM_ALIGN_SHIFT);
68
69 dev_priv->vram_initialized = 1;
70 dev_priv->vram_offset = fb->offset;
71
72 mutex_unlock(&dev->struct_mutex);
73 DRM_DEBUG("offset = %u, size = %u\n", fb->offset, fb->size);
74
75 return 0;
76
77}
78
79int via_final_context(struct drm_device *dev, int context)
80{
81 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
82
83 via_release_futex(dev_priv, context);
84
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);
92 }
93 return 1;
94}
95
96void via_lastclose(struct drm_device *dev)
97{
98 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
99
100 if (!dev_priv)
101 return;
102
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;
107 }
108 if (dev_priv->agp_initialized) {
109 drm_mm_takedown(&dev_priv->agp_mm);
110 dev_priv->agp_initialized = 0;
111 }
112 mutex_unlock(&dev->struct_mutex);
113}
114
115int via_mem_alloc(struct drm_device *dev, void *data,
116 struct drm_file *file)
117{
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;
124
125 if (mem->type > VIA_MEM_AGP) {
126 DRM_ERROR("Unknown memory type allocation\n");
127 return -EINVAL;
128 }
129 mutex_lock(&dev->struct_mutex);
130 if (0 == ((mem->type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized :
131 dev_priv->agp_initialized)) {
132 DRM_ERROR
133 ("Attempt to allocate from uninitialized memory manager.\n");
134 mutex_unlock(&dev->struct_mutex);
135 return -EINVAL;
136 }
137
138 item = kzalloc(sizeof(*item), GFP_KERNEL);
139 if (!item) {
140 retval = -ENOMEM;
141 goto fail_alloc;
142 }
143
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,
147 &item->mm_node,
148 tmpSize);
149 else
150 retval = drm_mm_insert_node(&dev_priv->vram_mm,
151 &item->mm_node,
152 tmpSize);
153 if (retval)
154 goto fail_alloc;
155
156 retval = idr_alloc(&dev_priv->object_idr, item, 1, 0, GFP_KERNEL);
157 if (retval < 0)
158 goto fail_idr;
159 user_key = retval;
160
161 list_add(&item->owner_list, &file_priv->obj_list);
162 mutex_unlock(&dev->struct_mutex);
163
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;
168
169 return 0;
170
171fail_idr:
172 drm_mm_remove_node(&item->mm_node);
173fail_alloc:
174 kfree(item);
175 mutex_unlock(&dev->struct_mutex);
176
177 mem->offset = 0;
178 mem->size = 0;
179 mem->index = 0;
180 DRM_DEBUG("Video memory allocation failed\n");
181
182 return retval;
183}
184
185int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
186{
187 drm_via_private_t *dev_priv = dev->dev_private;
188 drm_via_mem_t *mem = data;
189 struct via_memblock *obj;
190
191 mutex_lock(&dev->struct_mutex);
192 obj = idr_find(&dev_priv->object_idr, mem->index);
193 if (obj == NULL) {
194 mutex_unlock(&dev->struct_mutex);
195 return -EINVAL;
196 }
197
198 idr_remove(&dev_priv->object_idr, mem->index);
199 list_del(&obj->owner_list);
200 drm_mm_remove_node(&obj->mm_node);
201 kfree(obj);
202 mutex_unlock(&dev->struct_mutex);
203
204 DRM_DEBUG("free = 0x%lx\n", mem->index);
205
206 return 0;
207}
208
209
210void via_reclaim_buffers_locked(struct drm_device *dev,
211 struct drm_file *file)
212{
213 struct via_file_private *file_priv = file->driver_priv;
214 struct via_memblock *entry, *next;
215
216 if (!(dev->master && file->master->lock.hw_lock))
217 return;
218
219 drm_legacy_idlelock_take(&file->master->lock);
220
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);
225
226 return;
227 }
228
229 via_driver_dma_quiescent(dev);
230
231 list_for_each_entry_safe(entry, next, &file_priv->obj_list,
232 owner_list) {
233 list_del(&entry->owner_list);
234 drm_mm_remove_node(&entry->mm_node);
235 kfree(entry);
236 }
237 mutex_unlock(&dev->struct_mutex);
238
239 drm_legacy_idlelock_release(&file->master->lock);
240
241 return;
242}