radeon_gart.c 11 KB

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