via_mm.c 6.4 KB
Newer Older
D
Dave Airlie 已提交
1
/*
2 3
 * Copyright 2006 Tungsten Graphics Inc., Bismarck, ND., USA.
 * All rights reserved.
D
Dave Airlie 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sub license,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 20
 * THE AUTHORS OR COPYRIGHT HOLDERS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
D
Dave Airlie 已提交
21 22 23
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
24
/*
25
 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
26 27
 */

28 29
#include <drm/drmP.h>
#include <drm/via_drm.h>
D
Dave Airlie 已提交
30 31
#include "via_drv.h"

32
#define VIA_MM_ALIGN_SHIFT 4
33
#define VIA_MM_ALIGN_MASK ((1 << VIA_MM_ALIGN_SHIFT) - 1)
D
Dave Airlie 已提交
34

35 36 37 38 39
struct via_memblock {
	struct drm_mm_node mm_node;
	struct list_head owner_list;
};

40
int via_agp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
D
Dave Airlie 已提交
41
{
42
	drm_via_agp_t *agp = data;
43
	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
D
Dave Airlie 已提交
44

45
	mutex_lock(&dev->struct_mutex);
46
	drm_mm_init(&dev_priv->agp_mm, 0, agp->size >> VIA_MM_ALIGN_SHIFT);
D
Dave Airlie 已提交
47

48
	dev_priv->agp_initialized = 1;
49
	dev_priv->agp_offset = agp->offset;
50
	mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
51

52
	DRM_DEBUG("offset = %u, size = %u\n", agp->offset, agp->size);
D
Dave Airlie 已提交
53 54 55
	return 0;
}

56
int via_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
D
Dave Airlie 已提交
57
{
58
	drm_via_fb_t *fb = data;
59
	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
D
Dave Airlie 已提交
60

61
	mutex_lock(&dev->struct_mutex);
62
	drm_mm_init(&dev_priv->vram_mm, 0, fb->size >> VIA_MM_ALIGN_SHIFT);
D
Dave Airlie 已提交
63

64
	dev_priv->vram_initialized = 1;
65
	dev_priv->vram_offset = fb->offset;
D
Dave Airlie 已提交
66

67
	mutex_unlock(&dev->struct_mutex);
68
	DRM_DEBUG("offset = %u, size = %u\n", fb->offset, fb->size);
69 70

	return 0;
D
Dave Airlie 已提交
71 72 73 74

}

int via_final_context(struct drm_device *dev, int context)
D
Dave Airlie 已提交
75
{
D
Dave Airlie 已提交
76 77
	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;

D
Dave Airlie 已提交
78 79
	via_release_futex(dev_priv, context);

D
Dave Airlie 已提交
80 81 82
	/* Linux specific until context tracking code gets ported to BSD */
	/* Last context, perform cleanup */
	if (dev->ctx_count == 1 && dev->dev_private) {
D
Dave Airlie 已提交
83
		DRM_DEBUG("Last Context\n");
J
Jesse Barnes 已提交
84
		drm_irq_uninstall(dev);
D
Dave Airlie 已提交
85 86 87 88 89 90
		via_cleanup_futex(dev_priv);
		via_do_cleanup_map(dev);
	}
	return 1;
}

91 92 93 94 95 96 97 98
void via_lastclose(struct drm_device *dev)
{
	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;

	if (!dev_priv)
		return;

	mutex_lock(&dev->struct_mutex);
99 100 101 102 103 104 105 106
	if (dev_priv->vram_initialized) {
		drm_mm_takedown(&dev_priv->vram_mm);
		dev_priv->vram_initialized = 0;
	}
	if (dev_priv->agp_initialized) {
		drm_mm_takedown(&dev_priv->agp_mm);
		dev_priv->agp_initialized = 0;
	}
107
	mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
108
}
109

110
int via_mem_alloc(struct drm_device *dev, void *data,
111
		  struct drm_file *file)
D
Dave Airlie 已提交
112
{
113
	drm_via_mem_t *mem = data;
114
	int retval = 0, user_key;
115
	struct via_memblock *item;
116
	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
117
	struct via_file_private *file_priv = file->driver_priv;
118
	unsigned long tmpSize;
D
Dave Airlie 已提交
119

120
	if (mem->type > VIA_MEM_AGP) {
121
		DRM_ERROR("Unknown memory type allocation\n");
E
Eric Anholt 已提交
122
		return -EINVAL;
D
Dave Airlie 已提交
123
	}
124
	mutex_lock(&dev->struct_mutex);
125
	if (0 == ((mem->type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized :
126 127 128 129
		      dev_priv->agp_initialized)) {
		DRM_ERROR
		    ("Attempt to allocate from uninitialized memory manager.\n");
		mutex_unlock(&dev->struct_mutex);
E
Eric Anholt 已提交
130
		return -EINVAL;
D
Dave Airlie 已提交
131 132
	}

133
	item = kzalloc(sizeof(*item), GFP_KERNEL);
134 135 136 137
	if (!item) {
		retval = -ENOMEM;
		goto fail_alloc;
	}
138

139 140 141 142
	tmpSize = (mem->size + VIA_MM_ALIGN_MASK) >> VIA_MM_ALIGN_SHIFT;
	if (mem->type == VIA_MEM_AGP)
		retval = drm_mm_insert_node(&dev_priv->agp_mm,
					    &item->mm_node,
143
					    tmpSize, 0, DRM_MM_SEARCH_DEFAULT);
144 145 146
	else
		retval = drm_mm_insert_node(&dev_priv->vram_mm,
					    &item->mm_node,
147
					    tmpSize, 0, DRM_MM_SEARCH_DEFAULT);
148 149 150
	if (retval)
		goto fail_alloc;

T
Tejun Heo 已提交
151 152
	retval = idr_alloc(&dev_priv->object_idr, item, 1, 0, GFP_KERNEL);
	if (retval < 0)
153
		goto fail_idr;
T
Tejun Heo 已提交
154
	user_key = retval;
155 156

	list_add(&item->owner_list, &file_priv->obj_list);
157
	mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
158

159 160
	mem->offset = ((mem->type == VIA_MEM_VIDEO) ?
		      dev_priv->vram_offset : dev_priv->agp_offset) +
161
	    ((item->mm_node.start) << VIA_MM_ALIGN_SHIFT);
162 163 164 165 166
	mem->index = user_key;

	return 0;

fail_idr:
167
	drm_mm_remove_node(&item->mm_node);
168
fail_alloc:
169
	kfree(item);
170 171 172 173 174 175 176
	mutex_unlock(&dev->struct_mutex);

	mem->offset = 0;
	mem->size = 0;
	mem->index = 0;
	DRM_DEBUG("Video memory allocation failed\n");

D
Dave Airlie 已提交
177 178 179
	return retval;
}

180
int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
D
Dave Airlie 已提交
181
{
182
	drm_via_private_t *dev_priv = dev->dev_private;
183
	drm_via_mem_t *mem = data;
184
	struct via_memblock *obj;
D
Dave Airlie 已提交
185

186
	mutex_lock(&dev->struct_mutex);
187 188 189 190 191 192 193
	obj = idr_find(&dev_priv->object_idr, mem->index);
	if (obj == NULL) {
		mutex_unlock(&dev->struct_mutex);
		return -EINVAL;
	}

	idr_remove(&dev_priv->object_idr, mem->index);
194 195 196
	list_del(&obj->owner_list);
	drm_mm_remove_node(&obj->mm_node);
	kfree(obj);
197
	mutex_unlock(&dev->struct_mutex);
198

199
	DRM_DEBUG("free = 0x%lx\n", mem->index);
D
Dave Airlie 已提交
200

201
	return 0;
D
Dave Airlie 已提交
202 203 204
}


205
void via_reclaim_buffers_locked(struct drm_device *dev,
206
				struct drm_file *file)
D
Dave Airlie 已提交
207
{
208
	struct via_file_private *file_priv = file->driver_priv;
209
	struct via_memblock *entry, *next;
D
Dave Airlie 已提交
210

211 212 213 214 215
	if (!(file->minor->master && file->master->lock.hw_lock))
		return;

	drm_idlelock_take(&file->master->lock);

216
	mutex_lock(&dev->struct_mutex);
217
	if (list_empty(&file_priv->obj_list)) {
218
		mutex_unlock(&dev->struct_mutex);
219 220
		drm_idlelock_release(&file->master->lock);

221
		return;
D
Dave Airlie 已提交
222 223
	}

224
	via_driver_dma_quiescent(dev);
D
Dave Airlie 已提交
225

226 227
	list_for_each_entry_safe(entry, next, &file_priv->obj_list,
				 owner_list) {
228 229 230
		list_del(&entry->owner_list);
		drm_mm_remove_node(&entry->mm_node);
		kfree(entry);
231
	}
232
	mutex_unlock(&dev->struct_mutex);
233 234 235

	drm_idlelock_release(&file->master->lock);

236
	return;
D
Dave Airlie 已提交
237
}