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