OSDN Git Service

drm: drop Linux < 2.6.21 support
[android-x86/external-libdrm.git] / bsd-core / drm_agpsupport.c
1 /*-
2  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * Author:
26  *    Rickard E. (Rik) Faith <faith@valinux.com>
27  *    Gareth Hughes <gareth@valinux.com>
28  *
29  */
30
31 /** @file drm_agpsupport.c
32  * Support code for tying the kernel AGP support to DRM drivers and
33  * the DRM's AGP ioctls.
34  */
35
36 #include "drmP.h"
37
38 #if __FreeBSD_version >= 800004
39 #include <dev/agp/agpreg.h>
40 #else /* __FreeBSD_version >= 800004 */
41 #include <pci/agpreg.h>
42 #endif /* __FreeBSD_version >= 800004 */
43 #include <dev/pci/pcireg.h>
44
45 /* Returns 1 if AGP or 0 if not. */
46 static int
47 drm_device_find_capability(struct drm_device *dev, int cap)
48 {
49 #if __FreeBSD_version >= 602102
50
51         return (pci_find_extcap(dev->device, cap, NULL) == 0);
52 #else
53         /* Code taken from agp.c.  IWBNI that was a public interface. */
54         u_int32_t status;
55         u_int8_t ptr, next;
56
57         /*
58          * Check the CAP_LIST bit of the PCI status register first.
59          */
60         status = pci_read_config(dev->device, PCIR_STATUS, 2);
61         if (!(status & 0x10))
62                 return 0;
63
64         /*
65          * Traverse the capabilities list.
66          */
67         for (ptr = pci_read_config(dev->device, AGP_CAPPTR, 1);
68              ptr != 0;
69              ptr = next) {
70                 u_int32_t capid = pci_read_config(dev->device, ptr, 4);
71                 next = AGP_CAPID_GET_NEXT_PTR(capid);
72
73                 /*
74                  * If this capability entry ID is cap, then we are done.
75                  */
76                 if (AGP_CAPID_GET_CAP_ID(capid) == cap)
77                         return 1;
78         }
79
80         return 0;
81 #endif
82 }
83
84 int drm_device_is_agp(struct drm_device *dev)
85 {
86         if (dev->driver->device_is_agp != NULL) {
87                 int ret;
88
89                 /* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely
90                  * AGP, 2 = fall back to PCI capability
91                  */
92                 ret = (*dev->driver->device_is_agp)(dev);
93                 if (ret != DRM_MIGHT_BE_AGP)
94                         return ret;
95         }
96
97         return (drm_device_find_capability(dev, PCIY_AGP));
98 }
99
100 int drm_device_is_pcie(struct drm_device *dev)
101 {
102         return (drm_device_find_capability(dev, PCIY_EXPRESS));
103 }
104
105 int drm_agp_info(struct drm_device * dev, struct drm_agp_info *info)
106 {
107         struct agp_info *kern;
108
109         if (!dev->agp || !dev->agp->acquired)
110                 return EINVAL;
111
112         kern                   = &dev->agp->info;
113         agp_get_info(dev->agp->agpdev, kern);
114         info->agp_version_major = 1;
115         info->agp_version_minor = 0;
116         info->mode              = kern->ai_mode;
117         info->aperture_base     = kern->ai_aperture_base;
118         info->aperture_size     = kern->ai_aperture_size;
119         info->memory_allowed    = kern->ai_memory_allowed;
120         info->memory_used       = kern->ai_memory_used;
121         info->id_vendor         = kern->ai_devid & 0xffff;
122         info->id_device         = kern->ai_devid >> 16;
123
124         return 0;
125 }
126
127 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
128                        struct drm_file *file_priv)
129 {
130         int err;
131         struct drm_agp_info info;
132
133         err = drm_agp_info(dev, &info);
134         if (err != 0)
135                 return err;
136
137         *(struct drm_agp_info *) data = info;
138         return 0;
139 }
140
141 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
142                           struct drm_file *file_priv)
143 {
144
145         return drm_agp_acquire(dev);
146 }
147
148 int drm_agp_acquire(struct drm_device *dev)
149 {
150         int retcode;
151
152         if (!dev->agp || dev->agp->acquired)
153                 return EINVAL;
154
155         retcode = agp_acquire(dev->agp->agpdev);
156         if (retcode)
157                 return retcode;
158
159         dev->agp->acquired = 1;
160         return 0;
161 }
162
163 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
164                           struct drm_file *file_priv)
165 {
166
167         return drm_agp_release(dev);
168 }
169
170 int drm_agp_release(struct drm_device * dev)
171 {
172         if (!dev->agp || !dev->agp->acquired)
173                 return EINVAL;
174         agp_release(dev->agp->agpdev);
175         dev->agp->acquired = 0;
176         return 0;
177 }
178
179 int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
180 {
181
182         if (!dev->agp || !dev->agp->acquired)
183                 return EINVAL;
184         
185         dev->agp->mode    = mode.mode;
186         agp_enable(dev->agp->agpdev, mode.mode);
187         dev->agp->enabled = 1;
188         return 0;
189 }
190
191 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
192                          struct drm_file *file_priv)
193 {
194         struct drm_agp_mode mode;
195
196         mode = *(struct drm_agp_mode *) data;
197
198         return drm_agp_enable(dev, mode);
199 }
200
201 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
202 {
203         drm_agp_mem_t    *entry;
204         void             *handle;
205         unsigned long    pages;
206         u_int32_t        type;
207         struct agp_memory_info info;
208
209         if (!dev->agp || !dev->agp->acquired)
210                 return EINVAL;
211
212         entry = malloc(sizeof(*entry), DRM_MEM_AGPLISTS, M_NOWAIT | M_ZERO);
213         if (entry == NULL)
214                 return ENOMEM;
215
216         pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
217         type = (u_int32_t) request->type;
218
219         DRM_UNLOCK();
220         handle = drm_agp_allocate_memory(pages, type);
221         DRM_LOCK();
222         if (handle == NULL) {
223                 free(entry, DRM_MEM_AGPLISTS);
224                 return ENOMEM;
225         }
226         
227         entry->handle    = handle;
228         entry->bound     = 0;
229         entry->pages     = pages;
230         entry->prev      = NULL;
231         entry->next      = dev->agp->memory;
232         if (dev->agp->memory)
233                 dev->agp->memory->prev = entry;
234         dev->agp->memory = entry;
235
236         agp_memory_info(dev->agp->agpdev, entry->handle, &info);
237
238         request->handle   = (unsigned long) entry->handle;
239         request->physical = info.ami_physical;
240
241         return 0;
242 }
243
244 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
245                         struct drm_file *file_priv)
246 {
247         struct drm_agp_buffer request;
248         int retcode;
249
250         request = *(struct drm_agp_buffer *) data;
251
252         DRM_LOCK();
253         retcode = drm_agp_alloc(dev, &request);
254         DRM_UNLOCK();
255
256         *(struct drm_agp_buffer *) data = request;
257
258         return retcode;
259 }
260
261 static drm_agp_mem_t * drm_agp_lookup_entry(struct drm_device *dev,
262                                             void *handle)
263 {
264         drm_agp_mem_t *entry;
265
266         for (entry = dev->agp->memory; entry; entry = entry->next) {
267                 if (entry->handle == handle) return entry;
268         }
269         return NULL;
270 }
271
272 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
273 {
274         drm_agp_mem_t     *entry;
275         int retcode;
276
277         if (!dev->agp || !dev->agp->acquired)
278                 return EINVAL;
279
280         entry = drm_agp_lookup_entry(dev, (void *)request->handle);
281         if (entry == NULL || !entry->bound)
282                 return EINVAL;
283
284         DRM_UNLOCK();
285         retcode = drm_agp_unbind_memory(entry->handle);
286         DRM_LOCK();
287
288         if (retcode == 0)
289                 entry->bound = 0;
290
291         return retcode;
292 }
293
294 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
295                          struct drm_file *file_priv)
296 {
297         struct drm_agp_binding request;
298         int retcode;
299
300         request = *(struct drm_agp_binding *) data;
301
302         DRM_LOCK();
303         retcode = drm_agp_unbind(dev, &request);
304         DRM_UNLOCK();
305
306         return retcode;
307 }
308
309 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
310 {
311         drm_agp_mem_t     *entry;
312         int               retcode;
313         int               page;
314         
315         if (!dev->agp || !dev->agp->acquired)
316                 return EINVAL;
317
318         DRM_DEBUG("agp_bind, page_size=%x\n", PAGE_SIZE);
319
320         entry = drm_agp_lookup_entry(dev, (void *)request->handle);
321         if (entry == NULL || entry->bound)
322                 return EINVAL;
323
324         page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
325
326         DRM_UNLOCK();
327         retcode = drm_agp_bind_memory(entry->handle, page);
328         DRM_LOCK();
329         if (retcode == 0)
330                 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
331
332         return retcode;
333 }
334
335 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
336                        struct drm_file *file_priv)
337 {
338         struct drm_agp_binding request;
339         int retcode;
340
341         request = *(struct drm_agp_binding *) data;
342
343         DRM_LOCK();
344         retcode = drm_agp_bind(dev, &request);
345         DRM_UNLOCK();
346
347         return retcode;
348 }
349
350 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
351 {
352         drm_agp_mem_t    *entry;
353         
354         if (!dev->agp || !dev->agp->acquired)
355                 return EINVAL;
356
357         entry = drm_agp_lookup_entry(dev, (void*)request->handle);
358         if (entry == NULL)
359                 return EINVAL;
360    
361         if (entry->prev)
362                 entry->prev->next = entry->next;
363         else
364                 dev->agp->memory  = entry->next;
365         if (entry->next)
366                 entry->next->prev = entry->prev;
367
368         DRM_UNLOCK();
369         if (entry->bound)
370                 drm_agp_unbind_memory(entry->handle);
371         drm_agp_free_memory(entry->handle);
372         DRM_LOCK();
373
374         free(entry, DRM_MEM_AGPLISTS);
375
376         return 0;
377
378 }
379
380 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
381                        struct drm_file *file_priv)
382 {
383         struct drm_agp_buffer request;
384         int retcode;
385
386         request = *(struct drm_agp_buffer *) data;
387
388         DRM_LOCK();
389         retcode = drm_agp_free(dev, &request);
390         DRM_UNLOCK();
391
392         return retcode;
393 }
394
395 drm_agp_head_t *drm_agp_init(void)
396 {
397         device_t agpdev;
398         drm_agp_head_t *head   = NULL;
399         int      agp_available = 1;
400    
401         agpdev = DRM_AGP_FIND_DEVICE();
402         if (!agpdev)
403                 agp_available = 0;
404
405         DRM_DEBUG("agp_available = %d\n", agp_available);
406
407         if (agp_available) {
408                 head = malloc(sizeof(*head), DRM_MEM_AGPLISTS,
409                     M_NOWAIT | M_ZERO);
410                 if (head == NULL)
411                         return NULL;
412                 head->agpdev = agpdev;
413                 agp_get_info(agpdev, &head->info);
414                 head->base = head->info.ai_aperture_base;
415                 head->memory = NULL;
416                 DRM_INFO("AGP at 0x%08lx %dMB\n",
417                          (long)head->info.ai_aperture_base,
418                          (int)(head->info.ai_aperture_size >> 20));
419         }
420         return head;
421 }
422
423 void *drm_agp_allocate_memory(size_t pages, u32 type)
424 {
425         device_t agpdev;
426
427         agpdev = DRM_AGP_FIND_DEVICE();
428         if (!agpdev)
429                 return NULL;
430
431         return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
432 }
433
434 int drm_agp_free_memory(void *handle)
435 {
436         device_t agpdev;
437
438         agpdev = DRM_AGP_FIND_DEVICE();
439         if (!agpdev || !handle)
440                 return 0;
441
442         agp_free_memory(agpdev, handle);
443         return 1;
444 }
445
446 int drm_agp_bind_memory(void *handle, off_t start)
447 {
448         device_t agpdev;
449
450         agpdev = DRM_AGP_FIND_DEVICE();
451         if (!agpdev || !handle)
452                 return EINVAL;
453
454         return agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
455 }
456
457 int drm_agp_unbind_memory(void *handle)
458 {
459         device_t agpdev;
460
461         agpdev = DRM_AGP_FIND_DEVICE();
462         if (!agpdev || !handle)
463                 return EINVAL;
464
465         return agp_unbind_memory(agpdev, handle);
466 }