amdgpu_gart.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. int r;
  69. if (adev->gart.robj == NULL) {
  70. r = amdgpu_bo_create(adev, adev->gart.table_size,
  71. PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
  72. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  73. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  74. NULL, NULL, 0, &adev->gart.robj);
  75. if (r) {
  76. return r;
  77. }
  78. }
  79. return 0;
  80. }
  81. /**
  82. * amdgpu_gart_table_vram_pin - pin gart page table in vram
  83. *
  84. * @adev: amdgpu_device pointer
  85. *
  86. * Pin the GART page table in vram so it will not be moved
  87. * by the memory manager (pcie r4xx, r5xx+). These asics require the
  88. * gart table to be in video memory.
  89. * Returns 0 for success, error for failure.
  90. */
  91. int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev)
  92. {
  93. uint64_t gpu_addr;
  94. int r;
  95. r = amdgpu_bo_reserve(adev->gart.robj, false);
  96. if (unlikely(r != 0))
  97. return r;
  98. r = amdgpu_bo_pin(adev->gart.robj,
  99. AMDGPU_GEM_DOMAIN_VRAM, &gpu_addr);
  100. if (r) {
  101. amdgpu_bo_unreserve(adev->gart.robj);
  102. return r;
  103. }
  104. r = amdgpu_bo_kmap(adev->gart.robj, &adev->gart.ptr);
  105. if (r)
  106. amdgpu_bo_unpin(adev->gart.robj);
  107. amdgpu_bo_unreserve(adev->gart.robj);
  108. adev->gart.table_addr = gpu_addr;
  109. return r;
  110. }
  111. /**
  112. * amdgpu_gart_table_vram_unpin - unpin gart page table in vram
  113. *
  114. * @adev: amdgpu_device pointer
  115. *
  116. * Unpin the GART page table in vram (pcie r4xx, r5xx+).
  117. * These asics require the gart table to be in video memory.
  118. */
  119. void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev)
  120. {
  121. int r;
  122. if (adev->gart.robj == NULL) {
  123. return;
  124. }
  125. r = amdgpu_bo_reserve(adev->gart.robj, true);
  126. if (likely(r == 0)) {
  127. amdgpu_bo_kunmap(adev->gart.robj);
  128. amdgpu_bo_unpin(adev->gart.robj);
  129. amdgpu_bo_unreserve(adev->gart.robj);
  130. adev->gart.ptr = NULL;
  131. }
  132. }
  133. /**
  134. * amdgpu_gart_table_vram_free - free gart page table vram
  135. *
  136. * @adev: amdgpu_device pointer
  137. *
  138. * Free the video memory used for the GART page table
  139. * (pcie r4xx, r5xx+). These asics require the gart table to
  140. * be in video memory.
  141. */
  142. void amdgpu_gart_table_vram_free(struct amdgpu_device *adev)
  143. {
  144. if (adev->gart.robj == NULL) {
  145. return;
  146. }
  147. amdgpu_bo_unref(&adev->gart.robj);
  148. }
  149. /*
  150. * Common gart functions.
  151. */
  152. /**
  153. * amdgpu_gart_unbind - unbind pages from the gart page table
  154. *
  155. * @adev: amdgpu_device pointer
  156. * @offset: offset into the GPU's gart aperture
  157. * @pages: number of pages to unbind
  158. *
  159. * Unbinds the requested pages from the gart page table and
  160. * replaces them with the dummy page (all asics).
  161. * Returns 0 for success, -EINVAL for failure.
  162. */
  163. int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
  164. int pages)
  165. {
  166. unsigned t;
  167. unsigned p;
  168. int i, j;
  169. u64 page_base;
  170. /* Starting from VEGA10, system bit must be 0 to mean invalid. */
  171. uint64_t flags = 0;
  172. if (!adev->gart.ready) {
  173. WARN(1, "trying to unbind memory from uninitialized GART !\n");
  174. return -EINVAL;
  175. }
  176. t = offset / AMDGPU_GPU_PAGE_SIZE;
  177. p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  178. for (i = 0; i < pages; i++, p++) {
  179. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  180. adev->gart.pages[p] = NULL;
  181. #endif
  182. page_base = adev->dummy_page.addr;
  183. if (!adev->gart.ptr)
  184. continue;
  185. for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
  186. amdgpu_gart_set_pte_pde(adev, adev->gart.ptr,
  187. t, page_base, flags);
  188. page_base += AMDGPU_GPU_PAGE_SIZE;
  189. }
  190. }
  191. mb();
  192. amdgpu_gart_flush_gpu_tlb(adev, 0);
  193. return 0;
  194. }
  195. /**
  196. * amdgpu_gart_map - map dma_addresses into GART entries
  197. *
  198. * @adev: amdgpu_device pointer
  199. * @offset: offset into the GPU's gart aperture
  200. * @pages: number of pages to bind
  201. * @dma_addr: DMA addresses of pages
  202. *
  203. * Map the dma_addresses into GART entries (all asics).
  204. * Returns 0 for success, -EINVAL for failure.
  205. */
  206. int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
  207. int pages, dma_addr_t *dma_addr, uint64_t flags,
  208. void *dst)
  209. {
  210. uint64_t page_base;
  211. unsigned i, j, t;
  212. if (!adev->gart.ready) {
  213. WARN(1, "trying to bind memory to uninitialized GART !\n");
  214. return -EINVAL;
  215. }
  216. t = offset / AMDGPU_GPU_PAGE_SIZE;
  217. for (i = 0; i < pages; i++) {
  218. page_base = dma_addr[i];
  219. for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
  220. amdgpu_gart_set_pte_pde(adev, dst, t, page_base, flags);
  221. page_base += AMDGPU_GPU_PAGE_SIZE;
  222. }
  223. }
  224. return 0;
  225. }
  226. /**
  227. * amdgpu_gart_bind - bind pages into the gart page table
  228. *
  229. * @adev: amdgpu_device pointer
  230. * @offset: offset into the GPU's gart aperture
  231. * @pages: number of pages to bind
  232. * @pagelist: pages to bind
  233. * @dma_addr: DMA addresses of pages
  234. *
  235. * Binds the requested pages to the gart page table
  236. * (all asics).
  237. * Returns 0 for success, -EINVAL for failure.
  238. */
  239. int amdgpu_gart_bind(struct amdgpu_device *adev, uint64_t offset,
  240. int pages, struct page **pagelist, dma_addr_t *dma_addr,
  241. uint64_t flags)
  242. {
  243. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  244. unsigned i,t,p;
  245. #endif
  246. int r;
  247. if (!adev->gart.ready) {
  248. WARN(1, "trying to bind memory to uninitialized GART !\n");
  249. return -EINVAL;
  250. }
  251. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  252. t = offset / AMDGPU_GPU_PAGE_SIZE;
  253. p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  254. for (i = 0; i < pages; i++, p++)
  255. adev->gart.pages[p] = pagelist[i];
  256. #endif
  257. if (!adev->gart.ptr)
  258. return 0;
  259. r = amdgpu_gart_map(adev, offset, pages, dma_addr, flags,
  260. adev->gart.ptr);
  261. if (r)
  262. return r;
  263. mb();
  264. amdgpu_gart_flush_gpu_tlb(adev, 0);
  265. return 0;
  266. }
  267. /**
  268. * amdgpu_gart_init - init the driver info for managing the gart
  269. *
  270. * @adev: amdgpu_device pointer
  271. *
  272. * Allocate the dummy page and init the gart driver info (all asics).
  273. * Returns 0 for success, error for failure.
  274. */
  275. int amdgpu_gart_init(struct amdgpu_device *adev)
  276. {
  277. int r;
  278. if (adev->dummy_page.page)
  279. return 0;
  280. /* We need PAGE_SIZE >= AMDGPU_GPU_PAGE_SIZE */
  281. if (PAGE_SIZE < AMDGPU_GPU_PAGE_SIZE) {
  282. DRM_ERROR("Page size is smaller than GPU page size!\n");
  283. return -EINVAL;
  284. }
  285. r = amdgpu_dummy_page_init(adev);
  286. if (r)
  287. return r;
  288. /* Compute table size */
  289. adev->gart.num_cpu_pages = adev->mc.gart_size / PAGE_SIZE;
  290. adev->gart.num_gpu_pages = adev->mc.gart_size / AMDGPU_GPU_PAGE_SIZE;
  291. DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
  292. adev->gart.num_cpu_pages, adev->gart.num_gpu_pages);
  293. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  294. /* Allocate pages table */
  295. adev->gart.pages = vzalloc(sizeof(void *) * adev->gart.num_cpu_pages);
  296. if (adev->gart.pages == NULL)
  297. return -ENOMEM;
  298. #endif
  299. return 0;
  300. }
  301. /**
  302. * amdgpu_gart_fini - tear down the driver info for managing the gart
  303. *
  304. * @adev: amdgpu_device pointer
  305. *
  306. * Tear down the gart driver info and free the dummy page (all asics).
  307. */
  308. void amdgpu_gart_fini(struct amdgpu_device *adev)
  309. {
  310. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  311. vfree(adev->gart.pages);
  312. adev->gart.pages = NULL;
  313. #endif
  314. amdgpu_dummy_page_fini(adev);
  315. }