amdgpu_object.c 25 KB

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