drm/sman: rip out owner tracking
[deliverable/linux.git] / drivers / gpu / drm / via / via_mm.c
CommitLineData
22f579c6 1/*
ce65a44d
TH
2 * Copyright 2006 Tungsten Graphics Inc., Bismarck, ND., USA.
3 * All rights reserved.
22f579c6
DA
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
ce65a44d
TH
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,
22f579c6
DA
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
ce65a44d 24/*
96de0e25 25 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
ce65a44d
TH
26 */
27
22f579c6
DA
28#include "drmP.h"
29#include "via_drm.h"
30#include "via_drv.h"
ce65a44d 31#include "drm_sman.h"
22f579c6 32
ce65a44d 33#define VIA_MM_ALIGN_SHIFT 4
58c1e85a 34#define VIA_MM_ALIGN_MASK ((1 << VIA_MM_ALIGN_SHIFT) - 1)
22f579c6 35
c153f45f 36int via_agp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
22f579c6 37{
c153f45f 38 drm_via_agp_t *agp = data;
ce65a44d
TH
39 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
40 int ret;
22f579c6 41
ce65a44d
TH
42 mutex_lock(&dev->struct_mutex);
43 ret = drm_sman_set_range(&dev_priv->sman, VIA_MEM_AGP, 0,
c153f45f 44 agp->size >> VIA_MM_ALIGN_SHIFT);
ce65a44d
TH
45
46 if (ret) {
47 DRM_ERROR("AGP memory manager initialisation error\n");
48 mutex_unlock(&dev->struct_mutex);
49 return ret;
50 }
22f579c6 51
d40c8533 52 dev_priv->agp_initialized = 1;
c153f45f 53 dev_priv->agp_offset = agp->offset;
ce65a44d 54 mutex_unlock(&dev->struct_mutex);
22f579c6 55
3e684eae 56 DRM_DEBUG("offset = %u, size = %u\n", agp->offset, agp->size);
22f579c6
DA
57 return 0;
58}
59
c153f45f 60int via_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
22f579c6 61{
c153f45f 62 drm_via_fb_t *fb = data;
ce65a44d
TH
63 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
64 int ret;
22f579c6 65
ce65a44d
TH
66 mutex_lock(&dev->struct_mutex);
67 ret = drm_sman_set_range(&dev_priv->sman, VIA_MEM_VIDEO, 0,
c153f45f 68 fb->size >> VIA_MM_ALIGN_SHIFT);
22f579c6 69
ce65a44d
TH
70 if (ret) {
71 DRM_ERROR("VRAM memory manager initialisation error\n");
72 mutex_unlock(&dev->struct_mutex);
73 return ret;
74 }
22f579c6 75
d40c8533 76 dev_priv->vram_initialized = 1;
c153f45f 77 dev_priv->vram_offset = fb->offset;
22f579c6 78
ce65a44d 79 mutex_unlock(&dev->struct_mutex);
3e684eae 80 DRM_DEBUG("offset = %u, size = %u\n", fb->offset, fb->size);
ce65a44d
TH
81
82 return 0;
22f579c6 83
22f579c6
DA
84}
85
86int via_final_context(struct drm_device *dev, int context)
b5e89ed5 87{
22f579c6
DA
88 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
89
b5e89ed5
DA
90 via_release_futex(dev_priv, context);
91
22f579c6
DA
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) {
b5e89ed5 95 DRM_DEBUG("Last Context\n");
9bfbd5cb 96 drm_irq_uninstall(dev);
22f579c6
DA
97 via_cleanup_futex(dev_priv);
98 via_do_cleanup_map(dev);
99 }
22f579c6
DA
100 return 1;
101}
102
ce65a44d
TH
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);
a1d0fcf5
AM
112 dev_priv->vram_initialized = 0;
113 dev_priv->agp_initialized = 0;
ce65a44d 114 mutex_unlock(&dev->struct_mutex);
bc5f4523 115}
ce65a44d 116
c153f45f 117int via_mem_alloc(struct drm_device *dev, void *data,
c828e204 118 struct drm_file *file)
22f579c6 119{
c153f45f 120 drm_via_mem_t *mem = data;
ce65a44d 121 int retval = 0;
9698b4db 122 struct drm_memblock_item *item;
ce65a44d 123 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
c828e204 124 struct via_file_private *file_priv = file->driver_priv;
ce65a44d 125 unsigned long tmpSize;
22f579c6 126
c153f45f 127 if (mem->type > VIA_MEM_AGP) {
ce65a44d 128 DRM_ERROR("Unknown memory type allocation\n");
20caafa6 129 return -EINVAL;
22f579c6 130 }
ce65a44d 131 mutex_lock(&dev->struct_mutex);
c153f45f 132 if (0 == ((mem->type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized :
ce65a44d
TH
133 dev_priv->agp_initialized)) {
134 DRM_ERROR
135 ("Attempt to allocate from uninitialized memory manager.\n");
136 mutex_unlock(&dev->struct_mutex);
20caafa6 137 return -EINVAL;
22f579c6
DA
138 }
139
c153f45f 140 tmpSize = (mem->size + VIA_MM_ALIGN_MASK) >> VIA_MM_ALIGN_SHIFT;
c828e204
DV
141 item = drm_sman_alloc(&dev_priv->sman, mem->type, tmpSize, 0, 0);
142
ce65a44d 143 if (item) {
763240de 144 list_add(&item->owner_list, &file_priv->obj_list);
c153f45f 145 mem->offset = ((mem->type == VIA_MEM_VIDEO) ?
ce65a44d
TH
146 dev_priv->vram_offset : dev_priv->agp_offset) +
147 (item->mm->
148 offset(item->mm, item->mm_info) << VIA_MM_ALIGN_SHIFT);
c153f45f 149 mem->index = item->user_hash.key;
22f579c6 150 } else {
c153f45f
EA
151 mem->offset = 0;
152 mem->size = 0;
153 mem->index = 0;
ce65a44d 154 DRM_DEBUG("Video memory allocation failed\n");
20caafa6 155 retval = -ENOMEM;
22f579c6 156 }
c828e204 157 mutex_unlock(&dev->struct_mutex);
22f579c6 158
22f579c6
DA
159 return retval;
160}
161
c153f45f 162int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
22f579c6 163{
ce65a44d 164 drm_via_private_t *dev_priv = dev->dev_private;
c153f45f 165 drm_via_mem_t *mem = data;
ce65a44d 166 int ret;
22f579c6 167
ce65a44d 168 mutex_lock(&dev->struct_mutex);
c153f45f 169 ret = drm_sman_free_key(&dev_priv->sman, mem->index);
ce65a44d 170 mutex_unlock(&dev->struct_mutex);
c153f45f 171 DRM_DEBUG("free = 0x%lx\n", mem->index);
22f579c6 172
ce65a44d 173 return ret;
22f579c6
DA
174}
175
22f579c6 176
58c1e85a 177void via_reclaim_buffers_locked(struct drm_device *dev,
c828e204 178 struct drm_file *file)
22f579c6 179{
c828e204
DV
180 struct via_file_private *file_priv = file->driver_priv;
181 struct drm_memblock_item *entry, *next;
22f579c6 182
ce65a44d 183 mutex_lock(&dev->struct_mutex);
c828e204 184 if (list_empty(&file_priv->obj_list)) {
ce65a44d
TH
185 mutex_unlock(&dev->struct_mutex);
186 return;
22f579c6
DA
187 }
188
58c1e85a 189 if (dev->driver->dma_quiescent)
ce65a44d 190 dev->driver->dma_quiescent(dev);
22f579c6 191
c828e204
DV
192 list_for_each_entry_safe(entry, next, &file_priv->obj_list,
193 owner_list) {
194 drm_sman_free(entry);
195 }
ce65a44d
TH
196 mutex_unlock(&dev->struct_mutex);
197 return;
22f579c6 198}
This page took 0.770711 seconds and 5 git commands to generate.