amdgpu_gart.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #include "amdgpu.h"
  31. /*
  32. * GART
  33. * The GART (Graphics Aperture Remapping Table) is an aperture
  34. * in the GPU's address space. System pages can be mapped into
  35. * the aperture and look like contiguous pages from the GPU's
  36. * perspective. A page table maps the pages in the aperture
  37. * to the actual backing pages in system memory.
  38. *
  39. * Radeon GPUs support both an internal GART, as described above,
  40. * and AGP. AGP works similarly, but the GART table is configured
  41. * and maintained by the northbridge rather than the driver.
  42. * Radeon hw has a separate AGP aperture that is programmed to
  43. * point to the AGP aperture provided by the northbridge and the
  44. * requests are passed through to the northbridge aperture.
  45. * Both AGP and internal GART can be used at the same time, however
  46. * that is not currently supported by the driver.
  47. *
  48. * This file handles the common internal GART management.
  49. */
  50. /*
  51. * Common GART table functions.
  52. */
  53. /**
  54. * amdgpu_gart_table_ram_alloc - allocate system ram for gart page table
  55. *
  56. * @adev: amdgpu_device pointer
  57. *
  58. * Allocate system memory for GART page table
  59. * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
  60. * gart table to be in system memory.
  61. * Returns 0 for success, -ENOMEM for failure.
  62. */
  63. int amdgpu_gart_table_ram_alloc(struct amdgpu_device *adev)
  64. {
  65. void *ptr;
  66. ptr = pci_alloc_consistent(adev->pdev, adev->gart.table_size,
  67. &adev->gart.table_addr);
  68. if (ptr == NULL) {
  69. return -ENOMEM;
  70. }
  71. #ifdef CONFIG_X86
  72. if (0) {
  73. set_memory_uc((unsigned long)ptr,
  74. adev->gart.table_size >> PAGE_SHIFT);
  75. }
  76. #endif
  77. adev->gart.ptr = ptr;
  78. memset((void *)adev->gart.ptr, 0, adev->gart.table_size);
  79. return 0;
  80. }
  81. /**
  82. * amdgpu_gart_table_ram_free - free system ram for gart page table
  83. *
  84. * @adev: amdgpu_device pointer
  85. *
  86. * Free system memory for GART page table
  87. * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
  88. * gart table to be in system memory.
  89. */
  90. void amdgpu_gart_table_ram_free(struct amdgpu_device *adev)
  91. {
  92. if (adev->gart.ptr == NULL) {
  93. return;
  94. }
  95. #ifdef CONFIG_X86
  96. if (0) {
  97. set_memory_wb((unsigned long)adev->gart.ptr,
  98. adev->gart.table_size >> PAGE_SHIFT);
  99. }
  100. #endif
  101. pci_free_consistent(adev->pdev, adev->gart.table_size,
  102. (void *)adev->gart.ptr,
  103. adev->gart.table_addr);
  104. adev->gart.ptr = NULL;
  105. adev->gart.table_addr = 0;
  106. }
  107. /**
  108. * amdgpu_gart_table_vram_alloc - allocate vram for gart page table
  109. *
  110. * @adev: amdgpu_device pointer
  111. *
  112. * Allocate video memory for GART page table
  113. * (pcie r4xx, r5xx+). These asics require the
  114. * gart table to be in video memory.
  115. * Returns 0 for success, error for failure.
  116. */
  117. int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
  118. {
  119. int r;
  120. if (adev->gart.robj == NULL) {
  121. r = amdgpu_bo_create(adev, adev->gart.table_size,
  122. PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
  123. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  124. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  125. NULL, NULL, &adev->gart.robj);
  126. if (r) {
  127. return r;
  128. }
  129. }
  130. return 0;
  131. }
  132. /**
  133. * amdgpu_gart_table_vram_pin - pin gart page table in vram
  134. *
  135. * @adev: amdgpu_device pointer
  136. *
  137. * Pin the GART page table in vram so it will not be moved
  138. * by the memory manager (pcie r4xx, r5xx+). These asics require the
  139. * gart table to be in video memory.
  140. * Returns 0 for success, error for failure.
  141. */
  142. int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev)
  143. {
  144. uint64_t gpu_addr;
  145. int r;
  146. r = amdgpu_bo_reserve(adev->gart.robj, false);
  147. if (unlikely(r != 0))
  148. return r;
  149. r = amdgpu_bo_pin(adev->gart.robj,
  150. AMDGPU_GEM_DOMAIN_VRAM, &gpu_addr);
  151. if (r) {
  152. amdgpu_bo_unreserve(adev->gart.robj);
  153. return r;
  154. }
  155. r = amdgpu_bo_kmap(adev->gart.robj, &adev->gart.ptr);
  156. if (r)
  157. amdgpu_bo_unpin(adev->gart.robj);
  158. amdgpu_bo_unreserve(adev->gart.robj);
  159. adev->gart.table_addr = gpu_addr;
  160. return r;
  161. }
  162. /**
  163. * amdgpu_gart_table_vram_unpin - unpin gart page table in vram
  164. *
  165. * @adev: amdgpu_device pointer
  166. *
  167. * Unpin the GART page table in vram (pcie r4xx, r5xx+).
  168. * These asics require the gart table to be in video memory.
  169. */
  170. void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev)
  171. {
  172. int r;
  173. if (adev->gart.robj == NULL) {
  174. return;
  175. }
  176. r = amdgpu_bo_reserve(adev->gart.robj, false);
  177. if (likely(r == 0)) {
  178. amdgpu_bo_kunmap(adev->gart.robj);
  179. amdgpu_bo_unpin(adev->gart.robj);
  180. amdgpu_bo_unreserve(adev->gart.robj);
  181. adev->gart.ptr = NULL;
  182. }
  183. }
  184. /**
  185. * amdgpu_gart_table_vram_free - free gart page table vram
  186. *
  187. * @adev: amdgpu_device pointer
  188. *
  189. * Free the video memory used for the GART page table
  190. * (pcie r4xx, r5xx+). These asics require the gart table to
  191. * be in video memory.
  192. */
  193. void amdgpu_gart_table_vram_free(struct amdgpu_device *adev)
  194. {
  195. if (adev->gart.robj == NULL) {
  196. return;
  197. }
  198. amdgpu_bo_unref(&adev->gart.robj);
  199. }
  200. /*
  201. * Common gart functions.
  202. */
  203. /**
  204. * amdgpu_gart_unbind - unbind pages from the gart page table
  205. *
  206. * @adev: amdgpu_device pointer
  207. * @offset: offset into the GPU's gart aperture
  208. * @pages: number of pages to unbind
  209. *
  210. * Unbinds the requested pages from the gart page table and
  211. * replaces them with the dummy page (all asics).
  212. */
  213. void amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
  214. int pages)
  215. {
  216. unsigned t;
  217. unsigned p;
  218. int i, j;
  219. u64 page_base;
  220. /* Starting from VEGA10, system bit must be 0 to mean invalid. */
  221. uint64_t flags = 0;
  222. if (!adev->gart.ready) {
  223. WARN(1, "trying to unbind memory from uninitialized GART !\n");
  224. return;
  225. }
  226. t = offset / AMDGPU_GPU_PAGE_SIZE;
  227. p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  228. for (i = 0; i < pages; i++, p++) {
  229. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  230. adev->gart.pages[p] = NULL;
  231. #endif
  232. page_base = adev->dummy_page.addr;
  233. if (!adev->gart.ptr)
  234. continue;
  235. for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
  236. amdgpu_gart_set_pte_pde(adev, adev->gart.ptr,
  237. t, page_base, flags);
  238. page_base += AMDGPU_GPU_PAGE_SIZE;
  239. }
  240. }
  241. mb();
  242. amdgpu_gart_flush_gpu_tlb(adev, 0);
  243. }
  244. /**
  245. * amdgpu_gart_bind - bind pages into the gart page table
  246. *
  247. * @adev: amdgpu_device pointer
  248. * @offset: offset into the GPU's gart aperture
  249. * @pages: number of pages to bind
  250. * @pagelist: pages to bind
  251. * @dma_addr: DMA addresses of pages
  252. *
  253. * Binds the requested pages to the gart page table
  254. * (all asics).
  255. * Returns 0 for success, -EINVAL for failure.
  256. */
  257. int amdgpu_gart_bind(struct amdgpu_device *adev, uint64_t offset,
  258. int pages, struct page **pagelist, dma_addr_t *dma_addr,
  259. uint64_t flags)
  260. {
  261. unsigned t;
  262. unsigned p;
  263. uint64_t page_base;
  264. int i, j;
  265. if (!adev->gart.ready) {
  266. WARN(1, "trying to bind memory to uninitialized GART !\n");
  267. return -EINVAL;
  268. }
  269. t = offset / AMDGPU_GPU_PAGE_SIZE;
  270. p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  271. for (i = 0; i < pages; i++, p++) {
  272. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  273. adev->gart.pages[p] = pagelist[i];
  274. #endif
  275. if (adev->gart.ptr) {
  276. page_base = dma_addr[i];
  277. for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
  278. amdgpu_gart_set_pte_pde(adev, adev->gart.ptr, t, page_base, flags);
  279. page_base += AMDGPU_GPU_PAGE_SIZE;
  280. }
  281. }
  282. }
  283. mb();
  284. amdgpu_gart_flush_gpu_tlb(adev, 0);
  285. return 0;
  286. }
  287. /**
  288. * amdgpu_gart_init - init the driver info for managing the gart
  289. *
  290. * @adev: amdgpu_device pointer
  291. *
  292. * Allocate the dummy page and init the gart driver info (all asics).
  293. * Returns 0 for success, error for failure.
  294. */
  295. int amdgpu_gart_init(struct amdgpu_device *adev)
  296. {
  297. int r;
  298. if (adev->dummy_page.page)
  299. return 0;
  300. /* We need PAGE_SIZE >= AMDGPU_GPU_PAGE_SIZE */
  301. if (PAGE_SIZE < AMDGPU_GPU_PAGE_SIZE) {
  302. DRM_ERROR("Page size is smaller than GPU page size!\n");
  303. return -EINVAL;
  304. }
  305. r = amdgpu_dummy_page_init(adev);
  306. if (r)
  307. return r;
  308. /* Compute table size */
  309. adev->gart.num_cpu_pages = adev->mc.gtt_size / PAGE_SIZE;
  310. adev->gart.num_gpu_pages = adev->mc.gtt_size / AMDGPU_GPU_PAGE_SIZE;
  311. DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
  312. adev->gart.num_cpu_pages, adev->gart.num_gpu_pages);
  313. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  314. /* Allocate pages table */
  315. adev->gart.pages = vzalloc(sizeof(void *) * adev->gart.num_cpu_pages);
  316. if (adev->gart.pages == NULL) {
  317. amdgpu_gart_fini(adev);
  318. return -ENOMEM;
  319. }
  320. #endif
  321. return 0;
  322. }
  323. /**
  324. * amdgpu_gart_fini - tear down the driver info for managing the gart
  325. *
  326. * @adev: amdgpu_device pointer
  327. *
  328. * Tear down the gart driver info and free the dummy page (all asics).
  329. */
  330. void amdgpu_gart_fini(struct amdgpu_device *adev)
  331. {
  332. if (adev->gart.ready) {
  333. /* unbind pages */
  334. amdgpu_gart_unbind(adev, 0, adev->gart.num_cpu_pages);
  335. }
  336. adev->gart.ready = false;
  337. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  338. vfree(adev->gart.pages);
  339. adev->gart.pages = NULL;
  340. #endif
  341. amdgpu_dummy_page_fini(adev);
  342. }