nouveau_bo.c 27 KB

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