amdgpu_object.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. size_t acc_size;
  283. int r;
  284. page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
  285. size = ALIGN(size, PAGE_SIZE);
  286. if (kernel) {
  287. type = ttm_bo_type_kernel;
  288. } else if (sg) {
  289. type = ttm_bo_type_sg;
  290. } else {
  291. type = ttm_bo_type_device;
  292. }
  293. *bo_ptr = NULL;
  294. acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
  295. sizeof(struct amdgpu_bo));
  296. bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
  297. if (bo == NULL)
  298. return -ENOMEM;
  299. r = drm_gem_object_init(adev->ddev, &bo->gem_base, size);
  300. if (unlikely(r)) {
  301. kfree(bo);
  302. return r;
  303. }
  304. INIT_LIST_HEAD(&bo->shadow_list);
  305. INIT_LIST_HEAD(&bo->va);
  306. bo->prefered_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM |
  307. AMDGPU_GEM_DOMAIN_GTT |
  308. AMDGPU_GEM_DOMAIN_CPU |
  309. AMDGPU_GEM_DOMAIN_GDS |
  310. AMDGPU_GEM_DOMAIN_GWS |
  311. AMDGPU_GEM_DOMAIN_OA);
  312. bo->allowed_domains = bo->prefered_domains;
  313. if (!kernel && bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
  314. bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
  315. bo->flags = flags;
  316. /* For architectures that don't support WC memory,
  317. * mask out the WC flag from the BO
  318. */
  319. if (!drm_arch_can_wc_memory())
  320. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  321. amdgpu_fill_placement_to_bo(bo, placement);
  322. /* Kernel allocation are uninterruptible */
  323. r = ttm_bo_init(&adev->mman.bdev, &bo->tbo, size, type,
  324. &bo->placement, page_align, !kernel, NULL,
  325. acc_size, sg, resv, &amdgpu_ttm_bo_destroy);
  326. if (unlikely(r != 0)) {
  327. return r;
  328. }
  329. if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
  330. bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
  331. struct dma_fence *fence;
  332. r = amdgpu_bo_reserve(bo, false);
  333. if (unlikely(r != 0))
  334. goto fail_free;
  335. amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
  336. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  337. if (unlikely(r != 0))
  338. goto fail_unreserve;
  339. r = amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence);
  340. if (unlikely(r))
  341. goto fail_unreserve;
  342. amdgpu_bo_fence(bo, fence, false);
  343. amdgpu_bo_unreserve(bo);
  344. dma_fence_put(bo->tbo.moving);
  345. bo->tbo.moving = dma_fence_get(fence);
  346. dma_fence_put(fence);
  347. }
  348. *bo_ptr = bo;
  349. trace_amdgpu_bo_create(bo);
  350. return 0;
  351. fail_unreserve:
  352. amdgpu_bo_unreserve(bo);
  353. fail_free:
  354. amdgpu_bo_unref(&bo);
  355. return r;
  356. }
  357. static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
  358. unsigned long size, int byte_align,
  359. struct amdgpu_bo *bo)
  360. {
  361. struct ttm_placement placement = {0};
  362. struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
  363. int r;
  364. if (bo->shadow)
  365. return 0;
  366. bo->flags |= AMDGPU_GEM_CREATE_SHADOW;
  367. memset(&placements, 0,
  368. (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
  369. amdgpu_ttm_placement_init(adev, &placement,
  370. placements, AMDGPU_GEM_DOMAIN_GTT,
  371. AMDGPU_GEM_CREATE_CPU_GTT_USWC);
  372. r = amdgpu_bo_create_restricted(adev, size, byte_align, true,
  373. AMDGPU_GEM_DOMAIN_GTT,
  374. AMDGPU_GEM_CREATE_CPU_GTT_USWC,
  375. NULL, &placement,
  376. bo->tbo.resv,
  377. &bo->shadow);
  378. if (!r) {
  379. bo->shadow->parent = amdgpu_bo_ref(bo);
  380. mutex_lock(&adev->shadow_list_lock);
  381. list_add_tail(&bo->shadow_list, &adev->shadow_list);
  382. mutex_unlock(&adev->shadow_list_lock);
  383. }
  384. return r;
  385. }
  386. int amdgpu_bo_create(struct amdgpu_device *adev,
  387. unsigned long size, int byte_align,
  388. bool kernel, u32 domain, u64 flags,
  389. struct sg_table *sg,
  390. struct reservation_object *resv,
  391. struct amdgpu_bo **bo_ptr)
  392. {
  393. struct ttm_placement placement = {0};
  394. struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
  395. int r;
  396. memset(&placements, 0,
  397. (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
  398. amdgpu_ttm_placement_init(adev, &placement,
  399. placements, domain, flags);
  400. r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
  401. domain, flags, sg, &placement,
  402. resv, bo_ptr);
  403. if (r)
  404. return r;
  405. if (amdgpu_need_backup(adev) && (flags & AMDGPU_GEM_CREATE_SHADOW)) {
  406. r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
  407. if (r)
  408. amdgpu_bo_unref(bo_ptr);
  409. }
  410. return r;
  411. }
  412. int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
  413. struct amdgpu_ring *ring,
  414. struct amdgpu_bo *bo,
  415. struct reservation_object *resv,
  416. struct dma_fence **fence,
  417. bool direct)
  418. {
  419. struct amdgpu_bo *shadow = bo->shadow;
  420. uint64_t bo_addr, shadow_addr;
  421. int r;
  422. if (!shadow)
  423. return -EINVAL;
  424. bo_addr = amdgpu_bo_gpu_offset(bo);
  425. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  426. r = reservation_object_reserve_shared(bo->tbo.resv);
  427. if (r)
  428. goto err;
  429. r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
  430. amdgpu_bo_size(bo), resv, fence,
  431. direct);
  432. if (!r)
  433. amdgpu_bo_fence(bo, *fence, true);
  434. err:
  435. return r;
  436. }
  437. int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
  438. struct amdgpu_ring *ring,
  439. struct amdgpu_bo *bo,
  440. struct reservation_object *resv,
  441. struct dma_fence **fence,
  442. bool direct)
  443. {
  444. struct amdgpu_bo *shadow = bo->shadow;
  445. uint64_t bo_addr, shadow_addr;
  446. int r;
  447. if (!shadow)
  448. return -EINVAL;
  449. bo_addr = amdgpu_bo_gpu_offset(bo);
  450. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  451. r = reservation_object_reserve_shared(bo->tbo.resv);
  452. if (r)
  453. goto err;
  454. r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
  455. amdgpu_bo_size(bo), resv, fence,
  456. direct);
  457. if (!r)
  458. amdgpu_bo_fence(bo, *fence, true);
  459. err:
  460. return r;
  461. }
  462. int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
  463. {
  464. bool is_iomem;
  465. long r;
  466. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  467. return -EPERM;
  468. if (bo->kptr) {
  469. if (ptr) {
  470. *ptr = bo->kptr;
  471. }
  472. return 0;
  473. }
  474. r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
  475. MAX_SCHEDULE_TIMEOUT);
  476. if (r < 0)
  477. return r;
  478. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  479. if (r)
  480. return r;
  481. bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  482. if (ptr)
  483. *ptr = bo->kptr;
  484. return 0;
  485. }
  486. void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
  487. {
  488. if (bo->kptr == NULL)
  489. return;
  490. bo->kptr = NULL;
  491. ttm_bo_kunmap(&bo->kmap);
  492. }
  493. struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
  494. {
  495. if (bo == NULL)
  496. return NULL;
  497. ttm_bo_reference(&bo->tbo);
  498. return bo;
  499. }
  500. void amdgpu_bo_unref(struct amdgpu_bo **bo)
  501. {
  502. struct ttm_buffer_object *tbo;
  503. if ((*bo) == NULL)
  504. return;
  505. tbo = &((*bo)->tbo);
  506. ttm_bo_unref(&tbo);
  507. if (tbo == NULL)
  508. *bo = NULL;
  509. }
  510. int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
  511. u64 min_offset, u64 max_offset,
  512. u64 *gpu_addr)
  513. {
  514. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  515. int r, i;
  516. unsigned fpfn, lpfn;
  517. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
  518. return -EPERM;
  519. if (WARN_ON_ONCE(min_offset > max_offset))
  520. return -EINVAL;
  521. if (bo->pin_count) {
  522. uint32_t mem_type = bo->tbo.mem.mem_type;
  523. if (domain != amdgpu_mem_type_to_domain(mem_type))
  524. return -EINVAL;
  525. bo->pin_count++;
  526. if (gpu_addr)
  527. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  528. if (max_offset != 0) {
  529. u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
  530. WARN_ON_ONCE(max_offset <
  531. (amdgpu_bo_gpu_offset(bo) - domain_start));
  532. }
  533. return 0;
  534. }
  535. bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  536. amdgpu_ttm_placement_from_domain(bo, domain);
  537. for (i = 0; i < bo->placement.num_placement; i++) {
  538. /* force to pin into visible video ram */
  539. if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
  540. !(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) &&
  541. (!max_offset || max_offset >
  542. adev->mc.visible_vram_size)) {
  543. if (WARN_ON_ONCE(min_offset >
  544. adev->mc.visible_vram_size))
  545. return -EINVAL;
  546. fpfn = min_offset >> PAGE_SHIFT;
  547. lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
  548. } else {
  549. fpfn = min_offset >> PAGE_SHIFT;
  550. lpfn = max_offset >> PAGE_SHIFT;
  551. }
  552. if (fpfn > bo->placements[i].fpfn)
  553. bo->placements[i].fpfn = fpfn;
  554. if (!bo->placements[i].lpfn ||
  555. (lpfn && lpfn < bo->placements[i].lpfn))
  556. bo->placements[i].lpfn = lpfn;
  557. bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
  558. }
  559. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  560. if (unlikely(r)) {
  561. dev_err(adev->dev, "%p pin failed\n", bo);
  562. goto error;
  563. }
  564. r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
  565. if (unlikely(r)) {
  566. dev_err(adev->dev, "%p bind failed\n", bo);
  567. goto error;
  568. }
  569. bo->pin_count = 1;
  570. if (gpu_addr != NULL)
  571. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  572. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  573. adev->vram_pin_size += amdgpu_bo_size(bo);
  574. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  575. adev->invisible_pin_size += amdgpu_bo_size(bo);
  576. } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
  577. adev->gart_pin_size += amdgpu_bo_size(bo);
  578. }
  579. error:
  580. return r;
  581. }
  582. int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
  583. {
  584. return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
  585. }
  586. int amdgpu_bo_unpin(struct amdgpu_bo *bo)
  587. {
  588. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  589. int r, i;
  590. if (!bo->pin_count) {
  591. dev_warn(adev->dev, "%p unpin not necessary\n", bo);
  592. return 0;
  593. }
  594. bo->pin_count--;
  595. if (bo->pin_count)
  596. return 0;
  597. for (i = 0; i < bo->placement.num_placement; i++) {
  598. bo->placements[i].lpfn = 0;
  599. bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
  600. }
  601. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  602. if (unlikely(r)) {
  603. dev_err(adev->dev, "%p validate failed for unpin\n", bo);
  604. goto error;
  605. }
  606. if (bo->tbo.mem.mem_type == TTM_PL_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 (bo->tbo.mem.mem_type == TTM_PL_TT) {
  611. adev->gart_pin_size -= amdgpu_bo_size(bo);
  612. }
  613. error:
  614. return r;
  615. }
  616. int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
  617. {
  618. /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
  619. if (0 && (adev->flags & AMD_IS_APU)) {
  620. /* Useless to evict on IGP chips */
  621. return 0;
  622. }
  623. return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
  624. }
  625. static const char *amdgpu_vram_names[] = {
  626. "UNKNOWN",
  627. "GDDR1",
  628. "DDR2",
  629. "GDDR3",
  630. "GDDR4",
  631. "GDDR5",
  632. "HBM",
  633. "DDR3"
  634. };
  635. int amdgpu_bo_init(struct amdgpu_device *adev)
  636. {
  637. /* reserve PAT memory space to WC for VRAM */
  638. arch_io_reserve_memtype_wc(adev->mc.aper_base,
  639. adev->mc.aper_size);
  640. /* Add an MTRR for the VRAM */
  641. adev->mc.vram_mtrr = arch_phys_wc_add(adev->mc.aper_base,
  642. adev->mc.aper_size);
  643. DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
  644. adev->mc.mc_vram_size >> 20,
  645. (unsigned long long)adev->mc.aper_size >> 20);
  646. DRM_INFO("RAM width %dbits %s\n",
  647. adev->mc.vram_width, amdgpu_vram_names[adev->mc.vram_type]);
  648. return amdgpu_ttm_init(adev);
  649. }
  650. void amdgpu_bo_fini(struct amdgpu_device *adev)
  651. {
  652. amdgpu_ttm_fini(adev);
  653. arch_phys_wc_del(adev->mc.vram_mtrr);
  654. arch_io_free_memtype_wc(adev->mc.aper_base, adev->mc.aper_size);
  655. }
  656. int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
  657. struct vm_area_struct *vma)
  658. {
  659. return ttm_fbdev_mmap(vma, &bo->tbo);
  660. }
  661. int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
  662. {
  663. if (AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
  664. return -EINVAL;
  665. bo->tiling_flags = tiling_flags;
  666. return 0;
  667. }
  668. void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
  669. {
  670. lockdep_assert_held(&bo->tbo.resv->lock.base);
  671. if (tiling_flags)
  672. *tiling_flags = bo->tiling_flags;
  673. }
  674. int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
  675. uint32_t metadata_size, uint64_t flags)
  676. {
  677. void *buffer;
  678. if (!metadata_size) {
  679. if (bo->metadata_size) {
  680. kfree(bo->metadata);
  681. bo->metadata = NULL;
  682. bo->metadata_size = 0;
  683. }
  684. return 0;
  685. }
  686. if (metadata == NULL)
  687. return -EINVAL;
  688. buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
  689. if (buffer == NULL)
  690. return -ENOMEM;
  691. kfree(bo->metadata);
  692. bo->metadata_flags = flags;
  693. bo->metadata = buffer;
  694. bo->metadata_size = metadata_size;
  695. return 0;
  696. }
  697. int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
  698. size_t buffer_size, uint32_t *metadata_size,
  699. uint64_t *flags)
  700. {
  701. if (!buffer && !metadata_size)
  702. return -EINVAL;
  703. if (buffer) {
  704. if (buffer_size < bo->metadata_size)
  705. return -EINVAL;
  706. if (bo->metadata_size)
  707. memcpy(buffer, bo->metadata, bo->metadata_size);
  708. }
  709. if (metadata_size)
  710. *metadata_size = bo->metadata_size;
  711. if (flags)
  712. *flags = bo->metadata_flags;
  713. return 0;
  714. }
  715. void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
  716. struct ttm_mem_reg *new_mem)
  717. {
  718. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  719. struct amdgpu_bo *abo;
  720. struct ttm_mem_reg *old_mem = &bo->mem;
  721. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  722. return;
  723. abo = container_of(bo, struct amdgpu_bo, tbo);
  724. amdgpu_vm_bo_invalidate(adev, abo);
  725. /* update statistics */
  726. if (!new_mem)
  727. return;
  728. /* move_notify is called before move happens */
  729. amdgpu_update_memory_usage(adev, &bo->mem, new_mem);
  730. trace_amdgpu_ttm_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
  731. }
  732. int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  733. {
  734. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  735. struct amdgpu_bo *abo;
  736. unsigned long offset, size, lpfn;
  737. int i, r;
  738. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  739. return 0;
  740. abo = container_of(bo, struct amdgpu_bo, tbo);
  741. if (bo->mem.mem_type != TTM_PL_VRAM)
  742. return 0;
  743. size = bo->mem.num_pages << PAGE_SHIFT;
  744. offset = bo->mem.start << PAGE_SHIFT;
  745. /* TODO: figure out how to map scattered VRAM to the CPU */
  746. if ((offset + size) <= adev->mc.visible_vram_size &&
  747. (abo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS))
  748. return 0;
  749. /* Can't move a pinned BO to visible VRAM */
  750. if (abo->pin_count > 0)
  751. return -EINVAL;
  752. /* hurrah the memory is not visible ! */
  753. abo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  754. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM);
  755. lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
  756. for (i = 0; i < abo->placement.num_placement; i++) {
  757. /* Force into visible VRAM */
  758. if ((abo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
  759. (!abo->placements[i].lpfn ||
  760. abo->placements[i].lpfn > lpfn))
  761. abo->placements[i].lpfn = lpfn;
  762. }
  763. r = ttm_bo_validate(bo, &abo->placement, false, false);
  764. if (unlikely(r == -ENOMEM)) {
  765. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
  766. return ttm_bo_validate(bo, &abo->placement, false, false);
  767. } else if (unlikely(r != 0)) {
  768. return r;
  769. }
  770. offset = bo->mem.start << PAGE_SHIFT;
  771. /* this should never happen */
  772. if ((offset + size) > adev->mc.visible_vram_size)
  773. return -EINVAL;
  774. return 0;
  775. }
  776. /**
  777. * amdgpu_bo_fence - add fence to buffer object
  778. *
  779. * @bo: buffer object in question
  780. * @fence: fence to add
  781. * @shared: true if fence should be added shared
  782. *
  783. */
  784. void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
  785. bool shared)
  786. {
  787. struct reservation_object *resv = bo->tbo.resv;
  788. if (shared)
  789. reservation_object_add_shared_fence(resv, fence);
  790. else
  791. reservation_object_add_excl_fence(resv, fence);
  792. }
  793. /**
  794. * amdgpu_bo_gpu_offset - return GPU offset of bo
  795. * @bo: amdgpu object for which we query the offset
  796. *
  797. * Returns current GPU offset of the object.
  798. *
  799. * Note: object should either be pinned or reserved when calling this
  800. * function, it might be useful to add check for this for debugging.
  801. */
  802. u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
  803. {
  804. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
  805. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT &&
  806. !amdgpu_ttm_is_bound(bo->tbo.ttm));
  807. WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
  808. !bo->pin_count);
  809. WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
  810. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM &&
  811. !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
  812. return bo->tbo.offset;
  813. }