Path: blob/master/drivers/gpu/drm/nouveau/nouveau_calc.c
15112 views
/*1* Copyright 1993-2003 NVIDIA, Corporation2* Copyright 2007-2009 Stuart Bennett3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be included in12* all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,18* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF19* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*/2223#include "drmP.h"24#include "nouveau_drv.h"25#include "nouveau_hw.h"2627/****************************************************************************\28* *29* The video arbitration routines calculate some "magic" numbers. Fixes *30* the snow seen when accessing the framebuffer without it. *31* It just works (I hope). *32* *33\****************************************************************************/3435struct nv_fifo_info {36int lwm;37int burst;38};3940struct nv_sim_state {41int pclk_khz;42int mclk_khz;43int nvclk_khz;44int bpp;45int mem_page_miss;46int mem_latency;47int memory_type;48int memory_width;49int two_heads;50};5152static void53nv04_calc_arb(struct nv_fifo_info *fifo, struct nv_sim_state *arb)54{55int pagemiss, cas, width, bpp;56int nvclks, mclks, pclks, crtpagemiss;57int found, mclk_extra, mclk_loop, cbs, m1, p1;58int mclk_freq, pclk_freq, nvclk_freq;59int us_m, us_n, us_p, crtc_drain_rate;60int cpm_us, us_crt, clwm;6162pclk_freq = arb->pclk_khz;63mclk_freq = arb->mclk_khz;64nvclk_freq = arb->nvclk_khz;65pagemiss = arb->mem_page_miss;66cas = arb->mem_latency;67width = arb->memory_width >> 6;68bpp = arb->bpp;69cbs = 128;7071pclks = 2;72nvclks = 10;73mclks = 13 + cas;74mclk_extra = 3;75found = 0;7677while (!found) {78found = 1;7980mclk_loop = mclks + mclk_extra;81us_m = mclk_loop * 1000 * 1000 / mclk_freq;82us_n = nvclks * 1000 * 1000 / nvclk_freq;83us_p = nvclks * 1000 * 1000 / pclk_freq;8485crtc_drain_rate = pclk_freq * bpp / 8;86crtpagemiss = 2;87crtpagemiss += 1;88cpm_us = crtpagemiss * pagemiss * 1000 * 1000 / mclk_freq;89us_crt = cpm_us + us_m + us_n + us_p;90clwm = us_crt * crtc_drain_rate / (1000 * 1000);91clwm++;9293m1 = clwm + cbs - 512;94p1 = m1 * pclk_freq / mclk_freq;95p1 = p1 * bpp / 8;96if ((p1 < m1 && m1 > 0) || clwm > 519) {97found = !mclk_extra;98mclk_extra--;99}100if (clwm < 384)101clwm = 384;102103fifo->lwm = clwm;104fifo->burst = cbs;105}106}107108static void109nv10_calc_arb(struct nv_fifo_info *fifo, struct nv_sim_state *arb)110{111int fill_rate, drain_rate;112int pclks, nvclks, mclks, xclks;113int pclk_freq, nvclk_freq, mclk_freq;114int fill_lat, extra_lat;115int max_burst_o, max_burst_l;116int fifo_len, min_lwm, max_lwm;117const int burst_lat = 80; /* Maximum allowable latency due118* to the CRTC FIFO burst. (ns) */119120pclk_freq = arb->pclk_khz;121nvclk_freq = arb->nvclk_khz;122mclk_freq = arb->mclk_khz;123124fill_rate = mclk_freq * arb->memory_width / 8; /* kB/s */125drain_rate = pclk_freq * arb->bpp / 8; /* kB/s */126127fifo_len = arb->two_heads ? 1536 : 1024; /* B */128129/* Fixed FIFO refill latency. */130131pclks = 4; /* lwm detect. */132133nvclks = 3 /* lwm -> sync. */134+ 2 /* fbi bus cycles (1 req + 1 busy) */135+ 1 /* 2 edge sync. may be very close to edge so136* just put one. */137+ 1 /* fbi_d_rdv_n */138+ 1 /* Fbi_d_rdata */139+ 1; /* crtfifo load */140141mclks = 1 /* 2 edge sync. may be very close to edge so142* just put one. */143+ 1 /* arb_hp_req */144+ 5 /* tiling pipeline */145+ 2 /* latency fifo */146+ 2 /* memory request to fbio block */147+ 7; /* data returned from fbio block */148149/* Need to accumulate 256 bits for read */150mclks += (arb->memory_type == 0 ? 2 : 1)151* arb->memory_width / 32;152153fill_lat = mclks * 1000 * 1000 / mclk_freq /* minimum mclk latency */154+ nvclks * 1000 * 1000 / nvclk_freq /* nvclk latency */155+ pclks * 1000 * 1000 / pclk_freq; /* pclk latency */156157/* Conditional FIFO refill latency. */158159xclks = 2 * arb->mem_page_miss + mclks /* Extra latency due to160* the overlay. */161+ 2 * arb->mem_page_miss /* Extra pagemiss latency. */162+ (arb->bpp == 32 ? 8 : 4); /* Margin of error. */163164extra_lat = xclks * 1000 * 1000 / mclk_freq;165166if (arb->two_heads)167/* Account for another CRTC. */168extra_lat += fill_lat + extra_lat + burst_lat;169170/* FIFO burst */171172/* Max burst not leading to overflows. */173max_burst_o = (1 + fifo_len - extra_lat * drain_rate / (1000 * 1000))174* (fill_rate / 1000) / ((fill_rate - drain_rate) / 1000);175fifo->burst = min(max_burst_o, 1024);176177/* Max burst value with an acceptable latency. */178max_burst_l = burst_lat * fill_rate / (1000 * 1000);179fifo->burst = min(max_burst_l, fifo->burst);180181fifo->burst = rounddown_pow_of_two(fifo->burst);182183/* FIFO low watermark */184185min_lwm = (fill_lat + extra_lat) * drain_rate / (1000 * 1000) + 1;186max_lwm = fifo_len - fifo->burst187+ fill_lat * drain_rate / (1000 * 1000)188+ fifo->burst * drain_rate / fill_rate;189190fifo->lwm = min_lwm + 10 * (max_lwm - min_lwm) / 100; /* Empirical. */191}192193static void194nv04_update_arb(struct drm_device *dev, int VClk, int bpp,195int *burst, int *lwm)196{197struct drm_nouveau_private *dev_priv = dev->dev_private;198struct nv_fifo_info fifo_data;199struct nv_sim_state sim_data;200int MClk = nouveau_hw_get_clock(dev, PLL_MEMORY);201int NVClk = nouveau_hw_get_clock(dev, PLL_CORE);202uint32_t cfg1 = nvReadFB(dev, NV04_PFB_CFG1);203204sim_data.pclk_khz = VClk;205sim_data.mclk_khz = MClk;206sim_data.nvclk_khz = NVClk;207sim_data.bpp = bpp;208sim_data.two_heads = nv_two_heads(dev);209if ((dev->pci_device & 0xffff) == 0x01a0 /*CHIPSET_NFORCE*/ ||210(dev->pci_device & 0xffff) == 0x01f0 /*CHIPSET_NFORCE2*/) {211uint32_t type;212213pci_read_config_dword(pci_get_bus_and_slot(0, 1), 0x7c, &type);214215sim_data.memory_type = (type >> 12) & 1;216sim_data.memory_width = 64;217sim_data.mem_latency = 3;218sim_data.mem_page_miss = 10;219} else {220sim_data.memory_type = nvReadFB(dev, NV04_PFB_CFG0) & 0x1;221sim_data.memory_width = (nvReadEXTDEV(dev, NV_PEXTDEV_BOOT_0) & 0x10) ? 128 : 64;222sim_data.mem_latency = cfg1 & 0xf;223sim_data.mem_page_miss = ((cfg1 >> 4) & 0xf) + ((cfg1 >> 31) & 0x1);224}225226if (dev_priv->card_type == NV_04)227nv04_calc_arb(&fifo_data, &sim_data);228else229nv10_calc_arb(&fifo_data, &sim_data);230231*burst = ilog2(fifo_data.burst >> 4);232*lwm = fifo_data.lwm >> 3;233}234235static void236nv20_update_arb(int *burst, int *lwm)237{238unsigned int fifo_size, burst_size, graphics_lwm;239240fifo_size = 2048;241burst_size = 512;242graphics_lwm = fifo_size - burst_size;243244*burst = ilog2(burst_size >> 5);245*lwm = graphics_lwm >> 3;246}247248void249nouveau_calc_arb(struct drm_device *dev, int vclk, int bpp, int *burst, int *lwm)250{251struct drm_nouveau_private *dev_priv = dev->dev_private;252253if (dev_priv->card_type < NV_20)254nv04_update_arb(dev, vclk, bpp, burst, lwm);255else if ((dev->pci_device & 0xfff0) == 0x0240 /*CHIPSET_C51*/ ||256(dev->pci_device & 0xfff0) == 0x03d0 /*CHIPSET_C512*/) {257*burst = 128;258*lwm = 0x0480;259} else260nv20_update_arb(burst, lwm);261}262263static int264getMNP_single(struct drm_device *dev, struct pll_lims *pll_lim, int clk,265struct nouveau_pll_vals *bestpv)266{267/* Find M, N and P for a single stage PLL268*269* Note that some bioses (NV3x) have lookup tables of precomputed MNP270* values, but we're too lazy to use those atm271*272* "clk" parameter in kHz273* returns calculated clock274*/275struct drm_nouveau_private *dev_priv = dev->dev_private;276int cv = dev_priv->vbios.chip_version;277int minvco = pll_lim->vco1.minfreq, maxvco = pll_lim->vco1.maxfreq;278int minM = pll_lim->vco1.min_m, maxM = pll_lim->vco1.max_m;279int minN = pll_lim->vco1.min_n, maxN = pll_lim->vco1.max_n;280int minU = pll_lim->vco1.min_inputfreq;281int maxU = pll_lim->vco1.max_inputfreq;282int minP = pll_lim->max_p ? pll_lim->min_p : 0;283int maxP = pll_lim->max_p ? pll_lim->max_p : pll_lim->max_usable_log2p;284int crystal = pll_lim->refclk;285int M, N, thisP, P;286int clkP, calcclk;287int delta, bestdelta = INT_MAX;288int bestclk = 0;289290/* this division verified for nv20, nv18, nv28 (Haiku), and nv34 */291/* possibly correlated with introduction of 27MHz crystal */292if (dev_priv->card_type < NV_50) {293if (cv < 0x17 || cv == 0x1a || cv == 0x20) {294if (clk > 250000)295maxM = 6;296if (clk > 340000)297maxM = 2;298} else if (cv < 0x40) {299if (clk > 150000)300maxM = 6;301if (clk > 200000)302maxM = 4;303if (clk > 340000)304maxM = 2;305}306}307308P = pll_lim->max_p ? maxP : (1 << maxP);309if ((clk * P) < minvco) {310minvco = clk * maxP;311maxvco = minvco * 2;312}313314if (clk + clk/200 > maxvco) /* +0.5% */315maxvco = clk + clk/200;316317/* NV34 goes maxlog2P->0, NV20 goes 0->maxlog2P */318for (thisP = minP; thisP <= maxP; thisP++) {319P = pll_lim->max_p ? thisP : (1 << thisP);320clkP = clk * P;321322if (clkP < minvco)323continue;324if (clkP > maxvco)325return bestclk;326327for (M = minM; M <= maxM; M++) {328if (crystal/M < minU)329return bestclk;330if (crystal/M > maxU)331continue;332333/* add crystal/2 to round better */334N = (clkP * M + crystal/2) / crystal;335336if (N < minN)337continue;338if (N > maxN)339break;340341/* more rounding additions */342calcclk = ((N * crystal + P/2) / P + M/2) / M;343delta = abs(calcclk - clk);344/* we do an exhaustive search rather than terminating345* on an optimality condition...346*/347if (delta < bestdelta) {348bestdelta = delta;349bestclk = calcclk;350bestpv->N1 = N;351bestpv->M1 = M;352bestpv->log2P = thisP;353if (delta == 0) /* except this one */354return bestclk;355}356}357}358359return bestclk;360}361362static int363getMNP_double(struct drm_device *dev, struct pll_lims *pll_lim, int clk,364struct nouveau_pll_vals *bestpv)365{366/* Find M, N and P for a two stage PLL367*368* Note that some bioses (NV30+) have lookup tables of precomputed MNP369* values, but we're too lazy to use those atm370*371* "clk" parameter in kHz372* returns calculated clock373*/374struct drm_nouveau_private *dev_priv = dev->dev_private;375int chip_version = dev_priv->vbios.chip_version;376int minvco1 = pll_lim->vco1.minfreq, maxvco1 = pll_lim->vco1.maxfreq;377int minvco2 = pll_lim->vco2.minfreq, maxvco2 = pll_lim->vco2.maxfreq;378int minU1 = pll_lim->vco1.min_inputfreq, minU2 = pll_lim->vco2.min_inputfreq;379int maxU1 = pll_lim->vco1.max_inputfreq, maxU2 = pll_lim->vco2.max_inputfreq;380int minM1 = pll_lim->vco1.min_m, maxM1 = pll_lim->vco1.max_m;381int minN1 = pll_lim->vco1.min_n, maxN1 = pll_lim->vco1.max_n;382int minM2 = pll_lim->vco2.min_m, maxM2 = pll_lim->vco2.max_m;383int minN2 = pll_lim->vco2.min_n, maxN2 = pll_lim->vco2.max_n;384int maxlog2P = pll_lim->max_usable_log2p;385int crystal = pll_lim->refclk;386bool fixedgain2 = (minM2 == maxM2 && minN2 == maxN2);387int M1, N1, M2, N2, log2P;388int clkP, calcclk1, calcclk2, calcclkout;389int delta, bestdelta = INT_MAX;390int bestclk = 0;391392int vco2 = (maxvco2 - maxvco2/200) / 2;393for (log2P = 0; clk && log2P < maxlog2P && clk <= (vco2 >> log2P); log2P++)394;395clkP = clk << log2P;396397if (maxvco2 < clk + clk/200) /* +0.5% */398maxvco2 = clk + clk/200;399400for (M1 = minM1; M1 <= maxM1; M1++) {401if (crystal/M1 < minU1)402return bestclk;403if (crystal/M1 > maxU1)404continue;405406for (N1 = minN1; N1 <= maxN1; N1++) {407calcclk1 = crystal * N1 / M1;408if (calcclk1 < minvco1)409continue;410if (calcclk1 > maxvco1)411break;412413for (M2 = minM2; M2 <= maxM2; M2++) {414if (calcclk1/M2 < minU2)415break;416if (calcclk1/M2 > maxU2)417continue;418419/* add calcclk1/2 to round better */420N2 = (clkP * M2 + calcclk1/2) / calcclk1;421if (N2 < minN2)422continue;423if (N2 > maxN2)424break;425426if (!fixedgain2) {427if (chip_version < 0x60)428if (N2/M2 < 4 || N2/M2 > 10)429continue;430431calcclk2 = calcclk1 * N2 / M2;432if (calcclk2 < minvco2)433break;434if (calcclk2 > maxvco2)435continue;436} else437calcclk2 = calcclk1;438439calcclkout = calcclk2 >> log2P;440delta = abs(calcclkout - clk);441/* we do an exhaustive search rather than terminating442* on an optimality condition...443*/444if (delta < bestdelta) {445bestdelta = delta;446bestclk = calcclkout;447bestpv->N1 = N1;448bestpv->M1 = M1;449bestpv->N2 = N2;450bestpv->M2 = M2;451bestpv->log2P = log2P;452if (delta == 0) /* except this one */453return bestclk;454}455}456}457}458459return bestclk;460}461462int463nouveau_calc_pll_mnp(struct drm_device *dev, struct pll_lims *pll_lim, int clk,464struct nouveau_pll_vals *pv)465{466int outclk;467468if (!pll_lim->vco2.maxfreq)469outclk = getMNP_single(dev, pll_lim, clk, pv);470else471outclk = getMNP_double(dev, pll_lim, clk, pv);472473if (!outclk)474NV_ERROR(dev, "Could not find a compatible set of PLL values\n");475476return outclk;477}478479480