ttm_bo.c 43 KB

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