nouveau_mem.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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 <core/mm.h>
  38. #include <subdev/vm.h>
  39. #include <engine/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. }
  146. bool
  147. nouveau_mem_flags_valid(struct drm_device *dev, u32 tile_flags)
  148. {
  149. if (!(tile_flags & NOUVEAU_GEM_TILE_LAYOUT_MASK))
  150. return true;
  151. return false;
  152. }
  153. static const struct vram_types {
  154. int value;
  155. const char *name;
  156. } vram_type_map[] = {
  157. { NV_MEM_TYPE_STOLEN , "stolen system memory" },
  158. { NV_MEM_TYPE_SGRAM , "SGRAM" },
  159. { NV_MEM_TYPE_SDRAM , "SDRAM" },
  160. { NV_MEM_TYPE_DDR1 , "DDR1" },
  161. { NV_MEM_TYPE_DDR2 , "DDR2" },
  162. { NV_MEM_TYPE_DDR3 , "DDR3" },
  163. { NV_MEM_TYPE_GDDR2 , "GDDR2" },
  164. { NV_MEM_TYPE_GDDR3 , "GDDR3" },
  165. { NV_MEM_TYPE_GDDR4 , "GDDR4" },
  166. { NV_MEM_TYPE_GDDR5 , "GDDR5" },
  167. { NV_MEM_TYPE_UNKNOWN, "unknown type" }
  168. };
  169. int
  170. nouveau_mem_vram_init(struct drm_device *dev)
  171. {
  172. struct drm_nouveau_private *dev_priv = dev->dev_private;
  173. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  174. const struct vram_types *vram_type;
  175. int ret, dma_bits;
  176. dma_bits = 32;
  177. if (dev_priv->card_type >= NV_50) {
  178. if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(40)))
  179. dma_bits = 40;
  180. } else
  181. if (0 && pci_is_pcie(dev->pdev) &&
  182. dev_priv->chipset > 0x40 &&
  183. dev_priv->chipset != 0x45) {
  184. if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(39)))
  185. dma_bits = 39;
  186. }
  187. ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  188. if (ret)
  189. return ret;
  190. ret = pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  191. if (ret) {
  192. /* Reset to default value. */
  193. pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32));
  194. }
  195. ret = nouveau_ttm_global_init(dev_priv);
  196. if (ret)
  197. return ret;
  198. ret = ttm_bo_device_init(&dev_priv->ttm.bdev,
  199. dev_priv->ttm.bo_global_ref.ref.object,
  200. &nouveau_bo_driver, DRM_FILE_PAGE_OFFSET,
  201. dma_bits <= 32 ? true : false);
  202. if (ret) {
  203. NV_ERROR(dev, "Error initialising bo driver: %d\n", ret);
  204. return ret;
  205. }
  206. vram_type = vram_type_map;
  207. while (vram_type->value != NV_MEM_TYPE_UNKNOWN) {
  208. if (nouveau_vram_type) {
  209. if (!strcasecmp(nouveau_vram_type, vram_type->name))
  210. break;
  211. dev_priv->vram_type = vram_type->value;
  212. } else {
  213. if (vram_type->value == dev_priv->vram_type)
  214. break;
  215. }
  216. vram_type++;
  217. }
  218. NV_INFO(dev, "Detected %dMiB VRAM (%s)\n",
  219. (int)(dev_priv->vram_size >> 20), vram_type->name);
  220. if (dev_priv->vram_sys_base) {
  221. NV_INFO(dev, "Stolen system memory at: 0x%010llx\n",
  222. dev_priv->vram_sys_base);
  223. }
  224. dev_priv->fb_available_size = dev_priv->vram_size;
  225. dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
  226. if (dev_priv->fb_mappable_pages > pci_resource_len(dev->pdev, 1))
  227. dev_priv->fb_mappable_pages = pci_resource_len(dev->pdev, 1);
  228. dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
  229. dev_priv->fb_available_size -= dev_priv->ramin_rsvd_vram;
  230. dev_priv->fb_aper_free = dev_priv->fb_available_size;
  231. /* mappable vram */
  232. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  233. dev_priv->fb_available_size >> PAGE_SHIFT);
  234. if (ret) {
  235. NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
  236. return ret;
  237. }
  238. if (dev_priv->card_type < NV_50) {
  239. ret = nouveau_bo_new(dev, 256*1024, 0, TTM_PL_FLAG_VRAM,
  240. 0, 0, NULL, &dev_priv->vga_ram);
  241. if (ret == 0)
  242. ret = nouveau_bo_pin(dev_priv->vga_ram,
  243. TTM_PL_FLAG_VRAM);
  244. if (ret) {
  245. NV_WARN(dev, "failed to reserve VGA memory\n");
  246. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  247. }
  248. }
  249. dev_priv->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 1),
  250. pci_resource_len(dev->pdev, 1),
  251. DRM_MTRR_WC);
  252. return 0;
  253. }
  254. int
  255. nouveau_mem_gart_init(struct drm_device *dev)
  256. {
  257. struct drm_nouveau_private *dev_priv = dev->dev_private;
  258. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  259. int ret;
  260. if (!nvdrm_gart_init(dev, &dev_priv->gart_info.aper_base,
  261. &dev_priv->gart_info.aper_size))
  262. dev_priv->gart_info.type = NOUVEAU_GART_AGP;
  263. if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
  264. ret = nouveau_sgdma_init(dev);
  265. if (ret) {
  266. NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
  267. return ret;
  268. }
  269. }
  270. NV_INFO(dev, "%d MiB GART (aperture)\n",
  271. (int)(dev_priv->gart_info.aper_size >> 20));
  272. dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
  273. ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
  274. dev_priv->gart_info.aper_size >> PAGE_SHIFT);
  275. if (ret) {
  276. NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
  277. return ret;
  278. }
  279. return 0;
  280. }
  281. static int
  282. nv40_mem_timing_calc(struct drm_device *dev, u32 freq,
  283. struct nouveau_pm_tbl_entry *e, u8 len,
  284. struct nouveau_pm_memtiming *boot,
  285. struct nouveau_pm_memtiming *t)
  286. {
  287. t->reg[0] = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
  288. /* XXX: I don't trust the -1's and +1's... they must come
  289. * from somewhere! */
  290. t->reg[1] = (e->tWR + 2 + (t->tCWL - 1)) << 24 |
  291. 1 << 16 |
  292. (e->tWTR + 2 + (t->tCWL - 1)) << 8 |
  293. (e->tCL + 2 - (t->tCWL - 1));
  294. t->reg[2] = 0x20200000 |
  295. ((t->tCWL - 1) << 24 |
  296. e->tRRD << 16 |
  297. e->tRCDWR << 8 |
  298. e->tRCDRD);
  299. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x\n", t->id,
  300. t->reg[0], t->reg[1], t->reg[2]);
  301. return 0;
  302. }
  303. static int
  304. nv50_mem_timing_calc(struct drm_device *dev, u32 freq,
  305. struct nouveau_pm_tbl_entry *e, u8 len,
  306. struct nouveau_pm_memtiming *boot,
  307. struct nouveau_pm_memtiming *t)
  308. {
  309. struct drm_nouveau_private *dev_priv = dev->dev_private;
  310. struct bit_entry P;
  311. uint8_t unk18 = 1, unk20 = 0, unk21 = 0, tmp7_3;
  312. if (bit_table(dev, 'P', &P))
  313. return -EINVAL;
  314. switch (min(len, (u8) 22)) {
  315. case 22:
  316. unk21 = e->tUNK_21;
  317. case 21:
  318. unk20 = e->tUNK_20;
  319. case 20:
  320. if (e->tCWL > 0)
  321. t->tCWL = e->tCWL;
  322. case 19:
  323. unk18 = e->tUNK_18;
  324. break;
  325. }
  326. t->reg[0] = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
  327. t->reg[1] = (e->tWR + 2 + (t->tCWL - 1)) << 24 |
  328. max(unk18, (u8) 1) << 16 |
  329. (e->tWTR + 2 + (t->tCWL - 1)) << 8;
  330. t->reg[2] = ((t->tCWL - 1) << 24 |
  331. e->tRRD << 16 |
  332. e->tRCDWR << 8 |
  333. e->tRCDRD);
  334. t->reg[4] = e->tUNK_13 << 8 | e->tUNK_13;
  335. t->reg[5] = (e->tRFC << 24 | max(e->tRCDRD, e->tRCDWR) << 16 | e->tRP);
  336. t->reg[8] = boot->reg[8] & 0xffffff00;
  337. if (P.version == 1) {
  338. t->reg[1] |= (e->tCL + 2 - (t->tCWL - 1));
  339. t->reg[3] = (0x14 + e->tCL) << 24 |
  340. 0x16 << 16 |
  341. (e->tCL - 1) << 8 |
  342. (e->tCL - 1);
  343. t->reg[4] |= boot->reg[4] & 0xffff0000;
  344. t->reg[6] = (0x33 - t->tCWL) << 16 |
  345. t->tCWL << 8 |
  346. (0x2e + e->tCL - t->tCWL);
  347. t->reg[7] = 0x4000202 | (e->tCL - 1) << 16;
  348. /* XXX: P.version == 1 only has DDR2 and GDDR3? */
  349. if (dev_priv->vram_type == NV_MEM_TYPE_DDR2) {
  350. t->reg[5] |= (e->tCL + 3) << 8;
  351. t->reg[6] |= (t->tCWL - 2) << 8;
  352. t->reg[8] |= (e->tCL - 4);
  353. } else {
  354. t->reg[5] |= (e->tCL + 2) << 8;
  355. t->reg[6] |= t->tCWL << 8;
  356. t->reg[8] |= (e->tCL - 2);
  357. }
  358. } else {
  359. t->reg[1] |= (5 + e->tCL - (t->tCWL));
  360. /* XXX: 0xb? 0x30? */
  361. t->reg[3] = (0x30 + e->tCL) << 24 |
  362. (boot->reg[3] & 0x00ff0000)|
  363. (0xb + e->tCL) << 8 |
  364. (e->tCL - 1);
  365. t->reg[4] |= (unk20 << 24 | unk21 << 16);
  366. /* XXX: +6? */
  367. t->reg[5] |= (t->tCWL + 6) << 8;
  368. t->reg[6] = (0x5a + e->tCL) << 16 |
  369. (6 - e->tCL + t->tCWL) << 8 |
  370. (0x50 + e->tCL - t->tCWL);
  371. tmp7_3 = (boot->reg[7] & 0xff000000) >> 24;
  372. t->reg[7] = (tmp7_3 << 24) |
  373. ((tmp7_3 - 6 + e->tCL) << 16) |
  374. 0x202;
  375. }
  376. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x %08x\n", t->id,
  377. t->reg[0], t->reg[1], t->reg[2], t->reg[3]);
  378. NV_DEBUG(dev, " 230: %08x %08x %08x %08x\n",
  379. t->reg[4], t->reg[5], t->reg[6], t->reg[7]);
  380. NV_DEBUG(dev, " 240: %08x\n", t->reg[8]);
  381. return 0;
  382. }
  383. static int
  384. nvc0_mem_timing_calc(struct drm_device *dev, u32 freq,
  385. struct nouveau_pm_tbl_entry *e, u8 len,
  386. struct nouveau_pm_memtiming *boot,
  387. struct nouveau_pm_memtiming *t)
  388. {
  389. if (e->tCWL > 0)
  390. t->tCWL = e->tCWL;
  391. t->reg[0] = (e->tRP << 24 | (e->tRAS & 0x7f) << 17 |
  392. e->tRFC << 8 | e->tRC);
  393. t->reg[1] = (boot->reg[1] & 0xff000000) |
  394. (e->tRCDWR & 0x0f) << 20 |
  395. (e->tRCDRD & 0x0f) << 14 |
  396. (t->tCWL << 7) |
  397. (e->tCL & 0x0f);
  398. t->reg[2] = (boot->reg[2] & 0xff0000ff) |
  399. e->tWR << 16 | e->tWTR << 8;
  400. t->reg[3] = (e->tUNK_20 & 0x1f) << 9 |
  401. (e->tUNK_21 & 0xf) << 5 |
  402. (e->tUNK_13 & 0x1f);
  403. t->reg[4] = (boot->reg[4] & 0xfff00fff) |
  404. (e->tRRD&0x1f) << 15;
  405. NV_DEBUG(dev, "Entry %d: 290: %08x %08x %08x %08x\n", t->id,
  406. t->reg[0], t->reg[1], t->reg[2], t->reg[3]);
  407. NV_DEBUG(dev, " 2a0: %08x\n", t->reg[4]);
  408. return 0;
  409. }
  410. /**
  411. * MR generation methods
  412. */
  413. static int
  414. nouveau_mem_ddr2_mr(struct drm_device *dev, u32 freq,
  415. struct nouveau_pm_tbl_entry *e, u8 len,
  416. struct nouveau_pm_memtiming *boot,
  417. struct nouveau_pm_memtiming *t)
  418. {
  419. t->drive_strength = 0;
  420. if (len < 15) {
  421. t->odt = boot->odt;
  422. } else {
  423. t->odt = e->RAM_FT1 & 0x07;
  424. }
  425. if (e->tCL >= NV_MEM_CL_DDR2_MAX) {
  426. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  427. return -ERANGE;
  428. }
  429. if (e->tWR >= NV_MEM_WR_DDR2_MAX) {
  430. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  431. return -ERANGE;
  432. }
  433. if (t->odt > 3) {
  434. NV_WARN(dev, "(%u) Invalid odt value, assuming disabled: %x",
  435. t->id, t->odt);
  436. t->odt = 0;
  437. }
  438. t->mr[0] = (boot->mr[0] & 0x100f) |
  439. (e->tCL) << 4 |
  440. (e->tWR - 1) << 9;
  441. t->mr[1] = (boot->mr[1] & 0x101fbb) |
  442. (t->odt & 0x1) << 2 |
  443. (t->odt & 0x2) << 5;
  444. NV_DEBUG(dev, "(%u) MR: %08x", t->id, t->mr[0]);
  445. return 0;
  446. }
  447. uint8_t nv_mem_wr_lut_ddr3[NV_MEM_WR_DDR3_MAX] = {
  448. 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
  449. static int
  450. nouveau_mem_ddr3_mr(struct drm_device *dev, u32 freq,
  451. struct nouveau_pm_tbl_entry *e, u8 len,
  452. struct nouveau_pm_memtiming *boot,
  453. struct nouveau_pm_memtiming *t)
  454. {
  455. u8 cl = e->tCL - 4;
  456. t->drive_strength = 0;
  457. if (len < 15) {
  458. t->odt = boot->odt;
  459. } else {
  460. t->odt = e->RAM_FT1 & 0x07;
  461. }
  462. if (e->tCL >= NV_MEM_CL_DDR3_MAX || e->tCL < 4) {
  463. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  464. return -ERANGE;
  465. }
  466. if (e->tWR >= NV_MEM_WR_DDR3_MAX || e->tWR < 4) {
  467. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  468. return -ERANGE;
  469. }
  470. if (e->tCWL < 5) {
  471. NV_WARN(dev, "(%u) Invalid tCWL: %u", t->id, e->tCWL);
  472. return -ERANGE;
  473. }
  474. t->mr[0] = (boot->mr[0] & 0x180b) |
  475. /* CAS */
  476. (cl & 0x7) << 4 |
  477. (cl & 0x8) >> 1 |
  478. (nv_mem_wr_lut_ddr3[e->tWR]) << 9;
  479. t->mr[1] = (boot->mr[1] & 0x101dbb) |
  480. (t->odt & 0x1) << 2 |
  481. (t->odt & 0x2) << 5 |
  482. (t->odt & 0x4) << 7;
  483. t->mr[2] = (boot->mr[2] & 0x20ffb7) | (e->tCWL - 5) << 3;
  484. NV_DEBUG(dev, "(%u) MR: %08x %08x", t->id, t->mr[0], t->mr[2]);
  485. return 0;
  486. }
  487. uint8_t nv_mem_cl_lut_gddr3[NV_MEM_CL_GDDR3_MAX] = {
  488. 0, 0, 0, 0, 4, 5, 6, 7, 0, 1, 2, 3, 8, 9, 10, 11};
  489. uint8_t nv_mem_wr_lut_gddr3[NV_MEM_WR_GDDR3_MAX] = {
  490. 0, 0, 0, 0, 0, 2, 3, 8, 9, 10, 11, 0, 0, 1, 1, 0, 3};
  491. static int
  492. nouveau_mem_gddr3_mr(struct drm_device *dev, u32 freq,
  493. struct nouveau_pm_tbl_entry *e, u8 len,
  494. struct nouveau_pm_memtiming *boot,
  495. struct nouveau_pm_memtiming *t)
  496. {
  497. if (len < 15) {
  498. t->drive_strength = boot->drive_strength;
  499. t->odt = boot->odt;
  500. } else {
  501. t->drive_strength = (e->RAM_FT1 & 0x30) >> 4;
  502. t->odt = e->RAM_FT1 & 0x07;
  503. }
  504. if (e->tCL >= NV_MEM_CL_GDDR3_MAX) {
  505. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  506. return -ERANGE;
  507. }
  508. if (e->tWR >= NV_MEM_WR_GDDR3_MAX) {
  509. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  510. return -ERANGE;
  511. }
  512. if (t->odt > 3) {
  513. NV_WARN(dev, "(%u) Invalid odt value, assuming autocal: %x",
  514. t->id, t->odt);
  515. t->odt = 0;
  516. }
  517. t->mr[0] = (boot->mr[0] & 0xe0b) |
  518. /* CAS */
  519. ((nv_mem_cl_lut_gddr3[e->tCL] & 0x7) << 4) |
  520. ((nv_mem_cl_lut_gddr3[e->tCL] & 0x8) >> 2);
  521. t->mr[1] = (boot->mr[1] & 0x100f40) | t->drive_strength |
  522. (t->odt << 2) |
  523. (nv_mem_wr_lut_gddr3[e->tWR] & 0xf) << 4;
  524. t->mr[2] = boot->mr[2];
  525. NV_DEBUG(dev, "(%u) MR: %08x %08x %08x", t->id,
  526. t->mr[0], t->mr[1], t->mr[2]);
  527. return 0;
  528. }
  529. static int
  530. nouveau_mem_gddr5_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. if (len < 15) {
  536. t->drive_strength = boot->drive_strength;
  537. t->odt = boot->odt;
  538. } else {
  539. t->drive_strength = (e->RAM_FT1 & 0x30) >> 4;
  540. t->odt = e->RAM_FT1 & 0x03;
  541. }
  542. if (e->tCL >= NV_MEM_CL_GDDR5_MAX) {
  543. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  544. return -ERANGE;
  545. }
  546. if (e->tWR >= NV_MEM_WR_GDDR5_MAX) {
  547. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  548. return -ERANGE;
  549. }
  550. if (t->odt > 3) {
  551. NV_WARN(dev, "(%u) Invalid odt value, assuming autocal: %x",
  552. t->id, t->odt);
  553. t->odt = 0;
  554. }
  555. t->mr[0] = (boot->mr[0] & 0x007) |
  556. ((e->tCL - 5) << 3) |
  557. ((e->tWR - 4) << 8);
  558. t->mr[1] = (boot->mr[1] & 0x1007f0) |
  559. t->drive_strength |
  560. (t->odt << 2);
  561. NV_DEBUG(dev, "(%u) MR: %08x %08x", t->id, t->mr[0], t->mr[1]);
  562. return 0;
  563. }
  564. int
  565. nouveau_mem_timing_calc(struct drm_device *dev, u32 freq,
  566. struct nouveau_pm_memtiming *t)
  567. {
  568. struct drm_nouveau_private *dev_priv = dev->dev_private;
  569. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  570. struct nouveau_pm_memtiming *boot = &pm->boot.timing;
  571. struct nouveau_pm_tbl_entry *e;
  572. u8 ver, len, *ptr, *ramcfg;
  573. int ret;
  574. ptr = nouveau_perf_timing(dev, freq, &ver, &len);
  575. if (!ptr || ptr[0] == 0x00) {
  576. *t = *boot;
  577. return 0;
  578. }
  579. e = (struct nouveau_pm_tbl_entry *)ptr;
  580. t->tCWL = boot->tCWL;
  581. switch (dev_priv->card_type) {
  582. case NV_40:
  583. ret = nv40_mem_timing_calc(dev, freq, e, len, boot, t);
  584. break;
  585. case NV_50:
  586. ret = nv50_mem_timing_calc(dev, freq, e, len, boot, t);
  587. break;
  588. case NV_C0:
  589. case NV_D0:
  590. ret = nvc0_mem_timing_calc(dev, freq, e, len, boot, t);
  591. break;
  592. default:
  593. ret = -ENODEV;
  594. break;
  595. }
  596. switch (dev_priv->vram_type * !ret) {
  597. case NV_MEM_TYPE_GDDR3:
  598. ret = nouveau_mem_gddr3_mr(dev, freq, e, len, boot, t);
  599. break;
  600. case NV_MEM_TYPE_GDDR5:
  601. ret = nouveau_mem_gddr5_mr(dev, freq, e, len, boot, t);
  602. break;
  603. case NV_MEM_TYPE_DDR2:
  604. ret = nouveau_mem_ddr2_mr(dev, freq, e, len, boot, t);
  605. break;
  606. case NV_MEM_TYPE_DDR3:
  607. ret = nouveau_mem_ddr3_mr(dev, freq, e, len, boot, t);
  608. break;
  609. default:
  610. ret = -EINVAL;
  611. break;
  612. }
  613. ramcfg = nouveau_perf_ramcfg(dev, freq, &ver, &len);
  614. if (ramcfg) {
  615. int dll_off;
  616. if (ver == 0x00)
  617. dll_off = !!(ramcfg[3] & 0x04);
  618. else
  619. dll_off = !!(ramcfg[2] & 0x40);
  620. switch (dev_priv->vram_type) {
  621. case NV_MEM_TYPE_GDDR3:
  622. t->mr[1] &= ~0x00000040;
  623. t->mr[1] |= 0x00000040 * dll_off;
  624. break;
  625. default:
  626. t->mr[1] &= ~0x00000001;
  627. t->mr[1] |= 0x00000001 * dll_off;
  628. break;
  629. }
  630. }
  631. return ret;
  632. }
  633. void
  634. nouveau_mem_timing_read(struct drm_device *dev, struct nouveau_pm_memtiming *t)
  635. {
  636. struct drm_nouveau_private *dev_priv = dev->dev_private;
  637. u32 timing_base, timing_regs, mr_base;
  638. int i;
  639. if (dev_priv->card_type >= 0xC0) {
  640. timing_base = 0x10f290;
  641. mr_base = 0x10f300;
  642. } else {
  643. timing_base = 0x100220;
  644. mr_base = 0x1002c0;
  645. }
  646. t->id = -1;
  647. switch (dev_priv->card_type) {
  648. case NV_50:
  649. timing_regs = 9;
  650. break;
  651. case NV_C0:
  652. case NV_D0:
  653. timing_regs = 5;
  654. break;
  655. case NV_30:
  656. case NV_40:
  657. timing_regs = 3;
  658. break;
  659. default:
  660. timing_regs = 0;
  661. return;
  662. }
  663. for(i = 0; i < timing_regs; i++)
  664. t->reg[i] = nv_rd32(dev, timing_base + (0x04 * i));
  665. t->tCWL = 0;
  666. if (dev_priv->card_type < NV_C0) {
  667. t->tCWL = ((nv_rd32(dev, 0x100228) & 0x0f000000) >> 24) + 1;
  668. } else if (dev_priv->card_type <= NV_D0) {
  669. t->tCWL = ((nv_rd32(dev, 0x10f294) & 0x00000f80) >> 7);
  670. }
  671. t->mr[0] = nv_rd32(dev, mr_base);
  672. t->mr[1] = nv_rd32(dev, mr_base + 0x04);
  673. t->mr[2] = nv_rd32(dev, mr_base + 0x20);
  674. t->mr[3] = nv_rd32(dev, mr_base + 0x24);
  675. t->odt = 0;
  676. t->drive_strength = 0;
  677. switch (dev_priv->vram_type) {
  678. case NV_MEM_TYPE_DDR3:
  679. t->odt |= (t->mr[1] & 0x200) >> 7;
  680. case NV_MEM_TYPE_DDR2:
  681. t->odt |= (t->mr[1] & 0x04) >> 2 |
  682. (t->mr[1] & 0x40) >> 5;
  683. break;
  684. case NV_MEM_TYPE_GDDR3:
  685. case NV_MEM_TYPE_GDDR5:
  686. t->drive_strength = t->mr[1] & 0x03;
  687. t->odt = (t->mr[1] & 0x0c) >> 2;
  688. break;
  689. default:
  690. break;
  691. }
  692. }
  693. int
  694. nouveau_mem_exec(struct nouveau_mem_exec_func *exec,
  695. struct nouveau_pm_level *perflvl)
  696. {
  697. struct drm_nouveau_private *dev_priv = exec->dev->dev_private;
  698. struct nouveau_pm_memtiming *info = &perflvl->timing;
  699. u32 tMRD = 1000, tCKSRE = 0, tCKSRX = 0, tXS = 0, tDLLK = 0;
  700. u32 mr[3] = { info->mr[0], info->mr[1], info->mr[2] };
  701. u32 mr1_dlloff;
  702. switch (dev_priv->vram_type) {
  703. case NV_MEM_TYPE_DDR2:
  704. tDLLK = 2000;
  705. mr1_dlloff = 0x00000001;
  706. break;
  707. case NV_MEM_TYPE_DDR3:
  708. tDLLK = 12000;
  709. tCKSRE = 2000;
  710. tXS = 1000;
  711. mr1_dlloff = 0x00000001;
  712. break;
  713. case NV_MEM_TYPE_GDDR3:
  714. tDLLK = 40000;
  715. mr1_dlloff = 0x00000040;
  716. break;
  717. default:
  718. NV_ERROR(exec->dev, "cannot reclock unsupported memtype\n");
  719. return -ENODEV;
  720. }
  721. /* fetch current MRs */
  722. switch (dev_priv->vram_type) {
  723. case NV_MEM_TYPE_GDDR3:
  724. case NV_MEM_TYPE_DDR3:
  725. mr[2] = exec->mrg(exec, 2);
  726. default:
  727. mr[1] = exec->mrg(exec, 1);
  728. mr[0] = exec->mrg(exec, 0);
  729. break;
  730. }
  731. /* DLL 'on' -> DLL 'off' mode, disable before entering self-refresh */
  732. if (!(mr[1] & mr1_dlloff) && (info->mr[1] & mr1_dlloff)) {
  733. exec->precharge(exec);
  734. exec->mrs (exec, 1, mr[1] | mr1_dlloff);
  735. exec->wait(exec, tMRD);
  736. }
  737. /* enter self-refresh mode */
  738. exec->precharge(exec);
  739. exec->refresh(exec);
  740. exec->refresh(exec);
  741. exec->refresh_auto(exec, false);
  742. exec->refresh_self(exec, true);
  743. exec->wait(exec, tCKSRE);
  744. /* modify input clock frequency */
  745. exec->clock_set(exec);
  746. /* exit self-refresh mode */
  747. exec->wait(exec, tCKSRX);
  748. exec->precharge(exec);
  749. exec->refresh_self(exec, false);
  750. exec->refresh_auto(exec, true);
  751. exec->wait(exec, tXS);
  752. exec->wait(exec, tXS);
  753. /* update MRs */
  754. if (mr[2] != info->mr[2]) {
  755. exec->mrs (exec, 2, info->mr[2]);
  756. exec->wait(exec, tMRD);
  757. }
  758. if (mr[1] != info->mr[1]) {
  759. /* need to keep DLL off until later, at least on GDDR3 */
  760. exec->mrs (exec, 1, info->mr[1] | (mr[1] & mr1_dlloff));
  761. exec->wait(exec, tMRD);
  762. }
  763. if (mr[0] != info->mr[0]) {
  764. exec->mrs (exec, 0, info->mr[0]);
  765. exec->wait(exec, tMRD);
  766. }
  767. /* update PFB timing registers */
  768. exec->timing_set(exec);
  769. /* DLL (enable + ) reset */
  770. if (!(info->mr[1] & mr1_dlloff)) {
  771. if (mr[1] & mr1_dlloff) {
  772. exec->mrs (exec, 1, info->mr[1]);
  773. exec->wait(exec, tMRD);
  774. }
  775. exec->mrs (exec, 0, info->mr[0] | 0x00000100);
  776. exec->wait(exec, tMRD);
  777. exec->mrs (exec, 0, info->mr[0] | 0x00000000);
  778. exec->wait(exec, tMRD);
  779. exec->wait(exec, tDLLK);
  780. if (dev_priv->vram_type == NV_MEM_TYPE_GDDR3)
  781. exec->precharge(exec);
  782. }
  783. return 0;
  784. }
  785. int
  786. nouveau_mem_vbios_type(struct drm_device *dev)
  787. {
  788. struct bit_entry M;
  789. u8 ramcfg = (nv_rd32(dev, 0x101000) & 0x0000003c) >> 2;
  790. if (!bit_table(dev, 'M', &M) || M.version != 2 || M.length < 5) {
  791. u8 *table = ROMPTR(dev, M.data[3]);
  792. if (table && table[0] == 0x10 && ramcfg < table[3]) {
  793. u8 *entry = table + table[1] + (ramcfg * table[2]);
  794. switch (entry[0] & 0x0f) {
  795. case 0: return NV_MEM_TYPE_DDR2;
  796. case 1: return NV_MEM_TYPE_DDR3;
  797. case 2: return NV_MEM_TYPE_GDDR3;
  798. case 3: return NV_MEM_TYPE_GDDR5;
  799. default:
  800. break;
  801. }
  802. }
  803. }
  804. return NV_MEM_TYPE_UNKNOWN;
  805. }
  806. static int
  807. nouveau_vram_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  808. {
  809. /* nothing to do */
  810. return 0;
  811. }
  812. static int
  813. nouveau_vram_manager_fini(struct ttm_mem_type_manager *man)
  814. {
  815. /* nothing to do */
  816. return 0;
  817. }
  818. static inline void
  819. nouveau_mem_node_cleanup(struct nouveau_mem *node)
  820. {
  821. if (node->vma[0].node) {
  822. nouveau_vm_unmap(&node->vma[0]);
  823. nouveau_vm_put(&node->vma[0]);
  824. }
  825. if (node->vma[1].node) {
  826. nouveau_vm_unmap(&node->vma[1]);
  827. nouveau_vm_put(&node->vma[1]);
  828. }
  829. }
  830. static void
  831. nouveau_vram_manager_del(struct ttm_mem_type_manager *man,
  832. struct ttm_mem_reg *mem)
  833. {
  834. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  835. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  836. struct drm_device *dev = dev_priv->dev;
  837. nouveau_mem_node_cleanup(mem->mm_node);
  838. vram->put(dev, (struct nouveau_mem **)&mem->mm_node);
  839. }
  840. static int
  841. nouveau_vram_manager_new(struct ttm_mem_type_manager *man,
  842. struct ttm_buffer_object *bo,
  843. struct ttm_placement *placement,
  844. struct ttm_mem_reg *mem)
  845. {
  846. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  847. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  848. struct drm_device *dev = dev_priv->dev;
  849. struct nouveau_bo *nvbo = nouveau_bo(bo);
  850. struct nouveau_mem *node;
  851. u32 size_nc = 0;
  852. int ret;
  853. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG)
  854. size_nc = 1 << nvbo->page_shift;
  855. ret = vram->get(dev, mem->num_pages << PAGE_SHIFT,
  856. mem->page_alignment << PAGE_SHIFT, size_nc,
  857. (nvbo->tile_flags >> 8) & 0x3ff, &node);
  858. if (ret) {
  859. mem->mm_node = NULL;
  860. return (ret == -ENOSPC) ? 0 : ret;
  861. }
  862. node->page_shift = nvbo->page_shift;
  863. mem->mm_node = node;
  864. mem->start = node->offset >> PAGE_SHIFT;
  865. return 0;
  866. }
  867. void
  868. nouveau_vram_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  869. {
  870. struct nouveau_mm *mm = man->priv;
  871. struct nouveau_mm_node *r;
  872. u32 total = 0, free = 0;
  873. mutex_lock(&mm->mutex);
  874. list_for_each_entry(r, &mm->nodes, nl_entry) {
  875. printk(KERN_DEBUG "%s %d: 0x%010llx 0x%010llx\n",
  876. prefix, r->type, ((u64)r->offset << 12),
  877. (((u64)r->offset + r->length) << 12));
  878. total += r->length;
  879. if (!r->type)
  880. free += r->length;
  881. }
  882. mutex_unlock(&mm->mutex);
  883. printk(KERN_DEBUG "%s total: 0x%010llx free: 0x%010llx\n",
  884. prefix, (u64)total << 12, (u64)free << 12);
  885. printk(KERN_DEBUG "%s block: 0x%08x\n",
  886. prefix, mm->block_size << 12);
  887. }
  888. const struct ttm_mem_type_manager_func nouveau_vram_manager = {
  889. nouveau_vram_manager_init,
  890. nouveau_vram_manager_fini,
  891. nouveau_vram_manager_new,
  892. nouveau_vram_manager_del,
  893. nouveau_vram_manager_debug
  894. };
  895. static int
  896. nouveau_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  897. {
  898. return 0;
  899. }
  900. static int
  901. nouveau_gart_manager_fini(struct ttm_mem_type_manager *man)
  902. {
  903. return 0;
  904. }
  905. static void
  906. nouveau_gart_manager_del(struct ttm_mem_type_manager *man,
  907. struct ttm_mem_reg *mem)
  908. {
  909. nouveau_mem_node_cleanup(mem->mm_node);
  910. kfree(mem->mm_node);
  911. mem->mm_node = NULL;
  912. }
  913. static int
  914. nouveau_gart_manager_new(struct ttm_mem_type_manager *man,
  915. struct ttm_buffer_object *bo,
  916. struct ttm_placement *placement,
  917. struct ttm_mem_reg *mem)
  918. {
  919. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  920. struct nouveau_mem *node;
  921. if (unlikely((mem->num_pages << PAGE_SHIFT) >=
  922. dev_priv->gart_info.aper_size))
  923. return -ENOMEM;
  924. node = kzalloc(sizeof(*node), GFP_KERNEL);
  925. if (!node)
  926. return -ENOMEM;
  927. node->page_shift = 12;
  928. mem->mm_node = node;
  929. mem->start = 0;
  930. return 0;
  931. }
  932. void
  933. nouveau_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  934. {
  935. }
  936. const struct ttm_mem_type_manager_func nouveau_gart_manager = {
  937. nouveau_gart_manager_init,
  938. nouveau_gart_manager_fini,
  939. nouveau_gart_manager_new,
  940. nouveau_gart_manager_del,
  941. nouveau_gart_manager_debug
  942. };