amdgpu_gart.c 11 KB

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