nouveau_bo.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  1. /*
  2. * Copyright 2007 Dave Airlied
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. /*
  25. * Authors: Dave Airlied <airlied@linux.ie>
  26. * Ben Skeggs <darktama@iinet.net.au>
  27. * Jeremy Kolb <jkolb@brandeis.edu>
  28. */
  29. #include "drmP.h"
  30. #include "ttm/ttm_page_alloc.h"
  31. #include <nouveau_drm.h>
  32. #include "nouveau_drv.h"
  33. #include "nouveau_dma.h"
  34. #include <core/mm.h>
  35. #include "nouveau_fence.h"
  36. #include <core/ramht.h>
  37. #include <engine/fifo.h>
  38. #include <linux/log2.h>
  39. #include <linux/slab.h>
  40. /*
  41. * NV10-NV40 tiling helpers
  42. */
  43. static void
  44. nv10_bo_update_tile_region(struct drm_device *dev,
  45. struct nouveau_tile_reg *tilereg, uint32_t addr,
  46. uint32_t size, uint32_t pitch, uint32_t flags)
  47. {
  48. struct drm_nouveau_private *dev_priv = dev->dev_private;
  49. int i = tilereg - dev_priv->tile.reg, j;
  50. struct nouveau_fb_tile *tile = nvfb_tile(dev, i);
  51. unsigned long save;
  52. nouveau_fence_unref(&tilereg->fence);
  53. if (tile->pitch)
  54. nvfb_tile_fini(dev, i);
  55. if (pitch)
  56. nvfb_tile_init(dev, i, addr, size, pitch, flags);
  57. spin_lock_irqsave(&dev_priv->context_switch_lock, save);
  58. nv_wr32(dev, NV03_PFIFO_CACHES, 0);
  59. nv04_fifo_cache_pull(dev, false);
  60. nouveau_wait_for_idle(dev);
  61. nvfb_tile_prog(dev, i);
  62. for (j = 0; j < NVOBJ_ENGINE_NR; j++) {
  63. if (dev_priv->eng[j] && dev_priv->eng[j]->set_tile_region)
  64. dev_priv->eng[j]->set_tile_region(dev, i);
  65. }
  66. nv04_fifo_cache_pull(dev, true);
  67. nv_wr32(dev, NV03_PFIFO_CACHES, 1);
  68. spin_unlock_irqrestore(&dev_priv->context_switch_lock, save);
  69. }
  70. static struct nouveau_tile_reg *
  71. nv10_bo_get_tile_region(struct drm_device *dev, int i)
  72. {
  73. struct drm_nouveau_private *dev_priv = dev->dev_private;
  74. struct nouveau_tile_reg *tile = &dev_priv->tile.reg[i];
  75. spin_lock(&dev_priv->tile.lock);
  76. if (!tile->used &&
  77. (!tile->fence || nouveau_fence_done(tile->fence)))
  78. tile->used = true;
  79. else
  80. tile = NULL;
  81. spin_unlock(&dev_priv->tile.lock);
  82. return tile;
  83. }
  84. static void
  85. nv10_bo_put_tile_region(struct drm_device *dev, struct nouveau_tile_reg *tile,
  86. struct nouveau_fence *fence)
  87. {
  88. struct drm_nouveau_private *dev_priv = dev->dev_private;
  89. if (tile) {
  90. spin_lock(&dev_priv->tile.lock);
  91. if (fence) {
  92. /* Mark it as pending. */
  93. tile->fence = fence;
  94. nouveau_fence_ref(fence);
  95. }
  96. tile->used = false;
  97. spin_unlock(&dev_priv->tile.lock);
  98. }
  99. }
  100. static struct nouveau_tile_reg *
  101. nv10_bo_set_tiling(struct drm_device *dev, uint32_t addr, uint32_t size,
  102. uint32_t pitch, uint32_t flags)
  103. {
  104. struct nouveau_tile_reg *tile, *found = NULL;
  105. int i;
  106. for (i = 0; i < nvfb_tile_nr(dev); i++) {
  107. tile = nv10_bo_get_tile_region(dev, i);
  108. if (pitch && !found) {
  109. found = tile;
  110. continue;
  111. } else if (tile && nvfb_tile(dev, i)->pitch) {
  112. /* Kill an unused tile region. */
  113. nv10_bo_update_tile_region(dev, tile, 0, 0, 0, 0);
  114. }
  115. nv10_bo_put_tile_region(dev, tile, NULL);
  116. }
  117. if (found)
  118. nv10_bo_update_tile_region(dev, found, addr, size,
  119. pitch, flags);
  120. return found;
  121. }
  122. static void
  123. nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
  124. {
  125. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  126. struct drm_device *dev = dev_priv->dev;
  127. struct nouveau_bo *nvbo = nouveau_bo(bo);
  128. if (unlikely(nvbo->gem))
  129. DRM_ERROR("bo %p still attached to GEM object\n", bo);
  130. nv10_bo_put_tile_region(dev, nvbo->tile, NULL);
  131. kfree(nvbo);
  132. }
  133. static void
  134. nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags,
  135. int *align, int *size)
  136. {
  137. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  138. if (dev_priv->card_type < NV_50) {
  139. if (nvbo->tile_mode) {
  140. if (dev_priv->chipset >= 0x40) {
  141. *align = 65536;
  142. *size = roundup(*size, 64 * nvbo->tile_mode);
  143. } else if (dev_priv->chipset >= 0x30) {
  144. *align = 32768;
  145. *size = roundup(*size, 64 * nvbo->tile_mode);
  146. } else if (dev_priv->chipset >= 0x20) {
  147. *align = 16384;
  148. *size = roundup(*size, 64 * nvbo->tile_mode);
  149. } else if (dev_priv->chipset >= 0x10) {
  150. *align = 16384;
  151. *size = roundup(*size, 32 * nvbo->tile_mode);
  152. }
  153. }
  154. } else {
  155. *size = roundup(*size, (1 << nvbo->page_shift));
  156. *align = max((1 << nvbo->page_shift), *align);
  157. }
  158. *size = roundup(*size, PAGE_SIZE);
  159. }
  160. int
  161. nouveau_bo_new(struct drm_device *dev, int size, int align,
  162. uint32_t flags, uint32_t tile_mode, uint32_t tile_flags,
  163. struct sg_table *sg,
  164. struct nouveau_bo **pnvbo)
  165. {
  166. struct drm_nouveau_private *dev_priv = dev->dev_private;
  167. struct nouveau_bo *nvbo;
  168. size_t acc_size;
  169. int ret;
  170. int type = ttm_bo_type_device;
  171. if (sg)
  172. type = ttm_bo_type_sg;
  173. nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
  174. if (!nvbo)
  175. return -ENOMEM;
  176. INIT_LIST_HEAD(&nvbo->head);
  177. INIT_LIST_HEAD(&nvbo->entry);
  178. INIT_LIST_HEAD(&nvbo->vma_list);
  179. nvbo->tile_mode = tile_mode;
  180. nvbo->tile_flags = tile_flags;
  181. nvbo->bo.bdev = &dev_priv->ttm.bdev;
  182. nvbo->page_shift = 12;
  183. if (dev_priv->chan_vm) {
  184. if (!(flags & TTM_PL_FLAG_TT) && size > 256 * 1024)
  185. nvbo->page_shift = nvvm_lpg_shift(dev_priv->chan_vm);
  186. }
  187. nouveau_bo_fixup_align(nvbo, flags, &align, &size);
  188. nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
  189. nouveau_bo_placement_set(nvbo, flags, 0);
  190. acc_size = ttm_bo_dma_acc_size(&dev_priv->ttm.bdev, size,
  191. sizeof(struct nouveau_bo));
  192. ret = ttm_bo_init(&dev_priv->ttm.bdev, &nvbo->bo, size,
  193. type, &nvbo->placement,
  194. align >> PAGE_SHIFT, 0, false, NULL, acc_size, sg,
  195. nouveau_bo_del_ttm);
  196. if (ret) {
  197. /* ttm will call nouveau_bo_del_ttm if it fails.. */
  198. return ret;
  199. }
  200. *pnvbo = nvbo;
  201. return 0;
  202. }
  203. static void
  204. set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
  205. {
  206. *n = 0;
  207. if (type & TTM_PL_FLAG_VRAM)
  208. pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
  209. if (type & TTM_PL_FLAG_TT)
  210. pl[(*n)++] = TTM_PL_FLAG_TT | flags;
  211. if (type & TTM_PL_FLAG_SYSTEM)
  212. pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
  213. }
  214. static void
  215. set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
  216. {
  217. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  218. int vram_pages = nvfb_vram_size(dev_priv->dev) >> PAGE_SHIFT;
  219. if (dev_priv->card_type == NV_10 &&
  220. nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM) &&
  221. nvbo->bo.mem.num_pages < vram_pages / 4) {
  222. /*
  223. * Make sure that the color and depth buffers are handled
  224. * by independent memory controller units. Up to a 9x
  225. * speed up when alpha-blending and depth-test are enabled
  226. * at the same time.
  227. */
  228. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
  229. nvbo->placement.fpfn = vram_pages / 2;
  230. nvbo->placement.lpfn = ~0;
  231. } else {
  232. nvbo->placement.fpfn = 0;
  233. nvbo->placement.lpfn = vram_pages / 2;
  234. }
  235. }
  236. }
  237. void
  238. nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
  239. {
  240. struct ttm_placement *pl = &nvbo->placement;
  241. uint32_t flags = TTM_PL_MASK_CACHING |
  242. (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
  243. pl->placement = nvbo->placements;
  244. set_placement_list(nvbo->placements, &pl->num_placement,
  245. type, flags);
  246. pl->busy_placement = nvbo->busy_placements;
  247. set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
  248. type | busy, flags);
  249. set_placement_range(nvbo, type);
  250. }
  251. int
  252. nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
  253. {
  254. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  255. struct ttm_buffer_object *bo = &nvbo->bo;
  256. int ret;
  257. if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
  258. NV_ERROR(nouveau_bdev(bo->bdev)->dev,
  259. "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
  260. 1 << bo->mem.mem_type, memtype);
  261. return -EINVAL;
  262. }
  263. if (nvbo->pin_refcnt++)
  264. return 0;
  265. ret = ttm_bo_reserve(bo, false, false, false, 0);
  266. if (ret)
  267. goto out;
  268. nouveau_bo_placement_set(nvbo, memtype, 0);
  269. ret = nouveau_bo_validate(nvbo, false, false, false);
  270. if (ret == 0) {
  271. switch (bo->mem.mem_type) {
  272. case TTM_PL_VRAM:
  273. dev_priv->fb_aper_free -= bo->mem.size;
  274. break;
  275. case TTM_PL_TT:
  276. dev_priv->gart_info.aper_free -= bo->mem.size;
  277. break;
  278. default:
  279. break;
  280. }
  281. }
  282. ttm_bo_unreserve(bo);
  283. out:
  284. if (unlikely(ret))
  285. nvbo->pin_refcnt--;
  286. return ret;
  287. }
  288. int
  289. nouveau_bo_unpin(struct nouveau_bo *nvbo)
  290. {
  291. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  292. struct ttm_buffer_object *bo = &nvbo->bo;
  293. int ret;
  294. if (--nvbo->pin_refcnt)
  295. return 0;
  296. ret = ttm_bo_reserve(bo, false, false, false, 0);
  297. if (ret)
  298. return ret;
  299. nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
  300. ret = nouveau_bo_validate(nvbo, false, false, false);
  301. if (ret == 0) {
  302. switch (bo->mem.mem_type) {
  303. case TTM_PL_VRAM:
  304. dev_priv->fb_aper_free += bo->mem.size;
  305. break;
  306. case TTM_PL_TT:
  307. dev_priv->gart_info.aper_free += bo->mem.size;
  308. break;
  309. default:
  310. break;
  311. }
  312. }
  313. ttm_bo_unreserve(bo);
  314. return ret;
  315. }
  316. int
  317. nouveau_bo_map(struct nouveau_bo *nvbo)
  318. {
  319. int ret;
  320. ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
  321. if (ret)
  322. return ret;
  323. ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
  324. ttm_bo_unreserve(&nvbo->bo);
  325. return ret;
  326. }
  327. void
  328. nouveau_bo_unmap(struct nouveau_bo *nvbo)
  329. {
  330. if (nvbo)
  331. ttm_bo_kunmap(&nvbo->kmap);
  332. }
  333. int
  334. nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
  335. bool no_wait_reserve, bool no_wait_gpu)
  336. {
  337. int ret;
  338. ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, interruptible,
  339. no_wait_reserve, no_wait_gpu);
  340. if (ret)
  341. return ret;
  342. return 0;
  343. }
  344. u16
  345. nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
  346. {
  347. bool is_iomem;
  348. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  349. mem = &mem[index];
  350. if (is_iomem)
  351. return ioread16_native((void __force __iomem *)mem);
  352. else
  353. return *mem;
  354. }
  355. void
  356. nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
  357. {
  358. bool is_iomem;
  359. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  360. mem = &mem[index];
  361. if (is_iomem)
  362. iowrite16_native(val, (void __force __iomem *)mem);
  363. else
  364. *mem = val;
  365. }
  366. u32
  367. nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
  368. {
  369. bool is_iomem;
  370. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  371. mem = &mem[index];
  372. if (is_iomem)
  373. return ioread32_native((void __force __iomem *)mem);
  374. else
  375. return *mem;
  376. }
  377. void
  378. nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
  379. {
  380. bool is_iomem;
  381. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  382. mem = &mem[index];
  383. if (is_iomem)
  384. iowrite32_native(val, (void __force __iomem *)mem);
  385. else
  386. *mem = val;
  387. }
  388. static struct ttm_tt *
  389. nouveau_ttm_tt_create(struct ttm_bo_device *bdev,
  390. unsigned long size, uint32_t page_flags,
  391. struct page *dummy_read_page)
  392. {
  393. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  394. struct drm_device *dev = dev_priv->dev;
  395. switch (dev_priv->gart_info.type) {
  396. #if __OS_HAS_AGP
  397. case NOUVEAU_GART_AGP:
  398. return ttm_agp_tt_create(bdev, dev->agp->bridge,
  399. size, page_flags, dummy_read_page);
  400. #endif
  401. case NOUVEAU_GART_PDMA:
  402. case NOUVEAU_GART_HW:
  403. return nouveau_sgdma_create_ttm(bdev, size, page_flags,
  404. dummy_read_page);
  405. default:
  406. NV_ERROR(dev, "Unknown GART type %d\n",
  407. dev_priv->gart_info.type);
  408. break;
  409. }
  410. return NULL;
  411. }
  412. static int
  413. nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  414. {
  415. /* We'll do this from user space. */
  416. return 0;
  417. }
  418. static int
  419. nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  420. struct ttm_mem_type_manager *man)
  421. {
  422. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  423. struct drm_device *dev = dev_priv->dev;
  424. switch (type) {
  425. case TTM_PL_SYSTEM:
  426. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  427. man->available_caching = TTM_PL_MASK_CACHING;
  428. man->default_caching = TTM_PL_FLAG_CACHED;
  429. break;
  430. case TTM_PL_VRAM:
  431. if (dev_priv->card_type >= NV_50) {
  432. man->func = &nouveau_vram_manager;
  433. man->io_reserve_fastpath = false;
  434. man->use_io_reserve_lru = true;
  435. } else {
  436. man->func = &ttm_bo_manager_func;
  437. }
  438. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  439. TTM_MEMTYPE_FLAG_MAPPABLE;
  440. man->available_caching = TTM_PL_FLAG_UNCACHED |
  441. TTM_PL_FLAG_WC;
  442. man->default_caching = TTM_PL_FLAG_WC;
  443. break;
  444. case TTM_PL_TT:
  445. if (dev_priv->card_type >= NV_50)
  446. man->func = &nouveau_gart_manager;
  447. else
  448. if (dev_priv->gart_info.type != NOUVEAU_GART_AGP)
  449. man->func = &nv04_gart_manager;
  450. else
  451. man->func = &ttm_bo_manager_func;
  452. switch (dev_priv->gart_info.type) {
  453. case NOUVEAU_GART_AGP:
  454. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  455. man->available_caching = TTM_PL_FLAG_UNCACHED |
  456. TTM_PL_FLAG_WC;
  457. man->default_caching = TTM_PL_FLAG_WC;
  458. break;
  459. case NOUVEAU_GART_PDMA:
  460. case NOUVEAU_GART_HW:
  461. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
  462. TTM_MEMTYPE_FLAG_CMA;
  463. man->available_caching = TTM_PL_MASK_CACHING;
  464. man->default_caching = TTM_PL_FLAG_CACHED;
  465. break;
  466. default:
  467. NV_ERROR(dev, "Unknown GART type: %d\n",
  468. dev_priv->gart_info.type);
  469. return -EINVAL;
  470. }
  471. break;
  472. default:
  473. NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
  474. return -EINVAL;
  475. }
  476. return 0;
  477. }
  478. static void
  479. nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  480. {
  481. struct nouveau_bo *nvbo = nouveau_bo(bo);
  482. switch (bo->mem.mem_type) {
  483. case TTM_PL_VRAM:
  484. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
  485. TTM_PL_FLAG_SYSTEM);
  486. break;
  487. default:
  488. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
  489. break;
  490. }
  491. *pl = nvbo->placement;
  492. }
  493. /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
  494. * TTM_PL_{VRAM,TT} directly.
  495. */
  496. static int
  497. nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
  498. struct nouveau_bo *nvbo, bool evict,
  499. bool no_wait_reserve, bool no_wait_gpu,
  500. struct ttm_mem_reg *new_mem)
  501. {
  502. struct nouveau_fence *fence = NULL;
  503. int ret;
  504. ret = nouveau_fence_new(chan, &fence);
  505. if (ret)
  506. return ret;
  507. ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict,
  508. no_wait_reserve, no_wait_gpu, new_mem);
  509. nouveau_fence_unref(&fence);
  510. return ret;
  511. }
  512. static int
  513. nve0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  514. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  515. {
  516. struct nouveau_mem *node = old_mem->mm_node;
  517. int ret = RING_SPACE(chan, 10);
  518. if (ret == 0) {
  519. BEGIN_NVC0(chan, NvSubCopy, 0x0400, 8);
  520. OUT_RING (chan, upper_32_bits(node->vma[0].offset));
  521. OUT_RING (chan, lower_32_bits(node->vma[0].offset));
  522. OUT_RING (chan, upper_32_bits(node->vma[1].offset));
  523. OUT_RING (chan, lower_32_bits(node->vma[1].offset));
  524. OUT_RING (chan, PAGE_SIZE);
  525. OUT_RING (chan, PAGE_SIZE);
  526. OUT_RING (chan, PAGE_SIZE);
  527. OUT_RING (chan, new_mem->num_pages);
  528. BEGIN_IMC0(chan, NvSubCopy, 0x0300, 0x0386);
  529. }
  530. return ret;
  531. }
  532. static int
  533. nvc0_bo_move_init(struct nouveau_channel *chan, u32 handle)
  534. {
  535. int ret = RING_SPACE(chan, 2);
  536. if (ret == 0) {
  537. BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1);
  538. OUT_RING (chan, handle);
  539. }
  540. return ret;
  541. }
  542. static int
  543. nvc0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  544. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  545. {
  546. struct nouveau_mem *node = old_mem->mm_node;
  547. u64 src_offset = node->vma[0].offset;
  548. u64 dst_offset = node->vma[1].offset;
  549. u32 page_count = new_mem->num_pages;
  550. int ret;
  551. page_count = new_mem->num_pages;
  552. while (page_count) {
  553. int line_count = (page_count > 8191) ? 8191 : page_count;
  554. ret = RING_SPACE(chan, 11);
  555. if (ret)
  556. return ret;
  557. BEGIN_NVC0(chan, NvSubCopy, 0x030c, 8);
  558. OUT_RING (chan, upper_32_bits(src_offset));
  559. OUT_RING (chan, lower_32_bits(src_offset));
  560. OUT_RING (chan, upper_32_bits(dst_offset));
  561. OUT_RING (chan, lower_32_bits(dst_offset));
  562. OUT_RING (chan, PAGE_SIZE);
  563. OUT_RING (chan, PAGE_SIZE);
  564. OUT_RING (chan, PAGE_SIZE);
  565. OUT_RING (chan, line_count);
  566. BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
  567. OUT_RING (chan, 0x00000110);
  568. page_count -= line_count;
  569. src_offset += (PAGE_SIZE * line_count);
  570. dst_offset += (PAGE_SIZE * line_count);
  571. }
  572. return 0;
  573. }
  574. static int
  575. nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  576. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  577. {
  578. struct nouveau_mem *node = old_mem->mm_node;
  579. u64 src_offset = node->vma[0].offset;
  580. u64 dst_offset = node->vma[1].offset;
  581. u32 page_count = new_mem->num_pages;
  582. int ret;
  583. page_count = new_mem->num_pages;
  584. while (page_count) {
  585. int line_count = (page_count > 2047) ? 2047 : page_count;
  586. ret = RING_SPACE(chan, 12);
  587. if (ret)
  588. return ret;
  589. BEGIN_NVC0(chan, NvSubCopy, 0x0238, 2);
  590. OUT_RING (chan, upper_32_bits(dst_offset));
  591. OUT_RING (chan, lower_32_bits(dst_offset));
  592. BEGIN_NVC0(chan, NvSubCopy, 0x030c, 6);
  593. OUT_RING (chan, upper_32_bits(src_offset));
  594. OUT_RING (chan, lower_32_bits(src_offset));
  595. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  596. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  597. OUT_RING (chan, PAGE_SIZE); /* line_length */
  598. OUT_RING (chan, line_count);
  599. BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
  600. OUT_RING (chan, 0x00100110);
  601. page_count -= line_count;
  602. src_offset += (PAGE_SIZE * line_count);
  603. dst_offset += (PAGE_SIZE * line_count);
  604. }
  605. return 0;
  606. }
  607. static int
  608. nva3_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  609. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  610. {
  611. struct nouveau_mem *node = old_mem->mm_node;
  612. u64 src_offset = node->vma[0].offset;
  613. u64 dst_offset = node->vma[1].offset;
  614. u32 page_count = new_mem->num_pages;
  615. int ret;
  616. page_count = new_mem->num_pages;
  617. while (page_count) {
  618. int line_count = (page_count > 8191) ? 8191 : page_count;
  619. ret = RING_SPACE(chan, 11);
  620. if (ret)
  621. return ret;
  622. BEGIN_NV04(chan, NvSubCopy, 0x030c, 8);
  623. OUT_RING (chan, upper_32_bits(src_offset));
  624. OUT_RING (chan, lower_32_bits(src_offset));
  625. OUT_RING (chan, upper_32_bits(dst_offset));
  626. OUT_RING (chan, lower_32_bits(dst_offset));
  627. OUT_RING (chan, PAGE_SIZE);
  628. OUT_RING (chan, PAGE_SIZE);
  629. OUT_RING (chan, PAGE_SIZE);
  630. OUT_RING (chan, line_count);
  631. BEGIN_NV04(chan, NvSubCopy, 0x0300, 1);
  632. OUT_RING (chan, 0x00000110);
  633. page_count -= line_count;
  634. src_offset += (PAGE_SIZE * line_count);
  635. dst_offset += (PAGE_SIZE * line_count);
  636. }
  637. return 0;
  638. }
  639. static int
  640. nv98_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  641. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  642. {
  643. struct nouveau_mem *node = old_mem->mm_node;
  644. int ret = RING_SPACE(chan, 7);
  645. if (ret == 0) {
  646. BEGIN_NV04(chan, NvSubCopy, 0x0320, 6);
  647. OUT_RING (chan, upper_32_bits(node->vma[0].offset));
  648. OUT_RING (chan, lower_32_bits(node->vma[0].offset));
  649. OUT_RING (chan, upper_32_bits(node->vma[1].offset));
  650. OUT_RING (chan, lower_32_bits(node->vma[1].offset));
  651. OUT_RING (chan, 0x00000000 /* COPY */);
  652. OUT_RING (chan, new_mem->num_pages << PAGE_SHIFT);
  653. }
  654. return ret;
  655. }
  656. static int
  657. nv84_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  658. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  659. {
  660. struct nouveau_mem *node = old_mem->mm_node;
  661. int ret = RING_SPACE(chan, 7);
  662. if (ret == 0) {
  663. BEGIN_NV04(chan, NvSubCopy, 0x0304, 6);
  664. OUT_RING (chan, new_mem->num_pages << PAGE_SHIFT);
  665. OUT_RING (chan, upper_32_bits(node->vma[0].offset));
  666. OUT_RING (chan, lower_32_bits(node->vma[0].offset));
  667. OUT_RING (chan, upper_32_bits(node->vma[1].offset));
  668. OUT_RING (chan, lower_32_bits(node->vma[1].offset));
  669. OUT_RING (chan, 0x00000000 /* MODE_COPY, QUERY_NONE */);
  670. }
  671. return ret;
  672. }
  673. static int
  674. nv50_bo_move_init(struct nouveau_channel *chan, u32 handle)
  675. {
  676. int ret = nouveau_notifier_alloc(chan, NvNotify0, 32, 0xfe0, 0x1000,
  677. &chan->m2mf_ntfy);
  678. if (ret == 0) {
  679. ret = RING_SPACE(chan, 6);
  680. if (ret == 0) {
  681. BEGIN_NV04(chan, NvSubCopy, 0x0000, 1);
  682. OUT_RING (chan, handle);
  683. BEGIN_NV04(chan, NvSubCopy, 0x0180, 3);
  684. OUT_RING (chan, NvNotify0);
  685. OUT_RING (chan, NvDmaFB);
  686. OUT_RING (chan, NvDmaFB);
  687. } else {
  688. nouveau_ramht_remove(chan, NvNotify0);
  689. }
  690. }
  691. return ret;
  692. }
  693. static int
  694. nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  695. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  696. {
  697. struct nouveau_mem *node = old_mem->mm_node;
  698. struct nouveau_bo *nvbo = nouveau_bo(bo);
  699. u64 length = (new_mem->num_pages << PAGE_SHIFT);
  700. u64 src_offset = node->vma[0].offset;
  701. u64 dst_offset = node->vma[1].offset;
  702. int ret;
  703. while (length) {
  704. u32 amount, stride, height;
  705. amount = min(length, (u64)(4 * 1024 * 1024));
  706. stride = 16 * 4;
  707. height = amount / stride;
  708. if (new_mem->mem_type == TTM_PL_VRAM &&
  709. nouveau_bo_tile_layout(nvbo)) {
  710. ret = RING_SPACE(chan, 8);
  711. if (ret)
  712. return ret;
  713. BEGIN_NV04(chan, NvSubCopy, 0x0200, 7);
  714. OUT_RING (chan, 0);
  715. OUT_RING (chan, 0);
  716. OUT_RING (chan, stride);
  717. OUT_RING (chan, height);
  718. OUT_RING (chan, 1);
  719. OUT_RING (chan, 0);
  720. OUT_RING (chan, 0);
  721. } else {
  722. ret = RING_SPACE(chan, 2);
  723. if (ret)
  724. return ret;
  725. BEGIN_NV04(chan, NvSubCopy, 0x0200, 1);
  726. OUT_RING (chan, 1);
  727. }
  728. if (old_mem->mem_type == TTM_PL_VRAM &&
  729. nouveau_bo_tile_layout(nvbo)) {
  730. ret = RING_SPACE(chan, 8);
  731. if (ret)
  732. return ret;
  733. BEGIN_NV04(chan, NvSubCopy, 0x021c, 7);
  734. OUT_RING (chan, 0);
  735. OUT_RING (chan, 0);
  736. OUT_RING (chan, stride);
  737. OUT_RING (chan, height);
  738. OUT_RING (chan, 1);
  739. OUT_RING (chan, 0);
  740. OUT_RING (chan, 0);
  741. } else {
  742. ret = RING_SPACE(chan, 2);
  743. if (ret)
  744. return ret;
  745. BEGIN_NV04(chan, NvSubCopy, 0x021c, 1);
  746. OUT_RING (chan, 1);
  747. }
  748. ret = RING_SPACE(chan, 14);
  749. if (ret)
  750. return ret;
  751. BEGIN_NV04(chan, NvSubCopy, 0x0238, 2);
  752. OUT_RING (chan, upper_32_bits(src_offset));
  753. OUT_RING (chan, upper_32_bits(dst_offset));
  754. BEGIN_NV04(chan, NvSubCopy, 0x030c, 8);
  755. OUT_RING (chan, lower_32_bits(src_offset));
  756. OUT_RING (chan, lower_32_bits(dst_offset));
  757. OUT_RING (chan, stride);
  758. OUT_RING (chan, stride);
  759. OUT_RING (chan, stride);
  760. OUT_RING (chan, height);
  761. OUT_RING (chan, 0x00000101);
  762. OUT_RING (chan, 0x00000000);
  763. BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  764. OUT_RING (chan, 0);
  765. length -= amount;
  766. src_offset += amount;
  767. dst_offset += amount;
  768. }
  769. return 0;
  770. }
  771. static int
  772. nv04_bo_move_init(struct nouveau_channel *chan, u32 handle)
  773. {
  774. int ret = nouveau_notifier_alloc(chan, NvNotify0, 32, 0xfe0, 0x1000,
  775. &chan->m2mf_ntfy);
  776. if (ret == 0) {
  777. ret = RING_SPACE(chan, 4);
  778. if (ret == 0) {
  779. BEGIN_NV04(chan, NvSubCopy, 0x0000, 1);
  780. OUT_RING (chan, handle);
  781. BEGIN_NV04(chan, NvSubCopy, 0x0180, 1);
  782. OUT_RING (chan, NvNotify0);
  783. }
  784. }
  785. return ret;
  786. }
  787. static inline uint32_t
  788. nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
  789. struct nouveau_channel *chan, struct ttm_mem_reg *mem)
  790. {
  791. if (mem->mem_type == TTM_PL_TT)
  792. return chan->gart_handle;
  793. return chan->vram_handle;
  794. }
  795. static int
  796. nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  797. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  798. {
  799. u32 src_offset = old_mem->start << PAGE_SHIFT;
  800. u32 dst_offset = new_mem->start << PAGE_SHIFT;
  801. u32 page_count = new_mem->num_pages;
  802. int ret;
  803. ret = RING_SPACE(chan, 3);
  804. if (ret)
  805. return ret;
  806. BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
  807. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  808. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  809. page_count = new_mem->num_pages;
  810. while (page_count) {
  811. int line_count = (page_count > 2047) ? 2047 : page_count;
  812. ret = RING_SPACE(chan, 11);
  813. if (ret)
  814. return ret;
  815. BEGIN_NV04(chan, NvSubCopy,
  816. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  817. OUT_RING (chan, src_offset);
  818. OUT_RING (chan, dst_offset);
  819. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  820. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  821. OUT_RING (chan, PAGE_SIZE); /* line_length */
  822. OUT_RING (chan, line_count);
  823. OUT_RING (chan, 0x00000101);
  824. OUT_RING (chan, 0x00000000);
  825. BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  826. OUT_RING (chan, 0);
  827. page_count -= line_count;
  828. src_offset += (PAGE_SIZE * line_count);
  829. dst_offset += (PAGE_SIZE * line_count);
  830. }
  831. return 0;
  832. }
  833. static int
  834. nouveau_vma_getmap(struct nouveau_channel *chan, struct nouveau_bo *nvbo,
  835. struct ttm_mem_reg *mem, struct nouveau_vma *vma)
  836. {
  837. struct nouveau_mem *node = mem->mm_node;
  838. int ret;
  839. ret = nouveau_vm_get(chan->vm, mem->num_pages << PAGE_SHIFT,
  840. node->page_shift, NV_MEM_ACCESS_RO, vma);
  841. if (ret)
  842. return ret;
  843. if (mem->mem_type == TTM_PL_VRAM)
  844. nouveau_vm_map(vma, node);
  845. else
  846. nouveau_vm_map_sg(vma, 0, mem->num_pages << PAGE_SHIFT, node);
  847. return 0;
  848. }
  849. static int
  850. nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
  851. bool no_wait_reserve, bool no_wait_gpu,
  852. struct ttm_mem_reg *new_mem)
  853. {
  854. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  855. struct nouveau_channel *chan = chan = dev_priv->channel;
  856. struct nouveau_bo *nvbo = nouveau_bo(bo);
  857. struct ttm_mem_reg *old_mem = &bo->mem;
  858. int ret;
  859. mutex_lock_nested(&chan->mutex, NOUVEAU_KCHANNEL_MUTEX);
  860. /* create temporary vmas for the transfer and attach them to the
  861. * old nouveau_mem node, these will get cleaned up after ttm has
  862. * destroyed the ttm_mem_reg
  863. */
  864. if (dev_priv->card_type >= NV_50) {
  865. struct nouveau_mem *node = old_mem->mm_node;
  866. ret = nouveau_vma_getmap(chan, nvbo, old_mem, &node->vma[0]);
  867. if (ret)
  868. goto out;
  869. ret = nouveau_vma_getmap(chan, nvbo, new_mem, &node->vma[1]);
  870. if (ret)
  871. goto out;
  872. }
  873. ret = dev_priv->ttm.move(chan, bo, &bo->mem, new_mem);
  874. if (ret == 0) {
  875. ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
  876. no_wait_reserve,
  877. no_wait_gpu, new_mem);
  878. }
  879. out:
  880. mutex_unlock(&chan->mutex);
  881. return ret;
  882. }
  883. void
  884. nouveau_bo_move_init(struct nouveau_channel *chan)
  885. {
  886. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  887. static const struct {
  888. const char *name;
  889. int engine;
  890. u32 oclass;
  891. int (*exec)(struct nouveau_channel *,
  892. struct ttm_buffer_object *,
  893. struct ttm_mem_reg *, struct ttm_mem_reg *);
  894. int (*init)(struct nouveau_channel *, u32 handle);
  895. } _methods[] = {
  896. { "COPY", 0, 0xa0b5, nve0_bo_move_copy, nvc0_bo_move_init },
  897. { "COPY1", 5, 0x90b8, nvc0_bo_move_copy, nvc0_bo_move_init },
  898. { "COPY0", 4, 0x90b5, nvc0_bo_move_copy, nvc0_bo_move_init },
  899. { "COPY", 0, 0x85b5, nva3_bo_move_copy, nv50_bo_move_init },
  900. { "CRYPT", 0, 0x74c1, nv84_bo_move_exec, nv50_bo_move_init },
  901. { "M2MF", 0, 0x9039, nvc0_bo_move_m2mf, nvc0_bo_move_init },
  902. { "M2MF", 0, 0x5039, nv50_bo_move_m2mf, nv50_bo_move_init },
  903. { "M2MF", 0, 0x0039, nv04_bo_move_m2mf, nv04_bo_move_init },
  904. {},
  905. { "CRYPT", 0, 0x88b4, nv98_bo_move_exec, nv50_bo_move_init },
  906. }, *mthd = _methods;
  907. const char *name = "CPU";
  908. int ret;
  909. do {
  910. u32 handle = (mthd->engine << 16) | mthd->oclass;
  911. ret = nouveau_gpuobj_gr_new(chan, handle, mthd->oclass);
  912. if (ret == 0) {
  913. ret = mthd->init(chan, handle);
  914. if (ret == 0) {
  915. dev_priv->ttm.move = mthd->exec;
  916. name = mthd->name;
  917. break;
  918. }
  919. }
  920. } while ((++mthd)->exec);
  921. NV_INFO(chan->dev, "MM: using %s for buffer copies\n", name);
  922. }
  923. static int
  924. nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
  925. bool no_wait_reserve, bool no_wait_gpu,
  926. struct ttm_mem_reg *new_mem)
  927. {
  928. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  929. struct ttm_placement placement;
  930. struct ttm_mem_reg tmp_mem;
  931. int ret;
  932. placement.fpfn = placement.lpfn = 0;
  933. placement.num_placement = placement.num_busy_placement = 1;
  934. placement.placement = placement.busy_placement = &placement_memtype;
  935. tmp_mem = *new_mem;
  936. tmp_mem.mm_node = NULL;
  937. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  938. if (ret)
  939. return ret;
  940. ret = ttm_tt_bind(bo->ttm, &tmp_mem);
  941. if (ret)
  942. goto out;
  943. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
  944. if (ret)
  945. goto out;
  946. ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, new_mem);
  947. out:
  948. ttm_bo_mem_put(bo, &tmp_mem);
  949. return ret;
  950. }
  951. static int
  952. nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
  953. bool no_wait_reserve, bool no_wait_gpu,
  954. struct ttm_mem_reg *new_mem)
  955. {
  956. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  957. struct ttm_placement placement;
  958. struct ttm_mem_reg tmp_mem;
  959. int ret;
  960. placement.fpfn = placement.lpfn = 0;
  961. placement.num_placement = placement.num_busy_placement = 1;
  962. placement.placement = placement.busy_placement = &placement_memtype;
  963. tmp_mem = *new_mem;
  964. tmp_mem.mm_node = NULL;
  965. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  966. if (ret)
  967. return ret;
  968. ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, &tmp_mem);
  969. if (ret)
  970. goto out;
  971. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, new_mem);
  972. if (ret)
  973. goto out;
  974. out:
  975. ttm_bo_mem_put(bo, &tmp_mem);
  976. return ret;
  977. }
  978. static void
  979. nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem)
  980. {
  981. struct nouveau_bo *nvbo = nouveau_bo(bo);
  982. struct nouveau_vma *vma;
  983. /* ttm can now (stupidly) pass the driver bos it didn't create... */
  984. if (bo->destroy != nouveau_bo_del_ttm)
  985. return;
  986. list_for_each_entry(vma, &nvbo->vma_list, head) {
  987. if (new_mem && new_mem->mem_type == TTM_PL_VRAM) {
  988. nouveau_vm_map(vma, new_mem->mm_node);
  989. } else
  990. if (new_mem && new_mem->mem_type == TTM_PL_TT &&
  991. nvbo->page_shift == nvvm_spg_shift(vma->vm)) {
  992. if (((struct nouveau_mem *)new_mem->mm_node)->sg)
  993. nouveau_vm_map_sg_table(vma, 0, new_mem->
  994. num_pages << PAGE_SHIFT,
  995. new_mem->mm_node);
  996. else
  997. nouveau_vm_map_sg(vma, 0, new_mem->
  998. num_pages << PAGE_SHIFT,
  999. new_mem->mm_node);
  1000. } else {
  1001. nouveau_vm_unmap(vma);
  1002. }
  1003. }
  1004. }
  1005. static int
  1006. nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
  1007. struct nouveau_tile_reg **new_tile)
  1008. {
  1009. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  1010. struct drm_device *dev = dev_priv->dev;
  1011. struct nouveau_bo *nvbo = nouveau_bo(bo);
  1012. u64 offset = new_mem->start << PAGE_SHIFT;
  1013. *new_tile = NULL;
  1014. if (new_mem->mem_type != TTM_PL_VRAM)
  1015. return 0;
  1016. if (dev_priv->card_type >= NV_10) {
  1017. *new_tile = nv10_bo_set_tiling(dev, offset, new_mem->size,
  1018. nvbo->tile_mode,
  1019. nvbo->tile_flags);
  1020. }
  1021. return 0;
  1022. }
  1023. static void
  1024. nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
  1025. struct nouveau_tile_reg *new_tile,
  1026. struct nouveau_tile_reg **old_tile)
  1027. {
  1028. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  1029. struct drm_device *dev = dev_priv->dev;
  1030. nv10_bo_put_tile_region(dev, *old_tile, bo->sync_obj);
  1031. *old_tile = new_tile;
  1032. }
  1033. static int
  1034. nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
  1035. bool no_wait_reserve, bool no_wait_gpu,
  1036. struct ttm_mem_reg *new_mem)
  1037. {
  1038. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  1039. struct nouveau_bo *nvbo = nouveau_bo(bo);
  1040. struct ttm_mem_reg *old_mem = &bo->mem;
  1041. struct nouveau_tile_reg *new_tile = NULL;
  1042. int ret = 0;
  1043. if (dev_priv->card_type < NV_50) {
  1044. ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
  1045. if (ret)
  1046. return ret;
  1047. }
  1048. /* Fake bo copy. */
  1049. if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
  1050. BUG_ON(bo->mem.mm_node != NULL);
  1051. bo->mem = *new_mem;
  1052. new_mem->mm_node = NULL;
  1053. goto out;
  1054. }
  1055. /* CPU copy if we have no accelerated method available */
  1056. if (!dev_priv->ttm.move) {
  1057. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  1058. goto out;
  1059. }
  1060. /* Hardware assisted copy. */
  1061. if (new_mem->mem_type == TTM_PL_SYSTEM)
  1062. ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  1063. else if (old_mem->mem_type == TTM_PL_SYSTEM)
  1064. ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  1065. else
  1066. ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  1067. if (!ret)
  1068. goto out;
  1069. /* Fallback to software copy. */
  1070. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  1071. out:
  1072. if (dev_priv->card_type < NV_50) {
  1073. if (ret)
  1074. nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
  1075. else
  1076. nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
  1077. }
  1078. return ret;
  1079. }
  1080. static int
  1081. nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  1082. {
  1083. return 0;
  1084. }
  1085. static int
  1086. nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1087. {
  1088. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  1089. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  1090. struct drm_device *dev = dev_priv->dev;
  1091. int ret;
  1092. mem->bus.addr = NULL;
  1093. mem->bus.offset = 0;
  1094. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  1095. mem->bus.base = 0;
  1096. mem->bus.is_iomem = false;
  1097. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  1098. return -EINVAL;
  1099. switch (mem->mem_type) {
  1100. case TTM_PL_SYSTEM:
  1101. /* System memory */
  1102. return 0;
  1103. case TTM_PL_TT:
  1104. #if __OS_HAS_AGP
  1105. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  1106. mem->bus.offset = mem->start << PAGE_SHIFT;
  1107. mem->bus.base = dev_priv->gart_info.aper_base;
  1108. mem->bus.is_iomem = true;
  1109. }
  1110. #endif
  1111. break;
  1112. case TTM_PL_VRAM:
  1113. mem->bus.offset = mem->start << PAGE_SHIFT;
  1114. mem->bus.base = pci_resource_start(dev->pdev, 1);
  1115. mem->bus.is_iomem = true;
  1116. if (dev_priv->card_type >= NV_50) {
  1117. struct nouveau_mem *node = mem->mm_node;
  1118. ret = nvbar_map(dev, node, NV_MEM_ACCESS_RW,
  1119. &node->bar_vma);
  1120. if (ret)
  1121. return ret;
  1122. mem->bus.offset = node->bar_vma.offset;
  1123. }
  1124. break;
  1125. default:
  1126. return -EINVAL;
  1127. }
  1128. return 0;
  1129. }
  1130. static void
  1131. nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1132. {
  1133. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  1134. struct nouveau_mem *node = mem->mm_node;
  1135. if (mem->mem_type != TTM_PL_VRAM)
  1136. return;
  1137. if (!node->bar_vma.node)
  1138. return;
  1139. nvbar_unmap(dev_priv->dev, &node->bar_vma);
  1140. }
  1141. static int
  1142. nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  1143. {
  1144. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  1145. struct nouveau_bo *nvbo = nouveau_bo(bo);
  1146. /* as long as the bo isn't in vram, and isn't tiled, we've got
  1147. * nothing to do here.
  1148. */
  1149. if (bo->mem.mem_type != TTM_PL_VRAM) {
  1150. if (dev_priv->card_type < NV_50 ||
  1151. !nouveau_bo_tile_layout(nvbo))
  1152. return 0;
  1153. }
  1154. /* make sure bo is in mappable vram */
  1155. if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages)
  1156. return 0;
  1157. nvbo->placement.fpfn = 0;
  1158. nvbo->placement.lpfn = dev_priv->fb_mappable_pages;
  1159. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_VRAM, 0);
  1160. return nouveau_bo_validate(nvbo, false, true, false);
  1161. }
  1162. static int
  1163. nouveau_ttm_tt_populate(struct ttm_tt *ttm)
  1164. {
  1165. struct ttm_dma_tt *ttm_dma = (void *)ttm;
  1166. struct drm_nouveau_private *dev_priv;
  1167. struct drm_device *dev;
  1168. unsigned i;
  1169. int r;
  1170. bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  1171. if (ttm->state != tt_unpopulated)
  1172. return 0;
  1173. if (slave && ttm->sg) {
  1174. /* make userspace faulting work */
  1175. drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
  1176. ttm_dma->dma_address, ttm->num_pages);
  1177. ttm->state = tt_unbound;
  1178. return 0;
  1179. }
  1180. dev_priv = nouveau_bdev(ttm->bdev);
  1181. dev = dev_priv->dev;
  1182. #if __OS_HAS_AGP
  1183. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  1184. return ttm_agp_tt_populate(ttm);
  1185. }
  1186. #endif
  1187. #ifdef CONFIG_SWIOTLB
  1188. if (swiotlb_nr_tbl()) {
  1189. return ttm_dma_populate((void *)ttm, dev->dev);
  1190. }
  1191. #endif
  1192. r = ttm_pool_populate(ttm);
  1193. if (r) {
  1194. return r;
  1195. }
  1196. for (i = 0; i < ttm->num_pages; i++) {
  1197. ttm_dma->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i],
  1198. 0, PAGE_SIZE,
  1199. PCI_DMA_BIDIRECTIONAL);
  1200. if (pci_dma_mapping_error(dev->pdev, ttm_dma->dma_address[i])) {
  1201. while (--i) {
  1202. pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
  1203. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  1204. ttm_dma->dma_address[i] = 0;
  1205. }
  1206. ttm_pool_unpopulate(ttm);
  1207. return -EFAULT;
  1208. }
  1209. }
  1210. return 0;
  1211. }
  1212. static void
  1213. nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
  1214. {
  1215. struct ttm_dma_tt *ttm_dma = (void *)ttm;
  1216. struct drm_nouveau_private *dev_priv;
  1217. struct drm_device *dev;
  1218. unsigned i;
  1219. bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  1220. if (slave)
  1221. return;
  1222. dev_priv = nouveau_bdev(ttm->bdev);
  1223. dev = dev_priv->dev;
  1224. #if __OS_HAS_AGP
  1225. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  1226. ttm_agp_tt_unpopulate(ttm);
  1227. return;
  1228. }
  1229. #endif
  1230. #ifdef CONFIG_SWIOTLB
  1231. if (swiotlb_nr_tbl()) {
  1232. ttm_dma_unpopulate((void *)ttm, dev->dev);
  1233. return;
  1234. }
  1235. #endif
  1236. for (i = 0; i < ttm->num_pages; i++) {
  1237. if (ttm_dma->dma_address[i]) {
  1238. pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
  1239. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  1240. }
  1241. }
  1242. ttm_pool_unpopulate(ttm);
  1243. }
  1244. void
  1245. nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
  1246. {
  1247. struct nouveau_fence *old_fence = NULL;
  1248. if (likely(fence))
  1249. nouveau_fence_ref(fence);
  1250. spin_lock(&nvbo->bo.bdev->fence_lock);
  1251. old_fence = nvbo->bo.sync_obj;
  1252. nvbo->bo.sync_obj = fence;
  1253. spin_unlock(&nvbo->bo.bdev->fence_lock);
  1254. nouveau_fence_unref(&old_fence);
  1255. }
  1256. static void
  1257. nouveau_bo_fence_unref(void **sync_obj)
  1258. {
  1259. nouveau_fence_unref((struct nouveau_fence **)sync_obj);
  1260. }
  1261. static void *
  1262. nouveau_bo_fence_ref(void *sync_obj)
  1263. {
  1264. return nouveau_fence_ref(sync_obj);
  1265. }
  1266. static bool
  1267. nouveau_bo_fence_signalled(void *sync_obj, void *sync_arg)
  1268. {
  1269. return nouveau_fence_done(sync_obj);
  1270. }
  1271. static int
  1272. nouveau_bo_fence_wait(void *sync_obj, void *sync_arg, bool lazy, bool intr)
  1273. {
  1274. return nouveau_fence_wait(sync_obj, lazy, intr);
  1275. }
  1276. static int
  1277. nouveau_bo_fence_flush(void *sync_obj, void *sync_arg)
  1278. {
  1279. return 0;
  1280. }
  1281. struct ttm_bo_driver nouveau_bo_driver = {
  1282. .ttm_tt_create = &nouveau_ttm_tt_create,
  1283. .ttm_tt_populate = &nouveau_ttm_tt_populate,
  1284. .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
  1285. .invalidate_caches = nouveau_bo_invalidate_caches,
  1286. .init_mem_type = nouveau_bo_init_mem_type,
  1287. .evict_flags = nouveau_bo_evict_flags,
  1288. .move_notify = nouveau_bo_move_ntfy,
  1289. .move = nouveau_bo_move,
  1290. .verify_access = nouveau_bo_verify_access,
  1291. .sync_obj_signaled = nouveau_bo_fence_signalled,
  1292. .sync_obj_wait = nouveau_bo_fence_wait,
  1293. .sync_obj_flush = nouveau_bo_fence_flush,
  1294. .sync_obj_unref = nouveau_bo_fence_unref,
  1295. .sync_obj_ref = nouveau_bo_fence_ref,
  1296. .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
  1297. .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
  1298. .io_mem_free = &nouveau_ttm_io_mem_free,
  1299. };
  1300. struct nouveau_vma *
  1301. nouveau_bo_vma_find(struct nouveau_bo *nvbo, struct nouveau_vm *vm)
  1302. {
  1303. struct nouveau_vma *vma;
  1304. list_for_each_entry(vma, &nvbo->vma_list, head) {
  1305. if (vma->vm == vm)
  1306. return vma;
  1307. }
  1308. return NULL;
  1309. }
  1310. int
  1311. nouveau_bo_vma_add(struct nouveau_bo *nvbo, struct nouveau_vm *vm,
  1312. struct nouveau_vma *vma)
  1313. {
  1314. const u32 size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
  1315. struct nouveau_mem *node = nvbo->bo.mem.mm_node;
  1316. int ret;
  1317. ret = nouveau_vm_get(vm, size, nvbo->page_shift,
  1318. NV_MEM_ACCESS_RW, vma);
  1319. if (ret)
  1320. return ret;
  1321. if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
  1322. nouveau_vm_map(vma, nvbo->bo.mem.mm_node);
  1323. else if (nvbo->bo.mem.mem_type == TTM_PL_TT) {
  1324. if (node->sg)
  1325. nouveau_vm_map_sg_table(vma, 0, size, node);
  1326. else
  1327. nouveau_vm_map_sg(vma, 0, size, node);
  1328. }
  1329. list_add_tail(&vma->head, &nvbo->vma_list);
  1330. vma->refcount = 1;
  1331. return 0;
  1332. }
  1333. void
  1334. nouveau_bo_vma_del(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
  1335. {
  1336. if (vma->node) {
  1337. if (nvbo->bo.mem.mem_type != TTM_PL_SYSTEM) {
  1338. spin_lock(&nvbo->bo.bdev->fence_lock);
  1339. ttm_bo_wait(&nvbo->bo, false, false, false);
  1340. spin_unlock(&nvbo->bo.bdev->fence_lock);
  1341. nouveau_vm_unmap(vma);
  1342. }
  1343. nouveau_vm_put(vma);
  1344. list_del(&vma->head);
  1345. }
  1346. }