amdgpu_object.c 25 KB

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