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