amdgpu_object.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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 <linux/list.h>
  33. #include <linux/slab.h>
  34. #include <drm/drmP.h>
  35. #include <drm/amdgpu_drm.h>
  36. #include <drm/drm_cache.h>
  37. #include "amdgpu.h"
  38. #include "amdgpu_trace.h"
  39. static u64 amdgpu_get_vis_part_size(struct amdgpu_device *adev,
  40. struct ttm_mem_reg *mem)
  41. {
  42. if (mem->start << PAGE_SHIFT >= adev->mc.visible_vram_size)
  43. return 0;
  44. return ((mem->start << PAGE_SHIFT) + mem->size) >
  45. adev->mc.visible_vram_size ?
  46. adev->mc.visible_vram_size - (mem->start << PAGE_SHIFT) :
  47. mem->size;
  48. }
  49. static void amdgpu_update_memory_usage(struct amdgpu_device *adev,
  50. struct ttm_mem_reg *old_mem,
  51. struct ttm_mem_reg *new_mem)
  52. {
  53. u64 vis_size;
  54. if (!adev)
  55. return;
  56. if (new_mem) {
  57. switch (new_mem->mem_type) {
  58. case TTM_PL_TT:
  59. atomic64_add(new_mem->size, &adev->gtt_usage);
  60. break;
  61. case TTM_PL_VRAM:
  62. atomic64_add(new_mem->size, &adev->vram_usage);
  63. vis_size = amdgpu_get_vis_part_size(adev, new_mem);
  64. atomic64_add(vis_size, &adev->vram_vis_usage);
  65. break;
  66. }
  67. }
  68. if (old_mem) {
  69. switch (old_mem->mem_type) {
  70. case TTM_PL_TT:
  71. atomic64_sub(old_mem->size, &adev->gtt_usage);
  72. break;
  73. case TTM_PL_VRAM:
  74. atomic64_sub(old_mem->size, &adev->vram_usage);
  75. vis_size = amdgpu_get_vis_part_size(adev, old_mem);
  76. atomic64_sub(vis_size, &adev->vram_vis_usage);
  77. break;
  78. }
  79. }
  80. }
  81. static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
  82. {
  83. struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
  84. struct amdgpu_bo *bo;
  85. bo = container_of(tbo, struct amdgpu_bo, tbo);
  86. amdgpu_update_memory_usage(adev, &bo->tbo.mem, NULL);
  87. drm_gem_object_release(&bo->gem_base);
  88. amdgpu_bo_unref(&bo->parent);
  89. if (!list_empty(&bo->shadow_list)) {
  90. mutex_lock(&adev->shadow_list_lock);
  91. list_del_init(&bo->shadow_list);
  92. mutex_unlock(&adev->shadow_list_lock);
  93. }
  94. kfree(bo->metadata);
  95. kfree(bo);
  96. }
  97. bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
  98. {
  99. if (bo->destroy == &amdgpu_ttm_bo_destroy)
  100. return true;
  101. return false;
  102. }
  103. static void amdgpu_ttm_placement_init(struct amdgpu_device *adev,
  104. struct ttm_placement *placement,
  105. struct ttm_place *places,
  106. u32 domain, u64 flags)
  107. {
  108. u32 c = 0;
  109. if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
  110. unsigned visible_pfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
  111. unsigned lpfn = 0;
  112. /* This forces a reallocation if the flag wasn't set before */
  113. if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
  114. lpfn = adev->mc.real_vram_size >> PAGE_SHIFT;
  115. places[c].fpfn = 0;
  116. places[c].lpfn = lpfn;
  117. places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
  118. TTM_PL_FLAG_VRAM;
  119. if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
  120. places[c].lpfn = visible_pfn;
  121. else
  122. places[c].flags |= TTM_PL_FLAG_TOPDOWN;
  123. c++;
  124. }
  125. if (domain & AMDGPU_GEM_DOMAIN_GTT) {
  126. places[c].fpfn = 0;
  127. places[c].lpfn = 0;
  128. places[c].flags = TTM_PL_FLAG_TT;
  129. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  130. places[c].flags |= TTM_PL_FLAG_WC |
  131. TTM_PL_FLAG_UNCACHED;
  132. else
  133. places[c].flags |= TTM_PL_FLAG_CACHED;
  134. c++;
  135. }
  136. if (domain & AMDGPU_GEM_DOMAIN_CPU) {
  137. places[c].fpfn = 0;
  138. places[c].lpfn = 0;
  139. places[c].flags = TTM_PL_FLAG_SYSTEM;
  140. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  141. places[c].flags |= TTM_PL_FLAG_WC |
  142. TTM_PL_FLAG_UNCACHED;
  143. else
  144. places[c].flags |= TTM_PL_FLAG_CACHED;
  145. c++;
  146. }
  147. if (domain & AMDGPU_GEM_DOMAIN_GDS) {
  148. places[c].fpfn = 0;
  149. places[c].lpfn = 0;
  150. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
  151. c++;
  152. }
  153. if (domain & AMDGPU_GEM_DOMAIN_GWS) {
  154. places[c].fpfn = 0;
  155. places[c].lpfn = 0;
  156. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
  157. c++;
  158. }
  159. if (domain & AMDGPU_GEM_DOMAIN_OA) {
  160. places[c].fpfn = 0;
  161. places[c].lpfn = 0;
  162. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
  163. c++;
  164. }
  165. if (!c) {
  166. places[c].fpfn = 0;
  167. places[c].lpfn = 0;
  168. places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  169. c++;
  170. }
  171. placement->num_placement = c;
  172. placement->placement = places;
  173. placement->num_busy_placement = c;
  174. placement->busy_placement = places;
  175. }
  176. void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain)
  177. {
  178. struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
  179. amdgpu_ttm_placement_init(adev, &abo->placement, abo->placements,
  180. domain, abo->flags);
  181. }
  182. static void amdgpu_fill_placement_to_bo(struct amdgpu_bo *bo,
  183. struct ttm_placement *placement)
  184. {
  185. BUG_ON(placement->num_placement > (AMDGPU_GEM_DOMAIN_MAX + 1));
  186. memcpy(bo->placements, placement->placement,
  187. placement->num_placement * sizeof(struct ttm_place));
  188. bo->placement.num_placement = placement->num_placement;
  189. bo->placement.num_busy_placement = placement->num_busy_placement;
  190. bo->placement.placement = bo->placements;
  191. bo->placement.busy_placement = bo->placements;
  192. }
  193. /**
  194. * amdgpu_bo_create_kernel - create BO for kernel use
  195. *
  196. * @adev: amdgpu device object
  197. * @size: size for the new BO
  198. * @align: alignment for the new BO
  199. * @domain: where to place it
  200. * @bo_ptr: resulting BO
  201. * @gpu_addr: GPU addr of the pinned BO
  202. * @cpu_addr: optional CPU address mapping
  203. *
  204. * Allocates and pins a BO for kernel internal use.
  205. *
  206. * Returns 0 on success, negative error code otherwise.
  207. */
  208. int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
  209. unsigned long size, int align,
  210. u32 domain, struct amdgpu_bo **bo_ptr,
  211. u64 *gpu_addr, void **cpu_addr)
  212. {
  213. int r;
  214. r = amdgpu_bo_create(adev, size, align, true, domain,
  215. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  216. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  217. NULL, NULL, bo_ptr);
  218. if (r) {
  219. dev_err(adev->dev, "(%d) failed to allocate kernel bo\n", r);
  220. return r;
  221. }
  222. r = amdgpu_bo_reserve(*bo_ptr, false);
  223. if (r) {
  224. dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
  225. goto error_free;
  226. }
  227. r = amdgpu_bo_pin(*bo_ptr, domain, gpu_addr);
  228. if (r) {
  229. dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
  230. goto error_unreserve;
  231. }
  232. if (cpu_addr) {
  233. r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
  234. if (r) {
  235. dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
  236. goto error_unreserve;
  237. }
  238. }
  239. amdgpu_bo_unreserve(*bo_ptr);
  240. return 0;
  241. error_unreserve:
  242. amdgpu_bo_unreserve(*bo_ptr);
  243. error_free:
  244. amdgpu_bo_unref(bo_ptr);
  245. return r;
  246. }
  247. /**
  248. * amdgpu_bo_free_kernel - free BO for kernel use
  249. *
  250. * @bo: amdgpu BO to free
  251. *
  252. * unmaps and unpin a BO for kernel internal use.
  253. */
  254. void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
  255. void **cpu_addr)
  256. {
  257. if (*bo == NULL)
  258. return;
  259. if (likely(amdgpu_bo_reserve(*bo, false) == 0)) {
  260. if (cpu_addr)
  261. amdgpu_bo_kunmap(*bo);
  262. amdgpu_bo_unpin(*bo);
  263. amdgpu_bo_unreserve(*bo);
  264. }
  265. amdgpu_bo_unref(bo);
  266. if (gpu_addr)
  267. *gpu_addr = 0;
  268. if (cpu_addr)
  269. *cpu_addr = NULL;
  270. }
  271. int amdgpu_bo_create_restricted(struct amdgpu_device *adev,
  272. unsigned long size, int byte_align,
  273. bool kernel, u32 domain, u64 flags,
  274. struct sg_table *sg,
  275. struct ttm_placement *placement,
  276. struct reservation_object *resv,
  277. struct amdgpu_bo **bo_ptr)
  278. {
  279. struct amdgpu_bo *bo;
  280. enum ttm_bo_type type;
  281. unsigned long page_align;
  282. u64 initial_bytes_moved;
  283. size_t acc_size;
  284. int r;
  285. page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
  286. size = ALIGN(size, PAGE_SIZE);
  287. if (kernel) {
  288. type = ttm_bo_type_kernel;
  289. } else if (sg) {
  290. type = ttm_bo_type_sg;
  291. } else {
  292. type = ttm_bo_type_device;
  293. }
  294. *bo_ptr = NULL;
  295. acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
  296. sizeof(struct amdgpu_bo));
  297. bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
  298. if (bo == NULL)
  299. return -ENOMEM;
  300. r = drm_gem_object_init(adev->ddev, &bo->gem_base, size);
  301. if (unlikely(r)) {
  302. kfree(bo);
  303. return r;
  304. }
  305. INIT_LIST_HEAD(&bo->shadow_list);
  306. INIT_LIST_HEAD(&bo->va);
  307. bo->prefered_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM |
  308. AMDGPU_GEM_DOMAIN_GTT |
  309. AMDGPU_GEM_DOMAIN_CPU |
  310. AMDGPU_GEM_DOMAIN_GDS |
  311. AMDGPU_GEM_DOMAIN_GWS |
  312. AMDGPU_GEM_DOMAIN_OA);
  313. bo->allowed_domains = bo->prefered_domains;
  314. if (!kernel && bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
  315. bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
  316. bo->flags = flags;
  317. #ifdef CONFIG_X86_32
  318. /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
  319. * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
  320. */
  321. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  322. #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
  323. /* Don't try to enable write-combining when it can't work, or things
  324. * may be slow
  325. * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
  326. */
  327. #ifndef CONFIG_COMPILE_TEST
  328. #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
  329. thanks to write-combining
  330. #endif
  331. if (bo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  332. DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
  333. "better performance thanks to write-combining\n");
  334. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  335. #else
  336. /* For architectures that don't support WC memory,
  337. * mask out the WC flag from the BO
  338. */
  339. if (!drm_arch_can_wc_memory())
  340. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  341. #endif
  342. amdgpu_fill_placement_to_bo(bo, placement);
  343. /* Kernel allocation are uninterruptible */
  344. initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
  345. r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, type,
  346. &bo->placement, page_align, !kernel, NULL,
  347. acc_size, sg, resv, &amdgpu_ttm_bo_destroy);
  348. amdgpu_cs_report_moved_bytes(adev,
  349. atomic64_read(&adev->num_bytes_moved) - initial_bytes_moved);
  350. if (unlikely(r != 0))
  351. return r;
  352. if (kernel)
  353. bo->tbo.priority = 1;
  354. if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
  355. bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
  356. struct dma_fence *fence;
  357. r = amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence);
  358. if (unlikely(r))
  359. goto fail_unreserve;
  360. amdgpu_bo_fence(bo, fence, false);
  361. dma_fence_put(bo->tbo.moving);
  362. bo->tbo.moving = dma_fence_get(fence);
  363. dma_fence_put(fence);
  364. }
  365. if (!resv)
  366. amdgpu_bo_unreserve(bo);
  367. *bo_ptr = bo;
  368. trace_amdgpu_bo_create(bo);
  369. return 0;
  370. fail_unreserve:
  371. if (!resv)
  372. ww_mutex_unlock(&bo->tbo.resv->lock);
  373. amdgpu_bo_unref(&bo);
  374. return r;
  375. }
  376. static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
  377. unsigned long size, int byte_align,
  378. struct amdgpu_bo *bo)
  379. {
  380. struct ttm_placement placement = {0};
  381. struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
  382. int r;
  383. if (bo->shadow)
  384. return 0;
  385. bo->flags |= AMDGPU_GEM_CREATE_SHADOW;
  386. memset(&placements, 0,
  387. (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
  388. amdgpu_ttm_placement_init(adev, &placement,
  389. placements, AMDGPU_GEM_DOMAIN_GTT,
  390. AMDGPU_GEM_CREATE_CPU_GTT_USWC);
  391. r = amdgpu_bo_create_restricted(adev, size, byte_align, true,
  392. AMDGPU_GEM_DOMAIN_GTT,
  393. AMDGPU_GEM_CREATE_CPU_GTT_USWC,
  394. NULL, &placement,
  395. bo->tbo.resv,
  396. &bo->shadow);
  397. if (!r) {
  398. bo->shadow->parent = amdgpu_bo_ref(bo);
  399. mutex_lock(&adev->shadow_list_lock);
  400. list_add_tail(&bo->shadow_list, &adev->shadow_list);
  401. mutex_unlock(&adev->shadow_list_lock);
  402. }
  403. return r;
  404. }
  405. int amdgpu_bo_create(struct amdgpu_device *adev,
  406. unsigned long size, int byte_align,
  407. bool kernel, u32 domain, u64 flags,
  408. struct sg_table *sg,
  409. struct reservation_object *resv,
  410. struct amdgpu_bo **bo_ptr)
  411. {
  412. struct ttm_placement placement = {0};
  413. struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
  414. int r;
  415. memset(&placements, 0,
  416. (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
  417. amdgpu_ttm_placement_init(adev, &placement,
  418. placements, domain, flags);
  419. r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
  420. domain, flags, sg, &placement,
  421. resv, bo_ptr);
  422. if (r)
  423. return r;
  424. if (amdgpu_need_backup(adev) && (flags & AMDGPU_GEM_CREATE_SHADOW)) {
  425. if (!resv) {
  426. r = ww_mutex_lock(&(*bo_ptr)->tbo.resv->lock, NULL);
  427. WARN_ON(r != 0);
  428. }
  429. r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
  430. if (!resv)
  431. ww_mutex_unlock(&(*bo_ptr)->tbo.resv->lock);
  432. if (r)
  433. amdgpu_bo_unref(bo_ptr);
  434. }
  435. return r;
  436. }
  437. int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
  438. struct amdgpu_ring *ring,
  439. struct amdgpu_bo *bo,
  440. struct reservation_object *resv,
  441. struct dma_fence **fence,
  442. bool direct)
  443. {
  444. struct amdgpu_bo *shadow = bo->shadow;
  445. uint64_t bo_addr, shadow_addr;
  446. int r;
  447. if (!shadow)
  448. return -EINVAL;
  449. bo_addr = amdgpu_bo_gpu_offset(bo);
  450. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  451. r = reservation_object_reserve_shared(bo->tbo.resv);
  452. if (r)
  453. goto err;
  454. r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
  455. amdgpu_bo_size(bo), resv, fence,
  456. direct);
  457. if (!r)
  458. amdgpu_bo_fence(bo, *fence, true);
  459. err:
  460. return r;
  461. }
  462. int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
  463. struct amdgpu_ring *ring,
  464. struct amdgpu_bo *bo,
  465. struct reservation_object *resv,
  466. struct dma_fence **fence,
  467. bool direct)
  468. {
  469. struct amdgpu_bo *shadow = bo->shadow;
  470. uint64_t bo_addr, shadow_addr;
  471. int r;
  472. if (!shadow)
  473. return -EINVAL;
  474. bo_addr = amdgpu_bo_gpu_offset(bo);
  475. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  476. r = reservation_object_reserve_shared(bo->tbo.resv);
  477. if (r)
  478. goto err;
  479. r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
  480. amdgpu_bo_size(bo), resv, fence,
  481. direct);
  482. if (!r)
  483. amdgpu_bo_fence(bo, *fence, true);
  484. err:
  485. return r;
  486. }
  487. int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
  488. {
  489. bool is_iomem;
  490. long r;
  491. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  492. return -EPERM;
  493. if (bo->kptr) {
  494. if (ptr) {
  495. *ptr = bo->kptr;
  496. }
  497. return 0;
  498. }
  499. r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
  500. MAX_SCHEDULE_TIMEOUT);
  501. if (r < 0)
  502. return r;
  503. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  504. if (r)
  505. return r;
  506. bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  507. if (ptr)
  508. *ptr = bo->kptr;
  509. return 0;
  510. }
  511. void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
  512. {
  513. if (bo->kptr == NULL)
  514. return;
  515. bo->kptr = NULL;
  516. ttm_bo_kunmap(&bo->kmap);
  517. }
  518. struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
  519. {
  520. if (bo == NULL)
  521. return NULL;
  522. ttm_bo_reference(&bo->tbo);
  523. return bo;
  524. }
  525. void amdgpu_bo_unref(struct amdgpu_bo **bo)
  526. {
  527. struct ttm_buffer_object *tbo;
  528. if ((*bo) == NULL)
  529. return;
  530. tbo = &((*bo)->tbo);
  531. ttm_bo_unref(&tbo);
  532. if (tbo == NULL)
  533. *bo = NULL;
  534. }
  535. int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
  536. u64 min_offset, u64 max_offset,
  537. u64 *gpu_addr)
  538. {
  539. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  540. int r, i;
  541. unsigned fpfn, lpfn;
  542. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
  543. return -EPERM;
  544. if (WARN_ON_ONCE(min_offset > max_offset))
  545. return -EINVAL;
  546. if (bo->pin_count) {
  547. uint32_t mem_type = bo->tbo.mem.mem_type;
  548. if (domain != amdgpu_mem_type_to_domain(mem_type))
  549. return -EINVAL;
  550. bo->pin_count++;
  551. if (gpu_addr)
  552. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  553. if (max_offset != 0) {
  554. u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
  555. WARN_ON_ONCE(max_offset <
  556. (amdgpu_bo_gpu_offset(bo) - domain_start));
  557. }
  558. return 0;
  559. }
  560. bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  561. amdgpu_ttm_placement_from_domain(bo, domain);
  562. for (i = 0; i < bo->placement.num_placement; i++) {
  563. /* force to pin into visible video ram */
  564. if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
  565. !(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) &&
  566. (!max_offset || max_offset >
  567. adev->mc.visible_vram_size)) {
  568. if (WARN_ON_ONCE(min_offset >
  569. adev->mc.visible_vram_size))
  570. return -EINVAL;
  571. fpfn = min_offset >> PAGE_SHIFT;
  572. lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
  573. } else {
  574. fpfn = min_offset >> PAGE_SHIFT;
  575. lpfn = max_offset >> PAGE_SHIFT;
  576. }
  577. if (fpfn > bo->placements[i].fpfn)
  578. bo->placements[i].fpfn = fpfn;
  579. if (!bo->placements[i].lpfn ||
  580. (lpfn && lpfn < bo->placements[i].lpfn))
  581. bo->placements[i].lpfn = lpfn;
  582. bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
  583. }
  584. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  585. if (unlikely(r)) {
  586. dev_err(adev->dev, "%p pin failed\n", bo);
  587. goto error;
  588. }
  589. r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
  590. if (unlikely(r)) {
  591. dev_err(adev->dev, "%p bind failed\n", bo);
  592. goto error;
  593. }
  594. bo->pin_count = 1;
  595. if (gpu_addr != NULL)
  596. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  597. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  598. adev->vram_pin_size += amdgpu_bo_size(bo);
  599. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  600. adev->invisible_pin_size += amdgpu_bo_size(bo);
  601. } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
  602. adev->gart_pin_size += amdgpu_bo_size(bo);
  603. }
  604. error:
  605. return r;
  606. }
  607. int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
  608. {
  609. return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
  610. }
  611. int amdgpu_bo_unpin(struct amdgpu_bo *bo)
  612. {
  613. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  614. int r, i;
  615. if (!bo->pin_count) {
  616. dev_warn(adev->dev, "%p unpin not necessary\n", bo);
  617. return 0;
  618. }
  619. bo->pin_count--;
  620. if (bo->pin_count)
  621. return 0;
  622. for (i = 0; i < bo->placement.num_placement; i++) {
  623. bo->placements[i].lpfn = 0;
  624. bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
  625. }
  626. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  627. if (unlikely(r)) {
  628. dev_err(adev->dev, "%p validate failed for unpin\n", bo);
  629. goto error;
  630. }
  631. if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
  632. adev->vram_pin_size -= amdgpu_bo_size(bo);
  633. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  634. adev->invisible_pin_size -= amdgpu_bo_size(bo);
  635. } else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
  636. adev->gart_pin_size -= amdgpu_bo_size(bo);
  637. }
  638. error:
  639. return r;
  640. }
  641. int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
  642. {
  643. /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
  644. if (0 && (adev->flags & AMD_IS_APU)) {
  645. /* Useless to evict on IGP chips */
  646. return 0;
  647. }
  648. return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
  649. }
  650. static const char *amdgpu_vram_names[] = {
  651. "UNKNOWN",
  652. "GDDR1",
  653. "DDR2",
  654. "GDDR3",
  655. "GDDR4",
  656. "GDDR5",
  657. "HBM",
  658. "DDR3"
  659. };
  660. int amdgpu_bo_init(struct amdgpu_device *adev)
  661. {
  662. /* reserve PAT memory space to WC for VRAM */
  663. arch_io_reserve_memtype_wc(adev->mc.aper_base,
  664. adev->mc.aper_size);
  665. /* Add an MTRR for the VRAM */
  666. adev->mc.vram_mtrr = arch_phys_wc_add(adev->mc.aper_base,
  667. adev->mc.aper_size);
  668. DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
  669. adev->mc.mc_vram_size >> 20,
  670. (unsigned long long)adev->mc.aper_size >> 20);
  671. DRM_INFO("RAM width %dbits %s\n",
  672. adev->mc.vram_width, amdgpu_vram_names[adev->mc.vram_type]);
  673. return amdgpu_ttm_init(adev);
  674. }
  675. void amdgpu_bo_fini(struct amdgpu_device *adev)
  676. {
  677. amdgpu_ttm_fini(adev);
  678. arch_phys_wc_del(adev->mc.vram_mtrr);
  679. arch_io_free_memtype_wc(adev->mc.aper_base, adev->mc.aper_size);
  680. }
  681. int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
  682. struct vm_area_struct *vma)
  683. {
  684. return ttm_fbdev_mmap(vma, &bo->tbo);
  685. }
  686. int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
  687. {
  688. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  689. if (adev->family <= AMDGPU_FAMILY_CZ &&
  690. AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
  691. return -EINVAL;
  692. bo->tiling_flags = tiling_flags;
  693. return 0;
  694. }
  695. void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
  696. {
  697. lockdep_assert_held(&bo->tbo.resv->lock.base);
  698. if (tiling_flags)
  699. *tiling_flags = bo->tiling_flags;
  700. }
  701. int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
  702. uint32_t metadata_size, uint64_t flags)
  703. {
  704. void *buffer;
  705. if (!metadata_size) {
  706. if (bo->metadata_size) {
  707. kfree(bo->metadata);
  708. bo->metadata = NULL;
  709. bo->metadata_size = 0;
  710. }
  711. return 0;
  712. }
  713. if (metadata == NULL)
  714. return -EINVAL;
  715. buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
  716. if (buffer == NULL)
  717. return -ENOMEM;
  718. kfree(bo->metadata);
  719. bo->metadata_flags = flags;
  720. bo->metadata = buffer;
  721. bo->metadata_size = metadata_size;
  722. return 0;
  723. }
  724. int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
  725. size_t buffer_size, uint32_t *metadata_size,
  726. uint64_t *flags)
  727. {
  728. if (!buffer && !metadata_size)
  729. return -EINVAL;
  730. if (buffer) {
  731. if (buffer_size < bo->metadata_size)
  732. return -EINVAL;
  733. if (bo->metadata_size)
  734. memcpy(buffer, bo->metadata, bo->metadata_size);
  735. }
  736. if (metadata_size)
  737. *metadata_size = bo->metadata_size;
  738. if (flags)
  739. *flags = bo->metadata_flags;
  740. return 0;
  741. }
  742. void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
  743. bool evict,
  744. struct ttm_mem_reg *new_mem)
  745. {
  746. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  747. struct amdgpu_bo *abo;
  748. struct ttm_mem_reg *old_mem = &bo->mem;
  749. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  750. return;
  751. abo = container_of(bo, struct amdgpu_bo, tbo);
  752. amdgpu_vm_bo_invalidate(adev, abo);
  753. /* remember the eviction */
  754. if (evict)
  755. atomic64_inc(&adev->num_evictions);
  756. /* update statistics */
  757. if (!new_mem)
  758. return;
  759. /* move_notify is called before move happens */
  760. amdgpu_update_memory_usage(adev, &bo->mem, new_mem);
  761. trace_amdgpu_ttm_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
  762. }
  763. int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  764. {
  765. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  766. struct amdgpu_bo *abo;
  767. unsigned long offset, size, lpfn;
  768. int i, r;
  769. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  770. return 0;
  771. abo = container_of(bo, struct amdgpu_bo, tbo);
  772. if (bo->mem.mem_type != TTM_PL_VRAM)
  773. return 0;
  774. size = bo->mem.num_pages << PAGE_SHIFT;
  775. offset = bo->mem.start << PAGE_SHIFT;
  776. /* TODO: figure out how to map scattered VRAM to the CPU */
  777. if ((offset + size) <= adev->mc.visible_vram_size &&
  778. (abo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS))
  779. return 0;
  780. /* Can't move a pinned BO to visible VRAM */
  781. if (abo->pin_count > 0)
  782. return -EINVAL;
  783. /* hurrah the memory is not visible ! */
  784. abo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  785. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM);
  786. lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
  787. for (i = 0; i < abo->placement.num_placement; i++) {
  788. /* Force into visible VRAM */
  789. if ((abo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
  790. (!abo->placements[i].lpfn ||
  791. abo->placements[i].lpfn > lpfn))
  792. abo->placements[i].lpfn = lpfn;
  793. }
  794. r = ttm_bo_validate(bo, &abo->placement, false, false);
  795. if (unlikely(r == -ENOMEM)) {
  796. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
  797. return ttm_bo_validate(bo, &abo->placement, false, false);
  798. } else if (unlikely(r != 0)) {
  799. return r;
  800. }
  801. offset = bo->mem.start << PAGE_SHIFT;
  802. /* this should never happen */
  803. if ((offset + size) > adev->mc.visible_vram_size)
  804. return -EINVAL;
  805. return 0;
  806. }
  807. /**
  808. * amdgpu_bo_fence - add fence to buffer object
  809. *
  810. * @bo: buffer object in question
  811. * @fence: fence to add
  812. * @shared: true if fence should be added shared
  813. *
  814. */
  815. void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
  816. bool shared)
  817. {
  818. struct reservation_object *resv = bo->tbo.resv;
  819. if (shared)
  820. reservation_object_add_shared_fence(resv, fence);
  821. else
  822. reservation_object_add_excl_fence(resv, fence);
  823. }
  824. /**
  825. * amdgpu_bo_gpu_offset - return GPU offset of bo
  826. * @bo: amdgpu object for which we query the offset
  827. *
  828. * Returns current GPU offset of the object.
  829. *
  830. * Note: object should either be pinned or reserved when calling this
  831. * function, it might be useful to add check for this for debugging.
  832. */
  833. u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
  834. {
  835. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
  836. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT &&
  837. !amdgpu_ttm_is_bound(bo->tbo.ttm));
  838. WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
  839. !bo->pin_count);
  840. WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
  841. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM &&
  842. !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
  843. return bo->tbo.offset;
  844. }