amdgpu_object.c 25 KB

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