blob: 9278891054834992ba30c5b9ac2a4f5b09403438 [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
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
29#include <drm/via_drm.h>
Dave Airlie22f579c2005-06-28 22:48:56 +100030#include "via_drv.h"
Dave Airlie22f579c2005-06-28 22:48:56 +100031
Thomas Hellstromce65a442006-08-07 22:03:22 +100032#define VIA_MM_ALIGN_SHIFT 4
Nicolas Kaiser58c1e852010-07-11 15:32:42 +020033#define VIA_MM_ALIGN_MASK ((1 << VIA_MM_ALIGN_SHIFT) - 1)
Dave Airlie22f579c2005-06-28 22:48:56 +100034
Daniel Vetter977b4f62011-10-26 22:21:13 +020035struct via_memblock {
36 struct drm_mm_node mm_node;
37 struct list_head owner_list;
38};
39
Eric Anholtc153f452007-09-03 12:06:45 +100040int via_agp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlie22f579c2005-06-28 22:48:56 +100041{
Eric Anholtc153f452007-09-03 12:06:45 +100042 drm_via_agp_t *agp = data;
Thomas Hellstromce65a442006-08-07 22:03:22 +100043 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
Dave Airlie22f579c2005-06-28 22:48:56 +100044
Thomas Hellstromce65a442006-08-07 22:03:22 +100045 mutex_lock(&dev->struct_mutex);
Daniel Vetter977b4f62011-10-26 22:21:13 +020046 drm_mm_init(&dev_priv->agp_mm, 0, agp->size >> VIA_MM_ALIGN_SHIFT);
Dave Airlie22f579c2005-06-28 22:48:56 +100047
Dave Airlied40c8532006-08-19 17:40:50 +100048 dev_priv->agp_initialized = 1;
Eric Anholtc153f452007-09-03 12:06:45 +100049 dev_priv->agp_offset = agp->offset;
Thomas Hellstromce65a442006-08-07 22:03:22 +100050 mutex_unlock(&dev->struct_mutex);
Dave Airlie22f579c2005-06-28 22:48:56 +100051
Márton Németh3e684ea2008-01-24 15:58:57 +100052 DRM_DEBUG("offset = %u, size = %u\n", agp->offset, agp->size);
Dave Airlie22f579c2005-06-28 22:48:56 +100053 return 0;
54}
55
Eric Anholtc153f452007-09-03 12:06:45 +100056int via_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlie22f579c2005-06-28 22:48:56 +100057{
Eric Anholtc153f452007-09-03 12:06:45 +100058 drm_via_fb_t *fb = data;
Thomas Hellstromce65a442006-08-07 22:03:22 +100059 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
Dave Airlie22f579c2005-06-28 22:48:56 +100060
Thomas Hellstromce65a442006-08-07 22:03:22 +100061 mutex_lock(&dev->struct_mutex);
Daniel Vetter977b4f62011-10-26 22:21:13 +020062 drm_mm_init(&dev_priv->vram_mm, 0, fb->size >> VIA_MM_ALIGN_SHIFT);
Dave Airlie22f579c2005-06-28 22:48:56 +100063
Dave Airlied40c8532006-08-19 17:40:50 +100064 dev_priv->vram_initialized = 1;
Eric Anholtc153f452007-09-03 12:06:45 +100065 dev_priv->vram_offset = fb->offset;
Thomas Hellstromce65a442006-08-07 22:03:22 +100066
67 mutex_unlock(&dev->struct_mutex);
Márton Németh3e684ea2008-01-24 15:58:57 +100068 DRM_DEBUG("offset = %u, size = %u\n", fb->offset, fb->size);
Thomas Hellstromce65a442006-08-07 22:03:22 +100069
70 return 0;
71
Dave Airlie22f579c2005-06-28 22:48:56 +100072}
73
74int via_final_context(struct drm_device *dev, int context)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100075{
Dave Airlie22f579c2005-06-28 22:48:56 +100076 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
77
Dave Airlieb5e89ed2005-09-25 14:28:13 +100078 via_release_futex(dev_priv, context);
79
Dave Airlie22f579c2005-06-28 22:48:56 +100080 /* Linux specific until context tracking code gets ported to BSD */
81 /* Last context, perform cleanup */
Ville Syrjäläfc6ff192013-10-04 14:53:39 +030082 if (list_is_singular(&dev->ctxlist) && dev->dev_private) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100083 DRM_DEBUG("Last Context\n");
Jesse Barnes9bfbd5c2008-09-15 15:00:33 -070084 drm_irq_uninstall(dev);
Dave Airlie22f579c2005-06-28 22:48:56 +100085 via_cleanup_futex(dev_priv);
86 via_do_cleanup_map(dev);
87 }
Dave Airlie22f579c2005-06-28 22:48:56 +100088 return 1;
89}
90
Thomas Hellstromce65a442006-08-07 22:03:22 +100091void via_lastclose(struct drm_device *dev)
92{
93 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
94
95 if (!dev_priv)
96 return;
97
98 mutex_lock(&dev->struct_mutex);
Daniel Vetter977b4f62011-10-26 22:21:13 +020099 if (dev_priv->vram_initialized) {
100 drm_mm_takedown(&dev_priv->vram_mm);
101 dev_priv->vram_initialized = 0;
102 }
103 if (dev_priv->agp_initialized) {
104 drm_mm_takedown(&dev_priv->agp_mm);
105 dev_priv->agp_initialized = 0;
106 }
Thomas Hellstromce65a442006-08-07 22:03:22 +1000107 mutex_unlock(&dev->struct_mutex);
Dave Airliebc5f4522007-11-05 12:50:58 +1000108}
Thomas Hellstromce65a442006-08-07 22:03:22 +1000109
Eric Anholtc153f452007-09-03 12:06:45 +1000110int via_mem_alloc(struct drm_device *dev, void *data,
Daniel Vetterc828e202011-10-25 16:32:45 +0200111 struct drm_file *file)
Dave Airlie22f579c2005-06-28 22:48:56 +1000112{
Eric Anholtc153f452007-09-03 12:06:45 +1000113 drm_via_mem_t *mem = data;
Daniel Vetter77ee8f32011-10-25 17:55:31 +0200114 int retval = 0, user_key;
Daniel Vetter977b4f62011-10-26 22:21:13 +0200115 struct via_memblock *item;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000116 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
Daniel Vetterc828e202011-10-25 16:32:45 +0200117 struct via_file_private *file_priv = file->driver_priv;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000118 unsigned long tmpSize;
Dave Airlie22f579c2005-06-28 22:48:56 +1000119
Eric Anholtc153f452007-09-03 12:06:45 +1000120 if (mem->type > VIA_MEM_AGP) {
Thomas Hellstromce65a442006-08-07 22:03:22 +1000121 DRM_ERROR("Unknown memory type allocation\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000122 return -EINVAL;
Thomas Hellstromce65a442006-08-07 22:03:22 +1000123 }
124 mutex_lock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000125 if (0 == ((mem->type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized :
Thomas Hellstromce65a442006-08-07 22:03:22 +1000126 dev_priv->agp_initialized)) {
127 DRM_ERROR
128 ("Attempt to allocate from uninitialized memory manager.\n");
129 mutex_unlock(&dev->struct_mutex);
Eric Anholt20caafa2007-08-25 19:22:43 +1000130 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000131 }
132
Daniel Vetter977b4f62011-10-26 22:21:13 +0200133 item = kzalloc(sizeof(*item), GFP_KERNEL);
Daniel Vetter77ee8f32011-10-25 17:55:31 +0200134 if (!item) {
Eric Anholt20caafa2007-08-25 19:22:43 +1000135 retval = -ENOMEM;
Daniel Vetter77ee8f32011-10-25 17:55:31 +0200136 goto fail_alloc;
Dave Airlie22f579c2005-06-28 22:48:56 +1000137 }
Daniel Vetter77ee8f32011-10-25 17:55:31 +0200138
Daniel Vetter977b4f62011-10-26 22:21:13 +0200139 tmpSize = (mem->size + VIA_MM_ALIGN_MASK) >> VIA_MM_ALIGN_SHIFT;
140 if (mem->type == VIA_MEM_AGP)
141 retval = drm_mm_insert_node(&dev_priv->agp_mm,
142 &item->mm_node,
David Herrmann31e5d7c2013-07-27 13:36:27 +0200143 tmpSize, 0, DRM_MM_SEARCH_DEFAULT);
Daniel Vetter977b4f62011-10-26 22:21:13 +0200144 else
145 retval = drm_mm_insert_node(&dev_priv->vram_mm,
146 &item->mm_node,
David Herrmann31e5d7c2013-07-27 13:36:27 +0200147 tmpSize, 0, DRM_MM_SEARCH_DEFAULT);
Daniel Vetter977b4f62011-10-26 22:21:13 +0200148 if (retval)
149 goto fail_alloc;
150
Tejun Heo36888db2013-02-27 17:04:12 -0800151 retval = idr_alloc(&dev_priv->object_idr, item, 1, 0, GFP_KERNEL);
152 if (retval < 0)
Daniel Vetter77ee8f32011-10-25 17:55:31 +0200153 goto fail_idr;
Tejun Heo36888db2013-02-27 17:04:12 -0800154 user_key = retval;
Daniel Vetter77ee8f32011-10-25 17:55:31 +0200155
156 list_add(&item->owner_list, &file_priv->obj_list);
Daniel Vetterc828e202011-10-25 16:32:45 +0200157 mutex_unlock(&dev->struct_mutex);
Dave Airlie22f579c2005-06-28 22:48:56 +1000158
Daniel Vetter77ee8f32011-10-25 17:55:31 +0200159 mem->offset = ((mem->type == VIA_MEM_VIDEO) ?
160 dev_priv->vram_offset : dev_priv->agp_offset) +
Daniel Vetter977b4f62011-10-26 22:21:13 +0200161 ((item->mm_node.start) << VIA_MM_ALIGN_SHIFT);
Daniel Vetter77ee8f32011-10-25 17:55:31 +0200162 mem->index = user_key;
163
164 return 0;
165
166fail_idr:
Daniel Vetter977b4f62011-10-26 22:21:13 +0200167 drm_mm_remove_node(&item->mm_node);
Daniel Vetter77ee8f32011-10-25 17:55:31 +0200168fail_alloc:
Daniel Vetter977b4f62011-10-26 22:21:13 +0200169 kfree(item);
Daniel Vetter77ee8f32011-10-25 17:55:31 +0200170 mutex_unlock(&dev->struct_mutex);
171
172 mem->offset = 0;
173 mem->size = 0;
174 mem->index = 0;
175 DRM_DEBUG("Video memory allocation failed\n");
176
Dave Airlie22f579c2005-06-28 22:48:56 +1000177 return retval;
178}
179
Eric Anholtc153f452007-09-03 12:06:45 +1000180int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlie22f579c2005-06-28 22:48:56 +1000181{
Thomas Hellstromce65a442006-08-07 22:03:22 +1000182 drm_via_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000183 drm_via_mem_t *mem = data;
Daniel Vetter977b4f62011-10-26 22:21:13 +0200184 struct via_memblock *obj;
Dave Airlie22f579c2005-06-28 22:48:56 +1000185
Thomas Hellstromce65a442006-08-07 22:03:22 +1000186 mutex_lock(&dev->struct_mutex);
Daniel Vetter77ee8f32011-10-25 17:55:31 +0200187 obj = idr_find(&dev_priv->object_idr, mem->index);
188 if (obj == NULL) {
189 mutex_unlock(&dev->struct_mutex);
190 return -EINVAL;
191 }
192
193 idr_remove(&dev_priv->object_idr, mem->index);
Daniel Vetter977b4f62011-10-26 22:21:13 +0200194 list_del(&obj->owner_list);
195 drm_mm_remove_node(&obj->mm_node);
196 kfree(obj);
Thomas Hellstromce65a442006-08-07 22:03:22 +1000197 mutex_unlock(&dev->struct_mutex);
Daniel Vetter77ee8f32011-10-25 17:55:31 +0200198
Eric Anholtc153f452007-09-03 12:06:45 +1000199 DRM_DEBUG("free = 0x%lx\n", mem->index);
Dave Airlie22f579c2005-06-28 22:48:56 +1000200
Daniel Vetterb5215ef2012-01-08 22:42:27 +0100201 return 0;
Dave Airlie22f579c2005-06-28 22:48:56 +1000202}
203
Thomas Hellstromce65a442006-08-07 22:03:22 +1000204
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200205void via_reclaim_buffers_locked(struct drm_device *dev,
Daniel Vetterc828e202011-10-25 16:32:45 +0200206 struct drm_file *file)
Dave Airlie22f579c2005-06-28 22:48:56 +1000207{
Daniel Vetterc828e202011-10-25 16:32:45 +0200208 struct via_file_private *file_priv = file->driver_priv;
Daniel Vetter977b4f62011-10-26 22:21:13 +0200209 struct via_memblock *entry, *next;
Dave Airlie22f579c2005-06-28 22:48:56 +1000210
Daniel Vetter834859c2011-10-25 23:37:09 +0200211 if (!(file->minor->master && file->master->lock.hw_lock))
212 return;
213
214 drm_idlelock_take(&file->master->lock);
215
Thomas Hellstromce65a442006-08-07 22:03:22 +1000216 mutex_lock(&dev->struct_mutex);
Daniel Vetterc828e202011-10-25 16:32:45 +0200217 if (list_empty(&file_priv->obj_list)) {
Thomas Hellstromce65a442006-08-07 22:03:22 +1000218 mutex_unlock(&dev->struct_mutex);
Daniel Vetter834859c2011-10-25 23:37:09 +0200219 drm_idlelock_release(&file->master->lock);
220
Thomas Hellstromce65a442006-08-07 22:03:22 +1000221 return;
Dave Airlie22f579c2005-06-28 22:48:56 +1000222 }
223
Daniel Vetter834859c2011-10-25 23:37:09 +0200224 via_driver_dma_quiescent(dev);
Dave Airlie22f579c2005-06-28 22:48:56 +1000225
Daniel Vetterc828e202011-10-25 16:32:45 +0200226 list_for_each_entry_safe(entry, next, &file_priv->obj_list,
227 owner_list) {
Daniel Vetter977b4f62011-10-26 22:21:13 +0200228 list_del(&entry->owner_list);
229 drm_mm_remove_node(&entry->mm_node);
230 kfree(entry);
Daniel Vetterc828e202011-10-25 16:32:45 +0200231 }
Thomas Hellstromce65a442006-08-07 22:03:22 +1000232 mutex_unlock(&dev->struct_mutex);
Daniel Vetter834859c2011-10-25 23:37:09 +0200233
234 drm_idlelock_release(&file->master->lock);
235
Thomas Hellstromce65a442006-08-07 22:03:22 +1000236 return;
Dave Airlie22f579c2005-06-28 22:48:56 +1000237}