intel_display.c 258 KB

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