Merge mga-1-0-0-branch into trunk.
[platform/upstream/libdrm.git] / linux-core / drm_agpsupport.c
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
3  *
4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * All Rights Reserved.
7  *
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:
14  *
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
17  * Software.
18  *
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.
26  *
27  * Author:
28  *    Rickard E. (Rik) Faith <faith@valinux.com>
29  *    Gareth Hughes <gareth@valinux.com>
30  */
31
32 #define __NO_VERSION__
33 #include "drmP.h"
34 #include <linux/module.h>
35
36 #if __REALLY_HAVE_AGP
37
38 #if LINUX_VERSION_CODE < 0x020400
39 #include "agpsupport-pre24.h"
40 #else
41 #define DRM_AGP_GET (drm_agp_t *)inter_module_get("drm_agp")
42 #define DRM_AGP_PUT inter_module_put("drm_agp")
43 #endif
44
45 static const drm_agp_t *drm_agp = NULL;
46
47 int DRM(agp_info)(struct inode *inode, struct file *filp,
48                   unsigned int cmd, unsigned long arg)
49 {
50         drm_file_t       *priv   = filp->private_data;
51         drm_device_t     *dev    = priv->dev;
52         agp_kern_info    *kern;
53         drm_agp_info_t   info;
54
55         if (!dev->agp->acquired || !drm_agp->copy_info) return -EINVAL;
56
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;
67
68         if (copy_to_user((drm_agp_info_t *)arg, &info, sizeof(info)))
69                 return -EFAULT;
70         return 0;
71 }
72
73 int DRM(agp_acquire)(struct inode *inode, struct file *filp,
74                      unsigned int cmd, unsigned long arg)
75 {
76         drm_file_t       *priv   = filp->private_data;
77         drm_device_t     *dev    = priv->dev;
78         int              retcode;
79
80         if (dev->agp->acquired || !drm_agp->acquire) return -EINVAL;
81         if ((retcode = drm_agp->acquire())) return retcode;
82         dev->agp->acquired = 1;
83         return 0;
84 }
85
86 int DRM(agp_release)(struct inode *inode, struct file *filp,
87                      unsigned int cmd, unsigned long arg)
88 {
89         drm_file_t       *priv   = filp->private_data;
90         drm_device_t     *dev    = priv->dev;
91
92         if (!dev->agp->acquired || !drm_agp->release) return -EINVAL;
93         drm_agp->release();
94         dev->agp->acquired = 0;
95         return 0;
96
97 }
98
99 void DRM(agp_do_release)(void)
100 {
101         if (drm_agp->release) drm_agp->release();
102 }
103
104 int DRM(agp_enable)(struct inode *inode, struct file *filp,
105                     unsigned int cmd, unsigned long arg)
106 {
107         drm_file_t       *priv   = filp->private_data;
108         drm_device_t     *dev    = priv->dev;
109         drm_agp_mode_t   mode;
110
111         if (!dev->agp->acquired || !drm_agp->enable) return -EINVAL;
112
113         if (copy_from_user(&mode, (drm_agp_mode_t *)arg, sizeof(mode)))
114                 return -EFAULT;
115
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;
120         return 0;
121 }
122
123 int DRM(agp_alloc)(struct inode *inode, struct file *filp,
124                    unsigned int cmd, unsigned long arg)
125 {
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;
130         agp_memory       *memory;
131         unsigned long    pages;
132         u32              type;
133
134         if (!dev->agp->acquired) return -EINVAL;
135         if (copy_from_user(&request, (drm_agp_buffer_t *)arg, sizeof(request)))
136                 return -EFAULT;
137         if (!(entry = DRM(alloc)(sizeof(*entry), DRM_MEM_AGPLISTS)))
138                 return -ENOMEM;
139
140         memset(entry, 0, sizeof(*entry));
141
142         pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
143         type = (u32) request.type;
144
145         if (!(memory = DRM(alloc_agp)(pages, type))) {
146                 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
147                 return -ENOMEM;
148         }
149
150         entry->handle    = (unsigned long)memory->memory;
151         entry->memory    = memory;
152         entry->bound     = 0;
153         entry->pages     = pages;
154         entry->prev      = NULL;
155         entry->next      = dev->agp->memory;
156         if (dev->agp->memory) dev->agp->memory->prev = entry;
157         dev->agp->memory = entry;
158
159         request.handle   = entry->handle;
160         request.physical = memory->physical;
161
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);
167                 return -EFAULT;
168         }
169         return 0;
170 }
171
172 static drm_agp_mem_t *DRM(agp_lookup_entry)(drm_device_t *dev,
173                                             unsigned long handle)
174 {
175         drm_agp_mem_t *entry;
176
177         for (entry = dev->agp->memory; entry; entry = entry->next) {
178                 if (entry->handle == handle) return entry;
179         }
180         return NULL;
181 }
182
183 int DRM(agp_unbind)(struct inode *inode, struct file *filp,
184                     unsigned int cmd, unsigned long arg)
185 {
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;
190
191         if (!dev->agp->acquired) return -EINVAL;
192         if (copy_from_user(&request, (drm_agp_binding_t *)arg, sizeof(request)))
193                 return -EFAULT;
194         if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
195                 return -EINVAL;
196         if (!entry->bound) return -EINVAL;
197         return DRM(unbind_agp)(entry->memory);
198 }
199
200 int DRM(agp_bind)(struct inode *inode, struct file *filp,
201                   unsigned int cmd, unsigned long arg)
202 {
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;
207         int               retcode;
208         int               page;
209
210         if (!dev->agp->acquired || !drm_agp->bind_memory) return -EINVAL;
211         if (copy_from_user(&request, (drm_agp_binding_t *)arg, sizeof(request)))
212                 return -EFAULT;
213         if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
214                 return -EINVAL;
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);
221         return 0;
222 }
223
224 int DRM(agp_free)(struct inode *inode, struct file *filp,
225                   unsigned int cmd, unsigned long arg)
226 {
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;
231
232         if (!dev->agp->acquired) return -EINVAL;
233         if (copy_from_user(&request, (drm_agp_buffer_t *)arg, sizeof(request)))
234                 return -EFAULT;
235         if (!(entry = DRM(agp_lookup_entry)(dev, request.handle)))
236                 return -EINVAL;
237         if (entry->bound) DRM(unbind_agp)(entry->memory);
238
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);
244         return 0;
245 }
246
247 drm_agp_head_t *DRM(agp_init)(void)
248 {
249         drm_agp_head_t *head         = NULL;
250
251         drm_agp = DRM_AGP_GET;
252         if (drm_agp) {
253                 if (!(head = DRM(alloc)(sizeof(*head), DRM_MEM_AGPLISTS)))
254                         return NULL;
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);
259                         return NULL;
260                 }
261                 head->memory = NULL;
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;
268
269 #if LINUX_VERSION_CODE >= 0x020400
270                 case INTEL_I840:        head->chipset = "Intel i840";    break;
271 #endif
272
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;
276
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";
280                         break;
281                 case VIA_APOLLO_KT133:  head->chipset = "VIA Apollo KT133";
282                         break;
283 #endif
284
285                 case VIA_APOLLO_PRO:    head->chipset = "VIA Apollo Pro";
286                         break;
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;
293                 }
294                 DRM_INFO("AGP %d.%d on %s @ 0x%08lx %ZuMB\n",
295                          head->agp_info.version.major,
296                          head->agp_info.version.minor,
297                          head->chipset,
298                          head->agp_info.aper_base,
299                          head->agp_info.aper_size);
300         }
301         return head;
302 }
303
304 void DRM(agp_uninit)(void)
305 {
306         DRM_AGP_PUT;
307         drm_agp = NULL;
308 }
309
310 agp_memory *DRM(agp_allocate_memory)(size_t pages, u32 type)
311 {
312         if (!drm_agp->allocate_memory) return NULL;
313         return drm_agp->allocate_memory(pages, type);
314 }
315
316 int DRM(agp_free_memory)(agp_memory *handle)
317 {
318         if (!handle || !drm_agp->free_memory) return 0;
319         drm_agp->free_memory(handle);
320         return 1;
321 }
322
323 int DRM(agp_bind_memory)(agp_memory *handle, off_t start)
324 {
325         if (!handle || !drm_agp->bind_memory) return -EINVAL;
326         return drm_agp->bind_memory(handle, start);
327 }
328
329 int DRM(agp_unbind_memory)(agp_memory *handle)
330 {
331         if (!handle || !drm_agp->unbind_memory) return -EINVAL;
332         return drm_agp->unbind_memory(handle);
333 }
334
335 #endif /* __REALLY_HAVE_AGP */