via_mm.c 5.5 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
 */

D
Dave Airlie 已提交
28 29 30
#include "drmP.h"
#include "via_drm.h"
#include "via_drv.h"
31
#include "drm_sman.h"
D
Dave Airlie 已提交
32

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

36
int via_agp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
D
Dave Airlie 已提交
37
{
38
	drm_via_agp_t *agp = data;
39 40
	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
	int ret;
D
Dave Airlie 已提交
41

42 43
	mutex_lock(&dev->struct_mutex);
	ret = drm_sman_set_range(&dev_priv->sman, VIA_MEM_AGP, 0,
44
				 agp->size >> VIA_MM_ALIGN_SHIFT);
45 46 47 48 49 50

	if (ret) {
		DRM_ERROR("AGP memory manager initialisation error\n");
		mutex_unlock(&dev->struct_mutex);
		return ret;
	}
D
Dave Airlie 已提交
51

52
	dev_priv->agp_initialized = 1;
53
	dev_priv->agp_offset = agp->offset;
54
	mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
55

56
	DRM_DEBUG("offset = %u, size = %u\n", agp->offset, agp->size);
D
Dave Airlie 已提交
57 58 59
	return 0;
}

60
int via_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
D
Dave Airlie 已提交
61
{
62
	drm_via_fb_t *fb = data;
63 64
	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
	int ret;
D
Dave Airlie 已提交
65

66 67
	mutex_lock(&dev->struct_mutex);
	ret = drm_sman_set_range(&dev_priv->sman, VIA_MEM_VIDEO, 0,
68
				 fb->size >> VIA_MM_ALIGN_SHIFT);
D
Dave Airlie 已提交
69

70 71 72 73 74
	if (ret) {
		DRM_ERROR("VRAM memory manager initialisation error\n");
		mutex_unlock(&dev->struct_mutex);
		return ret;
	}
D
Dave Airlie 已提交
75

76
	dev_priv->vram_initialized = 1;
77
	dev_priv->vram_offset = fb->offset;
D
Dave Airlie 已提交
78

79
	mutex_unlock(&dev->struct_mutex);
80
	DRM_DEBUG("offset = %u, size = %u\n", fb->offset, fb->size);
81 82

	return 0;
D
Dave Airlie 已提交
83 84 85 86

}

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

D
Dave Airlie 已提交
90 91
	via_release_futex(dev_priv, context);

D
Dave Airlie 已提交
92 93 94
	/* 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 已提交
95
		DRM_DEBUG("Last Context\n");
D
Dave Airlie 已提交
96 97 98 99 100 101 102 103
		if (dev->irq)
			drm_irq_uninstall(dev);
		via_cleanup_futex(dev_priv);
		via_do_cleanup_map(dev);
	}
	return 1;
}

104 105 106 107 108 109 110 111 112
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);
	drm_sman_cleanup(&dev_priv->sman);
113 114
	dev_priv->vram_initialized = 0;
	dev_priv->agp_initialized = 0;
115
	mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
116
}
117

118 119
int via_mem_alloc(struct drm_device *dev, void *data,
		  struct drm_file *file_priv)
D
Dave Airlie 已提交
120
{
121
	drm_via_mem_t *mem = data;
122
	int retval = 0;
D
Dave Airlie 已提交
123
	struct drm_memblock_item *item;
124 125
	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
	unsigned long tmpSize;
D
Dave Airlie 已提交
126

127
	if (mem->type > VIA_MEM_AGP) {
128
		DRM_ERROR("Unknown memory type allocation\n");
E
Eric Anholt 已提交
129
		return -EINVAL;
D
Dave Airlie 已提交
130
	}
131
	mutex_lock(&dev->struct_mutex);
132
	if (0 == ((mem->type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized :
133 134 135 136
		      dev_priv->agp_initialized)) {
		DRM_ERROR
		    ("Attempt to allocate from uninitialized memory manager.\n");
		mutex_unlock(&dev->struct_mutex);
E
Eric Anholt 已提交
137
		return -EINVAL;
D
Dave Airlie 已提交
138 139
	}

140 141
	tmpSize = (mem->size + VIA_MM_ALIGN_MASK) >> VIA_MM_ALIGN_SHIFT;
	item = drm_sman_alloc(&dev_priv->sman, mem->type, tmpSize, 0,
142
			      (unsigned long)file_priv);
143 144
	mutex_unlock(&dev->struct_mutex);
	if (item) {
145
		mem->offset = ((mem->type == VIA_MEM_VIDEO) ?
146 147 148
			      dev_priv->vram_offset : dev_priv->agp_offset) +
		    (item->mm->
		     offset(item->mm, item->mm_info) << VIA_MM_ALIGN_SHIFT);
149
		mem->index = item->user_hash.key;
D
Dave Airlie 已提交
150
	} else {
151 152 153
		mem->offset = 0;
		mem->size = 0;
		mem->index = 0;
154
		DRM_DEBUG("Video memory allocation failed\n");
E
Eric Anholt 已提交
155
		retval = -ENOMEM;
D
Dave Airlie 已提交
156 157 158 159 160
	}

	return retval;
}

161
int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
D
Dave Airlie 已提交
162
{
163
	drm_via_private_t *dev_priv = dev->dev_private;
164
	drm_via_mem_t *mem = data;
165
	int ret;
D
Dave Airlie 已提交
166

167
	mutex_lock(&dev->struct_mutex);
168
	ret = drm_sman_free_key(&dev_priv->sman, mem->index);
169
	mutex_unlock(&dev->struct_mutex);
170
	DRM_DEBUG("free = 0x%lx\n", mem->index);
D
Dave Airlie 已提交
171

172
	return ret;
D
Dave Airlie 已提交
173 174 175
}


176 177
void via_reclaim_buffers_locked(struct drm_device * dev,
				struct drm_file *file_priv)
D
Dave Airlie 已提交
178
{
179
	drm_via_private_t *dev_priv = dev->dev_private;
D
Dave Airlie 已提交
180

181
	mutex_lock(&dev->struct_mutex);
182
	if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)file_priv)) {
183 184
		mutex_unlock(&dev->struct_mutex);
		return;
D
Dave Airlie 已提交
185 186
	}

187 188 189
	if (dev->driver->dma_quiescent) {
		dev->driver->dma_quiescent(dev);
	}
D
Dave Airlie 已提交
190

191
	drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)file_priv);
192 193
	mutex_unlock(&dev->struct_mutex);
	return;
D
Dave Airlie 已提交
194
}