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 /** @file drm_agpsupport.c
32 * Support code for tying the kernel AGP support to DRM drivers and
33 * the DRM's AGP ioctls.
36 #include "dev/drm/drmP.h"
38 #include <dev/agp/agpreg.h>
39 #include <bus/pci/pcireg.h>
41 /* Returns 1 if AGP or 0 if not. */
43 drm_device_find_capability(struct drm_device *dev, int cap)
45 /* Code taken from agp.c. IWBNI that was a public interface. */
50 * Check the CAP_LIST bit of the PCI status register first.
52 status = pci_read_config(dev->device, PCIR_STATUS, 2);
57 * Traverse the capabilities list.
59 for (ptr = pci_read_config(dev->device, AGP_CAPPTR, 1);
62 u_int32_t capid = pci_read_config(dev->device, ptr, 4);
63 next = AGP_CAPID_GET_NEXT_PTR(capid);
66 * If this capability entry ID is cap, then we are done.
68 if (AGP_CAPID_GET_CAP_ID(capid) == cap)
75 int drm_device_is_agp(struct drm_device *dev)
77 if (dev->driver->device_is_agp != NULL) {
80 /* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely
81 * AGP, 2 = fall back to PCI capability
83 ret = (*dev->driver->device_is_agp)(dev);
84 if (ret != DRM_MIGHT_BE_AGP)
88 return (drm_device_find_capability(dev, PCIY_AGP));
91 int drm_device_is_pcie(struct drm_device *dev)
93 return (drm_device_find_capability(dev, PCIY_EXPRESS));
96 int drm_agp_info(struct drm_device * dev, struct drm_agp_info *info)
98 struct agp_info *kern;
100 if (!dev->agp || !dev->agp->acquired)
103 kern = &dev->agp->info;
104 agp_get_info(dev->agp->agpdev, kern);
105 info->agp_version_major = 1;
106 info->agp_version_minor = 0;
107 info->mode = kern->ai_mode;
108 info->aperture_base = kern->ai_aperture_base;
109 info->aperture_size = kern->ai_aperture_size;
110 info->memory_allowed = kern->ai_memory_allowed;
111 info->memory_used = kern->ai_memory_used;
112 info->id_vendor = kern->ai_devid & 0xffff;
113 info->id_device = kern->ai_devid >> 16;
118 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
119 struct drm_file *file_priv)
122 struct drm_agp_info info;
124 err = drm_agp_info(dev, &info);
128 *(struct drm_agp_info *) data = info;
132 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
133 struct drm_file *file_priv)
136 return drm_agp_acquire(dev);
139 int drm_agp_acquire(struct drm_device *dev)
143 if (!dev->agp || dev->agp->acquired)
146 retcode = agp_acquire(dev->agp->agpdev);
150 dev->agp->acquired = 1;
154 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
155 struct drm_file *file_priv)
158 return drm_agp_release(dev);
161 int drm_agp_release(struct drm_device * dev)
163 if (!dev->agp || !dev->agp->acquired)
165 agp_release(dev->agp->agpdev);
166 dev->agp->acquired = 0;
170 int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
173 if (!dev->agp || !dev->agp->acquired)
176 dev->agp->mode = mode.mode;
177 agp_enable(dev->agp->agpdev, mode.mode);
178 dev->agp->enabled = 1;
182 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
183 struct drm_file *file_priv)
185 struct drm_agp_mode mode;
187 mode = *(struct drm_agp_mode *) data;
189 return drm_agp_enable(dev, mode);
192 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
194 drm_agp_mem_t *entry;
198 struct agp_memory_info info;
200 if (!dev->agp || !dev->agp->acquired)
203 entry = malloc(sizeof(*entry), DRM_MEM_AGPLISTS, M_NOWAIT | M_ZERO);
207 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
208 type = (u_int32_t) request->type;
211 handle = drm_agp_allocate_memory(pages, type);
213 if (handle == NULL) {
214 free(entry, DRM_MEM_AGPLISTS);
218 entry->handle = handle;
220 entry->pages = pages;
222 entry->next = dev->agp->memory;
223 if (dev->agp->memory)
224 dev->agp->memory->prev = entry;
225 dev->agp->memory = entry;
227 agp_memory_info(dev->agp->agpdev, entry->handle, &info);
229 request->handle = (unsigned long) entry->handle;
230 request->physical = info.ami_physical;
235 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
236 struct drm_file *file_priv)
238 struct drm_agp_buffer request;
241 request = *(struct drm_agp_buffer *) data;
244 retcode = drm_agp_alloc(dev, &request);
247 *(struct drm_agp_buffer *) data = request;
252 static drm_agp_mem_t * drm_agp_lookup_entry(struct drm_device *dev,
255 drm_agp_mem_t *entry;
257 for (entry = dev->agp->memory; entry; entry = entry->next) {
258 if (entry->handle == handle) return entry;
263 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
265 drm_agp_mem_t *entry;
268 if (!dev->agp || !dev->agp->acquired)
271 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
272 if (entry == NULL || !entry->bound)
276 retcode = drm_agp_unbind_memory(entry->handle);
285 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
286 struct drm_file *file_priv)
288 struct drm_agp_binding request;
291 request = *(struct drm_agp_binding *) data;
294 retcode = drm_agp_unbind(dev, &request);
300 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
302 drm_agp_mem_t *entry;
306 if (!dev->agp || !dev->agp->acquired)
309 DRM_DEBUG("agp_bind, page_size=%x\n", PAGE_SIZE);
311 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
312 if (entry == NULL || entry->bound)
315 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
318 retcode = drm_agp_bind_memory(entry->handle, page);
321 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
326 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
327 struct drm_file *file_priv)
329 struct drm_agp_binding request;
332 request = *(struct drm_agp_binding *) data;
335 retcode = drm_agp_bind(dev, &request);
341 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
343 drm_agp_mem_t *entry;
345 if (!dev->agp || !dev->agp->acquired)
348 entry = drm_agp_lookup_entry(dev, (void*)request->handle);
353 entry->prev->next = entry->next;
355 dev->agp->memory = entry->next;
357 entry->next->prev = entry->prev;
361 drm_agp_unbind_memory(entry->handle);
362 drm_agp_free_memory(entry->handle);
365 free(entry, DRM_MEM_AGPLISTS);
371 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
372 struct drm_file *file_priv)
374 struct drm_agp_buffer request;
377 request = *(struct drm_agp_buffer *) data;
380 retcode = drm_agp_free(dev, &request);
386 drm_agp_head_t *drm_agp_init(void)
389 drm_agp_head_t *head = NULL;
390 int agp_available = 1;
392 agpdev = DRM_AGP_FIND_DEVICE();
396 DRM_DEBUG("agp_available = %d\n", agp_available);
399 head = malloc(sizeof(*head), DRM_MEM_AGPLISTS,
403 head->agpdev = agpdev;
404 agp_get_info(agpdev, &head->info);
405 head->base = head->info.ai_aperture_base;
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);