* Add this nice filesystem testing tool that I've recently
[dragonfly.git] / sys / dev / drm / drm_agpsupport.h
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  * $FreeBSD: src/sys/dev/drm/drm_agpsupport.h,v 1.2.2.1 2003/04/26 07:05:28 anholt Exp $
31  * $DragonFly: src/sys/dev/drm/Attic/drm_agpsupport.h,v 1.2 2003/06/17 04:28:24 dillon Exp $
32  */
33
34 #include "dev/drm/drmP.h"
35
36 int DRM(agp_info)(DRM_IOCTL_ARGS)
37 {
38         DRM_DEVICE;
39         struct agp_info *kern;
40         drm_agp_info_t   info;
41
42         if (!dev->agp || !dev->agp->acquired)
43                 return EINVAL;
44
45         kern                   = &dev->agp->info;
46         agp_get_info(dev->agp->agpdev, kern);
47         info.agp_version_major = 1;
48         info.agp_version_minor = 0;
49         info.mode              = kern->ai_mode;
50         info.aperture_base     = kern->ai_aperture_base;
51         info.aperture_size     = kern->ai_aperture_size;
52         info.memory_allowed    = kern->ai_memory_allowed;
53         info.memory_used       = kern->ai_memory_used;
54         info.id_vendor         = kern->ai_devid & 0xffff;
55         info.id_device         = kern->ai_devid >> 16;
56
57         *(drm_agp_info_t *) data = info;
58         return 0;
59 }
60
61 int DRM(agp_acquire)(DRM_IOCTL_ARGS)
62 {
63         DRM_DEVICE;
64         int          retcode;
65
66         if (!dev->agp || dev->agp->acquired)
67                 return EINVAL;
68         retcode = agp_acquire(dev->agp->agpdev);
69         if (retcode)
70                 return retcode;
71         dev->agp->acquired = 1;
72         return 0;
73 }
74
75 int DRM(agp_release)(DRM_IOCTL_ARGS)
76 {
77         DRM_DEVICE;
78
79         if (!dev->agp || !dev->agp->acquired)
80                 return EINVAL;
81         agp_release(dev->agp->agpdev);
82         dev->agp->acquired = 0;
83         return 0;
84         
85 }
86
87 void DRM(agp_do_release)(void)
88 {
89         device_t agpdev;
90
91         agpdev = DRM_AGP_FIND_DEVICE();
92         if (agpdev)
93                 agp_release(agpdev);
94 }
95
96 int DRM(agp_enable)(DRM_IOCTL_ARGS)
97 {
98         DRM_DEVICE;
99         drm_agp_mode_t mode;
100
101         if (!dev->agp || !dev->agp->acquired)
102                 return EINVAL;
103
104         mode = *(drm_agp_mode_t *) data;
105         
106         dev->agp->mode    = mode.mode;
107         agp_enable(dev->agp->agpdev, mode.mode);
108         dev->agp->base    = dev->agp->info.ai_aperture_base;
109         dev->agp->enabled = 1;
110         return 0;
111 }
112
113 int DRM(agp_alloc)(DRM_IOCTL_ARGS)
114 {
115         DRM_DEVICE;
116         drm_agp_buffer_t request;
117         drm_agp_mem_t    *entry;
118         void             *handle;
119         unsigned long    pages;
120         u_int32_t        type;
121         struct agp_memory_info info;
122
123         if (!dev->agp || !dev->agp->acquired)
124                 return EINVAL;
125
126         request = *(drm_agp_buffer_t *) data;
127
128         if (!(entry = DRM(alloc)(sizeof(*entry), DRM_MEM_AGPLISTS)))
129                 return ENOMEM;
130    
131         bzero(entry, sizeof(*entry));
132
133         pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
134         type = (u_int32_t) request.type;
135
136         if (!(handle = DRM(alloc_agp)(pages, type))) {
137                 DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
138                 return ENOMEM;
139         }
140         
141         entry->handle    = handle;
142         entry->bound     = 0;
143         entry->pages     = pages;
144         entry->prev      = NULL;
145         entry->next      = dev->agp->memory;
146         if (dev->agp->memory)
147                 dev->agp->memory->prev = entry;
148         dev->agp->memory = entry;
149
150         agp_memory_info(dev->agp->agpdev, entry->handle, &info);
151
152         request.handle   = (unsigned long) entry->handle;
153         request.physical = info.ami_physical;
154
155         *(drm_agp_buffer_t *) data = request;
156
157         return 0;
158 }
159
160 static drm_agp_mem_t * DRM(agp_lookup_entry)(drm_device_t *dev, void *handle)
161 {
162         drm_agp_mem_t *entry;
163
164         for (entry = dev->agp->memory; entry; entry = entry->next) {
165                 if (entry->handle == handle) return entry;
166         }
167         return NULL;
168 }
169
170 int DRM(agp_unbind)(DRM_IOCTL_ARGS)
171 {
172         DRM_DEVICE;
173         drm_agp_binding_t request;
174         drm_agp_mem_t     *entry;
175         int retcode;
176
177         if (!dev->agp || !dev->agp->acquired)
178                 return EINVAL;
179         request = *(drm_agp_binding_t *) data;
180         if (!(entry = DRM(agp_lookup_entry)(dev, (void *) request.handle)))
181                 return EINVAL;
182         if (!entry->bound) return EINVAL;
183         retcode=DRM(unbind_agp)(entry->handle);
184         if (!retcode)
185         {
186                 entry->bound=0;
187                 return 0;
188         }
189         else
190                 return retcode;
191 }
192
193 int DRM(agp_bind)(DRM_IOCTL_ARGS)
194 {
195         DRM_DEVICE;
196         drm_agp_binding_t request;
197         drm_agp_mem_t     *entry;
198         int               retcode;
199         int               page;
200         
201         DRM_DEBUG("agp_bind, page_size=%x\n", PAGE_SIZE);
202         if (!dev->agp || !dev->agp->acquired)
203                 return EINVAL;
204         request = *(drm_agp_binding_t *) data;
205         if (!(entry = DRM(agp_lookup_entry)(dev, (void *) request.handle)))
206                 return EINVAL;
207         if (entry->bound) return EINVAL;
208         page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
209         if ((retcode = DRM(bind_agp)(entry->handle, page)))
210                 return retcode;
211         entry->bound = dev->agp->base + (page << PAGE_SHIFT);
212         return 0;
213 }
214
215 int DRM(agp_free)(DRM_IOCTL_ARGS)
216 {
217         DRM_DEVICE;
218         drm_agp_buffer_t request;
219         drm_agp_mem_t    *entry;
220         
221         if (!dev->agp || !dev->agp->acquired)
222                 return EINVAL;
223         request = *(drm_agp_buffer_t *) data;
224         if (!(entry = DRM(agp_lookup_entry)(dev, (void*) request.handle)))
225                 return EINVAL;
226         if (entry->bound)
227                 DRM(unbind_agp)(entry->handle);
228    
229         if (entry->prev)
230                 entry->prev->next = entry->next;
231         else
232                 dev->agp->memory  = entry->next;
233         if (entry->next)
234                 entry->next->prev = entry->prev;
235         DRM(free_agp)(entry->handle, entry->pages);
236         DRM(free)(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
237         return 0;
238 }
239
240 drm_agp_head_t *DRM(agp_init)(void)
241 {
242         device_t agpdev;
243         drm_agp_head_t *head   = NULL;
244         int      agp_available = 1;
245    
246         agpdev = DRM_AGP_FIND_DEVICE();
247         if (!agpdev)
248                 agp_available = 0;
249
250         DRM_DEBUG("agp_available = %d\n", agp_available);
251
252         if (agp_available) {
253                 if (!(head = DRM(alloc)(sizeof(*head), DRM_MEM_AGPLISTS)))
254                         return NULL;
255                 bzero((void *)head, sizeof(*head));
256                 head->agpdev = agpdev;
257                 agp_get_info(agpdev, &head->info);
258                 head->memory = NULL;
259 #if 0                           /* bogus */
260                 switch (head->agp_info.chipset) {
261                 case INTEL_GENERIC:  head->chipset = "Intel";          break;
262                 case INTEL_LX:       head->chipset = "Intel 440LX";    break;
263                 case INTEL_BX:       head->chipset = "Intel 440BX";    break;
264                 case INTEL_GX:       head->chipset = "Intel 440GX";    break;
265                 case INTEL_I810:     head->chipset = "Intel i810";     break;
266                 case VIA_GENERIC:    head->chipset = "VIA";            break;
267                 case VIA_VP3:        head->chipset = "VIA VP3";        break;
268                 case VIA_MVP3:       head->chipset = "VIA MVP3";       break;
269                 case VIA_APOLLO_PRO: head->chipset = "VIA Apollo Pro"; break;
270                 case SIS_GENERIC:    head->chipset = "SiS";            break;
271                 case AMD_GENERIC:    head->chipset = "AMD";            break;
272                 case AMD_IRONGATE:   head->chipset = "AMD Irongate";   break;
273                 case ALI_GENERIC:    head->chipset = "ALi";            break;
274                 case ALI_M1541:      head->chipset = "ALi M1541";      break;
275                 default:
276                 }
277 #endif
278                 DRM_INFO("AGP at 0x%08lx %dMB\n",
279                          (long)head->info.ai_aperture_base,
280                          (int)(head->info.ai_aperture_size >> 20));
281         }
282         return head;
283 }
284
285 void DRM(agp_uninit)(void)
286 {
287 /* FIXME: What goes here */
288 }
289
290
291 agp_memory *DRM(agp_allocate_memory)(size_t pages, u32 type)
292 {
293         device_t agpdev;
294
295         agpdev = DRM_AGP_FIND_DEVICE();
296         if (!agpdev)
297                 return NULL;
298
299         return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
300 }
301
302 int DRM(agp_free_memory)(agp_memory *handle)
303 {
304         device_t agpdev;
305
306         agpdev = DRM_AGP_FIND_DEVICE();
307         if (!agpdev || !handle)
308                 return 0;
309
310         agp_free_memory(agpdev, handle);
311         return 1;
312 }
313
314 int DRM(agp_bind_memory)(agp_memory *handle, off_t start)
315 {
316         device_t agpdev;
317
318         agpdev = DRM_AGP_FIND_DEVICE();
319         if (!agpdev || !handle)
320                 return EINVAL;
321
322         return agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
323 }
324
325 int DRM(agp_unbind_memory)(agp_memory *handle)
326 {
327         device_t agpdev;
328
329         agpdev = DRM_AGP_FIND_DEVICE();
330         if (!agpdev || !handle)
331                 return EINVAL;
332
333         return agp_unbind_memory(agpdev, handle);
334 }