ttm_bo.c 42 KB

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