Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 1 | /* |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 2 | * Copyright 2006 Tungsten Graphics Inc., Bismarck, ND., USA. |
| 3 | * All rights reserved. |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 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 |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 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, |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
| 23 | */ |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 24 | /* |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 25 | * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com> |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 26 | */ |
| 27 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 28 | #include "drmP.h" |
| 29 | #include "via_drm.h" |
| 30 | #include "via_drv.h" |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 31 | #include "drm_sman.h" |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 32 | |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 33 | #define VIA_MM_ALIGN_SHIFT 4 |
Nicolas Kaiser | 58c1e85 | 2010-07-11 15:32:42 +0200 | [diff] [blame] | 34 | #define VIA_MM_ALIGN_MASK ((1 << VIA_MM_ALIGN_SHIFT) - 1) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 35 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 36 | int via_agp_init(struct drm_device *dev, void *data, struct drm_file *file_priv) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 37 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 38 | drm_via_agp_t *agp = data; |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 39 | drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private; |
| 40 | int ret; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 41 | |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 42 | mutex_lock(&dev->struct_mutex); |
| 43 | ret = drm_sman_set_range(&dev_priv->sman, VIA_MEM_AGP, 0, |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 44 | agp->size >> VIA_MM_ALIGN_SHIFT); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 45 | |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 46 | if (ret) { |
| 47 | DRM_ERROR("AGP memory manager initialisation error\n"); |
| 48 | mutex_unlock(&dev->struct_mutex); |
| 49 | return ret; |
| 50 | } |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 51 | |
Dave Airlie | d40c853 | 2006-08-19 17:40:50 +1000 | [diff] [blame] | 52 | dev_priv->agp_initialized = 1; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 53 | dev_priv->agp_offset = agp->offset; |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 54 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 55 | |
Márton Németh | 3e684ea | 2008-01-24 15:58:57 +1000 | [diff] [blame] | 56 | DRM_DEBUG("offset = %u, size = %u\n", agp->offset, agp->size); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 60 | int via_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 61 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 62 | drm_via_fb_t *fb = data; |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 63 | drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private; |
| 64 | int ret; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 65 | |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 66 | mutex_lock(&dev->struct_mutex); |
| 67 | ret = drm_sman_set_range(&dev_priv->sman, VIA_MEM_VIDEO, 0, |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 68 | fb->size >> VIA_MM_ALIGN_SHIFT); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 69 | |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 70 | if (ret) { |
| 71 | DRM_ERROR("VRAM memory manager initialisation error\n"); |
| 72 | mutex_unlock(&dev->struct_mutex); |
| 73 | return ret; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 74 | } |
| 75 | |
Dave Airlie | d40c853 | 2006-08-19 17:40:50 +1000 | [diff] [blame] | 76 | dev_priv->vram_initialized = 1; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 77 | dev_priv->vram_offset = fb->offset; |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 78 | |
| 79 | mutex_unlock(&dev->struct_mutex); |
Márton Németh | 3e684ea | 2008-01-24 15:58:57 +1000 | [diff] [blame] | 80 | DRM_DEBUG("offset = %u, size = %u\n", fb->offset, fb->size); |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 81 | |
| 82 | return 0; |
| 83 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | int via_final_context(struct drm_device *dev, int context) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 87 | { |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 88 | drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private; |
| 89 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 90 | via_release_futex(dev_priv, context); |
| 91 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 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) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 95 | DRM_DEBUG("Last Context\n"); |
Jesse Barnes | 9bfbd5c | 2008-09-15 15:00:33 -0700 | [diff] [blame] | 96 | drm_irq_uninstall(dev); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 97 | via_cleanup_futex(dev_priv); |
| 98 | via_do_cleanup_map(dev); |
| 99 | } |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 100 | return 1; |
| 101 | } |
| 102 | |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 103 | void 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 Morton | a1d0fcf | 2006-08-14 11:35:15 +1000 | [diff] [blame] | 112 | dev_priv->vram_initialized = 0; |
| 113 | dev_priv->agp_initialized = 0; |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 114 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | bc5f452 | 2007-11-05 12:50:58 +1000 | [diff] [blame] | 115 | } |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 116 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 117 | int via_mem_alloc(struct drm_device *dev, void *data, |
| 118 | struct drm_file *file_priv) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 119 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 120 | drm_via_mem_t *mem = data; |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 121 | int retval = 0; |
Dave Airlie | 9698b4d | 2007-07-12 10:21:05 +1000 | [diff] [blame] | 122 | struct drm_memblock_item *item; |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 123 | drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private; |
| 124 | unsigned long tmpSize; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 125 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 126 | if (mem->type > VIA_MEM_AGP) { |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 127 | DRM_ERROR("Unknown memory type allocation\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 128 | return -EINVAL; |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 129 | } |
| 130 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 131 | if (0 == ((mem->type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized : |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 132 | dev_priv->agp_initialized)) { |
| 133 | DRM_ERROR |
| 134 | ("Attempt to allocate from uninitialized memory manager.\n"); |
| 135 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 136 | return -EINVAL; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 137 | } |
| 138 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 139 | tmpSize = (mem->size + VIA_MM_ALIGN_MASK) >> VIA_MM_ALIGN_SHIFT; |
| 140 | item = drm_sman_alloc(&dev_priv->sman, mem->type, tmpSize, 0, |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 141 | (unsigned long)file_priv); |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 142 | mutex_unlock(&dev->struct_mutex); |
| 143 | if (item) { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 144 | mem->offset = ((mem->type == VIA_MEM_VIDEO) ? |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 145 | dev_priv->vram_offset : dev_priv->agp_offset) + |
| 146 | (item->mm-> |
| 147 | offset(item->mm, item->mm_info) << VIA_MM_ALIGN_SHIFT); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 148 | mem->index = item->user_hash.key; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 149 | } else { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 150 | mem->offset = 0; |
| 151 | mem->size = 0; |
| 152 | mem->index = 0; |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 153 | DRM_DEBUG("Video memory allocation failed\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 154 | retval = -ENOMEM; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 155 | } |
| 156 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 157 | return retval; |
| 158 | } |
| 159 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 160 | int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 161 | { |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 162 | drm_via_private_t *dev_priv = dev->dev_private; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 163 | drm_via_mem_t *mem = data; |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 164 | int ret; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 165 | |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 166 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 167 | ret = drm_sman_free_key(&dev_priv->sman, mem->index); |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 168 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 169 | DRM_DEBUG("free = 0x%lx\n", mem->index); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 170 | |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 171 | return ret; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 172 | } |
| 173 | |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 174 | |
Nicolas Kaiser | 58c1e85 | 2010-07-11 15:32:42 +0200 | [diff] [blame] | 175 | void via_reclaim_buffers_locked(struct drm_device *dev, |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 176 | struct drm_file *file_priv) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 177 | { |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 178 | drm_via_private_t *dev_priv = dev->dev_private; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 179 | |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 180 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 181 | if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)file_priv)) { |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 182 | mutex_unlock(&dev->struct_mutex); |
| 183 | return; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 184 | } |
| 185 | |
Nicolas Kaiser | 58c1e85 | 2010-07-11 15:32:42 +0200 | [diff] [blame] | 186 | if (dev->driver->dma_quiescent) |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 187 | dev->driver->dma_quiescent(dev); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 188 | |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 189 | drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)file_priv); |
Thomas Hellstrom | ce65a44 | 2006-08-07 22:03:22 +1000 | [diff] [blame] | 190 | mutex_unlock(&dev->struct_mutex); |
| 191 | return; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 192 | } |