Merge tag 'pwm/for-5.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry...
[linux-2.6-microblaze.git] / drivers / gpu / drm / 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 <linux/module.h>
35 #include <linux/pci.h>
36 #include <linux/slab.h>
37
38 #include <asm/agp.h>
39
40 #include <drm/drm_agpsupport.h>
41 #include <drm/drm_device.h>
42 #include <drm/drm_drv.h>
43 #include <drm/drm_file.h>
44 #include <drm/drm_print.h>
45
46 #include "drm_legacy.h"
47
48 /*
49  * Get AGP information.
50  *
51  * \return zero on success or a negative number on failure.
52  *
53  * Verifies the AGP device has been initialized and acquired and fills in the
54  * drm_agp_info structure with the information in drm_agp_head::agp_info.
55  */
56 int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
57 {
58         struct agp_kern_info *kern;
59
60         if (!dev->agp || !dev->agp->acquired)
61                 return -EINVAL;
62
63         kern = &dev->agp->agp_info;
64         info->agp_version_major = kern->version.major;
65         info->agp_version_minor = kern->version.minor;
66         info->mode = kern->mode;
67         info->aperture_base = kern->aper_base;
68         info->aperture_size = kern->aper_size * 1024 * 1024;
69         info->memory_allowed = kern->max_memory << PAGE_SHIFT;
70         info->memory_used = kern->current_memory << PAGE_SHIFT;
71         info->id_vendor = kern->device->vendor;
72         info->id_device = kern->device->device;
73
74         return 0;
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         struct pci_dev *pdev = to_pci_dev(dev->dev);
103
104         if (!dev->agp)
105                 return -ENODEV;
106         if (dev->agp->acquired)
107                 return -EBUSY;
108         dev->agp->bridge = agp_backend_acquire(pdev);
109         if (!dev->agp->bridge)
110                 return -ENODEV;
111         dev->agp->acquired = 1;
112         return 0;
113 }
114 EXPORT_SYMBOL(drm_agp_acquire);
115
116 /*
117  * Acquire the AGP device (ioctl).
118  *
119  * \return zero on success or a negative number on failure.
120  *
121  * Verifies the AGP device hasn't been acquired before and calls
122  * \c agp_backend_acquire.
123  */
124 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
125                           struct drm_file *file_priv)
126 {
127         return drm_agp_acquire((struct drm_device *) file_priv->minor->dev);
128 }
129
130 /*
131  * Release the AGP device.
132  *
133  * \param dev DRM device that is to release AGP.
134  * \return zero on success or a negative number on failure.
135  *
136  * Verifies the AGP device has been acquired and calls \c agp_backend_release.
137  */
138 int drm_agp_release(struct drm_device *dev)
139 {
140         if (!dev->agp || !dev->agp->acquired)
141                 return -EINVAL;
142         agp_backend_release(dev->agp->bridge);
143         dev->agp->acquired = 0;
144         return 0;
145 }
146 EXPORT_SYMBOL(drm_agp_release);
147
148 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
149                           struct drm_file *file_priv)
150 {
151         return drm_agp_release(dev);
152 }
153
154 /*
155  * Enable the AGP bus.
156  *
157  * \param dev DRM device that has previously acquired AGP.
158  * \param mode Requested AGP mode.
159  * \return zero on success or a negative number on failure.
160  *
161  * Verifies the AGP device has been acquired but not enabled, and calls
162  * \c agp_enable.
163  */
164 int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
165 {
166         if (!dev->agp || !dev->agp->acquired)
167                 return -EINVAL;
168
169         dev->agp->mode = mode.mode;
170         agp_enable(dev->agp->bridge, mode.mode);
171         dev->agp->enabled = 1;
172         return 0;
173 }
174 EXPORT_SYMBOL(drm_agp_enable);
175
176 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
177                          struct drm_file *file_priv)
178 {
179         struct drm_agp_mode *mode = data;
180
181         return drm_agp_enable(dev, *mode);
182 }
183
184 /*
185  * Allocate AGP memory.
186  *
187  * \return zero on success or a negative number on failure.
188  *
189  * Verifies the AGP device is present and has been acquired, allocates the
190  * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
191  */
192 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
193 {
194         struct drm_agp_mem *entry;
195         struct agp_memory *memory;
196         unsigned long pages;
197         u32 type;
198
199         if (!dev->agp || !dev->agp->acquired)
200                 return -EINVAL;
201         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
202         if (!entry)
203                 return -ENOMEM;
204
205         pages = DIV_ROUND_UP(request->size, PAGE_SIZE);
206         type = (u32) request->type;
207         memory = agp_allocate_memory(dev->agp->bridge, pages, type);
208         if (!memory) {
209                 kfree(entry);
210                 return -ENOMEM;
211         }
212
213         entry->handle = (unsigned long)memory->key + 1;
214         entry->memory = memory;
215         entry->bound = 0;
216         entry->pages = pages;
217         list_add(&entry->head, &dev->agp->memory);
218
219         request->handle = entry->handle;
220         request->physical = memory->physical;
221
222         return 0;
223 }
224 EXPORT_SYMBOL(drm_agp_alloc);
225
226
227 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
228                         struct drm_file *file_priv)
229 {
230         struct drm_agp_buffer *request = data;
231
232         return drm_agp_alloc(dev, request);
233 }
234
235 /*
236  * Search for the AGP memory entry associated with a handle.
237  *
238  * \param dev DRM device structure.
239  * \param handle AGP memory handle.
240  * \return pointer to the drm_agp_mem structure associated with \p handle.
241  *
242  * Walks through drm_agp_head::memory until finding a matching handle.
243  */
244 static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device *dev,
245                                                 unsigned long handle)
246 {
247         struct drm_agp_mem *entry;
248
249         list_for_each_entry(entry, &dev->agp->memory, head) {
250                 if (entry->handle == handle)
251                         return entry;
252         }
253         return NULL;
254 }
255
256 /*
257  * Unbind AGP memory from the GATT (ioctl).
258  *
259  * \return zero on success or a negative number on failure.
260  *
261  * Verifies the AGP device is present and acquired, looks-up the AGP memory
262  * entry and passes it to the unbind_agp() function.
263  */
264 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
265 {
266         struct drm_agp_mem *entry;
267         int ret;
268
269         if (!dev->agp || !dev->agp->acquired)
270                 return -EINVAL;
271         entry = drm_agp_lookup_entry(dev, request->handle);
272         if (!entry || !entry->bound)
273                 return -EINVAL;
274         ret = agp_unbind_memory(entry->memory);
275         if (ret == 0)
276                 entry->bound = 0;
277         return ret;
278 }
279 EXPORT_SYMBOL(drm_agp_unbind);
280
281
282 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
283                          struct drm_file *file_priv)
284 {
285         struct drm_agp_binding *request = data;
286
287         return drm_agp_unbind(dev, request);
288 }
289
290 /*
291  * Bind AGP memory into the GATT (ioctl)
292  *
293  * \return zero on success or a negative number on failure.
294  *
295  * Verifies the AGP device is present and has been acquired and that no memory
296  * is currently bound into the GATT. Looks-up the AGP memory entry and passes
297  * it to bind_agp() function.
298  */
299 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
300 {
301         struct drm_agp_mem *entry;
302         int retcode;
303         int page;
304
305         if (!dev->agp || !dev->agp->acquired)
306                 return -EINVAL;
307         entry = drm_agp_lookup_entry(dev, request->handle);
308         if (!entry || entry->bound)
309                 return -EINVAL;
310         page = DIV_ROUND_UP(request->offset, PAGE_SIZE);
311         retcode = agp_bind_memory(entry->memory, page);
312         if (retcode)
313                 return retcode;
314         entry->bound = dev->agp->base + (page << PAGE_SHIFT);
315         DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
316                   dev->agp->base, entry->bound);
317         return 0;
318 }
319 EXPORT_SYMBOL(drm_agp_bind);
320
321
322 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
323                        struct drm_file *file_priv)
324 {
325         struct drm_agp_binding *request = data;
326
327         return drm_agp_bind(dev, request);
328 }
329
330 /*
331  * Free AGP memory (ioctl).
332  *
333  * \return zero on success or a negative number on failure.
334  *
335  * Verifies the AGP device is present and has been acquired and looks up the
336  * AGP memory entry. If the memory is currently bound, unbind it via
337  * unbind_agp(). Frees it via free_agp() as well as the entry itself
338  * and unlinks from the doubly linked list it's inserted in.
339  */
340 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
341 {
342         struct drm_agp_mem *entry;
343
344         if (!dev->agp || !dev->agp->acquired)
345                 return -EINVAL;
346         entry = drm_agp_lookup_entry(dev, request->handle);
347         if (!entry)
348                 return -EINVAL;
349         if (entry->bound)
350                 agp_unbind_memory(entry->memory);
351
352         list_del(&entry->head);
353
354         agp_free_memory(entry->memory);
355         kfree(entry);
356         return 0;
357 }
358 EXPORT_SYMBOL(drm_agp_free);
359
360
361 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
362                        struct drm_file *file_priv)
363 {
364         struct drm_agp_buffer *request = data;
365
366         return drm_agp_free(dev, request);
367 }
368
369 /*
370  * Initialize the AGP resources.
371  *
372  * \return pointer to a drm_agp_head structure.
373  *
374  * Gets the drm_agp_t structure which is made available by the agpgart module
375  * via the inter_module_* functions. Creates and initializes a drm_agp_head
376  * structure.
377  *
378  * Note that final cleanup of the kmalloced structure is directly done in
379  * drm_pci_agp_destroy.
380  */
381 struct drm_agp_head *drm_agp_init(struct drm_device *dev)
382 {
383         struct pci_dev *pdev = to_pci_dev(dev->dev);
384         struct drm_agp_head *head = NULL;
385
386         head = kzalloc(sizeof(*head), GFP_KERNEL);
387         if (!head)
388                 return NULL;
389         head->bridge = agp_find_bridge(pdev);
390         if (!head->bridge) {
391                 head->bridge = agp_backend_acquire(pdev);
392                 if (!head->bridge) {
393                         kfree(head);
394                         return NULL;
395                 }
396                 agp_copy_info(head->bridge, &head->agp_info);
397                 agp_backend_release(head->bridge);
398         } else {
399                 agp_copy_info(head->bridge, &head->agp_info);
400         }
401         if (head->agp_info.chipset == NOT_SUPPORTED) {
402                 kfree(head);
403                 return NULL;
404         }
405         INIT_LIST_HEAD(&head->memory);
406         head->cant_use_aperture = head->agp_info.cant_use_aperture;
407         head->page_mask = head->agp_info.page_mask;
408         head->base = head->agp_info.aper_base;
409         return head;
410 }
411 /* Only exported for i810.ko */
412 EXPORT_SYMBOL(drm_agp_init);
413
414 /**
415  * drm_legacy_agp_clear - Clear AGP resource list
416  * @dev: DRM device
417  *
418  * Iterate over all AGP resources and remove them. But keep the AGP head
419  * intact so it can still be used. It is safe to call this if AGP is disabled or
420  * was already removed.
421  *
422  * Cleanup is only done for drivers who have DRIVER_LEGACY set.
423  */
424 void drm_legacy_agp_clear(struct drm_device *dev)
425 {
426         struct drm_agp_mem *entry, *tempe;
427
428         if (!dev->agp)
429                 return;
430         if (!drm_core_check_feature(dev, DRIVER_LEGACY))
431                 return;
432
433         list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
434                 if (entry->bound)
435                         agp_unbind_memory(entry->memory);
436                 agp_free_memory(entry->memory);
437                 kfree(entry);
438         }
439         INIT_LIST_HEAD(&dev->agp->memory);
440
441         if (dev->agp->acquired)
442                 drm_agp_release(dev);
443
444         dev->agp->acquired = 0;
445         dev->agp->enabled = 0;
446 }