nouveau_bo.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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 <[email protected]>
  26. * Ben Skeggs <[email protected]>
  27. * Jeremy Kolb <[email protected]>
  28. */
  29. #include <linux/dma-mapping.h>
  30. #include "nouveau_drv.h"
  31. #include "nouveau_chan.h"
  32. #include "nouveau_fence.h"
  33. #include "nouveau_bo.h"
  34. #include "nouveau_ttm.h"
  35. #include "nouveau_gem.h"
  36. #include "nouveau_mem.h"
  37. #include "nouveau_vmm.h"
  38. #include <nvif/class.h>
  39. #include <nvif/if500b.h>
  40. #include <nvif/if900b.h>
  41. static int nouveau_ttm_tt_bind(struct ttm_device *bdev, struct ttm_tt *ttm,
  42. struct ttm_resource *reg);
  43. static void nouveau_ttm_tt_unbind(struct ttm_device *bdev, struct ttm_tt *ttm);
  44. /*
  45. * NV10-NV40 tiling helpers
  46. */
  47. static void
  48. nv10_bo_update_tile_region(struct drm_device *dev, struct nouveau_drm_tile *reg,
  49. u32 addr, u32 size, u32 pitch, u32 flags)
  50. {
  51. struct nouveau_drm *drm = nouveau_drm(dev);
  52. int i = reg - drm->tile.reg;
  53. struct nvkm_fb *fb = nvxx_fb(&drm->client.device);
  54. struct nvkm_fb_tile *tile = &fb->tile.region[i];
  55. nouveau_fence_unref(&reg->fence);
  56. if (tile->pitch)
  57. nvkm_fb_tile_fini(fb, i, tile);
  58. if (pitch)
  59. nvkm_fb_tile_init(fb, i, addr, size, pitch, flags, tile);
  60. nvkm_fb_tile_prog(fb, i, tile);
  61. }
  62. static struct nouveau_drm_tile *
  63. nv10_bo_get_tile_region(struct drm_device *dev, int i)
  64. {
  65. struct nouveau_drm *drm = nouveau_drm(dev);
  66. struct nouveau_drm_tile *tile = &drm->tile.reg[i];
  67. spin_lock(&drm->tile.lock);
  68. if (!tile->used &&
  69. (!tile->fence || nouveau_fence_done(tile->fence)))
  70. tile->used = true;
  71. else
  72. tile = NULL;
  73. spin_unlock(&drm->tile.lock);
  74. return tile;
  75. }
  76. static void
  77. nv10_bo_put_tile_region(struct drm_device *dev, struct nouveau_drm_tile *tile,
  78. struct dma_fence *fence)
  79. {
  80. struct nouveau_drm *drm = nouveau_drm(dev);
  81. if (tile) {
  82. spin_lock(&drm->tile.lock);
  83. tile->fence = (struct nouveau_fence *)dma_fence_get(fence);
  84. tile->used = false;
  85. spin_unlock(&drm->tile.lock);
  86. }
  87. }
  88. static struct nouveau_drm_tile *
  89. nv10_bo_set_tiling(struct drm_device *dev, u32 addr,
  90. u32 size, u32 pitch, u32 zeta)
  91. {
  92. struct nouveau_drm *drm = nouveau_drm(dev);
  93. struct nvkm_fb *fb = nvxx_fb(&drm->client.device);
  94. struct nouveau_drm_tile *tile, *found = NULL;
  95. int i;
  96. for (i = 0; i < fb->tile.regions; i++) {
  97. tile = nv10_bo_get_tile_region(dev, i);
  98. if (pitch && !found) {
  99. found = tile;
  100. continue;
  101. } else if (tile && fb->tile.region[i].pitch) {
  102. /* Kill an unused tile region. */
  103. nv10_bo_update_tile_region(dev, tile, 0, 0, 0, 0);
  104. }
  105. nv10_bo_put_tile_region(dev, tile, NULL);
  106. }
  107. if (found)
  108. nv10_bo_update_tile_region(dev, found, addr, size, pitch, zeta);
  109. return found;
  110. }
  111. static void
  112. nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
  113. {
  114. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  115. struct drm_device *dev = drm->dev;
  116. struct nouveau_bo *nvbo = nouveau_bo(bo);
  117. WARN_ON(nvbo->bo.pin_count > 0);
  118. nouveau_bo_del_io_reserve_lru(bo);
  119. nv10_bo_put_tile_region(dev, nvbo->tile, NULL);
  120. /*
  121. * If nouveau_bo_new() allocated this buffer, the GEM object was never
  122. * initialized, so don't attempt to release it.
  123. */
  124. if (bo->base.dev)
  125. drm_gem_object_release(&bo->base);
  126. else
  127. dma_resv_fini(&bo->base._resv);
  128. kfree(nvbo);
  129. }
  130. static inline u64
  131. roundup_64(u64 x, u32 y)
  132. {
  133. x += y - 1;
  134. do_div(x, y);
  135. return x * y;
  136. }
  137. static void
  138. nouveau_bo_fixup_align(struct nouveau_bo *nvbo, int *align, u64 *size)
  139. {
  140. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  141. struct nvif_device *device = &drm->client.device;
  142. if (device->info.family < NV_DEVICE_INFO_V0_TESLA) {
  143. if (nvbo->mode) {
  144. if (device->info.chipset >= 0x40) {
  145. *align = 65536;
  146. *size = roundup_64(*size, 64 * nvbo->mode);
  147. } else if (device->info.chipset >= 0x30) {
  148. *align = 32768;
  149. *size = roundup_64(*size, 64 * nvbo->mode);
  150. } else if (device->info.chipset >= 0x20) {
  151. *align = 16384;
  152. *size = roundup_64(*size, 64 * nvbo->mode);
  153. } else if (device->info.chipset >= 0x10) {
  154. *align = 16384;
  155. *size = roundup_64(*size, 32 * nvbo->mode);
  156. }
  157. }
  158. } else {
  159. *size = roundup_64(*size, (1 << nvbo->page));
  160. *align = max((1 << nvbo->page), *align);
  161. }
  162. *size = roundup_64(*size, PAGE_SIZE);
  163. }
  164. struct nouveau_bo *
  165. nouveau_bo_alloc(struct nouveau_cli *cli, u64 *size, int *align, u32 domain,
  166. u32 tile_mode, u32 tile_flags)
  167. {
  168. struct nouveau_drm *drm = cli->drm;
  169. struct nouveau_bo *nvbo;
  170. struct nvif_mmu *mmu = &cli->mmu;
  171. struct nvif_vmm *vmm = cli->svm.cli ? &cli->svm.vmm : &cli->vmm.vmm;
  172. int i, pi = -1;
  173. if (!*size) {
  174. NV_WARN(drm, "skipped size %016llx\n", *size);
  175. return ERR_PTR(-EINVAL);
  176. }
  177. nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
  178. if (!nvbo)
  179. return ERR_PTR(-ENOMEM);
  180. INIT_LIST_HEAD(&nvbo->head);
  181. INIT_LIST_HEAD(&nvbo->entry);
  182. INIT_LIST_HEAD(&nvbo->vma_list);
  183. nvbo->bo.bdev = &drm->ttm.bdev;
  184. /* This is confusing, and doesn't actually mean we want an uncached
  185. * mapping, but is what NOUVEAU_GEM_DOMAIN_COHERENT gets translated
  186. * into in nouveau_gem_new().
  187. */
  188. if (domain & NOUVEAU_GEM_DOMAIN_COHERENT) {
  189. /* Determine if we can get a cache-coherent map, forcing
  190. * uncached mapping if we can't.
  191. */
  192. if (!nouveau_drm_use_coherent_gpu_mapping(drm))
  193. nvbo->force_coherent = true;
  194. }
  195. if (cli->device.info.family >= NV_DEVICE_INFO_V0_FERMI) {
  196. nvbo->kind = (tile_flags & 0x0000ff00) >> 8;
  197. if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) {
  198. kfree(nvbo);
  199. return ERR_PTR(-EINVAL);
  200. }
  201. nvbo->comp = mmu->kind[nvbo->kind] != nvbo->kind;
  202. } else
  203. if (cli->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
  204. nvbo->kind = (tile_flags & 0x00007f00) >> 8;
  205. nvbo->comp = (tile_flags & 0x00030000) >> 16;
  206. if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) {
  207. kfree(nvbo);
  208. return ERR_PTR(-EINVAL);
  209. }
  210. } else {
  211. nvbo->zeta = (tile_flags & 0x00000007);
  212. }
  213. nvbo->mode = tile_mode;
  214. nvbo->contig = !(tile_flags & NOUVEAU_GEM_TILE_NONCONTIG);
  215. /* Determine the desirable target GPU page size for the buffer. */
  216. for (i = 0; i < vmm->page_nr; i++) {
  217. /* Because we cannot currently allow VMM maps to fail
  218. * during buffer migration, we need to determine page
  219. * size for the buffer up-front, and pre-allocate its
  220. * page tables.
  221. *
  222. * Skip page sizes that can't support needed domains.
  223. */
  224. if (cli->device.info.family > NV_DEVICE_INFO_V0_CURIE &&
  225. (domain & NOUVEAU_GEM_DOMAIN_VRAM) && !vmm->page[i].vram)
  226. continue;
  227. if ((domain & NOUVEAU_GEM_DOMAIN_GART) &&
  228. (!vmm->page[i].host || vmm->page[i].shift > PAGE_SHIFT))
  229. continue;
  230. /* Select this page size if it's the first that supports
  231. * the potential memory domains, or when it's compatible
  232. * with the requested compression settings.
  233. */
  234. if (pi < 0 || !nvbo->comp || vmm->page[i].comp)
  235. pi = i;
  236. /* Stop once the buffer is larger than the current page size. */
  237. if (*size >= 1ULL << vmm->page[i].shift)
  238. break;
  239. }
  240. if (WARN_ON(pi < 0)) {
  241. kfree(nvbo);
  242. return ERR_PTR(-EINVAL);
  243. }
  244. /* Disable compression if suitable settings couldn't be found. */
  245. if (nvbo->comp && !vmm->page[pi].comp) {
  246. if (mmu->object.oclass >= NVIF_CLASS_MMU_GF100)
  247. nvbo->kind = mmu->kind[nvbo->kind];
  248. nvbo->comp = 0;
  249. }
  250. nvbo->page = vmm->page[pi].shift;
  251. nouveau_bo_fixup_align(nvbo, align, size);
  252. return nvbo;
  253. }
  254. int
  255. nouveau_bo_init(struct nouveau_bo *nvbo, u64 size, int align, u32 domain,
  256. struct sg_table *sg, struct dma_resv *robj)
  257. {
  258. int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
  259. int ret;
  260. nouveau_bo_placement_set(nvbo, domain, 0);
  261. INIT_LIST_HEAD(&nvbo->io_reserve_lru);
  262. ret = ttm_bo_init_validate(nvbo->bo.bdev, &nvbo->bo, type,
  263. &nvbo->placement, align >> PAGE_SHIFT, false,
  264. sg, robj, nouveau_bo_del_ttm);
  265. if (ret) {
  266. /* ttm will call nouveau_bo_del_ttm if it fails.. */
  267. return ret;
  268. }
  269. return 0;
  270. }
  271. int
  272. nouveau_bo_new(struct nouveau_cli *cli, u64 size, int align,
  273. uint32_t domain, uint32_t tile_mode, uint32_t tile_flags,
  274. struct sg_table *sg, struct dma_resv *robj,
  275. struct nouveau_bo **pnvbo)
  276. {
  277. struct nouveau_bo *nvbo;
  278. int ret;
  279. nvbo = nouveau_bo_alloc(cli, &size, &align, domain, tile_mode,
  280. tile_flags);
  281. if (IS_ERR(nvbo))
  282. return PTR_ERR(nvbo);
  283. nvbo->bo.base.size = size;
  284. dma_resv_init(&nvbo->bo.base._resv);
  285. drm_vma_node_reset(&nvbo->bo.base.vma_node);
  286. ret = nouveau_bo_init(nvbo, size, align, domain, sg, robj);
  287. if (ret)
  288. return ret;
  289. *pnvbo = nvbo;
  290. return 0;
  291. }
  292. static void
  293. set_placement_list(struct ttm_place *pl, unsigned *n, uint32_t domain)
  294. {
  295. *n = 0;
  296. if (domain & NOUVEAU_GEM_DOMAIN_VRAM) {
  297. pl[*n].mem_type = TTM_PL_VRAM;
  298. pl[*n].flags = 0;
  299. (*n)++;
  300. }
  301. if (domain & NOUVEAU_GEM_DOMAIN_GART) {
  302. pl[*n].mem_type = TTM_PL_TT;
  303. pl[*n].flags = 0;
  304. (*n)++;
  305. }
  306. if (domain & NOUVEAU_GEM_DOMAIN_CPU) {
  307. pl[*n].mem_type = TTM_PL_SYSTEM;
  308. pl[(*n)++].flags = 0;
  309. }
  310. }
  311. static void
  312. set_placement_range(struct nouveau_bo *nvbo, uint32_t domain)
  313. {
  314. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  315. u64 vram_size = drm->client.device.info.ram_size;
  316. unsigned i, fpfn, lpfn;
  317. if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CELSIUS &&
  318. nvbo->mode && (domain & NOUVEAU_GEM_DOMAIN_VRAM) &&
  319. nvbo->bo.base.size < vram_size / 4) {
  320. /*
  321. * Make sure that the color and depth buffers are handled
  322. * by independent memory controller units. Up to a 9x
  323. * speed up when alpha-blending and depth-test are enabled
  324. * at the same time.
  325. */
  326. if (nvbo->zeta) {
  327. fpfn = (vram_size / 2) >> PAGE_SHIFT;
  328. lpfn = ~0;
  329. } else {
  330. fpfn = 0;
  331. lpfn = (vram_size / 2) >> PAGE_SHIFT;
  332. }
  333. for (i = 0; i < nvbo->placement.num_placement; ++i) {
  334. nvbo->placements[i].fpfn = fpfn;
  335. nvbo->placements[i].lpfn = lpfn;
  336. }
  337. for (i = 0; i < nvbo->placement.num_busy_placement; ++i) {
  338. nvbo->busy_placements[i].fpfn = fpfn;
  339. nvbo->busy_placements[i].lpfn = lpfn;
  340. }
  341. }
  342. }
  343. void
  344. nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t domain,
  345. uint32_t busy)
  346. {
  347. struct ttm_placement *pl = &nvbo->placement;
  348. pl->placement = nvbo->placements;
  349. set_placement_list(nvbo->placements, &pl->num_placement, domain);
  350. pl->busy_placement = nvbo->busy_placements;
  351. set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
  352. domain | busy);
  353. set_placement_range(nvbo, domain);
  354. }
  355. int
  356. nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t domain, bool contig)
  357. {
  358. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  359. struct ttm_buffer_object *bo = &nvbo->bo;
  360. bool force = false, evict = false;
  361. int ret;
  362. ret = ttm_bo_reserve(bo, false, false, NULL);
  363. if (ret)
  364. return ret;
  365. if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
  366. domain == NOUVEAU_GEM_DOMAIN_VRAM && contig) {
  367. if (!nvbo->contig) {
  368. nvbo->contig = true;
  369. force = true;
  370. evict = true;
  371. }
  372. }
  373. if (nvbo->bo.pin_count) {
  374. bool error = evict;
  375. switch (bo->resource->mem_type) {
  376. case TTM_PL_VRAM:
  377. error |= !(domain & NOUVEAU_GEM_DOMAIN_VRAM);
  378. break;
  379. case TTM_PL_TT:
  380. error |= !(domain & NOUVEAU_GEM_DOMAIN_GART);
  381. break;
  382. default:
  383. break;
  384. }
  385. if (error) {
  386. NV_ERROR(drm, "bo %p pinned elsewhere: "
  387. "0x%08x vs 0x%08x\n", bo,
  388. bo->resource->mem_type, domain);
  389. ret = -EBUSY;
  390. }
  391. ttm_bo_pin(&nvbo->bo);
  392. goto out;
  393. }
  394. if (evict) {
  395. nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART, 0);
  396. ret = nouveau_bo_validate(nvbo, false, false);
  397. if (ret)
  398. goto out;
  399. }
  400. nouveau_bo_placement_set(nvbo, domain, 0);
  401. ret = nouveau_bo_validate(nvbo, false, false);
  402. if (ret)
  403. goto out;
  404. ttm_bo_pin(&nvbo->bo);
  405. switch (bo->resource->mem_type) {
  406. case TTM_PL_VRAM:
  407. drm->gem.vram_available -= bo->base.size;
  408. break;
  409. case TTM_PL_TT:
  410. drm->gem.gart_available -= bo->base.size;
  411. break;
  412. default:
  413. break;
  414. }
  415. out:
  416. if (force && ret)
  417. nvbo->contig = false;
  418. ttm_bo_unreserve(bo);
  419. return ret;
  420. }
  421. int
  422. nouveau_bo_unpin(struct nouveau_bo *nvbo)
  423. {
  424. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  425. struct ttm_buffer_object *bo = &nvbo->bo;
  426. int ret;
  427. ret = ttm_bo_reserve(bo, false, false, NULL);
  428. if (ret)
  429. return ret;
  430. ttm_bo_unpin(&nvbo->bo);
  431. if (!nvbo->bo.pin_count) {
  432. switch (bo->resource->mem_type) {
  433. case TTM_PL_VRAM:
  434. drm->gem.vram_available += bo->base.size;
  435. break;
  436. case TTM_PL_TT:
  437. drm->gem.gart_available += bo->base.size;
  438. break;
  439. default:
  440. break;
  441. }
  442. }
  443. ttm_bo_unreserve(bo);
  444. return 0;
  445. }
  446. int
  447. nouveau_bo_map(struct nouveau_bo *nvbo)
  448. {
  449. int ret;
  450. ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
  451. if (ret)
  452. return ret;
  453. ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.resource->num_pages, &nvbo->kmap);
  454. ttm_bo_unreserve(&nvbo->bo);
  455. return ret;
  456. }
  457. void
  458. nouveau_bo_unmap(struct nouveau_bo *nvbo)
  459. {
  460. if (!nvbo)
  461. return;
  462. ttm_bo_kunmap(&nvbo->kmap);
  463. }
  464. void
  465. nouveau_bo_sync_for_device(struct nouveau_bo *nvbo)
  466. {
  467. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  468. struct ttm_tt *ttm_dma = (struct ttm_tt *)nvbo->bo.ttm;
  469. int i, j;
  470. if (!ttm_dma || !ttm_dma->dma_address)
  471. return;
  472. if (!ttm_dma->pages) {
  473. NV_DEBUG(drm, "ttm_dma 0x%p: pages NULL\n", ttm_dma);
  474. return;
  475. }
  476. /* Don't waste time looping if the object is coherent */
  477. if (nvbo->force_coherent)
  478. return;
  479. i = 0;
  480. while (i < ttm_dma->num_pages) {
  481. struct page *p = ttm_dma->pages[i];
  482. size_t num_pages = 1;
  483. for (j = i + 1; j < ttm_dma->num_pages; ++j) {
  484. if (++p != ttm_dma->pages[j])
  485. break;
  486. ++num_pages;
  487. }
  488. dma_sync_single_for_device(drm->dev->dev,
  489. ttm_dma->dma_address[i],
  490. num_pages * PAGE_SIZE, DMA_TO_DEVICE);
  491. i += num_pages;
  492. }
  493. }
  494. void
  495. nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo)
  496. {
  497. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  498. struct ttm_tt *ttm_dma = (struct ttm_tt *)nvbo->bo.ttm;
  499. int i, j;
  500. if (!ttm_dma || !ttm_dma->dma_address)
  501. return;
  502. if (!ttm_dma->pages) {
  503. NV_DEBUG(drm, "ttm_dma 0x%p: pages NULL\n", ttm_dma);
  504. return;
  505. }
  506. /* Don't waste time looping if the object is coherent */
  507. if (nvbo->force_coherent)
  508. return;
  509. i = 0;
  510. while (i < ttm_dma->num_pages) {
  511. struct page *p = ttm_dma->pages[i];
  512. size_t num_pages = 1;
  513. for (j = i + 1; j < ttm_dma->num_pages; ++j) {
  514. if (++p != ttm_dma->pages[j])
  515. break;
  516. ++num_pages;
  517. }
  518. dma_sync_single_for_cpu(drm->dev->dev, ttm_dma->dma_address[i],
  519. num_pages * PAGE_SIZE, DMA_FROM_DEVICE);
  520. i += num_pages;
  521. }
  522. }
  523. void nouveau_bo_add_io_reserve_lru(struct ttm_buffer_object *bo)
  524. {
  525. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  526. struct nouveau_bo *nvbo = nouveau_bo(bo);
  527. mutex_lock(&drm->ttm.io_reserve_mutex);
  528. list_move_tail(&nvbo->io_reserve_lru, &drm->ttm.io_reserve_lru);
  529. mutex_unlock(&drm->ttm.io_reserve_mutex);
  530. }
  531. void nouveau_bo_del_io_reserve_lru(struct ttm_buffer_object *bo)
  532. {
  533. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  534. struct nouveau_bo *nvbo = nouveau_bo(bo);
  535. mutex_lock(&drm->ttm.io_reserve_mutex);
  536. list_del_init(&nvbo->io_reserve_lru);
  537. mutex_unlock(&drm->ttm.io_reserve_mutex);
  538. }
  539. int
  540. nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
  541. bool no_wait_gpu)
  542. {
  543. struct ttm_operation_ctx ctx = { interruptible, no_wait_gpu };
  544. int ret;
  545. ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, &ctx);
  546. if (ret)
  547. return ret;
  548. nouveau_bo_sync_for_device(nvbo);
  549. return 0;
  550. }
  551. void
  552. nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
  553. {
  554. bool is_iomem;
  555. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  556. mem += index;
  557. if (is_iomem)
  558. iowrite16_native(val, (void __force __iomem *)mem);
  559. else
  560. *mem = val;
  561. }
  562. u32
  563. nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
  564. {
  565. bool is_iomem;
  566. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  567. mem += index;
  568. if (is_iomem)
  569. return ioread32_native((void __force __iomem *)mem);
  570. else
  571. return *mem;
  572. }
  573. void
  574. nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
  575. {
  576. bool is_iomem;
  577. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  578. mem += index;
  579. if (is_iomem)
  580. iowrite32_native(val, (void __force __iomem *)mem);
  581. else
  582. *mem = val;
  583. }
  584. static struct ttm_tt *
  585. nouveau_ttm_tt_create(struct ttm_buffer_object *bo, uint32_t page_flags)
  586. {
  587. #if IS_ENABLED(CONFIG_AGP)
  588. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  589. if (drm->agp.bridge) {
  590. return ttm_agp_tt_create(bo, drm->agp.bridge, page_flags);
  591. }
  592. #endif
  593. return nouveau_sgdma_create_ttm(bo, page_flags);
  594. }
  595. static int
  596. nouveau_ttm_tt_bind(struct ttm_device *bdev, struct ttm_tt *ttm,
  597. struct ttm_resource *reg)
  598. {
  599. #if IS_ENABLED(CONFIG_AGP)
  600. struct nouveau_drm *drm = nouveau_bdev(bdev);
  601. #endif
  602. if (!reg)
  603. return -EINVAL;
  604. #if IS_ENABLED(CONFIG_AGP)
  605. if (drm->agp.bridge)
  606. return ttm_agp_bind(ttm, reg);
  607. #endif
  608. return nouveau_sgdma_bind(bdev, ttm, reg);
  609. }
  610. static void
  611. nouveau_ttm_tt_unbind(struct ttm_device *bdev, struct ttm_tt *ttm)
  612. {
  613. #if IS_ENABLED(CONFIG_AGP)
  614. struct nouveau_drm *drm = nouveau_bdev(bdev);
  615. if (drm->agp.bridge) {
  616. ttm_agp_unbind(ttm);
  617. return;
  618. }
  619. #endif
  620. nouveau_sgdma_unbind(bdev, ttm);
  621. }
  622. static void
  623. nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  624. {
  625. struct nouveau_bo *nvbo = nouveau_bo(bo);
  626. switch (bo->resource->mem_type) {
  627. case TTM_PL_VRAM:
  628. nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART,
  629. NOUVEAU_GEM_DOMAIN_CPU);
  630. break;
  631. default:
  632. nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_CPU, 0);
  633. break;
  634. }
  635. *pl = nvbo->placement;
  636. }
  637. static int
  638. nouveau_bo_move_prep(struct nouveau_drm *drm, struct ttm_buffer_object *bo,
  639. struct ttm_resource *reg)
  640. {
  641. struct nouveau_mem *old_mem = nouveau_mem(bo->resource);
  642. struct nouveau_mem *new_mem = nouveau_mem(reg);
  643. struct nvif_vmm *vmm = &drm->client.vmm.vmm;
  644. int ret;
  645. ret = nvif_vmm_get(vmm, LAZY, false, old_mem->mem.page, 0,
  646. old_mem->mem.size, &old_mem->vma[0]);
  647. if (ret)
  648. return ret;
  649. ret = nvif_vmm_get(vmm, LAZY, false, new_mem->mem.page, 0,
  650. new_mem->mem.size, &old_mem->vma[1]);
  651. if (ret)
  652. goto done;
  653. ret = nouveau_mem_map(old_mem, vmm, &old_mem->vma[0]);
  654. if (ret)
  655. goto done;
  656. ret = nouveau_mem_map(new_mem, vmm, &old_mem->vma[1]);
  657. done:
  658. if (ret) {
  659. nvif_vmm_put(vmm, &old_mem->vma[1]);
  660. nvif_vmm_put(vmm, &old_mem->vma[0]);
  661. }
  662. return 0;
  663. }
  664. static int
  665. nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict,
  666. struct ttm_operation_ctx *ctx,
  667. struct ttm_resource *new_reg)
  668. {
  669. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  670. struct nouveau_channel *chan = drm->ttm.chan;
  671. struct nouveau_cli *cli = (void *)chan->user.client;
  672. struct nouveau_fence *fence;
  673. int ret;
  674. /* create temporary vmas for the transfer and attach them to the
  675. * old nvkm_mem node, these will get cleaned up after ttm has
  676. * destroyed the ttm_resource
  677. */
  678. if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
  679. ret = nouveau_bo_move_prep(drm, bo, new_reg);
  680. if (ret)
  681. return ret;
  682. }
  683. if (drm_drv_uses_atomic_modeset(drm->dev))
  684. mutex_lock(&cli->mutex);
  685. else
  686. mutex_lock_nested(&cli->mutex, SINGLE_DEPTH_NESTING);
  687. ret = nouveau_fence_sync(nouveau_bo(bo), chan, true, ctx->interruptible);
  688. if (ret == 0) {
  689. ret = drm->ttm.move(chan, bo, bo->resource, new_reg);
  690. if (ret == 0) {
  691. ret = nouveau_fence_new(chan, false, &fence);
  692. if (ret == 0) {
  693. /* TODO: figure out a better solution here
  694. *
  695. * wait on the fence here explicitly as going through
  696. * ttm_bo_move_accel_cleanup somehow doesn't seem to do it.
  697. *
  698. * Without this the operation can timeout and we'll fallback to a
  699. * software copy, which might take several minutes to finish.
  700. */
  701. nouveau_fence_wait(fence, false, false);
  702. ret = ttm_bo_move_accel_cleanup(bo,
  703. &fence->base,
  704. evict, false,
  705. new_reg);
  706. nouveau_fence_unref(&fence);
  707. }
  708. }
  709. }
  710. mutex_unlock(&cli->mutex);
  711. return ret;
  712. }
  713. void
  714. nouveau_bo_move_init(struct nouveau_drm *drm)
  715. {
  716. static const struct _method_table {
  717. const char *name;
  718. int engine;
  719. s32 oclass;
  720. int (*exec)(struct nouveau_channel *,
  721. struct ttm_buffer_object *,
  722. struct ttm_resource *, struct ttm_resource *);
  723. int (*init)(struct nouveau_channel *, u32 handle);
  724. } _methods[] = {
  725. { "COPY", 4, 0xc7b5, nve0_bo_move_copy, nve0_bo_move_init },
  726. { "COPY", 4, 0xc5b5, nve0_bo_move_copy, nve0_bo_move_init },
  727. { "GRCE", 0, 0xc5b5, nve0_bo_move_copy, nvc0_bo_move_init },
  728. { "COPY", 4, 0xc3b5, nve0_bo_move_copy, nve0_bo_move_init },
  729. { "GRCE", 0, 0xc3b5, nve0_bo_move_copy, nvc0_bo_move_init },
  730. { "COPY", 4, 0xc1b5, nve0_bo_move_copy, nve0_bo_move_init },
  731. { "GRCE", 0, 0xc1b5, nve0_bo_move_copy, nvc0_bo_move_init },
  732. { "COPY", 4, 0xc0b5, nve0_bo_move_copy, nve0_bo_move_init },
  733. { "GRCE", 0, 0xc0b5, nve0_bo_move_copy, nvc0_bo_move_init },
  734. { "COPY", 4, 0xb0b5, nve0_bo_move_copy, nve0_bo_move_init },
  735. { "GRCE", 0, 0xb0b5, nve0_bo_move_copy, nvc0_bo_move_init },
  736. { "COPY", 4, 0xa0b5, nve0_bo_move_copy, nve0_bo_move_init },
  737. { "GRCE", 0, 0xa0b5, nve0_bo_move_copy, nvc0_bo_move_init },
  738. { "COPY1", 5, 0x90b8, nvc0_bo_move_copy, nvc0_bo_move_init },
  739. { "COPY0", 4, 0x90b5, nvc0_bo_move_copy, nvc0_bo_move_init },
  740. { "COPY", 0, 0x85b5, nva3_bo_move_copy, nv50_bo_move_init },
  741. { "CRYPT", 0, 0x74c1, nv84_bo_move_exec, nv50_bo_move_init },
  742. { "M2MF", 0, 0x9039, nvc0_bo_move_m2mf, nvc0_bo_move_init },
  743. { "M2MF", 0, 0x5039, nv50_bo_move_m2mf, nv50_bo_move_init },
  744. { "M2MF", 0, 0x0039, nv04_bo_move_m2mf, nv04_bo_move_init },
  745. {},
  746. };
  747. const struct _method_table *mthd = _methods;
  748. const char *name = "CPU";
  749. int ret;
  750. do {
  751. struct nouveau_channel *chan;
  752. if (mthd->engine)
  753. chan = drm->cechan;
  754. else
  755. chan = drm->channel;
  756. if (chan == NULL)
  757. continue;
  758. ret = nvif_object_ctor(&chan->user, "ttmBoMove",
  759. mthd->oclass | (mthd->engine << 16),
  760. mthd->oclass, NULL, 0,
  761. &drm->ttm.copy);
  762. if (ret == 0) {
  763. ret = mthd->init(chan, drm->ttm.copy.handle);
  764. if (ret) {
  765. nvif_object_dtor(&drm->ttm.copy);
  766. continue;
  767. }
  768. drm->ttm.move = mthd->exec;
  769. drm->ttm.chan = chan;
  770. name = mthd->name;
  771. break;
  772. }
  773. } while ((++mthd)->exec);
  774. NV_INFO(drm, "MM: using %s for buffer copies\n", name);
  775. }
  776. static void nouveau_bo_move_ntfy(struct ttm_buffer_object *bo,
  777. struct ttm_resource *new_reg)
  778. {
  779. struct nouveau_mem *mem = new_reg ? nouveau_mem(new_reg) : NULL;
  780. struct nouveau_bo *nvbo = nouveau_bo(bo);
  781. struct nouveau_vma *vma;
  782. /* ttm can now (stupidly) pass the driver bos it didn't create... */
  783. if (bo->destroy != nouveau_bo_del_ttm)
  784. return;
  785. nouveau_bo_del_io_reserve_lru(bo);
  786. if (mem && new_reg->mem_type != TTM_PL_SYSTEM &&
  787. mem->mem.page == nvbo->page) {
  788. list_for_each_entry(vma, &nvbo->vma_list, head) {
  789. nouveau_vma_map(vma, mem);
  790. }
  791. } else {
  792. list_for_each_entry(vma, &nvbo->vma_list, head) {
  793. WARN_ON(ttm_bo_wait(bo, false, false));
  794. nouveau_vma_unmap(vma);
  795. }
  796. }
  797. if (new_reg)
  798. nvbo->offset = (new_reg->start << PAGE_SHIFT);
  799. }
  800. static int
  801. nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_resource *new_reg,
  802. struct nouveau_drm_tile **new_tile)
  803. {
  804. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  805. struct drm_device *dev = drm->dev;
  806. struct nouveau_bo *nvbo = nouveau_bo(bo);
  807. u64 offset = new_reg->start << PAGE_SHIFT;
  808. *new_tile = NULL;
  809. if (new_reg->mem_type != TTM_PL_VRAM)
  810. return 0;
  811. if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
  812. *new_tile = nv10_bo_set_tiling(dev, offset, bo->base.size,
  813. nvbo->mode, nvbo->zeta);
  814. }
  815. return 0;
  816. }
  817. static void
  818. nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
  819. struct nouveau_drm_tile *new_tile,
  820. struct nouveau_drm_tile **old_tile)
  821. {
  822. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  823. struct drm_device *dev = drm->dev;
  824. struct dma_fence *fence;
  825. int ret;
  826. ret = dma_resv_get_singleton(bo->base.resv, DMA_RESV_USAGE_WRITE,
  827. &fence);
  828. if (ret)
  829. dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_WRITE,
  830. false, MAX_SCHEDULE_TIMEOUT);
  831. nv10_bo_put_tile_region(dev, *old_tile, fence);
  832. *old_tile = new_tile;
  833. }
  834. static int
  835. nouveau_bo_move(struct ttm_buffer_object *bo, bool evict,
  836. struct ttm_operation_ctx *ctx,
  837. struct ttm_resource *new_reg,
  838. struct ttm_place *hop)
  839. {
  840. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  841. struct nouveau_bo *nvbo = nouveau_bo(bo);
  842. struct ttm_resource *old_reg = bo->resource;
  843. struct nouveau_drm_tile *new_tile = NULL;
  844. int ret = 0;
  845. if (new_reg->mem_type == TTM_PL_TT) {
  846. ret = nouveau_ttm_tt_bind(bo->bdev, bo->ttm, new_reg);
  847. if (ret)
  848. return ret;
  849. }
  850. nouveau_bo_move_ntfy(bo, new_reg);
  851. ret = ttm_bo_wait_ctx(bo, ctx);
  852. if (ret)
  853. goto out_ntfy;
  854. if (nvbo->bo.pin_count)
  855. NV_WARN(drm, "Moving pinned object %p!\n", nvbo);
  856. if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  857. ret = nouveau_bo_vm_bind(bo, new_reg, &new_tile);
  858. if (ret)
  859. goto out_ntfy;
  860. }
  861. /* Fake bo copy. */
  862. if (!old_reg || (old_reg->mem_type == TTM_PL_SYSTEM &&
  863. !bo->ttm)) {
  864. ttm_bo_move_null(bo, new_reg);
  865. goto out;
  866. }
  867. if (old_reg->mem_type == TTM_PL_SYSTEM &&
  868. new_reg->mem_type == TTM_PL_TT) {
  869. ttm_bo_move_null(bo, new_reg);
  870. goto out;
  871. }
  872. if (old_reg->mem_type == TTM_PL_TT &&
  873. new_reg->mem_type == TTM_PL_SYSTEM) {
  874. nouveau_ttm_tt_unbind(bo->bdev, bo->ttm);
  875. ttm_resource_free(bo, &bo->resource);
  876. ttm_bo_assign_mem(bo, new_reg);
  877. goto out;
  878. }
  879. /* Hardware assisted copy. */
  880. if (drm->ttm.move) {
  881. if ((old_reg->mem_type == TTM_PL_SYSTEM &&
  882. new_reg->mem_type == TTM_PL_VRAM) ||
  883. (old_reg->mem_type == TTM_PL_VRAM &&
  884. new_reg->mem_type == TTM_PL_SYSTEM)) {
  885. hop->fpfn = 0;
  886. hop->lpfn = 0;
  887. hop->mem_type = TTM_PL_TT;
  888. hop->flags = 0;
  889. return -EMULTIHOP;
  890. }
  891. ret = nouveau_bo_move_m2mf(bo, evict, ctx,
  892. new_reg);
  893. } else
  894. ret = -ENODEV;
  895. if (ret) {
  896. /* Fallback to software copy. */
  897. ret = ttm_bo_move_memcpy(bo, ctx, new_reg);
  898. }
  899. out:
  900. if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  901. if (ret)
  902. nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
  903. else
  904. nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
  905. }
  906. out_ntfy:
  907. if (ret) {
  908. nouveau_bo_move_ntfy(bo, bo->resource);
  909. }
  910. return ret;
  911. }
  912. static void
  913. nouveau_ttm_io_mem_free_locked(struct nouveau_drm *drm,
  914. struct ttm_resource *reg)
  915. {
  916. struct nouveau_mem *mem = nouveau_mem(reg);
  917. if (drm->client.mem->oclass >= NVIF_CLASS_MEM_NV50) {
  918. switch (reg->mem_type) {
  919. case TTM_PL_TT:
  920. if (mem->kind)
  921. nvif_object_unmap_handle(&mem->mem.object);
  922. break;
  923. case TTM_PL_VRAM:
  924. nvif_object_unmap_handle(&mem->mem.object);
  925. break;
  926. default:
  927. break;
  928. }
  929. }
  930. }
  931. static int
  932. nouveau_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *reg)
  933. {
  934. struct nouveau_drm *drm = nouveau_bdev(bdev);
  935. struct nvkm_device *device = nvxx_device(&drm->client.device);
  936. struct nouveau_mem *mem = nouveau_mem(reg);
  937. struct nvif_mmu *mmu = &drm->client.mmu;
  938. int ret;
  939. mutex_lock(&drm->ttm.io_reserve_mutex);
  940. retry:
  941. switch (reg->mem_type) {
  942. case TTM_PL_SYSTEM:
  943. /* System memory */
  944. ret = 0;
  945. goto out;
  946. case TTM_PL_TT:
  947. #if IS_ENABLED(CONFIG_AGP)
  948. if (drm->agp.bridge) {
  949. reg->bus.offset = (reg->start << PAGE_SHIFT) +
  950. drm->agp.base;
  951. reg->bus.is_iomem = !drm->agp.cma;
  952. reg->bus.caching = ttm_write_combined;
  953. }
  954. #endif
  955. if (drm->client.mem->oclass < NVIF_CLASS_MEM_NV50 ||
  956. !mem->kind) {
  957. /* untiled */
  958. ret = 0;
  959. break;
  960. }
  961. fallthrough; /* tiled memory */
  962. case TTM_PL_VRAM:
  963. reg->bus.offset = (reg->start << PAGE_SHIFT) +
  964. device->func->resource_addr(device, 1);
  965. reg->bus.is_iomem = true;
  966. /* Some BARs do not support being ioremapped WC */
  967. if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
  968. mmu->type[drm->ttm.type_vram].type & NVIF_MEM_UNCACHED)
  969. reg->bus.caching = ttm_uncached;
  970. else
  971. reg->bus.caching = ttm_write_combined;
  972. if (drm->client.mem->oclass >= NVIF_CLASS_MEM_NV50) {
  973. union {
  974. struct nv50_mem_map_v0 nv50;
  975. struct gf100_mem_map_v0 gf100;
  976. } args;
  977. u64 handle, length;
  978. u32 argc = 0;
  979. switch (mem->mem.object.oclass) {
  980. case NVIF_CLASS_MEM_NV50:
  981. args.nv50.version = 0;
  982. args.nv50.ro = 0;
  983. args.nv50.kind = mem->kind;
  984. args.nv50.comp = mem->comp;
  985. argc = sizeof(args.nv50);
  986. break;
  987. case NVIF_CLASS_MEM_GF100:
  988. args.gf100.version = 0;
  989. args.gf100.ro = 0;
  990. args.gf100.kind = mem->kind;
  991. argc = sizeof(args.gf100);
  992. break;
  993. default:
  994. WARN_ON(1);
  995. break;
  996. }
  997. ret = nvif_object_map_handle(&mem->mem.object,
  998. &args, argc,
  999. &handle, &length);
  1000. if (ret != 1) {
  1001. if (WARN_ON(ret == 0))
  1002. ret = -EINVAL;
  1003. goto out;
  1004. }
  1005. reg->bus.offset = handle;
  1006. }
  1007. ret = 0;
  1008. break;
  1009. default:
  1010. ret = -EINVAL;
  1011. }
  1012. out:
  1013. if (ret == -ENOSPC) {
  1014. struct nouveau_bo *nvbo;
  1015. nvbo = list_first_entry_or_null(&drm->ttm.io_reserve_lru,
  1016. typeof(*nvbo),
  1017. io_reserve_lru);
  1018. if (nvbo) {
  1019. list_del_init(&nvbo->io_reserve_lru);
  1020. drm_vma_node_unmap(&nvbo->bo.base.vma_node,
  1021. bdev->dev_mapping);
  1022. nouveau_ttm_io_mem_free_locked(drm, nvbo->bo.resource);
  1023. goto retry;
  1024. }
  1025. }
  1026. mutex_unlock(&drm->ttm.io_reserve_mutex);
  1027. return ret;
  1028. }
  1029. static void
  1030. nouveau_ttm_io_mem_free(struct ttm_device *bdev, struct ttm_resource *reg)
  1031. {
  1032. struct nouveau_drm *drm = nouveau_bdev(bdev);
  1033. mutex_lock(&drm->ttm.io_reserve_mutex);
  1034. nouveau_ttm_io_mem_free_locked(drm, reg);
  1035. mutex_unlock(&drm->ttm.io_reserve_mutex);
  1036. }
  1037. vm_fault_t nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  1038. {
  1039. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  1040. struct nouveau_bo *nvbo = nouveau_bo(bo);
  1041. struct nvkm_device *device = nvxx_device(&drm->client.device);
  1042. u32 mappable = device->func->resource_size(device, 1) >> PAGE_SHIFT;
  1043. int i, ret;
  1044. /* as long as the bo isn't in vram, and isn't tiled, we've got
  1045. * nothing to do here.
  1046. */
  1047. if (bo->resource->mem_type != TTM_PL_VRAM) {
  1048. if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA ||
  1049. !nvbo->kind)
  1050. return 0;
  1051. if (bo->resource->mem_type != TTM_PL_SYSTEM)
  1052. return 0;
  1053. nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART, 0);
  1054. } else {
  1055. /* make sure bo is in mappable vram */
  1056. if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA ||
  1057. bo->resource->start + bo->resource->num_pages < mappable)
  1058. return 0;
  1059. for (i = 0; i < nvbo->placement.num_placement; ++i) {
  1060. nvbo->placements[i].fpfn = 0;
  1061. nvbo->placements[i].lpfn = mappable;
  1062. }
  1063. for (i = 0; i < nvbo->placement.num_busy_placement; ++i) {
  1064. nvbo->busy_placements[i].fpfn = 0;
  1065. nvbo->busy_placements[i].lpfn = mappable;
  1066. }
  1067. nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_VRAM, 0);
  1068. }
  1069. ret = nouveau_bo_validate(nvbo, false, false);
  1070. if (unlikely(ret == -EBUSY || ret == -ERESTARTSYS))
  1071. return VM_FAULT_NOPAGE;
  1072. else if (unlikely(ret))
  1073. return VM_FAULT_SIGBUS;
  1074. ttm_bo_move_to_lru_tail_unlocked(bo);
  1075. return 0;
  1076. }
  1077. static int
  1078. nouveau_ttm_tt_populate(struct ttm_device *bdev,
  1079. struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
  1080. {
  1081. struct ttm_tt *ttm_dma = (void *)ttm;
  1082. struct nouveau_drm *drm;
  1083. bool slave = !!(ttm->page_flags & TTM_TT_FLAG_EXTERNAL);
  1084. if (ttm_tt_is_populated(ttm))
  1085. return 0;
  1086. if (slave && ttm->sg) {
  1087. drm_prime_sg_to_dma_addr_array(ttm->sg, ttm_dma->dma_address,
  1088. ttm->num_pages);
  1089. return 0;
  1090. }
  1091. drm = nouveau_bdev(bdev);
  1092. return ttm_pool_alloc(&drm->ttm.bdev.pool, ttm, ctx);
  1093. }
  1094. static void
  1095. nouveau_ttm_tt_unpopulate(struct ttm_device *bdev,
  1096. struct ttm_tt *ttm)
  1097. {
  1098. struct nouveau_drm *drm;
  1099. bool slave = !!(ttm->page_flags & TTM_TT_FLAG_EXTERNAL);
  1100. if (slave)
  1101. return;
  1102. nouveau_ttm_tt_unbind(bdev, ttm);
  1103. drm = nouveau_bdev(bdev);
  1104. return ttm_pool_free(&drm->ttm.bdev.pool, ttm);
  1105. }
  1106. static void
  1107. nouveau_ttm_tt_destroy(struct ttm_device *bdev,
  1108. struct ttm_tt *ttm)
  1109. {
  1110. #if IS_ENABLED(CONFIG_AGP)
  1111. struct nouveau_drm *drm = nouveau_bdev(bdev);
  1112. if (drm->agp.bridge) {
  1113. ttm_agp_destroy(ttm);
  1114. return;
  1115. }
  1116. #endif
  1117. nouveau_sgdma_destroy(bdev, ttm);
  1118. }
  1119. void
  1120. nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence, bool exclusive)
  1121. {
  1122. struct dma_resv *resv = nvbo->bo.base.resv;
  1123. if (!fence)
  1124. return;
  1125. dma_resv_add_fence(resv, &fence->base, exclusive ?
  1126. DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_READ);
  1127. }
  1128. static void
  1129. nouveau_bo_delete_mem_notify(struct ttm_buffer_object *bo)
  1130. {
  1131. nouveau_bo_move_ntfy(bo, NULL);
  1132. }
  1133. struct ttm_device_funcs nouveau_bo_driver = {
  1134. .ttm_tt_create = &nouveau_ttm_tt_create,
  1135. .ttm_tt_populate = &nouveau_ttm_tt_populate,
  1136. .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
  1137. .ttm_tt_destroy = &nouveau_ttm_tt_destroy,
  1138. .eviction_valuable = ttm_bo_eviction_valuable,
  1139. .evict_flags = nouveau_bo_evict_flags,
  1140. .delete_mem_notify = nouveau_bo_delete_mem_notify,
  1141. .move = nouveau_bo_move,
  1142. .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
  1143. .io_mem_free = &nouveau_ttm_io_mem_free,
  1144. };