ttm_bo.c 43 KB

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