amdgpu_gart.c 11 KB

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