nouveau_bo.c 41 KB

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