nouveau_mem.c 30 KB

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