amdgpu_object.c 25 KB

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