radeon_ttm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /*
  2. * Copyright 2009 Jerome Glisse.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. /*
  27. * Authors:
  28. * Jerome Glisse <glisse@freedesktop.org>
  29. * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
  30. * Dave Airlie
  31. */
  32. #include <ttm/ttm_bo_api.h>
  33. #include <ttm/ttm_bo_driver.h>
  34. #include <ttm/ttm_placement.h>
  35. #include <ttm/ttm_module.h>
  36. #include <ttm/ttm_page_alloc.h>
  37. #include <drm/drmP.h>
  38. #include <drm/radeon_drm.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/slab.h>
  41. #include <linux/swiotlb.h>
  42. #include "radeon_reg.h"
  43. #include "radeon.h"
  44. #define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
  45. static int radeon_ttm_debugfs_init(struct radeon_device *rdev);
  46. static struct radeon_device *radeon_get_rdev(struct ttm_bo_device *bdev)
  47. {
  48. struct radeon_mman *mman;
  49. struct radeon_device *rdev;
  50. mman = container_of(bdev, struct radeon_mman, bdev);
  51. rdev = container_of(mman, struct radeon_device, mman);
  52. return rdev;
  53. }
  54. /*
  55. * Global memory.
  56. */
  57. static int radeon_ttm_mem_global_init(struct drm_global_reference *ref)
  58. {
  59. return ttm_mem_global_init(ref->object);
  60. }
  61. static void radeon_ttm_mem_global_release(struct drm_global_reference *ref)
  62. {
  63. ttm_mem_global_release(ref->object);
  64. }
  65. static int radeon_ttm_global_init(struct radeon_device *rdev)
  66. {
  67. struct drm_global_reference *global_ref;
  68. int r;
  69. rdev->mman.mem_global_referenced = false;
  70. global_ref = &rdev->mman.mem_global_ref;
  71. global_ref->global_type = DRM_GLOBAL_TTM_MEM;
  72. global_ref->size = sizeof(struct ttm_mem_global);
  73. global_ref->init = &radeon_ttm_mem_global_init;
  74. global_ref->release = &radeon_ttm_mem_global_release;
  75. r = drm_global_item_ref(global_ref);
  76. if (r != 0) {
  77. DRM_ERROR("Failed setting up TTM memory accounting "
  78. "subsystem.\n");
  79. return r;
  80. }
  81. rdev->mman.bo_global_ref.mem_glob =
  82. rdev->mman.mem_global_ref.object;
  83. global_ref = &rdev->mman.bo_global_ref.ref;
  84. global_ref->global_type = DRM_GLOBAL_TTM_BO;
  85. global_ref->size = sizeof(struct ttm_bo_global);
  86. global_ref->init = &ttm_bo_global_init;
  87. global_ref->release = &ttm_bo_global_release;
  88. r = drm_global_item_ref(global_ref);
  89. if (r != 0) {
  90. DRM_ERROR("Failed setting up TTM BO subsystem.\n");
  91. drm_global_item_unref(&rdev->mman.mem_global_ref);
  92. return r;
  93. }
  94. rdev->mman.mem_global_referenced = true;
  95. return 0;
  96. }
  97. static void radeon_ttm_global_fini(struct radeon_device *rdev)
  98. {
  99. if (rdev->mman.mem_global_referenced) {
  100. drm_global_item_unref(&rdev->mman.bo_global_ref.ref);
  101. drm_global_item_unref(&rdev->mman.mem_global_ref);
  102. rdev->mman.mem_global_referenced = false;
  103. }
  104. }
  105. static int radeon_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  106. {
  107. return 0;
  108. }
  109. static int radeon_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  110. struct ttm_mem_type_manager *man)
  111. {
  112. struct radeon_device *rdev;
  113. rdev = radeon_get_rdev(bdev);
  114. switch (type) {
  115. case TTM_PL_SYSTEM:
  116. /* System memory */
  117. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  118. man->available_caching = TTM_PL_MASK_CACHING;
  119. man->default_caching = TTM_PL_FLAG_CACHED;
  120. break;
  121. case TTM_PL_TT:
  122. man->func = &ttm_bo_manager_func;
  123. man->gpu_offset = rdev->mc.gtt_start;
  124. man->available_caching = TTM_PL_MASK_CACHING;
  125. man->default_caching = TTM_PL_FLAG_CACHED;
  126. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | TTM_MEMTYPE_FLAG_CMA;
  127. #if __OS_HAS_AGP
  128. if (rdev->flags & RADEON_IS_AGP) {
  129. if (!(drm_core_has_AGP(rdev->ddev) && rdev->ddev->agp)) {
  130. DRM_ERROR("AGP is not enabled for memory type %u\n",
  131. (unsigned)type);
  132. return -EINVAL;
  133. }
  134. if (!rdev->ddev->agp->cant_use_aperture)
  135. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  136. man->available_caching = TTM_PL_FLAG_UNCACHED |
  137. TTM_PL_FLAG_WC;
  138. man->default_caching = TTM_PL_FLAG_WC;
  139. }
  140. #endif
  141. break;
  142. case TTM_PL_VRAM:
  143. /* "On-card" video ram */
  144. man->func = &ttm_bo_manager_func;
  145. man->gpu_offset = rdev->mc.vram_start;
  146. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  147. TTM_MEMTYPE_FLAG_MAPPABLE;
  148. man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC;
  149. man->default_caching = TTM_PL_FLAG_WC;
  150. break;
  151. default:
  152. DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
  153. return -EINVAL;
  154. }
  155. return 0;
  156. }
  157. static void radeon_evict_flags(struct ttm_buffer_object *bo,
  158. struct ttm_placement *placement)
  159. {
  160. struct radeon_bo *rbo;
  161. static u32 placements = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  162. if (!radeon_ttm_bo_is_radeon_bo(bo)) {
  163. placement->fpfn = 0;
  164. placement->lpfn = 0;
  165. placement->placement = &placements;
  166. placement->busy_placement = &placements;
  167. placement->num_placement = 1;
  168. placement->num_busy_placement = 1;
  169. return;
  170. }
  171. rbo = container_of(bo, struct radeon_bo, tbo);
  172. switch (bo->mem.mem_type) {
  173. case TTM_PL_VRAM:
  174. if (rbo->rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready == false)
  175. radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_CPU);
  176. else
  177. radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_GTT);
  178. break;
  179. case TTM_PL_TT:
  180. default:
  181. radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_CPU);
  182. }
  183. *placement = rbo->placement;
  184. }
  185. static int radeon_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  186. {
  187. struct radeon_bo *rbo = container_of(bo, struct radeon_bo, tbo);
  188. return drm_vma_node_verify_access(&rbo->gem_base.vma_node, filp);
  189. }
  190. static void radeon_move_null(struct ttm_buffer_object *bo,
  191. struct ttm_mem_reg *new_mem)
  192. {
  193. struct ttm_mem_reg *old_mem = &bo->mem;
  194. BUG_ON(old_mem->mm_node != NULL);
  195. *old_mem = *new_mem;
  196. new_mem->mm_node = NULL;
  197. }
  198. static int radeon_move_blit(struct ttm_buffer_object *bo,
  199. bool evict, bool no_wait_gpu,
  200. struct ttm_mem_reg *new_mem,
  201. struct ttm_mem_reg *old_mem)
  202. {
  203. struct radeon_device *rdev;
  204. uint64_t old_start, new_start;
  205. struct radeon_fence *fence;
  206. int r, ridx;
  207. rdev = radeon_get_rdev(bo->bdev);
  208. ridx = radeon_copy_ring_index(rdev);
  209. old_start = old_mem->start << PAGE_SHIFT;
  210. new_start = new_mem->start << PAGE_SHIFT;
  211. switch (old_mem->mem_type) {
  212. case TTM_PL_VRAM:
  213. old_start += rdev->mc.vram_start;
  214. break;
  215. case TTM_PL_TT:
  216. old_start += rdev->mc.gtt_start;
  217. break;
  218. default:
  219. DRM_ERROR("Unknown placement %d\n", old_mem->mem_type);
  220. return -EINVAL;
  221. }
  222. switch (new_mem->mem_type) {
  223. case TTM_PL_VRAM:
  224. new_start += rdev->mc.vram_start;
  225. break;
  226. case TTM_PL_TT:
  227. new_start += rdev->mc.gtt_start;
  228. break;
  229. default:
  230. DRM_ERROR("Unknown placement %d\n", old_mem->mem_type);
  231. return -EINVAL;
  232. }
  233. if (!rdev->ring[ridx].ready) {
  234. DRM_ERROR("Trying to move memory with ring turned off.\n");
  235. return -EINVAL;
  236. }
  237. BUILD_BUG_ON((PAGE_SIZE % RADEON_GPU_PAGE_SIZE) != 0);
  238. /* sync other rings */
  239. fence = bo->sync_obj;
  240. r = radeon_copy(rdev, old_start, new_start,
  241. new_mem->num_pages * (PAGE_SIZE / RADEON_GPU_PAGE_SIZE), /* GPU pages */
  242. &fence);
  243. /* FIXME: handle copy error */
  244. r = ttm_bo_move_accel_cleanup(bo, (void *)fence,
  245. evict, no_wait_gpu, new_mem);
  246. radeon_fence_unref(&fence);
  247. return r;
  248. }
  249. static int radeon_move_vram_ram(struct ttm_buffer_object *bo,
  250. bool evict, bool interruptible,
  251. bool no_wait_gpu,
  252. struct ttm_mem_reg *new_mem)
  253. {
  254. struct radeon_device *rdev;
  255. struct ttm_mem_reg *old_mem = &bo->mem;
  256. struct ttm_mem_reg tmp_mem;
  257. u32 placements;
  258. struct ttm_placement placement;
  259. int r;
  260. rdev = radeon_get_rdev(bo->bdev);
  261. tmp_mem = *new_mem;
  262. tmp_mem.mm_node = NULL;
  263. placement.fpfn = 0;
  264. placement.lpfn = 0;
  265. placement.num_placement = 1;
  266. placement.placement = &placements;
  267. placement.num_busy_placement = 1;
  268. placement.busy_placement = &placements;
  269. placements = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
  270. r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
  271. interruptible, no_wait_gpu);
  272. if (unlikely(r)) {
  273. return r;
  274. }
  275. r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement);
  276. if (unlikely(r)) {
  277. goto out_cleanup;
  278. }
  279. r = ttm_tt_bind(bo->ttm, &tmp_mem);
  280. if (unlikely(r)) {
  281. goto out_cleanup;
  282. }
  283. r = radeon_move_blit(bo, true, no_wait_gpu, &tmp_mem, old_mem);
  284. if (unlikely(r)) {
  285. goto out_cleanup;
  286. }
  287. r = ttm_bo_move_ttm(bo, true, no_wait_gpu, new_mem);
  288. out_cleanup:
  289. ttm_bo_mem_put(bo, &tmp_mem);
  290. return r;
  291. }
  292. static int radeon_move_ram_vram(struct ttm_buffer_object *bo,
  293. bool evict, bool interruptible,
  294. bool no_wait_gpu,
  295. struct ttm_mem_reg *new_mem)
  296. {
  297. struct radeon_device *rdev;
  298. struct ttm_mem_reg *old_mem = &bo->mem;
  299. struct ttm_mem_reg tmp_mem;
  300. struct ttm_placement placement;
  301. u32 placements;
  302. int r;
  303. rdev = radeon_get_rdev(bo->bdev);
  304. tmp_mem = *new_mem;
  305. tmp_mem.mm_node = NULL;
  306. placement.fpfn = 0;
  307. placement.lpfn = 0;
  308. placement.num_placement = 1;
  309. placement.placement = &placements;
  310. placement.num_busy_placement = 1;
  311. placement.busy_placement = &placements;
  312. placements = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
  313. r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
  314. interruptible, no_wait_gpu);
  315. if (unlikely(r)) {
  316. return r;
  317. }
  318. r = ttm_bo_move_ttm(bo, true, no_wait_gpu, &tmp_mem);
  319. if (unlikely(r)) {
  320. goto out_cleanup;
  321. }
  322. r = radeon_move_blit(bo, true, no_wait_gpu, new_mem, old_mem);
  323. if (unlikely(r)) {
  324. goto out_cleanup;
  325. }
  326. out_cleanup:
  327. ttm_bo_mem_put(bo, &tmp_mem);
  328. return r;
  329. }
  330. static int radeon_bo_move(struct ttm_buffer_object *bo,
  331. bool evict, bool interruptible,
  332. bool no_wait_gpu,
  333. struct ttm_mem_reg *new_mem)
  334. {
  335. struct radeon_device *rdev;
  336. struct ttm_mem_reg *old_mem = &bo->mem;
  337. int r;
  338. rdev = radeon_get_rdev(bo->bdev);
  339. if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  340. radeon_move_null(bo, new_mem);
  341. return 0;
  342. }
  343. if ((old_mem->mem_type == TTM_PL_TT &&
  344. new_mem->mem_type == TTM_PL_SYSTEM) ||
  345. (old_mem->mem_type == TTM_PL_SYSTEM &&
  346. new_mem->mem_type == TTM_PL_TT)) {
  347. /* bind is enough */
  348. radeon_move_null(bo, new_mem);
  349. return 0;
  350. }
  351. if (!rdev->ring[radeon_copy_ring_index(rdev)].ready ||
  352. rdev->asic->copy.copy == NULL) {
  353. /* use memcpy */
  354. goto memcpy;
  355. }
  356. if (old_mem->mem_type == TTM_PL_VRAM &&
  357. new_mem->mem_type == TTM_PL_SYSTEM) {
  358. r = radeon_move_vram_ram(bo, evict, interruptible,
  359. no_wait_gpu, new_mem);
  360. } else if (old_mem->mem_type == TTM_PL_SYSTEM &&
  361. new_mem->mem_type == TTM_PL_VRAM) {
  362. r = radeon_move_ram_vram(bo, evict, interruptible,
  363. no_wait_gpu, new_mem);
  364. } else {
  365. r = radeon_move_blit(bo, evict, no_wait_gpu, new_mem, old_mem);
  366. }
  367. if (r) {
  368. memcpy:
  369. r = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem);
  370. }
  371. return r;
  372. }
  373. static int radeon_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  374. {
  375. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  376. struct radeon_device *rdev = radeon_get_rdev(bdev);
  377. mem->bus.addr = NULL;
  378. mem->bus.offset = 0;
  379. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  380. mem->bus.base = 0;
  381. mem->bus.is_iomem = false;
  382. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  383. return -EINVAL;
  384. switch (mem->mem_type) {
  385. case TTM_PL_SYSTEM:
  386. /* system memory */
  387. return 0;
  388. case TTM_PL_TT:
  389. #if __OS_HAS_AGP
  390. if (rdev->flags & RADEON_IS_AGP) {
  391. /* RADEON_IS_AGP is set only if AGP is active */
  392. mem->bus.offset = mem->start << PAGE_SHIFT;
  393. mem->bus.base = rdev->mc.agp_base;
  394. mem->bus.is_iomem = !rdev->ddev->agp->cant_use_aperture;
  395. }
  396. #endif
  397. break;
  398. case TTM_PL_VRAM:
  399. mem->bus.offset = mem->start << PAGE_SHIFT;
  400. /* check if it's visible */
  401. if ((mem->bus.offset + mem->bus.size) > rdev->mc.visible_vram_size)
  402. return -EINVAL;
  403. mem->bus.base = rdev->mc.aper_base;
  404. mem->bus.is_iomem = true;
  405. #ifdef __alpha__
  406. /*
  407. * Alpha: use bus.addr to hold the ioremap() return,
  408. * so we can modify bus.base below.
  409. */
  410. if (mem->placement & TTM_PL_FLAG_WC)
  411. mem->bus.addr =
  412. ioremap_wc(mem->bus.base + mem->bus.offset,
  413. mem->bus.size);
  414. else
  415. mem->bus.addr =
  416. ioremap_nocache(mem->bus.base + mem->bus.offset,
  417. mem->bus.size);
  418. /*
  419. * Alpha: Use just the bus offset plus
  420. * the hose/domain memory base for bus.base.
  421. * It then can be used to build PTEs for VRAM
  422. * access, as done in ttm_bo_vm_fault().
  423. */
  424. mem->bus.base = (mem->bus.base & 0x0ffffffffUL) +
  425. rdev->ddev->hose->dense_mem_base;
  426. #endif
  427. break;
  428. default:
  429. return -EINVAL;
  430. }
  431. return 0;
  432. }
  433. static void radeon_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  434. {
  435. }
  436. static int radeon_sync_obj_wait(void *sync_obj, bool lazy, bool interruptible)
  437. {
  438. return radeon_fence_wait((struct radeon_fence *)sync_obj, interruptible);
  439. }
  440. static int radeon_sync_obj_flush(void *sync_obj)
  441. {
  442. return 0;
  443. }
  444. static void radeon_sync_obj_unref(void **sync_obj)
  445. {
  446. radeon_fence_unref((struct radeon_fence **)sync_obj);
  447. }
  448. static void *radeon_sync_obj_ref(void *sync_obj)
  449. {
  450. return radeon_fence_ref((struct radeon_fence *)sync_obj);
  451. }
  452. static bool radeon_sync_obj_signaled(void *sync_obj)
  453. {
  454. return radeon_fence_signaled((struct radeon_fence *)sync_obj);
  455. }
  456. /*
  457. * TTM backend functions.
  458. */
  459. struct radeon_ttm_tt {
  460. struct ttm_dma_tt ttm;
  461. struct radeon_device *rdev;
  462. u64 offset;
  463. };
  464. static int radeon_ttm_backend_bind(struct ttm_tt *ttm,
  465. struct ttm_mem_reg *bo_mem)
  466. {
  467. struct radeon_ttm_tt *gtt = (void*)ttm;
  468. int r;
  469. gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
  470. if (!ttm->num_pages) {
  471. WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
  472. ttm->num_pages, bo_mem, ttm);
  473. }
  474. r = radeon_gart_bind(gtt->rdev, gtt->offset,
  475. ttm->num_pages, ttm->pages, gtt->ttm.dma_address);
  476. if (r) {
  477. DRM_ERROR("failed to bind %lu pages at 0x%08X\n",
  478. ttm->num_pages, (unsigned)gtt->offset);
  479. return r;
  480. }
  481. return 0;
  482. }
  483. static int radeon_ttm_backend_unbind(struct ttm_tt *ttm)
  484. {
  485. struct radeon_ttm_tt *gtt = (void *)ttm;
  486. radeon_gart_unbind(gtt->rdev, gtt->offset, ttm->num_pages);
  487. return 0;
  488. }
  489. static void radeon_ttm_backend_destroy(struct ttm_tt *ttm)
  490. {
  491. struct radeon_ttm_tt *gtt = (void *)ttm;
  492. ttm_dma_tt_fini(&gtt->ttm);
  493. kfree(gtt);
  494. }
  495. static struct ttm_backend_func radeon_backend_func = {
  496. .bind = &radeon_ttm_backend_bind,
  497. .unbind = &radeon_ttm_backend_unbind,
  498. .destroy = &radeon_ttm_backend_destroy,
  499. };
  500. static struct ttm_tt *radeon_ttm_tt_create(struct ttm_bo_device *bdev,
  501. unsigned long size, uint32_t page_flags,
  502. struct page *dummy_read_page)
  503. {
  504. struct radeon_device *rdev;
  505. struct radeon_ttm_tt *gtt;
  506. rdev = radeon_get_rdev(bdev);
  507. #if __OS_HAS_AGP
  508. if (rdev->flags & RADEON_IS_AGP) {
  509. return ttm_agp_tt_create(bdev, rdev->ddev->agp->bridge,
  510. size, page_flags, dummy_read_page);
  511. }
  512. #endif
  513. gtt = kzalloc(sizeof(struct radeon_ttm_tt), GFP_KERNEL);
  514. if (gtt == NULL) {
  515. return NULL;
  516. }
  517. gtt->ttm.ttm.func = &radeon_backend_func;
  518. gtt->rdev = rdev;
  519. if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags, dummy_read_page)) {
  520. kfree(gtt);
  521. return NULL;
  522. }
  523. return &gtt->ttm.ttm;
  524. }
  525. static int radeon_ttm_tt_populate(struct ttm_tt *ttm)
  526. {
  527. struct radeon_device *rdev;
  528. struct radeon_ttm_tt *gtt = (void *)ttm;
  529. unsigned i;
  530. int r;
  531. bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  532. if (ttm->state != tt_unpopulated)
  533. return 0;
  534. if (slave && ttm->sg) {
  535. drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
  536. gtt->ttm.dma_address, ttm->num_pages);
  537. ttm->state = tt_unbound;
  538. return 0;
  539. }
  540. rdev = radeon_get_rdev(ttm->bdev);
  541. #if __OS_HAS_AGP
  542. if (rdev->flags & RADEON_IS_AGP) {
  543. return ttm_agp_tt_populate(ttm);
  544. }
  545. #endif
  546. #ifdef CONFIG_SWIOTLB
  547. if (swiotlb_nr_tbl()) {
  548. return ttm_dma_populate(&gtt->ttm, rdev->dev);
  549. }
  550. #endif
  551. r = ttm_pool_populate(ttm);
  552. if (r) {
  553. return r;
  554. }
  555. for (i = 0; i < ttm->num_pages; i++) {
  556. gtt->ttm.dma_address[i] = pci_map_page(rdev->pdev, ttm->pages[i],
  557. 0, PAGE_SIZE,
  558. PCI_DMA_BIDIRECTIONAL);
  559. if (pci_dma_mapping_error(rdev->pdev, gtt->ttm.dma_address[i])) {
  560. while (--i) {
  561. pci_unmap_page(rdev->pdev, gtt->ttm.dma_address[i],
  562. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  563. gtt->ttm.dma_address[i] = 0;
  564. }
  565. ttm_pool_unpopulate(ttm);
  566. return -EFAULT;
  567. }
  568. }
  569. return 0;
  570. }
  571. static void radeon_ttm_tt_unpopulate(struct ttm_tt *ttm)
  572. {
  573. struct radeon_device *rdev;
  574. struct radeon_ttm_tt *gtt = (void *)ttm;
  575. unsigned i;
  576. bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  577. if (slave)
  578. return;
  579. rdev = radeon_get_rdev(ttm->bdev);
  580. #if __OS_HAS_AGP
  581. if (rdev->flags & RADEON_IS_AGP) {
  582. ttm_agp_tt_unpopulate(ttm);
  583. return;
  584. }
  585. #endif
  586. #ifdef CONFIG_SWIOTLB
  587. if (swiotlb_nr_tbl()) {
  588. ttm_dma_unpopulate(&gtt->ttm, rdev->dev);
  589. return;
  590. }
  591. #endif
  592. for (i = 0; i < ttm->num_pages; i++) {
  593. if (gtt->ttm.dma_address[i]) {
  594. pci_unmap_page(rdev->pdev, gtt->ttm.dma_address[i],
  595. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  596. }
  597. }
  598. ttm_pool_unpopulate(ttm);
  599. }
  600. static struct ttm_bo_driver radeon_bo_driver = {
  601. .ttm_tt_create = &radeon_ttm_tt_create,
  602. .ttm_tt_populate = &radeon_ttm_tt_populate,
  603. .ttm_tt_unpopulate = &radeon_ttm_tt_unpopulate,
  604. .invalidate_caches = &radeon_invalidate_caches,
  605. .init_mem_type = &radeon_init_mem_type,
  606. .evict_flags = &radeon_evict_flags,
  607. .move = &radeon_bo_move,
  608. .verify_access = &radeon_verify_access,
  609. .sync_obj_signaled = &radeon_sync_obj_signaled,
  610. .sync_obj_wait = &radeon_sync_obj_wait,
  611. .sync_obj_flush = &radeon_sync_obj_flush,
  612. .sync_obj_unref = &radeon_sync_obj_unref,
  613. .sync_obj_ref = &radeon_sync_obj_ref,
  614. .move_notify = &radeon_bo_move_notify,
  615. .fault_reserve_notify = &radeon_bo_fault_reserve_notify,
  616. .io_mem_reserve = &radeon_ttm_io_mem_reserve,
  617. .io_mem_free = &radeon_ttm_io_mem_free,
  618. };
  619. int radeon_ttm_init(struct radeon_device *rdev)
  620. {
  621. int r;
  622. r = radeon_ttm_global_init(rdev);
  623. if (r) {
  624. return r;
  625. }
  626. /* No others user of address space so set it to 0 */
  627. r = ttm_bo_device_init(&rdev->mman.bdev,
  628. rdev->mman.bo_global_ref.ref.object,
  629. &radeon_bo_driver, DRM_FILE_PAGE_OFFSET,
  630. rdev->need_dma32);
  631. if (r) {
  632. DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
  633. return r;
  634. }
  635. rdev->mman.initialized = true;
  636. r = ttm_bo_init_mm(&rdev->mman.bdev, TTM_PL_VRAM,
  637. rdev->mc.real_vram_size >> PAGE_SHIFT);
  638. if (r) {
  639. DRM_ERROR("Failed initializing VRAM heap.\n");
  640. return r;
  641. }
  642. r = radeon_bo_create(rdev, 256 * 1024, PAGE_SIZE, true,
  643. RADEON_GEM_DOMAIN_VRAM,
  644. NULL, &rdev->stollen_vga_memory);
  645. if (r) {
  646. return r;
  647. }
  648. r = radeon_bo_reserve(rdev->stollen_vga_memory, false);
  649. if (r)
  650. return r;
  651. r = radeon_bo_pin(rdev->stollen_vga_memory, RADEON_GEM_DOMAIN_VRAM, NULL);
  652. radeon_bo_unreserve(rdev->stollen_vga_memory);
  653. if (r) {
  654. radeon_bo_unref(&rdev->stollen_vga_memory);
  655. return r;
  656. }
  657. DRM_INFO("radeon: %uM of VRAM memory ready\n",
  658. (unsigned) (rdev->mc.real_vram_size / (1024 * 1024)));
  659. r = ttm_bo_init_mm(&rdev->mman.bdev, TTM_PL_TT,
  660. rdev->mc.gtt_size >> PAGE_SHIFT);
  661. if (r) {
  662. DRM_ERROR("Failed initializing GTT heap.\n");
  663. return r;
  664. }
  665. DRM_INFO("radeon: %uM of GTT memory ready.\n",
  666. (unsigned)(rdev->mc.gtt_size / (1024 * 1024)));
  667. rdev->mman.bdev.dev_mapping = rdev->ddev->dev_mapping;
  668. r = radeon_ttm_debugfs_init(rdev);
  669. if (r) {
  670. DRM_ERROR("Failed to init debugfs\n");
  671. return r;
  672. }
  673. return 0;
  674. }
  675. void radeon_ttm_fini(struct radeon_device *rdev)
  676. {
  677. int r;
  678. if (!rdev->mman.initialized)
  679. return;
  680. if (rdev->stollen_vga_memory) {
  681. r = radeon_bo_reserve(rdev->stollen_vga_memory, false);
  682. if (r == 0) {
  683. radeon_bo_unpin(rdev->stollen_vga_memory);
  684. radeon_bo_unreserve(rdev->stollen_vga_memory);
  685. }
  686. radeon_bo_unref(&rdev->stollen_vga_memory);
  687. }
  688. ttm_bo_clean_mm(&rdev->mman.bdev, TTM_PL_VRAM);
  689. ttm_bo_clean_mm(&rdev->mman.bdev, TTM_PL_TT);
  690. ttm_bo_device_release(&rdev->mman.bdev);
  691. radeon_gart_fini(rdev);
  692. radeon_ttm_global_fini(rdev);
  693. rdev->mman.initialized = false;
  694. DRM_INFO("radeon: ttm finalized\n");
  695. }
  696. /* this should only be called at bootup or when userspace
  697. * isn't running */
  698. void radeon_ttm_set_active_vram_size(struct radeon_device *rdev, u64 size)
  699. {
  700. struct ttm_mem_type_manager *man;
  701. if (!rdev->mman.initialized)
  702. return;
  703. man = &rdev->mman.bdev.man[TTM_PL_VRAM];
  704. /* this just adjusts TTM size idea, which sets lpfn to the correct value */
  705. man->size = size >> PAGE_SHIFT;
  706. }
  707. static struct vm_operations_struct radeon_ttm_vm_ops;
  708. static const struct vm_operations_struct *ttm_vm_ops = NULL;
  709. static int radeon_ttm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  710. {
  711. struct ttm_buffer_object *bo;
  712. struct radeon_device *rdev;
  713. int r;
  714. bo = (struct ttm_buffer_object *)vma->vm_private_data;
  715. if (bo == NULL) {
  716. return VM_FAULT_NOPAGE;
  717. }
  718. rdev = radeon_get_rdev(bo->bdev);
  719. down_read(&rdev->pm.mclk_lock);
  720. r = ttm_vm_ops->fault(vma, vmf);
  721. up_read(&rdev->pm.mclk_lock);
  722. return r;
  723. }
  724. int radeon_mmap(struct file *filp, struct vm_area_struct *vma)
  725. {
  726. struct drm_file *file_priv;
  727. struct radeon_device *rdev;
  728. int r;
  729. if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET)) {
  730. return drm_mmap(filp, vma);
  731. }
  732. file_priv = filp->private_data;
  733. rdev = file_priv->minor->dev->dev_private;
  734. if (rdev == NULL) {
  735. return -EINVAL;
  736. }
  737. r = ttm_bo_mmap(filp, vma, &rdev->mman.bdev);
  738. if (unlikely(r != 0)) {
  739. return r;
  740. }
  741. if (unlikely(ttm_vm_ops == NULL)) {
  742. ttm_vm_ops = vma->vm_ops;
  743. radeon_ttm_vm_ops = *ttm_vm_ops;
  744. radeon_ttm_vm_ops.fault = &radeon_ttm_fault;
  745. }
  746. vma->vm_ops = &radeon_ttm_vm_ops;
  747. return 0;
  748. }
  749. #define RADEON_DEBUGFS_MEM_TYPES 2
  750. #if defined(CONFIG_DEBUG_FS)
  751. static int radeon_mm_dump_table(struct seq_file *m, void *data)
  752. {
  753. struct drm_info_node *node = (struct drm_info_node *)m->private;
  754. struct drm_mm *mm = (struct drm_mm *)node->info_ent->data;
  755. struct drm_device *dev = node->minor->dev;
  756. struct radeon_device *rdev = dev->dev_private;
  757. int ret;
  758. struct ttm_bo_global *glob = rdev->mman.bdev.glob;
  759. spin_lock(&glob->lru_lock);
  760. ret = drm_mm_dump_table(m, mm);
  761. spin_unlock(&glob->lru_lock);
  762. return ret;
  763. }
  764. #endif
  765. static int radeon_ttm_debugfs_init(struct radeon_device *rdev)
  766. {
  767. #if defined(CONFIG_DEBUG_FS)
  768. static struct drm_info_list radeon_mem_types_list[RADEON_DEBUGFS_MEM_TYPES+2];
  769. static char radeon_mem_types_names[RADEON_DEBUGFS_MEM_TYPES+2][32];
  770. unsigned i;
  771. for (i = 0; i < RADEON_DEBUGFS_MEM_TYPES; i++) {
  772. if (i == 0)
  773. sprintf(radeon_mem_types_names[i], "radeon_vram_mm");
  774. else
  775. sprintf(radeon_mem_types_names[i], "radeon_gtt_mm");
  776. radeon_mem_types_list[i].name = radeon_mem_types_names[i];
  777. radeon_mem_types_list[i].show = &radeon_mm_dump_table;
  778. radeon_mem_types_list[i].driver_features = 0;
  779. if (i == 0)
  780. radeon_mem_types_list[i].data = rdev->mman.bdev.man[TTM_PL_VRAM].priv;
  781. else
  782. radeon_mem_types_list[i].data = rdev->mman.bdev.man[TTM_PL_TT].priv;
  783. }
  784. /* Add ttm page pool to debugfs */
  785. sprintf(radeon_mem_types_names[i], "ttm_page_pool");
  786. radeon_mem_types_list[i].name = radeon_mem_types_names[i];
  787. radeon_mem_types_list[i].show = &ttm_page_alloc_debugfs;
  788. radeon_mem_types_list[i].driver_features = 0;
  789. radeon_mem_types_list[i++].data = NULL;
  790. #ifdef CONFIG_SWIOTLB
  791. if (swiotlb_nr_tbl()) {
  792. sprintf(radeon_mem_types_names[i], "ttm_dma_page_pool");
  793. radeon_mem_types_list[i].name = radeon_mem_types_names[i];
  794. radeon_mem_types_list[i].show = &ttm_dma_page_alloc_debugfs;
  795. radeon_mem_types_list[i].driver_features = 0;
  796. radeon_mem_types_list[i++].data = NULL;
  797. }
  798. #endif
  799. return radeon_debugfs_add_files(rdev, radeon_mem_types_list, i);
  800. #endif
  801. return 0;
  802. }