1 /* agpsupport.c -- 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.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * Author: Rickard E. (Rik) Faith <faith@precisioninsight.com>
28 * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/kernel/agpsupport.c,v 1.1 2000/02/11 17:26:02 dawes Exp $
32 #define __NO_VERSION__
35 drm_agp_func_t drm_agp = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
37 /* The C standard says that 'void *' is not guaranteed to hold a function
38 pointer, so we use this union to define a generic pointer that is
39 guaranteed to hold any of the function pointers we care about. */
41 void (*free_memory)(agp_memory *);
42 agp_memory *(*allocate_memory)(size_t, u32);
43 int (*bind_memory)(agp_memory *, off_t);
44 int (*unbind_memory)(agp_memory *);
47 void (*release)(void);
48 void (*copy_info)(agp_kern_info *);
49 unsigned long address;
52 typedef struct drm_agp_fill {
57 static drm_agp_fill_t drm_agp_fill[] = {
58 { __MODULE_STRING(agp_free_memory),
59 (drm_agp_func_u *)&drm_agp.free_memory },
60 { __MODULE_STRING(agp_allocate_memory),
61 (drm_agp_func_u *)&drm_agp.allocate_memory },
62 { __MODULE_STRING(agp_bind_memory),
63 (drm_agp_func_u *)&drm_agp.bind_memory },
64 { __MODULE_STRING(agp_unbind_memory),
65 (drm_agp_func_u *)&drm_agp.unbind_memory },
66 { __MODULE_STRING(agp_enable),
67 (drm_agp_func_u *)&drm_agp.enable },
68 { __MODULE_STRING(agp_backend_acquire),
69 (drm_agp_func_u *)&drm_agp.acquire },
70 { __MODULE_STRING(agp_backend_release),
71 (drm_agp_func_u *)&drm_agp.release },
72 { __MODULE_STRING(agp_copy_info),
73 (drm_agp_func_u *)&drm_agp.copy_info },
77 int drm_agp_info(struct inode *inode, struct file *filp, unsigned int cmd,
80 drm_file_t *priv = filp->private_data;
81 drm_device_t *dev = priv->dev;
85 if (!dev->agp->acquired || !drm_agp.copy_info) return -EINVAL;
87 kern = &dev->agp->agp_info;
88 info.agp_version_major = kern->version.major;
89 info.agp_version_minor = kern->version.minor;
90 info.mode = kern->mode;
91 info.aperture_base = kern->aper_base;
92 info.aperture_size = kern->aper_size * 1024 * 1024;
93 info.memory_allowed = kern->max_memory << PAGE_SHIFT;
94 info.memory_used = kern->current_memory << PAGE_SHIFT;
95 info.id_vendor = kern->device->vendor;
96 info.id_device = kern->device->device;
98 copy_to_user_ret((drm_agp_info_t *)arg, &info, sizeof(info), -EFAULT);
102 int drm_agp_acquire(struct inode *inode, struct file *filp, unsigned int cmd,
105 drm_file_t *priv = filp->private_data;
106 drm_device_t *dev = priv->dev;
109 if (dev->agp->acquired || !drm_agp.acquire) return -EINVAL;
110 if ((retcode = (*drm_agp.acquire)())) return retcode;
111 dev->agp->acquired = 1;
115 int drm_agp_release(struct inode *inode, struct file *filp, unsigned int cmd,
118 drm_file_t *priv = filp->private_data;
119 drm_device_t *dev = priv->dev;
121 if (!dev->agp->acquired || !drm_agp.release) return -EINVAL;
122 (*drm_agp.release)();
123 dev->agp->acquired = 0;
128 int drm_agp_enable(struct inode *inode, struct file *filp, unsigned int cmd,
131 drm_file_t *priv = filp->private_data;
132 drm_device_t *dev = priv->dev;
135 if (!dev->agp->acquired || !drm_agp.enable) return -EINVAL;
137 copy_from_user_ret(&mode, (drm_agp_mode_t *)arg, sizeof(mode),
140 dev->agp->mode = mode.mode;
141 (*drm_agp.enable)(mode.mode);
142 dev->agp->base = dev->agp->agp_info.aper_base;
143 dev->agp->enabled = 1;
147 int drm_agp_alloc(struct inode *inode, struct file *filp, unsigned int cmd,
150 drm_file_t *priv = filp->private_data;
151 drm_device_t *dev = priv->dev;
152 drm_agp_buffer_t request;
153 drm_agp_mem_t *entry;
157 if (!dev->agp->acquired) return -EINVAL;
158 copy_from_user_ret(&request, (drm_agp_buffer_t *)arg, sizeof(request),
160 if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
163 memset(entry, 0, sizeof(*entry));
165 pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
166 type = (u32) request.type;
168 if (!(memory = drm_alloc_agp(pages, type))) {
169 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
173 entry->handle = (unsigned long)memory->memory;
174 entry->memory = memory;
176 entry->pages = pages;
178 entry->next = dev->agp->memory;
179 if (dev->agp->memory) dev->agp->memory->prev = entry;
180 dev->agp->memory = entry;
182 request.handle = entry->handle;
183 request.physical = memory->physical;
185 if (copy_to_user((drm_agp_buffer_t *)arg, &request, sizeof(request))) {
186 dev->agp->memory = entry->next;
187 dev->agp->memory->prev = NULL;
188 drm_free_agp(memory, pages);
189 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
195 static drm_agp_mem_t *drm_agp_lookup_entry(drm_device_t *dev,
196 unsigned long handle)
198 drm_agp_mem_t *entry;
200 for (entry = dev->agp->memory; entry; entry = entry->next) {
201 if (entry->handle == handle) return entry;
206 int drm_agp_unbind(struct inode *inode, struct file *filp, unsigned int cmd,
209 drm_file_t *priv = filp->private_data;
210 drm_device_t *dev = priv->dev;
211 drm_agp_binding_t request;
212 drm_agp_mem_t *entry;
214 if (!dev->agp->acquired) return -EINVAL;
215 copy_from_user_ret(&request, (drm_agp_binding_t *)arg, sizeof(request),
217 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
219 if (!entry->bound) return -EINVAL;
220 return drm_unbind_agp(entry->memory);
223 int drm_agp_bind(struct inode *inode, struct file *filp, unsigned int cmd,
226 drm_file_t *priv = filp->private_data;
227 drm_device_t *dev = priv->dev;
228 drm_agp_binding_t request;
229 drm_agp_mem_t *entry;
233 if (!dev->agp->acquired || !drm_agp.bind_memory) return -EINVAL;
234 copy_from_user_ret(&request, (drm_agp_binding_t *)arg, sizeof(request),
236 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
238 if (entry->bound) return -EINVAL;
239 page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
240 if ((retcode = drm_bind_agp(entry->memory, page))) return retcode;
241 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
245 int drm_agp_free(struct inode *inode, struct file *filp, unsigned int cmd,
248 drm_file_t *priv = filp->private_data;
249 drm_device_t *dev = priv->dev;
250 drm_agp_buffer_t request;
251 drm_agp_mem_t *entry;
253 if (!dev->agp->acquired) return -EINVAL;
254 copy_from_user_ret(&request, (drm_agp_buffer_t *)arg, sizeof(request),
256 if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
258 if (entry->bound) drm_unbind_agp(entry->memory);
260 if (entry->prev) entry->prev->next = entry->next;
261 else dev->agp->memory = entry->next;
262 if (entry->next) entry->next->prev = entry->prev;
263 drm_free_agp(entry->memory, entry->pages);
264 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
268 drm_agp_head_t *drm_agp_init(void)
270 drm_agp_fill_t *fill;
271 drm_agp_head_t *head = NULL;
272 int agp_available = 1;
274 for (fill = &drm_agp_fill[0]; fill->name; fill++) {
275 char *n = (char *)fill->name;
276 *fill->f = (drm_agp_func_u)get_module_symbol(NULL, n);
277 DRM_DEBUG("%s resolves to 0x%08lx\n", n, (*fill->f).address);
278 if (!(*fill->f).address) agp_available = 0;
281 DRM_DEBUG("agp_available = %d\n", agp_available);
284 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
286 memset((void *)head, 0, sizeof(*head));
287 (*drm_agp.copy_info)(&head->agp_info);
289 switch (head->agp_info.chipset) {
290 case INTEL_GENERIC: head->chipset = "Intel"; break;
291 case INTEL_LX: head->chipset = "Intel 440LX"; break;
292 case INTEL_BX: head->chipset = "Intel 440BX"; break;
293 case INTEL_GX: head->chipset = "Intel 440GX"; break;
294 case INTEL_I810: head->chipset = "Intel i810"; break;
295 case VIA_GENERIC: head->chipset = "VIA"; break;
296 case VIA_VP3: head->chipset = "VIA VP3"; break;
297 case VIA_MVP3: head->chipset = "VIA MVP3"; break;
298 case VIA_APOLLO_PRO: head->chipset = "VIA Apollo Pro"; break;
299 case SIS_GENERIC: head->chipset = "SiS"; break;
300 case AMD_GENERIC: head->chipset = "AMD"; break;
301 case AMD_IRONGATE: head->chipset = "AMD Irongate"; break;
302 case ALI_GENERIC: head->chipset = "ALi"; break;
303 case ALI_M1541: head->chipset = "ALi M1541"; break;
306 DRM_INFO("AGP %d.%d on %s @ 0x%08lx %dMB\n",
307 head->agp_info.version.major,
308 head->agp_info.version.minor,
310 head->agp_info.aper_base,
311 head->agp_info.aper_size);