nouveau_bo.c 39 KB

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