Merged mga branch with trunk
[platform/upstream/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         memset(entry, 0, sizeof(*entry));
164
165         pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
166         type = (u32) request.type;
167
168         if (!(memory = drm_alloc_agp(pages, type))) {
169                 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
170                 return -ENOMEM;
171         }
172         
173         entry->handle    = (unsigned long)memory->memory;
174         entry->memory    = memory;
175         entry->bound     = 0;
176         entry->pages     = pages;
177         entry->prev      = NULL;
178         entry->next      = dev->agp->memory;
179         if (dev->agp->memory) dev->agp->memory->prev = entry;
180         dev->agp->memory = entry;
181
182         request.handle   = entry->handle;
183         request.physical = memory->physical;
184
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);
190                 return -EFAULT;
191         }
192         return 0;
193 }
194
195 static drm_agp_mem_t *drm_agp_lookup_entry(drm_device_t *dev,
196                                            unsigned long handle)
197 {
198         drm_agp_mem_t *entry;
199
200         for (entry = dev->agp->memory; entry; entry = entry->next) {
201                 if (entry->handle == handle) return entry;
202         }
203         return NULL;
204 }
205
206 int drm_agp_unbind(struct inode *inode, struct file *filp, unsigned int cmd,
207                    unsigned long arg)
208 {
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;
213
214         if (!dev->agp->acquired) return -EINVAL;
215         copy_from_user_ret(&request, (drm_agp_binding_t *)arg, sizeof(request),
216                            -EFAULT);
217         if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
218                 return -EINVAL;
219         if (!entry->bound) return -EINVAL;
220         return drm_unbind_agp(entry->memory);
221 }
222
223 int drm_agp_bind(struct inode *inode, struct file *filp, unsigned int cmd,
224                  unsigned long arg)
225 {
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;
230         int               retcode;
231         int               page;
232         
233         if (!dev->agp->acquired || !drm_agp.bind_memory) return -EINVAL;
234         copy_from_user_ret(&request, (drm_agp_binding_t *)arg, sizeof(request),
235                            -EFAULT);
236         if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
237                 return -EINVAL;
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);
242         return 0;
243 }
244
245 int drm_agp_free(struct inode *inode, struct file *filp, unsigned int cmd,
246                  unsigned long arg)
247 {
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;
252         
253         if (!dev->agp->acquired) return -EINVAL;
254         copy_from_user_ret(&request, (drm_agp_buffer_t *)arg, sizeof(request),
255                            -EFAULT);
256         if (!(entry = drm_agp_lookup_entry(dev, request.handle)))
257                 return -EINVAL;
258         if (entry->bound) drm_unbind_agp(entry->memory);
259    
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);
265         return 0;
266 }
267
268 drm_agp_head_t *drm_agp_init(void)
269 {
270         drm_agp_fill_t *fill;
271         drm_agp_head_t *head         = NULL;
272         int            agp_available = 1;
273
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;
279         }
280    
281         DRM_DEBUG("agp_available = %d\n", agp_available);
282
283         if (agp_available) {
284                 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
285                         return NULL;
286                 memset((void *)head, 0, sizeof(*head));
287                 (*drm_agp.copy_info)(&head->agp_info);
288                 head->memory = NULL;
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;
304                 default:
305                 }
306                 DRM_INFO("AGP %d.%d on %s @ 0x%08lx %dMB\n",
307                          head->agp_info.version.major,
308                          head->agp_info.version.minor,
309                          head->chipset,
310                          head->agp_info.aper_base,
311                          head->agp_info.aper_size);
312         }
313         return head;
314 }