2 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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:
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
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.
26 * Rickard E. (Rik) Faith <faith@valinux.com>
27 * Gareth Hughes <gareth@valinux.com>
31 /** @file drm_agpsupport.c
32 * Support code for tying the kernel AGP support to DRM drivers and
33 * the DRM's AGP ioctls.
36 #include "dev/drm/drmP.h"
38 #include <dev/agp/agpreg.h>
39 #include <bus/pci/pcireg.h>
41 /* Returns 1 if AGP or 0 if not. */
43 drm_device_find_capability(struct drm_device
*dev
, int cap
)
45 return (pci_find_extcap(dev
->device
, cap
, NULL
) == 0);
48 int drm_device_is_agp(struct drm_device
*dev
)
50 if (dev
->driver
->device_is_agp
!= NULL
) {
53 /* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely
54 * AGP, 2 = fall back to PCI capability
56 ret
= (*dev
->driver
->device_is_agp
)(dev
);
57 if (ret
!= DRM_MIGHT_BE_AGP
)
61 return (drm_device_find_capability(dev
, PCIY_AGP
));
64 int drm_device_is_pcie(struct drm_device
*dev
)
66 return (drm_device_find_capability(dev
, PCIY_EXPRESS
));
69 int drm_agp_info(struct drm_device
* dev
, struct drm_agp_info
*info
)
71 struct agp_info
*kern
;
73 if (!dev
->agp
|| !dev
->agp
->acquired
)
76 kern
= &dev
->agp
->info
;
77 agp_get_info(dev
->agp
->agpdev
, kern
);
78 info
->agp_version_major
= 1;
79 info
->agp_version_minor
= 0;
80 info
->mode
= kern
->ai_mode
;
81 info
->aperture_base
= kern
->ai_aperture_base
;
82 info
->aperture_size
= kern
->ai_aperture_size
;
83 info
->memory_allowed
= kern
->ai_memory_allowed
;
84 info
->memory_used
= kern
->ai_memory_used
;
85 info
->id_vendor
= kern
->ai_devid
& 0xffff;
86 info
->id_device
= kern
->ai_devid
>> 16;
91 int drm_agp_info_ioctl(struct drm_device
*dev
, void *data
,
92 struct drm_file
*file_priv
)
95 struct drm_agp_info info
;
97 err
= drm_agp_info(dev
, &info
);
101 *(struct drm_agp_info
*) data
= info
;
105 int drm_agp_acquire_ioctl(struct drm_device
*dev
, void *data
,
106 struct drm_file
*file_priv
)
109 return drm_agp_acquire(dev
);
112 int drm_agp_acquire(struct drm_device
*dev
)
116 if (!dev
->agp
|| dev
->agp
->acquired
)
119 retcode
= agp_acquire(dev
->agp
->agpdev
);
123 dev
->agp
->acquired
= 1;
127 int drm_agp_release_ioctl(struct drm_device
*dev
, void *data
,
128 struct drm_file
*file_priv
)
131 return drm_agp_release(dev
);
134 int drm_agp_release(struct drm_device
* dev
)
136 if (!dev
->agp
|| !dev
->agp
->acquired
)
138 agp_release(dev
->agp
->agpdev
);
139 dev
->agp
->acquired
= 0;
143 int drm_agp_enable(struct drm_device
*dev
, struct drm_agp_mode mode
)
146 if (!dev
->agp
|| !dev
->agp
->acquired
)
149 dev
->agp
->mode
= mode
.mode
;
150 agp_enable(dev
->agp
->agpdev
, mode
.mode
);
151 dev
->agp
->enabled
= 1;
155 int drm_agp_enable_ioctl(struct drm_device
*dev
, void *data
,
156 struct drm_file
*file_priv
)
158 struct drm_agp_mode mode
;
160 mode
= *(struct drm_agp_mode
*) data
;
162 return drm_agp_enable(dev
, mode
);
165 int drm_agp_alloc(struct drm_device
*dev
, struct drm_agp_buffer
*request
)
167 drm_agp_mem_t
*entry
;
171 struct agp_memory_info info
;
173 if (!dev
->agp
|| !dev
->agp
->acquired
)
176 entry
= malloc(sizeof(*entry
), DRM_MEM_AGPLISTS
, M_NOWAIT
| M_ZERO
);
180 pages
= (request
->size
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
181 type
= (u_int32_t
) request
->type
;
184 handle
= drm_agp_allocate_memory(pages
, type
);
186 if (handle
== NULL
) {
187 free(entry
, DRM_MEM_AGPLISTS
);
191 entry
->handle
= handle
;
193 entry
->pages
= pages
;
195 entry
->next
= dev
->agp
->memory
;
196 if (dev
->agp
->memory
)
197 dev
->agp
->memory
->prev
= entry
;
198 dev
->agp
->memory
= entry
;
200 agp_memory_info(dev
->agp
->agpdev
, entry
->handle
, &info
);
202 request
->handle
= (unsigned long) entry
->handle
;
203 request
->physical
= info
.ami_physical
;
208 int drm_agp_alloc_ioctl(struct drm_device
*dev
, void *data
,
209 struct drm_file
*file_priv
)
211 struct drm_agp_buffer request
;
214 request
= *(struct drm_agp_buffer
*) data
;
217 retcode
= drm_agp_alloc(dev
, &request
);
220 *(struct drm_agp_buffer
*) data
= request
;
225 static drm_agp_mem_t
* drm_agp_lookup_entry(struct drm_device
*dev
,
228 drm_agp_mem_t
*entry
;
230 for (entry
= dev
->agp
->memory
; entry
; entry
= entry
->next
) {
231 if (entry
->handle
== handle
) return entry
;
236 int drm_agp_unbind(struct drm_device
*dev
, struct drm_agp_binding
*request
)
238 drm_agp_mem_t
*entry
;
241 if (!dev
->agp
|| !dev
->agp
->acquired
)
244 entry
= drm_agp_lookup_entry(dev
, (void *)request
->handle
);
245 if (entry
== NULL
|| !entry
->bound
)
249 retcode
= drm_agp_unbind_memory(entry
->handle
);
258 int drm_agp_unbind_ioctl(struct drm_device
*dev
, void *data
,
259 struct drm_file
*file_priv
)
261 struct drm_agp_binding request
;
264 request
= *(struct drm_agp_binding
*) data
;
267 retcode
= drm_agp_unbind(dev
, &request
);
273 int drm_agp_bind(struct drm_device
*dev
, struct drm_agp_binding
*request
)
275 drm_agp_mem_t
*entry
;
279 if (!dev
->agp
|| !dev
->agp
->acquired
)
282 DRM_DEBUG("agp_bind, page_size=%x\n", PAGE_SIZE
);
284 entry
= drm_agp_lookup_entry(dev
, (void *)request
->handle
);
285 if (entry
== NULL
|| entry
->bound
)
288 page
= (request
->offset
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
291 retcode
= drm_agp_bind_memory(entry
->handle
, page
);
294 entry
->bound
= dev
->agp
->base
+ (page
<< PAGE_SHIFT
);
299 int drm_agp_bind_ioctl(struct drm_device
*dev
, void *data
,
300 struct drm_file
*file_priv
)
302 struct drm_agp_binding request
;
305 request
= *(struct drm_agp_binding
*) data
;
308 retcode
= drm_agp_bind(dev
, &request
);
314 int drm_agp_free(struct drm_device
*dev
, struct drm_agp_buffer
*request
)
316 drm_agp_mem_t
*entry
;
318 if (!dev
->agp
|| !dev
->agp
->acquired
)
321 entry
= drm_agp_lookup_entry(dev
, (void*)request
->handle
);
326 entry
->prev
->next
= entry
->next
;
328 dev
->agp
->memory
= entry
->next
;
330 entry
->next
->prev
= entry
->prev
;
334 drm_agp_unbind_memory(entry
->handle
);
335 drm_agp_free_memory(entry
->handle
);
338 free(entry
, DRM_MEM_AGPLISTS
);
344 int drm_agp_free_ioctl(struct drm_device
*dev
, void *data
,
345 struct drm_file
*file_priv
)
347 struct drm_agp_buffer request
;
350 request
= *(struct drm_agp_buffer
*) data
;
353 retcode
= drm_agp_free(dev
, &request
);
359 drm_agp_head_t
*drm_agp_init(void)
362 drm_agp_head_t
*head
= NULL
;
363 int agp_available
= 1;
365 agpdev
= DRM_AGP_FIND_DEVICE();
369 DRM_DEBUG("agp_available = %d\n", agp_available
);
372 head
= malloc(sizeof(*head
), DRM_MEM_AGPLISTS
,
376 head
->agpdev
= agpdev
;
377 agp_get_info(agpdev
, &head
->info
);
378 head
->base
= head
->info
.ai_aperture_base
;
380 DRM_INFO("AGP at 0x%08lx %dMB\n",
381 (long)head
->info
.ai_aperture_base
,
382 (int)(head
->info
.ai_aperture_size
>> 20));
387 void *drm_agp_allocate_memory(size_t pages
, u32 type
)
391 agpdev
= DRM_AGP_FIND_DEVICE();
395 return agp_alloc_memory(agpdev
, type
, pages
<< AGP_PAGE_SHIFT
);
398 int drm_agp_free_memory(void *handle
)
402 agpdev
= DRM_AGP_FIND_DEVICE();
403 if (!agpdev
|| !handle
)
406 agp_free_memory(agpdev
, handle
);
410 int drm_agp_bind_memory(void *handle
, off_t start
)
414 agpdev
= DRM_AGP_FIND_DEVICE();
415 if (!agpdev
|| !handle
)
418 return agp_bind_memory(agpdev
, handle
, start
* PAGE_SIZE
);
421 int drm_agp_unbind_memory(void *handle
)
425 agpdev
= DRM_AGP_FIND_DEVICE();
426 if (!agpdev
|| !handle
)
429 return agp_unbind_memory(agpdev
, handle
);