intel_display.c 304 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * 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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_dp_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <linux/dma_remapping.h>
  42. static void intel_increase_pllclock(struct drm_crtc *crtc);
  43. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  44. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  45. struct intel_crtc_config *pipe_config);
  46. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  47. struct intel_crtc_config *pipe_config);
  48. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  49. int x, int y, struct drm_framebuffer *old_fb);
  50. typedef struct {
  51. int min, max;
  52. } intel_range_t;
  53. typedef struct {
  54. int dot_limit;
  55. int p2_slow, p2_fast;
  56. } intel_p2_t;
  57. typedef struct intel_limit intel_limit_t;
  58. struct intel_limit {
  59. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  60. intel_p2_t p2;
  61. };
  62. int
  63. intel_pch_rawclk(struct drm_device *dev)
  64. {
  65. struct drm_i915_private *dev_priv = dev->dev_private;
  66. WARN_ON(!HAS_PCH_SPLIT(dev));
  67. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  68. }
  69. static inline u32 /* units of 100MHz */
  70. intel_fdi_link_freq(struct drm_device *dev)
  71. {
  72. if (IS_GEN5(dev)) {
  73. struct drm_i915_private *dev_priv = dev->dev_private;
  74. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  75. } else
  76. return 27;
  77. }
  78. static const intel_limit_t intel_limits_i8xx_dac = {
  79. .dot = { .min = 25000, .max = 350000 },
  80. .vco = { .min = 930000, .max = 1400000 },
  81. .n = { .min = 3, .max = 16 },
  82. .m = { .min = 96, .max = 140 },
  83. .m1 = { .min = 18, .max = 26 },
  84. .m2 = { .min = 6, .max = 16 },
  85. .p = { .min = 4, .max = 128 },
  86. .p1 = { .min = 2, .max = 33 },
  87. .p2 = { .dot_limit = 165000,
  88. .p2_slow = 4, .p2_fast = 2 },
  89. };
  90. static const intel_limit_t intel_limits_i8xx_dvo = {
  91. .dot = { .min = 25000, .max = 350000 },
  92. .vco = { .min = 930000, .max = 1400000 },
  93. .n = { .min = 3, .max = 16 },
  94. .m = { .min = 96, .max = 140 },
  95. .m1 = { .min = 18, .max = 26 },
  96. .m2 = { .min = 6, .max = 16 },
  97. .p = { .min = 4, .max = 128 },
  98. .p1 = { .min = 2, .max = 33 },
  99. .p2 = { .dot_limit = 165000,
  100. .p2_slow = 4, .p2_fast = 4 },
  101. };
  102. static const intel_limit_t intel_limits_i8xx_lvds = {
  103. .dot = { .min = 25000, .max = 350000 },
  104. .vco = { .min = 930000, .max = 1400000 },
  105. .n = { .min = 3, .max = 16 },
  106. .m = { .min = 96, .max = 140 },
  107. .m1 = { .min = 18, .max = 26 },
  108. .m2 = { .min = 6, .max = 16 },
  109. .p = { .min = 4, .max = 128 },
  110. .p1 = { .min = 1, .max = 6 },
  111. .p2 = { .dot_limit = 165000,
  112. .p2_slow = 14, .p2_fast = 7 },
  113. };
  114. static const intel_limit_t intel_limits_i9xx_sdvo = {
  115. .dot = { .min = 20000, .max = 400000 },
  116. .vco = { .min = 1400000, .max = 2800000 },
  117. .n = { .min = 1, .max = 6 },
  118. .m = { .min = 70, .max = 120 },
  119. .m1 = { .min = 8, .max = 18 },
  120. .m2 = { .min = 3, .max = 7 },
  121. .p = { .min = 5, .max = 80 },
  122. .p1 = { .min = 1, .max = 8 },
  123. .p2 = { .dot_limit = 200000,
  124. .p2_slow = 10, .p2_fast = 5 },
  125. };
  126. static const intel_limit_t intel_limits_i9xx_lvds = {
  127. .dot = { .min = 20000, .max = 400000 },
  128. .vco = { .min = 1400000, .max = 2800000 },
  129. .n = { .min = 1, .max = 6 },
  130. .m = { .min = 70, .max = 120 },
  131. .m1 = { .min = 8, .max = 18 },
  132. .m2 = { .min = 3, .max = 7 },
  133. .p = { .min = 7, .max = 98 },
  134. .p1 = { .min = 1, .max = 8 },
  135. .p2 = { .dot_limit = 112000,
  136. .p2_slow = 14, .p2_fast = 7 },
  137. };
  138. static const intel_limit_t intel_limits_g4x_sdvo = {
  139. .dot = { .min = 25000, .max = 270000 },
  140. .vco = { .min = 1750000, .max = 3500000},
  141. .n = { .min = 1, .max = 4 },
  142. .m = { .min = 104, .max = 138 },
  143. .m1 = { .min = 17, .max = 23 },
  144. .m2 = { .min = 5, .max = 11 },
  145. .p = { .min = 10, .max = 30 },
  146. .p1 = { .min = 1, .max = 3},
  147. .p2 = { .dot_limit = 270000,
  148. .p2_slow = 10,
  149. .p2_fast = 10
  150. },
  151. };
  152. static const intel_limit_t intel_limits_g4x_hdmi = {
  153. .dot = { .min = 22000, .max = 400000 },
  154. .vco = { .min = 1750000, .max = 3500000},
  155. .n = { .min = 1, .max = 4 },
  156. .m = { .min = 104, .max = 138 },
  157. .m1 = { .min = 16, .max = 23 },
  158. .m2 = { .min = 5, .max = 11 },
  159. .p = { .min = 5, .max = 80 },
  160. .p1 = { .min = 1, .max = 8},
  161. .p2 = { .dot_limit = 165000,
  162. .p2_slow = 10, .p2_fast = 5 },
  163. };
  164. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  165. .dot = { .min = 20000, .max = 115000 },
  166. .vco = { .min = 1750000, .max = 3500000 },
  167. .n = { .min = 1, .max = 3 },
  168. .m = { .min = 104, .max = 138 },
  169. .m1 = { .min = 17, .max = 23 },
  170. .m2 = { .min = 5, .max = 11 },
  171. .p = { .min = 28, .max = 112 },
  172. .p1 = { .min = 2, .max = 8 },
  173. .p2 = { .dot_limit = 0,
  174. .p2_slow = 14, .p2_fast = 14
  175. },
  176. };
  177. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  178. .dot = { .min = 80000, .max = 224000 },
  179. .vco = { .min = 1750000, .max = 3500000 },
  180. .n = { .min = 1, .max = 3 },
  181. .m = { .min = 104, .max = 138 },
  182. .m1 = { .min = 17, .max = 23 },
  183. .m2 = { .min = 5, .max = 11 },
  184. .p = { .min = 14, .max = 42 },
  185. .p1 = { .min = 2, .max = 6 },
  186. .p2 = { .dot_limit = 0,
  187. .p2_slow = 7, .p2_fast = 7
  188. },
  189. };
  190. static const intel_limit_t intel_limits_pineview_sdvo = {
  191. .dot = { .min = 20000, .max = 400000},
  192. .vco = { .min = 1700000, .max = 3500000 },
  193. /* Pineview's Ncounter is a ring counter */
  194. .n = { .min = 3, .max = 6 },
  195. .m = { .min = 2, .max = 256 },
  196. /* Pineview only has one combined m divider, which we treat as m2. */
  197. .m1 = { .min = 0, .max = 0 },
  198. .m2 = { .min = 0, .max = 254 },
  199. .p = { .min = 5, .max = 80 },
  200. .p1 = { .min = 1, .max = 8 },
  201. .p2 = { .dot_limit = 200000,
  202. .p2_slow = 10, .p2_fast = 5 },
  203. };
  204. static const intel_limit_t intel_limits_pineview_lvds = {
  205. .dot = { .min = 20000, .max = 400000 },
  206. .vco = { .min = 1700000, .max = 3500000 },
  207. .n = { .min = 3, .max = 6 },
  208. .m = { .min = 2, .max = 256 },
  209. .m1 = { .min = 0, .max = 0 },
  210. .m2 = { .min = 0, .max = 254 },
  211. .p = { .min = 7, .max = 112 },
  212. .p1 = { .min = 1, .max = 8 },
  213. .p2 = { .dot_limit = 112000,
  214. .p2_slow = 14, .p2_fast = 14 },
  215. };
  216. /* Ironlake / Sandybridge
  217. *
  218. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  219. * the range value for them is (actual_value - 2).
  220. */
  221. static const intel_limit_t intel_limits_ironlake_dac = {
  222. .dot = { .min = 25000, .max = 350000 },
  223. .vco = { .min = 1760000, .max = 3510000 },
  224. .n = { .min = 1, .max = 5 },
  225. .m = { .min = 79, .max = 127 },
  226. .m1 = { .min = 12, .max = 22 },
  227. .m2 = { .min = 5, .max = 9 },
  228. .p = { .min = 5, .max = 80 },
  229. .p1 = { .min = 1, .max = 8 },
  230. .p2 = { .dot_limit = 225000,
  231. .p2_slow = 10, .p2_fast = 5 },
  232. };
  233. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  234. .dot = { .min = 25000, .max = 350000 },
  235. .vco = { .min = 1760000, .max = 3510000 },
  236. .n = { .min = 1, .max = 3 },
  237. .m = { .min = 79, .max = 118 },
  238. .m1 = { .min = 12, .max = 22 },
  239. .m2 = { .min = 5, .max = 9 },
  240. .p = { .min = 28, .max = 112 },
  241. .p1 = { .min = 2, .max = 8 },
  242. .p2 = { .dot_limit = 225000,
  243. .p2_slow = 14, .p2_fast = 14 },
  244. };
  245. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  246. .dot = { .min = 25000, .max = 350000 },
  247. .vco = { .min = 1760000, .max = 3510000 },
  248. .n = { .min = 1, .max = 3 },
  249. .m = { .min = 79, .max = 127 },
  250. .m1 = { .min = 12, .max = 22 },
  251. .m2 = { .min = 5, .max = 9 },
  252. .p = { .min = 14, .max = 56 },
  253. .p1 = { .min = 2, .max = 8 },
  254. .p2 = { .dot_limit = 225000,
  255. .p2_slow = 7, .p2_fast = 7 },
  256. };
  257. /* LVDS 100mhz refclk limits. */
  258. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  259. .dot = { .min = 25000, .max = 350000 },
  260. .vco = { .min = 1760000, .max = 3510000 },
  261. .n = { .min = 1, .max = 2 },
  262. .m = { .min = 79, .max = 126 },
  263. .m1 = { .min = 12, .max = 22 },
  264. .m2 = { .min = 5, .max = 9 },
  265. .p = { .min = 28, .max = 112 },
  266. .p1 = { .min = 2, .max = 8 },
  267. .p2 = { .dot_limit = 225000,
  268. .p2_slow = 14, .p2_fast = 14 },
  269. };
  270. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  271. .dot = { .min = 25000, .max = 350000 },
  272. .vco = { .min = 1760000, .max = 3510000 },
  273. .n = { .min = 1, .max = 3 },
  274. .m = { .min = 79, .max = 126 },
  275. .m1 = { .min = 12, .max = 22 },
  276. .m2 = { .min = 5, .max = 9 },
  277. .p = { .min = 14, .max = 42 },
  278. .p1 = { .min = 2, .max = 6 },
  279. .p2 = { .dot_limit = 225000,
  280. .p2_slow = 7, .p2_fast = 7 },
  281. };
  282. static const intel_limit_t intel_limits_vlv = {
  283. /*
  284. * These are the data rate limits (measured in fast clocks)
  285. * since those are the strictest limits we have. The fast
  286. * clock and actual rate limits are more relaxed, so checking
  287. * them would make no difference.
  288. */
  289. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  290. .vco = { .min = 4000000, .max = 6000000 },
  291. .n = { .min = 1, .max = 7 },
  292. .m1 = { .min = 2, .max = 3 },
  293. .m2 = { .min = 11, .max = 156 },
  294. .p1 = { .min = 2, .max = 3 },
  295. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  296. };
  297. static void vlv_clock(int refclk, intel_clock_t *clock)
  298. {
  299. clock->m = clock->m1 * clock->m2;
  300. clock->p = clock->p1 * clock->p2;
  301. clock->vco = refclk * clock->m / clock->n;
  302. clock->dot = clock->vco / clock->p;
  303. }
  304. /**
  305. * Returns whether any output on the specified pipe is of the specified type
  306. */
  307. static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  308. {
  309. struct drm_device *dev = crtc->dev;
  310. struct intel_encoder *encoder;
  311. for_each_encoder_on_crtc(dev, crtc, encoder)
  312. if (encoder->type == type)
  313. return true;
  314. return false;
  315. }
  316. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  317. int refclk)
  318. {
  319. struct drm_device *dev = crtc->dev;
  320. const intel_limit_t *limit;
  321. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  322. if (intel_is_dual_link_lvds(dev)) {
  323. if (refclk == 100000)
  324. limit = &intel_limits_ironlake_dual_lvds_100m;
  325. else
  326. limit = &intel_limits_ironlake_dual_lvds;
  327. } else {
  328. if (refclk == 100000)
  329. limit = &intel_limits_ironlake_single_lvds_100m;
  330. else
  331. limit = &intel_limits_ironlake_single_lvds;
  332. }
  333. } else
  334. limit = &intel_limits_ironlake_dac;
  335. return limit;
  336. }
  337. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  338. {
  339. struct drm_device *dev = crtc->dev;
  340. const intel_limit_t *limit;
  341. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  342. if (intel_is_dual_link_lvds(dev))
  343. limit = &intel_limits_g4x_dual_channel_lvds;
  344. else
  345. limit = &intel_limits_g4x_single_channel_lvds;
  346. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  347. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  348. limit = &intel_limits_g4x_hdmi;
  349. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  350. limit = &intel_limits_g4x_sdvo;
  351. } else /* The option is for other outputs */
  352. limit = &intel_limits_i9xx_sdvo;
  353. return limit;
  354. }
  355. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  356. {
  357. struct drm_device *dev = crtc->dev;
  358. const intel_limit_t *limit;
  359. if (HAS_PCH_SPLIT(dev))
  360. limit = intel_ironlake_limit(crtc, refclk);
  361. else if (IS_G4X(dev)) {
  362. limit = intel_g4x_limit(crtc);
  363. } else if (IS_PINEVIEW(dev)) {
  364. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  365. limit = &intel_limits_pineview_lvds;
  366. else
  367. limit = &intel_limits_pineview_sdvo;
  368. } else if (IS_VALLEYVIEW(dev)) {
  369. limit = &intel_limits_vlv;
  370. } else if (!IS_GEN2(dev)) {
  371. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  372. limit = &intel_limits_i9xx_lvds;
  373. else
  374. limit = &intel_limits_i9xx_sdvo;
  375. } else {
  376. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  377. limit = &intel_limits_i8xx_lvds;
  378. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  379. limit = &intel_limits_i8xx_dvo;
  380. else
  381. limit = &intel_limits_i8xx_dac;
  382. }
  383. return limit;
  384. }
  385. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  386. static void pineview_clock(int refclk, intel_clock_t *clock)
  387. {
  388. clock->m = clock->m2 + 2;
  389. clock->p = clock->p1 * clock->p2;
  390. clock->vco = refclk * clock->m / clock->n;
  391. clock->dot = clock->vco / clock->p;
  392. }
  393. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  394. {
  395. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  396. }
  397. static void i9xx_clock(int refclk, intel_clock_t *clock)
  398. {
  399. clock->m = i9xx_dpll_compute_m(clock);
  400. clock->p = clock->p1 * clock->p2;
  401. clock->vco = refclk * clock->m / (clock->n + 2);
  402. clock->dot = clock->vco / clock->p;
  403. }
  404. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  405. /**
  406. * Returns whether the given set of divisors are valid for a given refclk with
  407. * the given connectors.
  408. */
  409. static bool intel_PLL_is_valid(struct drm_device *dev,
  410. const intel_limit_t *limit,
  411. const intel_clock_t *clock)
  412. {
  413. if (clock->n < limit->n.min || limit->n.max < clock->n)
  414. INTELPllInvalid("n out of range\n");
  415. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  416. INTELPllInvalid("p1 out of range\n");
  417. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  418. INTELPllInvalid("m2 out of range\n");
  419. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  420. INTELPllInvalid("m1 out of range\n");
  421. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  422. if (clock->m1 <= clock->m2)
  423. INTELPllInvalid("m1 <= m2\n");
  424. if (!IS_VALLEYVIEW(dev)) {
  425. if (clock->p < limit->p.min || limit->p.max < clock->p)
  426. INTELPllInvalid("p out of range\n");
  427. if (clock->m < limit->m.min || limit->m.max < clock->m)
  428. INTELPllInvalid("m out of range\n");
  429. }
  430. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  431. INTELPllInvalid("vco out of range\n");
  432. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  433. * connector, etc., rather than just a single range.
  434. */
  435. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  436. INTELPllInvalid("dot out of range\n");
  437. return true;
  438. }
  439. static bool
  440. i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  441. int target, int refclk, intel_clock_t *match_clock,
  442. intel_clock_t *best_clock)
  443. {
  444. struct drm_device *dev = crtc->dev;
  445. intel_clock_t clock;
  446. int err = target;
  447. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  448. /*
  449. * For LVDS just rely on its current settings for dual-channel.
  450. * We haven't figured out how to reliably set up different
  451. * single/dual channel state, if we even can.
  452. */
  453. if (intel_is_dual_link_lvds(dev))
  454. clock.p2 = limit->p2.p2_fast;
  455. else
  456. clock.p2 = limit->p2.p2_slow;
  457. } else {
  458. if (target < limit->p2.dot_limit)
  459. clock.p2 = limit->p2.p2_slow;
  460. else
  461. clock.p2 = limit->p2.p2_fast;
  462. }
  463. memset(best_clock, 0, sizeof(*best_clock));
  464. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  465. clock.m1++) {
  466. for (clock.m2 = limit->m2.min;
  467. clock.m2 <= limit->m2.max; clock.m2++) {
  468. if (clock.m2 >= clock.m1)
  469. break;
  470. for (clock.n = limit->n.min;
  471. clock.n <= limit->n.max; clock.n++) {
  472. for (clock.p1 = limit->p1.min;
  473. clock.p1 <= limit->p1.max; clock.p1++) {
  474. int this_err;
  475. i9xx_clock(refclk, &clock);
  476. if (!intel_PLL_is_valid(dev, limit,
  477. &clock))
  478. continue;
  479. if (match_clock &&
  480. clock.p != match_clock->p)
  481. continue;
  482. this_err = abs(clock.dot - target);
  483. if (this_err < err) {
  484. *best_clock = clock;
  485. err = this_err;
  486. }
  487. }
  488. }
  489. }
  490. }
  491. return (err != target);
  492. }
  493. static bool
  494. pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  495. int target, int refclk, intel_clock_t *match_clock,
  496. intel_clock_t *best_clock)
  497. {
  498. struct drm_device *dev = crtc->dev;
  499. intel_clock_t clock;
  500. int err = target;
  501. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  502. /*
  503. * For LVDS just rely on its current settings for dual-channel.
  504. * We haven't figured out how to reliably set up different
  505. * single/dual channel state, if we even can.
  506. */
  507. if (intel_is_dual_link_lvds(dev))
  508. clock.p2 = limit->p2.p2_fast;
  509. else
  510. clock.p2 = limit->p2.p2_slow;
  511. } else {
  512. if (target < limit->p2.dot_limit)
  513. clock.p2 = limit->p2.p2_slow;
  514. else
  515. clock.p2 = limit->p2.p2_fast;
  516. }
  517. memset(best_clock, 0, sizeof(*best_clock));
  518. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  519. clock.m1++) {
  520. for (clock.m2 = limit->m2.min;
  521. clock.m2 <= limit->m2.max; clock.m2++) {
  522. for (clock.n = limit->n.min;
  523. clock.n <= limit->n.max; clock.n++) {
  524. for (clock.p1 = limit->p1.min;
  525. clock.p1 <= limit->p1.max; clock.p1++) {
  526. int this_err;
  527. pineview_clock(refclk, &clock);
  528. if (!intel_PLL_is_valid(dev, limit,
  529. &clock))
  530. continue;
  531. if (match_clock &&
  532. clock.p != match_clock->p)
  533. continue;
  534. this_err = abs(clock.dot - target);
  535. if (this_err < err) {
  536. *best_clock = clock;
  537. err = this_err;
  538. }
  539. }
  540. }
  541. }
  542. }
  543. return (err != target);
  544. }
  545. static bool
  546. g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  547. int target, int refclk, intel_clock_t *match_clock,
  548. intel_clock_t *best_clock)
  549. {
  550. struct drm_device *dev = crtc->dev;
  551. intel_clock_t clock;
  552. int max_n;
  553. bool found;
  554. /* approximately equals target * 0.00585 */
  555. int err_most = (target >> 8) + (target >> 9);
  556. found = false;
  557. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  558. if (intel_is_dual_link_lvds(dev))
  559. clock.p2 = limit->p2.p2_fast;
  560. else
  561. clock.p2 = limit->p2.p2_slow;
  562. } else {
  563. if (target < limit->p2.dot_limit)
  564. clock.p2 = limit->p2.p2_slow;
  565. else
  566. clock.p2 = limit->p2.p2_fast;
  567. }
  568. memset(best_clock, 0, sizeof(*best_clock));
  569. max_n = limit->n.max;
  570. /* based on hardware requirement, prefer smaller n to precision */
  571. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  572. /* based on hardware requirement, prefere larger m1,m2 */
  573. for (clock.m1 = limit->m1.max;
  574. clock.m1 >= limit->m1.min; clock.m1--) {
  575. for (clock.m2 = limit->m2.max;
  576. clock.m2 >= limit->m2.min; clock.m2--) {
  577. for (clock.p1 = limit->p1.max;
  578. clock.p1 >= limit->p1.min; clock.p1--) {
  579. int this_err;
  580. i9xx_clock(refclk, &clock);
  581. if (!intel_PLL_is_valid(dev, limit,
  582. &clock))
  583. continue;
  584. this_err = abs(clock.dot - target);
  585. if (this_err < err_most) {
  586. *best_clock = clock;
  587. err_most = this_err;
  588. max_n = clock.n;
  589. found = true;
  590. }
  591. }
  592. }
  593. }
  594. }
  595. return found;
  596. }
  597. static bool
  598. vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  599. int target, int refclk, intel_clock_t *match_clock,
  600. intel_clock_t *best_clock)
  601. {
  602. struct drm_device *dev = crtc->dev;
  603. intel_clock_t clock;
  604. unsigned int bestppm = 1000000;
  605. /* min update 19.2 MHz */
  606. int max_n = min(limit->n.max, refclk / 19200);
  607. bool found = false;
  608. target *= 5; /* fast clock */
  609. memset(best_clock, 0, sizeof(*best_clock));
  610. /* based on hardware requirement, prefer smaller n to precision */
  611. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  612. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  613. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  614. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  615. clock.p = clock.p1 * clock.p2;
  616. /* based on hardware requirement, prefer bigger m1,m2 values */
  617. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  618. unsigned int ppm, diff;
  619. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  620. refclk * clock.m1);
  621. vlv_clock(refclk, &clock);
  622. if (!intel_PLL_is_valid(dev, limit,
  623. &clock))
  624. continue;
  625. diff = abs(clock.dot - target);
  626. ppm = div_u64(1000000ULL * diff, target);
  627. if (ppm < 100 && clock.p > best_clock->p) {
  628. bestppm = 0;
  629. *best_clock = clock;
  630. found = true;
  631. }
  632. if (bestppm >= 10 && ppm < bestppm - 10) {
  633. bestppm = ppm;
  634. *best_clock = clock;
  635. found = true;
  636. }
  637. }
  638. }
  639. }
  640. }
  641. return found;
  642. }
  643. bool intel_crtc_active(struct drm_crtc *crtc)
  644. {
  645. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  646. /* Be paranoid as we can arrive here with only partial
  647. * state retrieved from the hardware during setup.
  648. *
  649. * We can ditch the adjusted_mode.crtc_clock check as soon
  650. * as Haswell has gained clock readout/fastboot support.
  651. *
  652. * We can ditch the crtc->fb check as soon as we can
  653. * properly reconstruct framebuffers.
  654. */
  655. return intel_crtc->active && crtc->fb &&
  656. intel_crtc->config.adjusted_mode.crtc_clock;
  657. }
  658. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  659. enum pipe pipe)
  660. {
  661. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  663. return intel_crtc->config.cpu_transcoder;
  664. }
  665. static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
  666. {
  667. struct drm_i915_private *dev_priv = dev->dev_private;
  668. u32 frame, frame_reg = PIPEFRAME(pipe);
  669. frame = I915_READ(frame_reg);
  670. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  671. DRM_DEBUG_KMS("vblank wait timed out\n");
  672. }
  673. /**
  674. * intel_wait_for_vblank - wait for vblank on a given pipe
  675. * @dev: drm device
  676. * @pipe: pipe to wait for
  677. *
  678. * Wait for vblank to occur on a given pipe. Needed for various bits of
  679. * mode setting code.
  680. */
  681. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  682. {
  683. struct drm_i915_private *dev_priv = dev->dev_private;
  684. int pipestat_reg = PIPESTAT(pipe);
  685. if (INTEL_INFO(dev)->gen >= 5) {
  686. ironlake_wait_for_vblank(dev, pipe);
  687. return;
  688. }
  689. /* Clear existing vblank status. Note this will clear any other
  690. * sticky status fields as well.
  691. *
  692. * This races with i915_driver_irq_handler() with the result
  693. * that either function could miss a vblank event. Here it is not
  694. * fatal, as we will either wait upon the next vblank interrupt or
  695. * timeout. Generally speaking intel_wait_for_vblank() is only
  696. * called during modeset at which time the GPU should be idle and
  697. * should *not* be performing page flips and thus not waiting on
  698. * vblanks...
  699. * Currently, the result of us stealing a vblank from the irq
  700. * handler is that a single frame will be skipped during swapbuffers.
  701. */
  702. I915_WRITE(pipestat_reg,
  703. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  704. /* Wait for vblank interrupt bit to set */
  705. if (wait_for(I915_READ(pipestat_reg) &
  706. PIPE_VBLANK_INTERRUPT_STATUS,
  707. 50))
  708. DRM_DEBUG_KMS("vblank wait timed out\n");
  709. }
  710. /*
  711. * intel_wait_for_pipe_off - wait for pipe to turn off
  712. * @dev: drm device
  713. * @pipe: pipe to wait for
  714. *
  715. * After disabling a pipe, we can't wait for vblank in the usual way,
  716. * spinning on the vblank interrupt status bit, since we won't actually
  717. * see an interrupt when the pipe is disabled.
  718. *
  719. * On Gen4 and above:
  720. * wait for the pipe register state bit to turn off
  721. *
  722. * Otherwise:
  723. * wait for the display line value to settle (it usually
  724. * ends up stopping at the start of the next frame).
  725. *
  726. */
  727. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  728. {
  729. struct drm_i915_private *dev_priv = dev->dev_private;
  730. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  731. pipe);
  732. if (INTEL_INFO(dev)->gen >= 4) {
  733. int reg = PIPECONF(cpu_transcoder);
  734. /* Wait for the Pipe State to go off */
  735. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  736. 100))
  737. WARN(1, "pipe_off wait timed out\n");
  738. } else {
  739. u32 last_line, line_mask;
  740. int reg = PIPEDSL(pipe);
  741. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  742. if (IS_GEN2(dev))
  743. line_mask = DSL_LINEMASK_GEN2;
  744. else
  745. line_mask = DSL_LINEMASK_GEN3;
  746. /* Wait for the display line to settle */
  747. do {
  748. last_line = I915_READ(reg) & line_mask;
  749. mdelay(5);
  750. } while (((I915_READ(reg) & line_mask) != last_line) &&
  751. time_after(timeout, jiffies));
  752. if (time_after(jiffies, timeout))
  753. WARN(1, "pipe_off wait timed out\n");
  754. }
  755. }
  756. /*
  757. * ibx_digital_port_connected - is the specified port connected?
  758. * @dev_priv: i915 private structure
  759. * @port: the port to test
  760. *
  761. * Returns true if @port is connected, false otherwise.
  762. */
  763. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  764. struct intel_digital_port *port)
  765. {
  766. u32 bit;
  767. if (HAS_PCH_IBX(dev_priv->dev)) {
  768. switch(port->port) {
  769. case PORT_B:
  770. bit = SDE_PORTB_HOTPLUG;
  771. break;
  772. case PORT_C:
  773. bit = SDE_PORTC_HOTPLUG;
  774. break;
  775. case PORT_D:
  776. bit = SDE_PORTD_HOTPLUG;
  777. break;
  778. default:
  779. return true;
  780. }
  781. } else {
  782. switch(port->port) {
  783. case PORT_B:
  784. bit = SDE_PORTB_HOTPLUG_CPT;
  785. break;
  786. case PORT_C:
  787. bit = SDE_PORTC_HOTPLUG_CPT;
  788. break;
  789. case PORT_D:
  790. bit = SDE_PORTD_HOTPLUG_CPT;
  791. break;
  792. default:
  793. return true;
  794. }
  795. }
  796. return I915_READ(SDEISR) & bit;
  797. }
  798. static const char *state_string(bool enabled)
  799. {
  800. return enabled ? "on" : "off";
  801. }
  802. /* Only for pre-ILK configs */
  803. void assert_pll(struct drm_i915_private *dev_priv,
  804. enum pipe pipe, bool state)
  805. {
  806. int reg;
  807. u32 val;
  808. bool cur_state;
  809. reg = DPLL(pipe);
  810. val = I915_READ(reg);
  811. cur_state = !!(val & DPLL_VCO_ENABLE);
  812. WARN(cur_state != state,
  813. "PLL state assertion failure (expected %s, current %s)\n",
  814. state_string(state), state_string(cur_state));
  815. }
  816. /* XXX: the dsi pll is shared between MIPI DSI ports */
  817. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  818. {
  819. u32 val;
  820. bool cur_state;
  821. mutex_lock(&dev_priv->dpio_lock);
  822. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  823. mutex_unlock(&dev_priv->dpio_lock);
  824. cur_state = val & DSI_PLL_VCO_EN;
  825. WARN(cur_state != state,
  826. "DSI PLL state assertion failure (expected %s, current %s)\n",
  827. state_string(state), state_string(cur_state));
  828. }
  829. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  830. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  831. struct intel_shared_dpll *
  832. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  833. {
  834. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  835. if (crtc->config.shared_dpll < 0)
  836. return NULL;
  837. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  838. }
  839. /* For ILK+ */
  840. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  841. struct intel_shared_dpll *pll,
  842. bool state)
  843. {
  844. bool cur_state;
  845. struct intel_dpll_hw_state hw_state;
  846. if (HAS_PCH_LPT(dev_priv->dev)) {
  847. DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
  848. return;
  849. }
  850. if (WARN (!pll,
  851. "asserting DPLL %s with no DPLL\n", state_string(state)))
  852. return;
  853. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  854. WARN(cur_state != state,
  855. "%s assertion failure (expected %s, current %s)\n",
  856. pll->name, state_string(state), state_string(cur_state));
  857. }
  858. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  859. enum pipe pipe, bool state)
  860. {
  861. int reg;
  862. u32 val;
  863. bool cur_state;
  864. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  865. pipe);
  866. if (HAS_DDI(dev_priv->dev)) {
  867. /* DDI does not have a specific FDI_TX register */
  868. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  869. val = I915_READ(reg);
  870. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  871. } else {
  872. reg = FDI_TX_CTL(pipe);
  873. val = I915_READ(reg);
  874. cur_state = !!(val & FDI_TX_ENABLE);
  875. }
  876. WARN(cur_state != state,
  877. "FDI TX state assertion failure (expected %s, current %s)\n",
  878. state_string(state), state_string(cur_state));
  879. }
  880. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  881. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  882. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  883. enum pipe pipe, bool state)
  884. {
  885. int reg;
  886. u32 val;
  887. bool cur_state;
  888. reg = FDI_RX_CTL(pipe);
  889. val = I915_READ(reg);
  890. cur_state = !!(val & FDI_RX_ENABLE);
  891. WARN(cur_state != state,
  892. "FDI RX state assertion failure (expected %s, current %s)\n",
  893. state_string(state), state_string(cur_state));
  894. }
  895. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  896. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  897. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  898. enum pipe pipe)
  899. {
  900. int reg;
  901. u32 val;
  902. /* ILK FDI PLL is always enabled */
  903. if (dev_priv->info->gen == 5)
  904. return;
  905. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  906. if (HAS_DDI(dev_priv->dev))
  907. return;
  908. reg = FDI_TX_CTL(pipe);
  909. val = I915_READ(reg);
  910. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  911. }
  912. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  913. enum pipe pipe, bool state)
  914. {
  915. int reg;
  916. u32 val;
  917. bool cur_state;
  918. reg = FDI_RX_CTL(pipe);
  919. val = I915_READ(reg);
  920. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  921. WARN(cur_state != state,
  922. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  923. state_string(state), state_string(cur_state));
  924. }
  925. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  926. enum pipe pipe)
  927. {
  928. int pp_reg, lvds_reg;
  929. u32 val;
  930. enum pipe panel_pipe = PIPE_A;
  931. bool locked = true;
  932. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  933. pp_reg = PCH_PP_CONTROL;
  934. lvds_reg = PCH_LVDS;
  935. } else {
  936. pp_reg = PP_CONTROL;
  937. lvds_reg = LVDS;
  938. }
  939. val = I915_READ(pp_reg);
  940. if (!(val & PANEL_POWER_ON) ||
  941. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  942. locked = false;
  943. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  944. panel_pipe = PIPE_B;
  945. WARN(panel_pipe == pipe && locked,
  946. "panel assertion failure, pipe %c regs locked\n",
  947. pipe_name(pipe));
  948. }
  949. static void assert_cursor(struct drm_i915_private *dev_priv,
  950. enum pipe pipe, bool state)
  951. {
  952. struct drm_device *dev = dev_priv->dev;
  953. bool cur_state;
  954. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  955. cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
  956. else if (IS_845G(dev) || IS_I865G(dev))
  957. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  958. else
  959. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  960. WARN(cur_state != state,
  961. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  962. pipe_name(pipe), state_string(state), state_string(cur_state));
  963. }
  964. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  965. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  966. void assert_pipe(struct drm_i915_private *dev_priv,
  967. enum pipe pipe, bool state)
  968. {
  969. int reg;
  970. u32 val;
  971. bool cur_state;
  972. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  973. pipe);
  974. /* if we need the pipe A quirk it must be always on */
  975. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  976. state = true;
  977. if (!intel_display_power_enabled(dev_priv->dev,
  978. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  979. cur_state = false;
  980. } else {
  981. reg = PIPECONF(cpu_transcoder);
  982. val = I915_READ(reg);
  983. cur_state = !!(val & PIPECONF_ENABLE);
  984. }
  985. WARN(cur_state != state,
  986. "pipe %c assertion failure (expected %s, current %s)\n",
  987. pipe_name(pipe), state_string(state), state_string(cur_state));
  988. }
  989. static void assert_plane(struct drm_i915_private *dev_priv,
  990. enum plane plane, bool state)
  991. {
  992. int reg;
  993. u32 val;
  994. bool cur_state;
  995. reg = DSPCNTR(plane);
  996. val = I915_READ(reg);
  997. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  998. WARN(cur_state != state,
  999. "plane %c assertion failure (expected %s, current %s)\n",
  1000. plane_name(plane), state_string(state), state_string(cur_state));
  1001. }
  1002. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1003. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1004. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1005. enum pipe pipe)
  1006. {
  1007. struct drm_device *dev = dev_priv->dev;
  1008. int reg, i;
  1009. u32 val;
  1010. int cur_pipe;
  1011. /* Primary planes are fixed to pipes on gen4+ */
  1012. if (INTEL_INFO(dev)->gen >= 4) {
  1013. reg = DSPCNTR(pipe);
  1014. val = I915_READ(reg);
  1015. WARN((val & DISPLAY_PLANE_ENABLE),
  1016. "plane %c assertion failure, should be disabled but not\n",
  1017. plane_name(pipe));
  1018. return;
  1019. }
  1020. /* Need to check both planes against the pipe */
  1021. for_each_pipe(i) {
  1022. reg = DSPCNTR(i);
  1023. val = I915_READ(reg);
  1024. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1025. DISPPLANE_SEL_PIPE_SHIFT;
  1026. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1027. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1028. plane_name(i), pipe_name(pipe));
  1029. }
  1030. }
  1031. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1032. enum pipe pipe)
  1033. {
  1034. struct drm_device *dev = dev_priv->dev;
  1035. int reg, i;
  1036. u32 val;
  1037. if (IS_VALLEYVIEW(dev)) {
  1038. for (i = 0; i < dev_priv->num_plane; i++) {
  1039. reg = SPCNTR(pipe, i);
  1040. val = I915_READ(reg);
  1041. WARN((val & SP_ENABLE),
  1042. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1043. sprite_name(pipe, i), pipe_name(pipe));
  1044. }
  1045. } else if (INTEL_INFO(dev)->gen >= 7) {
  1046. reg = SPRCTL(pipe);
  1047. val = I915_READ(reg);
  1048. WARN((val & SPRITE_ENABLE),
  1049. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1050. plane_name(pipe), pipe_name(pipe));
  1051. } else if (INTEL_INFO(dev)->gen >= 5) {
  1052. reg = DVSCNTR(pipe);
  1053. val = I915_READ(reg);
  1054. WARN((val & DVS_ENABLE),
  1055. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1056. plane_name(pipe), pipe_name(pipe));
  1057. }
  1058. }
  1059. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1060. {
  1061. u32 val;
  1062. bool enabled;
  1063. if (HAS_PCH_LPT(dev_priv->dev)) {
  1064. DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
  1065. return;
  1066. }
  1067. val = I915_READ(PCH_DREF_CONTROL);
  1068. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1069. DREF_SUPERSPREAD_SOURCE_MASK));
  1070. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1071. }
  1072. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1073. enum pipe pipe)
  1074. {
  1075. int reg;
  1076. u32 val;
  1077. bool enabled;
  1078. reg = PCH_TRANSCONF(pipe);
  1079. val = I915_READ(reg);
  1080. enabled = !!(val & TRANS_ENABLE);
  1081. WARN(enabled,
  1082. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1083. pipe_name(pipe));
  1084. }
  1085. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1086. enum pipe pipe, u32 port_sel, u32 val)
  1087. {
  1088. if ((val & DP_PORT_EN) == 0)
  1089. return false;
  1090. if (HAS_PCH_CPT(dev_priv->dev)) {
  1091. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1092. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1093. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1094. return false;
  1095. } else {
  1096. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1097. return false;
  1098. }
  1099. return true;
  1100. }
  1101. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1102. enum pipe pipe, u32 val)
  1103. {
  1104. if ((val & SDVO_ENABLE) == 0)
  1105. return false;
  1106. if (HAS_PCH_CPT(dev_priv->dev)) {
  1107. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1108. return false;
  1109. } else {
  1110. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1111. return false;
  1112. }
  1113. return true;
  1114. }
  1115. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1116. enum pipe pipe, u32 val)
  1117. {
  1118. if ((val & LVDS_PORT_EN) == 0)
  1119. return false;
  1120. if (HAS_PCH_CPT(dev_priv->dev)) {
  1121. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1122. return false;
  1123. } else {
  1124. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1125. return false;
  1126. }
  1127. return true;
  1128. }
  1129. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1130. enum pipe pipe, u32 val)
  1131. {
  1132. if ((val & ADPA_DAC_ENABLE) == 0)
  1133. return false;
  1134. if (HAS_PCH_CPT(dev_priv->dev)) {
  1135. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1136. return false;
  1137. } else {
  1138. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1139. return false;
  1140. }
  1141. return true;
  1142. }
  1143. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1144. enum pipe pipe, int reg, u32 port_sel)
  1145. {
  1146. u32 val = I915_READ(reg);
  1147. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1148. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1149. reg, pipe_name(pipe));
  1150. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1151. && (val & DP_PIPEB_SELECT),
  1152. "IBX PCH dp port still using transcoder B\n");
  1153. }
  1154. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1155. enum pipe pipe, int reg)
  1156. {
  1157. u32 val = I915_READ(reg);
  1158. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1159. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1160. reg, pipe_name(pipe));
  1161. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1162. && (val & SDVO_PIPE_B_SELECT),
  1163. "IBX PCH hdmi port still using transcoder B\n");
  1164. }
  1165. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1166. enum pipe pipe)
  1167. {
  1168. int reg;
  1169. u32 val;
  1170. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1171. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1172. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1173. reg = PCH_ADPA;
  1174. val = I915_READ(reg);
  1175. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1176. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1177. pipe_name(pipe));
  1178. reg = PCH_LVDS;
  1179. val = I915_READ(reg);
  1180. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1181. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1182. pipe_name(pipe));
  1183. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1184. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1185. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1186. }
  1187. static void intel_init_dpio(struct drm_device *dev)
  1188. {
  1189. struct drm_i915_private *dev_priv = dev->dev_private;
  1190. if (!IS_VALLEYVIEW(dev))
  1191. return;
  1192. /*
  1193. * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
  1194. * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
  1195. * a. GUnit 0x2110 bit[0] set to 1 (def 0)
  1196. * b. The other bits such as sfr settings / modesel may all be set
  1197. * to 0.
  1198. *
  1199. * This should only be done on init and resume from S3 with both
  1200. * PLLs disabled, or we risk losing DPIO and PLL synchronization.
  1201. */
  1202. I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
  1203. }
  1204. static void vlv_enable_pll(struct intel_crtc *crtc)
  1205. {
  1206. struct drm_device *dev = crtc->base.dev;
  1207. struct drm_i915_private *dev_priv = dev->dev_private;
  1208. int reg = DPLL(crtc->pipe);
  1209. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1210. assert_pipe_disabled(dev_priv, crtc->pipe);
  1211. /* No really, not for ILK+ */
  1212. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1213. /* PLL is protected by panel, make sure we can write it */
  1214. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1215. assert_panel_unlocked(dev_priv, crtc->pipe);
  1216. I915_WRITE(reg, dpll);
  1217. POSTING_READ(reg);
  1218. udelay(150);
  1219. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1220. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1221. I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
  1222. POSTING_READ(DPLL_MD(crtc->pipe));
  1223. /* We do this three times for luck */
  1224. I915_WRITE(reg, dpll);
  1225. POSTING_READ(reg);
  1226. udelay(150); /* wait for warmup */
  1227. I915_WRITE(reg, dpll);
  1228. POSTING_READ(reg);
  1229. udelay(150); /* wait for warmup */
  1230. I915_WRITE(reg, dpll);
  1231. POSTING_READ(reg);
  1232. udelay(150); /* wait for warmup */
  1233. }
  1234. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1235. {
  1236. struct drm_device *dev = crtc->base.dev;
  1237. struct drm_i915_private *dev_priv = dev->dev_private;
  1238. int reg = DPLL(crtc->pipe);
  1239. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1240. assert_pipe_disabled(dev_priv, crtc->pipe);
  1241. /* No really, not for ILK+ */
  1242. BUG_ON(dev_priv->info->gen >= 5);
  1243. /* PLL is protected by panel, make sure we can write it */
  1244. if (IS_MOBILE(dev) && !IS_I830(dev))
  1245. assert_panel_unlocked(dev_priv, crtc->pipe);
  1246. I915_WRITE(reg, dpll);
  1247. /* Wait for the clocks to stabilize. */
  1248. POSTING_READ(reg);
  1249. udelay(150);
  1250. if (INTEL_INFO(dev)->gen >= 4) {
  1251. I915_WRITE(DPLL_MD(crtc->pipe),
  1252. crtc->config.dpll_hw_state.dpll_md);
  1253. } else {
  1254. /* The pixel multiplier can only be updated once the
  1255. * DPLL is enabled and the clocks are stable.
  1256. *
  1257. * So write it again.
  1258. */
  1259. I915_WRITE(reg, dpll);
  1260. }
  1261. /* We do this three times for luck */
  1262. I915_WRITE(reg, dpll);
  1263. POSTING_READ(reg);
  1264. udelay(150); /* wait for warmup */
  1265. I915_WRITE(reg, dpll);
  1266. POSTING_READ(reg);
  1267. udelay(150); /* wait for warmup */
  1268. I915_WRITE(reg, dpll);
  1269. POSTING_READ(reg);
  1270. udelay(150); /* wait for warmup */
  1271. }
  1272. /**
  1273. * i9xx_disable_pll - disable a PLL
  1274. * @dev_priv: i915 private structure
  1275. * @pipe: pipe PLL to disable
  1276. *
  1277. * Disable the PLL for @pipe, making sure the pipe is off first.
  1278. *
  1279. * Note! This is for pre-ILK only.
  1280. */
  1281. static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1282. {
  1283. /* Don't disable pipe A or pipe A PLLs if needed */
  1284. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1285. return;
  1286. /* Make sure the pipe isn't still relying on us */
  1287. assert_pipe_disabled(dev_priv, pipe);
  1288. I915_WRITE(DPLL(pipe), 0);
  1289. POSTING_READ(DPLL(pipe));
  1290. }
  1291. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1292. {
  1293. u32 val = 0;
  1294. /* Make sure the pipe isn't still relying on us */
  1295. assert_pipe_disabled(dev_priv, pipe);
  1296. /* Leave integrated clock source enabled */
  1297. if (pipe == PIPE_B)
  1298. val = DPLL_INTEGRATED_CRI_CLK_VLV;
  1299. I915_WRITE(DPLL(pipe), val);
  1300. POSTING_READ(DPLL(pipe));
  1301. }
  1302. void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
  1303. {
  1304. u32 port_mask;
  1305. if (!port)
  1306. port_mask = DPLL_PORTB_READY_MASK;
  1307. else
  1308. port_mask = DPLL_PORTC_READY_MASK;
  1309. if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
  1310. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1311. 'B' + port, I915_READ(DPLL(0)));
  1312. }
  1313. /**
  1314. * ironlake_enable_shared_dpll - enable PCH PLL
  1315. * @dev_priv: i915 private structure
  1316. * @pipe: pipe PLL to enable
  1317. *
  1318. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1319. * drives the transcoder clock.
  1320. */
  1321. static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
  1322. {
  1323. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1324. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1325. /* PCH PLLs only available on ILK, SNB and IVB */
  1326. BUG_ON(dev_priv->info->gen < 5);
  1327. if (WARN_ON(pll == NULL))
  1328. return;
  1329. if (WARN_ON(pll->refcount == 0))
  1330. return;
  1331. DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
  1332. pll->name, pll->active, pll->on,
  1333. crtc->base.base.id);
  1334. if (pll->active++) {
  1335. WARN_ON(!pll->on);
  1336. assert_shared_dpll_enabled(dev_priv, pll);
  1337. return;
  1338. }
  1339. WARN_ON(pll->on);
  1340. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1341. pll->enable(dev_priv, pll);
  1342. pll->on = true;
  1343. }
  1344. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1345. {
  1346. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1347. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1348. /* PCH only available on ILK+ */
  1349. BUG_ON(dev_priv->info->gen < 5);
  1350. if (WARN_ON(pll == NULL))
  1351. return;
  1352. if (WARN_ON(pll->refcount == 0))
  1353. return;
  1354. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1355. pll->name, pll->active, pll->on,
  1356. crtc->base.base.id);
  1357. if (WARN_ON(pll->active == 0)) {
  1358. assert_shared_dpll_disabled(dev_priv, pll);
  1359. return;
  1360. }
  1361. assert_shared_dpll_enabled(dev_priv, pll);
  1362. WARN_ON(!pll->on);
  1363. if (--pll->active)
  1364. return;
  1365. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1366. pll->disable(dev_priv, pll);
  1367. pll->on = false;
  1368. }
  1369. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1370. enum pipe pipe)
  1371. {
  1372. struct drm_device *dev = dev_priv->dev;
  1373. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1374. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1375. uint32_t reg, val, pipeconf_val;
  1376. /* PCH only available on ILK+ */
  1377. BUG_ON(dev_priv->info->gen < 5);
  1378. /* Make sure PCH DPLL is enabled */
  1379. assert_shared_dpll_enabled(dev_priv,
  1380. intel_crtc_to_shared_dpll(intel_crtc));
  1381. /* FDI must be feeding us bits for PCH ports */
  1382. assert_fdi_tx_enabled(dev_priv, pipe);
  1383. assert_fdi_rx_enabled(dev_priv, pipe);
  1384. if (HAS_PCH_CPT(dev)) {
  1385. /* Workaround: Set the timing override bit before enabling the
  1386. * pch transcoder. */
  1387. reg = TRANS_CHICKEN2(pipe);
  1388. val = I915_READ(reg);
  1389. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1390. I915_WRITE(reg, val);
  1391. }
  1392. reg = PCH_TRANSCONF(pipe);
  1393. val = I915_READ(reg);
  1394. pipeconf_val = I915_READ(PIPECONF(pipe));
  1395. if (HAS_PCH_IBX(dev_priv->dev)) {
  1396. /*
  1397. * make the BPC in transcoder be consistent with
  1398. * that in pipeconf reg.
  1399. */
  1400. val &= ~PIPECONF_BPC_MASK;
  1401. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1402. }
  1403. val &= ~TRANS_INTERLACE_MASK;
  1404. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1405. if (HAS_PCH_IBX(dev_priv->dev) &&
  1406. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1407. val |= TRANS_LEGACY_INTERLACED_ILK;
  1408. else
  1409. val |= TRANS_INTERLACED;
  1410. else
  1411. val |= TRANS_PROGRESSIVE;
  1412. I915_WRITE(reg, val | TRANS_ENABLE);
  1413. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1414. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1415. }
  1416. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1417. enum transcoder cpu_transcoder)
  1418. {
  1419. u32 val, pipeconf_val;
  1420. /* PCH only available on ILK+ */
  1421. BUG_ON(dev_priv->info->gen < 5);
  1422. /* FDI must be feeding us bits for PCH ports */
  1423. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1424. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1425. /* Workaround: set timing override bit. */
  1426. val = I915_READ(_TRANSA_CHICKEN2);
  1427. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1428. I915_WRITE(_TRANSA_CHICKEN2, val);
  1429. val = TRANS_ENABLE;
  1430. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1431. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1432. PIPECONF_INTERLACED_ILK)
  1433. val |= TRANS_INTERLACED;
  1434. else
  1435. val |= TRANS_PROGRESSIVE;
  1436. I915_WRITE(LPT_TRANSCONF, val);
  1437. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1438. DRM_ERROR("Failed to enable PCH transcoder\n");
  1439. }
  1440. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1441. enum pipe pipe)
  1442. {
  1443. struct drm_device *dev = dev_priv->dev;
  1444. uint32_t reg, val;
  1445. /* FDI relies on the transcoder */
  1446. assert_fdi_tx_disabled(dev_priv, pipe);
  1447. assert_fdi_rx_disabled(dev_priv, pipe);
  1448. /* Ports must be off as well */
  1449. assert_pch_ports_disabled(dev_priv, pipe);
  1450. reg = PCH_TRANSCONF(pipe);
  1451. val = I915_READ(reg);
  1452. val &= ~TRANS_ENABLE;
  1453. I915_WRITE(reg, val);
  1454. /* wait for PCH transcoder off, transcoder state */
  1455. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1456. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1457. if (!HAS_PCH_IBX(dev)) {
  1458. /* Workaround: Clear the timing override chicken bit again. */
  1459. reg = TRANS_CHICKEN2(pipe);
  1460. val = I915_READ(reg);
  1461. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1462. I915_WRITE(reg, val);
  1463. }
  1464. }
  1465. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1466. {
  1467. u32 val;
  1468. val = I915_READ(LPT_TRANSCONF);
  1469. val &= ~TRANS_ENABLE;
  1470. I915_WRITE(LPT_TRANSCONF, val);
  1471. /* wait for PCH transcoder off, transcoder state */
  1472. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1473. DRM_ERROR("Failed to disable PCH transcoder\n");
  1474. /* Workaround: clear timing override bit. */
  1475. val = I915_READ(_TRANSA_CHICKEN2);
  1476. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1477. I915_WRITE(_TRANSA_CHICKEN2, val);
  1478. }
  1479. /**
  1480. * intel_enable_pipe - enable a pipe, asserting requirements
  1481. * @dev_priv: i915 private structure
  1482. * @pipe: pipe to enable
  1483. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1484. *
  1485. * Enable @pipe, making sure that various hardware specific requirements
  1486. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1487. *
  1488. * @pipe should be %PIPE_A or %PIPE_B.
  1489. *
  1490. * Will wait until the pipe is actually running (i.e. first vblank) before
  1491. * returning.
  1492. */
  1493. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1494. bool pch_port, bool dsi)
  1495. {
  1496. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1497. pipe);
  1498. enum pipe pch_transcoder;
  1499. int reg;
  1500. u32 val;
  1501. assert_planes_disabled(dev_priv, pipe);
  1502. assert_cursor_disabled(dev_priv, pipe);
  1503. assert_sprites_disabled(dev_priv, pipe);
  1504. if (HAS_PCH_LPT(dev_priv->dev))
  1505. pch_transcoder = TRANSCODER_A;
  1506. else
  1507. pch_transcoder = pipe;
  1508. /*
  1509. * A pipe without a PLL won't actually be able to drive bits from
  1510. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1511. * need the check.
  1512. */
  1513. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1514. if (dsi)
  1515. assert_dsi_pll_enabled(dev_priv);
  1516. else
  1517. assert_pll_enabled(dev_priv, pipe);
  1518. else {
  1519. if (pch_port) {
  1520. /* if driving the PCH, we need FDI enabled */
  1521. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1522. assert_fdi_tx_pll_enabled(dev_priv,
  1523. (enum pipe) cpu_transcoder);
  1524. }
  1525. /* FIXME: assert CPU port conditions for SNB+ */
  1526. }
  1527. reg = PIPECONF(cpu_transcoder);
  1528. val = I915_READ(reg);
  1529. if (val & PIPECONF_ENABLE)
  1530. return;
  1531. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1532. intel_wait_for_vblank(dev_priv->dev, pipe);
  1533. }
  1534. /**
  1535. * intel_disable_pipe - disable a pipe, asserting requirements
  1536. * @dev_priv: i915 private structure
  1537. * @pipe: pipe to disable
  1538. *
  1539. * Disable @pipe, making sure that various hardware specific requirements
  1540. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1541. *
  1542. * @pipe should be %PIPE_A or %PIPE_B.
  1543. *
  1544. * Will wait until the pipe has shut down before returning.
  1545. */
  1546. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1547. enum pipe pipe)
  1548. {
  1549. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1550. pipe);
  1551. int reg;
  1552. u32 val;
  1553. /*
  1554. * Make sure planes won't keep trying to pump pixels to us,
  1555. * or we might hang the display.
  1556. */
  1557. assert_planes_disabled(dev_priv, pipe);
  1558. assert_cursor_disabled(dev_priv, pipe);
  1559. assert_sprites_disabled(dev_priv, pipe);
  1560. /* Don't disable pipe A or pipe A PLLs if needed */
  1561. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1562. return;
  1563. reg = PIPECONF(cpu_transcoder);
  1564. val = I915_READ(reg);
  1565. if ((val & PIPECONF_ENABLE) == 0)
  1566. return;
  1567. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1568. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1569. }
  1570. /*
  1571. * Plane regs are double buffered, going from enabled->disabled needs a
  1572. * trigger in order to latch. The display address reg provides this.
  1573. */
  1574. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1575. enum plane plane)
  1576. {
  1577. u32 reg = dev_priv->info->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1578. I915_WRITE(reg, I915_READ(reg));
  1579. POSTING_READ(reg);
  1580. }
  1581. /**
  1582. * intel_enable_plane - enable a display plane on a given pipe
  1583. * @dev_priv: i915 private structure
  1584. * @plane: plane to enable
  1585. * @pipe: pipe being fed
  1586. *
  1587. * Enable @plane on @pipe, making sure that @pipe is running first.
  1588. */
  1589. static void intel_enable_plane(struct drm_i915_private *dev_priv,
  1590. enum plane plane, enum pipe pipe)
  1591. {
  1592. struct intel_crtc *intel_crtc =
  1593. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1594. int reg;
  1595. u32 val;
  1596. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1597. assert_pipe_enabled(dev_priv, pipe);
  1598. intel_crtc->primary_disabled = false;
  1599. reg = DSPCNTR(plane);
  1600. val = I915_READ(reg);
  1601. if (val & DISPLAY_PLANE_ENABLE)
  1602. return;
  1603. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1604. intel_flush_primary_plane(dev_priv, plane);
  1605. intel_wait_for_vblank(dev_priv->dev, pipe);
  1606. }
  1607. /**
  1608. * intel_disable_plane - disable a display plane
  1609. * @dev_priv: i915 private structure
  1610. * @plane: plane to disable
  1611. * @pipe: pipe consuming the data
  1612. *
  1613. * Disable @plane; should be an independent operation.
  1614. */
  1615. static void intel_disable_plane(struct drm_i915_private *dev_priv,
  1616. enum plane plane, enum pipe pipe)
  1617. {
  1618. struct intel_crtc *intel_crtc =
  1619. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1620. int reg;
  1621. u32 val;
  1622. intel_crtc->primary_disabled = true;
  1623. reg = DSPCNTR(plane);
  1624. val = I915_READ(reg);
  1625. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  1626. return;
  1627. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1628. intel_flush_primary_plane(dev_priv, plane);
  1629. intel_wait_for_vblank(dev_priv->dev, pipe);
  1630. }
  1631. static bool need_vtd_wa(struct drm_device *dev)
  1632. {
  1633. #ifdef CONFIG_INTEL_IOMMU
  1634. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1635. return true;
  1636. #endif
  1637. return false;
  1638. }
  1639. int
  1640. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1641. struct drm_i915_gem_object *obj,
  1642. struct intel_ring_buffer *pipelined)
  1643. {
  1644. struct drm_i915_private *dev_priv = dev->dev_private;
  1645. u32 alignment;
  1646. int ret;
  1647. switch (obj->tiling_mode) {
  1648. case I915_TILING_NONE:
  1649. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1650. alignment = 128 * 1024;
  1651. else if (INTEL_INFO(dev)->gen >= 4)
  1652. alignment = 4 * 1024;
  1653. else
  1654. alignment = 64 * 1024;
  1655. break;
  1656. case I915_TILING_X:
  1657. /* pin() will align the object as required by fence */
  1658. alignment = 0;
  1659. break;
  1660. case I915_TILING_Y:
  1661. /* Despite that we check this in framebuffer_init userspace can
  1662. * screw us over and change the tiling after the fact. Only
  1663. * pinned buffers can't change their tiling. */
  1664. DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
  1665. return -EINVAL;
  1666. default:
  1667. BUG();
  1668. }
  1669. /* Note that the w/a also requires 64 PTE of padding following the
  1670. * bo. We currently fill all unused PTE with the shadow page and so
  1671. * we should always have valid PTE following the scanout preventing
  1672. * the VT-d warning.
  1673. */
  1674. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1675. alignment = 256 * 1024;
  1676. dev_priv->mm.interruptible = false;
  1677. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1678. if (ret)
  1679. goto err_interruptible;
  1680. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1681. * fence, whereas 965+ only requires a fence if using
  1682. * framebuffer compression. For simplicity, we always install
  1683. * a fence as the cost is not that onerous.
  1684. */
  1685. ret = i915_gem_object_get_fence(obj);
  1686. if (ret)
  1687. goto err_unpin;
  1688. i915_gem_object_pin_fence(obj);
  1689. dev_priv->mm.interruptible = true;
  1690. return 0;
  1691. err_unpin:
  1692. i915_gem_object_unpin_from_display_plane(obj);
  1693. err_interruptible:
  1694. dev_priv->mm.interruptible = true;
  1695. return ret;
  1696. }
  1697. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1698. {
  1699. i915_gem_object_unpin_fence(obj);
  1700. i915_gem_object_unpin_from_display_plane(obj);
  1701. }
  1702. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1703. * is assumed to be a power-of-two. */
  1704. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1705. unsigned int tiling_mode,
  1706. unsigned int cpp,
  1707. unsigned int pitch)
  1708. {
  1709. if (tiling_mode != I915_TILING_NONE) {
  1710. unsigned int tile_rows, tiles;
  1711. tile_rows = *y / 8;
  1712. *y %= 8;
  1713. tiles = *x / (512/cpp);
  1714. *x %= 512/cpp;
  1715. return tile_rows * pitch * 8 + tiles * 4096;
  1716. } else {
  1717. unsigned int offset;
  1718. offset = *y * pitch + *x * cpp;
  1719. *y = 0;
  1720. *x = (offset & 4095) / cpp;
  1721. return offset & -4096;
  1722. }
  1723. }
  1724. static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1725. int x, int y)
  1726. {
  1727. struct drm_device *dev = crtc->dev;
  1728. struct drm_i915_private *dev_priv = dev->dev_private;
  1729. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1730. struct intel_framebuffer *intel_fb;
  1731. struct drm_i915_gem_object *obj;
  1732. int plane = intel_crtc->plane;
  1733. unsigned long linear_offset;
  1734. u32 dspcntr;
  1735. u32 reg;
  1736. switch (plane) {
  1737. case 0:
  1738. case 1:
  1739. break;
  1740. default:
  1741. DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
  1742. return -EINVAL;
  1743. }
  1744. intel_fb = to_intel_framebuffer(fb);
  1745. obj = intel_fb->obj;
  1746. reg = DSPCNTR(plane);
  1747. dspcntr = I915_READ(reg);
  1748. /* Mask out pixel format bits in case we change it */
  1749. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1750. switch (fb->pixel_format) {
  1751. case DRM_FORMAT_C8:
  1752. dspcntr |= DISPPLANE_8BPP;
  1753. break;
  1754. case DRM_FORMAT_XRGB1555:
  1755. case DRM_FORMAT_ARGB1555:
  1756. dspcntr |= DISPPLANE_BGRX555;
  1757. break;
  1758. case DRM_FORMAT_RGB565:
  1759. dspcntr |= DISPPLANE_BGRX565;
  1760. break;
  1761. case DRM_FORMAT_XRGB8888:
  1762. case DRM_FORMAT_ARGB8888:
  1763. dspcntr |= DISPPLANE_BGRX888;
  1764. break;
  1765. case DRM_FORMAT_XBGR8888:
  1766. case DRM_FORMAT_ABGR8888:
  1767. dspcntr |= DISPPLANE_RGBX888;
  1768. break;
  1769. case DRM_FORMAT_XRGB2101010:
  1770. case DRM_FORMAT_ARGB2101010:
  1771. dspcntr |= DISPPLANE_BGRX101010;
  1772. break;
  1773. case DRM_FORMAT_XBGR2101010:
  1774. case DRM_FORMAT_ABGR2101010:
  1775. dspcntr |= DISPPLANE_RGBX101010;
  1776. break;
  1777. default:
  1778. BUG();
  1779. }
  1780. if (INTEL_INFO(dev)->gen >= 4) {
  1781. if (obj->tiling_mode != I915_TILING_NONE)
  1782. dspcntr |= DISPPLANE_TILED;
  1783. else
  1784. dspcntr &= ~DISPPLANE_TILED;
  1785. }
  1786. if (IS_G4X(dev))
  1787. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1788. I915_WRITE(reg, dspcntr);
  1789. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1790. if (INTEL_INFO(dev)->gen >= 4) {
  1791. intel_crtc->dspaddr_offset =
  1792. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  1793. fb->bits_per_pixel / 8,
  1794. fb->pitches[0]);
  1795. linear_offset -= intel_crtc->dspaddr_offset;
  1796. } else {
  1797. intel_crtc->dspaddr_offset = linear_offset;
  1798. }
  1799. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1800. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  1801. fb->pitches[0]);
  1802. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1803. if (INTEL_INFO(dev)->gen >= 4) {
  1804. I915_MODIFY_DISPBASE(DSPSURF(plane),
  1805. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  1806. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1807. I915_WRITE(DSPLINOFF(plane), linear_offset);
  1808. } else
  1809. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  1810. POSTING_READ(reg);
  1811. return 0;
  1812. }
  1813. static int ironlake_update_plane(struct drm_crtc *crtc,
  1814. struct drm_framebuffer *fb, int x, int y)
  1815. {
  1816. struct drm_device *dev = crtc->dev;
  1817. struct drm_i915_private *dev_priv = dev->dev_private;
  1818. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1819. struct intel_framebuffer *intel_fb;
  1820. struct drm_i915_gem_object *obj;
  1821. int plane = intel_crtc->plane;
  1822. unsigned long linear_offset;
  1823. u32 dspcntr;
  1824. u32 reg;
  1825. switch (plane) {
  1826. case 0:
  1827. case 1:
  1828. case 2:
  1829. break;
  1830. default:
  1831. DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
  1832. return -EINVAL;
  1833. }
  1834. intel_fb = to_intel_framebuffer(fb);
  1835. obj = intel_fb->obj;
  1836. reg = DSPCNTR(plane);
  1837. dspcntr = I915_READ(reg);
  1838. /* Mask out pixel format bits in case we change it */
  1839. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1840. switch (fb->pixel_format) {
  1841. case DRM_FORMAT_C8:
  1842. dspcntr |= DISPPLANE_8BPP;
  1843. break;
  1844. case DRM_FORMAT_RGB565:
  1845. dspcntr |= DISPPLANE_BGRX565;
  1846. break;
  1847. case DRM_FORMAT_XRGB8888:
  1848. case DRM_FORMAT_ARGB8888:
  1849. dspcntr |= DISPPLANE_BGRX888;
  1850. break;
  1851. case DRM_FORMAT_XBGR8888:
  1852. case DRM_FORMAT_ABGR8888:
  1853. dspcntr |= DISPPLANE_RGBX888;
  1854. break;
  1855. case DRM_FORMAT_XRGB2101010:
  1856. case DRM_FORMAT_ARGB2101010:
  1857. dspcntr |= DISPPLANE_BGRX101010;
  1858. break;
  1859. case DRM_FORMAT_XBGR2101010:
  1860. case DRM_FORMAT_ABGR2101010:
  1861. dspcntr |= DISPPLANE_RGBX101010;
  1862. break;
  1863. default:
  1864. BUG();
  1865. }
  1866. if (obj->tiling_mode != I915_TILING_NONE)
  1867. dspcntr |= DISPPLANE_TILED;
  1868. else
  1869. dspcntr &= ~DISPPLANE_TILED;
  1870. if (IS_HASWELL(dev))
  1871. dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
  1872. else
  1873. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1874. I915_WRITE(reg, dspcntr);
  1875. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1876. intel_crtc->dspaddr_offset =
  1877. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  1878. fb->bits_per_pixel / 8,
  1879. fb->pitches[0]);
  1880. linear_offset -= intel_crtc->dspaddr_offset;
  1881. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1882. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  1883. fb->pitches[0]);
  1884. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1885. I915_MODIFY_DISPBASE(DSPSURF(plane),
  1886. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  1887. if (IS_HASWELL(dev)) {
  1888. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  1889. } else {
  1890. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1891. I915_WRITE(DSPLINOFF(plane), linear_offset);
  1892. }
  1893. POSTING_READ(reg);
  1894. return 0;
  1895. }
  1896. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1897. static int
  1898. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1899. int x, int y, enum mode_set_atomic state)
  1900. {
  1901. struct drm_device *dev = crtc->dev;
  1902. struct drm_i915_private *dev_priv = dev->dev_private;
  1903. if (dev_priv->display.disable_fbc)
  1904. dev_priv->display.disable_fbc(dev);
  1905. intel_increase_pllclock(crtc);
  1906. return dev_priv->display.update_plane(crtc, fb, x, y);
  1907. }
  1908. void intel_display_handle_reset(struct drm_device *dev)
  1909. {
  1910. struct drm_i915_private *dev_priv = dev->dev_private;
  1911. struct drm_crtc *crtc;
  1912. /*
  1913. * Flips in the rings have been nuked by the reset,
  1914. * so complete all pending flips so that user space
  1915. * will get its events and not get stuck.
  1916. *
  1917. * Also update the base address of all primary
  1918. * planes to the the last fb to make sure we're
  1919. * showing the correct fb after a reset.
  1920. *
  1921. * Need to make two loops over the crtcs so that we
  1922. * don't try to grab a crtc mutex before the
  1923. * pending_flip_queue really got woken up.
  1924. */
  1925. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1926. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1927. enum plane plane = intel_crtc->plane;
  1928. intel_prepare_page_flip(dev, plane);
  1929. intel_finish_page_flip_plane(dev, plane);
  1930. }
  1931. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1932. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1933. mutex_lock(&crtc->mutex);
  1934. if (intel_crtc->active)
  1935. dev_priv->display.update_plane(crtc, crtc->fb,
  1936. crtc->x, crtc->y);
  1937. mutex_unlock(&crtc->mutex);
  1938. }
  1939. }
  1940. static int
  1941. intel_finish_fb(struct drm_framebuffer *old_fb)
  1942. {
  1943. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1944. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1945. bool was_interruptible = dev_priv->mm.interruptible;
  1946. int ret;
  1947. /* Big Hammer, we also need to ensure that any pending
  1948. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1949. * current scanout is retired before unpinning the old
  1950. * framebuffer.
  1951. *
  1952. * This should only fail upon a hung GPU, in which case we
  1953. * can safely continue.
  1954. */
  1955. dev_priv->mm.interruptible = false;
  1956. ret = i915_gem_object_finish_gpu(obj);
  1957. dev_priv->mm.interruptible = was_interruptible;
  1958. return ret;
  1959. }
  1960. static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
  1961. {
  1962. struct drm_device *dev = crtc->dev;
  1963. struct drm_i915_master_private *master_priv;
  1964. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1965. if (!dev->primary->master)
  1966. return;
  1967. master_priv = dev->primary->master->driver_priv;
  1968. if (!master_priv->sarea_priv)
  1969. return;
  1970. switch (intel_crtc->pipe) {
  1971. case 0:
  1972. master_priv->sarea_priv->pipeA_x = x;
  1973. master_priv->sarea_priv->pipeA_y = y;
  1974. break;
  1975. case 1:
  1976. master_priv->sarea_priv->pipeB_x = x;
  1977. master_priv->sarea_priv->pipeB_y = y;
  1978. break;
  1979. default:
  1980. break;
  1981. }
  1982. }
  1983. static int
  1984. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1985. struct drm_framebuffer *fb)
  1986. {
  1987. struct drm_device *dev = crtc->dev;
  1988. struct drm_i915_private *dev_priv = dev->dev_private;
  1989. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1990. struct drm_framebuffer *old_fb;
  1991. int ret;
  1992. /* no fb bound */
  1993. if (!fb) {
  1994. DRM_ERROR("No FB bound\n");
  1995. return 0;
  1996. }
  1997. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  1998. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  1999. plane_name(intel_crtc->plane),
  2000. INTEL_INFO(dev)->num_pipes);
  2001. return -EINVAL;
  2002. }
  2003. mutex_lock(&dev->struct_mutex);
  2004. ret = intel_pin_and_fence_fb_obj(dev,
  2005. to_intel_framebuffer(fb)->obj,
  2006. NULL);
  2007. if (ret != 0) {
  2008. mutex_unlock(&dev->struct_mutex);
  2009. DRM_ERROR("pin & fence failed\n");
  2010. return ret;
  2011. }
  2012. /*
  2013. * Update pipe size and adjust fitter if needed: the reason for this is
  2014. * that in compute_mode_changes we check the native mode (not the pfit
  2015. * mode) to see if we can flip rather than do a full mode set. In the
  2016. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2017. * pfit state, we'll end up with a big fb scanned out into the wrong
  2018. * sized surface.
  2019. *
  2020. * To fix this properly, we need to hoist the checks up into
  2021. * compute_mode_changes (or above), check the actual pfit state and
  2022. * whether the platform allows pfit disable with pipe active, and only
  2023. * then update the pipesrc and pfit state, even on the flip path.
  2024. */
  2025. if (i915_fastboot) {
  2026. const struct drm_display_mode *adjusted_mode =
  2027. &intel_crtc->config.adjusted_mode;
  2028. I915_WRITE(PIPESRC(intel_crtc->pipe),
  2029. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2030. (adjusted_mode->crtc_vdisplay - 1));
  2031. if (!intel_crtc->config.pch_pfit.enabled &&
  2032. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2033. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2034. I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
  2035. I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
  2036. I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
  2037. }
  2038. }
  2039. ret = dev_priv->display.update_plane(crtc, fb, x, y);
  2040. if (ret) {
  2041. intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
  2042. mutex_unlock(&dev->struct_mutex);
  2043. DRM_ERROR("failed to update base address\n");
  2044. return ret;
  2045. }
  2046. old_fb = crtc->fb;
  2047. crtc->fb = fb;
  2048. crtc->x = x;
  2049. crtc->y = y;
  2050. if (old_fb) {
  2051. if (intel_crtc->active && old_fb != fb)
  2052. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2053. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  2054. }
  2055. intel_update_fbc(dev);
  2056. intel_edp_psr_update(dev);
  2057. mutex_unlock(&dev->struct_mutex);
  2058. intel_crtc_update_sarea_pos(crtc, x, y);
  2059. return 0;
  2060. }
  2061. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2062. {
  2063. struct drm_device *dev = crtc->dev;
  2064. struct drm_i915_private *dev_priv = dev->dev_private;
  2065. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2066. int pipe = intel_crtc->pipe;
  2067. u32 reg, temp;
  2068. /* enable normal train */
  2069. reg = FDI_TX_CTL(pipe);
  2070. temp = I915_READ(reg);
  2071. if (IS_IVYBRIDGE(dev)) {
  2072. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2073. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2074. } else {
  2075. temp &= ~FDI_LINK_TRAIN_NONE;
  2076. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2077. }
  2078. I915_WRITE(reg, temp);
  2079. reg = FDI_RX_CTL(pipe);
  2080. temp = I915_READ(reg);
  2081. if (HAS_PCH_CPT(dev)) {
  2082. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2083. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2084. } else {
  2085. temp &= ~FDI_LINK_TRAIN_NONE;
  2086. temp |= FDI_LINK_TRAIN_NONE;
  2087. }
  2088. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2089. /* wait one idle pattern time */
  2090. POSTING_READ(reg);
  2091. udelay(1000);
  2092. /* IVB wants error correction enabled */
  2093. if (IS_IVYBRIDGE(dev))
  2094. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2095. FDI_FE_ERRC_ENABLE);
  2096. }
  2097. static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
  2098. {
  2099. return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
  2100. }
  2101. static void ivb_modeset_global_resources(struct drm_device *dev)
  2102. {
  2103. struct drm_i915_private *dev_priv = dev->dev_private;
  2104. struct intel_crtc *pipe_B_crtc =
  2105. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2106. struct intel_crtc *pipe_C_crtc =
  2107. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2108. uint32_t temp;
  2109. /*
  2110. * When everything is off disable fdi C so that we could enable fdi B
  2111. * with all lanes. Note that we don't care about enabled pipes without
  2112. * an enabled pch encoder.
  2113. */
  2114. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2115. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2116. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2117. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2118. temp = I915_READ(SOUTH_CHICKEN1);
  2119. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2120. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2121. I915_WRITE(SOUTH_CHICKEN1, temp);
  2122. }
  2123. }
  2124. /* The FDI link training functions for ILK/Ibexpeak. */
  2125. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2126. {
  2127. struct drm_device *dev = crtc->dev;
  2128. struct drm_i915_private *dev_priv = dev->dev_private;
  2129. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2130. int pipe = intel_crtc->pipe;
  2131. int plane = intel_crtc->plane;
  2132. u32 reg, temp, tries;
  2133. /* FDI needs bits from pipe & plane first */
  2134. assert_pipe_enabled(dev_priv, pipe);
  2135. assert_plane_enabled(dev_priv, plane);
  2136. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2137. for train result */
  2138. reg = FDI_RX_IMR(pipe);
  2139. temp = I915_READ(reg);
  2140. temp &= ~FDI_RX_SYMBOL_LOCK;
  2141. temp &= ~FDI_RX_BIT_LOCK;
  2142. I915_WRITE(reg, temp);
  2143. I915_READ(reg);
  2144. udelay(150);
  2145. /* enable CPU FDI TX and PCH FDI RX */
  2146. reg = FDI_TX_CTL(pipe);
  2147. temp = I915_READ(reg);
  2148. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2149. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2150. temp &= ~FDI_LINK_TRAIN_NONE;
  2151. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2152. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2153. reg = FDI_RX_CTL(pipe);
  2154. temp = I915_READ(reg);
  2155. temp &= ~FDI_LINK_TRAIN_NONE;
  2156. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2157. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2158. POSTING_READ(reg);
  2159. udelay(150);
  2160. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2161. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2162. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2163. FDI_RX_PHASE_SYNC_POINTER_EN);
  2164. reg = FDI_RX_IIR(pipe);
  2165. for (tries = 0; tries < 5; tries++) {
  2166. temp = I915_READ(reg);
  2167. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2168. if ((temp & FDI_RX_BIT_LOCK)) {
  2169. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2170. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2171. break;
  2172. }
  2173. }
  2174. if (tries == 5)
  2175. DRM_ERROR("FDI train 1 fail!\n");
  2176. /* Train 2 */
  2177. reg = FDI_TX_CTL(pipe);
  2178. temp = I915_READ(reg);
  2179. temp &= ~FDI_LINK_TRAIN_NONE;
  2180. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2181. I915_WRITE(reg, temp);
  2182. reg = FDI_RX_CTL(pipe);
  2183. temp = I915_READ(reg);
  2184. temp &= ~FDI_LINK_TRAIN_NONE;
  2185. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2186. I915_WRITE(reg, temp);
  2187. POSTING_READ(reg);
  2188. udelay(150);
  2189. reg = FDI_RX_IIR(pipe);
  2190. for (tries = 0; tries < 5; tries++) {
  2191. temp = I915_READ(reg);
  2192. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2193. if (temp & FDI_RX_SYMBOL_LOCK) {
  2194. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2195. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2196. break;
  2197. }
  2198. }
  2199. if (tries == 5)
  2200. DRM_ERROR("FDI train 2 fail!\n");
  2201. DRM_DEBUG_KMS("FDI train done\n");
  2202. }
  2203. static const int snb_b_fdi_train_param[] = {
  2204. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2205. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2206. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2207. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2208. };
  2209. /* The FDI link training functions for SNB/Cougarpoint. */
  2210. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2211. {
  2212. struct drm_device *dev = crtc->dev;
  2213. struct drm_i915_private *dev_priv = dev->dev_private;
  2214. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2215. int pipe = intel_crtc->pipe;
  2216. u32 reg, temp, i, retry;
  2217. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2218. for train result */
  2219. reg = FDI_RX_IMR(pipe);
  2220. temp = I915_READ(reg);
  2221. temp &= ~FDI_RX_SYMBOL_LOCK;
  2222. temp &= ~FDI_RX_BIT_LOCK;
  2223. I915_WRITE(reg, temp);
  2224. POSTING_READ(reg);
  2225. udelay(150);
  2226. /* enable CPU FDI TX and PCH FDI RX */
  2227. reg = FDI_TX_CTL(pipe);
  2228. temp = I915_READ(reg);
  2229. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2230. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2231. temp &= ~FDI_LINK_TRAIN_NONE;
  2232. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2233. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2234. /* SNB-B */
  2235. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2236. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2237. I915_WRITE(FDI_RX_MISC(pipe),
  2238. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2239. reg = FDI_RX_CTL(pipe);
  2240. temp = I915_READ(reg);
  2241. if (HAS_PCH_CPT(dev)) {
  2242. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2243. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2244. } else {
  2245. temp &= ~FDI_LINK_TRAIN_NONE;
  2246. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2247. }
  2248. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2249. POSTING_READ(reg);
  2250. udelay(150);
  2251. for (i = 0; i < 4; i++) {
  2252. reg = FDI_TX_CTL(pipe);
  2253. temp = I915_READ(reg);
  2254. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2255. temp |= snb_b_fdi_train_param[i];
  2256. I915_WRITE(reg, temp);
  2257. POSTING_READ(reg);
  2258. udelay(500);
  2259. for (retry = 0; retry < 5; retry++) {
  2260. reg = FDI_RX_IIR(pipe);
  2261. temp = I915_READ(reg);
  2262. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2263. if (temp & FDI_RX_BIT_LOCK) {
  2264. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2265. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2266. break;
  2267. }
  2268. udelay(50);
  2269. }
  2270. if (retry < 5)
  2271. break;
  2272. }
  2273. if (i == 4)
  2274. DRM_ERROR("FDI train 1 fail!\n");
  2275. /* Train 2 */
  2276. reg = FDI_TX_CTL(pipe);
  2277. temp = I915_READ(reg);
  2278. temp &= ~FDI_LINK_TRAIN_NONE;
  2279. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2280. if (IS_GEN6(dev)) {
  2281. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2282. /* SNB-B */
  2283. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2284. }
  2285. I915_WRITE(reg, temp);
  2286. reg = FDI_RX_CTL(pipe);
  2287. temp = I915_READ(reg);
  2288. if (HAS_PCH_CPT(dev)) {
  2289. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2290. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2291. } else {
  2292. temp &= ~FDI_LINK_TRAIN_NONE;
  2293. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2294. }
  2295. I915_WRITE(reg, temp);
  2296. POSTING_READ(reg);
  2297. udelay(150);
  2298. for (i = 0; i < 4; i++) {
  2299. reg = FDI_TX_CTL(pipe);
  2300. temp = I915_READ(reg);
  2301. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2302. temp |= snb_b_fdi_train_param[i];
  2303. I915_WRITE(reg, temp);
  2304. POSTING_READ(reg);
  2305. udelay(500);
  2306. for (retry = 0; retry < 5; retry++) {
  2307. reg = FDI_RX_IIR(pipe);
  2308. temp = I915_READ(reg);
  2309. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2310. if (temp & FDI_RX_SYMBOL_LOCK) {
  2311. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2312. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2313. break;
  2314. }
  2315. udelay(50);
  2316. }
  2317. if (retry < 5)
  2318. break;
  2319. }
  2320. if (i == 4)
  2321. DRM_ERROR("FDI train 2 fail!\n");
  2322. DRM_DEBUG_KMS("FDI train done.\n");
  2323. }
  2324. /* Manual link training for Ivy Bridge A0 parts */
  2325. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2326. {
  2327. struct drm_device *dev = crtc->dev;
  2328. struct drm_i915_private *dev_priv = dev->dev_private;
  2329. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2330. int pipe = intel_crtc->pipe;
  2331. u32 reg, temp, i, j;
  2332. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2333. for train result */
  2334. reg = FDI_RX_IMR(pipe);
  2335. temp = I915_READ(reg);
  2336. temp &= ~FDI_RX_SYMBOL_LOCK;
  2337. temp &= ~FDI_RX_BIT_LOCK;
  2338. I915_WRITE(reg, temp);
  2339. POSTING_READ(reg);
  2340. udelay(150);
  2341. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2342. I915_READ(FDI_RX_IIR(pipe)));
  2343. /* Try each vswing and preemphasis setting twice before moving on */
  2344. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2345. /* disable first in case we need to retry */
  2346. reg = FDI_TX_CTL(pipe);
  2347. temp = I915_READ(reg);
  2348. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2349. temp &= ~FDI_TX_ENABLE;
  2350. I915_WRITE(reg, temp);
  2351. reg = FDI_RX_CTL(pipe);
  2352. temp = I915_READ(reg);
  2353. temp &= ~FDI_LINK_TRAIN_AUTO;
  2354. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2355. temp &= ~FDI_RX_ENABLE;
  2356. I915_WRITE(reg, temp);
  2357. /* enable CPU FDI TX and PCH FDI RX */
  2358. reg = FDI_TX_CTL(pipe);
  2359. temp = I915_READ(reg);
  2360. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2361. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2362. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2363. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2364. temp |= snb_b_fdi_train_param[j/2];
  2365. temp |= FDI_COMPOSITE_SYNC;
  2366. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2367. I915_WRITE(FDI_RX_MISC(pipe),
  2368. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2369. reg = FDI_RX_CTL(pipe);
  2370. temp = I915_READ(reg);
  2371. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2372. temp |= FDI_COMPOSITE_SYNC;
  2373. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2374. POSTING_READ(reg);
  2375. udelay(1); /* should be 0.5us */
  2376. for (i = 0; i < 4; i++) {
  2377. reg = FDI_RX_IIR(pipe);
  2378. temp = I915_READ(reg);
  2379. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2380. if (temp & FDI_RX_BIT_LOCK ||
  2381. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2382. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2383. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2384. i);
  2385. break;
  2386. }
  2387. udelay(1); /* should be 0.5us */
  2388. }
  2389. if (i == 4) {
  2390. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2391. continue;
  2392. }
  2393. /* Train 2 */
  2394. reg = FDI_TX_CTL(pipe);
  2395. temp = I915_READ(reg);
  2396. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2397. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2398. I915_WRITE(reg, temp);
  2399. reg = FDI_RX_CTL(pipe);
  2400. temp = I915_READ(reg);
  2401. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2402. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2403. I915_WRITE(reg, temp);
  2404. POSTING_READ(reg);
  2405. udelay(2); /* should be 1.5us */
  2406. for (i = 0; i < 4; i++) {
  2407. reg = FDI_RX_IIR(pipe);
  2408. temp = I915_READ(reg);
  2409. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2410. if (temp & FDI_RX_SYMBOL_LOCK ||
  2411. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2412. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2413. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2414. i);
  2415. goto train_done;
  2416. }
  2417. udelay(2); /* should be 1.5us */
  2418. }
  2419. if (i == 4)
  2420. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2421. }
  2422. train_done:
  2423. DRM_DEBUG_KMS("FDI train done.\n");
  2424. }
  2425. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2426. {
  2427. struct drm_device *dev = intel_crtc->base.dev;
  2428. struct drm_i915_private *dev_priv = dev->dev_private;
  2429. int pipe = intel_crtc->pipe;
  2430. u32 reg, temp;
  2431. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2432. reg = FDI_RX_CTL(pipe);
  2433. temp = I915_READ(reg);
  2434. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2435. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2436. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2437. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2438. POSTING_READ(reg);
  2439. udelay(200);
  2440. /* Switch from Rawclk to PCDclk */
  2441. temp = I915_READ(reg);
  2442. I915_WRITE(reg, temp | FDI_PCDCLK);
  2443. POSTING_READ(reg);
  2444. udelay(200);
  2445. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2446. reg = FDI_TX_CTL(pipe);
  2447. temp = I915_READ(reg);
  2448. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2449. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2450. POSTING_READ(reg);
  2451. udelay(100);
  2452. }
  2453. }
  2454. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2455. {
  2456. struct drm_device *dev = intel_crtc->base.dev;
  2457. struct drm_i915_private *dev_priv = dev->dev_private;
  2458. int pipe = intel_crtc->pipe;
  2459. u32 reg, temp;
  2460. /* Switch from PCDclk to Rawclk */
  2461. reg = FDI_RX_CTL(pipe);
  2462. temp = I915_READ(reg);
  2463. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2464. /* Disable CPU FDI TX PLL */
  2465. reg = FDI_TX_CTL(pipe);
  2466. temp = I915_READ(reg);
  2467. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2468. POSTING_READ(reg);
  2469. udelay(100);
  2470. reg = FDI_RX_CTL(pipe);
  2471. temp = I915_READ(reg);
  2472. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2473. /* Wait for the clocks to turn off. */
  2474. POSTING_READ(reg);
  2475. udelay(100);
  2476. }
  2477. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2478. {
  2479. struct drm_device *dev = crtc->dev;
  2480. struct drm_i915_private *dev_priv = dev->dev_private;
  2481. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2482. int pipe = intel_crtc->pipe;
  2483. u32 reg, temp;
  2484. /* disable CPU FDI tx and PCH FDI rx */
  2485. reg = FDI_TX_CTL(pipe);
  2486. temp = I915_READ(reg);
  2487. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2488. POSTING_READ(reg);
  2489. reg = FDI_RX_CTL(pipe);
  2490. temp = I915_READ(reg);
  2491. temp &= ~(0x7 << 16);
  2492. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2493. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2494. POSTING_READ(reg);
  2495. udelay(100);
  2496. /* Ironlake workaround, disable clock pointer after downing FDI */
  2497. if (HAS_PCH_IBX(dev)) {
  2498. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2499. }
  2500. /* still set train pattern 1 */
  2501. reg = FDI_TX_CTL(pipe);
  2502. temp = I915_READ(reg);
  2503. temp &= ~FDI_LINK_TRAIN_NONE;
  2504. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2505. I915_WRITE(reg, temp);
  2506. reg = FDI_RX_CTL(pipe);
  2507. temp = I915_READ(reg);
  2508. if (HAS_PCH_CPT(dev)) {
  2509. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2510. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2511. } else {
  2512. temp &= ~FDI_LINK_TRAIN_NONE;
  2513. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2514. }
  2515. /* BPC in FDI rx is consistent with that in PIPECONF */
  2516. temp &= ~(0x07 << 16);
  2517. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2518. I915_WRITE(reg, temp);
  2519. POSTING_READ(reg);
  2520. udelay(100);
  2521. }
  2522. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2523. {
  2524. struct drm_device *dev = crtc->dev;
  2525. struct drm_i915_private *dev_priv = dev->dev_private;
  2526. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2527. unsigned long flags;
  2528. bool pending;
  2529. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2530. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2531. return false;
  2532. spin_lock_irqsave(&dev->event_lock, flags);
  2533. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2534. spin_unlock_irqrestore(&dev->event_lock, flags);
  2535. return pending;
  2536. }
  2537. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2538. {
  2539. struct drm_device *dev = crtc->dev;
  2540. struct drm_i915_private *dev_priv = dev->dev_private;
  2541. if (crtc->fb == NULL)
  2542. return;
  2543. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  2544. wait_event(dev_priv->pending_flip_queue,
  2545. !intel_crtc_has_pending_flip(crtc));
  2546. mutex_lock(&dev->struct_mutex);
  2547. intel_finish_fb(crtc->fb);
  2548. mutex_unlock(&dev->struct_mutex);
  2549. }
  2550. /* Program iCLKIP clock to the desired frequency */
  2551. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2552. {
  2553. struct drm_device *dev = crtc->dev;
  2554. struct drm_i915_private *dev_priv = dev->dev_private;
  2555. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  2556. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2557. u32 temp;
  2558. mutex_lock(&dev_priv->dpio_lock);
  2559. /* It is necessary to ungate the pixclk gate prior to programming
  2560. * the divisors, and gate it back when it is done.
  2561. */
  2562. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2563. /* Disable SSCCTL */
  2564. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2565. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  2566. SBI_SSCCTL_DISABLE,
  2567. SBI_ICLK);
  2568. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2569. if (clock == 20000) {
  2570. auxdiv = 1;
  2571. divsel = 0x41;
  2572. phaseinc = 0x20;
  2573. } else {
  2574. /* The iCLK virtual clock root frequency is in MHz,
  2575. * but the adjusted_mode->crtc_clock in in KHz. To get the
  2576. * divisors, it is necessary to divide one by another, so we
  2577. * convert the virtual clock precision to KHz here for higher
  2578. * precision.
  2579. */
  2580. u32 iclk_virtual_root_freq = 172800 * 1000;
  2581. u32 iclk_pi_range = 64;
  2582. u32 desired_divisor, msb_divisor_value, pi_value;
  2583. desired_divisor = (iclk_virtual_root_freq / clock);
  2584. msb_divisor_value = desired_divisor / iclk_pi_range;
  2585. pi_value = desired_divisor % iclk_pi_range;
  2586. auxdiv = 0;
  2587. divsel = msb_divisor_value - 2;
  2588. phaseinc = pi_value;
  2589. }
  2590. /* This should not happen with any sane values */
  2591. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2592. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2593. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2594. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2595. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2596. clock,
  2597. auxdiv,
  2598. divsel,
  2599. phasedir,
  2600. phaseinc);
  2601. /* Program SSCDIVINTPHASE6 */
  2602. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  2603. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2604. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2605. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2606. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2607. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2608. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2609. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  2610. /* Program SSCAUXDIV */
  2611. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  2612. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2613. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2614. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  2615. /* Enable modulator and associated divider */
  2616. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  2617. temp &= ~SBI_SSCCTL_DISABLE;
  2618. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  2619. /* Wait for initialization time */
  2620. udelay(24);
  2621. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2622. mutex_unlock(&dev_priv->dpio_lock);
  2623. }
  2624. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  2625. enum pipe pch_transcoder)
  2626. {
  2627. struct drm_device *dev = crtc->base.dev;
  2628. struct drm_i915_private *dev_priv = dev->dev_private;
  2629. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  2630. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  2631. I915_READ(HTOTAL(cpu_transcoder)));
  2632. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  2633. I915_READ(HBLANK(cpu_transcoder)));
  2634. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  2635. I915_READ(HSYNC(cpu_transcoder)));
  2636. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  2637. I915_READ(VTOTAL(cpu_transcoder)));
  2638. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  2639. I915_READ(VBLANK(cpu_transcoder)));
  2640. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  2641. I915_READ(VSYNC(cpu_transcoder)));
  2642. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  2643. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  2644. }
  2645. /*
  2646. * Enable PCH resources required for PCH ports:
  2647. * - PCH PLLs
  2648. * - FDI training & RX/TX
  2649. * - update transcoder timings
  2650. * - DP transcoding bits
  2651. * - transcoder
  2652. */
  2653. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2654. {
  2655. struct drm_device *dev = crtc->dev;
  2656. struct drm_i915_private *dev_priv = dev->dev_private;
  2657. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2658. int pipe = intel_crtc->pipe;
  2659. u32 reg, temp;
  2660. assert_pch_transcoder_disabled(dev_priv, pipe);
  2661. /* Write the TU size bits before fdi link training, so that error
  2662. * detection works. */
  2663. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2664. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2665. /* For PCH output, training FDI link */
  2666. dev_priv->display.fdi_link_train(crtc);
  2667. /* We need to program the right clock selection before writing the pixel
  2668. * mutliplier into the DPLL. */
  2669. if (HAS_PCH_CPT(dev)) {
  2670. u32 sel;
  2671. temp = I915_READ(PCH_DPLL_SEL);
  2672. temp |= TRANS_DPLL_ENABLE(pipe);
  2673. sel = TRANS_DPLLB_SEL(pipe);
  2674. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  2675. temp |= sel;
  2676. else
  2677. temp &= ~sel;
  2678. I915_WRITE(PCH_DPLL_SEL, temp);
  2679. }
  2680. /* XXX: pch pll's can be enabled any time before we enable the PCH
  2681. * transcoder, and we actually should do this to not upset any PCH
  2682. * transcoder that already use the clock when we share it.
  2683. *
  2684. * Note that enable_shared_dpll tries to do the right thing, but
  2685. * get_shared_dpll unconditionally resets the pll - we need that to have
  2686. * the right LVDS enable sequence. */
  2687. ironlake_enable_shared_dpll(intel_crtc);
  2688. /* set transcoder timing, panel must allow it */
  2689. assert_panel_unlocked(dev_priv, pipe);
  2690. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  2691. intel_fdi_normal_train(crtc);
  2692. /* For PCH DP, enable TRANS_DP_CTL */
  2693. if (HAS_PCH_CPT(dev) &&
  2694. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  2695. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2696. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  2697. reg = TRANS_DP_CTL(pipe);
  2698. temp = I915_READ(reg);
  2699. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2700. TRANS_DP_SYNC_MASK |
  2701. TRANS_DP_BPC_MASK);
  2702. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2703. TRANS_DP_ENH_FRAMING);
  2704. temp |= bpc << 9; /* same format but at 11:9 */
  2705. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2706. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2707. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2708. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2709. switch (intel_trans_dp_port_sel(crtc)) {
  2710. case PCH_DP_B:
  2711. temp |= TRANS_DP_PORT_SEL_B;
  2712. break;
  2713. case PCH_DP_C:
  2714. temp |= TRANS_DP_PORT_SEL_C;
  2715. break;
  2716. case PCH_DP_D:
  2717. temp |= TRANS_DP_PORT_SEL_D;
  2718. break;
  2719. default:
  2720. BUG();
  2721. }
  2722. I915_WRITE(reg, temp);
  2723. }
  2724. ironlake_enable_pch_transcoder(dev_priv, pipe);
  2725. }
  2726. static void lpt_pch_enable(struct drm_crtc *crtc)
  2727. {
  2728. struct drm_device *dev = crtc->dev;
  2729. struct drm_i915_private *dev_priv = dev->dev_private;
  2730. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2731. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  2732. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  2733. lpt_program_iclkip(crtc);
  2734. /* Set transcoder timing. */
  2735. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  2736. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  2737. }
  2738. static void intel_put_shared_dpll(struct intel_crtc *crtc)
  2739. {
  2740. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  2741. if (pll == NULL)
  2742. return;
  2743. if (pll->refcount == 0) {
  2744. WARN(1, "bad %s refcount\n", pll->name);
  2745. return;
  2746. }
  2747. if (--pll->refcount == 0) {
  2748. WARN_ON(pll->on);
  2749. WARN_ON(pll->active);
  2750. }
  2751. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  2752. }
  2753. static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  2754. {
  2755. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  2756. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  2757. enum intel_dpll_id i;
  2758. if (pll) {
  2759. DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
  2760. crtc->base.base.id, pll->name);
  2761. intel_put_shared_dpll(crtc);
  2762. }
  2763. if (HAS_PCH_IBX(dev_priv->dev)) {
  2764. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  2765. i = (enum intel_dpll_id) crtc->pipe;
  2766. pll = &dev_priv->shared_dplls[i];
  2767. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  2768. crtc->base.base.id, pll->name);
  2769. goto found;
  2770. }
  2771. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  2772. pll = &dev_priv->shared_dplls[i];
  2773. /* Only want to check enabled timings first */
  2774. if (pll->refcount == 0)
  2775. continue;
  2776. if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
  2777. sizeof(pll->hw_state)) == 0) {
  2778. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
  2779. crtc->base.base.id,
  2780. pll->name, pll->refcount, pll->active);
  2781. goto found;
  2782. }
  2783. }
  2784. /* Ok no matching timings, maybe there's a free one? */
  2785. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  2786. pll = &dev_priv->shared_dplls[i];
  2787. if (pll->refcount == 0) {
  2788. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  2789. crtc->base.base.id, pll->name);
  2790. goto found;
  2791. }
  2792. }
  2793. return NULL;
  2794. found:
  2795. crtc->config.shared_dpll = i;
  2796. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  2797. pipe_name(crtc->pipe));
  2798. if (pll->active == 0) {
  2799. memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
  2800. sizeof(pll->hw_state));
  2801. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  2802. WARN_ON(pll->on);
  2803. assert_shared_dpll_disabled(dev_priv, pll);
  2804. pll->mode_set(dev_priv, pll);
  2805. }
  2806. pll->refcount++;
  2807. return pll;
  2808. }
  2809. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  2810. {
  2811. struct drm_i915_private *dev_priv = dev->dev_private;
  2812. int dslreg = PIPEDSL(pipe);
  2813. u32 temp;
  2814. temp = I915_READ(dslreg);
  2815. udelay(500);
  2816. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  2817. if (wait_for(I915_READ(dslreg) != temp, 5))
  2818. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  2819. }
  2820. }
  2821. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  2822. {
  2823. struct drm_device *dev = crtc->base.dev;
  2824. struct drm_i915_private *dev_priv = dev->dev_private;
  2825. int pipe = crtc->pipe;
  2826. if (crtc->config.pch_pfit.enabled) {
  2827. /* Force use of hard-coded filter coefficients
  2828. * as some pre-programmed values are broken,
  2829. * e.g. x201.
  2830. */
  2831. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  2832. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  2833. PF_PIPE_SEL_IVB(pipe));
  2834. else
  2835. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  2836. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  2837. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  2838. }
  2839. }
  2840. static void intel_enable_planes(struct drm_crtc *crtc)
  2841. {
  2842. struct drm_device *dev = crtc->dev;
  2843. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  2844. struct intel_plane *intel_plane;
  2845. list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
  2846. if (intel_plane->pipe == pipe)
  2847. intel_plane_restore(&intel_plane->base);
  2848. }
  2849. static void intel_disable_planes(struct drm_crtc *crtc)
  2850. {
  2851. struct drm_device *dev = crtc->dev;
  2852. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  2853. struct intel_plane *intel_plane;
  2854. list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
  2855. if (intel_plane->pipe == pipe)
  2856. intel_plane_disable(&intel_plane->base);
  2857. }
  2858. void hsw_enable_ips(struct intel_crtc *crtc)
  2859. {
  2860. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  2861. if (!crtc->config.ips_enabled)
  2862. return;
  2863. /* We can only enable IPS after we enable a plane and wait for a vblank.
  2864. * We guarantee that the plane is enabled by calling intel_enable_ips
  2865. * only after intel_enable_plane. And intel_enable_plane already waits
  2866. * for a vblank, so all we need to do here is to enable the IPS bit. */
  2867. assert_plane_enabled(dev_priv, crtc->plane);
  2868. I915_WRITE(IPS_CTL, IPS_ENABLE);
  2869. }
  2870. void hsw_disable_ips(struct intel_crtc *crtc)
  2871. {
  2872. struct drm_device *dev = crtc->base.dev;
  2873. struct drm_i915_private *dev_priv = dev->dev_private;
  2874. if (!crtc->config.ips_enabled)
  2875. return;
  2876. assert_plane_enabled(dev_priv, crtc->plane);
  2877. I915_WRITE(IPS_CTL, 0);
  2878. POSTING_READ(IPS_CTL);
  2879. /* We need to wait for a vblank before we can disable the plane. */
  2880. intel_wait_for_vblank(dev, crtc->pipe);
  2881. }
  2882. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  2883. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  2884. {
  2885. struct drm_device *dev = crtc->dev;
  2886. struct drm_i915_private *dev_priv = dev->dev_private;
  2887. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2888. enum pipe pipe = intel_crtc->pipe;
  2889. int palreg = PALETTE(pipe);
  2890. int i;
  2891. bool reenable_ips = false;
  2892. /* The clocks have to be on to load the palette. */
  2893. if (!crtc->enabled || !intel_crtc->active)
  2894. return;
  2895. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  2896. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  2897. assert_dsi_pll_enabled(dev_priv);
  2898. else
  2899. assert_pll_enabled(dev_priv, pipe);
  2900. }
  2901. /* use legacy palette for Ironlake */
  2902. if (HAS_PCH_SPLIT(dev))
  2903. palreg = LGC_PALETTE(pipe);
  2904. /* Workaround : Do not read or write the pipe palette/gamma data while
  2905. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  2906. */
  2907. if (intel_crtc->config.ips_enabled &&
  2908. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  2909. GAMMA_MODE_MODE_SPLIT)) {
  2910. hsw_disable_ips(intel_crtc);
  2911. reenable_ips = true;
  2912. }
  2913. for (i = 0; i < 256; i++) {
  2914. I915_WRITE(palreg + 4 * i,
  2915. (intel_crtc->lut_r[i] << 16) |
  2916. (intel_crtc->lut_g[i] << 8) |
  2917. intel_crtc->lut_b[i]);
  2918. }
  2919. if (reenable_ips)
  2920. hsw_enable_ips(intel_crtc);
  2921. }
  2922. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2923. {
  2924. struct drm_device *dev = crtc->dev;
  2925. struct drm_i915_private *dev_priv = dev->dev_private;
  2926. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2927. struct intel_encoder *encoder;
  2928. int pipe = intel_crtc->pipe;
  2929. int plane = intel_crtc->plane;
  2930. WARN_ON(!crtc->enabled);
  2931. if (intel_crtc->active)
  2932. return;
  2933. intel_crtc->active = true;
  2934. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  2935. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  2936. for_each_encoder_on_crtc(dev, crtc, encoder)
  2937. if (encoder->pre_enable)
  2938. encoder->pre_enable(encoder);
  2939. if (intel_crtc->config.has_pch_encoder) {
  2940. /* Note: FDI PLL enabling _must_ be done before we enable the
  2941. * cpu pipes, hence this is separate from all the other fdi/pch
  2942. * enabling. */
  2943. ironlake_fdi_pll_enable(intel_crtc);
  2944. } else {
  2945. assert_fdi_tx_disabled(dev_priv, pipe);
  2946. assert_fdi_rx_disabled(dev_priv, pipe);
  2947. }
  2948. ironlake_pfit_enable(intel_crtc);
  2949. /*
  2950. * On ILK+ LUT must be loaded before the pipe is running but with
  2951. * clocks enabled
  2952. */
  2953. intel_crtc_load_lut(crtc);
  2954. intel_update_watermarks(crtc);
  2955. intel_enable_pipe(dev_priv, pipe,
  2956. intel_crtc->config.has_pch_encoder, false);
  2957. intel_enable_plane(dev_priv, plane, pipe);
  2958. intel_enable_planes(crtc);
  2959. intel_crtc_update_cursor(crtc, true);
  2960. if (intel_crtc->config.has_pch_encoder)
  2961. ironlake_pch_enable(crtc);
  2962. mutex_lock(&dev->struct_mutex);
  2963. intel_update_fbc(dev);
  2964. mutex_unlock(&dev->struct_mutex);
  2965. for_each_encoder_on_crtc(dev, crtc, encoder)
  2966. encoder->enable(encoder);
  2967. if (HAS_PCH_CPT(dev))
  2968. cpt_verify_modeset(dev, intel_crtc->pipe);
  2969. /*
  2970. * There seems to be a race in PCH platform hw (at least on some
  2971. * outputs) where an enabled pipe still completes any pageflip right
  2972. * away (as if the pipe is off) instead of waiting for vblank. As soon
  2973. * as the first vblank happend, everything works as expected. Hence just
  2974. * wait for one vblank before returning to avoid strange things
  2975. * happening.
  2976. */
  2977. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2978. }
  2979. /* IPS only exists on ULT machines and is tied to pipe A. */
  2980. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  2981. {
  2982. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  2983. }
  2984. static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
  2985. {
  2986. struct drm_device *dev = crtc->dev;
  2987. struct drm_i915_private *dev_priv = dev->dev_private;
  2988. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2989. int pipe = intel_crtc->pipe;
  2990. int plane = intel_crtc->plane;
  2991. intel_enable_plane(dev_priv, plane, pipe);
  2992. intel_enable_planes(crtc);
  2993. intel_crtc_update_cursor(crtc, true);
  2994. hsw_enable_ips(intel_crtc);
  2995. mutex_lock(&dev->struct_mutex);
  2996. intel_update_fbc(dev);
  2997. mutex_unlock(&dev->struct_mutex);
  2998. }
  2999. static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
  3000. {
  3001. struct drm_device *dev = crtc->dev;
  3002. struct drm_i915_private *dev_priv = dev->dev_private;
  3003. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3004. int pipe = intel_crtc->pipe;
  3005. int plane = intel_crtc->plane;
  3006. intel_crtc_wait_for_pending_flips(crtc);
  3007. drm_vblank_off(dev, pipe);
  3008. /* FBC must be disabled before disabling the plane on HSW. */
  3009. if (dev_priv->fbc.plane == plane)
  3010. intel_disable_fbc(dev);
  3011. hsw_disable_ips(intel_crtc);
  3012. intel_crtc_update_cursor(crtc, false);
  3013. intel_disable_planes(crtc);
  3014. intel_disable_plane(dev_priv, plane, pipe);
  3015. }
  3016. /*
  3017. * This implements the workaround described in the "notes" section of the mode
  3018. * set sequence documentation. When going from no pipes or single pipe to
  3019. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3020. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3021. */
  3022. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3023. {
  3024. struct drm_device *dev = crtc->base.dev;
  3025. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3026. /* We want to get the other_active_crtc only if there's only 1 other
  3027. * active crtc. */
  3028. list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
  3029. if (!crtc_it->active || crtc_it == crtc)
  3030. continue;
  3031. if (other_active_crtc)
  3032. return;
  3033. other_active_crtc = crtc_it;
  3034. }
  3035. if (!other_active_crtc)
  3036. return;
  3037. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3038. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3039. }
  3040. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3041. {
  3042. struct drm_device *dev = crtc->dev;
  3043. struct drm_i915_private *dev_priv = dev->dev_private;
  3044. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3045. struct intel_encoder *encoder;
  3046. int pipe = intel_crtc->pipe;
  3047. WARN_ON(!crtc->enabled);
  3048. if (intel_crtc->active)
  3049. return;
  3050. intel_crtc->active = true;
  3051. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3052. if (intel_crtc->config.has_pch_encoder)
  3053. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3054. if (intel_crtc->config.has_pch_encoder)
  3055. dev_priv->display.fdi_link_train(crtc);
  3056. for_each_encoder_on_crtc(dev, crtc, encoder)
  3057. if (encoder->pre_enable)
  3058. encoder->pre_enable(encoder);
  3059. intel_ddi_enable_pipe_clock(intel_crtc);
  3060. ironlake_pfit_enable(intel_crtc);
  3061. /*
  3062. * On ILK+ LUT must be loaded before the pipe is running but with
  3063. * clocks enabled
  3064. */
  3065. intel_crtc_load_lut(crtc);
  3066. intel_ddi_set_pipe_settings(crtc);
  3067. intel_ddi_enable_transcoder_func(crtc);
  3068. intel_update_watermarks(crtc);
  3069. intel_enable_pipe(dev_priv, pipe,
  3070. intel_crtc->config.has_pch_encoder, false);
  3071. if (intel_crtc->config.has_pch_encoder)
  3072. lpt_pch_enable(crtc);
  3073. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3074. encoder->enable(encoder);
  3075. intel_opregion_notify_encoder(encoder, true);
  3076. }
  3077. /* If we change the relative order between pipe/planes enabling, we need
  3078. * to change the workaround. */
  3079. haswell_mode_set_planes_workaround(intel_crtc);
  3080. haswell_crtc_enable_planes(crtc);
  3081. /*
  3082. * There seems to be a race in PCH platform hw (at least on some
  3083. * outputs) where an enabled pipe still completes any pageflip right
  3084. * away (as if the pipe is off) instead of waiting for vblank. As soon
  3085. * as the first vblank happend, everything works as expected. Hence just
  3086. * wait for one vblank before returning to avoid strange things
  3087. * happening.
  3088. */
  3089. intel_wait_for_vblank(dev, intel_crtc->pipe);
  3090. }
  3091. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3092. {
  3093. struct drm_device *dev = crtc->base.dev;
  3094. struct drm_i915_private *dev_priv = dev->dev_private;
  3095. int pipe = crtc->pipe;
  3096. /* To avoid upsetting the power well on haswell only disable the pfit if
  3097. * it's in use. The hw state code will make sure we get this right. */
  3098. if (crtc->config.pch_pfit.enabled) {
  3099. I915_WRITE(PF_CTL(pipe), 0);
  3100. I915_WRITE(PF_WIN_POS(pipe), 0);
  3101. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3102. }
  3103. }
  3104. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3105. {
  3106. struct drm_device *dev = crtc->dev;
  3107. struct drm_i915_private *dev_priv = dev->dev_private;
  3108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3109. struct intel_encoder *encoder;
  3110. int pipe = intel_crtc->pipe;
  3111. int plane = intel_crtc->plane;
  3112. u32 reg, temp;
  3113. if (!intel_crtc->active)
  3114. return;
  3115. for_each_encoder_on_crtc(dev, crtc, encoder)
  3116. encoder->disable(encoder);
  3117. intel_crtc_wait_for_pending_flips(crtc);
  3118. drm_vblank_off(dev, pipe);
  3119. if (dev_priv->fbc.plane == plane)
  3120. intel_disable_fbc(dev);
  3121. intel_crtc_update_cursor(crtc, false);
  3122. intel_disable_planes(crtc);
  3123. intel_disable_plane(dev_priv, plane, pipe);
  3124. if (intel_crtc->config.has_pch_encoder)
  3125. intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
  3126. intel_disable_pipe(dev_priv, pipe);
  3127. ironlake_pfit_disable(intel_crtc);
  3128. for_each_encoder_on_crtc(dev, crtc, encoder)
  3129. if (encoder->post_disable)
  3130. encoder->post_disable(encoder);
  3131. if (intel_crtc->config.has_pch_encoder) {
  3132. ironlake_fdi_disable(crtc);
  3133. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3134. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3135. if (HAS_PCH_CPT(dev)) {
  3136. /* disable TRANS_DP_CTL */
  3137. reg = TRANS_DP_CTL(pipe);
  3138. temp = I915_READ(reg);
  3139. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3140. TRANS_DP_PORT_SEL_MASK);
  3141. temp |= TRANS_DP_PORT_SEL_NONE;
  3142. I915_WRITE(reg, temp);
  3143. /* disable DPLL_SEL */
  3144. temp = I915_READ(PCH_DPLL_SEL);
  3145. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3146. I915_WRITE(PCH_DPLL_SEL, temp);
  3147. }
  3148. /* disable PCH DPLL */
  3149. intel_disable_shared_dpll(intel_crtc);
  3150. ironlake_fdi_pll_disable(intel_crtc);
  3151. }
  3152. intel_crtc->active = false;
  3153. intel_update_watermarks(crtc);
  3154. mutex_lock(&dev->struct_mutex);
  3155. intel_update_fbc(dev);
  3156. mutex_unlock(&dev->struct_mutex);
  3157. }
  3158. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3159. {
  3160. struct drm_device *dev = crtc->dev;
  3161. struct drm_i915_private *dev_priv = dev->dev_private;
  3162. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3163. struct intel_encoder *encoder;
  3164. int pipe = intel_crtc->pipe;
  3165. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3166. if (!intel_crtc->active)
  3167. return;
  3168. haswell_crtc_disable_planes(crtc);
  3169. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3170. intel_opregion_notify_encoder(encoder, false);
  3171. encoder->disable(encoder);
  3172. }
  3173. if (intel_crtc->config.has_pch_encoder)
  3174. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
  3175. intel_disable_pipe(dev_priv, pipe);
  3176. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3177. ironlake_pfit_disable(intel_crtc);
  3178. intel_ddi_disable_pipe_clock(intel_crtc);
  3179. for_each_encoder_on_crtc(dev, crtc, encoder)
  3180. if (encoder->post_disable)
  3181. encoder->post_disable(encoder);
  3182. if (intel_crtc->config.has_pch_encoder) {
  3183. lpt_disable_pch_transcoder(dev_priv);
  3184. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3185. intel_ddi_fdi_disable(crtc);
  3186. }
  3187. intel_crtc->active = false;
  3188. intel_update_watermarks(crtc);
  3189. mutex_lock(&dev->struct_mutex);
  3190. intel_update_fbc(dev);
  3191. mutex_unlock(&dev->struct_mutex);
  3192. }
  3193. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3194. {
  3195. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3196. intel_put_shared_dpll(intel_crtc);
  3197. }
  3198. static void haswell_crtc_off(struct drm_crtc *crtc)
  3199. {
  3200. intel_ddi_put_crtc_pll(crtc);
  3201. }
  3202. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3203. {
  3204. if (!enable && intel_crtc->overlay) {
  3205. struct drm_device *dev = intel_crtc->base.dev;
  3206. struct drm_i915_private *dev_priv = dev->dev_private;
  3207. mutex_lock(&dev->struct_mutex);
  3208. dev_priv->mm.interruptible = false;
  3209. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3210. dev_priv->mm.interruptible = true;
  3211. mutex_unlock(&dev->struct_mutex);
  3212. }
  3213. /* Let userspace switch the overlay on again. In most cases userspace
  3214. * has to recompute where to put it anyway.
  3215. */
  3216. }
  3217. /**
  3218. * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
  3219. * cursor plane briefly if not already running after enabling the display
  3220. * plane.
  3221. * This workaround avoids occasional blank screens when self refresh is
  3222. * enabled.
  3223. */
  3224. static void
  3225. g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
  3226. {
  3227. u32 cntl = I915_READ(CURCNTR(pipe));
  3228. if ((cntl & CURSOR_MODE) == 0) {
  3229. u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
  3230. I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
  3231. I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
  3232. intel_wait_for_vblank(dev_priv->dev, pipe);
  3233. I915_WRITE(CURCNTR(pipe), cntl);
  3234. I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
  3235. I915_WRITE(FW_BLC_SELF, fw_bcl_self);
  3236. }
  3237. }
  3238. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3239. {
  3240. struct drm_device *dev = crtc->base.dev;
  3241. struct drm_i915_private *dev_priv = dev->dev_private;
  3242. struct intel_crtc_config *pipe_config = &crtc->config;
  3243. if (!crtc->config.gmch_pfit.control)
  3244. return;
  3245. /*
  3246. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3247. * according to register description and PRM.
  3248. */
  3249. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3250. assert_pipe_disabled(dev_priv, crtc->pipe);
  3251. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3252. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3253. /* Border color in case we don't scale up to the full screen. Black by
  3254. * default, change to something else for debugging. */
  3255. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3256. }
  3257. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  3258. {
  3259. struct drm_device *dev = crtc->dev;
  3260. struct drm_i915_private *dev_priv = dev->dev_private;
  3261. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3262. struct intel_encoder *encoder;
  3263. int pipe = intel_crtc->pipe;
  3264. int plane = intel_crtc->plane;
  3265. bool is_dsi;
  3266. WARN_ON(!crtc->enabled);
  3267. if (intel_crtc->active)
  3268. return;
  3269. intel_crtc->active = true;
  3270. for_each_encoder_on_crtc(dev, crtc, encoder)
  3271. if (encoder->pre_pll_enable)
  3272. encoder->pre_pll_enable(encoder);
  3273. is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
  3274. if (!is_dsi)
  3275. vlv_enable_pll(intel_crtc);
  3276. for_each_encoder_on_crtc(dev, crtc, encoder)
  3277. if (encoder->pre_enable)
  3278. encoder->pre_enable(encoder);
  3279. i9xx_pfit_enable(intel_crtc);
  3280. intel_crtc_load_lut(crtc);
  3281. intel_update_watermarks(crtc);
  3282. intel_enable_pipe(dev_priv, pipe, false, is_dsi);
  3283. intel_enable_plane(dev_priv, plane, pipe);
  3284. intel_enable_planes(crtc);
  3285. intel_crtc_update_cursor(crtc, true);
  3286. intel_update_fbc(dev);
  3287. for_each_encoder_on_crtc(dev, crtc, encoder)
  3288. encoder->enable(encoder);
  3289. }
  3290. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  3291. {
  3292. struct drm_device *dev = crtc->dev;
  3293. struct drm_i915_private *dev_priv = dev->dev_private;
  3294. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3295. struct intel_encoder *encoder;
  3296. int pipe = intel_crtc->pipe;
  3297. int plane = intel_crtc->plane;
  3298. WARN_ON(!crtc->enabled);
  3299. if (intel_crtc->active)
  3300. return;
  3301. intel_crtc->active = true;
  3302. for_each_encoder_on_crtc(dev, crtc, encoder)
  3303. if (encoder->pre_enable)
  3304. encoder->pre_enable(encoder);
  3305. i9xx_enable_pll(intel_crtc);
  3306. i9xx_pfit_enable(intel_crtc);
  3307. intel_crtc_load_lut(crtc);
  3308. intel_update_watermarks(crtc);
  3309. intel_enable_pipe(dev_priv, pipe, false, false);
  3310. intel_enable_plane(dev_priv, plane, pipe);
  3311. intel_enable_planes(crtc);
  3312. /* The fixup needs to happen before cursor is enabled */
  3313. if (IS_G4X(dev))
  3314. g4x_fixup_plane(dev_priv, pipe);
  3315. intel_crtc_update_cursor(crtc, true);
  3316. /* Give the overlay scaler a chance to enable if it's on this pipe */
  3317. intel_crtc_dpms_overlay(intel_crtc, true);
  3318. intel_update_fbc(dev);
  3319. for_each_encoder_on_crtc(dev, crtc, encoder)
  3320. encoder->enable(encoder);
  3321. }
  3322. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  3323. {
  3324. struct drm_device *dev = crtc->base.dev;
  3325. struct drm_i915_private *dev_priv = dev->dev_private;
  3326. if (!crtc->config.gmch_pfit.control)
  3327. return;
  3328. assert_pipe_disabled(dev_priv, crtc->pipe);
  3329. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  3330. I915_READ(PFIT_CONTROL));
  3331. I915_WRITE(PFIT_CONTROL, 0);
  3332. }
  3333. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  3334. {
  3335. struct drm_device *dev = crtc->dev;
  3336. struct drm_i915_private *dev_priv = dev->dev_private;
  3337. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3338. struct intel_encoder *encoder;
  3339. int pipe = intel_crtc->pipe;
  3340. int plane = intel_crtc->plane;
  3341. if (!intel_crtc->active)
  3342. return;
  3343. for_each_encoder_on_crtc(dev, crtc, encoder)
  3344. encoder->disable(encoder);
  3345. /* Give the overlay scaler a chance to disable if it's on this pipe */
  3346. intel_crtc_wait_for_pending_flips(crtc);
  3347. drm_vblank_off(dev, pipe);
  3348. if (dev_priv->fbc.plane == plane)
  3349. intel_disable_fbc(dev);
  3350. intel_crtc_dpms_overlay(intel_crtc, false);
  3351. intel_crtc_update_cursor(crtc, false);
  3352. intel_disable_planes(crtc);
  3353. intel_disable_plane(dev_priv, plane, pipe);
  3354. intel_disable_pipe(dev_priv, pipe);
  3355. i9xx_pfit_disable(intel_crtc);
  3356. for_each_encoder_on_crtc(dev, crtc, encoder)
  3357. if (encoder->post_disable)
  3358. encoder->post_disable(encoder);
  3359. if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  3360. vlv_disable_pll(dev_priv, pipe);
  3361. else if (!IS_VALLEYVIEW(dev))
  3362. i9xx_disable_pll(dev_priv, pipe);
  3363. intel_crtc->active = false;
  3364. intel_update_watermarks(crtc);
  3365. intel_update_fbc(dev);
  3366. }
  3367. static void i9xx_crtc_off(struct drm_crtc *crtc)
  3368. {
  3369. }
  3370. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  3371. bool enabled)
  3372. {
  3373. struct drm_device *dev = crtc->dev;
  3374. struct drm_i915_master_private *master_priv;
  3375. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3376. int pipe = intel_crtc->pipe;
  3377. if (!dev->primary->master)
  3378. return;
  3379. master_priv = dev->primary->master->driver_priv;
  3380. if (!master_priv->sarea_priv)
  3381. return;
  3382. switch (pipe) {
  3383. case 0:
  3384. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  3385. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  3386. break;
  3387. case 1:
  3388. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  3389. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  3390. break;
  3391. default:
  3392. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  3393. break;
  3394. }
  3395. }
  3396. /**
  3397. * Sets the power management mode of the pipe and plane.
  3398. */
  3399. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  3400. {
  3401. struct drm_device *dev = crtc->dev;
  3402. struct drm_i915_private *dev_priv = dev->dev_private;
  3403. struct intel_encoder *intel_encoder;
  3404. bool enable = false;
  3405. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3406. enable |= intel_encoder->connectors_active;
  3407. if (enable)
  3408. dev_priv->display.crtc_enable(crtc);
  3409. else
  3410. dev_priv->display.crtc_disable(crtc);
  3411. intel_crtc_update_sarea(crtc, enable);
  3412. }
  3413. static void intel_crtc_disable(struct drm_crtc *crtc)
  3414. {
  3415. struct drm_device *dev = crtc->dev;
  3416. struct drm_connector *connector;
  3417. struct drm_i915_private *dev_priv = dev->dev_private;
  3418. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3419. /* crtc should still be enabled when we disable it. */
  3420. WARN_ON(!crtc->enabled);
  3421. dev_priv->display.crtc_disable(crtc);
  3422. intel_crtc->eld_vld = false;
  3423. intel_crtc_update_sarea(crtc, false);
  3424. dev_priv->display.off(crtc);
  3425. assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
  3426. assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  3427. assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
  3428. if (crtc->fb) {
  3429. mutex_lock(&dev->struct_mutex);
  3430. intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
  3431. mutex_unlock(&dev->struct_mutex);
  3432. crtc->fb = NULL;
  3433. }
  3434. /* Update computed state. */
  3435. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  3436. if (!connector->encoder || !connector->encoder->crtc)
  3437. continue;
  3438. if (connector->encoder->crtc != crtc)
  3439. continue;
  3440. connector->dpms = DRM_MODE_DPMS_OFF;
  3441. to_intel_encoder(connector->encoder)->connectors_active = false;
  3442. }
  3443. }
  3444. void intel_encoder_destroy(struct drm_encoder *encoder)
  3445. {
  3446. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  3447. drm_encoder_cleanup(encoder);
  3448. kfree(intel_encoder);
  3449. }
  3450. /* Simple dpms helper for encoders with just one connector, no cloning and only
  3451. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  3452. * state of the entire output pipe. */
  3453. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  3454. {
  3455. if (mode == DRM_MODE_DPMS_ON) {
  3456. encoder->connectors_active = true;
  3457. intel_crtc_update_dpms(encoder->base.crtc);
  3458. } else {
  3459. encoder->connectors_active = false;
  3460. intel_crtc_update_dpms(encoder->base.crtc);
  3461. }
  3462. }
  3463. /* Cross check the actual hw state with our own modeset state tracking (and it's
  3464. * internal consistency). */
  3465. static void intel_connector_check_state(struct intel_connector *connector)
  3466. {
  3467. if (connector->get_hw_state(connector)) {
  3468. struct intel_encoder *encoder = connector->encoder;
  3469. struct drm_crtc *crtc;
  3470. bool encoder_enabled;
  3471. enum pipe pipe;
  3472. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  3473. connector->base.base.id,
  3474. drm_get_connector_name(&connector->base));
  3475. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  3476. "wrong connector dpms state\n");
  3477. WARN(connector->base.encoder != &encoder->base,
  3478. "active connector not linked to encoder\n");
  3479. WARN(!encoder->connectors_active,
  3480. "encoder->connectors_active not set\n");
  3481. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  3482. WARN(!encoder_enabled, "encoder not enabled\n");
  3483. if (WARN_ON(!encoder->base.crtc))
  3484. return;
  3485. crtc = encoder->base.crtc;
  3486. WARN(!crtc->enabled, "crtc not enabled\n");
  3487. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  3488. WARN(pipe != to_intel_crtc(crtc)->pipe,
  3489. "encoder active on the wrong pipe\n");
  3490. }
  3491. }
  3492. /* Even simpler default implementation, if there's really no special case to
  3493. * consider. */
  3494. void intel_connector_dpms(struct drm_connector *connector, int mode)
  3495. {
  3496. struct intel_encoder *encoder = intel_attached_encoder(connector);
  3497. /* All the simple cases only support two dpms states. */
  3498. if (mode != DRM_MODE_DPMS_ON)
  3499. mode = DRM_MODE_DPMS_OFF;
  3500. if (mode == connector->dpms)
  3501. return;
  3502. connector->dpms = mode;
  3503. /* Only need to change hw state when actually enabled */
  3504. if (encoder->base.crtc)
  3505. intel_encoder_dpms(encoder, mode);
  3506. else
  3507. WARN_ON(encoder->connectors_active != false);
  3508. intel_modeset_check_state(connector->dev);
  3509. }
  3510. /* Simple connector->get_hw_state implementation for encoders that support only
  3511. * one connector and no cloning and hence the encoder state determines the state
  3512. * of the connector. */
  3513. bool intel_connector_get_hw_state(struct intel_connector *connector)
  3514. {
  3515. enum pipe pipe = 0;
  3516. struct intel_encoder *encoder = connector->encoder;
  3517. return encoder->get_hw_state(encoder, &pipe);
  3518. }
  3519. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  3520. struct intel_crtc_config *pipe_config)
  3521. {
  3522. struct drm_i915_private *dev_priv = dev->dev_private;
  3523. struct intel_crtc *pipe_B_crtc =
  3524. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  3525. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  3526. pipe_name(pipe), pipe_config->fdi_lanes);
  3527. if (pipe_config->fdi_lanes > 4) {
  3528. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  3529. pipe_name(pipe), pipe_config->fdi_lanes);
  3530. return false;
  3531. }
  3532. if (IS_HASWELL(dev)) {
  3533. if (pipe_config->fdi_lanes > 2) {
  3534. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  3535. pipe_config->fdi_lanes);
  3536. return false;
  3537. } else {
  3538. return true;
  3539. }
  3540. }
  3541. if (INTEL_INFO(dev)->num_pipes == 2)
  3542. return true;
  3543. /* Ivybridge 3 pipe is really complicated */
  3544. switch (pipe) {
  3545. case PIPE_A:
  3546. return true;
  3547. case PIPE_B:
  3548. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  3549. pipe_config->fdi_lanes > 2) {
  3550. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  3551. pipe_name(pipe), pipe_config->fdi_lanes);
  3552. return false;
  3553. }
  3554. return true;
  3555. case PIPE_C:
  3556. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  3557. pipe_B_crtc->config.fdi_lanes <= 2) {
  3558. if (pipe_config->fdi_lanes > 2) {
  3559. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  3560. pipe_name(pipe), pipe_config->fdi_lanes);
  3561. return false;
  3562. }
  3563. } else {
  3564. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  3565. return false;
  3566. }
  3567. return true;
  3568. default:
  3569. BUG();
  3570. }
  3571. }
  3572. #define RETRY 1
  3573. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  3574. struct intel_crtc_config *pipe_config)
  3575. {
  3576. struct drm_device *dev = intel_crtc->base.dev;
  3577. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  3578. int lane, link_bw, fdi_dotclock;
  3579. bool setup_ok, needs_recompute = false;
  3580. retry:
  3581. /* FDI is a binary signal running at ~2.7GHz, encoding
  3582. * each output octet as 10 bits. The actual frequency
  3583. * is stored as a divider into a 100MHz clock, and the
  3584. * mode pixel clock is stored in units of 1KHz.
  3585. * Hence the bw of each lane in terms of the mode signal
  3586. * is:
  3587. */
  3588. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  3589. fdi_dotclock = adjusted_mode->crtc_clock;
  3590. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  3591. pipe_config->pipe_bpp);
  3592. pipe_config->fdi_lanes = lane;
  3593. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  3594. link_bw, &pipe_config->fdi_m_n);
  3595. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  3596. intel_crtc->pipe, pipe_config);
  3597. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  3598. pipe_config->pipe_bpp -= 2*3;
  3599. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  3600. pipe_config->pipe_bpp);
  3601. needs_recompute = true;
  3602. pipe_config->bw_constrained = true;
  3603. goto retry;
  3604. }
  3605. if (needs_recompute)
  3606. return RETRY;
  3607. return setup_ok ? 0 : -EINVAL;
  3608. }
  3609. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  3610. struct intel_crtc_config *pipe_config)
  3611. {
  3612. pipe_config->ips_enabled = i915_enable_ips &&
  3613. hsw_crtc_supports_ips(crtc) &&
  3614. pipe_config->pipe_bpp <= 24;
  3615. }
  3616. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  3617. struct intel_crtc_config *pipe_config)
  3618. {
  3619. struct drm_device *dev = crtc->base.dev;
  3620. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  3621. /* FIXME should check pixel clock limits on all platforms */
  3622. if (INTEL_INFO(dev)->gen < 4) {
  3623. struct drm_i915_private *dev_priv = dev->dev_private;
  3624. int clock_limit =
  3625. dev_priv->display.get_display_clock_speed(dev);
  3626. /*
  3627. * Enable pixel doubling when the dot clock
  3628. * is > 90% of the (display) core speed.
  3629. *
  3630. * GDG double wide on either pipe,
  3631. * otherwise pipe A only.
  3632. */
  3633. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  3634. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  3635. clock_limit *= 2;
  3636. pipe_config->double_wide = true;
  3637. }
  3638. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  3639. return -EINVAL;
  3640. }
  3641. /*
  3642. * Pipe horizontal size must be even in:
  3643. * - DVO ganged mode
  3644. * - LVDS dual channel mode
  3645. * - Double wide pipe
  3646. */
  3647. if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  3648. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  3649. pipe_config->pipe_src_w &= ~1;
  3650. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  3651. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  3652. */
  3653. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  3654. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  3655. return -EINVAL;
  3656. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  3657. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  3658. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  3659. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  3660. * for lvds. */
  3661. pipe_config->pipe_bpp = 8*3;
  3662. }
  3663. if (HAS_IPS(dev))
  3664. hsw_compute_ips_config(crtc, pipe_config);
  3665. /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
  3666. * clock survives for now. */
  3667. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  3668. pipe_config->shared_dpll = crtc->config.shared_dpll;
  3669. if (pipe_config->has_pch_encoder)
  3670. return ironlake_fdi_compute_config(crtc, pipe_config);
  3671. return 0;
  3672. }
  3673. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  3674. {
  3675. return 400000; /* FIXME */
  3676. }
  3677. static int i945_get_display_clock_speed(struct drm_device *dev)
  3678. {
  3679. return 400000;
  3680. }
  3681. static int i915_get_display_clock_speed(struct drm_device *dev)
  3682. {
  3683. return 333000;
  3684. }
  3685. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  3686. {
  3687. return 200000;
  3688. }
  3689. static int pnv_get_display_clock_speed(struct drm_device *dev)
  3690. {
  3691. u16 gcfgc = 0;
  3692. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  3693. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  3694. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  3695. return 267000;
  3696. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  3697. return 333000;
  3698. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  3699. return 444000;
  3700. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  3701. return 200000;
  3702. default:
  3703. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  3704. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  3705. return 133000;
  3706. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  3707. return 167000;
  3708. }
  3709. }
  3710. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  3711. {
  3712. u16 gcfgc = 0;
  3713. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  3714. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  3715. return 133000;
  3716. else {
  3717. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  3718. case GC_DISPLAY_CLOCK_333_MHZ:
  3719. return 333000;
  3720. default:
  3721. case GC_DISPLAY_CLOCK_190_200_MHZ:
  3722. return 190000;
  3723. }
  3724. }
  3725. }
  3726. static int i865_get_display_clock_speed(struct drm_device *dev)
  3727. {
  3728. return 266000;
  3729. }
  3730. static int i855_get_display_clock_speed(struct drm_device *dev)
  3731. {
  3732. u16 hpllcc = 0;
  3733. /* Assume that the hardware is in the high speed state. This
  3734. * should be the default.
  3735. */
  3736. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  3737. case GC_CLOCK_133_200:
  3738. case GC_CLOCK_100_200:
  3739. return 200000;
  3740. case GC_CLOCK_166_250:
  3741. return 250000;
  3742. case GC_CLOCK_100_133:
  3743. return 133000;
  3744. }
  3745. /* Shouldn't happen */
  3746. return 0;
  3747. }
  3748. static int i830_get_display_clock_speed(struct drm_device *dev)
  3749. {
  3750. return 133000;
  3751. }
  3752. static void
  3753. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  3754. {
  3755. while (*num > DATA_LINK_M_N_MASK ||
  3756. *den > DATA_LINK_M_N_MASK) {
  3757. *num >>= 1;
  3758. *den >>= 1;
  3759. }
  3760. }
  3761. static void compute_m_n(unsigned int m, unsigned int n,
  3762. uint32_t *ret_m, uint32_t *ret_n)
  3763. {
  3764. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  3765. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  3766. intel_reduce_m_n_ratio(ret_m, ret_n);
  3767. }
  3768. void
  3769. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  3770. int pixel_clock, int link_clock,
  3771. struct intel_link_m_n *m_n)
  3772. {
  3773. m_n->tu = 64;
  3774. compute_m_n(bits_per_pixel * pixel_clock,
  3775. link_clock * nlanes * 8,
  3776. &m_n->gmch_m, &m_n->gmch_n);
  3777. compute_m_n(pixel_clock, link_clock,
  3778. &m_n->link_m, &m_n->link_n);
  3779. }
  3780. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  3781. {
  3782. if (i915_panel_use_ssc >= 0)
  3783. return i915_panel_use_ssc != 0;
  3784. return dev_priv->vbt.lvds_use_ssc
  3785. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  3786. }
  3787. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  3788. {
  3789. struct drm_device *dev = crtc->dev;
  3790. struct drm_i915_private *dev_priv = dev->dev_private;
  3791. int refclk;
  3792. if (IS_VALLEYVIEW(dev)) {
  3793. refclk = 100000;
  3794. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3795. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  3796. refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
  3797. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3798. refclk / 1000);
  3799. } else if (!IS_GEN2(dev)) {
  3800. refclk = 96000;
  3801. } else {
  3802. refclk = 48000;
  3803. }
  3804. return refclk;
  3805. }
  3806. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  3807. {
  3808. return (1 << dpll->n) << 16 | dpll->m2;
  3809. }
  3810. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  3811. {
  3812. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  3813. }
  3814. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  3815. intel_clock_t *reduced_clock)
  3816. {
  3817. struct drm_device *dev = crtc->base.dev;
  3818. struct drm_i915_private *dev_priv = dev->dev_private;
  3819. int pipe = crtc->pipe;
  3820. u32 fp, fp2 = 0;
  3821. if (IS_PINEVIEW(dev)) {
  3822. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  3823. if (reduced_clock)
  3824. fp2 = pnv_dpll_compute_fp(reduced_clock);
  3825. } else {
  3826. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  3827. if (reduced_clock)
  3828. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  3829. }
  3830. I915_WRITE(FP0(pipe), fp);
  3831. crtc->config.dpll_hw_state.fp0 = fp;
  3832. crtc->lowfreq_avail = false;
  3833. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  3834. reduced_clock && i915_powersave) {
  3835. I915_WRITE(FP1(pipe), fp2);
  3836. crtc->config.dpll_hw_state.fp1 = fp2;
  3837. crtc->lowfreq_avail = true;
  3838. } else {
  3839. I915_WRITE(FP1(pipe), fp);
  3840. crtc->config.dpll_hw_state.fp1 = fp;
  3841. }
  3842. }
  3843. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  3844. pipe)
  3845. {
  3846. u32 reg_val;
  3847. /*
  3848. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  3849. * and set it to a reasonable value instead.
  3850. */
  3851. reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF(1));
  3852. reg_val &= 0xffffff00;
  3853. reg_val |= 0x00000030;
  3854. vlv_dpio_write(dev_priv, pipe, DPIO_IREF(1), reg_val);
  3855. reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_CALIBRATION);
  3856. reg_val &= 0x8cffffff;
  3857. reg_val = 0x8c000000;
  3858. vlv_dpio_write(dev_priv, pipe, DPIO_CALIBRATION, reg_val);
  3859. reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF(1));
  3860. reg_val &= 0xffffff00;
  3861. vlv_dpio_write(dev_priv, pipe, DPIO_IREF(1), reg_val);
  3862. reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_CALIBRATION);
  3863. reg_val &= 0x00ffffff;
  3864. reg_val |= 0xb0000000;
  3865. vlv_dpio_write(dev_priv, pipe, DPIO_CALIBRATION, reg_val);
  3866. }
  3867. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  3868. struct intel_link_m_n *m_n)
  3869. {
  3870. struct drm_device *dev = crtc->base.dev;
  3871. struct drm_i915_private *dev_priv = dev->dev_private;
  3872. int pipe = crtc->pipe;
  3873. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  3874. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  3875. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  3876. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  3877. }
  3878. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  3879. struct intel_link_m_n *m_n)
  3880. {
  3881. struct drm_device *dev = crtc->base.dev;
  3882. struct drm_i915_private *dev_priv = dev->dev_private;
  3883. int pipe = crtc->pipe;
  3884. enum transcoder transcoder = crtc->config.cpu_transcoder;
  3885. if (INTEL_INFO(dev)->gen >= 5) {
  3886. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  3887. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  3888. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  3889. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  3890. } else {
  3891. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  3892. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  3893. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  3894. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  3895. }
  3896. }
  3897. static void intel_dp_set_m_n(struct intel_crtc *crtc)
  3898. {
  3899. if (crtc->config.has_pch_encoder)
  3900. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  3901. else
  3902. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  3903. }
  3904. static void vlv_update_pll(struct intel_crtc *crtc)
  3905. {
  3906. struct drm_device *dev = crtc->base.dev;
  3907. struct drm_i915_private *dev_priv = dev->dev_private;
  3908. int pipe = crtc->pipe;
  3909. u32 dpll, mdiv;
  3910. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  3911. u32 coreclk, reg_val, dpll_md;
  3912. mutex_lock(&dev_priv->dpio_lock);
  3913. bestn = crtc->config.dpll.n;
  3914. bestm1 = crtc->config.dpll.m1;
  3915. bestm2 = crtc->config.dpll.m2;
  3916. bestp1 = crtc->config.dpll.p1;
  3917. bestp2 = crtc->config.dpll.p2;
  3918. /* See eDP HDMI DPIO driver vbios notes doc */
  3919. /* PLL B needs special handling */
  3920. if (pipe)
  3921. vlv_pllb_recal_opamp(dev_priv, pipe);
  3922. /* Set up Tx target for periodic Rcomp update */
  3923. vlv_dpio_write(dev_priv, pipe, DPIO_IREF_BCAST, 0x0100000f);
  3924. /* Disable target IRef on PLL */
  3925. reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF_CTL(pipe));
  3926. reg_val &= 0x00ffffff;
  3927. vlv_dpio_write(dev_priv, pipe, DPIO_IREF_CTL(pipe), reg_val);
  3928. /* Disable fast lock */
  3929. vlv_dpio_write(dev_priv, pipe, DPIO_FASTCLK_DISABLE, 0x610);
  3930. /* Set idtafcrecal before PLL is enabled */
  3931. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  3932. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  3933. mdiv |= ((bestn << DPIO_N_SHIFT));
  3934. mdiv |= (1 << DPIO_K_SHIFT);
  3935. /*
  3936. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  3937. * but we don't support that).
  3938. * Note: don't use the DAC post divider as it seems unstable.
  3939. */
  3940. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  3941. vlv_dpio_write(dev_priv, pipe, DPIO_DIV(pipe), mdiv);
  3942. mdiv |= DPIO_ENABLE_CALIBRATION;
  3943. vlv_dpio_write(dev_priv, pipe, DPIO_DIV(pipe), mdiv);
  3944. /* Set HBR and RBR LPF coefficients */
  3945. if (crtc->config.port_clock == 162000 ||
  3946. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
  3947. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
  3948. vlv_dpio_write(dev_priv, pipe, DPIO_LPF_COEFF(pipe),
  3949. 0x009f0003);
  3950. else
  3951. vlv_dpio_write(dev_priv, pipe, DPIO_LPF_COEFF(pipe),
  3952. 0x00d0000f);
  3953. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
  3954. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
  3955. /* Use SSC source */
  3956. if (!pipe)
  3957. vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
  3958. 0x0df40000);
  3959. else
  3960. vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
  3961. 0x0df70000);
  3962. } else { /* HDMI or VGA */
  3963. /* Use bend source */
  3964. if (!pipe)
  3965. vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
  3966. 0x0df70000);
  3967. else
  3968. vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
  3969. 0x0df40000);
  3970. }
  3971. coreclk = vlv_dpio_read(dev_priv, pipe, DPIO_CORE_CLK(pipe));
  3972. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  3973. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
  3974. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
  3975. coreclk |= 0x01000000;
  3976. vlv_dpio_write(dev_priv, pipe, DPIO_CORE_CLK(pipe), coreclk);
  3977. vlv_dpio_write(dev_priv, pipe, DPIO_PLL_CML(pipe), 0x87871000);
  3978. /* Enable DPIO clock input */
  3979. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  3980. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  3981. /* We should never disable this, set it here for state tracking */
  3982. if (pipe == PIPE_B)
  3983. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  3984. dpll |= DPLL_VCO_ENABLE;
  3985. crtc->config.dpll_hw_state.dpll = dpll;
  3986. dpll_md = (crtc->config.pixel_multiplier - 1)
  3987. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3988. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  3989. if (crtc->config.has_dp_encoder)
  3990. intel_dp_set_m_n(crtc);
  3991. mutex_unlock(&dev_priv->dpio_lock);
  3992. }
  3993. static void i9xx_update_pll(struct intel_crtc *crtc,
  3994. intel_clock_t *reduced_clock,
  3995. int num_connectors)
  3996. {
  3997. struct drm_device *dev = crtc->base.dev;
  3998. struct drm_i915_private *dev_priv = dev->dev_private;
  3999. u32 dpll;
  4000. bool is_sdvo;
  4001. struct dpll *clock = &crtc->config.dpll;
  4002. i9xx_update_pll_dividers(crtc, reduced_clock);
  4003. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  4004. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  4005. dpll = DPLL_VGA_MODE_DIS;
  4006. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  4007. dpll |= DPLLB_MODE_LVDS;
  4008. else
  4009. dpll |= DPLLB_MODE_DAC_SERIAL;
  4010. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4011. dpll |= (crtc->config.pixel_multiplier - 1)
  4012. << SDVO_MULTIPLIER_SHIFT_HIRES;
  4013. }
  4014. if (is_sdvo)
  4015. dpll |= DPLL_SDVO_HIGH_SPEED;
  4016. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  4017. dpll |= DPLL_SDVO_HIGH_SPEED;
  4018. /* compute bitmask from p1 value */
  4019. if (IS_PINEVIEW(dev))
  4020. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4021. else {
  4022. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4023. if (IS_G4X(dev) && reduced_clock)
  4024. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4025. }
  4026. switch (clock->p2) {
  4027. case 5:
  4028. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4029. break;
  4030. case 7:
  4031. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4032. break;
  4033. case 10:
  4034. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4035. break;
  4036. case 14:
  4037. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4038. break;
  4039. }
  4040. if (INTEL_INFO(dev)->gen >= 4)
  4041. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4042. if (crtc->config.sdvo_tv_clock)
  4043. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4044. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4045. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4046. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4047. else
  4048. dpll |= PLL_REF_INPUT_DREFCLK;
  4049. dpll |= DPLL_VCO_ENABLE;
  4050. crtc->config.dpll_hw_state.dpll = dpll;
  4051. if (INTEL_INFO(dev)->gen >= 4) {
  4052. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  4053. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4054. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4055. }
  4056. if (crtc->config.has_dp_encoder)
  4057. intel_dp_set_m_n(crtc);
  4058. }
  4059. static void i8xx_update_pll(struct intel_crtc *crtc,
  4060. intel_clock_t *reduced_clock,
  4061. int num_connectors)
  4062. {
  4063. struct drm_device *dev = crtc->base.dev;
  4064. struct drm_i915_private *dev_priv = dev->dev_private;
  4065. u32 dpll;
  4066. struct dpll *clock = &crtc->config.dpll;
  4067. i9xx_update_pll_dividers(crtc, reduced_clock);
  4068. dpll = DPLL_VGA_MODE_DIS;
  4069. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  4070. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4071. } else {
  4072. if (clock->p1 == 2)
  4073. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4074. else
  4075. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4076. if (clock->p2 == 4)
  4077. dpll |= PLL_P2_DIVIDE_BY_4;
  4078. }
  4079. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
  4080. dpll |= DPLL_DVO_2X_MODE;
  4081. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4082. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4083. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4084. else
  4085. dpll |= PLL_REF_INPUT_DREFCLK;
  4086. dpll |= DPLL_VCO_ENABLE;
  4087. crtc->config.dpll_hw_state.dpll = dpll;
  4088. }
  4089. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  4090. {
  4091. struct drm_device *dev = intel_crtc->base.dev;
  4092. struct drm_i915_private *dev_priv = dev->dev_private;
  4093. enum pipe pipe = intel_crtc->pipe;
  4094. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4095. struct drm_display_mode *adjusted_mode =
  4096. &intel_crtc->config.adjusted_mode;
  4097. uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
  4098. /* We need to be careful not to changed the adjusted mode, for otherwise
  4099. * the hw state checker will get angry at the mismatch. */
  4100. crtc_vtotal = adjusted_mode->crtc_vtotal;
  4101. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  4102. if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4103. /* the chip adds 2 halflines automatically */
  4104. crtc_vtotal -= 1;
  4105. crtc_vblank_end -= 1;
  4106. vsyncshift = adjusted_mode->crtc_hsync_start
  4107. - adjusted_mode->crtc_htotal / 2;
  4108. } else {
  4109. vsyncshift = 0;
  4110. }
  4111. if (INTEL_INFO(dev)->gen > 3)
  4112. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  4113. I915_WRITE(HTOTAL(cpu_transcoder),
  4114. (adjusted_mode->crtc_hdisplay - 1) |
  4115. ((adjusted_mode->crtc_htotal - 1) << 16));
  4116. I915_WRITE(HBLANK(cpu_transcoder),
  4117. (adjusted_mode->crtc_hblank_start - 1) |
  4118. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4119. I915_WRITE(HSYNC(cpu_transcoder),
  4120. (adjusted_mode->crtc_hsync_start - 1) |
  4121. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4122. I915_WRITE(VTOTAL(cpu_transcoder),
  4123. (adjusted_mode->crtc_vdisplay - 1) |
  4124. ((crtc_vtotal - 1) << 16));
  4125. I915_WRITE(VBLANK(cpu_transcoder),
  4126. (adjusted_mode->crtc_vblank_start - 1) |
  4127. ((crtc_vblank_end - 1) << 16));
  4128. I915_WRITE(VSYNC(cpu_transcoder),
  4129. (adjusted_mode->crtc_vsync_start - 1) |
  4130. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4131. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  4132. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  4133. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  4134. * bits. */
  4135. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  4136. (pipe == PIPE_B || pipe == PIPE_C))
  4137. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  4138. /* pipesrc controls the size that is scaled from, which should
  4139. * always be the user's requested size.
  4140. */
  4141. I915_WRITE(PIPESRC(pipe),
  4142. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  4143. (intel_crtc->config.pipe_src_h - 1));
  4144. }
  4145. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  4146. struct intel_crtc_config *pipe_config)
  4147. {
  4148. struct drm_device *dev = crtc->base.dev;
  4149. struct drm_i915_private *dev_priv = dev->dev_private;
  4150. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  4151. uint32_t tmp;
  4152. tmp = I915_READ(HTOTAL(cpu_transcoder));
  4153. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  4154. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  4155. tmp = I915_READ(HBLANK(cpu_transcoder));
  4156. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  4157. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  4158. tmp = I915_READ(HSYNC(cpu_transcoder));
  4159. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  4160. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  4161. tmp = I915_READ(VTOTAL(cpu_transcoder));
  4162. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  4163. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  4164. tmp = I915_READ(VBLANK(cpu_transcoder));
  4165. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  4166. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  4167. tmp = I915_READ(VSYNC(cpu_transcoder));
  4168. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  4169. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  4170. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  4171. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  4172. pipe_config->adjusted_mode.crtc_vtotal += 1;
  4173. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  4174. }
  4175. tmp = I915_READ(PIPESRC(crtc->pipe));
  4176. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  4177. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  4178. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  4179. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  4180. }
  4181. static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
  4182. struct intel_crtc_config *pipe_config)
  4183. {
  4184. struct drm_crtc *crtc = &intel_crtc->base;
  4185. crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  4186. crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
  4187. crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  4188. crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  4189. crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  4190. crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  4191. crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  4192. crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  4193. crtc->mode.flags = pipe_config->adjusted_mode.flags;
  4194. crtc->mode.clock = pipe_config->adjusted_mode.crtc_clock;
  4195. crtc->mode.flags |= pipe_config->adjusted_mode.flags;
  4196. }
  4197. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  4198. {
  4199. struct drm_device *dev = intel_crtc->base.dev;
  4200. struct drm_i915_private *dev_priv = dev->dev_private;
  4201. uint32_t pipeconf;
  4202. pipeconf = 0;
  4203. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  4204. I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
  4205. pipeconf |= PIPECONF_ENABLE;
  4206. if (intel_crtc->config.double_wide)
  4207. pipeconf |= PIPECONF_DOUBLE_WIDE;
  4208. /* only g4x and later have fancy bpc/dither controls */
  4209. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  4210. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  4211. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  4212. pipeconf |= PIPECONF_DITHER_EN |
  4213. PIPECONF_DITHER_TYPE_SP;
  4214. switch (intel_crtc->config.pipe_bpp) {
  4215. case 18:
  4216. pipeconf |= PIPECONF_6BPC;
  4217. break;
  4218. case 24:
  4219. pipeconf |= PIPECONF_8BPC;
  4220. break;
  4221. case 30:
  4222. pipeconf |= PIPECONF_10BPC;
  4223. break;
  4224. default:
  4225. /* Case prevented by intel_choose_pipe_bpp_dither. */
  4226. BUG();
  4227. }
  4228. }
  4229. if (HAS_PIPE_CXSR(dev)) {
  4230. if (intel_crtc->lowfreq_avail) {
  4231. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4232. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4233. } else {
  4234. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4235. }
  4236. }
  4237. if (!IS_GEN2(dev) &&
  4238. intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  4239. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4240. else
  4241. pipeconf |= PIPECONF_PROGRESSIVE;
  4242. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  4243. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  4244. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  4245. POSTING_READ(PIPECONF(intel_crtc->pipe));
  4246. }
  4247. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  4248. int x, int y,
  4249. struct drm_framebuffer *fb)
  4250. {
  4251. struct drm_device *dev = crtc->dev;
  4252. struct drm_i915_private *dev_priv = dev->dev_private;
  4253. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4254. int pipe = intel_crtc->pipe;
  4255. int plane = intel_crtc->plane;
  4256. int refclk, num_connectors = 0;
  4257. intel_clock_t clock, reduced_clock;
  4258. u32 dspcntr;
  4259. bool ok, has_reduced_clock = false;
  4260. bool is_lvds = false, is_dsi = false;
  4261. struct intel_encoder *encoder;
  4262. const intel_limit_t *limit;
  4263. int ret;
  4264. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4265. switch (encoder->type) {
  4266. case INTEL_OUTPUT_LVDS:
  4267. is_lvds = true;
  4268. break;
  4269. case INTEL_OUTPUT_DSI:
  4270. is_dsi = true;
  4271. break;
  4272. }
  4273. num_connectors++;
  4274. }
  4275. if (is_dsi)
  4276. goto skip_dpll;
  4277. if (!intel_crtc->config.clock_set) {
  4278. refclk = i9xx_get_refclk(crtc, num_connectors);
  4279. /*
  4280. * Returns a set of divisors for the desired target clock with
  4281. * the given refclk, or FALSE. The returned values represent
  4282. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  4283. * 2) / p1 / p2.
  4284. */
  4285. limit = intel_limit(crtc, refclk);
  4286. ok = dev_priv->display.find_dpll(limit, crtc,
  4287. intel_crtc->config.port_clock,
  4288. refclk, NULL, &clock);
  4289. if (!ok) {
  4290. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4291. return -EINVAL;
  4292. }
  4293. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4294. /*
  4295. * Ensure we match the reduced clock's P to the target
  4296. * clock. If the clocks don't match, we can't switch
  4297. * the display clock by using the FP0/FP1. In such case
  4298. * we will disable the LVDS downclock feature.
  4299. */
  4300. has_reduced_clock =
  4301. dev_priv->display.find_dpll(limit, crtc,
  4302. dev_priv->lvds_downclock,
  4303. refclk, &clock,
  4304. &reduced_clock);
  4305. }
  4306. /* Compat-code for transition, will disappear. */
  4307. intel_crtc->config.dpll.n = clock.n;
  4308. intel_crtc->config.dpll.m1 = clock.m1;
  4309. intel_crtc->config.dpll.m2 = clock.m2;
  4310. intel_crtc->config.dpll.p1 = clock.p1;
  4311. intel_crtc->config.dpll.p2 = clock.p2;
  4312. }
  4313. if (IS_GEN2(dev)) {
  4314. i8xx_update_pll(intel_crtc,
  4315. has_reduced_clock ? &reduced_clock : NULL,
  4316. num_connectors);
  4317. } else if (IS_VALLEYVIEW(dev)) {
  4318. vlv_update_pll(intel_crtc);
  4319. } else {
  4320. i9xx_update_pll(intel_crtc,
  4321. has_reduced_clock ? &reduced_clock : NULL,
  4322. num_connectors);
  4323. }
  4324. skip_dpll:
  4325. /* Set up the display plane register */
  4326. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4327. if (!IS_VALLEYVIEW(dev)) {
  4328. if (pipe == 0)
  4329. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  4330. else
  4331. dspcntr |= DISPPLANE_SEL_PIPE_B;
  4332. }
  4333. intel_set_pipe_timings(intel_crtc);
  4334. /* pipesrc and dspsize control the size that is scaled from,
  4335. * which should always be the user's requested size.
  4336. */
  4337. I915_WRITE(DSPSIZE(plane),
  4338. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  4339. (intel_crtc->config.pipe_src_w - 1));
  4340. I915_WRITE(DSPPOS(plane), 0);
  4341. i9xx_set_pipeconf(intel_crtc);
  4342. I915_WRITE(DSPCNTR(plane), dspcntr);
  4343. POSTING_READ(DSPCNTR(plane));
  4344. ret = intel_pipe_set_base(crtc, x, y, fb);
  4345. return ret;
  4346. }
  4347. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  4348. struct intel_crtc_config *pipe_config)
  4349. {
  4350. struct drm_device *dev = crtc->base.dev;
  4351. struct drm_i915_private *dev_priv = dev->dev_private;
  4352. uint32_t tmp;
  4353. tmp = I915_READ(PFIT_CONTROL);
  4354. if (!(tmp & PFIT_ENABLE))
  4355. return;
  4356. /* Check whether the pfit is attached to our pipe. */
  4357. if (INTEL_INFO(dev)->gen < 4) {
  4358. if (crtc->pipe != PIPE_B)
  4359. return;
  4360. } else {
  4361. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  4362. return;
  4363. }
  4364. pipe_config->gmch_pfit.control = tmp;
  4365. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  4366. if (INTEL_INFO(dev)->gen < 5)
  4367. pipe_config->gmch_pfit.lvds_border_bits =
  4368. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  4369. }
  4370. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  4371. struct intel_crtc_config *pipe_config)
  4372. {
  4373. struct drm_device *dev = crtc->base.dev;
  4374. struct drm_i915_private *dev_priv = dev->dev_private;
  4375. int pipe = pipe_config->cpu_transcoder;
  4376. intel_clock_t clock;
  4377. u32 mdiv;
  4378. int refclk = 100000;
  4379. mutex_lock(&dev_priv->dpio_lock);
  4380. mdiv = vlv_dpio_read(dev_priv, pipe, DPIO_DIV(pipe));
  4381. mutex_unlock(&dev_priv->dpio_lock);
  4382. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  4383. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  4384. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  4385. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  4386. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  4387. clock.vco = refclk * clock.m1 * clock.m2 / clock.n;
  4388. clock.dot = 2 * clock.vco / (clock.p1 * clock.p2);
  4389. pipe_config->port_clock = clock.dot / 10;
  4390. }
  4391. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  4392. struct intel_crtc_config *pipe_config)
  4393. {
  4394. struct drm_device *dev = crtc->base.dev;
  4395. struct drm_i915_private *dev_priv = dev->dev_private;
  4396. uint32_t tmp;
  4397. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  4398. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  4399. tmp = I915_READ(PIPECONF(crtc->pipe));
  4400. if (!(tmp & PIPECONF_ENABLE))
  4401. return false;
  4402. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  4403. switch (tmp & PIPECONF_BPC_MASK) {
  4404. case PIPECONF_6BPC:
  4405. pipe_config->pipe_bpp = 18;
  4406. break;
  4407. case PIPECONF_8BPC:
  4408. pipe_config->pipe_bpp = 24;
  4409. break;
  4410. case PIPECONF_10BPC:
  4411. pipe_config->pipe_bpp = 30;
  4412. break;
  4413. default:
  4414. break;
  4415. }
  4416. }
  4417. if (INTEL_INFO(dev)->gen < 4)
  4418. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  4419. intel_get_pipe_timings(crtc, pipe_config);
  4420. i9xx_get_pfit_config(crtc, pipe_config);
  4421. if (INTEL_INFO(dev)->gen >= 4) {
  4422. tmp = I915_READ(DPLL_MD(crtc->pipe));
  4423. pipe_config->pixel_multiplier =
  4424. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  4425. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  4426. pipe_config->dpll_hw_state.dpll_md = tmp;
  4427. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4428. tmp = I915_READ(DPLL(crtc->pipe));
  4429. pipe_config->pixel_multiplier =
  4430. ((tmp & SDVO_MULTIPLIER_MASK)
  4431. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  4432. } else {
  4433. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  4434. * port and will be fixed up in the encoder->get_config
  4435. * function. */
  4436. pipe_config->pixel_multiplier = 1;
  4437. }
  4438. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  4439. if (!IS_VALLEYVIEW(dev)) {
  4440. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  4441. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  4442. } else {
  4443. /* Mask out read-only status bits. */
  4444. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  4445. DPLL_PORTC_READY_MASK |
  4446. DPLL_PORTB_READY_MASK);
  4447. }
  4448. if (IS_VALLEYVIEW(dev))
  4449. vlv_crtc_clock_get(crtc, pipe_config);
  4450. else
  4451. i9xx_crtc_clock_get(crtc, pipe_config);
  4452. return true;
  4453. }
  4454. static void ironlake_init_pch_refclk(struct drm_device *dev)
  4455. {
  4456. struct drm_i915_private *dev_priv = dev->dev_private;
  4457. struct drm_mode_config *mode_config = &dev->mode_config;
  4458. struct intel_encoder *encoder;
  4459. u32 val, final;
  4460. bool has_lvds = false;
  4461. bool has_cpu_edp = false;
  4462. bool has_panel = false;
  4463. bool has_ck505 = false;
  4464. bool can_ssc = false;
  4465. /* We need to take the global config into account */
  4466. list_for_each_entry(encoder, &mode_config->encoder_list,
  4467. base.head) {
  4468. switch (encoder->type) {
  4469. case INTEL_OUTPUT_LVDS:
  4470. has_panel = true;
  4471. has_lvds = true;
  4472. break;
  4473. case INTEL_OUTPUT_EDP:
  4474. has_panel = true;
  4475. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  4476. has_cpu_edp = true;
  4477. break;
  4478. }
  4479. }
  4480. if (HAS_PCH_IBX(dev)) {
  4481. has_ck505 = dev_priv->vbt.display_clock_mode;
  4482. can_ssc = has_ck505;
  4483. } else {
  4484. has_ck505 = false;
  4485. can_ssc = true;
  4486. }
  4487. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  4488. has_panel, has_lvds, has_ck505);
  4489. /* Ironlake: try to setup display ref clock before DPLL
  4490. * enabling. This is only under driver's control after
  4491. * PCH B stepping, previous chipset stepping should be
  4492. * ignoring this setting.
  4493. */
  4494. val = I915_READ(PCH_DREF_CONTROL);
  4495. /* As we must carefully and slowly disable/enable each source in turn,
  4496. * compute the final state we want first and check if we need to
  4497. * make any changes at all.
  4498. */
  4499. final = val;
  4500. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  4501. if (has_ck505)
  4502. final |= DREF_NONSPREAD_CK505_ENABLE;
  4503. else
  4504. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  4505. final &= ~DREF_SSC_SOURCE_MASK;
  4506. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4507. final &= ~DREF_SSC1_ENABLE;
  4508. if (has_panel) {
  4509. final |= DREF_SSC_SOURCE_ENABLE;
  4510. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  4511. final |= DREF_SSC1_ENABLE;
  4512. if (has_cpu_edp) {
  4513. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  4514. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4515. else
  4516. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4517. } else
  4518. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4519. } else {
  4520. final |= DREF_SSC_SOURCE_DISABLE;
  4521. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4522. }
  4523. if (final == val)
  4524. return;
  4525. /* Always enable nonspread source */
  4526. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  4527. if (has_ck505)
  4528. val |= DREF_NONSPREAD_CK505_ENABLE;
  4529. else
  4530. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  4531. if (has_panel) {
  4532. val &= ~DREF_SSC_SOURCE_MASK;
  4533. val |= DREF_SSC_SOURCE_ENABLE;
  4534. /* SSC must be turned on before enabling the CPU output */
  4535. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  4536. DRM_DEBUG_KMS("Using SSC on panel\n");
  4537. val |= DREF_SSC1_ENABLE;
  4538. } else
  4539. val &= ~DREF_SSC1_ENABLE;
  4540. /* Get SSC going before enabling the outputs */
  4541. I915_WRITE(PCH_DREF_CONTROL, val);
  4542. POSTING_READ(PCH_DREF_CONTROL);
  4543. udelay(200);
  4544. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4545. /* Enable CPU source on CPU attached eDP */
  4546. if (has_cpu_edp) {
  4547. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  4548. DRM_DEBUG_KMS("Using SSC on eDP\n");
  4549. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4550. }
  4551. else
  4552. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4553. } else
  4554. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4555. I915_WRITE(PCH_DREF_CONTROL, val);
  4556. POSTING_READ(PCH_DREF_CONTROL);
  4557. udelay(200);
  4558. } else {
  4559. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  4560. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4561. /* Turn off CPU output */
  4562. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4563. I915_WRITE(PCH_DREF_CONTROL, val);
  4564. POSTING_READ(PCH_DREF_CONTROL);
  4565. udelay(200);
  4566. /* Turn off the SSC source */
  4567. val &= ~DREF_SSC_SOURCE_MASK;
  4568. val |= DREF_SSC_SOURCE_DISABLE;
  4569. /* Turn off SSC1 */
  4570. val &= ~DREF_SSC1_ENABLE;
  4571. I915_WRITE(PCH_DREF_CONTROL, val);
  4572. POSTING_READ(PCH_DREF_CONTROL);
  4573. udelay(200);
  4574. }
  4575. BUG_ON(val != final);
  4576. }
  4577. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  4578. {
  4579. uint32_t tmp;
  4580. tmp = I915_READ(SOUTH_CHICKEN2);
  4581. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  4582. I915_WRITE(SOUTH_CHICKEN2, tmp);
  4583. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  4584. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  4585. DRM_ERROR("FDI mPHY reset assert timeout\n");
  4586. tmp = I915_READ(SOUTH_CHICKEN2);
  4587. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  4588. I915_WRITE(SOUTH_CHICKEN2, tmp);
  4589. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  4590. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  4591. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  4592. }
  4593. /* WaMPhyProgramming:hsw */
  4594. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  4595. {
  4596. uint32_t tmp;
  4597. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  4598. tmp &= ~(0xFF << 24);
  4599. tmp |= (0x12 << 24);
  4600. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  4601. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  4602. tmp |= (1 << 11);
  4603. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  4604. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  4605. tmp |= (1 << 11);
  4606. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  4607. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  4608. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  4609. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  4610. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  4611. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  4612. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  4613. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  4614. tmp &= ~(7 << 13);
  4615. tmp |= (5 << 13);
  4616. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  4617. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  4618. tmp &= ~(7 << 13);
  4619. tmp |= (5 << 13);
  4620. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  4621. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  4622. tmp &= ~0xFF;
  4623. tmp |= 0x1C;
  4624. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  4625. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  4626. tmp &= ~0xFF;
  4627. tmp |= 0x1C;
  4628. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  4629. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  4630. tmp &= ~(0xFF << 16);
  4631. tmp |= (0x1C << 16);
  4632. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  4633. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  4634. tmp &= ~(0xFF << 16);
  4635. tmp |= (0x1C << 16);
  4636. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  4637. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  4638. tmp |= (1 << 27);
  4639. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  4640. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  4641. tmp |= (1 << 27);
  4642. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  4643. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  4644. tmp &= ~(0xF << 28);
  4645. tmp |= (4 << 28);
  4646. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  4647. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  4648. tmp &= ~(0xF << 28);
  4649. tmp |= (4 << 28);
  4650. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  4651. }
  4652. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  4653. * Programming" based on the parameters passed:
  4654. * - Sequence to enable CLKOUT_DP
  4655. * - Sequence to enable CLKOUT_DP without spread
  4656. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  4657. */
  4658. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  4659. bool with_fdi)
  4660. {
  4661. struct drm_i915_private *dev_priv = dev->dev_private;
  4662. uint32_t reg, tmp;
  4663. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  4664. with_spread = true;
  4665. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  4666. with_fdi, "LP PCH doesn't have FDI\n"))
  4667. with_fdi = false;
  4668. mutex_lock(&dev_priv->dpio_lock);
  4669. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  4670. tmp &= ~SBI_SSCCTL_DISABLE;
  4671. tmp |= SBI_SSCCTL_PATHALT;
  4672. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  4673. udelay(24);
  4674. if (with_spread) {
  4675. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  4676. tmp &= ~SBI_SSCCTL_PATHALT;
  4677. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  4678. if (with_fdi) {
  4679. lpt_reset_fdi_mphy(dev_priv);
  4680. lpt_program_fdi_mphy(dev_priv);
  4681. }
  4682. }
  4683. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  4684. SBI_GEN0 : SBI_DBUFF0;
  4685. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  4686. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  4687. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  4688. mutex_unlock(&dev_priv->dpio_lock);
  4689. }
  4690. /* Sequence to disable CLKOUT_DP */
  4691. static void lpt_disable_clkout_dp(struct drm_device *dev)
  4692. {
  4693. struct drm_i915_private *dev_priv = dev->dev_private;
  4694. uint32_t reg, tmp;
  4695. mutex_lock(&dev_priv->dpio_lock);
  4696. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  4697. SBI_GEN0 : SBI_DBUFF0;
  4698. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  4699. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  4700. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  4701. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  4702. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  4703. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  4704. tmp |= SBI_SSCCTL_PATHALT;
  4705. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  4706. udelay(32);
  4707. }
  4708. tmp |= SBI_SSCCTL_DISABLE;
  4709. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  4710. }
  4711. mutex_unlock(&dev_priv->dpio_lock);
  4712. }
  4713. static void lpt_init_pch_refclk(struct drm_device *dev)
  4714. {
  4715. struct drm_mode_config *mode_config = &dev->mode_config;
  4716. struct intel_encoder *encoder;
  4717. bool has_vga = false;
  4718. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4719. switch (encoder->type) {
  4720. case INTEL_OUTPUT_ANALOG:
  4721. has_vga = true;
  4722. break;
  4723. }
  4724. }
  4725. if (has_vga)
  4726. lpt_enable_clkout_dp(dev, true, true);
  4727. else
  4728. lpt_disable_clkout_dp(dev);
  4729. }
  4730. /*
  4731. * Initialize reference clocks when the driver loads
  4732. */
  4733. void intel_init_pch_refclk(struct drm_device *dev)
  4734. {
  4735. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  4736. ironlake_init_pch_refclk(dev);
  4737. else if (HAS_PCH_LPT(dev))
  4738. lpt_init_pch_refclk(dev);
  4739. }
  4740. static int ironlake_get_refclk(struct drm_crtc *crtc)
  4741. {
  4742. struct drm_device *dev = crtc->dev;
  4743. struct drm_i915_private *dev_priv = dev->dev_private;
  4744. struct intel_encoder *encoder;
  4745. int num_connectors = 0;
  4746. bool is_lvds = false;
  4747. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4748. switch (encoder->type) {
  4749. case INTEL_OUTPUT_LVDS:
  4750. is_lvds = true;
  4751. break;
  4752. }
  4753. num_connectors++;
  4754. }
  4755. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4756. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  4757. dev_priv->vbt.lvds_ssc_freq);
  4758. return dev_priv->vbt.lvds_ssc_freq * 1000;
  4759. }
  4760. return 120000;
  4761. }
  4762. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  4763. {
  4764. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4765. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4766. int pipe = intel_crtc->pipe;
  4767. uint32_t val;
  4768. val = 0;
  4769. switch (intel_crtc->config.pipe_bpp) {
  4770. case 18:
  4771. val |= PIPECONF_6BPC;
  4772. break;
  4773. case 24:
  4774. val |= PIPECONF_8BPC;
  4775. break;
  4776. case 30:
  4777. val |= PIPECONF_10BPC;
  4778. break;
  4779. case 36:
  4780. val |= PIPECONF_12BPC;
  4781. break;
  4782. default:
  4783. /* Case prevented by intel_choose_pipe_bpp_dither. */
  4784. BUG();
  4785. }
  4786. if (intel_crtc->config.dither)
  4787. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  4788. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  4789. val |= PIPECONF_INTERLACED_ILK;
  4790. else
  4791. val |= PIPECONF_PROGRESSIVE;
  4792. if (intel_crtc->config.limited_color_range)
  4793. val |= PIPECONF_COLOR_RANGE_SELECT;
  4794. I915_WRITE(PIPECONF(pipe), val);
  4795. POSTING_READ(PIPECONF(pipe));
  4796. }
  4797. /*
  4798. * Set up the pipe CSC unit.
  4799. *
  4800. * Currently only full range RGB to limited range RGB conversion
  4801. * is supported, but eventually this should handle various
  4802. * RGB<->YCbCr scenarios as well.
  4803. */
  4804. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  4805. {
  4806. struct drm_device *dev = crtc->dev;
  4807. struct drm_i915_private *dev_priv = dev->dev_private;
  4808. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4809. int pipe = intel_crtc->pipe;
  4810. uint16_t coeff = 0x7800; /* 1.0 */
  4811. /*
  4812. * TODO: Check what kind of values actually come out of the pipe
  4813. * with these coeff/postoff values and adjust to get the best
  4814. * accuracy. Perhaps we even need to take the bpc value into
  4815. * consideration.
  4816. */
  4817. if (intel_crtc->config.limited_color_range)
  4818. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  4819. /*
  4820. * GY/GU and RY/RU should be the other way around according
  4821. * to BSpec, but reality doesn't agree. Just set them up in
  4822. * a way that results in the correct picture.
  4823. */
  4824. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  4825. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  4826. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  4827. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  4828. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  4829. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  4830. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  4831. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  4832. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  4833. if (INTEL_INFO(dev)->gen > 6) {
  4834. uint16_t postoff = 0;
  4835. if (intel_crtc->config.limited_color_range)
  4836. postoff = (16 * (1 << 13) / 255) & 0x1fff;
  4837. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  4838. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  4839. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  4840. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  4841. } else {
  4842. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  4843. if (intel_crtc->config.limited_color_range)
  4844. mode |= CSC_BLACK_SCREEN_OFFSET;
  4845. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  4846. }
  4847. }
  4848. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  4849. {
  4850. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4851. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4852. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4853. uint32_t val;
  4854. val = 0;
  4855. if (intel_crtc->config.dither)
  4856. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  4857. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  4858. val |= PIPECONF_INTERLACED_ILK;
  4859. else
  4860. val |= PIPECONF_PROGRESSIVE;
  4861. I915_WRITE(PIPECONF(cpu_transcoder), val);
  4862. POSTING_READ(PIPECONF(cpu_transcoder));
  4863. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  4864. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  4865. }
  4866. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  4867. intel_clock_t *clock,
  4868. bool *has_reduced_clock,
  4869. intel_clock_t *reduced_clock)
  4870. {
  4871. struct drm_device *dev = crtc->dev;
  4872. struct drm_i915_private *dev_priv = dev->dev_private;
  4873. struct intel_encoder *intel_encoder;
  4874. int refclk;
  4875. const intel_limit_t *limit;
  4876. bool ret, is_lvds = false;
  4877. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  4878. switch (intel_encoder->type) {
  4879. case INTEL_OUTPUT_LVDS:
  4880. is_lvds = true;
  4881. break;
  4882. }
  4883. }
  4884. refclk = ironlake_get_refclk(crtc);
  4885. /*
  4886. * Returns a set of divisors for the desired target clock with the given
  4887. * refclk, or FALSE. The returned values represent the clock equation:
  4888. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4889. */
  4890. limit = intel_limit(crtc, refclk);
  4891. ret = dev_priv->display.find_dpll(limit, crtc,
  4892. to_intel_crtc(crtc)->config.port_clock,
  4893. refclk, NULL, clock);
  4894. if (!ret)
  4895. return false;
  4896. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4897. /*
  4898. * Ensure we match the reduced clock's P to the target clock.
  4899. * If the clocks don't match, we can't switch the display clock
  4900. * by using the FP0/FP1. In such case we will disable the LVDS
  4901. * downclock feature.
  4902. */
  4903. *has_reduced_clock =
  4904. dev_priv->display.find_dpll(limit, crtc,
  4905. dev_priv->lvds_downclock,
  4906. refclk, clock,
  4907. reduced_clock);
  4908. }
  4909. return true;
  4910. }
  4911. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  4912. {
  4913. struct drm_i915_private *dev_priv = dev->dev_private;
  4914. uint32_t temp;
  4915. temp = I915_READ(SOUTH_CHICKEN1);
  4916. if (temp & FDI_BC_BIFURCATION_SELECT)
  4917. return;
  4918. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  4919. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  4920. temp |= FDI_BC_BIFURCATION_SELECT;
  4921. DRM_DEBUG_KMS("enabling fdi C rx\n");
  4922. I915_WRITE(SOUTH_CHICKEN1, temp);
  4923. POSTING_READ(SOUTH_CHICKEN1);
  4924. }
  4925. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  4926. {
  4927. struct drm_device *dev = intel_crtc->base.dev;
  4928. struct drm_i915_private *dev_priv = dev->dev_private;
  4929. switch (intel_crtc->pipe) {
  4930. case PIPE_A:
  4931. break;
  4932. case PIPE_B:
  4933. if (intel_crtc->config.fdi_lanes > 2)
  4934. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  4935. else
  4936. cpt_enable_fdi_bc_bifurcation(dev);
  4937. break;
  4938. case PIPE_C:
  4939. cpt_enable_fdi_bc_bifurcation(dev);
  4940. break;
  4941. default:
  4942. BUG();
  4943. }
  4944. }
  4945. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  4946. {
  4947. /*
  4948. * Account for spread spectrum to avoid
  4949. * oversubscribing the link. Max center spread
  4950. * is 2.5%; use 5% for safety's sake.
  4951. */
  4952. u32 bps = target_clock * bpp * 21 / 20;
  4953. return bps / (link_bw * 8) + 1;
  4954. }
  4955. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  4956. {
  4957. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  4958. }
  4959. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  4960. u32 *fp,
  4961. intel_clock_t *reduced_clock, u32 *fp2)
  4962. {
  4963. struct drm_crtc *crtc = &intel_crtc->base;
  4964. struct drm_device *dev = crtc->dev;
  4965. struct drm_i915_private *dev_priv = dev->dev_private;
  4966. struct intel_encoder *intel_encoder;
  4967. uint32_t dpll;
  4968. int factor, num_connectors = 0;
  4969. bool is_lvds = false, is_sdvo = false;
  4970. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  4971. switch (intel_encoder->type) {
  4972. case INTEL_OUTPUT_LVDS:
  4973. is_lvds = true;
  4974. break;
  4975. case INTEL_OUTPUT_SDVO:
  4976. case INTEL_OUTPUT_HDMI:
  4977. is_sdvo = true;
  4978. break;
  4979. }
  4980. num_connectors++;
  4981. }
  4982. /* Enable autotuning of the PLL clock (if permissible) */
  4983. factor = 21;
  4984. if (is_lvds) {
  4985. if ((intel_panel_use_ssc(dev_priv) &&
  4986. dev_priv->vbt.lvds_ssc_freq == 100) ||
  4987. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  4988. factor = 25;
  4989. } else if (intel_crtc->config.sdvo_tv_clock)
  4990. factor = 20;
  4991. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  4992. *fp |= FP_CB_TUNE;
  4993. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  4994. *fp2 |= FP_CB_TUNE;
  4995. dpll = 0;
  4996. if (is_lvds)
  4997. dpll |= DPLLB_MODE_LVDS;
  4998. else
  4999. dpll |= DPLLB_MODE_DAC_SERIAL;
  5000. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  5001. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  5002. if (is_sdvo)
  5003. dpll |= DPLL_SDVO_HIGH_SPEED;
  5004. if (intel_crtc->config.has_dp_encoder)
  5005. dpll |= DPLL_SDVO_HIGH_SPEED;
  5006. /* compute bitmask from p1 value */
  5007. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5008. /* also FPA1 */
  5009. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5010. switch (intel_crtc->config.dpll.p2) {
  5011. case 5:
  5012. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5013. break;
  5014. case 7:
  5015. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5016. break;
  5017. case 10:
  5018. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5019. break;
  5020. case 14:
  5021. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5022. break;
  5023. }
  5024. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5025. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5026. else
  5027. dpll |= PLL_REF_INPUT_DREFCLK;
  5028. return dpll | DPLL_VCO_ENABLE;
  5029. }
  5030. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  5031. int x, int y,
  5032. struct drm_framebuffer *fb)
  5033. {
  5034. struct drm_device *dev = crtc->dev;
  5035. struct drm_i915_private *dev_priv = dev->dev_private;
  5036. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5037. int pipe = intel_crtc->pipe;
  5038. int plane = intel_crtc->plane;
  5039. int num_connectors = 0;
  5040. intel_clock_t clock, reduced_clock;
  5041. u32 dpll = 0, fp = 0, fp2 = 0;
  5042. bool ok, has_reduced_clock = false;
  5043. bool is_lvds = false;
  5044. struct intel_encoder *encoder;
  5045. struct intel_shared_dpll *pll;
  5046. int ret;
  5047. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5048. switch (encoder->type) {
  5049. case INTEL_OUTPUT_LVDS:
  5050. is_lvds = true;
  5051. break;
  5052. }
  5053. num_connectors++;
  5054. }
  5055. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  5056. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  5057. ok = ironlake_compute_clocks(crtc, &clock,
  5058. &has_reduced_clock, &reduced_clock);
  5059. if (!ok && !intel_crtc->config.clock_set) {
  5060. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5061. return -EINVAL;
  5062. }
  5063. /* Compat-code for transition, will disappear. */
  5064. if (!intel_crtc->config.clock_set) {
  5065. intel_crtc->config.dpll.n = clock.n;
  5066. intel_crtc->config.dpll.m1 = clock.m1;
  5067. intel_crtc->config.dpll.m2 = clock.m2;
  5068. intel_crtc->config.dpll.p1 = clock.p1;
  5069. intel_crtc->config.dpll.p2 = clock.p2;
  5070. }
  5071. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  5072. if (intel_crtc->config.has_pch_encoder) {
  5073. fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
  5074. if (has_reduced_clock)
  5075. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  5076. dpll = ironlake_compute_dpll(intel_crtc,
  5077. &fp, &reduced_clock,
  5078. has_reduced_clock ? &fp2 : NULL);
  5079. intel_crtc->config.dpll_hw_state.dpll = dpll;
  5080. intel_crtc->config.dpll_hw_state.fp0 = fp;
  5081. if (has_reduced_clock)
  5082. intel_crtc->config.dpll_hw_state.fp1 = fp2;
  5083. else
  5084. intel_crtc->config.dpll_hw_state.fp1 = fp;
  5085. pll = intel_get_shared_dpll(intel_crtc);
  5086. if (pll == NULL) {
  5087. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  5088. pipe_name(pipe));
  5089. return -EINVAL;
  5090. }
  5091. } else
  5092. intel_put_shared_dpll(intel_crtc);
  5093. if (intel_crtc->config.has_dp_encoder)
  5094. intel_dp_set_m_n(intel_crtc);
  5095. if (is_lvds && has_reduced_clock && i915_powersave)
  5096. intel_crtc->lowfreq_avail = true;
  5097. else
  5098. intel_crtc->lowfreq_avail = false;
  5099. if (intel_crtc->config.has_pch_encoder) {
  5100. pll = intel_crtc_to_shared_dpll(intel_crtc);
  5101. }
  5102. intel_set_pipe_timings(intel_crtc);
  5103. if (intel_crtc->config.has_pch_encoder) {
  5104. intel_cpu_transcoder_set_m_n(intel_crtc,
  5105. &intel_crtc->config.fdi_m_n);
  5106. }
  5107. if (IS_IVYBRIDGE(dev))
  5108. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  5109. ironlake_set_pipeconf(crtc);
  5110. /* Set up the display plane register */
  5111. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
  5112. POSTING_READ(DSPCNTR(plane));
  5113. ret = intel_pipe_set_base(crtc, x, y, fb);
  5114. return ret;
  5115. }
  5116. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  5117. struct intel_link_m_n *m_n)
  5118. {
  5119. struct drm_device *dev = crtc->base.dev;
  5120. struct drm_i915_private *dev_priv = dev->dev_private;
  5121. enum pipe pipe = crtc->pipe;
  5122. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  5123. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  5124. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  5125. & ~TU_SIZE_MASK;
  5126. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  5127. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  5128. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5129. }
  5130. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  5131. enum transcoder transcoder,
  5132. struct intel_link_m_n *m_n)
  5133. {
  5134. struct drm_device *dev = crtc->base.dev;
  5135. struct drm_i915_private *dev_priv = dev->dev_private;
  5136. enum pipe pipe = crtc->pipe;
  5137. if (INTEL_INFO(dev)->gen >= 5) {
  5138. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  5139. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  5140. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  5141. & ~TU_SIZE_MASK;
  5142. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  5143. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  5144. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5145. } else {
  5146. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  5147. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  5148. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  5149. & ~TU_SIZE_MASK;
  5150. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  5151. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  5152. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5153. }
  5154. }
  5155. void intel_dp_get_m_n(struct intel_crtc *crtc,
  5156. struct intel_crtc_config *pipe_config)
  5157. {
  5158. if (crtc->config.has_pch_encoder)
  5159. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  5160. else
  5161. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  5162. &pipe_config->dp_m_n);
  5163. }
  5164. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  5165. struct intel_crtc_config *pipe_config)
  5166. {
  5167. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  5168. &pipe_config->fdi_m_n);
  5169. }
  5170. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  5171. struct intel_crtc_config *pipe_config)
  5172. {
  5173. struct drm_device *dev = crtc->base.dev;
  5174. struct drm_i915_private *dev_priv = dev->dev_private;
  5175. uint32_t tmp;
  5176. tmp = I915_READ(PF_CTL(crtc->pipe));
  5177. if (tmp & PF_ENABLE) {
  5178. pipe_config->pch_pfit.enabled = true;
  5179. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  5180. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  5181. /* We currently do not free assignements of panel fitters on
  5182. * ivb/hsw (since we don't use the higher upscaling modes which
  5183. * differentiates them) so just WARN about this case for now. */
  5184. if (IS_GEN7(dev)) {
  5185. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  5186. PF_PIPE_SEL_IVB(crtc->pipe));
  5187. }
  5188. }
  5189. }
  5190. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  5191. struct intel_crtc_config *pipe_config)
  5192. {
  5193. struct drm_device *dev = crtc->base.dev;
  5194. struct drm_i915_private *dev_priv = dev->dev_private;
  5195. uint32_t tmp;
  5196. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5197. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5198. tmp = I915_READ(PIPECONF(crtc->pipe));
  5199. if (!(tmp & PIPECONF_ENABLE))
  5200. return false;
  5201. switch (tmp & PIPECONF_BPC_MASK) {
  5202. case PIPECONF_6BPC:
  5203. pipe_config->pipe_bpp = 18;
  5204. break;
  5205. case PIPECONF_8BPC:
  5206. pipe_config->pipe_bpp = 24;
  5207. break;
  5208. case PIPECONF_10BPC:
  5209. pipe_config->pipe_bpp = 30;
  5210. break;
  5211. case PIPECONF_12BPC:
  5212. pipe_config->pipe_bpp = 36;
  5213. break;
  5214. default:
  5215. break;
  5216. }
  5217. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  5218. struct intel_shared_dpll *pll;
  5219. pipe_config->has_pch_encoder = true;
  5220. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  5221. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  5222. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  5223. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  5224. if (HAS_PCH_IBX(dev_priv->dev)) {
  5225. pipe_config->shared_dpll =
  5226. (enum intel_dpll_id) crtc->pipe;
  5227. } else {
  5228. tmp = I915_READ(PCH_DPLL_SEL);
  5229. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  5230. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  5231. else
  5232. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  5233. }
  5234. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  5235. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  5236. &pipe_config->dpll_hw_state));
  5237. tmp = pipe_config->dpll_hw_state.dpll;
  5238. pipe_config->pixel_multiplier =
  5239. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  5240. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  5241. ironlake_pch_clock_get(crtc, pipe_config);
  5242. } else {
  5243. pipe_config->pixel_multiplier = 1;
  5244. }
  5245. intel_get_pipe_timings(crtc, pipe_config);
  5246. ironlake_get_pfit_config(crtc, pipe_config);
  5247. return true;
  5248. }
  5249. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  5250. {
  5251. struct drm_device *dev = dev_priv->dev;
  5252. struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
  5253. struct intel_crtc *crtc;
  5254. unsigned long irqflags;
  5255. uint32_t val;
  5256. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
  5257. WARN(crtc->base.enabled, "CRTC for pipe %c enabled\n",
  5258. pipe_name(crtc->pipe));
  5259. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  5260. WARN(plls->spll_refcount, "SPLL enabled\n");
  5261. WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
  5262. WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
  5263. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  5264. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  5265. "CPU PWM1 enabled\n");
  5266. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  5267. "CPU PWM2 enabled\n");
  5268. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  5269. "PCH PWM1 enabled\n");
  5270. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  5271. "Utility pin enabled\n");
  5272. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  5273. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  5274. val = I915_READ(DEIMR);
  5275. WARN((val & ~DE_PCH_EVENT_IVB) != val,
  5276. "Unexpected DEIMR bits enabled: 0x%x\n", val);
  5277. val = I915_READ(SDEIMR);
  5278. WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
  5279. "Unexpected SDEIMR bits enabled: 0x%x\n", val);
  5280. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  5281. }
  5282. /*
  5283. * This function implements pieces of two sequences from BSpec:
  5284. * - Sequence for display software to disable LCPLL
  5285. * - Sequence for display software to allow package C8+
  5286. * The steps implemented here are just the steps that actually touch the LCPLL
  5287. * register. Callers should take care of disabling all the display engine
  5288. * functions, doing the mode unset, fixing interrupts, etc.
  5289. */
  5290. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  5291. bool switch_to_fclk, bool allow_power_down)
  5292. {
  5293. uint32_t val;
  5294. assert_can_disable_lcpll(dev_priv);
  5295. val = I915_READ(LCPLL_CTL);
  5296. if (switch_to_fclk) {
  5297. val |= LCPLL_CD_SOURCE_FCLK;
  5298. I915_WRITE(LCPLL_CTL, val);
  5299. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  5300. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  5301. DRM_ERROR("Switching to FCLK failed\n");
  5302. val = I915_READ(LCPLL_CTL);
  5303. }
  5304. val |= LCPLL_PLL_DISABLE;
  5305. I915_WRITE(LCPLL_CTL, val);
  5306. POSTING_READ(LCPLL_CTL);
  5307. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  5308. DRM_ERROR("LCPLL still locked\n");
  5309. val = I915_READ(D_COMP);
  5310. val |= D_COMP_COMP_DISABLE;
  5311. mutex_lock(&dev_priv->rps.hw_lock);
  5312. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
  5313. DRM_ERROR("Failed to disable D_COMP\n");
  5314. mutex_unlock(&dev_priv->rps.hw_lock);
  5315. POSTING_READ(D_COMP);
  5316. ndelay(100);
  5317. if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
  5318. DRM_ERROR("D_COMP RCOMP still in progress\n");
  5319. if (allow_power_down) {
  5320. val = I915_READ(LCPLL_CTL);
  5321. val |= LCPLL_POWER_DOWN_ALLOW;
  5322. I915_WRITE(LCPLL_CTL, val);
  5323. POSTING_READ(LCPLL_CTL);
  5324. }
  5325. }
  5326. /*
  5327. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  5328. * source.
  5329. */
  5330. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  5331. {
  5332. uint32_t val;
  5333. val = I915_READ(LCPLL_CTL);
  5334. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  5335. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  5336. return;
  5337. /* Make sure we're not on PC8 state before disabling PC8, otherwise
  5338. * we'll hang the machine! */
  5339. dev_priv->uncore.funcs.force_wake_get(dev_priv);
  5340. if (val & LCPLL_POWER_DOWN_ALLOW) {
  5341. val &= ~LCPLL_POWER_DOWN_ALLOW;
  5342. I915_WRITE(LCPLL_CTL, val);
  5343. POSTING_READ(LCPLL_CTL);
  5344. }
  5345. val = I915_READ(D_COMP);
  5346. val |= D_COMP_COMP_FORCE;
  5347. val &= ~D_COMP_COMP_DISABLE;
  5348. mutex_lock(&dev_priv->rps.hw_lock);
  5349. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
  5350. DRM_ERROR("Failed to enable D_COMP\n");
  5351. mutex_unlock(&dev_priv->rps.hw_lock);
  5352. POSTING_READ(D_COMP);
  5353. val = I915_READ(LCPLL_CTL);
  5354. val &= ~LCPLL_PLL_DISABLE;
  5355. I915_WRITE(LCPLL_CTL, val);
  5356. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  5357. DRM_ERROR("LCPLL not locked yet\n");
  5358. if (val & LCPLL_CD_SOURCE_FCLK) {
  5359. val = I915_READ(LCPLL_CTL);
  5360. val &= ~LCPLL_CD_SOURCE_FCLK;
  5361. I915_WRITE(LCPLL_CTL, val);
  5362. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  5363. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  5364. DRM_ERROR("Switching back to LCPLL failed\n");
  5365. }
  5366. dev_priv->uncore.funcs.force_wake_put(dev_priv);
  5367. }
  5368. void hsw_enable_pc8_work(struct work_struct *__work)
  5369. {
  5370. struct drm_i915_private *dev_priv =
  5371. container_of(to_delayed_work(__work), struct drm_i915_private,
  5372. pc8.enable_work);
  5373. struct drm_device *dev = dev_priv->dev;
  5374. uint32_t val;
  5375. if (dev_priv->pc8.enabled)
  5376. return;
  5377. DRM_DEBUG_KMS("Enabling package C8+\n");
  5378. dev_priv->pc8.enabled = true;
  5379. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  5380. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  5381. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  5382. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  5383. }
  5384. lpt_disable_clkout_dp(dev);
  5385. hsw_pc8_disable_interrupts(dev);
  5386. hsw_disable_lcpll(dev_priv, true, true);
  5387. }
  5388. static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
  5389. {
  5390. WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
  5391. WARN(dev_priv->pc8.disable_count < 1,
  5392. "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
  5393. dev_priv->pc8.disable_count--;
  5394. if (dev_priv->pc8.disable_count != 0)
  5395. return;
  5396. schedule_delayed_work(&dev_priv->pc8.enable_work,
  5397. msecs_to_jiffies(i915_pc8_timeout));
  5398. }
  5399. static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
  5400. {
  5401. struct drm_device *dev = dev_priv->dev;
  5402. uint32_t val;
  5403. WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
  5404. WARN(dev_priv->pc8.disable_count < 0,
  5405. "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
  5406. dev_priv->pc8.disable_count++;
  5407. if (dev_priv->pc8.disable_count != 1)
  5408. return;
  5409. cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
  5410. if (!dev_priv->pc8.enabled)
  5411. return;
  5412. DRM_DEBUG_KMS("Disabling package C8+\n");
  5413. hsw_restore_lcpll(dev_priv);
  5414. hsw_pc8_restore_interrupts(dev);
  5415. lpt_init_pch_refclk(dev);
  5416. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  5417. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  5418. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  5419. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  5420. }
  5421. intel_prepare_ddi(dev);
  5422. i915_gem_init_swizzling(dev);
  5423. mutex_lock(&dev_priv->rps.hw_lock);
  5424. gen6_update_ring_freq(dev);
  5425. mutex_unlock(&dev_priv->rps.hw_lock);
  5426. dev_priv->pc8.enabled = false;
  5427. }
  5428. void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
  5429. {
  5430. mutex_lock(&dev_priv->pc8.lock);
  5431. __hsw_enable_package_c8(dev_priv);
  5432. mutex_unlock(&dev_priv->pc8.lock);
  5433. }
  5434. void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
  5435. {
  5436. mutex_lock(&dev_priv->pc8.lock);
  5437. __hsw_disable_package_c8(dev_priv);
  5438. mutex_unlock(&dev_priv->pc8.lock);
  5439. }
  5440. static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
  5441. {
  5442. struct drm_device *dev = dev_priv->dev;
  5443. struct intel_crtc *crtc;
  5444. uint32_t val;
  5445. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
  5446. if (crtc->base.enabled)
  5447. return false;
  5448. /* This case is still possible since we have the i915.disable_power_well
  5449. * parameter and also the KVMr or something else might be requesting the
  5450. * power well. */
  5451. val = I915_READ(HSW_PWR_WELL_DRIVER);
  5452. if (val != 0) {
  5453. DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
  5454. return false;
  5455. }
  5456. return true;
  5457. }
  5458. /* Since we're called from modeset_global_resources there's no way to
  5459. * symmetrically increase and decrease the refcount, so we use
  5460. * dev_priv->pc8.requirements_met to track whether we already have the refcount
  5461. * or not.
  5462. */
  5463. static void hsw_update_package_c8(struct drm_device *dev)
  5464. {
  5465. struct drm_i915_private *dev_priv = dev->dev_private;
  5466. bool allow;
  5467. if (!i915_enable_pc8)
  5468. return;
  5469. mutex_lock(&dev_priv->pc8.lock);
  5470. allow = hsw_can_enable_package_c8(dev_priv);
  5471. if (allow == dev_priv->pc8.requirements_met)
  5472. goto done;
  5473. dev_priv->pc8.requirements_met = allow;
  5474. if (allow)
  5475. __hsw_enable_package_c8(dev_priv);
  5476. else
  5477. __hsw_disable_package_c8(dev_priv);
  5478. done:
  5479. mutex_unlock(&dev_priv->pc8.lock);
  5480. }
  5481. static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
  5482. {
  5483. if (!dev_priv->pc8.gpu_idle) {
  5484. dev_priv->pc8.gpu_idle = true;
  5485. hsw_enable_package_c8(dev_priv);
  5486. }
  5487. }
  5488. static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
  5489. {
  5490. if (dev_priv->pc8.gpu_idle) {
  5491. dev_priv->pc8.gpu_idle = false;
  5492. hsw_disable_package_c8(dev_priv);
  5493. }
  5494. }
  5495. static void haswell_modeset_global_resources(struct drm_device *dev)
  5496. {
  5497. bool enable = false;
  5498. struct intel_crtc *crtc;
  5499. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  5500. if (!crtc->base.enabled)
  5501. continue;
  5502. if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.enabled ||
  5503. crtc->config.cpu_transcoder != TRANSCODER_EDP)
  5504. enable = true;
  5505. }
  5506. intel_set_power_well(dev, enable);
  5507. hsw_update_package_c8(dev);
  5508. }
  5509. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  5510. int x, int y,
  5511. struct drm_framebuffer *fb)
  5512. {
  5513. struct drm_device *dev = crtc->dev;
  5514. struct drm_i915_private *dev_priv = dev->dev_private;
  5515. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5516. int plane = intel_crtc->plane;
  5517. int ret;
  5518. if (!intel_ddi_pll_mode_set(crtc))
  5519. return -EINVAL;
  5520. if (intel_crtc->config.has_dp_encoder)
  5521. intel_dp_set_m_n(intel_crtc);
  5522. intel_crtc->lowfreq_avail = false;
  5523. intel_set_pipe_timings(intel_crtc);
  5524. if (intel_crtc->config.has_pch_encoder) {
  5525. intel_cpu_transcoder_set_m_n(intel_crtc,
  5526. &intel_crtc->config.fdi_m_n);
  5527. }
  5528. haswell_set_pipeconf(crtc);
  5529. intel_set_pipe_csc(crtc);
  5530. /* Set up the display plane register */
  5531. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
  5532. POSTING_READ(DSPCNTR(plane));
  5533. ret = intel_pipe_set_base(crtc, x, y, fb);
  5534. return ret;
  5535. }
  5536. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  5537. struct intel_crtc_config *pipe_config)
  5538. {
  5539. struct drm_device *dev = crtc->base.dev;
  5540. struct drm_i915_private *dev_priv = dev->dev_private;
  5541. enum intel_display_power_domain pfit_domain;
  5542. uint32_t tmp;
  5543. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5544. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5545. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  5546. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  5547. enum pipe trans_edp_pipe;
  5548. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  5549. default:
  5550. WARN(1, "unknown pipe linked to edp transcoder\n");
  5551. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  5552. case TRANS_DDI_EDP_INPUT_A_ON:
  5553. trans_edp_pipe = PIPE_A;
  5554. break;
  5555. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  5556. trans_edp_pipe = PIPE_B;
  5557. break;
  5558. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  5559. trans_edp_pipe = PIPE_C;
  5560. break;
  5561. }
  5562. if (trans_edp_pipe == crtc->pipe)
  5563. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  5564. }
  5565. if (!intel_display_power_enabled(dev,
  5566. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  5567. return false;
  5568. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  5569. if (!(tmp & PIPECONF_ENABLE))
  5570. return false;
  5571. /*
  5572. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  5573. * DDI E. So just check whether this pipe is wired to DDI E and whether
  5574. * the PCH transcoder is on.
  5575. */
  5576. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  5577. if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
  5578. I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  5579. pipe_config->has_pch_encoder = true;
  5580. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  5581. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  5582. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  5583. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  5584. }
  5585. intel_get_pipe_timings(crtc, pipe_config);
  5586. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  5587. if (intel_display_power_enabled(dev, pfit_domain))
  5588. ironlake_get_pfit_config(crtc, pipe_config);
  5589. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  5590. (I915_READ(IPS_CTL) & IPS_ENABLE);
  5591. pipe_config->pixel_multiplier = 1;
  5592. return true;
  5593. }
  5594. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  5595. int x, int y,
  5596. struct drm_framebuffer *fb)
  5597. {
  5598. struct drm_device *dev = crtc->dev;
  5599. struct drm_i915_private *dev_priv = dev->dev_private;
  5600. struct intel_encoder *encoder;
  5601. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5602. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  5603. int pipe = intel_crtc->pipe;
  5604. int ret;
  5605. drm_vblank_pre_modeset(dev, pipe);
  5606. ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
  5607. drm_vblank_post_modeset(dev, pipe);
  5608. if (ret != 0)
  5609. return ret;
  5610. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5611. DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  5612. encoder->base.base.id,
  5613. drm_get_encoder_name(&encoder->base),
  5614. mode->base.id, mode->name);
  5615. encoder->mode_set(encoder);
  5616. }
  5617. return 0;
  5618. }
  5619. static bool intel_eld_uptodate(struct drm_connector *connector,
  5620. int reg_eldv, uint32_t bits_eldv,
  5621. int reg_elda, uint32_t bits_elda,
  5622. int reg_edid)
  5623. {
  5624. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5625. uint8_t *eld = connector->eld;
  5626. uint32_t i;
  5627. i = I915_READ(reg_eldv);
  5628. i &= bits_eldv;
  5629. if (!eld[0])
  5630. return !i;
  5631. if (!i)
  5632. return false;
  5633. i = I915_READ(reg_elda);
  5634. i &= ~bits_elda;
  5635. I915_WRITE(reg_elda, i);
  5636. for (i = 0; i < eld[2]; i++)
  5637. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  5638. return false;
  5639. return true;
  5640. }
  5641. static void g4x_write_eld(struct drm_connector *connector,
  5642. struct drm_crtc *crtc)
  5643. {
  5644. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5645. uint8_t *eld = connector->eld;
  5646. uint32_t eldv;
  5647. uint32_t len;
  5648. uint32_t i;
  5649. i = I915_READ(G4X_AUD_VID_DID);
  5650. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  5651. eldv = G4X_ELDV_DEVCL_DEVBLC;
  5652. else
  5653. eldv = G4X_ELDV_DEVCTG;
  5654. if (intel_eld_uptodate(connector,
  5655. G4X_AUD_CNTL_ST, eldv,
  5656. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  5657. G4X_HDMIW_HDMIEDID))
  5658. return;
  5659. i = I915_READ(G4X_AUD_CNTL_ST);
  5660. i &= ~(eldv | G4X_ELD_ADDR);
  5661. len = (i >> 9) & 0x1f; /* ELD buffer size */
  5662. I915_WRITE(G4X_AUD_CNTL_ST, i);
  5663. if (!eld[0])
  5664. return;
  5665. len = min_t(uint8_t, eld[2], len);
  5666. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5667. for (i = 0; i < len; i++)
  5668. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  5669. i = I915_READ(G4X_AUD_CNTL_ST);
  5670. i |= eldv;
  5671. I915_WRITE(G4X_AUD_CNTL_ST, i);
  5672. }
  5673. static void haswell_write_eld(struct drm_connector *connector,
  5674. struct drm_crtc *crtc)
  5675. {
  5676. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5677. uint8_t *eld = connector->eld;
  5678. struct drm_device *dev = crtc->dev;
  5679. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5680. uint32_t eldv;
  5681. uint32_t i;
  5682. int len;
  5683. int pipe = to_intel_crtc(crtc)->pipe;
  5684. int tmp;
  5685. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  5686. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  5687. int aud_config = HSW_AUD_CFG(pipe);
  5688. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  5689. DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
  5690. /* Audio output enable */
  5691. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  5692. tmp = I915_READ(aud_cntrl_st2);
  5693. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  5694. I915_WRITE(aud_cntrl_st2, tmp);
  5695. /* Wait for 1 vertical blank */
  5696. intel_wait_for_vblank(dev, pipe);
  5697. /* Set ELD valid state */
  5698. tmp = I915_READ(aud_cntrl_st2);
  5699. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
  5700. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  5701. I915_WRITE(aud_cntrl_st2, tmp);
  5702. tmp = I915_READ(aud_cntrl_st2);
  5703. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
  5704. /* Enable HDMI mode */
  5705. tmp = I915_READ(aud_config);
  5706. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
  5707. /* clear N_programing_enable and N_value_index */
  5708. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  5709. I915_WRITE(aud_config, tmp);
  5710. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  5711. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  5712. intel_crtc->eld_vld = true;
  5713. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  5714. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  5715. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  5716. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  5717. } else
  5718. I915_WRITE(aud_config, 0);
  5719. if (intel_eld_uptodate(connector,
  5720. aud_cntrl_st2, eldv,
  5721. aud_cntl_st, IBX_ELD_ADDRESS,
  5722. hdmiw_hdmiedid))
  5723. return;
  5724. i = I915_READ(aud_cntrl_st2);
  5725. i &= ~eldv;
  5726. I915_WRITE(aud_cntrl_st2, i);
  5727. if (!eld[0])
  5728. return;
  5729. i = I915_READ(aud_cntl_st);
  5730. i &= ~IBX_ELD_ADDRESS;
  5731. I915_WRITE(aud_cntl_st, i);
  5732. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  5733. DRM_DEBUG_DRIVER("port num:%d\n", i);
  5734. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  5735. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5736. for (i = 0; i < len; i++)
  5737. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  5738. i = I915_READ(aud_cntrl_st2);
  5739. i |= eldv;
  5740. I915_WRITE(aud_cntrl_st2, i);
  5741. }
  5742. static void ironlake_write_eld(struct drm_connector *connector,
  5743. struct drm_crtc *crtc)
  5744. {
  5745. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5746. uint8_t *eld = connector->eld;
  5747. uint32_t eldv;
  5748. uint32_t i;
  5749. int len;
  5750. int hdmiw_hdmiedid;
  5751. int aud_config;
  5752. int aud_cntl_st;
  5753. int aud_cntrl_st2;
  5754. int pipe = to_intel_crtc(crtc)->pipe;
  5755. if (HAS_PCH_IBX(connector->dev)) {
  5756. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  5757. aud_config = IBX_AUD_CFG(pipe);
  5758. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  5759. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  5760. } else {
  5761. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  5762. aud_config = CPT_AUD_CFG(pipe);
  5763. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  5764. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  5765. }
  5766. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  5767. i = I915_READ(aud_cntl_st);
  5768. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  5769. if (!i) {
  5770. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  5771. /* operate blindly on all ports */
  5772. eldv = IBX_ELD_VALIDB;
  5773. eldv |= IBX_ELD_VALIDB << 4;
  5774. eldv |= IBX_ELD_VALIDB << 8;
  5775. } else {
  5776. DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
  5777. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  5778. }
  5779. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  5780. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  5781. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  5782. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  5783. } else
  5784. I915_WRITE(aud_config, 0);
  5785. if (intel_eld_uptodate(connector,
  5786. aud_cntrl_st2, eldv,
  5787. aud_cntl_st, IBX_ELD_ADDRESS,
  5788. hdmiw_hdmiedid))
  5789. return;
  5790. i = I915_READ(aud_cntrl_st2);
  5791. i &= ~eldv;
  5792. I915_WRITE(aud_cntrl_st2, i);
  5793. if (!eld[0])
  5794. return;
  5795. i = I915_READ(aud_cntl_st);
  5796. i &= ~IBX_ELD_ADDRESS;
  5797. I915_WRITE(aud_cntl_st, i);
  5798. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  5799. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5800. for (i = 0; i < len; i++)
  5801. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  5802. i = I915_READ(aud_cntrl_st2);
  5803. i |= eldv;
  5804. I915_WRITE(aud_cntrl_st2, i);
  5805. }
  5806. void intel_write_eld(struct drm_encoder *encoder,
  5807. struct drm_display_mode *mode)
  5808. {
  5809. struct drm_crtc *crtc = encoder->crtc;
  5810. struct drm_connector *connector;
  5811. struct drm_device *dev = encoder->dev;
  5812. struct drm_i915_private *dev_priv = dev->dev_private;
  5813. connector = drm_select_eld(encoder, mode);
  5814. if (!connector)
  5815. return;
  5816. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5817. connector->base.id,
  5818. drm_get_connector_name(connector),
  5819. connector->encoder->base.id,
  5820. drm_get_encoder_name(connector->encoder));
  5821. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  5822. if (dev_priv->display.write_eld)
  5823. dev_priv->display.write_eld(connector, crtc);
  5824. }
  5825. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  5826. {
  5827. struct drm_device *dev = crtc->dev;
  5828. struct drm_i915_private *dev_priv = dev->dev_private;
  5829. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5830. bool visible = base != 0;
  5831. u32 cntl;
  5832. if (intel_crtc->cursor_visible == visible)
  5833. return;
  5834. cntl = I915_READ(_CURACNTR);
  5835. if (visible) {
  5836. /* On these chipsets we can only modify the base whilst
  5837. * the cursor is disabled.
  5838. */
  5839. I915_WRITE(_CURABASE, base);
  5840. cntl &= ~(CURSOR_FORMAT_MASK);
  5841. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  5842. cntl |= CURSOR_ENABLE |
  5843. CURSOR_GAMMA_ENABLE |
  5844. CURSOR_FORMAT_ARGB;
  5845. } else
  5846. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  5847. I915_WRITE(_CURACNTR, cntl);
  5848. intel_crtc->cursor_visible = visible;
  5849. }
  5850. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  5851. {
  5852. struct drm_device *dev = crtc->dev;
  5853. struct drm_i915_private *dev_priv = dev->dev_private;
  5854. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5855. int pipe = intel_crtc->pipe;
  5856. bool visible = base != 0;
  5857. if (intel_crtc->cursor_visible != visible) {
  5858. uint32_t cntl = I915_READ(CURCNTR(pipe));
  5859. if (base) {
  5860. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  5861. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5862. cntl |= pipe << 28; /* Connect to correct pipe */
  5863. } else {
  5864. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5865. cntl |= CURSOR_MODE_DISABLE;
  5866. }
  5867. I915_WRITE(CURCNTR(pipe), cntl);
  5868. intel_crtc->cursor_visible = visible;
  5869. }
  5870. /* and commit changes on next vblank */
  5871. I915_WRITE(CURBASE(pipe), base);
  5872. }
  5873. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  5874. {
  5875. struct drm_device *dev = crtc->dev;
  5876. struct drm_i915_private *dev_priv = dev->dev_private;
  5877. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5878. int pipe = intel_crtc->pipe;
  5879. bool visible = base != 0;
  5880. if (intel_crtc->cursor_visible != visible) {
  5881. uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
  5882. if (base) {
  5883. cntl &= ~CURSOR_MODE;
  5884. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5885. } else {
  5886. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5887. cntl |= CURSOR_MODE_DISABLE;
  5888. }
  5889. if (IS_HASWELL(dev)) {
  5890. cntl |= CURSOR_PIPE_CSC_ENABLE;
  5891. cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
  5892. }
  5893. I915_WRITE(CURCNTR_IVB(pipe), cntl);
  5894. intel_crtc->cursor_visible = visible;
  5895. }
  5896. /* and commit changes on next vblank */
  5897. I915_WRITE(CURBASE_IVB(pipe), base);
  5898. }
  5899. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  5900. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  5901. bool on)
  5902. {
  5903. struct drm_device *dev = crtc->dev;
  5904. struct drm_i915_private *dev_priv = dev->dev_private;
  5905. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5906. int pipe = intel_crtc->pipe;
  5907. int x = intel_crtc->cursor_x;
  5908. int y = intel_crtc->cursor_y;
  5909. u32 base = 0, pos = 0;
  5910. bool visible;
  5911. if (on)
  5912. base = intel_crtc->cursor_addr;
  5913. if (x >= intel_crtc->config.pipe_src_w)
  5914. base = 0;
  5915. if (y >= intel_crtc->config.pipe_src_h)
  5916. base = 0;
  5917. if (x < 0) {
  5918. if (x + intel_crtc->cursor_width <= 0)
  5919. base = 0;
  5920. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  5921. x = -x;
  5922. }
  5923. pos |= x << CURSOR_X_SHIFT;
  5924. if (y < 0) {
  5925. if (y + intel_crtc->cursor_height <= 0)
  5926. base = 0;
  5927. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  5928. y = -y;
  5929. }
  5930. pos |= y << CURSOR_Y_SHIFT;
  5931. visible = base != 0;
  5932. if (!visible && !intel_crtc->cursor_visible)
  5933. return;
  5934. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  5935. I915_WRITE(CURPOS_IVB(pipe), pos);
  5936. ivb_update_cursor(crtc, base);
  5937. } else {
  5938. I915_WRITE(CURPOS(pipe), pos);
  5939. if (IS_845G(dev) || IS_I865G(dev))
  5940. i845_update_cursor(crtc, base);
  5941. else
  5942. i9xx_update_cursor(crtc, base);
  5943. }
  5944. }
  5945. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  5946. struct drm_file *file,
  5947. uint32_t handle,
  5948. uint32_t width, uint32_t height)
  5949. {
  5950. struct drm_device *dev = crtc->dev;
  5951. struct drm_i915_private *dev_priv = dev->dev_private;
  5952. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5953. struct drm_i915_gem_object *obj;
  5954. uint32_t addr;
  5955. int ret;
  5956. /* if we want to turn off the cursor ignore width and height */
  5957. if (!handle) {
  5958. DRM_DEBUG_KMS("cursor off\n");
  5959. addr = 0;
  5960. obj = NULL;
  5961. mutex_lock(&dev->struct_mutex);
  5962. goto finish;
  5963. }
  5964. /* Currently we only support 64x64 cursors */
  5965. if (width != 64 || height != 64) {
  5966. DRM_ERROR("we currently only support 64x64 cursors\n");
  5967. return -EINVAL;
  5968. }
  5969. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  5970. if (&obj->base == NULL)
  5971. return -ENOENT;
  5972. if (obj->base.size < width * height * 4) {
  5973. DRM_ERROR("buffer is to small\n");
  5974. ret = -ENOMEM;
  5975. goto fail;
  5976. }
  5977. /* we only need to pin inside GTT if cursor is non-phy */
  5978. mutex_lock(&dev->struct_mutex);
  5979. if (!dev_priv->info->cursor_needs_physical) {
  5980. unsigned alignment;
  5981. if (obj->tiling_mode) {
  5982. DRM_ERROR("cursor cannot be tiled\n");
  5983. ret = -EINVAL;
  5984. goto fail_locked;
  5985. }
  5986. /* Note that the w/a also requires 2 PTE of padding following
  5987. * the bo. We currently fill all unused PTE with the shadow
  5988. * page and so we should always have valid PTE following the
  5989. * cursor preventing the VT-d warning.
  5990. */
  5991. alignment = 0;
  5992. if (need_vtd_wa(dev))
  5993. alignment = 64*1024;
  5994. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  5995. if (ret) {
  5996. DRM_ERROR("failed to move cursor bo into the GTT\n");
  5997. goto fail_locked;
  5998. }
  5999. ret = i915_gem_object_put_fence(obj);
  6000. if (ret) {
  6001. DRM_ERROR("failed to release fence for cursor");
  6002. goto fail_unpin;
  6003. }
  6004. addr = i915_gem_obj_ggtt_offset(obj);
  6005. } else {
  6006. int align = IS_I830(dev) ? 16 * 1024 : 256;
  6007. ret = i915_gem_attach_phys_object(dev, obj,
  6008. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  6009. align);
  6010. if (ret) {
  6011. DRM_ERROR("failed to attach phys object\n");
  6012. goto fail_locked;
  6013. }
  6014. addr = obj->phys_obj->handle->busaddr;
  6015. }
  6016. if (IS_GEN2(dev))
  6017. I915_WRITE(CURSIZE, (height << 12) | width);
  6018. finish:
  6019. if (intel_crtc->cursor_bo) {
  6020. if (dev_priv->info->cursor_needs_physical) {
  6021. if (intel_crtc->cursor_bo != obj)
  6022. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  6023. } else
  6024. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  6025. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  6026. }
  6027. mutex_unlock(&dev->struct_mutex);
  6028. intel_crtc->cursor_addr = addr;
  6029. intel_crtc->cursor_bo = obj;
  6030. intel_crtc->cursor_width = width;
  6031. intel_crtc->cursor_height = height;
  6032. if (intel_crtc->active)
  6033. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6034. return 0;
  6035. fail_unpin:
  6036. i915_gem_object_unpin_from_display_plane(obj);
  6037. fail_locked:
  6038. mutex_unlock(&dev->struct_mutex);
  6039. fail:
  6040. drm_gem_object_unreference_unlocked(&obj->base);
  6041. return ret;
  6042. }
  6043. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  6044. {
  6045. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6046. intel_crtc->cursor_x = x;
  6047. intel_crtc->cursor_y = y;
  6048. if (intel_crtc->active)
  6049. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6050. return 0;
  6051. }
  6052. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  6053. u16 *blue, uint32_t start, uint32_t size)
  6054. {
  6055. int end = (start + size > 256) ? 256 : start + size, i;
  6056. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6057. for (i = start; i < end; i++) {
  6058. intel_crtc->lut_r[i] = red[i] >> 8;
  6059. intel_crtc->lut_g[i] = green[i] >> 8;
  6060. intel_crtc->lut_b[i] = blue[i] >> 8;
  6061. }
  6062. intel_crtc_load_lut(crtc);
  6063. }
  6064. /* VESA 640x480x72Hz mode to set on the pipe */
  6065. static struct drm_display_mode load_detect_mode = {
  6066. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6067. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6068. };
  6069. static struct drm_framebuffer *
  6070. intel_framebuffer_create(struct drm_device *dev,
  6071. struct drm_mode_fb_cmd2 *mode_cmd,
  6072. struct drm_i915_gem_object *obj)
  6073. {
  6074. struct intel_framebuffer *intel_fb;
  6075. int ret;
  6076. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6077. if (!intel_fb) {
  6078. drm_gem_object_unreference_unlocked(&obj->base);
  6079. return ERR_PTR(-ENOMEM);
  6080. }
  6081. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  6082. if (ret) {
  6083. drm_gem_object_unreference_unlocked(&obj->base);
  6084. kfree(intel_fb);
  6085. return ERR_PTR(ret);
  6086. }
  6087. return &intel_fb->base;
  6088. }
  6089. static u32
  6090. intel_framebuffer_pitch_for_width(int width, int bpp)
  6091. {
  6092. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  6093. return ALIGN(pitch, 64);
  6094. }
  6095. static u32
  6096. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  6097. {
  6098. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  6099. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  6100. }
  6101. static struct drm_framebuffer *
  6102. intel_framebuffer_create_for_mode(struct drm_device *dev,
  6103. struct drm_display_mode *mode,
  6104. int depth, int bpp)
  6105. {
  6106. struct drm_i915_gem_object *obj;
  6107. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  6108. obj = i915_gem_alloc_object(dev,
  6109. intel_framebuffer_size_for_mode(mode, bpp));
  6110. if (obj == NULL)
  6111. return ERR_PTR(-ENOMEM);
  6112. mode_cmd.width = mode->hdisplay;
  6113. mode_cmd.height = mode->vdisplay;
  6114. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  6115. bpp);
  6116. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  6117. return intel_framebuffer_create(dev, &mode_cmd, obj);
  6118. }
  6119. static struct drm_framebuffer *
  6120. mode_fits_in_fbdev(struct drm_device *dev,
  6121. struct drm_display_mode *mode)
  6122. {
  6123. struct drm_i915_private *dev_priv = dev->dev_private;
  6124. struct drm_i915_gem_object *obj;
  6125. struct drm_framebuffer *fb;
  6126. if (dev_priv->fbdev == NULL)
  6127. return NULL;
  6128. obj = dev_priv->fbdev->ifb.obj;
  6129. if (obj == NULL)
  6130. return NULL;
  6131. fb = &dev_priv->fbdev->ifb.base;
  6132. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  6133. fb->bits_per_pixel))
  6134. return NULL;
  6135. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  6136. return NULL;
  6137. return fb;
  6138. }
  6139. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  6140. struct drm_display_mode *mode,
  6141. struct intel_load_detect_pipe *old)
  6142. {
  6143. struct intel_crtc *intel_crtc;
  6144. struct intel_encoder *intel_encoder =
  6145. intel_attached_encoder(connector);
  6146. struct drm_crtc *possible_crtc;
  6147. struct drm_encoder *encoder = &intel_encoder->base;
  6148. struct drm_crtc *crtc = NULL;
  6149. struct drm_device *dev = encoder->dev;
  6150. struct drm_framebuffer *fb;
  6151. int i = -1;
  6152. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6153. connector->base.id, drm_get_connector_name(connector),
  6154. encoder->base.id, drm_get_encoder_name(encoder));
  6155. /*
  6156. * Algorithm gets a little messy:
  6157. *
  6158. * - if the connector already has an assigned crtc, use it (but make
  6159. * sure it's on first)
  6160. *
  6161. * - try to find the first unused crtc that can drive this connector,
  6162. * and use that if we find one
  6163. */
  6164. /* See if we already have a CRTC for this connector */
  6165. if (encoder->crtc) {
  6166. crtc = encoder->crtc;
  6167. mutex_lock(&crtc->mutex);
  6168. old->dpms_mode = connector->dpms;
  6169. old->load_detect_temp = false;
  6170. /* Make sure the crtc and connector are running */
  6171. if (connector->dpms != DRM_MODE_DPMS_ON)
  6172. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  6173. return true;
  6174. }
  6175. /* Find an unused one (if possible) */
  6176. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  6177. i++;
  6178. if (!(encoder->possible_crtcs & (1 << i)))
  6179. continue;
  6180. if (!possible_crtc->enabled) {
  6181. crtc = possible_crtc;
  6182. break;
  6183. }
  6184. }
  6185. /*
  6186. * If we didn't find an unused CRTC, don't use any.
  6187. */
  6188. if (!crtc) {
  6189. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  6190. return false;
  6191. }
  6192. mutex_lock(&crtc->mutex);
  6193. intel_encoder->new_crtc = to_intel_crtc(crtc);
  6194. to_intel_connector(connector)->new_encoder = intel_encoder;
  6195. intel_crtc = to_intel_crtc(crtc);
  6196. old->dpms_mode = connector->dpms;
  6197. old->load_detect_temp = true;
  6198. old->release_fb = NULL;
  6199. if (!mode)
  6200. mode = &load_detect_mode;
  6201. /* We need a framebuffer large enough to accommodate all accesses
  6202. * that the plane may generate whilst we perform load detection.
  6203. * We can not rely on the fbcon either being present (we get called
  6204. * during its initialisation to detect all boot displays, or it may
  6205. * not even exist) or that it is large enough to satisfy the
  6206. * requested mode.
  6207. */
  6208. fb = mode_fits_in_fbdev(dev, mode);
  6209. if (fb == NULL) {
  6210. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  6211. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  6212. old->release_fb = fb;
  6213. } else
  6214. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  6215. if (IS_ERR(fb)) {
  6216. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  6217. mutex_unlock(&crtc->mutex);
  6218. return false;
  6219. }
  6220. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  6221. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  6222. if (old->release_fb)
  6223. old->release_fb->funcs->destroy(old->release_fb);
  6224. mutex_unlock(&crtc->mutex);
  6225. return false;
  6226. }
  6227. /* let the connector get through one full cycle before testing */
  6228. intel_wait_for_vblank(dev, intel_crtc->pipe);
  6229. return true;
  6230. }
  6231. void intel_release_load_detect_pipe(struct drm_connector *connector,
  6232. struct intel_load_detect_pipe *old)
  6233. {
  6234. struct intel_encoder *intel_encoder =
  6235. intel_attached_encoder(connector);
  6236. struct drm_encoder *encoder = &intel_encoder->base;
  6237. struct drm_crtc *crtc = encoder->crtc;
  6238. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6239. connector->base.id, drm_get_connector_name(connector),
  6240. encoder->base.id, drm_get_encoder_name(encoder));
  6241. if (old->load_detect_temp) {
  6242. to_intel_connector(connector)->new_encoder = NULL;
  6243. intel_encoder->new_crtc = NULL;
  6244. intel_set_mode(crtc, NULL, 0, 0, NULL);
  6245. if (old->release_fb) {
  6246. drm_framebuffer_unregister_private(old->release_fb);
  6247. drm_framebuffer_unreference(old->release_fb);
  6248. }
  6249. mutex_unlock(&crtc->mutex);
  6250. return;
  6251. }
  6252. /* Switch crtc and encoder back off if necessary */
  6253. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  6254. connector->funcs->dpms(connector, old->dpms_mode);
  6255. mutex_unlock(&crtc->mutex);
  6256. }
  6257. static int i9xx_pll_refclk(struct drm_device *dev,
  6258. const struct intel_crtc_config *pipe_config)
  6259. {
  6260. struct drm_i915_private *dev_priv = dev->dev_private;
  6261. u32 dpll = pipe_config->dpll_hw_state.dpll;
  6262. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  6263. return dev_priv->vbt.lvds_ssc_freq * 1000;
  6264. else if (HAS_PCH_SPLIT(dev))
  6265. return 120000;
  6266. else if (!IS_GEN2(dev))
  6267. return 96000;
  6268. else
  6269. return 48000;
  6270. }
  6271. /* Returns the clock of the currently programmed mode of the given pipe. */
  6272. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  6273. struct intel_crtc_config *pipe_config)
  6274. {
  6275. struct drm_device *dev = crtc->base.dev;
  6276. struct drm_i915_private *dev_priv = dev->dev_private;
  6277. int pipe = pipe_config->cpu_transcoder;
  6278. u32 dpll = pipe_config->dpll_hw_state.dpll;
  6279. u32 fp;
  6280. intel_clock_t clock;
  6281. int refclk = i9xx_pll_refclk(dev, pipe_config);
  6282. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  6283. fp = pipe_config->dpll_hw_state.fp0;
  6284. else
  6285. fp = pipe_config->dpll_hw_state.fp1;
  6286. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  6287. if (IS_PINEVIEW(dev)) {
  6288. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  6289. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  6290. } else {
  6291. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  6292. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  6293. }
  6294. if (!IS_GEN2(dev)) {
  6295. if (IS_PINEVIEW(dev))
  6296. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  6297. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  6298. else
  6299. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  6300. DPLL_FPA01_P1_POST_DIV_SHIFT);
  6301. switch (dpll & DPLL_MODE_MASK) {
  6302. case DPLLB_MODE_DAC_SERIAL:
  6303. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  6304. 5 : 10;
  6305. break;
  6306. case DPLLB_MODE_LVDS:
  6307. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  6308. 7 : 14;
  6309. break;
  6310. default:
  6311. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  6312. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  6313. return;
  6314. }
  6315. if (IS_PINEVIEW(dev))
  6316. pineview_clock(refclk, &clock);
  6317. else
  6318. i9xx_clock(refclk, &clock);
  6319. } else {
  6320. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  6321. if (is_lvds) {
  6322. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  6323. DPLL_FPA01_P1_POST_DIV_SHIFT);
  6324. clock.p2 = 14;
  6325. } else {
  6326. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  6327. clock.p1 = 2;
  6328. else {
  6329. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  6330. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  6331. }
  6332. if (dpll & PLL_P2_DIVIDE_BY_4)
  6333. clock.p2 = 4;
  6334. else
  6335. clock.p2 = 2;
  6336. }
  6337. i9xx_clock(refclk, &clock);
  6338. }
  6339. /*
  6340. * This value includes pixel_multiplier. We will use
  6341. * port_clock to compute adjusted_mode.crtc_clock in the
  6342. * encoder's get_config() function.
  6343. */
  6344. pipe_config->port_clock = clock.dot;
  6345. }
  6346. int intel_dotclock_calculate(int link_freq,
  6347. const struct intel_link_m_n *m_n)
  6348. {
  6349. /*
  6350. * The calculation for the data clock is:
  6351. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  6352. * But we want to avoid losing precison if possible, so:
  6353. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  6354. *
  6355. * and the link clock is simpler:
  6356. * link_clock = (m * link_clock) / n
  6357. */
  6358. if (!m_n->link_n)
  6359. return 0;
  6360. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  6361. }
  6362. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  6363. struct intel_crtc_config *pipe_config)
  6364. {
  6365. struct drm_device *dev = crtc->base.dev;
  6366. /* read out port_clock from the DPLL */
  6367. i9xx_crtc_clock_get(crtc, pipe_config);
  6368. /*
  6369. * This value does not include pixel_multiplier.
  6370. * We will check that port_clock and adjusted_mode.crtc_clock
  6371. * agree once we know their relationship in the encoder's
  6372. * get_config() function.
  6373. */
  6374. pipe_config->adjusted_mode.crtc_clock =
  6375. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  6376. &pipe_config->fdi_m_n);
  6377. }
  6378. /** Returns the currently programmed mode of the given pipe. */
  6379. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  6380. struct drm_crtc *crtc)
  6381. {
  6382. struct drm_i915_private *dev_priv = dev->dev_private;
  6383. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6384. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  6385. struct drm_display_mode *mode;
  6386. struct intel_crtc_config pipe_config;
  6387. int htot = I915_READ(HTOTAL(cpu_transcoder));
  6388. int hsync = I915_READ(HSYNC(cpu_transcoder));
  6389. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  6390. int vsync = I915_READ(VSYNC(cpu_transcoder));
  6391. enum pipe pipe = intel_crtc->pipe;
  6392. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  6393. if (!mode)
  6394. return NULL;
  6395. /*
  6396. * Construct a pipe_config sufficient for getting the clock info
  6397. * back out of crtc_clock_get.
  6398. *
  6399. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  6400. * to use a real value here instead.
  6401. */
  6402. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  6403. pipe_config.pixel_multiplier = 1;
  6404. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  6405. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  6406. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  6407. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  6408. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  6409. mode->hdisplay = (htot & 0xffff) + 1;
  6410. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  6411. mode->hsync_start = (hsync & 0xffff) + 1;
  6412. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  6413. mode->vdisplay = (vtot & 0xffff) + 1;
  6414. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  6415. mode->vsync_start = (vsync & 0xffff) + 1;
  6416. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  6417. drm_mode_set_name(mode);
  6418. return mode;
  6419. }
  6420. static void intel_increase_pllclock(struct drm_crtc *crtc)
  6421. {
  6422. struct drm_device *dev = crtc->dev;
  6423. drm_i915_private_t *dev_priv = dev->dev_private;
  6424. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6425. int pipe = intel_crtc->pipe;
  6426. int dpll_reg = DPLL(pipe);
  6427. int dpll;
  6428. if (HAS_PCH_SPLIT(dev))
  6429. return;
  6430. if (!dev_priv->lvds_downclock_avail)
  6431. return;
  6432. dpll = I915_READ(dpll_reg);
  6433. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  6434. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  6435. assert_panel_unlocked(dev_priv, pipe);
  6436. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  6437. I915_WRITE(dpll_reg, dpll);
  6438. intel_wait_for_vblank(dev, pipe);
  6439. dpll = I915_READ(dpll_reg);
  6440. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  6441. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  6442. }
  6443. }
  6444. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  6445. {
  6446. struct drm_device *dev = crtc->dev;
  6447. drm_i915_private_t *dev_priv = dev->dev_private;
  6448. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6449. if (HAS_PCH_SPLIT(dev))
  6450. return;
  6451. if (!dev_priv->lvds_downclock_avail)
  6452. return;
  6453. /*
  6454. * Since this is called by a timer, we should never get here in
  6455. * the manual case.
  6456. */
  6457. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  6458. int pipe = intel_crtc->pipe;
  6459. int dpll_reg = DPLL(pipe);
  6460. int dpll;
  6461. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  6462. assert_panel_unlocked(dev_priv, pipe);
  6463. dpll = I915_READ(dpll_reg);
  6464. dpll |= DISPLAY_RATE_SELECT_FPA1;
  6465. I915_WRITE(dpll_reg, dpll);
  6466. intel_wait_for_vblank(dev, pipe);
  6467. dpll = I915_READ(dpll_reg);
  6468. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  6469. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  6470. }
  6471. }
  6472. void intel_mark_busy(struct drm_device *dev)
  6473. {
  6474. struct drm_i915_private *dev_priv = dev->dev_private;
  6475. hsw_package_c8_gpu_busy(dev_priv);
  6476. i915_update_gfx_val(dev_priv);
  6477. }
  6478. void intel_mark_idle(struct drm_device *dev)
  6479. {
  6480. struct drm_i915_private *dev_priv = dev->dev_private;
  6481. struct drm_crtc *crtc;
  6482. hsw_package_c8_gpu_idle(dev_priv);
  6483. if (!i915_powersave)
  6484. return;
  6485. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  6486. if (!crtc->fb)
  6487. continue;
  6488. intel_decrease_pllclock(crtc);
  6489. }
  6490. if (dev_priv->info->gen >= 6)
  6491. gen6_rps_idle(dev->dev_private);
  6492. }
  6493. void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
  6494. struct intel_ring_buffer *ring)
  6495. {
  6496. struct drm_device *dev = obj->base.dev;
  6497. struct drm_crtc *crtc;
  6498. if (!i915_powersave)
  6499. return;
  6500. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  6501. if (!crtc->fb)
  6502. continue;
  6503. if (to_intel_framebuffer(crtc->fb)->obj != obj)
  6504. continue;
  6505. intel_increase_pllclock(crtc);
  6506. if (ring && intel_fbc_enabled(dev))
  6507. ring->fbc_dirty = true;
  6508. }
  6509. }
  6510. static void intel_crtc_destroy(struct drm_crtc *crtc)
  6511. {
  6512. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6513. struct drm_device *dev = crtc->dev;
  6514. struct intel_unpin_work *work;
  6515. unsigned long flags;
  6516. spin_lock_irqsave(&dev->event_lock, flags);
  6517. work = intel_crtc->unpin_work;
  6518. intel_crtc->unpin_work = NULL;
  6519. spin_unlock_irqrestore(&dev->event_lock, flags);
  6520. if (work) {
  6521. cancel_work_sync(&work->work);
  6522. kfree(work);
  6523. }
  6524. intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
  6525. drm_crtc_cleanup(crtc);
  6526. kfree(intel_crtc);
  6527. }
  6528. static void intel_unpin_work_fn(struct work_struct *__work)
  6529. {
  6530. struct intel_unpin_work *work =
  6531. container_of(__work, struct intel_unpin_work, work);
  6532. struct drm_device *dev = work->crtc->dev;
  6533. mutex_lock(&dev->struct_mutex);
  6534. intel_unpin_fb_obj(work->old_fb_obj);
  6535. drm_gem_object_unreference(&work->pending_flip_obj->base);
  6536. drm_gem_object_unreference(&work->old_fb_obj->base);
  6537. intel_update_fbc(dev);
  6538. mutex_unlock(&dev->struct_mutex);
  6539. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  6540. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  6541. kfree(work);
  6542. }
  6543. static void do_intel_finish_page_flip(struct drm_device *dev,
  6544. struct drm_crtc *crtc)
  6545. {
  6546. drm_i915_private_t *dev_priv = dev->dev_private;
  6547. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6548. struct intel_unpin_work *work;
  6549. unsigned long flags;
  6550. /* Ignore early vblank irqs */
  6551. if (intel_crtc == NULL)
  6552. return;
  6553. spin_lock_irqsave(&dev->event_lock, flags);
  6554. work = intel_crtc->unpin_work;
  6555. /* Ensure we don't miss a work->pending update ... */
  6556. smp_rmb();
  6557. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  6558. spin_unlock_irqrestore(&dev->event_lock, flags);
  6559. return;
  6560. }
  6561. /* and that the unpin work is consistent wrt ->pending. */
  6562. smp_rmb();
  6563. intel_crtc->unpin_work = NULL;
  6564. if (work->event)
  6565. drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
  6566. drm_vblank_put(dev, intel_crtc->pipe);
  6567. spin_unlock_irqrestore(&dev->event_lock, flags);
  6568. wake_up_all(&dev_priv->pending_flip_queue);
  6569. queue_work(dev_priv->wq, &work->work);
  6570. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  6571. }
  6572. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  6573. {
  6574. drm_i915_private_t *dev_priv = dev->dev_private;
  6575. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  6576. do_intel_finish_page_flip(dev, crtc);
  6577. }
  6578. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  6579. {
  6580. drm_i915_private_t *dev_priv = dev->dev_private;
  6581. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  6582. do_intel_finish_page_flip(dev, crtc);
  6583. }
  6584. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  6585. {
  6586. drm_i915_private_t *dev_priv = dev->dev_private;
  6587. struct intel_crtc *intel_crtc =
  6588. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  6589. unsigned long flags;
  6590. /* NB: An MMIO update of the plane base pointer will also
  6591. * generate a page-flip completion irq, i.e. every modeset
  6592. * is also accompanied by a spurious intel_prepare_page_flip().
  6593. */
  6594. spin_lock_irqsave(&dev->event_lock, flags);
  6595. if (intel_crtc->unpin_work)
  6596. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  6597. spin_unlock_irqrestore(&dev->event_lock, flags);
  6598. }
  6599. inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  6600. {
  6601. /* Ensure that the work item is consistent when activating it ... */
  6602. smp_wmb();
  6603. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  6604. /* and that it is marked active as soon as the irq could fire. */
  6605. smp_wmb();
  6606. }
  6607. static int intel_gen2_queue_flip(struct drm_device *dev,
  6608. struct drm_crtc *crtc,
  6609. struct drm_framebuffer *fb,
  6610. struct drm_i915_gem_object *obj,
  6611. uint32_t flags)
  6612. {
  6613. struct drm_i915_private *dev_priv = dev->dev_private;
  6614. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6615. u32 flip_mask;
  6616. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6617. int ret;
  6618. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6619. if (ret)
  6620. goto err;
  6621. ret = intel_ring_begin(ring, 6);
  6622. if (ret)
  6623. goto err_unpin;
  6624. /* Can't queue multiple flips, so wait for the previous
  6625. * one to finish before executing the next.
  6626. */
  6627. if (intel_crtc->plane)
  6628. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  6629. else
  6630. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  6631. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  6632. intel_ring_emit(ring, MI_NOOP);
  6633. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  6634. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6635. intel_ring_emit(ring, fb->pitches[0]);
  6636. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  6637. intel_ring_emit(ring, 0); /* aux display base address, unused */
  6638. intel_mark_page_flip_active(intel_crtc);
  6639. __intel_ring_advance(ring);
  6640. return 0;
  6641. err_unpin:
  6642. intel_unpin_fb_obj(obj);
  6643. err:
  6644. return ret;
  6645. }
  6646. static int intel_gen3_queue_flip(struct drm_device *dev,
  6647. struct drm_crtc *crtc,
  6648. struct drm_framebuffer *fb,
  6649. struct drm_i915_gem_object *obj,
  6650. uint32_t flags)
  6651. {
  6652. struct drm_i915_private *dev_priv = dev->dev_private;
  6653. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6654. u32 flip_mask;
  6655. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6656. int ret;
  6657. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6658. if (ret)
  6659. goto err;
  6660. ret = intel_ring_begin(ring, 6);
  6661. if (ret)
  6662. goto err_unpin;
  6663. if (intel_crtc->plane)
  6664. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  6665. else
  6666. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  6667. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  6668. intel_ring_emit(ring, MI_NOOP);
  6669. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  6670. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6671. intel_ring_emit(ring, fb->pitches[0]);
  6672. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  6673. intel_ring_emit(ring, MI_NOOP);
  6674. intel_mark_page_flip_active(intel_crtc);
  6675. __intel_ring_advance(ring);
  6676. return 0;
  6677. err_unpin:
  6678. intel_unpin_fb_obj(obj);
  6679. err:
  6680. return ret;
  6681. }
  6682. static int intel_gen4_queue_flip(struct drm_device *dev,
  6683. struct drm_crtc *crtc,
  6684. struct drm_framebuffer *fb,
  6685. struct drm_i915_gem_object *obj,
  6686. uint32_t flags)
  6687. {
  6688. struct drm_i915_private *dev_priv = dev->dev_private;
  6689. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6690. uint32_t pf, pipesrc;
  6691. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6692. int ret;
  6693. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6694. if (ret)
  6695. goto err;
  6696. ret = intel_ring_begin(ring, 4);
  6697. if (ret)
  6698. goto err_unpin;
  6699. /* i965+ uses the linear or tiled offsets from the
  6700. * Display Registers (which do not change across a page-flip)
  6701. * so we need only reprogram the base address.
  6702. */
  6703. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  6704. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6705. intel_ring_emit(ring, fb->pitches[0]);
  6706. intel_ring_emit(ring,
  6707. (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
  6708. obj->tiling_mode);
  6709. /* XXX Enabling the panel-fitter across page-flip is so far
  6710. * untested on non-native modes, so ignore it for now.
  6711. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  6712. */
  6713. pf = 0;
  6714. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  6715. intel_ring_emit(ring, pf | pipesrc);
  6716. intel_mark_page_flip_active(intel_crtc);
  6717. __intel_ring_advance(ring);
  6718. return 0;
  6719. err_unpin:
  6720. intel_unpin_fb_obj(obj);
  6721. err:
  6722. return ret;
  6723. }
  6724. static int intel_gen6_queue_flip(struct drm_device *dev,
  6725. struct drm_crtc *crtc,
  6726. struct drm_framebuffer *fb,
  6727. struct drm_i915_gem_object *obj,
  6728. uint32_t flags)
  6729. {
  6730. struct drm_i915_private *dev_priv = dev->dev_private;
  6731. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6732. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6733. uint32_t pf, pipesrc;
  6734. int ret;
  6735. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6736. if (ret)
  6737. goto err;
  6738. ret = intel_ring_begin(ring, 4);
  6739. if (ret)
  6740. goto err_unpin;
  6741. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  6742. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6743. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  6744. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  6745. /* Contrary to the suggestions in the documentation,
  6746. * "Enable Panel Fitter" does not seem to be required when page
  6747. * flipping with a non-native mode, and worse causes a normal
  6748. * modeset to fail.
  6749. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  6750. */
  6751. pf = 0;
  6752. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  6753. intel_ring_emit(ring, pf | pipesrc);
  6754. intel_mark_page_flip_active(intel_crtc);
  6755. __intel_ring_advance(ring);
  6756. return 0;
  6757. err_unpin:
  6758. intel_unpin_fb_obj(obj);
  6759. err:
  6760. return ret;
  6761. }
  6762. static int intel_gen7_queue_flip(struct drm_device *dev,
  6763. struct drm_crtc *crtc,
  6764. struct drm_framebuffer *fb,
  6765. struct drm_i915_gem_object *obj,
  6766. uint32_t flags)
  6767. {
  6768. struct drm_i915_private *dev_priv = dev->dev_private;
  6769. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6770. struct intel_ring_buffer *ring;
  6771. uint32_t plane_bit = 0;
  6772. int len, ret;
  6773. ring = obj->ring;
  6774. if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
  6775. ring = &dev_priv->ring[BCS];
  6776. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6777. if (ret)
  6778. goto err;
  6779. switch(intel_crtc->plane) {
  6780. case PLANE_A:
  6781. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  6782. break;
  6783. case PLANE_B:
  6784. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  6785. break;
  6786. case PLANE_C:
  6787. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  6788. break;
  6789. default:
  6790. WARN_ONCE(1, "unknown plane in flip command\n");
  6791. ret = -ENODEV;
  6792. goto err_unpin;
  6793. }
  6794. len = 4;
  6795. if (ring->id == RCS)
  6796. len += 6;
  6797. ret = intel_ring_begin(ring, len);
  6798. if (ret)
  6799. goto err_unpin;
  6800. /* Unmask the flip-done completion message. Note that the bspec says that
  6801. * we should do this for both the BCS and RCS, and that we must not unmask
  6802. * more than one flip event at any time (or ensure that one flip message
  6803. * can be sent by waiting for flip-done prior to queueing new flips).
  6804. * Experimentation says that BCS works despite DERRMR masking all
  6805. * flip-done completion events and that unmasking all planes at once
  6806. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  6807. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  6808. */
  6809. if (ring->id == RCS) {
  6810. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  6811. intel_ring_emit(ring, DERRMR);
  6812. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  6813. DERRMR_PIPEB_PRI_FLIP_DONE |
  6814. DERRMR_PIPEC_PRI_FLIP_DONE));
  6815. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
  6816. intel_ring_emit(ring, DERRMR);
  6817. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  6818. }
  6819. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  6820. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  6821. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  6822. intel_ring_emit(ring, (MI_NOOP));
  6823. intel_mark_page_flip_active(intel_crtc);
  6824. __intel_ring_advance(ring);
  6825. return 0;
  6826. err_unpin:
  6827. intel_unpin_fb_obj(obj);
  6828. err:
  6829. return ret;
  6830. }
  6831. static int intel_default_queue_flip(struct drm_device *dev,
  6832. struct drm_crtc *crtc,
  6833. struct drm_framebuffer *fb,
  6834. struct drm_i915_gem_object *obj,
  6835. uint32_t flags)
  6836. {
  6837. return -ENODEV;
  6838. }
  6839. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  6840. struct drm_framebuffer *fb,
  6841. struct drm_pending_vblank_event *event,
  6842. uint32_t page_flip_flags)
  6843. {
  6844. struct drm_device *dev = crtc->dev;
  6845. struct drm_i915_private *dev_priv = dev->dev_private;
  6846. struct drm_framebuffer *old_fb = crtc->fb;
  6847. struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
  6848. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6849. struct intel_unpin_work *work;
  6850. unsigned long flags;
  6851. int ret;
  6852. /* Can't change pixel format via MI display flips. */
  6853. if (fb->pixel_format != crtc->fb->pixel_format)
  6854. return -EINVAL;
  6855. /*
  6856. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  6857. * Note that pitch changes could also affect these register.
  6858. */
  6859. if (INTEL_INFO(dev)->gen > 3 &&
  6860. (fb->offsets[0] != crtc->fb->offsets[0] ||
  6861. fb->pitches[0] != crtc->fb->pitches[0]))
  6862. return -EINVAL;
  6863. work = kzalloc(sizeof(*work), GFP_KERNEL);
  6864. if (work == NULL)
  6865. return -ENOMEM;
  6866. work->event = event;
  6867. work->crtc = crtc;
  6868. work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
  6869. INIT_WORK(&work->work, intel_unpin_work_fn);
  6870. ret = drm_vblank_get(dev, intel_crtc->pipe);
  6871. if (ret)
  6872. goto free_work;
  6873. /* We borrow the event spin lock for protecting unpin_work */
  6874. spin_lock_irqsave(&dev->event_lock, flags);
  6875. if (intel_crtc->unpin_work) {
  6876. spin_unlock_irqrestore(&dev->event_lock, flags);
  6877. kfree(work);
  6878. drm_vblank_put(dev, intel_crtc->pipe);
  6879. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  6880. return -EBUSY;
  6881. }
  6882. intel_crtc->unpin_work = work;
  6883. spin_unlock_irqrestore(&dev->event_lock, flags);
  6884. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  6885. flush_workqueue(dev_priv->wq);
  6886. ret = i915_mutex_lock_interruptible(dev);
  6887. if (ret)
  6888. goto cleanup;
  6889. /* Reference the objects for the scheduled work. */
  6890. drm_gem_object_reference(&work->old_fb_obj->base);
  6891. drm_gem_object_reference(&obj->base);
  6892. crtc->fb = fb;
  6893. work->pending_flip_obj = obj;
  6894. work->enable_stall_check = true;
  6895. atomic_inc(&intel_crtc->unpin_work_count);
  6896. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  6897. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
  6898. if (ret)
  6899. goto cleanup_pending;
  6900. intel_disable_fbc(dev);
  6901. intel_mark_fb_busy(obj, NULL);
  6902. mutex_unlock(&dev->struct_mutex);
  6903. trace_i915_flip_request(intel_crtc->plane, obj);
  6904. return 0;
  6905. cleanup_pending:
  6906. atomic_dec(&intel_crtc->unpin_work_count);
  6907. crtc->fb = old_fb;
  6908. drm_gem_object_unreference(&work->old_fb_obj->base);
  6909. drm_gem_object_unreference(&obj->base);
  6910. mutex_unlock(&dev->struct_mutex);
  6911. cleanup:
  6912. spin_lock_irqsave(&dev->event_lock, flags);
  6913. intel_crtc->unpin_work = NULL;
  6914. spin_unlock_irqrestore(&dev->event_lock, flags);
  6915. drm_vblank_put(dev, intel_crtc->pipe);
  6916. free_work:
  6917. kfree(work);
  6918. return ret;
  6919. }
  6920. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  6921. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  6922. .load_lut = intel_crtc_load_lut,
  6923. };
  6924. static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
  6925. struct drm_crtc *crtc)
  6926. {
  6927. struct drm_device *dev;
  6928. struct drm_crtc *tmp;
  6929. int crtc_mask = 1;
  6930. WARN(!crtc, "checking null crtc?\n");
  6931. dev = crtc->dev;
  6932. list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
  6933. if (tmp == crtc)
  6934. break;
  6935. crtc_mask <<= 1;
  6936. }
  6937. if (encoder->possible_crtcs & crtc_mask)
  6938. return true;
  6939. return false;
  6940. }
  6941. /**
  6942. * intel_modeset_update_staged_output_state
  6943. *
  6944. * Updates the staged output configuration state, e.g. after we've read out the
  6945. * current hw state.
  6946. */
  6947. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  6948. {
  6949. struct intel_encoder *encoder;
  6950. struct intel_connector *connector;
  6951. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6952. base.head) {
  6953. connector->new_encoder =
  6954. to_intel_encoder(connector->base.encoder);
  6955. }
  6956. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6957. base.head) {
  6958. encoder->new_crtc =
  6959. to_intel_crtc(encoder->base.crtc);
  6960. }
  6961. }
  6962. /**
  6963. * intel_modeset_commit_output_state
  6964. *
  6965. * This function copies the stage display pipe configuration to the real one.
  6966. */
  6967. static void intel_modeset_commit_output_state(struct drm_device *dev)
  6968. {
  6969. struct intel_encoder *encoder;
  6970. struct intel_connector *connector;
  6971. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6972. base.head) {
  6973. connector->base.encoder = &connector->new_encoder->base;
  6974. }
  6975. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6976. base.head) {
  6977. encoder->base.crtc = &encoder->new_crtc->base;
  6978. }
  6979. }
  6980. static void
  6981. connected_sink_compute_bpp(struct intel_connector * connector,
  6982. struct intel_crtc_config *pipe_config)
  6983. {
  6984. int bpp = pipe_config->pipe_bpp;
  6985. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  6986. connector->base.base.id,
  6987. drm_get_connector_name(&connector->base));
  6988. /* Don't use an invalid EDID bpc value */
  6989. if (connector->base.display_info.bpc &&
  6990. connector->base.display_info.bpc * 3 < bpp) {
  6991. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  6992. bpp, connector->base.display_info.bpc*3);
  6993. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  6994. }
  6995. /* Clamp bpp to 8 on screens without EDID 1.4 */
  6996. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  6997. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  6998. bpp);
  6999. pipe_config->pipe_bpp = 24;
  7000. }
  7001. }
  7002. static int
  7003. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  7004. struct drm_framebuffer *fb,
  7005. struct intel_crtc_config *pipe_config)
  7006. {
  7007. struct drm_device *dev = crtc->base.dev;
  7008. struct intel_connector *connector;
  7009. int bpp;
  7010. switch (fb->pixel_format) {
  7011. case DRM_FORMAT_C8:
  7012. bpp = 8*3; /* since we go through a colormap */
  7013. break;
  7014. case DRM_FORMAT_XRGB1555:
  7015. case DRM_FORMAT_ARGB1555:
  7016. /* checked in intel_framebuffer_init already */
  7017. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  7018. return -EINVAL;
  7019. case DRM_FORMAT_RGB565:
  7020. bpp = 6*3; /* min is 18bpp */
  7021. break;
  7022. case DRM_FORMAT_XBGR8888:
  7023. case DRM_FORMAT_ABGR8888:
  7024. /* checked in intel_framebuffer_init already */
  7025. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  7026. return -EINVAL;
  7027. case DRM_FORMAT_XRGB8888:
  7028. case DRM_FORMAT_ARGB8888:
  7029. bpp = 8*3;
  7030. break;
  7031. case DRM_FORMAT_XRGB2101010:
  7032. case DRM_FORMAT_ARGB2101010:
  7033. case DRM_FORMAT_XBGR2101010:
  7034. case DRM_FORMAT_ABGR2101010:
  7035. /* checked in intel_framebuffer_init already */
  7036. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  7037. return -EINVAL;
  7038. bpp = 10*3;
  7039. break;
  7040. /* TODO: gen4+ supports 16 bpc floating point, too. */
  7041. default:
  7042. DRM_DEBUG_KMS("unsupported depth\n");
  7043. return -EINVAL;
  7044. }
  7045. pipe_config->pipe_bpp = bpp;
  7046. /* Clamp display bpp to EDID value */
  7047. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7048. base.head) {
  7049. if (!connector->new_encoder ||
  7050. connector->new_encoder->new_crtc != crtc)
  7051. continue;
  7052. connected_sink_compute_bpp(connector, pipe_config);
  7053. }
  7054. return bpp;
  7055. }
  7056. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  7057. {
  7058. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  7059. "type: 0x%x flags: 0x%x\n",
  7060. mode->crtc_clock,
  7061. mode->crtc_hdisplay, mode->crtc_hsync_start,
  7062. mode->crtc_hsync_end, mode->crtc_htotal,
  7063. mode->crtc_vdisplay, mode->crtc_vsync_start,
  7064. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  7065. }
  7066. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  7067. struct intel_crtc_config *pipe_config,
  7068. const char *context)
  7069. {
  7070. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  7071. context, pipe_name(crtc->pipe));
  7072. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  7073. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  7074. pipe_config->pipe_bpp, pipe_config->dither);
  7075. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  7076. pipe_config->has_pch_encoder,
  7077. pipe_config->fdi_lanes,
  7078. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  7079. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  7080. pipe_config->fdi_m_n.tu);
  7081. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  7082. pipe_config->has_dp_encoder,
  7083. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  7084. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  7085. pipe_config->dp_m_n.tu);
  7086. DRM_DEBUG_KMS("requested mode:\n");
  7087. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  7088. DRM_DEBUG_KMS("adjusted mode:\n");
  7089. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  7090. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  7091. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  7092. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  7093. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  7094. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  7095. pipe_config->gmch_pfit.control,
  7096. pipe_config->gmch_pfit.pgm_ratios,
  7097. pipe_config->gmch_pfit.lvds_border_bits);
  7098. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  7099. pipe_config->pch_pfit.pos,
  7100. pipe_config->pch_pfit.size,
  7101. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  7102. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  7103. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  7104. }
  7105. static bool check_encoder_cloning(struct drm_crtc *crtc)
  7106. {
  7107. int num_encoders = 0;
  7108. bool uncloneable_encoders = false;
  7109. struct intel_encoder *encoder;
  7110. list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
  7111. base.head) {
  7112. if (&encoder->new_crtc->base != crtc)
  7113. continue;
  7114. num_encoders++;
  7115. if (!encoder->cloneable)
  7116. uncloneable_encoders = true;
  7117. }
  7118. return !(num_encoders > 1 && uncloneable_encoders);
  7119. }
  7120. static struct intel_crtc_config *
  7121. intel_modeset_pipe_config(struct drm_crtc *crtc,
  7122. struct drm_framebuffer *fb,
  7123. struct drm_display_mode *mode)
  7124. {
  7125. struct drm_device *dev = crtc->dev;
  7126. struct intel_encoder *encoder;
  7127. struct intel_crtc_config *pipe_config;
  7128. int plane_bpp, ret = -EINVAL;
  7129. bool retry = true;
  7130. if (!check_encoder_cloning(crtc)) {
  7131. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  7132. return ERR_PTR(-EINVAL);
  7133. }
  7134. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  7135. if (!pipe_config)
  7136. return ERR_PTR(-ENOMEM);
  7137. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  7138. drm_mode_copy(&pipe_config->requested_mode, mode);
  7139. pipe_config->cpu_transcoder =
  7140. (enum transcoder) to_intel_crtc(crtc)->pipe;
  7141. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7142. /*
  7143. * Sanitize sync polarity flags based on requested ones. If neither
  7144. * positive or negative polarity is requested, treat this as meaning
  7145. * negative polarity.
  7146. */
  7147. if (!(pipe_config->adjusted_mode.flags &
  7148. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  7149. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  7150. if (!(pipe_config->adjusted_mode.flags &
  7151. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  7152. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  7153. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  7154. * plane pixel format and any sink constraints into account. Returns the
  7155. * source plane bpp so that dithering can be selected on mismatches
  7156. * after encoders and crtc also have had their say. */
  7157. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  7158. fb, pipe_config);
  7159. if (plane_bpp < 0)
  7160. goto fail;
  7161. /*
  7162. * Determine the real pipe dimensions. Note that stereo modes can
  7163. * increase the actual pipe size due to the frame doubling and
  7164. * insertion of additional space for blanks between the frame. This
  7165. * is stored in the crtc timings. We use the requested mode to do this
  7166. * computation to clearly distinguish it from the adjusted mode, which
  7167. * can be changed by the connectors in the below retry loop.
  7168. */
  7169. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  7170. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  7171. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  7172. encoder_retry:
  7173. /* Ensure the port clock defaults are reset when retrying. */
  7174. pipe_config->port_clock = 0;
  7175. pipe_config->pixel_multiplier = 1;
  7176. /* Fill in default crtc timings, allow encoders to overwrite them. */
  7177. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  7178. /* Pass our mode to the connectors and the CRTC to give them a chance to
  7179. * adjust it according to limitations or connector properties, and also
  7180. * a chance to reject the mode entirely.
  7181. */
  7182. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7183. base.head) {
  7184. if (&encoder->new_crtc->base != crtc)
  7185. continue;
  7186. if (!(encoder->compute_config(encoder, pipe_config))) {
  7187. DRM_DEBUG_KMS("Encoder config failure\n");
  7188. goto fail;
  7189. }
  7190. }
  7191. /* Set default port clock if not overwritten by the encoder. Needs to be
  7192. * done afterwards in case the encoder adjusts the mode. */
  7193. if (!pipe_config->port_clock)
  7194. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  7195. * pipe_config->pixel_multiplier;
  7196. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  7197. if (ret < 0) {
  7198. DRM_DEBUG_KMS("CRTC fixup failed\n");
  7199. goto fail;
  7200. }
  7201. if (ret == RETRY) {
  7202. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  7203. ret = -EINVAL;
  7204. goto fail;
  7205. }
  7206. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  7207. retry = false;
  7208. goto encoder_retry;
  7209. }
  7210. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  7211. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  7212. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  7213. return pipe_config;
  7214. fail:
  7215. kfree(pipe_config);
  7216. return ERR_PTR(ret);
  7217. }
  7218. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  7219. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  7220. static void
  7221. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  7222. unsigned *prepare_pipes, unsigned *disable_pipes)
  7223. {
  7224. struct intel_crtc *intel_crtc;
  7225. struct drm_device *dev = crtc->dev;
  7226. struct intel_encoder *encoder;
  7227. struct intel_connector *connector;
  7228. struct drm_crtc *tmp_crtc;
  7229. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  7230. /* Check which crtcs have changed outputs connected to them, these need
  7231. * to be part of the prepare_pipes mask. We don't (yet) support global
  7232. * modeset across multiple crtcs, so modeset_pipes will only have one
  7233. * bit set at most. */
  7234. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7235. base.head) {
  7236. if (connector->base.encoder == &connector->new_encoder->base)
  7237. continue;
  7238. if (connector->base.encoder) {
  7239. tmp_crtc = connector->base.encoder->crtc;
  7240. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  7241. }
  7242. if (connector->new_encoder)
  7243. *prepare_pipes |=
  7244. 1 << connector->new_encoder->new_crtc->pipe;
  7245. }
  7246. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7247. base.head) {
  7248. if (encoder->base.crtc == &encoder->new_crtc->base)
  7249. continue;
  7250. if (encoder->base.crtc) {
  7251. tmp_crtc = encoder->base.crtc;
  7252. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  7253. }
  7254. if (encoder->new_crtc)
  7255. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  7256. }
  7257. /* Check for any pipes that will be fully disabled ... */
  7258. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  7259. base.head) {
  7260. bool used = false;
  7261. /* Don't try to disable disabled crtcs. */
  7262. if (!intel_crtc->base.enabled)
  7263. continue;
  7264. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7265. base.head) {
  7266. if (encoder->new_crtc == intel_crtc)
  7267. used = true;
  7268. }
  7269. if (!used)
  7270. *disable_pipes |= 1 << intel_crtc->pipe;
  7271. }
  7272. /* set_mode is also used to update properties on life display pipes. */
  7273. intel_crtc = to_intel_crtc(crtc);
  7274. if (crtc->enabled)
  7275. *prepare_pipes |= 1 << intel_crtc->pipe;
  7276. /*
  7277. * For simplicity do a full modeset on any pipe where the output routing
  7278. * changed. We could be more clever, but that would require us to be
  7279. * more careful with calling the relevant encoder->mode_set functions.
  7280. */
  7281. if (*prepare_pipes)
  7282. *modeset_pipes = *prepare_pipes;
  7283. /* ... and mask these out. */
  7284. *modeset_pipes &= ~(*disable_pipes);
  7285. *prepare_pipes &= ~(*disable_pipes);
  7286. /*
  7287. * HACK: We don't (yet) fully support global modesets. intel_set_config
  7288. * obies this rule, but the modeset restore mode of
  7289. * intel_modeset_setup_hw_state does not.
  7290. */
  7291. *modeset_pipes &= 1 << intel_crtc->pipe;
  7292. *prepare_pipes &= 1 << intel_crtc->pipe;
  7293. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  7294. *modeset_pipes, *prepare_pipes, *disable_pipes);
  7295. }
  7296. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  7297. {
  7298. struct drm_encoder *encoder;
  7299. struct drm_device *dev = crtc->dev;
  7300. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  7301. if (encoder->crtc == crtc)
  7302. return true;
  7303. return false;
  7304. }
  7305. static void
  7306. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  7307. {
  7308. struct intel_encoder *intel_encoder;
  7309. struct intel_crtc *intel_crtc;
  7310. struct drm_connector *connector;
  7311. list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
  7312. base.head) {
  7313. if (!intel_encoder->base.crtc)
  7314. continue;
  7315. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  7316. if (prepare_pipes & (1 << intel_crtc->pipe))
  7317. intel_encoder->connectors_active = false;
  7318. }
  7319. intel_modeset_commit_output_state(dev);
  7320. /* Update computed state. */
  7321. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  7322. base.head) {
  7323. intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
  7324. }
  7325. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  7326. if (!connector->encoder || !connector->encoder->crtc)
  7327. continue;
  7328. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  7329. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  7330. struct drm_property *dpms_property =
  7331. dev->mode_config.dpms_property;
  7332. connector->dpms = DRM_MODE_DPMS_ON;
  7333. drm_object_property_set_value(&connector->base,
  7334. dpms_property,
  7335. DRM_MODE_DPMS_ON);
  7336. intel_encoder = to_intel_encoder(connector->encoder);
  7337. intel_encoder->connectors_active = true;
  7338. }
  7339. }
  7340. }
  7341. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  7342. {
  7343. int diff;
  7344. if (clock1 == clock2)
  7345. return true;
  7346. if (!clock1 || !clock2)
  7347. return false;
  7348. diff = abs(clock1 - clock2);
  7349. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  7350. return true;
  7351. return false;
  7352. }
  7353. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  7354. list_for_each_entry((intel_crtc), \
  7355. &(dev)->mode_config.crtc_list, \
  7356. base.head) \
  7357. if (mask & (1 <<(intel_crtc)->pipe))
  7358. static bool
  7359. intel_pipe_config_compare(struct drm_device *dev,
  7360. struct intel_crtc_config *current_config,
  7361. struct intel_crtc_config *pipe_config)
  7362. {
  7363. #define PIPE_CONF_CHECK_X(name) \
  7364. if (current_config->name != pipe_config->name) { \
  7365. DRM_ERROR("mismatch in " #name " " \
  7366. "(expected 0x%08x, found 0x%08x)\n", \
  7367. current_config->name, \
  7368. pipe_config->name); \
  7369. return false; \
  7370. }
  7371. #define PIPE_CONF_CHECK_I(name) \
  7372. if (current_config->name != pipe_config->name) { \
  7373. DRM_ERROR("mismatch in " #name " " \
  7374. "(expected %i, found %i)\n", \
  7375. current_config->name, \
  7376. pipe_config->name); \
  7377. return false; \
  7378. }
  7379. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  7380. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  7381. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  7382. "(expected %i, found %i)\n", \
  7383. current_config->name & (mask), \
  7384. pipe_config->name & (mask)); \
  7385. return false; \
  7386. }
  7387. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  7388. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  7389. DRM_ERROR("mismatch in " #name " " \
  7390. "(expected %i, found %i)\n", \
  7391. current_config->name, \
  7392. pipe_config->name); \
  7393. return false; \
  7394. }
  7395. #define PIPE_CONF_QUIRK(quirk) \
  7396. ((current_config->quirks | pipe_config->quirks) & (quirk))
  7397. PIPE_CONF_CHECK_I(cpu_transcoder);
  7398. PIPE_CONF_CHECK_I(has_pch_encoder);
  7399. PIPE_CONF_CHECK_I(fdi_lanes);
  7400. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  7401. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  7402. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  7403. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  7404. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  7405. PIPE_CONF_CHECK_I(has_dp_encoder);
  7406. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  7407. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  7408. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  7409. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  7410. PIPE_CONF_CHECK_I(dp_m_n.tu);
  7411. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  7412. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  7413. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  7414. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  7415. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  7416. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  7417. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  7418. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  7419. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  7420. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  7421. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  7422. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  7423. PIPE_CONF_CHECK_I(pixel_multiplier);
  7424. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  7425. DRM_MODE_FLAG_INTERLACE);
  7426. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  7427. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  7428. DRM_MODE_FLAG_PHSYNC);
  7429. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  7430. DRM_MODE_FLAG_NHSYNC);
  7431. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  7432. DRM_MODE_FLAG_PVSYNC);
  7433. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  7434. DRM_MODE_FLAG_NVSYNC);
  7435. }
  7436. PIPE_CONF_CHECK_I(pipe_src_w);
  7437. PIPE_CONF_CHECK_I(pipe_src_h);
  7438. PIPE_CONF_CHECK_I(gmch_pfit.control);
  7439. /* pfit ratios are autocomputed by the hw on gen4+ */
  7440. if (INTEL_INFO(dev)->gen < 4)
  7441. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  7442. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  7443. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  7444. if (current_config->pch_pfit.enabled) {
  7445. PIPE_CONF_CHECK_I(pch_pfit.pos);
  7446. PIPE_CONF_CHECK_I(pch_pfit.size);
  7447. }
  7448. PIPE_CONF_CHECK_I(ips_enabled);
  7449. PIPE_CONF_CHECK_I(double_wide);
  7450. PIPE_CONF_CHECK_I(shared_dpll);
  7451. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  7452. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  7453. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  7454. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  7455. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  7456. PIPE_CONF_CHECK_I(pipe_bpp);
  7457. if (!IS_HASWELL(dev)) {
  7458. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  7459. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  7460. }
  7461. #undef PIPE_CONF_CHECK_X
  7462. #undef PIPE_CONF_CHECK_I
  7463. #undef PIPE_CONF_CHECK_FLAGS
  7464. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  7465. #undef PIPE_CONF_QUIRK
  7466. return true;
  7467. }
  7468. static void
  7469. check_connector_state(struct drm_device *dev)
  7470. {
  7471. struct intel_connector *connector;
  7472. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7473. base.head) {
  7474. /* This also checks the encoder/connector hw state with the
  7475. * ->get_hw_state callbacks. */
  7476. intel_connector_check_state(connector);
  7477. WARN(&connector->new_encoder->base != connector->base.encoder,
  7478. "connector's staged encoder doesn't match current encoder\n");
  7479. }
  7480. }
  7481. static void
  7482. check_encoder_state(struct drm_device *dev)
  7483. {
  7484. struct intel_encoder *encoder;
  7485. struct intel_connector *connector;
  7486. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7487. base.head) {
  7488. bool enabled = false;
  7489. bool active = false;
  7490. enum pipe pipe, tracked_pipe;
  7491. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  7492. encoder->base.base.id,
  7493. drm_get_encoder_name(&encoder->base));
  7494. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  7495. "encoder's stage crtc doesn't match current crtc\n");
  7496. WARN(encoder->connectors_active && !encoder->base.crtc,
  7497. "encoder's active_connectors set, but no crtc\n");
  7498. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7499. base.head) {
  7500. if (connector->base.encoder != &encoder->base)
  7501. continue;
  7502. enabled = true;
  7503. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  7504. active = true;
  7505. }
  7506. WARN(!!encoder->base.crtc != enabled,
  7507. "encoder's enabled state mismatch "
  7508. "(expected %i, found %i)\n",
  7509. !!encoder->base.crtc, enabled);
  7510. WARN(active && !encoder->base.crtc,
  7511. "active encoder with no crtc\n");
  7512. WARN(encoder->connectors_active != active,
  7513. "encoder's computed active state doesn't match tracked active state "
  7514. "(expected %i, found %i)\n", active, encoder->connectors_active);
  7515. active = encoder->get_hw_state(encoder, &pipe);
  7516. WARN(active != encoder->connectors_active,
  7517. "encoder's hw state doesn't match sw tracking "
  7518. "(expected %i, found %i)\n",
  7519. encoder->connectors_active, active);
  7520. if (!encoder->base.crtc)
  7521. continue;
  7522. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  7523. WARN(active && pipe != tracked_pipe,
  7524. "active encoder's pipe doesn't match"
  7525. "(expected %i, found %i)\n",
  7526. tracked_pipe, pipe);
  7527. }
  7528. }
  7529. static void
  7530. check_crtc_state(struct drm_device *dev)
  7531. {
  7532. drm_i915_private_t *dev_priv = dev->dev_private;
  7533. struct intel_crtc *crtc;
  7534. struct intel_encoder *encoder;
  7535. struct intel_crtc_config pipe_config;
  7536. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  7537. base.head) {
  7538. bool enabled = false;
  7539. bool active = false;
  7540. memset(&pipe_config, 0, sizeof(pipe_config));
  7541. DRM_DEBUG_KMS("[CRTC:%d]\n",
  7542. crtc->base.base.id);
  7543. WARN(crtc->active && !crtc->base.enabled,
  7544. "active crtc, but not enabled in sw tracking\n");
  7545. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7546. base.head) {
  7547. if (encoder->base.crtc != &crtc->base)
  7548. continue;
  7549. enabled = true;
  7550. if (encoder->connectors_active)
  7551. active = true;
  7552. }
  7553. WARN(active != crtc->active,
  7554. "crtc's computed active state doesn't match tracked active state "
  7555. "(expected %i, found %i)\n", active, crtc->active);
  7556. WARN(enabled != crtc->base.enabled,
  7557. "crtc's computed enabled state doesn't match tracked enabled state "
  7558. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  7559. active = dev_priv->display.get_pipe_config(crtc,
  7560. &pipe_config);
  7561. /* hw state is inconsistent with the pipe A quirk */
  7562. if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  7563. active = crtc->active;
  7564. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7565. base.head) {
  7566. enum pipe pipe;
  7567. if (encoder->base.crtc != &crtc->base)
  7568. continue;
  7569. if (encoder->get_config &&
  7570. encoder->get_hw_state(encoder, &pipe))
  7571. encoder->get_config(encoder, &pipe_config);
  7572. }
  7573. WARN(crtc->active != active,
  7574. "crtc active state doesn't match with hw state "
  7575. "(expected %i, found %i)\n", crtc->active, active);
  7576. if (active &&
  7577. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  7578. WARN(1, "pipe state doesn't match!\n");
  7579. intel_dump_pipe_config(crtc, &pipe_config,
  7580. "[hw state]");
  7581. intel_dump_pipe_config(crtc, &crtc->config,
  7582. "[sw state]");
  7583. }
  7584. }
  7585. }
  7586. static void
  7587. check_shared_dpll_state(struct drm_device *dev)
  7588. {
  7589. drm_i915_private_t *dev_priv = dev->dev_private;
  7590. struct intel_crtc *crtc;
  7591. struct intel_dpll_hw_state dpll_hw_state;
  7592. int i;
  7593. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7594. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  7595. int enabled_crtcs = 0, active_crtcs = 0;
  7596. bool active;
  7597. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  7598. DRM_DEBUG_KMS("%s\n", pll->name);
  7599. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  7600. WARN(pll->active > pll->refcount,
  7601. "more active pll users than references: %i vs %i\n",
  7602. pll->active, pll->refcount);
  7603. WARN(pll->active && !pll->on,
  7604. "pll in active use but not on in sw tracking\n");
  7605. WARN(pll->on && !pll->active,
  7606. "pll in on but not on in use in sw tracking\n");
  7607. WARN(pll->on != active,
  7608. "pll on state mismatch (expected %i, found %i)\n",
  7609. pll->on, active);
  7610. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  7611. base.head) {
  7612. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  7613. enabled_crtcs++;
  7614. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  7615. active_crtcs++;
  7616. }
  7617. WARN(pll->active != active_crtcs,
  7618. "pll active crtcs mismatch (expected %i, found %i)\n",
  7619. pll->active, active_crtcs);
  7620. WARN(pll->refcount != enabled_crtcs,
  7621. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  7622. pll->refcount, enabled_crtcs);
  7623. WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
  7624. sizeof(dpll_hw_state)),
  7625. "pll hw state mismatch\n");
  7626. }
  7627. }
  7628. void
  7629. intel_modeset_check_state(struct drm_device *dev)
  7630. {
  7631. check_connector_state(dev);
  7632. check_encoder_state(dev);
  7633. check_crtc_state(dev);
  7634. check_shared_dpll_state(dev);
  7635. }
  7636. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  7637. int dotclock)
  7638. {
  7639. /*
  7640. * FDI already provided one idea for the dotclock.
  7641. * Yell if the encoder disagrees.
  7642. */
  7643. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  7644. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  7645. pipe_config->adjusted_mode.crtc_clock, dotclock);
  7646. }
  7647. static int __intel_set_mode(struct drm_crtc *crtc,
  7648. struct drm_display_mode *mode,
  7649. int x, int y, struct drm_framebuffer *fb)
  7650. {
  7651. struct drm_device *dev = crtc->dev;
  7652. drm_i915_private_t *dev_priv = dev->dev_private;
  7653. struct drm_display_mode *saved_mode, *saved_hwmode;
  7654. struct intel_crtc_config *pipe_config = NULL;
  7655. struct intel_crtc *intel_crtc;
  7656. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  7657. int ret = 0;
  7658. saved_mode = kcalloc(2, sizeof(*saved_mode), GFP_KERNEL);
  7659. if (!saved_mode)
  7660. return -ENOMEM;
  7661. saved_hwmode = saved_mode + 1;
  7662. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  7663. &prepare_pipes, &disable_pipes);
  7664. *saved_hwmode = crtc->hwmode;
  7665. *saved_mode = crtc->mode;
  7666. /* Hack: Because we don't (yet) support global modeset on multiple
  7667. * crtcs, we don't keep track of the new mode for more than one crtc.
  7668. * Hence simply check whether any bit is set in modeset_pipes in all the
  7669. * pieces of code that are not yet converted to deal with mutliple crtcs
  7670. * changing their mode at the same time. */
  7671. if (modeset_pipes) {
  7672. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  7673. if (IS_ERR(pipe_config)) {
  7674. ret = PTR_ERR(pipe_config);
  7675. pipe_config = NULL;
  7676. goto out;
  7677. }
  7678. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  7679. "[modeset]");
  7680. }
  7681. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  7682. intel_crtc_disable(&intel_crtc->base);
  7683. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  7684. if (intel_crtc->base.enabled)
  7685. dev_priv->display.crtc_disable(&intel_crtc->base);
  7686. }
  7687. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  7688. * to set it here already despite that we pass it down the callchain.
  7689. */
  7690. if (modeset_pipes) {
  7691. crtc->mode = *mode;
  7692. /* mode_set/enable/disable functions rely on a correct pipe
  7693. * config. */
  7694. to_intel_crtc(crtc)->config = *pipe_config;
  7695. }
  7696. /* Only after disabling all output pipelines that will be changed can we
  7697. * update the the output configuration. */
  7698. intel_modeset_update_state(dev, prepare_pipes);
  7699. if (dev_priv->display.modeset_global_resources)
  7700. dev_priv->display.modeset_global_resources(dev);
  7701. /* Set up the DPLL and any encoders state that needs to adjust or depend
  7702. * on the DPLL.
  7703. */
  7704. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  7705. ret = intel_crtc_mode_set(&intel_crtc->base,
  7706. x, y, fb);
  7707. if (ret)
  7708. goto done;
  7709. }
  7710. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  7711. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
  7712. dev_priv->display.crtc_enable(&intel_crtc->base);
  7713. if (modeset_pipes) {
  7714. /* Store real post-adjustment hardware mode. */
  7715. crtc->hwmode = pipe_config->adjusted_mode;
  7716. /* Calculate and store various constants which
  7717. * are later needed by vblank and swap-completion
  7718. * timestamping. They are derived from true hwmode.
  7719. */
  7720. drm_calc_timestamping_constants(crtc);
  7721. }
  7722. /* FIXME: add subpixel order */
  7723. done:
  7724. if (ret && crtc->enabled) {
  7725. crtc->hwmode = *saved_hwmode;
  7726. crtc->mode = *saved_mode;
  7727. }
  7728. out:
  7729. kfree(pipe_config);
  7730. kfree(saved_mode);
  7731. return ret;
  7732. }
  7733. static int intel_set_mode(struct drm_crtc *crtc,
  7734. struct drm_display_mode *mode,
  7735. int x, int y, struct drm_framebuffer *fb)
  7736. {
  7737. int ret;
  7738. ret = __intel_set_mode(crtc, mode, x, y, fb);
  7739. if (ret == 0)
  7740. intel_modeset_check_state(crtc->dev);
  7741. return ret;
  7742. }
  7743. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  7744. {
  7745. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
  7746. }
  7747. #undef for_each_intel_crtc_masked
  7748. static void intel_set_config_free(struct intel_set_config *config)
  7749. {
  7750. if (!config)
  7751. return;
  7752. kfree(config->save_connector_encoders);
  7753. kfree(config->save_encoder_crtcs);
  7754. kfree(config);
  7755. }
  7756. static int intel_set_config_save_state(struct drm_device *dev,
  7757. struct intel_set_config *config)
  7758. {
  7759. struct drm_encoder *encoder;
  7760. struct drm_connector *connector;
  7761. int count;
  7762. config->save_encoder_crtcs =
  7763. kcalloc(dev->mode_config.num_encoder,
  7764. sizeof(struct drm_crtc *), GFP_KERNEL);
  7765. if (!config->save_encoder_crtcs)
  7766. return -ENOMEM;
  7767. config->save_connector_encoders =
  7768. kcalloc(dev->mode_config.num_connector,
  7769. sizeof(struct drm_encoder *), GFP_KERNEL);
  7770. if (!config->save_connector_encoders)
  7771. return -ENOMEM;
  7772. /* Copy data. Note that driver private data is not affected.
  7773. * Should anything bad happen only the expected state is
  7774. * restored, not the drivers personal bookkeeping.
  7775. */
  7776. count = 0;
  7777. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  7778. config->save_encoder_crtcs[count++] = encoder->crtc;
  7779. }
  7780. count = 0;
  7781. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  7782. config->save_connector_encoders[count++] = connector->encoder;
  7783. }
  7784. return 0;
  7785. }
  7786. static void intel_set_config_restore_state(struct drm_device *dev,
  7787. struct intel_set_config *config)
  7788. {
  7789. struct intel_encoder *encoder;
  7790. struct intel_connector *connector;
  7791. int count;
  7792. count = 0;
  7793. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  7794. encoder->new_crtc =
  7795. to_intel_crtc(config->save_encoder_crtcs[count++]);
  7796. }
  7797. count = 0;
  7798. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  7799. connector->new_encoder =
  7800. to_intel_encoder(config->save_connector_encoders[count++]);
  7801. }
  7802. }
  7803. static bool
  7804. is_crtc_connector_off(struct drm_mode_set *set)
  7805. {
  7806. int i;
  7807. if (set->num_connectors == 0)
  7808. return false;
  7809. if (WARN_ON(set->connectors == NULL))
  7810. return false;
  7811. for (i = 0; i < set->num_connectors; i++)
  7812. if (set->connectors[i]->encoder &&
  7813. set->connectors[i]->encoder->crtc == set->crtc &&
  7814. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  7815. return true;
  7816. return false;
  7817. }
  7818. static void
  7819. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  7820. struct intel_set_config *config)
  7821. {
  7822. /* We should be able to check here if the fb has the same properties
  7823. * and then just flip_or_move it */
  7824. if (is_crtc_connector_off(set)) {
  7825. config->mode_changed = true;
  7826. } else if (set->crtc->fb != set->fb) {
  7827. /* If we have no fb then treat it as a full mode set */
  7828. if (set->crtc->fb == NULL) {
  7829. struct intel_crtc *intel_crtc =
  7830. to_intel_crtc(set->crtc);
  7831. if (intel_crtc->active && i915_fastboot) {
  7832. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  7833. config->fb_changed = true;
  7834. } else {
  7835. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  7836. config->mode_changed = true;
  7837. }
  7838. } else if (set->fb == NULL) {
  7839. config->mode_changed = true;
  7840. } else if (set->fb->pixel_format !=
  7841. set->crtc->fb->pixel_format) {
  7842. config->mode_changed = true;
  7843. } else {
  7844. config->fb_changed = true;
  7845. }
  7846. }
  7847. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  7848. config->fb_changed = true;
  7849. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  7850. DRM_DEBUG_KMS("modes are different, full mode set\n");
  7851. drm_mode_debug_printmodeline(&set->crtc->mode);
  7852. drm_mode_debug_printmodeline(set->mode);
  7853. config->mode_changed = true;
  7854. }
  7855. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  7856. set->crtc->base.id, config->mode_changed, config->fb_changed);
  7857. }
  7858. static int
  7859. intel_modeset_stage_output_state(struct drm_device *dev,
  7860. struct drm_mode_set *set,
  7861. struct intel_set_config *config)
  7862. {
  7863. struct drm_crtc *new_crtc;
  7864. struct intel_connector *connector;
  7865. struct intel_encoder *encoder;
  7866. int ro;
  7867. /* The upper layers ensure that we either disable a crtc or have a list
  7868. * of connectors. For paranoia, double-check this. */
  7869. WARN_ON(!set->fb && (set->num_connectors != 0));
  7870. WARN_ON(set->fb && (set->num_connectors == 0));
  7871. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7872. base.head) {
  7873. /* Otherwise traverse passed in connector list and get encoders
  7874. * for them. */
  7875. for (ro = 0; ro < set->num_connectors; ro++) {
  7876. if (set->connectors[ro] == &connector->base) {
  7877. connector->new_encoder = connector->encoder;
  7878. break;
  7879. }
  7880. }
  7881. /* If we disable the crtc, disable all its connectors. Also, if
  7882. * the connector is on the changing crtc but not on the new
  7883. * connector list, disable it. */
  7884. if ((!set->fb || ro == set->num_connectors) &&
  7885. connector->base.encoder &&
  7886. connector->base.encoder->crtc == set->crtc) {
  7887. connector->new_encoder = NULL;
  7888. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  7889. connector->base.base.id,
  7890. drm_get_connector_name(&connector->base));
  7891. }
  7892. if (&connector->new_encoder->base != connector->base.encoder) {
  7893. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  7894. config->mode_changed = true;
  7895. }
  7896. }
  7897. /* connector->new_encoder is now updated for all connectors. */
  7898. /* Update crtc of enabled connectors. */
  7899. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7900. base.head) {
  7901. if (!connector->new_encoder)
  7902. continue;
  7903. new_crtc = connector->new_encoder->base.crtc;
  7904. for (ro = 0; ro < set->num_connectors; ro++) {
  7905. if (set->connectors[ro] == &connector->base)
  7906. new_crtc = set->crtc;
  7907. }
  7908. /* Make sure the new CRTC will work with the encoder */
  7909. if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
  7910. new_crtc)) {
  7911. return -EINVAL;
  7912. }
  7913. connector->encoder->new_crtc = to_intel_crtc(new_crtc);
  7914. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  7915. connector->base.base.id,
  7916. drm_get_connector_name(&connector->base),
  7917. new_crtc->base.id);
  7918. }
  7919. /* Check for any encoders that needs to be disabled. */
  7920. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7921. base.head) {
  7922. list_for_each_entry(connector,
  7923. &dev->mode_config.connector_list,
  7924. base.head) {
  7925. if (connector->new_encoder == encoder) {
  7926. WARN_ON(!connector->new_encoder->new_crtc);
  7927. goto next_encoder;
  7928. }
  7929. }
  7930. encoder->new_crtc = NULL;
  7931. next_encoder:
  7932. /* Only now check for crtc changes so we don't miss encoders
  7933. * that will be disabled. */
  7934. if (&encoder->new_crtc->base != encoder->base.crtc) {
  7935. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  7936. config->mode_changed = true;
  7937. }
  7938. }
  7939. /* Now we've also updated encoder->new_crtc for all encoders. */
  7940. return 0;
  7941. }
  7942. static int intel_crtc_set_config(struct drm_mode_set *set)
  7943. {
  7944. struct drm_device *dev;
  7945. struct drm_mode_set save_set;
  7946. struct intel_set_config *config;
  7947. int ret;
  7948. BUG_ON(!set);
  7949. BUG_ON(!set->crtc);
  7950. BUG_ON(!set->crtc->helper_private);
  7951. /* Enforce sane interface api - has been abused by the fb helper. */
  7952. BUG_ON(!set->mode && set->fb);
  7953. BUG_ON(set->fb && set->num_connectors == 0);
  7954. if (set->fb) {
  7955. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  7956. set->crtc->base.id, set->fb->base.id,
  7957. (int)set->num_connectors, set->x, set->y);
  7958. } else {
  7959. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  7960. }
  7961. dev = set->crtc->dev;
  7962. ret = -ENOMEM;
  7963. config = kzalloc(sizeof(*config), GFP_KERNEL);
  7964. if (!config)
  7965. goto out_config;
  7966. ret = intel_set_config_save_state(dev, config);
  7967. if (ret)
  7968. goto out_config;
  7969. save_set.crtc = set->crtc;
  7970. save_set.mode = &set->crtc->mode;
  7971. save_set.x = set->crtc->x;
  7972. save_set.y = set->crtc->y;
  7973. save_set.fb = set->crtc->fb;
  7974. /* Compute whether we need a full modeset, only an fb base update or no
  7975. * change at all. In the future we might also check whether only the
  7976. * mode changed, e.g. for LVDS where we only change the panel fitter in
  7977. * such cases. */
  7978. intel_set_config_compute_mode_changes(set, config);
  7979. ret = intel_modeset_stage_output_state(dev, set, config);
  7980. if (ret)
  7981. goto fail;
  7982. if (config->mode_changed) {
  7983. ret = intel_set_mode(set->crtc, set->mode,
  7984. set->x, set->y, set->fb);
  7985. } else if (config->fb_changed) {
  7986. intel_crtc_wait_for_pending_flips(set->crtc);
  7987. ret = intel_pipe_set_base(set->crtc,
  7988. set->x, set->y, set->fb);
  7989. }
  7990. if (ret) {
  7991. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  7992. set->crtc->base.id, ret);
  7993. fail:
  7994. intel_set_config_restore_state(dev, config);
  7995. /* Try to restore the config */
  7996. if (config->mode_changed &&
  7997. intel_set_mode(save_set.crtc, save_set.mode,
  7998. save_set.x, save_set.y, save_set.fb))
  7999. DRM_ERROR("failed to restore config after modeset failure\n");
  8000. }
  8001. out_config:
  8002. intel_set_config_free(config);
  8003. return ret;
  8004. }
  8005. static const struct drm_crtc_funcs intel_crtc_funcs = {
  8006. .cursor_set = intel_crtc_cursor_set,
  8007. .cursor_move = intel_crtc_cursor_move,
  8008. .gamma_set = intel_crtc_gamma_set,
  8009. .set_config = intel_crtc_set_config,
  8010. .destroy = intel_crtc_destroy,
  8011. .page_flip = intel_crtc_page_flip,
  8012. };
  8013. static void intel_cpu_pll_init(struct drm_device *dev)
  8014. {
  8015. if (HAS_DDI(dev))
  8016. intel_ddi_pll_init(dev);
  8017. }
  8018. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  8019. struct intel_shared_dpll *pll,
  8020. struct intel_dpll_hw_state *hw_state)
  8021. {
  8022. uint32_t val;
  8023. val = I915_READ(PCH_DPLL(pll->id));
  8024. hw_state->dpll = val;
  8025. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  8026. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  8027. return val & DPLL_VCO_ENABLE;
  8028. }
  8029. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  8030. struct intel_shared_dpll *pll)
  8031. {
  8032. I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
  8033. I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
  8034. }
  8035. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  8036. struct intel_shared_dpll *pll)
  8037. {
  8038. /* PCH refclock must be enabled first */
  8039. assert_pch_refclk_enabled(dev_priv);
  8040. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  8041. /* Wait for the clocks to stabilize. */
  8042. POSTING_READ(PCH_DPLL(pll->id));
  8043. udelay(150);
  8044. /* The pixel multiplier can only be updated once the
  8045. * DPLL is enabled and the clocks are stable.
  8046. *
  8047. * So write it again.
  8048. */
  8049. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  8050. POSTING_READ(PCH_DPLL(pll->id));
  8051. udelay(200);
  8052. }
  8053. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  8054. struct intel_shared_dpll *pll)
  8055. {
  8056. struct drm_device *dev = dev_priv->dev;
  8057. struct intel_crtc *crtc;
  8058. /* Make sure no transcoder isn't still depending on us. */
  8059. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  8060. if (intel_crtc_to_shared_dpll(crtc) == pll)
  8061. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  8062. }
  8063. I915_WRITE(PCH_DPLL(pll->id), 0);
  8064. POSTING_READ(PCH_DPLL(pll->id));
  8065. udelay(200);
  8066. }
  8067. static char *ibx_pch_dpll_names[] = {
  8068. "PCH DPLL A",
  8069. "PCH DPLL B",
  8070. };
  8071. static void ibx_pch_dpll_init(struct drm_device *dev)
  8072. {
  8073. struct drm_i915_private *dev_priv = dev->dev_private;
  8074. int i;
  8075. dev_priv->num_shared_dpll = 2;
  8076. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  8077. dev_priv->shared_dplls[i].id = i;
  8078. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  8079. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  8080. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  8081. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  8082. dev_priv->shared_dplls[i].get_hw_state =
  8083. ibx_pch_dpll_get_hw_state;
  8084. }
  8085. }
  8086. static void intel_shared_dpll_init(struct drm_device *dev)
  8087. {
  8088. struct drm_i915_private *dev_priv = dev->dev_private;
  8089. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  8090. ibx_pch_dpll_init(dev);
  8091. else
  8092. dev_priv->num_shared_dpll = 0;
  8093. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  8094. DRM_DEBUG_KMS("%i shared PLLs initialized\n",
  8095. dev_priv->num_shared_dpll);
  8096. }
  8097. static void intel_crtc_init(struct drm_device *dev, int pipe)
  8098. {
  8099. drm_i915_private_t *dev_priv = dev->dev_private;
  8100. struct intel_crtc *intel_crtc;
  8101. int i;
  8102. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  8103. if (intel_crtc == NULL)
  8104. return;
  8105. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  8106. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  8107. for (i = 0; i < 256; i++) {
  8108. intel_crtc->lut_r[i] = i;
  8109. intel_crtc->lut_g[i] = i;
  8110. intel_crtc->lut_b[i] = i;
  8111. }
  8112. /* Swap pipes & planes for FBC on pre-965 */
  8113. intel_crtc->pipe = pipe;
  8114. intel_crtc->plane = pipe;
  8115. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  8116. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  8117. intel_crtc->plane = !pipe;
  8118. }
  8119. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  8120. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  8121. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  8122. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  8123. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  8124. }
  8125. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  8126. struct drm_file *file)
  8127. {
  8128. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  8129. struct drm_mode_object *drmmode_obj;
  8130. struct intel_crtc *crtc;
  8131. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  8132. return -ENODEV;
  8133. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  8134. DRM_MODE_OBJECT_CRTC);
  8135. if (!drmmode_obj) {
  8136. DRM_ERROR("no such CRTC id\n");
  8137. return -EINVAL;
  8138. }
  8139. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  8140. pipe_from_crtc_id->pipe = crtc->pipe;
  8141. return 0;
  8142. }
  8143. static int intel_encoder_clones(struct intel_encoder *encoder)
  8144. {
  8145. struct drm_device *dev = encoder->base.dev;
  8146. struct intel_encoder *source_encoder;
  8147. int index_mask = 0;
  8148. int entry = 0;
  8149. list_for_each_entry(source_encoder,
  8150. &dev->mode_config.encoder_list, base.head) {
  8151. if (encoder == source_encoder)
  8152. index_mask |= (1 << entry);
  8153. /* Intel hw has only one MUX where enocoders could be cloned. */
  8154. if (encoder->cloneable && source_encoder->cloneable)
  8155. index_mask |= (1 << entry);
  8156. entry++;
  8157. }
  8158. return index_mask;
  8159. }
  8160. static bool has_edp_a(struct drm_device *dev)
  8161. {
  8162. struct drm_i915_private *dev_priv = dev->dev_private;
  8163. if (!IS_MOBILE(dev))
  8164. return false;
  8165. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  8166. return false;
  8167. if (IS_GEN5(dev) &&
  8168. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  8169. return false;
  8170. return true;
  8171. }
  8172. static void intel_setup_outputs(struct drm_device *dev)
  8173. {
  8174. struct drm_i915_private *dev_priv = dev->dev_private;
  8175. struct intel_encoder *encoder;
  8176. bool dpd_is_edp = false;
  8177. intel_lvds_init(dev);
  8178. if (!IS_ULT(dev))
  8179. intel_crt_init(dev);
  8180. if (HAS_DDI(dev)) {
  8181. int found;
  8182. /* Haswell uses DDI functions to detect digital outputs */
  8183. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  8184. /* DDI A only supports eDP */
  8185. if (found)
  8186. intel_ddi_init(dev, PORT_A);
  8187. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  8188. * register */
  8189. found = I915_READ(SFUSE_STRAP);
  8190. if (found & SFUSE_STRAP_DDIB_DETECTED)
  8191. intel_ddi_init(dev, PORT_B);
  8192. if (found & SFUSE_STRAP_DDIC_DETECTED)
  8193. intel_ddi_init(dev, PORT_C);
  8194. if (found & SFUSE_STRAP_DDID_DETECTED)
  8195. intel_ddi_init(dev, PORT_D);
  8196. } else if (HAS_PCH_SPLIT(dev)) {
  8197. int found;
  8198. dpd_is_edp = intel_dpd_is_edp(dev);
  8199. if (has_edp_a(dev))
  8200. intel_dp_init(dev, DP_A, PORT_A);
  8201. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  8202. /* PCH SDVOB multiplex with HDMIB */
  8203. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  8204. if (!found)
  8205. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  8206. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  8207. intel_dp_init(dev, PCH_DP_B, PORT_B);
  8208. }
  8209. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  8210. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  8211. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  8212. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  8213. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  8214. intel_dp_init(dev, PCH_DP_C, PORT_C);
  8215. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  8216. intel_dp_init(dev, PCH_DP_D, PORT_D);
  8217. } else if (IS_VALLEYVIEW(dev)) {
  8218. /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
  8219. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
  8220. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  8221. PORT_C);
  8222. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  8223. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
  8224. PORT_C);
  8225. }
  8226. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  8227. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  8228. PORT_B);
  8229. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  8230. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  8231. }
  8232. intel_dsi_init(dev);
  8233. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  8234. bool found = false;
  8235. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  8236. DRM_DEBUG_KMS("probing SDVOB\n");
  8237. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  8238. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  8239. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  8240. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  8241. }
  8242. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  8243. intel_dp_init(dev, DP_B, PORT_B);
  8244. }
  8245. /* Before G4X SDVOC doesn't have its own detect register */
  8246. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  8247. DRM_DEBUG_KMS("probing SDVOC\n");
  8248. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  8249. }
  8250. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  8251. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  8252. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  8253. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  8254. }
  8255. if (SUPPORTS_INTEGRATED_DP(dev))
  8256. intel_dp_init(dev, DP_C, PORT_C);
  8257. }
  8258. if (SUPPORTS_INTEGRATED_DP(dev) &&
  8259. (I915_READ(DP_D) & DP_DETECTED))
  8260. intel_dp_init(dev, DP_D, PORT_D);
  8261. } else if (IS_GEN2(dev))
  8262. intel_dvo_init(dev);
  8263. if (SUPPORTS_TV(dev))
  8264. intel_tv_init(dev);
  8265. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  8266. encoder->base.possible_crtcs = encoder->crtc_mask;
  8267. encoder->base.possible_clones =
  8268. intel_encoder_clones(encoder);
  8269. }
  8270. intel_init_pch_refclk(dev);
  8271. drm_helper_move_panel_connectors_to_head(dev);
  8272. }
  8273. void intel_framebuffer_fini(struct intel_framebuffer *fb)
  8274. {
  8275. drm_framebuffer_cleanup(&fb->base);
  8276. drm_gem_object_unreference_unlocked(&fb->obj->base);
  8277. }
  8278. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  8279. {
  8280. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  8281. intel_framebuffer_fini(intel_fb);
  8282. kfree(intel_fb);
  8283. }
  8284. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  8285. struct drm_file *file,
  8286. unsigned int *handle)
  8287. {
  8288. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  8289. struct drm_i915_gem_object *obj = intel_fb->obj;
  8290. return drm_gem_handle_create(file, &obj->base, handle);
  8291. }
  8292. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  8293. .destroy = intel_user_framebuffer_destroy,
  8294. .create_handle = intel_user_framebuffer_create_handle,
  8295. };
  8296. int intel_framebuffer_init(struct drm_device *dev,
  8297. struct intel_framebuffer *intel_fb,
  8298. struct drm_mode_fb_cmd2 *mode_cmd,
  8299. struct drm_i915_gem_object *obj)
  8300. {
  8301. int pitch_limit;
  8302. int ret;
  8303. if (obj->tiling_mode == I915_TILING_Y) {
  8304. DRM_DEBUG("hardware does not support tiling Y\n");
  8305. return -EINVAL;
  8306. }
  8307. if (mode_cmd->pitches[0] & 63) {
  8308. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  8309. mode_cmd->pitches[0]);
  8310. return -EINVAL;
  8311. }
  8312. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  8313. pitch_limit = 32*1024;
  8314. } else if (INTEL_INFO(dev)->gen >= 4) {
  8315. if (obj->tiling_mode)
  8316. pitch_limit = 16*1024;
  8317. else
  8318. pitch_limit = 32*1024;
  8319. } else if (INTEL_INFO(dev)->gen >= 3) {
  8320. if (obj->tiling_mode)
  8321. pitch_limit = 8*1024;
  8322. else
  8323. pitch_limit = 16*1024;
  8324. } else
  8325. /* XXX DSPC is limited to 4k tiled */
  8326. pitch_limit = 8*1024;
  8327. if (mode_cmd->pitches[0] > pitch_limit) {
  8328. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  8329. obj->tiling_mode ? "tiled" : "linear",
  8330. mode_cmd->pitches[0], pitch_limit);
  8331. return -EINVAL;
  8332. }
  8333. if (obj->tiling_mode != I915_TILING_NONE &&
  8334. mode_cmd->pitches[0] != obj->stride) {
  8335. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  8336. mode_cmd->pitches[0], obj->stride);
  8337. return -EINVAL;
  8338. }
  8339. /* Reject formats not supported by any plane early. */
  8340. switch (mode_cmd->pixel_format) {
  8341. case DRM_FORMAT_C8:
  8342. case DRM_FORMAT_RGB565:
  8343. case DRM_FORMAT_XRGB8888:
  8344. case DRM_FORMAT_ARGB8888:
  8345. break;
  8346. case DRM_FORMAT_XRGB1555:
  8347. case DRM_FORMAT_ARGB1555:
  8348. if (INTEL_INFO(dev)->gen > 3) {
  8349. DRM_DEBUG("unsupported pixel format: %s\n",
  8350. drm_get_format_name(mode_cmd->pixel_format));
  8351. return -EINVAL;
  8352. }
  8353. break;
  8354. case DRM_FORMAT_XBGR8888:
  8355. case DRM_FORMAT_ABGR8888:
  8356. case DRM_FORMAT_XRGB2101010:
  8357. case DRM_FORMAT_ARGB2101010:
  8358. case DRM_FORMAT_XBGR2101010:
  8359. case DRM_FORMAT_ABGR2101010:
  8360. if (INTEL_INFO(dev)->gen < 4) {
  8361. DRM_DEBUG("unsupported pixel format: %s\n",
  8362. drm_get_format_name(mode_cmd->pixel_format));
  8363. return -EINVAL;
  8364. }
  8365. break;
  8366. case DRM_FORMAT_YUYV:
  8367. case DRM_FORMAT_UYVY:
  8368. case DRM_FORMAT_YVYU:
  8369. case DRM_FORMAT_VYUY:
  8370. if (INTEL_INFO(dev)->gen < 5) {
  8371. DRM_DEBUG("unsupported pixel format: %s\n",
  8372. drm_get_format_name(mode_cmd->pixel_format));
  8373. return -EINVAL;
  8374. }
  8375. break;
  8376. default:
  8377. DRM_DEBUG("unsupported pixel format: %s\n",
  8378. drm_get_format_name(mode_cmd->pixel_format));
  8379. return -EINVAL;
  8380. }
  8381. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  8382. if (mode_cmd->offsets[0] != 0)
  8383. return -EINVAL;
  8384. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  8385. intel_fb->obj = obj;
  8386. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  8387. if (ret) {
  8388. DRM_ERROR("framebuffer init failed %d\n", ret);
  8389. return ret;
  8390. }
  8391. return 0;
  8392. }
  8393. static struct drm_framebuffer *
  8394. intel_user_framebuffer_create(struct drm_device *dev,
  8395. struct drm_file *filp,
  8396. struct drm_mode_fb_cmd2 *mode_cmd)
  8397. {
  8398. struct drm_i915_gem_object *obj;
  8399. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  8400. mode_cmd->handles[0]));
  8401. if (&obj->base == NULL)
  8402. return ERR_PTR(-ENOENT);
  8403. return intel_framebuffer_create(dev, mode_cmd, obj);
  8404. }
  8405. static const struct drm_mode_config_funcs intel_mode_funcs = {
  8406. .fb_create = intel_user_framebuffer_create,
  8407. .output_poll_changed = intel_fb_output_poll_changed,
  8408. };
  8409. /* Set up chip specific display functions */
  8410. static void intel_init_display(struct drm_device *dev)
  8411. {
  8412. struct drm_i915_private *dev_priv = dev->dev_private;
  8413. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  8414. dev_priv->display.find_dpll = g4x_find_best_dpll;
  8415. else if (IS_VALLEYVIEW(dev))
  8416. dev_priv->display.find_dpll = vlv_find_best_dpll;
  8417. else if (IS_PINEVIEW(dev))
  8418. dev_priv->display.find_dpll = pnv_find_best_dpll;
  8419. else
  8420. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  8421. if (HAS_DDI(dev)) {
  8422. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  8423. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  8424. dev_priv->display.crtc_enable = haswell_crtc_enable;
  8425. dev_priv->display.crtc_disable = haswell_crtc_disable;
  8426. dev_priv->display.off = haswell_crtc_off;
  8427. dev_priv->display.update_plane = ironlake_update_plane;
  8428. } else if (HAS_PCH_SPLIT(dev)) {
  8429. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  8430. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  8431. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  8432. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  8433. dev_priv->display.off = ironlake_crtc_off;
  8434. dev_priv->display.update_plane = ironlake_update_plane;
  8435. } else if (IS_VALLEYVIEW(dev)) {
  8436. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  8437. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  8438. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  8439. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  8440. dev_priv->display.off = i9xx_crtc_off;
  8441. dev_priv->display.update_plane = i9xx_update_plane;
  8442. } else {
  8443. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  8444. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  8445. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  8446. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  8447. dev_priv->display.off = i9xx_crtc_off;
  8448. dev_priv->display.update_plane = i9xx_update_plane;
  8449. }
  8450. /* Returns the core display clock speed */
  8451. if (IS_VALLEYVIEW(dev))
  8452. dev_priv->display.get_display_clock_speed =
  8453. valleyview_get_display_clock_speed;
  8454. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  8455. dev_priv->display.get_display_clock_speed =
  8456. i945_get_display_clock_speed;
  8457. else if (IS_I915G(dev))
  8458. dev_priv->display.get_display_clock_speed =
  8459. i915_get_display_clock_speed;
  8460. else if (IS_I945GM(dev) || IS_845G(dev))
  8461. dev_priv->display.get_display_clock_speed =
  8462. i9xx_misc_get_display_clock_speed;
  8463. else if (IS_PINEVIEW(dev))
  8464. dev_priv->display.get_display_clock_speed =
  8465. pnv_get_display_clock_speed;
  8466. else if (IS_I915GM(dev))
  8467. dev_priv->display.get_display_clock_speed =
  8468. i915gm_get_display_clock_speed;
  8469. else if (IS_I865G(dev))
  8470. dev_priv->display.get_display_clock_speed =
  8471. i865_get_display_clock_speed;
  8472. else if (IS_I85X(dev))
  8473. dev_priv->display.get_display_clock_speed =
  8474. i855_get_display_clock_speed;
  8475. else /* 852, 830 */
  8476. dev_priv->display.get_display_clock_speed =
  8477. i830_get_display_clock_speed;
  8478. if (HAS_PCH_SPLIT(dev)) {
  8479. if (IS_GEN5(dev)) {
  8480. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  8481. dev_priv->display.write_eld = ironlake_write_eld;
  8482. } else if (IS_GEN6(dev)) {
  8483. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  8484. dev_priv->display.write_eld = ironlake_write_eld;
  8485. } else if (IS_IVYBRIDGE(dev)) {
  8486. /* FIXME: detect B0+ stepping and use auto training */
  8487. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  8488. dev_priv->display.write_eld = ironlake_write_eld;
  8489. dev_priv->display.modeset_global_resources =
  8490. ivb_modeset_global_resources;
  8491. } else if (IS_HASWELL(dev)) {
  8492. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  8493. dev_priv->display.write_eld = haswell_write_eld;
  8494. dev_priv->display.modeset_global_resources =
  8495. haswell_modeset_global_resources;
  8496. }
  8497. } else if (IS_G4X(dev)) {
  8498. dev_priv->display.write_eld = g4x_write_eld;
  8499. }
  8500. /* Default just returns -ENODEV to indicate unsupported */
  8501. dev_priv->display.queue_flip = intel_default_queue_flip;
  8502. switch (INTEL_INFO(dev)->gen) {
  8503. case 2:
  8504. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  8505. break;
  8506. case 3:
  8507. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  8508. break;
  8509. case 4:
  8510. case 5:
  8511. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  8512. break;
  8513. case 6:
  8514. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  8515. break;
  8516. case 7:
  8517. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  8518. break;
  8519. }
  8520. }
  8521. /*
  8522. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  8523. * resume, or other times. This quirk makes sure that's the case for
  8524. * affected systems.
  8525. */
  8526. static void quirk_pipea_force(struct drm_device *dev)
  8527. {
  8528. struct drm_i915_private *dev_priv = dev->dev_private;
  8529. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  8530. DRM_INFO("applying pipe a force quirk\n");
  8531. }
  8532. /*
  8533. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  8534. */
  8535. static void quirk_ssc_force_disable(struct drm_device *dev)
  8536. {
  8537. struct drm_i915_private *dev_priv = dev->dev_private;
  8538. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  8539. DRM_INFO("applying lvds SSC disable quirk\n");
  8540. }
  8541. /*
  8542. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  8543. * brightness value
  8544. */
  8545. static void quirk_invert_brightness(struct drm_device *dev)
  8546. {
  8547. struct drm_i915_private *dev_priv = dev->dev_private;
  8548. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  8549. DRM_INFO("applying inverted panel brightness quirk\n");
  8550. }
  8551. /*
  8552. * Some machines (Dell XPS13) suffer broken backlight controls if
  8553. * BLM_PCH_PWM_ENABLE is set.
  8554. */
  8555. static void quirk_no_pcm_pwm_enable(struct drm_device *dev)
  8556. {
  8557. struct drm_i915_private *dev_priv = dev->dev_private;
  8558. dev_priv->quirks |= QUIRK_NO_PCH_PWM_ENABLE;
  8559. DRM_INFO("applying no-PCH_PWM_ENABLE quirk\n");
  8560. }
  8561. struct intel_quirk {
  8562. int device;
  8563. int subsystem_vendor;
  8564. int subsystem_device;
  8565. void (*hook)(struct drm_device *dev);
  8566. };
  8567. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  8568. struct intel_dmi_quirk {
  8569. void (*hook)(struct drm_device *dev);
  8570. const struct dmi_system_id (*dmi_id_list)[];
  8571. };
  8572. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  8573. {
  8574. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  8575. return 1;
  8576. }
  8577. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  8578. {
  8579. .dmi_id_list = &(const struct dmi_system_id[]) {
  8580. {
  8581. .callback = intel_dmi_reverse_brightness,
  8582. .ident = "NCR Corporation",
  8583. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  8584. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  8585. },
  8586. },
  8587. { } /* terminating entry */
  8588. },
  8589. .hook = quirk_invert_brightness,
  8590. },
  8591. };
  8592. static struct intel_quirk intel_quirks[] = {
  8593. /* HP Mini needs pipe A force quirk (LP: #322104) */
  8594. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  8595. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  8596. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  8597. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  8598. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  8599. /* 830/845 need to leave pipe A & dpll A up */
  8600. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  8601. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  8602. /* Lenovo U160 cannot use SSC on LVDS */
  8603. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  8604. /* Sony Vaio Y cannot use SSC on LVDS */
  8605. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  8606. /*
  8607. * All GM45 Acer (and its brands eMachines and Packard Bell) laptops
  8608. * seem to use inverted backlight PWM.
  8609. */
  8610. { 0x2a42, 0x1025, PCI_ANY_ID, quirk_invert_brightness },
  8611. /* Dell XPS13 HD Sandy Bridge */
  8612. { 0x0116, 0x1028, 0x052e, quirk_no_pcm_pwm_enable },
  8613. /* Dell XPS13 HD and XPS13 FHD Ivy Bridge */
  8614. { 0x0166, 0x1028, 0x058b, quirk_no_pcm_pwm_enable },
  8615. };
  8616. static void intel_init_quirks(struct drm_device *dev)
  8617. {
  8618. struct pci_dev *d = dev->pdev;
  8619. int i;
  8620. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  8621. struct intel_quirk *q = &intel_quirks[i];
  8622. if (d->device == q->device &&
  8623. (d->subsystem_vendor == q->subsystem_vendor ||
  8624. q->subsystem_vendor == PCI_ANY_ID) &&
  8625. (d->subsystem_device == q->subsystem_device ||
  8626. q->subsystem_device == PCI_ANY_ID))
  8627. q->hook(dev);
  8628. }
  8629. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  8630. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  8631. intel_dmi_quirks[i].hook(dev);
  8632. }
  8633. }
  8634. /* Disable the VGA plane that we never use */
  8635. static void i915_disable_vga(struct drm_device *dev)
  8636. {
  8637. struct drm_i915_private *dev_priv = dev->dev_private;
  8638. u8 sr1;
  8639. u32 vga_reg = i915_vgacntrl_reg(dev);
  8640. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  8641. outb(SR01, VGA_SR_INDEX);
  8642. sr1 = inb(VGA_SR_DATA);
  8643. outb(sr1 | 1<<5, VGA_SR_DATA);
  8644. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  8645. udelay(300);
  8646. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  8647. POSTING_READ(vga_reg);
  8648. }
  8649. static void i915_enable_vga_mem(struct drm_device *dev)
  8650. {
  8651. /* Enable VGA memory on Intel HD */
  8652. if (HAS_PCH_SPLIT(dev)) {
  8653. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  8654. outb(inb(VGA_MSR_READ) | VGA_MSR_MEM_EN, VGA_MSR_WRITE);
  8655. vga_set_legacy_decoding(dev->pdev, VGA_RSRC_LEGACY_IO |
  8656. VGA_RSRC_LEGACY_MEM |
  8657. VGA_RSRC_NORMAL_IO |
  8658. VGA_RSRC_NORMAL_MEM);
  8659. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  8660. }
  8661. }
  8662. void i915_disable_vga_mem(struct drm_device *dev)
  8663. {
  8664. /* Disable VGA memory on Intel HD */
  8665. if (HAS_PCH_SPLIT(dev)) {
  8666. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  8667. outb(inb(VGA_MSR_READ) & ~VGA_MSR_MEM_EN, VGA_MSR_WRITE);
  8668. vga_set_legacy_decoding(dev->pdev, VGA_RSRC_LEGACY_IO |
  8669. VGA_RSRC_NORMAL_IO |
  8670. VGA_RSRC_NORMAL_MEM);
  8671. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  8672. }
  8673. }
  8674. void intel_modeset_init_hw(struct drm_device *dev)
  8675. {
  8676. struct drm_i915_private *dev_priv = dev->dev_private;
  8677. intel_prepare_ddi(dev);
  8678. intel_init_clock_gating(dev);
  8679. /* Enable the CRI clock source so we can get at the display */
  8680. if (IS_VALLEYVIEW(dev))
  8681. I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
  8682. DPLL_INTEGRATED_CRI_CLK_VLV);
  8683. intel_init_dpio(dev);
  8684. mutex_lock(&dev->struct_mutex);
  8685. intel_enable_gt_powersave(dev);
  8686. mutex_unlock(&dev->struct_mutex);
  8687. }
  8688. void intel_modeset_suspend_hw(struct drm_device *dev)
  8689. {
  8690. intel_suspend_hw(dev);
  8691. }
  8692. void intel_modeset_init(struct drm_device *dev)
  8693. {
  8694. struct drm_i915_private *dev_priv = dev->dev_private;
  8695. int i, j, ret;
  8696. drm_mode_config_init(dev);
  8697. dev->mode_config.min_width = 0;
  8698. dev->mode_config.min_height = 0;
  8699. dev->mode_config.preferred_depth = 24;
  8700. dev->mode_config.prefer_shadow = 1;
  8701. dev->mode_config.funcs = &intel_mode_funcs;
  8702. intel_init_quirks(dev);
  8703. intel_init_pm(dev);
  8704. if (INTEL_INFO(dev)->num_pipes == 0)
  8705. return;
  8706. intel_init_display(dev);
  8707. if (IS_GEN2(dev)) {
  8708. dev->mode_config.max_width = 2048;
  8709. dev->mode_config.max_height = 2048;
  8710. } else if (IS_GEN3(dev)) {
  8711. dev->mode_config.max_width = 4096;
  8712. dev->mode_config.max_height = 4096;
  8713. } else {
  8714. dev->mode_config.max_width = 8192;
  8715. dev->mode_config.max_height = 8192;
  8716. }
  8717. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  8718. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  8719. INTEL_INFO(dev)->num_pipes,
  8720. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  8721. for_each_pipe(i) {
  8722. intel_crtc_init(dev, i);
  8723. for (j = 0; j < dev_priv->num_plane; j++) {
  8724. ret = intel_plane_init(dev, i, j);
  8725. if (ret)
  8726. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  8727. pipe_name(i), sprite_name(i, j), ret);
  8728. }
  8729. }
  8730. intel_cpu_pll_init(dev);
  8731. intel_shared_dpll_init(dev);
  8732. /* Just disable it once at startup */
  8733. i915_disable_vga(dev);
  8734. intel_setup_outputs(dev);
  8735. /* Just in case the BIOS is doing something questionable. */
  8736. intel_disable_fbc(dev);
  8737. }
  8738. static void
  8739. intel_connector_break_all_links(struct intel_connector *connector)
  8740. {
  8741. connector->base.dpms = DRM_MODE_DPMS_OFF;
  8742. connector->base.encoder = NULL;
  8743. connector->encoder->connectors_active = false;
  8744. connector->encoder->base.crtc = NULL;
  8745. }
  8746. static void intel_enable_pipe_a(struct drm_device *dev)
  8747. {
  8748. struct intel_connector *connector;
  8749. struct drm_connector *crt = NULL;
  8750. struct intel_load_detect_pipe load_detect_temp;
  8751. /* We can't just switch on the pipe A, we need to set things up with a
  8752. * proper mode and output configuration. As a gross hack, enable pipe A
  8753. * by enabling the load detect pipe once. */
  8754. list_for_each_entry(connector,
  8755. &dev->mode_config.connector_list,
  8756. base.head) {
  8757. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  8758. crt = &connector->base;
  8759. break;
  8760. }
  8761. }
  8762. if (!crt)
  8763. return;
  8764. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
  8765. intel_release_load_detect_pipe(crt, &load_detect_temp);
  8766. }
  8767. static bool
  8768. intel_check_plane_mapping(struct intel_crtc *crtc)
  8769. {
  8770. struct drm_device *dev = crtc->base.dev;
  8771. struct drm_i915_private *dev_priv = dev->dev_private;
  8772. u32 reg, val;
  8773. if (INTEL_INFO(dev)->num_pipes == 1)
  8774. return true;
  8775. reg = DSPCNTR(!crtc->plane);
  8776. val = I915_READ(reg);
  8777. if ((val & DISPLAY_PLANE_ENABLE) &&
  8778. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  8779. return false;
  8780. return true;
  8781. }
  8782. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  8783. {
  8784. struct drm_device *dev = crtc->base.dev;
  8785. struct drm_i915_private *dev_priv = dev->dev_private;
  8786. u32 reg;
  8787. /* Clear any frame start delays used for debugging left by the BIOS */
  8788. reg = PIPECONF(crtc->config.cpu_transcoder);
  8789. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  8790. /* We need to sanitize the plane -> pipe mapping first because this will
  8791. * disable the crtc (and hence change the state) if it is wrong. Note
  8792. * that gen4+ has a fixed plane -> pipe mapping. */
  8793. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  8794. struct intel_connector *connector;
  8795. bool plane;
  8796. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  8797. crtc->base.base.id);
  8798. /* Pipe has the wrong plane attached and the plane is active.
  8799. * Temporarily change the plane mapping and disable everything
  8800. * ... */
  8801. plane = crtc->plane;
  8802. crtc->plane = !plane;
  8803. dev_priv->display.crtc_disable(&crtc->base);
  8804. crtc->plane = plane;
  8805. /* ... and break all links. */
  8806. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8807. base.head) {
  8808. if (connector->encoder->base.crtc != &crtc->base)
  8809. continue;
  8810. intel_connector_break_all_links(connector);
  8811. }
  8812. WARN_ON(crtc->active);
  8813. crtc->base.enabled = false;
  8814. }
  8815. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  8816. crtc->pipe == PIPE_A && !crtc->active) {
  8817. /* BIOS forgot to enable pipe A, this mostly happens after
  8818. * resume. Force-enable the pipe to fix this, the update_dpms
  8819. * call below we restore the pipe to the right state, but leave
  8820. * the required bits on. */
  8821. intel_enable_pipe_a(dev);
  8822. }
  8823. /* Adjust the state of the output pipe according to whether we
  8824. * have active connectors/encoders. */
  8825. intel_crtc_update_dpms(&crtc->base);
  8826. if (crtc->active != crtc->base.enabled) {
  8827. struct intel_encoder *encoder;
  8828. /* This can happen either due to bugs in the get_hw_state
  8829. * functions or because the pipe is force-enabled due to the
  8830. * pipe A quirk. */
  8831. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  8832. crtc->base.base.id,
  8833. crtc->base.enabled ? "enabled" : "disabled",
  8834. crtc->active ? "enabled" : "disabled");
  8835. crtc->base.enabled = crtc->active;
  8836. /* Because we only establish the connector -> encoder ->
  8837. * crtc links if something is active, this means the
  8838. * crtc is now deactivated. Break the links. connector
  8839. * -> encoder links are only establish when things are
  8840. * actually up, hence no need to break them. */
  8841. WARN_ON(crtc->active);
  8842. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  8843. WARN_ON(encoder->connectors_active);
  8844. encoder->base.crtc = NULL;
  8845. }
  8846. }
  8847. }
  8848. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  8849. {
  8850. struct intel_connector *connector;
  8851. struct drm_device *dev = encoder->base.dev;
  8852. /* We need to check both for a crtc link (meaning that the
  8853. * encoder is active and trying to read from a pipe) and the
  8854. * pipe itself being active. */
  8855. bool has_active_crtc = encoder->base.crtc &&
  8856. to_intel_crtc(encoder->base.crtc)->active;
  8857. if (encoder->connectors_active && !has_active_crtc) {
  8858. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  8859. encoder->base.base.id,
  8860. drm_get_encoder_name(&encoder->base));
  8861. /* Connector is active, but has no active pipe. This is
  8862. * fallout from our resume register restoring. Disable
  8863. * the encoder manually again. */
  8864. if (encoder->base.crtc) {
  8865. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  8866. encoder->base.base.id,
  8867. drm_get_encoder_name(&encoder->base));
  8868. encoder->disable(encoder);
  8869. }
  8870. /* Inconsistent output/port/pipe state happens presumably due to
  8871. * a bug in one of the get_hw_state functions. Or someplace else
  8872. * in our code, like the register restore mess on resume. Clamp
  8873. * things to off as a safer default. */
  8874. list_for_each_entry(connector,
  8875. &dev->mode_config.connector_list,
  8876. base.head) {
  8877. if (connector->encoder != encoder)
  8878. continue;
  8879. intel_connector_break_all_links(connector);
  8880. }
  8881. }
  8882. /* Enabled encoders without active connectors will be fixed in
  8883. * the crtc fixup. */
  8884. }
  8885. void i915_redisable_vga(struct drm_device *dev)
  8886. {
  8887. struct drm_i915_private *dev_priv = dev->dev_private;
  8888. u32 vga_reg = i915_vgacntrl_reg(dev);
  8889. /* This function can be called both from intel_modeset_setup_hw_state or
  8890. * at a very early point in our resume sequence, where the power well
  8891. * structures are not yet restored. Since this function is at a very
  8892. * paranoid "someone might have enabled VGA while we were not looking"
  8893. * level, just check if the power well is enabled instead of trying to
  8894. * follow the "don't touch the power well if we don't need it" policy
  8895. * the rest of the driver uses. */
  8896. if (HAS_POWER_WELL(dev) &&
  8897. (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
  8898. return;
  8899. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  8900. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  8901. i915_disable_vga(dev);
  8902. i915_disable_vga_mem(dev);
  8903. }
  8904. }
  8905. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  8906. {
  8907. struct drm_i915_private *dev_priv = dev->dev_private;
  8908. enum pipe pipe;
  8909. struct intel_crtc *crtc;
  8910. struct intel_encoder *encoder;
  8911. struct intel_connector *connector;
  8912. int i;
  8913. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  8914. base.head) {
  8915. memset(&crtc->config, 0, sizeof(crtc->config));
  8916. crtc->active = dev_priv->display.get_pipe_config(crtc,
  8917. &crtc->config);
  8918. crtc->base.enabled = crtc->active;
  8919. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  8920. crtc->base.base.id,
  8921. crtc->active ? "enabled" : "disabled");
  8922. }
  8923. /* FIXME: Smash this into the new shared dpll infrastructure. */
  8924. if (HAS_DDI(dev))
  8925. intel_ddi_setup_hw_pll_state(dev);
  8926. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  8927. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  8928. pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
  8929. pll->active = 0;
  8930. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  8931. base.head) {
  8932. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  8933. pll->active++;
  8934. }
  8935. pll->refcount = pll->active;
  8936. DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
  8937. pll->name, pll->refcount, pll->on);
  8938. }
  8939. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8940. base.head) {
  8941. pipe = 0;
  8942. if (encoder->get_hw_state(encoder, &pipe)) {
  8943. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  8944. encoder->base.crtc = &crtc->base;
  8945. if (encoder->get_config)
  8946. encoder->get_config(encoder, &crtc->config);
  8947. } else {
  8948. encoder->base.crtc = NULL;
  8949. }
  8950. encoder->connectors_active = false;
  8951. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
  8952. encoder->base.base.id,
  8953. drm_get_encoder_name(&encoder->base),
  8954. encoder->base.crtc ? "enabled" : "disabled",
  8955. pipe);
  8956. }
  8957. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8958. base.head) {
  8959. if (connector->get_hw_state(connector)) {
  8960. connector->base.dpms = DRM_MODE_DPMS_ON;
  8961. connector->encoder->connectors_active = true;
  8962. connector->base.encoder = &connector->encoder->base;
  8963. } else {
  8964. connector->base.dpms = DRM_MODE_DPMS_OFF;
  8965. connector->base.encoder = NULL;
  8966. }
  8967. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  8968. connector->base.base.id,
  8969. drm_get_connector_name(&connector->base),
  8970. connector->base.encoder ? "enabled" : "disabled");
  8971. }
  8972. }
  8973. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  8974. * and i915 state tracking structures. */
  8975. void intel_modeset_setup_hw_state(struct drm_device *dev,
  8976. bool force_restore)
  8977. {
  8978. struct drm_i915_private *dev_priv = dev->dev_private;
  8979. enum pipe pipe;
  8980. struct intel_crtc *crtc;
  8981. struct intel_encoder *encoder;
  8982. int i;
  8983. intel_modeset_readout_hw_state(dev);
  8984. /*
  8985. * Now that we have the config, copy it to each CRTC struct
  8986. * Note that this could go away if we move to using crtc_config
  8987. * checking everywhere.
  8988. */
  8989. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  8990. base.head) {
  8991. if (crtc->active && i915_fastboot) {
  8992. intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
  8993. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  8994. crtc->base.base.id);
  8995. drm_mode_debug_printmodeline(&crtc->base.mode);
  8996. }
  8997. }
  8998. /* HW state is read out, now we need to sanitize this mess. */
  8999. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  9000. base.head) {
  9001. intel_sanitize_encoder(encoder);
  9002. }
  9003. for_each_pipe(pipe) {
  9004. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  9005. intel_sanitize_crtc(crtc);
  9006. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  9007. }
  9008. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9009. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9010. if (!pll->on || pll->active)
  9011. continue;
  9012. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  9013. pll->disable(dev_priv, pll);
  9014. pll->on = false;
  9015. }
  9016. if (force_restore) {
  9017. i915_redisable_vga(dev);
  9018. /*
  9019. * We need to use raw interfaces for restoring state to avoid
  9020. * checking (bogus) intermediate states.
  9021. */
  9022. for_each_pipe(pipe) {
  9023. struct drm_crtc *crtc =
  9024. dev_priv->pipe_to_crtc_mapping[pipe];
  9025. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  9026. crtc->fb);
  9027. }
  9028. } else {
  9029. intel_modeset_update_staged_output_state(dev);
  9030. }
  9031. intel_modeset_check_state(dev);
  9032. drm_mode_config_reset(dev);
  9033. }
  9034. void intel_modeset_gem_init(struct drm_device *dev)
  9035. {
  9036. intel_modeset_init_hw(dev);
  9037. intel_setup_overlay(dev);
  9038. intel_modeset_setup_hw_state(dev, false);
  9039. }
  9040. void intel_modeset_cleanup(struct drm_device *dev)
  9041. {
  9042. struct drm_i915_private *dev_priv = dev->dev_private;
  9043. struct drm_crtc *crtc;
  9044. struct drm_connector *connector;
  9045. /*
  9046. * Interrupts and polling as the first thing to avoid creating havoc.
  9047. * Too much stuff here (turning of rps, connectors, ...) would
  9048. * experience fancy races otherwise.
  9049. */
  9050. drm_irq_uninstall(dev);
  9051. cancel_work_sync(&dev_priv->hotplug_work);
  9052. /*
  9053. * Due to the hpd irq storm handling the hotplug work can re-arm the
  9054. * poll handlers. Hence disable polling after hpd handling is shut down.
  9055. */
  9056. drm_kms_helper_poll_fini(dev);
  9057. mutex_lock(&dev->struct_mutex);
  9058. intel_unregister_dsm_handler();
  9059. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  9060. /* Skip inactive CRTCs */
  9061. if (!crtc->fb)
  9062. continue;
  9063. intel_increase_pllclock(crtc);
  9064. }
  9065. intel_disable_fbc(dev);
  9066. i915_enable_vga_mem(dev);
  9067. intel_disable_gt_powersave(dev);
  9068. ironlake_teardown_rc6(dev);
  9069. mutex_unlock(&dev->struct_mutex);
  9070. /* flush any delayed tasks or pending work */
  9071. flush_scheduled_work();
  9072. /* destroy backlight, if any, before the connectors */
  9073. intel_panel_destroy_backlight(dev);
  9074. /* destroy the sysfs files before encoders/connectors */
  9075. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  9076. drm_sysfs_connector_remove(connector);
  9077. drm_mode_config_cleanup(dev);
  9078. intel_cleanup_overlay(dev);
  9079. }
  9080. /*
  9081. * Return which encoder is currently attached for connector.
  9082. */
  9083. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  9084. {
  9085. return &intel_attached_encoder(connector)->base;
  9086. }
  9087. void intel_connector_attach_encoder(struct intel_connector *connector,
  9088. struct intel_encoder *encoder)
  9089. {
  9090. connector->encoder = encoder;
  9091. drm_mode_connector_attach_encoder(&connector->base,
  9092. &encoder->base);
  9093. }
  9094. /*
  9095. * set vga decode state - true == enable VGA decode
  9096. */
  9097. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  9098. {
  9099. struct drm_i915_private *dev_priv = dev->dev_private;
  9100. u16 gmch_ctrl;
  9101. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  9102. if (state)
  9103. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  9104. else
  9105. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  9106. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  9107. return 0;
  9108. }
  9109. struct intel_display_error_state {
  9110. u32 power_well_driver;
  9111. int num_transcoders;
  9112. struct intel_cursor_error_state {
  9113. u32 control;
  9114. u32 position;
  9115. u32 base;
  9116. u32 size;
  9117. } cursor[I915_MAX_PIPES];
  9118. struct intel_pipe_error_state {
  9119. u32 source;
  9120. } pipe[I915_MAX_PIPES];
  9121. struct intel_plane_error_state {
  9122. u32 control;
  9123. u32 stride;
  9124. u32 size;
  9125. u32 pos;
  9126. u32 addr;
  9127. u32 surface;
  9128. u32 tile_offset;
  9129. } plane[I915_MAX_PIPES];
  9130. struct intel_transcoder_error_state {
  9131. enum transcoder cpu_transcoder;
  9132. u32 conf;
  9133. u32 htotal;
  9134. u32 hblank;
  9135. u32 hsync;
  9136. u32 vtotal;
  9137. u32 vblank;
  9138. u32 vsync;
  9139. } transcoder[4];
  9140. };
  9141. struct intel_display_error_state *
  9142. intel_display_capture_error_state(struct drm_device *dev)
  9143. {
  9144. drm_i915_private_t *dev_priv = dev->dev_private;
  9145. struct intel_display_error_state *error;
  9146. int transcoders[] = {
  9147. TRANSCODER_A,
  9148. TRANSCODER_B,
  9149. TRANSCODER_C,
  9150. TRANSCODER_EDP,
  9151. };
  9152. int i;
  9153. if (INTEL_INFO(dev)->num_pipes == 0)
  9154. return NULL;
  9155. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  9156. if (error == NULL)
  9157. return NULL;
  9158. if (HAS_POWER_WELL(dev))
  9159. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  9160. for_each_pipe(i) {
  9161. if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
  9162. error->cursor[i].control = I915_READ(CURCNTR(i));
  9163. error->cursor[i].position = I915_READ(CURPOS(i));
  9164. error->cursor[i].base = I915_READ(CURBASE(i));
  9165. } else {
  9166. error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
  9167. error->cursor[i].position = I915_READ(CURPOS_IVB(i));
  9168. error->cursor[i].base = I915_READ(CURBASE_IVB(i));
  9169. }
  9170. error->plane[i].control = I915_READ(DSPCNTR(i));
  9171. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  9172. if (INTEL_INFO(dev)->gen <= 3) {
  9173. error->plane[i].size = I915_READ(DSPSIZE(i));
  9174. error->plane[i].pos = I915_READ(DSPPOS(i));
  9175. }
  9176. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  9177. error->plane[i].addr = I915_READ(DSPADDR(i));
  9178. if (INTEL_INFO(dev)->gen >= 4) {
  9179. error->plane[i].surface = I915_READ(DSPSURF(i));
  9180. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  9181. }
  9182. error->pipe[i].source = I915_READ(PIPESRC(i));
  9183. }
  9184. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  9185. if (HAS_DDI(dev_priv->dev))
  9186. error->num_transcoders++; /* Account for eDP. */
  9187. for (i = 0; i < error->num_transcoders; i++) {
  9188. enum transcoder cpu_transcoder = transcoders[i];
  9189. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  9190. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  9191. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  9192. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  9193. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  9194. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  9195. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  9196. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  9197. }
  9198. /* In the code above we read the registers without checking if the power
  9199. * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
  9200. * prevent the next I915_WRITE from detecting it and printing an error
  9201. * message. */
  9202. intel_uncore_clear_errors(dev);
  9203. return error;
  9204. }
  9205. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  9206. void
  9207. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  9208. struct drm_device *dev,
  9209. struct intel_display_error_state *error)
  9210. {
  9211. int i;
  9212. if (!error)
  9213. return;
  9214. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  9215. if (HAS_POWER_WELL(dev))
  9216. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  9217. error->power_well_driver);
  9218. for_each_pipe(i) {
  9219. err_printf(m, "Pipe [%d]:\n", i);
  9220. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  9221. err_printf(m, "Plane [%d]:\n", i);
  9222. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  9223. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  9224. if (INTEL_INFO(dev)->gen <= 3) {
  9225. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  9226. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  9227. }
  9228. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  9229. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  9230. if (INTEL_INFO(dev)->gen >= 4) {
  9231. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  9232. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  9233. }
  9234. err_printf(m, "Cursor [%d]:\n", i);
  9235. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  9236. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  9237. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  9238. }
  9239. for (i = 0; i < error->num_transcoders; i++) {
  9240. err_printf(m, " CPU transcoder: %c\n",
  9241. transcoder_name(error->transcoder[i].cpu_transcoder));
  9242. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  9243. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  9244. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  9245. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  9246. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  9247. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  9248. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  9249. }
  9250. }