Import of XFree86 3.9.18
[gstreamer-omap:libdrm.git] / linux / agpsupport.c
1 /* agpsupport.c -- DRM support for AGP/GART backend -*- linux-c -*-
2  * Created: Mon Dec 13 09:56:45 1999 by faith@precisioninsight.com
3  *
4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5  * All Rights Reserved.
6  *
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:
13  * 
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
16  * Software.
17  * 
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.
25  * 
26  * Author: Rickard E. (Rik) Faith <faith@precisioninsight.com>
27  *
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 $
29  *
30  */
31
32 #define __NO_VERSION__
33 #include "drmP.h"
34
35 drm_agp_func_t drm_agp = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
36
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. */
40 typedef union {
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 *);
45         void          (*enable)(u32);
46         int           (*acquire)(void);
47         void          (*release)(void);
48         void          (*copy_info)(agp_kern_info *);
49         unsigned long address;
50 } drm_agp_func_u;
51
52 typedef struct drm_agp_fill {
53         const char     *name;
54         drm_agp_func_u *f;
55 } drm_agp_fill_t;
56
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       },
74         { NULL, NULL }
75 };
76
77 int drm_agp_info(struct inode *inode, struct file *filp, unsigned int cmd,
78                  unsigned long arg)
79 {
80         drm_file_t       *priv   = filp->private_data;
81         drm_device_t     *dev    = priv->dev;
82         agp_kern_info    *kern;
83         drm_agp_info_t   info;
84
85         if (!dev->agp->acquired || !drm_agp.copy_info) return -EINVAL;
86
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;
97
98         copy_to_user_ret((drm_agp_info_t *)arg, &info, sizeof(info), -EFAULT);
99         return 0;
100 }
101
102 int drm_agp_acquire(struct inode *inode, struct file *filp, unsigned int cmd,
103                     unsigned long arg)
104 {
105         drm_file_t       *priv   = filp->private_data;
106         drm_device_t     *dev    = priv->dev;
107         int              retcode;
108
109         if (dev->agp->acquired || !drm_agp.acquire) return -EINVAL;
110         if ((retcode = (*drm_agp.acquire)())) return retcode;
111         dev->agp->acquired = 1;
112         return 0;
113 }
114
115 int drm_agp_release(struct inode *inode, struct file *filp, unsigned int cmd,
116                     unsigned long arg)
117 {
118         drm_file_t       *priv   = filp->private_data;
119         drm_device_t     *dev    = priv->dev;
120
121         if (!dev->agp->acquired || !drm_agp.release) return -EINVAL;
122         (*drm_agp.release)();
123         dev->agp->acquired = 0;
124         return 0;
125         
126 }
127
128 int drm_agp_enable(struct inode *inode, struct file *filp, unsigned int cmd,
129                    unsigned long arg)
130 {
131         drm_file_t       *priv   = filp->private_data;
132         drm_device_t     *dev    = priv->dev;
133         drm_agp_mode_t   mode;
134
135         if (!dev->agp->acquired || !drm_agp.enable) return -EINVAL;
136
137         copy_from_user_ret(&mode, (drm_agp_mode_t *)arg, sizeof(mode),
138                            -EFAULT);
139         
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;
144         return 0;
145 }
146
147 int drm_agp_alloc(struct inode *inode, struct file *filp, unsigned int cmd,
148                   unsigned long arg)
149 {
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;
154         agp_memory       *memory;
155         unsigned long    pages;
156         u32              type;
157         if (!dev->agp->acquired) return -EINVAL;
158         copy_from_user_ret(&request, (drm_agp_buffer_t *)arg, sizeof(request),
159                            -EFAULT);
160         if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
161                 return -ENOMEM;
162
163         pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
164         type = (u32) request.type;
165
166         if (!(memory = drm_alloc_agp(pages, type))) {
167                 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
168                 return -ENOMEM;
169         }
170         
171         entry->handle    = (unsigned long)memory->memory;
172         entry->memory    = memory;
173         entry->bound     = 0;
174         entry->pages     = pages;
175         entry->prev      = NULL;
176         entry->next      = dev->agp->memory;
177         if (dev->agp->memory) dev->agp->memory->prev = entry;
178         dev->agp->memory = entry;
179
180         request.handle   = entry->handle;
181         request.physical = memory->physical;
182
183         if (copy_to_user((drm_agp_buffer_t *)arg, &request, sizeof(request))) {
184                 dev->agp->memory       = entry->next;
185                 dev->agp->memory->prev = NULL;
186                 drm_free_agp(memory, pages);
187                 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
188                 return -EFAULT;
189         }
190         return 0;
191 }
192
193 static drm_agp_mem_t *drm_agp_lookup_entry(drm_device_t *dev,
194                                            unsigned long handle)
195 {
196         drm_agp_mem_t *entry;
197
198         for (entry = dev->agp->memory; entry; entry = entry->next) {
199                 if (entry->handle == handle) return entry;
200         }
201         return NULL;
202 }
203
204 int drm_agp_unbind(struct inode *inode, struct file *filp, unsigned int cmd,
205                    unsigned long arg)
206 {
207         drm_file_t        *priv  = filp->private_data;
208         drm_device_t      *dev   = priv->dev;
209         drm_agp_binding_t request;
210         drm_agp_mem_t     *entry;
211
212         if (!dev->agp->acquired) return -EINVAL;
213         copy_from_user_ret(&request, (drm_agp_binding_t *)arg, sizeof(request),
214                            -EFAULT);
215         if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
216                 return -EINVAL;
217         if (!entry->bound) return -EINVAL;
218         return drm_unbind_agp(entry->memory);
219 }
220
221 int drm_agp_bind(struct inode *inode, struct file *filp, unsigned int cmd,
222                  unsigned long arg)
223 {
224         drm_file_t        *priv  = filp->private_data;
225         drm_device_t      *dev   = priv->dev;
226         drm_agp_binding_t request;
227         drm_agp_mem_t     *entry;
228         int               retcode;
229         int               page;
230         
231         if (!dev->agp->acquired || !drm_agp.bind_memory) return -EINVAL;
232         copy_from_user_ret(&request, (drm_agp_binding_t *)arg, sizeof(request),
233                            -EFAULT);
234         if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
235                 return -EINVAL;
236         if (entry->bound) return -EINVAL;
237         page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
238         if ((retcode = drm_bind_agp(entry->memory, page))) return retcode;
239         entry->bound = dev->agp->base + (page << PAGE_SHIFT);
240         return 0;
241 }
242
243 int drm_agp_free(struct inode *inode, struct file *filp, unsigned int cmd,
244                  unsigned long arg)
245 {
246         drm_file_t       *priv   = filp->private_data;
247         drm_device_t     *dev    = priv->dev;
248         drm_agp_buffer_t request;
249         drm_agp_mem_t    *entry;
250         
251         if (!dev->agp->acquired) return -EINVAL;
252         copy_from_user_ret(&request, (drm_agp_buffer_t *)arg, sizeof(request),
253                            -EFAULT);
254         if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
255                 return -EINVAL;
256         if (entry->bound) drm_unbind_agp(entry->memory);
257         entry->prev->next = entry->next;
258         entry->next->prev = entry->prev;
259         drm_free_agp(entry->memory, entry->pages);
260         drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
261         return 0;
262 }
263
264 drm_agp_head_t *drm_agp_init(void)
265 {
266         drm_agp_fill_t *fill;
267         drm_agp_head_t *head         = NULL;
268         int            agp_available = 1;
269
270         for (fill = &drm_agp_fill[0]; fill->name; fill++) {
271                 char *n  = (char *)fill->name;
272 #if 0
273                 *fill->f = (drm_agp_func_u)get_module_symbol(NULL, n);
274 #endif
275                 *fill->f = (drm_agp_func_u)get_module_symbol(NULL, n);
276                 printk("%s resolves to 0x%08lx\n", n, (*fill->f).address);
277                 if (!(*fill->f).address) agp_available = 0;
278         }
279    
280         printk("agp_available = %d\n", agp_available);
281
282         if (agp_available) {
283                 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
284                         return NULL;
285                 memset((void *)head, 0, sizeof(*head));
286                 (*drm_agp.copy_info)(&head->agp_info);
287                 head->memory = NULL;
288                 switch (head->agp_info.chipset) {
289                 case INTEL_GENERIC:  head->chipset = "Intel";          break;
290                 case INTEL_LX:       head->chipset = "Intel 440LX";    break;
291                 case INTEL_BX:       head->chipset = "Intel 440BX";    break;
292                 case INTEL_GX:       head->chipset = "Intel 440GX";    break;
293                 case INTEL_I810:     head->chipset = "Intel i810";     break;
294                 case VIA_GENERIC:    head->chipset = "VIA";            break;
295                 case VIA_VP3:        head->chipset = "VIA VP3";        break;
296                 case VIA_MVP3:       head->chipset = "VIA MVP3";       break;
297                 case VIA_APOLLO_PRO: head->chipset = "VIA Apollo Pro"; break;
298                 case SIS_GENERIC:    head->chipset = "SiS";            break;
299                 case AMD_GENERIC:    head->chipset = "AMD";            break;
300                 case AMD_IRONGATE:   head->chipset = "AMD Irongate";   break;
301                 case ALI_GENERIC:    head->chipset = "ALi";            break;
302                 case ALI_M1541:      head->chipset = "ALi M1541";      break;
303                 default:
304                 }
305                 DRM_INFO("AGP %d.%d on %s @ 0x%08lx %dMB\n",
306                          head->agp_info.version.major,
307                          head->agp_info.version.minor,
308                          head->chipset,
309                          head->agp_info.aper_base,
310                          head->agp_info.aper_size);
311         }
312         return head;
313 }