nouveau_calc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Copyright 1993-2003 NVIDIA, Corporation
  3. * Copyright 2007-2009 Stuart Bennett
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  20. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #include "drmP.h"
  24. #include "nouveau_drv.h"
  25. #include "nouveau_hw.h"
  26. /****************************************************************************\
  27. * *
  28. * The video arbitration routines calculate some "magic" numbers. Fixes *
  29. * the snow seen when accessing the framebuffer without it. *
  30. * It just works (I hope). *
  31. * *
  32. \****************************************************************************/
  33. struct nv_fifo_info {
  34. int lwm;
  35. int burst;
  36. };
  37. struct nv_sim_state {
  38. int pclk_khz;
  39. int mclk_khz;
  40. int nvclk_khz;
  41. int bpp;
  42. int mem_page_miss;
  43. int mem_latency;
  44. int memory_type;
  45. int memory_width;
  46. int two_heads;
  47. };
  48. static void
  49. nv04_calc_arb(struct nv_fifo_info *fifo, struct nv_sim_state *arb)
  50. {
  51. int pagemiss, cas, width, bpp;
  52. int nvclks, mclks, pclks, crtpagemiss;
  53. int found, mclk_extra, mclk_loop, cbs, m1, p1;
  54. int mclk_freq, pclk_freq, nvclk_freq;
  55. int us_m, us_n, us_p, crtc_drain_rate;
  56. int cpm_us, us_crt, clwm;
  57. pclk_freq = arb->pclk_khz;
  58. mclk_freq = arb->mclk_khz;
  59. nvclk_freq = arb->nvclk_khz;
  60. pagemiss = arb->mem_page_miss;
  61. cas = arb->mem_latency;
  62. width = arb->memory_width >> 6;
  63. bpp = arb->bpp;
  64. cbs = 128;
  65. pclks = 2;
  66. nvclks = 10;
  67. mclks = 13 + cas;
  68. mclk_extra = 3;
  69. found = 0;
  70. while (!found) {
  71. found = 1;
  72. mclk_loop = mclks + mclk_extra;
  73. us_m = mclk_loop * 1000 * 1000 / mclk_freq;
  74. us_n = nvclks * 1000 * 1000 / nvclk_freq;
  75. us_p = nvclks * 1000 * 1000 / pclk_freq;
  76. crtc_drain_rate = pclk_freq * bpp / 8;
  77. crtpagemiss = 2;
  78. crtpagemiss += 1;
  79. cpm_us = crtpagemiss * pagemiss * 1000 * 1000 / mclk_freq;
  80. us_crt = cpm_us + us_m + us_n + us_p;
  81. clwm = us_crt * crtc_drain_rate / (1000 * 1000);
  82. clwm++;
  83. m1 = clwm + cbs - 512;
  84. p1 = m1 * pclk_freq / mclk_freq;
  85. p1 = p1 * bpp / 8;
  86. if ((p1 < m1 && m1 > 0) || clwm > 519) {
  87. found = !mclk_extra;
  88. mclk_extra--;
  89. }
  90. if (clwm < 384)
  91. clwm = 384;
  92. fifo->lwm = clwm;
  93. fifo->burst = cbs;
  94. }
  95. }
  96. static void
  97. nv10_calc_arb(struct nv_fifo_info *fifo, struct nv_sim_state *arb)
  98. {
  99. int fill_rate, drain_rate;
  100. int pclks, nvclks, mclks, xclks;
  101. int pclk_freq, nvclk_freq, mclk_freq;
  102. int fill_lat, extra_lat;
  103. int max_burst_o, max_burst_l;
  104. int fifo_len, min_lwm, max_lwm;
  105. const int burst_lat = 80; /* Maximum allowable latency due
  106. * to the CRTC FIFO burst. (ns) */
  107. pclk_freq = arb->pclk_khz;
  108. nvclk_freq = arb->nvclk_khz;
  109. mclk_freq = arb->mclk_khz;
  110. fill_rate = mclk_freq * arb->memory_width / 8; /* kB/s */
  111. drain_rate = pclk_freq * arb->bpp / 8; /* kB/s */
  112. fifo_len = arb->two_heads ? 1536 : 1024; /* B */
  113. /* Fixed FIFO refill latency. */
  114. pclks = 4; /* lwm detect. */
  115. nvclks = 3 /* lwm -> sync. */
  116. + 2 /* fbi bus cycles (1 req + 1 busy) */
  117. + 1 /* 2 edge sync. may be very close to edge so
  118. * just put one. */
  119. + 1 /* fbi_d_rdv_n */
  120. + 1 /* Fbi_d_rdata */
  121. + 1; /* crtfifo load */
  122. mclks = 1 /* 2 edge sync. may be very close to edge so
  123. * just put one. */
  124. + 1 /* arb_hp_req */
  125. + 5 /* tiling pipeline */
  126. + 2 /* latency fifo */
  127. + 2 /* memory request to fbio block */
  128. + 7; /* data returned from fbio block */
  129. /* Need to accumulate 256 bits for read */
  130. mclks += (arb->memory_type == 0 ? 2 : 1)
  131. * arb->memory_width / 32;
  132. fill_lat = mclks * 1000 * 1000 / mclk_freq /* minimum mclk latency */
  133. + nvclks * 1000 * 1000 / nvclk_freq /* nvclk latency */
  134. + pclks * 1000 * 1000 / pclk_freq; /* pclk latency */
  135. /* Conditional FIFO refill latency. */
  136. xclks = 2 * arb->mem_page_miss + mclks /* Extra latency due to
  137. * the overlay. */
  138. + 2 * arb->mem_page_miss /* Extra pagemiss latency. */
  139. + (arb->bpp == 32 ? 8 : 4); /* Margin of error. */
  140. extra_lat = xclks * 1000 * 1000 / mclk_freq;
  141. if (arb->two_heads)
  142. /* Account for another CRTC. */
  143. extra_lat += fill_lat + extra_lat + burst_lat;
  144. /* FIFO burst */
  145. /* Max burst not leading to overflows. */
  146. max_burst_o = (1 + fifo_len - extra_lat * drain_rate / (1000 * 1000))
  147. * (fill_rate / 1000) / ((fill_rate - drain_rate) / 1000);
  148. fifo->burst = min(max_burst_o, 1024);
  149. /* Max burst value with an acceptable latency. */
  150. max_burst_l = burst_lat * fill_rate / (1000 * 1000);
  151. fifo->burst = min(max_burst_l, fifo->burst);
  152. fifo->burst = rounddown_pow_of_two(fifo->burst);
  153. /* FIFO low watermark */
  154. min_lwm = (fill_lat + extra_lat) * drain_rate / (1000 * 1000) + 1;
  155. max_lwm = fifo_len - fifo->burst
  156. + fill_lat * drain_rate / (1000 * 1000)
  157. + fifo->burst * drain_rate / fill_rate;
  158. fifo->lwm = min_lwm + 10 * (max_lwm - min_lwm) / 100; /* Empirical. */
  159. }
  160. static void
  161. nv04_update_arb(struct drm_device *dev, int VClk, int bpp,
  162. int *burst, int *lwm)
  163. {
  164. struct drm_nouveau_private *dev_priv = dev->dev_private;
  165. struct nv_fifo_info fifo_data;
  166. struct nv_sim_state sim_data;
  167. int MClk = nouveau_hw_get_clock(dev, MPLL);
  168. int NVClk = nouveau_hw_get_clock(dev, NVPLL);
  169. uint32_t cfg1 = nvReadFB(dev, NV_PFB_CFG1);
  170. sim_data.pclk_khz = VClk;
  171. sim_data.mclk_khz = MClk;
  172. sim_data.nvclk_khz = NVClk;
  173. sim_data.bpp = bpp;
  174. sim_data.two_heads = nv_two_heads(dev);
  175. if ((dev->pci_device & 0xffff) == 0x01a0 /*CHIPSET_NFORCE*/ ||
  176. (dev->pci_device & 0xffff) == 0x01f0 /*CHIPSET_NFORCE2*/) {
  177. uint32_t type;
  178. pci_read_config_dword(pci_get_bus_and_slot(0, 1), 0x7c, &type);
  179. sim_data.memory_type = (type >> 12) & 1;
  180. sim_data.memory_width = 64;
  181. sim_data.mem_latency = 3;
  182. sim_data.mem_page_miss = 10;
  183. } else {
  184. sim_data.memory_type = nvReadFB(dev, NV_PFB_CFG0) & 0x1;
  185. sim_data.memory_width = (nvReadEXTDEV(dev, NV_PEXTDEV_BOOT_0) & 0x10) ? 128 : 64;
  186. sim_data.mem_latency = cfg1 & 0xf;
  187. sim_data.mem_page_miss = ((cfg1 >> 4) & 0xf) + ((cfg1 >> 31) & 0x1);
  188. }
  189. if (dev_priv->card_type == NV_04)
  190. nv04_calc_arb(&fifo_data, &sim_data);
  191. else
  192. nv10_calc_arb(&fifo_data, &sim_data);
  193. *burst = ilog2(fifo_data.burst >> 4);
  194. *lwm = fifo_data.lwm >> 3;
  195. }
  196. static void
  197. nv30_update_arb(int *burst, int *lwm)
  198. {
  199. unsigned int fifo_size, burst_size, graphics_lwm;
  200. fifo_size = 2048;
  201. burst_size = 512;
  202. graphics_lwm = fifo_size - burst_size;
  203. *burst = ilog2(burst_size >> 5);
  204. *lwm = graphics_lwm >> 3;
  205. }
  206. void
  207. nouveau_calc_arb(struct drm_device *dev, int vclk, int bpp, int *burst, int *lwm)
  208. {
  209. struct drm_nouveau_private *dev_priv = dev->dev_private;
  210. if (dev_priv->card_type < NV_30)
  211. nv04_update_arb(dev, vclk, bpp, burst, lwm);
  212. else if ((dev->pci_device & 0xfff0) == 0x0240 /*CHIPSET_C51*/ ||
  213. (dev->pci_device & 0xfff0) == 0x03d0 /*CHIPSET_C512*/) {
  214. *burst = 128;
  215. *lwm = 0x0480;
  216. } else
  217. nv30_update_arb(burst, lwm);
  218. }
  219. static int
  220. getMNP_single(struct drm_device *dev, struct pll_lims *pll_lim, int clk,
  221. struct nouveau_pll_vals *bestpv)
  222. {
  223. /* Find M, N and P for a single stage PLL
  224. *
  225. * Note that some bioses (NV3x) have lookup tables of precomputed MNP
  226. * values, but we're too lazy to use those atm
  227. *
  228. * "clk" parameter in kHz
  229. * returns calculated clock
  230. */
  231. struct drm_nouveau_private *dev_priv = dev->dev_private;
  232. int cv = dev_priv->vbios->chip_version;
  233. int minvco = pll_lim->vco1.minfreq, maxvco = pll_lim->vco1.maxfreq;
  234. int minM = pll_lim->vco1.min_m, maxM = pll_lim->vco1.max_m;
  235. int minN = pll_lim->vco1.min_n, maxN = pll_lim->vco1.max_n;
  236. int minU = pll_lim->vco1.min_inputfreq;
  237. int maxU = pll_lim->vco1.max_inputfreq;
  238. int minP = pll_lim->max_p ? pll_lim->min_p : 0;
  239. int maxP = pll_lim->max_p ? pll_lim->max_p : pll_lim->max_usable_log2p;
  240. int crystal = pll_lim->refclk;
  241. int M, N, thisP, P;
  242. int clkP, calcclk;
  243. int delta, bestdelta = INT_MAX;
  244. int bestclk = 0;
  245. /* this division verified for nv20, nv18, nv28 (Haiku), and nv34 */
  246. /* possibly correlated with introduction of 27MHz crystal */
  247. if (dev_priv->card_type < NV_50) {
  248. if (cv < 0x17 || cv == 0x1a || cv == 0x20) {
  249. if (clk > 250000)
  250. maxM = 6;
  251. if (clk > 340000)
  252. maxM = 2;
  253. } else if (cv < 0x40) {
  254. if (clk > 150000)
  255. maxM = 6;
  256. if (clk > 200000)
  257. maxM = 4;
  258. if (clk > 340000)
  259. maxM = 2;
  260. }
  261. }
  262. P = pll_lim->max_p ? maxP : (1 << maxP);
  263. if ((clk * P) < minvco) {
  264. minvco = clk * maxP;
  265. maxvco = minvco * 2;
  266. }
  267. if (clk + clk/200 > maxvco) /* +0.5% */
  268. maxvco = clk + clk/200;
  269. /* NV34 goes maxlog2P->0, NV20 goes 0->maxlog2P */
  270. for (thisP = minP; thisP <= maxP; thisP++) {
  271. P = pll_lim->max_p ? thisP : (1 << thisP);
  272. clkP = clk * P;
  273. if (clkP < minvco)
  274. continue;
  275. if (clkP > maxvco)
  276. return bestclk;
  277. for (M = minM; M <= maxM; M++) {
  278. if (crystal/M < minU)
  279. return bestclk;
  280. if (crystal/M > maxU)
  281. continue;
  282. /* add crystal/2 to round better */
  283. N = (clkP * M + crystal/2) / crystal;
  284. if (N < minN)
  285. continue;
  286. if (N > maxN)
  287. break;
  288. /* more rounding additions */
  289. calcclk = ((N * crystal + P/2) / P + M/2) / M;
  290. delta = abs(calcclk - clk);
  291. /* we do an exhaustive search rather than terminating
  292. * on an optimality condition...
  293. */
  294. if (delta < bestdelta) {
  295. bestdelta = delta;
  296. bestclk = calcclk;
  297. bestpv->N1 = N;
  298. bestpv->M1 = M;
  299. bestpv->log2P = thisP;
  300. if (delta == 0) /* except this one */
  301. return bestclk;
  302. }
  303. }
  304. }
  305. return bestclk;
  306. }
  307. static int
  308. getMNP_double(struct drm_device *dev, struct pll_lims *pll_lim, int clk,
  309. struct nouveau_pll_vals *bestpv)
  310. {
  311. /* Find M, N and P for a two stage PLL
  312. *
  313. * Note that some bioses (NV30+) have lookup tables of precomputed MNP
  314. * values, but we're too lazy to use those atm
  315. *
  316. * "clk" parameter in kHz
  317. * returns calculated clock
  318. */
  319. struct drm_nouveau_private *dev_priv = dev->dev_private;
  320. int chip_version = dev_priv->vbios->chip_version;
  321. int minvco1 = pll_lim->vco1.minfreq, maxvco1 = pll_lim->vco1.maxfreq;
  322. int minvco2 = pll_lim->vco2.minfreq, maxvco2 = pll_lim->vco2.maxfreq;
  323. int minU1 = pll_lim->vco1.min_inputfreq, minU2 = pll_lim->vco2.min_inputfreq;
  324. int maxU1 = pll_lim->vco1.max_inputfreq, maxU2 = pll_lim->vco2.max_inputfreq;
  325. int minM1 = pll_lim->vco1.min_m, maxM1 = pll_lim->vco1.max_m;
  326. int minN1 = pll_lim->vco1.min_n, maxN1 = pll_lim->vco1.max_n;
  327. int minM2 = pll_lim->vco2.min_m, maxM2 = pll_lim->vco2.max_m;
  328. int minN2 = pll_lim->vco2.min_n, maxN2 = pll_lim->vco2.max_n;
  329. int maxlog2P = pll_lim->max_usable_log2p;
  330. int crystal = pll_lim->refclk;
  331. bool fixedgain2 = (minM2 == maxM2 && minN2 == maxN2);
  332. int M1, N1, M2, N2, log2P;
  333. int clkP, calcclk1, calcclk2, calcclkout;
  334. int delta, bestdelta = INT_MAX;
  335. int bestclk = 0;
  336. int vco2 = (maxvco2 - maxvco2/200) / 2;
  337. for (log2P = 0; clk && log2P < maxlog2P && clk <= (vco2 >> log2P); log2P++)
  338. ;
  339. clkP = clk << log2P;
  340. if (maxvco2 < clk + clk/200) /* +0.5% */
  341. maxvco2 = clk + clk/200;
  342. for (M1 = minM1; M1 <= maxM1; M1++) {
  343. if (crystal/M1 < minU1)
  344. return bestclk;
  345. if (crystal/M1 > maxU1)
  346. continue;
  347. for (N1 = minN1; N1 <= maxN1; N1++) {
  348. calcclk1 = crystal * N1 / M1;
  349. if (calcclk1 < minvco1)
  350. continue;
  351. if (calcclk1 > maxvco1)
  352. break;
  353. for (M2 = minM2; M2 <= maxM2; M2++) {
  354. if (calcclk1/M2 < minU2)
  355. break;
  356. if (calcclk1/M2 > maxU2)
  357. continue;
  358. /* add calcclk1/2 to round better */
  359. N2 = (clkP * M2 + calcclk1/2) / calcclk1;
  360. if (N2 < minN2)
  361. continue;
  362. if (N2 > maxN2)
  363. break;
  364. if (!fixedgain2) {
  365. if (chip_version < 0x60)
  366. if (N2/M2 < 4 || N2/M2 > 10)
  367. continue;
  368. calcclk2 = calcclk1 * N2 / M2;
  369. if (calcclk2 < minvco2)
  370. break;
  371. if (calcclk2 > maxvco2)
  372. continue;
  373. } else
  374. calcclk2 = calcclk1;
  375. calcclkout = calcclk2 >> log2P;
  376. delta = abs(calcclkout - clk);
  377. /* we do an exhaustive search rather than terminating
  378. * on an optimality condition...
  379. */
  380. if (delta < bestdelta) {
  381. bestdelta = delta;
  382. bestclk = calcclkout;
  383. bestpv->N1 = N1;
  384. bestpv->M1 = M1;
  385. bestpv->N2 = N2;
  386. bestpv->M2 = M2;
  387. bestpv->log2P = log2P;
  388. if (delta == 0) /* except this one */
  389. return bestclk;
  390. }
  391. }
  392. }
  393. }
  394. return bestclk;
  395. }
  396. int
  397. nouveau_calc_pll_mnp(struct drm_device *dev, struct pll_lims *pll_lim, int clk,
  398. struct nouveau_pll_vals *pv)
  399. {
  400. int outclk;
  401. if (!pll_lim->vco2.maxfreq)
  402. outclk = getMNP_single(dev, pll_lim, clk, pv);
  403. else
  404. outclk = getMNP_double(dev, pll_lim, clk, pv);
  405. if (!outclk)
  406. NV_ERROR(dev, "Could not find a compatible set of PLL values\n");
  407. return outclk;
  408. }