ttm_agp_backend.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_device.h>
  34. #include <drm/ttm/ttm_tt.h>
  35. #include <drm/ttm/ttm_resource.h>
  36. #include <linux/agp_backend.h>
  37. #include <linux/export.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_glob.dummy_read_page;
  51. struct agp_memory *mem;
  52. int ret, cached = ttm->caching == ttm_cached;
  53. unsigned i;
  54. if (agp_be->mem)
  55. return 0;
  56. mem = agp_allocate_memory(agp_be->bridge, ttm->num_pages, AGP_USER_MEMORY);
  57. if (unlikely(mem == NULL))
  58. return -ENOMEM;
  59. mem->page_count = 0;
  60. for (i = 0; i < ttm->num_pages; i++) {
  61. struct page *page = ttm->pages[i];
  62. if (!page)
  63. page = dummy_read_page;
  64. mem->pages[mem->page_count++] = page;
  65. }
  66. agp_be->mem = mem;
  67. mem->is_flushed = 1;
  68. mem->type = (cached) ? AGP_USER_CACHED_MEMORY : AGP_USER_MEMORY;
  69. ret = agp_bind_memory(mem, bo_mem->start);
  70. if (ret)
  71. pr_err("AGP Bind memory failed\n");
  72. return ret;
  73. }
  74. EXPORT_SYMBOL(ttm_agp_bind);
  75. void ttm_agp_unbind(struct ttm_tt *ttm)
  76. {
  77. struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
  78. if (agp_be->mem) {
  79. if (agp_be->mem->is_bound) {
  80. agp_unbind_memory(agp_be->mem);
  81. return;
  82. }
  83. agp_free_memory(agp_be->mem);
  84. agp_be->mem = NULL;
  85. }
  86. }
  87. EXPORT_SYMBOL(ttm_agp_unbind);
  88. bool ttm_agp_is_bound(struct ttm_tt *ttm)
  89. {
  90. struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
  91. if (!ttm)
  92. return false;
  93. return (agp_be->mem != NULL);
  94. }
  95. EXPORT_SYMBOL(ttm_agp_is_bound);
  96. void ttm_agp_destroy(struct ttm_tt *ttm)
  97. {
  98. struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
  99. if (agp_be->mem)
  100. ttm_agp_unbind(ttm);
  101. ttm_tt_fini(ttm);
  102. kfree(agp_be);
  103. }
  104. EXPORT_SYMBOL(ttm_agp_destroy);
  105. struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo,
  106. struct agp_bridge_data *bridge,
  107. uint32_t page_flags)
  108. {
  109. struct ttm_agp_backend *agp_be;
  110. agp_be = kmalloc_obj(*agp_be);
  111. if (!agp_be)
  112. return NULL;
  113. agp_be->mem = NULL;
  114. agp_be->bridge = bridge;
  115. if (ttm_tt_init(&agp_be->ttm, bo, page_flags, ttm_write_combined, 0)) {
  116. kfree(agp_be);
  117. return NULL;
  118. }
  119. return &agp_be->ttm;
  120. }
  121. EXPORT_SYMBOL(ttm_agp_tt_create);