amdgpu_ttm.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  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/amdgpu_drm.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/slab.h>
  41. #include <linux/swiotlb.h>
  42. #include <linux/swap.h>
  43. #include <linux/pagemap.h>
  44. #include <linux/debugfs.h>
  45. #include "amdgpu.h"
  46. #include "bif/bif_4_1_d.h"
  47. #define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
  48. static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
  49. static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev);
  50. static struct amdgpu_device *amdgpu_get_adev(struct ttm_bo_device *bdev)
  51. {
  52. struct amdgpu_mman *mman;
  53. struct amdgpu_device *adev;
  54. mman = container_of(bdev, struct amdgpu_mman, bdev);
  55. adev = container_of(mman, struct amdgpu_device, mman);
  56. return adev;
  57. }
  58. /*
  59. * Global memory.
  60. */
  61. static int amdgpu_ttm_mem_global_init(struct drm_global_reference *ref)
  62. {
  63. return ttm_mem_global_init(ref->object);
  64. }
  65. static void amdgpu_ttm_mem_global_release(struct drm_global_reference *ref)
  66. {
  67. ttm_mem_global_release(ref->object);
  68. }
  69. static int amdgpu_ttm_global_init(struct amdgpu_device *adev)
  70. {
  71. struct drm_global_reference *global_ref;
  72. int r;
  73. adev->mman.mem_global_referenced = false;
  74. global_ref = &adev->mman.mem_global_ref;
  75. global_ref->global_type = DRM_GLOBAL_TTM_MEM;
  76. global_ref->size = sizeof(struct ttm_mem_global);
  77. global_ref->init = &amdgpu_ttm_mem_global_init;
  78. global_ref->release = &amdgpu_ttm_mem_global_release;
  79. r = drm_global_item_ref(global_ref);
  80. if (r != 0) {
  81. DRM_ERROR("Failed setting up TTM memory accounting "
  82. "subsystem.\n");
  83. return r;
  84. }
  85. adev->mman.bo_global_ref.mem_glob =
  86. adev->mman.mem_global_ref.object;
  87. global_ref = &adev->mman.bo_global_ref.ref;
  88. global_ref->global_type = DRM_GLOBAL_TTM_BO;
  89. global_ref->size = sizeof(struct ttm_bo_global);
  90. global_ref->init = &ttm_bo_global_init;
  91. global_ref->release = &ttm_bo_global_release;
  92. r = drm_global_item_ref(global_ref);
  93. if (r != 0) {
  94. DRM_ERROR("Failed setting up TTM BO subsystem.\n");
  95. drm_global_item_unref(&adev->mman.mem_global_ref);
  96. return r;
  97. }
  98. adev->mman.mem_global_referenced = true;
  99. return 0;
  100. }
  101. static void amdgpu_ttm_global_fini(struct amdgpu_device *adev)
  102. {
  103. if (adev->mman.mem_global_referenced) {
  104. drm_global_item_unref(&adev->mman.bo_global_ref.ref);
  105. drm_global_item_unref(&adev->mman.mem_global_ref);
  106. adev->mman.mem_global_referenced = false;
  107. }
  108. }
  109. static int amdgpu_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  110. {
  111. return 0;
  112. }
  113. static int amdgpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  114. struct ttm_mem_type_manager *man)
  115. {
  116. struct amdgpu_device *adev;
  117. adev = amdgpu_get_adev(bdev);
  118. switch (type) {
  119. case TTM_PL_SYSTEM:
  120. /* System memory */
  121. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  122. man->available_caching = TTM_PL_MASK_CACHING;
  123. man->default_caching = TTM_PL_FLAG_CACHED;
  124. break;
  125. case TTM_PL_TT:
  126. man->func = &ttm_bo_manager_func;
  127. man->gpu_offset = adev->mc.gtt_start;
  128. man->available_caching = TTM_PL_MASK_CACHING;
  129. man->default_caching = TTM_PL_FLAG_CACHED;
  130. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | TTM_MEMTYPE_FLAG_CMA;
  131. break;
  132. case TTM_PL_VRAM:
  133. /* "On-card" video ram */
  134. man->func = &ttm_bo_manager_func;
  135. man->gpu_offset = adev->mc.vram_start;
  136. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  137. TTM_MEMTYPE_FLAG_MAPPABLE;
  138. man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC;
  139. man->default_caching = TTM_PL_FLAG_WC;
  140. break;
  141. case AMDGPU_PL_GDS:
  142. case AMDGPU_PL_GWS:
  143. case AMDGPU_PL_OA:
  144. /* On-chip GDS memory*/
  145. man->func = &ttm_bo_manager_func;
  146. man->gpu_offset = 0;
  147. man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_CMA;
  148. man->available_caching = TTM_PL_FLAG_UNCACHED;
  149. man->default_caching = TTM_PL_FLAG_UNCACHED;
  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 amdgpu_evict_flags(struct ttm_buffer_object *bo,
  158. struct ttm_placement *placement)
  159. {
  160. struct amdgpu_bo *rbo;
  161. static struct ttm_place placements = {
  162. .fpfn = 0,
  163. .lpfn = 0,
  164. .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
  165. };
  166. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo)) {
  167. placement->placement = &placements;
  168. placement->busy_placement = &placements;
  169. placement->num_placement = 1;
  170. placement->num_busy_placement = 1;
  171. return;
  172. }
  173. rbo = container_of(bo, struct amdgpu_bo, tbo);
  174. switch (bo->mem.mem_type) {
  175. case TTM_PL_VRAM:
  176. if (rbo->adev->mman.buffer_funcs_ring->ready == false)
  177. amdgpu_ttm_placement_from_domain(rbo, AMDGPU_GEM_DOMAIN_CPU);
  178. else
  179. amdgpu_ttm_placement_from_domain(rbo, AMDGPU_GEM_DOMAIN_GTT);
  180. break;
  181. case TTM_PL_TT:
  182. default:
  183. amdgpu_ttm_placement_from_domain(rbo, AMDGPU_GEM_DOMAIN_CPU);
  184. }
  185. *placement = rbo->placement;
  186. }
  187. static int amdgpu_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  188. {
  189. struct amdgpu_bo *rbo = container_of(bo, struct amdgpu_bo, tbo);
  190. return drm_vma_node_verify_access(&rbo->gem_base.vma_node, filp);
  191. }
  192. static void amdgpu_move_null(struct ttm_buffer_object *bo,
  193. struct ttm_mem_reg *new_mem)
  194. {
  195. struct ttm_mem_reg *old_mem = &bo->mem;
  196. BUG_ON(old_mem->mm_node != NULL);
  197. *old_mem = *new_mem;
  198. new_mem->mm_node = NULL;
  199. }
  200. static int amdgpu_move_blit(struct ttm_buffer_object *bo,
  201. bool evict, bool no_wait_gpu,
  202. struct ttm_mem_reg *new_mem,
  203. struct ttm_mem_reg *old_mem)
  204. {
  205. struct amdgpu_device *adev;
  206. struct amdgpu_ring *ring;
  207. uint64_t old_start, new_start;
  208. struct fence *fence;
  209. int r;
  210. adev = amdgpu_get_adev(bo->bdev);
  211. ring = adev->mman.buffer_funcs_ring;
  212. old_start = (u64)old_mem->start << PAGE_SHIFT;
  213. new_start = (u64)new_mem->start << PAGE_SHIFT;
  214. switch (old_mem->mem_type) {
  215. case TTM_PL_VRAM:
  216. old_start += adev->mc.vram_start;
  217. break;
  218. case TTM_PL_TT:
  219. old_start += adev->mc.gtt_start;
  220. break;
  221. default:
  222. DRM_ERROR("Unknown placement %d\n", old_mem->mem_type);
  223. return -EINVAL;
  224. }
  225. switch (new_mem->mem_type) {
  226. case TTM_PL_VRAM:
  227. new_start += adev->mc.vram_start;
  228. break;
  229. case TTM_PL_TT:
  230. new_start += adev->mc.gtt_start;
  231. break;
  232. default:
  233. DRM_ERROR("Unknown placement %d\n", old_mem->mem_type);
  234. return -EINVAL;
  235. }
  236. if (!ring->ready) {
  237. DRM_ERROR("Trying to move memory with ring turned off.\n");
  238. return -EINVAL;
  239. }
  240. BUILD_BUG_ON((PAGE_SIZE % AMDGPU_GPU_PAGE_SIZE) != 0);
  241. r = amdgpu_copy_buffer(ring, old_start, new_start,
  242. new_mem->num_pages * PAGE_SIZE, /* bytes */
  243. bo->resv, &fence);
  244. /* FIXME: handle copy error */
  245. r = ttm_bo_move_accel_cleanup(bo, fence,
  246. evict, no_wait_gpu, new_mem);
  247. fence_put(fence);
  248. return r;
  249. }
  250. static int amdgpu_move_vram_ram(struct ttm_buffer_object *bo,
  251. bool evict, bool interruptible,
  252. bool no_wait_gpu,
  253. struct ttm_mem_reg *new_mem)
  254. {
  255. struct amdgpu_device *adev;
  256. struct ttm_mem_reg *old_mem = &bo->mem;
  257. struct ttm_mem_reg tmp_mem;
  258. struct ttm_place placements;
  259. struct ttm_placement placement;
  260. int r;
  261. adev = amdgpu_get_adev(bo->bdev);
  262. tmp_mem = *new_mem;
  263. tmp_mem.mm_node = NULL;
  264. placement.num_placement = 1;
  265. placement.placement = &placements;
  266. placement.num_busy_placement = 1;
  267. placement.busy_placement = &placements;
  268. placements.fpfn = 0;
  269. placements.lpfn = 0;
  270. placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
  271. r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
  272. interruptible, no_wait_gpu);
  273. if (unlikely(r)) {
  274. return r;
  275. }
  276. r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement);
  277. if (unlikely(r)) {
  278. goto out_cleanup;
  279. }
  280. r = ttm_tt_bind(bo->ttm, &tmp_mem);
  281. if (unlikely(r)) {
  282. goto out_cleanup;
  283. }
  284. r = amdgpu_move_blit(bo, true, no_wait_gpu, &tmp_mem, old_mem);
  285. if (unlikely(r)) {
  286. goto out_cleanup;
  287. }
  288. r = ttm_bo_move_ttm(bo, true, no_wait_gpu, new_mem);
  289. out_cleanup:
  290. ttm_bo_mem_put(bo, &tmp_mem);
  291. return r;
  292. }
  293. static int amdgpu_move_ram_vram(struct ttm_buffer_object *bo,
  294. bool evict, bool interruptible,
  295. bool no_wait_gpu,
  296. struct ttm_mem_reg *new_mem)
  297. {
  298. struct amdgpu_device *adev;
  299. struct ttm_mem_reg *old_mem = &bo->mem;
  300. struct ttm_mem_reg tmp_mem;
  301. struct ttm_placement placement;
  302. struct ttm_place placements;
  303. int r;
  304. adev = amdgpu_get_adev(bo->bdev);
  305. tmp_mem = *new_mem;
  306. tmp_mem.mm_node = NULL;
  307. placement.num_placement = 1;
  308. placement.placement = &placements;
  309. placement.num_busy_placement = 1;
  310. placement.busy_placement = &placements;
  311. placements.fpfn = 0;
  312. placements.lpfn = 0;
  313. placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
  314. r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
  315. interruptible, no_wait_gpu);
  316. if (unlikely(r)) {
  317. return r;
  318. }
  319. r = ttm_bo_move_ttm(bo, true, no_wait_gpu, &tmp_mem);
  320. if (unlikely(r)) {
  321. goto out_cleanup;
  322. }
  323. r = amdgpu_move_blit(bo, true, no_wait_gpu, new_mem, old_mem);
  324. if (unlikely(r)) {
  325. goto out_cleanup;
  326. }
  327. out_cleanup:
  328. ttm_bo_mem_put(bo, &tmp_mem);
  329. return r;
  330. }
  331. static int amdgpu_bo_move(struct ttm_buffer_object *bo,
  332. bool evict, bool interruptible,
  333. bool no_wait_gpu,
  334. struct ttm_mem_reg *new_mem)
  335. {
  336. struct amdgpu_device *adev;
  337. struct ttm_mem_reg *old_mem = &bo->mem;
  338. int r;
  339. adev = amdgpu_get_adev(bo->bdev);
  340. if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  341. amdgpu_move_null(bo, new_mem);
  342. return 0;
  343. }
  344. if ((old_mem->mem_type == TTM_PL_TT &&
  345. new_mem->mem_type == TTM_PL_SYSTEM) ||
  346. (old_mem->mem_type == TTM_PL_SYSTEM &&
  347. new_mem->mem_type == TTM_PL_TT)) {
  348. /* bind is enough */
  349. amdgpu_move_null(bo, new_mem);
  350. return 0;
  351. }
  352. if (adev->mman.buffer_funcs == NULL ||
  353. adev->mman.buffer_funcs_ring == NULL ||
  354. !adev->mman.buffer_funcs_ring->ready) {
  355. /* use memcpy */
  356. goto memcpy;
  357. }
  358. if (old_mem->mem_type == TTM_PL_VRAM &&
  359. new_mem->mem_type == TTM_PL_SYSTEM) {
  360. r = amdgpu_move_vram_ram(bo, evict, interruptible,
  361. no_wait_gpu, new_mem);
  362. } else if (old_mem->mem_type == TTM_PL_SYSTEM &&
  363. new_mem->mem_type == TTM_PL_VRAM) {
  364. r = amdgpu_move_ram_vram(bo, evict, interruptible,
  365. no_wait_gpu, new_mem);
  366. } else {
  367. r = amdgpu_move_blit(bo, evict, no_wait_gpu, new_mem, old_mem);
  368. }
  369. if (r) {
  370. memcpy:
  371. r = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem);
  372. if (r) {
  373. return r;
  374. }
  375. }
  376. /* update statistics */
  377. atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved);
  378. return 0;
  379. }
  380. static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  381. {
  382. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  383. struct amdgpu_device *adev = amdgpu_get_adev(bdev);
  384. mem->bus.addr = NULL;
  385. mem->bus.offset = 0;
  386. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  387. mem->bus.base = 0;
  388. mem->bus.is_iomem = false;
  389. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  390. return -EINVAL;
  391. switch (mem->mem_type) {
  392. case TTM_PL_SYSTEM:
  393. /* system memory */
  394. return 0;
  395. case TTM_PL_TT:
  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) > adev->mc.visible_vram_size)
  401. return -EINVAL;
  402. mem->bus.base = adev->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. adev->ddev->hose->dense_mem_base;
  425. #endif
  426. break;
  427. default:
  428. return -EINVAL;
  429. }
  430. return 0;
  431. }
  432. static void amdgpu_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  433. {
  434. }
  435. /*
  436. * TTM backend functions.
  437. */
  438. struct amdgpu_ttm_tt {
  439. struct ttm_dma_tt ttm;
  440. struct amdgpu_device *adev;
  441. u64 offset;
  442. uint64_t userptr;
  443. struct mm_struct *usermm;
  444. uint32_t userflags;
  445. };
  446. /* prepare the sg table with the user pages */
  447. static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm)
  448. {
  449. struct amdgpu_device *adev = amdgpu_get_adev(ttm->bdev);
  450. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  451. unsigned pinned = 0, nents;
  452. int r;
  453. int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
  454. unsigned int flags = 0;
  455. enum dma_data_direction direction = write ?
  456. DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
  457. if (write)
  458. flags |= FOLL_WRITE;
  459. if (current->mm != gtt->usermm)
  460. return -EPERM;
  461. if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
  462. /* check that we only pin down anonymous memory
  463. to prevent problems with writeback */
  464. unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
  465. struct vm_area_struct *vma;
  466. vma = find_vma(gtt->usermm, gtt->userptr);
  467. if (!vma || vma->vm_file || vma->vm_end < end)
  468. return -EPERM;
  469. }
  470. do {
  471. unsigned num_pages = ttm->num_pages - pinned;
  472. uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
  473. struct page **pages = ttm->pages + pinned;
  474. r = get_user_pages(current, current->mm, userptr, num_pages,
  475. flags, pages, NULL);
  476. if (r < 0)
  477. goto release_pages;
  478. pinned += r;
  479. } while (pinned < ttm->num_pages);
  480. r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
  481. ttm->num_pages << PAGE_SHIFT,
  482. GFP_KERNEL);
  483. if (r)
  484. goto release_sg;
  485. r = -ENOMEM;
  486. nents = dma_map_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
  487. if (nents != ttm->sg->nents)
  488. goto release_sg;
  489. drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
  490. gtt->ttm.dma_address, ttm->num_pages);
  491. return 0;
  492. release_sg:
  493. kfree(ttm->sg);
  494. release_pages:
  495. release_pages(ttm->pages, pinned, 0);
  496. return r;
  497. }
  498. static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
  499. {
  500. struct amdgpu_device *adev = amdgpu_get_adev(ttm->bdev);
  501. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  502. struct sg_page_iter sg_iter;
  503. int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
  504. enum dma_data_direction direction = write ?
  505. DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
  506. /* double check that we don't free the table twice */
  507. if (!ttm->sg->sgl)
  508. return;
  509. /* free the sg table and pages again */
  510. dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
  511. for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) {
  512. struct page *page = sg_page_iter_page(&sg_iter);
  513. if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
  514. set_page_dirty(page);
  515. mark_page_accessed(page);
  516. page_cache_release(page);
  517. }
  518. sg_free_table(ttm->sg);
  519. }
  520. static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm,
  521. struct ttm_mem_reg *bo_mem)
  522. {
  523. struct amdgpu_ttm_tt *gtt = (void*)ttm;
  524. uint32_t flags = amdgpu_ttm_tt_pte_flags(gtt->adev, ttm, bo_mem);
  525. int r;
  526. if (gtt->userptr) {
  527. r = amdgpu_ttm_tt_pin_userptr(ttm);
  528. if (r) {
  529. DRM_ERROR("failed to pin userptr\n");
  530. return r;
  531. }
  532. }
  533. gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
  534. if (!ttm->num_pages) {
  535. WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
  536. ttm->num_pages, bo_mem, ttm);
  537. }
  538. if (bo_mem->mem_type == AMDGPU_PL_GDS ||
  539. bo_mem->mem_type == AMDGPU_PL_GWS ||
  540. bo_mem->mem_type == AMDGPU_PL_OA)
  541. return -EINVAL;
  542. r = amdgpu_gart_bind(gtt->adev, gtt->offset, ttm->num_pages,
  543. ttm->pages, gtt->ttm.dma_address, flags);
  544. if (r) {
  545. DRM_ERROR("failed to bind %lu pages at 0x%08X\n",
  546. ttm->num_pages, (unsigned)gtt->offset);
  547. return r;
  548. }
  549. return 0;
  550. }
  551. static int amdgpu_ttm_backend_unbind(struct ttm_tt *ttm)
  552. {
  553. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  554. /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
  555. if (gtt->adev->gart.ready)
  556. amdgpu_gart_unbind(gtt->adev, gtt->offset, ttm->num_pages);
  557. if (gtt->userptr)
  558. amdgpu_ttm_tt_unpin_userptr(ttm);
  559. return 0;
  560. }
  561. static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
  562. {
  563. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  564. ttm_dma_tt_fini(&gtt->ttm);
  565. kfree(gtt);
  566. }
  567. static struct ttm_backend_func amdgpu_backend_func = {
  568. .bind = &amdgpu_ttm_backend_bind,
  569. .unbind = &amdgpu_ttm_backend_unbind,
  570. .destroy = &amdgpu_ttm_backend_destroy,
  571. };
  572. static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_bo_device *bdev,
  573. unsigned long size, uint32_t page_flags,
  574. struct page *dummy_read_page)
  575. {
  576. struct amdgpu_device *adev;
  577. struct amdgpu_ttm_tt *gtt;
  578. adev = amdgpu_get_adev(bdev);
  579. gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
  580. if (gtt == NULL) {
  581. return NULL;
  582. }
  583. gtt->ttm.ttm.func = &amdgpu_backend_func;
  584. gtt->adev = adev;
  585. if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags, dummy_read_page)) {
  586. kfree(gtt);
  587. return NULL;
  588. }
  589. return &gtt->ttm.ttm;
  590. }
  591. static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm)
  592. {
  593. struct amdgpu_device *adev;
  594. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  595. unsigned i;
  596. int r;
  597. bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  598. if (ttm->state != tt_unpopulated)
  599. return 0;
  600. if (gtt && gtt->userptr) {
  601. ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
  602. if (!ttm->sg)
  603. return -ENOMEM;
  604. ttm->page_flags |= TTM_PAGE_FLAG_SG;
  605. ttm->state = tt_unbound;
  606. return 0;
  607. }
  608. if (slave && ttm->sg) {
  609. drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
  610. gtt->ttm.dma_address, ttm->num_pages);
  611. ttm->state = tt_unbound;
  612. return 0;
  613. }
  614. adev = amdgpu_get_adev(ttm->bdev);
  615. #ifdef CONFIG_SWIOTLB
  616. if (swiotlb_nr_tbl()) {
  617. return ttm_dma_populate(&gtt->ttm, adev->dev);
  618. }
  619. #endif
  620. r = ttm_pool_populate(ttm);
  621. if (r) {
  622. return r;
  623. }
  624. for (i = 0; i < ttm->num_pages; i++) {
  625. gtt->ttm.dma_address[i] = pci_map_page(adev->pdev, ttm->pages[i],
  626. 0, PAGE_SIZE,
  627. PCI_DMA_BIDIRECTIONAL);
  628. if (pci_dma_mapping_error(adev->pdev, gtt->ttm.dma_address[i])) {
  629. while (i--) {
  630. pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
  631. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  632. gtt->ttm.dma_address[i] = 0;
  633. }
  634. ttm_pool_unpopulate(ttm);
  635. return -EFAULT;
  636. }
  637. }
  638. return 0;
  639. }
  640. static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
  641. {
  642. struct amdgpu_device *adev;
  643. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  644. unsigned i;
  645. bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  646. if (gtt && gtt->userptr) {
  647. kfree(ttm->sg);
  648. ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
  649. return;
  650. }
  651. if (slave)
  652. return;
  653. adev = amdgpu_get_adev(ttm->bdev);
  654. #ifdef CONFIG_SWIOTLB
  655. if (swiotlb_nr_tbl()) {
  656. ttm_dma_unpopulate(&gtt->ttm, adev->dev);
  657. return;
  658. }
  659. #endif
  660. for (i = 0; i < ttm->num_pages; i++) {
  661. if (gtt->ttm.dma_address[i]) {
  662. pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
  663. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  664. }
  665. }
  666. ttm_pool_unpopulate(ttm);
  667. }
  668. int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
  669. uint32_t flags)
  670. {
  671. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  672. if (gtt == NULL)
  673. return -EINVAL;
  674. gtt->userptr = addr;
  675. gtt->usermm = current->mm;
  676. gtt->userflags = flags;
  677. return 0;
  678. }
  679. bool amdgpu_ttm_tt_has_userptr(struct ttm_tt *ttm)
  680. {
  681. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  682. if (gtt == NULL)
  683. return false;
  684. return !!gtt->userptr;
  685. }
  686. bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
  687. unsigned long end)
  688. {
  689. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  690. unsigned long size;
  691. if (gtt == NULL)
  692. return false;
  693. if (gtt->ttm.ttm.state != tt_bound || !gtt->userptr)
  694. return false;
  695. size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE;
  696. if (gtt->userptr > end || gtt->userptr + size <= start)
  697. return false;
  698. return true;
  699. }
  700. bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
  701. {
  702. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  703. if (gtt == NULL)
  704. return false;
  705. return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
  706. }
  707. uint32_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
  708. struct ttm_mem_reg *mem)
  709. {
  710. uint32_t flags = 0;
  711. if (mem && mem->mem_type != TTM_PL_SYSTEM)
  712. flags |= AMDGPU_PTE_VALID;
  713. if (mem && mem->mem_type == TTM_PL_TT) {
  714. flags |= AMDGPU_PTE_SYSTEM;
  715. if (ttm->caching_state == tt_cached)
  716. flags |= AMDGPU_PTE_SNOOPED;
  717. }
  718. if (adev->asic_type >= CHIP_TONGA)
  719. flags |= AMDGPU_PTE_EXECUTABLE;
  720. flags |= AMDGPU_PTE_READABLE;
  721. if (!amdgpu_ttm_tt_is_readonly(ttm))
  722. flags |= AMDGPU_PTE_WRITEABLE;
  723. return flags;
  724. }
  725. static struct ttm_bo_driver amdgpu_bo_driver = {
  726. .ttm_tt_create = &amdgpu_ttm_tt_create,
  727. .ttm_tt_populate = &amdgpu_ttm_tt_populate,
  728. .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
  729. .invalidate_caches = &amdgpu_invalidate_caches,
  730. .init_mem_type = &amdgpu_init_mem_type,
  731. .evict_flags = &amdgpu_evict_flags,
  732. .move = &amdgpu_bo_move,
  733. .verify_access = &amdgpu_verify_access,
  734. .move_notify = &amdgpu_bo_move_notify,
  735. .fault_reserve_notify = &amdgpu_bo_fault_reserve_notify,
  736. .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
  737. .io_mem_free = &amdgpu_ttm_io_mem_free,
  738. };
  739. int amdgpu_ttm_init(struct amdgpu_device *adev)
  740. {
  741. int r;
  742. r = amdgpu_ttm_global_init(adev);
  743. if (r) {
  744. return r;
  745. }
  746. /* No others user of address space so set it to 0 */
  747. r = ttm_bo_device_init(&adev->mman.bdev,
  748. adev->mman.bo_global_ref.ref.object,
  749. &amdgpu_bo_driver,
  750. adev->ddev->anon_inode->i_mapping,
  751. DRM_FILE_PAGE_OFFSET,
  752. adev->need_dma32);
  753. if (r) {
  754. DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
  755. return r;
  756. }
  757. adev->mman.initialized = true;
  758. r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_VRAM,
  759. adev->mc.real_vram_size >> PAGE_SHIFT);
  760. if (r) {
  761. DRM_ERROR("Failed initializing VRAM heap.\n");
  762. return r;
  763. }
  764. /* Change the size here instead of the init above so only lpfn is affected */
  765. amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
  766. r = amdgpu_bo_create(adev, 256 * 1024, PAGE_SIZE, true,
  767. AMDGPU_GEM_DOMAIN_VRAM,
  768. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  769. NULL, NULL, &adev->stollen_vga_memory);
  770. if (r) {
  771. return r;
  772. }
  773. r = amdgpu_bo_reserve(adev->stollen_vga_memory, false);
  774. if (r)
  775. return r;
  776. r = amdgpu_bo_pin(adev->stollen_vga_memory, AMDGPU_GEM_DOMAIN_VRAM, NULL);
  777. amdgpu_bo_unreserve(adev->stollen_vga_memory);
  778. if (r) {
  779. amdgpu_bo_unref(&adev->stollen_vga_memory);
  780. return r;
  781. }
  782. DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
  783. (unsigned) (adev->mc.real_vram_size / (1024 * 1024)));
  784. r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_TT,
  785. adev->mc.gtt_size >> PAGE_SHIFT);
  786. if (r) {
  787. DRM_ERROR("Failed initializing GTT heap.\n");
  788. return r;
  789. }
  790. DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
  791. (unsigned)(adev->mc.gtt_size / (1024 * 1024)));
  792. adev->gds.mem.total_size = adev->gds.mem.total_size << AMDGPU_GDS_SHIFT;
  793. adev->gds.mem.gfx_partition_size = adev->gds.mem.gfx_partition_size << AMDGPU_GDS_SHIFT;
  794. adev->gds.mem.cs_partition_size = adev->gds.mem.cs_partition_size << AMDGPU_GDS_SHIFT;
  795. adev->gds.gws.total_size = adev->gds.gws.total_size << AMDGPU_GWS_SHIFT;
  796. adev->gds.gws.gfx_partition_size = adev->gds.gws.gfx_partition_size << AMDGPU_GWS_SHIFT;
  797. adev->gds.gws.cs_partition_size = adev->gds.gws.cs_partition_size << AMDGPU_GWS_SHIFT;
  798. adev->gds.oa.total_size = adev->gds.oa.total_size << AMDGPU_OA_SHIFT;
  799. adev->gds.oa.gfx_partition_size = adev->gds.oa.gfx_partition_size << AMDGPU_OA_SHIFT;
  800. adev->gds.oa.cs_partition_size = adev->gds.oa.cs_partition_size << AMDGPU_OA_SHIFT;
  801. /* GDS Memory */
  802. r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GDS,
  803. adev->gds.mem.total_size >> PAGE_SHIFT);
  804. if (r) {
  805. DRM_ERROR("Failed initializing GDS heap.\n");
  806. return r;
  807. }
  808. /* GWS */
  809. r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GWS,
  810. adev->gds.gws.total_size >> PAGE_SHIFT);
  811. if (r) {
  812. DRM_ERROR("Failed initializing gws heap.\n");
  813. return r;
  814. }
  815. /* OA */
  816. r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_OA,
  817. adev->gds.oa.total_size >> PAGE_SHIFT);
  818. if (r) {
  819. DRM_ERROR("Failed initializing oa heap.\n");
  820. return r;
  821. }
  822. r = amdgpu_ttm_debugfs_init(adev);
  823. if (r) {
  824. DRM_ERROR("Failed to init debugfs\n");
  825. return r;
  826. }
  827. return 0;
  828. }
  829. void amdgpu_ttm_fini(struct amdgpu_device *adev)
  830. {
  831. int r;
  832. if (!adev->mman.initialized)
  833. return;
  834. amdgpu_ttm_debugfs_fini(adev);
  835. if (adev->stollen_vga_memory) {
  836. r = amdgpu_bo_reserve(adev->stollen_vga_memory, false);
  837. if (r == 0) {
  838. amdgpu_bo_unpin(adev->stollen_vga_memory);
  839. amdgpu_bo_unreserve(adev->stollen_vga_memory);
  840. }
  841. amdgpu_bo_unref(&adev->stollen_vga_memory);
  842. }
  843. ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_VRAM);
  844. ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_TT);
  845. ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GDS);
  846. ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS);
  847. ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA);
  848. ttm_bo_device_release(&adev->mman.bdev);
  849. amdgpu_gart_fini(adev);
  850. amdgpu_ttm_global_fini(adev);
  851. adev->mman.initialized = false;
  852. DRM_INFO("amdgpu: ttm finalized\n");
  853. }
  854. /* this should only be called at bootup or when userspace
  855. * isn't running */
  856. void amdgpu_ttm_set_active_vram_size(struct amdgpu_device *adev, u64 size)
  857. {
  858. struct ttm_mem_type_manager *man;
  859. if (!adev->mman.initialized)
  860. return;
  861. man = &adev->mman.bdev.man[TTM_PL_VRAM];
  862. /* this just adjusts TTM size idea, which sets lpfn to the correct value */
  863. man->size = size >> PAGE_SHIFT;
  864. }
  865. int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
  866. {
  867. struct drm_file *file_priv;
  868. struct amdgpu_device *adev;
  869. if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
  870. return -EINVAL;
  871. file_priv = filp->private_data;
  872. adev = file_priv->minor->dev->dev_private;
  873. if (adev == NULL)
  874. return -EINVAL;
  875. return ttm_bo_mmap(filp, vma, &adev->mman.bdev);
  876. }
  877. int amdgpu_copy_buffer(struct amdgpu_ring *ring,
  878. uint64_t src_offset,
  879. uint64_t dst_offset,
  880. uint32_t byte_count,
  881. struct reservation_object *resv,
  882. struct fence **fence)
  883. {
  884. struct amdgpu_device *adev = ring->adev;
  885. uint32_t max_bytes;
  886. unsigned num_loops, num_dw;
  887. struct amdgpu_ib *ib;
  888. unsigned i;
  889. int r;
  890. max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
  891. num_loops = DIV_ROUND_UP(byte_count, max_bytes);
  892. num_dw = num_loops * adev->mman.buffer_funcs->copy_num_dw;
  893. /* for IB padding */
  894. while (num_dw & 0x7)
  895. num_dw++;
  896. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  897. if (!ib)
  898. return -ENOMEM;
  899. r = amdgpu_ib_get(ring, NULL, num_dw * 4, ib);
  900. if (r) {
  901. kfree(ib);
  902. return r;
  903. }
  904. ib->length_dw = 0;
  905. if (resv) {
  906. r = amdgpu_sync_resv(adev, &ib->sync, resv,
  907. AMDGPU_FENCE_OWNER_UNDEFINED);
  908. if (r) {
  909. DRM_ERROR("sync failed (%d).\n", r);
  910. goto error_free;
  911. }
  912. }
  913. for (i = 0; i < num_loops; i++) {
  914. uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
  915. amdgpu_emit_copy_buffer(adev, ib, src_offset, dst_offset,
  916. cur_size_in_bytes);
  917. src_offset += cur_size_in_bytes;
  918. dst_offset += cur_size_in_bytes;
  919. byte_count -= cur_size_in_bytes;
  920. }
  921. amdgpu_vm_pad_ib(adev, ib);
  922. WARN_ON(ib->length_dw > num_dw);
  923. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  924. &amdgpu_vm_free_job,
  925. AMDGPU_FENCE_OWNER_UNDEFINED,
  926. fence);
  927. if (r)
  928. goto error_free;
  929. if (!amdgpu_enable_scheduler) {
  930. amdgpu_ib_free(adev, ib);
  931. kfree(ib);
  932. }
  933. return 0;
  934. error_free:
  935. amdgpu_ib_free(adev, ib);
  936. kfree(ib);
  937. return r;
  938. }
  939. #if defined(CONFIG_DEBUG_FS)
  940. static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
  941. {
  942. struct drm_info_node *node = (struct drm_info_node *)m->private;
  943. unsigned ttm_pl = *(int *)node->info_ent->data;
  944. struct drm_device *dev = node->minor->dev;
  945. struct amdgpu_device *adev = dev->dev_private;
  946. struct drm_mm *mm = (struct drm_mm *)adev->mman.bdev.man[ttm_pl].priv;
  947. int ret;
  948. struct ttm_bo_global *glob = adev->mman.bdev.glob;
  949. spin_lock(&glob->lru_lock);
  950. ret = drm_mm_dump_table(m, mm);
  951. spin_unlock(&glob->lru_lock);
  952. if (ttm_pl == TTM_PL_VRAM)
  953. seq_printf(m, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
  954. adev->mman.bdev.man[ttm_pl].size,
  955. (u64)atomic64_read(&adev->vram_usage) >> 20,
  956. (u64)atomic64_read(&adev->vram_vis_usage) >> 20);
  957. return ret;
  958. }
  959. static int ttm_pl_vram = TTM_PL_VRAM;
  960. static int ttm_pl_tt = TTM_PL_TT;
  961. static struct drm_info_list amdgpu_ttm_debugfs_list[] = {
  962. {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, &ttm_pl_vram},
  963. {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, &ttm_pl_tt},
  964. {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
  965. #ifdef CONFIG_SWIOTLB
  966. {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
  967. #endif
  968. };
  969. static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
  970. size_t size, loff_t *pos)
  971. {
  972. struct amdgpu_device *adev = f->f_inode->i_private;
  973. ssize_t result = 0;
  974. int r;
  975. if (size & 0x3 || *pos & 0x3)
  976. return -EINVAL;
  977. if (*pos >= adev->mc.mc_vram_size)
  978. return -ENXIO;
  979. while (size) {
  980. unsigned long flags;
  981. uint32_t value;
  982. if (*pos >= adev->mc.mc_vram_size)
  983. return result;
  984. spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  985. WREG32(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
  986. WREG32(mmMM_INDEX_HI, *pos >> 31);
  987. value = RREG32(mmMM_DATA);
  988. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  989. r = put_user(value, (uint32_t *)buf);
  990. if (r)
  991. return r;
  992. result += 4;
  993. buf += 4;
  994. *pos += 4;
  995. size -= 4;
  996. }
  997. return result;
  998. }
  999. static const struct file_operations amdgpu_ttm_vram_fops = {
  1000. .owner = THIS_MODULE,
  1001. .read = amdgpu_ttm_vram_read,
  1002. .llseek = default_llseek
  1003. };
  1004. static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
  1005. size_t size, loff_t *pos)
  1006. {
  1007. struct amdgpu_device *adev = f->f_inode->i_private;
  1008. ssize_t result = 0;
  1009. int r;
  1010. while (size) {
  1011. loff_t p = *pos / PAGE_SIZE;
  1012. unsigned off = *pos & ~PAGE_MASK;
  1013. size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
  1014. struct page *page;
  1015. void *ptr;
  1016. if (p >= adev->gart.num_cpu_pages)
  1017. return result;
  1018. page = adev->gart.pages[p];
  1019. if (page) {
  1020. ptr = kmap(page);
  1021. ptr += off;
  1022. r = copy_to_user(buf, ptr, cur_size);
  1023. kunmap(adev->gart.pages[p]);
  1024. } else
  1025. r = clear_user(buf, cur_size);
  1026. if (r)
  1027. return -EFAULT;
  1028. result += cur_size;
  1029. buf += cur_size;
  1030. *pos += cur_size;
  1031. size -= cur_size;
  1032. }
  1033. return result;
  1034. }
  1035. static const struct file_operations amdgpu_ttm_gtt_fops = {
  1036. .owner = THIS_MODULE,
  1037. .read = amdgpu_ttm_gtt_read,
  1038. .llseek = default_llseek
  1039. };
  1040. #endif
  1041. static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
  1042. {
  1043. #if defined(CONFIG_DEBUG_FS)
  1044. unsigned count;
  1045. struct drm_minor *minor = adev->ddev->primary;
  1046. struct dentry *ent, *root = minor->debugfs_root;
  1047. ent = debugfs_create_file("amdgpu_vram", S_IFREG | S_IRUGO, root,
  1048. adev, &amdgpu_ttm_vram_fops);
  1049. if (IS_ERR(ent))
  1050. return PTR_ERR(ent);
  1051. i_size_write(ent->d_inode, adev->mc.mc_vram_size);
  1052. adev->mman.vram = ent;
  1053. ent = debugfs_create_file("amdgpu_gtt", S_IFREG | S_IRUGO, root,
  1054. adev, &amdgpu_ttm_gtt_fops);
  1055. if (IS_ERR(ent))
  1056. return PTR_ERR(ent);
  1057. i_size_write(ent->d_inode, adev->mc.gtt_size);
  1058. adev->mman.gtt = ent;
  1059. count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
  1060. #ifdef CONFIG_SWIOTLB
  1061. if (!swiotlb_nr_tbl())
  1062. --count;
  1063. #endif
  1064. return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);
  1065. #else
  1066. return 0;
  1067. #endif
  1068. }
  1069. static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev)
  1070. {
  1071. #if defined(CONFIG_DEBUG_FS)
  1072. debugfs_remove(adev->mman.vram);
  1073. adev->mman.vram = NULL;
  1074. debugfs_remove(adev->mman.gtt);
  1075. adev->mman.gtt = NULL;
  1076. #endif
  1077. }