amdgpu_object.c 25 KB

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