ttm_agp_backend.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. /*
  29. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  30. * Keith Packard.
  31. */
  32. #define pr_fmt(fmt) "[TTM] " fmt
  33. #include <drm/ttm/ttm_module.h>
  34. #include <drm/ttm/ttm_bo_driver.h>
  35. #include <drm/ttm/ttm_page_alloc.h>
  36. #include <drm/ttm/ttm_placement.h>
  37. #include <linux/agp_backend.h>
  38. #include <linux/module.h>
  39. #include <linux/slab.h>
  40. #include <linux/io.h>
  41. #include <asm/agp.h>
  42. struct ttm_agp_backend {
  43. struct ttm_tt ttm;
  44. struct agp_memory *mem;
  45. struct agp_bridge_data *bridge;
  46. };
  47. int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_resource *bo_mem)
  48. {
  49. struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
  50. struct page *dummy_read_page = ttm_bo_glob.dummy_read_page;
  51. struct drm_mm_node *node = bo_mem->mm_node;
  52. struct agp_memory *mem;
  53. int ret, cached = (bo_mem->placement & TTM_PL_FLAG_CACHED);
  54. unsigned i;
  55. if (agp_be->mem)
  56. return 0;
  57. mem = agp_allocate_memory(agp_be->bridge, ttm->num_pages, AGP_USER_MEMORY);
  58. if (unlikely(mem == NULL))
  59. return -ENOMEM;
  60. mem->page_count = 0;
  61. for (i = 0; i < ttm->num_pages; i++) {
  62. struct page *page = ttm->pages[i];
  63. if (!page)
  64. page = dummy_read_page;
  65. mem->pages[mem->page_count++] = page;
  66. }
  67. agp_be->mem = mem;
  68. mem->is_flushed = 1;
  69. mem->type = (cached) ? AGP_USER_CACHED_MEMORY : AGP_USER_MEMORY;
  70. ret = agp_bind_memory(mem, node->start);
  71. if (ret)
  72. pr_err("AGP Bind memory failed\n");
  73. return ret;
  74. }
  75. EXPORT_SYMBOL(ttm_agp_bind);
  76. void ttm_agp_unbind(struct ttm_tt *ttm)
  77. {
  78. struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
  79. if (agp_be->mem) {
  80. if (agp_be->mem->is_bound) {
  81. agp_unbind_memory(agp_be->mem);
  82. return;
  83. }
  84. agp_free_memory(agp_be->mem);
  85. agp_be->mem = NULL;
  86. }
  87. }
  88. EXPORT_SYMBOL(ttm_agp_unbind);
  89. bool ttm_agp_is_bound(struct ttm_tt *ttm)
  90. {
  91. struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
  92. if (!ttm)
  93. return false;
  94. return (agp_be->mem != NULL);
  95. }
  96. EXPORT_SYMBOL(ttm_agp_is_bound);
  97. void ttm_agp_destroy(struct ttm_tt *ttm)
  98. {
  99. struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
  100. if (agp_be->mem)
  101. ttm_agp_unbind(ttm);
  102. ttm_tt_fini(ttm);
  103. kfree(agp_be);
  104. }
  105. EXPORT_SYMBOL(ttm_agp_destroy);
  106. struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo,
  107. struct agp_bridge_data *bridge,
  108. uint32_t page_flags)
  109. {
  110. struct ttm_agp_backend *agp_be;
  111. agp_be = kmalloc(sizeof(*agp_be), GFP_KERNEL);
  112. if (!agp_be)
  113. return NULL;
  114. agp_be->mem = NULL;
  115. agp_be->bridge = bridge;
  116. if (ttm_tt_init(&agp_be->ttm, bo, page_flags)) {
  117. kfree(agp_be);
  118. return NULL;
  119. }
  120. return &agp_be->ttm;
  121. }
  122. EXPORT_SYMBOL(ttm_agp_tt_create);