ttm_bo.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  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. if (!man->has_type || !man->use_type)
  744. continue;
  745. type_ok = ttm_bo_mt_compatible(man, mem_type, place,
  746. &cur_flags);
  747. if (!type_ok)
  748. continue;
  749. type_found = true;
  750. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  751. cur_flags);
  752. /*
  753. * Use the access and other non-mapping-related flag bits from
  754. * the memory placement flags to the current flags
  755. */
  756. ttm_flag_masked(&cur_flags, place->flags,
  757. ~TTM_PL_MASK_MEMTYPE);
  758. if (mem_type == TTM_PL_SYSTEM)
  759. break;
  760. ret = (*man->func->get_node)(man, bo, place, mem);
  761. if (unlikely(ret))
  762. return ret;
  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. for (i = 0; i < placement->num_busy_placement; ++i) {
  772. const struct ttm_place *place = &placement->busy_placement[i];
  773. ret = ttm_mem_type_from_place(place, &mem_type);
  774. if (ret)
  775. return ret;
  776. man = &bdev->man[mem_type];
  777. if (!man->has_type || !man->use_type)
  778. continue;
  779. if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
  780. continue;
  781. type_found = true;
  782. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  783. cur_flags);
  784. /*
  785. * Use the access and other non-mapping-related flag bits from
  786. * the memory placement flags to the current flags
  787. */
  788. ttm_flag_masked(&cur_flags, place->flags,
  789. ~TTM_PL_MASK_MEMTYPE);
  790. if (mem_type == TTM_PL_SYSTEM) {
  791. mem->mem_type = mem_type;
  792. mem->placement = cur_flags;
  793. mem->mm_node = NULL;
  794. return 0;
  795. }
  796. ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
  797. interruptible, no_wait_gpu);
  798. if (ret == 0 && mem->mm_node) {
  799. mem->placement = cur_flags;
  800. return 0;
  801. }
  802. if (ret == -ERESTARTSYS)
  803. has_erestartsys = true;
  804. }
  805. if (!type_found) {
  806. printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
  807. return -EINVAL;
  808. }
  809. return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
  810. }
  811. EXPORT_SYMBOL(ttm_bo_mem_space);
  812. static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  813. struct ttm_placement *placement,
  814. bool interruptible,
  815. bool no_wait_gpu)
  816. {
  817. int ret = 0;
  818. struct ttm_mem_reg mem;
  819. lockdep_assert_held(&bo->resv->lock.base);
  820. /*
  821. * FIXME: It's possible to pipeline buffer moves.
  822. * Have the driver move function wait for idle when necessary,
  823. * instead of doing it here.
  824. */
  825. ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
  826. if (ret)
  827. return ret;
  828. mem.num_pages = bo->num_pages;
  829. mem.size = mem.num_pages << PAGE_SHIFT;
  830. mem.page_alignment = bo->mem.page_alignment;
  831. mem.bus.io_reserved_vm = false;
  832. mem.bus.io_reserved_count = 0;
  833. /*
  834. * Determine where to move the buffer.
  835. */
  836. ret = ttm_bo_mem_space(bo, placement, &mem,
  837. interruptible, no_wait_gpu);
  838. if (ret)
  839. goto out_unlock;
  840. ret = ttm_bo_handle_move_mem(bo, &mem, false,
  841. interruptible, no_wait_gpu);
  842. out_unlock:
  843. if (ret && mem.mm_node)
  844. ttm_bo_mem_put(bo, &mem);
  845. return ret;
  846. }
  847. static bool ttm_bo_mem_compat(struct ttm_placement *placement,
  848. struct ttm_mem_reg *mem,
  849. uint32_t *new_flags)
  850. {
  851. int i;
  852. for (i = 0; i < placement->num_placement; i++) {
  853. const struct ttm_place *heap = &placement->placement[i];
  854. if (mem->mm_node &&
  855. (mem->start < heap->fpfn ||
  856. (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
  857. continue;
  858. *new_flags = heap->flags;
  859. if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
  860. (*new_flags & mem->placement & TTM_PL_MASK_MEM))
  861. return true;
  862. }
  863. for (i = 0; i < placement->num_busy_placement; i++) {
  864. const struct ttm_place *heap = &placement->busy_placement[i];
  865. if (mem->mm_node &&
  866. (mem->start < heap->fpfn ||
  867. (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
  868. continue;
  869. *new_flags = heap->flags;
  870. if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
  871. (*new_flags & mem->placement & TTM_PL_MASK_MEM))
  872. return true;
  873. }
  874. return false;
  875. }
  876. int ttm_bo_validate(struct ttm_buffer_object *bo,
  877. struct ttm_placement *placement,
  878. bool interruptible,
  879. bool no_wait_gpu)
  880. {
  881. int ret;
  882. uint32_t new_flags;
  883. lockdep_assert_held(&bo->resv->lock.base);
  884. /*
  885. * Check whether we need to move buffer.
  886. */
  887. if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
  888. ret = ttm_bo_move_buffer(bo, placement, interruptible,
  889. no_wait_gpu);
  890. if (ret)
  891. return ret;
  892. } else {
  893. /*
  894. * Use the access and other non-mapping-related flag bits from
  895. * the compatible memory placement flags to the active flags
  896. */
  897. ttm_flag_masked(&bo->mem.placement, new_flags,
  898. ~TTM_PL_MASK_MEMTYPE);
  899. }
  900. /*
  901. * We might need to add a TTM.
  902. */
  903. if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  904. ret = ttm_bo_add_ttm(bo, true);
  905. if (ret)
  906. return ret;
  907. }
  908. return 0;
  909. }
  910. EXPORT_SYMBOL(ttm_bo_validate);
  911. int ttm_bo_init(struct ttm_bo_device *bdev,
  912. struct ttm_buffer_object *bo,
  913. unsigned long size,
  914. enum ttm_bo_type type,
  915. struct ttm_placement *placement,
  916. uint32_t page_alignment,
  917. bool interruptible,
  918. struct file *persistent_swap_storage,
  919. size_t acc_size,
  920. struct sg_table *sg,
  921. struct reservation_object *resv,
  922. void (*destroy) (struct ttm_buffer_object *))
  923. {
  924. int ret = 0;
  925. unsigned long num_pages;
  926. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  927. bool locked;
  928. ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  929. if (ret) {
  930. pr_err("Out of kernel memory\n");
  931. if (destroy)
  932. (*destroy)(bo);
  933. else
  934. kfree(bo);
  935. return -ENOMEM;
  936. }
  937. num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  938. if (num_pages == 0) {
  939. pr_err("Illegal buffer object size\n");
  940. if (destroy)
  941. (*destroy)(bo);
  942. else
  943. kfree(bo);
  944. ttm_mem_global_free(mem_glob, acc_size);
  945. return -EINVAL;
  946. }
  947. bo->destroy = destroy;
  948. kref_init(&bo->kref);
  949. kref_init(&bo->list_kref);
  950. atomic_set(&bo->cpu_writers, 0);
  951. INIT_LIST_HEAD(&bo->lru);
  952. INIT_LIST_HEAD(&bo->ddestroy);
  953. INIT_LIST_HEAD(&bo->swap);
  954. INIT_LIST_HEAD(&bo->io_reserve_lru);
  955. mutex_init(&bo->wu_mutex);
  956. bo->bdev = bdev;
  957. bo->glob = bdev->glob;
  958. bo->type = type;
  959. bo->num_pages = num_pages;
  960. bo->mem.size = num_pages << PAGE_SHIFT;
  961. bo->mem.mem_type = TTM_PL_SYSTEM;
  962. bo->mem.num_pages = bo->num_pages;
  963. bo->mem.mm_node = NULL;
  964. bo->mem.page_alignment = page_alignment;
  965. bo->mem.bus.io_reserved_vm = false;
  966. bo->mem.bus.io_reserved_count = 0;
  967. bo->priv_flags = 0;
  968. bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
  969. bo->persistent_swap_storage = persistent_swap_storage;
  970. bo->acc_size = acc_size;
  971. bo->sg = sg;
  972. if (resv) {
  973. bo->resv = resv;
  974. lockdep_assert_held(&bo->resv->lock.base);
  975. } else {
  976. bo->resv = &bo->ttm_resv;
  977. reservation_object_init(&bo->ttm_resv);
  978. }
  979. atomic_inc(&bo->glob->bo_count);
  980. drm_vma_node_reset(&bo->vma_node);
  981. /*
  982. * For ttm_bo_type_device buffers, allocate
  983. * address space from the device.
  984. */
  985. if (bo->type == ttm_bo_type_device ||
  986. bo->type == ttm_bo_type_sg)
  987. ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
  988. bo->mem.num_pages);
  989. /* passed reservation objects should already be locked,
  990. * since otherwise lockdep will be angered in radeon.
  991. */
  992. if (!resv) {
  993. locked = ww_mutex_trylock(&bo->resv->lock);
  994. WARN_ON(!locked);
  995. }
  996. if (likely(!ret))
  997. ret = ttm_bo_validate(bo, placement, interruptible, false);
  998. if (!resv)
  999. ttm_bo_unreserve(bo);
  1000. if (unlikely(ret))
  1001. ttm_bo_unref(&bo);
  1002. return ret;
  1003. }
  1004. EXPORT_SYMBOL(ttm_bo_init);
  1005. size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
  1006. unsigned long bo_size,
  1007. unsigned struct_size)
  1008. {
  1009. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1010. size_t size = 0;
  1011. size += ttm_round_pot(struct_size);
  1012. size += PAGE_ALIGN(npages * sizeof(void *));
  1013. size += ttm_round_pot(sizeof(struct ttm_tt));
  1014. return size;
  1015. }
  1016. EXPORT_SYMBOL(ttm_bo_acc_size);
  1017. size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
  1018. unsigned long bo_size,
  1019. unsigned struct_size)
  1020. {
  1021. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1022. size_t size = 0;
  1023. size += ttm_round_pot(struct_size);
  1024. size += PAGE_ALIGN(npages * sizeof(void *));
  1025. size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
  1026. size += ttm_round_pot(sizeof(struct ttm_dma_tt));
  1027. return size;
  1028. }
  1029. EXPORT_SYMBOL(ttm_bo_dma_acc_size);
  1030. int ttm_bo_create(struct ttm_bo_device *bdev,
  1031. unsigned long size,
  1032. enum ttm_bo_type type,
  1033. struct ttm_placement *placement,
  1034. uint32_t page_alignment,
  1035. bool interruptible,
  1036. struct file *persistent_swap_storage,
  1037. struct ttm_buffer_object **p_bo)
  1038. {
  1039. struct ttm_buffer_object *bo;
  1040. size_t acc_size;
  1041. int ret;
  1042. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  1043. if (unlikely(bo == NULL))
  1044. return -ENOMEM;
  1045. acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
  1046. ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
  1047. interruptible, persistent_swap_storage, acc_size,
  1048. NULL, NULL, NULL);
  1049. if (likely(ret == 0))
  1050. *p_bo = bo;
  1051. return ret;
  1052. }
  1053. EXPORT_SYMBOL(ttm_bo_create);
  1054. static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
  1055. unsigned mem_type, bool allow_errors)
  1056. {
  1057. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1058. struct ttm_bo_global *glob = bdev->glob;
  1059. int ret;
  1060. /*
  1061. * Can't use standard list traversal since we're unlocking.
  1062. */
  1063. spin_lock(&glob->lru_lock);
  1064. while (!list_empty(&man->lru)) {
  1065. spin_unlock(&glob->lru_lock);
  1066. ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
  1067. if (ret) {
  1068. if (allow_errors) {
  1069. return ret;
  1070. } else {
  1071. pr_err("Cleanup eviction failed\n");
  1072. }
  1073. }
  1074. spin_lock(&glob->lru_lock);
  1075. }
  1076. spin_unlock(&glob->lru_lock);
  1077. return 0;
  1078. }
  1079. int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1080. {
  1081. struct ttm_mem_type_manager *man;
  1082. int ret = -EINVAL;
  1083. if (mem_type >= TTM_NUM_MEM_TYPES) {
  1084. pr_err("Illegal memory type %d\n", mem_type);
  1085. return ret;
  1086. }
  1087. man = &bdev->man[mem_type];
  1088. if (!man->has_type) {
  1089. pr_err("Trying to take down uninitialized memory manager type %u\n",
  1090. mem_type);
  1091. return ret;
  1092. }
  1093. man->use_type = false;
  1094. man->has_type = false;
  1095. ret = 0;
  1096. if (mem_type > 0) {
  1097. ttm_bo_force_list_clean(bdev, mem_type, false);
  1098. ret = (*man->func->takedown)(man);
  1099. }
  1100. return ret;
  1101. }
  1102. EXPORT_SYMBOL(ttm_bo_clean_mm);
  1103. int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1104. {
  1105. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1106. if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
  1107. pr_err("Illegal memory manager memory type %u\n", mem_type);
  1108. return -EINVAL;
  1109. }
  1110. if (!man->has_type) {
  1111. pr_err("Memory type %u has not been initialized\n", mem_type);
  1112. return 0;
  1113. }
  1114. return ttm_bo_force_list_clean(bdev, mem_type, true);
  1115. }
  1116. EXPORT_SYMBOL(ttm_bo_evict_mm);
  1117. int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
  1118. unsigned long p_size)
  1119. {
  1120. int ret = -EINVAL;
  1121. struct ttm_mem_type_manager *man;
  1122. BUG_ON(type >= TTM_NUM_MEM_TYPES);
  1123. man = &bdev->man[type];
  1124. BUG_ON(man->has_type);
  1125. man->io_reserve_fastpath = true;
  1126. man->use_io_reserve_lru = false;
  1127. mutex_init(&man->io_reserve_mutex);
  1128. INIT_LIST_HEAD(&man->io_reserve_lru);
  1129. ret = bdev->driver->init_mem_type(bdev, type, man);
  1130. if (ret)
  1131. return ret;
  1132. man->bdev = bdev;
  1133. ret = 0;
  1134. if (type != TTM_PL_SYSTEM) {
  1135. ret = (*man->func->init)(man, p_size);
  1136. if (ret)
  1137. return ret;
  1138. }
  1139. man->has_type = true;
  1140. man->use_type = true;
  1141. man->size = p_size;
  1142. INIT_LIST_HEAD(&man->lru);
  1143. return 0;
  1144. }
  1145. EXPORT_SYMBOL(ttm_bo_init_mm);
  1146. static void ttm_bo_global_kobj_release(struct kobject *kobj)
  1147. {
  1148. struct ttm_bo_global *glob =
  1149. container_of(kobj, struct ttm_bo_global, kobj);
  1150. ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
  1151. __free_page(glob->dummy_read_page);
  1152. kfree(glob);
  1153. }
  1154. void ttm_bo_global_release(struct drm_global_reference *ref)
  1155. {
  1156. struct ttm_bo_global *glob = ref->object;
  1157. kobject_del(&glob->kobj);
  1158. kobject_put(&glob->kobj);
  1159. }
  1160. EXPORT_SYMBOL(ttm_bo_global_release);
  1161. int ttm_bo_global_init(struct drm_global_reference *ref)
  1162. {
  1163. struct ttm_bo_global_ref *bo_ref =
  1164. container_of(ref, struct ttm_bo_global_ref, ref);
  1165. struct ttm_bo_global *glob = ref->object;
  1166. int ret;
  1167. mutex_init(&glob->device_list_mutex);
  1168. spin_lock_init(&glob->lru_lock);
  1169. glob->mem_glob = bo_ref->mem_glob;
  1170. glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
  1171. if (unlikely(glob->dummy_read_page == NULL)) {
  1172. ret = -ENOMEM;
  1173. goto out_no_drp;
  1174. }
  1175. INIT_LIST_HEAD(&glob->swap_lru);
  1176. INIT_LIST_HEAD(&glob->device_list);
  1177. ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
  1178. ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
  1179. if (unlikely(ret != 0)) {
  1180. pr_err("Could not register buffer object swapout\n");
  1181. goto out_no_shrink;
  1182. }
  1183. atomic_set(&glob->bo_count, 0);
  1184. ret = kobject_init_and_add(
  1185. &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
  1186. if (unlikely(ret != 0))
  1187. kobject_put(&glob->kobj);
  1188. return ret;
  1189. out_no_shrink:
  1190. __free_page(glob->dummy_read_page);
  1191. out_no_drp:
  1192. kfree(glob);
  1193. return ret;
  1194. }
  1195. EXPORT_SYMBOL(ttm_bo_global_init);
  1196. int ttm_bo_device_release(struct ttm_bo_device *bdev)
  1197. {
  1198. int ret = 0;
  1199. unsigned i = TTM_NUM_MEM_TYPES;
  1200. struct ttm_mem_type_manager *man;
  1201. struct ttm_bo_global *glob = bdev->glob;
  1202. while (i--) {
  1203. man = &bdev->man[i];
  1204. if (man->has_type) {
  1205. man->use_type = false;
  1206. if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
  1207. ret = -EBUSY;
  1208. pr_err("DRM memory manager type %d is not clean\n",
  1209. i);
  1210. }
  1211. man->has_type = false;
  1212. }
  1213. }
  1214. mutex_lock(&glob->device_list_mutex);
  1215. list_del(&bdev->device_list);
  1216. mutex_unlock(&glob->device_list_mutex);
  1217. cancel_delayed_work_sync(&bdev->wq);
  1218. while (ttm_bo_delayed_delete(bdev, true))
  1219. ;
  1220. spin_lock(&glob->lru_lock);
  1221. if (list_empty(&bdev->ddestroy))
  1222. TTM_DEBUG("Delayed destroy list was clean\n");
  1223. if (list_empty(&bdev->man[0].lru))
  1224. TTM_DEBUG("Swap list was clean\n");
  1225. spin_unlock(&glob->lru_lock);
  1226. drm_vma_offset_manager_destroy(&bdev->vma_manager);
  1227. return ret;
  1228. }
  1229. EXPORT_SYMBOL(ttm_bo_device_release);
  1230. int ttm_bo_device_init(struct ttm_bo_device *bdev,
  1231. struct ttm_bo_global *glob,
  1232. struct ttm_bo_driver *driver,
  1233. struct address_space *mapping,
  1234. uint64_t file_page_offset,
  1235. bool need_dma32)
  1236. {
  1237. int ret = -EINVAL;
  1238. bdev->driver = driver;
  1239. memset(bdev->man, 0, sizeof(bdev->man));
  1240. /*
  1241. * Initialize the system memory buffer type.
  1242. * Other types need to be driver / IOCTL initialized.
  1243. */
  1244. ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
  1245. if (unlikely(ret != 0))
  1246. goto out_no_sys;
  1247. drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
  1248. 0x10000000);
  1249. INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
  1250. INIT_LIST_HEAD(&bdev->ddestroy);
  1251. bdev->dev_mapping = mapping;
  1252. bdev->glob = glob;
  1253. bdev->need_dma32 = need_dma32;
  1254. bdev->val_seq = 0;
  1255. mutex_lock(&glob->device_list_mutex);
  1256. list_add_tail(&bdev->device_list, &glob->device_list);
  1257. mutex_unlock(&glob->device_list_mutex);
  1258. return 0;
  1259. out_no_sys:
  1260. return ret;
  1261. }
  1262. EXPORT_SYMBOL(ttm_bo_device_init);
  1263. /*
  1264. * buffer object vm functions.
  1265. */
  1266. bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1267. {
  1268. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  1269. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  1270. if (mem->mem_type == TTM_PL_SYSTEM)
  1271. return false;
  1272. if (man->flags & TTM_MEMTYPE_FLAG_CMA)
  1273. return false;
  1274. if (mem->placement & TTM_PL_FLAG_CACHED)
  1275. return false;
  1276. }
  1277. return true;
  1278. }
  1279. void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
  1280. {
  1281. struct ttm_bo_device *bdev = bo->bdev;
  1282. drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
  1283. ttm_mem_io_free_vm(bo);
  1284. }
  1285. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
  1286. {
  1287. struct ttm_bo_device *bdev = bo->bdev;
  1288. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  1289. ttm_mem_io_lock(man, false);
  1290. ttm_bo_unmap_virtual_locked(bo);
  1291. ttm_mem_io_unlock(man);
  1292. }
  1293. EXPORT_SYMBOL(ttm_bo_unmap_virtual);
  1294. int ttm_bo_wait(struct ttm_buffer_object *bo,
  1295. bool lazy, bool interruptible, bool no_wait)
  1296. {
  1297. struct reservation_object_list *fobj;
  1298. struct reservation_object *resv;
  1299. struct fence *excl;
  1300. long timeout = 15 * HZ;
  1301. int i;
  1302. resv = bo->resv;
  1303. fobj = reservation_object_get_list(resv);
  1304. excl = reservation_object_get_excl(resv);
  1305. if (excl) {
  1306. if (!fence_is_signaled(excl)) {
  1307. if (no_wait)
  1308. return -EBUSY;
  1309. timeout = fence_wait_timeout(excl,
  1310. interruptible, timeout);
  1311. }
  1312. }
  1313. for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) {
  1314. struct fence *fence;
  1315. fence = rcu_dereference_protected(fobj->shared[i],
  1316. reservation_object_held(resv));
  1317. if (!fence_is_signaled(fence)) {
  1318. if (no_wait)
  1319. return -EBUSY;
  1320. timeout = fence_wait_timeout(fence,
  1321. interruptible, timeout);
  1322. }
  1323. }
  1324. if (timeout < 0)
  1325. return timeout;
  1326. if (timeout == 0)
  1327. return -EBUSY;
  1328. reservation_object_add_excl_fence(resv, NULL);
  1329. clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
  1330. return 0;
  1331. }
  1332. EXPORT_SYMBOL(ttm_bo_wait);
  1333. int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
  1334. {
  1335. int ret = 0;
  1336. /*
  1337. * Using ttm_bo_reserve makes sure the lru lists are updated.
  1338. */
  1339. ret = ttm_bo_reserve(bo, true, no_wait, false, NULL);
  1340. if (unlikely(ret != 0))
  1341. return ret;
  1342. ret = ttm_bo_wait(bo, false, true, no_wait);
  1343. if (likely(ret == 0))
  1344. atomic_inc(&bo->cpu_writers);
  1345. ttm_bo_unreserve(bo);
  1346. return ret;
  1347. }
  1348. EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
  1349. void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
  1350. {
  1351. atomic_dec(&bo->cpu_writers);
  1352. }
  1353. EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
  1354. /**
  1355. * A buffer object shrink method that tries to swap out the first
  1356. * buffer object on the bo_global::swap_lru list.
  1357. */
  1358. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
  1359. {
  1360. struct ttm_bo_global *glob =
  1361. container_of(shrink, struct ttm_bo_global, shrink);
  1362. struct ttm_buffer_object *bo;
  1363. int ret = -EBUSY;
  1364. int put_count;
  1365. uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
  1366. spin_lock(&glob->lru_lock);
  1367. list_for_each_entry(bo, &glob->swap_lru, swap) {
  1368. ret = __ttm_bo_reserve(bo, false, true, false, NULL);
  1369. if (!ret)
  1370. break;
  1371. }
  1372. if (ret) {
  1373. spin_unlock(&glob->lru_lock);
  1374. return ret;
  1375. }
  1376. kref_get(&bo->list_kref);
  1377. if (!list_empty(&bo->ddestroy)) {
  1378. ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
  1379. kref_put(&bo->list_kref, ttm_bo_release_list);
  1380. return ret;
  1381. }
  1382. put_count = ttm_bo_del_from_lru(bo);
  1383. spin_unlock(&glob->lru_lock);
  1384. ttm_bo_list_ref_sub(bo, put_count, true);
  1385. /**
  1386. * Wait for GPU, then move to system cached.
  1387. */
  1388. ret = ttm_bo_wait(bo, false, false, false);
  1389. if (unlikely(ret != 0))
  1390. goto out;
  1391. if ((bo->mem.placement & swap_placement) != swap_placement) {
  1392. struct ttm_mem_reg evict_mem;
  1393. evict_mem = bo->mem;
  1394. evict_mem.mm_node = NULL;
  1395. evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
  1396. evict_mem.mem_type = TTM_PL_SYSTEM;
  1397. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
  1398. false, false);
  1399. if (unlikely(ret != 0))
  1400. goto out;
  1401. }
  1402. ttm_bo_unmap_virtual(bo);
  1403. /**
  1404. * Swap out. Buffer will be swapped in again as soon as
  1405. * anyone tries to access a ttm page.
  1406. */
  1407. if (bo->bdev->driver->swap_notify)
  1408. bo->bdev->driver->swap_notify(bo);
  1409. ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
  1410. out:
  1411. /**
  1412. *
  1413. * Unreserve without putting on LRU to avoid swapping out an
  1414. * already swapped buffer.
  1415. */
  1416. __ttm_bo_unreserve(bo);
  1417. kref_put(&bo->list_kref, ttm_bo_release_list);
  1418. return ret;
  1419. }
  1420. void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
  1421. {
  1422. while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
  1423. ;
  1424. }
  1425. EXPORT_SYMBOL(ttm_bo_swapout_all);
  1426. /**
  1427. * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
  1428. * unreserved
  1429. *
  1430. * @bo: Pointer to buffer
  1431. */
  1432. int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
  1433. {
  1434. int ret;
  1435. /*
  1436. * In the absense of a wait_unlocked API,
  1437. * Use the bo::wu_mutex to avoid triggering livelocks due to
  1438. * concurrent use of this function. Note that this use of
  1439. * bo::wu_mutex can go away if we change locking order to
  1440. * mmap_sem -> bo::reserve.
  1441. */
  1442. ret = mutex_lock_interruptible(&bo->wu_mutex);
  1443. if (unlikely(ret != 0))
  1444. return -ERESTARTSYS;
  1445. if (!ww_mutex_is_locked(&bo->resv->lock))
  1446. goto out_unlock;
  1447. ret = __ttm_bo_reserve(bo, true, false, false, NULL);
  1448. if (unlikely(ret != 0))
  1449. goto out_unlock;
  1450. __ttm_bo_unreserve(bo);
  1451. out_unlock:
  1452. mutex_unlock(&bo->wu_mutex);
  1453. return ret;
  1454. }