amdgpu_object.c 24 KB

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