amdgpu_gart.c 11 KB

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