Qian Cai | 45b2fda | 2019-07-15 09:42:53 -0400 | [diff] [blame^] | 1 | /* |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 2 | * \file drm_agpsupport.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * DRM support for AGP/GART backend |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * \author Rickard E. (Rik) Faith <faith@valinux.com> |
| 6 | * \author Gareth Hughes <gareth@valinux.com> |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. |
| 11 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. |
| 12 | * All Rights Reserved. |
| 13 | * |
| 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: |
| 20 | * |
| 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. |
| 24 | * |
| 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. |
| 32 | */ |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/module.h> |
Sam Ravnborg | 0500c04 | 2019-05-26 19:35:35 +0200 | [diff] [blame] | 35 | #include <linux/pci.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Eric Anholt | 1bb88ed | 2009-01-15 01:16:25 -0800 | [diff] [blame] | 38 | #include <asm/agp.h> |
| 39 | |
Sam Ravnborg | 0500c04 | 2019-05-26 19:35:35 +0200 | [diff] [blame] | 40 | #include <drm/drm_agpsupport.h> |
| 41 | #include <drm/drm_device.h> |
| 42 | #include <drm/drm_drv.h> |
| 43 | #include <drm/drm_file.h> |
| 44 | #include <drm/drm_print.h> |
| 45 | |
| 46 | #include "drm_legacy.h" |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /** |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 49 | * Get AGP information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | * |
| 51 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 52 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | * \param cmd command. |
| 54 | * \param arg pointer to a (output) drm_agp_info structure. |
| 55 | * \return zero on success or a negative number on failure. |
| 56 | * |
| 57 | * Verifies the AGP device has been initialized and acquired and fills in the |
| 58 | * drm_agp_info structure with the information in drm_agp_head::agp_info. |
| 59 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 60 | int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
Daniel Vetter | d2e546b | 2013-12-11 11:34:40 +0100 | [diff] [blame] | 62 | struct agp_kern_info *kern; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | if (!dev->agp || !dev->agp->acquired) |
| 65 | return -EINVAL; |
| 66 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 67 | kern = &dev->agp->agp_info; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 68 | info->agp_version_major = kern->version.major; |
| 69 | info->agp_version_minor = kern->version.minor; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 70 | info->mode = kern->mode; |
| 71 | info->aperture_base = kern->aper_base; |
| 72 | info->aperture_size = kern->aper_size * 1024 * 1024; |
| 73 | info->memory_allowed = kern->max_memory << PAGE_SHIFT; |
| 74 | info->memory_used = kern->current_memory << PAGE_SHIFT; |
| 75 | info->id_vendor = kern->device->vendor; |
| 76 | info->id_device = kern->device->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | EXPORT_SYMBOL(drm_agp_info); |
| 81 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 82 | int drm_agp_info_ioctl(struct drm_device *dev, void *data, |
| 83 | struct drm_file *file_priv) |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 84 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 85 | struct drm_agp_info *info = data; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 86 | int err; |
| 87 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 88 | err = drm_agp_info(dev, info); |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 89 | if (err) |
| 90 | return err; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /** |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 96 | * Acquire the AGP device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | * |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 98 | * \param dev DRM device that is to acquire AGP. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 99 | * \return zero on success or a negative number on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | * |
| 101 | * Verifies the AGP device hasn't been acquired before and calls |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 102 | * \c agp_backend_acquire. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | */ |
Meghana Madhyastha | e3cbeaf | 2017-09-14 13:39:14 +0530 | [diff] [blame] | 104 | int drm_agp_acquire(struct drm_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if (!dev->agp) |
| 107 | return -ENODEV; |
| 108 | if (dev->agp->acquired) |
| 109 | return -EBUSY; |
Meghana Madhyastha | 182e61d | 2017-09-14 13:40:49 +0530 | [diff] [blame] | 110 | dev->agp->bridge = agp_backend_acquire(dev->pdev); |
| 111 | if (!dev->agp->bridge) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | return -ENODEV; |
| 113 | dev->agp->acquired = 1; |
| 114 | return 0; |
| 115 | } |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 116 | EXPORT_SYMBOL(drm_agp_acquire); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 119 | * Acquire the AGP device (ioctl). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | * |
| 121 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 122 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | * \param cmd command. |
| 124 | * \param arg user argument. |
| 125 | * \return zero on success or a negative number on failure. |
| 126 | * |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 127 | * Verifies the AGP device hasn't been acquired before and calls |
| 128 | * \c agp_backend_acquire. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 130 | int drm_agp_acquire_ioctl(struct drm_device *dev, void *data, |
| 131 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 133 | return drm_agp_acquire((struct drm_device *) file_priv->minor->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Release the AGP device. |
| 138 | * |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 139 | * \param dev DRM device that is to release AGP. |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 140 | * \return zero on success or a negative number on failure. |
| 141 | * |
| 142 | * Verifies the AGP device has been acquired and calls \c agp_backend_release. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | */ |
Meghana Madhyastha | e3cbeaf | 2017-09-14 13:39:14 +0530 | [diff] [blame] | 144 | int drm_agp_release(struct drm_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 146 | if (!dev->agp || !dev->agp->acquired) |
| 147 | return -EINVAL; |
| 148 | agp_backend_release(dev->agp->bridge); |
| 149 | dev->agp->acquired = 0; |
| 150 | return 0; |
| 151 | } |
| 152 | EXPORT_SYMBOL(drm_agp_release); |
| 153 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 154 | int drm_agp_release_ioctl(struct drm_device *dev, void *data, |
| 155 | struct drm_file *file_priv) |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 156 | { |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 157 | return drm_agp_release(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Enable the AGP bus. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 162 | * |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 163 | * \param dev DRM device that has previously acquired AGP. |
| 164 | * \param mode Requested AGP mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | * \return zero on success or a negative number on failure. |
| 166 | * |
| 167 | * Verifies the AGP device has been acquired but not enabled, and calls |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 168 | * \c agp_enable. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | */ |
Meghana Madhyastha | e3cbeaf | 2017-09-14 13:39:14 +0530 | [diff] [blame] | 170 | int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | if (!dev->agp || !dev->agp->acquired) |
| 173 | return -EINVAL; |
| 174 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 175 | dev->agp->mode = mode.mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | agp_enable(dev->agp->bridge, mode.mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | dev->agp->enabled = 1; |
| 178 | return 0; |
| 179 | } |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 180 | EXPORT_SYMBOL(drm_agp_enable); |
| 181 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 182 | int drm_agp_enable_ioctl(struct drm_device *dev, void *data, |
| 183 | struct drm_file *file_priv) |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 184 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 185 | struct drm_agp_mode *mode = data; |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 186 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 187 | return drm_agp_enable(dev, *mode); |
Dave Airlie | 7ab9840 | 2005-07-10 16:56:52 +1000 | [diff] [blame] | 188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | /** |
| 191 | * Allocate AGP memory. |
| 192 | * |
| 193 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 194 | * \param file_priv file private pointer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 198 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | * Verifies the AGP device is present and has been acquired, allocates the |
Daniel Vetter | 89c3726 | 2010-08-23 22:53:36 +0200 | [diff] [blame] | 200 | * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 202 | int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 204 | struct drm_agp_mem *entry; |
Daniel Vetter | d2e546b | 2013-12-11 11:34:40 +0100 | [diff] [blame] | 205 | struct agp_memory *memory; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 206 | unsigned long pages; |
| 207 | u32 type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | if (!dev->agp || !dev->agp->acquired) |
| 210 | return -EINVAL; |
Meghana Madhyastha | 182e61d | 2017-09-14 13:40:49 +0530 | [diff] [blame] | 211 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 212 | if (!entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | return -ENOMEM; |
| 214 | |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 215 | pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE; |
| 216 | type = (u32) request->type; |
Meghana Madhyastha | 182e61d | 2017-09-14 13:40:49 +0530 | [diff] [blame] | 217 | memory = agp_allocate_memory(dev->agp->bridge, pages, type); |
| 218 | if (!memory) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 219 | kfree(entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return -ENOMEM; |
| 221 | } |
| 222 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 223 | entry->handle = (unsigned long)memory->key + 1; |
| 224 | entry->memory = memory; |
| 225 | entry->bound = 0; |
| 226 | entry->pages = pages; |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 227 | list_add(&entry->head, &dev->agp->memory); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 229 | request->handle = entry->handle; |
| 230 | request->physical = memory->physical; |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | EXPORT_SYMBOL(drm_agp_alloc); |
| 235 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 236 | |
| 237 | int drm_agp_alloc_ioctl(struct drm_device *dev, void *data, |
| 238 | struct drm_file *file_priv) |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 239 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 240 | struct drm_agp_buffer *request = data; |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 241 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 242 | return drm_agp_alloc(dev, request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Search for the AGP memory entry associated with a handle. |
| 247 | * |
| 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. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 251 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | * Walks through drm_agp_head::memory until finding a matching handle. |
| 253 | */ |
Meghana Madhyastha | e3cbeaf | 2017-09-14 13:39:14 +0530 | [diff] [blame] | 254 | static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device *dev, |
| 255 | unsigned long handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 257 | struct drm_agp_mem *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 259 | list_for_each_entry(entry, &dev->agp->memory, head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | if (entry->handle == handle) |
| 261 | return entry; |
| 262 | } |
| 263 | return NULL; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Unbind AGP memory from the GATT (ioctl). |
| 268 | * |
| 269 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 270 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
| 274 | * |
| 275 | * Verifies the AGP device is present and acquired, looks-up the AGP memory |
| 276 | * entry and passes it to the unbind_agp() function. |
| 277 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 278 | int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 280 | struct drm_agp_mem *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | int ret; |
| 282 | |
| 283 | if (!dev->agp || !dev->agp->acquired) |
| 284 | return -EINVAL; |
Meghana Madhyastha | 182e61d | 2017-09-14 13:40:49 +0530 | [diff] [blame] | 285 | entry = drm_agp_lookup_entry(dev, request->handle); |
| 286 | if (!entry || !entry->bound) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | return -EINVAL; |
| 288 | ret = drm_unbind_agp(entry->memory); |
| 289 | if (ret == 0) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 290 | entry->bound = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | return ret; |
| 292 | } |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 293 | EXPORT_SYMBOL(drm_agp_unbind); |
| 294 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 295 | |
| 296 | int drm_agp_unbind_ioctl(struct drm_device *dev, void *data, |
| 297 | struct drm_file *file_priv) |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 298 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 299 | struct drm_agp_binding *request = data; |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 300 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 301 | return drm_agp_unbind(dev, request); |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 302 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
| 304 | /** |
| 305 | * Bind AGP memory into the GATT (ioctl) |
| 306 | * |
| 307 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 308 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | * \param cmd command. |
| 310 | * \param arg pointer to a drm_agp_binding structure. |
| 311 | * \return zero on success or a negative number on failure. |
| 312 | * |
| 313 | * Verifies the AGP device is present and has been acquired and that no memory |
| 314 | * is currently bound into the GATT. Looks-up the AGP memory entry and passes |
| 315 | * it to bind_agp() function. |
| 316 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 317 | int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 319 | struct drm_agp_mem *entry; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 320 | int retcode; |
| 321 | int page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
| 323 | if (!dev->agp || !dev->agp->acquired) |
| 324 | return -EINVAL; |
Meghana Madhyastha | 182e61d | 2017-09-14 13:40:49 +0530 | [diff] [blame] | 325 | entry = drm_agp_lookup_entry(dev, request->handle); |
| 326 | if (!entry || entry->bound) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | return -EINVAL; |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 328 | page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE; |
Meghana Madhyastha | 182e61d | 2017-09-14 13:40:49 +0530 | [diff] [blame] | 329 | retcode = drm_bind_agp(entry->memory, page); |
| 330 | if (retcode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | return retcode; |
| 332 | entry->bound = dev->agp->base + (page << PAGE_SHIFT); |
| 333 | DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n", |
| 334 | dev->agp->base, entry->bound); |
| 335 | return 0; |
| 336 | } |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 337 | EXPORT_SYMBOL(drm_agp_bind); |
| 338 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 339 | |
| 340 | int drm_agp_bind_ioctl(struct drm_device *dev, void *data, |
| 341 | struct drm_file *file_priv) |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 342 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 343 | struct drm_agp_binding *request = data; |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 344 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 345 | return drm_agp_bind(dev, request); |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | /** |
| 349 | * Free AGP memory (ioctl). |
| 350 | * |
| 351 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 352 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | * \param cmd command. |
| 354 | * \param arg pointer to a drm_agp_buffer structure. |
| 355 | * \return zero on success or a negative number on failure. |
| 356 | * |
| 357 | * Verifies the AGP device is present and has been acquired and looks up the |
Matt Roper | 1e55a53 | 2019-02-01 17:23:26 -0800 | [diff] [blame] | 358 | * AGP memory entry. If the memory is currently bound, unbind it via |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | * unbind_agp(). Frees it via free_agp() as well as the entry itself |
| 360 | * and unlinks from the doubly linked list it's inserted in. |
| 361 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 362 | int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 364 | struct drm_agp_mem *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | if (!dev->agp || !dev->agp->acquired) |
| 367 | return -EINVAL; |
Meghana Madhyastha | 182e61d | 2017-09-14 13:40:49 +0530 | [diff] [blame] | 368 | entry = drm_agp_lookup_entry(dev, request->handle); |
| 369 | if (!entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | return -EINVAL; |
| 371 | if (entry->bound) |
| 372 | drm_unbind_agp(entry->memory); |
| 373 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 374 | list_del(&entry->head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
| 376 | drm_free_agp(entry->memory, entry->pages); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 377 | kfree(entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | return 0; |
| 379 | } |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 380 | EXPORT_SYMBOL(drm_agp_free); |
| 381 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 382 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 383 | int drm_agp_free_ioctl(struct drm_device *dev, void *data, |
| 384 | struct drm_file *file_priv) |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 385 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 386 | struct drm_agp_buffer *request = data; |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 387 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 388 | return drm_agp_free(dev, request); |
Dave Airlie | efa5839 | 2005-11-11 22:33:39 +1100 | [diff] [blame] | 389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
| 391 | /** |
| 392 | * Initialize the AGP resources. |
| 393 | * |
| 394 | * \return pointer to a drm_agp_head structure. |
| 395 | * |
Dave Airlie | d985c10 | 2006-01-02 21:32:48 +1100 | [diff] [blame] | 396 | * Gets the drm_agp_t structure which is made available by the agpgart module |
| 397 | * via the inter_module_* functions. Creates and initializes a drm_agp_head |
| 398 | * structure. |
Daniel Vetter | d6e4b28 | 2013-12-11 11:34:37 +0100 | [diff] [blame] | 399 | * |
| 400 | * Note that final cleanup of the kmalloced structure is directly done in |
| 401 | * drm_pci_agp_destroy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | */ |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 403 | struct drm_agp_head *drm_agp_init(struct drm_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | { |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 405 | struct drm_agp_head *head = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
Meghana Madhyastha | 182e61d | 2017-09-14 13:40:49 +0530 | [diff] [blame] | 407 | head = kzalloc(sizeof(*head), GFP_KERNEL); |
| 408 | if (!head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | head->bridge = agp_find_bridge(dev->pdev); |
| 411 | if (!head->bridge) { |
Meghana Madhyastha | 182e61d | 2017-09-14 13:40:49 +0530 | [diff] [blame] | 412 | head->bridge = agp_backend_acquire(dev->pdev); |
| 413 | if (!head->bridge) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 414 | kfree(head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | return NULL; |
| 416 | } |
| 417 | agp_copy_info(head->bridge, &head->agp_info); |
| 418 | agp_backend_release(head->bridge); |
| 419 | } else { |
| 420 | agp_copy_info(head->bridge, &head->agp_info); |
| 421 | } |
| 422 | if (head->agp_info.chipset == NOT_SUPPORTED) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 423 | kfree(head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | return NULL; |
| 425 | } |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 426 | INIT_LIST_HEAD(&head->memory); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | head->cant_use_aperture = head->agp_info.cant_use_aperture; |
| 428 | head->page_mask = head->agp_info.page_mask; |
Eric Anholt | 8b40958 | 2007-11-22 16:40:37 +1000 | [diff] [blame] | 429 | head->base = head->agp_info.aper_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | return head; |
| 431 | } |
Daniel Vetter | 49d66d8 | 2017-01-25 07:26:50 +0100 | [diff] [blame] | 432 | /* Only exported for i810.ko */ |
| 433 | EXPORT_SYMBOL(drm_agp_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 435 | /** |
Daniel Vetter | 366884b | 2016-04-26 19:29:34 +0200 | [diff] [blame] | 436 | * drm_legacy_agp_clear - Clear AGP resource list |
David Herrmann | 28ec711 | 2013-07-27 16:37:00 +0200 | [diff] [blame] | 437 | * @dev: DRM device |
| 438 | * |
| 439 | * Iterate over all AGP resources and remove them. But keep the AGP head |
| 440 | * intact so it can still be used. It is safe to call this if AGP is disabled or |
| 441 | * was already removed. |
| 442 | * |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 443 | * Cleanup is only done for drivers who have DRIVER_LEGACY set. |
David Herrmann | 28ec711 | 2013-07-27 16:37:00 +0200 | [diff] [blame] | 444 | */ |
Daniel Vetter | 366884b | 2016-04-26 19:29:34 +0200 | [diff] [blame] | 445 | void drm_legacy_agp_clear(struct drm_device *dev) |
David Herrmann | 28ec711 | 2013-07-27 16:37:00 +0200 | [diff] [blame] | 446 | { |
| 447 | struct drm_agp_mem *entry, *tempe; |
| 448 | |
Daniel Vetter | d990675 | 2013-12-11 11:34:35 +0100 | [diff] [blame] | 449 | if (!dev->agp) |
David Herrmann | 28ec711 | 2013-07-27 16:37:00 +0200 | [diff] [blame] | 450 | return; |
Daniel Vetter | fa53864 | 2016-08-03 21:11:10 +0200 | [diff] [blame] | 451 | if (!drm_core_check_feature(dev, DRIVER_LEGACY)) |
David Herrmann | 28ec711 | 2013-07-27 16:37:00 +0200 | [diff] [blame] | 452 | return; |
| 453 | |
| 454 | list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) { |
| 455 | if (entry->bound) |
| 456 | drm_unbind_agp(entry->memory); |
| 457 | drm_free_agp(entry->memory, entry->pages); |
| 458 | kfree(entry); |
| 459 | } |
| 460 | INIT_LIST_HEAD(&dev->agp->memory); |
| 461 | |
| 462 | if (dev->agp->acquired) |
| 463 | drm_agp_release(dev); |
| 464 | |
| 465 | dev->agp->acquired = 0; |
| 466 | dev->agp->enabled = 0; |
| 467 | } |