]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm2/drm_agpsupport.c
Revert crap accidentally committed
[FreeBSD/FreeBSD.git] / sys / dev / drm2 / drm_agpsupport.c
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 <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <dev/drm2/drmP.h>
38 #include <linux/slab.h>
39
40 #if __OS_HAS_AGP
41
42 /**
43  * Get AGP information.
44  *
45  * \param inode device inode.
46  * \param file_priv DRM file private.
47  * \param cmd command.
48  * \param arg pointer to a (output) drm_agp_info structure.
49  * \return zero on success or a negative number on failure.
50  *
51  * Verifies the AGP device has been initialized and acquired and fills in the
52  * drm_agp_info structure with the information in drm_agp_head::agp_info.
53  */
54 int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
55 {
56         DRM_AGP_KERN *kern;
57
58         if (!dev->agp || !dev->agp->acquired)
59                 return -EINVAL;
60
61         kern = &dev->agp->agp_info;
62         agp_get_info(dev->agp->bridge, kern);
63         info->agp_version_major = 1;
64         info->agp_version_minor = 0;
65         info->mode              = kern->ai_mode;
66         info->aperture_base     = kern->ai_aperture_base;
67         info->aperture_size     = kern->ai_aperture_size;
68         info->memory_allowed    = kern->ai_memory_allowed;
69         info->memory_used       = kern->ai_memory_used;
70         info->id_vendor         = kern->ai_devid & 0xffff;
71         info->id_device         = kern->ai_devid >> 16;
72
73         return 0;
74 }
75
76 EXPORT_SYMBOL(drm_agp_info);
77
78 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
79                        struct drm_file *file_priv)
80 {
81         struct drm_agp_info *info = data;
82         int err;
83
84         err = drm_agp_info(dev, info);
85         if (err)
86                 return err;
87
88         return 0;
89 }
90
91 /**
92  * Acquire the AGP device.
93  *
94  * \param dev DRM device that is to acquire AGP.
95  * \return zero on success or a negative number on failure.
96  *
97  * Verifies the AGP device hasn't been acquired before and calls
98  * \c agp_backend_acquire.
99  */
100 int drm_agp_acquire(struct drm_device * dev)
101 {
102         int retcode;
103
104         if (!dev->agp)
105                 return -ENODEV;
106         if (dev->agp->acquired)
107                 return -EBUSY;
108         retcode = agp_acquire(dev->agp->bridge);
109         if (retcode)
110                 return -retcode;
111         dev->agp->acquired = 1;
112         return 0;
113 }
114
115 EXPORT_SYMBOL(drm_agp_acquire);
116
117 /**
118  * Acquire the AGP device (ioctl).
119  *
120  * \param inode device inode.
121  * \param file_priv DRM file private.
122  * \param cmd command.
123  * \param arg user argument.
124  * \return zero on success or a negative number on failure.
125  *
126  * Verifies the AGP device hasn't been acquired before and calls
127  * \c agp_backend_acquire.
128  */
129 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
130                           struct drm_file *file_priv)
131 {
132         return drm_agp_acquire((struct drm_device *) file_priv->minor->dev);
133 }
134
135 /**
136  * Release the AGP device.
137  *
138  * \param dev DRM device that is to release AGP.
139  * \return zero on success or a negative number on failure.
140  *
141  * Verifies the AGP device has been acquired and calls \c agp_backend_release.
142  */
143 int drm_agp_release(struct drm_device * dev)
144 {
145         if (!dev->agp || !dev->agp->acquired)
146                 return -EINVAL;
147         agp_release(dev->agp->bridge);
148         dev->agp->acquired = 0;
149         return 0;
150 }
151 EXPORT_SYMBOL(drm_agp_release);
152
153 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
154                           struct drm_file *file_priv)
155 {
156         return drm_agp_release(dev);
157 }
158
159 /**
160  * Enable the AGP bus.
161  *
162  * \param dev DRM device that has previously acquired AGP.
163  * \param mode Requested AGP mode.
164  * \return zero on success or a negative number on failure.
165  *
166  * Verifies the AGP device has been acquired but not enabled, and calls
167  * \c agp_enable.
168  */
169 int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode)
170 {
171         if (!dev->agp || !dev->agp->acquired)
172                 return -EINVAL;
173
174         dev->agp->mode = mode.mode;
175         agp_enable(dev->agp->bridge, mode.mode);
176         dev->agp->enabled = 1;
177         return 0;
178 }
179
180 EXPORT_SYMBOL(drm_agp_enable);
181
182 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
183                          struct drm_file *file_priv)
184 {
185         struct drm_agp_mode *mode = data;
186
187         return drm_agp_enable(dev, *mode);
188 }
189
190 /**
191  * Allocate AGP memory.
192  *
193  * \param inode device inode.
194  * \param file_priv file private pointer.
195  * \param cmd command.
196  * \param arg pointer to a drm_agp_buffer structure.
197  * \return zero on success or a negative number on failure.
198  *
199  * Verifies the AGP device is present and has been acquired, allocates the
200  * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
201  */
202 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
203 {
204         struct drm_agp_mem *entry;
205         DRM_AGP_MEM *memory;
206         unsigned long pages;
207         u32 type;
208         struct agp_memory_info info;
209
210         if (!dev->agp || !dev->agp->acquired)
211                 return -EINVAL;
212         if (!(entry = kzalloc(sizeof(*entry), GFP_KERNEL)))
213                 return -ENOMEM;
214
215         pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
216         type = (u32) request->type;
217         if (!(memory = agp_alloc_memory(dev->agp->bridge, type, pages << PAGE_SHIFT))) {
218                 kfree(entry);
219                 return -ENOMEM;
220         }
221
222         entry->handle = (unsigned long)memory;
223         entry->memory = memory;
224         entry->bound = 0;
225         entry->pages = pages;
226         list_add(&entry->head, &dev->agp->memory);
227
228         agp_memory_info(dev->agp->bridge, entry->memory, &info);
229
230         request->handle = entry->handle;
231         request->physical = info.ami_physical;
232
233         return 0;
234 }
235 EXPORT_SYMBOL(drm_agp_alloc);
236
237
238 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
239                         struct drm_file *file_priv)
240 {
241         struct drm_agp_buffer *request = data;
242
243         return drm_agp_alloc(dev, request);
244 }
245
246 /**
247  * Search for the AGP memory entry associated with a handle.
248  *
249  * \param dev DRM device structure.
250  * \param handle AGP memory handle.
251  * \return pointer to the drm_agp_mem structure associated with \p handle.
252  *
253  * Walks through drm_agp_head::memory until finding a matching handle.
254  */
255 static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
256                                            unsigned long handle)
257 {
258         struct drm_agp_mem *entry;
259
260         list_for_each_entry(entry, &dev->agp->memory, head) {
261                 if (entry->handle == handle)
262                         return entry;
263         }
264         return NULL;
265 }
266
267 /**
268  * Unbind AGP memory from the GATT (ioctl).
269  *
270  * \param inode device inode.
271  * \param file_priv DRM file private.
272  * \param cmd command.
273  * \param arg pointer to a drm_agp_binding structure.
274  * \return zero on success or a negative number on failure.
275  *
276  * Verifies the AGP device is present and acquired, looks-up the AGP memory
277  * entry and passes it to the unbind_agp() function.
278  */
279 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
280 {
281         struct drm_agp_mem *entry;
282         int ret;
283
284         if (!dev->agp || !dev->agp->acquired)
285                 return -EINVAL;
286         if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
287                 return -EINVAL;
288         if (!entry->bound)
289                 return -EINVAL;
290         ret = drm_unbind_agp(entry->memory);
291         if (ret == 0)
292                 entry->bound = 0;
293         return ret;
294 }
295 EXPORT_SYMBOL(drm_agp_unbind);
296
297
298 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
299                          struct drm_file *file_priv)
300 {
301         struct drm_agp_binding *request = data;
302
303         return drm_agp_unbind(dev, request);
304 }
305
306 /**
307  * Bind AGP memory into the GATT (ioctl)
308  *
309  * \param inode device inode.
310  * \param file_priv DRM file private.
311  * \param cmd command.
312  * \param arg pointer to a drm_agp_binding structure.
313  * \return zero on success or a negative number on failure.
314  *
315  * Verifies the AGP device is present and has been acquired and that no memory
316  * is currently bound into the GATT. Looks-up the AGP memory entry and passes
317  * it to bind_agp() function.
318  */
319 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
320 {
321         struct drm_agp_mem *entry;
322         int retcode;
323         int page;
324
325         if (!dev->agp || !dev->agp->acquired)
326                 return -EINVAL;
327         if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
328                 return -EINVAL;
329         if (entry->bound)
330                 return -EINVAL;
331         page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
332         if ((retcode = drm_bind_agp(entry->memory, page)))
333                 return retcode;
334         entry->bound = dev->agp->base + (page << PAGE_SHIFT);
335         DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
336                   dev->agp->base, entry->bound);
337         return 0;
338 }
339 EXPORT_SYMBOL(drm_agp_bind);
340
341
342 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
343                        struct drm_file *file_priv)
344 {
345         struct drm_agp_binding *request = data;
346
347         return drm_agp_bind(dev, request);
348 }
349
350 /**
351  * Free AGP memory (ioctl).
352  *
353  * \param inode device inode.
354  * \param file_priv DRM file private.
355  * \param cmd command.
356  * \param arg pointer to a drm_agp_buffer structure.
357  * \return zero on success or a negative number on failure.
358  *
359  * Verifies the AGP device is present and has been acquired and looks up the
360  * AGP memory entry. If the memory it's currently bound, unbind it via
361  * unbind_agp(). Frees it via free_agp() as well as the entry itself
362  * and unlinks from the doubly linked list it's inserted in.
363  */
364 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
365 {
366         struct drm_agp_mem *entry;
367
368         if (!dev->agp || !dev->agp->acquired)
369                 return -EINVAL;
370         if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
371                 return -EINVAL;
372         if (entry->bound)
373                 drm_unbind_agp(entry->memory);
374
375         list_del(&entry->head);
376
377         drm_free_agp(entry->memory, entry->pages);
378         kfree(entry);
379         return 0;
380 }
381 EXPORT_SYMBOL(drm_agp_free);
382
383
384
385 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
386                        struct drm_file *file_priv)
387 {
388         struct drm_agp_buffer *request = data;
389
390         return drm_agp_free(dev, request);
391 }
392
393 /**
394  * Initialize the AGP resources.
395  *
396  * \return pointer to a drm_agp_head structure.
397  *
398  * Gets the drm_agp_t structure which is made available by the agpgart module
399  * via the inter_module_* functions. Creates and initializes a drm_agp_head
400  * structure.
401  */
402 struct drm_agp_head *drm_agp_init(struct drm_device *dev)
403 {
404         struct drm_agp_head *head = NULL;
405
406         if (!(head = kzalloc(sizeof(*head), GFP_KERNEL)))
407                 return NULL;
408         head->bridge = agp_find_device();
409         if (!head->bridge) {
410                 kfree(head);
411                 return NULL;
412         } else {
413                 agp_get_info(head->bridge, &head->agp_info);
414         }
415         INIT_LIST_HEAD(&head->memory);
416         head->cant_use_aperture = 0;
417         head->base = head->agp_info.ai_aperture_base;
418         return head;
419 }
420
421 #ifdef FREEBSD_NOTYET
422 /**
423  * Binds a collection of pages into AGP memory at the given offset, returning
424  * the AGP memory structure containing them.
425  *
426  * No reference is held on the pages during this time -- it is up to the
427  * caller to handle that.
428  */
429 DRM_AGP_MEM *
430 drm_agp_bind_pages(struct drm_device *dev,
431                    struct page **pages,
432                    unsigned long num_pages,
433                    uint32_t gtt_offset,
434                    u32 type)
435 {
436         DRM_AGP_MEM *mem;
437         int ret, i;
438
439         DRM_DEBUG("\n");
440
441         mem = agp_allocate_memory(dev->agp->bridge, num_pages,
442                                       type);
443         if (mem == NULL) {
444                 DRM_ERROR("Failed to allocate memory for %ld pages\n",
445                           num_pages);
446                 return NULL;
447         }
448
449         for (i = 0; i < num_pages; i++)
450                 mem->pages[i] = pages[i];
451         mem->page_count = num_pages;
452
453         mem->is_flushed = true;
454         ret = agp_bind_memory(mem, gtt_offset / PAGE_SIZE);
455         if (ret != 0) {
456                 DRM_ERROR("Failed to bind AGP memory: %d\n", ret);
457                 agp_free_memory(mem);
458                 return NULL;
459         }
460
461         return mem;
462 }
463 EXPORT_SYMBOL(drm_agp_bind_pages);
464 #endif /* FREEBSD_NOTYET */
465
466 #endif /* __OS_HAS_AGP */