nouveau_bo.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
  114. nouveau_bo_placement_set(nvbo, flags, 0);
  115. nvbo->channel = chan;
  116. ret = ttm_bo_init(&dev_priv->ttm.bdev, &nvbo->bo, size,
  117. ttm_bo_type_device, &nvbo->placement, align, 0,
  118. false, NULL, size, nouveau_bo_del_ttm);
  119. if (ret) {
  120. /* ttm will call nouveau_bo_del_ttm if it fails.. */
  121. return ret;
  122. }
  123. nvbo->channel = NULL;
  124. if (nvbo->vma.node) {
  125. if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
  126. nvbo->bo.offset = nvbo->vma.offset;
  127. }
  128. *pnvbo = nvbo;
  129. return 0;
  130. }
  131. static void
  132. set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
  133. {
  134. *n = 0;
  135. if (type & TTM_PL_FLAG_VRAM)
  136. pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
  137. if (type & TTM_PL_FLAG_TT)
  138. pl[(*n)++] = TTM_PL_FLAG_TT | flags;
  139. if (type & TTM_PL_FLAG_SYSTEM)
  140. pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
  141. }
  142. static void
  143. set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
  144. {
  145. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  146. int vram_pages = dev_priv->vram_size >> PAGE_SHIFT;
  147. if (dev_priv->card_type == NV_10 &&
  148. nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM) &&
  149. nvbo->bo.mem.num_pages < vram_pages / 2) {
  150. /*
  151. * Make sure that the color and depth buffers are handled
  152. * by independent memory controller units. Up to a 9x
  153. * speed up when alpha-blending and depth-test are enabled
  154. * at the same time.
  155. */
  156. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
  157. nvbo->placement.fpfn = vram_pages / 2;
  158. nvbo->placement.lpfn = ~0;
  159. } else {
  160. nvbo->placement.fpfn = 0;
  161. nvbo->placement.lpfn = vram_pages / 2;
  162. }
  163. }
  164. }
  165. void
  166. nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
  167. {
  168. struct ttm_placement *pl = &nvbo->placement;
  169. uint32_t flags = TTM_PL_MASK_CACHING |
  170. (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
  171. pl->placement = nvbo->placements;
  172. set_placement_list(nvbo->placements, &pl->num_placement,
  173. type, flags);
  174. pl->busy_placement = nvbo->busy_placements;
  175. set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
  176. type | busy, flags);
  177. set_placement_range(nvbo, type);
  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 = nouveau_bo_validate(nvbo, 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 = nouveau_bo_validate(nvbo, 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. int
  262. nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
  263. bool no_wait_reserve, bool no_wait_gpu)
  264. {
  265. int ret;
  266. ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, interruptible,
  267. no_wait_reserve, no_wait_gpu);
  268. if (ret)
  269. return ret;
  270. if (nvbo->vma.node) {
  271. if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
  272. nvbo->bo.offset = nvbo->vma.offset;
  273. }
  274. return 0;
  275. }
  276. u16
  277. nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
  278. {
  279. bool is_iomem;
  280. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  281. mem = &mem[index];
  282. if (is_iomem)
  283. return ioread16_native((void __force __iomem *)mem);
  284. else
  285. return *mem;
  286. }
  287. void
  288. nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
  289. {
  290. bool is_iomem;
  291. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  292. mem = &mem[index];
  293. if (is_iomem)
  294. iowrite16_native(val, (void __force __iomem *)mem);
  295. else
  296. *mem = val;
  297. }
  298. u32
  299. nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
  300. {
  301. bool is_iomem;
  302. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  303. mem = &mem[index];
  304. if (is_iomem)
  305. return ioread32_native((void __force __iomem *)mem);
  306. else
  307. return *mem;
  308. }
  309. void
  310. nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
  311. {
  312. bool is_iomem;
  313. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  314. mem = &mem[index];
  315. if (is_iomem)
  316. iowrite32_native(val, (void __force __iomem *)mem);
  317. else
  318. *mem = val;
  319. }
  320. static struct ttm_backend *
  321. nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
  322. {
  323. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  324. struct drm_device *dev = dev_priv->dev;
  325. switch (dev_priv->gart_info.type) {
  326. #if __OS_HAS_AGP
  327. case NOUVEAU_GART_AGP:
  328. return ttm_agp_backend_init(bdev, dev->agp->bridge);
  329. #endif
  330. case NOUVEAU_GART_SGDMA:
  331. return nouveau_sgdma_init_ttm(dev);
  332. default:
  333. NV_ERROR(dev, "Unknown GART type %d\n",
  334. dev_priv->gart_info.type);
  335. break;
  336. }
  337. return NULL;
  338. }
  339. static int
  340. nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  341. {
  342. /* We'll do this from user space. */
  343. return 0;
  344. }
  345. static int
  346. nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  347. struct ttm_mem_type_manager *man)
  348. {
  349. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  350. struct drm_device *dev = dev_priv->dev;
  351. switch (type) {
  352. case TTM_PL_SYSTEM:
  353. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  354. man->available_caching = TTM_PL_MASK_CACHING;
  355. man->default_caching = TTM_PL_FLAG_CACHED;
  356. break;
  357. case TTM_PL_VRAM:
  358. if (dev_priv->card_type >= NV_50) {
  359. man->func = &nouveau_vram_manager;
  360. man->io_reserve_fastpath = false;
  361. man->use_io_reserve_lru = true;
  362. } else {
  363. man->func = &ttm_bo_manager_func;
  364. }
  365. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  366. TTM_MEMTYPE_FLAG_MAPPABLE;
  367. man->available_caching = TTM_PL_FLAG_UNCACHED |
  368. TTM_PL_FLAG_WC;
  369. man->default_caching = TTM_PL_FLAG_WC;
  370. break;
  371. case TTM_PL_TT:
  372. man->func = &ttm_bo_manager_func;
  373. switch (dev_priv->gart_info.type) {
  374. case NOUVEAU_GART_AGP:
  375. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  376. man->available_caching = TTM_PL_FLAG_UNCACHED |
  377. TTM_PL_FLAG_WC;
  378. man->default_caching = TTM_PL_FLAG_WC;
  379. break;
  380. case NOUVEAU_GART_SGDMA:
  381. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
  382. TTM_MEMTYPE_FLAG_CMA;
  383. man->available_caching = TTM_PL_MASK_CACHING;
  384. man->default_caching = TTM_PL_FLAG_CACHED;
  385. man->gpu_offset = dev_priv->gart_info.aper_base;
  386. break;
  387. default:
  388. NV_ERROR(dev, "Unknown GART type: %d\n",
  389. dev_priv->gart_info.type);
  390. return -EINVAL;
  391. }
  392. break;
  393. default:
  394. NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
  395. return -EINVAL;
  396. }
  397. return 0;
  398. }
  399. static void
  400. nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  401. {
  402. struct nouveau_bo *nvbo = nouveau_bo(bo);
  403. switch (bo->mem.mem_type) {
  404. case TTM_PL_VRAM:
  405. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
  406. TTM_PL_FLAG_SYSTEM);
  407. break;
  408. default:
  409. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
  410. break;
  411. }
  412. *pl = nvbo->placement;
  413. }
  414. /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
  415. * TTM_PL_{VRAM,TT} directly.
  416. */
  417. static int
  418. nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
  419. struct nouveau_bo *nvbo, bool evict,
  420. bool no_wait_reserve, bool no_wait_gpu,
  421. struct ttm_mem_reg *new_mem)
  422. {
  423. struct nouveau_fence *fence = NULL;
  424. int ret;
  425. ret = nouveau_fence_new(chan, &fence, true);
  426. if (ret)
  427. return ret;
  428. ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict,
  429. no_wait_reserve, no_wait_gpu, new_mem);
  430. nouveau_fence_unref(&fence);
  431. return ret;
  432. }
  433. static inline uint32_t
  434. nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
  435. struct nouveau_channel *chan, struct ttm_mem_reg *mem)
  436. {
  437. struct nouveau_bo *nvbo = nouveau_bo(bo);
  438. if (nvbo->no_vm) {
  439. if (mem->mem_type == TTM_PL_TT)
  440. return NvDmaGART;
  441. return NvDmaVRAM;
  442. }
  443. if (mem->mem_type == TTM_PL_TT)
  444. return chan->gart_handle;
  445. return chan->vram_handle;
  446. }
  447. static int
  448. nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  449. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  450. {
  451. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  452. struct nouveau_bo *nvbo = nouveau_bo(bo);
  453. u64 src_offset = old_mem->start << PAGE_SHIFT;
  454. u64 dst_offset = new_mem->start << PAGE_SHIFT;
  455. u32 page_count = new_mem->num_pages;
  456. int ret;
  457. if (!nvbo->no_vm) {
  458. if (old_mem->mem_type == TTM_PL_VRAM)
  459. src_offset = nvbo->vma.offset;
  460. else
  461. src_offset += dev_priv->gart_info.aper_base;
  462. if (new_mem->mem_type == TTM_PL_VRAM)
  463. dst_offset = nvbo->vma.offset;
  464. else
  465. dst_offset += dev_priv->gart_info.aper_base;
  466. }
  467. page_count = new_mem->num_pages;
  468. while (page_count) {
  469. int line_count = (page_count > 2047) ? 2047 : page_count;
  470. ret = RING_SPACE(chan, 12);
  471. if (ret)
  472. return ret;
  473. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0238, 2);
  474. OUT_RING (chan, upper_32_bits(dst_offset));
  475. OUT_RING (chan, lower_32_bits(dst_offset));
  476. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x030c, 6);
  477. OUT_RING (chan, upper_32_bits(src_offset));
  478. OUT_RING (chan, lower_32_bits(src_offset));
  479. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  480. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  481. OUT_RING (chan, PAGE_SIZE); /* line_length */
  482. OUT_RING (chan, line_count);
  483. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0300, 1);
  484. OUT_RING (chan, 0x00100110);
  485. page_count -= line_count;
  486. src_offset += (PAGE_SIZE * line_count);
  487. dst_offset += (PAGE_SIZE * line_count);
  488. }
  489. return 0;
  490. }
  491. static int
  492. nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  493. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  494. {
  495. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  496. struct nouveau_bo *nvbo = nouveau_bo(bo);
  497. u64 length = (new_mem->num_pages << PAGE_SHIFT);
  498. u64 src_offset, dst_offset;
  499. int ret;
  500. src_offset = old_mem->start << PAGE_SHIFT;
  501. dst_offset = new_mem->start << PAGE_SHIFT;
  502. if (!nvbo->no_vm) {
  503. if (old_mem->mem_type == TTM_PL_VRAM)
  504. src_offset = nvbo->vma.offset;
  505. else
  506. src_offset += dev_priv->gart_info.aper_base;
  507. if (new_mem->mem_type == TTM_PL_VRAM)
  508. dst_offset = nvbo->vma.offset;
  509. else
  510. dst_offset += dev_priv->gart_info.aper_base;
  511. }
  512. ret = RING_SPACE(chan, 3);
  513. if (ret)
  514. return ret;
  515. BEGIN_RING(chan, NvSubM2MF, 0x0184, 2);
  516. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  517. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  518. while (length) {
  519. u32 amount, stride, height;
  520. amount = min(length, (u64)(4 * 1024 * 1024));
  521. stride = 16 * 4;
  522. height = amount / stride;
  523. if (new_mem->mem_type == TTM_PL_VRAM &&
  524. nouveau_bo_tile_layout(nvbo)) {
  525. ret = RING_SPACE(chan, 8);
  526. if (ret)
  527. return ret;
  528. BEGIN_RING(chan, NvSubM2MF, 0x0200, 7);
  529. OUT_RING (chan, 0);
  530. OUT_RING (chan, 0);
  531. OUT_RING (chan, stride);
  532. OUT_RING (chan, height);
  533. OUT_RING (chan, 1);
  534. OUT_RING (chan, 0);
  535. OUT_RING (chan, 0);
  536. } else {
  537. ret = RING_SPACE(chan, 2);
  538. if (ret)
  539. return ret;
  540. BEGIN_RING(chan, NvSubM2MF, 0x0200, 1);
  541. OUT_RING (chan, 1);
  542. }
  543. if (old_mem->mem_type == TTM_PL_VRAM &&
  544. nouveau_bo_tile_layout(nvbo)) {
  545. ret = RING_SPACE(chan, 8);
  546. if (ret)
  547. return ret;
  548. BEGIN_RING(chan, NvSubM2MF, 0x021c, 7);
  549. OUT_RING (chan, 0);
  550. OUT_RING (chan, 0);
  551. OUT_RING (chan, stride);
  552. OUT_RING (chan, height);
  553. OUT_RING (chan, 1);
  554. OUT_RING (chan, 0);
  555. OUT_RING (chan, 0);
  556. } else {
  557. ret = RING_SPACE(chan, 2);
  558. if (ret)
  559. return ret;
  560. BEGIN_RING(chan, NvSubM2MF, 0x021c, 1);
  561. OUT_RING (chan, 1);
  562. }
  563. ret = RING_SPACE(chan, 14);
  564. if (ret)
  565. return ret;
  566. BEGIN_RING(chan, NvSubM2MF, 0x0238, 2);
  567. OUT_RING (chan, upper_32_bits(src_offset));
  568. OUT_RING (chan, upper_32_bits(dst_offset));
  569. BEGIN_RING(chan, NvSubM2MF, 0x030c, 8);
  570. OUT_RING (chan, lower_32_bits(src_offset));
  571. OUT_RING (chan, lower_32_bits(dst_offset));
  572. OUT_RING (chan, stride);
  573. OUT_RING (chan, stride);
  574. OUT_RING (chan, stride);
  575. OUT_RING (chan, height);
  576. OUT_RING (chan, 0x00000101);
  577. OUT_RING (chan, 0x00000000);
  578. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  579. OUT_RING (chan, 0);
  580. length -= amount;
  581. src_offset += amount;
  582. dst_offset += amount;
  583. }
  584. return 0;
  585. }
  586. static int
  587. nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  588. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  589. {
  590. u32 src_offset = old_mem->start << PAGE_SHIFT;
  591. u32 dst_offset = new_mem->start << PAGE_SHIFT;
  592. u32 page_count = new_mem->num_pages;
  593. int ret;
  594. ret = RING_SPACE(chan, 3);
  595. if (ret)
  596. return ret;
  597. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
  598. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  599. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  600. page_count = new_mem->num_pages;
  601. while (page_count) {
  602. int line_count = (page_count > 2047) ? 2047 : page_count;
  603. ret = RING_SPACE(chan, 11);
  604. if (ret)
  605. return ret;
  606. BEGIN_RING(chan, NvSubM2MF,
  607. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  608. OUT_RING (chan, src_offset);
  609. OUT_RING (chan, dst_offset);
  610. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  611. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  612. OUT_RING (chan, PAGE_SIZE); /* line_length */
  613. OUT_RING (chan, line_count);
  614. OUT_RING (chan, 0x00000101);
  615. OUT_RING (chan, 0x00000000);
  616. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  617. OUT_RING (chan, 0);
  618. page_count -= line_count;
  619. src_offset += (PAGE_SIZE * line_count);
  620. dst_offset += (PAGE_SIZE * line_count);
  621. }
  622. return 0;
  623. }
  624. static int
  625. nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
  626. bool no_wait_reserve, bool no_wait_gpu,
  627. struct ttm_mem_reg *new_mem)
  628. {
  629. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  630. struct nouveau_bo *nvbo = nouveau_bo(bo);
  631. struct nouveau_channel *chan;
  632. int ret;
  633. chan = nvbo->channel;
  634. if (!chan || nvbo->no_vm) {
  635. chan = dev_priv->channel;
  636. mutex_lock_nested(&chan->mutex, NOUVEAU_KCHANNEL_MUTEX);
  637. }
  638. if (dev_priv->card_type < NV_50)
  639. ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  640. else
  641. if (dev_priv->card_type < NV_C0)
  642. ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  643. else
  644. ret = nvc0_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  645. if (ret == 0) {
  646. ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
  647. no_wait_reserve,
  648. no_wait_gpu, new_mem);
  649. }
  650. if (chan == dev_priv->channel)
  651. mutex_unlock(&chan->mutex);
  652. return ret;
  653. }
  654. static int
  655. nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
  656. bool no_wait_reserve, bool no_wait_gpu,
  657. struct ttm_mem_reg *new_mem)
  658. {
  659. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  660. struct ttm_placement placement;
  661. struct ttm_mem_reg tmp_mem;
  662. int ret;
  663. placement.fpfn = placement.lpfn = 0;
  664. placement.num_placement = placement.num_busy_placement = 1;
  665. placement.placement = placement.busy_placement = &placement_memtype;
  666. tmp_mem = *new_mem;
  667. tmp_mem.mm_node = NULL;
  668. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  669. if (ret)
  670. return ret;
  671. ret = ttm_tt_bind(bo->ttm, &tmp_mem);
  672. if (ret)
  673. goto out;
  674. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
  675. if (ret)
  676. goto out;
  677. ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, new_mem);
  678. out:
  679. ttm_bo_mem_put(bo, &tmp_mem);
  680. return ret;
  681. }
  682. static int
  683. nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
  684. bool no_wait_reserve, bool no_wait_gpu,
  685. struct ttm_mem_reg *new_mem)
  686. {
  687. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  688. struct ttm_placement placement;
  689. struct ttm_mem_reg tmp_mem;
  690. int ret;
  691. placement.fpfn = placement.lpfn = 0;
  692. placement.num_placement = placement.num_busy_placement = 1;
  693. placement.placement = placement.busy_placement = &placement_memtype;
  694. tmp_mem = *new_mem;
  695. tmp_mem.mm_node = NULL;
  696. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  697. if (ret)
  698. return ret;
  699. ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, &tmp_mem);
  700. if (ret)
  701. goto out;
  702. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, new_mem);
  703. if (ret)
  704. goto out;
  705. out:
  706. ttm_bo_mem_put(bo, &tmp_mem);
  707. return ret;
  708. }
  709. static int
  710. nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
  711. struct nouveau_tile_reg **new_tile)
  712. {
  713. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  714. struct drm_device *dev = dev_priv->dev;
  715. struct nouveau_bo *nvbo = nouveau_bo(bo);
  716. uint64_t offset;
  717. if (nvbo->no_vm || new_mem->mem_type != TTM_PL_VRAM) {
  718. /* Nothing to do. */
  719. *new_tile = NULL;
  720. return 0;
  721. }
  722. offset = new_mem->start << PAGE_SHIFT;
  723. if (dev_priv->chan_vm) {
  724. nouveau_vm_map(&nvbo->vma, new_mem->mm_node);
  725. } else if (dev_priv->card_type >= NV_10) {
  726. *new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size,
  727. nvbo->tile_mode,
  728. nvbo->tile_flags);
  729. }
  730. return 0;
  731. }
  732. static void
  733. nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
  734. struct nouveau_tile_reg *new_tile,
  735. struct nouveau_tile_reg **old_tile)
  736. {
  737. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  738. struct drm_device *dev = dev_priv->dev;
  739. if (dev_priv->card_type >= NV_10 &&
  740. dev_priv->card_type < NV_50) {
  741. nv10_mem_put_tile_region(dev, *old_tile, bo->sync_obj);
  742. *old_tile = new_tile;
  743. }
  744. }
  745. static int
  746. nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
  747. bool no_wait_reserve, bool no_wait_gpu,
  748. struct ttm_mem_reg *new_mem)
  749. {
  750. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  751. struct nouveau_bo *nvbo = nouveau_bo(bo);
  752. struct ttm_mem_reg *old_mem = &bo->mem;
  753. struct nouveau_tile_reg *new_tile = NULL;
  754. int ret = 0;
  755. ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
  756. if (ret)
  757. return ret;
  758. /* Fake bo copy. */
  759. if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
  760. BUG_ON(bo->mem.mm_node != NULL);
  761. bo->mem = *new_mem;
  762. new_mem->mm_node = NULL;
  763. goto out;
  764. }
  765. /* Software copy if the card isn't up and running yet. */
  766. if (!dev_priv->channel) {
  767. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  768. goto out;
  769. }
  770. /* Hardware assisted copy. */
  771. if (new_mem->mem_type == TTM_PL_SYSTEM)
  772. ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  773. else if (old_mem->mem_type == TTM_PL_SYSTEM)
  774. ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  775. else
  776. ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  777. if (!ret)
  778. goto out;
  779. /* Fallback to software copy. */
  780. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  781. out:
  782. if (ret)
  783. nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
  784. else
  785. nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
  786. return ret;
  787. }
  788. static int
  789. nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  790. {
  791. return 0;
  792. }
  793. static int
  794. nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  795. {
  796. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  797. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  798. struct drm_device *dev = dev_priv->dev;
  799. int ret;
  800. mem->bus.addr = NULL;
  801. mem->bus.offset = 0;
  802. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  803. mem->bus.base = 0;
  804. mem->bus.is_iomem = false;
  805. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  806. return -EINVAL;
  807. switch (mem->mem_type) {
  808. case TTM_PL_SYSTEM:
  809. /* System memory */
  810. return 0;
  811. case TTM_PL_TT:
  812. #if __OS_HAS_AGP
  813. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  814. mem->bus.offset = mem->start << PAGE_SHIFT;
  815. mem->bus.base = dev_priv->gart_info.aper_base;
  816. mem->bus.is_iomem = true;
  817. }
  818. #endif
  819. break;
  820. case TTM_PL_VRAM:
  821. {
  822. struct nouveau_vram *vram = mem->mm_node;
  823. u8 page_shift;
  824. if (!dev_priv->bar1_vm) {
  825. mem->bus.offset = mem->start << PAGE_SHIFT;
  826. mem->bus.base = pci_resource_start(dev->pdev, 1);
  827. mem->bus.is_iomem = true;
  828. break;
  829. }
  830. if (dev_priv->card_type == NV_C0)
  831. page_shift = vram->page_shift;
  832. else
  833. page_shift = 12;
  834. ret = nouveau_vm_get(dev_priv->bar1_vm, mem->bus.size,
  835. page_shift, NV_MEM_ACCESS_RW,
  836. &vram->bar_vma);
  837. if (ret)
  838. return ret;
  839. nouveau_vm_map(&vram->bar_vma, vram);
  840. if (ret) {
  841. nouveau_vm_put(&vram->bar_vma);
  842. return ret;
  843. }
  844. mem->bus.offset = vram->bar_vma.offset;
  845. if (dev_priv->card_type == NV_50) /*XXX*/
  846. mem->bus.offset -= 0x0020000000ULL;
  847. mem->bus.base = pci_resource_start(dev->pdev, 1);
  848. mem->bus.is_iomem = true;
  849. }
  850. break;
  851. default:
  852. return -EINVAL;
  853. }
  854. return 0;
  855. }
  856. static void
  857. nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  858. {
  859. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  860. struct nouveau_vram *vram = mem->mm_node;
  861. if (!dev_priv->bar1_vm || mem->mem_type != TTM_PL_VRAM)
  862. return;
  863. if (!vram->bar_vma.node)
  864. return;
  865. nouveau_vm_unmap(&vram->bar_vma);
  866. nouveau_vm_put(&vram->bar_vma);
  867. }
  868. static int
  869. nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  870. {
  871. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  872. struct nouveau_bo *nvbo = nouveau_bo(bo);
  873. /* as long as the bo isn't in vram, and isn't tiled, we've got
  874. * nothing to do here.
  875. */
  876. if (bo->mem.mem_type != TTM_PL_VRAM) {
  877. if (dev_priv->card_type < NV_50 ||
  878. !nouveau_bo_tile_layout(nvbo))
  879. return 0;
  880. }
  881. /* make sure bo is in mappable vram */
  882. if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages)
  883. return 0;
  884. nvbo->placement.fpfn = 0;
  885. nvbo->placement.lpfn = dev_priv->fb_mappable_pages;
  886. nouveau_bo_placement_set(nvbo, TTM_PL_VRAM, 0);
  887. return nouveau_bo_validate(nvbo, false, true, false);
  888. }
  889. void
  890. nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
  891. {
  892. struct nouveau_fence *old_fence;
  893. if (likely(fence))
  894. nouveau_fence_ref(fence);
  895. spin_lock(&nvbo->bo.bdev->fence_lock);
  896. old_fence = nvbo->bo.sync_obj;
  897. nvbo->bo.sync_obj = fence;
  898. spin_unlock(&nvbo->bo.bdev->fence_lock);
  899. nouveau_fence_unref(&old_fence);
  900. }
  901. struct ttm_bo_driver nouveau_bo_driver = {
  902. .create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,
  903. .invalidate_caches = nouveau_bo_invalidate_caches,
  904. .init_mem_type = nouveau_bo_init_mem_type,
  905. .evict_flags = nouveau_bo_evict_flags,
  906. .move = nouveau_bo_move,
  907. .verify_access = nouveau_bo_verify_access,
  908. .sync_obj_signaled = __nouveau_fence_signalled,
  909. .sync_obj_wait = __nouveau_fence_wait,
  910. .sync_obj_flush = __nouveau_fence_flush,
  911. .sync_obj_unref = __nouveau_fence_unref,
  912. .sync_obj_ref = __nouveau_fence_ref,
  913. .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
  914. .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
  915. .io_mem_free = &nouveau_ttm_io_mem_free,
  916. };