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>
29 * $FreeBSD: src/sys/dev/drm2/drm_agpsupport.c,v 1.1 2012/05/22 11:07:44 kib Exp $
32 /** @file drm_agpsupport.c
33 * Support code for tying the kernel AGP support to DRM drivers and
34 * the DRM's AGP ioctls.
39 #include <dev/agp/agpreg.h>
40 #include <bus/pci/pcireg.h>
42 /* Returns 1 if AGP or 0 if not. */
44 drm_device_find_capability(struct drm_device *dev, int cap)
46 return (pci_find_extcap(dev->dev, cap, NULL) == 0);
49 int drm_device_is_agp(struct drm_device *dev)
51 if (dev->driver->device_is_agp != NULL) {
54 /* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely
55 * AGP, 2 = fall back to PCI capability
57 ret = (*dev->driver->device_is_agp)(dev);
58 if (ret != DRM_MIGHT_BE_AGP)
62 return (drm_device_find_capability(dev, PCIY_AGP));
65 int drm_device_is_pcie(struct drm_device *dev)
67 return (drm_device_find_capability(dev, PCIY_EXPRESS));
70 int drm_agp_info(struct drm_device * dev, struct drm_agp_info *info)
72 struct agp_info *kern;
74 if (!dev->agp || !dev->agp->acquired)
77 kern = &dev->agp->agp_info;
78 agp_get_info(dev->agp->agpdev, kern);
79 info->agp_version_major = 1;
80 info->agp_version_minor = 0;
81 info->mode = kern->ai_mode;
82 info->aperture_base = kern->ai_aperture_base;
83 info->aperture_size = kern->ai_aperture_size;
84 info->memory_allowed = kern->ai_memory_allowed;
85 info->memory_used = kern->ai_memory_used;
86 info->id_vendor = kern->ai_devid & 0xffff;
87 info->id_device = kern->ai_devid >> 16;
92 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
93 struct drm_file *file_priv)
96 struct drm_agp_info info;
98 err = drm_agp_info(dev, &info);
102 *(struct drm_agp_info *) data = info;
106 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
107 struct drm_file *file_priv)
110 return drm_agp_acquire(dev);
113 int drm_agp_acquire(struct drm_device *dev)
117 if (!dev->agp || dev->agp->acquired)
120 retcode = agp_acquire(dev->agp->agpdev);
124 dev->agp->acquired = 1;
128 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
129 struct drm_file *file_priv)
132 return drm_agp_release(dev);
135 int drm_agp_release(struct drm_device * dev)
137 if (!dev->agp || !dev->agp->acquired)
139 agp_release(dev->agp->agpdev);
140 dev->agp->acquired = 0;
144 int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
147 if (!dev->agp || !dev->agp->acquired)
150 dev->agp->mode = mode.mode;
151 agp_enable(dev->agp->agpdev, mode.mode);
152 dev->agp->enabled = 1;
156 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
157 struct drm_file *file_priv)
159 struct drm_agp_mode mode;
161 mode = *(struct drm_agp_mode *) data;
163 return drm_agp_enable(dev, mode);
166 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
168 drm_agp_mem_t *entry;
172 struct agp_memory_info info;
174 if (!dev->agp || !dev->agp->acquired)
177 entry = kmalloc(sizeof(*entry), DRM_MEM_AGPLISTS, M_NOWAIT | M_ZERO);
181 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
182 type = (u_int32_t) request->type;
184 handle = drm_agp_allocate_memory(pages, type);
185 if (handle == NULL) {
186 drm_free(entry, DRM_MEM_AGPLISTS);
190 entry->handle = handle;
192 entry->pages = pages;
194 entry->next = dev->agp->memory;
195 if (dev->agp->memory)
196 dev->agp->memory->prev = entry;
197 dev->agp->memory = entry;
199 agp_memory_info(dev->agp->agpdev, entry->handle, &info);
201 request->handle = (unsigned long) entry->handle;
202 request->physical = info.ami_physical;
207 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
208 struct drm_file *file_priv)
210 struct drm_agp_buffer request;
213 request = *(struct drm_agp_buffer *) data;
215 retcode = drm_agp_alloc(dev, &request);
217 *(struct drm_agp_buffer *) data = request;
222 static drm_agp_mem_t * drm_agp_lookup_entry(struct drm_device *dev,
225 drm_agp_mem_t *entry;
227 for (entry = dev->agp->memory; entry; entry = entry->next) {
228 if (entry->handle == handle) return entry;
233 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
235 drm_agp_mem_t *entry;
238 if (!dev->agp || !dev->agp->acquired)
241 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
242 if (entry == NULL || !entry->bound)
245 retcode = drm_agp_unbind_memory(entry->handle);
253 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
254 struct drm_file *file_priv)
256 struct drm_agp_binding request;
259 request = *(struct drm_agp_binding *) data;
261 retcode = drm_agp_unbind(dev, &request);
266 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
268 drm_agp_mem_t *entry;
272 if (!dev->agp || !dev->agp->acquired)
275 DRM_DEBUG("agp_bind, page_size=%x\n", (int)PAGE_SIZE);
277 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
278 if (entry == NULL || entry->bound)
281 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
283 retcode = drm_agp_bind_memory(entry->handle, page);
285 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
290 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
291 struct drm_file *file_priv)
293 struct drm_agp_binding request;
296 request = *(struct drm_agp_binding *) data;
298 retcode = drm_agp_bind(dev, &request);
303 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
305 drm_agp_mem_t *entry;
307 if (!dev->agp || !dev->agp->acquired)
310 entry = drm_agp_lookup_entry(dev, (void*)request->handle);
315 entry->prev->next = entry->next;
317 dev->agp->memory = entry->next;
319 entry->next->prev = entry->prev;
322 drm_agp_unbind_memory(entry->handle);
323 drm_agp_free_memory(entry->handle);
325 drm_free(entry, DRM_MEM_AGPLISTS);
331 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
332 struct drm_file *file_priv)
334 struct drm_agp_buffer request;
337 request = *(struct drm_agp_buffer *) data;
339 retcode = drm_agp_free(dev, &request);
344 drm_agp_head_t *drm_agp_init(void)
347 drm_agp_head_t *head = NULL;
348 int agp_available = 1;
350 agpdev = DRM_AGP_FIND_DEVICE();
354 DRM_DEBUG("agp_available = %d\n", agp_available);
357 head = kmalloc(sizeof(*head), DRM_MEM_AGPLISTS,
361 head->agpdev = agpdev;
362 agp_get_info(agpdev, &head->agp_info);
363 head->base = head->agp_info.ai_aperture_base;
365 DRM_INFO("AGP at 0x%08lx %dMB\n",
366 (long)head->agp_info.ai_aperture_base,
367 (int)(head->agp_info.ai_aperture_size >> 20));
372 void *drm_agp_allocate_memory(size_t pages, u32 type)
376 agpdev = DRM_AGP_FIND_DEVICE();
380 return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
383 int drm_agp_free_memory(void *handle)
387 agpdev = DRM_AGP_FIND_DEVICE();
388 if (!agpdev || !handle)
391 agp_free_memory(agpdev, handle);
395 int drm_agp_bind_memory(void *handle, off_t start)
399 agpdev = DRM_AGP_FIND_DEVICE();
400 if (!agpdev || !handle)
403 return agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
406 int drm_agp_unbind_memory(void *handle)
410 agpdev = DRM_AGP_FIND_DEVICE();
411 if (!agpdev || !handle)
414 return agp_unbind_memory(agpdev, handle);