xref: /openbmc/linux/drivers/gpu/drm/drm_agpsupport.c (revision fa5386459f06dc3b9181d4c954f980b127d1a32f)
1c0e09200SDave Airlie /**
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 
34760285e7SDavid Howells #include <drm/drmP.h>
35c0e09200SDave Airlie #include <linux/module.h>
365a0e3ad6STejun Heo #include <linux/slab.h>
37cc5ea594SDavid Herrmann #include "drm_legacy.h"
38c0e09200SDave Airlie 
391bb88edbSEric Anholt #include <asm/agp.h>
401bb88edbSEric Anholt 
41c0e09200SDave Airlie /**
42c0e09200SDave Airlie  * Get AGP information.
43c0e09200SDave Airlie  *
44c0e09200SDave Airlie  * \param inode device inode.
45c0e09200SDave Airlie  * \param file_priv DRM file private.
46c0e09200SDave Airlie  * \param cmd command.
47c0e09200SDave Airlie  * \param arg pointer to a (output) drm_agp_info structure.
48c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
49c0e09200SDave Airlie  *
50c0e09200SDave Airlie  * Verifies the AGP device has been initialized and acquired and fills in the
51c0e09200SDave Airlie  * drm_agp_info structure with the information in drm_agp_head::agp_info.
52c0e09200SDave Airlie  */
53c0e09200SDave Airlie int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
54c0e09200SDave Airlie {
55d2e546b8SDaniel Vetter 	struct agp_kern_info *kern;
56c0e09200SDave Airlie 
57c0e09200SDave Airlie 	if (!dev->agp || !dev->agp->acquired)
58c0e09200SDave Airlie 		return -EINVAL;
59c0e09200SDave Airlie 
60c0e09200SDave Airlie 	kern = &dev->agp->agp_info;
61c0e09200SDave Airlie 	info->agp_version_major = kern->version.major;
62c0e09200SDave Airlie 	info->agp_version_minor = kern->version.minor;
63c0e09200SDave Airlie 	info->mode = kern->mode;
64c0e09200SDave Airlie 	info->aperture_base = kern->aper_base;
65c0e09200SDave Airlie 	info->aperture_size = kern->aper_size * 1024 * 1024;
66c0e09200SDave Airlie 	info->memory_allowed = kern->max_memory << PAGE_SHIFT;
67c0e09200SDave Airlie 	info->memory_used = kern->current_memory << PAGE_SHIFT;
68c0e09200SDave Airlie 	info->id_vendor = kern->device->vendor;
69c0e09200SDave Airlie 	info->id_device = kern->device->device;
70c0e09200SDave Airlie 
71c0e09200SDave Airlie 	return 0;
72c0e09200SDave Airlie }
73c0e09200SDave Airlie 
74c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_info);
75c0e09200SDave Airlie 
76c0e09200SDave Airlie int drm_agp_info_ioctl(struct drm_device *dev, void *data,
77c0e09200SDave Airlie 		       struct drm_file *file_priv)
78c0e09200SDave Airlie {
79c0e09200SDave Airlie 	struct drm_agp_info *info = data;
80c0e09200SDave Airlie 	int err;
81c0e09200SDave Airlie 
82c0e09200SDave Airlie 	err = drm_agp_info(dev, info);
83c0e09200SDave Airlie 	if (err)
84c0e09200SDave Airlie 		return err;
85c0e09200SDave Airlie 
86c0e09200SDave Airlie 	return 0;
87c0e09200SDave Airlie }
88c0e09200SDave Airlie 
89c0e09200SDave Airlie /**
90c0e09200SDave Airlie  * Acquire the AGP device.
91c0e09200SDave Airlie  *
92c0e09200SDave Airlie  * \param dev DRM device that is to acquire AGP.
93c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
94c0e09200SDave Airlie  *
95c0e09200SDave Airlie  * Verifies the AGP device hasn't been acquired before and calls
96c0e09200SDave Airlie  * \c agp_backend_acquire.
97c0e09200SDave Airlie  */
98c0e09200SDave Airlie int drm_agp_acquire(struct drm_device * dev)
99c0e09200SDave Airlie {
100c0e09200SDave Airlie 	if (!dev->agp)
101c0e09200SDave Airlie 		return -ENODEV;
102c0e09200SDave Airlie 	if (dev->agp->acquired)
103c0e09200SDave Airlie 		return -EBUSY;
104c0e09200SDave Airlie 	if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev)))
105c0e09200SDave Airlie 		return -ENODEV;
106c0e09200SDave Airlie 	dev->agp->acquired = 1;
107c0e09200SDave Airlie 	return 0;
108c0e09200SDave Airlie }
109c0e09200SDave Airlie 
110c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_acquire);
111c0e09200SDave Airlie 
112c0e09200SDave Airlie /**
113c0e09200SDave Airlie  * Acquire the AGP device (ioctl).
114c0e09200SDave Airlie  *
115c0e09200SDave Airlie  * \param inode device inode.
116c0e09200SDave Airlie  * \param file_priv DRM file private.
117c0e09200SDave Airlie  * \param cmd command.
118c0e09200SDave Airlie  * \param arg user argument.
119c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
120c0e09200SDave Airlie  *
121c0e09200SDave Airlie  * Verifies the AGP device hasn't been acquired before and calls
122c0e09200SDave Airlie  * \c agp_backend_acquire.
123c0e09200SDave Airlie  */
124c0e09200SDave Airlie int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
125c0e09200SDave Airlie 			  struct drm_file *file_priv)
126c0e09200SDave Airlie {
127c0e09200SDave Airlie 	return drm_agp_acquire((struct drm_device *) file_priv->minor->dev);
128c0e09200SDave Airlie }
129c0e09200SDave Airlie 
130c0e09200SDave Airlie /**
131c0e09200SDave Airlie  * Release the AGP device.
132c0e09200SDave Airlie  *
133c0e09200SDave Airlie  * \param dev DRM device that is to release AGP.
134c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
135c0e09200SDave Airlie  *
136c0e09200SDave Airlie  * Verifies the AGP device has been acquired and calls \c agp_backend_release.
137c0e09200SDave Airlie  */
138c0e09200SDave Airlie int drm_agp_release(struct drm_device * dev)
139c0e09200SDave Airlie {
140c0e09200SDave Airlie 	if (!dev->agp || !dev->agp->acquired)
141c0e09200SDave Airlie 		return -EINVAL;
142c0e09200SDave Airlie 	agp_backend_release(dev->agp->bridge);
143c0e09200SDave Airlie 	dev->agp->acquired = 0;
144c0e09200SDave Airlie 	return 0;
145c0e09200SDave Airlie }
146c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_release);
147c0e09200SDave Airlie 
148c0e09200SDave Airlie int drm_agp_release_ioctl(struct drm_device *dev, void *data,
149c0e09200SDave Airlie 			  struct drm_file *file_priv)
150c0e09200SDave Airlie {
151c0e09200SDave Airlie 	return drm_agp_release(dev);
152c0e09200SDave Airlie }
153c0e09200SDave Airlie 
154c0e09200SDave Airlie /**
155c0e09200SDave Airlie  * Enable the AGP bus.
156c0e09200SDave Airlie  *
157c0e09200SDave Airlie  * \param dev DRM device that has previously acquired AGP.
158c0e09200SDave Airlie  * \param mode Requested AGP mode.
159c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
160c0e09200SDave Airlie  *
161c0e09200SDave Airlie  * Verifies the AGP device has been acquired but not enabled, and calls
162c0e09200SDave Airlie  * \c agp_enable.
163c0e09200SDave Airlie  */
164c0e09200SDave Airlie int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode)
165c0e09200SDave Airlie {
166c0e09200SDave Airlie 	if (!dev->agp || !dev->agp->acquired)
167c0e09200SDave Airlie 		return -EINVAL;
168c0e09200SDave Airlie 
169c0e09200SDave Airlie 	dev->agp->mode = mode.mode;
170c0e09200SDave Airlie 	agp_enable(dev->agp->bridge, mode.mode);
171c0e09200SDave Airlie 	dev->agp->enabled = 1;
172c0e09200SDave Airlie 	return 0;
173c0e09200SDave Airlie }
174c0e09200SDave Airlie 
175c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_enable);
176c0e09200SDave Airlie 
177c0e09200SDave Airlie int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
178c0e09200SDave Airlie 			 struct drm_file *file_priv)
179c0e09200SDave Airlie {
180c0e09200SDave Airlie 	struct drm_agp_mode *mode = data;
181c0e09200SDave Airlie 
182c0e09200SDave Airlie 	return drm_agp_enable(dev, *mode);
183c0e09200SDave Airlie }
184c0e09200SDave Airlie 
185c0e09200SDave Airlie /**
186c0e09200SDave Airlie  * Allocate AGP memory.
187c0e09200SDave Airlie  *
188c0e09200SDave Airlie  * \param inode device inode.
189c0e09200SDave Airlie  * \param file_priv file private pointer.
190c0e09200SDave Airlie  * \param cmd command.
191c0e09200SDave Airlie  * \param arg pointer to a drm_agp_buffer structure.
192c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
193c0e09200SDave Airlie  *
194c0e09200SDave Airlie  * Verifies the AGP device is present and has been acquired, allocates the
19589c37264SDaniel Vetter  * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
196c0e09200SDave Airlie  */
197c0e09200SDave Airlie int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
198c0e09200SDave Airlie {
199c0e09200SDave Airlie 	struct drm_agp_mem *entry;
200d2e546b8SDaniel Vetter 	struct agp_memory *memory;
201c0e09200SDave Airlie 	unsigned long pages;
202c0e09200SDave Airlie 	u32 type;
203c0e09200SDave Airlie 
204c0e09200SDave Airlie 	if (!dev->agp || !dev->agp->acquired)
205c0e09200SDave Airlie 		return -EINVAL;
206d5e41ad3SDave Airlie 	if (!(entry = kzalloc(sizeof(*entry), GFP_KERNEL)))
207c0e09200SDave Airlie 		return -ENOMEM;
208c0e09200SDave Airlie 
209c0e09200SDave Airlie 	pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
210c0e09200SDave Airlie 	type = (u32) request->type;
21189c37264SDaniel Vetter 	if (!(memory = agp_allocate_memory(dev->agp->bridge, pages, type))) {
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 }
227c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_alloc);
228c0e09200SDave Airlie 
229c0e09200SDave Airlie 
230c0e09200SDave Airlie int drm_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 
235c0e09200SDave Airlie 	return drm_agp_alloc(dev, request);
236c0e09200SDave Airlie }
237c0e09200SDave Airlie 
238c0e09200SDave Airlie /**
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  */
247c0e09200SDave Airlie static struct drm_agp_mem *drm_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 
259c0e09200SDave Airlie /**
260c0e09200SDave Airlie  * Unbind AGP memory from the GATT (ioctl).
261c0e09200SDave Airlie  *
262c0e09200SDave Airlie  * \param inode device inode.
263c0e09200SDave Airlie  * \param file_priv DRM file private.
264c0e09200SDave Airlie  * \param cmd command.
265c0e09200SDave Airlie  * \param arg pointer to a drm_agp_binding structure.
266c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
267c0e09200SDave Airlie  *
268c0e09200SDave Airlie  * Verifies the AGP device is present and acquired, looks-up the AGP memory
269c0e09200SDave Airlie  * entry and passes it to the unbind_agp() function.
270c0e09200SDave Airlie  */
271c0e09200SDave Airlie int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
272c0e09200SDave Airlie {
273c0e09200SDave Airlie 	struct drm_agp_mem *entry;
274c0e09200SDave Airlie 	int ret;
275c0e09200SDave Airlie 
276c0e09200SDave Airlie 	if (!dev->agp || !dev->agp->acquired)
277c0e09200SDave Airlie 		return -EINVAL;
278c0e09200SDave Airlie 	if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
279c0e09200SDave Airlie 		return -EINVAL;
280c0e09200SDave Airlie 	if (!entry->bound)
281c0e09200SDave Airlie 		return -EINVAL;
282c0e09200SDave Airlie 	ret = drm_unbind_agp(entry->memory);
283c0e09200SDave Airlie 	if (ret == 0)
284c0e09200SDave Airlie 		entry->bound = 0;
285c0e09200SDave Airlie 	return ret;
286c0e09200SDave Airlie }
287c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_unbind);
288c0e09200SDave Airlie 
289c0e09200SDave Airlie 
290c0e09200SDave Airlie int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
291c0e09200SDave Airlie 			 struct drm_file *file_priv)
292c0e09200SDave Airlie {
293c0e09200SDave Airlie 	struct drm_agp_binding *request = data;
294c0e09200SDave Airlie 
295c0e09200SDave Airlie 	return drm_agp_unbind(dev, request);
296c0e09200SDave Airlie }
297c0e09200SDave Airlie 
298c0e09200SDave Airlie /**
299c0e09200SDave Airlie  * Bind AGP memory into the GATT (ioctl)
300c0e09200SDave Airlie  *
301c0e09200SDave Airlie  * \param inode device inode.
302c0e09200SDave Airlie  * \param file_priv DRM file private.
303c0e09200SDave Airlie  * \param cmd command.
304c0e09200SDave Airlie  * \param arg pointer to a drm_agp_binding structure.
305c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
306c0e09200SDave Airlie  *
307c0e09200SDave Airlie  * Verifies the AGP device is present and has been acquired and that no memory
308c0e09200SDave Airlie  * is currently bound into the GATT. Looks-up the AGP memory entry and passes
309c0e09200SDave Airlie  * it to bind_agp() function.
310c0e09200SDave Airlie  */
311c0e09200SDave Airlie int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
312c0e09200SDave Airlie {
313c0e09200SDave Airlie 	struct drm_agp_mem *entry;
314c0e09200SDave Airlie 	int retcode;
315c0e09200SDave Airlie 	int page;
316c0e09200SDave Airlie 
317c0e09200SDave Airlie 	if (!dev->agp || !dev->agp->acquired)
318c0e09200SDave Airlie 		return -EINVAL;
319c0e09200SDave Airlie 	if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
320c0e09200SDave Airlie 		return -EINVAL;
321c0e09200SDave Airlie 	if (entry->bound)
322c0e09200SDave Airlie 		return -EINVAL;
323c0e09200SDave Airlie 	page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
324c0e09200SDave Airlie 	if ((retcode = drm_bind_agp(entry->memory, page)))
325c0e09200SDave Airlie 		return retcode;
326c0e09200SDave Airlie 	entry->bound = dev->agp->base + (page << PAGE_SHIFT);
327c0e09200SDave Airlie 	DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
328c0e09200SDave Airlie 		  dev->agp->base, entry->bound);
329c0e09200SDave Airlie 	return 0;
330c0e09200SDave Airlie }
331c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_bind);
332c0e09200SDave Airlie 
333c0e09200SDave Airlie 
334c0e09200SDave Airlie int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
335c0e09200SDave Airlie 		       struct drm_file *file_priv)
336c0e09200SDave Airlie {
337c0e09200SDave Airlie 	struct drm_agp_binding *request = data;
338c0e09200SDave Airlie 
339c0e09200SDave Airlie 	return drm_agp_bind(dev, request);
340c0e09200SDave Airlie }
341c0e09200SDave Airlie 
342c0e09200SDave Airlie /**
343c0e09200SDave Airlie  * Free AGP memory (ioctl).
344c0e09200SDave Airlie  *
345c0e09200SDave Airlie  * \param inode device inode.
346c0e09200SDave Airlie  * \param file_priv DRM file private.
347c0e09200SDave Airlie  * \param cmd command.
348c0e09200SDave Airlie  * \param arg pointer to a drm_agp_buffer structure.
349c0e09200SDave Airlie  * \return zero on success or a negative number on failure.
350c0e09200SDave Airlie  *
351c0e09200SDave Airlie  * Verifies the AGP device is present and has been acquired and looks up the
352c0e09200SDave Airlie  * AGP memory entry. If the memory it's currently bound, unbind it via
353c0e09200SDave Airlie  * unbind_agp(). Frees it via free_agp() as well as the entry itself
354c0e09200SDave Airlie  * and unlinks from the doubly linked list it's inserted in.
355c0e09200SDave Airlie  */
356c0e09200SDave Airlie int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
357c0e09200SDave Airlie {
358c0e09200SDave Airlie 	struct drm_agp_mem *entry;
359c0e09200SDave Airlie 
360c0e09200SDave Airlie 	if (!dev->agp || !dev->agp->acquired)
361c0e09200SDave Airlie 		return -EINVAL;
362c0e09200SDave Airlie 	if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
363c0e09200SDave Airlie 		return -EINVAL;
364c0e09200SDave Airlie 	if (entry->bound)
365c0e09200SDave Airlie 		drm_unbind_agp(entry->memory);
366c0e09200SDave Airlie 
367c0e09200SDave Airlie 	list_del(&entry->head);
368c0e09200SDave Airlie 
369c0e09200SDave Airlie 	drm_free_agp(entry->memory, entry->pages);
3709a298b2aSEric Anholt 	kfree(entry);
371c0e09200SDave Airlie 	return 0;
372c0e09200SDave Airlie }
373c0e09200SDave Airlie EXPORT_SYMBOL(drm_agp_free);
374c0e09200SDave Airlie 
375c0e09200SDave Airlie 
376c0e09200SDave Airlie 
377c0e09200SDave Airlie int drm_agp_free_ioctl(struct drm_device *dev, void *data,
378c0e09200SDave Airlie 		       struct drm_file *file_priv)
379c0e09200SDave Airlie {
380c0e09200SDave Airlie 	struct drm_agp_buffer *request = data;
381c0e09200SDave Airlie 
382c0e09200SDave Airlie 	return drm_agp_free(dev, request);
383c0e09200SDave Airlie }
384c0e09200SDave Airlie 
385c0e09200SDave Airlie /**
386c0e09200SDave Airlie  * Initialize the AGP resources.
387c0e09200SDave Airlie  *
388c0e09200SDave Airlie  * \return pointer to a drm_agp_head structure.
389c0e09200SDave Airlie  *
390c0e09200SDave Airlie  * Gets the drm_agp_t structure which is made available by the agpgart module
391c0e09200SDave Airlie  * via the inter_module_* functions. Creates and initializes a drm_agp_head
392c0e09200SDave Airlie  * structure.
393d6e4b28bSDaniel Vetter  *
394d6e4b28bSDaniel Vetter  * Note that final cleanup of the kmalloced structure is directly done in
395d6e4b28bSDaniel Vetter  * drm_pci_agp_destroy.
396c0e09200SDave Airlie  */
397c0e09200SDave Airlie struct drm_agp_head *drm_agp_init(struct drm_device *dev)
398c0e09200SDave Airlie {
399c0e09200SDave Airlie 	struct drm_agp_head *head = NULL;
400c0e09200SDave Airlie 
401d5e41ad3SDave Airlie 	if (!(head = kzalloc(sizeof(*head), GFP_KERNEL)))
402c0e09200SDave Airlie 		return NULL;
403c0e09200SDave Airlie 	head->bridge = agp_find_bridge(dev->pdev);
404c0e09200SDave Airlie 	if (!head->bridge) {
405c0e09200SDave Airlie 		if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
4069a298b2aSEric Anholt 			kfree(head);
407c0e09200SDave Airlie 			return NULL;
408c0e09200SDave Airlie 		}
409c0e09200SDave Airlie 		agp_copy_info(head->bridge, &head->agp_info);
410c0e09200SDave Airlie 		agp_backend_release(head->bridge);
411c0e09200SDave Airlie 	} else {
412c0e09200SDave Airlie 		agp_copy_info(head->bridge, &head->agp_info);
413c0e09200SDave Airlie 	}
414c0e09200SDave Airlie 	if (head->agp_info.chipset == NOT_SUPPORTED) {
4159a298b2aSEric Anholt 		kfree(head);
416c0e09200SDave Airlie 		return NULL;
417c0e09200SDave Airlie 	}
418c0e09200SDave Airlie 	INIT_LIST_HEAD(&head->memory);
419c0e09200SDave Airlie 	head->cant_use_aperture = head->agp_info.cant_use_aperture;
420c0e09200SDave Airlie 	head->page_mask = head->agp_info.page_mask;
421c0e09200SDave Airlie 	head->base = head->agp_info.aper_base;
422c0e09200SDave Airlie 	return head;
423c0e09200SDave Airlie }
424c0e09200SDave Airlie 
425673a394bSEric Anholt /**
426366884b1SDaniel Vetter  * drm_legacy_agp_clear - Clear AGP resource list
42728ec711cSDavid Herrmann  * @dev: DRM device
42828ec711cSDavid Herrmann  *
42928ec711cSDavid Herrmann  * Iterate over all AGP resources and remove them. But keep the AGP head
43028ec711cSDavid Herrmann  * intact so it can still be used. It is safe to call this if AGP is disabled or
43128ec711cSDavid Herrmann  * was already removed.
43228ec711cSDavid Herrmann  *
433*fa538645SDaniel Vetter  * Cleanup is only done for drivers who have DRIVER_LEGACY set.
43428ec711cSDavid Herrmann  */
435366884b1SDaniel Vetter void drm_legacy_agp_clear(struct drm_device *dev)
43628ec711cSDavid Herrmann {
43728ec711cSDavid Herrmann 	struct drm_agp_mem *entry, *tempe;
43828ec711cSDavid Herrmann 
439d9906753SDaniel Vetter 	if (!dev->agp)
44028ec711cSDavid Herrmann 		return;
441*fa538645SDaniel Vetter 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
44228ec711cSDavid Herrmann 		return;
44328ec711cSDavid Herrmann 
44428ec711cSDavid Herrmann 	list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
44528ec711cSDavid Herrmann 		if (entry->bound)
44628ec711cSDavid Herrmann 			drm_unbind_agp(entry->memory);
44728ec711cSDavid Herrmann 		drm_free_agp(entry->memory, entry->pages);
44828ec711cSDavid Herrmann 		kfree(entry);
44928ec711cSDavid Herrmann 	}
45028ec711cSDavid Herrmann 	INIT_LIST_HEAD(&dev->agp->memory);
45128ec711cSDavid Herrmann 
45228ec711cSDavid Herrmann 	if (dev->agp->acquired)
45328ec711cSDavid Herrmann 		drm_agp_release(dev);
45428ec711cSDavid Herrmann 
45528ec711cSDavid Herrmann 	dev->agp->acquired = 0;
45628ec711cSDavid Herrmann 	dev->agp->enabled = 0;
45728ec711cSDavid Herrmann }
45828ec711cSDavid Herrmann 
45928ec711cSDavid Herrmann /**
460673a394bSEric Anholt  * Binds a collection of pages into AGP memory at the given offset, returning
461673a394bSEric Anholt  * the AGP memory structure containing them.
462673a394bSEric Anholt  *
463673a394bSEric Anholt  * No reference is held on the pages during this time -- it is up to the
464673a394bSEric Anholt  * caller to handle that.
465673a394bSEric Anholt  */
466d2e546b8SDaniel Vetter struct agp_memory *
467673a394bSEric Anholt drm_agp_bind_pages(struct drm_device *dev,
468673a394bSEric Anholt 		   struct page **pages,
469673a394bSEric Anholt 		   unsigned long num_pages,
470ba1eb1d8SKeith Packard 		   uint32_t gtt_offset,
471ba1eb1d8SKeith Packard 		   u32 type)
472673a394bSEric Anholt {
473d2e546b8SDaniel Vetter 	struct agp_memory *mem;
474673a394bSEric Anholt 	int ret, i;
475673a394bSEric Anholt 
476673a394bSEric Anholt 	DRM_DEBUG("\n");
477673a394bSEric Anholt 
47889c37264SDaniel Vetter 	mem = agp_allocate_memory(dev->agp->bridge, num_pages,
479ba1eb1d8SKeith Packard 				      type);
480673a394bSEric Anholt 	if (mem == NULL) {
481673a394bSEric Anholt 		DRM_ERROR("Failed to allocate memory for %ld pages\n",
482673a394bSEric Anholt 			  num_pages);
483673a394bSEric Anholt 		return NULL;
484673a394bSEric Anholt 	}
485673a394bSEric Anholt 
486673a394bSEric Anholt 	for (i = 0; i < num_pages; i++)
48707613ba2SDave Airlie 		mem->pages[i] = pages[i];
488673a394bSEric Anholt 	mem->page_count = num_pages;
489673a394bSEric Anholt 
490673a394bSEric Anholt 	mem->is_flushed = true;
49189c37264SDaniel Vetter 	ret = agp_bind_memory(mem, gtt_offset / PAGE_SIZE);
492673a394bSEric Anholt 	if (ret != 0) {
493673a394bSEric Anholt 		DRM_ERROR("Failed to bind AGP memory: %d\n", ret);
494673a394bSEric Anholt 		agp_free_memory(mem);
495673a394bSEric Anholt 		return NULL;
496673a394bSEric Anholt 	}
497673a394bSEric Anholt 
498673a394bSEric Anholt 	return mem;
499673a394bSEric Anholt }
500673a394bSEric Anholt EXPORT_SYMBOL(drm_agp_bind_pages);
501