amdgpu_object.c 24 KB

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