radeon_ttm.c 23 KB

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