ttm_bo.c 42 KB

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