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
5 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
6 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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:
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
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.
29 * Rickard E. (Rik) Faith <faith@valinux.com>
30 * Gareth Hughes <gareth@valinux.com>
37 #include <pci/agpreg.h>
38 #include <dev/pci/pcireg.h>
41 /* Returns 1 if AGP or 0 if not. */
43 drm_device_find_capability(drm_device_t *dev, int cap)
46 #if __FreeBSD_version >= 602102
48 return (pci_find_extcap(dev->device, cap, NULL) == 0);
50 /* Code taken from agp.c. IWBNI that was a public interface. */
55 * Check the CAP_LIST bit of the PCI status register first.
57 status = pci_read_config(dev->device, PCIR_STATUS, 2);
62 * Traverse the capabilities list.
64 for (ptr = pci_read_config(dev->device, AGP_CAPPTR, 1);
67 u_int32_t capid = pci_read_config(dev->device, ptr, 4);
68 next = AGP_CAPID_GET_NEXT_PTR(capid);
71 * If this capability entry ID is cap, then we are done.
73 if (AGP_CAPID_GET_CAP_ID(capid) == cap)
80 /* XXX: fill me in for non-FreeBSD */
85 int drm_device_is_agp(drm_device_t *dev)
87 if (dev->driver.device_is_agp != NULL) {
90 /* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely
91 * AGP, 2 = fall back to PCI capability
93 ret = (*dev->driver.device_is_agp)(dev);
94 if (ret != DRM_MIGHT_BE_AGP)
98 return (drm_device_find_capability(dev, PCIY_AGP));
101 int drm_device_is_pcie(drm_device_t *dev)
103 return (drm_device_find_capability(dev, PCIY_EXPRESS));
106 int drm_agp_info(drm_device_t * dev, drm_agp_info_t *info)
108 struct agp_info *kern;
110 if (!dev->agp || !dev->agp->acquired)
113 kern = &dev->agp->info;
114 agp_get_info(dev->agp->agpdev, kern);
115 info->agp_version_major = 1;
116 info->agp_version_minor = 0;
117 info->mode = kern->ai_mode;
118 info->aperture_base = kern->ai_aperture_base;
119 info->aperture_size = kern->ai_aperture_size;
120 info->memory_allowed = kern->ai_memory_allowed;
121 info->memory_used = kern->ai_memory_used;
122 info->id_vendor = kern->ai_devid & 0xffff;
123 info->id_device = kern->ai_devid >> 16;
128 int drm_agp_info_ioctl(drm_device_t *dev, void *data, struct drm_file *file_priv)
133 err = drm_agp_info(dev, &info);
137 *(drm_agp_info_t *) data = info;
141 int drm_agp_acquire_ioctl(drm_device_t *dev, void *data, struct drm_file *file_priv)
144 return drm_agp_acquire(dev);
147 int drm_agp_acquire(drm_device_t *dev)
151 if (!dev->agp || dev->agp->acquired)
154 retcode = agp_acquire(dev->agp->agpdev);
158 dev->agp->acquired = 1;
162 int drm_agp_release_ioctl(drm_device_t *dev, void *data, struct drm_file *file_priv)
165 return drm_agp_release(dev);
168 int drm_agp_release(drm_device_t * dev)
170 if (!dev->agp || !dev->agp->acquired)
172 agp_release(dev->agp->agpdev);
173 dev->agp->acquired = 0;
177 int drm_agp_enable(drm_device_t *dev, drm_agp_mode_t mode)
180 if (!dev->agp || !dev->agp->acquired)
183 dev->agp->mode = mode.mode;
184 agp_enable(dev->agp->agpdev, mode.mode);
185 dev->agp->base = dev->agp->info.ai_aperture_base;
186 dev->agp->enabled = 1;
190 int drm_agp_enable_ioctl(drm_device_t *dev, void *data, struct drm_file *file_priv)
194 mode = *(drm_agp_mode_t *) data;
196 return drm_agp_enable(dev, mode);
199 int drm_agp_alloc(drm_device_t *dev, drm_agp_buffer_t *request)
201 drm_agp_mem_t *entry;
205 struct agp_memory_info info;
207 if (!dev->agp || !dev->agp->acquired)
210 entry = malloc(sizeof(*entry), M_DRM, M_NOWAIT | M_ZERO);
214 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
215 type = (u_int32_t) request->type;
218 handle = drm_agp_allocate_memory(pages, type);
220 if (handle == NULL) {
225 entry->handle = handle;
227 entry->pages = pages;
229 entry->next = dev->agp->memory;
230 if (dev->agp->memory)
231 dev->agp->memory->prev = entry;
232 dev->agp->memory = entry;
234 agp_memory_info(dev->agp->agpdev, entry->handle, &info);
236 request->handle = (unsigned long) entry->handle;
237 request->physical = info.ami_physical;
242 int drm_agp_alloc_ioctl(drm_device_t *dev, void *data, struct drm_file *file_priv)
244 drm_agp_buffer_t request;
247 request = *(drm_agp_buffer_t *) data;
250 retcode = drm_agp_alloc(dev, &request);
253 *(drm_agp_buffer_t *) data = request;
258 static drm_agp_mem_t * drm_agp_lookup_entry(drm_device_t *dev, void *handle)
260 drm_agp_mem_t *entry;
262 for (entry = dev->agp->memory; entry; entry = entry->next) {
263 if (entry->handle == handle) return entry;
268 int drm_agp_unbind(drm_device_t *dev, drm_agp_binding_t *request)
270 drm_agp_mem_t *entry;
273 if (!dev->agp || !dev->agp->acquired)
276 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
277 if (entry == NULL || !entry->bound)
281 retcode = drm_agp_unbind_memory(entry->handle);
290 int drm_agp_unbind_ioctl(drm_device_t *dev, void *data, struct drm_file *file_priv)
292 drm_agp_binding_t request;
295 request = *(drm_agp_binding_t *) data;
298 retcode = drm_agp_unbind(dev, &request);
304 int drm_agp_bind(drm_device_t *dev, drm_agp_binding_t *request)
306 drm_agp_mem_t *entry;
310 if (!dev->agp || !dev->agp->acquired)
313 DRM_DEBUG("agp_bind, page_size=%x\n", PAGE_SIZE);
315 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
316 if (entry == NULL || entry->bound)
319 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
322 retcode = drm_agp_bind_memory(entry->handle, page);
325 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
330 int drm_agp_bind_ioctl(drm_device_t *dev, void *data, struct drm_file *file_priv)
332 drm_agp_binding_t request;
335 request = *(drm_agp_binding_t *) data;
338 retcode = drm_agp_bind(dev, &request);
344 int drm_agp_free(drm_device_t *dev, drm_agp_buffer_t *request)
346 drm_agp_mem_t *entry;
348 if (!dev->agp || !dev->agp->acquired)
351 entry = drm_agp_lookup_entry(dev, (void*)request->handle);
356 entry->prev->next = entry->next;
358 dev->agp->memory = entry->next;
360 entry->next->prev = entry->prev;
364 drm_agp_unbind_memory(entry->handle);
365 drm_agp_free_memory(entry->handle);
374 int drm_agp_free_ioctl(drm_device_t *dev, void *data, struct drm_file *file_priv)
376 drm_agp_buffer_t request;
379 request = *(drm_agp_buffer_t *) data;
382 retcode = drm_agp_free(dev, &request);
388 drm_agp_head_t *drm_agp_init(void)
391 drm_agp_head_t *head = NULL;
392 int agp_available = 1;
394 agpdev = DRM_AGP_FIND_DEVICE();
398 DRM_DEBUG("agp_available = %d\n", agp_available);
401 head = malloc(sizeof(*head), M_DRM, M_NOWAIT | M_ZERO);
404 head->agpdev = agpdev;
405 agp_get_info(agpdev, &head->info);
407 DRM_INFO("AGP at 0x%08lx %dMB\n",
408 (long)head->info.ai_aperture_base,
409 (int)(head->info.ai_aperture_size >> 20));
414 void *drm_agp_allocate_memory(size_t pages, u32 type)
418 agpdev = DRM_AGP_FIND_DEVICE();
422 return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
425 int drm_agp_free_memory(void *handle)
429 agpdev = DRM_AGP_FIND_DEVICE();
430 if (!agpdev || !handle)
433 agp_free_memory(agpdev, handle);
437 int drm_agp_bind_memory(void *handle, off_t start)
441 agpdev = DRM_AGP_FIND_DEVICE();
442 if (!agpdev || !handle)
445 return agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
448 int drm_agp_unbind_memory(void *handle)
452 agpdev = DRM_AGP_FIND_DEVICE();
453 if (!agpdev || !handle)
456 return agp_unbind_memory(agpdev, handle);