amdgpu_gart.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <drm/drmP.h>
  29. #include <drm/amdgpu_drm.h>
  30. #ifdef CONFIG_X86
  31. #include <asm/set_memory.h>
  32. #endif
  33. #include "amdgpu.h"
  34. /*
  35. * GART
  36. * The GART (Graphics Aperture Remapping Table) is an aperture
  37. * in the GPU's address space. System pages can be mapped into
  38. * the aperture and look like contiguous pages from the GPU's
  39. * perspective. A page table maps the pages in the aperture
  40. * to the actual backing pages in system memory.
  41. *
  42. * Radeon GPUs support both an internal GART, as described above,
  43. * and AGP. AGP works similarly, but the GART table is configured
  44. * and maintained by the northbridge rather than the driver.
  45. * Radeon hw has a separate AGP aperture that is programmed to
  46. * point to the AGP aperture provided by the northbridge and the
  47. * requests are passed through to the northbridge aperture.
  48. * Both AGP and internal GART can be used at the same time, however
  49. * that is not currently supported by the driver.
  50. *
  51. * This file handles the common internal GART management.
  52. */
  53. /*
  54. * Common GART table functions.
  55. */
  56. /**
  57. * amdgpu_gart_table_vram_alloc - allocate vram for gart page table
  58. *
  59. * @adev: amdgpu_device pointer
  60. *
  61. * Allocate video memory for GART page table
  62. * (pcie r4xx, r5xx+). These asics require the
  63. * gart table to be in video memory.
  64. * Returns 0 for success, error for failure.
  65. */
  66. int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
  67. {
  68. return amdgpu_bo_create_kernel(adev, adev->gart.table_size, PAGE_SIZE,
  69. AMDGPU_GEM_DOMAIN_VRAM, &adev->gart.robj,
  70. &adev->gart.table_addr, &adev->gart.ptr);
  71. }
  72. /**
  73. * amdgpu_gart_table_vram_free - free gart page table vram
  74. *
  75. * @adev: amdgpu_device pointer
  76. *
  77. * Free the video memory used for the GART page table
  78. * (pcie r4xx, r5xx+). These asics require the gart table to
  79. * be in video memory.
  80. */
  81. void amdgpu_gart_table_vram_free(struct amdgpu_device *adev)
  82. {
  83. amdgpu_bo_free_kernel(&adev->gart.robj,
  84. &adev->gart.table_addr,
  85. &adev->gart.ptr);
  86. }
  87. /*
  88. * Common gart functions.
  89. */
  90. /**
  91. * amdgpu_gart_unbind - unbind pages from the gart page table
  92. *
  93. * @adev: amdgpu_device pointer
  94. * @offset: offset into the GPU's gart aperture
  95. * @pages: number of pages to unbind
  96. *
  97. * Unbinds the requested pages from the gart page table and
  98. * replaces them with the dummy page (all asics).
  99. * Returns 0 for success, -EINVAL for failure.
  100. */
  101. int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
  102. int pages)
  103. {
  104. unsigned t;
  105. unsigned p;
  106. int i, j;
  107. u64 page_base;
  108. /* Starting from VEGA10, system bit must be 0 to mean invalid. */
  109. uint64_t flags = 0;
  110. if (!adev->gart.ready) {
  111. WARN(1, "trying to unbind memory from uninitialized GART !\n");
  112. return -EINVAL;
  113. }
  114. t = offset / AMDGPU_GPU_PAGE_SIZE;
  115. p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  116. for (i = 0; i < pages; i++, p++) {
  117. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  118. adev->gart.pages[p] = NULL;
  119. #endif
  120. page_base = adev->dummy_page.addr;
  121. if (!adev->gart.ptr)
  122. continue;
  123. for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
  124. amdgpu_gart_set_pte_pde(adev, adev->gart.ptr,
  125. t, page_base, flags);
  126. page_base += AMDGPU_GPU_PAGE_SIZE;
  127. }
  128. }
  129. mb();
  130. amdgpu_gart_flush_gpu_tlb(adev, 0);
  131. return 0;
  132. }
  133. /**
  134. * amdgpu_gart_map - map dma_addresses into GART entries
  135. *
  136. * @adev: amdgpu_device pointer
  137. * @offset: offset into the GPU's gart aperture
  138. * @pages: number of pages to bind
  139. * @dma_addr: DMA addresses of pages
  140. *
  141. * Map the dma_addresses into GART entries (all asics).
  142. * Returns 0 for success, -EINVAL for failure.
  143. */
  144. int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
  145. int pages, dma_addr_t *dma_addr, uint64_t flags,
  146. void *dst)
  147. {
  148. uint64_t page_base;
  149. unsigned i, j, t;
  150. if (!adev->gart.ready) {
  151. WARN(1, "trying to bind memory to uninitialized GART !\n");
  152. return -EINVAL;
  153. }
  154. t = offset / AMDGPU_GPU_PAGE_SIZE;
  155. for (i = 0; i < pages; i++) {
  156. page_base = dma_addr[i];
  157. for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
  158. amdgpu_gart_set_pte_pde(adev, dst, t, page_base, flags);
  159. page_base += AMDGPU_GPU_PAGE_SIZE;
  160. }
  161. }
  162. return 0;
  163. }
  164. /**
  165. * amdgpu_gart_bind - bind pages into the gart page table
  166. *
  167. * @adev: amdgpu_device pointer
  168. * @offset: offset into the GPU's gart aperture
  169. * @pages: number of pages to bind
  170. * @pagelist: pages to bind
  171. * @dma_addr: DMA addresses of pages
  172. *
  173. * Binds the requested pages to the gart page table
  174. * (all asics).
  175. * Returns 0 for success, -EINVAL for failure.
  176. */
  177. int amdgpu_gart_bind(struct amdgpu_device *adev, uint64_t offset,
  178. int pages, struct page **pagelist, dma_addr_t *dma_addr,
  179. uint64_t flags)
  180. {
  181. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  182. unsigned i,t,p;
  183. #endif
  184. int r;
  185. if (!adev->gart.ready) {
  186. WARN(1, "trying to bind memory to uninitialized GART !\n");
  187. return -EINVAL;
  188. }
  189. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  190. t = offset / AMDGPU_GPU_PAGE_SIZE;
  191. p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  192. for (i = 0; i < pages; i++, p++)
  193. adev->gart.pages[p] = pagelist[i];
  194. #endif
  195. if (!adev->gart.ptr)
  196. return 0;
  197. r = amdgpu_gart_map(adev, offset, pages, dma_addr, flags,
  198. adev->gart.ptr);
  199. if (r)
  200. return r;
  201. mb();
  202. amdgpu_gart_flush_gpu_tlb(adev, 0);
  203. return 0;
  204. }
  205. /**
  206. * amdgpu_gart_init - init the driver info for managing the gart
  207. *
  208. * @adev: amdgpu_device pointer
  209. *
  210. * Allocate the dummy page and init the gart driver info (all asics).
  211. * Returns 0 for success, error for failure.
  212. */
  213. int amdgpu_gart_init(struct amdgpu_device *adev)
  214. {
  215. int r;
  216. if (adev->dummy_page.page)
  217. return 0;
  218. /* We need PAGE_SIZE >= AMDGPU_GPU_PAGE_SIZE */
  219. if (PAGE_SIZE < AMDGPU_GPU_PAGE_SIZE) {
  220. DRM_ERROR("Page size is smaller than GPU page size!\n");
  221. return -EINVAL;
  222. }
  223. r = amdgpu_dummy_page_init(adev);
  224. if (r)
  225. return r;
  226. /* Compute table size */
  227. adev->gart.num_cpu_pages = adev->mc.gart_size / PAGE_SIZE;
  228. adev->gart.num_gpu_pages = adev->mc.gart_size / AMDGPU_GPU_PAGE_SIZE;
  229. DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
  230. adev->gart.num_cpu_pages, adev->gart.num_gpu_pages);
  231. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  232. /* Allocate pages table */
  233. adev->gart.pages = vzalloc(sizeof(void *) * adev->gart.num_cpu_pages);
  234. if (adev->gart.pages == NULL) {
  235. amdgpu_gart_fini(adev);
  236. return -ENOMEM;
  237. }
  238. #endif
  239. return 0;
  240. }
  241. /**
  242. * amdgpu_gart_fini - tear down the driver info for managing the gart
  243. *
  244. * @adev: amdgpu_device pointer
  245. *
  246. * Tear down the gart driver info and free the dummy page (all asics).
  247. */
  248. void amdgpu_gart_fini(struct amdgpu_device *adev)
  249. {
  250. if (adev->gart.ready) {
  251. /* unbind pages */
  252. amdgpu_gart_unbind(adev, 0, adev->gart.num_cpu_pages);
  253. }
  254. adev->gart.ready = false;
  255. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  256. vfree(adev->gart.pages);
  257. adev->gart.pages = NULL;
  258. #endif
  259. amdgpu_dummy_page_fini(adev);
  260. }