amdgpu_gart.c 12 KB

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