nouveau_bo.c 42 KB

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