amdgpu_object.c 26 KB

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