]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm/drm_agpsupport.c
Be a little more forgiving of lame BIOS writers. If a link device that
[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  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "dev/drm/drmP.h"
38
39 #ifdef __FreeBSD__
40 #include <pci/agpreg.h>
41 #include <dev/pci/pcireg.h>
42 #endif
43
44 /* Returns 1 if AGP or 0 if not. */
45 static int
46 drm_device_find_capability(drm_device_t *dev, int cap)
47 {
48         int ret;
49
50         if (dev->driver.device_is_agp != NULL) {
51                 ret = (*dev->driver.device_is_agp)(dev);
52                 
53                 if (ret != DRM_MIGHT_BE_AGP) {
54                         return ret == 2;
55                 }
56         }
57
58 #ifdef __FreeBSD__
59 #if __FreeBSD_version >= 700010
60
61         return (pci_find_extcap(dev->device, cap, NULL) == 0);
62 #else
63         /* Code taken from agp.c.  IWBNI that was a public interface. */
64         u_int32_t status;
65         u_int8_t ptr, next;
66
67         /*
68          * Check the CAP_LIST bit of the PCI status register first.
69          */
70         status = pci_read_config(dev->device, PCIR_STATUS, 2);
71         if (!(status & 0x10))
72                 return 0;
73
74         /*
75          * Traverse the capabilities list.
76          */
77         for (ptr = pci_read_config(dev->device, AGP_CAPPTR, 1);
78              ptr != 0;
79              ptr = next) {
80                 u_int32_t capid = pci_read_config(dev->device, ptr, 4);
81                 next = AGP_CAPID_GET_NEXT_PTR(capid);
82
83                 /*
84                  * If this capability entry ID is cap, then we are done.
85                  */
86                 if (AGP_CAPID_GET_CAP_ID(capid) == cap)
87                         return 1;
88         }
89
90         return 0;
91 #endif
92 #else
93         /* XXX: fill me in for non-FreeBSD */
94         return 1;
95 #endif
96 }
97
98 int drm_device_is_agp(drm_device_t *dev)
99 {
100         if (dev->driver.device_is_agp != NULL) {
101                 int ret;
102
103                 /* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely
104                  * AGP, 2 = fall back to PCI capability
105                  */
106                 ret = (*dev->driver.device_is_agp)(dev);
107                 if (ret != 2)
108                         return ret;
109         }
110
111         return (drm_device_find_capability(dev, PCIY_AGP));
112 }
113
114 int drm_device_is_pcie(drm_device_t *dev)
115 {
116         return (drm_device_find_capability(dev, PCIY_EXPRESS));
117 }
118
119 int drm_agp_info(drm_device_t * dev, drm_agp_info_t *info)
120 {
121         struct agp_info *kern;
122
123         if (!dev->agp || !dev->agp->acquired)
124                 return EINVAL;
125
126         kern                   = &dev->agp->info;
127         agp_get_info(dev->agp->agpdev, kern);
128         info->agp_version_major = 1;
129         info->agp_version_minor = 0;
130         info->mode              = kern->ai_mode;
131         info->aperture_base     = kern->ai_aperture_base;
132         info->aperture_size     = kern->ai_aperture_size;
133         info->memory_allowed    = kern->ai_memory_allowed;
134         info->memory_used       = kern->ai_memory_used;
135         info->id_vendor         = kern->ai_devid & 0xffff;
136         info->id_device         = kern->ai_devid >> 16;
137
138         return 0;
139 }
140
141 int drm_agp_info_ioctl(DRM_IOCTL_ARGS)
142 {
143         int err;
144         drm_agp_info_t info;
145         DRM_DEVICE;
146
147         err = drm_agp_info(dev, &info);
148         if (err != 0)
149                 return err;
150
151         *(drm_agp_info_t *) data = info;
152         return 0;
153 }
154
155 int drm_agp_acquire_ioctl(DRM_IOCTL_ARGS)
156 {
157         DRM_DEVICE;
158
159         return drm_agp_acquire(dev);
160 }
161
162 int drm_agp_acquire(drm_device_t *dev)
163 {
164         int retcode;
165
166         if (!dev->agp || dev->agp->acquired)
167                 return EINVAL;
168
169         retcode = agp_acquire(dev->agp->agpdev);
170         if (retcode)
171                 return retcode;
172
173         dev->agp->acquired = 1;
174         return 0;
175 }
176
177 int drm_agp_release_ioctl(DRM_IOCTL_ARGS)
178 {
179         DRM_DEVICE;
180
181         return drm_agp_release(dev);
182 }
183
184 int drm_agp_release(drm_device_t * dev)
185 {
186         if (!dev->agp || !dev->agp->acquired)
187                 return EINVAL;
188         agp_release(dev->agp->agpdev);
189         dev->agp->acquired = 0;
190         return 0;
191 }
192
193 int drm_agp_enable(drm_device_t *dev, drm_agp_mode_t mode)
194 {
195
196         if (!dev->agp || !dev->agp->acquired)
197                 return EINVAL;
198         
199         dev->agp->mode    = mode.mode;
200         agp_enable(dev->agp->agpdev, mode.mode);
201         dev->agp->base    = dev->agp->info.ai_aperture_base;
202         dev->agp->enabled = 1;
203         return 0;
204 }
205
206 int drm_agp_enable_ioctl(DRM_IOCTL_ARGS)
207 {
208         drm_agp_mode_t mode;
209         DRM_DEVICE;
210
211         mode = *(drm_agp_mode_t *) data;
212
213         return drm_agp_enable(dev, mode);
214 }
215
216 int drm_agp_alloc(drm_device_t *dev, drm_agp_buffer_t *request)
217 {
218         drm_agp_mem_t    *entry;
219         void             *handle;
220         unsigned long    pages;
221         u_int32_t        type;
222         struct agp_memory_info info;
223
224         if (!dev->agp || !dev->agp->acquired)
225                 return EINVAL;
226
227         entry = malloc(sizeof(*entry), M_DRM, M_NOWAIT | M_ZERO);
228         if (entry == NULL)
229                 return ENOMEM;
230
231         pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
232         type = (u_int32_t) request->type;
233
234         DRM_UNLOCK();
235         handle = drm_agp_allocate_memory(pages, type);
236         DRM_LOCK();
237         if (handle == NULL) {
238                 free(entry, M_DRM);
239                 return ENOMEM;
240         }
241         
242         entry->handle    = handle;
243         entry->bound     = 0;
244         entry->pages     = pages;
245         entry->prev      = NULL;
246         entry->next      = dev->agp->memory;
247         if (dev->agp->memory)
248                 dev->agp->memory->prev = entry;
249         dev->agp->memory = entry;
250
251         agp_memory_info(dev->agp->agpdev, entry->handle, &info);
252
253         request->handle   = (unsigned long) entry->handle;
254         request->physical = info.ami_physical;
255
256         return 0;
257 }
258
259 int drm_agp_alloc_ioctl(DRM_IOCTL_ARGS)
260 {
261         DRM_DEVICE;
262         drm_agp_buffer_t request;
263         int retcode;
264
265         request = *(drm_agp_buffer_t *) data;
266
267         DRM_LOCK();
268         retcode = drm_agp_alloc(dev, &request);
269         DRM_UNLOCK();
270
271         *(drm_agp_buffer_t *) data = request;
272
273         return retcode;
274 }
275
276 static drm_agp_mem_t * drm_agp_lookup_entry(drm_device_t *dev, void *handle)
277 {
278         drm_agp_mem_t *entry;
279
280         for (entry = dev->agp->memory; entry; entry = entry->next) {
281                 if (entry->handle == handle) return entry;
282         }
283         return NULL;
284 }
285
286 int drm_agp_unbind(drm_device_t *dev, drm_agp_binding_t *request)
287 {
288         drm_agp_mem_t     *entry;
289         int retcode;
290
291         if (!dev->agp || !dev->agp->acquired)
292                 return EINVAL;
293
294         entry = drm_agp_lookup_entry(dev, (void *)request->handle);
295         if (entry == NULL || !entry->bound)
296                 return EINVAL;
297
298         DRM_UNLOCK();
299         retcode = drm_agp_unbind_memory(entry->handle);
300         DRM_LOCK();
301
302         if (retcode == 0)
303                 entry->bound = 0;
304
305         return retcode;
306 }
307
308 int drm_agp_unbind_ioctl(DRM_IOCTL_ARGS)
309 {
310         DRM_DEVICE;
311         drm_agp_binding_t request;
312         int retcode;
313
314         request = *(drm_agp_binding_t *) data;
315
316         DRM_LOCK();
317         retcode = drm_agp_unbind(dev, &request);
318         DRM_UNLOCK();
319
320         return retcode;
321 }
322
323 int drm_agp_bind(drm_device_t *dev, drm_agp_binding_t *request)
324 {
325         drm_agp_mem_t     *entry;
326         int               retcode;
327         int               page;
328         
329         if (!dev->agp || !dev->agp->acquired)
330                 return EINVAL;
331
332         DRM_DEBUG("agp_bind, page_size=%x\n", PAGE_SIZE);
333
334         entry = drm_agp_lookup_entry(dev, (void *)request->handle);
335         if (entry == NULL || entry->bound)
336                 return EINVAL;
337
338         page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
339
340         DRM_UNLOCK();
341         retcode = drm_agp_bind_memory(entry->handle, page);
342         DRM_LOCK();
343         if (retcode == 0)
344                 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
345
346         return retcode;
347 }
348
349 int drm_agp_bind_ioctl(DRM_IOCTL_ARGS)
350 {
351         DRM_DEVICE;
352         drm_agp_binding_t request;
353         int retcode;
354
355         request = *(drm_agp_binding_t *) data;
356
357         DRM_LOCK();
358         retcode = drm_agp_bind(dev, &request);
359         DRM_UNLOCK();
360
361         return retcode;
362 }
363
364 int drm_agp_free(drm_device_t *dev, drm_agp_buffer_t *request)
365 {
366         drm_agp_mem_t    *entry;
367         
368         if (!dev->agp || !dev->agp->acquired)
369                 return EINVAL;
370
371         entry = drm_agp_lookup_entry(dev, (void*)request->handle);
372         if (entry == NULL)
373                 return EINVAL;
374    
375         if (entry->prev)
376                 entry->prev->next = entry->next;
377         else
378                 dev->agp->memory  = entry->next;
379         if (entry->next)
380                 entry->next->prev = entry->prev;
381
382         DRM_UNLOCK();
383         if (entry->bound)
384                 drm_agp_unbind_memory(entry->handle);
385         drm_agp_free_memory(entry->handle);
386         DRM_LOCK();
387
388         free(entry, M_DRM);
389
390         return 0;
391
392 }
393
394 int drm_agp_free_ioctl(DRM_IOCTL_ARGS)
395 {
396         DRM_DEVICE;
397         drm_agp_buffer_t request;
398         int retcode;
399
400         request = *(drm_agp_buffer_t *) data;
401
402         DRM_LOCK();
403         retcode = drm_agp_free(dev, &request);
404         DRM_UNLOCK();
405
406         return retcode;
407 }
408
409 drm_agp_head_t *drm_agp_init(void)
410 {
411         device_t agpdev;
412         drm_agp_head_t *head   = NULL;
413         int      agp_available = 1;
414    
415         agpdev = DRM_AGP_FIND_DEVICE();
416         if (!agpdev)
417                 agp_available = 0;
418
419         DRM_DEBUG("agp_available = %d\n", agp_available);
420
421         if (agp_available) {
422                 head = malloc(sizeof(*head), M_DRM, M_NOWAIT | M_ZERO);
423                 if (head == NULL)
424                         return NULL;
425                 head->agpdev = agpdev;
426                 agp_get_info(agpdev, &head->info);
427                 head->memory = NULL;
428                 DRM_INFO("AGP at 0x%08lx %dMB\n",
429                          (long)head->info.ai_aperture_base,
430                          (int)(head->info.ai_aperture_size >> 20));
431         }
432         return head;
433 }
434
435 void *drm_agp_allocate_memory(size_t pages, u32 type)
436 {
437         device_t agpdev;
438
439         agpdev = DRM_AGP_FIND_DEVICE();
440         if (!agpdev)
441                 return NULL;
442
443         return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
444 }
445
446 int drm_agp_free_memory(void *handle)
447 {
448         device_t agpdev;
449
450         agpdev = DRM_AGP_FIND_DEVICE();
451         if (!agpdev || !handle)
452                 return 0;
453
454         agp_free_memory(agpdev, handle);
455         return 1;
456 }
457
458 int drm_agp_bind_memory(void *handle, off_t start)
459 {
460         device_t agpdev;
461
462         agpdev = DRM_AGP_FIND_DEVICE();
463         if (!agpdev || !handle)
464                 return EINVAL;
465
466         return agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
467 }
468
469 int drm_agp_unbind_memory(void *handle)
470 {
471         device_t agpdev;
472
473         agpdev = DRM_AGP_FIND_DEVICE();
474         if (!agpdev || !handle)
475                 return EINVAL;
476
477         return agp_unbind_memory(agpdev, handle);
478 }