blob: a4ad6fd13abcf63c88e91900273acc88f861262e [file] [log] [blame]
Qian Cai45b2fda2019-07-15 09:42:53 -04001/*
Dave Airlied985c102006-01-02 21:32:48 +11002 * \file drm_agpsupport.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * DRM support for AGP/GART backend
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * \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 Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020035#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Thomas Zimmermanneb185a32021-05-10 16:52:18 +020038#if IS_ENABLED(CONFIG_AGP)
Eric Anholt1bb88ed2009-01-15 01:16:25 -080039#include <asm/agp.h>
Thomas Zimmermanneb185a32021-05-10 16:52:18 +020040#endif
Eric Anholt1bb88ed2009-01-15 01:16:25 -080041
Sam Ravnborg0500c042019-05-26 19:35:35 +020042#include <drm/drm_device.h>
43#include <drm/drm_drv.h>
44#include <drm/drm_file.h>
45#include <drm/drm_print.h>
46
47#include "drm_legacy.h"
48
Thomas Zimmermann04dfe192021-05-07 20:57:09 +020049#if IS_ENABLED(CONFIG_AGP)
50
Lee Jonescec76382021-01-15 18:15:55 +000051/*
Dave Airlie7ab98402005-07-10 16:56:52 +100052 * Get AGP information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * \return zero on success or a negative number on failure.
55 *
56 * Verifies the AGP device has been initialized and acquired and fills in the
57 * drm_agp_info structure with the information in drm_agp_head::agp_info.
58 */
Thomas Zimmermann04dfe192021-05-07 20:57:09 +020059int drm_legacy_agp_info(struct drm_device *dev, struct drm_agp_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Daniel Vetterd2e546b2013-12-11 11:34:40 +010061 struct agp_kern_info *kern;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 if (!dev->agp || !dev->agp->acquired)
64 return -EINVAL;
65
Dave Airlieb5e89ed2005-09-25 14:28:13 +100066 kern = &dev->agp->agp_info;
Dave Airlie7ab98402005-07-10 16:56:52 +100067 info->agp_version_major = kern->version.major;
68 info->agp_version_minor = kern->version.minor;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100069 info->mode = kern->mode;
70 info->aperture_base = kern->aper_base;
71 info->aperture_size = kern->aper_size * 1024 * 1024;
72 info->memory_allowed = kern->max_memory << PAGE_SHIFT;
73 info->memory_used = kern->current_memory << PAGE_SHIFT;
74 info->id_vendor = kern->device->vendor;
75 info->id_device = kern->device->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Dave Airlie7ab98402005-07-10 16:56:52 +100077 return 0;
78}
Thomas Zimmermann04dfe192021-05-07 20:57:09 +020079EXPORT_SYMBOL(drm_legacy_agp_info);
Dave Airlie7ab98402005-07-10 16:56:52 +100080
Thomas Zimmermann04dfe192021-05-07 20:57:09 +020081int drm_legacy_agp_info_ioctl(struct drm_device *dev, void *data,
82 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +100083{
Eric Anholtc153f452007-09-03 12:06:45 +100084 struct drm_agp_info *info = data;
Dave Airlie7ab98402005-07-10 16:56:52 +100085 int err;
86
Thomas Zimmermann04dfe192021-05-07 20:57:09 +020087 err = drm_legacy_agp_info(dev, info);
Dave Airlie7ab98402005-07-10 16:56:52 +100088 if (err)
89 return err;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return 0;
92}
93
Lee Jonescec76382021-01-15 18:15:55 +000094/*
Dave Airlie7ab98402005-07-10 16:56:52 +100095 * Acquire the AGP device.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Dave Airlied985c102006-01-02 21:32:48 +110097 * \param dev DRM device that is to acquire AGP.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098 * \return zero on success or a negative number on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 *
100 * Verifies the AGP device hasn't been acquired before and calls
Dave Airlie7ab98402005-07-10 16:56:52 +1000101 * \c agp_backend_acquire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200103int drm_legacy_agp_acquire(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Thomas Zimmermann36b73b02021-01-18 14:14:15 +0100105 struct pci_dev *pdev = to_pci_dev(dev->dev);
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (!dev->agp)
108 return -ENODEV;
109 if (dev->agp->acquired)
110 return -EBUSY;
Thomas Zimmermann36b73b02021-01-18 14:14:15 +0100111 dev->agp->bridge = agp_backend_acquire(pdev);
Meghana Madhyastha182e61d2017-09-14 13:40:49 +0530112 if (!dev->agp->bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return -ENODEV;
114 dev->agp->acquired = 1;
115 return 0;
116}
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200117EXPORT_SYMBOL(drm_legacy_agp_acquire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Lee Jonescec76382021-01-15 18:15:55 +0000119/*
Dave Airlie7ab98402005-07-10 16:56:52 +1000120 * Acquire the AGP device (ioctl).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * \return zero on success or a negative number on failure.
123 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000124 * Verifies the AGP device hasn't been acquired before and calls
125 * \c agp_backend_acquire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200127int drm_legacy_agp_acquire_ioctl(struct drm_device *dev, void *data,
128 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200130 return drm_legacy_agp_acquire((struct drm_device *)file_priv->minor->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Lee Jonescec76382021-01-15 18:15:55 +0000133/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * Release the AGP device.
135 *
Dave Airlied985c102006-01-02 21:32:48 +1100136 * \param dev DRM device that is to release AGP.
Dave Airlie7ab98402005-07-10 16:56:52 +1000137 * \return zero on success or a negative number on failure.
138 *
139 * Verifies the AGP device has been acquired and calls \c agp_backend_release.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200141int drm_legacy_agp_release(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Dave Airlie7ab98402005-07-10 16:56:52 +1000143 if (!dev->agp || !dev->agp->acquired)
144 return -EINVAL;
145 agp_backend_release(dev->agp->bridge);
146 dev->agp->acquired = 0;
147 return 0;
148}
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200149EXPORT_SYMBOL(drm_legacy_agp_release);
Dave Airlie7ab98402005-07-10 16:56:52 +1000150
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200151int drm_legacy_agp_release_ioctl(struct drm_device *dev, void *data,
152 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000153{
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200154 return drm_legacy_agp_release(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Lee Jonescec76382021-01-15 18:15:55 +0000157/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * Enable the AGP bus.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000160 * \param dev DRM device that has previously acquired AGP.
161 * \param mode Requested AGP mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * \return zero on success or a negative number on failure.
163 *
164 * Verifies the AGP device has been acquired but not enabled, and calls
Dave Airlie7ab98402005-07-10 16:56:52 +1000165 * \c agp_enable.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200167int drm_legacy_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (!dev->agp || !dev->agp->acquired)
170 return -EINVAL;
171
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000172 dev->agp->mode = mode.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 agp_enable(dev->agp->bridge, mode.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dev->agp->enabled = 1;
175 return 0;
176}
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200177EXPORT_SYMBOL(drm_legacy_agp_enable);
Dave Airlie7ab98402005-07-10 16:56:52 +1000178
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200179int drm_legacy_agp_enable_ioctl(struct drm_device *dev, void *data,
180 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000181{
Eric Anholtc153f452007-09-03 12:06:45 +1000182 struct drm_agp_mode *mode = data;
Dave Airlie7ab98402005-07-10 16:56:52 +1000183
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200184 return drm_legacy_agp_enable(dev, *mode);
Dave Airlie7ab98402005-07-10 16:56:52 +1000185}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Lee Jonescec76382021-01-15 18:15:55 +0000187/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * Allocate AGP memory.
189 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000191 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * Verifies the AGP device is present and has been acquired, allocates the
Daniel Vetter89c37262010-08-23 22:53:36 +0200193 * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200195int drm_legacy_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Dave Airlie55910512007-07-11 16:53:40 +1000197 struct drm_agp_mem *entry;
Daniel Vetterd2e546b2013-12-11 11:34:40 +0100198 struct agp_memory *memory;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000199 unsigned long pages;
200 u32 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (!dev->agp || !dev->agp->acquired)
203 return -EINVAL;
Meghana Madhyastha182e61d2017-09-14 13:40:49 +0530204 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
205 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return -ENOMEM;
207
Wambui Karugab8c8a852019-10-25 12:49:07 +0300208 pages = DIV_ROUND_UP(request->size, PAGE_SIZE);
Dave Airlieefa58392005-11-11 22:33:39 +1100209 type = (u32) request->type;
Meghana Madhyastha182e61d2017-09-14 13:40:49 +0530210 memory = agp_allocate_memory(dev->agp->bridge, pages, type);
211 if (!memory) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700212 kfree(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return -ENOMEM;
214 }
215
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000216 entry->handle = (unsigned long)memory->key + 1;
217 entry->memory = memory;
218 entry->bound = 0;
219 entry->pages = pages;
Dave Airliebd1b3312007-05-26 05:01:51 +1000220 list_add(&entry->head, &dev->agp->memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Dave Airlieefa58392005-11-11 22:33:39 +1100222 request->handle = entry->handle;
223 request->physical = memory->physical;
224
225 return 0;
226}
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200227EXPORT_SYMBOL(drm_legacy_agp_alloc);
Dave Airlieefa58392005-11-11 22:33:39 +1100228
Eric Anholtc153f452007-09-03 12:06:45 +1000229
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200230int drm_legacy_agp_alloc_ioctl(struct drm_device *dev, void *data,
Eric Anholtc153f452007-09-03 12:06:45 +1000231 struct drm_file *file_priv)
Dave Airlieefa58392005-11-11 22:33:39 +1100232{
Eric Anholtc153f452007-09-03 12:06:45 +1000233 struct drm_agp_buffer *request = data;
Dave Airlieefa58392005-11-11 22:33:39 +1100234
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200235 return drm_legacy_agp_alloc(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Lee Jonescec76382021-01-15 18:15:55 +0000238/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * Search for the AGP memory entry associated with a handle.
240 *
241 * \param dev DRM device structure.
242 * \param handle AGP memory handle.
243 * \return pointer to the drm_agp_mem structure associated with \p handle.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000244 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * Walks through drm_agp_head::memory until finding a matching handle.
246 */
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200247static struct drm_agp_mem *drm_legacy_agp_lookup_entry(struct drm_device *dev,
248 unsigned long handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Dave Airlie55910512007-07-11 16:53:40 +1000250 struct drm_agp_mem *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Dave Airliebd1b3312007-05-26 05:01:51 +1000252 list_for_each_entry(entry, &dev->agp->memory, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (entry->handle == handle)
254 return entry;
255 }
256 return NULL;
257}
258
Lee Jonescec76382021-01-15 18:15:55 +0000259/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 * Unbind AGP memory from the GATT (ioctl).
261 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 * \return zero on success or a negative number on failure.
263 *
264 * Verifies the AGP device is present and acquired, looks-up the AGP memory
265 * entry and passes it to the unbind_agp() function.
266 */
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200267int drm_legacy_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Dave Airlie55910512007-07-11 16:53:40 +1000269 struct drm_agp_mem *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 int ret;
271
272 if (!dev->agp || !dev->agp->acquired)
273 return -EINVAL;
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200274 entry = drm_legacy_agp_lookup_entry(dev, request->handle);
Meghana Madhyastha182e61d2017-09-14 13:40:49 +0530275 if (!entry || !entry->bound)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -EINVAL;
Thomas Zimmermannff28a9f2021-01-12 09:10:30 +0100277 ret = agp_unbind_memory(entry->memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (ret == 0)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000279 entry->bound = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return ret;
281}
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200282EXPORT_SYMBOL(drm_legacy_agp_unbind);
Dave Airlieefa58392005-11-11 22:33:39 +1100283
Eric Anholtc153f452007-09-03 12:06:45 +1000284
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200285int drm_legacy_agp_unbind_ioctl(struct drm_device *dev, void *data,
286 struct drm_file *file_priv)
Dave Airlieefa58392005-11-11 22:33:39 +1100287{
Eric Anholtc153f452007-09-03 12:06:45 +1000288 struct drm_agp_binding *request = data;
Dave Airlieefa58392005-11-11 22:33:39 +1100289
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200290 return drm_legacy_agp_unbind(dev, request);
Dave Airlieefa58392005-11-11 22:33:39 +1100291}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Lee Jonescec76382021-01-15 18:15:55 +0000293/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * Bind AGP memory into the GATT (ioctl)
295 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * \return zero on success or a negative number on failure.
297 *
298 * Verifies the AGP device is present and has been acquired and that no memory
299 * is currently bound into the GATT. Looks-up the AGP memory entry and passes
300 * it to bind_agp() function.
301 */
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200302int drm_legacy_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Dave Airlie55910512007-07-11 16:53:40 +1000304 struct drm_agp_mem *entry;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000305 int retcode;
306 int page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 if (!dev->agp || !dev->agp->acquired)
309 return -EINVAL;
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200310 entry = drm_legacy_agp_lookup_entry(dev, request->handle);
Meghana Madhyastha182e61d2017-09-14 13:40:49 +0530311 if (!entry || entry->bound)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return -EINVAL;
Wambui Karugab8c8a852019-10-25 12:49:07 +0300313 page = DIV_ROUND_UP(request->offset, PAGE_SIZE);
Thomas Zimmermannff28a9f2021-01-12 09:10:30 +0100314 retcode = agp_bind_memory(entry->memory, page);
Meghana Madhyastha182e61d2017-09-14 13:40:49 +0530315 if (retcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return retcode;
317 entry->bound = dev->agp->base + (page << PAGE_SHIFT);
318 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
319 dev->agp->base, entry->bound);
320 return 0;
321}
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200322EXPORT_SYMBOL(drm_legacy_agp_bind);
Dave Airlieefa58392005-11-11 22:33:39 +1100323
Eric Anholtc153f452007-09-03 12:06:45 +1000324
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200325int drm_legacy_agp_bind_ioctl(struct drm_device *dev, void *data,
326 struct drm_file *file_priv)
Dave Airlieefa58392005-11-11 22:33:39 +1100327{
Eric Anholtc153f452007-09-03 12:06:45 +1000328 struct drm_agp_binding *request = data;
Dave Airlieefa58392005-11-11 22:33:39 +1100329
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200330 return drm_legacy_agp_bind(dev, request);
Dave Airlieefa58392005-11-11 22:33:39 +1100331}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Lee Jonescec76382021-01-15 18:15:55 +0000333/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * Free AGP memory (ioctl).
335 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * \return zero on success or a negative number on failure.
337 *
338 * Verifies the AGP device is present and has been acquired and looks up the
Matt Roper1e55a532019-02-01 17:23:26 -0800339 * AGP memory entry. If the memory is currently bound, unbind it via
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * unbind_agp(). Frees it via free_agp() as well as the entry itself
341 * and unlinks from the doubly linked list it's inserted in.
342 */
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200343int drm_legacy_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Dave Airlie55910512007-07-11 16:53:40 +1000345 struct drm_agp_mem *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (!dev->agp || !dev->agp->acquired)
348 return -EINVAL;
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200349 entry = drm_legacy_agp_lookup_entry(dev, request->handle);
Meghana Madhyastha182e61d2017-09-14 13:40:49 +0530350 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -EINVAL;
352 if (entry->bound)
Thomas Zimmermannff28a9f2021-01-12 09:10:30 +0100353 agp_unbind_memory(entry->memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Dave Airliebd1b3312007-05-26 05:01:51 +1000355 list_del(&entry->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Thomas Zimmermannff28a9f2021-01-12 09:10:30 +0100357 agp_free_memory(entry->memory);
Eric Anholt9a298b22009-03-24 12:23:04 -0700358 kfree(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 0;
360}
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200361EXPORT_SYMBOL(drm_legacy_agp_free);
Dave Airlieefa58392005-11-11 22:33:39 +1100362
Eric Anholtc153f452007-09-03 12:06:45 +1000363
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200364int drm_legacy_agp_free_ioctl(struct drm_device *dev, void *data,
365 struct drm_file *file_priv)
Dave Airlieefa58392005-11-11 22:33:39 +1100366{
Eric Anholtc153f452007-09-03 12:06:45 +1000367 struct drm_agp_buffer *request = data;
Dave Airlieefa58392005-11-11 22:33:39 +1100368
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200369 return drm_legacy_agp_free(dev, request);
Dave Airlieefa58392005-11-11 22:33:39 +1100370}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Lee Jonescec76382021-01-15 18:15:55 +0000372/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 * Initialize the AGP resources.
374 *
375 * \return pointer to a drm_agp_head structure.
376 *
Dave Airlied985c102006-01-02 21:32:48 +1100377 * Gets the drm_agp_t structure which is made available by the agpgart module
378 * via the inter_module_* functions. Creates and initializes a drm_agp_head
379 * structure.
Daniel Vetterd6e4b282013-12-11 11:34:37 +0100380 *
381 * Note that final cleanup of the kmalloced structure is directly done in
382 * drm_pci_agp_destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200384struct drm_agp_head *drm_legacy_agp_init(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Thomas Zimmermann36b73b02021-01-18 14:14:15 +0100386 struct pci_dev *pdev = to_pci_dev(dev->dev);
Dave Airlie55910512007-07-11 16:53:40 +1000387 struct drm_agp_head *head = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Meghana Madhyastha182e61d2017-09-14 13:40:49 +0530389 head = kzalloc(sizeof(*head), GFP_KERNEL);
390 if (!head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return NULL;
Thomas Zimmermann36b73b02021-01-18 14:14:15 +0100392 head->bridge = agp_find_bridge(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (!head->bridge) {
Thomas Zimmermann36b73b02021-01-18 14:14:15 +0100394 head->bridge = agp_backend_acquire(pdev);
Meghana Madhyastha182e61d2017-09-14 13:40:49 +0530395 if (!head->bridge) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700396 kfree(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return NULL;
398 }
399 agp_copy_info(head->bridge, &head->agp_info);
400 agp_backend_release(head->bridge);
401 } else {
402 agp_copy_info(head->bridge, &head->agp_info);
403 }
404 if (head->agp_info.chipset == NOT_SUPPORTED) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700405 kfree(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return NULL;
407 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000408 INIT_LIST_HEAD(&head->memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 head->cant_use_aperture = head->agp_info.cant_use_aperture;
410 head->page_mask = head->agp_info.page_mask;
Eric Anholt8b409582007-11-22 16:40:37 +1000411 head->base = head->agp_info.aper_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return head;
413}
Daniel Vetter49d66d82017-01-25 07:26:50 +0100414/* Only exported for i810.ko */
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200415EXPORT_SYMBOL(drm_legacy_agp_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Eric Anholt673a3942008-07-30 12:06:12 -0700417/**
Daniel Vetter366884b2016-04-26 19:29:34 +0200418 * drm_legacy_agp_clear - Clear AGP resource list
David Herrmann28ec7112013-07-27 16:37:00 +0200419 * @dev: DRM device
420 *
421 * Iterate over all AGP resources and remove them. But keep the AGP head
422 * intact so it can still be used. It is safe to call this if AGP is disabled or
423 * was already removed.
424 *
Daniel Vetterfa538642016-08-03 21:11:10 +0200425 * Cleanup is only done for drivers who have DRIVER_LEGACY set.
David Herrmann28ec7112013-07-27 16:37:00 +0200426 */
Daniel Vetter366884b2016-04-26 19:29:34 +0200427void drm_legacy_agp_clear(struct drm_device *dev)
David Herrmann28ec7112013-07-27 16:37:00 +0200428{
429 struct drm_agp_mem *entry, *tempe;
430
Daniel Vetterd9906752013-12-11 11:34:35 +0100431 if (!dev->agp)
David Herrmann28ec7112013-07-27 16:37:00 +0200432 return;
Daniel Vetterfa538642016-08-03 21:11:10 +0200433 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
David Herrmann28ec7112013-07-27 16:37:00 +0200434 return;
435
436 list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
437 if (entry->bound)
Thomas Zimmermannff28a9f2021-01-12 09:10:30 +0100438 agp_unbind_memory(entry->memory);
439 agp_free_memory(entry->memory);
David Herrmann28ec7112013-07-27 16:37:00 +0200440 kfree(entry);
441 }
442 INIT_LIST_HEAD(&dev->agp->memory);
443
444 if (dev->agp->acquired)
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200445 drm_legacy_agp_release(dev);
David Herrmann28ec7112013-07-27 16:37:00 +0200446
447 dev->agp->acquired = 0;
448 dev->agp->enabled = 0;
449}
Thomas Zimmermann04dfe192021-05-07 20:57:09 +0200450
451#endif