amdgpu_object.c 25 KB

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