nouveau_bo.c 21 KB

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