blob: 6cc2dadae3ef06acb3e94ff60da0d968470ac431 [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
Nicolas Kaiser58c1e852010-07-11 15:32:42 +020034#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
Márton Németh3e684ea2008-01-24 15:58:57 +100056 DRM_DEBUG("offset = %u, size = %u\n", 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);
Márton Németh3e684ea2008-01-24 15:58:57 +100080 DRM_DEBUG("offset = %u, size = %u\n", 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");
Jesse Barnes9bfbd5c2008-09-15 15:00:33 -070096 drm_irq_uninstall(dev);
Dave Airlie22f579c2005-06-28 22:48:56 +100097 via_cleanup_futex(dev_priv);
98 via_do_cleanup_map(dev);
99 }
Dave Airlie22f579c2005-06-28 22:48:56 +1000100 return 1;
101}
102
Thomas Hellstromce65a442006-08-07 22:03:22 +1000103void 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);
Andrew Mortona1d0fcf2006-08-14 11:35:15 +1000112 dev_priv->vram_initialized = 0;
113 dev_priv->agp_initialized = 0;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000114 mutex_unlock(&dev->struct_mutex);
Dave Airliebc5f4522007-11-05 12:50:58 +1000115}
Thomas Hellstromce65a442006-08-07 22:03:22 +1000116
Eric Anholtc153f452007-09-03 12:06:45 +1000117int via_mem_alloc(struct drm_device *dev, void *data,
118 struct drm_file *file_priv)
Dave Airlie22f579c2005-06-28 22:48:56 +1000119{
Eric Anholtc153f452007-09-03 12:06:45 +1000120 drm_via_mem_t *mem = data;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000121 int retval = 0;
Dave Airlie9698b4d2007-07-12 10:21:05 +1000122 struct drm_memblock_item *item;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000123 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
124 unsigned long tmpSize;
Dave Airlie22f579c2005-06-28 22:48:56 +1000125
Eric Anholtc153f452007-09-03 12:06:45 +1000126 if (mem->type > VIA_MEM_AGP) {
Thomas Hellstromce65a442006-08-07 22:03:22 +1000127 DRM_ERROR("Unknown memory type allocation\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000128 return -EINVAL;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000129 }
130 mutex_lock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000131 if (0 == ((mem->type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized :
Thomas Hellstromce65a442006-08-07 22:03:22 +1000132 dev_priv->agp_initialized)) {
133 DRM_ERROR
134 ("Attempt to allocate from uninitialized memory manager.\n");
135 mutex_unlock(&dev->struct_mutex);
Eric Anholt20caafa2007-08-25 19:22:43 +1000136 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000137 }
138
Eric Anholtc153f452007-09-03 12:06:45 +1000139 tmpSize = (mem->size + VIA_MM_ALIGN_MASK) >> VIA_MM_ALIGN_SHIFT;
140 item = drm_sman_alloc(&dev_priv->sman, mem->type, tmpSize, 0,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000141 (unsigned long)file_priv);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000142 mutex_unlock(&dev->struct_mutex);
143 if (item) {
Eric Anholtc153f452007-09-03 12:06:45 +1000144 mem->offset = ((mem->type == VIA_MEM_VIDEO) ?
Thomas Hellstromce65a442006-08-07 22:03:22 +1000145 dev_priv->vram_offset : dev_priv->agp_offset) +
146 (item->mm->
147 offset(item->mm, item->mm_info) << VIA_MM_ALIGN_SHIFT);
Eric Anholtc153f452007-09-03 12:06:45 +1000148 mem->index = item->user_hash.key;
Dave Airlie22f579c2005-06-28 22:48:56 +1000149 } else {
Eric Anholtc153f452007-09-03 12:06:45 +1000150 mem->offset = 0;
151 mem->size = 0;
152 mem->index = 0;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000153 DRM_DEBUG("Video memory allocation failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000154 retval = -ENOMEM;
Dave Airlie22f579c2005-06-28 22:48:56 +1000155 }
156
Dave Airlie22f579c2005-06-28 22:48:56 +1000157 return retval;
158}
159
Eric Anholtc153f452007-09-03 12:06:45 +1000160int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlie22f579c2005-06-28 22:48:56 +1000161{
Thomas Hellstromce65a442006-08-07 22:03:22 +1000162 drm_via_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000163 drm_via_mem_t *mem = data;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000164 int ret;
Dave Airlie22f579c2005-06-28 22:48:56 +1000165
Thomas Hellstromce65a442006-08-07 22:03:22 +1000166 mutex_lock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000167 ret = drm_sman_free_key(&dev_priv->sman, mem->index);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000168 mutex_unlock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000169 DRM_DEBUG("free = 0x%lx\n", mem->index);
Dave Airlie22f579c2005-06-28 22:48:56 +1000170
Thomas Hellstromce65a442006-08-07 22:03:22 +1000171 return ret;
Dave Airlie22f579c2005-06-28 22:48:56 +1000172}
173
Thomas Hellstromce65a442006-08-07 22:03:22 +1000174
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200175void via_reclaim_buffers_locked(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000176 struct drm_file *file_priv)
Dave Airlie22f579c2005-06-28 22:48:56 +1000177{
Thomas Hellstromce65a442006-08-07 22:03:22 +1000178 drm_via_private_t *dev_priv = dev->dev_private;
Dave Airlie22f579c2005-06-28 22:48:56 +1000179
Thomas Hellstromce65a442006-08-07 22:03:22 +1000180 mutex_lock(&dev->struct_mutex);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000181 if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)file_priv)) {
Thomas Hellstromce65a442006-08-07 22:03:22 +1000182 mutex_unlock(&dev->struct_mutex);
183 return;
Dave Airlie22f579c2005-06-28 22:48:56 +1000184 }
185
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200186 if (dev->driver->dma_quiescent)
Thomas Hellstromce65a442006-08-07 22:03:22 +1000187 dev->driver->dma_quiescent(dev);
Dave Airlie22f579c2005-06-28 22:48:56 +1000188
Eric Anholt6c340ea2007-08-25 20:23:09 +1000189 drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)file_priv);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000190 mutex_unlock(&dev->struct_mutex);
191 return;
Dave Airlie22f579c2005-06-28 22:48:56 +1000192}