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