nouveau_bo.c 39 KB

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