]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm/drm_agpsupport.c
This commit was generated by cvs2svn to compensate for changes in r151940,
[FreeBSD/FreeBSD.git] / sys / dev / drm / 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 /*-
5  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
6  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
7  * All Rights Reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  *
28  * Author:
29  *    Rickard E. (Rik) Faith <faith@valinux.com>
30  *    Gareth Hughes <gareth@valinux.com>
31  *
32  * $FreeBSD$
33  */
34
35 #include "dev/drm/drmP.h"
36
37 #ifdef __FreeBSD__
38 #include <pci/agpreg.h>
39 #include <dev/pci/pcireg.h>
40 #endif
41
42 static int
43 drm_device_find_capability(drm_device_t *dev, int cap)
44 {
45 #ifdef __FreeBSD__
46         /* Code taken from agp.c.  IWBNI that was a public interface. */
47         u_int32_t status;
48         u_int8_t ptr, next;
49
50         /*
51          * Check the CAP_LIST bit of the PCI status register first.
52          */
53         status = pci_read_config(dev->device, PCIR_STATUS, 2);
54         if (!(status & 0x10))
55                 return 0;
56
57         /*
58          * Traverse the capabilities list.
59          */
60         for (ptr = pci_read_config(dev->device, AGP_CAPPTR, 1);
61              ptr != 0;
62              ptr = next) {
63                 u_int32_t capid = pci_read_config(dev->device, ptr, 4);
64                 next = AGP_CAPID_GET_NEXT_PTR(capid);
65
66                 /*
67                  * If this capability entry ID is cap, then we are done.
68                  */
69                 if (AGP_CAPID_GET_CAP_ID(capid) == cap)
70                         return 1;
71         }
72
73         return 0;
74 #else
75         /* XXX: fill me in for non-FreeBSD */
76         return 1;
77 #endif
78 }
79
80 int
81 drm_device_is_agp(drm_device_t *dev)
82 {
83         return (drm_device_find_capability(dev, PCIY_AGP));
84 }
85
86 int
87 drm_device_is_pcie(drm_device_t *dev)
88 {
89         return (drm_device_find_capability(dev, PCIY_EXPRESS));
90 }
91
92 int drm_agp_info(DRM_IOCTL_ARGS)
93 {
94         DRM_DEVICE;
95         struct agp_info *kern;
96         drm_agp_info_t   info;
97
98         if (!dev->agp || !dev->agp->acquired)
99                 return EINVAL;
100
101         kern                   = &dev->agp->info;
102         agp_get_info(dev->agp->agpdev, kern);
103         info.agp_version_major = 1;
104         info.agp_version_minor = 0;
105         info.mode              = kern->ai_mode;
106         info.aperture_base     = kern->ai_aperture_base;
107         info.aperture_size     = kern->ai_aperture_size;
108         info.memory_allowed    = kern->ai_memory_allowed;
109         info.memory_used       = kern->ai_memory_used;
110         info.id_vendor         = kern->ai_devid & 0xffff;
111         info.id_device         = kern->ai_devid >> 16;
112
113         *(drm_agp_info_t *) data = info;
114         return 0;
115 }
116
117 int drm_agp_acquire(DRM_IOCTL_ARGS)
118 {
119         DRM_DEVICE;
120         int          retcode;
121
122         if (!dev->agp || dev->agp->acquired)
123                 return EINVAL;
124         retcode = agp_acquire(dev->agp->agpdev);
125         if (retcode)
126                 return retcode;
127         dev->agp->acquired = 1;
128         return 0;
129 }
130
131 int drm_agp_release(DRM_IOCTL_ARGS)
132 {
133         DRM_DEVICE;
134
135         if (!dev->agp || !dev->agp->acquired)
136                 return EINVAL;
137         agp_release(dev->agp->agpdev);
138         dev->agp->acquired = 0;
139         return 0;
140         
141 }
142
143 void drm_agp_do_release(void)
144 {
145         device_t agpdev;
146
147         agpdev = DRM_AGP_FIND_DEVICE();
148         if (agpdev)
149                 agp_release(agpdev);
150 }
151
152 int drm_agp_enable(DRM_IOCTL_ARGS)
153 {
154         DRM_DEVICE;
155         drm_agp_mode_t mode;
156
157         if (!dev->agp || !dev->agp->acquired)
158                 return EINVAL;
159
160         mode = *(drm_agp_mode_t *) data;
161         
162         dev->agp->mode    = mode.mode;
163         agp_enable(dev->agp->agpdev, mode.mode);
164         dev->agp->base    = dev->agp->info.ai_aperture_base;
165         dev->agp->enabled = 1;
166         return 0;
167 }
168
169 int drm_agp_alloc(DRM_IOCTL_ARGS)
170 {
171         DRM_DEVICE;
172         drm_agp_buffer_t request;
173         drm_agp_mem_t    *entry;
174         void             *handle;
175         unsigned long    pages;
176         u_int32_t        type;
177         struct agp_memory_info info;
178
179         if (!dev->agp || !dev->agp->acquired)
180                 return EINVAL;
181
182         request = *(drm_agp_buffer_t *) data;
183
184         entry = malloc(sizeof(*entry), M_DRM, M_NOWAIT | M_ZERO);
185         if (entry == NULL)
186                 return ENOMEM;
187
188         pages = (request.size + PAGE_SIZE - 1) / PAGE_SIZE;
189         type = (u_int32_t) request.type;
190
191         if (!(handle = drm_agp_allocate_memory(pages, type))) {
192                 free(entry, M_DRM);
193                 return ENOMEM;
194         }
195         
196         entry->handle    = handle;
197         entry->bound     = 0;
198         entry->pages     = pages;
199         entry->prev      = NULL;
200         entry->next      = dev->agp->memory;
201         if (dev->agp->memory)
202                 dev->agp->memory->prev = entry;
203         dev->agp->memory = entry;
204
205         agp_memory_info(dev->agp->agpdev, entry->handle, &info);
206
207         request.handle   = (unsigned long) entry->handle;
208         request.physical = info.ami_physical;
209
210         *(drm_agp_buffer_t *) data = request;
211
212         return 0;
213 }
214
215 static drm_agp_mem_t * drm_agp_lookup_entry(drm_device_t *dev, void *handle)
216 {
217         drm_agp_mem_t *entry;
218
219         for (entry = dev->agp->memory; entry; entry = entry->next) {
220                 if (entry->handle == handle) return entry;
221         }
222         return NULL;
223 }
224
225 int drm_agp_unbind(DRM_IOCTL_ARGS)
226 {
227         DRM_DEVICE;
228         drm_agp_binding_t request;
229         drm_agp_mem_t     *entry;
230         int retcode;
231
232         if (!dev->agp || !dev->agp->acquired)
233                 return EINVAL;
234         request = *(drm_agp_binding_t *) data;
235         if (!(entry = drm_agp_lookup_entry(dev, (void *)request.handle)))
236                 return EINVAL;
237         if (!entry->bound) return EINVAL;
238         retcode = drm_agp_unbind_memory(entry->handle);
239         if (!retcode)
240         {
241                 entry->bound=0;
242                 return 0;
243         }
244         else
245                 return retcode;
246 }
247
248 int drm_agp_bind(DRM_IOCTL_ARGS)
249 {
250         DRM_DEVICE;
251         drm_agp_binding_t request;
252         drm_agp_mem_t     *entry;
253         int               retcode;
254         int               page;
255         
256         DRM_DEBUG("agp_bind, page_size=%x\n", PAGE_SIZE);
257         if (!dev->agp || !dev->agp->acquired)
258                 return EINVAL;
259         request = *(drm_agp_binding_t *) data;
260         if (!(entry = drm_agp_lookup_entry(dev, (void *)request.handle)))
261                 return EINVAL;
262         if (entry->bound) return EINVAL;
263         page = (request.offset + PAGE_SIZE - 1) / PAGE_SIZE;
264         if ((retcode = drm_agp_bind_memory(entry->handle, page)))
265                 return retcode;
266         entry->bound = dev->agp->base + (page << PAGE_SHIFT);
267         return 0;
268 }
269
270 int drm_agp_free(DRM_IOCTL_ARGS)
271 {
272         DRM_DEVICE;
273         drm_agp_buffer_t request;
274         drm_agp_mem_t    *entry;
275         
276         if (!dev->agp || !dev->agp->acquired)
277                 return EINVAL;
278         request = *(drm_agp_buffer_t *) data;
279         if (!(entry = drm_agp_lookup_entry(dev, (void*)request.handle)))
280                 return EINVAL;
281         if (entry->bound)
282                 drm_agp_unbind_memory(entry->handle);
283    
284         if (entry->prev)
285                 entry->prev->next = entry->next;
286         else
287                 dev->agp->memory  = entry->next;
288         if (entry->next)
289                 entry->next->prev = entry->prev;
290         drm_agp_free_memory(entry->handle);
291         free(entry, M_DRM);
292         return 0;
293 }
294
295 drm_agp_head_t *drm_agp_init(void)
296 {
297         device_t agpdev;
298         drm_agp_head_t *head   = NULL;
299         int      agp_available = 1;
300    
301         agpdev = DRM_AGP_FIND_DEVICE();
302         if (!agpdev)
303                 agp_available = 0;
304
305         DRM_DEBUG("agp_available = %d\n", agp_available);
306
307         if (agp_available) {
308                 head = malloc(sizeof(*head), M_DRM, M_NOWAIT | M_ZERO);
309                 if (head == NULL)
310                         return NULL;
311                 head->agpdev = agpdev;
312                 agp_get_info(agpdev, &head->info);
313                 head->memory = NULL;
314                 DRM_INFO("AGP at 0x%08lx %dMB\n",
315                          (long)head->info.ai_aperture_base,
316                          (int)(head->info.ai_aperture_size >> 20));
317         }
318         return head;
319 }
320
321 void drm_agp_uninit(void)
322 {
323 /* FIXME: What goes here */
324 }
325
326
327 void *drm_agp_allocate_memory(size_t pages, u32 type)
328 {
329         device_t agpdev;
330
331         agpdev = DRM_AGP_FIND_DEVICE();
332         if (!agpdev)
333                 return NULL;
334
335         return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
336 }
337
338 int drm_agp_free_memory(void *handle)
339 {
340         device_t agpdev;
341
342         agpdev = DRM_AGP_FIND_DEVICE();
343         if (!agpdev || !handle)
344                 return 0;
345
346         agp_free_memory(agpdev, handle);
347         return 1;
348 }
349
350 int drm_agp_bind_memory(void *handle, off_t start)
351 {
352         device_t agpdev;
353
354         agpdev = DRM_AGP_FIND_DEVICE();
355         if (!agpdev || !handle)
356                 return EINVAL;
357
358         return agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
359 }
360
361 int drm_agp_unbind_memory(void *handle)
362 {
363         device_t agpdev;
364
365         agpdev = DRM_AGP_FIND_DEVICE();
366         if (!agpdev || !handle)
367                 return EINVAL;
368
369         return agp_unbind_memory(agpdev, handle);
370 }