nouveau_bo.c 24 KB

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