amdgpu_object.c 25 KB

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