2 * \file drm_agpsupport.c
3 * DRM support for AGP/GART backend
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 * OTHER DEALINGS IN THE SOFTWARE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <dev/drm2/drmP.h>
38 #include <linux/slab.h>
43 * Get AGP information.
45 * \param inode device inode.
46 * \param file_priv DRM file private.
48 * \param arg pointer to a (output) drm_agp_info structure.
49 * \return zero on success or a negative number on failure.
51 * Verifies the AGP device has been initialized and acquired and fills in the
52 * drm_agp_info structure with the information in drm_agp_head::agp_info.
54 int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
58 if (!dev->agp || !dev->agp->acquired)
61 kern = &dev->agp->agp_info;
62 agp_get_info(dev->agp->bridge, kern);
63 info->agp_version_major = 1;
64 info->agp_version_minor = 0;
65 info->mode = kern->ai_mode;
66 info->aperture_base = kern->ai_aperture_base;
67 info->aperture_size = kern->ai_aperture_size;
68 info->memory_allowed = kern->ai_memory_allowed;
69 info->memory_used = kern->ai_memory_used;
70 info->id_vendor = kern->ai_devid & 0xffff;
71 info->id_device = kern->ai_devid >> 16;
76 EXPORT_SYMBOL(drm_agp_info);
78 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
79 struct drm_file *file_priv)
81 struct drm_agp_info *info = data;
84 err = drm_agp_info(dev, info);
92 * Acquire the AGP device.
94 * \param dev DRM device that is to acquire AGP.
95 * \return zero on success or a negative number on failure.
97 * Verifies the AGP device hasn't been acquired before and calls
98 * \c agp_backend_acquire.
100 int drm_agp_acquire(struct drm_device * dev)
106 if (dev->agp->acquired)
108 retcode = agp_acquire(dev->agp->bridge);
111 dev->agp->acquired = 1;
115 EXPORT_SYMBOL(drm_agp_acquire);
118 * Acquire the AGP device (ioctl).
120 * \param inode device inode.
121 * \param file_priv DRM file private.
122 * \param cmd command.
123 * \param arg user argument.
124 * \return zero on success or a negative number on failure.
126 * Verifies the AGP device hasn't been acquired before and calls
127 * \c agp_backend_acquire.
129 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
130 struct drm_file *file_priv)
132 return drm_agp_acquire((struct drm_device *) file_priv->minor->dev);
136 * Release the AGP device.
138 * \param dev DRM device that is to release AGP.
139 * \return zero on success or a negative number on failure.
141 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
143 int drm_agp_release(struct drm_device * dev)
145 if (!dev->agp || !dev->agp->acquired)
147 agp_release(dev->agp->bridge);
148 dev->agp->acquired = 0;
151 EXPORT_SYMBOL(drm_agp_release);
153 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
154 struct drm_file *file_priv)
156 return drm_agp_release(dev);
160 * Enable the AGP bus.
162 * \param dev DRM device that has previously acquired AGP.
163 * \param mode Requested AGP mode.
164 * \return zero on success or a negative number on failure.
166 * Verifies the AGP device has been acquired but not enabled, and calls
169 int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode)
171 if (!dev->agp || !dev->agp->acquired)
174 dev->agp->mode = mode.mode;
175 agp_enable(dev->agp->bridge, mode.mode);
176 dev->agp->enabled = 1;
180 EXPORT_SYMBOL(drm_agp_enable);
182 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
183 struct drm_file *file_priv)
185 struct drm_agp_mode *mode = data;
187 return drm_agp_enable(dev, *mode);
191 * Allocate AGP memory.
193 * \param inode device inode.
194 * \param file_priv file private pointer.
195 * \param cmd command.
196 * \param arg pointer to a drm_agp_buffer structure.
197 * \return zero on success or a negative number on failure.
199 * Verifies the AGP device is present and has been acquired, allocates the
200 * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
202 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
204 struct drm_agp_mem *entry;
208 struct agp_memory_info info;
210 if (!dev->agp || !dev->agp->acquired)
212 if (!(entry = kzalloc(sizeof(*entry), GFP_KERNEL)))
215 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
216 type = (u32) request->type;
217 if (!(memory = agp_alloc_memory(dev->agp->bridge, type, pages << PAGE_SHIFT))) {
222 entry->handle = (unsigned long)memory;
223 entry->memory = memory;
225 entry->pages = pages;
226 list_add(&entry->head, &dev->agp->memory);
228 agp_memory_info(dev->agp->bridge, entry->memory, &info);
230 request->handle = entry->handle;
231 request->physical = info.ami_physical;
235 EXPORT_SYMBOL(drm_agp_alloc);
238 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
239 struct drm_file *file_priv)
241 struct drm_agp_buffer *request = data;
243 return drm_agp_alloc(dev, request);
247 * Search for the AGP memory entry associated with a handle.
249 * \param dev DRM device structure.
250 * \param handle AGP memory handle.
251 * \return pointer to the drm_agp_mem structure associated with \p handle.
253 * Walks through drm_agp_head::memory until finding a matching handle.
255 static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
256 unsigned long handle)
258 struct drm_agp_mem *entry;
260 list_for_each_entry(entry, &dev->agp->memory, head) {
261 if (entry->handle == handle)
268 * Unbind AGP memory from the GATT (ioctl).
270 * \param inode device inode.
271 * \param file_priv DRM file private.
272 * \param cmd command.
273 * \param arg pointer to a drm_agp_binding structure.
274 * \return zero on success or a negative number on failure.
276 * Verifies the AGP device is present and acquired, looks-up the AGP memory
277 * entry and passes it to the unbind_agp() function.
279 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
281 struct drm_agp_mem *entry;
284 if (!dev->agp || !dev->agp->acquired)
286 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
290 ret = drm_unbind_agp(entry->memory);
295 EXPORT_SYMBOL(drm_agp_unbind);
298 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
299 struct drm_file *file_priv)
301 struct drm_agp_binding *request = data;
303 return drm_agp_unbind(dev, request);
307 * Bind AGP memory into the GATT (ioctl)
309 * \param inode device inode.
310 * \param file_priv DRM file private.
311 * \param cmd command.
312 * \param arg pointer to a drm_agp_binding structure.
313 * \return zero on success or a negative number on failure.
315 * Verifies the AGP device is present and has been acquired and that no memory
316 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
317 * it to bind_agp() function.
319 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
321 struct drm_agp_mem *entry;
325 if (!dev->agp || !dev->agp->acquired)
327 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
331 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
332 if ((retcode = drm_bind_agp(entry->memory, page)))
334 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
335 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
336 dev->agp->base, entry->bound);
339 EXPORT_SYMBOL(drm_agp_bind);
342 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
343 struct drm_file *file_priv)
345 struct drm_agp_binding *request = data;
347 return drm_agp_bind(dev, request);
351 * Free AGP memory (ioctl).
353 * \param inode device inode.
354 * \param file_priv DRM file private.
355 * \param cmd command.
356 * \param arg pointer to a drm_agp_buffer structure.
357 * \return zero on success or a negative number on failure.
359 * Verifies the AGP device is present and has been acquired and looks up the
360 * AGP memory entry. If the memory it's currently bound, unbind it via
361 * unbind_agp(). Frees it via free_agp() as well as the entry itself
362 * and unlinks from the doubly linked list it's inserted in.
364 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
366 struct drm_agp_mem *entry;
368 if (!dev->agp || !dev->agp->acquired)
370 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
373 drm_unbind_agp(entry->memory);
375 list_del(&entry->head);
377 drm_free_agp(entry->memory, entry->pages);
381 EXPORT_SYMBOL(drm_agp_free);
385 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
386 struct drm_file *file_priv)
388 struct drm_agp_buffer *request = data;
390 return drm_agp_free(dev, request);
394 * Initialize the AGP resources.
396 * \return pointer to a drm_agp_head structure.
398 * Gets the drm_agp_t structure which is made available by the agpgart module
399 * via the inter_module_* functions. Creates and initializes a drm_agp_head
402 struct drm_agp_head *drm_agp_init(struct drm_device *dev)
404 struct drm_agp_head *head = NULL;
406 if (!(head = kzalloc(sizeof(*head), GFP_KERNEL)))
408 head->bridge = agp_find_device();
413 agp_get_info(head->bridge, &head->agp_info);
415 INIT_LIST_HEAD(&head->memory);
416 head->cant_use_aperture = 0;
417 head->base = head->agp_info.ai_aperture_base;
421 #ifdef FREEBSD_NOTYET
423 * Binds a collection of pages into AGP memory at the given offset, returning
424 * the AGP memory structure containing them.
426 * No reference is held on the pages during this time -- it is up to the
427 * caller to handle that.
430 drm_agp_bind_pages(struct drm_device *dev,
432 unsigned long num_pages,
441 mem = agp_allocate_memory(dev->agp->bridge, num_pages,
444 DRM_ERROR("Failed to allocate memory for %ld pages\n",
449 for (i = 0; i < num_pages; i++)
450 mem->pages[i] = pages[i];
451 mem->page_count = num_pages;
453 mem->is_flushed = true;
454 ret = agp_bind_memory(mem, gtt_offset / PAGE_SIZE);
456 DRM_ERROR("Failed to bind AGP memory: %d\n", ret);
457 agp_free_memory(mem);
463 EXPORT_SYMBOL(drm_agp_bind_pages);
464 #endif /* FREEBSD_NOTYET */
466 #endif /* __OS_HAS_AGP */