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