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 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 /** @file drm_agpsupport.c
35 * Support code for tying the kernel AGP support to DRM drivers and
36 * the DRM's AGP ioctls.
39 #include "dev/drm/drmP.h"
41 #if __FreeBSD_version >= 800004
42 #include <dev/agp/agpreg.h>
43 #else /* __FreeBSD_version >= 800004 */
44 #include <pci/agpreg.h>
45 #endif /* __FreeBSD_version >= 800004 */
46 #include <dev/pci/pcireg.h>
48 /* Returns 1 if AGP or 0 if not. */
50 drm_device_find_capability(struct drm_device *dev, int cap)
52 #if __FreeBSD_version >= 602102
54 return (pci_find_cap(dev->device, cap, NULL) == 0);
56 /* Code taken from agp.c. IWBNI that was a public interface. */
61 * Check the CAP_LIST bit of the PCI status register first.
63 status = pci_read_config(dev->device, PCIR_STATUS, 2);
68 * Traverse the capabilities list.
70 for (ptr = pci_read_config(dev->device, AGP_CAPPTR, 1);
73 u_int32_t capid = pci_read_config(dev->device, ptr, 4);
74 next = AGP_CAPID_GET_NEXT_PTR(capid);
77 * If this capability entry ID is cap, then we are done.
79 if (AGP_CAPID_GET_CAP_ID(capid) == cap)
87 int drm_device_is_agp(struct drm_device *dev)
89 if (dev->driver->device_is_agp != NULL) {
92 /* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely
93 * AGP, 2 = fall back to PCI capability
95 ret = (*dev->driver->device_is_agp)(dev);
96 if (ret != DRM_MIGHT_BE_AGP)
100 return (drm_device_find_capability(dev, PCIY_AGP));
103 int drm_device_is_pcie(struct drm_device *dev)
105 return (drm_device_find_capability(dev, PCIY_EXPRESS));
108 int drm_agp_info(struct drm_device * dev, struct drm_agp_info *info)
110 struct agp_info *kern;
112 if (!dev->agp || !dev->agp->acquired)
115 kern = &dev->agp->info;
116 agp_get_info(dev->agp->agpdev, kern);
117 info->agp_version_major = 1;
118 info->agp_version_minor = 0;
119 info->mode = kern->ai_mode;
120 info->aperture_base = kern->ai_aperture_base;
121 info->aperture_size = kern->ai_aperture_size;
122 info->memory_allowed = kern->ai_memory_allowed;
123 info->memory_used = kern->ai_memory_used;
124 info->id_vendor = kern->ai_devid & 0xffff;
125 info->id_device = kern->ai_devid >> 16;
130 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
131 struct drm_file *file_priv)
134 struct drm_agp_info info;
136 err = drm_agp_info(dev, &info);
140 *(struct drm_agp_info *) data = info;
144 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
145 struct drm_file *file_priv)
148 return drm_agp_acquire(dev);
151 int drm_agp_acquire(struct drm_device *dev)
155 if (!dev->agp || dev->agp->acquired)
158 retcode = agp_acquire(dev->agp->agpdev);
162 dev->agp->acquired = 1;
166 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
167 struct drm_file *file_priv)
170 return drm_agp_release(dev);
173 int drm_agp_release(struct drm_device * dev)
175 if (!dev->agp || !dev->agp->acquired)
177 agp_release(dev->agp->agpdev);
178 dev->agp->acquired = 0;
182 int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
185 if (!dev->agp || !dev->agp->acquired)
188 dev->agp->mode = mode.mode;
189 agp_enable(dev->agp->agpdev, mode.mode);
190 dev->agp->enabled = 1;
194 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
195 struct drm_file *file_priv)
197 struct drm_agp_mode mode;
199 mode = *(struct drm_agp_mode *) data;
201 return drm_agp_enable(dev, mode);
204 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
206 drm_agp_mem_t *entry;
210 struct agp_memory_info info;
212 if (!dev->agp || !dev->agp->acquired)
215 entry = malloc(sizeof(*entry), DRM_MEM_AGPLISTS, M_NOWAIT | M_ZERO);
219 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
220 type = (u_int32_t) request->type;
223 handle = drm_agp_allocate_memory(pages, type);
225 if (handle == NULL) {
226 free(entry, DRM_MEM_AGPLISTS);
230 entry->handle = handle;
232 entry->pages = pages;
234 entry->next = dev->agp->memory;
235 if (dev->agp->memory)
236 dev->agp->memory->prev = entry;
237 dev->agp->memory = entry;
239 agp_memory_info(dev->agp->agpdev, entry->handle, &info);
241 request->handle = (unsigned long) entry->handle;
242 request->physical = info.ami_physical;
247 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
248 struct drm_file *file_priv)
250 struct drm_agp_buffer request;
253 request = *(struct drm_agp_buffer *) data;
256 retcode = drm_agp_alloc(dev, &request);
259 *(struct drm_agp_buffer *) data = request;
264 static drm_agp_mem_t * drm_agp_lookup_entry(struct drm_device *dev,
267 drm_agp_mem_t *entry;
269 for (entry = dev->agp->memory; entry; entry = entry->next) {
270 if (entry->handle == handle) return entry;
275 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
277 drm_agp_mem_t *entry;
280 if (!dev->agp || !dev->agp->acquired)
283 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
284 if (entry == NULL || !entry->bound)
288 retcode = drm_agp_unbind_memory(entry->handle);
297 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
298 struct drm_file *file_priv)
300 struct drm_agp_binding request;
303 request = *(struct drm_agp_binding *) data;
306 retcode = drm_agp_unbind(dev, &request);
312 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
314 drm_agp_mem_t *entry;
318 if (!dev->agp || !dev->agp->acquired)
321 DRM_DEBUG("agp_bind, page_size=%x\n", (int)PAGE_SIZE);
323 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
324 if (entry == NULL || entry->bound)
327 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
330 retcode = drm_agp_bind_memory(entry->handle, page);
333 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
338 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
339 struct drm_file *file_priv)
341 struct drm_agp_binding request;
344 request = *(struct drm_agp_binding *) data;
347 retcode = drm_agp_bind(dev, &request);
353 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
355 drm_agp_mem_t *entry;
357 if (!dev->agp || !dev->agp->acquired)
360 entry = drm_agp_lookup_entry(dev, (void*)request->handle);
365 entry->prev->next = entry->next;
367 dev->agp->memory = entry->next;
369 entry->next->prev = entry->prev;
373 drm_agp_unbind_memory(entry->handle);
374 drm_agp_free_memory(entry->handle);
377 free(entry, DRM_MEM_AGPLISTS);
383 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
384 struct drm_file *file_priv)
386 struct drm_agp_buffer request;
389 request = *(struct drm_agp_buffer *) data;
392 retcode = drm_agp_free(dev, &request);
398 drm_agp_head_t *drm_agp_init(void)
401 drm_agp_head_t *head = NULL;
402 int agp_available = 1;
404 agpdev = DRM_AGP_FIND_DEVICE();
408 DRM_DEBUG("agp_available = %d\n", agp_available);
411 head = malloc(sizeof(*head), DRM_MEM_AGPLISTS,
415 head->agpdev = agpdev;
416 agp_get_info(agpdev, &head->info);
417 head->base = head->info.ai_aperture_base;
419 DRM_INFO("AGP at 0x%08lx %dMB\n",
420 (long)head->info.ai_aperture_base,
421 (int)(head->info.ai_aperture_size >> 20));
426 void *drm_agp_allocate_memory(size_t pages, u32 type)
430 agpdev = DRM_AGP_FIND_DEVICE();
434 return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
437 int drm_agp_free_memory(void *handle)
441 agpdev = DRM_AGP_FIND_DEVICE();
442 if (!agpdev || !handle)
445 agp_free_memory(agpdev, handle);
449 int drm_agp_bind_memory(void *handle, off_t start)
453 agpdev = DRM_AGP_FIND_DEVICE();
454 if (!agpdev || !handle)
457 return agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
460 int drm_agp_unbind_memory(void *handle)
464 agpdev = DRM_AGP_FIND_DEVICE();
465 if (!agpdev || !handle)
468 return agp_unbind_memory(agpdev, handle);