nouveau_bo.c 23 KB

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