ttm_bo.c 43 KB

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