nouveau_bo.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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 "drmP.h"
  30. #include "nouveau_drm.h"
  31. #include "nouveau_drv.h"
  32. #include "nouveau_dma.h"
  33. #include <linux/log2.h>
  34. #include <linux/slab.h>
  35. static void
  36. nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
  37. {
  38. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  39. struct drm_device *dev = dev_priv->dev;
  40. struct nouveau_bo *nvbo = nouveau_bo(bo);
  41. if (unlikely(nvbo->gem))
  42. DRM_ERROR("bo %p still attached to GEM object\n", bo);
  43. if (nvbo->tile)
  44. nv10_mem_expire_tiling(dev, nvbo->tile, NULL);
  45. kfree(nvbo);
  46. }
  47. static void
  48. nouveau_bo_fixup_align(struct drm_device *dev,
  49. uint32_t tile_mode, uint32_t tile_flags,
  50. int *align, int *size)
  51. {
  52. struct drm_nouveau_private *dev_priv = dev->dev_private;
  53. /*
  54. * Some of the tile_flags have a periodic structure of N*4096 bytes,
  55. * align to to that as well as the page size. Align the size to the
  56. * appropriate boundaries. This does imply that sizes are rounded up
  57. * 3-7 pages, so be aware of this and do not waste memory by allocating
  58. * many small buffers.
  59. */
  60. if (dev_priv->card_type == NV_50) {
  61. uint32_t block_size = dev_priv->vram_size >> 15;
  62. int i;
  63. switch (tile_flags) {
  64. case 0x1800:
  65. case 0x2800:
  66. case 0x4800:
  67. case 0x7a00:
  68. if (is_power_of_2(block_size)) {
  69. for (i = 1; i < 10; i++) {
  70. *align = 12 * i * block_size;
  71. if (!(*align % 65536))
  72. break;
  73. }
  74. } else {
  75. for (i = 1; i < 10; i++) {
  76. *align = 8 * i * block_size;
  77. if (!(*align % 65536))
  78. break;
  79. }
  80. }
  81. *size = roundup(*size, *align);
  82. break;
  83. default:
  84. break;
  85. }
  86. } else {
  87. if (tile_mode) {
  88. if (dev_priv->chipset >= 0x40) {
  89. *align = 65536;
  90. *size = roundup(*size, 64 * tile_mode);
  91. } else if (dev_priv->chipset >= 0x30) {
  92. *align = 32768;
  93. *size = roundup(*size, 64 * tile_mode);
  94. } else if (dev_priv->chipset >= 0x20) {
  95. *align = 16384;
  96. *size = roundup(*size, 64 * tile_mode);
  97. } else if (dev_priv->chipset >= 0x10) {
  98. *align = 16384;
  99. *size = roundup(*size, 32 * tile_mode);
  100. }
  101. }
  102. }
  103. /* ALIGN works only on powers of two. */
  104. *size = roundup(*size, PAGE_SIZE);
  105. if (dev_priv->card_type == NV_50) {
  106. *size = roundup(*size, 65536);
  107. *align = max(65536, *align);
  108. }
  109. }
  110. int
  111. nouveau_bo_new(struct drm_device *dev, struct nouveau_channel *chan,
  112. int size, int align, uint32_t flags, uint32_t tile_mode,
  113. uint32_t tile_flags, bool no_vm, bool mappable,
  114. struct nouveau_bo **pnvbo)
  115. {
  116. struct drm_nouveau_private *dev_priv = dev->dev_private;
  117. struct nouveau_bo *nvbo;
  118. int ret = 0;
  119. nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
  120. if (!nvbo)
  121. return -ENOMEM;
  122. INIT_LIST_HEAD(&nvbo->head);
  123. INIT_LIST_HEAD(&nvbo->entry);
  124. nvbo->mappable = mappable;
  125. nvbo->no_vm = no_vm;
  126. nvbo->tile_mode = tile_mode;
  127. nvbo->tile_flags = tile_flags;
  128. nvbo->bo.bdev = &dev_priv->ttm.bdev;
  129. nouveau_bo_fixup_align(dev, tile_mode, nouveau_bo_tile_layout(nvbo),
  130. &align, &size);
  131. align >>= PAGE_SHIFT;
  132. nouveau_bo_placement_set(nvbo, flags, 0);
  133. nvbo->channel = chan;
  134. ret = ttm_bo_init(&dev_priv->ttm.bdev, &nvbo->bo, size,
  135. ttm_bo_type_device, &nvbo->placement, align, 0,
  136. false, NULL, size, nouveau_bo_del_ttm);
  137. if (ret) {
  138. /* ttm will call nouveau_bo_del_ttm if it fails.. */
  139. return ret;
  140. }
  141. nvbo->channel = NULL;
  142. *pnvbo = nvbo;
  143. return 0;
  144. }
  145. static void
  146. set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
  147. {
  148. *n = 0;
  149. if (type & TTM_PL_FLAG_VRAM)
  150. pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
  151. if (type & TTM_PL_FLAG_TT)
  152. pl[(*n)++] = TTM_PL_FLAG_TT | flags;
  153. if (type & TTM_PL_FLAG_SYSTEM)
  154. pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
  155. }
  156. static void
  157. set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
  158. {
  159. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  160. if (dev_priv->card_type == NV_10 &&
  161. nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM)) {
  162. /*
  163. * Make sure that the color and depth buffers are handled
  164. * by independent memory controller units. Up to a 9x
  165. * speed up when alpha-blending and depth-test are enabled
  166. * at the same time.
  167. */
  168. int vram_pages = dev_priv->vram_size >> PAGE_SHIFT;
  169. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
  170. nvbo->placement.fpfn = vram_pages / 2;
  171. nvbo->placement.lpfn = ~0;
  172. } else {
  173. nvbo->placement.fpfn = 0;
  174. nvbo->placement.lpfn = vram_pages / 2;
  175. }
  176. }
  177. }
  178. void
  179. nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
  180. {
  181. struct ttm_placement *pl = &nvbo->placement;
  182. uint32_t flags = TTM_PL_MASK_CACHING |
  183. (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
  184. pl->placement = nvbo->placements;
  185. set_placement_list(nvbo->placements, &pl->num_placement,
  186. type, flags);
  187. pl->busy_placement = nvbo->busy_placements;
  188. set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
  189. type | busy, flags);
  190. set_placement_range(nvbo, type);
  191. }
  192. int
  193. nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
  194. {
  195. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  196. struct ttm_buffer_object *bo = &nvbo->bo;
  197. int ret;
  198. if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
  199. NV_ERROR(nouveau_bdev(bo->bdev)->dev,
  200. "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
  201. 1 << bo->mem.mem_type, memtype);
  202. return -EINVAL;
  203. }
  204. if (nvbo->pin_refcnt++)
  205. return 0;
  206. ret = ttm_bo_reserve(bo, false, false, false, 0);
  207. if (ret)
  208. goto out;
  209. nouveau_bo_placement_set(nvbo, memtype, 0);
  210. ret = ttm_bo_validate(bo, &nvbo->placement, false, false, false);
  211. if (ret == 0) {
  212. switch (bo->mem.mem_type) {
  213. case TTM_PL_VRAM:
  214. dev_priv->fb_aper_free -= bo->mem.size;
  215. break;
  216. case TTM_PL_TT:
  217. dev_priv->gart_info.aper_free -= bo->mem.size;
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. ttm_bo_unreserve(bo);
  224. out:
  225. if (unlikely(ret))
  226. nvbo->pin_refcnt--;
  227. return ret;
  228. }
  229. int
  230. nouveau_bo_unpin(struct nouveau_bo *nvbo)
  231. {
  232. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  233. struct ttm_buffer_object *bo = &nvbo->bo;
  234. int ret;
  235. if (--nvbo->pin_refcnt)
  236. return 0;
  237. ret = ttm_bo_reserve(bo, false, false, false, 0);
  238. if (ret)
  239. return ret;
  240. nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
  241. ret = ttm_bo_validate(bo, &nvbo->placement, false, false, false);
  242. if (ret == 0) {
  243. switch (bo->mem.mem_type) {
  244. case TTM_PL_VRAM:
  245. dev_priv->fb_aper_free += bo->mem.size;
  246. break;
  247. case TTM_PL_TT:
  248. dev_priv->gart_info.aper_free += bo->mem.size;
  249. break;
  250. default:
  251. break;
  252. }
  253. }
  254. ttm_bo_unreserve(bo);
  255. return ret;
  256. }
  257. int
  258. nouveau_bo_map(struct nouveau_bo *nvbo)
  259. {
  260. int ret;
  261. ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
  262. if (ret)
  263. return ret;
  264. ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
  265. ttm_bo_unreserve(&nvbo->bo);
  266. return ret;
  267. }
  268. void
  269. nouveau_bo_unmap(struct nouveau_bo *nvbo)
  270. {
  271. if (nvbo)
  272. ttm_bo_kunmap(&nvbo->kmap);
  273. }
  274. u16
  275. nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
  276. {
  277. bool is_iomem;
  278. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  279. mem = &mem[index];
  280. if (is_iomem)
  281. return ioread16_native((void __force __iomem *)mem);
  282. else
  283. return *mem;
  284. }
  285. void
  286. nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
  287. {
  288. bool is_iomem;
  289. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  290. mem = &mem[index];
  291. if (is_iomem)
  292. iowrite16_native(val, (void __force __iomem *)mem);
  293. else
  294. *mem = val;
  295. }
  296. u32
  297. nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
  298. {
  299. bool is_iomem;
  300. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  301. mem = &mem[index];
  302. if (is_iomem)
  303. return ioread32_native((void __force __iomem *)mem);
  304. else
  305. return *mem;
  306. }
  307. void
  308. nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
  309. {
  310. bool is_iomem;
  311. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  312. mem = &mem[index];
  313. if (is_iomem)
  314. iowrite32_native(val, (void __force __iomem *)mem);
  315. else
  316. *mem = val;
  317. }
  318. static struct ttm_backend *
  319. nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
  320. {
  321. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  322. struct drm_device *dev = dev_priv->dev;
  323. switch (dev_priv->gart_info.type) {
  324. #if __OS_HAS_AGP
  325. case NOUVEAU_GART_AGP:
  326. return ttm_agp_backend_init(bdev, dev->agp->bridge);
  327. #endif
  328. case NOUVEAU_GART_SGDMA:
  329. return nouveau_sgdma_init_ttm(dev);
  330. default:
  331. NV_ERROR(dev, "Unknown GART type %d\n",
  332. dev_priv->gart_info.type);
  333. break;
  334. }
  335. return NULL;
  336. }
  337. static int
  338. nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  339. {
  340. /* We'll do this from user space. */
  341. return 0;
  342. }
  343. static int
  344. nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  345. struct ttm_mem_type_manager *man)
  346. {
  347. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  348. struct drm_device *dev = dev_priv->dev;
  349. switch (type) {
  350. case TTM_PL_SYSTEM:
  351. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  352. man->available_caching = TTM_PL_MASK_CACHING;
  353. man->default_caching = TTM_PL_FLAG_CACHED;
  354. break;
  355. case TTM_PL_VRAM:
  356. man->func = &ttm_bo_manager_func;
  357. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  358. TTM_MEMTYPE_FLAG_MAPPABLE;
  359. man->available_caching = TTM_PL_FLAG_UNCACHED |
  360. TTM_PL_FLAG_WC;
  361. man->default_caching = TTM_PL_FLAG_WC;
  362. if (dev_priv->card_type == NV_50)
  363. man->gpu_offset = 0x40000000;
  364. else
  365. man->gpu_offset = 0;
  366. break;
  367. case TTM_PL_TT:
  368. man->func = &ttm_bo_manager_func;
  369. switch (dev_priv->gart_info.type) {
  370. case NOUVEAU_GART_AGP:
  371. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  372. man->available_caching = TTM_PL_FLAG_UNCACHED;
  373. man->default_caching = TTM_PL_FLAG_UNCACHED;
  374. break;
  375. case NOUVEAU_GART_SGDMA:
  376. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
  377. TTM_MEMTYPE_FLAG_CMA;
  378. man->available_caching = TTM_PL_MASK_CACHING;
  379. man->default_caching = TTM_PL_FLAG_CACHED;
  380. break;
  381. default:
  382. NV_ERROR(dev, "Unknown GART type: %d\n",
  383. dev_priv->gart_info.type);
  384. return -EINVAL;
  385. }
  386. man->gpu_offset = dev_priv->vm_gart_base;
  387. break;
  388. default:
  389. NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
  390. return -EINVAL;
  391. }
  392. return 0;
  393. }
  394. static void
  395. nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  396. {
  397. struct nouveau_bo *nvbo = nouveau_bo(bo);
  398. switch (bo->mem.mem_type) {
  399. case TTM_PL_VRAM:
  400. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
  401. TTM_PL_FLAG_SYSTEM);
  402. break;
  403. default:
  404. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
  405. break;
  406. }
  407. *pl = nvbo->placement;
  408. }
  409. /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
  410. * TTM_PL_{VRAM,TT} directly.
  411. */
  412. static int
  413. nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
  414. struct nouveau_bo *nvbo, bool evict,
  415. bool no_wait_reserve, bool no_wait_gpu,
  416. struct ttm_mem_reg *new_mem)
  417. {
  418. struct nouveau_fence *fence = NULL;
  419. int ret;
  420. ret = nouveau_fence_new(chan, &fence, true);
  421. if (ret)
  422. return ret;
  423. ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict,
  424. no_wait_reserve, no_wait_gpu, new_mem);
  425. nouveau_fence_unref((void *)&fence);
  426. return ret;
  427. }
  428. static inline uint32_t
  429. nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
  430. struct nouveau_channel *chan, struct ttm_mem_reg *mem)
  431. {
  432. struct nouveau_bo *nvbo = nouveau_bo(bo);
  433. if (nvbo->no_vm) {
  434. if (mem->mem_type == TTM_PL_TT)
  435. return NvDmaGART;
  436. return NvDmaVRAM;
  437. }
  438. if (mem->mem_type == TTM_PL_TT)
  439. return chan->gart_handle;
  440. return chan->vram_handle;
  441. }
  442. static int
  443. nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  444. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  445. {
  446. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  447. struct nouveau_bo *nvbo = nouveau_bo(bo);
  448. u64 length = (new_mem->num_pages << PAGE_SHIFT);
  449. u64 src_offset, dst_offset;
  450. int ret;
  451. src_offset = old_mem->start << PAGE_SHIFT;
  452. dst_offset = new_mem->start << PAGE_SHIFT;
  453. if (!nvbo->no_vm) {
  454. if (old_mem->mem_type == TTM_PL_VRAM)
  455. src_offset += dev_priv->vm_vram_base;
  456. else
  457. src_offset += dev_priv->vm_gart_base;
  458. if (new_mem->mem_type == TTM_PL_VRAM)
  459. dst_offset += dev_priv->vm_vram_base;
  460. else
  461. dst_offset += dev_priv->vm_gart_base;
  462. }
  463. ret = RING_SPACE(chan, 3);
  464. if (ret)
  465. return ret;
  466. BEGIN_RING(chan, NvSubM2MF, 0x0184, 2);
  467. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  468. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  469. while (length) {
  470. u32 amount, stride, height;
  471. amount = min(length, (u64)(4 * 1024 * 1024));
  472. stride = 16 * 4;
  473. height = amount / stride;
  474. if (new_mem->mem_type == TTM_PL_VRAM &&
  475. nouveau_bo_tile_layout(nvbo)) {
  476. ret = RING_SPACE(chan, 8);
  477. if (ret)
  478. return ret;
  479. BEGIN_RING(chan, NvSubM2MF, 0x0200, 7);
  480. OUT_RING (chan, 0);
  481. OUT_RING (chan, 0);
  482. OUT_RING (chan, stride);
  483. OUT_RING (chan, height);
  484. OUT_RING (chan, 1);
  485. OUT_RING (chan, 0);
  486. OUT_RING (chan, 0);
  487. } else {
  488. ret = RING_SPACE(chan, 2);
  489. if (ret)
  490. return ret;
  491. BEGIN_RING(chan, NvSubM2MF, 0x0200, 1);
  492. OUT_RING (chan, 1);
  493. }
  494. if (old_mem->mem_type == TTM_PL_VRAM &&
  495. nouveau_bo_tile_layout(nvbo)) {
  496. ret = RING_SPACE(chan, 8);
  497. if (ret)
  498. return ret;
  499. BEGIN_RING(chan, NvSubM2MF, 0x021c, 7);
  500. OUT_RING (chan, 0);
  501. OUT_RING (chan, 0);
  502. OUT_RING (chan, stride);
  503. OUT_RING (chan, height);
  504. OUT_RING (chan, 1);
  505. OUT_RING (chan, 0);
  506. OUT_RING (chan, 0);
  507. } else {
  508. ret = RING_SPACE(chan, 2);
  509. if (ret)
  510. return ret;
  511. BEGIN_RING(chan, NvSubM2MF, 0x021c, 1);
  512. OUT_RING (chan, 1);
  513. }
  514. ret = RING_SPACE(chan, 14);
  515. if (ret)
  516. return ret;
  517. BEGIN_RING(chan, NvSubM2MF, 0x0238, 2);
  518. OUT_RING (chan, upper_32_bits(src_offset));
  519. OUT_RING (chan, upper_32_bits(dst_offset));
  520. BEGIN_RING(chan, NvSubM2MF, 0x030c, 8);
  521. OUT_RING (chan, lower_32_bits(src_offset));
  522. OUT_RING (chan, lower_32_bits(dst_offset));
  523. OUT_RING (chan, stride);
  524. OUT_RING (chan, stride);
  525. OUT_RING (chan, stride);
  526. OUT_RING (chan, height);
  527. OUT_RING (chan, 0x00000101);
  528. OUT_RING (chan, 0x00000000);
  529. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  530. OUT_RING (chan, 0);
  531. length -= amount;
  532. src_offset += amount;
  533. dst_offset += amount;
  534. }
  535. return 0;
  536. }
  537. static int
  538. nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  539. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  540. {
  541. u32 src_offset = old_mem->start << PAGE_SHIFT;
  542. u32 dst_offset = new_mem->start << PAGE_SHIFT;
  543. u32 page_count = new_mem->num_pages;
  544. int ret;
  545. ret = RING_SPACE(chan, 3);
  546. if (ret)
  547. return ret;
  548. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
  549. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  550. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  551. page_count = new_mem->num_pages;
  552. while (page_count) {
  553. int line_count = (page_count > 2047) ? 2047 : page_count;
  554. ret = RING_SPACE(chan, 11);
  555. if (ret)
  556. return ret;
  557. BEGIN_RING(chan, NvSubM2MF,
  558. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  559. OUT_RING (chan, src_offset);
  560. OUT_RING (chan, dst_offset);
  561. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  562. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  563. OUT_RING (chan, PAGE_SIZE); /* line_length */
  564. OUT_RING (chan, line_count);
  565. OUT_RING (chan, 0x00000101);
  566. OUT_RING (chan, 0x00000000);
  567. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  568. OUT_RING (chan, 0);
  569. page_count -= line_count;
  570. src_offset += (PAGE_SIZE * line_count);
  571. dst_offset += (PAGE_SIZE * line_count);
  572. }
  573. return 0;
  574. }
  575. static int
  576. nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
  577. bool no_wait_reserve, bool no_wait_gpu,
  578. struct ttm_mem_reg *new_mem)
  579. {
  580. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  581. struct nouveau_bo *nvbo = nouveau_bo(bo);
  582. struct nouveau_channel *chan;
  583. int ret;
  584. chan = nvbo->channel;
  585. if (!chan || nvbo->no_vm) {
  586. chan = dev_priv->channel;
  587. mutex_lock(&chan->mutex);
  588. }
  589. if (dev_priv->card_type < NV_50)
  590. ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  591. else
  592. ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  593. if (ret == 0) {
  594. ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
  595. no_wait_reserve,
  596. no_wait_gpu, new_mem);
  597. }
  598. if (chan == dev_priv->channel)
  599. mutex_unlock(&chan->mutex);
  600. return ret;
  601. }
  602. static int
  603. nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
  604. bool no_wait_reserve, bool no_wait_gpu,
  605. struct ttm_mem_reg *new_mem)
  606. {
  607. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  608. struct ttm_placement placement;
  609. struct ttm_mem_reg tmp_mem;
  610. int ret;
  611. placement.fpfn = placement.lpfn = 0;
  612. placement.num_placement = placement.num_busy_placement = 1;
  613. placement.placement = placement.busy_placement = &placement_memtype;
  614. tmp_mem = *new_mem;
  615. tmp_mem.mm_node = NULL;
  616. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  617. if (ret)
  618. return ret;
  619. ret = ttm_tt_bind(bo->ttm, &tmp_mem);
  620. if (ret)
  621. goto out;
  622. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
  623. if (ret)
  624. goto out;
  625. ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  626. out:
  627. ttm_bo_mem_put(bo, &tmp_mem);
  628. return ret;
  629. }
  630. static int
  631. nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
  632. bool no_wait_reserve, bool no_wait_gpu,
  633. struct ttm_mem_reg *new_mem)
  634. {
  635. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  636. struct ttm_placement placement;
  637. struct ttm_mem_reg tmp_mem;
  638. int ret;
  639. placement.fpfn = placement.lpfn = 0;
  640. placement.num_placement = placement.num_busy_placement = 1;
  641. placement.placement = placement.busy_placement = &placement_memtype;
  642. tmp_mem = *new_mem;
  643. tmp_mem.mm_node = NULL;
  644. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  645. if (ret)
  646. return ret;
  647. ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, &tmp_mem);
  648. if (ret)
  649. goto out;
  650. ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  651. if (ret)
  652. goto out;
  653. out:
  654. ttm_bo_mem_put(bo, &tmp_mem);
  655. return ret;
  656. }
  657. static int
  658. nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
  659. struct nouveau_tile_reg **new_tile)
  660. {
  661. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  662. struct drm_device *dev = dev_priv->dev;
  663. struct nouveau_bo *nvbo = nouveau_bo(bo);
  664. uint64_t offset;
  665. int ret;
  666. if (nvbo->no_vm || new_mem->mem_type != TTM_PL_VRAM) {
  667. /* Nothing to do. */
  668. *new_tile = NULL;
  669. return 0;
  670. }
  671. offset = new_mem->start << PAGE_SHIFT;
  672. if (dev_priv->card_type == NV_50) {
  673. ret = nv50_mem_vm_bind_linear(dev,
  674. offset + dev_priv->vm_vram_base,
  675. new_mem->size,
  676. nouveau_bo_tile_layout(nvbo),
  677. offset);
  678. if (ret)
  679. return ret;
  680. } else if (dev_priv->card_type >= NV_10) {
  681. *new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size,
  682. nvbo->tile_mode);
  683. }
  684. return 0;
  685. }
  686. static void
  687. nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
  688. struct nouveau_tile_reg *new_tile,
  689. struct nouveau_tile_reg **old_tile)
  690. {
  691. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  692. struct drm_device *dev = dev_priv->dev;
  693. if (dev_priv->card_type >= NV_10 &&
  694. dev_priv->card_type < NV_50) {
  695. if (*old_tile)
  696. nv10_mem_expire_tiling(dev, *old_tile, bo->sync_obj);
  697. *old_tile = new_tile;
  698. }
  699. }
  700. static int
  701. nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
  702. bool no_wait_reserve, bool no_wait_gpu,
  703. struct ttm_mem_reg *new_mem)
  704. {
  705. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  706. struct nouveau_bo *nvbo = nouveau_bo(bo);
  707. struct ttm_mem_reg *old_mem = &bo->mem;
  708. struct nouveau_tile_reg *new_tile = NULL;
  709. int ret = 0;
  710. ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
  711. if (ret)
  712. return ret;
  713. /* Fake bo copy. */
  714. if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
  715. BUG_ON(bo->mem.mm_node != NULL);
  716. bo->mem = *new_mem;
  717. new_mem->mm_node = NULL;
  718. goto out;
  719. }
  720. /* Software copy if the card isn't up and running yet. */
  721. if (!dev_priv->channel) {
  722. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  723. goto out;
  724. }
  725. /* Hardware assisted copy. */
  726. if (new_mem->mem_type == TTM_PL_SYSTEM)
  727. ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  728. else if (old_mem->mem_type == TTM_PL_SYSTEM)
  729. ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  730. else
  731. ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  732. if (!ret)
  733. goto out;
  734. /* Fallback to software copy. */
  735. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  736. out:
  737. if (ret)
  738. nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
  739. else
  740. nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
  741. return ret;
  742. }
  743. static int
  744. nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  745. {
  746. return 0;
  747. }
  748. static int
  749. nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  750. {
  751. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  752. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  753. struct drm_device *dev = dev_priv->dev;
  754. mem->bus.addr = NULL;
  755. mem->bus.offset = 0;
  756. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  757. mem->bus.base = 0;
  758. mem->bus.is_iomem = false;
  759. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  760. return -EINVAL;
  761. switch (mem->mem_type) {
  762. case TTM_PL_SYSTEM:
  763. /* System memory */
  764. return 0;
  765. case TTM_PL_TT:
  766. #if __OS_HAS_AGP
  767. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  768. mem->bus.offset = mem->start << PAGE_SHIFT;
  769. mem->bus.base = dev_priv->gart_info.aper_base;
  770. mem->bus.is_iomem = true;
  771. }
  772. #endif
  773. break;
  774. case TTM_PL_VRAM:
  775. mem->bus.offset = mem->start << PAGE_SHIFT;
  776. mem->bus.base = pci_resource_start(dev->pdev, 1);
  777. mem->bus.is_iomem = true;
  778. break;
  779. default:
  780. return -EINVAL;
  781. }
  782. return 0;
  783. }
  784. static void
  785. nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  786. {
  787. }
  788. static int
  789. nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  790. {
  791. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  792. struct nouveau_bo *nvbo = nouveau_bo(bo);
  793. /* as long as the bo isn't in vram, and isn't tiled, we've got
  794. * nothing to do here.
  795. */
  796. if (bo->mem.mem_type != TTM_PL_VRAM) {
  797. if (dev_priv->card_type < NV_50 ||
  798. !nouveau_bo_tile_layout(nvbo))
  799. return 0;
  800. }
  801. /* make sure bo is in mappable vram */
  802. if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages)
  803. return 0;
  804. nvbo->placement.fpfn = 0;
  805. nvbo->placement.lpfn = dev_priv->fb_mappable_pages;
  806. nouveau_bo_placement_set(nvbo, TTM_PL_VRAM, 0);
  807. return ttm_bo_validate(bo, &nvbo->placement, false, true, false);
  808. }
  809. struct ttm_bo_driver nouveau_bo_driver = {
  810. .create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,
  811. .invalidate_caches = nouveau_bo_invalidate_caches,
  812. .init_mem_type = nouveau_bo_init_mem_type,
  813. .evict_flags = nouveau_bo_evict_flags,
  814. .move = nouveau_bo_move,
  815. .verify_access = nouveau_bo_verify_access,
  816. .sync_obj_signaled = nouveau_fence_signalled,
  817. .sync_obj_wait = nouveau_fence_wait,
  818. .sync_obj_flush = nouveau_fence_flush,
  819. .sync_obj_unref = nouveau_fence_unref,
  820. .sync_obj_ref = nouveau_fence_ref,
  821. .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
  822. .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
  823. .io_mem_free = &nouveau_ttm_io_mem_free,
  824. };