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 #include <dev/drm2/drmP.h>
40 * Get AGP information.
42 * \param inode device inode.
43 * \param file_priv DRM file private.
45 * \param arg pointer to a (output) drm_agp_info structure.
46 * \return zero on success or a negative number on failure.
48 * Verifies the AGP device has been initialized and acquired and fills in the
49 * drm_agp_info structure with the information in drm_agp_head::agp_info.
51 int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
55 if (!dev->agp || !dev->agp->acquired)
58 kern = &dev->agp->agp_info;
59 agp_get_info(dev->agp->bridge, kern);
60 info->agp_version_major = 1;
61 info->agp_version_minor = 0;
62 info->mode = kern->ai_mode;
63 info->aperture_base = kern->ai_aperture_base;
64 info->aperture_size = kern->ai_aperture_size;
65 info->memory_allowed = kern->ai_memory_allowed;
66 info->memory_used = kern->ai_memory_used;
67 info->id_vendor = kern->ai_devid & 0xffff;
68 info->id_device = kern->ai_devid >> 16;
73 EXPORT_SYMBOL(drm_agp_info);
75 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
76 struct drm_file *file_priv)
78 struct drm_agp_info *info = data;
81 err = drm_agp_info(dev, info);
89 * Acquire the AGP device.
91 * \param dev DRM device that is to acquire AGP.
92 * \return zero on success or a negative number on failure.
94 * Verifies the AGP device hasn't been acquired before and calls
95 * \c agp_backend_acquire.
97 int drm_agp_acquire(struct drm_device * dev)
103 if (dev->agp->acquired)
105 retcode = agp_acquire(dev->agp->bridge);
108 dev->agp->acquired = 1;
112 EXPORT_SYMBOL(drm_agp_acquire);
115 * Acquire the AGP device (ioctl).
117 * \param inode device inode.
118 * \param file_priv DRM file private.
119 * \param cmd command.
120 * \param arg user argument.
121 * \return zero on success or a negative number on failure.
123 * Verifies the AGP device hasn't been acquired before and calls
124 * \c agp_backend_acquire.
126 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
127 struct drm_file *file_priv)
129 return drm_agp_acquire((struct drm_device *) file_priv->minor->dev);
133 * Release the AGP device.
135 * \param dev DRM device that is to release AGP.
136 * \return zero on success or a negative number on failure.
138 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
140 int drm_agp_release(struct drm_device * dev)
142 if (!dev->agp || !dev->agp->acquired)
144 agp_release(dev->agp->bridge);
145 dev->agp->acquired = 0;
148 EXPORT_SYMBOL(drm_agp_release);
150 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
151 struct drm_file *file_priv)
153 return drm_agp_release(dev);
157 * Enable the AGP bus.
159 * \param dev DRM device that has previously acquired AGP.
160 * \param mode Requested AGP mode.
161 * \return zero on success or a negative number on failure.
163 * Verifies the AGP device has been acquired but not enabled, and calls
166 int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode)
168 if (!dev->agp || !dev->agp->acquired)
171 dev->agp->mode = mode.mode;
172 agp_enable(dev->agp->bridge, mode.mode);
173 dev->agp->enabled = 1;
177 EXPORT_SYMBOL(drm_agp_enable);
179 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
180 struct drm_file *file_priv)
182 struct drm_agp_mode *mode = data;
184 return drm_agp_enable(dev, *mode);
188 * Allocate AGP memory.
190 * \param inode device inode.
191 * \param file_priv file private pointer.
192 * \param cmd command.
193 * \param arg pointer to a drm_agp_buffer structure.
194 * \return zero on success or a negative number on failure.
196 * Verifies the AGP device is present and has been acquired, allocates the
197 * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
199 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
201 struct drm_agp_mem *entry;
205 struct agp_memory_info info;
207 if (!dev->agp || !dev->agp->acquired)
209 if (!(entry = malloc(sizeof(*entry), DRM_MEM_AGPLISTS, M_NOWAIT)))
212 memset(entry, 0, sizeof(*entry));
214 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
215 type = (u32) request->type;
216 if (!(memory = agp_alloc_memory(dev->agp->bridge, type, pages << PAGE_SHIFT))) {
217 free(entry, DRM_MEM_AGPLISTS);
221 entry->handle = (unsigned long)memory;
222 entry->memory = memory;
224 entry->pages = pages;
225 list_add(&entry->head, &dev->agp->memory);
227 agp_memory_info(dev->agp->bridge, entry->memory, &info);
229 request->handle = entry->handle;
230 request->physical = info.ami_physical;
234 EXPORT_SYMBOL(drm_agp_alloc);
237 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
238 struct drm_file *file_priv)
240 struct drm_agp_buffer *request = data;
242 return drm_agp_alloc(dev, request);
246 * Search for the AGP memory entry associated with a handle.
248 * \param dev DRM device structure.
249 * \param handle AGP memory handle.
250 * \return pointer to the drm_agp_mem structure associated with \p handle.
252 * Walks through drm_agp_head::memory until finding a matching handle.
254 static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
255 unsigned long handle)
257 struct drm_agp_mem *entry;
259 list_for_each_entry(entry, &dev->agp->memory, head) {
260 if (entry->handle == handle)
267 * Unbind AGP memory from the GATT (ioctl).
269 * \param inode device inode.
270 * \param file_priv DRM file private.
271 * \param cmd command.
272 * \param arg pointer to a drm_agp_binding structure.
273 * \return zero on success or a negative number on failure.
275 * Verifies the AGP device is present and acquired, looks-up the AGP memory
276 * entry and passes it to the unbind_agp() function.
278 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
280 struct drm_agp_mem *entry;
283 if (!dev->agp || !dev->agp->acquired)
285 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
289 ret = drm_unbind_agp(entry->memory);
294 EXPORT_SYMBOL(drm_agp_unbind);
297 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
298 struct drm_file *file_priv)
300 struct drm_agp_binding *request = data;
302 return drm_agp_unbind(dev, request);
306 * Bind AGP memory into the GATT (ioctl)
308 * \param inode device inode.
309 * \param file_priv DRM file private.
310 * \param cmd command.
311 * \param arg pointer to a drm_agp_binding structure.
312 * \return zero on success or a negative number on failure.
314 * Verifies the AGP device is present and has been acquired and that no memory
315 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
316 * it to bind_agp() function.
318 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
320 struct drm_agp_mem *entry;
324 if (!dev->agp || !dev->agp->acquired)
326 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
330 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
331 if ((retcode = drm_bind_agp(entry->memory, page)))
333 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
334 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
335 dev->agp->base, entry->bound);
338 EXPORT_SYMBOL(drm_agp_bind);
341 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
342 struct drm_file *file_priv)
344 struct drm_agp_binding *request = data;
346 return drm_agp_bind(dev, request);
350 * Free AGP memory (ioctl).
352 * \param inode device inode.
353 * \param file_priv DRM file private.
354 * \param cmd command.
355 * \param arg pointer to a drm_agp_buffer structure.
356 * \return zero on success or a negative number on failure.
358 * Verifies the AGP device is present and has been acquired and looks up the
359 * AGP memory entry. If the memory it's currently bound, unbind it via
360 * unbind_agp(). Frees it via free_agp() as well as the entry itself
361 * and unlinks from the doubly linked list it's inserted in.
363 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
365 struct drm_agp_mem *entry;
367 if (!dev->agp || !dev->agp->acquired)
369 if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
372 drm_unbind_agp(entry->memory);
374 list_del(&entry->head);
376 drm_free_agp(entry->memory, entry->pages);
377 free(entry, DRM_MEM_AGPLISTS);
380 EXPORT_SYMBOL(drm_agp_free);
384 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
385 struct drm_file *file_priv)
387 struct drm_agp_buffer *request = data;
389 return drm_agp_free(dev, request);
393 * Initialize the AGP resources.
395 * \return pointer to a drm_agp_head structure.
397 * Gets the drm_agp_t structure which is made available by the agpgart module
398 * via the inter_module_* functions. Creates and initializes a drm_agp_head
401 struct drm_agp_head *drm_agp_init(struct drm_device *dev)
403 struct drm_agp_head *head = NULL;
405 if (!(head = malloc(sizeof(*head), DRM_MEM_AGPLISTS, M_NOWAIT)))
407 memset((void *)head, 0, sizeof(*head));
408 head->bridge = agp_find_device();
410 free(head, DRM_MEM_AGPLISTS);
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 */