amdgpu_object.c 25 KB

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