amdgpu_gart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. if (adev->dummy_page.page)
  69. return 0;
  70. adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  71. if (adev->dummy_page.page == NULL)
  72. return -ENOMEM;
  73. adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
  74. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  75. if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
  76. dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
  77. __free_page(adev->dummy_page.page);
  78. adev->dummy_page.page = NULL;
  79. return -ENOMEM;
  80. }
  81. return 0;
  82. }
  83. /**
  84. * amdgpu_dummy_page_fini - free dummy page used by the driver
  85. *
  86. * @adev: amdgpu_device pointer
  87. *
  88. * Frees the dummy page used by the driver (all asics).
  89. */
  90. static void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
  91. {
  92. if (adev->dummy_page.page == NULL)
  93. return;
  94. pci_unmap_page(adev->pdev, adev->dummy_page.addr,
  95. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  96. __free_page(adev->dummy_page.page);
  97. adev->dummy_page.page = NULL;
  98. }
  99. /**
  100. * amdgpu_gart_table_vram_alloc - allocate vram for gart page table
  101. *
  102. * @adev: amdgpu_device pointer
  103. *
  104. * Allocate video memory for GART page table
  105. * (pcie r4xx, r5xx+). These asics require the
  106. * gart table to be in video memory.
  107. * Returns 0 for success, error for failure.
  108. */
  109. int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
  110. {
  111. int r;
  112. if (adev->gart.robj == NULL) {
  113. r = amdgpu_bo_create(adev, adev->gart.table_size,
  114. PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
  115. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  116. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  117. NULL, NULL, 0, &adev->gart.robj);
  118. if (r) {
  119. return r;
  120. }
  121. }
  122. return 0;
  123. }
  124. /**
  125. * amdgpu_gart_table_vram_pin - pin gart page table in vram
  126. *
  127. * @adev: amdgpu_device pointer
  128. *
  129. * Pin the GART page table in vram so it will not be moved
  130. * by the memory manager (pcie r4xx, r5xx+). These asics require the
  131. * gart table to be in video memory.
  132. * Returns 0 for success, error for failure.
  133. */
  134. int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev)
  135. {
  136. uint64_t gpu_addr;
  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,
  142. AMDGPU_GEM_DOMAIN_VRAM, &gpu_addr);
  143. if (r) {
  144. amdgpu_bo_unreserve(adev->gart.robj);
  145. return r;
  146. }
  147. r = amdgpu_bo_kmap(adev->gart.robj, &adev->gart.ptr);
  148. if (r)
  149. amdgpu_bo_unpin(adev->gart.robj);
  150. amdgpu_bo_unreserve(adev->gart.robj);
  151. adev->gart.table_addr = gpu_addr;
  152. return r;
  153. }
  154. /**
  155. * amdgpu_gart_table_vram_unpin - unpin gart page table in vram
  156. *
  157. * @adev: amdgpu_device pointer
  158. *
  159. * Unpin the GART page table in vram (pcie r4xx, r5xx+).
  160. * These asics require the gart table to be in video memory.
  161. */
  162. void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev)
  163. {
  164. int r;
  165. if (adev->gart.robj == NULL) {
  166. return;
  167. }
  168. r = amdgpu_bo_reserve(adev->gart.robj, true);
  169. if (likely(r == 0)) {
  170. amdgpu_bo_kunmap(adev->gart.robj);
  171. amdgpu_bo_unpin(adev->gart.robj);
  172. amdgpu_bo_unreserve(adev->gart.robj);
  173. adev->gart.ptr = NULL;
  174. }
  175. }
  176. /**
  177. * amdgpu_gart_table_vram_free - free gart page table vram
  178. *
  179. * @adev: amdgpu_device pointer
  180. *
  181. * Free the video memory used for the GART page table
  182. * (pcie r4xx, r5xx+). These asics require the gart table to
  183. * be in video memory.
  184. */
  185. void amdgpu_gart_table_vram_free(struct amdgpu_device *adev)
  186. {
  187. if (adev->gart.robj == NULL) {
  188. return;
  189. }
  190. amdgpu_bo_unref(&adev->gart.robj);
  191. }
  192. /*
  193. * Common gart functions.
  194. */
  195. /**
  196. * amdgpu_gart_unbind - unbind pages from the gart page table
  197. *
  198. * @adev: amdgpu_device pointer
  199. * @offset: offset into the GPU's gart aperture
  200. * @pages: number of pages to unbind
  201. *
  202. * Unbinds the requested pages from the gart page table and
  203. * replaces them with the dummy page (all asics).
  204. * Returns 0 for success, -EINVAL for failure.
  205. */
  206. int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
  207. int pages)
  208. {
  209. unsigned t;
  210. unsigned p;
  211. int i, j;
  212. u64 page_base;
  213. /* Starting from VEGA10, system bit must be 0 to mean invalid. */
  214. uint64_t flags = 0;
  215. if (!adev->gart.ready) {
  216. WARN(1, "trying to unbind memory from uninitialized GART !\n");
  217. return -EINVAL;
  218. }
  219. t = offset / AMDGPU_GPU_PAGE_SIZE;
  220. p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  221. for (i = 0; i < pages; i++, p++) {
  222. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  223. adev->gart.pages[p] = NULL;
  224. #endif
  225. page_base = adev->dummy_page.addr;
  226. if (!adev->gart.ptr)
  227. continue;
  228. for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
  229. amdgpu_gart_set_pte_pde(adev, adev->gart.ptr,
  230. t, page_base, flags);
  231. page_base += AMDGPU_GPU_PAGE_SIZE;
  232. }
  233. }
  234. mb();
  235. amdgpu_gart_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 < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
  263. amdgpu_gart_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 / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  297. for (i = 0; i < pages; i++, p++)
  298. adev->gart.pages[p] = pagelist[i];
  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_gart_flush_gpu_tlb(adev, 0);
  308. return 0;
  309. }
  310. /**
  311. * amdgpu_gart_init - init the driver info for managing the gart
  312. *
  313. * @adev: amdgpu_device pointer
  314. *
  315. * Allocate the dummy page and init the gart driver info (all asics).
  316. * Returns 0 for success, error for failure.
  317. */
  318. int amdgpu_gart_init(struct amdgpu_device *adev)
  319. {
  320. int r;
  321. if (adev->dummy_page.page)
  322. return 0;
  323. /* We need PAGE_SIZE >= AMDGPU_GPU_PAGE_SIZE */
  324. if (PAGE_SIZE < AMDGPU_GPU_PAGE_SIZE) {
  325. DRM_ERROR("Page size is smaller than GPU page size!\n");
  326. return -EINVAL;
  327. }
  328. r = amdgpu_gart_dummy_page_init(adev);
  329. if (r)
  330. return r;
  331. /* Compute table size */
  332. adev->gart.num_cpu_pages = adev->mc.gart_size / PAGE_SIZE;
  333. adev->gart.num_gpu_pages = adev->mc.gart_size / AMDGPU_GPU_PAGE_SIZE;
  334. DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
  335. adev->gart.num_cpu_pages, adev->gart.num_gpu_pages);
  336. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  337. /* Allocate pages table */
  338. adev->gart.pages = vzalloc(sizeof(void *) * adev->gart.num_cpu_pages);
  339. if (adev->gart.pages == NULL)
  340. return -ENOMEM;
  341. #endif
  342. return 0;
  343. }
  344. /**
  345. * amdgpu_gart_fini - tear down the driver info for managing the gart
  346. *
  347. * @adev: amdgpu_device pointer
  348. *
  349. * Tear down the gart driver info and free the dummy page (all asics).
  350. */
  351. void amdgpu_gart_fini(struct amdgpu_device *adev)
  352. {
  353. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  354. vfree(adev->gart.pages);
  355. adev->gart.pages = NULL;
  356. #endif
  357. amdgpu_gart_dummy_page_fini(adev);
  358. }