ttm_bo.c 42 KB

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