amdgpu_object.c 26 KB

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