nouveau_bo.c 25 KB

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