ttm_bo.c 41 KB

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