OSDN Git Service

initial import
[android-x86/device-viliv-s5.git] / psb-kernel-source-4.41.1 / 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 "drmP.h"
35 #include <linux/module.h>
36
37 #if __OS_HAS_AGP
38
39 /**
40  * Get AGP information.
41  *
42  * \param inode device inode.
43  * \param file_priv DRM file private.
44  * \param cmd command.
45  * \param arg pointer to a (output) drm_agp_info structure.
46  * \return zero on success or a negative number on failure.
47  *
48  * Verifies the AGP device has been initialized and acquired and fills in the
49  * drm_agp_info structure with the information in drm_agp_head::agp_info.
50  */
51 int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
52 {
53         DRM_AGP_KERN *kern;
54
55         if (!dev->agp || !dev->agp->acquired)
56                 return -EINVAL;
57
58         kern = &dev->agp->agp_info;
59         info->agp_version_major = kern->version.major;
60         info->agp_version_minor = kern->version.minor;
61         info->mode = kern->mode;
62         info->aperture_base = kern->aper_base;
63         info->aperture_size = kern->aper_size * 1024 * 1024;
64         info->memory_allowed = kern->max_memory << PAGE_SHIFT;
65         info->memory_used = kern->current_memory << PAGE_SHIFT;
66         info->id_vendor = kern->device->vendor;
67         info->id_device = kern->device->device;
68
69         return 0;
70 }
71 EXPORT_SYMBOL(drm_agp_info);
72
73 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
74                        struct drm_file *file_priv)
75 {
76         struct drm_agp_info *info = data;
77         int err;
78
79         err = drm_agp_info(dev, info);
80         if (err)
81                 return err;
82
83         return 0;
84 }
85
86 /**
87  * Acquire the AGP device.
88  *
89  * \param dev DRM device that is to acquire AGP.
90  * \return zero on success or a negative number on failure.
91  *
92  * Verifies the AGP device hasn't been acquired before and calls
93  * \c agp_backend_acquire.
94  */
95 int drm_agp_acquire(struct drm_device * dev)
96 {
97 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
98         int retcode;
99 #endif
100
101         if (!dev->agp)
102                 return -ENODEV;
103         if (dev->agp->acquired)
104                 return -EBUSY;
105 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
106         if ((retcode = agp_backend_acquire()))
107                 return retcode;
108 #else
109         if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev)))
110                 return -ENODEV;
111 #endif
112
113         dev->agp->acquired = 1;
114         return 0;
115 }
116 EXPORT_SYMBOL(drm_agp_acquire);
117
118 /**
119  * Acquire the AGP device (ioctl).
120  *
121  * \param inode device inode.
122  * \param file_priv DRM file private.
123  * \param cmd command.
124  * \param arg user argument.
125  * \return zero on success or a negative number on failure.
126  *
127  * Verifies the AGP device hasn't been acquired before and calls
128  * \c agp_backend_acquire.
129  */
130 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
131                           struct drm_file *file_priv)
132 {
133         return drm_agp_acquire((struct drm_device *) file_priv->head->dev);
134 }
135
136 /**
137  * Release the AGP device.
138  *
139  * \param dev DRM device that is to release AGP.
140  * \return zero on success or a negative number on failure.
141  *
142  * Verifies the AGP device has been acquired and calls \c agp_backend_release.
143  */
144 int drm_agp_release(struct drm_device *dev)
145 {
146         if (!dev->agp || !dev->agp->acquired)
147                 return -EINVAL;
148 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
149         agp_backend_release();
150 #else
151         agp_backend_release(dev->agp->bridge);
152 #endif
153         dev->agp->acquired = 0;
154         return 0;
155
156 }
157 EXPORT_SYMBOL(drm_agp_release);
158
159 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
160                           struct drm_file *file_priv)
161 {
162         return drm_agp_release(dev);
163 }
164
165 /**
166  * Enable the AGP bus.
167  *
168  * \param dev DRM device that has previously acquired AGP.
169  * \param mode Requested AGP mode.
170  * \return zero on success or a negative number on failure.
171  *
172  * Verifies the AGP device has been acquired but not enabled, and calls
173  * \c agp_enable.
174  */
175 int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
176 {
177         if (!dev->agp || !dev->agp->acquired)
178                 return -EINVAL;
179
180         dev->agp->mode = mode.mode;
181 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
182         agp_enable(mode.mode);
183 #else
184         agp_enable(dev->agp->bridge, mode.mode);
185 #endif
186         dev->agp->enabled = 1;
187         return 0;
188 }
189 EXPORT_SYMBOL(drm_agp_enable);
190
191 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
192                          struct drm_file *file_priv)
193 {
194         struct drm_agp_mode *mode = data;
195
196         return drm_agp_enable(dev, *mode);
197 }
198
199 /**
200  * Allocate AGP memory.
201  *
202  * \param inode device inode.
203  * \param file_priv file private pointer.
204  * \param cmd command.
205  * \param arg pointer to a drm_agp_buffer structure.
206  * \return zero on success or a negative number on failure.
207  *
208  * Verifies the AGP device is present and has been acquired, allocates the
209  * memory via alloc_agp() and creates a drm_agp_mem entry for it.
210  */
211 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
212 {
213         struct drm_agp_mem *entry;
214         DRM_AGP_MEM *memory;
215         unsigned long pages;
216         u32 type;
217
218         if (!dev->agp || !dev->agp->acquired)
219                 return -EINVAL;
220         if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
221                 return -ENOMEM;
222
223         memset(entry, 0, sizeof(*entry));
224
225         pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
226         type = (u32) request->type;
227         if (!(memory = drm_alloc_agp(dev, pages, type))) {
228                 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
229                 return -ENOMEM;
230         }
231
232         entry->handle = (unsigned long)memory->key + 1;
233         entry->memory = memory;
234         entry->bound = 0;
235         entry->pages = pages;
236         list_add(&entry->head, &dev->agp->memory);
237
238         request->handle = entry->handle;
239         request->physical = memory->physical;
240
241         return 0;
242 }
243 EXPORT_SYMBOL(drm_agp_alloc);
244
245
246 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
247                         struct drm_file *file_priv)
248 {
249         struct drm_agp_buffer *request = data;
250
251         return drm_agp_alloc(dev, request);
252 }
253
254 /**
255  * Search for the AGP memory entry associated with a handle.
256  *
257  * \param dev DRM device structure.
258  * \param handle AGP memory handle.
259  * \return pointer to the drm_agp_mem structure associated with \p handle.
260  *
261  * Walks through drm_agp_head::memory until finding a matching handle.
262  */
263 static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
264                                            unsigned long handle)
265 {
266         struct drm_agp_mem *entry;
267
268         list_for_each_entry(entry, &dev->agp->memory, head) {
269                 if (entry->handle == handle)
270                         return entry;
271         }
272         return NULL;
273 }
274
275 /**
276  * Unbind AGP memory from the GATT (ioctl).
277  *
278  * \param inode device inode.
279  * \param file_priv DRM file private.
280  * \param cmd command.
281  * \param arg pointer to a drm_agp_binding structure.
282  * \return zero on success or a negative number on failure.
283  *
284  * Verifies the AGP device is present and acquired, looks-up the AGP memory
285  * entry and passes it to the unbind_agp() function.
286  */
287 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
288 {
289         struct drm_agp_mem *entry;
290         int ret;
291
292         if (!dev->agp || !dev->agp->acquired)
293                 return -EINVAL;
294         if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
295                 return -EINVAL;
296         if (!entry->bound)
297                 return -EINVAL;
298         ret = drm_unbind_agp(entry->memory);
299         if (ret == 0)
300                 entry->bound = 0;
301         return ret;
302 }
303 EXPORT_SYMBOL(drm_agp_unbind);
304
305
306 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
307                          struct drm_file *file_priv)
308 {
309         struct drm_agp_binding *request = data;
310
311         return drm_agp_unbind(dev, request);
312 }
313
314
315 /**
316  * Bind AGP memory into the GATT (ioctl)
317  *
318  * \param inode device inode.
319  * \param file_priv DRM file private.
320  * \param cmd command.
321  * \param arg pointer to a drm_agp_binding structure.
322  * \return zero on success or a negative number on failure.
323  *
324  * Verifies the AGP device is present and has been acquired and that no memory
325  * is currently bound into the GATT. Looks-up the AGP memory entry and passes
326  * it to bind_agp() function.
327  */
328 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
329 {
330         struct drm_agp_mem *entry;
331         int retcode;
332         int page;
333
334         if (!dev->agp || !dev->agp->acquired)
335                 return -EINVAL;
336         if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
337                 return -EINVAL;
338         if (entry->bound)
339                 return -EINVAL;
340         page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
341         if ((retcode = drm_bind_agp(entry->memory, page)))
342                 return retcode;
343         entry->bound = dev->agp->base + (page << PAGE_SHIFT);
344         DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
345                   dev->agp->base, entry->bound);
346         return 0;
347 }
348 EXPORT_SYMBOL(drm_agp_bind);
349
350
351 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
352                        struct drm_file *file_priv)
353 {
354         struct drm_agp_binding *request = data;
355
356         return drm_agp_bind(dev, request);
357 }
358
359
360 /**
361  * Free AGP memory (ioctl).
362  *
363  * \param inode device inode.
364  * \param file_priv DRM file private.
365  * \param cmd command.
366  * \param arg pointer to a drm_agp_buffer structure.
367  * \return zero on success or a negative number on failure.
368  *
369  * Verifies the AGP device is present and has been acquired and looks up the
370  * AGP memory entry. If the memory it's currently bound, unbind it via
371  * unbind_agp(). Frees it via free_agp() as well as the entry itself
372  * and unlinks from the doubly linked list it's inserted in.
373  */
374 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
375 {
376         struct drm_agp_mem *entry;
377
378         if (!dev->agp || !dev->agp->acquired)
379                 return -EINVAL;
380         if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
381                 return -EINVAL;
382         if (entry->bound)
383                 drm_unbind_agp(entry->memory);
384
385         list_del(&entry->head);
386
387         drm_free_agp(entry->memory, entry->pages);
388         drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
389         return 0;
390 }
391 EXPORT_SYMBOL(drm_agp_free);
392
393
394
395 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
396                        struct drm_file *file_priv)
397 {
398         struct drm_agp_buffer *request = data;
399
400         return drm_agp_free(dev, request);
401 }
402
403
404 /**
405  * Initialize the AGP resources.
406  *
407  * \return pointer to a drm_agp_head structure.
408  *
409  * Gets the drm_agp_t structure which is made available by the agpgart module
410  * via the inter_module_* functions. Creates and initializes a drm_agp_head
411  * structure.
412  */
413 struct drm_agp_head *drm_agp_init(struct drm_device *dev)
414 {
415         struct drm_agp_head *head = NULL;
416
417         if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
418                 return NULL;
419         memset((void *)head, 0, sizeof(*head));
420
421 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
422         agp_copy_info(&head->agp_info);
423 #else
424         head->bridge = agp_find_bridge(dev->pdev);
425         if (!head->bridge) {
426                 if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
427                         drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
428                         return NULL;
429                 }
430                 agp_copy_info(head->bridge, &head->agp_info);
431                 agp_backend_release(head->bridge);
432         } else {
433                 agp_copy_info(head->bridge, &head->agp_info);
434         }
435 #endif
436         if (head->agp_info.chipset == NOT_SUPPORTED) {
437                 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
438                 return NULL;
439         }
440         INIT_LIST_HEAD(&head->memory);
441         head->cant_use_aperture = head->agp_info.cant_use_aperture;
442         head->page_mask = head->agp_info.page_mask;
443         head->base = head->agp_info.aper_base;
444         return head;
445 }
446
447 /** Calls agp_allocate_memory() */
448 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
449 DRM_AGP_MEM *drm_agp_allocate_memory(size_t pages, u32 type)
450 {
451         return agp_allocate_memory(pages, type);
452 }
453 #else
454 DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data *bridge,
455                                      size_t pages, u32 type)
456 {
457         return agp_allocate_memory(bridge, pages, type);
458 }
459 #endif
460
461 /** Calls agp_free_memory() */
462 int drm_agp_free_memory(DRM_AGP_MEM * handle)
463 {
464         if (!handle)
465                 return 0;
466         agp_free_memory(handle);
467         return 1;
468 }
469
470 /** Calls agp_bind_memory() */
471 int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start)
472 {
473         if (!handle)
474                 return -EINVAL;
475         return agp_bind_memory(handle, start);
476 }
477 EXPORT_SYMBOL(drm_agp_bind_memory);
478
479 /** Calls agp_unbind_memory() */
480 int drm_agp_unbind_memory(DRM_AGP_MEM * handle)
481 {
482         if (!handle)
483                 return -EINVAL;
484         return agp_unbind_memory(handle);
485 }
486
487
488
489 /*
490  * AGP ttm backend interface.
491  */
492
493 #ifndef AGP_USER_TYPES
494 #define AGP_USER_TYPES (1 << 16)
495 #define AGP_USER_MEMORY (AGP_USER_TYPES)
496 #define AGP_USER_CACHED_MEMORY (AGP_USER_TYPES + 1)
497 #endif
498 #define AGP_REQUIRED_MAJOR 0
499 #define AGP_REQUIRED_MINOR 102
500
501 static int drm_agp_needs_unbind_cache_adjust(struct drm_ttm_backend *backend)
502 {
503         return ((backend->flags & DRM_BE_FLAG_BOUND_CACHED) ? 0 : 1);
504 }
505
506
507 static int drm_agp_populate(struct drm_ttm_backend *backend,
508                             unsigned long num_pages, struct page **pages)
509 {
510         struct drm_agp_ttm_backend *agp_be =
511                 container_of(backend, struct drm_agp_ttm_backend, backend);
512         struct page **cur_page, **last_page = pages + num_pages;
513         DRM_AGP_MEM *mem;
514
515         DRM_DEBUG("drm_agp_populate_ttm\n");
516 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
517         mem = drm_agp_allocate_memory(num_pages, AGP_USER_MEMORY);
518 #else
519         mem = drm_agp_allocate_memory(agp_be->bridge, num_pages, AGP_USER_MEMORY);
520 #endif
521         if (!mem)
522                 return -ENOMEM;
523
524         DRM_DEBUG("Current page count is %ld\n", (long) mem->page_count);
525         mem->page_count = 0;
526         for (cur_page = pages; cur_page < last_page; ++cur_page)
527                 mem->memory[mem->page_count++] = phys_to_gart(page_to_phys(*cur_page));
528         agp_be->mem = mem;
529         return 0;
530 }
531
532 static int drm_agp_bind_ttm(struct drm_ttm_backend *backend,
533                             struct drm_bo_mem_reg *bo_mem)
534 {
535         struct drm_agp_ttm_backend *agp_be =
536                 container_of(backend, struct drm_agp_ttm_backend, backend);
537         DRM_AGP_MEM *mem = agp_be->mem;
538         int ret;
539         int snooped = (bo_mem->flags & DRM_BO_FLAG_CACHED) && !(bo_mem->flags & DRM_BO_FLAG_CACHED_MAPPED);
540
541         DRM_DEBUG("drm_agp_bind_ttm\n");
542         mem->is_flushed = TRUE;
543         mem->type = AGP_USER_MEMORY;
544         /* CACHED MAPPED implies not snooped memory */
545         if (snooped)
546                 mem->type = AGP_USER_CACHED_MEMORY;
547
548         ret = drm_agp_bind_memory(mem, bo_mem->mm_node->start);
549         if (ret)
550                 DRM_ERROR("AGP Bind memory failed\n");
551
552         DRM_FLAG_MASKED(backend->flags, (bo_mem->flags & DRM_BO_FLAG_CACHED) ?
553                         DRM_BE_FLAG_BOUND_CACHED : 0,
554                         DRM_BE_FLAG_BOUND_CACHED);
555         return ret;
556 }
557
558 static int drm_agp_unbind_ttm(struct drm_ttm_backend *backend)
559 {
560         struct drm_agp_ttm_backend *agp_be =
561                 container_of(backend, struct drm_agp_ttm_backend, backend);
562
563         DRM_DEBUG("drm_agp_unbind_ttm\n");
564         if (agp_be->mem->is_bound)
565                 return drm_agp_unbind_memory(agp_be->mem);
566         else
567                 return 0;
568 }
569
570 static void drm_agp_clear_ttm(struct drm_ttm_backend *backend)
571 {
572         struct drm_agp_ttm_backend *agp_be =
573                 container_of(backend, struct drm_agp_ttm_backend, backend);
574         DRM_AGP_MEM *mem = agp_be->mem;
575
576         DRM_DEBUG("drm_agp_clear_ttm\n");
577         if (mem) {
578                 backend->func->unbind(backend);
579                 agp_free_memory(mem);
580         }
581         agp_be->mem = NULL;
582 }
583
584 static void drm_agp_destroy_ttm(struct drm_ttm_backend *backend)
585 {
586         struct drm_agp_ttm_backend *agp_be;
587
588         if (backend) {
589                 DRM_DEBUG("drm_agp_destroy_ttm\n");
590                 agp_be = container_of(backend, struct drm_agp_ttm_backend, backend);
591                 if (agp_be && agp_be->mem)
592                         backend->func->clear(backend);
593         }
594 }
595
596 static struct drm_ttm_backend_func agp_ttm_backend = {
597         .needs_ub_cache_adjust = drm_agp_needs_unbind_cache_adjust,
598         .populate = drm_agp_populate,
599         .clear = drm_agp_clear_ttm,
600         .bind = drm_agp_bind_ttm,
601         .unbind = drm_agp_unbind_ttm,
602         .destroy =  drm_agp_destroy_ttm,
603 };
604
605 struct drm_ttm_backend *drm_agp_init_ttm(struct drm_device *dev)
606 {
607
608         struct drm_agp_ttm_backend *agp_be;
609         struct agp_kern_info *info;
610
611         if (!dev->agp) {
612                 DRM_ERROR("AGP is not initialized.\n");
613                 return NULL;
614         }
615         info = &dev->agp->agp_info;
616
617         if (info->version.major != AGP_REQUIRED_MAJOR ||
618             info->version.minor < AGP_REQUIRED_MINOR) {
619                 DRM_ERROR("Wrong agpgart version %d.%d\n"
620                           "\tYou need at least version %d.%d.\n",
621                           info->version.major,
622                           info->version.minor,
623                           AGP_REQUIRED_MAJOR,
624                           AGP_REQUIRED_MINOR);
625                 return NULL;
626         }
627
628
629         agp_be = drm_calloc(1, sizeof(*agp_be), DRM_MEM_TTM);
630         if (!agp_be)
631                 return NULL;
632
633         agp_be->mem = NULL;
634
635         agp_be->bridge = dev->agp->bridge;
636         agp_be->populated = FALSE;
637         agp_be->backend.func = &agp_ttm_backend;
638         agp_be->backend.dev = dev;
639
640         return &agp_be->backend;
641 }
642 EXPORT_SYMBOL(drm_agp_init_ttm);
643
644 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
645 void drm_agp_flush_chipset(struct drm_device *dev)
646 {
647         agp_flush_chipset(dev->agp->bridge);
648 }
649 EXPORT_SYMBOL(drm_agp_flush_chipset);
650 #endif
651
652 #endif                          /* __OS_HAS_AGP */