radeon_gart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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/radeon_drm.h>
  30. #ifdef CONFIG_X86
  31. #include <asm/set_memory.h>
  32. #endif
  33. #include "radeon.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. * radeon_gart_table_ram_alloc - allocate system ram for gart page table
  58. *
  59. * @rdev: radeon_device pointer
  60. *
  61. * Allocate system memory for GART page table
  62. * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
  63. * gart table to be in system memory.
  64. * Returns 0 for success, -ENOMEM for failure.
  65. */
  66. int radeon_gart_table_ram_alloc(struct radeon_device *rdev)
  67. {
  68. void *ptr;
  69. ptr = pci_alloc_consistent(rdev->pdev, rdev->gart.table_size,
  70. &rdev->gart.table_addr);
  71. if (ptr == NULL) {
  72. return -ENOMEM;
  73. }
  74. #ifdef CONFIG_X86
  75. if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
  76. rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
  77. set_memory_uc((unsigned long)ptr,
  78. rdev->gart.table_size >> PAGE_SHIFT);
  79. }
  80. #endif
  81. rdev->gart.ptr = ptr;
  82. memset((void *)rdev->gart.ptr, 0, rdev->gart.table_size);
  83. return 0;
  84. }
  85. /**
  86. * radeon_gart_table_ram_free - free system ram for gart page table
  87. *
  88. * @rdev: radeon_device pointer
  89. *
  90. * Free system memory for GART page table
  91. * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
  92. * gart table to be in system memory.
  93. */
  94. void radeon_gart_table_ram_free(struct radeon_device *rdev)
  95. {
  96. if (rdev->gart.ptr == NULL) {
  97. return;
  98. }
  99. #ifdef CONFIG_X86
  100. if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
  101. rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
  102. set_memory_wb((unsigned long)rdev->gart.ptr,
  103. rdev->gart.table_size >> PAGE_SHIFT);
  104. }
  105. #endif
  106. pci_free_consistent(rdev->pdev, rdev->gart.table_size,
  107. (void *)rdev->gart.ptr,
  108. rdev->gart.table_addr);
  109. rdev->gart.ptr = NULL;
  110. rdev->gart.table_addr = 0;
  111. }
  112. /**
  113. * radeon_gart_table_vram_alloc - allocate vram for gart page table
  114. *
  115. * @rdev: radeon_device pointer
  116. *
  117. * Allocate video memory for GART page table
  118. * (pcie r4xx, r5xx+). These asics require the
  119. * gart table to be in video memory.
  120. * Returns 0 for success, error for failure.
  121. */
  122. int radeon_gart_table_vram_alloc(struct radeon_device *rdev)
  123. {
  124. int r;
  125. if (rdev->gart.robj == NULL) {
  126. r = radeon_bo_create(rdev, rdev->gart.table_size,
  127. PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM,
  128. 0, NULL, NULL, &rdev->gart.robj);
  129. if (r) {
  130. return r;
  131. }
  132. }
  133. return 0;
  134. }
  135. /**
  136. * radeon_gart_table_vram_pin - pin gart page table in vram
  137. *
  138. * @rdev: radeon_device pointer
  139. *
  140. * Pin the GART page table in vram so it will not be moved
  141. * by the memory manager (pcie r4xx, r5xx+). These asics require the
  142. * gart table to be in video memory.
  143. * Returns 0 for success, error for failure.
  144. */
  145. int radeon_gart_table_vram_pin(struct radeon_device *rdev)
  146. {
  147. uint64_t gpu_addr;
  148. int r;
  149. r = radeon_bo_reserve(rdev->gart.robj, false);
  150. if (unlikely(r != 0))
  151. return r;
  152. r = radeon_bo_pin(rdev->gart.robj,
  153. RADEON_GEM_DOMAIN_VRAM, &gpu_addr);
  154. if (r) {
  155. radeon_bo_unreserve(rdev->gart.robj);
  156. return r;
  157. }
  158. r = radeon_bo_kmap(rdev->gart.robj, &rdev->gart.ptr);
  159. if (r)
  160. radeon_bo_unpin(rdev->gart.robj);
  161. radeon_bo_unreserve(rdev->gart.robj);
  162. rdev->gart.table_addr = gpu_addr;
  163. if (!r) {
  164. int i;
  165. /* We might have dropped some GART table updates while it wasn't
  166. * mapped, restore all entries
  167. */
  168. for (i = 0; i < rdev->gart.num_gpu_pages; i++)
  169. radeon_gart_set_page(rdev, i, rdev->gart.pages_entry[i]);
  170. mb();
  171. radeon_gart_tlb_flush(rdev);
  172. }
  173. return r;
  174. }
  175. /**
  176. * radeon_gart_table_vram_unpin - unpin gart page table in vram
  177. *
  178. * @rdev: radeon_device pointer
  179. *
  180. * Unpin the GART page table in vram (pcie r4xx, r5xx+).
  181. * These asics require the gart table to be in video memory.
  182. */
  183. void radeon_gart_table_vram_unpin(struct radeon_device *rdev)
  184. {
  185. int r;
  186. if (rdev->gart.robj == NULL) {
  187. return;
  188. }
  189. r = radeon_bo_reserve(rdev->gart.robj, false);
  190. if (likely(r == 0)) {
  191. radeon_bo_kunmap(rdev->gart.robj);
  192. radeon_bo_unpin(rdev->gart.robj);
  193. radeon_bo_unreserve(rdev->gart.robj);
  194. rdev->gart.ptr = NULL;
  195. }
  196. }
  197. /**
  198. * radeon_gart_table_vram_free - free gart page table vram
  199. *
  200. * @rdev: radeon_device pointer
  201. *
  202. * Free the video memory used for the GART page table
  203. * (pcie r4xx, r5xx+). These asics require the gart table to
  204. * be in video memory.
  205. */
  206. void radeon_gart_table_vram_free(struct radeon_device *rdev)
  207. {
  208. if (rdev->gart.robj == NULL) {
  209. return;
  210. }
  211. radeon_bo_unref(&rdev->gart.robj);
  212. }
  213. /*
  214. * Common gart functions.
  215. */
  216. /**
  217. * radeon_gart_unbind - unbind pages from the gart page table
  218. *
  219. * @rdev: radeon_device pointer
  220. * @offset: offset into the GPU's gart aperture
  221. * @pages: number of pages to unbind
  222. *
  223. * Unbinds the requested pages from the gart page table and
  224. * replaces them with the dummy page (all asics).
  225. */
  226. void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset,
  227. int pages)
  228. {
  229. unsigned t;
  230. unsigned p;
  231. int i, j;
  232. if (!rdev->gart.ready) {
  233. WARN(1, "trying to unbind memory from uninitialized GART !\n");
  234. return;
  235. }
  236. t = offset / RADEON_GPU_PAGE_SIZE;
  237. p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
  238. for (i = 0; i < pages; i++, p++) {
  239. if (rdev->gart.pages[p]) {
  240. rdev->gart.pages[p] = NULL;
  241. for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
  242. rdev->gart.pages_entry[t] = rdev->dummy_page.entry;
  243. if (rdev->gart.ptr) {
  244. radeon_gart_set_page(rdev, t,
  245. rdev->dummy_page.entry);
  246. }
  247. }
  248. }
  249. }
  250. if (rdev->gart.ptr) {
  251. mb();
  252. radeon_gart_tlb_flush(rdev);
  253. }
  254. }
  255. /**
  256. * radeon_gart_bind - bind pages into the gart page table
  257. *
  258. * @rdev: radeon_device pointer
  259. * @offset: offset into the GPU's gart aperture
  260. * @pages: number of pages to bind
  261. * @pagelist: pages to bind
  262. * @dma_addr: DMA addresses of pages
  263. * @flags: RADEON_GART_PAGE_* flags
  264. *
  265. * Binds the requested pages to the gart page table
  266. * (all asics).
  267. * Returns 0 for success, -EINVAL for failure.
  268. */
  269. int radeon_gart_bind(struct radeon_device *rdev, unsigned offset,
  270. int pages, struct page **pagelist, dma_addr_t *dma_addr,
  271. uint32_t flags)
  272. {
  273. unsigned t;
  274. unsigned p;
  275. uint64_t page_base, page_entry;
  276. int i, j;
  277. if (!rdev->gart.ready) {
  278. WARN(1, "trying to bind memory to uninitialized GART !\n");
  279. return -EINVAL;
  280. }
  281. t = offset / RADEON_GPU_PAGE_SIZE;
  282. p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
  283. for (i = 0; i < pages; i++, p++) {
  284. rdev->gart.pages[p] = pagelist[i];
  285. page_base = dma_addr[i];
  286. for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
  287. page_entry = radeon_gart_get_page_entry(page_base, flags);
  288. rdev->gart.pages_entry[t] = page_entry;
  289. if (rdev->gart.ptr) {
  290. radeon_gart_set_page(rdev, t, page_entry);
  291. }
  292. page_base += RADEON_GPU_PAGE_SIZE;
  293. }
  294. }
  295. if (rdev->gart.ptr) {
  296. mb();
  297. radeon_gart_tlb_flush(rdev);
  298. }
  299. return 0;
  300. }
  301. /**
  302. * radeon_gart_init - init the driver info for managing the gart
  303. *
  304. * @rdev: radeon_device pointer
  305. *
  306. * Allocate the dummy page and init the gart driver info (all asics).
  307. * Returns 0 for success, error for failure.
  308. */
  309. int radeon_gart_init(struct radeon_device *rdev)
  310. {
  311. int r, i;
  312. if (rdev->gart.pages) {
  313. return 0;
  314. }
  315. /* We need PAGE_SIZE >= RADEON_GPU_PAGE_SIZE */
  316. if (PAGE_SIZE < RADEON_GPU_PAGE_SIZE) {
  317. DRM_ERROR("Page size is smaller than GPU page size!\n");
  318. return -EINVAL;
  319. }
  320. r = radeon_dummy_page_init(rdev);
  321. if (r)
  322. return r;
  323. /* Compute table size */
  324. rdev->gart.num_cpu_pages = rdev->mc.gtt_size / PAGE_SIZE;
  325. rdev->gart.num_gpu_pages = rdev->mc.gtt_size / RADEON_GPU_PAGE_SIZE;
  326. DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
  327. rdev->gart.num_cpu_pages, rdev->gart.num_gpu_pages);
  328. /* Allocate pages table */
  329. rdev->gart.pages = vzalloc(sizeof(void *) * rdev->gart.num_cpu_pages);
  330. if (rdev->gart.pages == NULL) {
  331. radeon_gart_fini(rdev);
  332. return -ENOMEM;
  333. }
  334. rdev->gart.pages_entry = vmalloc(array_size(sizeof(uint64_t),
  335. rdev->gart.num_gpu_pages));
  336. if (rdev->gart.pages_entry == NULL) {
  337. radeon_gart_fini(rdev);
  338. return -ENOMEM;
  339. }
  340. /* set GART entry to point to the dummy page by default */
  341. for (i = 0; i < rdev->gart.num_gpu_pages; i++)
  342. rdev->gart.pages_entry[i] = rdev->dummy_page.entry;
  343. return 0;
  344. }
  345. /**
  346. * radeon_gart_fini - tear down the driver info for managing the gart
  347. *
  348. * @rdev: radeon_device pointer
  349. *
  350. * Tear down the gart driver info and free the dummy page (all asics).
  351. */
  352. void radeon_gart_fini(struct radeon_device *rdev)
  353. {
  354. if (rdev->gart.ready) {
  355. /* unbind pages */
  356. radeon_gart_unbind(rdev, 0, rdev->gart.num_cpu_pages);
  357. }
  358. rdev->gart.ready = false;
  359. vfree(rdev->gart.pages);
  360. vfree(rdev->gart.pages_entry);
  361. rdev->gart.pages = NULL;
  362. rdev->gart.pages_entry = NULL;
  363. radeon_dummy_page_fini(rdev);
  364. }