nouveau_mem.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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 <engine/fifo.h>
  39. #include "nouveau_fence.h"
  40. /*
  41. * Cleanup everything
  42. */
  43. void
  44. nouveau_mem_vram_fini(struct drm_device *dev)
  45. {
  46. struct drm_nouveau_private *dev_priv = dev->dev_private;
  47. ttm_bo_device_release(&dev_priv->ttm.bdev);
  48. nouveau_ttm_global_release(dev_priv);
  49. if (dev_priv->fb_mtrr >= 0) {
  50. drm_mtrr_del(dev_priv->fb_mtrr,
  51. pci_resource_start(dev->pdev, 1),
  52. pci_resource_len(dev->pdev, 1), DRM_MTRR_WC);
  53. dev_priv->fb_mtrr = -1;
  54. }
  55. }
  56. void
  57. nouveau_mem_gart_fini(struct drm_device *dev)
  58. {
  59. nouveau_sgdma_takedown(dev);
  60. }
  61. int
  62. nouveau_mem_vram_init(struct drm_device *dev)
  63. {
  64. struct drm_nouveau_private *dev_priv = dev->dev_private;
  65. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  66. int ret, dma_bits;
  67. dma_bits = 32;
  68. if (dev_priv->card_type >= NV_50) {
  69. if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(40)))
  70. dma_bits = 40;
  71. } else
  72. if (0 && pci_is_pcie(dev->pdev) &&
  73. dev_priv->chipset > 0x40 &&
  74. dev_priv->chipset != 0x45) {
  75. if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(39)))
  76. dma_bits = 39;
  77. }
  78. ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  79. if (ret)
  80. return ret;
  81. ret = pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  82. if (ret) {
  83. /* Reset to default value. */
  84. pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32));
  85. }
  86. ret = nouveau_ttm_global_init(dev_priv);
  87. if (ret)
  88. return ret;
  89. ret = ttm_bo_device_init(&dev_priv->ttm.bdev,
  90. dev_priv->ttm.bo_global_ref.ref.object,
  91. &nouveau_bo_driver, DRM_FILE_PAGE_OFFSET,
  92. dma_bits <= 32 ? true : false);
  93. if (ret) {
  94. NV_ERROR(dev, "Error initialising bo driver: %d\n", ret);
  95. return ret;
  96. }
  97. dev_priv->fb_available_size = nvfb_vram_size(dev);
  98. dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
  99. if (dev_priv->fb_mappable_pages > pci_resource_len(dev->pdev, 1))
  100. dev_priv->fb_mappable_pages = pci_resource_len(dev->pdev, 1);
  101. dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
  102. dev_priv->fb_available_size -= nvimem_reserved(dev);
  103. dev_priv->fb_aper_free = dev_priv->fb_available_size;
  104. /* mappable vram */
  105. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  106. dev_priv->fb_available_size >> PAGE_SHIFT);
  107. if (ret) {
  108. NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
  109. return ret;
  110. }
  111. if (dev_priv->card_type < NV_50) {
  112. ret = nouveau_bo_new(dev, 256*1024, 0, TTM_PL_FLAG_VRAM,
  113. 0, 0, NULL, &dev_priv->vga_ram);
  114. if (ret == 0)
  115. ret = nouveau_bo_pin(dev_priv->vga_ram,
  116. TTM_PL_FLAG_VRAM);
  117. if (ret) {
  118. NV_WARN(dev, "failed to reserve VGA memory\n");
  119. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  120. }
  121. }
  122. dev_priv->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 1),
  123. pci_resource_len(dev->pdev, 1),
  124. DRM_MTRR_WC);
  125. return 0;
  126. }
  127. int
  128. nouveau_mem_gart_init(struct drm_device *dev)
  129. {
  130. struct drm_nouveau_private *dev_priv = dev->dev_private;
  131. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  132. int ret;
  133. if (!nvdrm_gart_init(dev, &dev_priv->gart_info.aper_base,
  134. &dev_priv->gart_info.aper_size))
  135. dev_priv->gart_info.type = NOUVEAU_GART_AGP;
  136. if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
  137. ret = nouveau_sgdma_init(dev);
  138. if (ret) {
  139. NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
  140. return ret;
  141. }
  142. }
  143. NV_INFO(dev, "%d MiB GART (aperture)\n",
  144. (int)(dev_priv->gart_info.aper_size >> 20));
  145. dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
  146. ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
  147. dev_priv->gart_info.aper_size >> PAGE_SHIFT);
  148. if (ret) {
  149. NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
  150. return ret;
  151. }
  152. return 0;
  153. }
  154. static int
  155. nv40_mem_timing_calc(struct drm_device *dev, u32 freq,
  156. struct nouveau_pm_tbl_entry *e, u8 len,
  157. struct nouveau_pm_memtiming *boot,
  158. struct nouveau_pm_memtiming *t)
  159. {
  160. t->reg[0] = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
  161. /* XXX: I don't trust the -1's and +1's... they must come
  162. * from somewhere! */
  163. t->reg[1] = (e->tWR + 2 + (t->tCWL - 1)) << 24 |
  164. 1 << 16 |
  165. (e->tWTR + 2 + (t->tCWL - 1)) << 8 |
  166. (e->tCL + 2 - (t->tCWL - 1));
  167. t->reg[2] = 0x20200000 |
  168. ((t->tCWL - 1) << 24 |
  169. e->tRRD << 16 |
  170. e->tRCDWR << 8 |
  171. e->tRCDRD);
  172. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x\n", t->id,
  173. t->reg[0], t->reg[1], t->reg[2]);
  174. return 0;
  175. }
  176. static int
  177. nv50_mem_timing_calc(struct drm_device *dev, u32 freq,
  178. struct nouveau_pm_tbl_entry *e, u8 len,
  179. struct nouveau_pm_memtiming *boot,
  180. struct nouveau_pm_memtiming *t)
  181. {
  182. struct bit_entry P;
  183. uint8_t unk18 = 1, unk20 = 0, unk21 = 0, tmp7_3;
  184. if (bit_table(dev, 'P', &P))
  185. return -EINVAL;
  186. switch (min(len, (u8) 22)) {
  187. case 22:
  188. unk21 = e->tUNK_21;
  189. case 21:
  190. unk20 = e->tUNK_20;
  191. case 20:
  192. if (e->tCWL > 0)
  193. t->tCWL = e->tCWL;
  194. case 19:
  195. unk18 = e->tUNK_18;
  196. break;
  197. }
  198. t->reg[0] = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
  199. t->reg[1] = (e->tWR + 2 + (t->tCWL - 1)) << 24 |
  200. max(unk18, (u8) 1) << 16 |
  201. (e->tWTR + 2 + (t->tCWL - 1)) << 8;
  202. t->reg[2] = ((t->tCWL - 1) << 24 |
  203. e->tRRD << 16 |
  204. e->tRCDWR << 8 |
  205. e->tRCDRD);
  206. t->reg[4] = e->tUNK_13 << 8 | e->tUNK_13;
  207. t->reg[5] = (e->tRFC << 24 | max(e->tRCDRD, e->tRCDWR) << 16 | e->tRP);
  208. t->reg[8] = boot->reg[8] & 0xffffff00;
  209. if (P.version == 1) {
  210. t->reg[1] |= (e->tCL + 2 - (t->tCWL - 1));
  211. t->reg[3] = (0x14 + e->tCL) << 24 |
  212. 0x16 << 16 |
  213. (e->tCL - 1) << 8 |
  214. (e->tCL - 1);
  215. t->reg[4] |= boot->reg[4] & 0xffff0000;
  216. t->reg[6] = (0x33 - t->tCWL) << 16 |
  217. t->tCWL << 8 |
  218. (0x2e + e->tCL - t->tCWL);
  219. t->reg[7] = 0x4000202 | (e->tCL - 1) << 16;
  220. /* XXX: P.version == 1 only has DDR2 and GDDR3? */
  221. if (nvfb_vram_type(dev) == NV_MEM_TYPE_DDR2) {
  222. t->reg[5] |= (e->tCL + 3) << 8;
  223. t->reg[6] |= (t->tCWL - 2) << 8;
  224. t->reg[8] |= (e->tCL - 4);
  225. } else {
  226. t->reg[5] |= (e->tCL + 2) << 8;
  227. t->reg[6] |= t->tCWL << 8;
  228. t->reg[8] |= (e->tCL - 2);
  229. }
  230. } else {
  231. t->reg[1] |= (5 + e->tCL - (t->tCWL));
  232. /* XXX: 0xb? 0x30? */
  233. t->reg[3] = (0x30 + e->tCL) << 24 |
  234. (boot->reg[3] & 0x00ff0000)|
  235. (0xb + e->tCL) << 8 |
  236. (e->tCL - 1);
  237. t->reg[4] |= (unk20 << 24 | unk21 << 16);
  238. /* XXX: +6? */
  239. t->reg[5] |= (t->tCWL + 6) << 8;
  240. t->reg[6] = (0x5a + e->tCL) << 16 |
  241. (6 - e->tCL + t->tCWL) << 8 |
  242. (0x50 + e->tCL - t->tCWL);
  243. tmp7_3 = (boot->reg[7] & 0xff000000) >> 24;
  244. t->reg[7] = (tmp7_3 << 24) |
  245. ((tmp7_3 - 6 + e->tCL) << 16) |
  246. 0x202;
  247. }
  248. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x %08x\n", t->id,
  249. t->reg[0], t->reg[1], t->reg[2], t->reg[3]);
  250. NV_DEBUG(dev, " 230: %08x %08x %08x %08x\n",
  251. t->reg[4], t->reg[5], t->reg[6], t->reg[7]);
  252. NV_DEBUG(dev, " 240: %08x\n", t->reg[8]);
  253. return 0;
  254. }
  255. static int
  256. nvc0_mem_timing_calc(struct drm_device *dev, u32 freq,
  257. struct nouveau_pm_tbl_entry *e, u8 len,
  258. struct nouveau_pm_memtiming *boot,
  259. struct nouveau_pm_memtiming *t)
  260. {
  261. if (e->tCWL > 0)
  262. t->tCWL = e->tCWL;
  263. t->reg[0] = (e->tRP << 24 | (e->tRAS & 0x7f) << 17 |
  264. e->tRFC << 8 | e->tRC);
  265. t->reg[1] = (boot->reg[1] & 0xff000000) |
  266. (e->tRCDWR & 0x0f) << 20 |
  267. (e->tRCDRD & 0x0f) << 14 |
  268. (t->tCWL << 7) |
  269. (e->tCL & 0x0f);
  270. t->reg[2] = (boot->reg[2] & 0xff0000ff) |
  271. e->tWR << 16 | e->tWTR << 8;
  272. t->reg[3] = (e->tUNK_20 & 0x1f) << 9 |
  273. (e->tUNK_21 & 0xf) << 5 |
  274. (e->tUNK_13 & 0x1f);
  275. t->reg[4] = (boot->reg[4] & 0xfff00fff) |
  276. (e->tRRD&0x1f) << 15;
  277. NV_DEBUG(dev, "Entry %d: 290: %08x %08x %08x %08x\n", t->id,
  278. t->reg[0], t->reg[1], t->reg[2], t->reg[3]);
  279. NV_DEBUG(dev, " 2a0: %08x\n", t->reg[4]);
  280. return 0;
  281. }
  282. /**
  283. * MR generation methods
  284. */
  285. static int
  286. nouveau_mem_ddr2_mr(struct drm_device *dev, u32 freq,
  287. struct nouveau_pm_tbl_entry *e, u8 len,
  288. struct nouveau_pm_memtiming *boot,
  289. struct nouveau_pm_memtiming *t)
  290. {
  291. t->drive_strength = 0;
  292. if (len < 15) {
  293. t->odt = boot->odt;
  294. } else {
  295. t->odt = e->RAM_FT1 & 0x07;
  296. }
  297. if (e->tCL >= NV_MEM_CL_DDR2_MAX) {
  298. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  299. return -ERANGE;
  300. }
  301. if (e->tWR >= NV_MEM_WR_DDR2_MAX) {
  302. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  303. return -ERANGE;
  304. }
  305. if (t->odt > 3) {
  306. NV_WARN(dev, "(%u) Invalid odt value, assuming disabled: %x",
  307. t->id, t->odt);
  308. t->odt = 0;
  309. }
  310. t->mr[0] = (boot->mr[0] & 0x100f) |
  311. (e->tCL) << 4 |
  312. (e->tWR - 1) << 9;
  313. t->mr[1] = (boot->mr[1] & 0x101fbb) |
  314. (t->odt & 0x1) << 2 |
  315. (t->odt & 0x2) << 5;
  316. NV_DEBUG(dev, "(%u) MR: %08x", t->id, t->mr[0]);
  317. return 0;
  318. }
  319. uint8_t nv_mem_wr_lut_ddr3[NV_MEM_WR_DDR3_MAX] = {
  320. 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
  321. static int
  322. nouveau_mem_ddr3_mr(struct drm_device *dev, u32 freq,
  323. struct nouveau_pm_tbl_entry *e, u8 len,
  324. struct nouveau_pm_memtiming *boot,
  325. struct nouveau_pm_memtiming *t)
  326. {
  327. u8 cl = e->tCL - 4;
  328. t->drive_strength = 0;
  329. if (len < 15) {
  330. t->odt = boot->odt;
  331. } else {
  332. t->odt = e->RAM_FT1 & 0x07;
  333. }
  334. if (e->tCL >= NV_MEM_CL_DDR3_MAX || e->tCL < 4) {
  335. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  336. return -ERANGE;
  337. }
  338. if (e->tWR >= NV_MEM_WR_DDR3_MAX || e->tWR < 4) {
  339. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  340. return -ERANGE;
  341. }
  342. if (e->tCWL < 5) {
  343. NV_WARN(dev, "(%u) Invalid tCWL: %u", t->id, e->tCWL);
  344. return -ERANGE;
  345. }
  346. t->mr[0] = (boot->mr[0] & 0x180b) |
  347. /* CAS */
  348. (cl & 0x7) << 4 |
  349. (cl & 0x8) >> 1 |
  350. (nv_mem_wr_lut_ddr3[e->tWR]) << 9;
  351. t->mr[1] = (boot->mr[1] & 0x101dbb) |
  352. (t->odt & 0x1) << 2 |
  353. (t->odt & 0x2) << 5 |
  354. (t->odt & 0x4) << 7;
  355. t->mr[2] = (boot->mr[2] & 0x20ffb7) | (e->tCWL - 5) << 3;
  356. NV_DEBUG(dev, "(%u) MR: %08x %08x", t->id, t->mr[0], t->mr[2]);
  357. return 0;
  358. }
  359. uint8_t nv_mem_cl_lut_gddr3[NV_MEM_CL_GDDR3_MAX] = {
  360. 0, 0, 0, 0, 4, 5, 6, 7, 0, 1, 2, 3, 8, 9, 10, 11};
  361. uint8_t nv_mem_wr_lut_gddr3[NV_MEM_WR_GDDR3_MAX] = {
  362. 0, 0, 0, 0, 0, 2, 3, 8, 9, 10, 11, 0, 0, 1, 1, 0, 3};
  363. static int
  364. nouveau_mem_gddr3_mr(struct drm_device *dev, u32 freq,
  365. struct nouveau_pm_tbl_entry *e, u8 len,
  366. struct nouveau_pm_memtiming *boot,
  367. struct nouveau_pm_memtiming *t)
  368. {
  369. if (len < 15) {
  370. t->drive_strength = boot->drive_strength;
  371. t->odt = boot->odt;
  372. } else {
  373. t->drive_strength = (e->RAM_FT1 & 0x30) >> 4;
  374. t->odt = e->RAM_FT1 & 0x07;
  375. }
  376. if (e->tCL >= NV_MEM_CL_GDDR3_MAX) {
  377. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  378. return -ERANGE;
  379. }
  380. if (e->tWR >= NV_MEM_WR_GDDR3_MAX) {
  381. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  382. return -ERANGE;
  383. }
  384. if (t->odt > 3) {
  385. NV_WARN(dev, "(%u) Invalid odt value, assuming autocal: %x",
  386. t->id, t->odt);
  387. t->odt = 0;
  388. }
  389. t->mr[0] = (boot->mr[0] & 0xe0b) |
  390. /* CAS */
  391. ((nv_mem_cl_lut_gddr3[e->tCL] & 0x7) << 4) |
  392. ((nv_mem_cl_lut_gddr3[e->tCL] & 0x8) >> 2);
  393. t->mr[1] = (boot->mr[1] & 0x100f40) | t->drive_strength |
  394. (t->odt << 2) |
  395. (nv_mem_wr_lut_gddr3[e->tWR] & 0xf) << 4;
  396. t->mr[2] = boot->mr[2];
  397. NV_DEBUG(dev, "(%u) MR: %08x %08x %08x", t->id,
  398. t->mr[0], t->mr[1], t->mr[2]);
  399. return 0;
  400. }
  401. static int
  402. nouveau_mem_gddr5_mr(struct drm_device *dev, u32 freq,
  403. struct nouveau_pm_tbl_entry *e, u8 len,
  404. struct nouveau_pm_memtiming *boot,
  405. struct nouveau_pm_memtiming *t)
  406. {
  407. if (len < 15) {
  408. t->drive_strength = boot->drive_strength;
  409. t->odt = boot->odt;
  410. } else {
  411. t->drive_strength = (e->RAM_FT1 & 0x30) >> 4;
  412. t->odt = e->RAM_FT1 & 0x03;
  413. }
  414. if (e->tCL >= NV_MEM_CL_GDDR5_MAX) {
  415. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  416. return -ERANGE;
  417. }
  418. if (e->tWR >= NV_MEM_WR_GDDR5_MAX) {
  419. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  420. return -ERANGE;
  421. }
  422. if (t->odt > 3) {
  423. NV_WARN(dev, "(%u) Invalid odt value, assuming autocal: %x",
  424. t->id, t->odt);
  425. t->odt = 0;
  426. }
  427. t->mr[0] = (boot->mr[0] & 0x007) |
  428. ((e->tCL - 5) << 3) |
  429. ((e->tWR - 4) << 8);
  430. t->mr[1] = (boot->mr[1] & 0x1007f0) |
  431. t->drive_strength |
  432. (t->odt << 2);
  433. NV_DEBUG(dev, "(%u) MR: %08x %08x", t->id, t->mr[0], t->mr[1]);
  434. return 0;
  435. }
  436. int
  437. nouveau_mem_timing_calc(struct drm_device *dev, u32 freq,
  438. struct nouveau_pm_memtiming *t)
  439. {
  440. struct drm_nouveau_private *dev_priv = dev->dev_private;
  441. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  442. struct nouveau_pm_memtiming *boot = &pm->boot.timing;
  443. struct nouveau_pm_tbl_entry *e;
  444. u8 ver, len, *ptr, *ramcfg;
  445. int ret;
  446. ptr = nouveau_perf_timing(dev, freq, &ver, &len);
  447. if (!ptr || ptr[0] == 0x00) {
  448. *t = *boot;
  449. return 0;
  450. }
  451. e = (struct nouveau_pm_tbl_entry *)ptr;
  452. t->tCWL = boot->tCWL;
  453. switch (dev_priv->card_type) {
  454. case NV_40:
  455. ret = nv40_mem_timing_calc(dev, freq, e, len, boot, t);
  456. break;
  457. case NV_50:
  458. ret = nv50_mem_timing_calc(dev, freq, e, len, boot, t);
  459. break;
  460. case NV_C0:
  461. case NV_D0:
  462. ret = nvc0_mem_timing_calc(dev, freq, e, len, boot, t);
  463. break;
  464. default:
  465. ret = -ENODEV;
  466. break;
  467. }
  468. switch (nvfb_vram_type(dev) * !ret) {
  469. case NV_MEM_TYPE_GDDR3:
  470. ret = nouveau_mem_gddr3_mr(dev, freq, e, len, boot, t);
  471. break;
  472. case NV_MEM_TYPE_GDDR5:
  473. ret = nouveau_mem_gddr5_mr(dev, freq, e, len, boot, t);
  474. break;
  475. case NV_MEM_TYPE_DDR2:
  476. ret = nouveau_mem_ddr2_mr(dev, freq, e, len, boot, t);
  477. break;
  478. case NV_MEM_TYPE_DDR3:
  479. ret = nouveau_mem_ddr3_mr(dev, freq, e, len, boot, t);
  480. break;
  481. default:
  482. ret = -EINVAL;
  483. break;
  484. }
  485. ramcfg = nouveau_perf_ramcfg(dev, freq, &ver, &len);
  486. if (ramcfg) {
  487. int dll_off;
  488. if (ver == 0x00)
  489. dll_off = !!(ramcfg[3] & 0x04);
  490. else
  491. dll_off = !!(ramcfg[2] & 0x40);
  492. switch (nvfb_vram_type(dev)) {
  493. case NV_MEM_TYPE_GDDR3:
  494. t->mr[1] &= ~0x00000040;
  495. t->mr[1] |= 0x00000040 * dll_off;
  496. break;
  497. default:
  498. t->mr[1] &= ~0x00000001;
  499. t->mr[1] |= 0x00000001 * dll_off;
  500. break;
  501. }
  502. }
  503. return ret;
  504. }
  505. void
  506. nouveau_mem_timing_read(struct drm_device *dev, struct nouveau_pm_memtiming *t)
  507. {
  508. struct drm_nouveau_private *dev_priv = dev->dev_private;
  509. u32 timing_base, timing_regs, mr_base;
  510. int i;
  511. if (dev_priv->card_type >= 0xC0) {
  512. timing_base = 0x10f290;
  513. mr_base = 0x10f300;
  514. } else {
  515. timing_base = 0x100220;
  516. mr_base = 0x1002c0;
  517. }
  518. t->id = -1;
  519. switch (dev_priv->card_type) {
  520. case NV_50:
  521. timing_regs = 9;
  522. break;
  523. case NV_C0:
  524. case NV_D0:
  525. timing_regs = 5;
  526. break;
  527. case NV_30:
  528. case NV_40:
  529. timing_regs = 3;
  530. break;
  531. default:
  532. timing_regs = 0;
  533. return;
  534. }
  535. for(i = 0; i < timing_regs; i++)
  536. t->reg[i] = nv_rd32(dev, timing_base + (0x04 * i));
  537. t->tCWL = 0;
  538. if (dev_priv->card_type < NV_C0) {
  539. t->tCWL = ((nv_rd32(dev, 0x100228) & 0x0f000000) >> 24) + 1;
  540. } else if (dev_priv->card_type <= NV_D0) {
  541. t->tCWL = ((nv_rd32(dev, 0x10f294) & 0x00000f80) >> 7);
  542. }
  543. t->mr[0] = nv_rd32(dev, mr_base);
  544. t->mr[1] = nv_rd32(dev, mr_base + 0x04);
  545. t->mr[2] = nv_rd32(dev, mr_base + 0x20);
  546. t->mr[3] = nv_rd32(dev, mr_base + 0x24);
  547. t->odt = 0;
  548. t->drive_strength = 0;
  549. switch (nvfb_vram_type(dev)) {
  550. case NV_MEM_TYPE_DDR3:
  551. t->odt |= (t->mr[1] & 0x200) >> 7;
  552. case NV_MEM_TYPE_DDR2:
  553. t->odt |= (t->mr[1] & 0x04) >> 2 |
  554. (t->mr[1] & 0x40) >> 5;
  555. break;
  556. case NV_MEM_TYPE_GDDR3:
  557. case NV_MEM_TYPE_GDDR5:
  558. t->drive_strength = t->mr[1] & 0x03;
  559. t->odt = (t->mr[1] & 0x0c) >> 2;
  560. break;
  561. default:
  562. break;
  563. }
  564. }
  565. int
  566. nouveau_mem_exec(struct nouveau_mem_exec_func *exec,
  567. struct nouveau_pm_level *perflvl)
  568. {
  569. struct drm_nouveau_private *dev_priv = exec->dev->dev_private;
  570. struct nouveau_pm_memtiming *info = &perflvl->timing;
  571. u32 tMRD = 1000, tCKSRE = 0, tCKSRX = 0, tXS = 0, tDLLK = 0;
  572. u32 mr[3] = { info->mr[0], info->mr[1], info->mr[2] };
  573. u32 mr1_dlloff;
  574. switch (nvfb_vram_type(dev_priv->dev)) {
  575. case NV_MEM_TYPE_DDR2:
  576. tDLLK = 2000;
  577. mr1_dlloff = 0x00000001;
  578. break;
  579. case NV_MEM_TYPE_DDR3:
  580. tDLLK = 12000;
  581. tCKSRE = 2000;
  582. tXS = 1000;
  583. mr1_dlloff = 0x00000001;
  584. break;
  585. case NV_MEM_TYPE_GDDR3:
  586. tDLLK = 40000;
  587. mr1_dlloff = 0x00000040;
  588. break;
  589. default:
  590. NV_ERROR(exec->dev, "cannot reclock unsupported memtype\n");
  591. return -ENODEV;
  592. }
  593. /* fetch current MRs */
  594. switch (nvfb_vram_type(dev_priv->dev)) {
  595. case NV_MEM_TYPE_GDDR3:
  596. case NV_MEM_TYPE_DDR3:
  597. mr[2] = exec->mrg(exec, 2);
  598. default:
  599. mr[1] = exec->mrg(exec, 1);
  600. mr[0] = exec->mrg(exec, 0);
  601. break;
  602. }
  603. /* DLL 'on' -> DLL 'off' mode, disable before entering self-refresh */
  604. if (!(mr[1] & mr1_dlloff) && (info->mr[1] & mr1_dlloff)) {
  605. exec->precharge(exec);
  606. exec->mrs (exec, 1, mr[1] | mr1_dlloff);
  607. exec->wait(exec, tMRD);
  608. }
  609. /* enter self-refresh mode */
  610. exec->precharge(exec);
  611. exec->refresh(exec);
  612. exec->refresh(exec);
  613. exec->refresh_auto(exec, false);
  614. exec->refresh_self(exec, true);
  615. exec->wait(exec, tCKSRE);
  616. /* modify input clock frequency */
  617. exec->clock_set(exec);
  618. /* exit self-refresh mode */
  619. exec->wait(exec, tCKSRX);
  620. exec->precharge(exec);
  621. exec->refresh_self(exec, false);
  622. exec->refresh_auto(exec, true);
  623. exec->wait(exec, tXS);
  624. exec->wait(exec, tXS);
  625. /* update MRs */
  626. if (mr[2] != info->mr[2]) {
  627. exec->mrs (exec, 2, info->mr[2]);
  628. exec->wait(exec, tMRD);
  629. }
  630. if (mr[1] != info->mr[1]) {
  631. /* need to keep DLL off until later, at least on GDDR3 */
  632. exec->mrs (exec, 1, info->mr[1] | (mr[1] & mr1_dlloff));
  633. exec->wait(exec, tMRD);
  634. }
  635. if (mr[0] != info->mr[0]) {
  636. exec->mrs (exec, 0, info->mr[0]);
  637. exec->wait(exec, tMRD);
  638. }
  639. /* update PFB timing registers */
  640. exec->timing_set(exec);
  641. /* DLL (enable + ) reset */
  642. if (!(info->mr[1] & mr1_dlloff)) {
  643. if (mr[1] & mr1_dlloff) {
  644. exec->mrs (exec, 1, info->mr[1]);
  645. exec->wait(exec, tMRD);
  646. }
  647. exec->mrs (exec, 0, info->mr[0] | 0x00000100);
  648. exec->wait(exec, tMRD);
  649. exec->mrs (exec, 0, info->mr[0] | 0x00000000);
  650. exec->wait(exec, tMRD);
  651. exec->wait(exec, tDLLK);
  652. if (nvfb_vram_type(dev_priv->dev) == NV_MEM_TYPE_GDDR3)
  653. exec->precharge(exec);
  654. }
  655. return 0;
  656. }
  657. int
  658. nouveau_mem_vbios_type(struct drm_device *dev)
  659. {
  660. struct bit_entry M;
  661. u8 ramcfg = (nv_rd32(dev, 0x101000) & 0x0000003c) >> 2;
  662. if (!bit_table(dev, 'M', &M) || M.version != 2 || M.length < 5) {
  663. u8 *table = ROMPTR(dev, M.data[3]);
  664. if (table && table[0] == 0x10 && ramcfg < table[3]) {
  665. u8 *entry = table + table[1] + (ramcfg * table[2]);
  666. switch (entry[0] & 0x0f) {
  667. case 0: return NV_MEM_TYPE_DDR2;
  668. case 1: return NV_MEM_TYPE_DDR3;
  669. case 2: return NV_MEM_TYPE_GDDR3;
  670. case 3: return NV_MEM_TYPE_GDDR5;
  671. default:
  672. break;
  673. }
  674. }
  675. }
  676. return NV_MEM_TYPE_UNKNOWN;
  677. }