drm/uapi_drm: Update to Linux 4.6
[dragonfly.git] / sys / dev / drm / drm_agpsupport.c
blobf53df4366ffdca89a6205831cd9aa434f922b4b1
1 /**
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>
7 */
9 /*
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
23 * Software.
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 <drm/drmP.h>
35 #include <linux/module.h>
36 #include "drm_legacy.h"
38 #if __OS_HAS_AGP
40 #include <dev/agp/agpreg.h>
41 #include <bus/pci/pcireg.h>
43 /**
44 * Get AGP information.
46 * \param inode device inode.
47 * \param file_priv DRM file private.
48 * \param cmd command.
49 * \param arg pointer to a (output) drm_agp_info structure.
50 * \return zero on success or a negative number on failure.
52 * Verifies the AGP device has been initialized and acquired and fills in the
53 * drm_agp_info structure with the information in drm_agp_head::agp_info.
55 int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
57 struct agp_info *kern;
59 if (!dev->agp || !dev->agp->acquired)
60 return -EINVAL;
62 kern = &dev->agp->agp_info;
63 agp_get_info(dev->agp->agpdev, kern);
64 info->agp_version_major = 1;
65 info->agp_version_minor = 0;
66 info->mode = kern->ai_mode;
67 info->aperture_base = kern->ai_aperture_base;
68 info->aperture_size = kern->ai_aperture_size;
69 info->memory_allowed = kern->ai_memory_allowed;
70 info->memory_used = kern->ai_memory_used;
71 info->id_vendor = kern->ai_devid & 0xffff;
72 info->id_device = kern->ai_devid >> 16;
74 return 0;
77 EXPORT_SYMBOL(drm_agp_info);
79 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
80 struct drm_file *file_priv)
82 struct drm_agp_info info;
83 int err;
85 err = drm_agp_info(dev, &info);
86 if (err)
87 return err;
89 *(struct drm_agp_info *) data = info;
90 return 0;
93 /**
94 * Acquire the AGP device.
96 * \param dev DRM device that is to acquire AGP.
97 * \return zero on success or a negative number on failure.
99 * Verifies the AGP device hasn't been acquired before and calls
100 * \c agp_backend_acquire.
102 int drm_agp_acquire(struct drm_device * dev)
104 int retcode;
106 if (!dev->agp || dev->agp->acquired)
107 return -EINVAL;
109 retcode = -agp_acquire(dev->agp->agpdev);
110 if (retcode)
111 return retcode;
113 dev->agp->acquired = 1;
114 return 0;
117 EXPORT_SYMBOL(drm_agp_acquire);
120 * Acquire the AGP device (ioctl).
122 * \param inode device inode.
123 * \param file_priv DRM file private.
124 * \param cmd command.
125 * \param arg user argument.
126 * \return zero on success or a negative number on failure.
128 * Verifies the AGP device hasn't been acquired before and calls
129 * \c agp_backend_acquire.
131 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
132 struct drm_file *file_priv)
134 return drm_agp_acquire(dev);
138 * Release the AGP device.
140 * \param dev DRM device that is to release AGP.
141 * \return zero on success or a negative number on failure.
143 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
145 int drm_agp_release(struct drm_device * dev)
147 if (!dev->agp || !dev->agp->acquired)
148 return -EINVAL;
149 agp_release(dev->agp->agpdev);
150 dev->agp->acquired = 0;
151 return 0;
153 EXPORT_SYMBOL(drm_agp_release);
155 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
156 struct drm_file *file_priv)
158 return drm_agp_release(dev);
162 * Enable the AGP bus.
164 * \param dev DRM device that has previously acquired AGP.
165 * \param mode Requested AGP mode.
166 * \return zero on success or a negative number on failure.
168 * Verifies the AGP device has been acquired but not enabled, and calls
169 * \c agp_enable.
171 int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode)
173 if (!dev->agp || !dev->agp->acquired)
174 return -EINVAL;
176 dev->agp->mode = mode.mode;
177 agp_enable(dev->agp->agpdev, mode.mode);
178 dev->agp->enabled = 1;
179 return 0;
182 EXPORT_SYMBOL(drm_agp_enable);
184 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
185 struct drm_file *file_priv)
187 struct drm_agp_mode mode;
189 mode = *(struct drm_agp_mode *) data;
191 return drm_agp_enable(dev, mode);
194 static void *drm_agp_allocate_memory(size_t pages, u32 type)
196 device_t agpdev;
198 agpdev = agp_find_device();
199 if (!agpdev)
200 return NULL;
202 return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
206 * Allocate AGP memory.
208 * \param inode device inode.
209 * \param file_priv file private pointer.
210 * \param cmd command.
211 * \param arg pointer to a drm_agp_buffer structure.
212 * \return zero on success or a negative number on failure.
214 * Verifies the AGP device is present and has been acquired, allocates the
215 * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
217 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
219 struct drm_agp_mem *entry;
220 void *handle;
221 unsigned long pages;
222 u_int32_t type;
223 struct agp_memory_info info;
225 if (!dev->agp || !dev->agp->acquired)
226 return -EINVAL;
228 entry = kmalloc(sizeof(*entry), M_DRM, M_WAITOK | M_NULLOK | M_ZERO);
229 if (entry == NULL)
230 return -ENOMEM;
232 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
233 type = (u_int32_t) request->type;
235 handle = drm_agp_allocate_memory(pages, type);
236 if (handle == NULL) {
237 drm_free(entry, M_DRM);
238 return -ENOMEM;
241 entry->handle = handle;
242 entry->bound = 0;
243 entry->pages = pages;
244 entry->prev = NULL;
245 entry->next = dev->agp->memory;
246 if (dev->agp->memory)
247 dev->agp->memory->prev = entry;
248 dev->agp->memory = entry;
250 agp_memory_info(dev->agp->agpdev, entry->handle, &info);
252 request->handle = (unsigned long) entry->handle;
253 request->physical = info.ami_physical;
255 return 0;
257 EXPORT_SYMBOL(drm_agp_alloc);
260 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
261 struct drm_file *file_priv)
263 struct drm_agp_buffer request;
264 int retcode;
266 request = *(struct drm_agp_buffer *) data;
268 retcode = drm_agp_alloc(dev, &request);
270 *(struct drm_agp_buffer *) data = request;
272 return retcode;
276 * Search for the AGP memory entry associated with a handle.
278 * \param dev DRM device structure.
279 * \param handle AGP memory handle.
280 * \return pointer to the drm_agp_mem structure associated with \p handle.
282 * Walks through drm_agp_head::memory until finding a matching handle.
284 static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
285 void *handle)
287 struct drm_agp_mem *entry;
289 for (entry = dev->agp->memory; entry; entry = entry->next) {
290 if (entry->handle == handle)
291 return entry;
293 return NULL;
296 static int drm_agp_unbind_memory(void *handle)
298 device_t agpdev;
300 agpdev = agp_find_device();
301 if (!agpdev || !handle)
302 return -EINVAL;
304 return -agp_unbind_memory(agpdev, handle);
308 * Unbind AGP memory from the GATT (ioctl).
310 * \param inode device inode.
311 * \param file_priv DRM file private.
312 * \param cmd command.
313 * \param arg pointer to a drm_agp_binding structure.
314 * \return zero on success or a negative number on failure.
316 * Verifies the AGP device is present and acquired, looks-up the AGP memory
317 * entry and passes it to the unbind_agp() function.
319 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
321 struct drm_agp_mem *entry;
322 int ret;
324 if (!dev->agp || !dev->agp->acquired)
325 return -EINVAL;
326 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
327 if (entry == NULL || !entry->bound)
328 return -EINVAL;
329 ret = drm_agp_unbind_memory(entry->handle);
330 if (ret == 0)
331 entry->bound = 0;
332 return ret;
334 EXPORT_SYMBOL(drm_agp_unbind);
337 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
338 struct drm_file *file_priv)
340 struct drm_agp_binding request;
342 request = *(struct drm_agp_binding *) data;
344 return drm_agp_unbind(dev, &request);
347 static int drm_agp_bind_memory(void *handle, off_t start)
349 device_t agpdev;
351 agpdev = agp_find_device();
352 if (!agpdev || !handle)
353 return -EINVAL;
355 return -agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
359 * Bind AGP memory into the GATT (ioctl)
361 * \param inode device inode.
362 * \param file_priv DRM file private.
363 * \param cmd command.
364 * \param arg pointer to a drm_agp_binding structure.
365 * \return zero on success or a negative number on failure.
367 * Verifies the AGP device is present and has been acquired and that no memory
368 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
369 * it to bind_agp() function.
371 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
373 struct drm_agp_mem *entry;
374 int retcode;
375 int page;
377 if (!dev->agp || !dev->agp->acquired)
378 return -EINVAL;
380 DRM_DEBUG("agp_bind, page_size=%x\n", (int)PAGE_SIZE);
382 entry = drm_agp_lookup_entry(dev, (void *)request->handle);
383 if (entry == NULL || entry->bound)
384 return -EINVAL;
386 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
388 retcode = drm_agp_bind_memory(entry->handle, page);
389 if (retcode == 0)
390 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
392 return retcode;
394 EXPORT_SYMBOL(drm_agp_bind);
397 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
398 struct drm_file *file_priv)
400 struct drm_agp_binding request;
402 request = *(struct drm_agp_binding *) data;
404 return drm_agp_bind(dev, &request);
407 static int drm_agp_free_memory(void *handle)
409 device_t agpdev;
411 agpdev = agp_find_device();
412 if (!agpdev || !handle)
413 return 0;
415 agp_free_memory(agpdev, handle);
416 return 1;
420 * Free AGP memory (ioctl).
422 * \param inode device inode.
423 * \param file_priv DRM file private.
424 * \param cmd command.
425 * \param arg pointer to a drm_agp_buffer structure.
426 * \return zero on success or a negative number on failure.
428 * Verifies the AGP device is present and has been acquired and looks up the
429 * AGP memory entry. If the memory it's currently bound, unbind it via
430 * unbind_agp(). Frees it via free_agp() as well as the entry itself
431 * and unlinks from the doubly linked list it's inserted in.
433 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
435 struct drm_agp_mem *entry;
437 if (!dev->agp || !dev->agp->acquired)
438 return -EINVAL;
439 entry = drm_agp_lookup_entry(dev, (void*)request->handle);
440 if (entry == NULL)
441 return -EINVAL;
443 if (entry->prev)
444 entry->prev->next = entry->next;
445 else
446 dev->agp->memory = entry->next;
447 if (entry->next)
448 entry->next->prev = entry->prev;
450 if (entry->bound)
451 drm_agp_unbind_memory(entry->handle);
453 drm_agp_free_memory(entry->handle);
454 kfree(entry);
455 return 0;
457 EXPORT_SYMBOL(drm_agp_free);
461 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
462 struct drm_file *file_priv)
464 struct drm_agp_buffer request;
466 request = *(struct drm_agp_buffer *) data;
468 return drm_agp_free(dev, &request);
472 * Initialize the AGP resources.
474 * \return pointer to a drm_agp_head structure.
476 * Gets the drm_agp_t structure which is made available by the agpgart module
477 * via the inter_module_* functions. Creates and initializes a drm_agp_head
478 * structure.
480 * Note that final cleanup of the kmalloced structure is directly done in
481 * drm_pci_agp_destroy.
483 struct drm_agp_head *drm_agp_init(struct drm_device *dev)
485 device_t agpdev;
486 struct drm_agp_head *head = NULL;
487 int agp_available = 1;
489 agpdev = agp_find_device();
490 if (!agpdev)
491 agp_available = 0;
493 DRM_DEBUG("agp_available = %d\n", agp_available);
495 if (agp_available) {
496 head = kmalloc(sizeof(*head), M_DRM,
497 M_WAITOK | M_NULLOK | M_ZERO);
498 if (head == NULL)
499 return NULL;
500 head->agpdev = agpdev;
501 agp_get_info(agpdev, &head->agp_info);
502 head->base = head->agp_info.ai_aperture_base;
503 head->memory = NULL;
504 DRM_INFO("AGP at 0x%08lx %dMB\n",
505 (long)head->agp_info.ai_aperture_base,
506 (int)(head->agp_info.ai_aperture_size >> 20));
508 return head;
512 * drm_agp_clear - Clear AGP resource list
513 * @dev: DRM device
515 * Iterate over all AGP resources and remove them. But keep the AGP head
516 * intact so it can still be used. It is safe to call this if AGP is disabled or
517 * was already removed.
519 * If DRIVER_MODESET is active, nothing is done to protect the modesetting
520 * resources from getting destroyed. Drivers are responsible of cleaning them up
521 * during device shutdown.
523 void drm_agp_clear(struct drm_device *dev)
525 struct drm_agp_mem *entry, *nexte;
527 if (!dev->agp)
528 return;
529 if (drm_core_check_feature(dev, DRIVER_MODESET))
530 return;
532 /* Remove AGP resources, but leave dev->agp intact until
533 * drm_unload is called.
535 for (entry = dev->agp->memory; entry; entry = nexte) {
536 nexte = entry->next;
537 if (entry->bound)
538 drm_agp_unbind_memory(entry->handle);
539 drm_agp_free_memory(entry->handle);
540 kfree(entry);
542 dev->agp->memory = NULL;
544 if (dev->agp->acquired)
545 drm_agp_release(dev);
547 dev->agp->acquired = 0;
548 dev->agp->enabled = 0;
551 #endif /* __OS_HAS_AGP */