blob: 3ffbf86498331453f64bdf1d7b7060d655088099 [file] [log] [blame]
Dave Airlie22f579c2005-06-28 22:48:56 +10001/*
Thomas Hellstromce65a442006-08-07 22:03:22 +10002 * Copyright 2006 Tungsten Graphics Inc., Bismarck, ND., USA.
3 * All rights reserved.
Dave Airlie22f579c2005-06-28 22:48:56 +10004 *
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
Thomas Hellstromce65a442006-08-07 22:03:22 +100019 * 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,
Dave Airlie22f579c2005-06-28 22:48:56 +100021 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
Thomas Hellstromce65a442006-08-07 22:03:22 +100024/*
Jan Engelhardt96de0e22007-10-19 23:21:04 +020025 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
Thomas Hellstromce65a442006-08-07 22:03:22 +100026 */
27
Dave Airlie22f579c2005-06-28 22:48:56 +100028#include "drmP.h"
29#include "via_drm.h"
30#include "via_drv.h"
Thomas Hellstromce65a442006-08-07 22:03:22 +100031#include "drm_sman.h"
Dave Airlie22f579c2005-06-28 22:48:56 +100032
Thomas Hellstromce65a442006-08-07 22:03:22 +100033#define VIA_MM_ALIGN_SHIFT 4
34#define VIA_MM_ALIGN_MASK ( (1 << VIA_MM_ALIGN_SHIFT) - 1)
Dave Airlie22f579c2005-06-28 22:48:56 +100035
Eric Anholtc153f452007-09-03 12:06:45 +100036int via_agp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlie22f579c2005-06-28 22:48:56 +100037{
Eric Anholtc153f452007-09-03 12:06:45 +100038 drm_via_agp_t *agp = data;
Thomas Hellstromce65a442006-08-07 22:03:22 +100039 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
40 int ret;
Dave Airlie22f579c2005-06-28 22:48:56 +100041
Thomas Hellstromce65a442006-08-07 22:03:22 +100042 mutex_lock(&dev->struct_mutex);
43 ret = drm_sman_set_range(&dev_priv->sman, VIA_MEM_AGP, 0,
Eric Anholtc153f452007-09-03 12:06:45 +100044 agp->size >> VIA_MM_ALIGN_SHIFT);
Dave Airlie22f579c2005-06-28 22:48:56 +100045
Thomas Hellstromce65a442006-08-07 22:03:22 +100046 if (ret) {
47 DRM_ERROR("AGP memory manager initialisation error\n");
48 mutex_unlock(&dev->struct_mutex);
49 return ret;
50 }
Dave Airlie22f579c2005-06-28 22:48:56 +100051
Dave Airlied40c8532006-08-19 17:40:50 +100052 dev_priv->agp_initialized = 1;
Eric Anholtc153f452007-09-03 12:06:45 +100053 dev_priv->agp_offset = agp->offset;
Thomas Hellstromce65a442006-08-07 22:03:22 +100054 mutex_unlock(&dev->struct_mutex);
Dave Airlie22f579c2005-06-28 22:48:56 +100055
Eric Anholtc153f452007-09-03 12:06:45 +100056 DRM_DEBUG("offset = %u, size = %u", agp->offset, agp->size);
Dave Airlie22f579c2005-06-28 22:48:56 +100057 return 0;
58}
59
Eric Anholtc153f452007-09-03 12:06:45 +100060int via_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlie22f579c2005-06-28 22:48:56 +100061{
Eric Anholtc153f452007-09-03 12:06:45 +100062 drm_via_fb_t *fb = data;
Thomas Hellstromce65a442006-08-07 22:03:22 +100063 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
64 int ret;
Dave Airlie22f579c2005-06-28 22:48:56 +100065
Thomas Hellstromce65a442006-08-07 22:03:22 +100066 mutex_lock(&dev->struct_mutex);
67 ret = drm_sman_set_range(&dev_priv->sman, VIA_MEM_VIDEO, 0,
Eric Anholtc153f452007-09-03 12:06:45 +100068 fb->size >> VIA_MM_ALIGN_SHIFT);
Dave Airlie22f579c2005-06-28 22:48:56 +100069
Thomas Hellstromce65a442006-08-07 22:03:22 +100070 if (ret) {
71 DRM_ERROR("VRAM memory manager initialisation error\n");
72 mutex_unlock(&dev->struct_mutex);
73 return ret;
Dave Airlie22f579c2005-06-28 22:48:56 +100074 }
75
Dave Airlied40c8532006-08-19 17:40:50 +100076 dev_priv->vram_initialized = 1;
Eric Anholtc153f452007-09-03 12:06:45 +100077 dev_priv->vram_offset = fb->offset;
Thomas Hellstromce65a442006-08-07 22:03:22 +100078
79 mutex_unlock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +100080 DRM_DEBUG("offset = %u, size = %u", fb->offset, fb->size);
Thomas Hellstromce65a442006-08-07 22:03:22 +100081
82 return 0;
83
Dave Airlie22f579c2005-06-28 22:48:56 +100084}
85
86int via_final_context(struct drm_device *dev, int context)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100087{
Dave Airlie22f579c2005-06-28 22:48:56 +100088 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
89
Dave Airlieb5e89ed2005-09-25 14:28:13 +100090 via_release_futex(dev_priv, context);
91
Dave Airlie22f579c2005-06-28 22:48:56 +100092 /* Linux specific until context tracking code gets ported to BSD */
93 /* Last context, perform cleanup */
94 if (dev->ctx_count == 1 && dev->dev_private) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100095 DRM_DEBUG("Last Context\n");
Dave Airlie22f579c2005-06-28 22:48:56 +100096 if (dev->irq)
97 drm_irq_uninstall(dev);
Dave Airlie22f579c2005-06-28 22:48:56 +100098 via_cleanup_futex(dev_priv);
99 via_do_cleanup_map(dev);
100 }
Dave Airlie22f579c2005-06-28 22:48:56 +1000101 return 1;
102}
103
Thomas Hellstromce65a442006-08-07 22:03:22 +1000104void via_lastclose(struct drm_device *dev)
105{
106 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
107
108 if (!dev_priv)
109 return;
110
111 mutex_lock(&dev->struct_mutex);
112 drm_sman_cleanup(&dev_priv->sman);
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000113 dev_priv->vram_initialized = 0;
114 dev_priv->agp_initialized = 0;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000115 mutex_unlock(&dev->struct_mutex);
116}
117
Eric Anholtc153f452007-09-03 12:06:45 +1000118int via_mem_alloc(struct drm_device *dev, void *data,
119 struct drm_file *file_priv)
Dave Airlie22f579c2005-06-28 22:48:56 +1000120{
Eric Anholtc153f452007-09-03 12:06:45 +1000121 drm_via_mem_t *mem = data;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000122 int retval = 0;
Dave Airlie9698b4d2007-07-12 10:21:05 +1000123 struct drm_memblock_item *item;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000124 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
125 unsigned long tmpSize;
Dave Airlie22f579c2005-06-28 22:48:56 +1000126
Eric Anholtc153f452007-09-03 12:06:45 +1000127 if (mem->type > VIA_MEM_AGP) {
Thomas Hellstromce65a442006-08-07 22:03:22 +1000128 DRM_ERROR("Unknown memory type allocation\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000129 return -EINVAL;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000130 }
131 mutex_lock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000132 if (0 == ((mem->type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized :
Thomas Hellstromce65a442006-08-07 22:03:22 +1000133 dev_priv->agp_initialized)) {
134 DRM_ERROR
135 ("Attempt to allocate from uninitialized memory manager.\n");
136 mutex_unlock(&dev->struct_mutex);
Eric Anholt20caafa2007-08-25 19:22:43 +1000137 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000138 }
139
Eric Anholtc153f452007-09-03 12:06:45 +1000140 tmpSize = (mem->size + VIA_MM_ALIGN_MASK) >> VIA_MM_ALIGN_SHIFT;
141 item = drm_sman_alloc(&dev_priv->sman, mem->type, tmpSize, 0,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000142 (unsigned long)file_priv);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000143 mutex_unlock(&dev->struct_mutex);
144 if (item) {
Eric Anholtc153f452007-09-03 12:06:45 +1000145 mem->offset = ((mem->type == VIA_MEM_VIDEO) ?
Thomas Hellstromce65a442006-08-07 22:03:22 +1000146 dev_priv->vram_offset : dev_priv->agp_offset) +
147 (item->mm->
148 offset(item->mm, item->mm_info) << VIA_MM_ALIGN_SHIFT);
Eric Anholtc153f452007-09-03 12:06:45 +1000149 mem->index = item->user_hash.key;
Dave Airlie22f579c2005-06-28 22:48:56 +1000150 } else {
Eric Anholtc153f452007-09-03 12:06:45 +1000151 mem->offset = 0;
152 mem->size = 0;
153 mem->index = 0;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000154 DRM_DEBUG("Video memory allocation failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000155 retval = -ENOMEM;
Dave Airlie22f579c2005-06-28 22:48:56 +1000156 }
157
Dave Airlie22f579c2005-06-28 22:48:56 +1000158 return retval;
159}
160
Eric Anholtc153f452007-09-03 12:06:45 +1000161int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlie22f579c2005-06-28 22:48:56 +1000162{
Thomas Hellstromce65a442006-08-07 22:03:22 +1000163 drm_via_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000164 drm_via_mem_t *mem = data;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000165 int ret;
Dave Airlie22f579c2005-06-28 22:48:56 +1000166
Thomas Hellstromce65a442006-08-07 22:03:22 +1000167 mutex_lock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000168 ret = drm_sman_free_key(&dev_priv->sman, mem->index);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000169 mutex_unlock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000170 DRM_DEBUG("free = 0x%lx\n", mem->index);
Dave Airlie22f579c2005-06-28 22:48:56 +1000171
Thomas Hellstromce65a442006-08-07 22:03:22 +1000172 return ret;
Dave Airlie22f579c2005-06-28 22:48:56 +1000173}
174
Thomas Hellstromce65a442006-08-07 22:03:22 +1000175
Eric Anholt6c340ea2007-08-25 20:23:09 +1000176void via_reclaim_buffers_locked(struct drm_device * dev,
177 struct drm_file *file_priv)
Dave Airlie22f579c2005-06-28 22:48:56 +1000178{
Thomas Hellstromce65a442006-08-07 22:03:22 +1000179 drm_via_private_t *dev_priv = dev->dev_private;
Dave Airlie22f579c2005-06-28 22:48:56 +1000180
Thomas Hellstromce65a442006-08-07 22:03:22 +1000181 mutex_lock(&dev->struct_mutex);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000182 if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)file_priv)) {
Thomas Hellstromce65a442006-08-07 22:03:22 +1000183 mutex_unlock(&dev->struct_mutex);
184 return;
Dave Airlie22f579c2005-06-28 22:48:56 +1000185 }
186
Thomas Hellstromce65a442006-08-07 22:03:22 +1000187 if (dev->driver->dma_quiescent) {
188 dev->driver->dma_quiescent(dev);
Dave Airlie22f579c2005-06-28 22:48:56 +1000189 }
190
Eric Anholt6c340ea2007-08-25 20:23:09 +1000191 drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)file_priv);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000192 mutex_unlock(&dev->struct_mutex);
193 return;
Dave Airlie22f579c2005-06-28 22:48:56 +1000194}