amdgpu_gart.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. #include "amdgpu.h"
  31. /*
  32. * GART
  33. * The GART (Graphics Aperture Remapping Table) is an aperture
  34. * in the GPU's address space. System pages can be mapped into
  35. * the aperture and look like contiguous pages from the GPU's
  36. * perspective. A page table maps the pages in the aperture
  37. * to the actual backing pages in system memory.
  38. *
  39. * Radeon GPUs support both an internal GART, as described above,
  40. * and AGP. AGP works similarly, but the GART table is configured
  41. * and maintained by the northbridge rather than the driver.
  42. * Radeon hw has a separate AGP aperture that is programmed to
  43. * point to the AGP aperture provided by the northbridge and the
  44. * requests are passed through to the northbridge aperture.
  45. * Both AGP and internal GART can be used at the same time, however
  46. * that is not currently supported by the driver.
  47. *
  48. * This file handles the common internal GART management.
  49. */
  50. /*
  51. * Common GART table functions.
  52. */
  53. /**
  54. * amdgpu_gart_table_ram_alloc - allocate system ram for gart page table
  55. *
  56. * @adev: amdgpu_device pointer
  57. *
  58. * Allocate system memory for GART page table
  59. * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
  60. * gart table to be in system memory.
  61. * Returns 0 for success, -ENOMEM for failure.
  62. */
  63. int amdgpu_gart_table_ram_alloc(struct amdgpu_device *adev)
  64. {
  65. void *ptr;
  66. ptr = pci_alloc_consistent(adev->pdev, adev->gart.table_size,
  67. &adev->gart.table_addr);
  68. if (ptr == NULL) {
  69. return -ENOMEM;
  70. }
  71. #ifdef CONFIG_X86
  72. if (0) {
  73. set_memory_uc((unsigned long)ptr,
  74. adev->gart.table_size >> PAGE_SHIFT);
  75. }
  76. #endif
  77. adev->gart.ptr = ptr;
  78. memset((void *)adev->gart.ptr, 0, adev->gart.table_size);
  79. return 0;
  80. }
  81. /**
  82. * amdgpu_gart_table_ram_free - free system ram for gart page table
  83. *
  84. * @adev: amdgpu_device pointer
  85. *
  86. * Free system memory for GART page table
  87. * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
  88. * gart table to be in system memory.
  89. */
  90. void amdgpu_gart_table_ram_free(struct amdgpu_device *adev)
  91. {
  92. if (adev->gart.ptr == NULL) {
  93. return;
  94. }
  95. #ifdef CONFIG_X86
  96. if (0) {
  97. set_memory_wb((unsigned long)adev->gart.ptr,
  98. adev->gart.table_size >> PAGE_SHIFT);
  99. }
  100. #endif
  101. pci_free_consistent(adev->pdev, adev->gart.table_size,
  102. (void *)adev->gart.ptr,
  103. adev->gart.table_addr);
  104. adev->gart.ptr = NULL;
  105. adev->gart.table_addr = 0;
  106. }
  107. /**
  108. * amdgpu_gart_table_vram_alloc - allocate vram for gart page table
  109. *
  110. * @adev: amdgpu_device pointer
  111. *
  112. * Allocate video memory for GART page table
  113. * (pcie r4xx, r5xx+). These asics require the
  114. * gart table to be in video memory.
  115. * Returns 0 for success, error for failure.
  116. */
  117. int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
  118. {
  119. int r;
  120. if (adev->gart.robj == NULL) {
  121. r = amdgpu_bo_create(adev, adev->gart.table_size,
  122. PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
  123. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  124. NULL, NULL, &adev->gart.robj);
  125. if (r) {
  126. return r;
  127. }
  128. }
  129. return 0;
  130. }
  131. /**
  132. * amdgpu_gart_table_vram_pin - pin gart page table in vram
  133. *
  134. * @adev: amdgpu_device pointer
  135. *
  136. * Pin the GART page table in vram so it will not be moved
  137. * by the memory manager (pcie r4xx, r5xx+). These asics require the
  138. * gart table to be in video memory.
  139. * Returns 0 for success, error for failure.
  140. */
  141. int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev)
  142. {
  143. uint64_t gpu_addr;
  144. int r;
  145. r = amdgpu_bo_reserve(adev->gart.robj, false);
  146. if (unlikely(r != 0))
  147. return r;
  148. r = amdgpu_bo_pin(adev->gart.robj,
  149. AMDGPU_GEM_DOMAIN_VRAM, &gpu_addr);
  150. if (r) {
  151. amdgpu_bo_unreserve(adev->gart.robj);
  152. return r;
  153. }
  154. r = amdgpu_bo_kmap(adev->gart.robj, &adev->gart.ptr);
  155. if (r)
  156. amdgpu_bo_unpin(adev->gart.robj);
  157. amdgpu_bo_unreserve(adev->gart.robj);
  158. adev->gart.table_addr = gpu_addr;
  159. return r;
  160. }
  161. /**
  162. * amdgpu_gart_table_vram_unpin - unpin gart page table in vram
  163. *
  164. * @adev: amdgpu_device pointer
  165. *
  166. * Unpin the GART page table in vram (pcie r4xx, r5xx+).
  167. * These asics require the gart table to be in video memory.
  168. */
  169. void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev)
  170. {
  171. int r;
  172. if (adev->gart.robj == NULL) {
  173. return;
  174. }
  175. r = amdgpu_bo_reserve(adev->gart.robj, false);
  176. if (likely(r == 0)) {
  177. amdgpu_bo_kunmap(adev->gart.robj);
  178. amdgpu_bo_unpin(adev->gart.robj);
  179. amdgpu_bo_unreserve(adev->gart.robj);
  180. adev->gart.ptr = NULL;
  181. }
  182. }
  183. /**
  184. * amdgpu_gart_table_vram_free - free gart page table vram
  185. *
  186. * @adev: amdgpu_device pointer
  187. *
  188. * Free the video memory used for the GART page table
  189. * (pcie r4xx, r5xx+). These asics require the gart table to
  190. * be in video memory.
  191. */
  192. void amdgpu_gart_table_vram_free(struct amdgpu_device *adev)
  193. {
  194. if (adev->gart.robj == NULL) {
  195. return;
  196. }
  197. amdgpu_bo_unref(&adev->gart.robj);
  198. }
  199. /*
  200. * Common gart functions.
  201. */
  202. /**
  203. * amdgpu_gart_unbind - unbind pages from the gart page table
  204. *
  205. * @adev: amdgpu_device pointer
  206. * @offset: offset into the GPU's gart aperture
  207. * @pages: number of pages to unbind
  208. *
  209. * Unbinds the requested pages from the gart page table and
  210. * replaces them with the dummy page (all asics).
  211. */
  212. void amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
  213. int pages)
  214. {
  215. unsigned t;
  216. unsigned p;
  217. int i, j;
  218. u64 page_base;
  219. uint32_t flags = AMDGPU_PTE_SYSTEM;
  220. if (!adev->gart.ready) {
  221. WARN(1, "trying to unbind memory from uninitialized GART !\n");
  222. return;
  223. }
  224. t = offset / AMDGPU_GPU_PAGE_SIZE;
  225. p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  226. for (i = 0; i < pages; i++, p++) {
  227. if (adev->gart.pages[p]) {
  228. adev->gart.pages[p] = NULL;
  229. adev->gart.pages_addr[p] = adev->dummy_page.addr;
  230. page_base = adev->gart.pages_addr[p];
  231. if (!adev->gart.ptr)
  232. continue;
  233. for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
  234. amdgpu_gart_set_pte_pde(adev, adev->gart.ptr,
  235. t, page_base, flags);
  236. page_base += AMDGPU_GPU_PAGE_SIZE;
  237. }
  238. }
  239. }
  240. mb();
  241. amdgpu_gart_flush_gpu_tlb(adev, 0);
  242. }
  243. /**
  244. * amdgpu_gart_bind - bind pages into the gart page table
  245. *
  246. * @adev: amdgpu_device pointer
  247. * @offset: offset into the GPU's gart aperture
  248. * @pages: number of pages to bind
  249. * @pagelist: pages to bind
  250. * @dma_addr: DMA addresses of pages
  251. *
  252. * Binds the requested pages to the gart page table
  253. * (all asics).
  254. * Returns 0 for success, -EINVAL for failure.
  255. */
  256. int amdgpu_gart_bind(struct amdgpu_device *adev, uint64_t offset,
  257. int pages, struct page **pagelist, dma_addr_t *dma_addr,
  258. uint32_t flags)
  259. {
  260. unsigned t;
  261. unsigned p;
  262. uint64_t page_base;
  263. int i, j;
  264. if (!adev->gart.ready) {
  265. WARN(1, "trying to bind memory to uninitialized GART !\n");
  266. return -EINVAL;
  267. }
  268. t = offset / AMDGPU_GPU_PAGE_SIZE;
  269. p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  270. for (i = 0; i < pages; i++, p++) {
  271. adev->gart.pages_addr[p] = dma_addr[i];
  272. adev->gart.pages[p] = pagelist[i];
  273. if (adev->gart.ptr) {
  274. page_base = adev->gart.pages_addr[p];
  275. for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
  276. amdgpu_gart_set_pte_pde(adev, adev->gart.ptr, t, page_base, flags);
  277. page_base += AMDGPU_GPU_PAGE_SIZE;
  278. }
  279. }
  280. }
  281. mb();
  282. amdgpu_gart_flush_gpu_tlb(adev, 0);
  283. return 0;
  284. }
  285. /**
  286. * amdgpu_gart_init - init the driver info for managing the gart
  287. *
  288. * @adev: amdgpu_device pointer
  289. *
  290. * Allocate the dummy page and init the gart driver info (all asics).
  291. * Returns 0 for success, error for failure.
  292. */
  293. int amdgpu_gart_init(struct amdgpu_device *adev)
  294. {
  295. int r, i;
  296. if (adev->gart.pages) {
  297. return 0;
  298. }
  299. /* We need PAGE_SIZE >= AMDGPU_GPU_PAGE_SIZE */
  300. if (PAGE_SIZE < AMDGPU_GPU_PAGE_SIZE) {
  301. DRM_ERROR("Page size is smaller than GPU page size!\n");
  302. return -EINVAL;
  303. }
  304. r = amdgpu_dummy_page_init(adev);
  305. if (r)
  306. return r;
  307. /* Compute table size */
  308. adev->gart.num_cpu_pages = adev->mc.gtt_size / PAGE_SIZE;
  309. adev->gart.num_gpu_pages = adev->mc.gtt_size / AMDGPU_GPU_PAGE_SIZE;
  310. DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
  311. adev->gart.num_cpu_pages, adev->gart.num_gpu_pages);
  312. /* Allocate pages table */
  313. adev->gart.pages = vzalloc(sizeof(void *) * adev->gart.num_cpu_pages);
  314. if (adev->gart.pages == NULL) {
  315. amdgpu_gart_fini(adev);
  316. return -ENOMEM;
  317. }
  318. adev->gart.pages_addr = vzalloc(sizeof(dma_addr_t) *
  319. adev->gart.num_cpu_pages);
  320. if (adev->gart.pages_addr == NULL) {
  321. amdgpu_gart_fini(adev);
  322. return -ENOMEM;
  323. }
  324. /* set GART entry to point to the dummy page by default */
  325. for (i = 0; i < adev->gart.num_cpu_pages; i++) {
  326. adev->gart.pages_addr[i] = adev->dummy_page.addr;
  327. }
  328. return 0;
  329. }
  330. /**
  331. * amdgpu_gart_fini - tear down the driver info for managing the gart
  332. *
  333. * @adev: amdgpu_device pointer
  334. *
  335. * Tear down the gart driver info and free the dummy page (all asics).
  336. */
  337. void amdgpu_gart_fini(struct amdgpu_device *adev)
  338. {
  339. if (adev->gart.pages && adev->gart.pages_addr && adev->gart.ready) {
  340. /* unbind pages */
  341. amdgpu_gart_unbind(adev, 0, adev->gart.num_cpu_pages);
  342. }
  343. adev->gart.ready = false;
  344. vfree(adev->gart.pages);
  345. vfree(adev->gart.pages_addr);
  346. adev->gart.pages = NULL;
  347. adev->gart.pages_addr = NULL;
  348. amdgpu_dummy_page_fini(adev);
  349. }