amdgpu_gart.c 11 KB

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