145b2fda3SQian Cai /*
2c0e09200SDave Airlie * \file drm_agpsupport.c
3c0e09200SDave Airlie * DRM support for AGP/GART backend
4c0e09200SDave Airlie *
5c0e09200SDave Airlie * \author Rickard E. (Rik) Faith <faith@valinux.com>
6c0e09200SDave Airlie * \author Gareth Hughes <gareth@valinux.com>
7c0e09200SDave Airlie */
8c0e09200SDave Airlie
9c0e09200SDave Airlie /*
10c0e09200SDave Airlie * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11c0e09200SDave Airlie * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12c0e09200SDave Airlie * All Rights Reserved.
13c0e09200SDave Airlie *
14c0e09200SDave Airlie * Permission is hereby granted, free of charge, to any person obtaining a
15c0e09200SDave Airlie * copy of this software and associated documentation files (the "Software"),
16c0e09200SDave Airlie * to deal in the Software without restriction, including without limitation
17c0e09200SDave Airlie * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18c0e09200SDave Airlie * and/or sell copies of the Software, and to permit persons to whom the
19c0e09200SDave Airlie * Software is furnished to do so, subject to the following conditions:
20c0e09200SDave Airlie *
21c0e09200SDave Airlie * The above copyright notice and this permission notice (including the next
22c0e09200SDave Airlie * paragraph) shall be included in all copies or substantial portions of the
23c0e09200SDave Airlie * Software.
24c0e09200SDave Airlie *
25c0e09200SDave Airlie * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26c0e09200SDave Airlie * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27c0e09200SDave Airlie * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28c0e09200SDave Airlie * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29c0e09200SDave Airlie * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30c0e09200SDave Airlie * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31c0e09200SDave Airlie * OTHER DEALINGS IN THE SOFTWARE.
32c0e09200SDave Airlie */
33c0e09200SDave Airlie
34c0e09200SDave Airlie #include <linux/module.h>
350500c04eSSam Ravnborg #include <linux/pci.h>
365a0e3ad6STejun Heo #include <linux/slab.h>
37c0e09200SDave Airlie
38*eb185a32SThomas Zimmermann #if IS_ENABLED(CONFIG_AGP)
391bb88edbSEric Anholt #include <asm/agp.h>
40*eb185a32SThomas Zimmermann #endif
411bb88edbSEric Anholt
420500c04eSSam Ravnborg #include <drm/drm_device.h>
430500c04eSSam Ravnborg #include <drm/drm_drv.h>
440500c04eSSam Ravnborg #include <drm/drm_file.h>
450500c04eSSam Ravnborg #include <drm/drm_print.h>
460500c04eSSam Ravnborg
470500c04eSSam Ravnborg #include "drm_legacy.h"
480500c04eSSam Ravnborg
4904dfe19aSThomas Zimmermann #if IS_ENABLED(CONFIG_AGP)
5004dfe19aSThomas Zimmermann
51cec7638bSLee Jones /*
52c0e09200SDave Airlie * Get AGP information.
53c0e09200SDave Airlie *
54c0e09200SDave Airlie * \return zero on success or a negative number on failure.
55c0e09200SDave Airlie *
56c0e09200SDave Airlie * Verifies the AGP device has been initialized and acquired and fills in the
57c0e09200SDave Airlie * drm_agp_info structure with the information in drm_agp_head::agp_info.
58c0e09200SDave Airlie */
drm_legacy_agp_info(struct drm_device * dev,struct drm_agp_info * info)5904dfe19aSThomas Zimmermann int drm_legacy_agp_info(struct drm_device *dev, struct drm_agp_info *info)
60c0e09200SDave Airlie {
61d2e546b8SDaniel Vetter struct agp_kern_info *kern;
62c0e09200SDave Airlie
63c0e09200SDave Airlie if (!dev->agp || !dev->agp->acquired)
64c0e09200SDave Airlie return -EINVAL;
65c0e09200SDave Airlie
66c0e09200SDave Airlie kern = &dev->agp->agp_info;
67c0e09200SDave Airlie info->agp_version_major = kern->version.major;
68c0e09200SDave Airlie info->agp_version_minor = kern->version.minor;
69c0e09200SDave Airlie info->mode = kern->mode;
70c0e09200SDave Airlie info->aperture_base = kern->aper_base;
71c0e09200SDave Airlie info->aperture_size = kern->aper_size * 1024 * 1024;
72c0e09200SDave Airlie info->memory_allowed = kern->max_memory << PAGE_SHIFT;
73c0e09200SDave Airlie info->memory_used = kern->current_memory << PAGE_SHIFT;
74c0e09200SDave Airlie info->id_vendor = kern->device->vendor;
75c0e09200SDave Airlie info->id_device = kern->device->device;
76c0e09200SDave Airlie
77c0e09200SDave Airlie return 0;
78c0e09200SDave Airlie }
7904dfe19aSThomas Zimmermann EXPORT_SYMBOL(drm_legacy_agp_info);
80c0e09200SDave Airlie
drm_legacy_agp_info_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)8104dfe19aSThomas Zimmermann int drm_legacy_agp_info_ioctl(struct drm_device *dev, void *data,
82c0e09200SDave Airlie struct drm_file *file_priv)
83c0e09200SDave Airlie {
84c0e09200SDave Airlie struct drm_agp_info *info = data;
85c0e09200SDave Airlie int err;
86c0e09200SDave Airlie
8704dfe19aSThomas Zimmermann err = drm_legacy_agp_info(dev, info);
88c0e09200SDave Airlie if (err)
89c0e09200SDave Airlie return err;
90c0e09200SDave Airlie
91c0e09200SDave Airlie return 0;
92c0e09200SDave Airlie }
93c0e09200SDave Airlie
94cec7638bSLee Jones /*
95c0e09200SDave Airlie * Acquire the AGP device.
96c0e09200SDave Airlie *
97c0e09200SDave Airlie * \param dev DRM device that is to acquire AGP.
98c0e09200SDave Airlie * \return zero on success or a negative number on failure.
99c0e09200SDave Airlie *
100c0e09200SDave Airlie * Verifies the AGP device hasn't been acquired before and calls
101c0e09200SDave Airlie * \c agp_backend_acquire.
102c0e09200SDave Airlie */
drm_legacy_agp_acquire(struct drm_device * dev)10304dfe19aSThomas Zimmermann int drm_legacy_agp_acquire(struct drm_device *dev)
104c0e09200SDave Airlie {
10536b73b05SThomas Zimmermann struct pci_dev *pdev = to_pci_dev(dev->dev);
10636b73b05SThomas Zimmermann
107c0e09200SDave Airlie if (!dev->agp)
108c0e09200SDave Airlie return -ENODEV;
109c0e09200SDave Airlie if (dev->agp->acquired)
110c0e09200SDave Airlie return -EBUSY;
11136b73b05SThomas Zimmermann dev->agp->bridge = agp_backend_acquire(pdev);
112182e61d1SMeghana Madhyastha if (!dev->agp->bridge)
113c0e09200SDave Airlie return -ENODEV;
114c0e09200SDave Airlie dev->agp->acquired = 1;
115c0e09200SDave Airlie return 0;
116c0e09200SDave Airlie }
11704dfe19aSThomas Zimmermann EXPORT_SYMBOL(drm_legacy_agp_acquire);
118c0e09200SDave Airlie
119cec7638bSLee Jones /*
120c0e09200SDave Airlie * Acquire the AGP device (ioctl).
121c0e09200SDave Airlie *
122c0e09200SDave Airlie * \return zero on success or a negative number on failure.
123c0e09200SDave Airlie *
124c0e09200SDave Airlie * Verifies the AGP device hasn't been acquired before and calls
125c0e09200SDave Airlie * \c agp_backend_acquire.
126c0e09200SDave Airlie */
drm_legacy_agp_acquire_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)12704dfe19aSThomas Zimmermann int drm_legacy_agp_acquire_ioctl(struct drm_device *dev, void *data,
128c0e09200SDave Airlie struct drm_file *file_priv)
129c0e09200SDave Airlie {
13004dfe19aSThomas Zimmermann return drm_legacy_agp_acquire((struct drm_device *)file_priv->minor->dev);
131c0e09200SDave Airlie }
132c0e09200SDave Airlie
133cec7638bSLee Jones /*
134c0e09200SDave Airlie * Release the AGP device.
135c0e09200SDave Airlie *
136c0e09200SDave Airlie * \param dev DRM device that is to release AGP.
137c0e09200SDave Airlie * \return zero on success or a negative number on failure.
138c0e09200SDave Airlie *
139c0e09200SDave Airlie * Verifies the AGP device has been acquired and calls \c agp_backend_release.
140c0e09200SDave Airlie */
drm_legacy_agp_release(struct drm_device * dev)14104dfe19aSThomas Zimmermann int drm_legacy_agp_release(struct drm_device *dev)
142c0e09200SDave Airlie {
143c0e09200SDave Airlie if (!dev->agp || !dev->agp->acquired)
144c0e09200SDave Airlie return -EINVAL;
145c0e09200SDave Airlie agp_backend_release(dev->agp->bridge);
146c0e09200SDave Airlie dev->agp->acquired = 0;
147c0e09200SDave Airlie return 0;
148c0e09200SDave Airlie }
14904dfe19aSThomas Zimmermann EXPORT_SYMBOL(drm_legacy_agp_release);
150c0e09200SDave Airlie
drm_legacy_agp_release_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)15104dfe19aSThomas Zimmermann int drm_legacy_agp_release_ioctl(struct drm_device *dev, void *data,
152c0e09200SDave Airlie struct drm_file *file_priv)
153c0e09200SDave Airlie {
15404dfe19aSThomas Zimmermann return drm_legacy_agp_release(dev);
155c0e09200SDave Airlie }
156c0e09200SDave Airlie
157cec7638bSLee Jones /*
158c0e09200SDave Airlie * Enable the AGP bus.
159c0e09200SDave Airlie *
160c0e09200SDave Airlie * \param dev DRM device that has previously acquired AGP.
161c0e09200SDave Airlie * \param mode Requested AGP mode.
162c0e09200SDave Airlie * \return zero on success or a negative number on failure.
163c0e09200SDave Airlie *
164c0e09200SDave Airlie * Verifies the AGP device has been acquired but not enabled, and calls
165c0e09200SDave Airlie * \c agp_enable.
166c0e09200SDave Airlie */
drm_legacy_agp_enable(struct drm_device * dev,struct drm_agp_mode mode)16704dfe19aSThomas Zimmermann int drm_legacy_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
168c0e09200SDave Airlie {
169c0e09200SDave Airlie if (!dev->agp || !dev->agp->acquired)
170c0e09200SDave Airlie return -EINVAL;
171c0e09200SDave Airlie
172c0e09200SDave Airlie dev->agp->mode = mode.mode;
173c0e09200SDave Airlie agp_enable(dev->agp->bridge, mode.mode);
174c0e09200SDave Airlie dev->agp->enabled = 1;
175c0e09200SDave Airlie return 0;
176c0e09200SDave Airlie }
17704dfe19aSThomas Zimmermann EXPORT_SYMBOL(drm_legacy_agp_enable);
178c0e09200SDave Airlie
drm_legacy_agp_enable_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)17904dfe19aSThomas Zimmermann int drm_legacy_agp_enable_ioctl(struct drm_device *dev, void *data,
180c0e09200SDave Airlie struct drm_file *file_priv)
181c0e09200SDave Airlie {
182c0e09200SDave Airlie struct drm_agp_mode *mode = data;
183c0e09200SDave Airlie
18404dfe19aSThomas Zimmermann return drm_legacy_agp_enable(dev, *mode);
185c0e09200SDave Airlie }
186c0e09200SDave Airlie
187cec7638bSLee Jones /*
188c0e09200SDave Airlie * Allocate AGP memory.
189c0e09200SDave Airlie *
190c0e09200SDave Airlie * \return zero on success or a negative number on failure.
191c0e09200SDave Airlie *
192c0e09200SDave Airlie * Verifies the AGP device is present and has been acquired, allocates the
19389c37264SDaniel Vetter * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
194c0e09200SDave Airlie */
drm_legacy_agp_alloc(struct drm_device * dev,struct drm_agp_buffer * request)19504dfe19aSThomas Zimmermann int drm_legacy_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
196c0e09200SDave Airlie {
197c0e09200SDave Airlie struct drm_agp_mem *entry;
198d2e546b8SDaniel Vetter struct agp_memory *memory;
199c0e09200SDave Airlie unsigned long pages;
200c0e09200SDave Airlie u32 type;
201c0e09200SDave Airlie
202c0e09200SDave Airlie if (!dev->agp || !dev->agp->acquired)
203c0e09200SDave Airlie return -EINVAL;
204182e61d1SMeghana Madhyastha entry = kzalloc(sizeof(*entry), GFP_KERNEL);
205182e61d1SMeghana Madhyastha if (!entry)
206c0e09200SDave Airlie return -ENOMEM;
207c0e09200SDave Airlie
208b8c8a859SWambui Karuga pages = DIV_ROUND_UP(request->size, PAGE_SIZE);
209c0e09200SDave Airlie type = (u32) request->type;
210182e61d1SMeghana Madhyastha memory = agp_allocate_memory(dev->agp->bridge, pages, type);
211182e61d1SMeghana Madhyastha if (!memory) {
2129a298b2aSEric Anholt kfree(entry);
213c0e09200SDave Airlie return -ENOMEM;
214c0e09200SDave Airlie }
215c0e09200SDave Airlie
216c0e09200SDave Airlie entry->handle = (unsigned long)memory->key + 1;
217c0e09200SDave Airlie entry->memory = memory;
218c0e09200SDave Airlie entry->bound = 0;
219c0e09200SDave Airlie entry->pages = pages;
220c0e09200SDave Airlie list_add(&entry->head, &dev->agp->memory);
221c0e09200SDave Airlie
222c0e09200SDave Airlie request->handle = entry->handle;
223c0e09200SDave Airlie request->physical = memory->physical;
224c0e09200SDave Airlie
225c0e09200SDave Airlie return 0;
226c0e09200SDave Airlie }
22704dfe19aSThomas Zimmermann EXPORT_SYMBOL(drm_legacy_agp_alloc);
228c0e09200SDave Airlie
229c0e09200SDave Airlie
drm_legacy_agp_alloc_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)23004dfe19aSThomas Zimmermann int drm_legacy_agp_alloc_ioctl(struct drm_device *dev, void *data,
231c0e09200SDave Airlie struct drm_file *file_priv)
232c0e09200SDave Airlie {
233c0e09200SDave Airlie struct drm_agp_buffer *request = data;
234c0e09200SDave Airlie
23504dfe19aSThomas Zimmermann return drm_legacy_agp_alloc(dev, request);
236c0e09200SDave Airlie }
237c0e09200SDave Airlie
238cec7638bSLee Jones /*
239c0e09200SDave Airlie * Search for the AGP memory entry associated with a handle.
240c0e09200SDave Airlie *
241c0e09200SDave Airlie * \param dev DRM device structure.
242c0e09200SDave Airlie * \param handle AGP memory handle.
243c0e09200SDave Airlie * \return pointer to the drm_agp_mem structure associated with \p handle.
244c0e09200SDave Airlie *
245c0e09200SDave Airlie * Walks through drm_agp_head::memory until finding a matching handle.
246c0e09200SDave Airlie */
drm_legacy_agp_lookup_entry(struct drm_device * dev,unsigned long handle)24704dfe19aSThomas Zimmermann static struct drm_agp_mem *drm_legacy_agp_lookup_entry(struct drm_device *dev,
248c0e09200SDave Airlie unsigned long handle)
249c0e09200SDave Airlie {
250c0e09200SDave Airlie struct drm_agp_mem *entry;
251c0e09200SDave Airlie
252c0e09200SDave Airlie list_for_each_entry(entry, &dev->agp->memory, head) {
253c0e09200SDave Airlie if (entry->handle == handle)
254c0e09200SDave Airlie return entry;
255c0e09200SDave Airlie }
256c0e09200SDave Airlie return NULL;
257c0e09200SDave Airlie }
258c0e09200SDave Airlie
259cec7638bSLee Jones /*
260c0e09200SDave Airlie * Unbind AGP memory from the GATT (ioctl).
261c0e09200SDave Airlie *
262c0e09200SDave Airlie * \return zero on success or a negative number on failure.
263c0e09200SDave Airlie *
264c0e09200SDave Airlie * Verifies the AGP device is present and acquired, looks-up the AGP memory
265c0e09200SDave Airlie * entry and passes it to the unbind_agp() function.
266c0e09200SDave Airlie */
drm_legacy_agp_unbind(struct drm_device * dev,struct drm_agp_binding * request)26704dfe19aSThomas Zimmermann int drm_legacy_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
268c0e09200SDave Airlie {
269c0e09200SDave Airlie struct drm_agp_mem *entry;
270c0e09200SDave Airlie int ret;
271c0e09200SDave Airlie
272c0e09200SDave Airlie if (!dev->agp || !dev->agp->acquired)
273c0e09200SDave Airlie return -EINVAL;
27404dfe19aSThomas Zimmermann entry = drm_legacy_agp_lookup_entry(dev, request->handle);
275182e61d1SMeghana Madhyastha if (!entry || !entry->bound)
276c0e09200SDave Airlie return -EINVAL;
277ff28a9f8SThomas Zimmermann ret = agp_unbind_memory(entry->memory);
278c0e09200SDave Airlie if (ret == 0)
279c0e09200SDave Airlie entry->bound = 0;
280c0e09200SDave Airlie return ret;
281c0e09200SDave Airlie }
28204dfe19aSThomas Zimmermann EXPORT_SYMBOL(drm_legacy_agp_unbind);
283c0e09200SDave Airlie
284c0e09200SDave Airlie
drm_legacy_agp_unbind_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)28504dfe19aSThomas Zimmermann int drm_legacy_agp_unbind_ioctl(struct drm_device *dev, void *data,
286c0e09200SDave Airlie struct drm_file *file_priv)
287c0e09200SDave Airlie {
288c0e09200SDave Airlie struct drm_agp_binding *request = data;
289c0e09200SDave Airlie
29004dfe19aSThomas Zimmermann return drm_legacy_agp_unbind(dev, request);
291c0e09200SDave Airlie }
292c0e09200SDave Airlie
293cec7638bSLee Jones /*
294c0e09200SDave Airlie * Bind AGP memory into the GATT (ioctl)
295c0e09200SDave Airlie *
296c0e09200SDave Airlie * \return zero on success or a negative number on failure.
297c0e09200SDave Airlie *
298c0e09200SDave Airlie * Verifies the AGP device is present and has been acquired and that no memory
299c0e09200SDave Airlie * is currently bound into the GATT. Looks-up the AGP memory entry and passes
300c0e09200SDave Airlie * it to bind_agp() function.
301c0e09200SDave Airlie */
drm_legacy_agp_bind(struct drm_device * dev,struct drm_agp_binding * request)30204dfe19aSThomas Zimmermann int drm_legacy_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
303c0e09200SDave Airlie {
304c0e09200SDave Airlie struct drm_agp_mem *entry;
305c0e09200SDave Airlie int retcode;
306c0e09200SDave Airlie int page;
307c0e09200SDave Airlie
308c0e09200SDave Airlie if (!dev->agp || !dev->agp->acquired)
309c0e09200SDave Airlie return -EINVAL;
31004dfe19aSThomas Zimmermann entry = drm_legacy_agp_lookup_entry(dev, request->handle);
311182e61d1SMeghana Madhyastha if (!entry || entry->bound)
312c0e09200SDave Airlie return -EINVAL;
313b8c8a859SWambui Karuga page = DIV_ROUND_UP(request->offset, PAGE_SIZE);
314ff28a9f8SThomas Zimmermann retcode = agp_bind_memory(entry->memory, page);
315182e61d1SMeghana Madhyastha if (retcode)
316c0e09200SDave Airlie return retcode;
317c0e09200SDave Airlie entry->bound = dev->agp->base + (page << PAGE_SHIFT);
318c0e09200SDave Airlie DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
319c0e09200SDave Airlie dev->agp->base, entry->bound);
320c0e09200SDave Airlie return 0;
321c0e09200SDave Airlie }
32204dfe19aSThomas Zimmermann EXPORT_SYMBOL(drm_legacy_agp_bind);
323c0e09200SDave Airlie
324c0e09200SDave Airlie
drm_legacy_agp_bind_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)32504dfe19aSThomas Zimmermann int drm_legacy_agp_bind_ioctl(struct drm_device *dev, void *data,
326c0e09200SDave Airlie struct drm_file *file_priv)
327c0e09200SDave Airlie {
328c0e09200SDave Airlie struct drm_agp_binding *request = data;
329c0e09200SDave Airlie
33004dfe19aSThomas Zimmermann return drm_legacy_agp_bind(dev, request);
331c0e09200SDave Airlie }
332c0e09200SDave Airlie
333cec7638bSLee Jones /*
334c0e09200SDave Airlie * Free AGP memory (ioctl).
335c0e09200SDave Airlie *
336c0e09200SDave Airlie * \return zero on success or a negative number on failure.
337c0e09200SDave Airlie *
338c0e09200SDave Airlie * Verifies the AGP device is present and has been acquired and looks up the
3391e55a53aSMatt Roper * AGP memory entry. If the memory is currently bound, unbind it via
340c0e09200SDave Airlie * unbind_agp(). Frees it via free_agp() as well as the entry itself
341c0e09200SDave Airlie * and unlinks from the doubly linked list it's inserted in.
342c0e09200SDave Airlie */
drm_legacy_agp_free(struct drm_device * dev,struct drm_agp_buffer * request)34304dfe19aSThomas Zimmermann int drm_legacy_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
344c0e09200SDave Airlie {
345c0e09200SDave Airlie struct drm_agp_mem *entry;
346c0e09200SDave Airlie
347c0e09200SDave Airlie if (!dev->agp || !dev->agp->acquired)
348c0e09200SDave Airlie return -EINVAL;
34904dfe19aSThomas Zimmermann entry = drm_legacy_agp_lookup_entry(dev, request->handle);
350182e61d1SMeghana Madhyastha if (!entry)
351c0e09200SDave Airlie return -EINVAL;
352c0e09200SDave Airlie if (entry->bound)
353ff28a9f8SThomas Zimmermann agp_unbind_memory(entry->memory);
354c0e09200SDave Airlie
355c0e09200SDave Airlie list_del(&entry->head);
356c0e09200SDave Airlie
357ff28a9f8SThomas Zimmermann agp_free_memory(entry->memory);
3589a298b2aSEric Anholt kfree(entry);
359c0e09200SDave Airlie return 0;
360c0e09200SDave Airlie }
36104dfe19aSThomas Zimmermann EXPORT_SYMBOL(drm_legacy_agp_free);
362c0e09200SDave Airlie
363c0e09200SDave Airlie
drm_legacy_agp_free_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)36404dfe19aSThomas Zimmermann int drm_legacy_agp_free_ioctl(struct drm_device *dev, void *data,
365c0e09200SDave Airlie struct drm_file *file_priv)
366c0e09200SDave Airlie {
367c0e09200SDave Airlie struct drm_agp_buffer *request = data;
368c0e09200SDave Airlie
36904dfe19aSThomas Zimmermann return drm_legacy_agp_free(dev, request);
370c0e09200SDave Airlie }
371c0e09200SDave Airlie
372cec7638bSLee Jones /*
373c0e09200SDave Airlie * Initialize the AGP resources.
374c0e09200SDave Airlie *
375c0e09200SDave Airlie * \return pointer to a drm_agp_head structure.
376c0e09200SDave Airlie *
377c0e09200SDave Airlie * Gets the drm_agp_t structure which is made available by the agpgart module
378c0e09200SDave Airlie * via the inter_module_* functions. Creates and initializes a drm_agp_head
379c0e09200SDave Airlie * structure.
380d6e4b28bSDaniel Vetter *
381d6e4b28bSDaniel Vetter * Note that final cleanup of the kmalloced structure is directly done in
382d6e4b28bSDaniel Vetter * drm_pci_agp_destroy.
383c0e09200SDave Airlie */
drm_legacy_agp_init(struct drm_device * dev)38404dfe19aSThomas Zimmermann struct drm_agp_head *drm_legacy_agp_init(struct drm_device *dev)
385c0e09200SDave Airlie {
38636b73b05SThomas Zimmermann struct pci_dev *pdev = to_pci_dev(dev->dev);
387c0e09200SDave Airlie struct drm_agp_head *head = NULL;
388c0e09200SDave Airlie
389182e61d1SMeghana Madhyastha head = kzalloc(sizeof(*head), GFP_KERNEL);
390182e61d1SMeghana Madhyastha if (!head)
391c0e09200SDave Airlie return NULL;
39236b73b05SThomas Zimmermann head->bridge = agp_find_bridge(pdev);
393c0e09200SDave Airlie if (!head->bridge) {
39436b73b05SThomas Zimmermann head->bridge = agp_backend_acquire(pdev);
395182e61d1SMeghana Madhyastha if (!head->bridge) {
3969a298b2aSEric Anholt kfree(head);
397c0e09200SDave Airlie return NULL;
398c0e09200SDave Airlie }
399c0e09200SDave Airlie agp_copy_info(head->bridge, &head->agp_info);
400c0e09200SDave Airlie agp_backend_release(head->bridge);
401c0e09200SDave Airlie } else {
402c0e09200SDave Airlie agp_copy_info(head->bridge, &head->agp_info);
403c0e09200SDave Airlie }
404c0e09200SDave Airlie if (head->agp_info.chipset == NOT_SUPPORTED) {
4059a298b2aSEric Anholt kfree(head);
406c0e09200SDave Airlie return NULL;
407c0e09200SDave Airlie }
408c0e09200SDave Airlie INIT_LIST_HEAD(&head->memory);
409c0e09200SDave Airlie head->cant_use_aperture = head->agp_info.cant_use_aperture;
410c0e09200SDave Airlie head->page_mask = head->agp_info.page_mask;
411c0e09200SDave Airlie head->base = head->agp_info.aper_base;
412c0e09200SDave Airlie return head;
413c0e09200SDave Airlie }
41449d66d8dSDaniel Vetter /* Only exported for i810.ko */
41504dfe19aSThomas Zimmermann EXPORT_SYMBOL(drm_legacy_agp_init);
416c0e09200SDave Airlie
417673a394bSEric Anholt /**
418366884b1SDaniel Vetter * drm_legacy_agp_clear - Clear AGP resource list
41928ec711cSDavid Herrmann * @dev: DRM device
42028ec711cSDavid Herrmann *
42128ec711cSDavid Herrmann * Iterate over all AGP resources and remove them. But keep the AGP head
42228ec711cSDavid Herrmann * intact so it can still be used. It is safe to call this if AGP is disabled or
42328ec711cSDavid Herrmann * was already removed.
42428ec711cSDavid Herrmann *
425fa538645SDaniel Vetter * Cleanup is only done for drivers who have DRIVER_LEGACY set.
42628ec711cSDavid Herrmann */
drm_legacy_agp_clear(struct drm_device * dev)427366884b1SDaniel Vetter void drm_legacy_agp_clear(struct drm_device *dev)
42828ec711cSDavid Herrmann {
42928ec711cSDavid Herrmann struct drm_agp_mem *entry, *tempe;
43028ec711cSDavid Herrmann
431d9906753SDaniel Vetter if (!dev->agp)
43228ec711cSDavid Herrmann return;
433fa538645SDaniel Vetter if (!drm_core_check_feature(dev, DRIVER_LEGACY))
43428ec711cSDavid Herrmann return;
43528ec711cSDavid Herrmann
43628ec711cSDavid Herrmann list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
43728ec711cSDavid Herrmann if (entry->bound)
438ff28a9f8SThomas Zimmermann agp_unbind_memory(entry->memory);
439ff28a9f8SThomas Zimmermann agp_free_memory(entry->memory);
44028ec711cSDavid Herrmann kfree(entry);
44128ec711cSDavid Herrmann }
44228ec711cSDavid Herrmann INIT_LIST_HEAD(&dev->agp->memory);
44328ec711cSDavid Herrmann
44428ec711cSDavid Herrmann if (dev->agp->acquired)
44504dfe19aSThomas Zimmermann drm_legacy_agp_release(dev);
44628ec711cSDavid Herrmann
44728ec711cSDavid Herrmann dev->agp->acquired = 0;
44828ec711cSDavid Herrmann dev->agp->enabled = 0;
44928ec711cSDavid Herrmann }
45004dfe19aSThomas Zimmermann
45104dfe19aSThomas Zimmermann #endif
452