1 /* drm_agpsupport.h -- DRM support for AGP/GART backend -*- linux-c -*-
2 * Created: Mon Dec 13 09:56:45 1999 by faith@precisioninsight.com
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
28 * Rickard E. (Rik) Faith <faith@valinux.com>
29 * Gareth Hughes <gareth@valinux.com>
32 #define __NO_VERSION__
34 #include <linux/module.h>
38 #if LINUX_VERSION_CODE < 0x020400
39 #include "agpsupport-pre24.h"
41 #define DRM_AGP_GET (drm_agp_t *)inter_module_get("drm_agp")
42 #define DRM_AGP_PUT inter_module_put("drm_agp")
45 static const drm_agp_t *drm_agp = NULL;
47 int DRM(agp_info)(struct inode *inode, struct file *filp,
48 unsigned int cmd, unsigned long arg)
50 drm_file_t *priv = filp->private_data;
51 drm_device_t *dev = priv->dev;
55 if (!dev->agp->acquired || !drm_agp->copy_info) return -EINVAL;
57 kern = &dev->agp->agp_info;
58 info.agp_version_major = kern->version.major;
59 info.agp_version_minor = kern->version.minor;
60 info.mode = kern->mode;
61 info.aperture_base = kern->aper_base;
62 info.aperture_size = kern->aper_size * 1024 * 1024;
63 info.memory_allowed = kern->max_memory << PAGE_SHIFT;
64 info.memory_used = kern->current_memory << PAGE_SHIFT;
65 info.id_vendor = kern->device->vendor;
66 info.id_device = kern->device->device;
68 if (copy_to_user((drm_agp_info_t *)arg, &info, sizeof(info)))
73 int DRM(agp_acquire)(struct inode *inode, struct file *filp,
74 unsigned int cmd, unsigned long arg)
76 drm_file_t *priv = filp->private_data;
77 drm_device_t *dev = priv->dev;
80 if (dev->agp->acquired || !drm_agp->acquire) return -EINVAL;
81 if ((retcode = drm_agp->acquire())) return retcode;
82 dev->agp->acquired = 1;
86 int DRM(agp_release)(struct inode *inode, struct file *filp,
87 unsigned int cmd, unsigned long arg)
89 drm_file_t *priv = filp->private_data;
90 drm_device_t *dev = priv->dev;
92 if (!dev->agp->acquired || !drm_agp->release) return -EINVAL;
94 dev->agp->acquired = 0;
99 void DRM(agp_do_release)(void)
101 if (drm_agp->release) drm_agp->release();
104 int DRM(agp_enable)(struct inode *inode, struct file *filp,
105 unsigned int cmd, unsigned long arg)
107 drm_file_t *priv = filp->private_data;
108 drm_device_t *dev = priv->dev;
111 if (!dev->agp->acquired || !drm_agp->enable) return -EINVAL;
113 if (copy_from_user(&mode, (drm_agp_mode_t *)arg, sizeof(mode)))
116 dev->agp->mode = mode.mode;
117 drm_agp->enable(mode.mode);
118 dev->agp->base = dev->agp->agp_info.aper_base;
119 dev->agp->enabled = 1;
123 int DRM(agp_alloc)(struct inode *inode, struct file *filp,
124 unsigned int cmd, unsigned long arg)
126 drm_file_t *priv = filp->private_data;
127 drm_device_t *dev = priv->dev;
128 drm_agp_buffer_t request;
129 drm_agp_mem_t *entry;
134 if (!dev->agp->acquired) return -EINVAL;
135 if (copy_from_user(&request, (drm_agp_buffer_t *)arg, sizeof(request)))
137 if (!(entry = DRM(alloc)(sizeof(*entry), DRM_MEM_AGPLISTS)))
140 memset(entry, 0, sizeof(*entry));
142 pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
143 type = (u32) request.type;
145 if (!(memory = DRM(alloc_agp)(pages, type))) {
146 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
150 entry->handle = (unsigned long)memory->memory;
151 entry->memory = memory;
153 entry->pages = pages;
155 entry->next = dev->agp->memory;
156 if (dev->agp->memory) dev->agp->memory->prev = entry;
157 dev->agp->memory = entry;
159 request.handle = entry->handle;
160 request.physical = memory->physical;
162 if (copy_to_user((drm_agp_buffer_t *)arg, &request, sizeof(request))) {
163 dev->agp->memory = entry->next;
164 dev->agp->memory->prev = NULL;
165 DRM(free_agp)(memory, pages);
166 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
172 static drm_agp_mem_t *DRM(agp_lookup_entry)(drm_device_t *dev,
173 unsigned long handle)
175 drm_agp_mem_t *entry;
177 for (entry = dev->agp->memory; entry; entry = entry->next) {
178 if (entry->handle == handle) return entry;
183 int DRM(agp_unbind)(struct inode *inode, struct file *filp,
184 unsigned int cmd, unsigned long arg)
186 drm_file_t *priv = filp->private_data;
187 drm_device_t *dev = priv->dev;
188 drm_agp_binding_t request;
189 drm_agp_mem_t *entry;
191 if (!dev->agp->acquired) return -EINVAL;
192 if (copy_from_user(&request, (drm_agp_binding_t *)arg, sizeof(request)))
194 if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
196 if (!entry->bound) return -EINVAL;
197 return DRM(unbind_agp)(entry->memory);
200 int DRM(agp_bind)(struct inode *inode, struct file *filp,
201 unsigned int cmd, unsigned long arg)
203 drm_file_t *priv = filp->private_data;
204 drm_device_t *dev = priv->dev;
205 drm_agp_binding_t request;
206 drm_agp_mem_t *entry;
210 if (!dev->agp->acquired || !drm_agp->bind_memory) return -EINVAL;
211 if (copy_from_user(&request, (drm_agp_binding_t *)arg, sizeof(request)))
213 if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
215 if (entry->bound) return -EINVAL;
216 page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
217 if ((retcode = DRM(bind_agp)(entry->memory, page))) return retcode;
218 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
219 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
220 dev->agp->base, entry->bound);
224 int DRM(agp_free)(struct inode *inode, struct file *filp,
225 unsigned int cmd, unsigned long arg)
227 drm_file_t *priv = filp->private_data;
228 drm_device_t *dev = priv->dev;
229 drm_agp_buffer_t request;
230 drm_agp_mem_t *entry;
232 if (!dev->agp->acquired) return -EINVAL;
233 if (copy_from_user(&request, (drm_agp_buffer_t *)arg, sizeof(request)))
235 if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
237 if (entry->bound) DRM(unbind_agp)(entry->memory);
239 if (entry->prev) entry->prev->next = entry->next;
240 else dev->agp->memory = entry->next;
241 if (entry->next) entry->next->prev = entry->prev;
242 DRM(free_agp)(entry->memory, entry->pages);
243 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
247 drm_agp_head_t *DRM(agp_init)(void)
249 drm_agp_head_t *head = NULL;
251 drm_agp = DRM_AGP_GET;
253 if (!(head = DRM(alloc)(sizeof(*head), DRM_MEM_AGPLISTS)))
255 memset((void *)head, 0, sizeof(*head));
256 drm_agp->copy_info(&head->agp_info);
257 if (head->agp_info.chipset == NOT_SUPPORTED) {
258 DRM(free)(head, sizeof(*head), DRM_MEM_AGPLISTS);
262 switch (head->agp_info.chipset) {
263 case INTEL_GENERIC: head->chipset = "Intel"; break;
264 case INTEL_LX: head->chipset = "Intel 440LX"; break;
265 case INTEL_BX: head->chipset = "Intel 440BX"; break;
266 case INTEL_GX: head->chipset = "Intel 440GX"; break;
267 case INTEL_I810: head->chipset = "Intel i810"; break;
269 #if LINUX_VERSION_CODE >= 0x020400
270 case INTEL_I840: head->chipset = "Intel i840"; break;
273 case VIA_GENERIC: head->chipset = "VIA"; break;
274 case VIA_VP3: head->chipset = "VIA VP3"; break;
275 case VIA_MVP3: head->chipset = "VIA MVP3"; break;
277 #if LINUX_VERSION_CODE >= 0x020400
278 case VIA_MVP4: head->chipset = "VIA MVP4"; break;
279 case VIA_APOLLO_KX133: head->chipset = "VIA Apollo KX133";
281 case VIA_APOLLO_KT133: head->chipset = "VIA Apollo KT133";
285 case VIA_APOLLO_PRO: head->chipset = "VIA Apollo Pro";
287 case SIS_GENERIC: head->chipset = "SiS"; break;
288 case AMD_GENERIC: head->chipset = "AMD"; break;
289 case AMD_IRONGATE: head->chipset = "AMD Irongate"; break;
290 case ALI_GENERIC: head->chipset = "ALi"; break;
291 case ALI_M1541: head->chipset = "ALi M1541"; break;
292 default: head->chipset = "Unknown"; break;
294 DRM_INFO("AGP %d.%d on %s @ 0x%08lx %ZuMB\n",
295 head->agp_info.version.major,
296 head->agp_info.version.minor,
298 head->agp_info.aper_base,
299 head->agp_info.aper_size);
304 void DRM(agp_uninit)(void)
310 agp_memory *DRM(agp_allocate_memory)(size_t pages, u32 type)
312 if (!drm_agp->allocate_memory) return NULL;
313 return drm_agp->allocate_memory(pages, type);
316 int DRM(agp_free_memory)(agp_memory *handle)
318 if (!handle || !drm_agp->free_memory) return 0;
319 drm_agp->free_memory(handle);
323 int DRM(agp_bind_memory)(agp_memory *handle, off_t start)
325 if (!handle || !drm_agp->bind_memory) return -EINVAL;
326 return drm_agp->bind_memory(handle, start);
329 int DRM(agp_unbind_memory)(agp_memory *handle)
331 if (!handle || !drm_agp->unbind_memory) return -EINVAL;
332 return drm_agp->unbind_memory(handle);
335 #endif /* __REALLY_HAVE_AGP */