radeon_gart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 == NULL) {
  73. return -ENOMEM;
  74. }
  75. #ifdef CONFIG_X86
  76. if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
  77. rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
  78. set_memory_uc((unsigned long)ptr,
  79. rdev->gart.table_size >> PAGE_SHIFT);
  80. }
  81. #endif
  82. rdev->gart.ptr = ptr;
  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. dma_free_coherent(&rdev->pdev->dev, rdev->gart.table_size,
  107. (void *)rdev->gart.ptr, rdev->gart.table_addr);
  108. rdev->gart.ptr = NULL;
  109. rdev->gart.table_addr = 0;
  110. }
  111. /**
  112. * radeon_gart_table_vram_alloc - allocate vram for gart page table
  113. *
  114. * @rdev: radeon_device pointer
  115. *
  116. * Allocate video memory for GART page table
  117. * (pcie r4xx, r5xx+). These asics require the
  118. * gart table to be in video memory.
  119. * Returns 0 for success, error for failure.
  120. */
  121. int radeon_gart_table_vram_alloc(struct radeon_device *rdev)
  122. {
  123. int r;
  124. if (rdev->gart.robj == NULL) {
  125. r = radeon_bo_create(rdev, rdev->gart.table_size,
  126. PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM,
  127. 0, NULL, NULL, &rdev->gart.robj);
  128. if (r) {
  129. return r;
  130. }
  131. }
  132. return 0;
  133. }
  134. /**
  135. * radeon_gart_table_vram_pin - pin gart page table in vram
  136. *
  137. * @rdev: radeon_device pointer
  138. *
  139. * Pin the GART page table in vram so it will not be moved
  140. * by the memory manager (pcie r4xx, r5xx+). These asics require the
  141. * gart table to be in video memory.
  142. * Returns 0 for success, error for failure.
  143. */
  144. int radeon_gart_table_vram_pin(struct radeon_device *rdev)
  145. {
  146. uint64_t gpu_addr;
  147. int r;
  148. r = radeon_bo_reserve(rdev->gart.robj, false);
  149. if (unlikely(r != 0))
  150. return r;
  151. r = radeon_bo_pin(rdev->gart.robj,
  152. RADEON_GEM_DOMAIN_VRAM, &gpu_addr);
  153. if (r) {
  154. radeon_bo_unreserve(rdev->gart.robj);
  155. return r;
  156. }
  157. r = radeon_bo_kmap(rdev->gart.robj, &rdev->gart.ptr);
  158. if (r)
  159. radeon_bo_unpin(rdev->gart.robj);
  160. radeon_bo_unreserve(rdev->gart.robj);
  161. rdev->gart.table_addr = gpu_addr;
  162. if (!r) {
  163. int i;
  164. /* We might have dropped some GART table updates while it wasn't
  165. * mapped, restore all entries
  166. */
  167. for (i = 0; i < rdev->gart.num_gpu_pages; i++)
  168. radeon_gart_set_page(rdev, i, rdev->gart.pages_entry[i]);
  169. mb();
  170. radeon_gart_tlb_flush(rdev);
  171. }
  172. return r;
  173. }
  174. /**
  175. * radeon_gart_table_vram_unpin - unpin gart page table in vram
  176. *
  177. * @rdev: radeon_device pointer
  178. *
  179. * Unpin the GART page table in vram (pcie r4xx, r5xx+).
  180. * These asics require the gart table to be in video memory.
  181. */
  182. void radeon_gart_table_vram_unpin(struct radeon_device *rdev)
  183. {
  184. int r;
  185. if (rdev->gart.robj == NULL) {
  186. return;
  187. }
  188. r = radeon_bo_reserve(rdev->gart.robj, false);
  189. if (likely(r == 0)) {
  190. radeon_bo_kunmap(rdev->gart.robj);
  191. radeon_bo_unpin(rdev->gart.robj);
  192. radeon_bo_unreserve(rdev->gart.robj);
  193. rdev->gart.ptr = NULL;
  194. }
  195. }
  196. /**
  197. * radeon_gart_table_vram_free - free gart page table vram
  198. *
  199. * @rdev: radeon_device pointer
  200. *
  201. * Free the video memory used for the GART page table
  202. * (pcie r4xx, r5xx+). These asics require the gart table to
  203. * be in video memory.
  204. */
  205. void radeon_gart_table_vram_free(struct radeon_device *rdev)
  206. {
  207. if (rdev->gart.robj == NULL) {
  208. return;
  209. }
  210. radeon_bo_unref(&rdev->gart.robj);
  211. }
  212. /*
  213. * Common gart functions.
  214. */
  215. /**
  216. * radeon_gart_unbind - unbind pages from the gart page table
  217. *
  218. * @rdev: radeon_device pointer
  219. * @offset: offset into the GPU's gart aperture
  220. * @pages: number of pages to unbind
  221. *
  222. * Unbinds the requested pages from the gart page table and
  223. * replaces them with the dummy page (all asics).
  224. */
  225. void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset,
  226. int pages)
  227. {
  228. unsigned t;
  229. unsigned p;
  230. int i, j;
  231. if (!rdev->gart.ready) {
  232. WARN(1, "trying to unbind memory from uninitialized GART !\n");
  233. return;
  234. }
  235. t = offset / RADEON_GPU_PAGE_SIZE;
  236. p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
  237. for (i = 0; i < pages; i++, p++) {
  238. if (rdev->gart.pages[p]) {
  239. rdev->gart.pages[p] = NULL;
  240. for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
  241. rdev->gart.pages_entry[t] = rdev->dummy_page.entry;
  242. if (rdev->gart.ptr) {
  243. radeon_gart_set_page(rdev, t,
  244. rdev->dummy_page.entry);
  245. }
  246. }
  247. }
  248. }
  249. if (rdev->gart.ptr) {
  250. mb();
  251. radeon_gart_tlb_flush(rdev);
  252. }
  253. }
  254. /**
  255. * radeon_gart_bind - bind pages into the gart page table
  256. *
  257. * @rdev: radeon_device pointer
  258. * @offset: offset into the GPU's gart aperture
  259. * @pages: number of pages to bind
  260. * @pagelist: pages to bind
  261. * @dma_addr: DMA addresses of pages
  262. * @flags: RADEON_GART_PAGE_* flags
  263. *
  264. * Binds the requested pages to the gart page table
  265. * (all asics).
  266. * Returns 0 for success, -EINVAL for failure.
  267. */
  268. int radeon_gart_bind(struct radeon_device *rdev, unsigned offset,
  269. int pages, struct page **pagelist, dma_addr_t *dma_addr,
  270. uint32_t flags)
  271. {
  272. unsigned t;
  273. unsigned p;
  274. uint64_t page_base, page_entry;
  275. int i, j;
  276. if (!rdev->gart.ready) {
  277. WARN(1, "trying to bind memory to uninitialized GART !\n");
  278. return -EINVAL;
  279. }
  280. t = offset / RADEON_GPU_PAGE_SIZE;
  281. p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
  282. for (i = 0; i < pages; i++, p++) {
  283. rdev->gart.pages[p] = pagelist ? pagelist[i] :
  284. rdev->dummy_page.page;
  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(array_size(sizeof(void *),
  330. rdev->gart.num_cpu_pages));
  331. if (rdev->gart.pages == NULL) {
  332. radeon_gart_fini(rdev);
  333. return -ENOMEM;
  334. }
  335. rdev->gart.pages_entry = vmalloc(array_size(sizeof(uint64_t),
  336. rdev->gart.num_gpu_pages));
  337. if (rdev->gart.pages_entry == NULL) {
  338. radeon_gart_fini(rdev);
  339. return -ENOMEM;
  340. }
  341. /* set GART entry to point to the dummy page by default */
  342. for (i = 0; i < rdev->gart.num_gpu_pages; i++)
  343. rdev->gart.pages_entry[i] = rdev->dummy_page.entry;
  344. return 0;
  345. }
  346. /**
  347. * radeon_gart_fini - tear down the driver info for managing the gart
  348. *
  349. * @rdev: radeon_device pointer
  350. *
  351. * Tear down the gart driver info and free the dummy page (all asics).
  352. */
  353. void radeon_gart_fini(struct radeon_device *rdev)
  354. {
  355. if (rdev->gart.ready) {
  356. /* unbind pages */
  357. radeon_gart_unbind(rdev, 0, rdev->gart.num_cpu_pages);
  358. }
  359. rdev->gart.ready = false;
  360. vfree(rdev->gart.pages);
  361. vfree(rdev->gart.pages_entry);
  362. rdev->gart.pages = NULL;
  363. rdev->gart.pages_entry = NULL;
  364. radeon_dummy_page_fini(rdev);
  365. }