radeon_ttm.c 24 KB

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