ttm_bo.c 43 KB

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