xref: /openbmc/linux/drivers/gpu/drm/drm_agpsupport.c (revision cec7638b8e7999521ebbddcc514e8ac0e3573d8c)
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 
381bb88edbSEric Anholt #include <asm/agp.h>
391bb88edbSEric Anholt 
400500c04eSSam Ravnborg #include <drm/drm_agpsupport.h>
410500c04eSSam Ravnborg #include <drm/drm_device.h>
420500c04eSSam Ravnborg #include <drm/drm_drv.h>
430500c04eSSam Ravnborg #include <drm/drm_file.h>
440500c04eSSam Ravnborg #include <drm/drm_print.h>
450500c04eSSam Ravnborg 
460500c04eSSam Ravnborg #include "drm_legacy.h"
470500c04eSSam Ravnborg 
48*cec7638bSLee Jones /*
49c0e09200SDave Airlie  * Get AGP information.
50c0e09200SDave Airlie  *
51c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
52c0e09200SDave Airlie  *
53c0e09200SDave Airlie  * Verifies the AGP device has been initialized and acquired and fills in the
54c0e09200SDave Airlie  * drm_agp_info structure with the information in drm_agp_head::agp_info.
55c0e09200SDave Airlie  */
56c0e09200SDave Airlie int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
57c0e09200SDave Airlie {
58d2e546b8SDaniel Vetter 	struct agp_kern_info *kern;
59c0e09200SDave Airlie 
60c0e09200SDave Airlie 	if (!dev->agp || !dev->agp->acquired)
61c0e09200SDave Airlie 		return -EINVAL;
62c0e09200SDave Airlie 
63c0e09200SDave Airlie 	kern = &dev->agp->agp_info;
64c0e09200SDave Airlie 	info->agp_version_major = kern->version.major;
65c0e09200SDave Airlie 	info->agp_version_minor = kern->version.minor;
66c0e09200SDave Airlie 	info->mode = kern->mode;
67c0e09200SDave Airlie 	info->aperture_base = kern->aper_base;
68c0e09200SDave Airlie 	info->aperture_size = kern->aper_size * 1024 * 1024;
69c0e09200SDave Airlie 	info->memory_allowed = kern->max_memory << PAGE_SHIFT;
70c0e09200SDave Airlie 	info->memory_used = kern->current_memory << PAGE_SHIFT;
71c0e09200SDave Airlie 	info->id_vendor = kern->device->vendor;
72c0e09200SDave Airlie 	info->id_device = kern->device->device;
73c0e09200SDave Airlie 
74c0e09200SDave Airlie 	return 0;
75c0e09200SDave Airlie }
76c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_info);
77c0e09200SDave Airlie 
78c0e09200SDave Airlie int drm_agp_info_ioctl(struct drm_device *dev, void *data,
79c0e09200SDave Airlie 		       struct drm_file *file_priv)
80c0e09200SDave Airlie {
81c0e09200SDave Airlie 	struct drm_agp_info *info = data;
82c0e09200SDave Airlie 	int err;
83c0e09200SDave Airlie 
84c0e09200SDave Airlie 	err = drm_agp_info(dev, info);
85c0e09200SDave Airlie 	if (err)
86c0e09200SDave Airlie 		return err;
87c0e09200SDave Airlie 
88c0e09200SDave Airlie 	return 0;
89c0e09200SDave Airlie }
90c0e09200SDave Airlie 
91*cec7638bSLee Jones /*
92c0e09200SDave Airlie  * Acquire the AGP device.
93c0e09200SDave Airlie  *
94c0e09200SDave Airlie  * \param dev DRM device that is to acquire AGP.
95c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
96c0e09200SDave Airlie  *
97c0e09200SDave Airlie  * Verifies the AGP device hasn't been acquired before and calls
98c0e09200SDave Airlie  * \c agp_backend_acquire.
99c0e09200SDave Airlie  */
100c0e09200SDave Airlie int drm_agp_acquire(struct drm_device *dev)
101c0e09200SDave Airlie {
102c0e09200SDave Airlie 	if (!dev->agp)
103c0e09200SDave Airlie 		return -ENODEV;
104c0e09200SDave Airlie 	if (dev->agp->acquired)
105c0e09200SDave Airlie 		return -EBUSY;
106182e61d1SMeghana Madhyastha 	dev->agp->bridge = agp_backend_acquire(dev->pdev);
107182e61d1SMeghana Madhyastha 	if (!dev->agp->bridge)
108c0e09200SDave Airlie 		return -ENODEV;
109c0e09200SDave Airlie 	dev->agp->acquired = 1;
110c0e09200SDave Airlie 	return 0;
111c0e09200SDave Airlie }
112c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_acquire);
113c0e09200SDave Airlie 
114*cec7638bSLee Jones /*
115c0e09200SDave Airlie  * Acquire the AGP device (ioctl).
116c0e09200SDave Airlie  *
117c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
118c0e09200SDave Airlie  *
119c0e09200SDave Airlie  * Verifies the AGP device hasn't been acquired before and calls
120c0e09200SDave Airlie  * \c agp_backend_acquire.
121c0e09200SDave Airlie  */
122c0e09200SDave Airlie int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
123c0e09200SDave Airlie 			  struct drm_file *file_priv)
124c0e09200SDave Airlie {
125c0e09200SDave Airlie 	return drm_agp_acquire((struct drm_device *) file_priv->minor->dev);
126c0e09200SDave Airlie }
127c0e09200SDave Airlie 
128*cec7638bSLee Jones /*
129c0e09200SDave Airlie  * Release the AGP device.
130c0e09200SDave Airlie  *
131c0e09200SDave Airlie  * \param dev DRM device that is to release AGP.
132c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
133c0e09200SDave Airlie  *
134c0e09200SDave Airlie  * Verifies the AGP device has been acquired and calls \c agp_backend_release.
135c0e09200SDave Airlie  */
136c0e09200SDave Airlie int drm_agp_release(struct drm_device *dev)
137c0e09200SDave Airlie {
138c0e09200SDave Airlie 	if (!dev->agp || !dev->agp->acquired)
139c0e09200SDave Airlie 		return -EINVAL;
140c0e09200SDave Airlie 	agp_backend_release(dev->agp->bridge);
141c0e09200SDave Airlie 	dev->agp->acquired = 0;
142c0e09200SDave Airlie 	return 0;
143c0e09200SDave Airlie }
144c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_release);
145c0e09200SDave Airlie 
146c0e09200SDave Airlie int drm_agp_release_ioctl(struct drm_device *dev, void *data,
147c0e09200SDave Airlie 			  struct drm_file *file_priv)
148c0e09200SDave Airlie {
149c0e09200SDave Airlie 	return drm_agp_release(dev);
150c0e09200SDave Airlie }
151c0e09200SDave Airlie 
152*cec7638bSLee Jones /*
153c0e09200SDave Airlie  * Enable the AGP bus.
154c0e09200SDave Airlie  *
155c0e09200SDave Airlie  * \param dev DRM device that has previously acquired AGP.
156c0e09200SDave Airlie  * \param mode Requested AGP mode.
157c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
158c0e09200SDave Airlie  *
159c0e09200SDave Airlie  * Verifies the AGP device has been acquired but not enabled, and calls
160c0e09200SDave Airlie  * \c agp_enable.
161c0e09200SDave Airlie  */
162c0e09200SDave Airlie int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
163c0e09200SDave Airlie {
164c0e09200SDave Airlie 	if (!dev->agp || !dev->agp->acquired)
165c0e09200SDave Airlie 		return -EINVAL;
166c0e09200SDave Airlie 
167c0e09200SDave Airlie 	dev->agp->mode = mode.mode;
168c0e09200SDave Airlie 	agp_enable(dev->agp->bridge, mode.mode);
169c0e09200SDave Airlie 	dev->agp->enabled = 1;
170c0e09200SDave Airlie 	return 0;
171c0e09200SDave Airlie }
172c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_enable);
173c0e09200SDave Airlie 
174c0e09200SDave Airlie int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
175c0e09200SDave Airlie 			 struct drm_file *file_priv)
176c0e09200SDave Airlie {
177c0e09200SDave Airlie 	struct drm_agp_mode *mode = data;
178c0e09200SDave Airlie 
179c0e09200SDave Airlie 	return drm_agp_enable(dev, *mode);
180c0e09200SDave Airlie }
181c0e09200SDave Airlie 
182*cec7638bSLee Jones /*
183c0e09200SDave Airlie  * Allocate AGP memory.
184c0e09200SDave Airlie  *
185c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
186c0e09200SDave Airlie  *
187c0e09200SDave Airlie  * Verifies the AGP device is present and has been acquired, allocates the
18889c37264SDaniel Vetter  * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
189c0e09200SDave Airlie  */
190c0e09200SDave Airlie int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
191c0e09200SDave Airlie {
192c0e09200SDave Airlie 	struct drm_agp_mem *entry;
193d2e546b8SDaniel Vetter 	struct agp_memory *memory;
194c0e09200SDave Airlie 	unsigned long pages;
195c0e09200SDave Airlie 	u32 type;
196c0e09200SDave Airlie 
197c0e09200SDave Airlie 	if (!dev->agp || !dev->agp->acquired)
198c0e09200SDave Airlie 		return -EINVAL;
199182e61d1SMeghana Madhyastha 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
200182e61d1SMeghana Madhyastha 	if (!entry)
201c0e09200SDave Airlie 		return -ENOMEM;
202c0e09200SDave Airlie 
203b8c8a859SWambui Karuga 	pages = DIV_ROUND_UP(request->size, PAGE_SIZE);
204c0e09200SDave Airlie 	type = (u32) request->type;
205182e61d1SMeghana Madhyastha 	memory = agp_allocate_memory(dev->agp->bridge, pages, type);
206182e61d1SMeghana Madhyastha 	if (!memory) {
2079a298b2aSEric Anholt 		kfree(entry);
208c0e09200SDave Airlie 		return -ENOMEM;
209c0e09200SDave Airlie 	}
210c0e09200SDave Airlie 
211c0e09200SDave Airlie 	entry->handle = (unsigned long)memory->key + 1;
212c0e09200SDave Airlie 	entry->memory = memory;
213c0e09200SDave Airlie 	entry->bound = 0;
214c0e09200SDave Airlie 	entry->pages = pages;
215c0e09200SDave Airlie 	list_add(&entry->head, &dev->agp->memory);
216c0e09200SDave Airlie 
217c0e09200SDave Airlie 	request->handle = entry->handle;
218c0e09200SDave Airlie 	request->physical = memory->physical;
219c0e09200SDave Airlie 
220c0e09200SDave Airlie 	return 0;
221c0e09200SDave Airlie }
222c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_alloc);
223c0e09200SDave Airlie 
224c0e09200SDave Airlie 
225c0e09200SDave Airlie int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
226c0e09200SDave Airlie 			struct drm_file *file_priv)
227c0e09200SDave Airlie {
228c0e09200SDave Airlie 	struct drm_agp_buffer *request = data;
229c0e09200SDave Airlie 
230c0e09200SDave Airlie 	return drm_agp_alloc(dev, request);
231c0e09200SDave Airlie }
232c0e09200SDave Airlie 
233*cec7638bSLee Jones /*
234c0e09200SDave Airlie  * Search for the AGP memory entry associated with a handle.
235c0e09200SDave Airlie  *
236c0e09200SDave Airlie  * \param dev DRM device structure.
237c0e09200SDave Airlie  * \param handle AGP memory handle.
238c0e09200SDave Airlie  * \return pointer to the drm_agp_mem structure associated with \p handle.
239c0e09200SDave Airlie  *
240c0e09200SDave Airlie  * Walks through drm_agp_head::memory until finding a matching handle.
241c0e09200SDave Airlie  */
242c0e09200SDave Airlie static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device *dev,
243c0e09200SDave Airlie 						unsigned long handle)
244c0e09200SDave Airlie {
245c0e09200SDave Airlie 	struct drm_agp_mem *entry;
246c0e09200SDave Airlie 
247c0e09200SDave Airlie 	list_for_each_entry(entry, &dev->agp->memory, head) {
248c0e09200SDave Airlie 		if (entry->handle == handle)
249c0e09200SDave Airlie 			return entry;
250c0e09200SDave Airlie 	}
251c0e09200SDave Airlie 	return NULL;
252c0e09200SDave Airlie }
253c0e09200SDave Airlie 
254*cec7638bSLee Jones /*
255c0e09200SDave Airlie  * Unbind AGP memory from the GATT (ioctl).
256c0e09200SDave Airlie  *
257c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
258c0e09200SDave Airlie  *
259c0e09200SDave Airlie  * Verifies the AGP device is present and acquired, looks-up the AGP memory
260c0e09200SDave Airlie  * entry and passes it to the unbind_agp() function.
261c0e09200SDave Airlie  */
262c0e09200SDave Airlie int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
263c0e09200SDave Airlie {
264c0e09200SDave Airlie 	struct drm_agp_mem *entry;
265c0e09200SDave Airlie 	int ret;
266c0e09200SDave Airlie 
267c0e09200SDave Airlie 	if (!dev->agp || !dev->agp->acquired)
268c0e09200SDave Airlie 		return -EINVAL;
269182e61d1SMeghana Madhyastha 	entry = drm_agp_lookup_entry(dev, request->handle);
270182e61d1SMeghana Madhyastha 	if (!entry || !entry->bound)
271c0e09200SDave Airlie 		return -EINVAL;
272ff28a9f8SThomas Zimmermann 	ret = agp_unbind_memory(entry->memory);
273c0e09200SDave Airlie 	if (ret == 0)
274c0e09200SDave Airlie 		entry->bound = 0;
275c0e09200SDave Airlie 	return ret;
276c0e09200SDave Airlie }
277c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_unbind);
278c0e09200SDave Airlie 
279c0e09200SDave Airlie 
280c0e09200SDave Airlie int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
281c0e09200SDave Airlie 			 struct drm_file *file_priv)
282c0e09200SDave Airlie {
283c0e09200SDave Airlie 	struct drm_agp_binding *request = data;
284c0e09200SDave Airlie 
285c0e09200SDave Airlie 	return drm_agp_unbind(dev, request);
286c0e09200SDave Airlie }
287c0e09200SDave Airlie 
288*cec7638bSLee Jones /*
289c0e09200SDave Airlie  * Bind AGP memory into the GATT (ioctl)
290c0e09200SDave Airlie  *
291c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
292c0e09200SDave Airlie  *
293c0e09200SDave Airlie  * Verifies the AGP device is present and has been acquired and that no memory
294c0e09200SDave Airlie  * is currently bound into the GATT. Looks-up the AGP memory entry and passes
295c0e09200SDave Airlie  * it to bind_agp() function.
296c0e09200SDave Airlie  */
297c0e09200SDave Airlie int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
298c0e09200SDave Airlie {
299c0e09200SDave Airlie 	struct drm_agp_mem *entry;
300c0e09200SDave Airlie 	int retcode;
301c0e09200SDave Airlie 	int page;
302c0e09200SDave Airlie 
303c0e09200SDave Airlie 	if (!dev->agp || !dev->agp->acquired)
304c0e09200SDave Airlie 		return -EINVAL;
305182e61d1SMeghana Madhyastha 	entry = drm_agp_lookup_entry(dev, request->handle);
306182e61d1SMeghana Madhyastha 	if (!entry || entry->bound)
307c0e09200SDave Airlie 		return -EINVAL;
308b8c8a859SWambui Karuga 	page = DIV_ROUND_UP(request->offset, PAGE_SIZE);
309ff28a9f8SThomas Zimmermann 	retcode = agp_bind_memory(entry->memory, page);
310182e61d1SMeghana Madhyastha 	if (retcode)
311c0e09200SDave Airlie 		return retcode;
312c0e09200SDave Airlie 	entry->bound = dev->agp->base + (page << PAGE_SHIFT);
313c0e09200SDave Airlie 	DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
314c0e09200SDave Airlie 		  dev->agp->base, entry->bound);
315c0e09200SDave Airlie 	return 0;
316c0e09200SDave Airlie }
317c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_bind);
318c0e09200SDave Airlie 
319c0e09200SDave Airlie 
320c0e09200SDave Airlie int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
321c0e09200SDave Airlie 		       struct drm_file *file_priv)
322c0e09200SDave Airlie {
323c0e09200SDave Airlie 	struct drm_agp_binding *request = data;
324c0e09200SDave Airlie 
325c0e09200SDave Airlie 	return drm_agp_bind(dev, request);
326c0e09200SDave Airlie }
327c0e09200SDave Airlie 
328*cec7638bSLee Jones /*
329c0e09200SDave Airlie  * Free AGP memory (ioctl).
330c0e09200SDave Airlie  *
331c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
332c0e09200SDave Airlie  *
333c0e09200SDave Airlie  * Verifies the AGP device is present and has been acquired and looks up the
3341e55a53aSMatt Roper  * AGP memory entry. If the memory is currently bound, unbind it via
335c0e09200SDave Airlie  * unbind_agp(). Frees it via free_agp() as well as the entry itself
336c0e09200SDave Airlie  * and unlinks from the doubly linked list it's inserted in.
337c0e09200SDave Airlie  */
338c0e09200SDave Airlie int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
339c0e09200SDave Airlie {
340c0e09200SDave Airlie 	struct drm_agp_mem *entry;
341c0e09200SDave Airlie 
342c0e09200SDave Airlie 	if (!dev->agp || !dev->agp->acquired)
343c0e09200SDave Airlie 		return -EINVAL;
344182e61d1SMeghana Madhyastha 	entry = drm_agp_lookup_entry(dev, request->handle);
345182e61d1SMeghana Madhyastha 	if (!entry)
346c0e09200SDave Airlie 		return -EINVAL;
347c0e09200SDave Airlie 	if (entry->bound)
348ff28a9f8SThomas Zimmermann 		agp_unbind_memory(entry->memory);
349c0e09200SDave Airlie 
350c0e09200SDave Airlie 	list_del(&entry->head);
351c0e09200SDave Airlie 
352ff28a9f8SThomas Zimmermann 	agp_free_memory(entry->memory);
3539a298b2aSEric Anholt 	kfree(entry);
354c0e09200SDave Airlie 	return 0;
355c0e09200SDave Airlie }
356c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_free);
357c0e09200SDave Airlie 
358c0e09200SDave Airlie 
359c0e09200SDave Airlie int drm_agp_free_ioctl(struct drm_device *dev, void *data,
360c0e09200SDave Airlie 		       struct drm_file *file_priv)
361c0e09200SDave Airlie {
362c0e09200SDave Airlie 	struct drm_agp_buffer *request = data;
363c0e09200SDave Airlie 
364c0e09200SDave Airlie 	return drm_agp_free(dev, request);
365c0e09200SDave Airlie }
366c0e09200SDave Airlie 
367*cec7638bSLee Jones /*
368c0e09200SDave Airlie  * Initialize the AGP resources.
369c0e09200SDave Airlie  *
370c0e09200SDave Airlie  * \return pointer to a drm_agp_head structure.
371c0e09200SDave Airlie  *
372c0e09200SDave Airlie  * Gets the drm_agp_t structure which is made available by the agpgart module
373c0e09200SDave Airlie  * via the inter_module_* functions. Creates and initializes a drm_agp_head
374c0e09200SDave Airlie  * structure.
375d6e4b28bSDaniel Vetter  *
376d6e4b28bSDaniel Vetter  * Note that final cleanup of the kmalloced structure is directly done in
377d6e4b28bSDaniel Vetter  * drm_pci_agp_destroy.
378c0e09200SDave Airlie  */
379c0e09200SDave Airlie struct drm_agp_head *drm_agp_init(struct drm_device *dev)
380c0e09200SDave Airlie {
381c0e09200SDave Airlie 	struct drm_agp_head *head = NULL;
382c0e09200SDave Airlie 
383182e61d1SMeghana Madhyastha 	head = kzalloc(sizeof(*head), GFP_KERNEL);
384182e61d1SMeghana Madhyastha 	if (!head)
385c0e09200SDave Airlie 		return NULL;
386c0e09200SDave Airlie 	head->bridge = agp_find_bridge(dev->pdev);
387c0e09200SDave Airlie 	if (!head->bridge) {
388182e61d1SMeghana Madhyastha 		head->bridge = agp_backend_acquire(dev->pdev);
389182e61d1SMeghana Madhyastha 		if (!head->bridge) {
3909a298b2aSEric Anholt 			kfree(head);
391c0e09200SDave Airlie 			return NULL;
392c0e09200SDave Airlie 		}
393c0e09200SDave Airlie 		agp_copy_info(head->bridge, &head->agp_info);
394c0e09200SDave Airlie 		agp_backend_release(head->bridge);
395c0e09200SDave Airlie 	} else {
396c0e09200SDave Airlie 		agp_copy_info(head->bridge, &head->agp_info);
397c0e09200SDave Airlie 	}
398c0e09200SDave Airlie 	if (head->agp_info.chipset == NOT_SUPPORTED) {
3999a298b2aSEric Anholt 		kfree(head);
400c0e09200SDave Airlie 		return NULL;
401c0e09200SDave Airlie 	}
402c0e09200SDave Airlie 	INIT_LIST_HEAD(&head->memory);
403c0e09200SDave Airlie 	head->cant_use_aperture = head->agp_info.cant_use_aperture;
404c0e09200SDave Airlie 	head->page_mask = head->agp_info.page_mask;
405c0e09200SDave Airlie 	head->base = head->agp_info.aper_base;
406c0e09200SDave Airlie 	return head;
407c0e09200SDave Airlie }
40849d66d8dSDaniel Vetter /* Only exported for i810.ko */
40949d66d8dSDaniel Vetter EXPORT_SYMBOL(drm_agp_init);
410c0e09200SDave Airlie 
411673a394bSEric Anholt /**
412366884b1SDaniel Vetter  * drm_legacy_agp_clear - Clear AGP resource list
41328ec711cSDavid Herrmann  * @dev: DRM device
41428ec711cSDavid Herrmann  *
41528ec711cSDavid Herrmann  * Iterate over all AGP resources and remove them. But keep the AGP head
41628ec711cSDavid Herrmann  * intact so it can still be used. It is safe to call this if AGP is disabled or
41728ec711cSDavid Herrmann  * was already removed.
41828ec711cSDavid Herrmann  *
419fa538645SDaniel Vetter  * Cleanup is only done for drivers who have DRIVER_LEGACY set.
42028ec711cSDavid Herrmann  */
421366884b1SDaniel Vetter void drm_legacy_agp_clear(struct drm_device *dev)
42228ec711cSDavid Herrmann {
42328ec711cSDavid Herrmann 	struct drm_agp_mem *entry, *tempe;
42428ec711cSDavid Herrmann 
425d9906753SDaniel Vetter 	if (!dev->agp)
42628ec711cSDavid Herrmann 		return;
427fa538645SDaniel Vetter 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
42828ec711cSDavid Herrmann 		return;
42928ec711cSDavid Herrmann 
43028ec711cSDavid Herrmann 	list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
43128ec711cSDavid Herrmann 		if (entry->bound)
432ff28a9f8SThomas Zimmermann 			agp_unbind_memory(entry->memory);
433ff28a9f8SThomas Zimmermann 		agp_free_memory(entry->memory);
43428ec711cSDavid Herrmann 		kfree(entry);
43528ec711cSDavid Herrmann 	}
43628ec711cSDavid Herrmann 	INIT_LIST_HEAD(&dev->agp->memory);
43728ec711cSDavid Herrmann 
43828ec711cSDavid Herrmann 	if (dev->agp->acquired)
43928ec711cSDavid Herrmann 		drm_agp_release(dev);
44028ec711cSDavid Herrmann 
44128ec711cSDavid Herrmann 	dev->agp->acquired = 0;
44228ec711cSDavid Herrmann 	dev->agp->enabled = 0;
44328ec711cSDavid Herrmann }
444