ttm_bo.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  29. */
  30. #define pr_fmt(fmt) "[TTM] " fmt
  31. #include <drm/ttm/ttm_module.h>
  32. #include <drm/ttm/ttm_bo_driver.h>
  33. #include <drm/ttm/ttm_placement.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/slab.h>
  36. #include <linux/sched.h>
  37. #include <linux/mm.h>
  38. #include <linux/file.h>
  39. #include <linux/module.h>
  40. #include <linux/atomic.h>
  41. #include <linux/reservation.h>
  42. #define TTM_ASSERT_LOCKED(param)
  43. #define TTM_DEBUG(fmt, arg...)
  44. #define TTM_BO_HASH_ORDER 13
  45. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
  46. static void ttm_bo_global_kobj_release(struct kobject *kobj);
  47. static struct attribute ttm_bo_count = {
  48. .name = "bo_count",
  49. .mode = S_IRUGO
  50. };
  51. static inline int ttm_mem_type_from_place(const struct ttm_place *place,
  52. uint32_t *mem_type)
  53. {
  54. int i;
  55. for (i = 0; i <= TTM_PL_PRIV5; i++)
  56. if (place->flags & (1 << i)) {
  57. *mem_type = i;
  58. return 0;
  59. }
  60. return -EINVAL;
  61. }
  62. static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
  63. {
  64. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  65. pr_err(" has_type: %d\n", man->has_type);
  66. pr_err(" use_type: %d\n", man->use_type);
  67. pr_err(" flags: 0x%08X\n", man->flags);
  68. pr_err(" gpu_offset: 0x%08llX\n", man->gpu_offset);
  69. pr_err(" size: %llu\n", man->size);
  70. pr_err(" available_caching: 0x%08X\n", man->available_caching);
  71. pr_err(" default_caching: 0x%08X\n", man->default_caching);
  72. if (mem_type != TTM_PL_SYSTEM)
  73. (*man->func->debug)(man, TTM_PFX);
  74. }
  75. static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
  76. struct ttm_placement *placement)
  77. {
  78. int i, ret, mem_type;
  79. pr_err("No space for %p (%lu pages, %luK, %luM)\n",
  80. bo, bo->mem.num_pages, bo->mem.size >> 10,
  81. bo->mem.size >> 20);
  82. for (i = 0; i < placement->num_placement; i++) {
  83. ret = ttm_mem_type_from_place(&placement->placement[i],
  84. &mem_type);
  85. if (ret)
  86. return;
  87. pr_err(" placement[%d]=0x%08X (%d)\n",
  88. i, placement->placement[i].flags, mem_type);
  89. ttm_mem_type_debug(bo->bdev, mem_type);
  90. }
  91. }
  92. static ssize_t ttm_bo_global_show(struct kobject *kobj,
  93. struct attribute *attr,
  94. char *buffer)
  95. {
  96. struct ttm_bo_global *glob =
  97. container_of(kobj, struct ttm_bo_global, kobj);
  98. return snprintf(buffer, PAGE_SIZE, "%lu\n",
  99. (unsigned long) atomic_read(&glob->bo_count));
  100. }
  101. static struct attribute *ttm_bo_global_attrs[] = {
  102. &ttm_bo_count,
  103. NULL
  104. };
  105. static const struct sysfs_ops ttm_bo_global_ops = {
  106. .show = &ttm_bo_global_show
  107. };
  108. static struct kobj_type ttm_bo_glob_kobj_type = {
  109. .release = &ttm_bo_global_kobj_release,
  110. .sysfs_ops = &ttm_bo_global_ops,
  111. .default_attrs = ttm_bo_global_attrs
  112. };
  113. static inline uint32_t ttm_bo_type_flags(unsigned type)
  114. {
  115. return 1 << (type);
  116. }
  117. static void ttm_bo_release_list(struct kref *list_kref)
  118. {
  119. struct ttm_buffer_object *bo =
  120. container_of(list_kref, struct ttm_buffer_object, list_kref);
  121. struct ttm_bo_device *bdev = bo->bdev;
  122. size_t acc_size = bo->acc_size;
  123. BUG_ON(atomic_read(&bo->list_kref.refcount));
  124. BUG_ON(atomic_read(&bo->kref.refcount));
  125. BUG_ON(atomic_read(&bo->cpu_writers));
  126. BUG_ON(bo->mem.mm_node != NULL);
  127. BUG_ON(!list_empty(&bo->lru));
  128. BUG_ON(!list_empty(&bo->ddestroy));
  129. ttm_tt_destroy(bo->ttm);
  130. atomic_dec(&bo->glob->bo_count);
  131. fence_put(bo->moving);
  132. if (bo->resv == &bo->ttm_resv)
  133. reservation_object_fini(&bo->ttm_resv);
  134. mutex_destroy(&bo->wu_mutex);
  135. if (bo->destroy)
  136. bo->destroy(bo);
  137. else {
  138. kfree(bo);
  139. }
  140. ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
  141. }
  142. void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
  143. {
  144. struct ttm_bo_device *bdev = bo->bdev;
  145. lockdep_assert_held(&bo->resv->lock.base);
  146. if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  147. BUG_ON(!list_empty(&bo->lru));
  148. list_add(&bo->lru, bdev->driver->lru_tail(bo));
  149. kref_get(&bo->list_kref);
  150. if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
  151. list_add(&bo->swap, bdev->driver->swap_lru_tail(bo));
  152. kref_get(&bo->list_kref);
  153. }
  154. }
  155. }
  156. EXPORT_SYMBOL(ttm_bo_add_to_lru);
  157. int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
  158. {
  159. struct ttm_bo_device *bdev = bo->bdev;
  160. int put_count = 0;
  161. if (bdev->driver->lru_removal)
  162. bdev->driver->lru_removal(bo);
  163. if (!list_empty(&bo->swap)) {
  164. list_del_init(&bo->swap);
  165. ++put_count;
  166. }
  167. if (!list_empty(&bo->lru)) {
  168. list_del_init(&bo->lru);
  169. ++put_count;
  170. }
  171. return put_count;
  172. }
  173. static void ttm_bo_ref_bug(struct kref *list_kref)
  174. {
  175. BUG();
  176. }
  177. void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
  178. bool never_free)
  179. {
  180. kref_sub(&bo->list_kref, count,
  181. (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
  182. }
  183. void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
  184. {
  185. int put_count;
  186. spin_lock(&bo->glob->lru_lock);
  187. put_count = ttm_bo_del_from_lru(bo);
  188. spin_unlock(&bo->glob->lru_lock);
  189. ttm_bo_list_ref_sub(bo, put_count, true);
  190. }
  191. EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
  192. void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
  193. {
  194. struct ttm_bo_device *bdev = bo->bdev;
  195. int put_count = 0;
  196. lockdep_assert_held(&bo->resv->lock.base);
  197. if (bdev->driver->lru_removal)
  198. bdev->driver->lru_removal(bo);
  199. put_count = ttm_bo_del_from_lru(bo);
  200. ttm_bo_list_ref_sub(bo, put_count, true);
  201. ttm_bo_add_to_lru(bo);
  202. }
  203. EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
  204. struct list_head *ttm_bo_default_lru_tail(struct ttm_buffer_object *bo)
  205. {
  206. return bo->bdev->man[bo->mem.mem_type].lru.prev;
  207. }
  208. EXPORT_SYMBOL(ttm_bo_default_lru_tail);
  209. struct list_head *ttm_bo_default_swap_lru_tail(struct ttm_buffer_object *bo)
  210. {
  211. return bo->glob->swap_lru.prev;
  212. }
  213. EXPORT_SYMBOL(ttm_bo_default_swap_lru_tail);
  214. /*
  215. * Call bo->mutex locked.
  216. */
  217. static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
  218. {
  219. struct ttm_bo_device *bdev = bo->bdev;
  220. struct ttm_bo_global *glob = bo->glob;
  221. int ret = 0;
  222. uint32_t page_flags = 0;
  223. TTM_ASSERT_LOCKED(&bo->mutex);
  224. bo->ttm = NULL;
  225. if (bdev->need_dma32)
  226. page_flags |= TTM_PAGE_FLAG_DMA32;
  227. switch (bo->type) {
  228. case ttm_bo_type_device:
  229. if (zero_alloc)
  230. page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
  231. case ttm_bo_type_kernel:
  232. bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
  233. page_flags, glob->dummy_read_page);
  234. if (unlikely(bo->ttm == NULL))
  235. ret = -ENOMEM;
  236. break;
  237. case ttm_bo_type_sg:
  238. bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
  239. page_flags | TTM_PAGE_FLAG_SG,
  240. glob->dummy_read_page);
  241. if (unlikely(bo->ttm == NULL)) {
  242. ret = -ENOMEM;
  243. break;
  244. }
  245. bo->ttm->sg = bo->sg;
  246. break;
  247. default:
  248. pr_err("Illegal buffer object type\n");
  249. ret = -EINVAL;
  250. break;
  251. }
  252. return ret;
  253. }
  254. static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
  255. struct ttm_mem_reg *mem,
  256. bool evict, bool interruptible,
  257. bool no_wait_gpu)
  258. {
  259. struct ttm_bo_device *bdev = bo->bdev;
  260. bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
  261. bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
  262. struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
  263. struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
  264. int ret = 0;
  265. if (old_is_pci || new_is_pci ||
  266. ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
  267. ret = ttm_mem_io_lock(old_man, true);
  268. if (unlikely(ret != 0))
  269. goto out_err;
  270. ttm_bo_unmap_virtual_locked(bo);
  271. ttm_mem_io_unlock(old_man);
  272. }
  273. /*
  274. * Create and bind a ttm if required.
  275. */
  276. if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  277. if (bo->ttm == NULL) {
  278. bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
  279. ret = ttm_bo_add_ttm(bo, zero);
  280. if (ret)
  281. goto out_err;
  282. }
  283. ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
  284. if (ret)
  285. goto out_err;
  286. if (mem->mem_type != TTM_PL_SYSTEM) {
  287. ret = ttm_tt_bind(bo->ttm, mem);
  288. if (ret)
  289. goto out_err;
  290. }
  291. if (bo->mem.mem_type == TTM_PL_SYSTEM) {
  292. if (bdev->driver->move_notify)
  293. bdev->driver->move_notify(bo, mem);
  294. bo->mem = *mem;
  295. mem->mm_node = NULL;
  296. goto moved;
  297. }
  298. }
  299. if (bdev->driver->move_notify)
  300. bdev->driver->move_notify(bo, mem);
  301. if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
  302. !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
  303. ret = ttm_bo_move_ttm(bo, evict, interruptible, no_wait_gpu,
  304. mem);
  305. else if (bdev->driver->move)
  306. ret = bdev->driver->move(bo, evict, interruptible,
  307. no_wait_gpu, mem);
  308. else
  309. ret = ttm_bo_move_memcpy(bo, evict, interruptible,
  310. no_wait_gpu, mem);
  311. if (ret) {
  312. if (bdev->driver->move_notify) {
  313. struct ttm_mem_reg tmp_mem = *mem;
  314. *mem = bo->mem;
  315. bo->mem = tmp_mem;
  316. bdev->driver->move_notify(bo, mem);
  317. bo->mem = *mem;
  318. *mem = tmp_mem;
  319. }
  320. goto out_err;
  321. }
  322. moved:
  323. if (bo->evicted) {
  324. if (bdev->driver->invalidate_caches) {
  325. ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
  326. if (ret)
  327. pr_err("Can not flush read caches\n");
  328. }
  329. bo->evicted = false;
  330. }
  331. if (bo->mem.mm_node) {
  332. bo->offset = (bo->mem.start << PAGE_SHIFT) +
  333. bdev->man[bo->mem.mem_type].gpu_offset;
  334. bo->cur_placement = bo->mem.placement;
  335. } else
  336. bo->offset = 0;
  337. return 0;
  338. out_err:
  339. new_man = &bdev->man[bo->mem.mem_type];
  340. if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
  341. ttm_tt_destroy(bo->ttm);
  342. bo->ttm = NULL;
  343. }
  344. return ret;
  345. }
  346. /**
  347. * Call bo::reserved.
  348. * Will release GPU memory type usage on destruction.
  349. * This is the place to put in driver specific hooks to release
  350. * driver private resources.
  351. * Will release the bo::reserved lock.
  352. */
  353. static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
  354. {
  355. if (bo->bdev->driver->move_notify)
  356. bo->bdev->driver->move_notify(bo, NULL);
  357. ttm_tt_destroy(bo->ttm);
  358. bo->ttm = NULL;
  359. ttm_bo_mem_put(bo, &bo->mem);
  360. ww_mutex_unlock (&bo->resv->lock);
  361. }
  362. static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
  363. {
  364. struct reservation_object_list *fobj;
  365. struct fence *fence;
  366. int i;
  367. fobj = reservation_object_get_list(bo->resv);
  368. fence = reservation_object_get_excl(bo->resv);
  369. if (fence && !fence->ops->signaled)
  370. fence_enable_sw_signaling(fence);
  371. for (i = 0; fobj && i < fobj->shared_count; ++i) {
  372. fence = rcu_dereference_protected(fobj->shared[i],
  373. reservation_object_held(bo->resv));
  374. if (!fence->ops->signaled)
  375. fence_enable_sw_signaling(fence);
  376. }
  377. }
  378. static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
  379. {
  380. struct ttm_bo_device *bdev = bo->bdev;
  381. struct ttm_bo_global *glob = bo->glob;
  382. int put_count;
  383. int ret;
  384. spin_lock(&glob->lru_lock);
  385. ret = __ttm_bo_reserve(bo, false, true, NULL);
  386. if (!ret) {
  387. if (!ttm_bo_wait(bo, false, true)) {
  388. put_count = ttm_bo_del_from_lru(bo);
  389. spin_unlock(&glob->lru_lock);
  390. ttm_bo_cleanup_memtype_use(bo);
  391. ttm_bo_list_ref_sub(bo, put_count, true);
  392. return;
  393. } else
  394. ttm_bo_flush_all_fences(bo);
  395. /*
  396. * Make NO_EVICT bos immediately available to
  397. * shrinkers, now that they are queued for
  398. * destruction.
  399. */
  400. if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
  401. bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
  402. ttm_bo_add_to_lru(bo);
  403. }
  404. __ttm_bo_unreserve(bo);
  405. }
  406. kref_get(&bo->list_kref);
  407. list_add_tail(&bo->ddestroy, &bdev->ddestroy);
  408. spin_unlock(&glob->lru_lock);
  409. schedule_delayed_work(&bdev->wq,
  410. ((HZ / 100) < 1) ? 1 : HZ / 100);
  411. }
  412. /**
  413. * function ttm_bo_cleanup_refs_and_unlock
  414. * If bo idle, remove from delayed- and lru lists, and unref.
  415. * If not idle, do nothing.
  416. *
  417. * Must be called with lru_lock and reservation held, this function
  418. * will drop both before returning.
  419. *
  420. * @interruptible Any sleeps should occur interruptibly.
  421. * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
  422. */
  423. static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
  424. bool interruptible,
  425. bool no_wait_gpu)
  426. {
  427. struct ttm_bo_global *glob = bo->glob;
  428. int put_count;
  429. int ret;
  430. ret = ttm_bo_wait(bo, false, true);
  431. if (ret && !no_wait_gpu) {
  432. long lret;
  433. ww_mutex_unlock(&bo->resv->lock);
  434. spin_unlock(&glob->lru_lock);
  435. lret = reservation_object_wait_timeout_rcu(bo->resv,
  436. true,
  437. interruptible,
  438. 30 * HZ);
  439. if (lret < 0)
  440. return lret;
  441. else if (lret == 0)
  442. return -EBUSY;
  443. spin_lock(&glob->lru_lock);
  444. ret = __ttm_bo_reserve(bo, false, true, NULL);
  445. /*
  446. * We raced, and lost, someone else holds the reservation now,
  447. * and is probably busy in ttm_bo_cleanup_memtype_use.
  448. *
  449. * Even if it's not the case, because we finished waiting any
  450. * delayed destruction would succeed, so just return success
  451. * here.
  452. */
  453. if (ret) {
  454. spin_unlock(&glob->lru_lock);
  455. return 0;
  456. }
  457. /*
  458. * remove sync_obj with ttm_bo_wait, the wait should be
  459. * finished, and no new wait object should have been added.
  460. */
  461. ret = ttm_bo_wait(bo, false, true);
  462. WARN_ON(ret);
  463. }
  464. if (ret || unlikely(list_empty(&bo->ddestroy))) {
  465. __ttm_bo_unreserve(bo);
  466. spin_unlock(&glob->lru_lock);
  467. return ret;
  468. }
  469. put_count = ttm_bo_del_from_lru(bo);
  470. list_del_init(&bo->ddestroy);
  471. ++put_count;
  472. spin_unlock(&glob->lru_lock);
  473. ttm_bo_cleanup_memtype_use(bo);
  474. ttm_bo_list_ref_sub(bo, put_count, true);
  475. return 0;
  476. }
  477. /**
  478. * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
  479. * encountered buffers.
  480. */
  481. static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
  482. {
  483. struct ttm_bo_global *glob = bdev->glob;
  484. struct ttm_buffer_object *entry = NULL;
  485. int ret = 0;
  486. spin_lock(&glob->lru_lock);
  487. if (list_empty(&bdev->ddestroy))
  488. goto out_unlock;
  489. entry = list_first_entry(&bdev->ddestroy,
  490. struct ttm_buffer_object, ddestroy);
  491. kref_get(&entry->list_kref);
  492. for (;;) {
  493. struct ttm_buffer_object *nentry = NULL;
  494. if (entry->ddestroy.next != &bdev->ddestroy) {
  495. nentry = list_first_entry(&entry->ddestroy,
  496. struct ttm_buffer_object, ddestroy);
  497. kref_get(&nentry->list_kref);
  498. }
  499. ret = __ttm_bo_reserve(entry, false, true, NULL);
  500. if (remove_all && ret) {
  501. spin_unlock(&glob->lru_lock);
  502. ret = __ttm_bo_reserve(entry, false, false, NULL);
  503. spin_lock(&glob->lru_lock);
  504. }
  505. if (!ret)
  506. ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
  507. !remove_all);
  508. else
  509. spin_unlock(&glob->lru_lock);
  510. kref_put(&entry->list_kref, ttm_bo_release_list);
  511. entry = nentry;
  512. if (ret || !entry)
  513. goto out;
  514. spin_lock(&glob->lru_lock);
  515. if (list_empty(&entry->ddestroy))
  516. break;
  517. }
  518. out_unlock:
  519. spin_unlock(&glob->lru_lock);
  520. out:
  521. if (entry)
  522. kref_put(&entry->list_kref, ttm_bo_release_list);
  523. return ret;
  524. }
  525. static void ttm_bo_delayed_workqueue(struct work_struct *work)
  526. {
  527. struct ttm_bo_device *bdev =
  528. container_of(work, struct ttm_bo_device, wq.work);
  529. if (ttm_bo_delayed_delete(bdev, false)) {
  530. schedule_delayed_work(&bdev->wq,
  531. ((HZ / 100) < 1) ? 1 : HZ / 100);
  532. }
  533. }
  534. static void ttm_bo_release(struct kref *kref)
  535. {
  536. struct ttm_buffer_object *bo =
  537. container_of(kref, struct ttm_buffer_object, kref);
  538. struct ttm_bo_device *bdev = bo->bdev;
  539. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  540. drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
  541. ttm_mem_io_lock(man, false);
  542. ttm_mem_io_free_vm(bo);
  543. ttm_mem_io_unlock(man);
  544. ttm_bo_cleanup_refs_or_queue(bo);
  545. kref_put(&bo->list_kref, ttm_bo_release_list);
  546. }
  547. void ttm_bo_unref(struct ttm_buffer_object **p_bo)
  548. {
  549. struct ttm_buffer_object *bo = *p_bo;
  550. *p_bo = NULL;
  551. kref_put(&bo->kref, ttm_bo_release);
  552. }
  553. EXPORT_SYMBOL(ttm_bo_unref);
  554. int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
  555. {
  556. return cancel_delayed_work_sync(&bdev->wq);
  557. }
  558. EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
  559. void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
  560. {
  561. if (resched)
  562. schedule_delayed_work(&bdev->wq,
  563. ((HZ / 100) < 1) ? 1 : HZ / 100);
  564. }
  565. EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
  566. static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
  567. bool no_wait_gpu)
  568. {
  569. struct ttm_bo_device *bdev = bo->bdev;
  570. struct ttm_mem_reg evict_mem;
  571. struct ttm_placement placement;
  572. int ret = 0;
  573. lockdep_assert_held(&bo->resv->lock.base);
  574. evict_mem = bo->mem;
  575. evict_mem.mm_node = NULL;
  576. evict_mem.bus.io_reserved_vm = false;
  577. evict_mem.bus.io_reserved_count = 0;
  578. placement.num_placement = 0;
  579. placement.num_busy_placement = 0;
  580. bdev->driver->evict_flags(bo, &placement);
  581. ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
  582. no_wait_gpu);
  583. if (ret) {
  584. if (ret != -ERESTARTSYS) {
  585. pr_err("Failed to find memory space for buffer 0x%p eviction\n",
  586. bo);
  587. ttm_bo_mem_space_debug(bo, &placement);
  588. }
  589. goto out;
  590. }
  591. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
  592. no_wait_gpu);
  593. if (unlikely(ret)) {
  594. if (ret != -ERESTARTSYS)
  595. pr_err("Buffer eviction failed\n");
  596. ttm_bo_mem_put(bo, &evict_mem);
  597. goto out;
  598. }
  599. bo->evicted = true;
  600. out:
  601. return ret;
  602. }
  603. static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
  604. uint32_t mem_type,
  605. const struct ttm_place *place,
  606. bool interruptible,
  607. bool no_wait_gpu)
  608. {
  609. struct ttm_bo_global *glob = bdev->glob;
  610. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  611. struct ttm_buffer_object *bo;
  612. int ret = -EBUSY, put_count;
  613. spin_lock(&glob->lru_lock);
  614. list_for_each_entry(bo, &man->lru, lru) {
  615. ret = __ttm_bo_reserve(bo, false, true, NULL);
  616. if (!ret) {
  617. if (place && (place->fpfn || place->lpfn)) {
  618. /* Don't evict this BO if it's outside of the
  619. * requested placement range
  620. */
  621. if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
  622. (place->lpfn && place->lpfn <= bo->mem.start)) {
  623. __ttm_bo_unreserve(bo);
  624. ret = -EBUSY;
  625. continue;
  626. }
  627. }
  628. break;
  629. }
  630. }
  631. if (ret) {
  632. spin_unlock(&glob->lru_lock);
  633. return ret;
  634. }
  635. kref_get(&bo->list_kref);
  636. if (!list_empty(&bo->ddestroy)) {
  637. ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
  638. no_wait_gpu);
  639. kref_put(&bo->list_kref, ttm_bo_release_list);
  640. return ret;
  641. }
  642. put_count = ttm_bo_del_from_lru(bo);
  643. spin_unlock(&glob->lru_lock);
  644. BUG_ON(ret != 0);
  645. ttm_bo_list_ref_sub(bo, put_count, true);
  646. ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
  647. ttm_bo_unreserve(bo);
  648. kref_put(&bo->list_kref, ttm_bo_release_list);
  649. return ret;
  650. }
  651. void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
  652. {
  653. struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
  654. if (mem->mm_node)
  655. (*man->func->put_node)(man, mem);
  656. }
  657. EXPORT_SYMBOL(ttm_bo_mem_put);
  658. /**
  659. * Add the last move fence to the BO and reserve a new shared slot.
  660. */
  661. static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
  662. struct ttm_mem_type_manager *man,
  663. struct ttm_mem_reg *mem)
  664. {
  665. struct fence *fence;
  666. int ret;
  667. spin_lock(&man->move_lock);
  668. fence = fence_get(man->move);
  669. spin_unlock(&man->move_lock);
  670. if (fence) {
  671. reservation_object_add_shared_fence(bo->resv, fence);
  672. ret = reservation_object_reserve_shared(bo->resv);
  673. if (unlikely(ret))
  674. return ret;
  675. fence_put(bo->moving);
  676. bo->moving = fence;
  677. }
  678. return 0;
  679. }
  680. /**
  681. * Repeatedly evict memory from the LRU for @mem_type until we create enough
  682. * space, or we've evicted everything and there isn't enough space.
  683. */
  684. static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  685. uint32_t mem_type,
  686. const struct ttm_place *place,
  687. struct ttm_mem_reg *mem,
  688. bool interruptible,
  689. bool no_wait_gpu)
  690. {
  691. struct ttm_bo_device *bdev = bo->bdev;
  692. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  693. int ret;
  694. do {
  695. ret = (*man->func->get_node)(man, bo, place, mem);
  696. if (unlikely(ret != 0))
  697. return ret;
  698. if (mem->mm_node)
  699. break;
  700. ret = ttm_mem_evict_first(bdev, mem_type, place,
  701. interruptible, no_wait_gpu);
  702. if (unlikely(ret != 0))
  703. return ret;
  704. } while (1);
  705. mem->mem_type = mem_type;
  706. return ttm_bo_add_move_fence(bo, man, mem);
  707. }
  708. static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
  709. uint32_t cur_placement,
  710. uint32_t proposed_placement)
  711. {
  712. uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
  713. uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
  714. /**
  715. * Keep current caching if possible.
  716. */
  717. if ((cur_placement & caching) != 0)
  718. result |= (cur_placement & caching);
  719. else if ((man->default_caching & caching) != 0)
  720. result |= man->default_caching;
  721. else if ((TTM_PL_FLAG_CACHED & caching) != 0)
  722. result |= TTM_PL_FLAG_CACHED;
  723. else if ((TTM_PL_FLAG_WC & caching) != 0)
  724. result |= TTM_PL_FLAG_WC;
  725. else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
  726. result |= TTM_PL_FLAG_UNCACHED;
  727. return result;
  728. }
  729. static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
  730. uint32_t mem_type,
  731. const struct ttm_place *place,
  732. uint32_t *masked_placement)
  733. {
  734. uint32_t cur_flags = ttm_bo_type_flags(mem_type);
  735. if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
  736. return false;
  737. if ((place->flags & man->available_caching) == 0)
  738. return false;
  739. cur_flags |= (place->flags & man->available_caching);
  740. *masked_placement = cur_flags;
  741. return true;
  742. }
  743. /**
  744. * Creates space for memory region @mem according to its type.
  745. *
  746. * This function first searches for free space in compatible memory types in
  747. * the priority order defined by the driver. If free space isn't found, then
  748. * ttm_bo_mem_force_space is attempted in priority order to evict and find
  749. * space.
  750. */
  751. int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  752. struct ttm_placement *placement,
  753. struct ttm_mem_reg *mem,
  754. bool interruptible,
  755. bool no_wait_gpu)
  756. {
  757. struct ttm_bo_device *bdev = bo->bdev;
  758. struct ttm_mem_type_manager *man;
  759. uint32_t mem_type = TTM_PL_SYSTEM;
  760. uint32_t cur_flags = 0;
  761. bool type_found = false;
  762. bool type_ok = false;
  763. bool has_erestartsys = false;
  764. int i, ret;
  765. ret = reservation_object_reserve_shared(bo->resv);
  766. if (unlikely(ret))
  767. return ret;
  768. mem->mm_node = NULL;
  769. for (i = 0; i < placement->num_placement; ++i) {
  770. const struct ttm_place *place = &placement->placement[i];
  771. ret = ttm_mem_type_from_place(place, &mem_type);
  772. if (ret)
  773. return ret;
  774. man = &bdev->man[mem_type];
  775. if (!man->has_type || !man->use_type)
  776. continue;
  777. type_ok = ttm_bo_mt_compatible(man, mem_type, place,
  778. &cur_flags);
  779. if (!type_ok)
  780. continue;
  781. type_found = true;
  782. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  783. cur_flags);
  784. /*
  785. * Use the access and other non-mapping-related flag bits from
  786. * the memory placement flags to the current flags
  787. */
  788. ttm_flag_masked(&cur_flags, place->flags,
  789. ~TTM_PL_MASK_MEMTYPE);
  790. if (mem_type == TTM_PL_SYSTEM)
  791. break;
  792. ret = (*man->func->get_node)(man, bo, place, mem);
  793. if (unlikely(ret))
  794. return ret;
  795. if (mem->mm_node) {
  796. ret = ttm_bo_add_move_fence(bo, man, mem);
  797. if (unlikely(ret)) {
  798. (*man->func->put_node)(man, mem);
  799. return ret;
  800. }
  801. break;
  802. }
  803. }
  804. if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
  805. mem->mem_type = mem_type;
  806. mem->placement = cur_flags;
  807. return 0;
  808. }
  809. for (i = 0; i < placement->num_busy_placement; ++i) {
  810. const struct ttm_place *place = &placement->busy_placement[i];
  811. ret = ttm_mem_type_from_place(place, &mem_type);
  812. if (ret)
  813. return ret;
  814. man = &bdev->man[mem_type];
  815. if (!man->has_type || !man->use_type)
  816. continue;
  817. if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
  818. continue;
  819. type_found = true;
  820. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  821. cur_flags);
  822. /*
  823. * Use the access and other non-mapping-related flag bits from
  824. * the memory placement flags to the current flags
  825. */
  826. ttm_flag_masked(&cur_flags, place->flags,
  827. ~TTM_PL_MASK_MEMTYPE);
  828. if (mem_type == TTM_PL_SYSTEM) {
  829. mem->mem_type = mem_type;
  830. mem->placement = cur_flags;
  831. mem->mm_node = NULL;
  832. return 0;
  833. }
  834. ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
  835. interruptible, no_wait_gpu);
  836. if (ret == 0 && mem->mm_node) {
  837. mem->placement = cur_flags;
  838. return 0;
  839. }
  840. if (ret == -ERESTARTSYS)
  841. has_erestartsys = true;
  842. }
  843. if (!type_found) {
  844. printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
  845. return -EINVAL;
  846. }
  847. return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
  848. }
  849. EXPORT_SYMBOL(ttm_bo_mem_space);
  850. static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  851. struct ttm_placement *placement,
  852. bool interruptible,
  853. bool no_wait_gpu)
  854. {
  855. int ret = 0;
  856. struct ttm_mem_reg mem;
  857. lockdep_assert_held(&bo->resv->lock.base);
  858. mem.num_pages = bo->num_pages;
  859. mem.size = mem.num_pages << PAGE_SHIFT;
  860. mem.page_alignment = bo->mem.page_alignment;
  861. mem.bus.io_reserved_vm = false;
  862. mem.bus.io_reserved_count = 0;
  863. /*
  864. * Determine where to move the buffer.
  865. */
  866. ret = ttm_bo_mem_space(bo, placement, &mem,
  867. interruptible, no_wait_gpu);
  868. if (ret)
  869. goto out_unlock;
  870. ret = ttm_bo_handle_move_mem(bo, &mem, false,
  871. interruptible, no_wait_gpu);
  872. out_unlock:
  873. if (ret && mem.mm_node)
  874. ttm_bo_mem_put(bo, &mem);
  875. return ret;
  876. }
  877. bool ttm_bo_mem_compat(struct ttm_placement *placement,
  878. struct ttm_mem_reg *mem,
  879. uint32_t *new_flags)
  880. {
  881. int i;
  882. for (i = 0; i < placement->num_placement; i++) {
  883. const struct ttm_place *heap = &placement->placement[i];
  884. if (mem->mm_node &&
  885. (mem->start < heap->fpfn ||
  886. (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
  887. continue;
  888. *new_flags = heap->flags;
  889. if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
  890. (*new_flags & mem->placement & TTM_PL_MASK_MEM))
  891. return true;
  892. }
  893. for (i = 0; i < placement->num_busy_placement; i++) {
  894. const struct ttm_place *heap = &placement->busy_placement[i];
  895. if (mem->mm_node &&
  896. (mem->start < heap->fpfn ||
  897. (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
  898. continue;
  899. *new_flags = heap->flags;
  900. if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
  901. (*new_flags & mem->placement & TTM_PL_MASK_MEM))
  902. return true;
  903. }
  904. return false;
  905. }
  906. EXPORT_SYMBOL(ttm_bo_mem_compat);
  907. int ttm_bo_validate(struct ttm_buffer_object *bo,
  908. struct ttm_placement *placement,
  909. bool interruptible,
  910. bool no_wait_gpu)
  911. {
  912. int ret;
  913. uint32_t new_flags;
  914. lockdep_assert_held(&bo->resv->lock.base);
  915. /*
  916. * Check whether we need to move buffer.
  917. */
  918. if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
  919. ret = ttm_bo_move_buffer(bo, placement, interruptible,
  920. no_wait_gpu);
  921. if (ret)
  922. return ret;
  923. } else {
  924. /*
  925. * Use the access and other non-mapping-related flag bits from
  926. * the compatible memory placement flags to the active flags
  927. */
  928. ttm_flag_masked(&bo->mem.placement, new_flags,
  929. ~TTM_PL_MASK_MEMTYPE);
  930. }
  931. /*
  932. * We might need to add a TTM.
  933. */
  934. if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  935. ret = ttm_bo_add_ttm(bo, true);
  936. if (ret)
  937. return ret;
  938. }
  939. return 0;
  940. }
  941. EXPORT_SYMBOL(ttm_bo_validate);
  942. int ttm_bo_init(struct ttm_bo_device *bdev,
  943. struct ttm_buffer_object *bo,
  944. unsigned long size,
  945. enum ttm_bo_type type,
  946. struct ttm_placement *placement,
  947. uint32_t page_alignment,
  948. bool interruptible,
  949. struct file *persistent_swap_storage,
  950. size_t acc_size,
  951. struct sg_table *sg,
  952. struct reservation_object *resv,
  953. void (*destroy) (struct ttm_buffer_object *))
  954. {
  955. int ret = 0;
  956. unsigned long num_pages;
  957. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  958. bool locked;
  959. ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  960. if (ret) {
  961. pr_err("Out of kernel memory\n");
  962. if (destroy)
  963. (*destroy)(bo);
  964. else
  965. kfree(bo);
  966. return -ENOMEM;
  967. }
  968. num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  969. if (num_pages == 0) {
  970. pr_err("Illegal buffer object size\n");
  971. if (destroy)
  972. (*destroy)(bo);
  973. else
  974. kfree(bo);
  975. ttm_mem_global_free(mem_glob, acc_size);
  976. return -EINVAL;
  977. }
  978. bo->destroy = destroy;
  979. kref_init(&bo->kref);
  980. kref_init(&bo->list_kref);
  981. atomic_set(&bo->cpu_writers, 0);
  982. INIT_LIST_HEAD(&bo->lru);
  983. INIT_LIST_HEAD(&bo->ddestroy);
  984. INIT_LIST_HEAD(&bo->swap);
  985. INIT_LIST_HEAD(&bo->io_reserve_lru);
  986. mutex_init(&bo->wu_mutex);
  987. bo->bdev = bdev;
  988. bo->glob = bdev->glob;
  989. bo->type = type;
  990. bo->num_pages = num_pages;
  991. bo->mem.size = num_pages << PAGE_SHIFT;
  992. bo->mem.mem_type = TTM_PL_SYSTEM;
  993. bo->mem.num_pages = bo->num_pages;
  994. bo->mem.mm_node = NULL;
  995. bo->mem.page_alignment = page_alignment;
  996. bo->mem.bus.io_reserved_vm = false;
  997. bo->mem.bus.io_reserved_count = 0;
  998. bo->moving = NULL;
  999. bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
  1000. bo->persistent_swap_storage = persistent_swap_storage;
  1001. bo->acc_size = acc_size;
  1002. bo->sg = sg;
  1003. if (resv) {
  1004. bo->resv = resv;
  1005. lockdep_assert_held(&bo->resv->lock.base);
  1006. } else {
  1007. bo->resv = &bo->ttm_resv;
  1008. reservation_object_init(&bo->ttm_resv);
  1009. }
  1010. atomic_inc(&bo->glob->bo_count);
  1011. drm_vma_node_reset(&bo->vma_node);
  1012. /*
  1013. * For ttm_bo_type_device buffers, allocate
  1014. * address space from the device.
  1015. */
  1016. if (bo->type == ttm_bo_type_device ||
  1017. bo->type == ttm_bo_type_sg)
  1018. ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
  1019. bo->mem.num_pages);
  1020. /* passed reservation objects should already be locked,
  1021. * since otherwise lockdep will be angered in radeon.
  1022. */
  1023. if (!resv) {
  1024. locked = ww_mutex_trylock(&bo->resv->lock);
  1025. WARN_ON(!locked);
  1026. }
  1027. if (likely(!ret))
  1028. ret = ttm_bo_validate(bo, placement, interruptible, false);
  1029. if (!resv) {
  1030. ttm_bo_unreserve(bo);
  1031. } else if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  1032. spin_lock(&bo->glob->lru_lock);
  1033. ttm_bo_add_to_lru(bo);
  1034. spin_unlock(&bo->glob->lru_lock);
  1035. }
  1036. if (unlikely(ret))
  1037. ttm_bo_unref(&bo);
  1038. return ret;
  1039. }
  1040. EXPORT_SYMBOL(ttm_bo_init);
  1041. size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
  1042. unsigned long bo_size,
  1043. unsigned struct_size)
  1044. {
  1045. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1046. size_t size = 0;
  1047. size += ttm_round_pot(struct_size);
  1048. size += ttm_round_pot(npages * sizeof(void *));
  1049. size += ttm_round_pot(sizeof(struct ttm_tt));
  1050. return size;
  1051. }
  1052. EXPORT_SYMBOL(ttm_bo_acc_size);
  1053. size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
  1054. unsigned long bo_size,
  1055. unsigned struct_size)
  1056. {
  1057. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1058. size_t size = 0;
  1059. size += ttm_round_pot(struct_size);
  1060. size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
  1061. size += ttm_round_pot(sizeof(struct ttm_dma_tt));
  1062. return size;
  1063. }
  1064. EXPORT_SYMBOL(ttm_bo_dma_acc_size);
  1065. int ttm_bo_create(struct ttm_bo_device *bdev,
  1066. unsigned long size,
  1067. enum ttm_bo_type type,
  1068. struct ttm_placement *placement,
  1069. uint32_t page_alignment,
  1070. bool interruptible,
  1071. struct file *persistent_swap_storage,
  1072. struct ttm_buffer_object **p_bo)
  1073. {
  1074. struct ttm_buffer_object *bo;
  1075. size_t acc_size;
  1076. int ret;
  1077. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  1078. if (unlikely(bo == NULL))
  1079. return -ENOMEM;
  1080. acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
  1081. ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
  1082. interruptible, persistent_swap_storage, acc_size,
  1083. NULL, NULL, NULL);
  1084. if (likely(ret == 0))
  1085. *p_bo = bo;
  1086. return ret;
  1087. }
  1088. EXPORT_SYMBOL(ttm_bo_create);
  1089. static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
  1090. unsigned mem_type, bool allow_errors)
  1091. {
  1092. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1093. struct ttm_bo_global *glob = bdev->glob;
  1094. struct fence *fence;
  1095. int ret;
  1096. /*
  1097. * Can't use standard list traversal since we're unlocking.
  1098. */
  1099. spin_lock(&glob->lru_lock);
  1100. while (!list_empty(&man->lru)) {
  1101. spin_unlock(&glob->lru_lock);
  1102. ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
  1103. if (ret) {
  1104. if (allow_errors) {
  1105. return ret;
  1106. } else {
  1107. pr_err("Cleanup eviction failed\n");
  1108. }
  1109. }
  1110. spin_lock(&glob->lru_lock);
  1111. }
  1112. spin_unlock(&glob->lru_lock);
  1113. spin_lock(&man->move_lock);
  1114. fence = fence_get(man->move);
  1115. spin_unlock(&man->move_lock);
  1116. if (fence) {
  1117. ret = fence_wait(fence, false);
  1118. fence_put(fence);
  1119. if (ret) {
  1120. if (allow_errors) {
  1121. return ret;
  1122. } else {
  1123. pr_err("Cleanup eviction failed\n");
  1124. }
  1125. }
  1126. }
  1127. return 0;
  1128. }
  1129. int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1130. {
  1131. struct ttm_mem_type_manager *man;
  1132. int ret = -EINVAL;
  1133. if (mem_type >= TTM_NUM_MEM_TYPES) {
  1134. pr_err("Illegal memory type %d\n", mem_type);
  1135. return ret;
  1136. }
  1137. man = &bdev->man[mem_type];
  1138. if (!man->has_type) {
  1139. pr_err("Trying to take down uninitialized memory manager type %u\n",
  1140. mem_type);
  1141. return ret;
  1142. }
  1143. fence_put(man->move);
  1144. man->use_type = false;
  1145. man->has_type = false;
  1146. ret = 0;
  1147. if (mem_type > 0) {
  1148. ttm_bo_force_list_clean(bdev, mem_type, false);
  1149. ret = (*man->func->takedown)(man);
  1150. }
  1151. return ret;
  1152. }
  1153. EXPORT_SYMBOL(ttm_bo_clean_mm);
  1154. int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1155. {
  1156. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1157. if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
  1158. pr_err("Illegal memory manager memory type %u\n", mem_type);
  1159. return -EINVAL;
  1160. }
  1161. if (!man->has_type) {
  1162. pr_err("Memory type %u has not been initialized\n", mem_type);
  1163. return 0;
  1164. }
  1165. return ttm_bo_force_list_clean(bdev, mem_type, true);
  1166. }
  1167. EXPORT_SYMBOL(ttm_bo_evict_mm);
  1168. int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
  1169. unsigned long p_size)
  1170. {
  1171. int ret = -EINVAL;
  1172. struct ttm_mem_type_manager *man;
  1173. BUG_ON(type >= TTM_NUM_MEM_TYPES);
  1174. man = &bdev->man[type];
  1175. BUG_ON(man->has_type);
  1176. man->io_reserve_fastpath = true;
  1177. man->use_io_reserve_lru = false;
  1178. mutex_init(&man->io_reserve_mutex);
  1179. spin_lock_init(&man->move_lock);
  1180. INIT_LIST_HEAD(&man->io_reserve_lru);
  1181. ret = bdev->driver->init_mem_type(bdev, type, man);
  1182. if (ret)
  1183. return ret;
  1184. man->bdev = bdev;
  1185. ret = 0;
  1186. if (type != TTM_PL_SYSTEM) {
  1187. ret = (*man->func->init)(man, p_size);
  1188. if (ret)
  1189. return ret;
  1190. }
  1191. man->has_type = true;
  1192. man->use_type = true;
  1193. man->size = p_size;
  1194. INIT_LIST_HEAD(&man->lru);
  1195. man->move = NULL;
  1196. return 0;
  1197. }
  1198. EXPORT_SYMBOL(ttm_bo_init_mm);
  1199. static void ttm_bo_global_kobj_release(struct kobject *kobj)
  1200. {
  1201. struct ttm_bo_global *glob =
  1202. container_of(kobj, struct ttm_bo_global, kobj);
  1203. ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
  1204. __free_page(glob->dummy_read_page);
  1205. kfree(glob);
  1206. }
  1207. void ttm_bo_global_release(struct drm_global_reference *ref)
  1208. {
  1209. struct ttm_bo_global *glob = ref->object;
  1210. kobject_del(&glob->kobj);
  1211. kobject_put(&glob->kobj);
  1212. }
  1213. EXPORT_SYMBOL(ttm_bo_global_release);
  1214. int ttm_bo_global_init(struct drm_global_reference *ref)
  1215. {
  1216. struct ttm_bo_global_ref *bo_ref =
  1217. container_of(ref, struct ttm_bo_global_ref, ref);
  1218. struct ttm_bo_global *glob = ref->object;
  1219. int ret;
  1220. mutex_init(&glob->device_list_mutex);
  1221. spin_lock_init(&glob->lru_lock);
  1222. glob->mem_glob = bo_ref->mem_glob;
  1223. glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
  1224. if (unlikely(glob->dummy_read_page == NULL)) {
  1225. ret = -ENOMEM;
  1226. goto out_no_drp;
  1227. }
  1228. INIT_LIST_HEAD(&glob->swap_lru);
  1229. INIT_LIST_HEAD(&glob->device_list);
  1230. ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
  1231. ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
  1232. if (unlikely(ret != 0)) {
  1233. pr_err("Could not register buffer object swapout\n");
  1234. goto out_no_shrink;
  1235. }
  1236. atomic_set(&glob->bo_count, 0);
  1237. ret = kobject_init_and_add(
  1238. &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
  1239. if (unlikely(ret != 0))
  1240. kobject_put(&glob->kobj);
  1241. return ret;
  1242. out_no_shrink:
  1243. __free_page(glob->dummy_read_page);
  1244. out_no_drp:
  1245. kfree(glob);
  1246. return ret;
  1247. }
  1248. EXPORT_SYMBOL(ttm_bo_global_init);
  1249. int ttm_bo_device_release(struct ttm_bo_device *bdev)
  1250. {
  1251. int ret = 0;
  1252. unsigned i = TTM_NUM_MEM_TYPES;
  1253. struct ttm_mem_type_manager *man;
  1254. struct ttm_bo_global *glob = bdev->glob;
  1255. while (i--) {
  1256. man = &bdev->man[i];
  1257. if (man->has_type) {
  1258. man->use_type = false;
  1259. if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
  1260. ret = -EBUSY;
  1261. pr_err("DRM memory manager type %d is not clean\n",
  1262. i);
  1263. }
  1264. man->has_type = false;
  1265. }
  1266. }
  1267. mutex_lock(&glob->device_list_mutex);
  1268. list_del(&bdev->device_list);
  1269. mutex_unlock(&glob->device_list_mutex);
  1270. cancel_delayed_work_sync(&bdev->wq);
  1271. while (ttm_bo_delayed_delete(bdev, true))
  1272. ;
  1273. spin_lock(&glob->lru_lock);
  1274. if (list_empty(&bdev->ddestroy))
  1275. TTM_DEBUG("Delayed destroy list was clean\n");
  1276. if (list_empty(&bdev->man[0].lru))
  1277. TTM_DEBUG("Swap list was clean\n");
  1278. spin_unlock(&glob->lru_lock);
  1279. drm_vma_offset_manager_destroy(&bdev->vma_manager);
  1280. return ret;
  1281. }
  1282. EXPORT_SYMBOL(ttm_bo_device_release);
  1283. int ttm_bo_device_init(struct ttm_bo_device *bdev,
  1284. struct ttm_bo_global *glob,
  1285. struct ttm_bo_driver *driver,
  1286. struct address_space *mapping,
  1287. uint64_t file_page_offset,
  1288. bool need_dma32)
  1289. {
  1290. int ret = -EINVAL;
  1291. bdev->driver = driver;
  1292. memset(bdev->man, 0, sizeof(bdev->man));
  1293. /*
  1294. * Initialize the system memory buffer type.
  1295. * Other types need to be driver / IOCTL initialized.
  1296. */
  1297. ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
  1298. if (unlikely(ret != 0))
  1299. goto out_no_sys;
  1300. drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
  1301. 0x10000000);
  1302. INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
  1303. INIT_LIST_HEAD(&bdev->ddestroy);
  1304. bdev->dev_mapping = mapping;
  1305. bdev->glob = glob;
  1306. bdev->need_dma32 = need_dma32;
  1307. mutex_lock(&glob->device_list_mutex);
  1308. list_add_tail(&bdev->device_list, &glob->device_list);
  1309. mutex_unlock(&glob->device_list_mutex);
  1310. return 0;
  1311. out_no_sys:
  1312. return ret;
  1313. }
  1314. EXPORT_SYMBOL(ttm_bo_device_init);
  1315. /*
  1316. * buffer object vm functions.
  1317. */
  1318. bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1319. {
  1320. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  1321. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  1322. if (mem->mem_type == TTM_PL_SYSTEM)
  1323. return false;
  1324. if (man->flags & TTM_MEMTYPE_FLAG_CMA)
  1325. return false;
  1326. if (mem->placement & TTM_PL_FLAG_CACHED)
  1327. return false;
  1328. }
  1329. return true;
  1330. }
  1331. void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
  1332. {
  1333. struct ttm_bo_device *bdev = bo->bdev;
  1334. drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
  1335. ttm_mem_io_free_vm(bo);
  1336. }
  1337. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
  1338. {
  1339. struct ttm_bo_device *bdev = bo->bdev;
  1340. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  1341. ttm_mem_io_lock(man, false);
  1342. ttm_bo_unmap_virtual_locked(bo);
  1343. ttm_mem_io_unlock(man);
  1344. }
  1345. EXPORT_SYMBOL(ttm_bo_unmap_virtual);
  1346. int ttm_bo_wait(struct ttm_buffer_object *bo,
  1347. bool interruptible, bool no_wait)
  1348. {
  1349. long timeout = no_wait ? 0 : 15 * HZ;
  1350. timeout = reservation_object_wait_timeout_rcu(bo->resv, true,
  1351. interruptible, timeout);
  1352. if (timeout < 0)
  1353. return timeout;
  1354. if (timeout == 0)
  1355. return -EBUSY;
  1356. reservation_object_add_excl_fence(bo->resv, NULL);
  1357. return 0;
  1358. }
  1359. EXPORT_SYMBOL(ttm_bo_wait);
  1360. int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
  1361. {
  1362. int ret = 0;
  1363. /*
  1364. * Using ttm_bo_reserve makes sure the lru lists are updated.
  1365. */
  1366. ret = ttm_bo_reserve(bo, true, no_wait, NULL);
  1367. if (unlikely(ret != 0))
  1368. return ret;
  1369. ret = ttm_bo_wait(bo, true, no_wait);
  1370. if (likely(ret == 0))
  1371. atomic_inc(&bo->cpu_writers);
  1372. ttm_bo_unreserve(bo);
  1373. return ret;
  1374. }
  1375. EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
  1376. void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
  1377. {
  1378. atomic_dec(&bo->cpu_writers);
  1379. }
  1380. EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
  1381. /**
  1382. * A buffer object shrink method that tries to swap out the first
  1383. * buffer object on the bo_global::swap_lru list.
  1384. */
  1385. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
  1386. {
  1387. struct ttm_bo_global *glob =
  1388. container_of(shrink, struct ttm_bo_global, shrink);
  1389. struct ttm_buffer_object *bo;
  1390. int ret = -EBUSY;
  1391. int put_count;
  1392. uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
  1393. spin_lock(&glob->lru_lock);
  1394. list_for_each_entry(bo, &glob->swap_lru, swap) {
  1395. ret = __ttm_bo_reserve(bo, false, true, NULL);
  1396. if (!ret)
  1397. break;
  1398. }
  1399. if (ret) {
  1400. spin_unlock(&glob->lru_lock);
  1401. return ret;
  1402. }
  1403. kref_get(&bo->list_kref);
  1404. if (!list_empty(&bo->ddestroy)) {
  1405. ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
  1406. kref_put(&bo->list_kref, ttm_bo_release_list);
  1407. return ret;
  1408. }
  1409. put_count = ttm_bo_del_from_lru(bo);
  1410. spin_unlock(&glob->lru_lock);
  1411. ttm_bo_list_ref_sub(bo, put_count, true);
  1412. /**
  1413. * Move to system cached
  1414. */
  1415. if ((bo->mem.placement & swap_placement) != swap_placement) {
  1416. struct ttm_mem_reg evict_mem;
  1417. evict_mem = bo->mem;
  1418. evict_mem.mm_node = NULL;
  1419. evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
  1420. evict_mem.mem_type = TTM_PL_SYSTEM;
  1421. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
  1422. false, false);
  1423. if (unlikely(ret != 0))
  1424. goto out;
  1425. }
  1426. /**
  1427. * Make sure BO is idle.
  1428. */
  1429. ret = ttm_bo_wait(bo, false, false);
  1430. if (unlikely(ret != 0))
  1431. goto out;
  1432. ttm_bo_unmap_virtual(bo);
  1433. /**
  1434. * Swap out. Buffer will be swapped in again as soon as
  1435. * anyone tries to access a ttm page.
  1436. */
  1437. if (bo->bdev->driver->swap_notify)
  1438. bo->bdev->driver->swap_notify(bo);
  1439. ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
  1440. out:
  1441. /**
  1442. *
  1443. * Unreserve without putting on LRU to avoid swapping out an
  1444. * already swapped buffer.
  1445. */
  1446. __ttm_bo_unreserve(bo);
  1447. kref_put(&bo->list_kref, ttm_bo_release_list);
  1448. return ret;
  1449. }
  1450. void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
  1451. {
  1452. while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
  1453. ;
  1454. }
  1455. EXPORT_SYMBOL(ttm_bo_swapout_all);
  1456. /**
  1457. * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
  1458. * unreserved
  1459. *
  1460. * @bo: Pointer to buffer
  1461. */
  1462. int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
  1463. {
  1464. int ret;
  1465. /*
  1466. * In the absense of a wait_unlocked API,
  1467. * Use the bo::wu_mutex to avoid triggering livelocks due to
  1468. * concurrent use of this function. Note that this use of
  1469. * bo::wu_mutex can go away if we change locking order to
  1470. * mmap_sem -> bo::reserve.
  1471. */
  1472. ret = mutex_lock_interruptible(&bo->wu_mutex);
  1473. if (unlikely(ret != 0))
  1474. return -ERESTARTSYS;
  1475. if (!ww_mutex_is_locked(&bo->resv->lock))
  1476. goto out_unlock;
  1477. ret = __ttm_bo_reserve(bo, true, false, NULL);
  1478. if (unlikely(ret != 0))
  1479. goto out_unlock;
  1480. __ttm_bo_unreserve(bo);
  1481. out_unlock:
  1482. mutex_unlock(&bo->wu_mutex);
  1483. return ret;
  1484. }