ttm_bo.c 45 KB

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