18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 1993-2003 NVIDIA, Corporation
38c2ecf20Sopenharmony_ci * Copyright 2007-2009 Stuart Bennett
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
68c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
78c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
88c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
98c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
108c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
138c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
188c2ecf20Sopenharmony_ci * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
198c2ecf20Sopenharmony_ci * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
208c2ecf20Sopenharmony_ci * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
218c2ecf20Sopenharmony_ci * SOFTWARE.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "nouveau_drv.h"
258c2ecf20Sopenharmony_ci#include "nouveau_reg.h"
268c2ecf20Sopenharmony_ci#include "hw.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/****************************************************************************\
298c2ecf20Sopenharmony_ci*                                                                            *
308c2ecf20Sopenharmony_ci* The video arbitration routines calculate some "magic" numbers.  Fixes      *
318c2ecf20Sopenharmony_ci* the snow seen when accessing the framebuffer without it.                   *
328c2ecf20Sopenharmony_ci* It just works (I hope).                                                    *
338c2ecf20Sopenharmony_ci*                                                                            *
348c2ecf20Sopenharmony_ci\****************************************************************************/
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistruct nv_fifo_info {
378c2ecf20Sopenharmony_ci	int lwm;
388c2ecf20Sopenharmony_ci	int burst;
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistruct nv_sim_state {
428c2ecf20Sopenharmony_ci	int pclk_khz;
438c2ecf20Sopenharmony_ci	int mclk_khz;
448c2ecf20Sopenharmony_ci	int nvclk_khz;
458c2ecf20Sopenharmony_ci	int bpp;
468c2ecf20Sopenharmony_ci	int mem_page_miss;
478c2ecf20Sopenharmony_ci	int mem_latency;
488c2ecf20Sopenharmony_ci	int memory_type;
498c2ecf20Sopenharmony_ci	int memory_width;
508c2ecf20Sopenharmony_ci	int two_heads;
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic void
548c2ecf20Sopenharmony_cinv04_calc_arb(struct nv_fifo_info *fifo, struct nv_sim_state *arb)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	int pagemiss, cas, bpp;
578c2ecf20Sopenharmony_ci	int nvclks, mclks, crtpagemiss;
588c2ecf20Sopenharmony_ci	int found, mclk_extra, mclk_loop, cbs, m1, p1;
598c2ecf20Sopenharmony_ci	int mclk_freq, pclk_freq, nvclk_freq;
608c2ecf20Sopenharmony_ci	int us_m, us_n, us_p, crtc_drain_rate;
618c2ecf20Sopenharmony_ci	int cpm_us, us_crt, clwm;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	pclk_freq = arb->pclk_khz;
648c2ecf20Sopenharmony_ci	mclk_freq = arb->mclk_khz;
658c2ecf20Sopenharmony_ci	nvclk_freq = arb->nvclk_khz;
668c2ecf20Sopenharmony_ci	pagemiss = arb->mem_page_miss;
678c2ecf20Sopenharmony_ci	cas = arb->mem_latency;
688c2ecf20Sopenharmony_ci	bpp = arb->bpp;
698c2ecf20Sopenharmony_ci	cbs = 128;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	nvclks = 10;
728c2ecf20Sopenharmony_ci	mclks = 13 + cas;
738c2ecf20Sopenharmony_ci	mclk_extra = 3;
748c2ecf20Sopenharmony_ci	found = 0;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	while (!found) {
778c2ecf20Sopenharmony_ci		found = 1;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci		mclk_loop = mclks + mclk_extra;
808c2ecf20Sopenharmony_ci		us_m = mclk_loop * 1000 * 1000 / mclk_freq;
818c2ecf20Sopenharmony_ci		us_n = nvclks * 1000 * 1000 / nvclk_freq;
828c2ecf20Sopenharmony_ci		us_p = nvclks * 1000 * 1000 / pclk_freq;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci		crtc_drain_rate = pclk_freq * bpp / 8;
858c2ecf20Sopenharmony_ci		crtpagemiss = 2;
868c2ecf20Sopenharmony_ci		crtpagemiss += 1;
878c2ecf20Sopenharmony_ci		cpm_us = crtpagemiss * pagemiss * 1000 * 1000 / mclk_freq;
888c2ecf20Sopenharmony_ci		us_crt = cpm_us + us_m + us_n + us_p;
898c2ecf20Sopenharmony_ci		clwm = us_crt * crtc_drain_rate / (1000 * 1000);
908c2ecf20Sopenharmony_ci		clwm++;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci		m1 = clwm + cbs - 512;
938c2ecf20Sopenharmony_ci		p1 = m1 * pclk_freq / mclk_freq;
948c2ecf20Sopenharmony_ci		p1 = p1 * bpp / 8;
958c2ecf20Sopenharmony_ci		if ((p1 < m1 && m1 > 0) || clwm > 519) {
968c2ecf20Sopenharmony_ci			found = !mclk_extra;
978c2ecf20Sopenharmony_ci			mclk_extra--;
988c2ecf20Sopenharmony_ci		}
998c2ecf20Sopenharmony_ci		if (clwm < 384)
1008c2ecf20Sopenharmony_ci			clwm = 384;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci		fifo->lwm = clwm;
1038c2ecf20Sopenharmony_ci		fifo->burst = cbs;
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic void
1088c2ecf20Sopenharmony_cinv10_calc_arb(struct nv_fifo_info *fifo, struct nv_sim_state *arb)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	int fill_rate, drain_rate;
1118c2ecf20Sopenharmony_ci	int pclks, nvclks, mclks, xclks;
1128c2ecf20Sopenharmony_ci	int pclk_freq, nvclk_freq, mclk_freq;
1138c2ecf20Sopenharmony_ci	int fill_lat, extra_lat;
1148c2ecf20Sopenharmony_ci	int max_burst_o, max_burst_l;
1158c2ecf20Sopenharmony_ci	int fifo_len, min_lwm, max_lwm;
1168c2ecf20Sopenharmony_ci	const int burst_lat = 80; /* Maximum allowable latency due
1178c2ecf20Sopenharmony_ci				   * to the CRTC FIFO burst. (ns) */
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	pclk_freq = arb->pclk_khz;
1208c2ecf20Sopenharmony_ci	nvclk_freq = arb->nvclk_khz;
1218c2ecf20Sopenharmony_ci	mclk_freq = arb->mclk_khz;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	fill_rate = mclk_freq * arb->memory_width / 8; /* kB/s */
1248c2ecf20Sopenharmony_ci	drain_rate = pclk_freq * arb->bpp / 8; /* kB/s */
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	fifo_len = arb->two_heads ? 1536 : 1024; /* B */
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	/* Fixed FIFO refill latency. */
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	pclks = 4;	/* lwm detect. */
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	nvclks = 3	/* lwm -> sync. */
1338c2ecf20Sopenharmony_ci		+ 2	/* fbi bus cycles (1 req + 1 busy) */
1348c2ecf20Sopenharmony_ci		+ 1	/* 2 edge sync.  may be very close to edge so
1358c2ecf20Sopenharmony_ci			 * just put one. */
1368c2ecf20Sopenharmony_ci		+ 1	/* fbi_d_rdv_n */
1378c2ecf20Sopenharmony_ci		+ 1	/* Fbi_d_rdata */
1388c2ecf20Sopenharmony_ci		+ 1;	/* crtfifo load */
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	mclks = 1	/* 2 edge sync.  may be very close to edge so
1418c2ecf20Sopenharmony_ci			 * just put one. */
1428c2ecf20Sopenharmony_ci		+ 1	/* arb_hp_req */
1438c2ecf20Sopenharmony_ci		+ 5	/* tiling pipeline */
1448c2ecf20Sopenharmony_ci		+ 2	/* latency fifo */
1458c2ecf20Sopenharmony_ci		+ 2	/* memory request to fbio block */
1468c2ecf20Sopenharmony_ci		+ 7;	/* data returned from fbio block */
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	/* Need to accumulate 256 bits for read */
1498c2ecf20Sopenharmony_ci	mclks += (arb->memory_type == 0 ? 2 : 1)
1508c2ecf20Sopenharmony_ci		* arb->memory_width / 32;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	fill_lat = mclks * 1000 * 1000 / mclk_freq   /* minimum mclk latency */
1538c2ecf20Sopenharmony_ci		+ nvclks * 1000 * 1000 / nvclk_freq  /* nvclk latency */
1548c2ecf20Sopenharmony_ci		+ pclks * 1000 * 1000 / pclk_freq;   /* pclk latency */
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/* Conditional FIFO refill latency. */
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	xclks = 2 * arb->mem_page_miss + mclks /* Extra latency due to
1598c2ecf20Sopenharmony_ci						* the overlay. */
1608c2ecf20Sopenharmony_ci		+ 2 * arb->mem_page_miss       /* Extra pagemiss latency. */
1618c2ecf20Sopenharmony_ci		+ (arb->bpp == 32 ? 8 : 4);    /* Margin of error. */
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	extra_lat = xclks * 1000 * 1000 / mclk_freq;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	if (arb->two_heads)
1668c2ecf20Sopenharmony_ci		/* Account for another CRTC. */
1678c2ecf20Sopenharmony_ci		extra_lat += fill_lat + extra_lat + burst_lat;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	/* FIFO burst */
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	/* Max burst not leading to overflows. */
1728c2ecf20Sopenharmony_ci	max_burst_o = (1 + fifo_len - extra_lat * drain_rate / (1000 * 1000))
1738c2ecf20Sopenharmony_ci		* (fill_rate / 1000) / ((fill_rate - drain_rate) / 1000);
1748c2ecf20Sopenharmony_ci	fifo->burst = min(max_burst_o, 1024);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	/* Max burst value with an acceptable latency. */
1778c2ecf20Sopenharmony_ci	max_burst_l = burst_lat * fill_rate / (1000 * 1000);
1788c2ecf20Sopenharmony_ci	fifo->burst = min(max_burst_l, fifo->burst);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	fifo->burst = rounddown_pow_of_two(fifo->burst);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	/* FIFO low watermark */
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	min_lwm = (fill_lat + extra_lat) * drain_rate / (1000 * 1000) + 1;
1858c2ecf20Sopenharmony_ci	max_lwm = fifo_len - fifo->burst
1868c2ecf20Sopenharmony_ci		+ fill_lat * drain_rate / (1000 * 1000)
1878c2ecf20Sopenharmony_ci		+ fifo->burst * drain_rate / fill_rate;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	fifo->lwm = min_lwm + 10 * (max_lwm - min_lwm) / 100; /* Empirical. */
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic void
1938c2ecf20Sopenharmony_cinv04_update_arb(struct drm_device *dev, int VClk, int bpp,
1948c2ecf20Sopenharmony_ci		int *burst, int *lwm)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
1978c2ecf20Sopenharmony_ci	struct nvif_object *device = &nouveau_drm(dev)->client.device.object;
1988c2ecf20Sopenharmony_ci	struct nv_fifo_info fifo_data;
1998c2ecf20Sopenharmony_ci	struct nv_sim_state sim_data;
2008c2ecf20Sopenharmony_ci	int MClk = nouveau_hw_get_clock(dev, PLL_MEMORY);
2018c2ecf20Sopenharmony_ci	int NVClk = nouveau_hw_get_clock(dev, PLL_CORE);
2028c2ecf20Sopenharmony_ci	uint32_t cfg1 = nvif_rd32(device, NV04_PFB_CFG1);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	sim_data.pclk_khz = VClk;
2058c2ecf20Sopenharmony_ci	sim_data.mclk_khz = MClk;
2068c2ecf20Sopenharmony_ci	sim_data.nvclk_khz = NVClk;
2078c2ecf20Sopenharmony_ci	sim_data.bpp = bpp;
2088c2ecf20Sopenharmony_ci	sim_data.two_heads = nv_two_heads(dev);
2098c2ecf20Sopenharmony_ci	if ((dev->pdev->device & 0xffff) == 0x01a0 /*CHIPSET_NFORCE*/ ||
2108c2ecf20Sopenharmony_ci	    (dev->pdev->device & 0xffff) == 0x01f0 /*CHIPSET_NFORCE2*/) {
2118c2ecf20Sopenharmony_ci		uint32_t type;
2128c2ecf20Sopenharmony_ci		int domain = pci_domain_nr(dev->pdev->bus);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci		pci_read_config_dword(pci_get_domain_bus_and_slot(domain, 0, 1),
2158c2ecf20Sopenharmony_ci				      0x7c, &type);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci		sim_data.memory_type = (type >> 12) & 1;
2188c2ecf20Sopenharmony_ci		sim_data.memory_width = 64;
2198c2ecf20Sopenharmony_ci		sim_data.mem_latency = 3;
2208c2ecf20Sopenharmony_ci		sim_data.mem_page_miss = 10;
2218c2ecf20Sopenharmony_ci	} else {
2228c2ecf20Sopenharmony_ci		sim_data.memory_type = nvif_rd32(device, NV04_PFB_CFG0) & 0x1;
2238c2ecf20Sopenharmony_ci		sim_data.memory_width = (nvif_rd32(device, NV_PEXTDEV_BOOT_0) & 0x10) ? 128 : 64;
2248c2ecf20Sopenharmony_ci		sim_data.mem_latency = cfg1 & 0xf;
2258c2ecf20Sopenharmony_ci		sim_data.mem_page_miss = ((cfg1 >> 4) & 0xf) + ((cfg1 >> 31) & 0x1);
2268c2ecf20Sopenharmony_ci	}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	if (drm->client.device.info.family == NV_DEVICE_INFO_V0_TNT)
2298c2ecf20Sopenharmony_ci		nv04_calc_arb(&fifo_data, &sim_data);
2308c2ecf20Sopenharmony_ci	else
2318c2ecf20Sopenharmony_ci		nv10_calc_arb(&fifo_data, &sim_data);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	*burst = ilog2(fifo_data.burst >> 4);
2348c2ecf20Sopenharmony_ci	*lwm = fifo_data.lwm >> 3;
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic void
2388c2ecf20Sopenharmony_cinv20_update_arb(int *burst, int *lwm)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	unsigned int fifo_size, burst_size, graphics_lwm;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	fifo_size = 2048;
2438c2ecf20Sopenharmony_ci	burst_size = 512;
2448c2ecf20Sopenharmony_ci	graphics_lwm = fifo_size - burst_size;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	*burst = ilog2(burst_size >> 5);
2478c2ecf20Sopenharmony_ci	*lwm = graphics_lwm >> 3;
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_civoid
2518c2ecf20Sopenharmony_cinouveau_calc_arb(struct drm_device *dev, int vclk, int bpp, int *burst, int *lwm)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_KELVIN)
2568c2ecf20Sopenharmony_ci		nv04_update_arb(dev, vclk, bpp, burst, lwm);
2578c2ecf20Sopenharmony_ci	else if ((dev->pdev->device & 0xfff0) == 0x0240 /*CHIPSET_C51*/ ||
2588c2ecf20Sopenharmony_ci		 (dev->pdev->device & 0xfff0) == 0x03d0 /*CHIPSET_C512*/) {
2598c2ecf20Sopenharmony_ci		*burst = 128;
2608c2ecf20Sopenharmony_ci		*lwm = 0x0480;
2618c2ecf20Sopenharmony_ci	} else
2628c2ecf20Sopenharmony_ci		nv20_update_arb(burst, lwm);
2638c2ecf20Sopenharmony_ci}
264