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