radeon_ttm.c 23 KB

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