1*592ffb21SWarner Losh /**
2*592ffb21SWarner Losh * \file drm_agpsupport.c
3*592ffb21SWarner Losh * DRM support for AGP/GART backend
4*592ffb21SWarner Losh *
5*592ffb21SWarner Losh * \author Rickard E. (Rik) Faith <faith@valinux.com>
6*592ffb21SWarner Losh * \author Gareth Hughes <gareth@valinux.com>
7*592ffb21SWarner Losh */
8*592ffb21SWarner Losh
9*592ffb21SWarner Losh /*
10*592ffb21SWarner Losh * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11*592ffb21SWarner Losh * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12*592ffb21SWarner Losh * All Rights Reserved.
13*592ffb21SWarner Losh *
14*592ffb21SWarner Losh * Permission is hereby granted, free of charge, to any person obtaining a
15*592ffb21SWarner Losh * copy of this software and associated documentation files (the "Software"),
16*592ffb21SWarner Losh * to deal in the Software without restriction, including without limitation
17*592ffb21SWarner Losh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18*592ffb21SWarner Losh * and/or sell copies of the Software, and to permit persons to whom the
19*592ffb21SWarner Losh * Software is furnished to do so, subject to the following conditions:
20*592ffb21SWarner Losh *
21*592ffb21SWarner Losh * The above copyright notice and this permission notice (including the next
22*592ffb21SWarner Losh * paragraph) shall be included in all copies or substantial portions of the
23*592ffb21SWarner Losh * Software.
24*592ffb21SWarner Losh *
25*592ffb21SWarner Losh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26*592ffb21SWarner Losh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27*592ffb21SWarner Losh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28*592ffb21SWarner Losh * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29*592ffb21SWarner Losh * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30*592ffb21SWarner Losh * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31*592ffb21SWarner Losh * OTHER DEALINGS IN THE SOFTWARE.
32*592ffb21SWarner Losh */
33*592ffb21SWarner Losh
34*592ffb21SWarner Losh #include <sys/cdefs.h>
35*592ffb21SWarner Losh #include <dev/drm2/drmP.h>
36*592ffb21SWarner Losh
37*592ffb21SWarner Losh #if __OS_HAS_AGP
38*592ffb21SWarner Losh
39*592ffb21SWarner Losh /**
40*592ffb21SWarner Losh * Get AGP information.
41*592ffb21SWarner Losh *
42*592ffb21SWarner Losh * \param inode device inode.
43*592ffb21SWarner Losh * \param file_priv DRM file private.
44*592ffb21SWarner Losh * \param cmd command.
45*592ffb21SWarner Losh * \param arg pointer to a (output) drm_agp_info structure.
46*592ffb21SWarner Losh * \return zero on success or a negative number on failure.
47*592ffb21SWarner Losh *
48*592ffb21SWarner Losh * Verifies the AGP device has been initialized and acquired and fills in the
49*592ffb21SWarner Losh * drm_agp_info structure with the information in drm_agp_head::agp_info.
50*592ffb21SWarner Losh */
drm_agp_info(struct drm_device * dev,struct drm_agp_info * info)51*592ffb21SWarner Losh int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
52*592ffb21SWarner Losh {
53*592ffb21SWarner Losh DRM_AGP_KERN *kern;
54*592ffb21SWarner Losh
55*592ffb21SWarner Losh if (!dev->agp || !dev->agp->acquired)
56*592ffb21SWarner Losh return -EINVAL;
57*592ffb21SWarner Losh
58*592ffb21SWarner Losh kern = &dev->agp->agp_info;
59*592ffb21SWarner Losh agp_get_info(dev->agp->bridge, kern);
60*592ffb21SWarner Losh info->agp_version_major = 1;
61*592ffb21SWarner Losh info->agp_version_minor = 0;
62*592ffb21SWarner Losh info->mode = kern->ai_mode;
63*592ffb21SWarner Losh info->aperture_base = kern->ai_aperture_base;
64*592ffb21SWarner Losh info->aperture_size = kern->ai_aperture_size;
65*592ffb21SWarner Losh info->memory_allowed = kern->ai_memory_allowed;
66*592ffb21SWarner Losh info->memory_used = kern->ai_memory_used;
67*592ffb21SWarner Losh info->id_vendor = kern->ai_devid & 0xffff;
68*592ffb21SWarner Losh info->id_device = kern->ai_devid >> 16;
69*592ffb21SWarner Losh
70*592ffb21SWarner Losh return 0;
71*592ffb21SWarner Losh }
72*592ffb21SWarner Losh
73*592ffb21SWarner Losh EXPORT_SYMBOL(drm_agp_info);
74*592ffb21SWarner Losh
drm_agp_info_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)75*592ffb21SWarner Losh int drm_agp_info_ioctl(struct drm_device *dev, void *data,
76*592ffb21SWarner Losh struct drm_file *file_priv)
77*592ffb21SWarner Losh {
78*592ffb21SWarner Losh struct drm_agp_info *info = data;
79*592ffb21SWarner Losh int err;
80*592ffb21SWarner Losh
81*592ffb21SWarner Losh err = drm_agp_info(dev, info);
82*592ffb21SWarner Losh if (err)
83*592ffb21SWarner Losh return err;
84*592ffb21SWarner Losh
85*592ffb21SWarner Losh return 0;
86*592ffb21SWarner Losh }
87*592ffb21SWarner Losh
88*592ffb21SWarner Losh /**
89*592ffb21SWarner Losh * Acquire the AGP device.
90*592ffb21SWarner Losh *
91*592ffb21SWarner Losh * \param dev DRM device that is to acquire AGP.
92*592ffb21SWarner Losh * \return zero on success or a negative number on failure.
93*592ffb21SWarner Losh *
94*592ffb21SWarner Losh * Verifies the AGP device hasn't been acquired before and calls
95*592ffb21SWarner Losh * \c agp_backend_acquire.
96*592ffb21SWarner Losh */
drm_agp_acquire(struct drm_device * dev)97*592ffb21SWarner Losh int drm_agp_acquire(struct drm_device * dev)
98*592ffb21SWarner Losh {
99*592ffb21SWarner Losh int retcode;
100*592ffb21SWarner Losh
101*592ffb21SWarner Losh if (!dev->agp)
102*592ffb21SWarner Losh return -ENODEV;
103*592ffb21SWarner Losh if (dev->agp->acquired)
104*592ffb21SWarner Losh return -EBUSY;
105*592ffb21SWarner Losh retcode = agp_acquire(dev->agp->bridge);
106*592ffb21SWarner Losh if (retcode)
107*592ffb21SWarner Losh return -retcode;
108*592ffb21SWarner Losh dev->agp->acquired = 1;
109*592ffb21SWarner Losh return 0;
110*592ffb21SWarner Losh }
111*592ffb21SWarner Losh
112*592ffb21SWarner Losh EXPORT_SYMBOL(drm_agp_acquire);
113*592ffb21SWarner Losh
114*592ffb21SWarner Losh /**
115*592ffb21SWarner Losh * Acquire the AGP device (ioctl).
116*592ffb21SWarner Losh *
117*592ffb21SWarner Losh * \param inode device inode.
118*592ffb21SWarner Losh * \param file_priv DRM file private.
119*592ffb21SWarner Losh * \param cmd command.
120*592ffb21SWarner Losh * \param arg user argument.
121*592ffb21SWarner Losh * \return zero on success or a negative number on failure.
122*592ffb21SWarner Losh *
123*592ffb21SWarner Losh * Verifies the AGP device hasn't been acquired before and calls
124*592ffb21SWarner Losh * \c agp_backend_acquire.
125*592ffb21SWarner Losh */
drm_agp_acquire_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)126*592ffb21SWarner Losh int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
127*592ffb21SWarner Losh struct drm_file *file_priv)
128*592ffb21SWarner Losh {
129*592ffb21SWarner Losh return drm_agp_acquire((struct drm_device *) file_priv->minor->dev);
130*592ffb21SWarner Losh }
131*592ffb21SWarner Losh
132*592ffb21SWarner Losh /**
133*592ffb21SWarner Losh * Release the AGP device.
134*592ffb21SWarner Losh *
135*592ffb21SWarner Losh * \param dev DRM device that is to release AGP.
136*592ffb21SWarner Losh * \return zero on success or a negative number on failure.
137*592ffb21SWarner Losh *
138*592ffb21SWarner Losh * Verifies the AGP device has been acquired and calls \c agp_backend_release.
139*592ffb21SWarner Losh */
drm_agp_release(struct drm_device * dev)140*592ffb21SWarner Losh int drm_agp_release(struct drm_device * dev)
141*592ffb21SWarner Losh {
142*592ffb21SWarner Losh if (!dev->agp || !dev->agp->acquired)
143*592ffb21SWarner Losh return -EINVAL;
144*592ffb21SWarner Losh agp_release(dev->agp->bridge);
145*592ffb21SWarner Losh dev->agp->acquired = 0;
146*592ffb21SWarner Losh return 0;
147*592ffb21SWarner Losh }
148*592ffb21SWarner Losh EXPORT_SYMBOL(drm_agp_release);
149*592ffb21SWarner Losh
drm_agp_release_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)150*592ffb21SWarner Losh int drm_agp_release_ioctl(struct drm_device *dev, void *data,
151*592ffb21SWarner Losh struct drm_file *file_priv)
152*592ffb21SWarner Losh {
153*592ffb21SWarner Losh return drm_agp_release(dev);
154*592ffb21SWarner Losh }
155*592ffb21SWarner Losh
156*592ffb21SWarner Losh /**
157*592ffb21SWarner Losh * Enable the AGP bus.
158*592ffb21SWarner Losh *
159*592ffb21SWarner Losh * \param dev DRM device that has previously acquired AGP.
160*592ffb21SWarner Losh * \param mode Requested AGP mode.
161*592ffb21SWarner Losh * \return zero on success or a negative number on failure.
162*592ffb21SWarner Losh *
163*592ffb21SWarner Losh * Verifies the AGP device has been acquired but not enabled, and calls
164*592ffb21SWarner Losh * \c agp_enable.
165*592ffb21SWarner Losh */
drm_agp_enable(struct drm_device * dev,struct drm_agp_mode mode)166*592ffb21SWarner Losh int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode)
167*592ffb21SWarner Losh {
168*592ffb21SWarner Losh if (!dev->agp || !dev->agp->acquired)
169*592ffb21SWarner Losh return -EINVAL;
170*592ffb21SWarner Losh
171*592ffb21SWarner Losh dev->agp->mode = mode.mode;
172*592ffb21SWarner Losh agp_enable(dev->agp->bridge, mode.mode);
173*592ffb21SWarner Losh dev->agp->enabled = 1;
174*592ffb21SWarner Losh return 0;
175*592ffb21SWarner Losh }
176*592ffb21SWarner Losh
177*592ffb21SWarner Losh EXPORT_SYMBOL(drm_agp_enable);
178*592ffb21SWarner Losh
drm_agp_enable_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)179*592ffb21SWarner Losh int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
180*592ffb21SWarner Losh struct drm_file *file_priv)
181*592ffb21SWarner Losh {
182*592ffb21SWarner Losh struct drm_agp_mode *mode = data;
183*592ffb21SWarner Losh
184*592ffb21SWarner Losh return drm_agp_enable(dev, *mode);
185*592ffb21SWarner Losh }
186*592ffb21SWarner Losh
187*592ffb21SWarner Losh /**
188*592ffb21SWarner Losh * Allocate AGP memory.
189*592ffb21SWarner Losh *
190*592ffb21SWarner Losh * \param inode device inode.
191*592ffb21SWarner Losh * \param file_priv file private pointer.
192*592ffb21SWarner Losh * \param cmd command.
193*592ffb21SWarner Losh * \param arg pointer to a drm_agp_buffer structure.
194*592ffb21SWarner Losh * \return zero on success or a negative number on failure.
195*592ffb21SWarner Losh *
196*592ffb21SWarner Losh * Verifies the AGP device is present and has been acquired, allocates the
197*592ffb21SWarner Losh * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
198*592ffb21SWarner Losh */
drm_agp_alloc(struct drm_device * dev,struct drm_agp_buffer * request)199*592ffb21SWarner Losh int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
200*592ffb21SWarner Losh {
201*592ffb21SWarner Losh struct drm_agp_mem *entry;
202*592ffb21SWarner Losh DRM_AGP_MEM *memory;
203*592ffb21SWarner Losh unsigned long pages;
204*592ffb21SWarner Losh u32 type;
205*592ffb21SWarner Losh struct agp_memory_info info;
206*592ffb21SWarner Losh
207*592ffb21SWarner Losh if (!dev->agp || !dev->agp->acquired)
208*592ffb21SWarner Losh return -EINVAL;
209*592ffb21SWarner Losh if (!(entry = malloc(sizeof(*entry), DRM_MEM_AGPLISTS, M_NOWAIT)))
210*592ffb21SWarner Losh return -ENOMEM;
211*592ffb21SWarner Losh
212*592ffb21SWarner Losh memset(entry, 0, sizeof(*entry));
213*592ffb21SWarner Losh
214*592ffb21SWarner Losh pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
215*592ffb21SWarner Losh type = (u32) request->type;
216*592ffb21SWarner Losh if (!(memory = agp_alloc_memory(dev->agp->bridge, type, pages << PAGE_SHIFT))) {
217*592ffb21SWarner Losh free(entry, DRM_MEM_AGPLISTS);
218*592ffb21SWarner Losh return -ENOMEM;
219*592ffb21SWarner Losh }
220*592ffb21SWarner Losh
221*592ffb21SWarner Losh entry->handle = (unsigned long)memory;
222*592ffb21SWarner Losh entry->memory = memory;
223*592ffb21SWarner Losh entry->bound = 0;
224*592ffb21SWarner Losh entry->pages = pages;
225*592ffb21SWarner Losh list_add(&entry->head, &dev->agp->memory);
226*592ffb21SWarner Losh
227*592ffb21SWarner Losh agp_memory_info(dev->agp->bridge, entry->memory, &info);
228*592ffb21SWarner Losh
229*592ffb21SWarner Losh request->handle = entry->handle;
230*592ffb21SWarner Losh request->physical = info.ami_physical;
231*592ffb21SWarner Losh
232*592ffb21SWarner Losh return 0;
233*592ffb21SWarner Losh }
234*592ffb21SWarner Losh EXPORT_SYMBOL(drm_agp_alloc);
235*592ffb21SWarner Losh
236*592ffb21SWarner Losh
drm_agp_alloc_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)237*592ffb21SWarner Losh int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
238*592ffb21SWarner Losh struct drm_file *file_priv)
239*592ffb21SWarner Losh {
240*592ffb21SWarner Losh struct drm_agp_buffer *request = data;
241*592ffb21SWarner Losh
242*592ffb21SWarner Losh return drm_agp_alloc(dev, request);
243*592ffb21SWarner Losh }
244*592ffb21SWarner Losh
245*592ffb21SWarner Losh /**
246*592ffb21SWarner Losh * Search for the AGP memory entry associated with a handle.
247*592ffb21SWarner Losh *
248*592ffb21SWarner Losh * \param dev DRM device structure.
249*592ffb21SWarner Losh * \param handle AGP memory handle.
250*592ffb21SWarner Losh * \return pointer to the drm_agp_mem structure associated with \p handle.
251*592ffb21SWarner Losh *
252*592ffb21SWarner Losh * Walks through drm_agp_head::memory until finding a matching handle.
253*592ffb21SWarner Losh */
drm_agp_lookup_entry(struct drm_device * dev,unsigned long handle)254*592ffb21SWarner Losh static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
255*592ffb21SWarner Losh unsigned long handle)
256*592ffb21SWarner Losh {
257*592ffb21SWarner Losh struct drm_agp_mem *entry;
258*592ffb21SWarner Losh
259*592ffb21SWarner Losh list_for_each_entry(entry, &dev->agp->memory, head) {
260*592ffb21SWarner Losh if (entry->handle == handle)
261*592ffb21SWarner Losh return entry;
262*592ffb21SWarner Losh }
263*592ffb21SWarner Losh return NULL;
264*592ffb21SWarner Losh }
265*592ffb21SWarner Losh
266*592ffb21SWarner Losh /**
267*592ffb21SWarner Losh * Unbind AGP memory from the GATT (ioctl).
268*592ffb21SWarner Losh *
269*592ffb21SWarner Losh * \param inode device inode.
270*592ffb21SWarner Losh * \param file_priv DRM file private.
271*592ffb21SWarner Losh * \param cmd command.
272*592ffb21SWarner Losh * \param arg pointer to a drm_agp_binding structure.
273*592ffb21SWarner Losh * \return zero on success or a negative number on failure.
274*592ffb21SWarner Losh *
275*592ffb21SWarner Losh * Verifies the AGP device is present and acquired, looks-up the AGP memory
276*592ffb21SWarner Losh * entry and passes it to the unbind_agp() function.
277*592ffb21SWarner Losh */
drm_agp_unbind(struct drm_device * dev,struct drm_agp_binding * request)278*592ffb21SWarner Losh int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
279*592ffb21SWarner Losh {
280*592ffb21SWarner Losh struct drm_agp_mem *entry;
281*592ffb21SWarner Losh int ret;
282*592ffb21SWarner Losh
283*592ffb21SWarner Losh if (!dev->agp || !dev->agp->acquired)
284*592ffb21SWarner Losh return -EINVAL;
285*592ffb21SWarner Losh if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
286*592ffb21SWarner Losh return -EINVAL;
287*592ffb21SWarner Losh if (!entry->bound)
288*592ffb21SWarner Losh return -EINVAL;
289*592ffb21SWarner Losh ret = drm_unbind_agp(entry->memory);
290*592ffb21SWarner Losh if (ret == 0)
291*592ffb21SWarner Losh entry->bound = 0;
292*592ffb21SWarner Losh return ret;
293*592ffb21SWarner Losh }
294*592ffb21SWarner Losh EXPORT_SYMBOL(drm_agp_unbind);
295*592ffb21SWarner Losh
296*592ffb21SWarner Losh
drm_agp_unbind_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)297*592ffb21SWarner Losh int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
298*592ffb21SWarner Losh struct drm_file *file_priv)
299*592ffb21SWarner Losh {
300*592ffb21SWarner Losh struct drm_agp_binding *request = data;
301*592ffb21SWarner Losh
302*592ffb21SWarner Losh return drm_agp_unbind(dev, request);
303*592ffb21SWarner Losh }
304*592ffb21SWarner Losh
305*592ffb21SWarner Losh /**
306*592ffb21SWarner Losh * Bind AGP memory into the GATT (ioctl)
307*592ffb21SWarner Losh *
308*592ffb21SWarner Losh * \param inode device inode.
309*592ffb21SWarner Losh * \param file_priv DRM file private.
310*592ffb21SWarner Losh * \param cmd command.
311*592ffb21SWarner Losh * \param arg pointer to a drm_agp_binding structure.
312*592ffb21SWarner Losh * \return zero on success or a negative number on failure.
313*592ffb21SWarner Losh *
314*592ffb21SWarner Losh * Verifies the AGP device is present and has been acquired and that no memory
315*592ffb21SWarner Losh * is currently bound into the GATT. Looks-up the AGP memory entry and passes
316*592ffb21SWarner Losh * it to bind_agp() function.
317*592ffb21SWarner Losh */
drm_agp_bind(struct drm_device * dev,struct drm_agp_binding * request)318*592ffb21SWarner Losh int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
319*592ffb21SWarner Losh {
320*592ffb21SWarner Losh struct drm_agp_mem *entry;
321*592ffb21SWarner Losh int retcode;
322*592ffb21SWarner Losh int page;
323*592ffb21SWarner Losh
324*592ffb21SWarner Losh if (!dev->agp || !dev->agp->acquired)
325*592ffb21SWarner Losh return -EINVAL;
326*592ffb21SWarner Losh if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
327*592ffb21SWarner Losh return -EINVAL;
328*592ffb21SWarner Losh if (entry->bound)
329*592ffb21SWarner Losh return -EINVAL;
330*592ffb21SWarner Losh page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
331*592ffb21SWarner Losh if ((retcode = drm_bind_agp(entry->memory, page)))
332*592ffb21SWarner Losh return retcode;
333*592ffb21SWarner Losh entry->bound = dev->agp->base + (page << PAGE_SHIFT);
334*592ffb21SWarner Losh DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
335*592ffb21SWarner Losh dev->agp->base, entry->bound);
336*592ffb21SWarner Losh return 0;
337*592ffb21SWarner Losh }
338*592ffb21SWarner Losh EXPORT_SYMBOL(drm_agp_bind);
339*592ffb21SWarner Losh
340*592ffb21SWarner Losh
drm_agp_bind_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)341*592ffb21SWarner Losh int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
342*592ffb21SWarner Losh struct drm_file *file_priv)
343*592ffb21SWarner Losh {
344*592ffb21SWarner Losh struct drm_agp_binding *request = data;
345*592ffb21SWarner Losh
346*592ffb21SWarner Losh return drm_agp_bind(dev, request);
347*592ffb21SWarner Losh }
348*592ffb21SWarner Losh
349*592ffb21SWarner Losh /**
350*592ffb21SWarner Losh * Free AGP memory (ioctl).
351*592ffb21SWarner Losh *
352*592ffb21SWarner Losh * \param inode device inode.
353*592ffb21SWarner Losh * \param file_priv DRM file private.
354*592ffb21SWarner Losh * \param cmd command.
355*592ffb21SWarner Losh * \param arg pointer to a drm_agp_buffer structure.
356*592ffb21SWarner Losh * \return zero on success or a negative number on failure.
357*592ffb21SWarner Losh *
358*592ffb21SWarner Losh * Verifies the AGP device is present and has been acquired and looks up the
359*592ffb21SWarner Losh * AGP memory entry. If the memory it's currently bound, unbind it via
360*592ffb21SWarner Losh * unbind_agp(). Frees it via free_agp() as well as the entry itself
361*592ffb21SWarner Losh * and unlinks from the doubly linked list it's inserted in.
362*592ffb21SWarner Losh */
drm_agp_free(struct drm_device * dev,struct drm_agp_buffer * request)363*592ffb21SWarner Losh int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
364*592ffb21SWarner Losh {
365*592ffb21SWarner Losh struct drm_agp_mem *entry;
366*592ffb21SWarner Losh
367*592ffb21SWarner Losh if (!dev->agp || !dev->agp->acquired)
368*592ffb21SWarner Losh return -EINVAL;
369*592ffb21SWarner Losh if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
370*592ffb21SWarner Losh return -EINVAL;
371*592ffb21SWarner Losh if (entry->bound)
372*592ffb21SWarner Losh drm_unbind_agp(entry->memory);
373*592ffb21SWarner Losh
374*592ffb21SWarner Losh list_del(&entry->head);
375*592ffb21SWarner Losh
376*592ffb21SWarner Losh drm_free_agp(entry->memory, entry->pages);
377*592ffb21SWarner Losh free(entry, DRM_MEM_AGPLISTS);
378*592ffb21SWarner Losh return 0;
379*592ffb21SWarner Losh }
380*592ffb21SWarner Losh EXPORT_SYMBOL(drm_agp_free);
381*592ffb21SWarner Losh
382*592ffb21SWarner Losh
383*592ffb21SWarner Losh
drm_agp_free_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)384*592ffb21SWarner Losh int drm_agp_free_ioctl(struct drm_device *dev, void *data,
385*592ffb21SWarner Losh struct drm_file *file_priv)
386*592ffb21SWarner Losh {
387*592ffb21SWarner Losh struct drm_agp_buffer *request = data;
388*592ffb21SWarner Losh
389*592ffb21SWarner Losh return drm_agp_free(dev, request);
390*592ffb21SWarner Losh }
391*592ffb21SWarner Losh
392*592ffb21SWarner Losh /**
393*592ffb21SWarner Losh * Initialize the AGP resources.
394*592ffb21SWarner Losh *
395*592ffb21SWarner Losh * \return pointer to a drm_agp_head structure.
396*592ffb21SWarner Losh *
397*592ffb21SWarner Losh * Gets the drm_agp_t structure which is made available by the agpgart module
398*592ffb21SWarner Losh * via the inter_module_* functions. Creates and initializes a drm_agp_head
399*592ffb21SWarner Losh * structure.
400*592ffb21SWarner Losh */
drm_agp_init(struct drm_device * dev)401*592ffb21SWarner Losh struct drm_agp_head *drm_agp_init(struct drm_device *dev)
402*592ffb21SWarner Losh {
403*592ffb21SWarner Losh struct drm_agp_head *head = NULL;
404*592ffb21SWarner Losh
405*592ffb21SWarner Losh if (!(head = malloc(sizeof(*head), DRM_MEM_AGPLISTS, M_NOWAIT)))
406*592ffb21SWarner Losh return NULL;
407*592ffb21SWarner Losh memset((void *)head, 0, sizeof(*head));
408*592ffb21SWarner Losh head->bridge = agp_find_device();
409*592ffb21SWarner Losh if (!head->bridge) {
410*592ffb21SWarner Losh free(head, DRM_MEM_AGPLISTS);
411*592ffb21SWarner Losh return NULL;
412*592ffb21SWarner Losh } else {
413*592ffb21SWarner Losh agp_get_info(head->bridge, &head->agp_info);
414*592ffb21SWarner Losh }
415*592ffb21SWarner Losh INIT_LIST_HEAD(&head->memory);
416*592ffb21SWarner Losh head->cant_use_aperture = 0;
417*592ffb21SWarner Losh head->base = head->agp_info.ai_aperture_base;
418*592ffb21SWarner Losh return head;
419*592ffb21SWarner Losh }
420*592ffb21SWarner Losh
421*592ffb21SWarner Losh #ifdef FREEBSD_NOTYET
422*592ffb21SWarner Losh /**
423*592ffb21SWarner Losh * Binds a collection of pages into AGP memory at the given offset, returning
424*592ffb21SWarner Losh * the AGP memory structure containing them.
425*592ffb21SWarner Losh *
426*592ffb21SWarner Losh * No reference is held on the pages during this time -- it is up to the
427*592ffb21SWarner Losh * caller to handle that.
428*592ffb21SWarner Losh */
429*592ffb21SWarner Losh DRM_AGP_MEM *
drm_agp_bind_pages(struct drm_device * dev,struct page ** pages,unsigned long num_pages,uint32_t gtt_offset,u32 type)430*592ffb21SWarner Losh drm_agp_bind_pages(struct drm_device *dev,
431*592ffb21SWarner Losh struct page **pages,
432*592ffb21SWarner Losh unsigned long num_pages,
433*592ffb21SWarner Losh uint32_t gtt_offset,
434*592ffb21SWarner Losh u32 type)
435*592ffb21SWarner Losh {
436*592ffb21SWarner Losh DRM_AGP_MEM *mem;
437*592ffb21SWarner Losh int ret, i;
438*592ffb21SWarner Losh
439*592ffb21SWarner Losh DRM_DEBUG("\n");
440*592ffb21SWarner Losh
441*592ffb21SWarner Losh mem = agp_allocate_memory(dev->agp->bridge, num_pages,
442*592ffb21SWarner Losh type);
443*592ffb21SWarner Losh if (mem == NULL) {
444*592ffb21SWarner Losh DRM_ERROR("Failed to allocate memory for %ld pages\n",
445*592ffb21SWarner Losh num_pages);
446*592ffb21SWarner Losh return NULL;
447*592ffb21SWarner Losh }
448*592ffb21SWarner Losh
449*592ffb21SWarner Losh for (i = 0; i < num_pages; i++)
450*592ffb21SWarner Losh mem->pages[i] = pages[i];
451*592ffb21SWarner Losh mem->page_count = num_pages;
452*592ffb21SWarner Losh
453*592ffb21SWarner Losh mem->is_flushed = true;
454*592ffb21SWarner Losh ret = agp_bind_memory(mem, gtt_offset / PAGE_SIZE);
455*592ffb21SWarner Losh if (ret != 0) {
456*592ffb21SWarner Losh DRM_ERROR("Failed to bind AGP memory: %d\n", ret);
457*592ffb21SWarner Losh agp_free_memory(mem);
458*592ffb21SWarner Losh return NULL;
459*592ffb21SWarner Losh }
460*592ffb21SWarner Losh
461*592ffb21SWarner Losh return mem;
462*592ffb21SWarner Losh }
463*592ffb21SWarner Losh EXPORT_SYMBOL(drm_agp_bind_pages);
464*592ffb21SWarner Losh #endif /* FREEBSD_NOTYET */
465*592ffb21SWarner Losh
466*592ffb21SWarner Losh #endif /* __OS_HAS_AGP */
467