Path: blob/master/drivers/gpu/drm/nouveau/nouveau_hw.h
15112 views
/*1* Copyright 2008 Stuart Bennett2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included in11* all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,17* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF18* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE19* SOFTWARE.20*/2122#ifndef __NOUVEAU_HW_H__23#define __NOUVEAU_HW_H__2425#include "drmP.h"26#include "nouveau_drv.h"2728#define MASK(field) ( \29(0xffffffff >> (31 - ((1 ? field) - (0 ? field)))) << (0 ? field))3031#define XLATE(src, srclowbit, outfield) ( \32(((src) >> (srclowbit)) << (0 ? outfield)) & MASK(outfield))3334void NVWriteVgaSeq(struct drm_device *, int head, uint8_t index, uint8_t value);35uint8_t NVReadVgaSeq(struct drm_device *, int head, uint8_t index);36void NVWriteVgaGr(struct drm_device *, int head, uint8_t index, uint8_t value);37uint8_t NVReadVgaGr(struct drm_device *, int head, uint8_t index);38void NVSetOwner(struct drm_device *, int owner);39void NVBlankScreen(struct drm_device *, int head, bool blank);40void nouveau_hw_setpll(struct drm_device *, uint32_t reg1,41struct nouveau_pll_vals *pv);42int nouveau_hw_get_pllvals(struct drm_device *, enum pll_types plltype,43struct nouveau_pll_vals *pllvals);44int nouveau_hw_pllvals_to_clk(struct nouveau_pll_vals *pllvals);45int nouveau_hw_get_clock(struct drm_device *, enum pll_types plltype);46void nouveau_hw_save_vga_fonts(struct drm_device *, bool save);47void nouveau_hw_save_state(struct drm_device *, int head,48struct nv04_mode_state *state);49void nouveau_hw_load_state(struct drm_device *, int head,50struct nv04_mode_state *state);51void nouveau_hw_load_state_palette(struct drm_device *, int head,52struct nv04_mode_state *state);5354/* nouveau_calc.c */55extern void nouveau_calc_arb(struct drm_device *, int vclk, int bpp,56int *burst, int *lwm);57extern int nouveau_calc_pll_mnp(struct drm_device *, struct pll_lims *pll_lim,58int clk, struct nouveau_pll_vals *pv);5960static inline uint32_t61nvReadMC(struct drm_device *dev, uint32_t reg)62{63uint32_t val = nv_rd32(dev, reg);64NV_REG_DEBUG(MC, dev, "reg %08x val %08x\n", reg, val);65return val;66}6768static inline void69nvWriteMC(struct drm_device *dev, uint32_t reg, uint32_t val)70{71NV_REG_DEBUG(MC, dev, "reg %08x val %08x\n", reg, val);72nv_wr32(dev, reg, val);73}7475static inline uint32_t76nvReadVIDEO(struct drm_device *dev, uint32_t reg)77{78uint32_t val = nv_rd32(dev, reg);79NV_REG_DEBUG(VIDEO, dev, "reg %08x val %08x\n", reg, val);80return val;81}8283static inline void84nvWriteVIDEO(struct drm_device *dev, uint32_t reg, uint32_t val)85{86NV_REG_DEBUG(VIDEO, dev, "reg %08x val %08x\n", reg, val);87nv_wr32(dev, reg, val);88}8990static inline uint32_t91nvReadFB(struct drm_device *dev, uint32_t reg)92{93uint32_t val = nv_rd32(dev, reg);94NV_REG_DEBUG(FB, dev, "reg %08x val %08x\n", reg, val);95return val;96}9798static inline void99nvWriteFB(struct drm_device *dev, uint32_t reg, uint32_t val)100{101NV_REG_DEBUG(FB, dev, "reg %08x val %08x\n", reg, val);102nv_wr32(dev, reg, val);103}104105static inline uint32_t106nvReadEXTDEV(struct drm_device *dev, uint32_t reg)107{108uint32_t val = nv_rd32(dev, reg);109NV_REG_DEBUG(EXTDEV, dev, "reg %08x val %08x\n", reg, val);110return val;111}112113static inline void114nvWriteEXTDEV(struct drm_device *dev, uint32_t reg, uint32_t val)115{116NV_REG_DEBUG(EXTDEV, dev, "reg %08x val %08x\n", reg, val);117nv_wr32(dev, reg, val);118}119120static inline uint32_t NVReadCRTC(struct drm_device *dev,121int head, uint32_t reg)122{123uint32_t val;124if (head)125reg += NV_PCRTC0_SIZE;126val = nv_rd32(dev, reg);127NV_REG_DEBUG(CRTC, dev, "head %d reg %08x val %08x\n", head, reg, val);128return val;129}130131static inline void NVWriteCRTC(struct drm_device *dev,132int head, uint32_t reg, uint32_t val)133{134if (head)135reg += NV_PCRTC0_SIZE;136NV_REG_DEBUG(CRTC, dev, "head %d reg %08x val %08x\n", head, reg, val);137nv_wr32(dev, reg, val);138}139140static inline uint32_t NVReadRAMDAC(struct drm_device *dev,141int head, uint32_t reg)142{143uint32_t val;144if (head)145reg += NV_PRAMDAC0_SIZE;146val = nv_rd32(dev, reg);147NV_REG_DEBUG(RAMDAC, dev, "head %d reg %08x val %08x\n",148head, reg, val);149return val;150}151152static inline void NVWriteRAMDAC(struct drm_device *dev,153int head, uint32_t reg, uint32_t val)154{155if (head)156reg += NV_PRAMDAC0_SIZE;157NV_REG_DEBUG(RAMDAC, dev, "head %d reg %08x val %08x\n",158head, reg, val);159nv_wr32(dev, reg, val);160}161162static inline uint8_t nv_read_tmds(struct drm_device *dev,163int or, int dl, uint8_t address)164{165int ramdac = (or & OUTPUT_C) >> 2;166167NVWriteRAMDAC(dev, ramdac, NV_PRAMDAC_FP_TMDS_CONTROL + dl * 8,168NV_PRAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE | address);169return NVReadRAMDAC(dev, ramdac, NV_PRAMDAC_FP_TMDS_DATA + dl * 8);170}171172static inline void nv_write_tmds(struct drm_device *dev,173int or, int dl, uint8_t address,174uint8_t data)175{176int ramdac = (or & OUTPUT_C) >> 2;177178NVWriteRAMDAC(dev, ramdac, NV_PRAMDAC_FP_TMDS_DATA + dl * 8, data);179NVWriteRAMDAC(dev, ramdac, NV_PRAMDAC_FP_TMDS_CONTROL + dl * 8, address);180}181182static inline void NVWriteVgaCrtc(struct drm_device *dev,183int head, uint8_t index, uint8_t value)184{185NV_REG_DEBUG(VGACRTC, dev, "head %d index 0x%02x data 0x%02x\n",186head, index, value);187nv_wr08(dev, NV_PRMCIO_CRX__COLOR + head * NV_PRMCIO_SIZE, index);188nv_wr08(dev, NV_PRMCIO_CR__COLOR + head * NV_PRMCIO_SIZE, value);189}190191static inline uint8_t NVReadVgaCrtc(struct drm_device *dev,192int head, uint8_t index)193{194uint8_t val;195nv_wr08(dev, NV_PRMCIO_CRX__COLOR + head * NV_PRMCIO_SIZE, index);196val = nv_rd08(dev, NV_PRMCIO_CR__COLOR + head * NV_PRMCIO_SIZE);197NV_REG_DEBUG(VGACRTC, dev, "head %d index 0x%02x data 0x%02x\n",198head, index, val);199return val;200}201202/* CR57 and CR58 are a fun pair of regs. CR57 provides an index (0-0xf) for CR58203* I suspect they in fact do nothing, but are merely a way to carry useful204* per-head variables around205*206* Known uses:207* CR57 CR58208* 0x00 index to the appropriate dcb entry (or 7f for inactive)209* 0x02 dcb entry's "or" value (or 00 for inactive)210* 0x03 bit0 set for dual link (LVDS, possibly elsewhere too)211* 0x08 or 0x09 pxclk in MHz212* 0x0f laptop panel info - low nibble for PEXTDEV_BOOT_0 strap213* high nibble for xlat strap value214*/215216static inline void217NVWriteVgaCrtc5758(struct drm_device *dev, int head, uint8_t index, uint8_t value)218{219NVWriteVgaCrtc(dev, head, NV_CIO_CRE_57, index);220NVWriteVgaCrtc(dev, head, NV_CIO_CRE_58, value);221}222223static inline uint8_t NVReadVgaCrtc5758(struct drm_device *dev, int head, uint8_t index)224{225NVWriteVgaCrtc(dev, head, NV_CIO_CRE_57, index);226return NVReadVgaCrtc(dev, head, NV_CIO_CRE_58);227}228229static inline uint8_t NVReadPRMVIO(struct drm_device *dev,230int head, uint32_t reg)231{232struct drm_nouveau_private *dev_priv = dev->dev_private;233uint8_t val;234235/* Only NV4x have two pvio ranges; other twoHeads cards MUST call236* NVSetOwner for the relevant head to be programmed */237if (head && dev_priv->card_type == NV_40)238reg += NV_PRMVIO_SIZE;239240val = nv_rd08(dev, reg);241NV_REG_DEBUG(RMVIO, dev, "head %d reg %08x val %02x\n", head, reg, val);242return val;243}244245static inline void NVWritePRMVIO(struct drm_device *dev,246int head, uint32_t reg, uint8_t value)247{248struct drm_nouveau_private *dev_priv = dev->dev_private;249250/* Only NV4x have two pvio ranges; other twoHeads cards MUST call251* NVSetOwner for the relevant head to be programmed */252if (head && dev_priv->card_type == NV_40)253reg += NV_PRMVIO_SIZE;254255NV_REG_DEBUG(RMVIO, dev, "head %d reg %08x val %02x\n",256head, reg, value);257nv_wr08(dev, reg, value);258}259260static inline void NVSetEnablePalette(struct drm_device *dev, int head, bool enable)261{262nv_rd08(dev, NV_PRMCIO_INP0__COLOR + head * NV_PRMCIO_SIZE);263nv_wr08(dev, NV_PRMCIO_ARX + head * NV_PRMCIO_SIZE, enable ? 0 : 0x20);264}265266static inline bool NVGetEnablePalette(struct drm_device *dev, int head)267{268nv_rd08(dev, NV_PRMCIO_INP0__COLOR + head * NV_PRMCIO_SIZE);269return !(nv_rd08(dev, NV_PRMCIO_ARX + head * NV_PRMCIO_SIZE) & 0x20);270}271272static inline void NVWriteVgaAttr(struct drm_device *dev,273int head, uint8_t index, uint8_t value)274{275if (NVGetEnablePalette(dev, head))276index &= ~0x20;277else278index |= 0x20;279280nv_rd08(dev, NV_PRMCIO_INP0__COLOR + head * NV_PRMCIO_SIZE);281NV_REG_DEBUG(VGAATTR, dev, "head %d index 0x%02x data 0x%02x\n",282head, index, value);283nv_wr08(dev, NV_PRMCIO_ARX + head * NV_PRMCIO_SIZE, index);284nv_wr08(dev, NV_PRMCIO_AR__WRITE + head * NV_PRMCIO_SIZE, value);285}286287static inline uint8_t NVReadVgaAttr(struct drm_device *dev,288int head, uint8_t index)289{290uint8_t val;291if (NVGetEnablePalette(dev, head))292index &= ~0x20;293else294index |= 0x20;295296nv_rd08(dev, NV_PRMCIO_INP0__COLOR + head * NV_PRMCIO_SIZE);297nv_wr08(dev, NV_PRMCIO_ARX + head * NV_PRMCIO_SIZE, index);298val = nv_rd08(dev, NV_PRMCIO_AR__READ + head * NV_PRMCIO_SIZE);299NV_REG_DEBUG(VGAATTR, dev, "head %d index 0x%02x data 0x%02x\n",300head, index, val);301return val;302}303304static inline void NVVgaSeqReset(struct drm_device *dev, int head, bool start)305{306NVWriteVgaSeq(dev, head, NV_VIO_SR_RESET_INDEX, start ? 0x1 : 0x3);307}308309static inline void NVVgaProtect(struct drm_device *dev, int head, bool protect)310{311uint8_t seq1 = NVReadVgaSeq(dev, head, NV_VIO_SR_CLOCK_INDEX);312313if (protect) {314NVVgaSeqReset(dev, head, true);315NVWriteVgaSeq(dev, head, NV_VIO_SR_CLOCK_INDEX, seq1 | 0x20);316} else {317/* Reenable sequencer, then turn on screen */318NVWriteVgaSeq(dev, head, NV_VIO_SR_CLOCK_INDEX, seq1 & ~0x20); /* reenable display */319NVVgaSeqReset(dev, head, false);320}321NVSetEnablePalette(dev, head, protect);322}323324static inline bool325nv_heads_tied(struct drm_device *dev)326{327struct drm_nouveau_private *dev_priv = dev->dev_private;328329if (dev_priv->chipset == 0x11)330return !!(nvReadMC(dev, NV_PBUS_DEBUG_1) & (1 << 28));331332return NVReadVgaCrtc(dev, 0, NV_CIO_CRE_44) & 0x4;333}334335/* makes cr0-7 on the specified head read-only */336static inline bool337nv_lock_vga_crtc_base(struct drm_device *dev, int head, bool lock)338{339uint8_t cr11 = NVReadVgaCrtc(dev, head, NV_CIO_CR_VRE_INDEX);340bool waslocked = cr11 & 0x80;341342if (lock)343cr11 |= 0x80;344else345cr11 &= ~0x80;346NVWriteVgaCrtc(dev, head, NV_CIO_CR_VRE_INDEX, cr11);347348return waslocked;349}350351static inline void352nv_lock_vga_crtc_shadow(struct drm_device *dev, int head, int lock)353{354/* shadow lock: connects 0x60?3d? regs to "real" 0x3d? regs355* bit7: unlocks HDT, HBS, HBE, HRS, HRE, HEB356* bit6: seems to have some effect on CR09 (double scan, VBS_9)357* bit5: unlocks HDE358* bit4: unlocks VDE359* bit3: unlocks VDT, OVL, VRS, ?VRE?, VBS, VBE, LSR, EBR360* bit2: same as bit 1 of 0x60?804361* bit0: same as bit 0 of 0x60?804362*/363364uint8_t cr21 = lock;365366if (lock < 0)367/* 0xfa is generic "unlock all" mask */368cr21 = NVReadVgaCrtc(dev, head, NV_CIO_CRE_21) | 0xfa;369370NVWriteVgaCrtc(dev, head, NV_CIO_CRE_21, cr21);371}372373/* renders the extended crtc regs (cr19+) on all crtcs impervious:374* immutable and unreadable375*/376static inline bool377NVLockVgaCrtcs(struct drm_device *dev, bool lock)378{379struct drm_nouveau_private *dev_priv = dev->dev_private;380bool waslocked = !NVReadVgaCrtc(dev, 0, NV_CIO_SR_LOCK_INDEX);381382NVWriteVgaCrtc(dev, 0, NV_CIO_SR_LOCK_INDEX,383lock ? NV_CIO_SR_LOCK_VALUE : NV_CIO_SR_UNLOCK_RW_VALUE);384/* NV11 has independently lockable extended crtcs, except when tied */385if (dev_priv->chipset == 0x11 && !nv_heads_tied(dev))386NVWriteVgaCrtc(dev, 1, NV_CIO_SR_LOCK_INDEX,387lock ? NV_CIO_SR_LOCK_VALUE :388NV_CIO_SR_UNLOCK_RW_VALUE);389390return waslocked;391}392393/* nv04 cursor max dimensions of 32x32 (A1R5G5B5) */394#define NV04_CURSOR_SIZE 32395/* limit nv10 cursors to 64x64 (ARGB8) (we could go to 64x255) */396#define NV10_CURSOR_SIZE 64397398static inline int nv_cursor_width(struct drm_device *dev)399{400struct drm_nouveau_private *dev_priv = dev->dev_private;401402return dev_priv->card_type >= NV_10 ? NV10_CURSOR_SIZE : NV04_CURSOR_SIZE;403}404405static inline void406nv_fix_nv40_hw_cursor(struct drm_device *dev, int head)407{408/* on some nv40 (such as the "true" (in the NV_PFB_BOOT_0 sense) nv40,409* the gf6800gt) a hardware bug requires a write to PRAMDAC_CURSOR_POS410* for changes to the CRTC CURCTL regs to take effect, whether changing411* the pixmap location, or just showing/hiding the cursor412*/413uint32_t curpos = NVReadRAMDAC(dev, head, NV_PRAMDAC_CU_START_POS);414NVWriteRAMDAC(dev, head, NV_PRAMDAC_CU_START_POS, curpos);415}416417static inline void418nv_set_crtc_base(struct drm_device *dev, int head, uint32_t offset)419{420struct drm_nouveau_private *dev_priv = dev->dev_private;421422NVWriteCRTC(dev, head, NV_PCRTC_START, offset);423424if (dev_priv->card_type == NV_04) {425/*426* Hilarious, the 24th bit doesn't want to stick to427* PCRTC_START...428*/429int cre_heb = NVReadVgaCrtc(dev, head, NV_CIO_CRE_HEB__INDEX);430431NVWriteVgaCrtc(dev, head, NV_CIO_CRE_HEB__INDEX,432(cre_heb & ~0x40) | ((offset >> 18) & 0x40));433}434}435436static inline void437nv_show_cursor(struct drm_device *dev, int head, bool show)438{439struct drm_nouveau_private *dev_priv = dev->dev_private;440uint8_t *curctl1 =441&dev_priv->mode_reg.crtc_reg[head].CRTC[NV_CIO_CRE_HCUR_ADDR1_INDEX];442443if (show)444*curctl1 |= MASK(NV_CIO_CRE_HCUR_ADDR1_ENABLE);445else446*curctl1 &= ~MASK(NV_CIO_CRE_HCUR_ADDR1_ENABLE);447NVWriteVgaCrtc(dev, head, NV_CIO_CRE_HCUR_ADDR1_INDEX, *curctl1);448449if (dev_priv->card_type == NV_40)450nv_fix_nv40_hw_cursor(dev, head);451}452453static inline uint32_t454nv_pitch_align(struct drm_device *dev, uint32_t width, int bpp)455{456struct drm_nouveau_private *dev_priv = dev->dev_private;457int mask;458459if (bpp == 15)460bpp = 16;461if (bpp == 24)462bpp = 8;463464/* Alignment requirements taken from the Haiku driver */465if (dev_priv->card_type == NV_04)466mask = 128 / bpp - 1;467else468mask = 512 / bpp - 1;469470return (width + mask) & ~mask;471}472473#endif /* __NOUVEAU_HW_H__ */474475476