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 #include <dev/agp/agpreg.h>
42 #include <dev/pci/pcireg.h>
44 /* Returns 1 if AGP or 0 if not. */
46 drm_device_find_capability(struct drm_device *dev, int cap)
49 return (pci_find_cap(dev->device, cap, NULL) == 0);
52 int drm_device_is_agp(struct drm_device *dev)
54 if (dev->driver->device_is_agp != NULL) {
57 /* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely
58 * AGP, 2 = fall back to PCI capability
60 ret = (*dev->driver->device_is_agp)(dev);
61 if (ret != DRM_MIGHT_BE_AGP)
65 return (drm_device_find_capability(dev, PCIY_AGP));
68 int drm_device_is_pcie(struct drm_device *dev)
70 return (drm_device_find_capability(dev, PCIY_EXPRESS));
73 int drm_agp_info(struct drm_device * dev, struct drm_agp_info *info)
75 struct agp_info *kern;
77 if (!dev->agp || !dev->agp->acquired)
80 kern = &dev->agp->info;
81 agp_get_info(dev->agp->agpdev, kern);
82 info->agp_version_major = 1;
83 info->agp_version_minor = 0;
84 info->mode = kern->ai_mode;
85 info->aperture_base = kern->ai_aperture_base;
86 info->aperture_size = kern->ai_aperture_size;
87 info->memory_allowed = kern->ai_memory_allowed;
88 info->memory_used = kern->ai_memory_used;
89 info->id_vendor = kern->ai_devid & 0xffff;
90 info->id_device = kern->ai_devid >> 16;
95 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
96 struct drm_file *file_priv)
99 struct drm_agp_info info;
101 err = drm_agp_info(dev, &info);
105 *(struct drm_agp_info *) data = info;
109 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
110 struct drm_file *file_priv)
113 return drm_agp_acquire(dev);
116 int drm_agp_acquire(struct drm_device *dev)
120 if (!dev->agp || dev->agp->acquired)
123 retcode = agp_acquire(dev->agp->agpdev);
127 dev->agp->acquired = 1;
131 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
132 struct drm_file *file_priv)
135 return drm_agp_release(dev);
138 int drm_agp_release(struct drm_device * dev)
140 if (!dev->agp || !dev->agp->acquired)
142 agp_release(dev->agp->agpdev);
143 dev->agp->acquired = 0;
147 int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
150 if (!dev->agp || !dev->agp->acquired)
153 dev->agp->mode = mode.mode;
154 agp_enable(dev->agp->agpdev, mode.mode);
155 dev->agp->enabled = 1;
159 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
160 struct drm_file *file_priv)
162 struct drm_agp_mode mode;
164 mode = *(struct drm_agp_mode *) data;
166 return drm_agp_enable(dev, mode);
169 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
171 drm_agp_mem_t *entry;
175 struct agp_memory_info info;
177 if (!dev->agp || !dev->agp->acquired)
180 entry = malloc(sizeof(*entry), DRM_MEM_AGPLISTS, M_NOWAIT | M_ZERO);
184 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
185 type = (u_int32_t) request->type;
188 handle = drm_agp_allocate_memory(pages, type);
190 if (handle == NULL) {
191 free(entry, DRM_MEM_AGPLISTS);
195 entry->handle = handle;
197 entry->pages = pages;
199 entry->next = dev->agp->memory;
200 if (dev->agp->memory)
201 dev->agp->memory->prev = entry;
202 dev->agp->memory = entry;
204 agp_memory_info(dev->agp->agpdev, entry->handle, &info);
206 request->handle = (unsigned long) entry->handle;
207 request->physical = info.ami_physical;
212 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
213 struct drm_file *file_priv)
215 struct drm_agp_buffer request;
218 request = *(struct drm_agp_buffer *) data;
221 retcode = drm_agp_alloc(dev, &request);
224 *(struct drm_agp_buffer *) data = request;
229 static drm_agp_mem_t * drm_agp_lookup_entry(struct drm_device *dev,
232 drm_agp_mem_t *entry;
234 for (entry = dev->agp->memory; entry; entry = entry->next) {
235 if (entry->handle == handle) return entry;
240 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
242 drm_agp_mem_t *entry;
245 if (!dev->agp || !dev->agp->acquired)
248 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
249 if (entry == NULL || !entry->bound)
253 retcode = drm_agp_unbind_memory(entry->handle);
262 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
263 struct drm_file *file_priv)
265 struct drm_agp_binding request;
268 request = *(struct drm_agp_binding *) data;
271 retcode = drm_agp_unbind(dev, &request);
277 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
279 drm_agp_mem_t *entry;
283 if (!dev->agp || !dev->agp->acquired)
286 DRM_DEBUG("agp_bind, page_size=%x\n", (int)PAGE_SIZE);
288 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
289 if (entry == NULL || entry->bound)
292 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
295 retcode = drm_agp_bind_memory(entry->handle, page);
298 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
303 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
304 struct drm_file *file_priv)
306 struct drm_agp_binding request;
309 request = *(struct drm_agp_binding *) data;
312 retcode = drm_agp_bind(dev, &request);
318 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
320 drm_agp_mem_t *entry;
322 if (!dev->agp || !dev->agp->acquired)
325 entry = drm_agp_lookup_entry(dev, (void*)request->handle);
330 entry->prev->next = entry->next;
332 dev->agp->memory = entry->next;
334 entry->next->prev = entry->prev;
338 drm_agp_unbind_memory(entry->handle);
339 drm_agp_free_memory(entry->handle);
342 free(entry, DRM_MEM_AGPLISTS);
348 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
349 struct drm_file *file_priv)
351 struct drm_agp_buffer request;
354 request = *(struct drm_agp_buffer *) data;
357 retcode = drm_agp_free(dev, &request);
363 drm_agp_head_t *drm_agp_init(void)
366 drm_agp_head_t *head = NULL;
367 int agp_available = 1;
369 agpdev = DRM_AGP_FIND_DEVICE();
373 DRM_DEBUG("agp_available = %d\n", agp_available);
376 head = malloc(sizeof(*head), DRM_MEM_AGPLISTS,
380 head->agpdev = agpdev;
381 agp_get_info(agpdev, &head->info);
382 head->base = head->info.ai_aperture_base;
384 DRM_INFO("AGP at 0x%08lx %dMB\n",
385 (long)head->info.ai_aperture_base,
386 (int)(head->info.ai_aperture_size >> 20));
391 void *drm_agp_allocate_memory(size_t pages, u32 type)
395 agpdev = DRM_AGP_FIND_DEVICE();
399 return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
402 int drm_agp_free_memory(void *handle)
406 agpdev = DRM_AGP_FIND_DEVICE();
407 if (!agpdev || !handle)
410 agp_free_memory(agpdev, handle);
414 int drm_agp_bind_memory(void *handle, off_t start)
418 agpdev = DRM_AGP_FIND_DEVICE();
419 if (!agpdev || !handle)
422 return agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
425 int drm_agp_unbind_memory(void *handle)
429 agpdev = DRM_AGP_FIND_DEVICE();
430 if (!agpdev || !handle)
433 return agp_unbind_memory(agpdev, handle);