nouveau_bo.c 39 KB

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