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>
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "dev/drm/drmP.h"
40 #include <pci/agpreg.h>
41 #include <dev/pci/pcireg.h>
44 /* Returns 1 if AGP or 0 if not. */
46 drm_device_find_capability(drm_device_t *dev, int cap)
50 if (dev->driver.device_is_agp != NULL) {
51 ret = (*dev->driver.device_is_agp)(dev);
53 if (ret != DRM_MIGHT_BE_AGP) {
59 #if __FreeBSD_version >= 700010
61 return (pci_find_extcap(dev->device, cap, NULL) == 0);
63 /* Code taken from agp.c. IWBNI that was a public interface. */
68 * Check the CAP_LIST bit of the PCI status register first.
70 status = pci_read_config(dev->device, PCIR_STATUS, 2);
75 * Traverse the capabilities list.
77 for (ptr = pci_read_config(dev->device, AGP_CAPPTR, 1);
80 u_int32_t capid = pci_read_config(dev->device, ptr, 4);
81 next = AGP_CAPID_GET_NEXT_PTR(capid);
84 * If this capability entry ID is cap, then we are done.
86 if (AGP_CAPID_GET_CAP_ID(capid) == cap)
93 /* XXX: fill me in for non-FreeBSD */
98 int drm_device_is_agp(drm_device_t *dev)
100 if (dev->driver.device_is_agp != NULL) {
103 /* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely
104 * AGP, 2 = fall back to PCI capability
106 ret = (*dev->driver.device_is_agp)(dev);
111 return (drm_device_find_capability(dev, PCIY_AGP));
114 int drm_device_is_pcie(drm_device_t *dev)
116 return (drm_device_find_capability(dev, PCIY_EXPRESS));
119 int drm_agp_info(drm_device_t * dev, drm_agp_info_t *info)
121 struct agp_info *kern;
123 if (!dev->agp || !dev->agp->acquired)
126 kern = &dev->agp->info;
127 agp_get_info(dev->agp->agpdev, kern);
128 info->agp_version_major = 1;
129 info->agp_version_minor = 0;
130 info->mode = kern->ai_mode;
131 info->aperture_base = kern->ai_aperture_base;
132 info->aperture_size = kern->ai_aperture_size;
133 info->memory_allowed = kern->ai_memory_allowed;
134 info->memory_used = kern->ai_memory_used;
135 info->id_vendor = kern->ai_devid & 0xffff;
136 info->id_device = kern->ai_devid >> 16;
141 int drm_agp_info_ioctl(DRM_IOCTL_ARGS)
147 err = drm_agp_info(dev, &info);
151 *(drm_agp_info_t *) data = info;
155 int drm_agp_acquire_ioctl(DRM_IOCTL_ARGS)
159 return drm_agp_acquire(dev);
162 int drm_agp_acquire(drm_device_t *dev)
166 if (!dev->agp || dev->agp->acquired)
169 retcode = agp_acquire(dev->agp->agpdev);
173 dev->agp->acquired = 1;
177 int drm_agp_release_ioctl(DRM_IOCTL_ARGS)
181 return drm_agp_release(dev);
184 int drm_agp_release(drm_device_t * dev)
186 if (!dev->agp || !dev->agp->acquired)
188 agp_release(dev->agp->agpdev);
189 dev->agp->acquired = 0;
193 int drm_agp_enable(drm_device_t *dev, drm_agp_mode_t mode)
196 if (!dev->agp || !dev->agp->acquired)
199 dev->agp->mode = mode.mode;
200 agp_enable(dev->agp->agpdev, mode.mode);
201 dev->agp->base = dev->agp->info.ai_aperture_base;
202 dev->agp->enabled = 1;
206 int drm_agp_enable_ioctl(DRM_IOCTL_ARGS)
211 mode = *(drm_agp_mode_t *) data;
213 return drm_agp_enable(dev, mode);
216 int drm_agp_alloc(drm_device_t *dev, drm_agp_buffer_t *request)
218 drm_agp_mem_t *entry;
222 struct agp_memory_info info;
224 if (!dev->agp || !dev->agp->acquired)
227 entry = malloc(sizeof(*entry), M_DRM, M_NOWAIT | M_ZERO);
231 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
232 type = (u_int32_t) request->type;
235 handle = drm_agp_allocate_memory(pages, type);
237 if (handle == NULL) {
242 entry->handle = handle;
244 entry->pages = pages;
246 entry->next = dev->agp->memory;
247 if (dev->agp->memory)
248 dev->agp->memory->prev = entry;
249 dev->agp->memory = entry;
251 agp_memory_info(dev->agp->agpdev, entry->handle, &info);
253 request->handle = (unsigned long) entry->handle;
254 request->physical = info.ami_physical;
259 int drm_agp_alloc_ioctl(DRM_IOCTL_ARGS)
262 drm_agp_buffer_t request;
265 request = *(drm_agp_buffer_t *) data;
268 retcode = drm_agp_alloc(dev, &request);
271 *(drm_agp_buffer_t *) data = request;
276 static drm_agp_mem_t * drm_agp_lookup_entry(drm_device_t *dev, void *handle)
278 drm_agp_mem_t *entry;
280 for (entry = dev->agp->memory; entry; entry = entry->next) {
281 if (entry->handle == handle) return entry;
286 int drm_agp_unbind(drm_device_t *dev, drm_agp_binding_t *request)
288 drm_agp_mem_t *entry;
291 if (!dev->agp || !dev->agp->acquired)
294 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
295 if (entry == NULL || !entry->bound)
299 retcode = drm_agp_unbind_memory(entry->handle);
308 int drm_agp_unbind_ioctl(DRM_IOCTL_ARGS)
311 drm_agp_binding_t request;
314 request = *(drm_agp_binding_t *) data;
317 retcode = drm_agp_unbind(dev, &request);
323 int drm_agp_bind(drm_device_t *dev, drm_agp_binding_t *request)
325 drm_agp_mem_t *entry;
329 if (!dev->agp || !dev->agp->acquired)
332 DRM_DEBUG("agp_bind, page_size=%x\n", PAGE_SIZE);
334 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
335 if (entry == NULL || entry->bound)
338 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
341 retcode = drm_agp_bind_memory(entry->handle, page);
344 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
349 int drm_agp_bind_ioctl(DRM_IOCTL_ARGS)
352 drm_agp_binding_t request;
355 request = *(drm_agp_binding_t *) data;
358 retcode = drm_agp_bind(dev, &request);
364 int drm_agp_free(drm_device_t *dev, drm_agp_buffer_t *request)
366 drm_agp_mem_t *entry;
368 if (!dev->agp || !dev->agp->acquired)
371 entry = drm_agp_lookup_entry(dev, (void*)request->handle);
376 entry->prev->next = entry->next;
378 dev->agp->memory = entry->next;
380 entry->next->prev = entry->prev;
384 drm_agp_unbind_memory(entry->handle);
385 drm_agp_free_memory(entry->handle);
394 int drm_agp_free_ioctl(DRM_IOCTL_ARGS)
397 drm_agp_buffer_t request;
400 request = *(drm_agp_buffer_t *) data;
403 retcode = drm_agp_free(dev, &request);
409 drm_agp_head_t *drm_agp_init(void)
412 drm_agp_head_t *head = NULL;
413 int agp_available = 1;
415 agpdev = DRM_AGP_FIND_DEVICE();
419 DRM_DEBUG("agp_available = %d\n", agp_available);
422 head = malloc(sizeof(*head), M_DRM, M_NOWAIT | M_ZERO);
425 head->agpdev = agpdev;
426 agp_get_info(agpdev, &head->info);
428 DRM_INFO("AGP at 0x%08lx %dMB\n",
429 (long)head->info.ai_aperture_base,
430 (int)(head->info.ai_aperture_size >> 20));
435 void *drm_agp_allocate_memory(size_t pages, u32 type)
439 agpdev = DRM_AGP_FIND_DEVICE();
443 return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
446 int drm_agp_free_memory(void *handle)
450 agpdev = DRM_AGP_FIND_DEVICE();
451 if (!agpdev || !handle)
454 agp_free_memory(agpdev, handle);
458 int drm_agp_bind_memory(void *handle, off_t start)
462 agpdev = DRM_AGP_FIND_DEVICE();
463 if (!agpdev || !handle)
466 return agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
469 int drm_agp_unbind_memory(void *handle)
473 agpdev = DRM_AGP_FIND_DEVICE();
474 if (!agpdev || !handle)
477 return agp_unbind_memory(agpdev, handle);