amdgpu_gart.c 11 KB

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