radeon_gart.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "drmP.h"
  29. #include "radeon_drm.h"
  30. #include "radeon.h"
  31. #include "radeon_reg.h"
  32. /*
  33. * Common GART table functions.
  34. */
  35. int radeon_gart_table_ram_alloc(struct radeon_device *rdev)
  36. {
  37. void *ptr;
  38. ptr = pci_alloc_consistent(rdev->pdev, rdev->gart.table_size,
  39. &rdev->gart.table_addr);
  40. if (ptr == NULL) {
  41. return -ENOMEM;
  42. }
  43. #ifdef CONFIG_X86
  44. if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
  45. rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
  46. set_memory_uc((unsigned long)ptr,
  47. rdev->gart.table_size >> PAGE_SHIFT);
  48. }
  49. #endif
  50. rdev->gart.table.ram.ptr = ptr;
  51. memset((void *)rdev->gart.table.ram.ptr, 0, rdev->gart.table_size);
  52. return 0;
  53. }
  54. void radeon_gart_table_ram_free(struct radeon_device *rdev)
  55. {
  56. if (rdev->gart.table.ram.ptr == NULL) {
  57. return;
  58. }
  59. #ifdef CONFIG_X86
  60. if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
  61. rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
  62. set_memory_wb((unsigned long)rdev->gart.table.ram.ptr,
  63. rdev->gart.table_size >> PAGE_SHIFT);
  64. }
  65. #endif
  66. pci_free_consistent(rdev->pdev, rdev->gart.table_size,
  67. (void *)rdev->gart.table.ram.ptr,
  68. rdev->gart.table_addr);
  69. rdev->gart.table.ram.ptr = NULL;
  70. rdev->gart.table_addr = 0;
  71. }
  72. int radeon_gart_table_vram_alloc(struct radeon_device *rdev)
  73. {
  74. int r;
  75. if (rdev->gart.table.vram.robj == NULL) {
  76. r = radeon_bo_create(rdev, rdev->gart.table_size,
  77. PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM,
  78. &rdev->gart.table.vram.robj);
  79. if (r) {
  80. return r;
  81. }
  82. }
  83. return 0;
  84. }
  85. int radeon_gart_table_vram_pin(struct radeon_device *rdev)
  86. {
  87. uint64_t gpu_addr;
  88. int r;
  89. r = radeon_bo_reserve(rdev->gart.table.vram.robj, false);
  90. if (unlikely(r != 0))
  91. return r;
  92. r = radeon_bo_pin(rdev->gart.table.vram.robj,
  93. RADEON_GEM_DOMAIN_VRAM, &gpu_addr);
  94. if (r) {
  95. radeon_bo_unreserve(rdev->gart.table.vram.robj);
  96. return r;
  97. }
  98. r = radeon_bo_kmap(rdev->gart.table.vram.robj,
  99. (void **)&rdev->gart.table.vram.ptr);
  100. if (r)
  101. radeon_bo_unpin(rdev->gart.table.vram.robj);
  102. radeon_bo_unreserve(rdev->gart.table.vram.robj);
  103. rdev->gart.table_addr = gpu_addr;
  104. return r;
  105. }
  106. void radeon_gart_table_vram_free(struct radeon_device *rdev)
  107. {
  108. int r;
  109. if (rdev->gart.table.vram.robj == NULL) {
  110. return;
  111. }
  112. r = radeon_bo_reserve(rdev->gart.table.vram.robj, false);
  113. if (likely(r == 0)) {
  114. radeon_bo_kunmap(rdev->gart.table.vram.robj);
  115. radeon_bo_unpin(rdev->gart.table.vram.robj);
  116. radeon_bo_unreserve(rdev->gart.table.vram.robj);
  117. }
  118. radeon_bo_unref(&rdev->gart.table.vram.robj);
  119. }
  120. /*
  121. * Common gart functions.
  122. */
  123. void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset,
  124. int pages)
  125. {
  126. unsigned t;
  127. unsigned p;
  128. int i, j;
  129. u64 page_base;
  130. if (!rdev->gart.ready) {
  131. WARN(1, "trying to unbind memory to unitialized GART !\n");
  132. return;
  133. }
  134. t = offset / RADEON_GPU_PAGE_SIZE;
  135. p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
  136. for (i = 0; i < pages; i++, p++) {
  137. if (rdev->gart.pages[p]) {
  138. if (!rdev->gart.ttm_alloced[p])
  139. pci_unmap_page(rdev->pdev, rdev->gart.pages_addr[p],
  140. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  141. rdev->gart.pages[p] = NULL;
  142. rdev->gart.pages_addr[p] = rdev->dummy_page.addr;
  143. page_base = rdev->gart.pages_addr[p];
  144. for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
  145. radeon_gart_set_page(rdev, t, page_base);
  146. page_base += RADEON_GPU_PAGE_SIZE;
  147. }
  148. }
  149. }
  150. mb();
  151. radeon_gart_tlb_flush(rdev);
  152. }
  153. int radeon_gart_bind(struct radeon_device *rdev, unsigned offset,
  154. int pages, struct page **pagelist, dma_addr_t *dma_addr)
  155. {
  156. unsigned t;
  157. unsigned p;
  158. uint64_t page_base;
  159. int i, j;
  160. if (!rdev->gart.ready) {
  161. WARN(1, "trying to bind memory to unitialized GART !\n");
  162. return -EINVAL;
  163. }
  164. t = offset / RADEON_GPU_PAGE_SIZE;
  165. p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
  166. for (i = 0; i < pages; i++, p++) {
  167. /* we reverted the patch using dma_addr in TTM for now but this
  168. * code stops building on alpha so just comment it out for now */
  169. if (0) { /*dma_addr[i] != DMA_ERROR_CODE) */
  170. rdev->gart.ttm_alloced[p] = true;
  171. rdev->gart.pages_addr[p] = dma_addr[i];
  172. } else {
  173. /* we need to support large memory configurations */
  174. /* assume that unbind have already been call on the range */
  175. rdev->gart.pages_addr[p] = pci_map_page(rdev->pdev, pagelist[i],
  176. 0, PAGE_SIZE,
  177. PCI_DMA_BIDIRECTIONAL);
  178. if (pci_dma_mapping_error(rdev->pdev, rdev->gart.pages_addr[p])) {
  179. /* FIXME: failed to map page (return -ENOMEM?) */
  180. radeon_gart_unbind(rdev, offset, pages);
  181. return -ENOMEM;
  182. }
  183. }
  184. rdev->gart.pages[p] = pagelist[i];
  185. page_base = rdev->gart.pages_addr[p];
  186. for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
  187. radeon_gart_set_page(rdev, t, page_base);
  188. page_base += RADEON_GPU_PAGE_SIZE;
  189. }
  190. }
  191. mb();
  192. radeon_gart_tlb_flush(rdev);
  193. return 0;
  194. }
  195. void radeon_gart_restore(struct radeon_device *rdev)
  196. {
  197. int i, j, t;
  198. u64 page_base;
  199. for (i = 0, t = 0; i < rdev->gart.num_cpu_pages; i++) {
  200. page_base = rdev->gart.pages_addr[i];
  201. for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
  202. radeon_gart_set_page(rdev, t, page_base);
  203. page_base += RADEON_GPU_PAGE_SIZE;
  204. }
  205. }
  206. mb();
  207. radeon_gart_tlb_flush(rdev);
  208. }
  209. int radeon_gart_init(struct radeon_device *rdev)
  210. {
  211. int r, i;
  212. if (rdev->gart.pages) {
  213. return 0;
  214. }
  215. /* We need PAGE_SIZE >= RADEON_GPU_PAGE_SIZE */
  216. if (PAGE_SIZE < RADEON_GPU_PAGE_SIZE) {
  217. DRM_ERROR("Page size is smaller than GPU page size!\n");
  218. return -EINVAL;
  219. }
  220. r = radeon_dummy_page_init(rdev);
  221. if (r)
  222. return r;
  223. /* Compute table size */
  224. rdev->gart.num_cpu_pages = rdev->mc.gtt_size / PAGE_SIZE;
  225. rdev->gart.num_gpu_pages = rdev->mc.gtt_size / RADEON_GPU_PAGE_SIZE;
  226. DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
  227. rdev->gart.num_cpu_pages, rdev->gart.num_gpu_pages);
  228. /* Allocate pages table */
  229. rdev->gart.pages = kzalloc(sizeof(void *) * rdev->gart.num_cpu_pages,
  230. GFP_KERNEL);
  231. if (rdev->gart.pages == NULL) {
  232. radeon_gart_fini(rdev);
  233. return -ENOMEM;
  234. }
  235. rdev->gart.pages_addr = kzalloc(sizeof(dma_addr_t) *
  236. rdev->gart.num_cpu_pages, GFP_KERNEL);
  237. if (rdev->gart.pages_addr == NULL) {
  238. radeon_gart_fini(rdev);
  239. return -ENOMEM;
  240. }
  241. rdev->gart.ttm_alloced = kzalloc(sizeof(bool) *
  242. rdev->gart.num_cpu_pages, GFP_KERNEL);
  243. if (rdev->gart.ttm_alloced == NULL) {
  244. radeon_gart_fini(rdev);
  245. return -ENOMEM;
  246. }
  247. /* set GART entry to point to the dummy page by default */
  248. for (i = 0; i < rdev->gart.num_cpu_pages; i++) {
  249. rdev->gart.pages_addr[i] = rdev->dummy_page.addr;
  250. }
  251. return 0;
  252. }
  253. void radeon_gart_fini(struct radeon_device *rdev)
  254. {
  255. if (rdev->gart.pages && rdev->gart.pages_addr && rdev->gart.ready) {
  256. /* unbind pages */
  257. radeon_gart_unbind(rdev, 0, rdev->gart.num_cpu_pages);
  258. }
  259. rdev->gart.ready = false;
  260. kfree(rdev->gart.pages);
  261. kfree(rdev->gart.pages_addr);
  262. kfree(rdev->gart.ttm_alloced);
  263. rdev->gart.pages = NULL;
  264. rdev->gart.pages_addr = NULL;
  265. rdev->gart.ttm_alloced = NULL;
  266. radeon_dummy_page_fini(rdev);
  267. }