nouveau_mem.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. /*
  2. * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
  3. * Copyright 2005 Stephane Marchesin
  4. *
  5. * The Weather Channel (TM) funded Tungsten Graphics to develop the
  6. * initial release of the Radeon 8500 driver under the XFree86 license.
  7. * This notice must be preserved.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * THE AUTHORS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  26. * DEALINGS IN THE SOFTWARE.
  27. *
  28. * Authors:
  29. * Ben Skeggs <bskeggs@redhat.com>
  30. * Roy Spliet <r.spliet@student.tudelft.nl>
  31. */
  32. #include "drmP.h"
  33. #include "drm.h"
  34. #include "drm_sarea.h"
  35. #include "nouveau_drv.h"
  36. #include "nouveau_pm.h"
  37. #include "nouveau_mm.h"
  38. #include "nouveau_vm.h"
  39. /*
  40. * NV10-NV40 tiling helpers
  41. */
  42. static void
  43. nv10_mem_update_tile_region(struct drm_device *dev,
  44. struct nouveau_tile_reg *tile, uint32_t addr,
  45. uint32_t size, uint32_t pitch, uint32_t flags)
  46. {
  47. struct drm_nouveau_private *dev_priv = dev->dev_private;
  48. struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
  49. struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
  50. int i = tile - dev_priv->tile.reg, j;
  51. unsigned long save;
  52. nouveau_fence_unref(&tile->fence);
  53. if (tile->pitch)
  54. pfb->free_tile_region(dev, i);
  55. if (pitch)
  56. pfb->init_tile_region(dev, i, addr, size, pitch, flags);
  57. spin_lock_irqsave(&dev_priv->context_switch_lock, save);
  58. pfifo->reassign(dev, false);
  59. pfifo->cache_pull(dev, false);
  60. nouveau_wait_for_idle(dev);
  61. pfb->set_tile_region(dev, i);
  62. for (j = 0; j < NVOBJ_ENGINE_NR; j++) {
  63. if (dev_priv->eng[j] && dev_priv->eng[j]->set_tile_region)
  64. dev_priv->eng[j]->set_tile_region(dev, i);
  65. }
  66. pfifo->cache_pull(dev, true);
  67. pfifo->reassign(dev, true);
  68. spin_unlock_irqrestore(&dev_priv->context_switch_lock, save);
  69. }
  70. static struct nouveau_tile_reg *
  71. nv10_mem_get_tile_region(struct drm_device *dev, int i)
  72. {
  73. struct drm_nouveau_private *dev_priv = dev->dev_private;
  74. struct nouveau_tile_reg *tile = &dev_priv->tile.reg[i];
  75. spin_lock(&dev_priv->tile.lock);
  76. if (!tile->used &&
  77. (!tile->fence || nouveau_fence_signalled(tile->fence)))
  78. tile->used = true;
  79. else
  80. tile = NULL;
  81. spin_unlock(&dev_priv->tile.lock);
  82. return tile;
  83. }
  84. void
  85. nv10_mem_put_tile_region(struct drm_device *dev, struct nouveau_tile_reg *tile,
  86. struct nouveau_fence *fence)
  87. {
  88. struct drm_nouveau_private *dev_priv = dev->dev_private;
  89. if (tile) {
  90. spin_lock(&dev_priv->tile.lock);
  91. if (fence) {
  92. /* Mark it as pending. */
  93. tile->fence = fence;
  94. nouveau_fence_ref(fence);
  95. }
  96. tile->used = false;
  97. spin_unlock(&dev_priv->tile.lock);
  98. }
  99. }
  100. struct nouveau_tile_reg *
  101. nv10_mem_set_tiling(struct drm_device *dev, uint32_t addr, uint32_t size,
  102. uint32_t pitch, uint32_t flags)
  103. {
  104. struct drm_nouveau_private *dev_priv = dev->dev_private;
  105. struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
  106. struct nouveau_tile_reg *tile, *found = NULL;
  107. int i;
  108. for (i = 0; i < pfb->num_tiles; i++) {
  109. tile = nv10_mem_get_tile_region(dev, i);
  110. if (pitch && !found) {
  111. found = tile;
  112. continue;
  113. } else if (tile && tile->pitch) {
  114. /* Kill an unused tile region. */
  115. nv10_mem_update_tile_region(dev, tile, 0, 0, 0, 0);
  116. }
  117. nv10_mem_put_tile_region(dev, tile, NULL);
  118. }
  119. if (found)
  120. nv10_mem_update_tile_region(dev, found, addr, size,
  121. pitch, flags);
  122. return found;
  123. }
  124. /*
  125. * Cleanup everything
  126. */
  127. void
  128. nouveau_mem_vram_fini(struct drm_device *dev)
  129. {
  130. struct drm_nouveau_private *dev_priv = dev->dev_private;
  131. ttm_bo_device_release(&dev_priv->ttm.bdev);
  132. nouveau_ttm_global_release(dev_priv);
  133. if (dev_priv->fb_mtrr >= 0) {
  134. drm_mtrr_del(dev_priv->fb_mtrr,
  135. pci_resource_start(dev->pdev, 1),
  136. pci_resource_len(dev->pdev, 1), DRM_MTRR_WC);
  137. dev_priv->fb_mtrr = -1;
  138. }
  139. }
  140. void
  141. nouveau_mem_gart_fini(struct drm_device *dev)
  142. {
  143. nouveau_sgdma_takedown(dev);
  144. if (drm_core_has_AGP(dev) && dev->agp) {
  145. struct drm_agp_mem *entry, *tempe;
  146. /* Remove AGP resources, but leave dev->agp
  147. intact until drv_cleanup is called. */
  148. list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
  149. if (entry->bound)
  150. drm_unbind_agp(entry->memory);
  151. drm_free_agp(entry->memory, entry->pages);
  152. kfree(entry);
  153. }
  154. INIT_LIST_HEAD(&dev->agp->memory);
  155. if (dev->agp->acquired)
  156. drm_agp_release(dev);
  157. dev->agp->acquired = 0;
  158. dev->agp->enabled = 0;
  159. }
  160. }
  161. bool
  162. nouveau_mem_flags_valid(struct drm_device *dev, u32 tile_flags)
  163. {
  164. if (!(tile_flags & NOUVEAU_GEM_TILE_LAYOUT_MASK))
  165. return true;
  166. return false;
  167. }
  168. #if __OS_HAS_AGP
  169. static unsigned long
  170. get_agp_mode(struct drm_device *dev, unsigned long mode)
  171. {
  172. struct drm_nouveau_private *dev_priv = dev->dev_private;
  173. /*
  174. * FW seems to be broken on nv18, it makes the card lock up
  175. * randomly.
  176. */
  177. if (dev_priv->chipset == 0x18)
  178. mode &= ~PCI_AGP_COMMAND_FW;
  179. /*
  180. * AGP mode set in the command line.
  181. */
  182. if (nouveau_agpmode > 0) {
  183. bool agpv3 = mode & 0x8;
  184. int rate = agpv3 ? nouveau_agpmode / 4 : nouveau_agpmode;
  185. mode = (mode & ~0x7) | (rate & 0x7);
  186. }
  187. return mode;
  188. }
  189. #endif
  190. int
  191. nouveau_mem_reset_agp(struct drm_device *dev)
  192. {
  193. #if __OS_HAS_AGP
  194. uint32_t saved_pci_nv_1, pmc_enable;
  195. int ret;
  196. /* First of all, disable fast writes, otherwise if it's
  197. * already enabled in the AGP bridge and we disable the card's
  198. * AGP controller we might be locking ourselves out of it. */
  199. if ((nv_rd32(dev, NV04_PBUS_PCI_NV_19) |
  200. dev->agp->mode) & PCI_AGP_COMMAND_FW) {
  201. struct drm_agp_info info;
  202. struct drm_agp_mode mode;
  203. ret = drm_agp_info(dev, &info);
  204. if (ret)
  205. return ret;
  206. mode.mode = get_agp_mode(dev, info.mode) & ~PCI_AGP_COMMAND_FW;
  207. ret = drm_agp_enable(dev, mode);
  208. if (ret)
  209. return ret;
  210. }
  211. saved_pci_nv_1 = nv_rd32(dev, NV04_PBUS_PCI_NV_1);
  212. /* clear busmaster bit */
  213. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1 & ~0x4);
  214. /* disable AGP */
  215. nv_wr32(dev, NV04_PBUS_PCI_NV_19, 0);
  216. /* power cycle pgraph, if enabled */
  217. pmc_enable = nv_rd32(dev, NV03_PMC_ENABLE);
  218. if (pmc_enable & NV_PMC_ENABLE_PGRAPH) {
  219. nv_wr32(dev, NV03_PMC_ENABLE,
  220. pmc_enable & ~NV_PMC_ENABLE_PGRAPH);
  221. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) |
  222. NV_PMC_ENABLE_PGRAPH);
  223. }
  224. /* and restore (gives effect of resetting AGP) */
  225. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1);
  226. #endif
  227. return 0;
  228. }
  229. int
  230. nouveau_mem_init_agp(struct drm_device *dev)
  231. {
  232. #if __OS_HAS_AGP
  233. struct drm_nouveau_private *dev_priv = dev->dev_private;
  234. struct drm_agp_info info;
  235. struct drm_agp_mode mode;
  236. int ret;
  237. if (!dev->agp->acquired) {
  238. ret = drm_agp_acquire(dev);
  239. if (ret) {
  240. NV_ERROR(dev, "Unable to acquire AGP: %d\n", ret);
  241. return ret;
  242. }
  243. }
  244. nouveau_mem_reset_agp(dev);
  245. ret = drm_agp_info(dev, &info);
  246. if (ret) {
  247. NV_ERROR(dev, "Unable to get AGP info: %d\n", ret);
  248. return ret;
  249. }
  250. /* see agp.h for the AGPSTAT_* modes available */
  251. mode.mode = get_agp_mode(dev, info.mode);
  252. ret = drm_agp_enable(dev, mode);
  253. if (ret) {
  254. NV_ERROR(dev, "Unable to enable AGP: %d\n", ret);
  255. return ret;
  256. }
  257. dev_priv->gart_info.type = NOUVEAU_GART_AGP;
  258. dev_priv->gart_info.aper_base = info.aperture_base;
  259. dev_priv->gart_info.aper_size = info.aperture_size;
  260. #endif
  261. return 0;
  262. }
  263. static const struct vram_types {
  264. int value;
  265. const char *name;
  266. } vram_type_map[] = {
  267. { NV_MEM_TYPE_STOLEN , "stolen system memory" },
  268. { NV_MEM_TYPE_SGRAM , "SGRAM" },
  269. { NV_MEM_TYPE_SDRAM , "SDRAM" },
  270. { NV_MEM_TYPE_DDR1 , "DDR1" },
  271. { NV_MEM_TYPE_DDR2 , "DDR2" },
  272. { NV_MEM_TYPE_DDR3 , "DDR3" },
  273. { NV_MEM_TYPE_GDDR2 , "GDDR2" },
  274. { NV_MEM_TYPE_GDDR3 , "GDDR3" },
  275. { NV_MEM_TYPE_GDDR4 , "GDDR4" },
  276. { NV_MEM_TYPE_GDDR5 , "GDDR5" },
  277. { NV_MEM_TYPE_UNKNOWN, "unknown type" }
  278. };
  279. int
  280. nouveau_mem_vram_init(struct drm_device *dev)
  281. {
  282. struct drm_nouveau_private *dev_priv = dev->dev_private;
  283. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  284. const struct vram_types *vram_type;
  285. int ret, dma_bits;
  286. dma_bits = 32;
  287. if (dev_priv->card_type >= NV_50) {
  288. if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(40)))
  289. dma_bits = 40;
  290. } else
  291. if (0 && pci_is_pcie(dev->pdev) &&
  292. dev_priv->chipset > 0x40 &&
  293. dev_priv->chipset != 0x45) {
  294. if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(39)))
  295. dma_bits = 39;
  296. }
  297. ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  298. if (ret)
  299. return ret;
  300. ret = pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  301. if (ret) {
  302. /* Reset to default value. */
  303. pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32));
  304. }
  305. ret = nouveau_ttm_global_init(dev_priv);
  306. if (ret)
  307. return ret;
  308. ret = ttm_bo_device_init(&dev_priv->ttm.bdev,
  309. dev_priv->ttm.bo_global_ref.ref.object,
  310. &nouveau_bo_driver, DRM_FILE_PAGE_OFFSET,
  311. dma_bits <= 32 ? true : false);
  312. if (ret) {
  313. NV_ERROR(dev, "Error initialising bo driver: %d\n", ret);
  314. return ret;
  315. }
  316. vram_type = vram_type_map;
  317. while (vram_type->value != NV_MEM_TYPE_UNKNOWN) {
  318. if (nouveau_vram_type) {
  319. if (!strcasecmp(nouveau_vram_type, vram_type->name))
  320. break;
  321. dev_priv->vram_type = vram_type->value;
  322. } else {
  323. if (vram_type->value == dev_priv->vram_type)
  324. break;
  325. }
  326. vram_type++;
  327. }
  328. NV_INFO(dev, "Detected %dMiB VRAM (%s)\n",
  329. (int)(dev_priv->vram_size >> 20), vram_type->name);
  330. if (dev_priv->vram_sys_base) {
  331. NV_INFO(dev, "Stolen system memory at: 0x%010llx\n",
  332. dev_priv->vram_sys_base);
  333. }
  334. dev_priv->fb_available_size = dev_priv->vram_size;
  335. dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
  336. if (dev_priv->fb_mappable_pages > pci_resource_len(dev->pdev, 1))
  337. dev_priv->fb_mappable_pages = pci_resource_len(dev->pdev, 1);
  338. dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
  339. dev_priv->fb_available_size -= dev_priv->ramin_rsvd_vram;
  340. dev_priv->fb_aper_free = dev_priv->fb_available_size;
  341. /* mappable vram */
  342. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  343. dev_priv->fb_available_size >> PAGE_SHIFT);
  344. if (ret) {
  345. NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
  346. return ret;
  347. }
  348. if (dev_priv->card_type < NV_50) {
  349. ret = nouveau_bo_new(dev, 256*1024, 0, TTM_PL_FLAG_VRAM,
  350. 0, 0, &dev_priv->vga_ram);
  351. if (ret == 0)
  352. ret = nouveau_bo_pin(dev_priv->vga_ram,
  353. TTM_PL_FLAG_VRAM);
  354. if (ret) {
  355. NV_WARN(dev, "failed to reserve VGA memory\n");
  356. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  357. }
  358. }
  359. dev_priv->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 1),
  360. pci_resource_len(dev->pdev, 1),
  361. DRM_MTRR_WC);
  362. return 0;
  363. }
  364. int
  365. nouveau_mem_gart_init(struct drm_device *dev)
  366. {
  367. struct drm_nouveau_private *dev_priv = dev->dev_private;
  368. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  369. int ret;
  370. dev_priv->gart_info.type = NOUVEAU_GART_NONE;
  371. #if !defined(__powerpc__) && !defined(__ia64__)
  372. if (drm_pci_device_is_agp(dev) && dev->agp && nouveau_agpmode) {
  373. ret = nouveau_mem_init_agp(dev);
  374. if (ret)
  375. NV_ERROR(dev, "Error initialising AGP: %d\n", ret);
  376. }
  377. #endif
  378. if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
  379. ret = nouveau_sgdma_init(dev);
  380. if (ret) {
  381. NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
  382. return ret;
  383. }
  384. }
  385. NV_INFO(dev, "%d MiB GART (aperture)\n",
  386. (int)(dev_priv->gart_info.aper_size >> 20));
  387. dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
  388. ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
  389. dev_priv->gart_info.aper_size >> PAGE_SHIFT);
  390. if (ret) {
  391. NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
  392. return ret;
  393. }
  394. return 0;
  395. }
  396. static int
  397. nv40_mem_timing_calc(struct drm_device *dev, u32 freq,
  398. struct nouveau_pm_tbl_entry *e, u8 len,
  399. struct nouveau_pm_memtiming *boot,
  400. struct nouveau_pm_memtiming *t)
  401. {
  402. t->reg[0] = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
  403. /* XXX: I don't trust the -1's and +1's... they must come
  404. * from somewhere! */
  405. t->reg[1] = (e->tWR + 2 + (t->tCWL - 1)) << 24 |
  406. 1 << 16 |
  407. (e->tWTR + 2 + (t->tCWL - 1)) << 8 |
  408. (e->tCL + 2 - (t->tCWL - 1));
  409. t->reg[2] = 0x20200000 |
  410. ((t->tCWL - 1) << 24 |
  411. e->tRRD << 16 |
  412. e->tRCDWR << 8 |
  413. e->tRCDRD);
  414. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x\n", t->id,
  415. t->reg[0], t->reg[1], t->reg[2]);
  416. return 0;
  417. }
  418. static int
  419. nv50_mem_timing_calc(struct drm_device *dev, u32 freq,
  420. struct nouveau_pm_tbl_entry *e, u8 len,
  421. struct nouveau_pm_memtiming *boot,
  422. struct nouveau_pm_memtiming *t)
  423. {
  424. struct drm_nouveau_private *dev_priv = dev->dev_private;
  425. struct bit_entry P;
  426. uint8_t unk18 = 1, unk20 = 0, unk21 = 0, tmp7_3;
  427. if (bit_table(dev, 'P', &P))
  428. return -EINVAL;
  429. switch (min(len, (u8) 22)) {
  430. case 22:
  431. unk21 = e->tUNK_21;
  432. case 21:
  433. unk20 = e->tUNK_20;
  434. case 20:
  435. if (e->tCWL > 0)
  436. t->tCWL = e->tCWL;
  437. case 19:
  438. unk18 = e->tUNK_18;
  439. break;
  440. }
  441. t->reg[0] = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
  442. t->reg[1] = (e->tWR + 2 + (t->tCWL - 1)) << 24 |
  443. max(unk18, (u8) 1) << 16 |
  444. (e->tWTR + 2 + (t->tCWL - 1)) << 8;
  445. t->reg[2] = ((t->tCWL - 1) << 24 |
  446. e->tRRD << 16 |
  447. e->tRCDWR << 8 |
  448. e->tRCDRD);
  449. t->reg[4] = e->tUNK_13 << 8 | e->tUNK_13;
  450. t->reg[5] = (e->tRFC << 24 | max(e->tRCDRD, e->tRCDWR) << 16 | e->tRP);
  451. t->reg[8] = boot->reg[8] & 0xffffff00;
  452. if (P.version == 1) {
  453. t->reg[1] |= (e->tCL + 2 - (t->tCWL - 1));
  454. t->reg[3] = (0x14 + e->tCL) << 24 |
  455. 0x16 << 16 |
  456. (e->tCL - 1) << 8 |
  457. (e->tCL - 1);
  458. t->reg[4] |= boot->reg[4] & 0xffff0000;
  459. t->reg[6] = (0x33 - t->tCWL) << 16 |
  460. t->tCWL << 8 |
  461. (0x2e + e->tCL - t->tCWL);
  462. t->reg[7] = 0x4000202 | (e->tCL - 1) << 16;
  463. /* XXX: P.version == 1 only has DDR2 and GDDR3? */
  464. if (dev_priv->vram_type == NV_MEM_TYPE_DDR2) {
  465. t->reg[5] |= (e->tCL + 3) << 8;
  466. t->reg[6] |= (t->tCWL - 2) << 8;
  467. t->reg[8] |= (e->tCL - 4);
  468. } else {
  469. t->reg[5] |= (e->tCL + 2) << 8;
  470. t->reg[6] |= t->tCWL << 8;
  471. t->reg[8] |= (e->tCL - 2);
  472. }
  473. } else {
  474. t->reg[1] |= (5 + e->tCL - (t->tCWL));
  475. /* XXX: 0xb? 0x30? */
  476. t->reg[3] = (0x30 + e->tCL) << 24 |
  477. (boot->reg[3] & 0x00ff0000)|
  478. (0xb + e->tCL) << 8 |
  479. (e->tCL - 1);
  480. t->reg[4] |= (unk20 << 24 | unk21 << 16);
  481. /* XXX: +6? */
  482. t->reg[5] |= (t->tCWL + 6) << 8;
  483. t->reg[6] = (0x5a + e->tCL) << 16 |
  484. (6 - e->tCL + t->tCWL) << 8 |
  485. (0x50 + e->tCL - t->tCWL);
  486. tmp7_3 = (boot->reg[7] & 0xff000000) >> 24;
  487. t->reg[7] = (tmp7_3 << 24) |
  488. ((tmp7_3 - 6 + e->tCL) << 16) |
  489. 0x202;
  490. }
  491. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x %08x\n", t->id,
  492. t->reg[0], t->reg[1], t->reg[2], t->reg[3]);
  493. NV_DEBUG(dev, " 230: %08x %08x %08x %08x\n",
  494. t->reg[4], t->reg[5], t->reg[6], t->reg[7]);
  495. NV_DEBUG(dev, " 240: %08x\n", t->reg[8]);
  496. return 0;
  497. }
  498. static int
  499. nvc0_mem_timing_calc(struct drm_device *dev, u32 freq,
  500. struct nouveau_pm_tbl_entry *e, u8 len,
  501. struct nouveau_pm_memtiming *boot,
  502. struct nouveau_pm_memtiming *t)
  503. {
  504. if (e->tCWL > 0)
  505. t->tCWL = e->tCWL;
  506. t->reg[0] = (e->tRP << 24 | (e->tRAS & 0x7f) << 17 |
  507. e->tRFC << 8 | e->tRC);
  508. t->reg[1] = (boot->reg[1] & 0xff000000) |
  509. (e->tRCDWR & 0x0f) << 20 |
  510. (e->tRCDRD & 0x0f) << 14 |
  511. (t->tCWL << 7) |
  512. (e->tCL & 0x0f);
  513. t->reg[2] = (boot->reg[2] & 0xff0000ff) |
  514. e->tWR << 16 | e->tWTR << 8;
  515. t->reg[3] = (e->tUNK_20 & 0x1f) << 9 |
  516. (e->tUNK_21 & 0xf) << 5 |
  517. (e->tUNK_13 & 0x1f);
  518. t->reg[4] = (boot->reg[4] & 0xfff00fff) |
  519. (e->tRRD&0x1f) << 15;
  520. NV_DEBUG(dev, "Entry %d: 290: %08x %08x %08x %08x\n", t->id,
  521. t->reg[0], t->reg[1], t->reg[2], t->reg[3]);
  522. NV_DEBUG(dev, " 2a0: %08x\n", t->reg[4]);
  523. return 0;
  524. }
  525. /**
  526. * MR generation methods
  527. */
  528. static int
  529. nouveau_mem_ddr2_mr(struct drm_device *dev, u32 freq,
  530. struct nouveau_pm_tbl_entry *e, u8 len,
  531. struct nouveau_pm_memtiming *boot,
  532. struct nouveau_pm_memtiming *t)
  533. {
  534. t->drive_strength = 0;
  535. if (len < 15) {
  536. t->odt = boot->odt;
  537. } else {
  538. t->odt = e->RAM_FT1 & 0x07;
  539. }
  540. if (e->tCL >= NV_MEM_CL_DDR2_MAX) {
  541. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  542. return -ERANGE;
  543. }
  544. if (e->tWR >= NV_MEM_WR_DDR2_MAX) {
  545. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  546. return -ERANGE;
  547. }
  548. if (t->odt > 3) {
  549. NV_WARN(dev, "(%u) Invalid odt value, assuming disabled: %x",
  550. t->id, t->odt);
  551. t->odt = 0;
  552. }
  553. t->mr[0] = (boot->mr[0] & 0x100f) |
  554. (e->tCL) << 4 |
  555. (e->tWR - 1) << 9;
  556. t->mr[1] = (boot->mr[1] & 0x101fbb) |
  557. (t->odt & 0x1) << 2 |
  558. (t->odt & 0x2) << 5;
  559. NV_DEBUG(dev, "(%u) MR: %08x", t->id, t->mr[0]);
  560. return 0;
  561. }
  562. uint8_t nv_mem_wr_lut_ddr3[NV_MEM_WR_DDR3_MAX] = {
  563. 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
  564. static int
  565. nouveau_mem_ddr3_mr(struct drm_device *dev, u32 freq,
  566. struct nouveau_pm_tbl_entry *e, u8 len,
  567. struct nouveau_pm_memtiming *boot,
  568. struct nouveau_pm_memtiming *t)
  569. {
  570. u8 cl = e->tCL - 4;
  571. t->drive_strength = 0;
  572. if (len < 15) {
  573. t->odt = boot->odt;
  574. } else {
  575. t->odt = e->RAM_FT1 & 0x07;
  576. }
  577. if (e->tCL >= NV_MEM_CL_DDR3_MAX || e->tCL < 4) {
  578. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  579. return -ERANGE;
  580. }
  581. if (e->tWR >= NV_MEM_WR_DDR3_MAX || e->tWR < 4) {
  582. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  583. return -ERANGE;
  584. }
  585. if (e->tCWL < 5) {
  586. NV_WARN(dev, "(%u) Invalid tCWL: %u", t->id, e->tCWL);
  587. return -ERANGE;
  588. }
  589. t->mr[0] = (boot->mr[0] & 0x180b) |
  590. /* CAS */
  591. (cl & 0x7) << 4 |
  592. (cl & 0x8) >> 1 |
  593. (nv_mem_wr_lut_ddr3[e->tWR]) << 9;
  594. t->mr[1] = (boot->mr[1] & 0x101dbb) |
  595. (t->odt & 0x1) << 2 |
  596. (t->odt & 0x2) << 5 |
  597. (t->odt & 0x4) << 7;
  598. t->mr[2] = (boot->mr[2] & 0x20ffb7) | (e->tCWL - 5) << 3;
  599. NV_DEBUG(dev, "(%u) MR: %08x %08x", t->id, t->mr[0], t->mr[2]);
  600. return 0;
  601. }
  602. uint8_t nv_mem_cl_lut_gddr3[NV_MEM_CL_GDDR3_MAX] = {
  603. 0, 0, 0, 0, 4, 5, 6, 7, 0, 1, 2, 3, 8, 9, 10, 11};
  604. uint8_t nv_mem_wr_lut_gddr3[NV_MEM_WR_GDDR3_MAX] = {
  605. 0, 0, 0, 0, 0, 2, 3, 8, 9, 10, 11, 0, 0, 1, 1, 0, 3};
  606. static int
  607. nouveau_mem_gddr3_mr(struct drm_device *dev, u32 freq,
  608. struct nouveau_pm_tbl_entry *e, u8 len,
  609. struct nouveau_pm_memtiming *boot,
  610. struct nouveau_pm_memtiming *t)
  611. {
  612. if (len < 15) {
  613. t->drive_strength = boot->drive_strength;
  614. t->odt = boot->odt;
  615. } else {
  616. t->drive_strength = (e->RAM_FT1 & 0x30) >> 4;
  617. t->odt = e->RAM_FT1 & 0x07;
  618. }
  619. if (e->tCL >= NV_MEM_CL_GDDR3_MAX) {
  620. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  621. return -ERANGE;
  622. }
  623. if (e->tWR >= NV_MEM_WR_GDDR3_MAX) {
  624. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  625. return -ERANGE;
  626. }
  627. if (t->odt > 3) {
  628. NV_WARN(dev, "(%u) Invalid odt value, assuming autocal: %x",
  629. t->id, t->odt);
  630. t->odt = 0;
  631. }
  632. t->mr[0] = (boot->mr[0] & 0xe0b) |
  633. /* CAS */
  634. ((nv_mem_cl_lut_gddr3[e->tCL] & 0x7) << 4) |
  635. ((nv_mem_cl_lut_gddr3[e->tCL] & 0x8) >> 2);
  636. t->mr[1] = (boot->mr[1] & 0x100f40) | t->drive_strength |
  637. (t->odt << 2) |
  638. (nv_mem_wr_lut_gddr3[e->tWR] & 0xf) << 4;
  639. t->mr[2] = boot->mr[2];
  640. NV_DEBUG(dev, "(%u) MR: %08x %08x %08x", t->id,
  641. t->mr[0], t->mr[1], t->mr[2]);
  642. return 0;
  643. }
  644. static int
  645. nouveau_mem_gddr5_mr(struct drm_device *dev, u32 freq,
  646. struct nouveau_pm_tbl_entry *e, u8 len,
  647. struct nouveau_pm_memtiming *boot,
  648. struct nouveau_pm_memtiming *t)
  649. {
  650. if (len < 15) {
  651. t->drive_strength = boot->drive_strength;
  652. t->odt = boot->odt;
  653. } else {
  654. t->drive_strength = (e->RAM_FT1 & 0x30) >> 4;
  655. t->odt = e->RAM_FT1 & 0x03;
  656. }
  657. if (e->tCL >= NV_MEM_CL_GDDR5_MAX) {
  658. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  659. return -ERANGE;
  660. }
  661. if (e->tWR >= NV_MEM_WR_GDDR5_MAX) {
  662. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  663. return -ERANGE;
  664. }
  665. if (t->odt > 3) {
  666. NV_WARN(dev, "(%u) Invalid odt value, assuming autocal: %x",
  667. t->id, t->odt);
  668. t->odt = 0;
  669. }
  670. t->mr[0] = (boot->mr[0] & 0x007) |
  671. ((e->tCL - 5) << 3) |
  672. ((e->tWR - 4) << 8);
  673. t->mr[1] = (boot->mr[1] & 0x1007f0) |
  674. t->drive_strength |
  675. (t->odt << 2);
  676. NV_DEBUG(dev, "(%u) MR: %08x %08x", t->id, t->mr[0], t->mr[1]);
  677. return 0;
  678. }
  679. int
  680. nouveau_mem_timing_calc(struct drm_device *dev, u32 freq,
  681. struct nouveau_pm_memtiming *t)
  682. {
  683. struct drm_nouveau_private *dev_priv = dev->dev_private;
  684. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  685. struct nouveau_pm_memtiming *boot = &pm->boot.timing;
  686. struct nouveau_pm_tbl_entry *e;
  687. u8 ver, len, *ptr, *ramcfg;
  688. int ret;
  689. ptr = nouveau_perf_timing(dev, freq, &ver, &len);
  690. if (!ptr || ptr[0] == 0x00) {
  691. *t = *boot;
  692. return 0;
  693. }
  694. e = (struct nouveau_pm_tbl_entry *)ptr;
  695. t->tCWL = boot->tCWL;
  696. switch (dev_priv->card_type) {
  697. case NV_40:
  698. ret = nv40_mem_timing_calc(dev, freq, e, len, boot, t);
  699. break;
  700. case NV_50:
  701. ret = nv50_mem_timing_calc(dev, freq, e, len, boot, t);
  702. break;
  703. case NV_C0:
  704. ret = nvc0_mem_timing_calc(dev, freq, e, len, boot, t);
  705. break;
  706. default:
  707. ret = -ENODEV;
  708. break;
  709. }
  710. switch (dev_priv->vram_type * !ret) {
  711. case NV_MEM_TYPE_GDDR3:
  712. ret = nouveau_mem_gddr3_mr(dev, freq, e, len, boot, t);
  713. break;
  714. case NV_MEM_TYPE_GDDR5:
  715. ret = nouveau_mem_gddr5_mr(dev, freq, e, len, boot, t);
  716. break;
  717. case NV_MEM_TYPE_DDR2:
  718. ret = nouveau_mem_ddr2_mr(dev, freq, e, len, boot, t);
  719. break;
  720. case NV_MEM_TYPE_DDR3:
  721. ret = nouveau_mem_ddr3_mr(dev, freq, e, len, boot, t);
  722. break;
  723. default:
  724. ret = -EINVAL;
  725. break;
  726. }
  727. ramcfg = nouveau_perf_ramcfg(dev, freq, &ver, &len);
  728. if (ramcfg) {
  729. int dll_off;
  730. if (ver == 0x00)
  731. dll_off = !!(ramcfg[3] & 0x04);
  732. else
  733. dll_off = !!(ramcfg[2] & 0x40);
  734. switch (dev_priv->vram_type) {
  735. case NV_MEM_TYPE_GDDR3:
  736. t->mr[1] &= ~0x00000040;
  737. t->mr[1] |= 0x00000040 * dll_off;
  738. break;
  739. default:
  740. t->mr[1] &= ~0x00000001;
  741. t->mr[1] |= 0x00000001 * dll_off;
  742. break;
  743. }
  744. }
  745. return ret;
  746. }
  747. void
  748. nouveau_mem_timing_read(struct drm_device *dev, struct nouveau_pm_memtiming *t)
  749. {
  750. struct drm_nouveau_private *dev_priv = dev->dev_private;
  751. u32 timing_base, timing_regs, mr_base;
  752. int i;
  753. if (dev_priv->card_type >= 0xC0) {
  754. timing_base = 0x10f290;
  755. mr_base = 0x10f300;
  756. } else {
  757. timing_base = 0x100220;
  758. mr_base = 0x1002c0;
  759. }
  760. t->id = -1;
  761. switch (dev_priv->card_type) {
  762. case NV_50:
  763. timing_regs = 9;
  764. break;
  765. case NV_C0:
  766. case NV_D0:
  767. timing_regs = 5;
  768. break;
  769. case NV_30:
  770. case NV_40:
  771. timing_regs = 3;
  772. break;
  773. default:
  774. timing_regs = 0;
  775. return;
  776. }
  777. for(i = 0; i < timing_regs; i++)
  778. t->reg[i] = nv_rd32(dev, timing_base + (0x04 * i));
  779. t->tCWL = 0;
  780. if (dev_priv->card_type < NV_C0) {
  781. t->tCWL = ((nv_rd32(dev, 0x100228) & 0x0f000000) >> 24) + 1;
  782. } else if (dev_priv->card_type <= NV_D0) {
  783. t->tCWL = ((nv_rd32(dev, 0x10f294) & 0x00000f80) >> 7);
  784. }
  785. t->mr[0] = nv_rd32(dev, mr_base);
  786. t->mr[1] = nv_rd32(dev, mr_base + 0x04);
  787. t->mr[2] = nv_rd32(dev, mr_base + 0x20);
  788. t->mr[3] = nv_rd32(dev, mr_base + 0x24);
  789. t->odt = 0;
  790. t->drive_strength = 0;
  791. switch (dev_priv->vram_type) {
  792. case NV_MEM_TYPE_DDR3:
  793. t->odt |= (t->mr[1] & 0x200) >> 7;
  794. case NV_MEM_TYPE_DDR2:
  795. t->odt |= (t->mr[1] & 0x04) >> 2 |
  796. (t->mr[1] & 0x40) >> 5;
  797. break;
  798. case NV_MEM_TYPE_GDDR3:
  799. case NV_MEM_TYPE_GDDR5:
  800. t->drive_strength = t->mr[1] & 0x03;
  801. t->odt = (t->mr[1] & 0x0c) >> 2;
  802. break;
  803. default:
  804. break;
  805. }
  806. }
  807. int
  808. nouveau_mem_exec(struct nouveau_mem_exec_func *exec,
  809. struct nouveau_pm_level *perflvl)
  810. {
  811. struct drm_nouveau_private *dev_priv = exec->dev->dev_private;
  812. struct nouveau_pm_memtiming *info = &perflvl->timing;
  813. u32 tMRD = 1000, tCKSRE = 0, tCKSRX = 0, tXS = 0, tDLLK = 0;
  814. u32 mr[3] = { info->mr[0], info->mr[1], info->mr[2] };
  815. u32 mr1_dlloff;
  816. switch (dev_priv->vram_type) {
  817. case NV_MEM_TYPE_DDR2:
  818. tDLLK = 2000;
  819. mr1_dlloff = 0x00000001;
  820. break;
  821. case NV_MEM_TYPE_DDR3:
  822. tDLLK = 12000;
  823. mr1_dlloff = 0x00000001;
  824. break;
  825. case NV_MEM_TYPE_GDDR3:
  826. tDLLK = 40000;
  827. mr1_dlloff = 0x00000040;
  828. break;
  829. default:
  830. NV_ERROR(exec->dev, "cannot reclock unsupported memtype\n");
  831. return -ENODEV;
  832. }
  833. /* fetch current MRs */
  834. switch (dev_priv->vram_type) {
  835. case NV_MEM_TYPE_GDDR3:
  836. case NV_MEM_TYPE_DDR3:
  837. mr[2] = exec->mrg(exec, 2);
  838. default:
  839. mr[1] = exec->mrg(exec, 1);
  840. mr[0] = exec->mrg(exec, 0);
  841. break;
  842. }
  843. /* DLL 'on' -> DLL 'off' mode, disable before entering self-refresh */
  844. if (!(mr[1] & mr1_dlloff) && (info->mr[1] & mr1_dlloff)) {
  845. exec->precharge(exec);
  846. exec->mrs (exec, 1, mr[1] | mr1_dlloff);
  847. exec->wait(exec, tMRD);
  848. }
  849. /* enter self-refresh mode */
  850. exec->precharge(exec);
  851. exec->refresh(exec);
  852. exec->refresh(exec);
  853. exec->refresh_auto(exec, false);
  854. exec->refresh_self(exec, true);
  855. exec->wait(exec, tCKSRE);
  856. /* modify input clock frequency */
  857. exec->clock_set(exec);
  858. /* exit self-refresh mode */
  859. exec->wait(exec, tCKSRX);
  860. exec->precharge(exec);
  861. exec->refresh_self(exec, false);
  862. exec->refresh_auto(exec, true);
  863. exec->wait(exec, tXS);
  864. /* update MRs */
  865. if (mr[2] != info->mr[2]) {
  866. exec->mrs (exec, 2, info->mr[2]);
  867. exec->wait(exec, tMRD);
  868. }
  869. if (mr[1] != info->mr[1]) {
  870. /* need to keep DLL off until later, at least on GDDR3 */
  871. exec->mrs (exec, 1, info->mr[1] | (mr[1] & mr1_dlloff));
  872. exec->wait(exec, tMRD);
  873. }
  874. if (mr[0] != info->mr[0]) {
  875. exec->mrs (exec, 0, info->mr[0]);
  876. exec->wait(exec, tMRD);
  877. }
  878. /* update PFB timing registers */
  879. exec->timing_set(exec);
  880. /* DLL (enable + ) reset */
  881. if (!(info->mr[1] & mr1_dlloff)) {
  882. if (mr[1] & mr1_dlloff) {
  883. exec->mrs (exec, 1, info->mr[1]);
  884. exec->wait(exec, tMRD);
  885. }
  886. exec->mrs (exec, 0, info->mr[0] | 0x00000100);
  887. exec->wait(exec, tMRD);
  888. exec->mrs (exec, 0, info->mr[0] | 0x00000000);
  889. exec->wait(exec, tMRD);
  890. exec->wait(exec, tDLLK);
  891. if (dev_priv->vram_type == NV_MEM_TYPE_GDDR3)
  892. exec->precharge(exec);
  893. }
  894. return 0;
  895. }
  896. int
  897. nouveau_mem_vbios_type(struct drm_device *dev)
  898. {
  899. struct bit_entry M;
  900. u8 ramcfg = (nv_rd32(dev, 0x101000) & 0x0000003c) >> 2;
  901. if (!bit_table(dev, 'M', &M) || M.version != 2 || M.length < 5) {
  902. u8 *table = ROMPTR(dev, M.data[3]);
  903. if (table && table[0] == 0x10 && ramcfg < table[3]) {
  904. u8 *entry = table + table[1] + (ramcfg * table[2]);
  905. switch (entry[0] & 0x0f) {
  906. case 0: return NV_MEM_TYPE_DDR2;
  907. case 1: return NV_MEM_TYPE_DDR3;
  908. case 2: return NV_MEM_TYPE_GDDR3;
  909. case 3: return NV_MEM_TYPE_GDDR5;
  910. default:
  911. break;
  912. }
  913. }
  914. }
  915. return NV_MEM_TYPE_UNKNOWN;
  916. }
  917. static int
  918. nouveau_vram_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  919. {
  920. /* nothing to do */
  921. return 0;
  922. }
  923. static int
  924. nouveau_vram_manager_fini(struct ttm_mem_type_manager *man)
  925. {
  926. /* nothing to do */
  927. return 0;
  928. }
  929. static inline void
  930. nouveau_mem_node_cleanup(struct nouveau_mem *node)
  931. {
  932. if (node->vma[0].node) {
  933. nouveau_vm_unmap(&node->vma[0]);
  934. nouveau_vm_put(&node->vma[0]);
  935. }
  936. if (node->vma[1].node) {
  937. nouveau_vm_unmap(&node->vma[1]);
  938. nouveau_vm_put(&node->vma[1]);
  939. }
  940. }
  941. static void
  942. nouveau_vram_manager_del(struct ttm_mem_type_manager *man,
  943. struct ttm_mem_reg *mem)
  944. {
  945. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  946. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  947. struct drm_device *dev = dev_priv->dev;
  948. nouveau_mem_node_cleanup(mem->mm_node);
  949. vram->put(dev, (struct nouveau_mem **)&mem->mm_node);
  950. }
  951. static int
  952. nouveau_vram_manager_new(struct ttm_mem_type_manager *man,
  953. struct ttm_buffer_object *bo,
  954. struct ttm_placement *placement,
  955. struct ttm_mem_reg *mem)
  956. {
  957. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  958. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  959. struct drm_device *dev = dev_priv->dev;
  960. struct nouveau_bo *nvbo = nouveau_bo(bo);
  961. struct nouveau_mem *node;
  962. u32 size_nc = 0;
  963. int ret;
  964. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG)
  965. size_nc = 1 << nvbo->page_shift;
  966. ret = vram->get(dev, mem->num_pages << PAGE_SHIFT,
  967. mem->page_alignment << PAGE_SHIFT, size_nc,
  968. (nvbo->tile_flags >> 8) & 0x3ff, &node);
  969. if (ret) {
  970. mem->mm_node = NULL;
  971. return (ret == -ENOSPC) ? 0 : ret;
  972. }
  973. node->page_shift = nvbo->page_shift;
  974. mem->mm_node = node;
  975. mem->start = node->offset >> PAGE_SHIFT;
  976. return 0;
  977. }
  978. void
  979. nouveau_vram_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  980. {
  981. struct nouveau_mm *mm = man->priv;
  982. struct nouveau_mm_node *r;
  983. u32 total = 0, free = 0;
  984. mutex_lock(&mm->mutex);
  985. list_for_each_entry(r, &mm->nodes, nl_entry) {
  986. printk(KERN_DEBUG "%s %d: 0x%010llx 0x%010llx\n",
  987. prefix, r->type, ((u64)r->offset << 12),
  988. (((u64)r->offset + r->length) << 12));
  989. total += r->length;
  990. if (!r->type)
  991. free += r->length;
  992. }
  993. mutex_unlock(&mm->mutex);
  994. printk(KERN_DEBUG "%s total: 0x%010llx free: 0x%010llx\n",
  995. prefix, (u64)total << 12, (u64)free << 12);
  996. printk(KERN_DEBUG "%s block: 0x%08x\n",
  997. prefix, mm->block_size << 12);
  998. }
  999. const struct ttm_mem_type_manager_func nouveau_vram_manager = {
  1000. nouveau_vram_manager_init,
  1001. nouveau_vram_manager_fini,
  1002. nouveau_vram_manager_new,
  1003. nouveau_vram_manager_del,
  1004. nouveau_vram_manager_debug
  1005. };
  1006. static int
  1007. nouveau_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  1008. {
  1009. return 0;
  1010. }
  1011. static int
  1012. nouveau_gart_manager_fini(struct ttm_mem_type_manager *man)
  1013. {
  1014. return 0;
  1015. }
  1016. static void
  1017. nouveau_gart_manager_del(struct ttm_mem_type_manager *man,
  1018. struct ttm_mem_reg *mem)
  1019. {
  1020. nouveau_mem_node_cleanup(mem->mm_node);
  1021. kfree(mem->mm_node);
  1022. mem->mm_node = NULL;
  1023. }
  1024. static int
  1025. nouveau_gart_manager_new(struct ttm_mem_type_manager *man,
  1026. struct ttm_buffer_object *bo,
  1027. struct ttm_placement *placement,
  1028. struct ttm_mem_reg *mem)
  1029. {
  1030. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  1031. struct nouveau_mem *node;
  1032. if (unlikely((mem->num_pages << PAGE_SHIFT) >=
  1033. dev_priv->gart_info.aper_size))
  1034. return -ENOMEM;
  1035. node = kzalloc(sizeof(*node), GFP_KERNEL);
  1036. if (!node)
  1037. return -ENOMEM;
  1038. node->page_shift = 12;
  1039. mem->mm_node = node;
  1040. mem->start = 0;
  1041. return 0;
  1042. }
  1043. void
  1044. nouveau_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  1045. {
  1046. }
  1047. const struct ttm_mem_type_manager_func nouveau_gart_manager = {
  1048. nouveau_gart_manager_init,
  1049. nouveau_gart_manager_fini,
  1050. nouveau_gart_manager_new,
  1051. nouveau_gart_manager_del,
  1052. nouveau_gart_manager_debug
  1053. };